-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathStringDictionary.cs
More file actions
759 lines (664 loc) · 23.1 KB
/
StringDictionary.cs
File metadata and controls
759 lines (664 loc) · 23.1 KB
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#define ASSERT_LOCKS
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Waher.Persistence.Serialization;
using Waher.Persistence.Files.Storage;
using Waher.Runtime.Collections;
namespace Waher.Persistence.Files
{
/// <summary>
/// This class manages a string dictionary in a persisted file.
/// </summary>
public class StringDictionary : IPersistentDictionary
{
private readonly Dictionary<string, object> inMemory;
private ObjectBTreeFile dictionaryFile;
private StringDictionaryRecords recordHandler;
private readonly KeyValueSerializer keyValueSerializer;
private readonly GenericObjectSerializer genericSerializer;
private readonly FilesProvider provider;
private readonly Encoding encoding;
private readonly string collectionName;
private readonly int timeoutMilliseconds;
/// <summary>
/// This class manages a string dictionary in a persisted file.
/// </summary>
/// <param name="CollectionName">Collection Name.</param>
/// <param name="Provider">Files provider.</param>
/// <param name="RetainInMemory">Retain the dictionary in memory.</param>
private StringDictionary(string CollectionName, FilesProvider Provider, bool RetainInMemory)
{
this.provider = Provider;
this.collectionName = CollectionName;
this.encoding = this.provider.Encoding;
this.timeoutMilliseconds = this.provider.TimeoutMilliseconds;
this.genericSerializer = new GenericObjectSerializer(this.provider);
this.keyValueSerializer = new KeyValueSerializer(this.provider, this.genericSerializer);
this.recordHandler = new StringDictionaryRecords(this.collectionName, this.encoding, this.genericSerializer, this.provider);
if (RetainInMemory)
this.inMemory = new Dictionary<string, object>();
else
this.inMemory = null;
}
/// <summary>
/// This class manages a string dictionary in a persisted file.
/// </summary>
/// <param name="FileName">File name of index file.</param>
/// <param name="BlobFileName">Name of file in which BLOBs are stored.</param>
/// <param name="CollectionName">Collection Name.</param>
/// <param name="Provider">Files provider.</param>
/// <param name="RetainInMemory">Retain the dictionary in memory.</param>
public static async Task<StringDictionary> Create(string FileName, string BlobFileName, string CollectionName, FilesProvider Provider, bool RetainInMemory)
{
StringDictionary Result = new StringDictionary(CollectionName, Provider, RetainInMemory);
Result.dictionaryFile = await ObjectBTreeFile.Create(FileName, Result.collectionName, BlobFileName,
Result.provider.BlockSize, Result.provider.BlobBlockSize, Result.provider, Result.encoding, Result.timeoutMilliseconds,
Result.provider.Encrypted, Result.recordHandler, null);
Provider.Register(Result);
return Result;
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public void Dispose()
{
this.provider.Unregister(this);
this.dictionaryFile?.Dispose();
this.dictionaryFile = null;
this.recordHandler = null;
}
/// <summary>
/// Determines whether the System.Collections.Generic.IDictionary{string,object} contains an element with the specified key.
/// </summary>
/// <param name="key">The key to locate in the System.Collections.Generic.IDictionary{string,object}.</param>
/// <returns>true if the System.Collections.Generic.IDictionary{string,object} contains an element with the key; otherwise, false.</returns>
public bool ContainsKey(string key)
{
Task<bool> Task = this.ContainsKeyAsync(key);
FilesProvider.Wait(Task, this.timeoutMilliseconds);
return Task.Result;
}
/// <summary>
/// Determines whether the System.Collections.Generic.IDictionary{string,object} contains an element with the specified key.
/// </summary>
/// <param name="key">The key to locate in the System.Collections.Generic.IDictionary{string,object}.</param>
/// <returns>true if the System.Collections.Generic.IDictionary{string,object} contains an element with the key; otherwise, false.</returns>
public async Task<bool> ContainsKeyAsync(string key)
{
if (!(this.inMemory is null))
{
lock (this.inMemory)
{
if (this.inMemory.ContainsKey(key))
return true;
}
}
if (this.dictionaryFile is null)
return false;
await this.dictionaryFile.BeginRead();
try
{
BlockInfo Info = await this.dictionaryFile.FindNodeLocked(key, false);
return !(Info is null);
}
finally
{
await this.dictionaryFile.EndRead();
}
}
/// <summary>
/// Adds an element with the provided key and value to the System.Collections.Generic.IDictionary{string,object}.
/// </summary>
/// <param name="key">The object to use as the key of the element to add.</param>
/// <param name="value">The object to use as the value of the element to add.</param>
/// <exception cref="ArgumentNullException">key is null</exception>
/// <exception cref="ArgumentException">An element with the same key already exists in the System.Collections.Generic.IDictionary{string,object}.</exception>
public void Add(string key, object value)
{
FilesProvider.Wait(this.AddAsync(key, value, false), this.timeoutMilliseconds);
}
/// <summary>
/// Adds an element with the provided key and value to the System.Collections.Generic.IDictionary{string,object}.
/// </summary>
/// <param name="key">The object to use as the key of the element to add.</param>
/// <param name="value">The object to use as the value of the element to add.</param>
/// <exception cref="ArgumentNullException">key is null</exception>
/// <exception cref="ArgumentException">An element with the same key already exists in the System.Collections.Generic.IDictionary{string,object}.</exception>
public Task AddAsync(string key, object value)
{
return this.AddAsync(key, value, false);
}
/// <summary>
/// Adds an element with the provided key and value to the System.Collections.Generic.IDictionary{string,object}.
/// </summary>
/// <param name="key">The object to use as the key of the element to add.</param>
/// <param name="value">The object to use as the value of the element to add.</param>
/// <param name="ReplaceIfExists">If replacement of any existing value is desired.</param>
/// <exception cref="ArgumentNullException">key is null</exception>
/// <exception cref="ArgumentException">An element with the same key already exists in the System.Collections.Generic.IDictionary{string,object}.</exception>
public async Task AddAsync(string key, object value, bool ReplaceIfExists)
{
if (key is null)
throw new ArgumentNullException("key is null.", "key");
Type Type = value?.GetType() ?? typeof(object);
IObjectSerializer Serializer = await this.provider.GetObjectSerializer(Type);
await this.dictionaryFile.BeginWrite();
try
{
byte[] Bin = await this.SerializeLocked(key, value, Serializer);
BlockInfo Info;
if (ReplaceIfExists)
{
Info = await this.dictionaryFile.FindNodeLocked(key, true);
if (Info.Match)
await this.dictionaryFile.ReplaceObjectLocked(Bin, Info, true);
else
await this.dictionaryFile.SaveNewObjectLocked(Bin, Info);
}
else
{
Info = await this.dictionaryFile.FindLeafNodeLocked(key);
if (Info is null)
throw new ArgumentException("A key with that value already exists.", nameof(key));
await this.dictionaryFile.SaveNewObjectLocked(Bin, Info);
}
}
finally
{
await this.dictionaryFile.EndWrite();
}
if (!(this.inMemory is null))
{
lock (this.inMemory)
{
this.inMemory[key] = value;
}
}
}
/// <summary>
/// Serializes a (Key,Value) pair.
/// </summary>
/// <param name="Key">Key</param>
/// <param name="Value">Value</param>
/// <param name="Serializer">Serializer.</param>
/// <returns>Serialized record.</returns>
private async Task<byte[]> SerializeLocked(string Key, object Value, IObjectSerializer Serializer)
{
BinarySerializer Writer = new BinarySerializer(this.collectionName, this.encoding);
Writer.WriteBit(true);
Writer.Write(Key);
await Serializer.Serialize(Writer, true, true, Value,
NestedLocks.CreateIfNested(this.dictionaryFile, true, Serializer));
return Writer.GetSerialization();
}
/// <summary>
/// Removes the element with the specified key from the System.Collections.IDictionary object.
/// </summary>
/// <param name="key">The key of the element to remove.</param>
/// <returns>If key was found and removed.</returns>
/// <exception cref="ArgumentNullException">key is null.</exception>
public bool Remove(string key)
{
Task<bool> Task = this.RemoveAsync(key);
FilesProvider.Wait(Task, this.timeoutMilliseconds);
return Task.Result;
}
/// <summary>
/// Removes the element with the specified key from the System.Collections.IDictionary object.
/// </summary>
/// <param name="key">The key of the element to remove.</param>
/// <returns>If key was found and removed.</returns>
/// <exception cref="ArgumentNullException">key is null.</exception>
public async Task<bool> RemoveAsync(string key)
{
if (key is null)
throw new ArgumentNullException("key is null.", "key");
if (!(this.inMemory is null))
{
lock (this.inMemory)
{
this.inMemory.Remove(key);
}
}
object DeletedObject;
await this.dictionaryFile.BeginWrite();
try
{
DeletedObject = await this.dictionaryFile.DeleteObjectLocked(key, false, true, this.keyValueSerializer, null, 0);
}
catch (KeyNotFoundException)
{
return false;
}
finally
{
await this.dictionaryFile.EndWrite();
}
return !(DeletedObject is null);
}
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <param name="key">The key whose value to get.</param>
/// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise,
/// the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
/// <returns>true if the object that implements System.Collections.Generic.IDictionary{string,object} contains an element with the specified key;
/// otherwise, false.</returns>
/// <exception cref="ArgumentNullException">key is null.</exception>
public bool TryGetValue(string key, out object value)
{
Task<KeyValuePair<bool, object>> Task = this.TryGetValueAsync(key);
FilesProvider.Wait(Task, this.timeoutMilliseconds);
KeyValuePair<bool, object> P = Task.Result;
value = P.Value;
return P.Key;
}
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <param name="key">The key whose value to get.</param>
/// <returns>Returns a pair of values:
///
/// First value is true if the object that implements System.Collections.Generic.IDictionary{string,object} contains an element
/// with the specified key; otherwise, false.
/// When this method returns, the second value associated with the specified key, if the key is found; otherwise,
/// the default value for the type of the value parameter. This parameter is passed uninitialized.</returns>
/// <exception cref="ArgumentNullException">key is null.</exception>
public async Task<KeyValuePair<bool, object>> TryGetValueAsync(string key)
{
if (key is null)
throw new ArgumentNullException("key is null.", "key");
if (!(this.inMemory is null))
{
lock (this.inMemory)
{
if (this.inMemory.TryGetValue(key, out object value))
return new KeyValuePair<bool, object>(true, value);
}
}
await this.dictionaryFile.BeginRead();
try
{
object Result = await this.dictionaryFile.TryLoadObjectLocked(key, this.keyValueSerializer);
if (Result is null)
return new KeyValuePair<bool, object>(false, null);
else if (Result is KeyValuePair<string, object> P)
return new KeyValuePair<bool, object>(P.Key == key, P.Value);
else
return new KeyValuePair<bool, object>(true, Result);
}
finally
{
await this.dictionaryFile.EndRead();
}
}
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <param name="key">The key whose value to get.</param>
/// <returns>Returns the value associated with the specified key, if the key is found.</returns>
/// <exception cref="ArgumentNullException">key is null.</exception>
/// <exception cref="KeyNotFoundException">If <paramref name="key"/> was not found.</exception>
public async Task<KeyValuePair<string, object>> GetValueAsync(string key)
{
if (key is null)
throw new ArgumentNullException("key is null.", "key");
if (!(this.inMemory is null))
{
lock (this.inMemory)
{
if (this.inMemory.TryGetValue(key, out object value))
return new KeyValuePair<string, object>(key, value);
}
}
await this.dictionaryFile.BeginRead();
try
{
object Obj = await this.dictionaryFile.LoadObjectLocked(key, this.keyValueSerializer);
if (Obj is KeyValuePair<string, object> P)
return P;
else
return new KeyValuePair<string, object>(key, Obj);
}
finally
{
await this.dictionaryFile.EndRead();
}
}
/// <summary>
/// <see cref="ICollection{T}.Add(T)"/>
/// </summary>
public void Add(KeyValuePair<string, object> item)
{
this.Add(item.Key, item.Value);
}
/// <summary>
/// <see cref="ICollection{T}.Clear()"/>
/// </summary>
public void Clear()
{
FilesProvider.Wait(this.ClearAsync(), this.timeoutMilliseconds);
}
/// <summary>
/// Clears the dictionary.
/// </summary>
public async Task ClearAsync()
{
if (!(this.dictionaryFile is null))
await this.dictionaryFile.ClearAsync();
if (!(this.inMemory is null))
{
lock (this.inMemory)
{
this.inMemory.Clear();
}
}
}
/// <summary>
/// <see cref="ICollection{T}.Contains(T)"/>
/// </summary>
public bool Contains(KeyValuePair<string, object> item)
{
if (!this.TryGetValue(item.Key, out object Value))
return false;
if (Value is null)
return item.Value is null;
else
return Value.Equals(item.Value);
}
/// <summary>
/// <see cref="ICollection{T}.CopyTo(T[], int)"/>
/// </summary>
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
Task Task = this.CopyToAsync(array, arrayIndex);
FilesProvider.Wait(Task, this.timeoutMilliseconds);
}
/// <summary>
/// Copies the contents of the dicitionary to an array.
/// </summary>
/// <param name="array">Array</param>
/// <param name="arrayIndex">Start index</param>
public async Task CopyToAsync(KeyValuePair<string, object>[] array, int arrayIndex)
{
await this.dictionaryFile.BeginRead();
try
{
ObjectBTreeFileCursor<KeyValuePair<string, object>> e = await this.GetEnumeratorLocked();
while (await e.MoveNextAsyncLocked())
array[arrayIndex++] = e.Current;
}
finally
{
await this.dictionaryFile.EndRead();
}
}
/// <summary>
/// Loads the entire table and returns it as an array.
/// </summary>
/// <returns>Array of key-value pairs.</returns>
public KeyValuePair<string, object>[] ToArray()
{
Task<KeyValuePair<string, object>[]> Task = this.ToArrayAsync();
FilesProvider.Wait(Task, this.timeoutMilliseconds);
return Task.Result;
}
/// <summary>
/// Loads the entire table and returns it as an array.
/// </summary>
/// <returns>Array of key-value pairs.</returns>
public async Task<KeyValuePair<string, object>[]> ToArrayAsync()
{
await this.dictionaryFile.BeginRead();
try
{
ChunkedList<KeyValuePair<string, object>> Result = new ChunkedList<KeyValuePair<string, object>>();
ObjectBTreeFileCursor<KeyValuePair<string, object>> e = await this.GetEnumeratorLocked();
while (await e.MoveNextAsyncLocked())
Result.Add(e.Current);
return Result.ToArray();
}
finally
{
await this.dictionaryFile.EndRead();
}
}
/// <summary>
/// <see cref="ICollection{T}.Remove(T)"/>
/// </summary>
public bool Remove(KeyValuePair<string, object> item)
{
if (!this.Contains(item))
return false;
else
return this.Remove(item.Key);
}
/// <summary>
/// <see cref="IEnumerable{T}.GetEnumerator"/>
/// </summary>
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
ObjectBTreeFileCursor<KeyValuePair<string, object>> Cursor = this.GetCursor();
CursorEnumerator<KeyValuePair<string, object>> e = new CursorEnumerator<KeyValuePair<string, object>>(Cursor, this.ResetCursor, this.timeoutMilliseconds);
return e;
}
/// <summary>
/// <see cref="IEnumerable.GetEnumerator"/>
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
ObjectBTreeFileCursor<KeyValuePair<string, object>> Cursor = this.GetCursor();
CursorEnumerator<KeyValuePair<string, object>> e = new CursorEnumerator<KeyValuePair<string, object>>(Cursor, this.ResetCursor, this.timeoutMilliseconds);
return e;
}
private ObjectBTreeFileCursor<KeyValuePair<string, object>> GetCursor()
{
Task<ObjectBTreeFileCursor<KeyValuePair<string, object>>> Task = this.GetCursorAsync();
FilesProvider.Wait(Task, this.timeoutMilliseconds);
return Task.Result;
}
private async Task<ObjectBTreeFileCursor<KeyValuePair<string, object>>> GetCursorAsync()
{
ObjectBTreeFileCursor<KeyValuePair<string, object>> Result;
try
{
await this.dictionaryFile.BeginRead();
Result = await this.GetEnumeratorLocked();
Result.readLock = true;
}
catch (Exception ex)
{
await this.dictionaryFile.EndRead();
ExceptionDispatchInfo.Capture(ex).Throw();
Result = null;
}
return Result;
}
/// <summary>
/// Gets an enumerator for all entries in the dictionary.
/// </summary>
/// <returns>Enumerator</returns>
public Task<ObjectBTreeFileCursor<KeyValuePair<string, object>>> GetEnumeratorLocked()
{
#if ASSERT_LOCKS
this.dictionaryFile.fileAccess.AssertReadingOrWriting();
#endif
return ObjectBTreeFileCursor<KeyValuePair<string, object>>.CreateLocked(this.dictionaryFile, this.recordHandler, this.keyValueSerializer);
}
private ICursor<KeyValuePair<string, object>> ResetCursor(ICursor<KeyValuePair<string, object>> Cursor)
{
if (Cursor is ObjectBTreeFileCursor<KeyValuePair<string, object>> e)
e.GoToFirstLocked().Wait();
return Cursor;
}
/// <summary>
/// Index file.
/// </summary>
public ObjectBTreeFile DictionaryFile => this.dictionaryFile;
/// <summary>
/// Name of corresponding collection name.
/// </summary>
public string CollectionName => this.collectionName;
/// <summary>
/// Encoding to use for text properties.
/// </summary>
public Encoding Encoding => this.encoding;
/// <summary>
/// <see cref="IDictionary{TKey, TValue}.Keys"/>
/// </summary>
public ICollection<string> Keys
{
get
{
if (this.keyCollection is null)
this.keyCollection = new KeyCollection(this);
return this.keyCollection;
}
}
private KeyCollection keyCollection = null;
/// <summary>
/// <see cref="IDictionary{TKey, TValue}.Values"/>
/// </summary>
public ICollection<object> Values
{
get
{
if (this.valueCollection is null)
this.valueCollection = new ValueCollection(this);
return this.valueCollection;
}
}
private ValueCollection valueCollection = null;
/// <summary>
/// <see cref="ICollection{T}.Count"/>
/// </summary>
public int Count
{
get
{
return (int)this.dictionaryFile.CountAsync.Result;
}
}
/// <summary>
/// <see cref="ICollection{T}.IsReadOnly"/>
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Gets or sets the element with the specified key.
/// </summary>
/// <param name="key">The key of the element to get or set.</param>
/// <returns>The element with the specified key.</returns>
/// <exception cref="ArgumentNullException">key is null.</exception>
/// <exception cref="KeyNotFoundException">The property is retrieved and key is not found.</exception>
public object this[string key]
{
get
{
Task<KeyValuePair<string, object>> Task = this.GetValueAsync(key);
FilesProvider.Wait(Task, this.timeoutMilliseconds);
return Task.Result.Value;
}
set
{
FilesProvider.Wait(this.AddAsync(key, value, true), this.timeoutMilliseconds);
}
}
/// <summary>
/// Deletes the dictionary and disposes the object.
/// </summary>
public void DeleteAndDispose()
{
if (!(this.dictionaryFile is null))
{
string FileName = this.dictionaryFile.FileName;
string BlobFileName = this.dictionaryFile.BlobFileName;
this.Dispose();
File.Delete(FileName);
File.Delete(BlobFileName);
}
}
/// <summary>
/// Gets a range of entries from one key to another.
/// </summary>
/// <param name="FromKey">Inclusive limit. If null, all keys to <paramref name="ToKey"/> are included.</param>
/// <param name="ToKey">Exclusive limit. If null, all keys from <paramref name="FromKey"/> are included.</param>
/// <returns>Found entries.</returns>
public async Task<KeyValuePair<string, object>[]> GetEntriesAsync(string FromKey, string ToKey)
{
await this.dictionaryFile.BeginRead();
try
{
ICursor<KeyValuePair<string, object>> Entries;
if (FromKey is null)
Entries = await this.GetEnumeratorLocked();
else
{
BlockInfo Info = await this.dictionaryFile.FindNodeLocked(FromKey, true);
ObjectBTreeFileCursor<KeyValuePair<string, object>> e =
await ObjectBTreeFileCursor<KeyValuePair<string, object>>.CreateLocked(this.dictionaryFile, this.recordHandler, this.keyValueSerializer);
e.SetStartingPoint(Info);
Entries = e;
}
ChunkedList<KeyValuePair<string, object>> Result = new ChunkedList<KeyValuePair<string, object>>();
if (ToKey is null)
{
while (await Entries.MoveNextAsyncLocked())
Result.Add(Entries.Current);
}
else
{
while (await Entries.MoveNextAsyncLocked())
{
if (string.Compare(Entries.Current.Key, ToKey, false) >= 0)
break;
Result.Add(Entries.Current);
}
}
return Result.ToArray();
}
finally
{
await this.dictionaryFile.EndRead();
}
}
/// <summary>
/// Copies available keys to a string array.
/// </summary>
/// <param name="Keys">Array to receive keys.</param>
/// <param name="Offset">Offset into array to start copying to.</param>
public Task CopyKeysToAsync(string[] Keys, int Offset)
{
return ((KeyCollection)this.Keys).CopyToAsync(Keys, Offset);
}
/// <summary>
/// Copies available values to an array.
/// </summary>
/// <param name="Values">Array to receive values.</param>
/// <param name="Offset">Offset into array to start copying to.</param>
public Task CopyValuesToAsync(object[] Values, int Offset)
{
return ((ValueCollection)this.Values).CopyToAsync(Values, Offset);
}
/// <summary>
/// Gets all keys.
/// </summary>
/// <returns>Array of keys.</returns>
public Task<string[]> GetKeysAsync()
{
return ((KeyCollection)this.Keys).GetKeysAsync();
}
/// <summary>
/// Gets all values.
/// </summary>
/// <returns>Array of values.</returns>
public Task<object[]> GetValuesAsync()
{
return ((ValueCollection)this.Values).GetValuesAsync();
}
}
}