示例#1
0
        public void WriteNode()
        {
            if (Block == null)
            {
                Block      = new Block(MemoryManager, IndexRelation);
                Block.Page = new Pointer(Page.PageNumber, 0);
            }

            foreach (BPlusTreeNodeValue value in Values)
            {
                CustomTuple tuple = new CustomTuple(IndexRelation);
                tuple.AddValueFor <TKeyType>("Value", (TKeyType)value.Value);

                if (IsLeaf && value == Values.First() && LeftSiblingPointer != null)
                {
                    tuple.AddValueFor("LeftPointer", LeftSiblingPointer.Short);
                }
                else
                {
                    if (value.LeftPointer != null)
                    {
                        tuple.AddValueFor("LeftPointer", value.LeftPointer.Short);
                    }
                    else
                    {
                        tuple.AddValueFor("LeftPointer", (uint)0);
                    }
                }

                if (value.Pointer != null)
                {
                    tuple.AddValueFor("ValuePointer", value.Pointer.Short);
                }
                else
                {
                    tuple.AddValueFor("ValuePointer", (uint)0);
                }

                if (IsLeaf && value == Values.Last() && RightSiblingPointer != null)
                {
                    tuple.AddValueFor("RightPointer", RightSiblingPointer.Short);
                }
                else
                {
                    if (value.RightPointer != null)
                    {
                        tuple.AddValueFor("RightPointer", value.RightPointer.Short);
                    }
                    else
                    {
                        tuple.AddValueFor("RightPointer", (uint)0);
                    }
                }

                Block.AddRecord(tuple.ToRecord());
            }

            MemoryManager.Persist(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();
                }
            }
        }