-
Notifications
You must be signed in to change notification settings - Fork 1
/
LPNamespace.cs
409 lines (333 loc) · 14.7 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
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();
#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 Regex SetNameRegEx() => setNameRegEx;
static private Regex NameSpaceRegEx() => nameSpaceRegEx;
#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)
: 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();
}
public LPNamespace(string name,
string safename,
IEnumerable<string> bins,
IEnumerable<string> safebins,
IEnumerable<LPSet> sets,
IEnumerable<LPSecondaryIndex> sindexes)
{
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; }
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;
}
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)
select new LPNamespace(nsGrp.Key, nsGrp.ToList(), nsBins)).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));
}
}
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);
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}}})
{{ }}
public {this.SafeName}_NamespaceCls(Aerospike.Database.LINQPadDriver.Extensions.ANamespaceAccess clone, Aerospike.Client.Expression expression)
: base(clone, expression)
{{ }}
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));
}}
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()
{
return new ExplorerItem($"{this.Name} ({this.Sets.Count()})",
ExplorerItemKind.Property,
ExplorerIcon.Table)
{
IsEnumerable = false,
DragText = this.SafeName,
Children = this.Sets.Select(s => s.CreateExplorerItem()).ToList(),
ToolTipText = $"Sets associated with 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()}";
}
}