forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadOnlyDb.cs
More file actions
87 lines (64 loc) · 2.99 KB
/
ReadOnlyDb.cs
File metadata and controls
87 lines (64 loc) · 2.99 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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using Nethermind.Core;
using Nethermind.Core.Buffers;
namespace Nethermind.Db
{
public class ReadOnlyDb(IDb wrappedDb, bool createInMemWriteStore) : IReadOnlyDb
{
private readonly MemDb _memDb = new();
public void Dispose() => _memDb.Dispose();
public string Name { get => wrappedDb.Name; }
public byte[]? Get(ReadOnlySpan<byte> key, ReadFlags flags = ReadFlags.None) =>
_memDb.Get(key, flags) ?? wrappedDb.Get(key, flags);
public void Set(ReadOnlySpan<byte> key, byte[]? value, WriteFlags flags = WriteFlags.None)
{
if (!createInMemWriteStore)
{
throw new InvalidOperationException($"This {nameof(ReadOnlyDb)} did not expect any writes.");
}
_memDb.Set(key, value, flags);
}
public KeyValuePair<byte[], byte[]>[] this[byte[][] keys]
{
get
{
KeyValuePair<byte[], byte[]>[]? result = wrappedDb[keys];
KeyValuePair<byte[], byte[]>[]? memResult = _memDb[keys];
for (int i = 0; i < memResult.Length; i++)
{
KeyValuePair<byte[], byte[]> memValue = memResult[i];
if (memValue.Value is not null)
{
result[i] = memValue;
}
}
return result;
}
}
public IEnumerable<KeyValuePair<byte[], byte[]>> GetAll(bool ordered = false) => _memDb.GetAll().Union(wrappedDb.GetAll());
public IEnumerable<byte[]> GetAllKeys(bool ordered = false) => _memDb.GetAllKeys().Union(wrappedDb.GetAllKeys());
public IEnumerable<byte[]> GetAllValues(bool ordered = false) => _memDb.GetAllValues().Union(wrappedDb.GetAllValues());
public IWriteBatch StartWriteBatch() => this.LikeABatch();
public IDbMeta.DbMetric GatherMetric() => wrappedDb.GatherMetric();
public void Remove(ReadOnlySpan<byte> key) { }
public bool KeyExists(ReadOnlySpan<byte> key) => _memDb.KeyExists(key) || wrappedDb.KeyExists(key);
public void Flush(bool onlyWal) { }
public void Clear() => throw new InvalidOperationException();
public virtual void ClearTempChanges() => _memDb.Clear();
public void PutSpan(ReadOnlySpan<byte> keyBytes, ReadOnlySpan<byte> value, WriteFlags writeFlags = WriteFlags.None)
{
if (!createInMemWriteStore)
{
throw new InvalidOperationException($"This {nameof(ReadOnlyDb)} did not expect any writes.");
}
_memDb.Set(keyBytes, value.ToArray(), writeFlags);
}
public void DangerousReleaseMemory(in ReadOnlySpan<byte> span) { }
public bool PreferWriteByArray => true; // Because of memdb buffer
}
}