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
6 changes: 3 additions & 3 deletions src/Plugins/RocksDBStore/Plugins/Storage/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ namespace Neo.Plugins.Storage
public static class Options
{
public static readonly DbOptions Default = CreateDbOptions();
public static readonly ReadOptions ReadDefault = new ReadOptions();
public static readonly WriteOptions WriteDefault = new WriteOptions();
public static readonly ReadOptions ReadDefault = new();
public static readonly WriteOptions WriteDefault = new();
public static readonly WriteOptions WriteDefaultSync = new WriteOptions().SetSync(true);

public static DbOptions CreateDbOptions()
{
DbOptions options = new DbOptions();
var options = new DbOptions();
options.SetCreateMissingColumnFamilies(true);
options.SetCreateIfMissing(true);
options.SetErrorIfExists(false);
Expand Down
47 changes: 24 additions & 23 deletions src/Plugins/RocksDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using RocksDbSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Neo.Plugins.Storage
{
Expand All @@ -21,43 +22,43 @@ namespace Neo.Plugins.Storage
/// </summary>
internal class Snapshot : ISnapshot
{
private readonly RocksDb db;
private readonly RocksDbSharp.Snapshot snapshot;
private readonly WriteBatch batch;
private readonly ReadOptions options;
private readonly RocksDb _db;
private readonly RocksDbSharp.Snapshot _snapshot;
private readonly WriteBatch _batch;
private readonly ReadOptions _options;

public Snapshot(RocksDb db)
{
this.db = db;
snapshot = db.CreateSnapshot();
batch = new WriteBatch();
_db = db;
_snapshot = db.CreateSnapshot();
_batch = new WriteBatch();

options = new ReadOptions();
options.SetFillCache(false);
options.SetSnapshot(snapshot);
_options = new ReadOptions();
_options.SetFillCache(false);
_options.SetSnapshot(_snapshot);
}

public void Commit()
{
db.Write(batch, Options.WriteDefault);
_db.Write(_batch, Options.WriteDefault);
}

public void Delete(byte[] key)
{
batch.Delete(key);
_batch.Delete(key);
}

public void Put(byte[] key, byte[] value)
{
batch.Put(key, value);
_batch.Put(key, value);
}

/// <inheritdoc/>
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction)
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[]? keyOrPrefix, SeekDirection direction)
{
if (keyOrPrefix == null) keyOrPrefix = Array.Empty<byte>();
keyOrPrefix ??= [];

using var it = db.NewIterator(readOptions: options);
using var it = _db.NewIterator(readOptions: _options);

if (direction == SeekDirection.Forward)
for (it.Seek(keyOrPrefix); it.Valid(); it.Next())
Expand All @@ -69,24 +70,24 @@ public void Put(byte[] key, byte[] value)

public bool Contains(byte[] key)
{
return db.Get(key, Array.Empty<byte>(), 0, 0, readOptions: options) >= 0;
return _db.Get(key, Array.Empty<byte>(), 0, 0, readOptions: _options) >= 0;
}

public byte[] TryGet(byte[] key)
public byte[]? TryGet(byte[] key)
{
return db.Get(key, readOptions: options);
return _db.Get(key, readOptions: _options);
}

public bool TryGet(byte[] key, out byte[] value)
public bool TryGet(byte[] key, [NotNullWhen(true)] out byte[]? value)
{
value = db.Get(key, readOptions: options);
value = _db.Get(key, readOptions: _options);
return value != null;
}

public void Dispose()
{
snapshot.Dispose();
batch.Dispose();
_snapshot.Dispose();
_batch.Dispose();
}
}
}
31 changes: 16 additions & 15 deletions src/Plugins/RocksDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,36 @@
using RocksDbSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace Neo.Plugins.Storage
{
internal class Store : IStore
{
private readonly RocksDb db;
private readonly RocksDb _db;

public Store(string path)
{
db = RocksDb.Open(Options.Default, Path.GetFullPath(path));
_db = RocksDb.Open(Options.Default, Path.GetFullPath(path));
}

public void Dispose()
{
db.Dispose();
_db.Dispose();
}

public ISnapshot GetSnapshot()
{
return new Snapshot(db);
return new Snapshot(_db);
}

/// <inheritdoc/>
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[]? keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
{
if (keyOrPrefix == null) keyOrPrefix = Array.Empty<byte>();
keyOrPrefix ??= [];

using var it = db.NewIterator();
using var it = _db.NewIterator();
if (direction == SeekDirection.Forward)
for (it.Seek(keyOrPrefix); it.Valid(); it.Next())
yield return (it.Key(), it.Value());
Expand All @@ -52,33 +53,33 @@ public ISnapshot GetSnapshot()

public bool Contains(byte[] key)
{
return db.Get(key, Array.Empty<byte>(), 0, 0) >= 0;
return _db.Get(key, Array.Empty<byte>(), 0, 0) >= 0;
}

public byte[] TryGet(byte[] key)
public byte[]? TryGet(byte[] key)
{
return db.Get(key);
return _db.Get(key);
}

public bool TryGet(byte[] key, out byte[] value)
public bool TryGet(byte[] key, [NotNullWhen(true)] out byte[]? value)
{
value = db.Get(key);
value = _db.Get(key);
return value != null;
}

public void Delete(byte[] key)
{
db.Remove(key);
_db.Remove(key);
}

public void Put(byte[] key, byte[] value)
{
db.Put(key, value);
_db.Put(key, value);
}

public void PutSync(byte[] key, byte[] value)
{
db.Put(key, value, writeOptions: Options.WriteDefaultSync);
_db.Put(key, value, writeOptions: Options.WriteDefaultSync);
}
}
}
1 change: 1 addition & 0 deletions src/Plugins/RocksDBStore/RocksDBStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<PackageId>Neo.Plugins.Storage.RocksDBStore</PackageId>
<RootNamespace>Neo.Plugins.Storage</RootNamespace>
<OutputPath>../../../bin/$(PackageId)</OutputPath>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading