Пример #1
0
        private void FillNodeFromBlock(BPlusTreeNode <TKeyType> node, Block block)
        {
            foreach (Record record in block.GetSortedRecords())
            {
                CustomTuple tuple = new CustomTuple(IndexRelation).FromRecord(record);

                uint leftPointer  = tuple.GetValueFor <uint>("LeftPointer");
                uint valuePointer = tuple.GetValueFor <uint>("ValuePointer");
                uint rightPointer = tuple.GetValueFor <uint>("RightPointer");

                BPlusTreeNodeValue value = new BPlusTreeNodeValue
                {
                    Value = tuple.GetValueFor <TKeyType>("Value")
                };

                if (leftPointer >= 0)
                {
                    value.LeftPointer = new Pointer(leftPointer);
                }

                if (valuePointer >= 0)
                {
                    value.Pointer = new Pointer(valuePointer);
                }

                if (rightPointer >= 0)
                {
                    value.RightPointer = new Pointer(rightPointer);
                }

                node.Values.Add(value);

                if (valuePointer > 0)
                {
                    node.IsLeaf = true;

                    if (leftPointer > 0)
                    {
                        node.LeftSiblingPointer = new Pointer(leftPointer);
                    }

                    if (rightPointer > 0)
                    {
                        node.RightSiblingPointer = new Pointer(rightPointer);
                    }
                }
            }

            if (block.GetSortedRecords().Count == 0)
            {
                node.IsLeaf = true;
            }

            node.Block = block;
        }
Пример #2
0
        public void Insert(CustomTuple tuple)
        {
            Block block;

            if (TableDefinition.HasClusteredIndex())
            {
                object  key  = tuple.GetValueFor <object>(TableDefinition.GetClusteredIndex().Column);
                Pointer spot = ClusteredIndex.Find(key, false);

                if (spot == null)
                {
                    spot = Program.RelationManager.GetTable(TableDefinition.Id).RootBlock.Page;
                }

                if (BulkMode && _bulkBlocks.TryGetValue(spot.Short, out Block foundBlockInCache))
                {
                    block = foundBlockInCache;
                }
                else
                {
                    block = MemoryManager.Read(TableDefinition, spot) as Block;

                    AddBulkBlock(block);
                }
            }
            else
            {
                block = RootBlock;
            }

            (Pointer indexKey, Block targetBlock) = block.AddRecord(tuple.ToRecord());

            if (!BulkMode)
            {
                targetBlock.Write();
            }
            else
            {
                AddBulkBlock(targetBlock);
                AddBulkBlock(block);
            }

            foreach (KeyValuePair <Index, IBPlusTreeNode> indexTree in _indexesWithTrees)
            {
                object value = tuple.GetValueFor <object>(indexTree.Key.Column);
                indexTree.Value.AddValue(value, indexKey);

                if (!BulkMode)
                {
                    indexTree.Value.WriteTree();
                }
            }
        }