Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/TestMemColumnDb.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using Nethermind.Db;

Expand Down Expand Up @@ -30,6 +31,12 @@ public IColumnsWriteBatch<TKey> StartWriteBatch()
{
return new InMemoryColumnWriteBatch<TKey>(this);
}

public IColumnDbSnapshot<TKey> CreateSnapshot()
{
throw new NotSupportedException("Snapshot not implemented");
}

public void Dispose() { }
public void Flush(bool onlyWal = false) { }
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Db.Rocks/ColumnDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void Compact()
public void Clear() { throw new NotSupportedException(); }

// Maybe it should be column specific metric?
public IDbMeta.DbMetric GatherMetric(bool includeSharedCache = false) => _mainDb.GatherMetric(includeSharedCache);
public IDbMeta.DbMetric GatherMetric() => _mainDb.GatherMetric();

public byte[]? FirstKey
{
Expand Down
33 changes: 33 additions & 0 deletions src/Nethermind/Nethermind.Db.Rocks/ColumnsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,37 @@ public void Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, WriteFlags f
_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(
ColumnsDb<T> columnsDb,
Snapshot snapshot
) : IColumnDbSnapshot<T>
{
private readonly Dictionary<T, IReadOnlyKeyValueStore> _columnDbs = columnsDb.ColumnKeys.ToDictionary(k => k, k =>
(IReadOnlyKeyValueStore)new RocksDbReader(
columnsDb,
() =>
{
ReadOptions options = new ReadOptions();
options.SetSnapshot(snapshot);
return options;
},
columnFamily: columnsDb._columnDbs[k]._columnFamily));

public IReadOnlyKeyValueStore GetColumn(T key)
{
return _columnDbs[key];
}

public void Dispose()
{
snapshot.Dispose();
}
}
}
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.Db.Rpc/RpcColumnsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public IColumnsWriteBatch<T> StartWriteBatch()
{
return new InMemoryColumnWriteBatch<T>(this);
}

public IColumnDbSnapshot<T> CreateSnapshot()
{
throw new NotSupportedException("Snapshot not implemented");
}

public void Dispose() { }
public void Flush(bool onlyWal = false) { }
}
Expand Down
17 changes: 17 additions & 0 deletions src/Nethermind/Nethermind.Db.Test/ColumnsDbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,21 @@ public void TestWriteBatch_WriteToAllColumn()
_db.GetColumnDb(ReceiptsColumns.Transactions).Get(TestItem.KeccakA).Should()
.BeEquivalentTo(TestItem.KeccakB.BytesToArray());
}

[Test]
public void SmokeTest_Snapshot()
{
IColumnsDb<ReceiptsColumns> asColumnsDb = _db;
IDb colA = _db.GetColumnDb(ReceiptsColumns.Blocks);

colA.Set(TestItem.KeccakA, TestItem.KeccakA.BytesToArray());

using IColumnDbSnapshot<ReceiptsColumns> snapshot = asColumnsDb.CreateSnapshot();

colA.Set(TestItem.KeccakA, TestItem.KeccakB.BytesToArray());
colA.Get(TestItem.KeccakA).Should().BeEquivalentTo(TestItem.KeccakB.BytesToArray());

snapshot.GetColumn(ReceiptsColumns.Blocks)
.Get(TestItem.KeccakA).Should().BeEquivalentTo(TestItem.KeccakA.BytesToArray());
}
}
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Db/IColumnsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ public interface IColumnsDb<TKey> : IDbMeta, IDisposable
IEnumerable<TKey> ColumnKeys { get; }
public IReadOnlyColumnDb<TKey> CreateReadOnly(bool createInMemWriteStore) => new ReadOnlyColumnsDb<TKey>(this, createInMemWriteStore);
IColumnsWriteBatch<TKey> StartWriteBatch();
IColumnDbSnapshot<TKey> CreateSnapshot();
}

public interface IColumnsWriteBatch<in TKey> : IDisposable
{
IWriteBatch GetColumnBatch(TKey key);
}


public interface IColumnDbSnapshot<in TKey> : IDisposable
{
IReadOnlyKeyValueStore GetColumn(TKey key);
}
}
8 changes: 8 additions & 0 deletions src/Nethermind/Nethermind.Db/MemColumnsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Nethermind.Core;

namespace Nethermind.Db
{
Expand Down Expand Up @@ -38,6 +40,12 @@ public IColumnsWriteBatch<TKey> StartWriteBatch()
{
return new InMemoryColumnWriteBatch<TKey>(this);
}

public IColumnDbSnapshot<TKey> CreateSnapshot()
{
throw new NotSupportedException("Snapshot not supported");
}

public void Dispose() { }
public void Flush(bool onlyWal = false) { }
}
Expand Down
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Db/ReadOnlyColumnsDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace Nethermind.Db
public class ReadOnlyColumnsDb<T> : IReadOnlyColumnDb<T>, IDisposable
{
private readonly IDictionary<T, IReadOnlyDb> _readOnlyColumns;
private readonly IColumnsDb<T> _baseColumnDb;

public ReadOnlyColumnsDb(IColumnsDb<T> baseColumnDb, bool createInMemWriteStore)
{
_baseColumnDb = baseColumnDb;
_readOnlyColumns = baseColumnDb.ColumnKeys
.Select(key => (key, db: baseColumnDb.GetColumnDb(key).CreateReadOnly(createInMemWriteStore)))
.ToDictionary(it => it.key, it => it.db);
Expand All @@ -29,6 +31,11 @@ public IColumnsWriteBatch<T> StartWriteBatch()
return new InMemoryColumnWriteBatch<T>(this);
}

public IColumnDbSnapshot<T> CreateSnapshot()
{
return _baseColumnDb.CreateSnapshot();
}

public void ClearTempChanges()
{
foreach (KeyValuePair<T, IReadOnlyDb> readOnlyColumn in _readOnlyColumns)
Expand Down