Exemplo n.º 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);
        }
Exemplo n.º 2
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));
            }
        }