Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/LevelDBStore/IO/Data/LevelDB/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Neo.IO.Data.LevelDB;

public static class Helper
{
public static IEnumerable<(byte[], byte[])> Seek(this DB db, ReadOptions options, byte[]? keyOrPrefix, SeekDirection direction)
public static IEnumerable<(byte[] Key, byte[] Value)> Seek(this DB db, ReadOptions options, byte[]? keyOrPrefix, SeekDirection direction)
{
keyOrPrefix ??= [];

Expand Down
51 changes: 11 additions & 40 deletions plugins/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,51 +79,22 @@ public void Dispose()
ArgumentNullException.ThrowIfNull(start);
ArgumentNullException.ThrowIfNull(end);

if (start.AsSpan().SequenceCompareTo(end) >= 0)
yield break;
Comment thread
ajara87 marked this conversation as resolved.

using var iterator = _db.CreateIterator(_readOptions);

if (direction == SeekDirection.Forward)
var order = start.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
iterator.Seek(start);

while (iterator.Valid())
{
var key = iterator.Key();
if (key is null) break;

if (key.AsSpan().SequenceCompareTo(end) >= 0)
break;

yield return (key, iterator.Value()!);
iterator.Next();
}
yield break;
}
else
{
iterator.Seek(end);

if (!iterator.Valid())
{
iterator.SeekToLast();
}
else
{
iterator.Prev();
}

while (iterator.Valid())
foreach (var item in _db.Seek(_readOptions, start, direction))
{
order = item.Key.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
var key = iterator.Key();
if (key is null) break;

if (key.AsSpan().SequenceCompareTo(start) < 0)
break;

yield return (key, iterator.Value()!);
iterator.Prev();
yield break; // end is the exclusive max key if forward, or the exclusive min key if backward
}
yield return item;
}
}

Expand Down
57 changes: 13 additions & 44 deletions plugins/LevelDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,57 +83,26 @@ public bool TryGet(byte[] key, [NotNullWhen(true)] out byte[]? value)
ArgumentNullException.ThrowIfNull(start);
ArgumentNullException.ThrowIfNull(end);

if (start.AsSpan().SequenceCompareTo(end) >= 0)
yield break;

using var iterator = _db.CreateIterator(ReadOptions.Default);

if (direction == SeekDirection.Forward)
var order = start.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
iterator.Seek(start);

while (iterator.Valid())
{
var key = iterator.Key();
if (key is null) break;

if (key.AsSpan().SequenceCompareTo(end) >= 0)
break;

yield return (key, iterator.Value()!);
iterator.Next();
}
yield break;
}
else
{
iterator.Seek(end);

if (!iterator.Valid())
{
iterator.SeekToLast();
}
else
{
iterator.Prev();
}

while (iterator.Valid())
foreach (var item in _db.Seek(ReadOptions.Default, start, direction))
{
order = item.Key.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
var key = iterator.Key();
if (key is null) break;

if (key.AsSpan().SequenceCompareTo(start) < 0)
break;

yield return (key, iterator.Value()!);
iterator.Prev();
yield break; // end is the exclusive max key if forward, or the exclusive min key if backward
}
yield return item;
}
}

public IEnumerator<KeyValuePair<byte[], byte[]>> GetEnumerator() =>
_db.GetEnumerator();
public IEnumerator<KeyValuePair<byte[], byte[]>> GetEnumerator() => _db.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
49 changes: 15 additions & 34 deletions plugins/RocksDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,59 +62,40 @@ public void Put(byte[] key, byte[] value)
public IEnumerable<(byte[] Key, byte[] Value)> Find(byte[]? keyOrPrefix, SeekDirection direction)
{
keyOrPrefix ??= [];

using var it = _db.NewIterator(readOptions: _options);

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 IEnumerable<(byte[] Key, byte[] Value)> FindRange(byte[] start, byte[] end, SeekDirection direction = SeekDirection.Forward)
{
ArgumentNullException.ThrowIfNull(start);
ArgumentNullException.ThrowIfNull(end);

if (start.AsSpan().SequenceCompareTo(end) >= 0)
yield break;

using var it = _db.NewIterator(readOptions: _options);

if (direction == SeekDirection.Forward)
var order = start.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
for (it.Seek(start); it.Valid(); it.Next())
{
var key = it.Key();
if (key.AsSpan().SequenceCompareTo(end) >= 0)
break;

yield return (key, it.Value());
}
yield break;
}
else
{
it.Seek(end);

if (!it.Valid())
{
it.SeekToLast();
}
else
{
it.Prev();
}

for (; it.Valid(); it.Prev())
foreach (var item in Find(start, direction))
{
order = item.Key.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
var key = it.Key();
if (key.AsSpan().SequenceCompareTo(start) < 0)
break;

yield return (key, it.Value());
yield break; // end is the exclusive max key if forward, or the exclusive min key if backward
}
yield return item;
}
}

Expand Down
48 changes: 15 additions & 33 deletions plugins/RocksDBStore/Plugins/Storage/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,58 +43,40 @@ public IStoreSnapshot GetSnapshot()
public IEnumerable<(byte[] Key, byte[] Value)> Find(byte[]? keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
{
keyOrPrefix ??= [];

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 IEnumerable<(byte[] Key, byte[] Value)> FindRange(byte[] start, byte[] end, SeekDirection direction = SeekDirection.Forward)
{
ArgumentNullException.ThrowIfNull(start);
ArgumentNullException.ThrowIfNull(end);

if (start.AsSpan().SequenceCompareTo(end) >= 0)
yield break;

using var it = _db.NewIterator();

if (direction == SeekDirection.Forward)
var order = start.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
for (it.Seek(start); it.Valid(); it.Next())
{
var key = it.Key();
if (key.AsSpan().SequenceCompareTo(end) >= 0)
break;

yield return (key, it.Value());
}
yield break;
}
else
{
it.Seek(end);

if (!it.Valid())
{
it.SeekToLast();
}
else
{
it.Prev();
}

for (; it.Valid(); it.Prev())
foreach (var item in Find(start, direction))
{
order = item.Key.AsSpan().SequenceCompareTo(end);
if ((direction == SeekDirection.Forward && order >= 0) ||
(direction == SeekDirection.Backward && order <= 0))
{
var key = it.Key();
if (key.AsSpan().SequenceCompareTo(start) < 0)
break;

yield return (key, it.Value());
yield break; // end is the exclusive max key if forward, or the exclusive min key if backward
}
yield return item;
}
}

Expand Down
Loading
Loading