-
Notifications
You must be signed in to change notification settings - Fork 2k
/
IAzureQueueDataAdapter.cs
97 lines (87 loc) · 4.05 KB
/
IAzureQueueDataAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Providers.Streams.Common;
using Orleans.Runtime;
using Orleans.Serialization;
using Orleans.Streams;
namespace Orleans.Providers.Streams.AzureQueue
{
/// <summary>
/// Original data adapter. Here to maintain backwards compatibility, but does not support json and other custom serializers
/// </summary>
[SerializationCallbacks(typeof(OnDeserializedCallbacks))]
public class AzureQueueDataAdapterV1 : IQueueDataAdapter<string, IBatchContainer>, IOnDeserialized
{
private Serializer<AzureQueueBatchContainer> serializer;
/// <summary>
/// Initializes a new instance of the <see cref="AzureQueueDataAdapterV1"/> class.
/// </summary>
/// <param name="serializer"></param>
public AzureQueueDataAdapterV1(Serializer serializer)
{
this.serializer = serializer.GetSerializer<AzureQueueBatchContainer>();
}
/// <summary>
/// Creates a cloud queue message from stream event data.
/// </summary>
public string ToQueueMessage<T>(StreamId streamId, IEnumerable<T> events, StreamSequenceToken token, Dictionary<string, object> requestContext)
{
var azureQueueBatchMessage = new AzureQueueBatchContainer(streamId, events.Cast<object>().ToList(), requestContext);
var rawBytes = this.serializer.SerializeToArray(azureQueueBatchMessage);
return Convert.ToBase64String(rawBytes);
}
/// <summary>
/// Creates a batch container from a cloud queue message
/// </summary>
public IBatchContainer FromQueueMessage(string cloudMsg, long sequenceId)
{
var azureQueueBatch = this.serializer.Deserialize(Convert.FromBase64String(cloudMsg));
azureQueueBatch.RealSequenceToken = new EventSequenceToken(sequenceId);
return azureQueueBatch;
}
void IOnDeserialized.OnDeserialized(DeserializationContext context)
{
this.serializer = context.ServiceProvider.GetRequiredService<Serializer<AzureQueueBatchContainer>>();
}
}
/// <summary>
/// Data adapter that uses types that support custom serializers (like json).
/// </summary>
[SerializationCallbacks(typeof(OnDeserializedCallbacks))]
public class AzureQueueDataAdapterV2 : IQueueDataAdapter<string, IBatchContainer>, IOnDeserialized
{
private Serializer<AzureQueueBatchContainerV2> serializer;
/// <summary>
/// Initializes a new instance of the <see cref="AzureQueueDataAdapterV2"/> class.
/// </summary>
/// <param name="serializer"></param>
public AzureQueueDataAdapterV2(Serializer serializer)
{
this.serializer = serializer.GetSerializer<AzureQueueBatchContainerV2>();
}
/// <summary>
/// Creates a cloud queue message from stream event data.
/// </summary>
public string ToQueueMessage<T>(StreamId streamId, IEnumerable<T> events, StreamSequenceToken token, Dictionary<string, object> requestContext)
{
var azureQueueBatchMessage = new AzureQueueBatchContainerV2(streamId, events.Cast<object>().ToList(), requestContext);
var rawBytes = this.serializer.SerializeToArray(azureQueueBatchMessage);
return Convert.ToBase64String(rawBytes);
}
/// <summary>
/// Creates a batch container from a cloud queue message
/// </summary>
public IBatchContainer FromQueueMessage(string cloudMsg, long sequenceId)
{
var azureQueueBatch = this.serializer.Deserialize(Convert.FromBase64String(cloudMsg));
azureQueueBatch.RealSequenceToken = new EventSequenceTokenV2(sequenceId);
return azureQueueBatch;
}
void IOnDeserialized.OnDeserialized(DeserializationContext context)
{
this.serializer = context.ServiceProvider.GetRequiredService<Serializer<AzureQueueBatchContainerV2>>();
}
}
}