public void IterateRelation(ODBIteratorRelationInfo relation) { var prefix = BuildRelationPrefix(relation.Id); long prevProtectionCounter = 0; long pos = 0; while (true) { if (pos == 0) { if (!_trkv.FindFirstKey(prefix)) { break; } } else { if (_trkv.CursorMovedCounter != prevProtectionCounter) { if (!_trkv.SetKeyIndex(prefix, pos)) { break; } } else { if (!_trkv.FindNextKey(prefix)) { break; } } } _fastVisitor.MarkCurrentKeyAsUsed(_trkv); prevProtectionCounter = _trkv.CursorMovedCounter; if (_visitor == null || _visitor.StartRelationKey()) { var keyReader = new SpanReader(_trkv.GetKey().Slice(prefix.Length)); var relationInfo = relation.VersionInfos[relation.LastPersistedVersion]; IterateFields(ref keyReader, relationInfo.PrimaryKeyFields.Span, null); _visitor?.EndRelationKey(); } if (_trkv.CursorMovedCounter != prevProtectionCounter) { if (!_trkv.SetKeyIndex(prefix, pos)) { break; } } if (_visitor == null || _visitor.StartRelationValue()) { var valueReader = new SpanReader(_trkv.GetValue()); var version = valueReader.ReadVUInt32(); var relationInfo = relation.VersionInfos[version]; IterateFields(ref valueReader, relationInfo.Fields.Span, new HashSet <int>()); _visitor?.EndRelationValue(); } pos++; } }
public IEnumerator <TKey> GetEnumerator() { long prevProtectionCounter = 0; var prevModificationCounter = 0; long pos = 0; while (true) { _keyValueTrProtector.Start(); if (pos == 0) { prevModificationCounter = _modificationCounter; _keyValueTr.SetKeyPrefix(_prefix); if (!_keyValueTr.FindFirstKey()) { break; } } else { if (_keyValueTrProtector.WasInterupted(prevProtectionCounter)) { if (prevModificationCounter != _modificationCounter) { ThrowModifiedDuringEnum(); } _keyValueTr.SetKeyPrefix(_prefix); if (!_keyValueTr.SetKeyIndex(pos)) { break; } } else { if (!_keyValueTr.FindNextKey()) { break; } } } prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; var keyBytes = _keyValueTr.GetKey(); var key = ByteArrayToKey(keyBytes); yield return(key); pos++; } }
void LoadVersionInfos(IKeyValueDBTransaction tr) { LastPersistedVersion = 0; var writer = new ByteBufferWriter(); writer.WriteByteArrayRaw(ObjectDB.RelationVersionsPrefix); writer.WriteVUInt32(_id); tr.SetKeyPrefix(writer.Data); if (!tr.FindFirstKey()) { return; } var keyReader = new KeyValueDBKeyReader(tr); var valueReader = new KeyValueDBValueReader(tr); do { keyReader.Restart(); valueReader.Restart(); LastPersistedVersion = keyReader.ReadVUInt32(); var relationVersionInfo = RelationVersionInfo.Load(valueReader, _relationInfoResolver.FieldHandlerFactory, _name); _relationVersions[LastPersistedVersion] = relationVersionInfo; } while (tr.FindNextKey()); }
/// <summary> /// Writes all key value pairs in current prefix to stream (prefix itself is not written) /// </summary> /// <param name="transaction">transaction from where export all data</param> /// <param name="stream">where to write it to</param> public static void Export(IKeyValueDBTransaction transaction, Stream stream) { if (transaction == null) throw new ArgumentNullException(nameof(transaction)); if (stream == null) throw new ArgumentNullException(nameof(stream)); if (!stream.CanWrite) throw new ArgumentException("stream must be writeable", nameof(stream)); var keyValueCount = transaction.GetKeyValueCount(); var tempbuf = new byte[16]; tempbuf[0] = (byte)'B'; tempbuf[1] = (byte)'T'; tempbuf[2] = (byte)'D'; tempbuf[3] = (byte)'B'; tempbuf[4] = (byte)'E'; tempbuf[5] = (byte)'X'; tempbuf[6] = (byte)'P'; tempbuf[7] = (byte)'2'; PackUnpack.PackInt64LE(tempbuf, 8, keyValueCount); stream.Write(tempbuf, 0, 16); transaction.FindFirstKey(); for (long kv = 0; kv < keyValueCount; kv++) { var key = transaction.GetKey(); PackUnpack.PackInt32LE(tempbuf, 0, key.Length); stream.Write(tempbuf, 0, 4); stream.Write(key.Buffer, key.Offset, key.Length); var value = transaction.GetValue(); PackUnpack.PackInt32LE(tempbuf, 0, value.Length); stream.Write(tempbuf, 0, 4); stream.Write(value.Buffer, value.Offset, value.Length); transaction.FindNextKey(); } }
/// <summary> /// Writes all key value pairs in current prefix to stream (prefix itself is not written) /// </summary> /// <param name="transaction">transaction from where export all data</param> /// <param name="stream">where to write it to</param> public static void Export(IKeyValueDBTransaction transaction, Stream stream) { if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanWrite) { throw new ArgumentException("stream must be writeable", nameof(stream)); } var keyValueCount = transaction.GetKeyValueCount(); var tempbuf = new byte[16]; tempbuf[0] = (byte)'B'; tempbuf[1] = (byte)'T'; tempbuf[2] = (byte)'D'; tempbuf[3] = (byte)'B'; tempbuf[4] = (byte)'E'; tempbuf[5] = (byte)'X'; tempbuf[6] = (byte)'P'; tempbuf[7] = (byte)'2'; PackUnpack.PackInt64LE(tempbuf, 8, keyValueCount); stream.Write(tempbuf, 0, 16); transaction.FindFirstKey(new ReadOnlySpan <byte>()); Span <byte> keyBuffer = stackalloc byte[256]; for (long kv = 0; kv < keyValueCount; kv++) { var key = transaction.GetKey(ref MemoryMarshal.GetReference(keyBuffer), keyBuffer.Length); PackUnpack.PackInt32LE(tempbuf, 0, key.Length); stream.Write(tempbuf, 0, 4); stream.Write(key); var value = transaction.GetValue(); PackUnpack.PackInt32LE(tempbuf, 0, value.Length); stream.Write(tempbuf, 0, 4); stream.Write(value); transaction.FindNextKey(new ReadOnlySpan <byte>()); } var ulongCount = transaction.GetUlongCount(); if (transaction.GetCommitUlong() != 0 || ulongCount != 0) { PackUnpack.PackUInt64LE(tempbuf, 0, transaction.GetCommitUlong()); stream.Write(tempbuf, 0, 8); } if (ulongCount != 0) { PackUnpack.PackUInt32LE(tempbuf, 0, ulongCount); stream.Write(tempbuf, 0, 4); for (var i = 0u; i < ulongCount; i++) { PackUnpack.PackUInt64LE(tempbuf, 0, transaction.GetUlong(i)); stream.Write(tempbuf, 0, 8); } } }
/// <summary> /// Writes all key value pairs in current prefix to stream (prefix itself is not written) /// </summary> /// <param name="transaction">transaction from where export all data</param> /// <param name="stream">where to write it to</param> public static void Export(IKeyValueDBTransaction transaction, Stream stream) { if (transaction == null) { throw new ArgumentNullException("transaction"); } if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanWrite) { throw new ArgumentException("stream must be writeable", "stream"); } var keyValueCount = transaction.GetKeyValueCount(); var tempbuf = new byte[16]; tempbuf[0] = (byte)'B'; tempbuf[1] = (byte)'T'; tempbuf[2] = (byte)'D'; tempbuf[3] = (byte)'B'; tempbuf[4] = (byte)'E'; tempbuf[5] = (byte)'X'; tempbuf[6] = (byte)'P'; tempbuf[7] = (byte)'1'; PackUnpack.PackInt64LE(tempbuf, 8, keyValueCount); stream.Write(tempbuf, 0, 16); transaction.FindFirstKey(); for (long kv = 0; kv < keyValueCount; kv++) { var keySize = transaction.GetKeySize(); PackUnpack.PackInt32LE(tempbuf, 0, keySize); stream.Write(tempbuf, 0, 4); long ofs = 0; while (ofs < keySize) { int len; byte[] buf; int bufOfs; transaction.PeekKey((int)ofs, out len, out buf, out bufOfs); stream.Write(buf, bufOfs, len); ofs += len; } var valueSize = transaction.GetValueSize(); PackUnpack.PackInt64LE(tempbuf, 0, valueSize); stream.Write(tempbuf, 0, 8); ofs = 0; while (ofs < valueSize) { int len; byte[] buf; int bufOfs; transaction.PeekValue(ofs, out len, out buf, out bufOfs); stream.Write(buf, bufOfs, len); ofs += len; } transaction.FindNextKey(); } }
public IEnumerator <TKey> GetEnumerator() { long prevProtectionCounter = 0; var prevModificationCounter = 0; long pos = 0; while (true) { if (pos == 0) { prevModificationCounter = _modificationCounter; if (!_keyValueTr.FindFirstKey(_prefix)) { break; } } else { if (_keyValueTr.CursorMovedCounter != prevProtectionCounter) { if (prevModificationCounter != _modificationCounter) { ThrowModifiedDuringEnum(); } if (!_keyValueTr.SetKeyIndex(_prefix, pos)) { break; } } else { if (!_keyValueTr.FindNextKey(_prefix)) { break; } } } prevProtectionCounter = _keyValueTr.CursorMovedCounter; var key = CurrentToKey(); yield return(key); pos++; } }
public static bool Enumerate(this IKeyValueDBTransaction transaction) { if (transaction.GetKeyIndex() < 0) { return(transaction.FindFirstKey()); } if (transaction.FindNextKey()) { return(true); } transaction.InvalidateCurrentKey(); return(false); }
/// <summary> /// Writes all key value pairs in current prefix to stream (prefix itself is not written) /// </summary> /// <param name="transaction">transaction from where export all data</param> /// <param name="stream">where to write it to</param> public static void Export(IKeyValueDBTransaction transaction, Stream stream) { if (transaction == null) throw new ArgumentNullException("transaction"); if (stream == null) throw new ArgumentNullException("stream"); if (!stream.CanWrite) throw new ArgumentException("stream must be writeable", "stream"); var keyValueCount = transaction.GetKeyValueCount(); var tempbuf = new byte[16]; tempbuf[0] = (byte)'B'; tempbuf[1] = (byte)'T'; tempbuf[2] = (byte)'D'; tempbuf[3] = (byte)'B'; tempbuf[4] = (byte)'E'; tempbuf[5] = (byte)'X'; tempbuf[6] = (byte)'P'; tempbuf[7] = (byte)'1'; PackUnpack.PackInt64LE(tempbuf, 8, keyValueCount); stream.Write(tempbuf, 0, 16); transaction.FindFirstKey(); for (long kv = 0; kv < keyValueCount; kv++) { var keySize = transaction.GetKeySize(); PackUnpack.PackInt32LE(tempbuf, 0, keySize); stream.Write(tempbuf, 0, 4); long ofs = 0; while (ofs < keySize) { int len; byte[] buf; int bufOfs; transaction.PeekKey((int)ofs, out len, out buf, out bufOfs); stream.Write(buf, bufOfs, len); ofs += len; } var valueSize = transaction.GetValueSize(); PackUnpack.PackInt64LE(tempbuf, 0, valueSize); stream.Write(tempbuf, 0, 8); ofs = 0; while (ofs < valueSize) { int len; byte[] buf; int bufOfs; transaction.PeekValue(ofs, out len, out buf, out bufOfs); stream.Write(buf, bufOfs, len); ofs += len; } transaction.FindNextKey(); } }
void ImportKeysWithPrefix(byte[] prefix, IKeyValueDBTransaction sourceKvTr) { sourceKvTr.SetKeyPrefix(prefix); if (!sourceKvTr.FindFirstKey()) return; using (var kvtr = _keyValueDb.StartWritingTransaction().Result) { kvtr.SetKeyPrefix(prefix); do { //create all keys, instead of value store only byte length of value kvtr.CreateOrUpdateKeyValue(sourceKvTr.GetKey(), Vuint2ByteBuffer(sourceKvTr.GetStorageSizeOfCurrentKey().Value)); } while (sourceKvTr.FindNextKey()); kvtr.Commit(); } }
void ImportKeysWithPrefix(byte[] prefix, IKeyValueDBTransaction sourceKvTr) { if (!sourceKvTr.FindFirstKey(prefix)) { return; } using (var kvtr = _keyValueDb.StartWritingTransaction().Result) { do { //create all keys, instead of value store only byte length of value kvtr.CreateOrUpdateKeyValue(sourceKvTr.GetKey(), Vuint2ByteBuffer(sourceKvTr.GetStorageSizeOfCurrentKey().Value)); } while (sourceKvTr.FindNextKey(prefix)); kvtr.Commit(); } }
/// <summary> /// Writes all key value pairs in current prefix to stream (prefix itself is not written) /// </summary> /// <param name="transaction">transaction from where export all data</param> /// <param name="stream">where to write it to</param> public static void Export(IKeyValueDBTransaction transaction, Stream stream) { if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanWrite) { throw new ArgumentException("stream must be writeable", nameof(stream)); } var keyValueCount = transaction.GetKeyValueCount(); var tempbuf = new byte[16]; tempbuf[0] = (byte)'B'; tempbuf[1] = (byte)'T'; tempbuf[2] = (byte)'D'; tempbuf[3] = (byte)'B'; tempbuf[4] = (byte)'E'; tempbuf[5] = (byte)'X'; tempbuf[6] = (byte)'P'; tempbuf[7] = (byte)'2'; PackUnpack.PackInt64LE(tempbuf, 8, keyValueCount); stream.Write(tempbuf, 0, 16); transaction.FindFirstKey(); for (long kv = 0; kv < keyValueCount; kv++) { var key = transaction.GetKey(); PackUnpack.PackInt32LE(tempbuf, 0, key.Length); stream.Write(tempbuf, 0, 4); stream.Write(key.Buffer, key.Offset, key.Length); var value = transaction.GetValue(); PackUnpack.PackInt32LE(tempbuf, 0, value.Length); stream.Write(tempbuf, 0, 4); stream.Write(value.Buffer, value.Offset, value.Length); transaction.FindNextKey(); } if (transaction.GetCommitUlong() != 0) { PackUnpack.PackUInt64LE(tempbuf, 0, transaction.GetCommitUlong()); stream.Write(tempbuf, 0, 8); } }
void IterateRelation(uint relationIndex, string name) { var relationVersions = new Dictionary <uint, RelationVersionInfo>(); var lastPersistedVersion = ReadRelationVersions(relationIndex, name, relationVersions); _tr.TransactionProtector.Start(); var o = ObjectDB.AllRelationsPKPrefix.Length; var prefix = new byte[o + PackUnpack.LengthVUInt(relationIndex)]; Array.Copy(ObjectDB.AllRelationsPKPrefix, prefix, o); PackUnpack.PackVUInt(prefix, ref o, relationIndex); var protector = _tr.TransactionProtector; long prevProtectionCounter = 0; long pos = 0; while (true) { protector.Start(); if (pos == 0) { _trkv.SetKeyPrefix(prefix); if (!_trkv.FindFirstKey()) { break; } } else { if (protector.WasInterupted(prevProtectionCounter)) { _trkv.SetKeyPrefix(prefix); if (!_trkv.SetKeyIndex(pos)) { break; } } else { if (!_trkv.FindNextKey()) { break; } } } _fastVisitor.MarkCurrentKeyAsUsed(_trkv); prevProtectionCounter = protector.ProtectionCounter; if (_visitor == null || _visitor.StartRelationKey()) { var keyReader = new KeyValueDBKeyReader(_trkv); var relationInfo = relationVersions[lastPersistedVersion]; IterateFields(keyReader, relationInfo.GetPrimaryKeyFields(), null); _visitor?.EndRelationKey(); } if (protector.WasInterupted(prevProtectionCounter)) { _trkv.SetKeyPrefix(prefix); if (!_trkv.SetKeyIndex(pos)) { break; } } if (_visitor == null || _visitor.StartRelationValue()) { var valueReader = new KeyValueDBValueReader(_trkv); var version = valueReader.ReadVUInt32(); var relationInfo = relationVersions[version]; IterateFields(valueReader, relationInfo.GetValueFields(), new HashSet <int>()); _visitor?.EndRelationValue(); } pos++; } }
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); } }
void FreeContent(IReaderCtx readerCtx, int cfgId) { var config = ODBDictionaryConfiguration.Get(cfgId); var ctx = (DBReaderWithFreeInfoCtx)readerCtx; if (config.FreeContent == null) { var method = ILBuilder.Instance.NewMethod <FreeContentFun>($"IDictFinder_Cfg_{cfgId}"); var ilGenerator = method.Generator; var readerLoc = ilGenerator.DeclareLocal(typeof(IReaderCtx)); ilGenerator .Ldarg(0) .Ldarg(2) // ReSharper disable once ObjectCreationAsStatement .Newobj(() => new DBReaderWithFreeInfoCtx(null, null)) .Stloc(readerLoc); var readerOrCtx = _valueHandler.NeedsCtx() ? (Action <IILGen>?)(il => il.Ldloc(readerLoc)) : null; _valueHandler.FreeContent(ilGenerator, il => il.Ldarg(1), readerOrCtx); ilGenerator.Ret(); config.FreeContent = method.Create(); } var findIDictAction = (FreeContentFun)config.FreeContent; long prevProtectionCounter = 0; long pos = 0; while (true) { if (pos == 0) { if (!_keyValueTr.FindFirstKey(_prefix)) { break; } } else { if (_keyValueTr.CursorMovedCounter != prevProtectionCounter) { if (!_keyValueTr.SetKeyIndex(_prefix, pos)) { break; } } else { if (!_keyValueTr.FindNextKey(_prefix)) { break; } } } prevProtectionCounter = _keyValueTr.CursorMovedCounter; var valueBytes = _keyValueTr.GetValue(); var valueReader = new SpanReader(valueBytes); findIDictAction(ctx.GetTransaction(), ref valueReader, ctx.DictIds); pos++; } }
public bool FindFirstKey() { LogSimpleOperation(KVReplayOperation.FindFirstKey); return(_tr.FindFirstKey()); }
public bool AnyWithPrefix(ByteBuffer keyBytesPrefix) { _transaction.TransactionProtector.Start(); _kvtr.SetKeyPrefix(keyBytesPrefix); return(_kvtr.FindFirstKey()); }
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++; } }
public bool FindFirstKey() { return(_keyValueDBTransaction.FindFirstKey()); }
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 <object> Enumerate(Type type) { if (type == typeof(object)) { type = null; } else if (type != null) { AutoRegisterType(type); } var taken = false; ulong oid = 0; ulong finalOid = _owner.GetLastAllocatedOid(); long prevProtectionCounter = 0; try { while (true) { if (!taken) { _keyValueTrProtector.Start(ref taken); } if (oid == 0) { prevProtectionCounter = _keyValueTrProtector.ProtectionCounter; _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix); if (!_keyValueTr.FindFirstKey()) { break; } } else { if (_keyValueTrProtector.WasInterupted(prevProtectionCounter)) { _keyValueTr.SetKeyPrefix(ObjectDB.AllObjectsPrefix); oid++; byte[] key = BuildKeyFromOid(oid); if (_keyValueTr.FindKey(key, 0, key.Length, FindKeyStrategy.OnlyNext) == FindKeyResult.NotFound) { oid--; break; } } else { if (!_keyValueTr.FindNextKey()) { break; } } } oid = ReadOidFromCurrentKeyInTransaction(); WeakReference weakObj; if (_objCache.TryGetValue(oid, out weakObj)) { var o = weakObj.Target; if (o != null) { if (type == null || type.IsAssignableFrom(o.GetType())) { _keyValueTrProtector.Stop(ref taken); yield return(o); continue; } continue; } } TableInfo tableInfo; KeyValueDBValueReader reader = ReadObjStart(oid, out tableInfo); if (type != null && !type.IsAssignableFrom(tableInfo.ClientType)) { continue; } object obj = ReadObjFinish(oid, tableInfo, reader); _keyValueTrProtector.Stop(ref taken); yield return(obj); } } finally { if (taken) { _keyValueTrProtector.Stop(); } } 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) { object obj = dObjPair.Value; if (type != null && !type.IsAssignableFrom(obj.GetType())) { continue; } yield return(obj); } }