-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathColumnsDb.cs
More file actions
284 lines (234 loc) · 10.4 KB
/
ColumnsDb.cs
File metadata and controls
284 lines (234 loc) · 10.4 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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using FastEnumUtility;
using Nethermind.Core;
using Nethermind.Db.Rocks.Config;
using Nethermind.Logging;
using RocksDbSharp;
using IWriteBatch = Nethermind.Core.IWriteBatch;
namespace Nethermind.Db.Rocks;
public class ColumnsDb<T> : DbOnTheRocks, IColumnsDb<T> where T : struct, Enum
{
private readonly IDictionary<T, ColumnDb> _columnDbs = new Dictionary<T, ColumnDb>();
// Cached for ColumnDbSnapshot to avoid per-snapshot recomputation.
// Initialized once on first CreateSnapshot call; both fields are idempotent (same result from any thread).
private volatile T[]? _cachedColumnKeys;
private volatile int _cachedMaxOrdinal = -1;
public ColumnsDb(string basePath, DbSettings settings, IDbConfig dbConfig, IRocksDbConfigFactory rocksDbConfigFactory, ILogManager logManager, IReadOnlyList<T> keys, IntPtr? sharedCache = null)
: this(basePath, settings, dbConfig, rocksDbConfigFactory, logManager, ResolveKeys(keys), sharedCache)
{
}
private ColumnsDb(string basePath, DbSettings settings, IDbConfig dbConfig, IRocksDbConfigFactory rocksDbConfigFactory, ILogManager logManager, (IReadOnlyList<T> Keys, IList<string> ColumnNames) keyInfo, IntPtr? sharedCache)
: base(basePath, settings, dbConfig, rocksDbConfigFactory, logManager, keyInfo.ColumnNames, sharedCache: sharedCache)
{
foreach (T key in keyInfo.Keys)
{
_columnDbs[key] = new ColumnDb(_db, this, key.ToString()!);
}
}
protected override long FetchTotalPropertyValue(string propertyName)
{
long total = 0;
foreach (KeyValuePair<T, ColumnDb> kv in _columnDbs)
{
long value = long.TryParse(_db.GetProperty(propertyName, kv.Value._columnFamily), out long parsedValue)
? parsedValue
: 0;
total += value;
}
return total;
}
public override void Compact()
{
foreach (T key in ColumnKeys)
{
_columnDbs[key].Compact();
}
}
private static IReadOnlyList<T> GetEnumKeys(IReadOnlyList<T> keys)
{
if (typeof(T).IsEnum && keys.Count == 0)
{
keys = FastEnum.GetValues<T>().ToArray();
}
return keys;
}
private static (IReadOnlyList<T> Keys, IList<string> ColumnNames) ResolveKeys(IReadOnlyList<T> keys)
{
IReadOnlyList<T> resolvedKeys = GetEnumKeys(keys);
IList<string> columnNames = resolvedKeys.Select(static key => key.ToString()).ToList();
return (resolvedKeys, columnNames);
}
protected override void BuildOptions<TOptions>(IRocksDbConfig dbConfig, Options<TOptions> options, IntPtr? sharedCache, IMergeOperator? mergeOperator)
{
base.BuildOptions(dbConfig, options, sharedCache, mergeOperator);
options.SetCreateMissingColumnFamilies();
}
public IDb GetColumnDb(T key) => _columnDbs[key];
public IEnumerable<T> ColumnKeys => _columnDbs.Keys;
public IReadOnlyColumnDb<T> CreateReadOnly(bool createInMemWriteStore)
{
return new ReadOnlyColumnsDb<T>(this, createInMemWriteStore);
}
public new IColumnsWriteBatch<T> StartWriteBatch()
{
return new RocksColumnsWriteBatch(this);
}
protected override void ApplyOptions(IDictionary<string, string> options)
{
string[] keys = options.Select<KeyValuePair<string, string>, string>(static e => e.Key).ToArray();
string[] values = options.Select<KeyValuePair<string, string>, string>(static e => e.Value).ToArray();
foreach (KeyValuePair<T, ColumnDb> cols in _columnDbs)
{
_rocksDbNative.rocksdb_set_options_cf(_db.Handle, cols.Value._columnFamily.Handle, keys.Length, keys, values);
}
base.ApplyOptions(options);
}
private class RocksColumnsWriteBatch : IColumnsWriteBatch<T>
{
internal readonly RocksDbWriteBatch WriteBatch;
private readonly ColumnsDb<T> _columnsDb;
public RocksColumnsWriteBatch(ColumnsDb<T> columnsDb)
{
WriteBatch = new RocksDbWriteBatch(columnsDb);
_columnsDb = columnsDb;
}
public IWriteBatch GetColumnBatch(T key) => new RocksColumnWriteBatch(_columnsDb._columnDbs[key], this);
public void Clear() => WriteBatch.Clear();
public void Dispose() => WriteBatch.Dispose();
}
private class RocksColumnWriteBatch : IWriteBatch
{
private readonly ColumnDb _column;
private readonly RocksColumnsWriteBatch _writeBatch;
public RocksColumnWriteBatch(ColumnDb column, RocksColumnsWriteBatch writeBatch)
{
_column = column;
_writeBatch = writeBatch;
}
public void Dispose()
{
_writeBatch.Dispose();
}
public void Clear()
{
_writeBatch.WriteBatch.Clear();
}
public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None)
{
_writeBatch.WriteBatch.Set(key, value, _column._columnFamily, flags);
}
public void Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, WriteFlags flags = WriteFlags.None)
{
_writeBatch.WriteBatch.Merge(key, value, _column._columnFamily, flags);
}
}
IColumnDbSnapshot<T> IColumnsDb<T>.CreateSnapshot()
{
Snapshot snapshot = _db.CreateSnapshot();
return new ColumnDbSnapshot(this, snapshot);
}
private class ColumnDbSnapshot : IColumnDbSnapshot<T>
{
private readonly Snapshot _snapshot;
private readonly ReadOptions _sharedReadOptions;
private readonly ReadOptions _sharedCacheMissReadOptions;
private int _disposed;
// Use a flat array indexed by enum ordinal instead of Dictionary<T, IReadOnlyKeyValueStore>.
// This eliminates the dictionary + backing array allocation per snapshot.
private readonly RocksDbReader[] _readers;
public ColumnDbSnapshot(ColumnsDb<T> columnsDb, Snapshot snapshot)
{
_snapshot = snapshot;
// Create two shared ReadOptions for all column readers instead of 2 per reader.
// ReadOptions in RocksDbSharp has a finalizer but no IDisposable — creating many
// short-lived instances causes Gen1/Gen2 GC pressure from finalizer queue buildup.
_sharedReadOptions = CreateReadOptions(columnsDb, snapshot);
_sharedCacheMissReadOptions = CreateReadOptions(columnsDb, snapshot);
_sharedCacheMissReadOptions.SetFillCache(false);
// Single shared delegate for GetViewBetween — avoids per-reader closure allocation.
// Note: each GetViewBetween call still creates a new ReadOptions with a finalizer;
// that is pre-existing behavior not addressed by this PR.
Func<ReadOptions> readOptionsFactory = () => CreateReadOptions(columnsDb, snapshot);
T[] keys = CreateKeyCache(columnsDb);
columnsDb._cachedMaxOrdinal = GetCachedMaxOrdinal(columnsDb, keys);
_readers = CreateReaders();
static ReadOptions CreateReadOptions(ColumnsDb<T> columnsDb, Snapshot snapshot)
{
ReadOptions options = new ReadOptions();
options.SetVerifyChecksums(columnsDb.VerifyChecksum);
options.SetSnapshot(snapshot);
return options;
}
// Cache column keys and max ordinal on the parent ColumnsDb to avoid per-snapshot
// recomputation. The race is benign (both threads compute identical results) and
// volatile ensures visibility across cores.
static T[] CreateKeyCache(ColumnsDb<T> columnsDb)
{
T[]? keys = columnsDb._cachedColumnKeys;
if (keys is null)
{
IDictionary<T, ColumnDb> columnDbs = columnsDb._columnDbs;
keys = new T[columnDbs.Count];
int idx = 0;
foreach (T key in columnDbs.Keys)
{
keys[idx++] = key;
}
columnsDb._cachedColumnKeys = keys;
}
return keys;
}
static int GetCachedMaxOrdinal(ColumnsDb<T> columnsDb, T[] keys)
{
if (columnsDb._cachedMaxOrdinal >= 0) return columnsDb._cachedMaxOrdinal;
int max = 0;
for (int i = 0; i < keys.Length; i++)
{
max = Math.Max(max, EnumToInt(keys[i]));
}
return max;
}
// Build flat array of readers indexed by column ordinal
RocksDbReader[] CreateReaders()
{
RocksDbReader[] readers = new RocksDbReader[columnsDb._cachedMaxOrdinal + 1];
for (int i = 0; i < keys.Length; i++)
{
T k = keys[i];
readers[EnumToInt(k)] = new RocksDbReader(
columnsDb,
_sharedReadOptions,
_sharedCacheMissReadOptions,
readOptionsFactory,
columnFamily: columnsDb._columnDbs[k]._columnFamily);
}
return readers;
}
}
public IReadOnlyKeyValueStore GetColumn(T key)
{
ObjectDisposedException.ThrowIf(_disposed != 0, this);
return _readers[EnumToInt(key)];
}
public void Dispose()
{
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;
// Explicitly destroy native ReadOptions handles to prevent finalizer queue buildup.
// GC.SuppressFinalize prevents the finalizer from running on already-destroyed handles.
DestroyReadOptions(_sharedReadOptions);
DestroyReadOptions(_sharedCacheMissReadOptions);
_snapshot.Dispose();
}
private static void DestroyReadOptions(ReadOptions options)
{
Native.Instance.rocksdb_readoptions_destroy(options.Handle);
GC.SuppressFinalize(options);
}
private static int EnumToInt(T value) => Convert.ToInt32(value);
}
}