-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryKeyValueStore.cs
81 lines (61 loc) · 2.64 KB
/
InMemoryKeyValueStore.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Compression;
using System.Threading.Tasks;
namespace DataCore.Adapter.Services {
/// <summary>
/// <see cref="IKeyValueStore"/> implementation that uses a <see cref="ConcurrentDictionary{TKey, TValue}"/>
/// to store values.
/// </summary>
/// <remarks>
/// This implementation does not provide any persistence of data.
/// </remarks>
public sealed class InMemoryKeyValueStore : KeyValueStore<KeyValueStoreOptions>, IDisposable {
/// <summary>
/// The value store.
/// </summary>
private readonly ConcurrentDictionary<string, byte[]> _values = new ConcurrentDictionary<string, byte[]>(StringComparer.Ordinal);
/// <summary>
/// Creats a new <see cref="InMemoryKeyValueStore"/> object.
/// </summary>
public InMemoryKeyValueStore(CompressionLevel compressionLevel = CompressionLevel.NoCompression)
: base(new KeyValueStoreOptions() { CompressionLevel = compressionLevel }, null) { }
/// <inheritdoc/>
protected override ValueTask WriteAsync(KVKey key, byte[] value) {
var keyAsString = System.Text.Encoding.UTF8.GetString(key);
_values[keyAsString] = value;
return default;
}
/// <inheritdoc/>
protected override ValueTask<byte[]?> ReadAsync(KVKey key) {
var keyAsString = System.Text.Encoding.UTF8.GetString(key);
if (!_values.TryGetValue(keyAsString, out var value)) {
return new ValueTask<byte[]?>((byte[]?) null);
}
return new ValueTask<byte[]?>(value);
}
/// <inheritdoc/>
protected override ValueTask<bool> DeleteAsync(KVKey key) {
var keyAsString = System.Text.Encoding.UTF8.GetString(key);
return new ValueTask<bool>(_values.TryRemove(keyAsString, out _));
}
/// <inheritdoc/>
protected override async IAsyncEnumerable<KVKey> GetKeysAsync(KVKey? prefix) {
await Task.Yield();
var utf8Prefix = prefix == null || prefix.Value.Length == 0
? null
: System.Text.Encoding.UTF8.GetString(prefix.Value);
foreach (var item in _values.Keys) {
if (utf8Prefix != null && !item.StartsWith(utf8Prefix, StringComparison.Ordinal)) {
continue;
}
yield return System.Text.Encoding.UTF8.GetBytes(item);
};
}
/// <inheritdoc/>
public void Dispose() {
_values.Clear();
}
}
}