Example #1
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);
                }
            }
        }
Example #2
0
        internal void CommitLastObjIdAndDictId(IKeyValueDBTransaction tr)
        {
            var lastOid = GetLastAllocatedOid();

            if (tr.GetUlong(0) < lastOid)
            {
                tr.SetUlong(0, lastOid);
            }

            var lastDistId = GetLastAllocatedDictId();

            if (tr.GetUlong(1) < lastDistId)
            {
                tr.SetUlong(1, lastDistId);
            }
        }
 public ulong GetUlong(uint idx)
 {
     return(_keyValueDBTransaction.GetUlong(idx));
 }