-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathCompositeContinuationToken.cs
129 lines (110 loc) · 5.09 KB
/
CompositeContinuationToken.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Query.Core.ContinuationTokens
{
using System.Collections.Generic;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Query.Core.Exceptions;
using Microsoft.Azure.Cosmos.Query.Core.Monads;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Documents.Routing;
using Newtonsoft.Json;
/// <summary>
/// A composite continuation token that has both backend continuation token and partition range information.
/// </summary>
internal sealed class CompositeContinuationToken : IPartitionedToken
{
private static class PropertyNames
{
public const string Token = "token";
public const string Range = "range";
public const string Min = "min";
public const string Max = "max";
}
[JsonProperty(PropertyNames.Token)]
public string Token
{
get;
set;
}
[JsonProperty(PropertyNames.Range)]
[JsonConverter(typeof(RangeJsonConverter))]
public Documents.Routing.Range<string> Range
{
get;
set;
}
[JsonIgnore]
public Range<string> PartitionRange => this.Range;
public object ShallowCopy()
{
return this.MemberwiseClone();
}
public static CosmosElement ToCosmosElement(CompositeContinuationToken compositeContinuationToken)
{
CosmosElement token = compositeContinuationToken.Token == null ? (CosmosElement)CosmosNull.Create() : (CosmosElement)CosmosString.Create(compositeContinuationToken.Token);
return CosmosObject.Create(
new Dictionary<string, CosmosElement>()
{
{ CompositeContinuationToken.PropertyNames.Token, token },
{
CompositeContinuationToken.PropertyNames.Range,
CosmosObject.Create(
new Dictionary<string, CosmosElement>()
{
{ PropertyNames.Min, CosmosString.Create(compositeContinuationToken.Range.Min) },
{ PropertyNames.Max, CosmosString.Create(compositeContinuationToken.Range.Max) }
})
},
});
}
public static TryCatch<CompositeContinuationToken> TryCreateFromCosmosElement(CosmosElement cosmosElement)
{
if (!(cosmosElement is CosmosObject cosmosObject))
{
return TryCatch<CompositeContinuationToken>.FromException(
new MalformedContinuationTokenException($"{nameof(CompositeContinuationToken)} is not an object: {cosmosElement}"));
}
if (!cosmosObject.TryGetValue(PropertyNames.Token, out CosmosElement rawToken))
{
return TryCatch<CompositeContinuationToken>.FromException(
new MalformedContinuationTokenException($"{nameof(CompositeContinuationToken)} is missing field: '{PropertyNames.Token}': {cosmosElement}"));
}
string token;
if (rawToken is CosmosString rawTokenString)
{
token = rawTokenString.Value;
}
else
{
token = null;
}
if (!cosmosObject.TryGetValue(PropertyNames.Range, out CosmosObject rawRange))
{
return TryCatch<CompositeContinuationToken>.FromException(
new MalformedContinuationTokenException($"{nameof(CompositeContinuationToken)} is missing field: '{PropertyNames.Range}': {cosmosElement}"));
}
if (!rawRange.TryGetValue(PropertyNames.Min, out CosmosString rawMin))
{
return TryCatch<CompositeContinuationToken>.FromException(
new MalformedContinuationTokenException($"{nameof(CompositeContinuationToken)} is missing field: '{PropertyNames.Min}': {cosmosElement}"));
}
string min = rawMin.Value;
if (!rawRange.TryGetValue(PropertyNames.Max, out CosmosString rawMax))
{
return TryCatch<CompositeContinuationToken>.FromException(
new MalformedContinuationTokenException($"{nameof(CompositeContinuationToken)} is missing field: '{PropertyNames.Max}': {cosmosElement}"));
}
string max = rawMax.Value;
Documents.Routing.Range<string> range = new Documents.Routing.Range<string>(min, max, true, false);
CompositeContinuationToken compositeContinuationToken = new CompositeContinuationToken()
{
Token = token,
Range = range,
};
return TryCatch<CompositeContinuationToken>.FromResult(compositeContinuationToken);
}
}
}