public void MarkCurrentKeyAsUsed(IKeyValueDBTransaction tr) { Keys = Keys.ResizingAppend(ByteBuffer.NewAsync(tr.GetKey())); Builder.Append("Used key: "); Print(tr.GetKey()); Builder.AppendFormat(" Value len:{0}", tr.GetStorageSizeOfCurrentKey().Value); Builder.AppendLine(); }
public void MarkCurrentKeyAsUsed(IKeyValueDBTransaction tr) { Keys = Keys.ResizingAppend(ByteBuffer.NewSync(tr.GetKeyPrefix())).ResizingAppend(tr.GetKey()); Builder.Append("Used key: "); Print(ByteBuffer.NewSync(tr.GetKeyPrefix())); Builder.Append('|'); Print(tr.GetKey()); Builder.AppendFormat(" Value len:{0}", tr.GetStorageSizeOfCurrentKey().Value); Builder.AppendLine(); }
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++; } }
/// <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); } } }
void MarkKeyAsUsed(IKeyValueDBTransaction tr) { _kvtr.SetKeyPrefix(tr.GetKeyPrefix()); if (_kvtr.Find(tr.GetKey()) == FindResult.Exact) { _kvtr.EraseCurrent(); } }
ulong ReadOidFromCurrentKeyInTransaction() { var key = _keyValueTr.GetKey(); var bufOfs = key.Offset; var oid = PackUnpack.UnpackVUInt(key.Buffer, ref bufOfs); return(oid); }
internal static IEnumerable <KeyValuePair <uint, string> > LoadRelationNamesEnum(IKeyValueDBTransaction tr) { tr.InvalidateCurrentKey(); while (tr.FindNextKey(RelationNamesPrefix)) { yield return(new KeyValuePair <uint, string>((uint)PackUnpack.UnpackVUInt(tr.GetValue()), new SpanReader(tr.GetKey().Slice(RelationNamesPrefixLen)).ReadString())); } }
TKey CurrentToKey() { Span <byte> buffer = stackalloc byte[512]; var reader = new SpanReader(_keyValueTr.GetKey(ref MemoryMarshal.GetReference(buffer), buffer.Length).Slice(_prefix.Length)); IReaderCtx ctx = null; if (_keyHandler.NeedsCtx()) { ctx = new DBReaderCtx(_tr); } return(_keyReader(ref reader, ctx)); }
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); } }
public void LoadGlobalInfo(bool sortTableByNameAsc = false) { LoadTableNamesDict(); LoadRelationInfoDict(); MarkLastDictId(); _trkv.InvalidateCurrentKey(); _singletons = new Dictionary <uint, ulong>(); while (_trkv.FindNextKey(ObjectDB.TableSingletonsPrefix)) { _singletons.Add(new SpanReader(_trkv.GetKey().Slice((int)ObjectDB.TableSingletonsPrefixLen)).ReadVUInt32(), new SpanReader(_trkv.GetValue()).ReadVUInt64()); } if (sortTableByNameAsc) { _singletons = _singletons.OrderBy(item => _tableId2Name.TryGetValue(item.Key, out var name) ? name : "").ToDictionary(item => item.Key, item => item.Value); } }
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); }
//secKeyBytes contains already AllRelationsSKPrefix public T FindBySecondaryKeyOrDefault(uint secondaryKeyIndex, uint prefixParametersCount, ByteBuffer secKeyBytes, bool throwWhenNotFound) { _transaction.TransactionProtector.Start(); _kvtr.SetKeyPrefix(secKeyBytes); if (!_kvtr.FindFirstKey()) { if (throwWhenNotFound) { throw new BTDBException("Not found."); } return(default(T)); } var keyBytes = _kvtr.GetKey(); if (_kvtr.FindNextKey()) { throw new BTDBException("Ambiguous result."); } return(CreateInstanceFromSK(secondaryKeyIndex, prefixParametersCount, secKeyBytes, keyBytes)); }
public static byte[] GetKeyAsByteArray(this IKeyValueDBTransaction transaction) { return(transaction.GetKey().ToByteArray()); }
public virtual ByteBuffer GetKeyBytes() { SeekCurrent(); return(_keyValueTr.GetKey()); }
void MarkKeyAsUsed(IKeyValueDBTransaction tr) { _kvtr.EraseCurrent(tr.GetKey()); }
void MarkKeyAsUsed(IKeyValueDBTransaction tr) { _kvtr.SetKeyPrefix(tr.GetKeyPrefix()); if (_kvtr.Find(tr.GetKey())==FindResult.Exact) _kvtr.EraseCurrent(); }
public ByteBuffer GetKeyBytes() { var key = ByteBuffer.NewEmpty(); return(key.ResizingAppend(ByteBuffer.NewSync(_keyValueTr.GetKeyPrefix())).ResizingAppend(_keyValueTr.GetKey())); }
public ByteBuffer GetKey() { return(_keyValueDBTransaction.GetKey()); }
public void Restart() { Restart(_transaction.GetKey()); }
public KeyValueDBKeyReader(IKeyValueDBTransaction transaction) : base(transaction.GetKey()) { _transaction = transaction; }