forked from sones/sones
/
SonesIndex.cs
454 lines (391 loc) · 13.8 KB
/
SonesIndex.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using sones.Plugins.Index.Helper;
using sones.Library.PropertyHyperGraph;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using sones.Plugins.Index.ErrorHandling;
using sones.Library.VersionedPluginManager;
using System.Threading;
using sones.Plugins.Index.Abstract;
namespace sones.Plugins.Index
{
/// <summary>
/// This index is the default implementation of
/// a sones GraphDB index.
///
/// This in-memory index wraps a .NET Concurrent
/// Dictionary to fulfill the methods.
/// It represents a multiple-value index where
/// 1-n values can be associated with a search key.
/// </summary>
public class SonesIndex : ASonesIndex, IPluginable
{
#region Private Members
/// <summary>
/// internal index structure
/// </summary>
private readonly ConcurrentDictionary<IComparable, HashSet<Int64>> _Index;
#region Counts
/// <summary>
/// Number of stored values
/// </summary>
private Int64 _ValueCount;
#endregion
#region Helper
/// <summary>
/// used for some thread safe actions
/// </summary>
private object _Lock;
#endregion
//#region PropertyID
///// <summary>
///// The ID of the indexed property
///// </summary>
//private Int64 _PropertyID;
//#endregion
#endregion
#region Properties
public override bool SupportsNullableKeys
{
get
{
return false;
}
}
#endregion
#region Constructor
/// <summary>
/// Creates an instance of the SonesIndex
/// </summary>
public SonesIndex()
{
_Index = new ConcurrentDictionary<IComparable, HashSet<Int64>>();
_ValueCount = 0;
_Lock = new object();
}
public SonesIndex(IList<Int64> myPropertyIDs) : this()
{
//_PropertyID = myPropertyIDs.FirstOrDefault();
_PropertyIDs = myPropertyIDs;
}
#endregion
#region ISonesIndex Members
/// <summary>
/// Returns the name of the index
/// </summary>
public override string IndexName
{
get { return "sonesindex"; }
}
/// <summary>
/// Returns the number of keys in the index.
/// </summary>
/// <returns>Number of keys</returns>
public override long KeyCount()
{
return _Index.Keys.LongCount();
}
/// <summary>
/// Returns the number of values in the index.
/// </summary>
/// <returns>Number of values</returns>
public override long ValueCount()
{
return _ValueCount;
}
/// <summary>
/// Returns all keys currently stored in the index
/// </summary>
/// <returns>All stored keys</returns>
public override ICloseableEnumerable<IComparable> Keys()
{
return new CloseableEnumerable<IComparable>(_Index.Keys);
}
/// <summary>
/// Returns the type of the stored keys or <code>IComparable</code>
/// if the index is empty.
/// </summary>
/// <returns>
/// The type of the stored keys or <code>IComparable</code>
/// if the index is empty.
/// </returns>
public override Type GetKeyType()
{
if (!_Index.IsEmpty)
{
return _Index.First().GetType();
}
return typeof(IComparable);
}
/// <summary>
/// Adds a given key and a vertexID to the index.
/// </summary>
/// <param name="myKey">Search key</param>
/// <param name="myVertexID">Associated vertexID</param>
/// <param name="myIndexAddStrategy">Define what happens if key exists</param>
public override void Add(IComparable myKey, long? myVertexID,
IndexAddStrategy myIndexAddStrategy = IndexAddStrategy.MERGE)
{
if (!KeyNullSupportCheck(myKey))
{
throw new NullKeysNotSupportedException("This index does not support >null< as key.");
}
HashSet<Int64> values;
HashSet<Int64> newValues;
if (myVertexID == null) newValues = new HashSet<long>();
else newValues = new HashSet<long>() { (Int64)myVertexID };
lock (_Lock)
{
int valueCountDiff = 0;
if (_Index.TryGetValue(myKey, out values))
{
// subtract number of old values
valueCountDiff -= values.Count;
switch (myIndexAddStrategy)
{
case IndexAddStrategy.MERGE:
values.UnionWith(newValues);
valueCountDiff += values.Count;
break;
case IndexAddStrategy.REPLACE:
values = newValues;
valueCountDiff++;
break;
case IndexAddStrategy.UNIQUE:
throw new IndexKeyExistsException(String.Format("Index key {0} already exist.", myKey.ToString()));
}
}
else
{
values = newValues;
valueCountDiff += newValues.Count;
}
// add it to the index
_Index.AddOrUpdate(myKey, values, (k, v) => values);
// update the value count
_ValueCount += valueCountDiff;
}
}
/// <summary>
/// Returns all values associated with given key, if it exists.
/// </summary>
/// <param name="myKey">Search key</param>
/// <param name="myVertexIDs">Associated values</param>
/// <returns>True, if the key exists</returns>
public override bool TryGetValues(IComparable myKey, out ICloseableEnumerable<long> myVertexIDs)
{
if (!KeyNullSupportCheck(myKey))
{
throw new NullKeysNotSupportedException("This index does not support >null< as key.");
}
var values = new HashSet<long>();
var done = _Index.TryGetValue(myKey, out values);
if (values == null)
{
myVertexIDs = null;
}
else
{
myVertexIDs = new CloseableEnumerable<long>(values);
}
return done;
}
/// <summary>
/// Returns all values for the given key or throws a
/// <code>IndexKeyNotFoundException</code> if the key
/// is not stored in the index.
/// If the key is null, null will be returned.
/// </summary>
/// <param name="myKey">Search key</param>
/// <returns>All values associated to the key.</returns>
public override ICloseableEnumerable<long> this[IComparable myKey]
{
get
{
if (!KeyNullSupportCheck(myKey))
{
throw new NullKeysNotSupportedException("This index does not support >null< as key.");
}
HashSet<Int64> values;
if (_Index.TryGetValue(myKey, out values))
{
return new CloseableEnumerable<Int64>(values);
}
else
{
throw new IndexKeyNotFoundException(String.Format("Index key {0} not found.", myKey.ToString()));
}
}
}
/// <summary>
/// Checks, if a key exists in the index.
/// </summary>
/// <param name="myKey"></param>
/// <returns></returns>
public override bool ContainsKey(IComparable myKey)
{
if (!KeyNullSupportCheck(myKey))
{
throw new NullKeysNotSupportedException("This index does not support >null< as key.");
}
return _Index.ContainsKey(myKey);
}
#region Remove / RemoveRange / TryRemoveValue
/// <summary>
/// Removes a key from the index.
/// </summary>
/// <param name="myKey"></param>
public override bool Remove(IComparable myKey)
{
if (!KeyNullSupportCheck(myKey))
{
throw new NullKeysNotSupportedException("This index does not support >null< as key.");
}
HashSet<Int64> values;
if (!_Index.TryRemove(myKey, out values))
{
return false;
}
else
{
lock (_Lock)
{
// update value count
_ValueCount -= values.Count;
}
return true;
}
}
/// <summary>
/// Removes a value associated with the given key
/// from the index.
/// </summary>
/// <param name="myKey">Search key</param>
/// <param name="myValue">Value to be removed.</param>
/// <returns>True, if the value has been removed.</returns>
public override bool TryRemoveValue(IComparable myKey, Int64 myValue)
{
if (!KeyNullSupportCheck(myKey))
{
throw new NullKeysNotSupportedException("This index does not support >null< as key.");
}
HashSet<Int64> values;
lock (_Lock)
{
// get values from internal index
if (_Index.TryGetValue(myKey, out values))
{
// if the defined value is stored
if (values.Contains(myValue))
{
// then remove it
values.Remove(myValue);
// decrement the value counter
_ValueCount--;
// and if there are still values associated with the key
if (values.Count > 0)
{
// update the value set
_Index.AddOrUpdate(myKey, values, (key, vals) => vals = values);
}
else
{
// or remove the key
return Remove(myKey);
}
return true;
}
}
}
return false;
}
/// <summary>
/// Removes all given key from the index.
/// </summary>
/// <param name="myKeys">Search keys to be removed</param>
public override void RemoveRange(IEnumerable<IComparable> myKeys)
{
Parallel.ForEach(myKeys, k => Remove(k));
}
#endregion
/// <summary>
/// Internal index structure doesn't support optimizations.
/// </summary>
public override void Optimize()
{
// silence is golden
}
#endregion
#region IPluginable Members
/// <summary>
/// Plugin name of this index.
/// </summary>
public string PluginName
{
get { return "sones.sonesindex"; }
}
/// <summary>
/// Plugin short name of this index.
/// </summary>
public string PluginShortName
{
get { return "sonesidx"; }
}
/// <summary>
/// Plugin short name of this index.
/// </summary>
public string PluginDescription
{
get { return "This index is the default implementation of a sones GraphDB index."; }
}
/// <summary>
/// Paramters which can be set for that index.
/// </summary>
public PluginParameters<Type> SetableParameters
{
get
{
return new PluginParameters<Type>
{
};
}
}
/// <summary>
/// Returns a new instance of that class.
/// </summary>
/// <param name="UniqueString"></param>
/// <param name="myParameters"></param>
/// <returns>A new instance of SonesIndex</returns>
public IPluginable InitializePlugin(string UniqueString, Dictionary<string, object> myParameters = null)
{
object propertyIDs;
if (myParameters.TryGetValue(IndexConstants.PROPERTY_IDS_OPTIONS_KEY, out propertyIDs))
{
return new SonesIndex((IList<Int64>) propertyIDs);
}
else
{
return new SonesIndex();
}
}
/// <summary>
/// Frees all ressources used by the index
/// </summary>
public void Dispose()
{}
#endregion
#region Clear
/// <summary>
/// Clears internal index
/// </summary>
public override void Clear()
{
_Index.Clear();
Interlocked.Exchange(ref _ValueCount, 0L);
}
#endregion
}
}