示例#1
0
        public int CompareTo(object obj)
        {
            RecordPointer rp = (RecordPointer)obj;

            return(this.Block.CompareTo(rp.Block) != 0 ? this.Block.CompareTo(rp.Block) :
                   this.Offset.CompareTo(rp.Offset));
        }
        private void InsertIntoEmployees_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            char gender = '-';

            if (InsertGender.Text.ToCharArray().Length > 0)
            {
                gender = InsertGender.Text.ToCharArray()[0];
            }
            Employee em = new Employee()
            {
                Id        = ParseTextBox(InsertId),
                Gender    = gender,
                FirstName = InsertFirstName.Text,
                LastName  = InsertLastName.Text,
                Salary    = ParseTextBox(InsertSalary)
            };

            RecordPointer previousEmRp = Employee.FindPreviousEmployee(tree, em.Id);
            RecordPointer emRp         = new RecordPointer(defaultE.GetCache().FindPlaceInFile(previousEmRp.Block));

            tree.Insert(em.Id, emRp);
            em.SetRecord(em, emRp.Block, emRp.Offset);
            recordCount++;

            sw.Stop();
            Console.WriteLine($"+++++++Inserting employee {em} into the table took {sw.ElapsedMilliseconds} millisenconds");

            DisplayRelation(1, recordCount);
            DisplayTree();
        }
示例#3
0
            public override List <RecordPointer> GetRange(K key1, K key2)
            {
                List <RecordPointer> results = new List <RecordPointer>();
                LeafNode             node    = this;

                while (node != null)
                {
                    IEnumerator <K>             keyEnumerator = node.Keys.GetEnumerator();
                    IEnumerator <RecordPointer> rpEnumerator  = node.RecordPointers.GetEnumerator();
                    while (keyEnumerator.MoveNext() && rpEnumerator.MoveNext())
                    {
                        K             key = keyEnumerator.Current;
                        RecordPointer rp  = rpEnumerator.Current;
                        int           c1  = key.CompareTo(key1);
                        int           c2  = key.CompareTo(key2);
                        if (c1 >= 0 && c2 <= 0)
                        {
                            results.Add(rp);
                        }
                        else if (c2 > 0)
                        {
                            return(results);
                        }
                    }
                    node = node.Next;
                }
                return(results);
            }
示例#4
0
            public override RecordPointer DeleteValue(K key)
            {
                Node          child  = this.GetChild(key);
                RecordPointer result = child.DeleteValue(key);

                if (child.IsUnderflow())
                {
                    Node childLeftBrother  = this.GetChildLeftBrother(key);
                    Node childRightBrother = this.GetChildRightBrother(key);
                    Node left  = childLeftBrother != null ? childLeftBrother : child;
                    Node right = childLeftBrother != null ? child : childRightBrother;
                    left.Merge(right);
                    this.DeleteChild(right.GetFirstLeafKey());
                    if (left.IsOverflow())
                    {
                        Node brother = left.Split();
                        this.InsertChild(brother.GetFirstLeafKey(), brother);
                    }
                    if (BPTree.Root.Keys.Count == 0)
                    {
                        BPTree.Root        = left;
                        BPTree.Root.Parent = null;
                    }
                }
                return(result);
            }
示例#5
0
        public static RecordPointer FindPreviousEmployee(BPlusTree <int> tree, int id)
        {
            RecordPointer result = tree.Find(id--);

            if (result != null)
            {
                return(result);
            }
            return(FindPreviousEmployee(tree, id--));
        }
示例#6
0
            public override RecordPointer DeleteValue(K key)
            {
                RecordPointer result = RecordPointer.Empty;

                int where = this.Keys.BinarySearch(key);
                if (where >= 0)
                {
                    result = this.RecordPointers.ElementAt(where);
                    this.Keys.RemoveAt(where);
                    this.RecordPointers.RemoveAt(where);
                }
                return(result);
            }
示例#7
0
            public override void InsertValue(K key, RecordPointer rp)
            {
                Node child = this.GetChild(key);

                child.InsertValue(key, rp);
                if (child.IsOverflow())
                {
                    Node brother = child.Split();
                    brother.Parent = child.Parent;
                    this.InsertChild(brother.GetFirstLeafKey(), brother);
                }
                if (BPTree.Root.IsOverflow())
                {
                    Node brother = this.Split();
                    this.CreateNewRoot(brother);
                }
            }
示例#8
0
            public override void InsertValue(K key, RecordPointer rp)
            {
                int where = this.Keys.BinarySearch(key);
                int keyIndex = where >= 0 ? where : -where - 1;

                if (where >= 0)
                {
                    this.RecordPointers[keyIndex] = rp;
                }
                else
                {
                    this.Keys.Insert(keyIndex, key);
                    this.RecordPointers.Insert(keyIndex, rp);
                }
                if (BPTree.Root.IsOverflow())
                {
                    Node brother = this.Split();
                    this.CreateNewRoot(brother);
                }
            }
        private void UpdateEmployees_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int id = 0;

            int.TryParse(UpdateId.Text, out id);
            Employee      em   = Employee.Empty;
            RecordPointer emRp = tree.Find(id);

            if (emRp.CompareTo(RecordPointer.Empty) != 0)
            {
                int salary = 0;
                em = em.GetRecord(emRp.Block, emRp.Offset);
                if (UpdateGender.Text.Trim().Length != 0)
                {
                    em.Gender = UpdateGender.Text.Trim().ToCharArray()[0];
                }
                if (UpdateFirstName.Text.Trim().Length != 0)
                {
                    em.FirstName = UpdateFirstName.Text.Trim();
                }
                if (UpdateLastName.Text.Trim().Length != 0)
                {
                    em.LastName = UpdateLastName.Text.Trim();
                }
                if (UpdateSalary.Text.Trim().Length != 0)
                {
                    int.TryParse(UpdateSalary.Text.Trim(), out salary);
                    em.Salary = salary;
                }
                em.SetRecord(em, emRp.Block, emRp.Offset);
            }

            sw.Stop();
            Console.WriteLine($"+++++++Updating employee with id {id} took {sw.ElapsedMilliseconds} millisenconds");

            DisplayRelation(1, recordCount);
            DisplayTree();
        }
        private void DeleteFromEmployees_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int id = 0;

            int.TryParse(DeleteId.Text, out id);
            RecordPointer emRp = tree.Delete(id);

            if (emRp.CompareTo(RecordPointer.Empty) != 0)
            {
                Employee.Empty.DeleteRecord(emRp.Block, emRp.Offset);
            }

            sw.Stop();
            Console.WriteLine($"+++++++Deleting employee with id {id} took {sw.ElapsedMilliseconds} millisenconds");

            DisplayRelation(1, recordCount);
            DisplayTree();
        }
示例#11
0
 public abstract void InsertValue(K key, RecordPointer rp);
示例#12
0
 public void Insert(K key, RecordPointer rp)
 {
     Root.InsertValue(key, rp);
 }