-
Notifications
You must be signed in to change notification settings - Fork 1
/
LPSecondaryIndex.cs
153 lines (134 loc) · 6.94 KB
/
LPSecondaryIndex.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
using Aerospike.Client;
using Aerospike.Database.LINQPadDriver.Extensions;
using LINQPad.Extensibility.DataContext;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
namespace Aerospike.Database.LINQPadDriver
{
[System.Diagnostics.DebuggerDisplay("{DebuggerDisplay}")]
public sealed partial class LPSecondaryIndex : ILPExplorer
{
public LPSecondaryIndex(string name,
LPNamespace aNamespace,
LPSet set,
string bin,
string type,
string indexType,
string context)
{
this.Name = name;
this.SafeName = Helpers.CheckName(name, "Idx");
this.Namespace = aNamespace;
this.Set = set;
this.Bin = bin;
this.Type = type;
this.IndexType = indexType;
this.Context = context == "null" ? null : context;
}
/// <summary>
/// ns=test:indexname=State_index:set=players:bin=State:type=string:indextype=default:context=null:state=RW;
/// ns=test:indexname=idx_list_map_bin_subobj:set=expressionExp:bin=map_bin:type=numeric:indextype=mapvalues:context=[list_value(*):state=RW
/// </summary>
#if NET7_0_OR_GREATER
[GeneratedRegex("ns=(?<namespace>[^:;]+):indexname=(?<indexname>[^:;]+):set=(?<setname>[^:;]+):bin=(?<binname>[^:;]+):type=(?<type>[^:;]+):indextype=(?<indextype>[^:;]+):context=(?<context>[^:;]+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase)]
static private partial Regex IdxRegEx();
#else
static private readonly Regex idxRegEx = new Regex("ns=(?<namespace>[^:;]+):indexname=(?<indexname>[^:;]+):set=(?<setname>[^:;]+):bin=(?<binname>[^:;]+):type=(?<type>[^:;]+):indextype=(?<indextype>[^:;]+):context=(?<context>[^:;]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static private Regex IdxRegEx() => idxRegEx;
#endif
/// <summary>
/// The name of the DB Secondary Index name
/// </summary>
public string Name { get; }
/// <summary>
/// The safe name of the index that can be used in a C# class name or property
/// </summary>
public string SafeName { get; }
/// <summary>
/// Associated namespace
/// </summary>
public LPNamespace Namespace { get; }
/// <summary>
/// Associated Set
/// </summary>
public LPSet Set { get; }
/// <summary>
/// The bin used to maintain the secondary index
/// </summary>
public string Bin { get; }
/// <summary>
/// The bin DB data type
/// </summary>
public string Type { get; }
public string IndexType { get; }
public string Context { get; }
public static IEnumerable<LPSecondaryIndex> Create(Client.Connection asConnection, IEnumerable<LPNamespace> namespaces)
{
LPSet FindSet(LPNamespace ns, string set)
{
return ns?.Sets?.FirstOrDefault(s => s.Name == set);
}
var idxsAttrib = Info.Request(asConnection, "sindex");
var idxs = (from nsSetIdx in idxsAttrib.Split(';', StringSplitOptions.RemoveEmptyEntries)
let match = IdxRegEx().Match(nsSetIdx)
let ns = match.Groups["namespace"].Value
let set = match.Groups["setname"].Value
let bin = match.Groups["binname"].Value
let idxName = match.Groups["indexname"].Value
let type = match.Groups["type"].Value
let idxType = match.Groups["indextype"].Value
let context = match.Groups["context"].Value
let aNamespace = namespaces.FirstOrDefault(n => n.Name == ns)
let aSet = FindSet(aNamespace,
set == "NULL" || string.IsNullOrEmpty(set)
? LPSet.NullSetName
: set)
select new LPSecondaryIndex(idxName,
aNamespace,
aSet,
bin,
type,
idxType,
context));
return idxs.ToArray();
}
//Aerospike.Database.LINQPadDriver.Extensions.ARecord
public string GenerateCode(Type idxDataType, bool useARecord)
{
if(useARecord)
return $@"
public Aerospike.Database.LINQPadDriver.Extensions.ASecondaryIndexAccess {this.SafeName}
{{ get => new Aerospike.Database.LINQPadDriver.Extensions.ASecondaryIndexAccess(this, ""{this.Name}"", ""{this.Bin}"", ""{this.Type}"", ""{this.IndexType}""); }}
";
return $@"
public Aerospike.Database.LINQPadDriver.Extensions.ASecondaryIndexAccess<RecordCls> {this.SafeName}
{{ get => new Aerospike.Database.LINQPadDriver.Extensions.ASecondaryIndexAccess<RecordCls>(this, ""{this.Name}"", ""{this.Bin}"", ""{this.Type}"", ""{this.IndexType}"", typeof({Helpers.GetRealTypeName(idxDataType ?? typeof(AValue))})); }}
";
}
public ExplorerItem CreateExplorerItem()
{
static string DetermineContext(string context) => string.IsNullOrEmpty(context) ? string.Empty : ":" + context;
return new ExplorerItem($"{this.Name} ({this.Bin})",
ExplorerItemKind.QueryableObject,
ExplorerIcon.Key)
{
IsEnumerable = true,
DragText = $"{this.Namespace.SafeName}.{this.Set.SafeName}.{this.SafeName}",
Children = new List<ExplorerItem>() { new ExplorerItem($"{this.Bin} ({this.Type}:{this.IndexType}{DetermineContext(this.Context)})",
ExplorerItemKind.Schema,
ExplorerIcon.Column)
{ DragText = this.Bin }
}
};
}
public override string ToString()
{
return this.Name;
}
private string DebuggerDisplay => $"{Namespace}.{Set}.{Bin}.{Name}";
}
}