From bde90cac3b673accfc8db3500985060879a469fa Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Tue, 20 Feb 2024 12:07:40 +0100 Subject: [PATCH 1/2] Fix name --- .../Plugins/Storage/LevelDBStore.cs | 54 ++++++++++--- .../Plugins/Storage/LevelDBStorePlugin.cs | 35 +++++++++ src/LevelDBStore/Plugins/Storage/Store.cs | 67 ---------------- .../Plugins/Storage/RocksDBStore.cs | 65 +++++++++++++--- .../Plugins/Storage/RocksDBStorePlugin.cs | 34 ++++++++ src/RocksDBStore/Plugins/Storage/Store.cs | 77 ------------------- tests/Neo.Plugins.Storage.Tests/StoreTest.cs | 4 +- 7 files changed, 168 insertions(+), 168 deletions(-) create mode 100644 src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs delete mode 100644 src/LevelDBStore/Plugins/Storage/Store.cs create mode 100644 src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs delete mode 100644 src/RocksDBStore/Plugins/Storage/Store.cs diff --git a/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs b/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs index 9c676e8a7..2206c5179 100644 --- a/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs +++ b/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs @@ -1,6 +1,6 @@ // Copyright (C) 2015-2024 The Neo Project. // -// LevelDBStore.cs file belongs to the neo project and is free +// Store.cs file belongs to the neo project and is free // software distributed under the MIT software license, see the // accompanying file LICENSE in the main directory of the // repository or http://www.opensource.org/licenses/mit-license.php @@ -11,25 +11,57 @@ using Neo.IO.Data.LevelDB; using Neo.Persistence; -using System; -using System.Linq; +using System.Collections.Generic; namespace Neo.Plugins.Storage { - public class LevelDBStore : Plugin, IStoreProvider + internal class LevelDBStore : IStore { - public override string Description => "Uses LevelDB to store the blockchain data"; + private readonly DB db; - public LevelDBStore() + public LevelDBStore(string path) { - StoreFactory.RegisterProvider(this); + this.db = DB.Open(path, new Options { CreateIfMissing = true, FilterPolicy = Native.leveldb_filterpolicy_create_bloom(15) }); } - public IStore GetStore(string path) + public void Delete(byte[] key) { - if (Environment.CommandLine.Split(' ').Any(p => p == "/repair" || p == "--repair")) - DB.Repair(path, Options.Default); - return new Store(path); + db.Delete(WriteOptions.Default, key); + } + + public void Dispose() + { + db.Dispose(); + } + + public IEnumerable<(byte[], byte[])> Seek(byte[] prefix, SeekDirection direction = SeekDirection.Forward) + { + return db.Seek(ReadOptions.Default, prefix, direction, (k, v) => (k, v)); + } + + public ISnapshot GetSnapshot() + { + return new Snapshot(db); + } + + public void Put(byte[] key, byte[] value) + { + db.Put(WriteOptions.Default, key, value); + } + + public void PutSync(byte[] key, byte[] value) + { + db.Put(WriteOptions.SyncWrite, key, value); + } + + public bool Contains(byte[] key) + { + return db.Contains(ReadOptions.Default, key); + } + + public byte[] TryGet(byte[] key) + { + return db.Get(ReadOptions.Default, key); } } } diff --git a/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs b/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs new file mode 100644 index 000000000..712a7224e --- /dev/null +++ b/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs @@ -0,0 +1,35 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// LevelDBStore.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.IO.Data.LevelDB; +using Neo.Persistence; +using System; +using System.Linq; + +namespace Neo.Plugins.Storage +{ + public class LevelDBStorePlugin : Plugin, IStoreProvider + { + public override string Description => "Uses LevelDB to store the blockchain data"; + + public LevelDBStorePlugin() + { + StoreFactory.RegisterProvider(this); + } + + public IStore GetStore(string path) + { + if (Environment.CommandLine.Split(' ').Any(p => p == "/repair" || p == "--repair")) + DB.Repair(path, Options.Default); + return new LevelDBStore(path); + } + } +} diff --git a/src/LevelDBStore/Plugins/Storage/Store.cs b/src/LevelDBStore/Plugins/Storage/Store.cs deleted file mode 100644 index 809858dda..000000000 --- a/src/LevelDBStore/Plugins/Storage/Store.cs +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (C) 2015-2024 The Neo Project. -// -// Store.cs file belongs to the neo project and is free -// software distributed under the MIT software license, see the -// accompanying file LICENSE in the main directory of the -// repository or http://www.opensource.org/licenses/mit-license.php -// for more details. -// -// Redistribution and use in source and binary forms with or without -// modifications are permitted. - -using Neo.IO.Data.LevelDB; -using Neo.Persistence; -using System.Collections.Generic; - -namespace Neo.Plugins.Storage -{ - internal class Store : IStore - { - private readonly DB db; - - public Store(string path) - { - this.db = DB.Open(path, new Options { CreateIfMissing = true, FilterPolicy = Native.leveldb_filterpolicy_create_bloom(15) }); - } - - public void Delete(byte[] key) - { - db.Delete(WriteOptions.Default, key); - } - - public void Dispose() - { - db.Dispose(); - } - - public IEnumerable<(byte[], byte[])> Seek(byte[] prefix, SeekDirection direction = SeekDirection.Forward) - { - return db.Seek(ReadOptions.Default, prefix, direction, (k, v) => (k, v)); - } - - public ISnapshot GetSnapshot() - { - return new Snapshot(db); - } - - public void Put(byte[] key, byte[] value) - { - db.Put(WriteOptions.Default, key, value); - } - - public void PutSync(byte[] key, byte[] value) - { - db.Put(WriteOptions.SyncWrite, key, value); - } - - public bool Contains(byte[] key) - { - return db.Contains(ReadOptions.Default, key); - } - - public byte[] TryGet(byte[] key) - { - return db.Get(ReadOptions.Default, key); - } - } -} diff --git a/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs b/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs index 079a012f6..0f1097151 100644 --- a/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs +++ b/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs @@ -1,6 +1,6 @@ // Copyright (C) 2015-2024 The Neo Project. // -// RocksDBStore.cs file belongs to the neo project and is free +// Store.cs file belongs to the neo project and is free // software distributed under the MIT software license, see the // accompanying file LICENSE in the main directory of the // repository or http://www.opensource.org/licenses/mit-license.php @@ -10,25 +10,68 @@ // modifications are permitted. using Neo.Persistence; +using RocksDbSharp; +using System; +using System.Collections.Generic; +using System.IO; namespace Neo.Plugins.Storage { - public class RocksDBStore : Plugin, IStoreProvider + internal class RocksDBStore : IStore { - public override string Description => "Uses RocksDBStore to store the blockchain data"; + private readonly RocksDb db; - public RocksDBStore() + public RocksDBStore(string path) { - StoreFactory.RegisterProvider(this); + db = RocksDb.Open(Options.Default, Path.GetFullPath(path)); } - /// - /// Get store - /// - /// RocksDbStore - public IStore GetStore(string path) + public void Dispose() { - return new Store(path); + db.Dispose(); + } + + public ISnapshot GetSnapshot() + { + return new Snapshot(db); + } + + public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward) + { + if (keyOrPrefix == null) keyOrPrefix = Array.Empty(); + + using var it = db.NewIterator(); + if (direction == SeekDirection.Forward) + for (it.Seek(keyOrPrefix); it.Valid(); it.Next()) + yield return (it.Key(), it.Value()); + else + for (it.SeekForPrev(keyOrPrefix); it.Valid(); it.Prev()) + yield return (it.Key(), it.Value()); + } + + public bool Contains(byte[] key) + { + return db.Get(key, Array.Empty(), 0, 0) >= 0; + } + + public byte[] TryGet(byte[] key) + { + return db.Get(key); + } + + public void Delete(byte[] key) + { + db.Remove(key); + } + + public void Put(byte[] key, byte[] value) + { + db.Put(key, value); + } + + public void PutSync(byte[] key, byte[] value) + { + db.Put(key, value, writeOptions: Options.WriteDefaultSync); } } } diff --git a/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs b/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs new file mode 100644 index 000000000..e8356616d --- /dev/null +++ b/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs @@ -0,0 +1,34 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// RocksDBStore.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.Persistence; + +namespace Neo.Plugins.Storage +{ + public class RocksDBStorePlugin : Plugin, IStoreProvider + { + public override string Description => "Uses RocksDBStore to store the blockchain data"; + + public RocksDBStorePlugin() + { + StoreFactory.RegisterProvider(this); + } + + /// + /// Get store + /// + /// RocksDbStore + public IStore GetStore(string path) + { + return new RocksDBStore(path); + } + } +} diff --git a/src/RocksDBStore/Plugins/Storage/Store.cs b/src/RocksDBStore/Plugins/Storage/Store.cs deleted file mode 100644 index ebf160dab..000000000 --- a/src/RocksDBStore/Plugins/Storage/Store.cs +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (C) 2015-2024 The Neo Project. -// -// Store.cs file belongs to the neo project and is free -// software distributed under the MIT software license, see the -// accompanying file LICENSE in the main directory of the -// repository or http://www.opensource.org/licenses/mit-license.php -// for more details. -// -// Redistribution and use in source and binary forms with or without -// modifications are permitted. - -using Neo.Persistence; -using RocksDbSharp; -using System; -using System.Collections.Generic; -using System.IO; - -namespace Neo.Plugins.Storage -{ - internal class Store : IStore - { - private readonly RocksDb db; - - public Store(string path) - { - db = RocksDb.Open(Options.Default, Path.GetFullPath(path)); - } - - public void Dispose() - { - db.Dispose(); - } - - public ISnapshot GetSnapshot() - { - return new Snapshot(db); - } - - public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[] keyOrPrefix, SeekDirection direction = SeekDirection.Forward) - { - if (keyOrPrefix == null) keyOrPrefix = Array.Empty(); - - using var it = db.NewIterator(); - if (direction == SeekDirection.Forward) - for (it.Seek(keyOrPrefix); it.Valid(); it.Next()) - yield return (it.Key(), it.Value()); - else - for (it.SeekForPrev(keyOrPrefix); it.Valid(); it.Prev()) - yield return (it.Key(), it.Value()); - } - - public bool Contains(byte[] key) - { - return db.Get(key, Array.Empty(), 0, 0) >= 0; - } - - public byte[] TryGet(byte[] key) - { - return db.Get(key); - } - - public void Delete(byte[] key) - { - db.Remove(key); - } - - public void Put(byte[] key, byte[] value) - { - db.Put(key, value); - } - - public void PutSync(byte[] key, byte[] value) - { - db.Put(key, value, writeOptions: Options.WriteDefaultSync); - } - } -} diff --git a/tests/Neo.Plugins.Storage.Tests/StoreTest.cs b/tests/Neo.Plugins.Storage.Tests/StoreTest.cs index 2773f92e8..2154d95be 100644 --- a/tests/Neo.Plugins.Storage.Tests/StoreTest.cs +++ b/tests/Neo.Plugins.Storage.Tests/StoreTest.cs @@ -49,7 +49,7 @@ public void TestMemory() [TestMethod] public void TestLevelDb() { - using var plugin = new LevelDBStore(); + using var plugin = new LevelDBStorePlugin(); TestPersistenceDelete(plugin.GetStore(path_leveldb)); // Test all with the same store @@ -66,7 +66,7 @@ public void TestLevelDb() [TestMethod] public void TestRocksDb() { - using var plugin = new RocksDBStore(); + using var plugin = new RocksDBStorePlugin(); TestPersistenceDelete(plugin.GetStore(path_rocksdb)); // Test all with the same store From 7b3cdd90a53ca10b4129610c39f7d5c28e6d7577 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Tue, 20 Feb 2024 12:08:46 +0100 Subject: [PATCH 2/2] Fix comment --- src/LevelDBStore/Plugins/Storage/LevelDBStore.cs | 2 +- src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs | 2 +- src/RocksDBStore/Plugins/Storage/RocksDBStore.cs | 2 +- src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs b/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs index 2206c5179..1b14de55f 100644 --- a/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs +++ b/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs @@ -1,6 +1,6 @@ // Copyright (C) 2015-2024 The Neo Project. // -// Store.cs file belongs to the neo project and is free +// LevelDBStore.cs file belongs to the neo project and is free // software distributed under the MIT software license, see the // accompanying file LICENSE in the main directory of the // repository or http://www.opensource.org/licenses/mit-license.php diff --git a/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs b/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs index 712a7224e..d772dc2b7 100644 --- a/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs +++ b/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs @@ -1,6 +1,6 @@ // Copyright (C) 2015-2024 The Neo Project. // -// LevelDBStore.cs file belongs to the neo project and is free +// LevelDBStorePlugin.cs file belongs to the neo project and is free // software distributed under the MIT software license, see the // accompanying file LICENSE in the main directory of the // repository or http://www.opensource.org/licenses/mit-license.php diff --git a/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs b/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs index 0f1097151..ada9846c3 100644 --- a/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs +++ b/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs @@ -1,6 +1,6 @@ // Copyright (C) 2015-2024 The Neo Project. // -// Store.cs file belongs to the neo project and is free +// RocksDBStore.cs file belongs to the neo project and is free // software distributed under the MIT software license, see the // accompanying file LICENSE in the main directory of the // repository or http://www.opensource.org/licenses/mit-license.php diff --git a/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs b/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs index e8356616d..aac6c30f4 100644 --- a/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs +++ b/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs @@ -1,6 +1,6 @@ // Copyright (C) 2015-2024 The Neo Project. // -// RocksDBStore.cs file belongs to the neo project and is free +// RocksDBStorePlugin.cs file belongs to the neo project and is free // software distributed under the MIT software license, see the // accompanying file LICENSE in the main directory of the // repository or http://www.opensource.org/licenses/mit-license.php