diff --git a/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs b/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs
index 9c676e8a7..1b14de55f 100644
--- a/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs
+++ b/src/LevelDBStore/Plugins/Storage/LevelDBStore.cs
@@ -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..d772dc2b7
--- /dev/null
+++ b/src/LevelDBStore/Plugins/Storage/LevelDBStorePlugin.cs
@@ -0,0 +1,35 @@
+// Copyright (C) 2015-2024 The Neo Project.
+//
+// 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
+// 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..ada9846c3 100644
--- a/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs
+++ b/src/RocksDBStore/Plugins/Storage/RocksDBStore.cs
@@ -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..aac6c30f4
--- /dev/null
+++ b/src/RocksDBStore/Plugins/Storage/RocksDBStorePlugin.cs
@@ -0,0 +1,34 @@
+// Copyright (C) 2015-2024 The Neo Project.
+//
+// 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
+// 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