Skip to content

Commit

Permalink
Replace db.Find by db.Seek (neo-project#279)
Browse files Browse the repository at this point in the history
* replace find by seek

* add db.seek

* fix

* format

Co-authored-by: Luchuan <[email protected]>
  • Loading branch information
2 people authored and 陈志同 committed Oct 13, 2020
1 parent 8b095da commit dfe8797
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 22 deletions.
15 changes: 9 additions & 6 deletions src/LevelDBStore/IO/Data/LevelDB/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.IO.Caching;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
Expand All @@ -6,15 +7,17 @@ namespace Neo.IO.Data.LevelDB
{
public static class Helper
{
public static IEnumerable<T> Find<T>(this DB db, ReadOptions options, byte[] prefix, Func<byte[], byte[], T> resultSelector)
public static IEnumerable<T> Seek<T>(this DB db, ReadOptions options, byte[] keyOrPrefix, SeekDirection direction, Func<byte[], byte[], T> 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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/LevelDBStore/LevelDBStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00958" />
<PackageReference Include="Neo" Version="3.0.0-CI00962" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions src/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.IO.Caching;
using Neo.IO.Data.LevelDB;
using Neo.Persistence;
using System.Collections.Generic;
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/LevelDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.IO.Caching;
using Neo.IO.Data.LevelDB;
using Neo.Persistence;
using System;
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 9 additions & 7 deletions src/RocksDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.IO.Caching;
using Neo.Persistence;
using RocksDbSharp;
using System;
Expand Down Expand Up @@ -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();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/RocksDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.IO.Caching;
using Neo.Persistence;
using RocksDbSharp;
using System;
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/RocksDBStore/RocksDBStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00958" />
<PackageReference Include="Neo" Version="3.0.0-CI00962" />
<PackageReference Include="RocksDbNative" Version="6.2.2" />
<PackageReference Include="RocksDbSharp" Version="6.2.2" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/RpcNep5Tracker/DbCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey>(1), v.AsSerializable<TValue>()));
return db.Seek(options, CreateKey(prefix, key_prefix), direction, (k, v) => (k.AsSerializable<TKey>(1), v.AsSerializable<TValue>()));
}

protected override TValue GetInternal(TKey key)
Expand Down

0 comments on commit dfe8797

Please sign in to comment.