From 644f30897053ab83b77cc664e79b2e4946182719 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Wed, 1 Jul 2020 22:27:52 +0800 Subject: [PATCH 1/4] replace find by seek --- src/LevelDBStore/IO/Data/LevelDB/Helper.cs | 2 +- src/RocksDBStore/Plugins/Storage/Store.cs | 2 +- src/RpcNep5Tracker/DbCache.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LevelDBStore/IO/Data/LevelDB/Helper.cs b/src/LevelDBStore/IO/Data/LevelDB/Helper.cs index dc44be92b..f08ea9882 100644 --- a/src/LevelDBStore/IO/Data/LevelDB/Helper.cs +++ b/src/LevelDBStore/IO/Data/LevelDB/Helper.cs @@ -6,7 +6,7 @@ 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[] prefix, Func resultSelector) { using Iterator it = db.NewIterator(options); for (it.Seek(prefix); it.Valid(); it.Next()) diff --git a/src/RocksDBStore/Plugins/Storage/Store.cs b/src/RocksDBStore/Plugins/Storage/Store.cs index 97d0e2f2c..40682f53e 100644 --- a/src/RocksDBStore/Plugins/Storage/Store.cs +++ b/src/RocksDBStore/Plugins/Storage/Store.cs @@ -81,7 +81,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) { using var it = db.NewIterator(GetFamily(table), Options.ReadDefault); for (it.Seek(prefix); it.Valid(); it.Next()) diff --git a/src/RpcNep5Tracker/DbCache.cs b/src/RpcNep5Tracker/DbCache.cs index 681b6404a..7b86520dc 100644 --- a/src/RpcNep5Tracker/DbCache.cs +++ b/src/RpcNep5Tracker/DbCache.cs @@ -47,7 +47,7 @@ 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) { return db.Find(options, CreateKey(prefix, key_prefix), (k, v) => (k.AsSerializable(1), v.AsSerializable())); } From d6759d2ee12ff738e75e2cea42e3735c099c6021 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Fri, 3 Jul 2020 18:56:53 +0800 Subject: [PATCH 2/4] add db.seek --- src/LevelDBStore/IO/Data/LevelDB/Helper.cs | 15 +++++++++------ src/LevelDBStore/LevelDBStore.csproj | 2 +- src/LevelDBStore/Plugins/Storage/Snapshot.cs | 5 +++-- src/LevelDBStore/Plugins/Storage/Store.cs | 5 +++-- src/RocksDBStore/Plugins/Storage/Snapshot.cs | 12 +++++++++--- src/RocksDBStore/Plugins/Storage/Store.cs | 3 ++- src/RocksDBStore/RocksDBStore.csproj | 2 +- src/RpcNep5Tracker/DbCache.cs | 4 ++-- 8 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/LevelDBStore/IO/Data/LevelDB/Helper.cs b/src/LevelDBStore/IO/Data/LevelDB/Helper.cs index f08ea9882..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 Seek(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..6c284051a 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,21 @@ 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; + byte[] y = keyOrPrefix; if (key.Length < y.Length) break; if (!key.AsSpan().StartsWith(y)) break; yield return (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 40682f53e..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)> Seek(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 7b86520dc..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)> SeekInternal(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) From d03d8beb912a73b2e3728514b99b9b834e16e320 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Fri, 3 Jul 2020 18:58:43 +0800 Subject: [PATCH 3/4] fix --- src/RocksDBStore/Plugins/Storage/Snapshot.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/RocksDBStore/Plugins/Storage/Snapshot.cs b/src/RocksDBStore/Plugins/Storage/Snapshot.cs index 6c284051a..8952ff57e 100644 --- a/src/RocksDBStore/Plugins/Storage/Snapshot.cs +++ b/src/RocksDBStore/Plugins/Storage/Snapshot.cs @@ -46,11 +46,7 @@ public void Put(byte table, byte[] key, byte[] value) using var it = db.NewIterator(store.GetFamily(table), options); for (it.Seek(keyOrPrefix); it.Valid();) { - var key = it.Key(); - byte[] y = keyOrPrefix; - 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(); From 72cd677f0a17d61d1d76b442a7496559cc78f74b Mon Sep 17 00:00:00 2001 From: Luchuan Date: Fri, 3 Jul 2020 19:01:02 +0800 Subject: [PATCH 4/4] format --- src/RocksDBStore/Plugins/Storage/Snapshot.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RocksDBStore/Plugins/Storage/Snapshot.cs b/src/RocksDBStore/Plugins/Storage/Snapshot.cs index 8952ff57e..c61bf460e 100644 --- a/src/RocksDBStore/Plugins/Storage/Snapshot.cs +++ b/src/RocksDBStore/Plugins/Storage/Snapshot.cs @@ -48,7 +48,7 @@ public void Put(byte table, byte[] key, byte[] value) { yield return (it.Key(), it.Value()); - if(direction == SeekDirection.Forward) + if (direction == SeekDirection.Forward) it.Next(); else it.Prev();