Esempio n. 1
0
        public IEnumerable <KeyValuePair <TKey, TValue> > GetReverseEnumerator()
        {
            long prevProtectionCounter   = 0;
            var  prevModificationCounter = 0;
            var  pos = long.MaxValue;

            while (true)
            {
                _keyValueTrProtector.Start();
                if (pos == long.MaxValue)
                {
                    prevModificationCounter = _modificationCounter;
                    _keyValueTr.SetKeyPrefix(_prefix);
                    if (!_keyValueTr.FindLastKey())
                    {
                        break;
                    }
                    pos = _keyValueTr.GetKeyIndex();
                }
                else
                {
                    if (_keyValueTrProtector.WasInterupted(prevProtectionCounter))
                    {
                        if (prevModificationCounter != _modificationCounter)
                        {
                            ThrowModifiedDuringEnum();
                        }
                        _keyValueTr.SetKeyPrefix(_prefix);
                        if (!_keyValueTr.SetKeyIndex(pos))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!_keyValueTr.FindPreviousKey())
                        {
                            break;
                        }
                    }
                }
                prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
                var keyBytes   = _keyValueTr.GetKeyAsByteArray();
                var valueBytes = _keyValueTr.GetValueAsByteArray();
                var key        = ByteArrayToKey(keyBytes);
                var value      = ByteArrayToValue(valueBytes);
                yield return(new KeyValuePair <TKey, TValue>(key, value));

                pos--;
            }
        }
Esempio n. 2
0
 public bool MoveNext()
 {
     if (!_seekNeeded)
     {
         _pos++;
     }
     if (_pos >= _count)
     {
         return(false);
     }
     _keyValueTrProtector.Start();
     if (_keyValueTrProtector.WasInterupted(_prevProtectionCounter))
     {
         _manipulator.CheckModifiedDuringEnum(_prevModificationCounter);
         _keyValueTr.SetKeyPrefix(_keyBytes);
         Seek();
     }
     else if (_seekNeeded)
     {
         Seek();
     }
     else
     {
         if (_ascending)
         {
             _keyValueTr.FindNextKey();
         }
         else
         {
             _keyValueTr.FindPreviousKey();
         }
     }
     _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
     return(true);
 }
Esempio n. 3
0
        public IEnumerable <TKey> GetReverseEnumerator()
        {
            long prevProtectionCounter   = 0;
            var  prevModificationCounter = 0;
            var  pos = long.MaxValue;

            while (true)
            {
                if (pos == long.MaxValue)
                {
                    prevModificationCounter = _modificationCounter;
                    if (!_keyValueTr.FindLastKey(_prefix))
                    {
                        break;
                    }
                    pos = _keyValueTr.GetKeyIndex();
                }
                else
                {
                    if (_keyValueTr.CursorMovedCounter != prevProtectionCounter)
                    {
                        if (prevModificationCounter != _modificationCounter)
                        {
                            ThrowModifiedDuringEnum();
                        }
                        if (!_keyValueTr.SetKeyIndex(_prefix, pos))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!_keyValueTr.FindPreviousKey(_prefix))
                        {
                            break;
                        }
                    }
                }

                prevProtectionCounter = _keyValueTr.CursorMovedCounter;
                var key = CurrentToKey();
                yield return(key);

                pos--;
            }
        }
Esempio n. 4
0
        public bool NextKey(out TKey key)
        {
            if (_seekState == SeekState.Ready)
            {
                _pos++;
            }
            if (_pos >= _count)
            {
                key = default(TKey);
                return(false);
            }

            _keyValueTrProtector.Start();
            if (_keyValueTrProtector.WasInterupted(_prevProtectionCounter))
            {
                _keyValueTr.SetKeyPrefix(_keyBytes);
                Seek();
            }
            else if (_seekState != SeekState.Ready)
            {
                Seek();
            }
            else
            {
                if (_ascending)
                {
                    _keyValueTr.FindNextKey();
                }
                else
                {
                    _keyValueTr.FindPreviousKey();
                }
            }

            _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
            //read key
            var keyData = _keyValueTr.GetKeyAsByteArray();
            var reader  = new ByteArrayReader(keyData);

            key = _keyReader(reader, null);
            return(true);
        }
Esempio n. 5
0
            public bool MoveNext()
            {
                if (_seekState == SeekState.Ready)
                {
                    _pos++;
                }
                if (_pos >= _count)
                {
                    Current = default;
                    return(false);
                }

                _keyValueTrProtector.Start();
                if (_keyValueTrProtector.WasInterupted(_prevProtectionCounter))
                {
                    if (_prevModificationCounter != _owner._modificationCounter)
                    {
                        ThrowModifiedDuringEnum();
                    }
                    _keyValueTr.SetKeyPrefix(_owner._prefix);
                    Seek();
                }
                else if (_seekState != SeekState.Ready)
                {
                    Seek();
                }
                else
                {
                    if (_ascending)
                    {
                        _keyValueTr.FindNextKey();
                    }
                    else
                    {
                        _keyValueTr.FindPreviousKey();
                    }
                }

                _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
                Current = _owner.ByteArrayToKey(_keyValueTr.GetKey());
                return(true);
            }
