Skip to content

Commit

Permalink
Fix seek (#281)
Browse files Browse the repository at this point in the history
* fix seek

* move Helper method

* Update Helper.cs

Co-authored-by: Luchuan <luchuan@neo.org>
  • Loading branch information
Tommo-L and Luchuan committed Jul 7, 2020
1 parent 50e245b commit 1cf385d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 38 deletions.
15 changes: 13 additions & 2 deletions src/LevelDBStore/IO/Data/LevelDB/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ namespace Neo.IO.Data.LevelDB
{
public static class Helper
{
public static IEnumerable<T> Seek<T>(this DB db, ReadOptions options, byte[] keyOrPrefix, SeekDirection direction, Func<byte[], byte[], T> resultSelector)
public static IEnumerable<T> Seek<T>(this DB db, ReadOptions options, byte table, byte[] prefix, SeekDirection direction, Func<byte[], byte[], T> resultSelector)
{
using Iterator it = db.NewIterator(options);
for (it.Seek(keyOrPrefix); it.Valid();)
for (it.Seek(CreateKey(table, prefix)); it.Valid();)
{
var key = it.Key();
if (key.Length < 1 || key[0] != table) break;
yield return resultSelector(it.Key(), it.Value());

if (direction == SeekDirection.Forward)
Expand Down Expand Up @@ -39,5 +41,14 @@ internal static byte[] ToByteArray(this IntPtr data, UIntPtr length)
Marshal.Copy(data, buffer, 0, (int)length);
return buffer;
}

public static byte[] CreateKey(byte table, byte[] key = null)
{
if (key is null || key.Length == 0) return new[] { table };
byte[] buffer = new byte[1 + key.Length];
buffer[0] = table;
Buffer.BlockCopy(key, 0, buffer, 1, key.Length);
return buffer;
}
}
}
16 changes: 0 additions & 16 deletions src/LevelDBStore/Plugins/Storage/Helper.cs

This file was deleted.

9 changes: 5 additions & 4 deletions src/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.Persistence;
using System.Collections.Generic;
using LSnapshot = Neo.IO.Data.LevelDB.Snapshot;
using LHelper = Neo.IO.Data.LevelDB.Helper;

namespace Neo.Plugins.Storage
{
Expand All @@ -28,7 +29,7 @@ public void Commit()

public void Delete(byte table, byte[] key)
{
batch.Delete(Helper.CreateKey(table, key));
batch.Delete(LHelper.CreateKey(table, key));
}

public void Dispose()
Expand All @@ -38,17 +39,17 @@ public void Dispose()

public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte table, byte[] prefix, SeekDirection direction)
{
return db.Seek(options, Helper.CreateKey(table, prefix), direction, (k, v) => (k[1..], v));
return db.Seek(options, table, prefix, direction, (k, v) => (k[1..], v));
}

public void Put(byte table, byte[] key, byte[] value)
{
batch.Put(Helper.CreateKey(table, key), value);
batch.Put(LHelper.CreateKey(table, key), value);
}

public byte[] TryGet(byte table, byte[] key)
{
return db.Get(options, Helper.CreateKey(table, key));
return db.Get(options, LHelper.CreateKey(table, key));
}
}
}
15 changes: 8 additions & 7 deletions src/LevelDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using LHelper = Neo.IO.Data.LevelDB.Helper;

namespace Neo.Plugins.Storage
{
Expand All @@ -16,7 +17,7 @@ internal class Store : IStore
public Store(string path)
{
this.db = DB.Open(path, new Options { CreateIfMissing = true });
byte[] value = db.Get(ReadOptions.Default, Helper.CreateKey(SYS_Version));
byte[] value = db.Get(ReadOptions.Default, LHelper.CreateKey(SYS_Version));
if (value != null && Version.TryParse(Encoding.ASCII.GetString(value), out Version version) && version >= Version.Parse("3.0.0"))
return;

Expand All @@ -36,13 +37,13 @@ public Store(string path)
}
}

db.Put(WriteOptions.Default, Helper.CreateKey(SYS_Version), Encoding.ASCII.GetBytes(Assembly.GetExecutingAssembly().GetName().Version.ToString()));
db.Put(WriteOptions.Default, LHelper.CreateKey(SYS_Version), Encoding.ASCII.GetBytes(Assembly.GetExecutingAssembly().GetName().Version.ToString()));
db.Write(WriteOptions.Default, batch);
}

public void Delete(byte table, byte[] key)
{
db.Delete(WriteOptions.Default, Helper.CreateKey(table, key));
db.Delete(WriteOptions.Default, LHelper.CreateKey(table, key));
}

public void Dispose()
Expand All @@ -52,7 +53,7 @@ public void Dispose()

public IEnumerable<(byte[], byte[])> Seek(byte table, byte[] prefix, SeekDirection direction)
{
return db.Seek(ReadOptions.Default, Helper.CreateKey(table, prefix), direction, (k, v) => (k[1..], v));
return db.Seek(ReadOptions.Default, table, prefix, direction, (k, v) => (k[1..], v));
}

public ISnapshot GetSnapshot()
Expand All @@ -62,17 +63,17 @@ public ISnapshot GetSnapshot()

public void Put(byte table, byte[] key, byte[] value)
{
db.Put(WriteOptions.Default, Helper.CreateKey(table, key), value);
db.Put(WriteOptions.Default, LHelper.CreateKey(table, key), value);
}

public void PutSync(byte table, byte[] key, byte[] value)
{
db.Put(WriteOptions.SyncWrite, Helper.CreateKey(table, key), value);
db.Put(WriteOptions.SyncWrite, LHelper.CreateKey(table, key), value);
}

public byte[] TryGet(byte table, byte[] key)
{
return db.Get(ReadOptions.Default, Helper.CreateKey(table, key));
return db.Get(ReadOptions.Default, LHelper.CreateKey(table, key));
}
}
}
1 change: 0 additions & 1 deletion src/RocksDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Neo.IO.Caching;
using Neo.Persistence;
using RocksDbSharp;
using System;
using System.Collections.Generic;

namespace Neo.Plugins.Storage
Expand Down
15 changes: 8 additions & 7 deletions src/RocksDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,17 @@ public ISnapshot GetSnapshot()
return new Snapshot(this, db);
}

public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte table, byte[] prefix, SeekDirection direction)
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte table, byte[] prefix, SeekDirection direction = SeekDirection.Forward)
{
using var it = db.NewIterator(GetFamily(table), Options.ReadDefault);
for (it.Seek(prefix); it.Valid(); it.Next())
for (it.Seek(prefix); 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
2 changes: 1 addition & 1 deletion src/RpcNep5Tracker/DbCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override void DeleteInternal(TKey key)

protected override IEnumerable<(TKey, TValue)> SeekInternal(byte[] key_prefix, SeekDirection direction)
{
return db.Seek(options, CreateKey(prefix, key_prefix), direction, (k, v) => (k.AsSerializable<TKey>(1), v.AsSerializable<TValue>()));
return db.Seek(options, 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 1cf385d

Please sign in to comment.