Example #1
0
        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()));
                }
            }
        }
Example #2
0
        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()));
                }
            }
        }
Example #3
0
 public static IEnumerable <T> FindRange <T>(this DB db, ReadOptions options, byte[] startKey, byte[] endKey, Func <byte[], byte[], T> resultSelector)
 {
     using Iterator it = db.NewIterator(options);
     for (it.Seek(startKey); it.Valid(); it.Next())
     {
         byte[] key = it.Key();
         if (key.AsSpan().SequenceCompareTo(endKey) > 0)
         {
             break;
         }
         yield return(resultSelector(key, it.Value()));
     }
 }
Example #4
0
 public static IEnumerable <T> Find <T>(this DB db, ReadOptions options, byte[] prefix, Func <byte[], byte[], T> resultSelector)
 {
     using Iterator it = db.NewIterator(options);
     for (it.Seek(prefix); it.Valid(); it.Next())
     {
         byte[] key = it.Key();
         if (key.Length < prefix.Length)
         {
             break;
         }
         if (!key.AsSpan().StartsWith(prefix))
         {
             break;
         }
         yield return(resultSelector(key, it.Value()));
     }
 }
Example #5
0
 public static IEnumerable <T> Find <T>(this DB db, ReadOptions options, Slice prefix, Func <Slice, Slice, T> resultSelector)
 {
     using (Iterator it = db.NewIterator(options))
     {
         for (it.Seek(prefix); it.Valid(); it.Next())
         {
             Slice  key = it.Key();
             byte[] x   = key.ToArray();
             byte[] y   = prefix.ToArray();
             if (x.Length < y.Length)
             {
                 break;
             }
             if (!x.Take(y.Length).SequenceEqual(y))
             {
                 break;
             }
             yield return(resultSelector(key, it.Value()));
         }
     }
 }
Example #6
0
        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(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)
                {
                    it.Next();
                }
                else
                {
                    it.Prev();
                }
            }
        }