forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnDb.cs
More file actions
176 lines (139 loc) · 6.55 KB
/
ColumnDb.cs
File metadata and controls
176 lines (139 loc) · 6.55 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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Buffers;
using System.Collections.Generic;
using Nethermind.Core;
using Nethermind.Core.Buffers;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using RocksDbSharp;
using IWriteBatch = Nethermind.Core.IWriteBatch;
namespace Nethermind.Db.Rocks;
public class ColumnDb : IDb, ISortedKeyValueStore, IMergeableKeyValueStore, IKeyValueStoreWithSnapshot
{
private readonly RocksDb _rocksDb;
internal readonly DbOnTheRocks _mainDb;
internal readonly ColumnFamilyHandle _columnFamily;
private readonly DbOnTheRocks.IteratorManager _iteratorManager;
private readonly RocksDbReader _reader;
public ColumnDb(RocksDb rocksDb, DbOnTheRocks mainDb, string name)
{
_rocksDb = rocksDb;
_mainDb = mainDb;
if (name == "Default") name = "default";
_columnFamily = _rocksDb.GetColumnFamily(name);
Name = name;
_iteratorManager = new DbOnTheRocks.IteratorManager(_rocksDb, _columnFamily, _mainDb._readAheadReadOptions);
_reader = new RocksDbReader(mainDb, mainDb.CreateReadOptions, _iteratorManager, _columnFamily);
}
public void Dispose() => _iteratorManager.Dispose();
public string Name { get; }
byte[]? IReadOnlyKeyValueStore.Get(ReadOnlySpan<byte> key, ReadFlags flags) => _reader.Get(key, flags);
Span<byte> IReadOnlyKeyValueStore.GetSpan(scoped ReadOnlySpan<byte> key, ReadFlags flags) => _reader.GetSpan(key, flags);
MemoryManager<byte>? IReadOnlyKeyValueStore.GetOwnedMemory(ReadOnlySpan<byte> key, ReadFlags flags)
{
Span<byte> span = ((IReadOnlyKeyValueStore)this).GetSpan(key, flags);
return span.IsNullOrEmpty() ? null : new DbSpanMemoryManager(this, span);
}
int IReadOnlyKeyValueStore.Get(scoped ReadOnlySpan<byte> key, Span<byte> output, ReadFlags flags) => _reader.Get(key, output, flags);
bool IReadOnlyKeyValueStore.KeyExists(ReadOnlySpan<byte> key) => _reader.KeyExists(key);
void IReadOnlyKeyValueStore.DangerousReleaseMemory(in ReadOnlySpan<byte> key) => _reader.DangerousReleaseMemory(key);
public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None) =>
_mainDb.SetWithColumnFamily(key, _columnFamily, value, flags);
public void PutSpan(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, WriteFlags writeFlags = WriteFlags.None) =>
_mainDb.SetWithColumnFamily(key, _columnFamily, value, writeFlags);
public void Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, WriteFlags writeFlags = WriteFlags.None) =>
_mainDb.MergeWithColumnFamily(key, _columnFamily, value, writeFlags);
public KeyValuePair<byte[], byte[]?>[] this[byte[][] keys]
{
get
{
ColumnFamilyHandle[] columnFamilies = new ColumnFamilyHandle[keys.Length];
Array.Fill(columnFamilies, _columnFamily);
return _rocksDb.MultiGet(keys, columnFamilies);
}
}
public IEnumerable<KeyValuePair<byte[], byte[]?>> GetAll(bool ordered = false)
{
Iterator iterator = _mainDb.CreateIterator(ordered, _columnFamily);
return _mainDb.GetAllCore(iterator);
}
public IEnumerable<byte[]> GetAllKeys(bool ordered = false)
{
Iterator iterator = _mainDb.CreateIterator(ordered, _columnFamily);
return _mainDb.GetAllKeysCore(iterator);
}
public IEnumerable<byte[]> GetAllValues(bool ordered = false)
{
Iterator iterator = _mainDb.CreateIterator(ordered, _columnFamily);
return _mainDb.GetAllValuesCore(iterator);
}
public IWriteBatch StartWriteBatch() => new ColumnsDbWriteBatch(this, (DbOnTheRocks.RocksDbWriteBatch)_mainDb.StartWriteBatch());
private class ColumnsDbWriteBatch(ColumnDb columnDb, DbOnTheRocks.RocksDbWriteBatch underlyingWriteBatch)
: IWriteBatch
{
public void Dispose() => underlyingWriteBatch.Dispose();
public void Clear() => underlyingWriteBatch.Clear();
public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None)
{
if (value is null)
{
underlyingWriteBatch.Delete(key, columnDb._columnFamily);
}
else
{
underlyingWriteBatch.Set(key, value, columnDb._columnFamily, flags);
}
}
public void PutSpan(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, WriteFlags flags = WriteFlags.None) =>
underlyingWriteBatch.Set(key, value, columnDb._columnFamily, flags);
public void Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, WriteFlags flags = WriteFlags.None) =>
underlyingWriteBatch.Merge(key, value, columnDb._columnFamily, flags);
}
public void Remove(ReadOnlySpan<byte> key) => Set(key, null);
public void Flush(bool onlyWal) => _mainDb.FlushWithColumnFamily(_columnFamily);
public void Compact() =>
_rocksDb.CompactRange(Keccak.Zero.BytesToArray(), Keccak.MaxValue.BytesToArray(), _columnFamily);
/// <summary>
/// Not sure how to handle delete of the columns DB
/// </summary>
/// <exception cref="NotSupportedException"></exception>
public void Clear() => throw new NotSupportedException();
// Maybe it should be column-specific metric?
public IDbMeta.DbMetric GatherMetric() => _mainDb.GatherMetric();
public byte[]? FirstKey
{
get
{
using Iterator iterator = _mainDb.CreateIterator(_mainDb.CreateReadOptions(), ch: _columnFamily);
iterator.SeekToFirst();
return iterator.Valid() ? iterator.GetKeySpan().ToArray() : null;
}
}
public byte[]? LastKey
{
get
{
using Iterator iterator = _mainDb.CreateIterator(_mainDb.CreateReadOptions(), ch: _columnFamily);
iterator.SeekToLast();
return iterator.Valid() ? iterator.GetKeySpan().ToArray() : null;
}
}
public ISortedView GetViewBetween(ReadOnlySpan<byte> firstKey, ReadOnlySpan<byte> lastKey) =>
_mainDb.GetViewBetween(firstKey, lastKey, _columnFamily);
public IKeyValueStoreSnapshot CreateSnapshot()
{
Snapshot snapshot = _rocksDb.CreateSnapshot();
return new DbOnTheRocks.RocksDbSnapshot(
_mainDb,
() =>
{
ReadOptions readOptions = _mainDb.CreateReadOptions();
readOptions.SetSnapshot(snapshot);
return readOptions;
},
_columnFamily,
snapshot);
}
}