-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathCosmosObject.cs
More file actions
322 lines (250 loc) · 11.6 KB
/
CosmosObject.cs
File metadata and controls
322 lines (250 loc) · 11.6 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.CosmosElements
{
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Query.Core.Monads;
using Microsoft.Azure.Cosmos.Query.Core.Pipeline.Distinct;
#if INTERNAL
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
#pragma warning disable SA1600 // Elements should be documented
#pragma warning disable SA1601 // Partial elements should be documented
public
#else
internal
#endif
abstract partial class CosmosObject : CosmosElement, IReadOnlyDictionary<string, CosmosElement>, IEquatable<CosmosObject>, IComparable<CosmosObject>
{
private const uint HashSeed = 1275696788;
private const uint NameHashSeed = 263659187;
protected CosmosObject()
: base()
{
}
public abstract KeyCollection Keys { get; }
IEnumerable<string> IReadOnlyDictionary<string, CosmosElement>.Keys => this.Keys;
public abstract ValueCollection Values { get; }
IEnumerable<CosmosElement> IReadOnlyDictionary<string, CosmosElement>.Values => this.Values;
public abstract int Count { get; }
public abstract CosmosElement this[string key] { get; }
public abstract bool ContainsKey(string key);
public abstract bool TryGetValue(string key, out CosmosElement value);
public override void Accept(ICosmosElementVisitor cosmosElementVisitor)
{
cosmosElementVisitor.Visit(this);
}
public override TResult Accept<TResult>(ICosmosElementVisitor<TResult> cosmosElementVisitor)
{
return cosmosElementVisitor.Visit(this);
}
public override TResult Accept<TArg, TResult>(ICosmosElementVisitor<TArg, TResult> cosmosElementVisitor, TArg input)
{
return cosmosElementVisitor.Visit(this, input);
}
public bool TryGetValue<TCosmosElement>(string key, out TCosmosElement typedCosmosElement)
where TCosmosElement : CosmosElement
{
if (!this.TryGetValue(key, out CosmosElement cosmosElement))
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
typedCosmosElement = default;
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
return false;
}
if (!(cosmosElement is TCosmosElement tCosmosElement))
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
typedCosmosElement = default;
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
return false;
}
typedCosmosElement = tCosmosElement;
return true;
}
public abstract Enumerator GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
IEnumerator<KeyValuePair<string, CosmosElement>> IEnumerable<KeyValuePair<string, CosmosElement>>.GetEnumerator() => this.GetEnumerator();
public override bool Equals(CosmosElement cosmosElement)
{
return cosmosElement is CosmosObject cosmosObject && this.Equals(cosmosObject);
}
public bool Equals(CosmosObject cosmosObject)
{
// Guard against stack exhaustion on deeply-nested input. Converts an
// otherwise-uncatchable StackOverflowException into a catchable
// InsufficientExecutionStackException.
RuntimeHelpers.EnsureSufficientExecutionStack();
if (this.Count != cosmosObject.Count)
{
return false;
}
// Order of properties does not mattter
foreach (KeyValuePair<string, CosmosElement> kvp in this)
{
string propertyName = kvp.Key;
CosmosElement propertyValue = kvp.Value;
if (!cosmosObject.TryGetValue(propertyName, out CosmosElement otherPropertyValue))
{
return false;
}
if (propertyValue != otherPropertyValue)
{
return false;
}
}
return true;
}
public override int GetHashCode()
{
// Guard against stack exhaustion on deeply-nested input. Converts an
// otherwise-uncatchable StackOverflowException into a catchable
// InsufficientExecutionStackException.
RuntimeHelpers.EnsureSufficientExecutionStack();
uint hash = HashSeed;
foreach (KeyValuePair<string, CosmosElement> kvp in this)
{
uint nameHash = MurmurHash3.Hash32(kvp.Key, NameHashSeed);
uint valueHash = MurmurHash3.Hash32(kvp.Value.GetHashCode(), nameHash);
//// xor is symmetric meaning that a ^ b = b ^ a
//// Which is great since now we can add the property hashes to the intermediate hash
//// in any order and get the same result, which upholds our definition of equality.
//// Note that we don't have to worry about a ^ a = 0 = b ^ b for duplicate property values,
//// since the hash of property values are seeded with the hash of property names,
//// which are unique within an object.
hash ^= valueHash;
}
return (int)hash;
}
public int CompareTo(CosmosObject cosmosObject)
{
UInt128 hash1 = DistinctHash.GetHash(this);
UInt128 hash2 = DistinctHash.GetHash(cosmosObject);
return UInt128BinaryComparer.Singleton.Compare(hash1, hash2);
}
public static CosmosObject Create(
IJsonNavigator jsonNavigator,
IJsonNavigatorNode jsonNavigatorNode)
{
return new LazyCosmosObject(jsonNavigator, jsonNavigatorNode);
}
public static CosmosObject Create(IReadOnlyDictionary<string, CosmosElement> dictionary)
{
return new EagerCosmosObject(dictionary);
}
public static new CosmosObject CreateFromBuffer(ReadOnlyMemory<byte> buffer)
{
return CosmosElement.CreateFromBuffer<CosmosObject>(buffer);
}
public static new CosmosObject Parse(string json)
{
return CosmosElement.Parse<CosmosObject>(json);
}
public static bool TryCreateFromBuffer(
ReadOnlyMemory<byte> buffer,
out CosmosObject cosmosObject)
{
return CosmosElement.TryCreateFromBuffer<CosmosObject>(buffer, out cosmosObject);
}
public static bool TryParse(
string json,
out CosmosObject cosmosObject)
{
return CosmosElement.TryParse<CosmosObject>(json, out cosmosObject);
}
public static new class Monadic
{
public static TryCatch<CosmosObject> CreateFromBuffer(ReadOnlyMemory<byte> buffer)
{
return CosmosElement.Monadic.CreateFromBuffer<CosmosObject>(buffer);
}
public static TryCatch<CosmosObject> Parse(string json)
{
return CosmosElement.Monadic.Parse<CosmosObject>(json);
}
}
public struct Enumerator : IEnumerator<KeyValuePair<string, CosmosElement>>
{
private Dictionary<string, CosmosElement>.Enumerator innerEnumerator;
internal Enumerator(Dictionary<string, CosmosElement>.Enumerator innerEnumerator)
{
this.innerEnumerator = innerEnumerator;
}
public KeyValuePair<string, CosmosElement> Current => this.innerEnumerator.Current;
object IEnumerator.Current => this.innerEnumerator.Current;
public void Dispose() => this.innerEnumerator.Dispose();
public bool MoveNext() => this.innerEnumerator.MoveNext();
public void Reset()
{
throw new NotImplementedException();
}
}
public struct KeyCollection : IEnumerable<string>
{
private Dictionary<string, CosmosElement>.KeyCollection innerCollection;
internal KeyCollection(Dictionary<string, CosmosElement>.KeyCollection innerCollection)
{
this.innerCollection = innerCollection;
}
public Enumerator GetEnumerator() => new Enumerator(this.innerCollection.GetEnumerator());
IEnumerator<string> IEnumerable<string>.GetEnumerator() => this.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
public struct Enumerator : IEnumerator<string>
{
private Dictionary<string, CosmosElement>.KeyCollection.Enumerator innerEnumerator;
internal Enumerator(Dictionary<string, CosmosElement>.KeyCollection.Enumerator innerEnumerator)
{
this.innerEnumerator = innerEnumerator;
}
public string Current => this.innerEnumerator.Current;
object IEnumerator.Current => this.innerEnumerator.Current;
public void Dispose() => this.innerEnumerator.Dispose();
public bool MoveNext()
{
return this.innerEnumerator.MoveNext();
}
public void Reset()
{
throw new NotImplementedException();
}
}
}
public struct ValueCollection : IEnumerable<CosmosElement>
{
private Dictionary<string, CosmosElement>.ValueCollection innerCollection;
internal ValueCollection(Dictionary<string, CosmosElement>.ValueCollection innerCollection)
{
this.innerCollection = innerCollection;
}
public Enumerator GetEnumerator() => new Enumerator(this.innerCollection.GetEnumerator());
IEnumerator<CosmosElement> IEnumerable<CosmosElement>.GetEnumerator() => this.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
public struct Enumerator : IEnumerator<CosmosElement>
{
private Dictionary<string, CosmosElement>.ValueCollection.Enumerator innerEnumerator;
internal Enumerator(Dictionary<string, CosmosElement>.ValueCollection.Enumerator innerEnumerator)
{
this.innerEnumerator = innerEnumerator;
}
public CosmosElement Current => this.innerEnumerator.Current;
object IEnumerator.Current => this.innerEnumerator.Current;
public void Dispose() => this.innerEnumerator.Dispose();
public bool MoveNext() => this.innerEnumerator.MoveNext();
public void Reset()
{
throw new NotImplementedException();
}
}
}
}
#if INTERNAL
#pragma warning restore SA1601 // Partial elements should be documented
#pragma warning restore SA1600 // Elements should be documented
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
#endif
}