diff --git a/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs b/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs index fd7febd451..c4a2709b6d 100644 --- a/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs +++ b/src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs @@ -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; } }