Skip to content

Commit

Permalink
thread safe (#3606)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y authored Dec 1, 2024
1 parent b8073d4 commit 1adc6e8
Showing 1 changed file with 14 additions and 10 deletions.
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

0 comments on commit 1adc6e8

Please sign in to comment.