Esempio n. 1
0
        internal static IBTreeNode CreateFirst(CreateOrUpdateCtx ctx)
        {
            var result = new BTreeLeaf(ctx.TransactionId, 1);

            result._keyvalues[0] = NewMemberFromCtx(ctx);
            return(result);
        }
Esempio n. 2
0
 public void CreateOrUpdate(CreateOrUpdateCtx ctx)
 {
     ctx.TransactionId = _transactionId;
     if (ctx.Stack == null)
     {
         ctx.Stack = new List <NodeIdxPair>();
     }
     else
     {
         ctx.Stack.Clear();
     }
     if (_rootNode == null)
     {
         _rootNode = ctx.WholeKeyLen > BTreeLeafComp.MaxTotalLen
             ? BTreeLeaf.CreateFirst(ctx)
             : BTreeLeafComp.CreateFirst(ctx);
         _keyValueCount = 1;
         ctx.Stack.Add(new NodeIdxPair {
             Node = _rootNode, Idx = 0
         });
         ctx.KeyIndex = 0;
         ctx.Created  = true;
         return;
     }
     ctx.Depth = 0;
     _rootNode.CreateOrUpdate(ctx);
     if (ctx.Split)
     {
         _rootNode = new BTreeBranch(ctx.TransactionId, ctx.Node1, ctx.Node2);
         ctx.Stack.Insert(0, new NodeIdxPair {
             Node = _rootNode, Idx = ctx.SplitInRight ? 1 : 0
         });
     }
     else if (ctx.Update)
     {
         _rootNode = ctx.Node1;
     }
     if (ctx.Created)
     {
         _keyValueCount++;
     }
 }
