public static IEnumerable <T> Seek <T>(this DB db, ReadOptions options, byte[] prefix, SeekDirection direction, Func <byte[], byte[], T> resultSelector) { using Iterator it = db.NewIterator(options); if (direction == SeekDirection.Forward) { for (it.Seek(prefix); it.Valid(); it.Next()) { yield return(resultSelector(it.Key(), it.Value())); } } else { // SeekForPrev it.Seek(prefix); if (!it.Valid()) { it.SeekToLast(); } else if (it.Key().AsSpan().SequenceCompareTo(prefix) > 0) { it.Prev(); } for (; it.Valid(); it.Prev()) { yield return(resultSelector(it.Key(), it.Value())); } } }
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); byte[] target = CreateKey(table, prefix); if (direction == SeekDirection.Forward) { for (it.Seek(target); it.Valid(); it.Next()) { var key = it.Key(); if (key.Length < 1 || key[0] != table) { break; } yield return(resultSelector(it.Key(), it.Value())); } } else { // SeekForPrev it.Seek(target); if (!it.Valid()) { it.SeekToLast(); } else if (it.Key().AsSpan().SequenceCompareTo(target) > 0) { it.Prev(); } for (; it.Valid(); it.Prev()) { var key = it.Key(); if (key.Length < 1 || key[0] != table) { break; } yield return(resultSelector(it.Key(), it.Value())); } } }