Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core Plugin] PR 3414 part 4. thread safe snapshot #3606

Merged
merged 2 commits into from
Dec 1, 2024
Merged
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
24 changes: 14 additions & 10 deletions src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,60 @@ internal class Snapshot : ISnapshot
{
private readonly DB _db;
private readonly LSnapshot _snapshot;
private readonly ReadOptions _options;
private readonly ReadOptions _readOptions;
private readonly WriteBatch _batch;
private readonly object _lock = new();

public Snapshot(DB db)
{
_db = db;
_snapshot = db.GetSnapshot();
_options = new ReadOptions { FillCache = false, Snapshot = _snapshot };
_readOptions = new ReadOptions { FillCache = false, Snapshot = _snapshot };
_batch = new WriteBatch();
}

public void Commit()
{
_db.Write(WriteOptions.Default, _batch);
lock (_lock)
_db.Write(WriteOptions.Default, _batch);
}

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

public void Dispose()
{
_snapshot.Dispose();
_options.Dispose();
_readOptions.Dispose();
}

public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] prefix, SeekDirection direction = SeekDirection.Forward)
{
return _db.Seek(_options, prefix, direction);
return _db.Seek(_readOptions, prefix, direction);
}

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

public bool Contains(byte[] key)
{
return _db.Contains(_options, key);
return _db.Contains(_readOptions, key);
}

public byte[] TryGet(byte[] key)
{
return _db.Get(_options, key);
return _db.Get(_readOptions, key);
}

public bool TryGet(byte[] key, out byte[] value)
{
value = _db.Get(_options, key);
value = _db.Get(_readOptions, key);
return value != null;
}
}
Expand Down
Loading