Esempio n. 3
0
        public IBTreeNode ReplaceValues(ReplaceValuesCtx ctx)
        {
            var result    = this;
            var keyvalues = _keyvalues;
            var map       = ctx._newPositionMap;

            for (var i = 0; i < keyvalues.Length; i++)
            {
                ref var ii = ref keyvalues[i];
                if (map.TryGetValue(((ulong)ii.ValueFileId << 32) | ii.ValueOfs, out var newOffset))
                {
                    if (result.TransactionId != ctx._transactionId)
                    {
                        var newKeyValues = new BTreeLeafMember[keyvalues.Length];
                        Array.Copy(keyvalues, newKeyValues, newKeyValues.Length);
                        result    = new BTreeLeaf(ctx._transactionId, newKeyValues);
                        keyvalues = newKeyValues;
                    }
                    keyvalues[i].ValueFileId = ctx._valueFileId;
                    keyvalues[i].ValueOfs    = newOffset;
                }
            }
Esempio n. 4
0
        public IBTreeNode RemappingIterate(long transactionId, BTreeRemappingIterateAction action)
        {
            var result    = this;
            var keyvalues = _keyvalues;

            for (var i = 0; i < keyvalues.Length; i++)
            {
                uint newFileId;
                uint newOffset;
                if (action(keyvalues[i].ValueFileId, keyvalues[i].ValueOfs, out newFileId, out newOffset))
                {
                    if (result.TransactionId != transactionId)
                    {
                        var newKeyValues = new BTreeLeafMember[keyvalues.Length];
                        Array.Copy(keyvalues, newKeyValues, newKeyValues.Length);
                        result    = new BTreeLeaf(transactionId, newKeyValues);
                        keyvalues = newKeyValues;
                    }
                    keyvalues[i].ValueFileId = newFileId;
                    keyvalues[i].ValueOfs    = newOffset;
                }
            }
            return(result);
        }
Esempio n. 5
0
        public void CreateOrUpdate(CreateOrUpdateCtx ctx)
        {
            var index = Find(ctx.KeyPrefix, ctx.Key);

            if ((index & 1) == 1)
            {
                index        = index / 2;
                ctx.Created  = false;
                ctx.KeyIndex = index;
                var m = _keyvalues[index];
                m.ValueFileId = ctx.ValueFileId;
                m.ValueOfs    = ctx.ValueOfs;
                m.ValueSize   = ctx.ValueSize;
                var leaf = this;
                if (ctx.TransactionId != TransactionId)
                {
                    leaf = new BTreeLeaf(ctx.TransactionId, _keyvalues.Length);
                    Array.Copy(_keyvalues, leaf._keyvalues, _keyvalues.Length);
                    ctx.Node1  = leaf;
                    ctx.Update = true;
                }

                leaf._keyvalues[index] = m;
                ctx.Stack.Add(new NodeIdxPair {
                    Node = leaf, Idx = index
                });
                return;
            }

            index        = index / 2;
            ctx.Created  = true;
            ctx.KeyIndex = index;
            if (_keyvalues.Length < MaxMembers)
            {
                var newKeyValues = new BTreeLeafMember[_keyvalues.Length + 1];
                Array.Copy(_keyvalues, 0, newKeyValues, 0, index);
                newKeyValues[index] = NewMemberFromCtx(ctx);
                Array.Copy(_keyvalues, index, newKeyValues, index + 1, _keyvalues.Length - index);
                var leaf = this;
                if (ctx.TransactionId != TransactionId)
                {
                    leaf       = new BTreeLeaf(ctx.TransactionId, newKeyValues);
                    ctx.Node1  = leaf;
                    ctx.Update = true;
                }
                else
                {
                    _keyvalues = newKeyValues;
                }

                ctx.Stack.Add(new NodeIdxPair {
                    Node = leaf, Idx = index
                });
                return;
            }

            ctx.Split = true;
            var keyCountLeft  = (_keyvalues.Length + 1) / 2;
            var keyCountRight = _keyvalues.Length + 1 - keyCountLeft;
            var leftNode      = new BTreeLeaf(ctx.TransactionId, keyCountLeft);
            var rightNode     = new BTreeLeaf(ctx.TransactionId, keyCountRight);

            ctx.Node1 = leftNode;
            ctx.Node2 = rightNode;
            if (index < keyCountLeft)
            {
                Array.Copy(_keyvalues, 0, leftNode._keyvalues, 0, index);
                leftNode._keyvalues[index] = NewMemberFromCtx(ctx);
                Array.Copy(_keyvalues, index, leftNode._keyvalues, index + 1, keyCountLeft - index - 1);
                Array.Copy(_keyvalues, keyCountLeft - 1, rightNode._keyvalues, 0, keyCountRight);
                ctx.Stack.Add(new NodeIdxPair {
                    Node = leftNode, Idx = index
                });
                ctx.SplitInRight = false;
            }
            else
            {
                Array.Copy(_keyvalues, 0, leftNode._keyvalues, 0, keyCountLeft);
                Array.Copy(_keyvalues, keyCountLeft, rightNode._keyvalues, 0, index - keyCountLeft);
                rightNode._keyvalues[index - keyCountLeft] = NewMemberFromCtx(ctx);
                Array.Copy(_keyvalues, index, rightNode._keyvalues, index - keyCountLeft + 1,
                           keyCountLeft + keyCountRight - 1 - index);
                ctx.Stack.Add(new NodeIdxPair {
                    Node = rightNode, Idx = index - keyCountLeft
                });
                ctx.SplitInRight = true;
            }
        }
Esempio n. 6
0
 public void CreateOrUpdate(CreateOrUpdateCtx ctx)
 {
     var index = Find(ctx.KeyPrefix, ctx.Key);
     if ((index & 1) == 1)
     {
         index = index / 2;
         ctx.Created = false;
         ctx.KeyIndex = index;
         var m = _keyvalues[index];
         m.ValueFileId = ctx.ValueFileId;
         m.ValueOfs = ctx.ValueOfs;
         m.ValueSize = ctx.ValueSize;
         var leaf = this;
         if (ctx.TransactionId != TransactionId)
         {
             leaf = new BTreeLeaf(ctx.TransactionId, _keyvalues.Length);
             Array.Copy(_keyvalues, leaf._keyvalues, _keyvalues.Length);
             ctx.Node1 = leaf;
             ctx.Update = true;
         }
         leaf._keyvalues[index] = m;
         ctx.Stack.Add(new NodeIdxPair { Node = leaf, Idx = index });
         return;
     }
     index = index / 2;
     ctx.Created = true;
     ctx.KeyIndex = index;
     if (_keyvalues.Length < MaxMembers)
     {
         var newKeyValues = new BTreeLeafMember[_keyvalues.Length + 1];
         Array.Copy(_keyvalues, 0, newKeyValues, 0, index);
         newKeyValues[index] = NewMemberFromCtx(ctx);
         Array.Copy(_keyvalues, index, newKeyValues, index + 1, _keyvalues.Length - index);
         var leaf = this;
         if (ctx.TransactionId != TransactionId)
         {
             leaf = new BTreeLeaf(ctx.TransactionId, newKeyValues);
             ctx.Node1 = leaf;
             ctx.Update = true;
         }
         else
         {
             _keyvalues = newKeyValues;
         }
         ctx.Stack.Add(new NodeIdxPair { Node = leaf, Idx = index });
         return;
     }
     ctx.Split = true;
     var keyCountLeft = (_keyvalues.Length + 1) / 2;
     var keyCountRight = _keyvalues.Length + 1 - keyCountLeft;
     var leftNode = new BTreeLeaf(ctx.TransactionId, keyCountLeft);
     var rightNode = new BTreeLeaf(ctx.TransactionId, keyCountRight);
     ctx.Node1 = leftNode;
     ctx.Node2 = rightNode;
     if (index < keyCountLeft)
     {
         Array.Copy(_keyvalues, 0, leftNode._keyvalues, 0, index);
         leftNode._keyvalues[index] = NewMemberFromCtx(ctx);
         Array.Copy(_keyvalues, index, leftNode._keyvalues, index + 1, keyCountLeft - index - 1);
         Array.Copy(_keyvalues, keyCountLeft - 1, rightNode._keyvalues, 0, keyCountRight);
         ctx.Stack.Add(new NodeIdxPair { Node = leftNode, Idx = index });
         ctx.SplitInRight = false;
     }
     else
     {
         Array.Copy(_keyvalues, 0, leftNode._keyvalues, 0, keyCountLeft);
         Array.Copy(_keyvalues, keyCountLeft, rightNode._keyvalues, 0, index - keyCountLeft);
         rightNode._keyvalues[index - keyCountLeft] = NewMemberFromCtx(ctx);
         Array.Copy(_keyvalues, index, rightNode._keyvalues, index - keyCountLeft + 1, keyCountLeft + keyCountRight - 1 - index);
         ctx.Stack.Add(new NodeIdxPair { Node = rightNode, Idx = index - keyCountLeft });
         ctx.SplitInRight = true;
     }
 }
Esempio n. 7
0
 internal static IBTreeNode CreateFirst(CreateOrUpdateCtx ctx)
 {
     var result = new BTreeLeaf(ctx.TransactionId, 1);
     result._keyvalues[0] = NewMemberFromCtx(ctx);
     return result;
 }
Esempio n. 8
0
 public IBTreeNode RemappingIterate(long transactionId, BTreeRemappingIterateAction action)
 {
     var result = this;
     var keyvalues = _keyvalues;
     for (var i = 0; i < keyvalues.Length; i++)
     {
         uint newFileId;
         uint newOffset;
         if (action(keyvalues[i].ValueFileId, keyvalues[i].ValueOfs, out newFileId, out newOffset))
         {
             if (result.TransactionId != transactionId)
             {
                 var newKeyValues = new BTreeLeafMember[keyvalues.Length];
                 Array.Copy(keyvalues, newKeyValues, newKeyValues.Length);
                 result = new BTreeLeaf(transactionId, newKeyValues);
                 keyvalues = newKeyValues;
             }
             keyvalues[i].ValueFileId = newFileId;
             keyvalues[i].ValueOfs = newOffset;
         }
     }
     return result;
 }