Exemplo n.º 1
0
        int RemovePrimaryKeysByPrefix(ByteBuffer keyBytesPrefix)
        {
            _transaction.TransactionProtector.Start();
            _kvtr.SetKeyPrefix(keyBytesPrefix);
            int removedCount = (int)_kvtr.GetKeyValueCount();

            if (removedCount > 0)
            {
                _kvtr.EraseAll();
                _modificationCounter.MarkModification();
            }
            return(removedCount);
        }
Exemplo n.º 2
0
 void NotifyRemoved()
 {
     if (_count != -1)
     {
         if (_count == int.MaxValue)
         {
             _count = (int)Math.Min(_keyValueTr.GetKeyValueCount(), int.MaxValue);
         }
         else
         {
             _count--;
         }
     }
 }
Exemplo n.º 3
0
        public RelationAdvancedEnumerator(
            IRelationDbManipulator manipulator, ByteBuffer prefixBytes, uint prefixFieldCount, int loaderIndex)
        {
            _prefixFieldCount = prefixFieldCount;
            _manipulator      = manipulator;
            ItemLoader        = _manipulator.RelationInfo.ItemLoaderInfos[loaderIndex];

            _ascending = true;

            _tr                    = manipulator.Transaction;
            _keyValueTr            = _tr.KeyValueDBTransaction;
            _keyValueTrProtector   = _tr.TransactionProtector;
            _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;

            _keyBytes = prefixBytes;

            _keyValueTrProtector.Start();
            _keyValueTr.SetKeyPrefix(_keyBytes);

            _prevModificationCounter = manipulator.ModificationCounter.ModificationCounter;

            _count                 = (uint)_keyValueTr.GetKeyValueCount();
            _startPos              = _ascending ? 0 : _count - 1;
            _pos                   = 0;
            _seekNeeded            = true;
            _lengthOfNonDataPrefix = manipulator.RelationInfo.Prefix.Length;
        }
Exemplo 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();
     }
 }
Exemplo 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);
                }
            }
        }
Exemplo n.º 6
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("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();
            }
        }
Exemplo n.º 7
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("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();
     }
 }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
