-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetSetBins.cs
176 lines (150 loc) · 6.56 KB
/
GetSetBins.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
using Aerospike.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Aerospike.Database.LINQPadDriver.Extensions;
namespace Aerospike.Database.LINQPadDriver
{
internal sealed class GetSetBins
{
public GetSetBins(IAerospikeClient connection, int timeout, bool compression)
{
this.Connection = connection;
this.QueryPolicy = new QueryPolicy() { socketTimeout = timeout, totalTimeout = 0, compress = compression };
}
IAerospikeClient Connection { get; }
/// <summary>
/// <see href="https://docs.aerospike.com/apidocs/csharp/html/t_aerospike_client_querypolicy"/>
/// </summary>
QueryPolicy QueryPolicy { get; }
public Exception LastException { get; private set; }
IEnumerable<Aerospike.Client.Record> GetRecords(string nsName, string setName, int maxRecords)
{
this.QueryPolicy.expectedDuration = maxRecords <= 100 ? QueryDuration.LONG : QueryDuration.SHORT;
using var recordset = this.Connection
.Query(this.QueryPolicy,
new Statement() { Namespace = nsName, SetName = setName, MaxRecords = maxRecords });
while (recordset.Next())
{
yield return recordset.Record;
}
}
static int IsDoc(object value)
{
if (value is IDictionary<object, object> dict)
{
if (dict.All(d => d.Key is string))
return 1;
}
else if (value is IList<object> list)
{
return list.Sum(i => IsDoc(i)) == list.Count ? 1 : 0;
}
return 0;
}
static Type GetdocType(object value, bool determineDocType)
{
if (value is null) return typeof(object);
if (!value.GetType().IsGenericType)
{
if(determineDocType && value is Value.GeoJSONValue geoObj)
{
if(!string.IsNullOrEmpty(geoObj.value))
try
{
return GeoJSONHelpers.ConvertToGeoJson(geoObj)
.GetType();
}
catch
{ }
return typeof(Value.GeoJSONValue);
}
return value.GetType();
}
try
{
if (value is IDictionary<object, object> dict)
{
if (dict.All(d => d.Key is string))
{
if (determineDocType)
return typeof(JsonDocument);
var valueType = GetdocType(dict.Values.ToList(), determineDocType);
return value.GetType()
.GetGenericTypeDefinition()
.MakeGenericType(typeof(string),
valueType.GetGenericArguments().First());
}
else
{
var keyType = GetdocType(dict.Keys.ToList(), determineDocType);
var valueType = GetdocType(dict.Values.ToList(), determineDocType);
return value.GetType()
.GetGenericTypeDefinition()
.MakeGenericType(keyType.GetGenericArguments().First(),
valueType.GetGenericArguments().First());
}
}
else if (value is IList<object> lst)
{
var typeLst = new List<Type>();
foreach (var item in lst)
{
//if (!item.GetType().IsGenericType) return value.GetType();
typeLst.Add(item is null ? typeof(object) : GetdocType(item, determineDocType));
}
var commonType = typeLst.GroupBy(i => i).Select(i => i.Key);
if (commonType.Count() == 1)
{
return value.GetType()
.GetGenericTypeDefinition()
.MakeGenericType(commonType.First());
}
}
}
catch { }
return value.GetType();
}
#pragma warning disable IDE0060 // Remove unused parameter
private static (string name, Type type) GetBinDocType(string nsName, string setName, string binName, object binValue, bool determineDocType)
#pragma warning restore IDE0060 // Remove unused parameter
{
return (binName, GetdocType(binValue, determineDocType));
}
public List<LPSet.BinType> Get(string nsName,
string setName,
bool determineDocType,
int maxRecords,
int minRecs)
{
if (maxRecords > 0)
try
{
this.LastException = null;
var records = GetRecords(nsName, setName, maxRecords);
var nbrRecs = records.Count();
if (nbrRecs >= minRecs)
{
return records
.SelectMany(r => r.bins.Select(b => GetBinDocType(nsName, setName, b.Key, b.Value, determineDocType)))
.GroupBy(x => x)
.Select(y => (y.Key.name, y.Key.type, y.Count()))
.GroupBy(y => y.name)
.SelectMany(x => x.Select(i => new LPSet.BinType(i.name, i.type, x.Count() > 1, x.Sum(y => y.Item3) >= nbrRecs))).ToList();
}
}
catch(Exception ex)
{
this.LastException = ex;
if (Client.Log.DebugEnabled())
{
Client.Log.Error($"GetSetBins.Get Exception {ex.GetType().Name} ({ex.Message})");
DynamicDriver.WriteToLog(ex, "GetSetBins.Get");
}
}
return new List<LPSet.BinType>(0);
}
}
}