Esempio n. 6
0
            public bool MoveNext()
            {
                if (_seekState == SeekState.Ready)
                {
                    _pos++;
                }
                if (_pos >= _count)
                {
                    Current = default;
                    return(false);
                }

                if (_keyValueTr.CursorMovedCounter != _prevProtectionCounter)
                {
                    if (_prevModificationCounter != _owner._modificationCounter)
                    {
                        ThrowModifiedDuringEnum();
                    }
                    Seek();
                }
                else if (_seekState != SeekState.Ready)
                {
                    Seek();
                }
                else
                {
                    if (_ascending)
                    {
                        _keyValueTr.FindNextKey(_owner._prefix);
                    }
                    else
                    {
                        _keyValueTr.FindPreviousKey(_owner._prefix);
                    }
                }

                _prevProtectionCounter = _keyValueTr.CursorMovedCounter;
                Current = _owner.CurrentToKey();
                return(true);
            }
Esempio n. 7
0
 public bool NextKey(out TKey key)
 {
     if (!_seekNeeded)
     {
         _pos++;
     }
     if (_pos >= _count)
     {
         key = default(TKey);
         return(false);
     }
     _keyValueTrProtector.Start();
     if (_keyValueTrProtector.WasInterupted(_prevProtectionCounter))
     {
         if (_prevModificationCounter != _owner._modificationCounter)
         {
             ThrowModifiedDuringEnum();
         }
         _keyValueTr.SetKeyPrefix(_owner._prefix);
         Seek();
     }
     else if (_seekNeeded)
     {
         Seek();
     }
     else
     {
         if (_ascending)
         {
             _keyValueTr.FindNextKey();
         }
         else
         {
             _keyValueTr.FindPreviousKey();
         }
     }
     key = _owner.ByteArrayToKey(_keyValueTr.GetKeyAsByteArray());
     return(true);
 }
Esempio n. 8
0
 public bool FindPreviousKey()
 {
     LogSimpleOperation(KVReplayOperation.FindPreviousKey);
     return(_tr.FindPreviousKey());
 }
 public bool FindPreviousKey()
 {
     return(_keyValueDBTransaction.FindPreviousKey());
 }
Esempio n. 10
0
        void DoRandomWork()
        {
            var opCounter = 0;
            var random    = new Random();

            using (var stream = CreateTestStream())
                //using (IKeyValueDB db = new KeyValueDBReplayProxy(new KeyValueDB(), new PositionLessStreamWriter(new PositionLessStreamProxy("btdb.log"))))
                using (IKeyValueDB db = new KeyValueDB())
                {
                    db.Open(stream, false);
                    IKeyValueDBTransaction tr = db.StartTransaction();
                    while (opCounter < 100000)
                    {
                        if (opCounter % 1000 == 0)
                        {
                            Console.WriteLine(string.Format("Operation {0}", opCounter));
                            Console.WriteLine(tr.CalculateStats().ToString());
                        }
                        opCounter++;
                        var action = random.Next(100);
                        if (action < 10)
                        {
                            if (action > 1)
                            {
                                //Console.WriteLine("Commit");
                                tr.Commit();
                            }
                            else
                            {
                                //Console.WriteLine("Rollback");
                            }
                            tr.Dispose();
                            tr = db.StartTransaction();
                        }
                        else if (action < 50 || tr.GetKeyIndex() < 0)
                        {
                            var key = new byte[random.Next(1, 1000)];
                            random.NextBytes(key);
                            //Console.WriteLine(string.Format("CreateKey {0}", key.Length));
                            tr.CreateKey(key);
                        }
                        else if (action < 60)
                        {
                            //Console.WriteLine("EraseCurrent");
                            tr.EraseCurrent();
                        }
                        else if (action < 65)
                        {
                            //Console.WriteLine("FindNextKey");
                            tr.FindNextKey();
                        }
                        else if (action < 70)
                        {
                            //Console.WriteLine("FindPreviousKey");
                            tr.FindPreviousKey();
                        }
                        else
                        {
                            var value = new byte[random.Next(1, 100000)];
                            random.NextBytes(value);
                            //Console.WriteLine(string.Format("SetValue {0}", value.Length));
                            tr.SetValue(value);
                        }
                    }
                    tr.Dispose();
                }
        }