-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathIndexingPolicy.cs
More file actions
596 lines (506 loc) · 23.8 KB
/
IndexingPolicy.cs
File metadata and controls
596 lines (506 loc) · 23.8 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Azure.Documents;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
/// <summary>
/// Represents the indexing policy configuration for a collection in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// Indexing policies can used to configure which properties (JSON paths) are included/excluded, whether the index is updated consistently
/// or offline (lazy), automatic vs. opt-in per-document, as well as the precision and type of index per path.
/// <para>
/// Refer to https://docs.microsoft.com/azure/cosmos-db/index-policy for additional information on how to specify
/// indexing policies.
/// </para>
/// </remarks>
/// <seealso cref="ContainerProperties"/>
public sealed class IndexingPolicy
{
internal const string DefaultPath = "/*";
/// <summary>
/// Initializes a new instance of the <see cref="IndexingPolicy"/> class for the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// Indexing mode is set to consistent.
/// </remarks>
public IndexingPolicy()
{
this.Automatic = true;
this.IndexingMode = IndexingMode.Consistent;
}
/// <summary>
/// Gets or sets a value that indicates whether automatic indexing is enabled for a collection in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// In automatic indexing, documents can be explicitly excluded from indexing using <see cref="T:Microsoft.Azure.Documents.Client.RequestOptions"/>.
/// In manual indexing, documents can be explicitly included.
/// </remarks>
/// <value>
/// True, if automatic indexing is enabled; otherwise, false.
/// </value>
[JsonProperty(PropertyName = Constants.Properties.Automatic)]
public bool Automatic { get; set; }
/// <summary>
/// Gets or sets the indexing mode (consistent or lazy) in the Azure Cosmos DB service.
/// </summary>
/// <value>
/// One of the values of the <see cref="T:Microsoft.Azure.Cosmos.IndexingMode"/> enumeration.
/// </value>
[JsonProperty(PropertyName = Constants.Properties.IndexingMode)]
[JsonConverter(typeof(StringEnumConverter))]
public IndexingMode IndexingMode { get; set; }
/// <summary>
/// Gets the collection containing <see cref="IncludedPath"/> objects in the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The collection containing <see cref="IncludedPath"/> objects.
/// </value>
[JsonProperty(PropertyName = Constants.Properties.IncludedPaths)]
public Collection<IncludedPath> IncludedPaths { get; internal set; } = new Collection<IncludedPath>();
/// <summary>
/// Gets the collection containing <see cref="ExcludedPath"/> objects in the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The collection containing <see cref="ExcludedPath"/> objects.
/// </value>
[JsonProperty(PropertyName = Constants.Properties.ExcludedPaths)]
public Collection<ExcludedPath> ExcludedPaths { get; internal set; } = new Collection<ExcludedPath>();
/// <summary>
/// Gets the composite indexes for additional indexes
/// </summary>
/// <example>
/// <![CDATA[
/// "composite": [
/// [
/// {
/// "path": "/joining_year",
/// "order": "ascending"
/// },
/// {
/// "path": "/level",
/// "order": "descending"
/// }
/// ],
/// [
/// {
/// "path": "/country"
/// },
/// {
/// "path": "/city"
/// }
/// ]
/// ]
/// ]]>
/// </example>
[JsonProperty(PropertyName = Constants.Properties.CompositeIndexes)]
public Collection<Collection<CompositePath>> CompositeIndexes { get; internal set; } = new Collection<Collection<CompositePath>>();
/// <summary>
/// Collection of spatial index definitions to be used
/// </summary>
[JsonProperty(PropertyName = Constants.Properties.SpatialIndexes)]
public Collection<SpatialPath> SpatialIndexes { get; internal set; } = new Collection<SpatialPath>();
/// <summary>
/// Gets the vector indexes for additional indexes
/// </summary>
/// <example>
/// <![CDATA[
/// "vectorIndexes": [
/// {
/// "path": "/vector1",
/// "type": "diskANN"
/// },
/// {
/// "path": "/vector2",
/// "type": "flat "
/// },
/// {
/// "path": "/vector3",
/// "type": "quantizedFlat"
/// }
/// ]
/// ]]>
/// </example>
[JsonProperty(PropertyName = "vectorIndexes", NullValueHandling = NullValueHandling.Ignore)]
public Collection<VectorIndexPath> VectorIndexes { get; set; } = new Collection<VectorIndexPath>();
/// <summary>
/// Gets the full text indexes
/// </summary>
/// <example>
/// <![CDATA[
/// "fullTextIndexes": [
/// {
/// "path": "/v1",
/// },
/// {
/// "path": "/v2",
/// },
/// {
/// "path": "/v3",
/// }
/// ]
/// ]]>
/// </example>
[JsonProperty(PropertyName = "fullTextIndexes", NullValueHandling = NullValueHandling.Ignore)]
public Collection<FullTextIndexPath> FullTextIndexes{ get; set; } = new Collection<FullTextIndexPath>();
/// <summary>
/// This contains additional values for scenarios where the SDK is not aware of new fields.
/// This ensures that if resource is read and updated none of the fields will be lost in the process.
/// </summary>
[JsonExtensionData]
internal IDictionary<string, JToken> AdditionalProperties { get; private set; }
#if INTERNAL
/// <summary>
/// Indexing policy annotation.
/// </summary>
[JsonProperty(PropertyName = "annotation", NullValueHandling = NullValueHandling.Ignore)]
public string Annotation { get; set; }
#endif
#region EqualityComparers
internal sealed class CompositePathEqualityComparer : IEqualityComparer<CompositePath>
{
public static readonly CompositePathEqualityComparer Singleton = new CompositePathEqualityComparer();
public bool Equals(CompositePath compositePath1, CompositePath compositePath2)
{
if (Object.ReferenceEquals(compositePath1, compositePath2))
{
return true;
}
if (compositePath1 == null || compositePath2 == null)
{
return false;
}
if (compositePath1.Path == compositePath2.Path &&
compositePath1.Order == compositePath2.Order &&
compositePath1.AdditionalProperties.EqualsTo(compositePath2.AdditionalProperties))
{
return true;
}
return false;
}
public int GetHashCode(CompositePath compositePath)
{
if (compositePath == null)
{
return 0;
}
return compositePath.Path.GetHashCode() ^ compositePath.Order.GetHashCode();
}
}
internal sealed class CompositePathsEqualityComparer : IEqualityComparer<HashSet<CompositePath>>
{
public static readonly CompositePathsEqualityComparer Singleton = new CompositePathsEqualityComparer();
private static readonly CompositePathEqualityComparer compositePathEqualityComparer = new CompositePathEqualityComparer();
public bool Equals(HashSet<CompositePath> compositePaths1, HashSet<CompositePath> compositePaths2)
{
if (Object.ReferenceEquals(compositePaths1, compositePaths2))
{
return true;
}
if (compositePaths1 == null || compositePaths2 == null)
{
return false;
}
return compositePaths1.SetEquals(compositePaths2);
}
public int GetHashCode(HashSet<CompositePath> obj)
{
if (obj == null)
{
return 0;
}
int hashCode = 0;
foreach (CompositePath compositePath in obj)
{
hashCode ^= compositePathEqualityComparer.GetHashCode(compositePath);
}
return hashCode;
}
}
private sealed class CompositeIndexesEqualityComparer : IEqualityComparer<Collection<Collection<CompositePath>>>
{
private static readonly CompositePathEqualityComparer compositePathEqualityComparer = new CompositePathEqualityComparer();
private static readonly CompositePathsEqualityComparer compositePathsEqualityComparer = new CompositePathsEqualityComparer();
public bool Equals(Collection<Collection<CompositePath>> compositeIndexes1, Collection<Collection<CompositePath>> compositeIndexes2)
{
if (Object.ReferenceEquals(compositeIndexes1, compositeIndexes2))
{
return true;
}
if (compositeIndexes1 == null || compositeIndexes2 == null)
{
return false;
}
HashSet<HashSet<CompositePath>> hashedCompositeIndexes1 = new HashSet<HashSet<CompositePath>>(compositePathsEqualityComparer);
HashSet<HashSet<CompositePath>> hashedCompositeIndexes2 = new HashSet<HashSet<CompositePath>>(compositePathsEqualityComparer);
foreach (Collection<CompositePath> compositePaths in compositeIndexes1)
{
HashSet<CompositePath> hashedCompositePaths = new HashSet<CompositePath>(compositePaths, compositePathEqualityComparer);
hashedCompositeIndexes1.Add(hashedCompositePaths);
}
foreach (Collection<CompositePath> compositePaths in compositeIndexes2)
{
HashSet<CompositePath> hashedCompositePaths = new HashSet<CompositePath>(compositePaths, compositePathEqualityComparer);
hashedCompositeIndexes2.Add(hashedCompositePaths);
}
return hashedCompositeIndexes1.SetEquals(hashedCompositeIndexes2);
}
public int GetHashCode(Collection<Collection<CompositePath>> compositeIndexes)
{
int hashCode = 0;
foreach (Collection<CompositePath> compositePaths in compositeIndexes)
{
HashSet<CompositePath> hashedCompositePaths = new HashSet<CompositePath>(compositePaths, compositePathEqualityComparer);
hashCode ^= compositePathsEqualityComparer.GetHashCode(hashedCompositePaths);
}
return hashCode;
}
}
internal sealed class SpatialSpecEqualityComparer : IEqualityComparer<SpatialPath>
{
public static readonly SpatialSpecEqualityComparer Singleton = new SpatialSpecEqualityComparer();
public bool Equals(SpatialPath spatialSpec1, SpatialPath spatialSpec2)
{
if (object.ReferenceEquals(spatialSpec1, spatialSpec2))
{
return true;
}
if (spatialSpec1 == null || spatialSpec2 == null)
{
return false;
}
if (spatialSpec1.Path != spatialSpec2.Path)
{
return false;
}
HashSet<SpatialType> hashedSpatialTypes1 = new HashSet<SpatialType>(spatialSpec1.SpatialTypes);
HashSet<SpatialType> hashedSpatialTypes2 = new HashSet<SpatialType>(spatialSpec2.SpatialTypes);
if (!hashedSpatialTypes1.SetEquals(hashedSpatialTypes2) ||
!spatialSpec1.AdditionalProperties.EqualsTo(spatialSpec2.AdditionalProperties))
{
return false;
}
return true;
}
public int GetHashCode(SpatialPath spatialSpec)
{
int hashCode = 0;
hashCode ^= spatialSpec.Path.GetHashCode();
foreach (SpatialType spatialType in spatialSpec.SpatialTypes)
{
hashCode ^= spatialType.GetHashCode();
}
return hashCode;
}
}
internal sealed class AdditionalSpatialIndexesEqualityComparer : IEqualityComparer<Collection<SpatialPath>>
{
private static readonly SpatialSpecEqualityComparer spatialSpecEqualityComparer = new SpatialSpecEqualityComparer();
public bool Equals(Collection<SpatialPath> additionalSpatialIndexes1, Collection<SpatialPath> additionalSpatialIndexes2)
{
if (object.ReferenceEquals(additionalSpatialIndexes1, additionalSpatialIndexes2))
{
return true;
}
if (additionalSpatialIndexes1 == null || additionalSpatialIndexes2 == null)
{
return false;
}
HashSet<SpatialPath> hashedAdditionalSpatialIndexes1 = new HashSet<SpatialPath>(additionalSpatialIndexes1, spatialSpecEqualityComparer);
HashSet<SpatialPath> hashedAdditionalSpatialIndexes2 = new HashSet<SpatialPath>(additionalSpatialIndexes2, spatialSpecEqualityComparer);
return hashedAdditionalSpatialIndexes1.SetEquals(hashedAdditionalSpatialIndexes2);
}
public int GetHashCode(Collection<SpatialPath> additionalSpatialIndexes)
{
int hashCode = 0;
foreach (SpatialPath spatialSpec in additionalSpatialIndexes)
{
hashCode ^= spatialSpecEqualityComparer.GetHashCode(spatialSpec);
}
return hashCode;
}
}
internal sealed class IndexEqualityComparer : IEqualityComparer<Cosmos.Index>
{
public static readonly IndexEqualityComparer Comparer = new IndexEqualityComparer();
public bool Equals(Index index1, Index index2)
{
if (object.ReferenceEquals(index1, index2))
{
return true;
}
if (index1 == null || index2 == null)
{
return false;
}
if (index1.Kind != index2.Kind || !index1.AdditionalProperties.EqualsTo(index2.AdditionalProperties))
{
return false;
}
switch (index1.Kind)
{
case IndexKind.Hash:
if (((HashIndex)index1).Precision != ((HashIndex)index2).Precision)
{
return false;
}
if (((HashIndex)index1).DataType != ((HashIndex)index2).DataType)
{
return false;
}
break;
case IndexKind.Range:
if (((RangeIndex)index1).Precision != ((RangeIndex)index2).Precision)
{
return false;
}
if (((RangeIndex)index1).DataType != ((RangeIndex)index2).DataType)
{
return false;
}
break;
case IndexKind.Spatial:
if (((SpatialIndex)index1).DataType != ((SpatialIndex)index2).DataType)
{
return false;
}
break;
default:
throw new ArgumentException($"Unexpected Kind: {index1.Kind}");
}
return true;
}
public int GetHashCode(Index index)
{
int hashCode = 0;
hashCode ^= (int)index.Kind;
switch (index.Kind)
{
case IndexKind.Hash:
hashCode = hashCode ^ ((HashIndex)index).Precision ?? 0;
hashCode ^= ((HashIndex)index).DataType.GetHashCode();
break;
case IndexKind.Range:
hashCode = hashCode ^ ((RangeIndex)index).Precision ?? 0;
hashCode ^= ((RangeIndex)index).DataType.GetHashCode();
break;
case IndexKind.Spatial:
hashCode ^= ((SpatialIndex)index).DataType.GetHashCode();
break;
default:
throw new ArgumentException($"Unexpected Kind: {index.Kind}");
}
return hashCode;
}
}
internal sealed class IncludedPathEqualityComparer : IEqualityComparer<IncludedPath>
{
public static readonly IncludedPathEqualityComparer Singleton = new IncludedPathEqualityComparer();
private static readonly IndexEqualityComparer indexEqualityComparer = new IndexEqualityComparer();
public bool Equals(IncludedPath includedPath1, IncludedPath includedPath2)
{
if (Object.ReferenceEquals(includedPath1, includedPath2))
{
return true;
}
if (includedPath1 == null || includedPath2 == null)
{
return false;
}
if (includedPath1.Path != includedPath2.Path ||
!includedPath1.AdditionalProperties.EqualsTo(includedPath2.AdditionalProperties))
{
return false;
}
HashSet<Cosmos.Index> indexes1 = new HashSet<Cosmos.Index>(includedPath1.Indexes, indexEqualityComparer);
HashSet<Cosmos.Index> indexes2 = new HashSet<Cosmos.Index>(includedPath2.Indexes, indexEqualityComparer);
return indexes1.SetEquals(indexes2);
}
public int GetHashCode(IncludedPath includedPath)
{
int hashCode = 0;
hashCode ^= includedPath.Path.GetHashCode();
foreach (Index index in includedPath.Indexes)
{
hashCode ^= indexEqualityComparer.GetHashCode(index);
}
return hashCode;
}
}
internal sealed class ExcludedPathEqualityComparer : IEqualityComparer<ExcludedPath>
{
public static readonly ExcludedPathEqualityComparer Singleton = new ExcludedPathEqualityComparer();
public bool Equals(ExcludedPath excludedPath1, ExcludedPath excludedPath2)
{
if (Object.ReferenceEquals(excludedPath1, excludedPath2))
{
return true;
}
if (excludedPath1 == null || excludedPath2 == null)
{
return false;
}
return excludedPath1.Path == excludedPath2.Path &&
excludedPath1.AdditionalProperties.EqualsTo(excludedPath2.AdditionalProperties);
}
public int GetHashCode(ExcludedPath excludedPath1)
{
return excludedPath1.Path.GetHashCode();
}
}
internal sealed class IndexingPolicyEqualityComparer : IEqualityComparer<IndexingPolicy>
{
public static readonly IndexingPolicyEqualityComparer Singleton = new IndexingPolicyEqualityComparer();
private static readonly IncludedPathEqualityComparer includedPathEqualityComparer = new IncludedPathEqualityComparer();
private static readonly ExcludedPathEqualityComparer excludedPathEqualityComparer = new ExcludedPathEqualityComparer();
private static readonly CompositeIndexesEqualityComparer compositeIndexesEqualityComparer = new CompositeIndexesEqualityComparer();
private static readonly AdditionalSpatialIndexesEqualityComparer additionalSpatialIndexesEqualityComparer = new AdditionalSpatialIndexesEqualityComparer();
public bool Equals(IndexingPolicy indexingPolicy1, IndexingPolicy indexingPolicy2)
{
if (Object.ReferenceEquals(indexingPolicy1, indexingPolicy2))
{
return true;
}
bool isEqual = true;
isEqual &= indexingPolicy1 != null && indexingPolicy2 != null;
isEqual &= indexingPolicy1.Automatic == indexingPolicy2.Automatic;
isEqual &= indexingPolicy1.IndexingMode == indexingPolicy2.IndexingMode;
isEqual &= compositeIndexesEqualityComparer.Equals(indexingPolicy1.CompositeIndexes, indexingPolicy2.CompositeIndexes);
isEqual &= additionalSpatialIndexesEqualityComparer.Equals(indexingPolicy1.SpatialIndexes, indexingPolicy2.SpatialIndexes);
isEqual &= indexingPolicy1.AdditionalProperties.EqualsTo(indexingPolicy2.AdditionalProperties);
HashSet<IncludedPath> includedPaths1 = new HashSet<IncludedPath>(indexingPolicy1.IncludedPaths, includedPathEqualityComparer);
HashSet<IncludedPath> includedPaths2 = new HashSet<IncludedPath>(indexingPolicy2.IncludedPaths, includedPathEqualityComparer);
isEqual &= includedPaths1.SetEquals(includedPaths2);
HashSet<ExcludedPath> excludedPaths1 = new HashSet<ExcludedPath>(indexingPolicy1.ExcludedPaths, excludedPathEqualityComparer);
HashSet<ExcludedPath> excludedPaths2 = new HashSet<ExcludedPath>(indexingPolicy2.ExcludedPaths, excludedPathEqualityComparer);
isEqual &= excludedPaths1.SetEquals(excludedPaths2);
return isEqual;
}
public int GetHashCode(IndexingPolicy indexingPolicy)
{
int hashCode = 0;
hashCode ^= indexingPolicy.Automatic.GetHashCode();
hashCode ^= indexingPolicy.IndexingMode.GetHashCode();
hashCode ^= compositeIndexesEqualityComparer.GetHashCode(indexingPolicy.CompositeIndexes);
hashCode ^= additionalSpatialIndexesEqualityComparer.GetHashCode(indexingPolicy.SpatialIndexes);
foreach (IncludedPath includedPath in indexingPolicy.IncludedPaths)
{
hashCode ^= includedPathEqualityComparer.GetHashCode(includedPath);
}
foreach (ExcludedPath excludedPath in indexingPolicy.ExcludedPaths)
{
hashCode ^= excludedPathEqualityComparer.GetHashCode(excludedPath);
}
return hashCode;
}
}
#endregion
}
}