-
Notifications
You must be signed in to change notification settings - Fork 1
/
ARecord.cs
1683 lines (1511 loc) · 75.7 KB
/
ARecord.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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Dynamic;
using Aerospike.Client;
using LPU = LINQPad.Util;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
namespace Aerospike.Database.LINQPadDriver.Extensions
{
/// <summary>
/// An extension to the Aerospike <see cref="Aerospike.Client.Record"/>.
/// </summary>
[DebuggerDisplay("{ToString()}")]
public class ARecord : //IEnumerable<BinName>,
IEqualityComparer<ARecord>,
IEquatable<ARecord>,
IEquatable<Client.Key>,
IEquatable<Value>,
IEquatable<string>,
IEquatable<AValue>
{
/// <summary>
/// The Aerospike Epoch
/// </summary>
public static readonly DateTimeOffset Epoch = new DateTime(2010, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
#region Constructors
public ARecord([NotNull] ANamespaceAccess setAccess,
[NotNull] Client.Key key,
[NotNull] Record record,
string[] binNames,
DumpTypes dumpType = DumpTypes.Record,
int setBinsHashCode = 0,
bool? inDoubt = null,
IEnumerable<LPSet.BinType> fkBins = null)
{
this.SetAccess = setAccess;
this.Aerospike = new AerospikeAPI(key,
record,
binNames == null
? Array.Empty<string>()
: (binNames.Length == 0 ? setAccess.BinNames : binNames),
inDoubt: inDoubt);
this.DumpType = dumpType;
this.SetBinsHashCode = setBinsHashCode;
this.FKBins = fkBins;
var recordBins = record?.bins?.Keys.ToArray();
this.BinsHashCode = Helpers.GetStableHashCode(recordBins);
if (this.BinsHashCode != this.SetBinsHashCode && this.DumpType == DumpTypes.Record)
{
if (recordBins is null
|| (recordBins.Length <= this.Aerospike.BinNames?.Length
&& recordBins.All(n => this.Aerospike.BinNames.Contains(n))))
{ }
else
{
this.DumpType = DumpTypes.Dynamic;
this.HasDifferentSchema= true;
}
}
}
/// <summary>
/// Creates an AS AerospikeRecord that can be used to add/update an Aerospike DB record.
/// </summary>
/// <param name="ns">Namespace Name</param>
/// <param name="set">Set Name</param>
/// <param name="keyValue">The primary AerospikeKey</param>
/// <param name="binValues">A dictionary where the key is the bin name and the value is the bin's value</param>
/// <param name="setAccess">The set instance that will be associated to this record.</param>
/// <param name="expirationDate">
/// Expiration Date of the record.
/// Note: If <paramref name="expiration"/> is not null that value is used.
/// </param>
/// <param name="expiration">
/// TTL Epoch in seconds from Jan 01 2010 00:00:00 GMT
/// Note: if this value is null, <paramref name="expirationDate"/> is used.
/// <see cref="AerospikeAPI.Expiration"/>
/// </param>
/// <param name="generation">record generation</param>
/// <param name="dumpType"><see cref="DumpTypes"/></param>
/// <param name="setBinsHashCode">An internal HashCode defined by the associated Set used to determine if this record has dynamic bins</param>
/// <param name="inDoubt">
/// For strong consistency, this indicates if this record's situation is uncertain of a transaction outcome.
/// <see cref="AerospikeAPI.InDoubt"/>
/// </param>
/// <param name="fkBins">
/// This record is associated a collection of FK bins
/// </param>
public ARecord([NotNull] string ns,
[NotNull] string set,
[NotNull] dynamic keyValue,
[NotNull] IDictionary<string, object> binValues,
ANamespaceAccess setAccess = null,
int? expiration = null,
DateTimeOffset? expirationDate = null,
int? generation = null,
DumpTypes dumpType = DumpTypes.Record,
int setBinsHashCode = 0,
bool? inDoubt = null,
IEnumerable<LPSet.BinType> fkBins = null)
{
this.SetAccess = setAccess;
this.Aerospike = new AerospikeAPI(ns,
set,
keyValue,
binValues,
expiration,
expirationDate,
generation,
inDoubt);
this.DumpType = dumpType;
this.SetBinsHashCode = setBinsHashCode;
this.FKBins = fkBins;
this.BinsHashCode = Helpers.GetStableHashCode(binValues.Keys.ToArray());
if (this.BinsHashCode != this.SetBinsHashCode && this.DumpType == DumpTypes.Record)
{
if (binValues.Count <= this.Aerospike.BinNames?.Length
&& binValues.All(n => this.Aerospike.BinNames.Contains(n.Key)))
{ }
else
{
this.DumpType = DumpTypes.Dynamic;
this.HasDifferentSchema = true;
}
}
}
/// <summary>
/// Creates an AS AerospikeRecord based on <paramref name="baseRecord"/>.
/// </summary>
/// <param name="baseRecord">
/// The record that will be used as the bases of this record.
/// </param>
/// <param name="keyValue">
/// If provided, this record will have a new PK, otherwise the PK of the <paramref name="baseRecord"/> is uded.
/// </param>
/// <param name="binValues">
/// A dictionary where the key is the bin name and the value is the bin's value.
/// If provided, this will be merged/replaced with the <paramref name="baseRecord"/> bin values.
/// </param>
/// <param name="expirationDate">
/// Expiration Date of the record.
/// If not provided, the value of <paramref name="baseRecord"/> is used.
/// Note: If <paramref name="expiration"/> is not null that value is used.
/// </param>
/// <param name="expiration">
/// TTL Epoch in seconds from Jan 01 2010 00:00:00 GMT
/// Note: if this value is null, <paramref name="expirationDate"/> is used.
/// <see cref="AerospikeAPI.Expiration"/>
/// </param>
/// <param name="generation">record generation</param>
/// <param name="dumpType"><see cref="DumpTypes"/></param>
/// <param name="inDoubt">
/// For strong consistency, this indicates if this record's situation is uncertain of a transaction outcome.
/// <see cref="AerospikeAPI.InDoubt"/>
/// </param>
public ARecord([NotNull] ARecord baseRecord,
dynamic keyValue = null,
IDictionary<string, object> binValues = null,
int? expiration = null,
DateTimeOffset? expirationDate = null,
int? generation = null,
DumpTypes? dumpType = null,
bool? inDoubt = null)
{
if(binValues is null)
binValues = baseRecord.ToDictionary();
else
binValues = baseRecord.ToDictionary()
.Concat(binValues)
.GroupBy(p => p.Key)
.ToDictionary(g => g.Key, g => g.Last().Value);
this.SetAccess = baseRecord.SetAccess;
this.Aerospike = new AerospikeAPI(baseRecord.Aerospike.Namespace,
baseRecord.Aerospike.SetName,
keyValue ?? baseRecord.Aerospike.PrimaryKey,
binValues,
expiration ?? baseRecord.Aerospike.Expiration,
expirationDate,
generation ?? baseRecord.Aerospike.Generation,
inDoubt ?? baseRecord.Aerospike.InDoubt);
this.DumpType = dumpType ?? baseRecord.DumpType;
this.SetBinsHashCode = baseRecord.SetBinsHashCode;
this.FKBins = baseRecord.FKBins;
this.BinsHashCode = Helpers.GetStableHashCode(binValues.Keys.ToArray());
if(this.BinsHashCode != this.SetBinsHashCode && this.DumpType == DumpTypes.Record)
{
if(binValues.Count <= this.Aerospike.BinNames?.Length
&& binValues.All(n => this.Aerospike.BinNames.Contains(n.Key)))
{ }
else
{
this.DumpType = DumpTypes.Dynamic;
this.HasDifferentSchema = true;
}
}
}
public ARecord([NotNull] ARecord cloneRecord)
{
this.SetAccess = cloneRecord.SetAccess;
this.Aerospike = new AerospikeAPI(cloneRecord.Aerospike);
this.DumpType = cloneRecord.DumpType;
this.SetBinsHashCode = cloneRecord.SetBinsHashCode;
this.BinsHashCode = cloneRecord.BinsHashCode;
this.HasDifferentSchema = cloneRecord.HasDifferentSchema;
this.RecordException= cloneRecord.RecordException;
this.FKBins = cloneRecord.FKBins;
}
#endregion
#region Settings, Record State, etc.
/// <summary>
/// How the record will be used by the <see cref="LINQPad.Extensions.Dump{T}(T)"/>.
/// </summary>
public enum DumpTypes
{
/// <summary>
/// Displays the record based on the detected bins of the set.
/// If this record's bin are different from the set, Dump BinType <see cref="Dynamic"/> is used.
/// </summary>
Record = 0,
/// <summary>
/// Similar to <see cref="Record"/> except all bins associated to this record are displayed regardless of the set defined bins.
/// </summary>
Dynamic = 1,
/// <summary>
/// Displays all properties/fields of this instance like the LinqPad <see cref="LINQPad.Extensions.Dump{T}(T)"/> method.
/// </summary>
Detail = 2,
LinqPad = Detail,
Normal = Detail
}
public static string DefaultASPIKeyName = "PK";
/// <summary>
/// How this record is displayed when using the LinqPad <see cref="LINQPad.Extensions.Dump{T}(T)"/> method.
/// The default is <see cref="DumpTypes.Record"/>
/// <see cref="DumpTypes"/>
/// </summary>
/// <seealso cref="DumpTypes"/>
/// <see cref="SetDumpType(DumpTypes)"/>
public DumpTypes DumpType { get; set; } = DumpTypes.Record;
/// <summary>
/// Changes how the instance's properties are displayed. See <see cref="DumpType"/>
/// </summary>
/// <param name="newType">The new BinType, see <see cref="DumpTypes"/></param>
/// <returns>Returns this instance</returns>
/// <seealso cref="DumpType"/>
/// <seealso cref="DumpTypes"/>
public ARecord SetDumpType(DumpTypes newType)
{
this.DumpType = newType;
return this;
}
/// <summary>
/// If not null, this record encountered an exception during procession.
/// </summary>
public Exception RecordException { get; private set; }
/// <summary>
/// Places record in exception state.
/// </summary>
/// <param name="exception"></param>
/// <returns>Returns the Record.</returns>
public ARecord SetException(Exception exception)
{
this.RecordException = exception;
return this;
}
/// <summary>
/// Returns true to indicated that this record's schema does not match when the set was scanned.
/// </summary>
public bool HasDifferentSchema
{
get;
}
/// <summary>
/// This is the Set's Bin Names Hash Code
/// </summary>
protected int SetBinsHashCode { get; }
/// <summary>
/// This is the Record's Bin Name Hash Code
/// </summary>
protected int BinsHashCode { get; }
/// <summary>
/// Gets the collection of this record's FKeys are associated with...
/// </summary>
/// <value>The collection of FKs or null.</value>
protected IEnumerable<LPSet.BinType> FKBins { get; }
/// <summary>
/// The Set Access instance that this record is associated with.
/// </summary>
public ANamespaceAccess SetAccess { get; set; }
#endregion
#region Aerospike Client Related API Items
public sealed class AerospikeAPI
{
internal AerospikeAPI(Client.Key key,
Client.Record record,
string[] binNames,
bool? inDoubt = null)
{
this.BinNames = binNames;
this.Record = record ?? new Record(new Dictionary<string, object>(0), 0, 0);
this.Key = key;
this.InDoubt = inDoubt;
}
internal AerospikeAPI([NotNull] string ns,
[NotNull] string set,
[NotNull] dynamic keyValue,
[NotNull] IDictionary<string, object> binValues,
int? expiration = null,
DateTimeOffset? expirationDate = null,
int? generation = null,
bool? inDoubt = null)
{
this.Key = Helpers.DetermineAerospikeKey(keyValue, ns, set);
this.Record = new Record((Dictionary<string, object>)binValues,
generation ?? 0,
expiration ?? (expirationDate.HasValue
? (int)(expirationDate.Value - Epoch).TotalSeconds
: 0));
this.BinNames = binValues.Keys.ToArray();
this._bins = this.Record?.bins?.Select(b => new Bin(b.Key, b.Value)).ToArray();
this.InDoubt = inDoubt;
}
internal AerospikeAPI(AerospikeAPI cloneRecord)
{
this.Key = new Client.Key(cloneRecord.Key.ns,
cloneRecord.Key.setName,
Value.Get(cloneRecord.Key.userKey));
this.Record = new Record(new Dictionary<string, object>(cloneRecord.Record.bins),
cloneRecord.Record.generation,
cloneRecord.Record.expiration);
this.BinNames = cloneRecord.BinNames;
this.InDoubt = cloneRecord.InDoubt;
}
private Bin[] _bins;
/// <summary>
/// Return all bin names that can be present in the record.
/// This can be all bins associated with the namespace.
/// </summary>
public string[] BinNames { get; }
/// <summary>
/// Returns the <seealso cref="Bin"/> actually defined for this record.
/// </summary>
public Bin[] Bins
{
get
{
if (this._bins == null)
return this._bins = this.Record?.bins?.Select(b => new Bin(b.Key, b.Value)).ToArray() ?? Array.Empty<Bin>();
return this._bins;
}
internal set { this._bins = value; }
}
/// <summary>
/// Returns the Aerospike <see cref="Aerospike.Client.Record"/> associated to this object.
/// </summary>
public Aerospike.Client.Record Record { get; }
/// <summary>
/// The primary key's value, if <see cref="Aerospike.Client.Policy.sendKey"/> is true.
/// Otherwise this will be the digest.
/// </summary>
/// <seealso cref="HasKeyValue"/>
/// <seealso cref="AerospikeAPI.Key"/>
/// <seealso cref="AerospikeAPI.KeyValue"/>
/// <seealso cref="AerospikeAPI.Digest"/>
/// <seealso cref="Equals(string)"/>
/// <seealso cref="SetRecords.Get(dynamic, string[])"/>
public APrimaryKey PrimaryKey { get => this.Key is null ? null : new APrimaryKey(this.Key); }
/// <summary>
/// If true, the PK has an actual value. If false, the digest is only provided.
/// </summary>
public bool HasKeyValue { get => this.KeyValue?.Object != null; }
/// <summary>
/// The Records Aerospike <see cref="Aerospike.Client.Key"/>
/// </summary>
/// <seealso cref="KeyValue"/>
/// <seealso cref="Equals(Client.Key)"/>
/// <seealso cref="Equals(string)"/>
public Client.Key Key { get; }
/// <summary>
/// Returns the key value (<see cref="Aerospike.Client.Key"/> or <see cref="Aerospike.Client.Value"/>) of the record. If null, the key was not saved but only the <see cref="Digest"/>
/// </summary>
/// <seealso cref="Digest"/>
/// <seealso cref="Equals(string)"/>
/// <seealso cref="Equals(Client.Key)"/>
/// <seealso cref="Key"/>
public Value KeyValue { get => this.Key.userKey; }
/// <summary>
/// This is the Namespace associated with this record.
/// </summary>
/// <seealso cref="SetName"/>
public string Namespace { get => this.Key.ns; }
/// <summary>
/// returns the name of the Set associated with this record
/// </summary>
/// <seealso cref="SetName"/>
public string SetName { get => this.Key.setName; }
/// <summary>
/// Returns the AerospikeKey's Digest
/// </summary>
/// <seealso cref="KeyValue"/>
/// <seealso cref="Equals(Client.Key)"/>
/// <seealso cref="Equals(string)"/>
public byte[] Digest { get => this.Key.digest; }
/// <summary>
/// Returns the Record's Aerospike Generation
/// </summary>
public int Generation { get => this.Record.generation; }
/// <summary>
/// Date record will expire, in seconds from Jan 01 2010 00:00:00 GMT
/// </summary>
/// <seealso cref="TTL"/>
public int Expiration { get => this.Record.expiration; }
public static TimeSpan? CalcTTLTimeSpan(int? ttl)
{
if (!ttl.HasValue) return null;
if (ttl == -2 || ttl == -1)
return null;
if (ttl <= 1)
return TimeSpan.Zero;
return TimeSpan.FromSeconds((double)ttl);
}
/// <summary>
/// Returns the time span of when this record will expire.
/// </summary>
/// <seealso cref="Expiration"/>
public TimeSpan? TTL
{
get => CalcTTLTimeSpan(this.Record.TimeToLive);
}
/// <summary>
/// Returns or sets the bin based on <paramref name="binName"/>
/// </summary>
/// <param name="binName">Name of the bin</param>
/// <returns>A BinName or null indicating the bin name does not exists</returns>
/// <seealso cref="GetValue(string)"/>
/// <seealso cref="SetValue(string, object, bool)"/>
/// <seealso cref="BinExists(string)"/>
/// <see cref="Values"/>
public Bin? this[string binName]
{
get
{
if (this.Record.bins.TryGetValue(binName, out object value))
{
return new Bin(binName, value);
}
return null;
}
}
/// <summary>
/// The number of bins defined in this record.
/// </summary>
public int Count { get => this.Record.bins?.Count ?? 0; }
/// <summary>
/// For strong consistency, this indicates if this record's situation is uncertain of a transaction outcome.
/// </summary>
/// <seealso href="https://support.aerospike.com/s/article/What-does-InDoubt-true-boolean-exception-response-means"/>
/// <seealso href="https://aerospike.com/blog/resolving-uncertain-transactions-in-aerospike/"/>
/// <seealso href="https://aerospike.com/blog/developers-understanding-aerospike-transactions/"/>
public bool? InDoubt { get; }
private ExpandoObject _definedValuesCache = null;
/// <summary>
/// Returns only the defined bin name/value pairs in the record including the primary key defined as <see cref="DefaultASPIKeyName"/>.
/// The returned object is actually an <see cref="ExpandoObject"/> instance.
/// </summary>
/// <seealso cref="this[string]"/>
/// <see cref="GetValues"/>
/// <see cref="GetValue(string)"/>
/// <see cref="ToValue(string)"/>
/// <see cref="BinExists(string)"/>
public dynamic Values
{
get
{
if (this._definedValuesCache == null)
{
var record = new ExpandoObject() as IDictionary<string, Object>;
record.Add(DefaultASPIKeyName, this.PrimaryKey);
if (this.Record.bins != null)
foreach (var bin in this.Record.bins)
{
record.Add(Helpers.CheckName(bin.Key, "Bin"), bin.Value);
}
this._definedValuesCache = (ExpandoObject)record;
}
return (dynamic)this._definedValuesCache;
}
}
/// <summary>
/// Gets the value based on <paramref name="binName"/>
/// </summary>
/// <param name="binName">The name of the bin within the record</param>
/// <returns>
/// The value of the bin or null indicating that the bin was not found.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="BinExists(string)"/>
/// <seealso cref="SetValue(string, object, bool)"/>
/// <seealso cref="ToValue(string)"/>
public object GetValue([NotNull] string binName)
{
return this.Record.bins != null
&& this.Record.bins.TryGetValue(binName, out object value) ? value : null;
}
/// <summary>
/// Gets the <see cref="AValue"/> based on <paramref name="binName"/>
/// </summary>
/// <param name="binName">The name of the bin within the record</param>
/// <returns>
/// The <see cref="AValue"/> of the bin or null indicating that the bin was not found.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="BinExists(string)"/>
/// <seealso cref="GetValue(string)"/>
/// <seealso cref="SetValue(string, object, bool)"/>
public AValue ToValue([NotNull] string binName)
{
return this.Record.bins != null
&& this.Record.bins.TryGetValue(binName, out object value) ? new AValue(value, binName, "Value") : null;
}
/// <summary>
/// Gets all defined values in the record and returns a Dictionary.
/// </summary>
/// <returns>
/// Returns a Dictionary of items where the key is the bin name and the value is the associated bin value.
/// </returns>
/// <seealso cref="Values"/>
public Dictionary<string, object> GetValues()
{
return this.Record.bins;
}
/// <summary>
/// Determines if the bin exists within the record.
/// </summary>
/// <param name="binName">BinName Name</param>
/// <returns>
/// True if it exists.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="GetValue(string)"/>
public bool BinExists(string binName)
{
return this.Record.bins.ContainsKey(binName);
}
}
/// <summary>
/// Container for Aerospike API Properties.
/// You can obtain <see cref="Aerospike.Client.Record"/> instance, namespace, set name, etc.
/// </summary>
/// <seealso cref="ARecord.AerospikeAPI.Record"/>
/// <seealso cref="ARecord.AerospikeAPI.Values"/>
public AerospikeAPI Aerospike { get; }
#endregion
/// <summary>
/// Returns the value associated with Primary Key (<see cref="DefaultASPIKeyName"/>), a bin, or null to indicate the bin doesn't exists.
/// </summary>
/// <param name="pkbinName">Name of the bin or primary key name defined by <see cref="DefaultASPIKeyName"/>.</param>
/// <returns>An <see cref="AValue"/> or null indicating the bin name does not exists</returns>
/// <seealso cref="AerospikeAPI.GetValue(string)"/>
/// <seealso cref="AerospikeAPI.ToValue(string)"/>
/// <seealso cref="AerospikeAPI.PrimaryKey"/>
/// <seealso cref="GetPK"/>
/// <seealso cref="SetValue(string, object, bool)"/>
/// <seealso cref="BinExists(string)"/>
/// <seealso cref="AerospikeAPI.Values"/>
public AValue this[string pkbinName]
{
get
{
if (pkbinName == DefaultASPIKeyName)
return new APrimaryKey(this.Aerospike.Key);
return this.Aerospike.ToValue(pkbinName);
}
}
public static implicit operator ExpandoObject(ARecord r) => (ExpandoObject) r.Aerospike.Values;
/// <summary>
/// Set a value to a bin within the record. This can be adding a new bin or updating an existing bin.
/// </summary>
/// <param name="binName">BinName Name</param>
/// <param name="value">
/// Value associated with the bin.
/// If null, the bin is removed from DB record.
/// <paramref name="value"/> can be a <see cref="Value"/>, <see cref="AerospikeAPI.Key"/>, <see cref="Bin"/> or a native type/class.
/// </param>
/// <param name="cloneRecord">
/// If true, this instance (record) is cloned and then updated.
/// </param>
/// <returns>
/// Returns the updated record.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="BinExists(string)"/>
/// <seealso cref="GetValue(string)"/>
/// <seealso cref="Refresh(Policy)"/>
/// <seealso cref="Update(WritePolicy)"/>
public ARecord SetValue([NotNull] string binName, object value, bool cloneRecord = false)
{
ARecord newRec = cloneRecord ? new ARecord(this) : this;
object binValue = value;
newRec.Aerospike.Bins = null;
if (value is Client.Key key)
binValue = key.userKey?.Object;
else if (value is Client.Value clientValue)
binValue = clientValue.Object;
else if (value is Client.Bin bValue)
binValue = bValue.value?.Object;
else if (value is AValue aValue)
binValue = aValue.Value;
if (newRec.Aerospike.Record.bins.ContainsKey(binName))
{
newRec.Aerospike.Record.bins[binName] = binValue;
}
else
{
newRec.Aerospike.Record.bins.Add(binName, binValue);
}
return newRec;
}
/// <summary>
/// Set a value to a bin within the record. This can be adding a new bin or updating an existing bin.
/// </summary>
/// <param name="binName">BinName Name</param>
/// <param name="value">
/// Value associated with the bin.
/// If null, the bin is removed from DB record.
/// <paramref name="value"/> can be a <see cref="Value"/>, <see cref="AerospikeAPI.Key"/>, <see cref="Bin"/> or a native type/class.
/// </param>
/// <param name="cloneRecord">
/// If true, this instance (record) is cloned and then updated.
/// </param>
/// <returns>
/// Returns the updated record.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="BinExists(string)"/>
/// <seealso cref="GetValue(string)"/>
public ARecord SetValue<T>([NotNull] string binName, Nullable<T> value, bool cloneRecord = false)
where T : struct
{
if(value.HasValue)
return this.SetValue(binName, value.Value, cloneRecord);
return this.SetValue(binName, null, cloneRecord);
}
/// <summary>
/// Determines if the bin exists within the record.
/// </summary>
/// <param name="binName">BinName Name</param>
/// <returns>
/// True if it exists.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="GetValue(string)"/>
/// <seealso cref="SetValue(string, object, bool)"/>
public bool BinExists([NotNull] string binName) => this.Aerospike.BinExists(binName);
/// <summary>
/// Returns the Bin's value.
/// </summary>
/// <param name="binName">The bin name</param>
/// <returns>
/// Returns an <see cref="AValue"/> or null indicating that the bin was not found.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="SetValue(string, object, bool)"/>
/// <seealso cref="BinExists(string)"/>
/// <seealso cref="AerospikeAPI.GetValue(string)"/>
/// <seealso cref="AerospikeAPI.ToValue(string)"/>
/// <seealso cref="AerospikeAPI.Bins"/>
/// <seealso cref="GetPK"/>
public AValue GetValue([NotNull]string binName) => this.Aerospike.ToValue(binName);
/// <summary>
/// Returns the Primary Key for this record.
/// </summary>
/// <returns>
/// Returns a <see cref="APrimaryKey"/> value.
/// </returns>
/// <seealso cref="this[string]"/>
/// <seealso cref="AerospikeAPI.Key"/>
/// <seealso cref="GetValue(string)"/>
public AValue GetPK() => new APrimaryKey(this.Aerospike.Key);
/// <summary>
/// Deletes the record from the DB.
/// </summary>
/// <param name="writePolicy"></param>
/// <returns>
/// True if deleted.
/// </returns>
/// <exception cref="NullReferenceException">
/// If <see cref="SetAccess"/> is null, a Null reference exception is thrown.
/// </exception>
/// <seealso cref="Update(WritePolicy)"/>
/// <seealso cref="Refresh(Policy)"/>
public bool Delete(WritePolicy writePolicy = null)
{
if (this.SetAccess == null)
throw new NullReferenceException("No Set Instance associated with this record. As such, it cannot be deleted.");
return this.SetAccess
.AerospikeConnection
.AerospikeClient.Delete(writePolicy, this.Aerospike.Key);
}
/// <summary>
/// Updates this record in the DB
/// </summary>
/// <param name="writePolicy"></param>
/// <returns>Returns this object</returns>
/// <exception cref="NullReferenceException">
/// If <see cref="SetAccess"/> is null, a Null reference exception is thrown.
/// </exception>
/// <seealso cref="Refresh(Policy)"/>
/// <seealso cref="Delete(WritePolicy)"/>
public ARecord Update(WritePolicy writePolicy = null)
{
if (this.SetAccess == null)
throw new NullReferenceException("No Set Instance associated with this record. As such, it cannot be updated.");
this.SetAccess
.AerospikeConnection
.AerospikeClient.Put(writePolicy, this.Aerospike.Key, this.Aerospike.Bins);
return this;
}
/// <summary>
/// Re-retrieves the record from the DB based on <see cref="Key"/>.
/// A new instance of record is created and returned.
/// </summary>
/// <param name="policy">A <see cref="Policy"/> instance or the default policy is used.</param>
/// <returns>
/// A new instance of <see cref="ARecord"/> returned from the DB.
/// If the record is not found in the DB, null is returned.
/// </returns>
/// <exception cref="NullReferenceException">
/// If <see cref="SetAccess"/> is null, a Null reference exception is thrown.
/// </exception>
/// <seealso cref="Update(Client.WritePolicy)"/>
/// <seealso cref="Delete(Client.WritePolicy)"/>
public ARecord Refresh(Policy policy = null)
{
if(this.SetAccess == null)
throw new NullReferenceException("No Set Instance associated with this record. As such, it cannot be retrieved.");
var record = this.SetAccess
.AerospikeConnection
.AerospikeClient
.Get(policy, this.Aerospike.Key);
return record is null
? null
: new ARecord(this.SetAccess,
this.Aerospike.Key,
record,
this.Aerospike.BinNames,
this.DumpType,
fkBins: this.FKBins);
}
/// <summary>
/// Will convert the record into a user defined class were the bin's name is matches the class's field/property name and type.
/// <seealso cref="Aerospike.Client.BinNameAttribute"/>
/// <seealso cref="Aerospike.Client.BinIgnoreAttribute"/>
/// <seealso cref="Aerospike.Client.ConstructorAttribute"/>
/// </summary>
/// <typeparam name="T">user defined class</typeparam>
/// <param name="transform">
/// A action that is called to perform customized transformation.
/// First argument -- the name of the property/field
/// Second argument -- the property/field type
/// Third argument -- bin name
/// Fourth argument -- bin value
/// Returns the new transformed object or null to indicate that this transformation should be skipped.
/// </param>
/// <returns>
/// An instance of the class or an exception.
/// </returns>
/// <exception cref="MissingMethodException">Thrown if the constructor for the type cannot be determined</exception>
/// <seealso cref="Aerospike.Client.BinNameAttribute"/>
/// <seealso cref="Aerospike.Client.BinIgnoreAttribute"/>
/// <seealso cref="Aerospike.Client.ConstructorAttribute"/>
/// <seealso cref="Cast{T}(object, Func{string, Type, string, object, object})"/>
public T Cast<T>(Func<string, Type, string, object, object> transform = null)
{
return (T)Helpers.Transform<T>(this.Aerospike.Record.bins, transform);
}
/// <summary>
/// Will convert the record into a user defined class were the bin's name is matches the class's field/property name and type.
/// <seealso cref="Aerospike.Client.BinNameAttribute"/>
/// <seealso cref="Aerospike.Client.BinIgnoreAttribute"/>
/// <seealso cref="Aerospike.Client.ConstructorAttribute"/>
/// </summary>
/// <typeparam name="T">user defined class</typeparam>
/// <param name="primaryKey">
/// The primary key can be a <see cref="Aerospike.Client.Key"/>, <see cref="Aerospike.Client.Value"/>, digest (byte[]), or a .net type.
/// </param>
/// <param name="transform">
/// A action that is called to perform customized transformation.
/// First argument -- the name of the property/field
/// Second argument -- the property/field type
/// Third argument -- bin name
/// Fourth argument -- bin value
/// Returns the new transformed object or null to indicate that this transformation should be skipped.
/// </param>
/// <returns>
/// An instance of the class or an exception.
/// </returns>
/// <exception cref="MissingMethodException">Thrown if the constructor for the type cannot be determined</exception>
/// <seealso cref="Aerospike.Client.BinNameAttribute"/>
/// <seealso cref="Aerospike.Client.BinIgnoreAttribute"/>
/// <seealso cref="Aerospike.Client.ConstructorAttribute"/>
/// <seealso cref="Cast{T}(Func{string, Type, string, object, object})"/>
public T Cast<T>(object primaryKey, Func<string, Type, string, object, object> transform = null)
{
Client.Key pk = null;
if (primaryKey != null)
{
pk = Helpers.DetermineAerospikeKey(primaryKey, this.Aerospike.Namespace, this.Aerospike.SetName);
}
return (T)Helpers.Transform<T>(this.Aerospike.Record.bins, transform, pk);
}
/// <summary>
/// Converts the record into a Dictionary<string, object> where the key is the bin name and the value is the bin's value.
/// To obtain the actual Aerospike Record see <see cref="AerospikeAPI.Record"/>.
/// </summary>
/// <returns>
/// An IDictionary where the key is the bin name and the value is the bin's value.
/// </returns>
/// <seealso cref="ARecord.AerospikeAPI.Values"/>
/// <seealso cref="ARecord.AerospikeAPI.Record"/>
/// <seealso cref="ARecord.AerospikeAPI.GetValues"/>
public IDictionary<string, object> ToDictionary() => new Dictionary<string, object>((IDictionary<string, object>)this.Aerospike.GetValues());
/// <summary>
/// Creates an Aerospike Digest byte array (always length of 20 bytes) based on <paramref name="value"/>.
/// </summary>
/// <param name="value">
/// The value that will be converted to a digest.
/// </param>
/// <returns>
/// A byte array of length 20.
/// </returns>
public byte[] CreateDigest(object value)
=> Key.ComputeDigest(this.Aerospike.PrimaryKey.AerospikeKey.setName,
Value.Get(value));
/// <summary>
/// Gets the associated Foreign Key of this Set, if defined.
/// </summary>
/// <param name="forFKBinName">Name of for Foreign Key bin.</param>
/// <returns>
/// A collection of <see cref="ARecord"/> based on the FK
/// or empty collection if the <paramref name="forFKBinName"/> was not found in this record.
/// Note: If the FK's PK is not found a empty record is returned.
/// </returns>
public IEnumerable<ARecord> GetFKValues(string forFKBinName)
{
var fxBin = this.FKBins.FirstOrDefault(f => f.BinName == forFKBinName);
if(fxBin is not null)
{
var aValues = this.GetValue(fxBin.BinName);
if(aValues is not null)
{
var nssetNames = fxBin.FKSetname?.Split(':');
string setName = null;
string namespaceName = null;
if(nssetNames is not null && nssetNames.Length > 0)
{
setName = nssetNames.Length == 2 ? nssetNames[1] : nssetNames[0];
namespaceName = nssetNames.Length == 2 ? nssetNames[0] : null;
}
var setAccess = namespaceName is null || namespaceName == this.SetAccess.Namespace
? this.SetAccess
: ANamespaceAccess.FindNamespace(namespaceName);
IEnumerable<ARecord> GetRecord(AValue aValue)
{
if(aValue.Value is byte[] digest && digest.Length == 20)
{
yield return setAccess
.Get(setName,
digest);
}
else if(aValue.IsCDT)
{
var recordList = new List<ARecord>();
foreach(var value in aValue.AsEnumerable())
{
recordList.AddRange(GetRecord(value));
}
foreach(var record in recordList)
{
yield return record;
}
}
else
{
yield return setAccess
.Get(setName,
aValue.Value);
}
}
return GetRecord(aValues);
}
}