Exemplo n.º 1
0
        private unsafe void WriteValues(BinaryWriter writer, BTreeRecord* record)
        {
            var count = (int)record->Value[0];
            writer.Write(count);

            for (var i = 0; i < count; i++)
            {
                writer.Write(record->Value[i + 1]);
            }
        }
Exemplo n.º 2
0
        private unsafe BTreeNode* InsertIntoLeafAfterSplitting(BTreeNode* root, BTreeNode* leaf, ulong key, BTreeRecord* pointer)
        {
            ulong newKey;
            BTreeNode* newLeaf;
            ulong* tempKeys;
            void** tempPointers;

            int insertionIndex, split, i, j;

            newLeaf = MakeLeaf();

            tempKeys = (ulong*)Memory.Alloc<ulong>(Order);
            tempPointers = Memory.AllocVoid(Order + 1);

            insertionIndex = 0;
            while (insertionIndex < Order - 1 && leaf->Keys[insertionIndex] < key)
            {
                insertionIndex++;
            }

            for (i = 0, j = 0; i < leaf->NumKeys; i++, j++)
            {
                if (j == insertionIndex) j++;
                tempKeys[j] = leaf->Keys[i];
                tempPointers[j] = leaf->Pointers[i];
            }

            tempKeys[insertionIndex] = key;
            tempPointers[insertionIndex] = pointer;

            leaf->NumKeys = 0;

            split = Cut(Order - 1);

            for (i = 0; i < split; i++)
            {
                leaf->Keys[i] = tempKeys[i];
                leaf->Pointers[i] = tempPointers[i];

                leaf->NumKeys++;
            }
            for (i = split, j = 0; i < Order; i++, j++)
            {
                newLeaf->Keys[j] = tempKeys[i];
                newLeaf->Pointers[j] = tempPointers[i];

                newLeaf->NumKeys++;
            }

            Memory.Free(tempKeys);
            Memory.Free(tempPointers);

            newLeaf->Pointers[Order - 1] = leaf->Pointers[Order - 1];
            leaf->Pointers[Order - 1] = newLeaf;

            for (i = leaf->NumKeys; i < Order - 1; i++)
            {
                leaf->Pointers[i] = null;
            }
            for (i = newLeaf->NumKeys; i < Order - 1; i++)
            {
                newLeaf->Pointers[i] = null;
            }

            newLeaf->Parent = leaf->Parent;
            newKey = newLeaf->Keys[0];

            return InsertIntoParent(root, leaf, newKey, newLeaf);
        }
Exemplo n.º 3
0
        private unsafe BTreeNode* StartNewTree(ulong key, BTreeRecord* pointer)
        {
            var root = MakeLeaf();

            root->Keys[0] = key;
            root->Pointers[0] = pointer;
            root->Pointers[Order - 1] = null;
            root->Parent = null;
            root->NumKeys++;

            return root;
        }
Exemplo n.º 4
0
        private static unsafe BTreeNode* InsertIntoLeaf(BTreeNode* leaf, ulong key, BTreeRecord* pointer)
        {
            var insertionPoint = 0;
            while (insertionPoint < leaf->NumKeys && leaf->Keys[insertionPoint] < key)
            {
                insertionPoint++;
            }

            for (var i = leaf->NumKeys; i > insertionPoint; i--)
            {
                leaf->Keys[i] = leaf->Keys[i - 1];
                leaf->Pointers[i] = leaf->Pointers[i - 1];
            }

            leaf->Keys[insertionPoint] = key;
            leaf->Pointers[insertionPoint] = pointer;

            leaf->NumKeys++;

            return leaf;
        }