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--; } }
public static bool Enumerate(this IKeyValueDBTransaction transaction) { if (transaction.GetKeyIndex() < 0) { return(transaction.FindFirstKey()); } if (transaction.FindNextKey()) { return(true); } transaction.InvalidateCurrentKey(); return(false); }
public AdvancedEnumerator(ODBSet <TKey> owner, AdvancedEnumeratorParam <TKey> param) { _owner = owner; _keyValueTr = _owner._keyValueTr; _ascending = param.Order == EnumerationOrder.Ascending; _prevModificationCounter = _owner._modificationCounter; _prevProtectionCounter = _keyValueTr.CursorMovedCounter; _keyValueTr.FindFirstKey(_owner._prefix); var prefixIndex = _keyValueTr.GetKeyIndex(); long startIndex; long endIndex; if (param.EndProposition == KeyProposition.Ignored) { _keyValueTr.FindLastKey(_owner._prefix); endIndex = _keyValueTr.GetKeyIndex() - prefixIndex - 1; } else { var keyBytes = _owner.KeyToByteArray(param.End); switch (_keyValueTr.Find(keyBytes, (uint)_owner._prefix.Length)) { case FindResult.Exact: endIndex = _keyValueTr.GetKeyIndex() - prefixIndex; if (param.EndProposition == KeyProposition.Excluded) { endIndex--; } break; case FindResult.Previous: endIndex = _keyValueTr.GetKeyIndex() - prefixIndex; break; case FindResult.Next: endIndex = _keyValueTr.GetKeyIndex() - prefixIndex - 1; break; case FindResult.NotFound: endIndex = -1; break; default: throw new ArgumentOutOfRangeException(); } } if (param.StartProposition == KeyProposition.Ignored) { startIndex = 0; } else { var keyBytes = _owner.KeyToByteArray(param.Start); switch (_keyValueTr.Find(keyBytes, (uint)_owner._prefix.Length)) { case FindResult.Exact: startIndex = _keyValueTr.GetKeyIndex() - prefixIndex; if (param.StartProposition == KeyProposition.Excluded) { startIndex++; } break; case FindResult.Previous: startIndex = _keyValueTr.GetKeyIndex() - prefixIndex + 1; break; case FindResult.Next: startIndex = _keyValueTr.GetKeyIndex() - prefixIndex; break; case FindResult.NotFound: startIndex = 0; break; default: throw new ArgumentOutOfRangeException(); } } _count = (uint)Math.Max(0, endIndex - startIndex + 1); _startPos = (uint)(_ascending ? startIndex : endIndex); _pos = 0; _seekState = SeekState.Undefined; }
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--; } }
public RelationAdvancedOrderedEnumerator(IRelationDbManipulator manipulator, ByteBuffer prefixBytes, uint prefixFieldCount, EnumerationOrder order, KeyProposition startKeyProposition, ByteBuffer startKeyBytes, KeyProposition endKeyProposition, ByteBuffer endKeyBytes, bool initKeyReader, int loaderIndex) { _prefixFieldCount = prefixFieldCount; _manipulator = manipulator; ItemLoader = _manipulator.RelationInfo.ItemLoaderInfos[loaderIndex]; _tr = manipulator.Transaction; _ascending = order == EnumerationOrder.Ascending; _keyValueTr = _tr.KeyValueDBTransaction; _keyValueTrProtector = _tr.TransactionProtector; _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyBytes = prefixBytes; if (endKeyProposition == KeyProposition.Included) { endKeyBytes = RelationAdvancedEnumerator <TValue> .FindLastKeyWithPrefix(_keyBytes, endKeyBytes, _keyValueTr, _keyValueTrProtector); } _keyValueTrProtector.Start(); _keyValueTr.SetKeyPrefix(_keyBytes); long startIndex; long endIndex; if (endKeyProposition == KeyProposition.Ignored) { endIndex = _keyValueTr.GetKeyValueCount() - 1; } else { switch (_keyValueTr.Find(endKeyBytes)) { case FindResult.Exact: endIndex = _keyValueTr.GetKeyIndex(); if (endKeyProposition == KeyProposition.Excluded) { endIndex--; } break; case FindResult.Previous: endIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.Next: endIndex = _keyValueTr.GetKeyIndex() - 1; break; case FindResult.NotFound: endIndex = -1; break; default: throw new ArgumentOutOfRangeException(); } } if (startKeyProposition == KeyProposition.Ignored) { startIndex = 0; } else { switch (_keyValueTr.Find(startKeyBytes)) { case FindResult.Exact: startIndex = _keyValueTr.GetKeyIndex(); if (startKeyProposition == KeyProposition.Excluded) { startIndex++; } break; case FindResult.Previous: startIndex = _keyValueTr.GetKeyIndex() + 1; break; case FindResult.Next: startIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.NotFound: startIndex = 0; break; default: throw new ArgumentOutOfRangeException(); } } _count = (uint)Math.Max(0, endIndex - startIndex + 1); _startPos = (uint)(_ascending ? startIndex : endIndex); _pos = 0; _seekState = SeekState.Undefined; if (initKeyReader) { var primaryKeyFields = manipulator.RelationInfo.ClientRelationVersionInfo.GetPrimaryKeyFields(); var advancedEnumParamField = primaryKeyFields[(int)_prefixFieldCount]; if (advancedEnumParamField.Handler.NeedsCtx()) { throw new BTDBException("Not supported."); } _keyReader = (Func <AbstractBufferedReader, IReaderCtx, TKey>)manipulator.RelationInfo .GetSimpleLoader(new RelationInfo.SimpleLoaderType(advancedEnumParamField.Handler, typeof(TKey))); _lengthOfNonDataPrefix = manipulator.RelationInfo.Prefix.Length; } }
public RelationAdvancedEnumerator( IRelationDbManipulator manipulator, ByteBuffer prefixBytes, uint prefixFieldCount, EnumerationOrder order, KeyProposition startKeyProposition, ByteBuffer startKeyBytes, KeyProposition endKeyProposition, ByteBuffer endKeyBytes, int loaderIndex) { _prefixFieldCount = prefixFieldCount; _manipulator = manipulator; ItemLoader = _manipulator.RelationInfo.ItemLoaderInfos[loaderIndex]; _ascending = order == EnumerationOrder.Ascending; _tr = manipulator.Transaction; _keyValueTr = _tr.KeyValueDBTransaction; _keyValueTrProtector = _tr.TransactionProtector; _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyBytes = prefixBytes; if (endKeyProposition == KeyProposition.Included) { endKeyBytes = FindLastKeyWithPrefix(_keyBytes, endKeyBytes, _keyValueTr, _keyValueTrProtector); } _keyValueTrProtector.Start(); _keyValueTr.SetKeyPrefix(_keyBytes); _prevModificationCounter = manipulator.ModificationCounter.ModificationCounter; long startIndex; long endIndex; if (endKeyProposition == KeyProposition.Ignored) { endIndex = _keyValueTr.GetKeyValueCount() - 1; } else { switch (_keyValueTr.Find(endKeyBytes)) { case FindResult.Exact: endIndex = _keyValueTr.GetKeyIndex(); if (endKeyProposition == KeyProposition.Excluded) { endIndex--; } break; case FindResult.Previous: endIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.Next: endIndex = _keyValueTr.GetKeyIndex() - 1; break; case FindResult.NotFound: endIndex = -1; break; default: throw new ArgumentOutOfRangeException(); } } if (startKeyProposition == KeyProposition.Ignored) { startIndex = 0; } else { switch (_keyValueTr.Find(startKeyBytes)) { case FindResult.Exact: startIndex = _keyValueTr.GetKeyIndex(); if (startKeyProposition == KeyProposition.Excluded) { startIndex++; } break; case FindResult.Previous: startIndex = _keyValueTr.GetKeyIndex() + 1; break; case FindResult.Next: startIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.NotFound: startIndex = 0; break; default: throw new ArgumentOutOfRangeException(); } } _count = (uint)Math.Max(0, endIndex - startIndex + 1); _startPos = (uint)(_ascending ? startIndex : endIndex); _pos = 0; _seekNeeded = true; _lengthOfNonDataPrefix = manipulator.RelationInfo.Prefix.Length; }
public long GetKeyIndex() { LogSimpleOperation(KVReplayOperation.GetKeyIndex); return(_tr.GetKeyIndex()); }
public long GetKeyIndex() { return(_keyValueDBTransaction.GetKeyIndex()); }
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(); } }
public AdvancedEnumerator(ODBDictionary <TKey, TValue> owner, AdvancedEnumeratorParam <TKey> param) { _owner = owner; _keyValueTrProtector = _owner._keyValueTrProtector; _keyValueTr = _owner._keyValueTr; _ascending = param.Order == EnumerationOrder.Ascending; _keyValueTrProtector.Start(); _prevModificationCounter = _owner._modificationCounter; _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyValueTr.SetKeyPrefix(_owner._prefix); long startIndex; long endIndex; if (param.EndProposition == KeyProposition.Ignored) { endIndex = _keyValueTr.GetKeyValueCount() - 1; } else { var keyBytes = _owner.KeyToByteArray(param.End); switch (_keyValueTr.Find(ByteBuffer.NewSync(keyBytes))) { case FindResult.Exact: endIndex = _keyValueTr.GetKeyIndex(); if (param.EndProposition == KeyProposition.Excluded) { endIndex--; } break; case FindResult.Previous: endIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.Next: endIndex = _keyValueTr.GetKeyIndex() - 1; break; case FindResult.NotFound: endIndex = -1; break; default: throw new ArgumentOutOfRangeException(); } } if (param.StartProposition == KeyProposition.Ignored) { startIndex = 0; } else { var keyBytes = _owner.KeyToByteArray(param.Start); switch (_keyValueTr.Find(ByteBuffer.NewSync(keyBytes))) { case FindResult.Exact: startIndex = _keyValueTr.GetKeyIndex(); if (param.StartProposition == KeyProposition.Excluded) { startIndex++; } break; case FindResult.Previous: startIndex = _keyValueTr.GetKeyIndex() + 1; break; case FindResult.Next: startIndex = _keyValueTr.GetKeyIndex(); break; case FindResult.NotFound: startIndex = 0; break; default: throw new ArgumentOutOfRangeException(); } } _count = (uint)Math.Max(0, endIndex - startIndex + 1); _startPos = (uint)(_ascending ? startIndex : endIndex); _pos = 0; _seekNeeded = true; }
public long CountWithProposition(ByteBuffer prefixBytes, KeyProposition startKeyProposition, ByteBuffer startKeyBytes, KeyProposition endKeyProposition, ByteBuffer endKeyBytes) { var keyValueTrProtector = _transaction.TransactionProtector; if (endKeyProposition == KeyProposition.Included) { endKeyBytes = RelationAdvancedEnumerator <T> .FindLastKeyWithPrefix(prefixBytes, endKeyBytes, _kvtr, keyValueTrProtector); } keyValueTrProtector.Start(); _kvtr.SetKeyPrefix(prefixBytes); long startIndex; long endIndex; if (endKeyProposition == KeyProposition.Ignored) { endIndex = _kvtr.GetKeyValueCount() - 1; } else { switch (_kvtr.Find(endKeyBytes)) { case FindResult.Exact: endIndex = _kvtr.GetKeyIndex(); if (endKeyProposition == KeyProposition.Excluded) { endIndex--; } break; case FindResult.Previous: endIndex = _kvtr.GetKeyIndex(); break; case FindResult.Next: endIndex = _kvtr.GetKeyIndex() - 1; break; case FindResult.NotFound: endIndex = -1; break; default: throw new ArgumentOutOfRangeException(); } } if (startKeyProposition == KeyProposition.Ignored) { startIndex = 0; } else { switch (_kvtr.Find(startKeyBytes)) { case FindResult.Exact: startIndex = _kvtr.GetKeyIndex(); if (startKeyProposition == KeyProposition.Excluded) { startIndex++; } break; case FindResult.Previous: startIndex = _kvtr.GetKeyIndex() + 1; break; case FindResult.Next: startIndex = _kvtr.GetKeyIndex(); break; case FindResult.NotFound: startIndex = 0; break; default: throw new ArgumentOutOfRangeException(); } } return(Math.Max(0, endIndex - startIndex + 1)); }