Exemple #1
0
 /// <summary>
 /// 查询前缀匹配的元素
 /// </summary>
 /// <typeparam name="T">值泛型</typeparam>
 /// <param name="db">待查询db</param>
 /// <param name="options">读选项</param>
 /// <param name="prefix">待查询前缀</param>
 /// <param name="resultSelector">值处理回调函数</param>
 /// <returns>T列表</returns>
 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()));
         }
     }
 }
Exemple #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);
            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();
                }
            }
        }