Exemplo n.º 1
0
 internal void CommitLastObjIdAndDictId(ulong newLastDictId, IKeyValueDBTransaction tr)
 {
     tr.SetUlong(0, (ulong)_lastObjId);
     if (_lastDictId != newLastDictId)
     {
         tr.SetUlong(1, newLastDictId);
         _lastDictId = newLastDictId;
     }
 }
Exemplo n.º 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 void SetUlong(uint idx, ulong value)
 {
     _keyValueDBTransaction.SetUlong(idx, value);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Reads and inserts all key value pairs into current prefix from stream
        /// </summary>
        /// <param name="transaction">transaction where to import all data</param>
        /// <param name="stream">where to read it from</param>
        public static void Import(IKeyValueDBTransaction transaction, Stream stream)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("stream must be readable", nameof(stream));
            }
            var tempbuf  = new byte[4096];
            var tempbuf2 = new byte[4096];

            if (stream.Read(tempbuf, 0, 16) != 16)
            {
                throw new EndOfStreamException();
            }
            if (tempbuf[0] != 'B' || tempbuf[1] != 'T' || tempbuf[2] != 'D' || tempbuf[3] != 'B' || tempbuf[4] != 'E' || tempbuf[5] != 'X' || tempbuf[6] != 'P' || tempbuf[7] != '2')
            {
                throw new BTDBException("Invalid header (it should Start with BTDBEXP2)");
            }
            var keyValuePairs = PackUnpack.UnpackInt64LE(tempbuf, 8);

            if (keyValuePairs < 0)
            {
                throw new BTDBException("Negative number of key value pairs");
            }
            for (var kv = 0; kv < keyValuePairs; kv++)
            {
                if (stream.Read(tempbuf, 0, 4) != 4)
                {
                    throw new EndOfStreamException();
                }
                var keySize = PackUnpack.UnpackInt32LE(tempbuf, 0);
                if (keySize < 0)
                {
                    throw new BTDBException("Negative key size");
                }
                if (keySize > tempbuf.Length)
                {
                    tempbuf = new byte[keySize];
                }
                if (stream.Read(tempbuf, 0, keySize) != keySize)
                {
                    throw new EndOfStreamException();
                }
                if (stream.Read(tempbuf2, 0, 4) != 4)
                {
                    throw new EndOfStreamException();
                }
                var valueSize = PackUnpack.UnpackInt32LE(tempbuf2, 0);
                if (valueSize < 0)
                {
                    throw new BTDBException("Negative value size");
                }
                if (valueSize > tempbuf2.Length)
                {
                    tempbuf2 = new byte[valueSize];
                }
                if (stream.Read(tempbuf2, 0, valueSize) != valueSize)
                {
                    throw new EndOfStreamException();
                }
                transaction.CreateOrUpdateKeyValue(tempbuf.AsSpan(0, keySize), tempbuf2.AsSpan(0, valueSize));
            }
            if (stream.Read(tempbuf, 0, 8) == 8)
            {
                transaction.SetCommitUlong(PackUnpack.UnpackUInt64LE(tempbuf, 0));
                if (stream.Read(tempbuf, 0, 4) == 4)
                {
                    var ulongCount = PackUnpack.UnpackUInt32LE(tempbuf, 0);
                    for (var i = 0u; i < ulongCount; i++)
                    {
                        if (stream.Read(tempbuf, 0, 8) != 8)
                        {
                            throw new EndOfStreamException();
                        }
                        transaction.SetUlong(i, PackUnpack.UnpackUInt64LE(tempbuf, 0));
                    }
                }
            }
        }