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

            result._keyvalues[0] = NewMemberFromCtx(ctx);
            return(result);
        }
Exemple #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++;
     }
 }
Exemple #3
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.Value = ctx.Value.ToByteArray();
                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;
            }
        }
Exemple #4
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.Value = ctx.Value.ToByteArray();
         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;
     }
 }
Exemple #5
0
 internal static IBTreeNode CreateFirst(CreateOrUpdateCtx ctx)
 {
     var result = new BTreeLeaf(ctx.TransactionId, 1);
     result._keyvalues[0] = NewMemberFromCtx(ctx);
     return result;
 }