Esempio n. 1
0
 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();
 }
Esempio n. 2
0
 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();
 }
Esempio n. 3
0
        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++;
            }
        }
Esempio n. 4
0
 /// <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();
     }
 }
Esempio n. 5
0
        /// <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);
                }
            }
        }
Esempio n. 6
0
 void MarkKeyAsUsed(IKeyValueDBTransaction tr)
 {
     _kvtr.SetKeyPrefix(tr.GetKeyPrefix());
     if (_kvtr.Find(tr.GetKey()) == FindResult.Exact)
     {
         _kvtr.EraseCurrent();
     }
 }
Esempio n. 7
0
        ulong ReadOidFromCurrentKeyInTransaction()
        {
            var key    = _keyValueTr.GetKey();
            var bufOfs = key.Offset;
            var oid    = PackUnpack.UnpackVUInt(key.Buffer, ref bufOfs);

            return(oid);
        }
Esempio n. 8
0
 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()));
     }
 }
Esempio n. 9
0
        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));
        }
Esempio n. 10
0
 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();
     }
 }
Esempio n. 11
0
 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();
     }
 }
Esempio n. 12
0
        /// <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);
            }
        }
Esempio n. 13
0
        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);
            }
        }
Esempio n. 14
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. 15
0
        //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));
        }
Esempio n. 16
0
 public static byte[] GetKeyAsByteArray(this IKeyValueDBTransaction transaction)
 {
     return(transaction.GetKey().ToByteArray());
 }
Esempio n. 17
0
 public virtual ByteBuffer GetKeyBytes()
 {
     SeekCurrent();
     return(_keyValueTr.GetKey());
 }
Esempio n. 18
0
 void MarkKeyAsUsed(IKeyValueDBTransaction tr)
 {
     _kvtr.EraseCurrent(tr.GetKey());
 }
Esempio n. 19
0
 void MarkKeyAsUsed(IKeyValueDBTransaction tr)
 {
     _kvtr.SetKeyPrefix(tr.GetKeyPrefix());
     if (_kvtr.Find(tr.GetKey())==FindResult.Exact) _kvtr.EraseCurrent();
 }
Esempio n. 20
0
        public ByteBuffer GetKeyBytes()
        {
            var key = ByteBuffer.NewEmpty();

            return(key.ResizingAppend(ByteBuffer.NewSync(_keyValueTr.GetKeyPrefix())).ResizingAppend(_keyValueTr.GetKey()));
        }
 public ByteBuffer GetKey()
 {
     return(_keyValueDBTransaction.GetKey());
 }
Esempio n. 22
0
 public void Restart()
 {
     Restart(_transaction.GetKey());
 }
Esempio n. 23
0
 public KeyValueDBKeyReader(IKeyValueDBTransaction transaction)
     : base(transaction.GetKey())
 {
     _transaction = transaction;
 }