public RelationAdvancedEnumerator( IRelationDbManipulator manipulator, ByteBuffer prefixBytes, uint prefixFieldCount, int loaderIndex) { _prefixFieldCount = prefixFieldCount; _manipulator = manipulator; ItemLoader = _manipulator.RelationInfo.ItemLoaderInfos[loaderIndex]; _ascending = true; _tr = manipulator.Transaction; _keyValueTr = _tr.KeyValueDBTransaction; _keyValueTrProtector = _tr.TransactionProtector; _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyBytes = prefixBytes; _keyValueTrProtector.Start(); _keyValueTr.SetKeyPrefix(_keyBytes); _prevModificationCounter = manipulator.ModificationCounter.ModificationCounter; _count = (uint)_keyValueTr.GetKeyValueCount(); _startPos = _ascending ? 0 : _count - 1; _pos = 0; _seekNeeded = true; _lengthOfNonDataPrefix = manipulator.RelationInfo.Prefix.Length; }
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); }
public bool MoveNext() { if (!_seekNeeded) { _pos++; } _keyValueTrProtector.Start(); if (_keyValueTrProtector.WasInterupted(_prevProtectionCounter)) { _modificationCounter.CheckModifiedDuringEnum(_prevModificationCounter); _keyValueTr.SetKeyPrefix(KeyBytes); } var ret = Seek(); _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; return(ret); }
internal static ByteBuffer FindLastKeyWithPrefix(ByteBuffer keyBytes, ByteBuffer endKeyBytes, IKeyValueDBTransaction keyValueTr, KeyValueDBTransactionProtector keyValueTrProtector) { var buffer = ByteBuffer.NewEmpty(); buffer = buffer.ResizingAppend(keyBytes).ResizingAppend(endKeyBytes); keyValueTrProtector.Start(); keyValueTr.SetKeyPrefix(buffer); if (!keyValueTr.FindLastKey()) { return(endKeyBytes); } var key = keyValueTr.GetKeyIncludingPrefix(); return(key.Slice(keyBytes.Length)); }
public RelationEnumerator(IInternalObjectDBTransaction tr, RelationInfo relationInfo, ByteBuffer keyBytes, IRelationModificationCounter modificationCounter) { RelationInfo = relationInfo; Transaction = tr; _keyValueTr = Transaction.KeyValueDBTransaction; _keyValueTrProtector = Transaction.TransactionProtector; _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; KeyBytes = keyBytes; _modificationCounter = modificationCounter; _keyValueTrProtector.Start(); _keyValueTr.SetKeyPrefix(KeyBytes); _pos = 0; _seekNeeded = true; _prevModificationCounter = _modificationCounter.ModificationCounter; }
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); }
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); }
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); }
public AbstractBufferedWriter PrepareToWriteObject(ulong id) { var shouldStop = false; try { _keyValueTrProtector.Start(ref shouldStop); _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix); var key = new byte[PackUnpack.LengthVUInt(id)]; var ofs = 0; PackUnpack.PackVUInt(key, ref ofs, id); _keyValueTr.CreateKey(key); var writer = new KeyValueDBValueProtectedWriter(_keyValueTr, _keyValueTrProtector); shouldStop = false; return(writer); } finally { if (shouldStop) { _keyValueTrProtector.Stop(); } } }
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 IEnumerable <object> Enumerate(Type type) { if (type == typeof(object)) { type = null; } else if (type != null) { AutoRegisterType(type); } ulong oid = 0; ulong finalOid = _owner.GetLastAllocatedOid(); long prevProtectionCounter = 0; while (true) { _keyValueTrProtector.Start(); if (oid == 0) { prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix); if (!_keyValueTr.FindFirstKey()) { break; } } else { if (_keyValueTrProtector.WasInterupted(prevProtectionCounter)) { _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix); oid++; var key = BuildKeyFromOid(oid); var result = _keyValueTr.Find(ByteBuffer.NewSync(key)); if (result == FindResult.Previous) { if (!_keyValueTr.FindNextKey()) { result = FindResult.NotFound; } } if (result == FindResult.NotFound) { oid--; break; } } else { if (!_keyValueTr.FindNextKey()) { break; } } prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; } oid = ReadOidFromCurrentKeyInTransaction(); var o = GetObjFromObjCacheByOid(oid); if (o != null) { if (type == null || type.IsInstanceOfType(o)) { yield return(o); } continue; } TableInfo tableInfo; var reader = ReadObjStart(oid, out tableInfo); if (type != null && !type.IsAssignableFrom(tableInfo.ClientType)) { continue; } var obj = ReadObjFinish(oid, tableInfo, reader); yield return(obj); } if (_dirtyObjSet == null) { yield break; } var dirtyObjsToEnum = _dirtyObjSet.Where(p => p.Key > oid && p.Key <= finalOid).ToList(); dirtyObjsToEnum.Sort((p1, p2) => { if (p1.Key < p2.Key) { return(-1); } if (p1.Key > p2.Key) { return(1); } return(0); }); foreach (var dObjPair in dirtyObjsToEnum) { var obj = dObjPair.Value; if (type != null && !type.IsInstanceOfType(obj)) { continue; } yield return(obj); } }
public AdvancedEnumerator(ODBSet <TKey> 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(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(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; _seekState = SeekState.Undefined; }
public void Clear() { _keyValueTrProtector.Start(); _modificationCounter++; _keyValueTr.SetKeyPrefix(_prefix); _keyValueTr.EraseAll(); _count = 0; }
void FreeContent(IReaderCtx readerCtx, int cfgId) { var config = (ODBDictionaryConfiguration)((IInstanceRegistry)readerCtx).FindInstance(cfgId); var ctx = (DBReaderWithFreeInfoCtx)readerCtx; if (config.FreeContent == null) { var method = ILBuilder.Instance .NewMethod <Action <IInternalObjectDBTransaction, AbstractBufferedReader, IList <ulong> > >( $"IDictFinder_Cfg_{cfgId}"); var ilGenerator = method.Generator; var readerLoc = ilGenerator.DeclareLocal(typeof(IReaderCtx)); ilGenerator .Ldarg(0) .Ldarg(1) .Ldarg(2) .Newobj(() => new DBReaderWithFreeInfoCtx(null, null, null)) .Stloc(readerLoc); Action <IILGen> readerOrCtx; if (_valueHandler.NeedsCtx()) { readerOrCtx = il => il.Ldloc(readerLoc); } else { readerOrCtx = il => il.Ldarg(1); } _valueHandler.FreeContent(ilGenerator, readerOrCtx); ilGenerator.Ret(); config.FreeContent = method.Create(); } var findIDictAction = (Action <IInternalObjectDBTransaction, AbstractBufferedReader, IList <ulong> >)config.FreeContent; long prevProtectionCounter = 0; long pos = 0; while (true) { _keyValueTrProtector.Start(); if (pos == 0) { _keyValueTr.SetKeyPrefix(_prefix); if (!_keyValueTr.FindFirstKey()) { break; } } else { if (_keyValueTrProtector.WasInterupted(prevProtectionCounter)) { _keyValueTr.SetKeyPrefix(_prefix); if (!_keyValueTr.SetKeyIndex(pos)) { break; } } else { if (!_keyValueTr.FindNextKey()) { break; } } } prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; var valueBytes = _keyValueTr.GetValueAsByteArray(); var valueReader = new ByteArrayReader(valueBytes); findIDictAction(ctx.GetTransaction(), valueReader, ctx.DictIds); pos++; } }