0
        public RelationAdvancedOrderedEnumerator(IRelationDbManipulator manipulator,
                                                 ByteBuffer prefixBytes, uint prefixFieldCount,
                                                 EnumerationOrder order,
                                                 KeyProposition startKeyProposition, ByteBuffer startKeyBytes,
                                                 KeyProposition endKeyProposition, ByteBuffer endKeyBytes, bool initKeyReader, int loaderIndex)
        {
            _prefixFieldCount = prefixFieldCount;
            _manipulator      = manipulator;
            ItemLoader        = _manipulator.RelationInfo.ItemLoaderInfos[loaderIndex];
            _tr        = manipulator.Transaction;
            _ascending = order == EnumerationOrder.Ascending;

            _keyValueTr            = _tr.KeyValueDBTransaction;
            _keyValueTrProtector   = _tr.TransactionProtector;
            _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;

            _keyBytes = prefixBytes;
            if (endKeyProposition == KeyProposition.Included)
            {
                endKeyBytes = RelationAdvancedEnumerator <TValue> .FindLastKeyWithPrefix(_keyBytes, endKeyBytes,
                                                                                         _keyValueTr, _keyValueTrProtector);
            }

            _keyValueTrProtector.Start();
            _keyValueTr.SetKeyPrefix(_keyBytes);

            long startIndex;
            long endIndex;

            if (endKeyProposition == KeyProposition.Ignored)
            {
                endIndex = _keyValueTr.GetKeyValueCount() - 1;
            }
            else
            {
                switch (_keyValueTr.Find(endKeyBytes))
                {
                case FindResult.Exact:
                    endIndex = _keyValueTr.GetKeyIndex();
                    if (endKeyProposition == KeyProposition.Excluded)
                    {
                        endIndex--;
                    }

                    break;

                case FindResult.Previous:
                    endIndex = _keyValueTr.GetKeyIndex();
                    break;

                case FindResult.Next:
                    endIndex = _keyValueTr.GetKeyIndex() - 1;
                    break;

                case FindResult.NotFound:
                    endIndex = -1;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (startKeyProposition == KeyProposition.Ignored)
            {
                startIndex = 0;
            }
            else
            {
                switch (_keyValueTr.Find(startKeyBytes))
                {
                case FindResult.Exact:
                    startIndex = _keyValueTr.GetKeyIndex();
                    if (startKeyProposition == KeyProposition.Excluded)
                    {
                        startIndex++;
                    }

                    break;

                case FindResult.Previous:
                    startIndex = _keyValueTr.GetKeyIndex() + 1;
                    break;

                case FindResult.Next:
                    startIndex = _keyValueTr.GetKeyIndex();
                    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;

            if (initKeyReader)
            {
                var primaryKeyFields       = manipulator.RelationInfo.ClientRelationVersionInfo.GetPrimaryKeyFields();
                var advancedEnumParamField = primaryKeyFields[(int)_prefixFieldCount];
                if (advancedEnumParamField.Handler.NeedsCtx())
                {
                    throw new BTDBException("Not supported.");
                }
                _keyReader = (Func <AbstractBufferedReader, IReaderCtx, TKey>)manipulator.RelationInfo
                             .GetSimpleLoader(new RelationInfo.SimpleLoaderType(advancedEnumParamField.Handler, typeof(TKey)));

                _lengthOfNonDataPrefix = manipulator.RelationInfo.Prefix.Length;
            }
        }
Exemplo n.º 10
0
        public RelationAdvancedEnumerator(
            IRelationDbManipulator manipulator,
            ByteBuffer prefixBytes, uint prefixFieldCount,
            EnumerationOrder order,
            KeyProposition startKeyProposition, ByteBuffer startKeyBytes,
            KeyProposition endKeyProposition, ByteBuffer endKeyBytes, int loaderIndex)
        {
            _prefixFieldCount = prefixFieldCount;
            _manipulator      = manipulator;
            ItemLoader        = _manipulator.RelationInfo.ItemLoaderInfos[loaderIndex];

            _ascending = order == EnumerationOrder.Ascending;

            _tr                    = manipulator.Transaction;
            _keyValueTr            = _tr.KeyValueDBTransaction;
            _keyValueTrProtector   = _tr.TransactionProtector;
            _prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;

            _keyBytes = prefixBytes;
            if (endKeyProposition == KeyProposition.Included)
            {
                endKeyBytes = FindLastKeyWithPrefix(_keyBytes, endKeyBytes, _keyValueTr, _keyValueTrProtector);
            }

            _keyValueTrProtector.Start();
            _keyValueTr.SetKeyPrefix(_keyBytes);

            _prevModificationCounter = manipulator.ModificationCounter.ModificationCounter;

            long startIndex;
            long endIndex;

            if (endKeyProposition == KeyProposition.Ignored)
            {
                endIndex = _keyValueTr.GetKeyValueCount() - 1;
            }
            else
            {
                switch (_keyValueTr.Find(endKeyBytes))
                {
                case FindResult.Exact:
                    endIndex = _keyValueTr.GetKeyIndex();
                    if (endKeyProposition == KeyProposition.Excluded)
                    {
                        endIndex--;
                    }

                    break;

                case FindResult.Previous:
                    endIndex = _keyValueTr.GetKeyIndex();
                    break;

                case FindResult.Next:
                    endIndex = _keyValueTr.GetKeyIndex() - 1;
                    break;

                case FindResult.NotFound:
                    endIndex = -1;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (startKeyProposition == KeyProposition.Ignored)
            {
                startIndex = 0;
            }
            else
            {
                switch (_keyValueTr.Find(startKeyBytes))
                {
                case FindResult.Exact:
                    startIndex = _keyValueTr.GetKeyIndex();
                    if (startKeyProposition == KeyProposition.Excluded)
                    {
                        startIndex++;
                    }

                    break;

                case FindResult.Previous:
                    startIndex = _keyValueTr.GetKeyIndex() + 1;
                    break;

                case FindResult.Next:
                    startIndex = _keyValueTr.GetKeyIndex();
                    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;
            _seekNeeded            = true;
            _lengthOfNonDataPrefix = manipulator.RelationInfo.Prefix.Length;
        }
Exemplo n.º 11
0
            public AdvancedEnumerator(ODBSet <TKey> owner, AdvancedEnumeratorParam <TKey> param)
            {
                _owner = owner;
                _keyValueTrProtector = _owner._keyValueTrProtector;
                _keyValueTr          = _owner._keyValueTr;
                _ascending           = param.Order == EnumerationOrder.Ascending;
                _keyValueTrProtector.Start();
                _prevModificationCounter = _owner._modificationCounter;
                _prevProtectionCounter   = _keyValueTrProtector.ProtectionCounter;
                _keyValueTr.SetKeyPrefix(_owner._prefix);
                long startIndex;
                long endIndex;

                if (param.EndProposition == KeyProposition.Ignored)
                {
                    endIndex = _keyValueTr.GetKeyValueCount() - 1;
                }
                else
                {
                    var keyBytes = _owner.KeyToByteArray(param.End);
                    switch (_keyValueTr.Find(keyBytes))
                    {
                    case FindResult.Exact:
                        endIndex = _keyValueTr.GetKeyIndex();
                        if (param.EndProposition == KeyProposition.Excluded)
                        {
                            endIndex--;
                        }

                        break;

                    case FindResult.Previous:
                        endIndex = _keyValueTr.GetKeyIndex();
                        break;

                    case FindResult.Next:
                        endIndex = _keyValueTr.GetKeyIndex() - 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))
                    {
                    case FindResult.Exact:
                        startIndex = _keyValueTr.GetKeyIndex();
                        if (param.StartProposition == KeyProposition.Excluded)
                        {
                            startIndex++;
                        }

                        break;

                    case FindResult.Previous:
                        startIndex = _keyValueTr.GetKeyIndex() + 1;
                        break;

                    case FindResult.Next:
                        startIndex = _keyValueTr.GetKeyIndex();
                        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;
            }
Exemplo n.º 12
0
 public long GetKeyValueCount()
 {
     LogSimpleOperation(KVReplayOperation.GetKeyValueCount);
     return(_tr.GetKeyValueCount());
 }
 public long GetKeyValueCount()
 {
     return(_keyValueDBTransaction.GetKeyValueCount());
 }