Пример #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
        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;
        }
Пример #3
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();
                }
            }
        }
Пример #4
0
        internal bool Joins(CustomTuple rightTuple, AttributeDefinition leftJoinColumn, AttributeDefinition rightJoinColumn)
        {
            CustomObject left  = GetEntryFor(leftJoinColumn.Name);
            CustomObject right = rightTuple.GetEntryFor(rightJoinColumn.Name);

            if (left.IsEqualTo(right))
            {
                return(true);
            }

            return(false);
        }
Пример #5
0
        public CustomTuple Merge(CustomTuple tuple2)
        {
            CustomTuple tuple = new CustomTuple(null);

            foreach (CustomObject obj in this.Entries)
            {
                tuple.Entries.Add(obj);
            }
            foreach (CustomObject obj in tuple2.Entries)
            {
                tuple.Entries.Add(obj);
            }

            return(tuple);
        }
Пример #6
0
        public CustomTuple Projection(List <AttributeDefinition> columns)
        {
            CustomTuple result = new CustomTuple(Relation);

            foreach (AttributeDefinition column in columns)
            {
                if (column.Name == "*")
                {
                    result.Entries.AddRange(Entries);
                }
                else
                {
                    result.Entries.Add(GetEntryFor(column));
                }
            }
            return(result);
        }
Пример #7
0
        public (IBPlusTreeNode, int) FindNodeForCondition(LeafCondition condition, bool first)
        {
            if (IsLeaf)
            {
                //CustomValueComparer comparer = Comparers.GetComparer(condition.Column.Type);

                // todo: if column not in index, fetch data
                if (first)
                {
                    for (int i = 0; i < Values.Count; i++)
                    {
                        CustomTuple tuple = new CustomTuple(DataRelation);
                        tuple.AddValueFor(condition.Column.Name, (int)Values[i].Value);

                        if (condition.SatisfiesCondition(tuple))
                        {
                            return(this, i);
                        }
                    }
                }
                else
                {
                    for (int i = Values.Count - 1; i >= 0; i--)
                    {
                        CustomTuple tuple = new CustomTuple(DataRelation);
                        tuple.AddValueFor(condition.Column.Name, (int)Values[i].Value);

                        if (condition.SatisfiesCondition(tuple))
                        {
                            return(this, i);
                        }
                    }
                }

                return(null, -1);
            }
            else
            {
                // todo: if GreaterThan, get right pointer
                Pointer target = GetTargetPointer((TKeyType)condition.Value);
                BPlusTreeNode <TKeyType> node = ReadNode(target);
                return(node.FindNodeForCondition(condition, first));
            }
        }
Пример #8
0
        internal bool IsEqualTo(CustomTuple otherRecord)
        {
            foreach (CustomObject obj in Entries)
            {
                bool found = false;
                foreach (CustomObject otherObj in otherRecord.Entries)
                {
                    if (obj.IsEqualTo(otherObj))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #9
0
        public void Insert(object[] entries)
        {
            CustomTuple tuple = new CustomTuple(TableDefinition).WithEntries(entries);

            Insert(tuple);
        }