diff --git a/src/LevelDBStore/IO/Data/LevelDB/Helper.cs b/src/LevelDBStore/IO/Data/LevelDB/Helper.cs index dc44be92b..a2c28e769 100644 --- a/src/LevelDBStore/IO/Data/LevelDB/Helper.cs +++ b/src/LevelDBStore/IO/Data/LevelDB/Helper.cs @@ -1,3 +1,4 @@ +using Neo.IO.Caching; using System; using System.Collections.Generic; using System.Runtime.InteropServices; @@ -6,15 +7,17 @@ namespace Neo.IO.Data.LevelDB { public static class Helper { - public static IEnumerable Find(this DB db, ReadOptions options, byte[] prefix, Func resultSelector) + public static IEnumerable Seek(this DB db, ReadOptions options, byte[] keyOrPrefix, SeekDirection direction, Func resultSelector) { using Iterator it = db.NewIterator(options); - for (it.Seek(prefix); it.Valid(); it.Next()) + for (it.Seek(keyOrPrefix); it.Valid();) { - byte[] key = it.Key(); - if (key.Length < prefix.Length) break; - if (!key.AsSpan().StartsWith(prefix)) break; - yield return resultSelector(key, it.Value()); + yield return resultSelector(it.Key(), it.Value()); + + if (direction == SeekDirection.Forward) + it.Next(); + else + it.Prev(); } } diff --git a/src/LevelDBStore/LevelDBStore.csproj b/src/LevelDBStore/LevelDBStore.csproj index e88f68d18..f1501ab71 100644 --- a/src/LevelDBStore/LevelDBStore.csproj +++ b/src/LevelDBStore/LevelDBStore.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/LevelDBStore/Plugins/Storage/Snapshot.cs b/src/LevelDBStore/Plugins/Storage/Snapshot.cs index aaba9951c..b547f1108 100644 --- a/src/LevelDBStore/Plugins/Storage/Snapshot.cs +++ b/src/LevelDBStore/Plugins/Storage/Snapshot.cs @@ -1,3 +1,4 @@ +using Neo.IO.Caching; using Neo.IO.Data.LevelDB; using Neo.Persistence; using System.Collections.Generic; @@ -35,9 +36,9 @@ public void Dispose() snapshot.Dispose(); } - public IEnumerable<(byte[] Key, byte[] Value)> Find(byte table, byte[] prefix) + public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte table, byte[] prefix, SeekDirection direction) { - return db.Find(options, Helper.CreateKey(table, prefix), (k, v) => (k[1..], v)); + return db.Seek(options, Helper.CreateKey(table, prefix), direction, (k, v) => (k[1..], v)); } public void Put(byte table, byte[] key, byte[] value) diff --git a/src/LevelDBStore/Plugins/Storage/Store.cs b/src/LevelDBStore/Plugins/Storage/Store.cs index fef486d27..48cb10018 100644 --- a/src/LevelDBStore/Plugins/Storage/Store.cs +++ b/src/LevelDBStore/Plugins/Storage/Store.cs @@ -1,3 +1,4 @@ +using Neo.IO.Caching; using Neo.IO.Data.LevelDB; using Neo.Persistence; using System; @@ -49,9 +50,9 @@ public void Dispose() db.Dispose(); } - public IEnumerable<(byte[], byte[])> Find(byte table, byte[] prefix) + public IEnumerable<(byte[], byte[])> Seek(byte table, byte[] prefix, SeekDirection direction) { - return db.Find(ReadOptions.Default, Helper.CreateKey(table, prefix), (k, v) => (k[1..], v)); + return db.Seek(ReadOptions.Default, Helper.CreateKey(table, prefix), direction, (k, v) => (k[1..], v)); } public ISnapshot GetSnapshot() diff --git a/src/RocksDBStore/Plugins/Storage/Snapshot.cs b/src/RocksDBStore/Plugins/Storage/Snapshot.cs index 163a8e661..c61bf460e 100644 --- a/src/RocksDBStore/Plugins/Storage/Snapshot.cs +++ b/src/RocksDBStore/Plugins/Storage/Snapshot.cs @@ -1,3 +1,4 @@ +using Neo.IO.Caching; using Neo.Persistence; using RocksDbSharp; using System; @@ -40,16 +41,17 @@ public void Put(byte table, byte[] key, byte[] value) batch.Put(key, value, store.GetFamily(table)); } - public IEnumerable<(byte[] Key, byte[] Value)> Find(byte table, byte[] prefix) + public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte table, byte[] keyOrPrefix, SeekDirection direction) { using var it = db.NewIterator(store.GetFamily(table), options); - for (it.Seek(prefix); it.Valid(); it.Next()) + for (it.Seek(keyOrPrefix); it.Valid();) { - var key = it.Key(); - byte[] y = prefix; - if (key.Length < y.Length) break; - if (!key.AsSpan().StartsWith(y)) break; - yield return (key, it.Value()); + yield return (it.Key(), it.Value()); + + if (direction == SeekDirection.Forward) + it.Next(); + else + it.Prev(); } } diff --git a/src/RocksDBStore/Plugins/Storage/Store.cs b/src/RocksDBStore/Plugins/Storage/Store.cs index 97d0e2f2c..f0959a596 100644 --- a/src/RocksDBStore/Plugins/Storage/Store.cs +++ b/src/RocksDBStore/Plugins/Storage/Store.cs @@ -1,3 +1,4 @@ +using Neo.IO.Caching; using Neo.Persistence; using RocksDbSharp; using System; @@ -81,7 +82,7 @@ public ISnapshot GetSnapshot() return new Snapshot(this, db); } - public IEnumerable<(byte[] Key, byte[] Value)> Find(byte table, byte[] prefix) + public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte table, byte[] prefix, SeekDirection direction) { using var it = db.NewIterator(GetFamily(table), Options.ReadDefault); for (it.Seek(prefix); it.Valid(); it.Next()) diff --git a/src/RocksDBStore/RocksDBStore.csproj b/src/RocksDBStore/RocksDBStore.csproj index 7992330f8..71964ff97 100644 --- a/src/RocksDBStore/RocksDBStore.csproj +++ b/src/RocksDBStore/RocksDBStore.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/RpcNep5Tracker/DbCache.cs b/src/RpcNep5Tracker/DbCache.cs index 681b6404a..bd2ff3cba 100644 --- a/src/RpcNep5Tracker/DbCache.cs +++ b/src/RpcNep5Tracker/DbCache.cs @@ -47,9 +47,9 @@ protected override void DeleteInternal(TKey key) batch?.Delete(CreateKey(prefix, key)); } - protected override IEnumerable<(TKey, TValue)> FindInternal(byte[] key_prefix) + protected override IEnumerable<(TKey, TValue)> SeekInternal(byte[] key_prefix, SeekDirection direction) { - return db.Find(options, CreateKey(prefix, key_prefix), (k, v) => (k.AsSerializable(1), v.AsSerializable())); + return db.Seek(options, CreateKey(prefix, key_prefix), direction, (k, v) => (k.AsSerializable(1), v.AsSerializable())); } protected override TValue GetInternal(TKey key)