-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathASecondaryIndexAccess.cs
729 lines (653 loc) · 36.4 KB
/
ASecondaryIndexAccess.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
using Aerospike.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using LPU = LINQPad.Util;
namespace Aerospike.Database.LINQPadDriver.Extensions
{
public class ASecondaryIndexAccess<T> : ASecondaryIndexAccess,
IEnumerable<AQueryRecord<T>>
where T : ARecord
{
public ASecondaryIndexAccess([NotNull] SetRecords<T> setRecords,
[NotNull] string name,
[NotNull] string idxBin,
[NotNull] string idxType,
[NotNull] string idxCollectionType,
[NotNull] Type idxBinDataType)
: base(setRecords, name, idxBin, idxType, idxCollectionType)
{
this.SetRecords = setRecords;
this.BinDataType = idxBinDataType;
}
/// <summary>
/// The set instance associated with this index.
/// <see cref="SetRecords"/> for more information.
/// </summary>
new public SetRecords<T> SetRecords { get; }
public Type BinDataType { get; }
#region Query Methods
/// <summary>
/// Performs a secondary index query using the provided <see cref="Client.Filter"/>.
/// </summary>
/// <param name="secondaryIdxFilter">The <see cref="Client.Filter"/> used against the secondary index</param>
/// <param name="bins">Only include these bins in the result.</param>
/// <returns>
/// A collection of records that match the filter.
/// </returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <seealso cref="Query(Filter, Exp, string[])"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
/// <seealso cref="SetRecords.Query(Filter, string[])"/>
new public IEnumerable<T> Query([NotNull] Client.Filter secondaryIdxFilter, params string[] bins)
=> this.SetRecords.Query(secondaryIdxFilter, bins);
/// <summary>
/// Performs a secondary index query using the provided <see cref="Client.Filter"/> and than apply the filter expression.
/// </summary>
/// <param name="secondaryIdxFilter">The <see cref="Client.Filter"/> used against the secondary index</param>
/// <param name="filterExpression">The Aerospike filter <see cref="Client.Exp"/> that will be applied after the index filter is applied.</param>
/// <param name="bins">Only include these bins in the result.</param>
/// <returns>
/// A collection of records that match the filters.
/// </returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="SetRecords.Query(Filter, Exp, string[])"/>
new public IEnumerable<T> Query([NotNull] Client.Filter secondaryIdxFilter, [NotNull] Client.Exp filterExpression, params string[] bins)
=> this.SetRecords.Query(secondaryIdxFilter, filterExpression, bins);
/// <summary>
/// Performs a search on the secondary index based <paramref name="idxBinValue"/> and the properties associated with the index.
/// For more information see <see cref="Client.Filter"/>.
/// </summary>
/// <param name="idxBinValue">The searchValue used to conduct the search associated with bin</param>
/// <param name = "bins" > Only include these bins in the result.</param>
/// <returns>
/// A collection of records that match the filters.
/// </returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
/// <seealso cref="BinDataType"/>
/// <seealso cref="IndexCollectionType"/>
new public IEnumerable<T> Query([NotNull] dynamic idxBinValue, params string[] bins)
{
return idxBinValue switch
{
Client.Key keyValue => this.Query(GetFilter(keyValue.userKey), bins),
Client.Value vValue => this.Query(GetFilter(vValue), bins),
_ => this.Query(GetFilter(idxBinValue), bins)
};
}
/// <summary>
/// Performs a secondary index range search based on <paramref name="inclusiveStartRange"/> and <paramref name="inclusiveEndRange"/>, inclusively.
/// For more information see <see cref="Client.Filter"/>.
/// </summary>
/// <param name="inclusiveStartRange">Start Rage, inclusive</param>
/// <param name="inclusiveEndRange">End Range, inclusive</param>
/// <param name="bins">Only include these bins in the result.</param>
/// <returns>A collection of records that match the filters.</returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="inclusiveStartRange"/> is greater than <paramref name="inclusiveEndRange"/></exception>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="SetRecords.Query(Filter, Exp, string[])"/>
new public IEnumerable<T> Query(long inclusiveStartRange, long inclusiveEndRange, params string[] bins)
=> this.Query(GetFilter(inclusiveStartRange, inclusiveEndRange), bins);
#endregion
#region IEnumerable
/// <summary>
/// Returns IEnumerable><see cref="AQueryRecord"/>< based on <see cref="ASecondaryIndexAccess.DefaultFilter"/> and <paramref name="filterExpression"/>.
/// </summary>
/// <param name="filterExpression">A Filter <see cref="Client.Exp"/> used to obtain the collection of records.</param>
/// <param name="returningOnlyMatchingCT">
/// if true (default), only index values that match the collection type are returned.
/// If false values that don't match the collection type are returned but will be wrapped with "!|'<value>'|!"
/// </param>
/// <returns/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="Query(Filter, Exp, string[])"/>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="ASecondaryIndexAccess.DefaultFilter"/>
/// <seealso cref="ASecondaryIndexAccess.AsEnumerable(Exp, bool)"/>
public new IEnumerable<AQueryRecord<T>> AsEnumerable(Client.Exp filterExpression = null, bool returningOnlyMatchingCT = true)
{
var setRecs = this.DefaultFilter == null
? this.SetRecords.AsEnumerable(filterExpression)
: this.SetRecords.Query(secondaryIdxFilter: this.DefaultFilter, filterExpression: filterExpression);
return setRecs.SelectMany(r => GroupRecord.GetGrpKeys(r.Aerospike.GetValue(this.BinName),
this.CollectionType,
r,
returningOnlyMatchingCT))
.GroupBy(dnis => dnis.GrpkeyValue)
.Select(dnis => new AQueryRecord<T>(dnis.Key, dnis.Select(r => (T) r.Record)));
}
public new IEnumerator<AQueryRecord<T>> GetEnumerator()
{
foreach(var queryRec in this.AsEnumerable())
{
yield return queryRec;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
[DebuggerDisplay("{ToString()}")]
public class ASecondaryIndexAccess : IEquatable<ARecord>,
IEquatable<SetRecords>,
IEquatable<ASecondaryIndexAccess>,
IEnumerable<AQueryRecord>
{
public ASecondaryIndexAccess([NotNull] SetRecords setRecords,
[NotNull] string name,
[NotNull] string idxBin,
[NotNull] string idxType,
[NotNull] string idxCollectionType)
{
this.SetRecords = new SetRecords(setRecords);
this.Name = name;
this.FullName = $"{SetRecords.SetFullName}.{this.Name}";
this.BinName = idxBin;
this.BinType = string.IsNullOrEmpty(idxType)
? Client.IndexType.STRING
: Enum.Parse<Client.IndexType>(idxType, true);
this.CollectionType = string.IsNullOrEmpty(idxCollectionType)
? IndexCollectionType.DEFAULT
: Enum.Parse<Client.IndexCollectionType>(idxCollectionType, true);
}
/// <summary>
/// Sets how records are displayed using the LinqPad <see cref="LINQPad.Extensions.Dump{T}(T)"/> method.
/// See <see cref="ARecord.DumpTypes"/> for more information.
/// </summary>
/// <seealso cref="ARecord.DumpTypes"/>
/// <seealso cref="ChangeRecordView(ARecord.DumpTypes)"/>
public ARecord.DumpTypes DefaultRecordView
{
get => this.SetRecords.DefaultRecordView;
set
{
this.SetRecords.DefaultRecordView = value;
}
}
/// <summary>
/// Changes how records are displayed using the LinqPad <see cref="LINQPad.Extensions.Dump{T}(T)"/> method.
/// </summary>
/// <param name="newRecordView">See <see cref="ARecord.DumpTypes"/> for more information.</param>
/// <returns>This instance</returns>
/// <seealso cref="ARecord.DumpTypes"/>
/// <seealso cref="DefaultRecordView"/>
public ASecondaryIndexAccess ChangeRecordView(ARecord.DumpTypes newRecordView)
{
this.DefaultRecordView = newRecordView;
return this;
}
/// <summary>
/// The set instance associated with this index.
/// <see cref="SetRecords"/> for more information.
/// </summary>
public SetRecords SetRecords { get; }
/// <summary>
/// Sets the default filter for this index used to obtain the record set. It can be overridden by using the <see cref="Query(Filter, string[])"/> methods
/// </summary>
/// <seealso cref="SetFilter(Filter)"/>
/// <seealso cref="Query(Filter, Exp, string[])"/>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="AsEnumerable(Exp, bool)"/>
public Client.Filter DefaultFilter { get; set; } = null;
/// <summary>
/// Set the filter that will be used by this secondary index as the default filter. See <see cref="DefaultFilter"/>
/// </summary>
/// <param name="secondaryIdxFilter">Set's the default filter. See <see cref="Client.Filter"/></param>
/// <returns>This object</returns>
/// <seealso cref="DefaultFilter"/>
/// <seealso cref="AsEnumerable(Exp, bool)"/>
public ASecondaryIndexAccess SetFilter(Client.Filter secondaryIdxFilter)
{
this.DefaultFilter= secondaryIdxFilter;
return this;
}
#region Aerospike Client API Items
/// <summary>
/// The name of the index.
/// </summary>
public string Name { get; }
/// <summary>
/// Returns the Indexes name prefixed with the namespace and set name.
/// </summary>
public string FullName { get; }
/// <summary>
/// The name of the bin that defines this index.
/// </summary>
public string BinName { get; }
/// <summary>
/// The Bin's searchValue type. <see cref="Client.IndexType"/>
/// </summary>
public Client.IndexType BinType { get; }
/// <summary>
/// The index's collection type. <see cref="Client.IndexCollectionType"/>
/// </summary>
public Client.IndexCollectionType CollectionType { get; }
/// <summary>
/// Returns the Aerospike "Namespace's" name
/// </summary>
public string Namespace { get { return this.SetRecords.Namespace; } }
/// <summary>
/// Returns the Aerospike "Set's" name
/// </summary>
public string SetName { get => this.SetRecords.SetName; }
/// <summary>
/// <see href="https://docs.aerospike.com/apidocs/csharp/html/t_aerospike_client_querypolicy"/>
/// </summary>
public QueryPolicy DefaultQueryPolicy
{
get => this.SetRecords.DefaultQueryPolicy;
set
{
this.SetRecords.DefaultQueryPolicy = value;
}
}
#endregion
#region Query Methods
/// <summary>
/// Performs a secondary index query using the provided <see cref="Client.Filter"/>.
/// </summary>
/// <param name="secondaryIdxFilter">The <see cref="Client.Filter"/> used against the secondary index</param>
/// <param name="bins">Only include these bins in the result.</param>
/// <returns>
/// A collection of records that match the filter.
/// </returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <seealso cref="Query(Filter, Exp, string[])"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
/// <seealso cref="SetRecords.Query(Filter, string[])"/>
public IEnumerable<ARecord> Query([NotNull] Client.Filter secondaryIdxFilter, params string[] bins)
=> this.SetRecords.Query(secondaryIdxFilter, bins);
/// <summary>
/// Performs a secondary index query using the provided <see cref="Client.Filter"/> and than apply the filter expression.
/// </summary>
/// <param name="secondaryIdxFilter">The <see cref="Client.Filter"/> used against the secondary index</param>
/// <param name="filterExpression">The Aerospike filter <see cref="Client.Exp"/> that will be applied after the index filter is applied.</param>
/// <param name="bins">Only include these bins in the result.</param>
/// <returns>
/// A collection of records that match the filters.
/// </returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
/// <seealso cref="SetRecords.Query(Filter, Exp, string[])"/>
public IEnumerable<ARecord> Query([NotNull] Client.Filter secondaryIdxFilter, [NotNull] Client.Exp filterExpression, params string[] bins)
=> this.SetRecords.Query(secondaryIdxFilter, filterExpression, bins);
/// <summary>
/// See <see cref="GetFilter(object, CTX[])"/>
/// </summary>
/// <param name="searchValue"></param>
/// <param name="ctxArgs"></param>
/// <returns>A <see cref="Client.Filter"/> to perform the search based on <paramref name="searchValue"/> and the secondary index attributes.</returns>
/// <seealso cref="GetFilter(object, CTX[])"/>
/// <seealso cref="GetFilter(long, long, CTX[])"/>
public Client.Filter GetFilter(Client.Value searchValue, params Client.CTX[] ctxArgs)
=> GetFilter(searchValue.Object,ctxArgs);
/// <summary>
/// Creates a <see cref="Client.Filter"/> based on <paramref name="searchValue"/> type and the secondary index attributes.
/// </summary>
/// <param name="searchValue">The value used to search within the secondary index.</param>
/// <param name="ctxArgs">Advance criteria for the search. See <see cref="Client.CTX"/>.</param>
/// <returns>A <see cref="Client.Filter"/> to perform the search</returns>
/// <seealso cref="GetFilter(Value, CTX[])"/>
/// <seealso cref="GetFilter(long, long, CTX[])"/>
/// <seealso cref="Client.Filter"/>
/// <seealso cref="Client.CTX"/>
/// <seealso cref="DefaultFilter"/>
/// <seealso cref="SetFilter(Filter)"/>
/// <seealso cref="Query(Filter, Exp, string[])"/>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
public Client.Filter GetFilter(object searchValue, params Client.CTX[] ctxArgs)
{
if (this.CollectionType == Client.IndexCollectionType.MAPKEYS)
{
return searchValue switch
{
string strValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, strValue, ctxArgs),
long lValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, lValue, ctxArgs),
int iValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, (long)iValue),
short sValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, (long)sValue, ctxArgs),
ulong ulValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, (long)ulValue, ctxArgs),
ushort usValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, (long)usValue, ctxArgs),
uint uiValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, (long)uiValue, ctxArgs),
_ => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPKEYS, searchValue?.ToString(), ctxArgs)
};
}
else if (this.CollectionType == Client.IndexCollectionType.MAPVALUES)
{
return searchValue switch
{
string strValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, strValue, ctxArgs),
long lValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, lValue, ctxArgs),
int iValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, (long)iValue, ctxArgs),
short sValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, (long)sValue, ctxArgs),
ulong ulValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, (long)ulValue, ctxArgs),
ushort usValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, (long)usValue, ctxArgs),
uint uiValue => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, (long)uiValue, ctxArgs),
_ => Client.Filter.Contains(this.BinName, IndexCollectionType.MAPVALUES, searchValue?.ToString(), ctxArgs)
};
}
else if (this.CollectionType == Client.IndexCollectionType.LIST)
{
return searchValue switch
{
string strValue => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, strValue, ctxArgs),
long lValue => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, lValue, ctxArgs),
int iValue => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, (long)iValue, ctxArgs),
short sValue => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, (long)sValue, ctxArgs),
ulong ulValue => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, (long)ulValue, ctxArgs),
ushort usValue => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, (long)usValue, ctxArgs),
uint uiValue => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, (long)uiValue, ctxArgs),
_ => Client.Filter.Contains(this.BinName, IndexCollectionType.LIST, searchValue?.ToString(), ctxArgs)
};
}
return searchValue switch
{
string strValue => Client.Filter.Equal(this.BinName, strValue, ctxArgs),
long lValue => Client.Filter.Equal(this.BinName, lValue, ctxArgs),
int iValue => Client.Filter.Equal(this.BinName, (long)iValue, ctxArgs),
short sValue => Client.Filter.Equal(this.BinName, (long)sValue, ctxArgs),
ulong ulValue => Client.Filter.Equal(this.BinName, (long)ulValue, ctxArgs),
ushort usValue => Client.Filter.Equal(this.BinName, (long)usValue, ctxArgs),
uint uiValue => Client.Filter.Equal(this.BinName, (long)uiValue, ctxArgs),
_ => Client.Filter.Equal(this.BinName, searchValue?.ToString(), ctxArgs)
};
}
/// <summary>
/// Creates a <see cref="Client.Filter.Range(string, long, long, CTX[])"/> based on the secondary index attributes.
/// </summary>
/// <param name="inclusiveStartRange">Start range, inclusive</param>
/// <param name="inclusiveEndRange">End range, inclusive</param>
/// <param name="ctxArgs">Advance criteria for the search. See <see cref="Client.CTX"/>.</param>
/// <returns>A <see cref="Client.Filter.Range(string, IndexCollectionType, long, long, CTX[])"/> filter</returns>
/// <exception cref="ArgumentException">Thrown if <paramref name="inclusiveStartRange"/> is greater than <paramref name="inclusiveEndRange"/></exception>
/// <seealso cref="GetFilter(object, CTX[])"/>
/// <seealso cref="Client.Filter"/>
/// <seealso cref="Client.CTX"/>
/// <seealso cref="DefaultFilter"/>
/// <seealso cref="SetFilter(Filter)"/>
/// <seealso cref="Query(Filter, Exp, string[])"/>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
public Client.Filter GetFilter(long inclusiveStartRange, long inclusiveEndRange, params Client.CTX[] ctxArgs)
{
if (inclusiveStartRange > inclusiveEndRange)
throw new ArgumentException($"Argument {nameof(inclusiveStartRange)} ({inclusiveStartRange}) is greater than {nameof(inclusiveEndRange)} ({inclusiveEndRange}).", nameof(inclusiveStartRange));
if (this.CollectionType == Client.IndexCollectionType.MAPKEYS)
{
return Client.Filter.Range(this.BinName, IndexCollectionType.MAPKEYS, inclusiveStartRange, inclusiveEndRange, ctxArgs);
}
else if (this.CollectionType == Client.IndexCollectionType.MAPVALUES)
{
return Client.Filter.Range(this.BinName, IndexCollectionType.MAPVALUES, inclusiveStartRange, inclusiveEndRange, ctxArgs);
}
else if (this.CollectionType == Client.IndexCollectionType.LIST)
{
return Client.Filter.Range(this.BinName, IndexCollectionType.LIST, inclusiveStartRange, inclusiveEndRange, ctxArgs);
}
return Client.Filter.Range(this.BinName, inclusiveStartRange, inclusiveEndRange, ctxArgs);
}
/// <summary>
/// Performs a search on the secondary index based <paramref name="idxBinValue"/> and the index associated bin (<seealso cref="BinName"/>).
/// For more information see <see cref="Client.Filter"/>.
/// </summary>
/// <param name="idxBinValue">The searchValue used to conduct the search associated with <see cref="BinName"/></param>
/// <param name = "bins" > Only include these bins in the result.</param>
/// <returns>
/// A collection of records that match the filters.
/// </returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <seealso cref="BinName"/>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(long, long, string[])"/>
/// <seealso cref="SetRecords.Query(Filter, Exp, string[])"/>
public IEnumerable<ARecord> Query([NotNull] dynamic idxBinValue, params string[] bins)
{
return idxBinValue switch
{
Client.Key keyValue => this.Query(GetFilter(keyValue.userKey), bins),
Client.Value vValue => this.Query(GetFilter(vValue), bins),
_ => this.Query(GetFilter(idxBinValue), bins)
};
}
/// <summary>
/// Performs a secondary index range search based on <paramref name="inclusiveStartRange"/> and <paramref name="inclusiveEndRange"/>, inclusively.
/// For more information see <see cref="Client.Filter"/>.
/// </summary>
/// <param name="inclusiveStartRange">Start Rage, inclusive</param>
/// <param name="inclusiveEndRange">End Range, inclusive</param>
/// <param name="bins">Only include these bins in the result.</param>
/// <returns>A collection of records that match the filters.</returns>
/// <exception cref="AerospikeException">Thrown if an index cannot be found to match the filter</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="inclusiveStartRange"/> is greater than <paramref name="inclusiveEndRange"/></exception>
/// <seealso cref="Query(Filter, string[])"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="GetFilter(long, long, CTX[])"/>
/// <seealso cref="SetRecords.Query(Filter, Exp, string[])"/>
public IEnumerable<ARecord> Query(long inclusiveStartRange, long inclusiveEndRange, params string[] bins)
=> this.Query(GetFilter(inclusiveStartRange, inclusiveEndRange), bins);
#endregion
/// <summary>
/// Drops this index
/// </summary>
public void Drop()
{
this.SetRecords.DropIndex(this.Name);
}
#region Overrides
public override bool Equals(object obj)
{
if (obj is SetRecords set) return this.Equals(set);
if (obj is ARecord rec) return this.Equals(rec);
if (obj is ASecondaryIndexAccess idx) return this.Equals(idx);
return base.Equals(obj);
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
public override string ToString()
{
return $"{this.Namespace}.{this.SetName}.{this.Name}({this.BinName})";
}
//protected object ToDump()
//{
// return LPU.ToExpando(this, include: "Namespace, SetName, Name, BinType, BinName, DefaultQueryPolicy, DefaultRecordView");
//}
#endregion
#region IEquatable
public bool Equals([AllowNull] ARecord other)
{
return this.SetRecords.Equals(other);
}
public bool Equals([AllowNull] SetRecords other)
{
return this.SetRecords.Equals(other);
}
public bool Equals([AllowNull] ASecondaryIndexAccess other)
{
if (other is null) return false;
if (ReferenceEquals(other, this)) return true;
return this.SetName == other.SetName
&& this.Namespace == other.Namespace
&& this.Name == other.Name;
}
#endregion
#region IEnumerable
protected struct GroupRecord
{
public object GrpkeyValue;
public ARecord Record;
/// <summary>
/// Returns the Grouping Key based on the index collection type.
/// </summary>
/// <param name="idxValue">Index Value</param>
/// <param name="collectionType">Index Collection Type</param>
/// <param name="rec">The record associated with the index</param>
/// <param name="returningOnlyMatching">
/// if true (default), only index values that match the collection type are returned.
/// If false values that don't match the collection type are returned but will be wrapped with "!|'<value>'|!"
/// </param>
/// <returns>
/// The collection of Grouping records based on the index value or Empty Array to indicate no collection type matches.
/// </returns>
public static IEnumerable<GroupRecord> GetGrpKeys(object idxValue,
Client.IndexCollectionType collectionType,
ARecord rec,
bool returningOnlyMatching = true)
{
if (idxValue is null)
return Enumerable.Empty<GroupRecord>();
static object DetermineGrpValue(object item)
{
var itemType = item.GetType();
if (Helpers.IsSubclassOfInterface(typeof(IDictionary<,>), itemType)
|| Helpers.IsSubclassOfInterface(typeof(IList<>), itemType)
|| typeof(JsonDocument) == itemType)
{
return $"CDT<{Helpers.GetRealTypeName(itemType)}-#{item.GetHashCode()}>";
}
return item;
}
if (idxValue is JsonDocument jsonDoc)
{
return GetGrpKeys(jsonDoc.ToDictionary(), collectionType, rec, returningOnlyMatching);
}
if (collectionType == IndexCollectionType.MAPKEYS)
{
if (idxValue is IDictionary<object, object> mapItems)
{
return mapItems.Keys
.Where(k => k is not null)
.Select(k => new GroupRecord()
{
GrpkeyValue = DetermineGrpValue(k),
Record = rec
});
}
else if (idxValue is IDictionary<string, object> mapsItems)
{
return mapsItems.Keys
.Where(k => k is not null)
.Select(k => new GroupRecord()
{
GrpkeyValue = k,
Record = rec
});
}
if (returningOnlyMatching) return Array.Empty<GroupRecord>();
idxValue = $"!|'{DetermineGrpValue(idxValue)}'|!";
}
else if(collectionType == IndexCollectionType.MAPVALUES)
{
if (idxValue is IDictionary<object, object> mapItems)
{
return mapItems.Values
.Where(k => k is not null)
.Select(k => new GroupRecord()
{
GrpkeyValue = DetermineGrpValue(k),
Record = rec
});
}
else if (idxValue is IDictionary<string, object> mapsItems)
{
return mapsItems.Values
.Where(k => k is not null)
.Select(k => new GroupRecord()
{
GrpkeyValue = DetermineGrpValue(k),
Record = rec
});
}
if (returningOnlyMatching) return Array.Empty<GroupRecord>();
idxValue = $"!|'{DetermineGrpValue(idxValue)}'|!";
}
else if( collectionType == IndexCollectionType.LIST)
{
if (idxValue is IList<object> listItems)
{
return listItems
.Where(k => k is not null)
.Select(k => new GroupRecord()
{
GrpkeyValue = DetermineGrpValue(k),
Record = rec
});
}
else if (idxValue is IList<JsonDocument> jlistItems)
{
return jlistItems
.Where(k => k is not null)
.Select(k => new GroupRecord()
{
GrpkeyValue = DetermineGrpValue(k),
Record = rec
});
}
if (returningOnlyMatching) return Array.Empty<GroupRecord>();
idxValue = $"!|'{DetermineGrpValue(idxValue)}'|!";
}
return new GroupRecord[] { new GroupRecord()
{
GrpkeyValue = DetermineGrpValue(idxValue),
Record = rec
} };
}
}
/// <summary>
/// Returns IEnumerable><see cref="AQueryRecord"/>< based on <see cref="DefaultFilter"/> and <see cref="DefaultQueryPolicy"/> or <paramref name="filterExpression"/>.
/// </summary>
/// <param name="filterExpression">A Filter <see cref="Client.Exp"/> used to obtain the collection of records.</param>
/// <param name="returningOnlyMatchingCT">
/// if true (default), only index values that match the collection type are returned.
/// If false values that don't match the collection type are returned but will be wrapped with "!|'<value>'|!"
/// </param>
/// <returns/>
/// <seealso cref="DefaultQueryPolicy"/>
/// <seealso cref="DefaultFilter"/>
/// <seealso cref="Query(dynamic, string[])"/>
/// <seealso cref="Query(Filter, Exp, string[])"/>
/// <seealso cref="Query(Filter, string[])"/>
public IEnumerable<AQueryRecord> AsEnumerable(Client.Exp filterExpression = null, bool returningOnlyMatchingCT = true)
{
var setRecs = this.DefaultFilter == null
? this.SetRecords.AsEnumerable(filterExpression)
: this.SetRecords.Query(secondaryIdxFilter: this.DefaultFilter, filterExpression: filterExpression);
return setRecs.SelectMany(r => GroupRecord.GetGrpKeys(r.Aerospike.GetValue(this.BinName),
this.CollectionType,
r,
returningOnlyMatchingCT))
.GroupBy(dnis => dnis.GrpkeyValue)
.Select(dnis => new AQueryRecord(dnis.Key, dnis.Select(r => r.Record)));
}
public IEnumerator<AQueryRecord> GetEnumerator()
{
foreach (var queryRec in this.AsEnumerable())
{
yield return queryRec;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
}