-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLPNamespace.cs
579 lines (486 loc) · 23.3 KB
/
LPNamespace.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
using Aerospike.Client;
using LINQPad.Extensibility.DataContext;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace Aerospike.Database.LINQPadDriver
{
/// <summary>
///
/// </summary>
[System.Diagnostics.DebuggerDisplay("{DebuggerDisplay}")]
public sealed partial class LPNamespace : IGenerateCode, ILPExplorer
{
private readonly static ConcurrentBag<LPNamespace> LPNamespacesBag = new ConcurrentBag<LPNamespace>();
public LPNamespace(string name)
{
this.Name = name;
this.SafeName = Helpers.CheckName(name, "Namespace");
LPNamespacesBag.Add(this);
}
#if NET7_0_OR_GREATER
[GeneratedRegex("set=(?<setname>[^:;]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
static private partial Regex SetNameRegEx();
[GeneratedRegex("ns=(?<namespace>[^:;]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
static private partial Regex NameSpaceRegEx();
[GeneratedRegex(@"^roster=(?<roster>\d+|(?:null)):pending_roster=(?<pending>\d+|(?:null)):observed_nodes=(?<observed>\d+|(?:null))", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
static private partial Regex NameSpaceRosterRegEx();
#else
static private readonly Regex setNameRegEx = new Regex("set=(?<setname>[^:;]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static private readonly Regex nameSpaceRegEx = new Regex("ns=(?<namespace>[^:;]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static private readonly Regex nameSpaceRosterRegEx = new Regex(@"^roster=(?<roster>\d+|(?:null)):pending_roster=(?<pending>\d+|(?:null)):observed_nodes=(?<observed>\d+|(?:null))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static private Regex SetNameRegEx() => setNameRegEx;
static private Regex NameSpaceRegEx() => nameSpaceRegEx;
static private Regex NameSpaceRosterRegEx() => nameSpaceRosterRegEx;
#endif
public LPNamespace(string name, IEnumerable<string> setAttribs)
: this(name)
{
/*
* ns=test:set=demo:objects=4:tombstones=0:memory_data_bytes=0:device_data_bytes=368:truncate_lut=0:sindexes=0:index_populating=false:disable-eviction=false:enable-index=false:stop-writes-count=0
*/
if(Client.Log.DebugEnabled())
{
if (setAttribs is null)
Client.Log.Debug($"NS {name} Set Attributes is null");
else if (setAttribs.Any())
{
foreach(var setAttr in setAttribs)
{
Client.Log.Debug($"NS {name} {setAttr}");
}
}
else
Client.Log.Debug($"NS {name} Set Attributes is Empty");
}
var setNames = from setAttrib in setAttribs
let matches = SetNameRegEx().Match(setAttrib)
select matches.Groups["setname"].Value;
var nsSets = setNames
.Where(s => !string.IsNullOrEmpty(s))
.Select(s => new LPSet(this, s))
.ToList();
nsSets.Add(new LPSet(this));
this.aSets = nsSets.ToList();
}
public LPNamespace(string name,
IEnumerable<string> setAttribs,
string binNames,
string rosterInfo,
string nsConfig)
: this(name, setAttribs ?? Enumerable.Empty<string>())
{
/*
* bin_names=62,bin_names_quota=65535,addbin,appendbin,prependbin,bbin,lbin,lbin2,lbin3,bbin3,bbin2,putgetbin,asqbin,name,age,B5,audfbin1,bin5,A,listmapbin,bin1,bin2,expirebin,D,B,C,H,E,genbin,hllbin_1,hllbin_2,hllbin_3,testbin,listbin2,listbin1,mapbin2,mapbin1,optintbin,optstringbin,optintbin1,optintbin2,opbbin,ophbin,ophbinother,ophbino,oplistbin,otherbin,opmapbin,bin3,bin4,l2,l1,map_bin,list,tqebin1,tqebin2,foo,password,fltint,listbin,mapbin,blob_data_1,catalog,diffbin
*/
if(Client.Log.DebugEnabled())
{
if (binNames is null)
Client.Log.Debug($"NS {name} Bins is null");
else if(binNames == string.Empty)
Client.Log.Debug($"NS {name} Bins is Empty");
else
{
Client.Log.Debug($"NS {name}: {binNames}");
}
}
var binNameSplit = binNames.Split(',', StringSplitOptions.RemoveEmptyEntries);
this.bins = binNameSplit.Where(s => !s.Contains('=')).ToList();
this.safeBins = this.Bins.Select(s => Helpers.CheckName(s, "Bin")).ToList();
//roster=2887538337:pending_roster=2887538337:observed_nodes=2887538337
//roster=null:pending_roster=null:observed_nodes=null
if(Client.Log.DebugEnabled())
{
if(rosterInfo is null)
Client.Log.Debug($"NS {name} Roster Info is null");
else if(rosterInfo == string.Empty)
Client.Log.Debug($"NS {name} Roster Info is Empty");
else
{
Client.Log.Debug($"NS Roster {name}: {rosterInfo}");
}
}
if(!string.IsNullOrEmpty(rosterInfo))
{
var rosterMatches = NameSpaceRosterRegEx().Match(rosterInfo);
this.IsStrongConsistencyMode = rosterMatches.Success
&& rosterMatches.Groups.ContainsKey("roster")
&& !rosterMatches.Groups["roster"].Value.Equals("null");
}
if(Client.Log.DebugEnabled())
{
if(nsConfig is null)
Client.Log.Debug($"NS {name} Config is null");
else if(nsConfig == string.Empty)
Client.Log.Debug($"NS {name} Config is Empty");
else
{
Client.Log.Debug($"NS Config {name}: {nsConfig}");
}
}
if(!string.IsNullOrEmpty(nsConfig))
{
var nsConfigLU = nsConfig.Split(';')
.Select(c => c.Split('='))
.OrderBy(ca => ca[0])
.ToLookup(ca => ca[0]?.Trim(), ca => ca[1].Trim());
if(nsConfigLU.Contains("strong-consistency"))
{
this.IsStrongConsistencyMode = nsConfigLU["strong-consistency"].Contains("true");
}
this.ConfigParams = nsConfigLU;
}
}
public LPNamespace(string name,
string safename,
IEnumerable<string> bins,
IEnumerable<string> safebins,
IEnumerable<LPSet> sets,
IEnumerable<LPSecondaryIndex> sindexes)
{
System.Diagnostics.Debugger.Break();
this.Name = name;
this.SafeName = safename;
this.bins = bins.ToList();
this.safeBins = safebins.ToList();
this.aSets = sets.ToList();
this.SIndexes = sindexes;
LPNamespacesBag.Add(this);
}
/// <summary>
/// The name of the namespace
/// </summary>
public string Name { get; }
/// <summary>
/// The namespace name that is safe to use as a C# class name or property
/// </summary>
public string SafeName { get; }
/// <summary>
/// Gets a value indicating whether this namespace is in strong consistency mode.
/// </summary>
/// <value><c>true</c> if this instance is strong consistency mode; otherwise, <c>false</c>.</value>
public bool IsStrongConsistencyMode { get; }
/// <summary>
/// Gets the configuration parameters for this namespace.
/// </summary>
/// <value>The configuration parameters or null.</value>
public ILookup<string,string> ConfigParams { get; }
private readonly List<string> bins = new List<string>();
/// <summary>
/// The Actual DB Bin Names
/// </summary>
public IEnumerable<string> Bins { get => this.bins; }
private readonly List<string> safeBins = new List<string> ();
/// <summary>
/// Bin names that are safe to use as C# class name or properties.
/// </summary>
public IEnumerable<string> SafeBins { get => this.safeBins; }
private readonly List<LPSet> aSets = new List<LPSet>();
/// <summary>
/// DB Sets
/// </summary>
public IEnumerable<LPSet> Sets { get => this.aSets; }
/// <summary>
/// DB Secondary Indexes
/// </summary>
public IEnumerable<LPSecondaryIndex> SIndexes { get; internal set; } = Enumerable.Empty<LPSecondaryIndex>();
public static IEnumerable<LPNamespace> Create(string staticNamespace, IEnumerable<string> staticSets)
{
var ns = new LPNamespace(staticNamespace);
foreach(var setName in staticSets)
{
var lpSet = new LPSet(ns, setName);
ns.aSets.Add(lpSet);
}
ns.aSets.Add(new LPSet(ns));
return new List<LPNamespace> { ns };
}
public static IEnumerable<LPNamespace> Create(Client.Connection asConnection, Version dbVersion)
{
var setsAttrib = Info.Request(asConnection, "sets");
string GetNSBins(string nsName)
{
if (dbVersion < AerospikeConnection.NoNSBinsRequest)
return Info.Request(asConnection, $"bins/{nsName}");
return string.Empty;
}
string GetNSRoster(string nsName)
{
//roster=2887538337:pending_roster=2887538337:observed_nodes=2887538337
//roster=null:pending_roster=null:observed_nodes=null
if(dbVersion >= AerospikeConnection.NoRosterRequest)
return Info.Request(asConnection, $"roster:namespace={nsName}");
return string.Empty;
}
string GetNSConfig(string nsName)
{
//active-rack=0;allow-ttl-without-nsup=false;auto-revive=false;background-query-max-rps=10000;conflict-resolution-policy=undefined;conflict-resolve-writes=false;default-read-touch-ttl-pct=0;default-ttl=0;disable-cold-start-eviction=false;disable-write-dup-res=false;disallow-expunge=false;disallow-null-setname=false;enable-benchmarks-batch-sub=false;enable-benchmarks-ops-sub=false;enable-benchmarks-read=false;enable-benchmarks-udf=false;enable-benchmarks-udf-sub=false;enable-benchmarks-write=false;enable-hist-proxy=false;evict-hist-buckets=10000;evict-indexes-memory-pct=0;evict-tenths-pct=5;force-long-queries=false;ignore-migrate-fill-d
if(dbVersion >= AerospikeConnection.NoConfigRequest)
return Info.Request(asConnection, $"get-config:context=namespace;namespace={nsName}");
return string.Empty;
}
var asNamespaces = (from nsSets in setsAttrib.Split(';', StringSplitOptions.RemoveEmptyEntries)
let ns = NameSpaceRegEx().Match(nsSets).Groups["namespace"].Value
group nsSets by ns into nsGrp
let nsBins = GetNSBins(nsGrp.Key)
let nsRoster = GetNSRoster(nsGrp.Key)
let nsConfig = GetNSConfig(nsGrp.Key)
select new LPNamespace(nsGrp.Key, nsGrp.ToList(), nsBins, nsRoster, nsConfig)).ToList();
var namespaces = Info.Request(asConnection, "namespaces")?.Split(';');
if(namespaces != null)
{
foreach(var ns in namespaces)
{
if(!asNamespaces.Any(ans => ans.Name == ns))
asNamespaces.Add(new LPNamespace(ns, Enumerable.Empty<string>()));
}
}
return asNamespaces.ToArray();
}
public LPSet TryAddSet(string setName, IEnumerable<LPSet.BinType> binTypes)
{
var fndASet = this.Sets.FirstOrDefault(s => s.Name == setName);
var updateCnt = true;
if(fndASet is null)
{
fndASet = new LPSet(this, setName, binTypes);
this.aSets.Add(fndASet);
Interlocked.Increment(ref nbrCodeUpdates);
updateCnt = false;
}
foreach (var binType in binTypes)
{
this.TryAddBin(binType.BinName, updateCnt);
}
return fndASet;
}
public bool TryAddBin(string binName, bool incUpdateCnt = true)
{
var fndBin = this.Bins.FirstOrDefault(b => b == binName);
if (fndBin is null)
{
this.bins.Add(binName);
this.safeBins.Add(Helpers.CheckName(binName, "Bin"));
if (incUpdateCnt) Interlocked.Increment(ref nbrCodeUpdates);
return true;
}
return false;
}
public LPSet TryRemoveSet(string setName, IEnumerable<LPSet.BinType> removeBinTypes)
{
var fndASet = this.Sets.FirstOrDefault(s => s.Name == setName);
if (fndASet != null)
{
foreach (var removeBinType in removeBinTypes)
{
this.TryRemoveBin(removeBinType.BinName);
}
}
return fndASet;
}
public bool TryRemoveBin(string removeBinName, bool incUpdateCnt = true)
{
var br = this.bins.Remove(removeBinName);
var sb = this.safeBins.Remove(Helpers.CheckName(removeBinName, "Bin"));
if(br || sb)
{
if (incUpdateCnt) Interlocked.Increment(ref nbrCodeUpdates);
return true;
}
return false;
}
internal void DetermineUpdateBinsBasedOnSets()
{
var binTypes = this.Sets
.SelectMany(s => s.BinTypes)
.Distinct(LPSet.BinType.DefaultNameComparer);
if (binTypes.Any())
{
this.bins.Clear();
this.bins.AddRange(binTypes.Select(b => b.BinName));
this.safeBins.Clear();
this.safeBins.AddRange(binTypes.Select(b => Helpers.CheckName(b.BinName, "Bin")));
var nullSet = this.Sets.First(s => s.IsNullSet);
nullSet.UpdateTypeBins(this.bins, false);
}
}
#region Code Generation
public (string classCode, string definePropCode, string createInstanceCode)
CodeCache
{ get; private set; }
internal long nbrCodeUpdates = 0;
public bool CodeNeedsUpdating { get => Interlocked.Read(ref nbrCodeUpdates) > 0; }
/// <summary>
///
/// </summary>
/// <param name="alwaysUseAValues"></param>
/// <param name="forceGeneration"></param>
/// <returns>
/// classCode -- Code used to define this Namespace's class
/// definePropCode -- Code used to define the property used to reference this Namespace
/// createInstanceCode -- Code used to create an instance of this Namespace
/// </returns>
public (string classCode, string definePropCode, string createInstanceCode)
CodeGeneration(bool alwaysUseAValues, bool forceGeneration = false)
{
if (!this.CodeNeedsUpdating && !forceGeneration && this.CodeCache.classCode != null)
return this.CodeCache;
var setProps = new StringBuilder();
var setClasses = new StringBuilder();
var binNames = new StringBuilder();
var generateSetsTask = Task.Run(() =>
{
//Code for getting RecordSets for Set.
foreach (var set in this.Sets)
{
var (setClass, setProp, ignore) = set.CodeGeneration(alwaysUseAValues);
setClasses.AppendLine(setClass);
setProps.AppendLine(setProp);
}
});
var generateBinsTask = Task.Run(() =>
{
foreach (var binName in this.Bins)
{
binNames.Append('"');
binNames.Append(binName);
binNames.Append("\", ");
}
});
Task.WaitAll(generateSetsTask, generateBinsTask);
Interlocked.Exchange(ref nbrCodeUpdates, 0);
var sc = this.IsStrongConsistencyMode ? "true" : "false";
return this.CodeCache = ($@"
public class {this.SafeName}_NamespaceCls : Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess
{{
public {this.SafeName}_NamespaceCls(System.Data.IDbConnection dbConnection)
: base(dbConnection,
Aerospike.Database.LINQPadDriver.LPNamespace.GetNamepsace(""{this.Name}""),
""{this.Name}"",
new string[] {{{binNames}}},
{sc})
{{ }}
public {this.SafeName}_NamespaceCls(Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess clone, Aerospike.Client.Expression expression)
: base(clone, expression)
{{ }}
public {this.SafeName}_NamespaceCls(Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess clone,
Aerospike.Client.Policy readPolicy = null,
Aerospike.Client.WritePolicy writePolicy = null,
Aerospike.Client.QueryPolicy queryPolicy = null,
Aerospike.Client.ScanPolicy scanPolicy = null)
: base(clone,
readPolicy,
writePolicy,
queryPolicy,
scanPolicy)
{{ }}
/// <summary>
/// Initializes a new instance of <see cref=""{this.SafeName}_NamespaceCls""/> as an Aerospike transactional unit.
/// If <see cref=""Commit""/> method is not called the server will abort (rollback) this transaction.
/// </summary>
/// <param name=""baseNS"">Base Namespace instance</param>
/// <param name=""txn"">The Aerospike <see cref=""Aerospike.Client.Txn""/> instance</param>
/// <exception cref=""System.ArgumentNullException"">txn</exception>
/// <exception cref=""System.ArgumentNullException"">clone</exception>
/// <seealso cref=""CreateTransaction""/>
/// <seealso cref=""Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess.Commit""/>
/// <seealso cref=""Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess.Abort""/>
public {this.SafeName}_NamespaceCls({this.SafeName}_NamespaceCls baseNS, Aerospike.Client.Txn txn)
: base(baseNS, txn)
{{ }}
/// <summary>
/// Clones the specified instance providing new policies, if provided.
/// </summary>
/// <param name=""newReadPolicy"">The new read policy.</param>
/// <param name=""newWritePolicy"">The new write policy.</param>
/// <param name=""newQueryPolicy"">The new query policy.</param>
/// <param name=""newScanPolicy"">The new scan policy.</param>
/// <returns>New clone of <see cref=""{this.SafeName}_NamespaceCls""/> instance.</returns>
new public {this.SafeName}_NamespaceCls Clone(Aerospike.Client.Policy newReadPolicy = null,
Aerospike.Client.WritePolicy newWritePolicy = null,
Aerospike.Client.QueryPolicy newQueryPolicy = null,
Aerospike.Client.ScanPolicy newScanPolicy = null)
=> new {this.SafeName}_NamespaceCls(this,
newReadPolicy,
newWritePolicy,
newQueryPolicy,
newScanPolicy);
public {this.SafeName}_NamespaceCls FilterExpression(Aerospike.Client.Expression expression)
{{
return new {this.SafeName}_NamespaceCls(this, expression);
}}
public {this.SafeName}_NamespaceCls FilterExpression(Aerospike.Client.Exp exp)
{{
return new {this.SafeName}_NamespaceCls(this, Aerospike.Client.Exp.Build(exp));
}}
/// <summary>
/// Creates an Aerospike transaction where all operations will be included in this transactional unit.
/// </summary>
/// <param name=""timeout"">
/// MRT timeout in seconds. The timer starts when the MRT monitor record is created.
/// This occurs when the first command in the MRT is executed. If the timeout is reached before
/// a commit or abort is called, the server will expire and rollback the MRT.
/// Defaults to 10 seconds.
/// </param>
/// <returns>Transaction Namespace instance</returns>
/// <seealso cref=""Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess.Commit""/>
/// <seealso cref=""Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess.Abort""/>
new public {this.SafeName}_NamespaceCls CreateTransaction(int timeout = 10) => new(this, new Aerospike.Client.Txn() {{ Timeout = timeout }});
public IAerospikeClient ASClient() => this.AerospikeConnection.AerospikeClient;
{setClasses}
{setProps}
}}",
//Code to access namespace properties.
$@"
public static {this.SafeName}_NamespaceCls {this.SafeName} {{get; private set; }}",
//Code to construct namespace instance
$@"
{this.SafeName} = new {this.SafeName}_NamespaceCls(dbConnection);"
);
}
#endregion
#region Explorer
public ExplorerItem CreateExplorerItem()
{
var isSC = this.IsStrongConsistencyMode ? "-SC" : string.Empty;
var sc = this.IsStrongConsistencyMode ? " Strong Consistency " : string.Empty;
var children = this.Sets.Select(s => s.CreateExplorerItem()).ToList();
if(this.ConfigParams is not null && this.ConfigParams.Any())
{
children.Add(new ExplorerItem("Configuration",
ExplorerItemKind.Category,
ExplorerIcon.Box)
{
IsEnumerable = false,
DragText = null,
Children = this.ConfigParams
.Select(c => c.Key + '=' + string.Join(",", c))
.Select(s => new ExplorerItem(s, ExplorerItemKind.Parameter, ExplorerIcon.ScalarFunction))
.ToList()
});
}
return new ExplorerItem($"{this.Name} ({this.Sets.Count()}{isSC})",
ExplorerItemKind.Property,
ExplorerIcon.Table)
{
IsEnumerable = false,
DragText = this.SafeName,
Children = children,
ToolTipText = $"Sets associated with{sc}namespace \"{this.Name}\""
};
}
#endregion
public override string ToString()
{
return this.Name;
}
public static LPNamespace GetNamepsace(string namespaceName) => LPNamespacesBag.FirstOrDefault(n => n.Name == namespaceName);
private string DebuggerDisplay => $"{Name} {Sets.Count()} {Bins.Count()} {this.IsStrongConsistencyMode}";
}
}