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;
        }
 public RelationAdvancedSecondaryKeyEnumerator(
     IRelationDbManipulator manipulator,
     ByteBuffer prefixBytes, uint prefixFieldCount,
     uint secondaryKeyIndex, int loaderIndex)
     : base(manipulator, prefixBytes, prefixFieldCount, loaderIndex)
 {
     _secondaryKeyIndex = secondaryKeyIndex;
 }
 public RelationSecondaryKeyEnumerator(IInternalObjectDBTransaction tr, RelationInfo relationInfo,
                                       ByteBuffer keyBytes, uint secondaryKeyIndex, uint fieldCountInKey, IRelationDbManipulator manipulator,
                                       int loaderIndex)
     : base(tr, relationInfo, keyBytes, manipulator.ModificationCounter, loaderIndex)
 {
     _secondaryKeyIndex = secondaryKeyIndex;
     _fieldCountInKey   = fieldCountInKey;
     _manipulator       = manipulator;
 }
 public RelationAdvancedSecondaryKeyEnumerator(
     IRelationDbManipulator manipulator,
     ByteBuffer prefixBytes, uint prefixFieldCount,
     EnumerationOrder order,
     KeyProposition startKeyProposition, ByteBuffer startKeyBytes,
     KeyProposition endKeyProposition, ByteBuffer endKeyBytes,
     uint secondaryKeyIndex, int loaderIndex)
     : base(manipulator, prefixBytes, prefixFieldCount, order,
            startKeyProposition, startKeyBytes,
            endKeyProposition, endKeyBytes, loaderIndex)
 {
     _secondaryKeyIndex = secondaryKeyIndex;
 }
        public RelationAdvancedOrderedSecondaryKeyEnumerator(IRelationDbManipulator manipulator,
                                                             ByteBuffer prefixBytes, uint prefixFieldCount, EnumerationOrder order,
                                                             KeyProposition startKeyProposition, ByteBuffer startKeyBytes,
                                                             KeyProposition endKeyProposition, ByteBuffer endKeyBytes,
                                                             uint secondaryKeyIndex, int loaderIndex)
            : base(manipulator, prefixBytes, prefixFieldCount, order,
                   startKeyProposition, startKeyBytes,
                   endKeyProposition, endKeyBytes, false, loaderIndex)
        {
            _secondaryKeyIndex = secondaryKeyIndex;
            var secKeyFields =
                manipulator.RelationInfo.ClientRelationVersionInfo.GetSecondaryKeyFields(secondaryKeyIndex);
            var advancedEnumParamField = secKeyFields[(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)));
        }
        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;
            }
        }
        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;
        }