Beispiel #1
0
        public void Add(DataFile fle, string key, long adr, IdxPos pos)
        {
            LastUpdate = DateTime.Now;
            Keys++;
            if (Root == 0)
            {
                IdxPage pg = new IdxPage(0, 0, 0, KeyLength);
                pos.Row    = 0;
                pos.Result = IdxPos.coIxBigger;
                pg.Add(fle, key, adr, false, this, pos);
                return;
            }
            pos.Page    = new IdxPage(0, 0, 0, KeyLength);
            pos.PageAdr = Root;
            ushort l = Levels;

            while (true)
            {
                fle.Read(pos.Page, pos.PageAdr, PageSize);
                pos.Page.Scan(key, pos);
                l--;
                if (l == 0)
                {
                    break;
                }
                pos.PageAdr = pos.RowAdr;
            }
            pos.Page.Add(fle, key, adr, false, this, pos);
        }
Beispiel #2
0
        public void Add(DataFile fle, string key, long adr, bool branch, IdxRoot root, IdxPos pos)
        {
            int    i;
            ushort j, mx, p, q, m, dx;
            int    pgsz = root.PageSize;
            ushort kyln = root.KeyLength;
            ushort row  = pos.Row;

            if (pos.Result == IdxPos.coIxSmaller)
            {
                row++;
            }

            if (Rows < coOrder)
            {
                for (i = Rows; i > row; i--)
                {
                    Table[i].Key     = Table[i - 1].Key;
                    Table[i].Address = Table[i - 1].Address;
                }
                Table[row].Key     = key;
                Table[row].Address = adr;
                Rows++;
                if (row == Rows - 1)
                {
                    LinkParent(fle, kyln, pgsz);
                }
                if (root.Root == 0)
                {
                    fle.Append(this, pgsz);
                    root.Levels = 1;
                    root.Root   = root.First = root.Last = this.Buffer.Position;
                }
                else
                {
                    fle.Write(this);
                    if (branch)
                    {
                        LinkChild(fle, adr, kyln, pgsz);
                    }
                }
                fle.Write(root);
                return;
            }

            IdxPage si = new IdxPage(0, 0, 0, kyln);

            if (Previous > 0)
            {
                fle.Read(si, Previous, root.PageSize);
                if (si.Rows < coOrder)
                {
                    dx = (ushort)((coOrder - si.Rows) / 2);
                    if (dx == 0)
                    {
                        dx = 1;
                    }
                    if (row < dx)
                    {
                        p = (ushort)(si.Rows + row);
                        q = coOrder + 1;
                        if (dx == 1)
                        {
                            si.Table[si.Rows].Key     = key;
                            si.Table[si.Rows].Address = adr;
                            si.Rows++;
                            fle.Write(si);
                            si.LinkParent(fle, kyln, pgsz);
                            if (branch)
                            {
                                si.LinkChild(fle, adr, kyln, pgsz);
                            }
                            return;
                        }
                    }
                    else
                    {
                        p = coOrder + 1;
                        q = (ushort)(row - dx);
                    }
                    for (i = 0; i < dx; i++)
                    {
                        if (si.Rows == p)
                        {
                            si.Table[si.Rows].Key     = key;
                            si.Table[si.Rows].Address = adr;
                            si.Rows++;
                            if (branch)
                            {
                                si.LinkChild(fle, adr, kyln, pgsz);
                            }
                            dx--;
                            if (dx == i)
                            {
                                break;
                            }
                        }
                        si.Table[si.Rows].Key     = Table[i].Key;
                        si.Table[si.Rows].Address = Table[i].Address;
                        si.Rows++;
                        if (branch)
                        {
                            si.LinkChild(fle, Table[i].Address, kyln, pgsz);
                        }
                    }
                    si.LinkParent(fle, kyln, pgsz);
                    fle.Write(si);
                    mx = (ushort)(coOrder - dx);
                    j  = dx;
                    m  = 0;
                    for (i = 0; i < mx; i++)
                    {
                        if (m == q)
                        {
                            Table[m].Key     = key;
                            Table[m].Address = adr;
                            if (branch)
                            {
                                LinkChild(fle, adr, kyln, pgsz);
                            }
                            m++;
                            dx--;
                            if (m == j)
                            {
                                break;
                            }
                        }
                        Table[m].Key     = Table[j].Key;
                        Table[m].Address = Table[j].Address;
                        m++;
                        j++;
                    }
                    Rows -= dx;
                    if (q == Rows)
                    {
                        Table[q].Key     = key;
                        Table[q].Address = adr;
                        Rows++;
                        if (branch)
                        {
                            LinkChild(fle, adr, kyln, pgsz);
                        }
                    }
                    if (q == Rows - 1)
                    {
                        LinkParent(fle, kyln, pgsz);
                    }
                    fle.Write(this);
                    fle.Write(root);
                    return;
                }
            }

            if (Next > 0)
            {
                fle.Read(si, Next, pgsz);
                if (si.Rows < coOrder)
                {
                    dx = (ushort)((coOrder - si.Rows) / 2);
                    if (dx == 0)
                    {
                        dx = 1;
                    }
                    mx = (ushort)(Rows - dx);
                    if (row > mx)
                    {
                        p = (ushort)(row - mx - 1);
                    }
                    else
                    {
                        p = coOrder + 1;
                    }
                    m = (ushort)(si.Rows + dx - 1);
                    j = (ushort)(m - dx);
                    for (i = 0; i < si.Rows; i++)
                    {
                        si.Table[m].Key     = si.Table[j].Key;
                        si.Table[m].Address = si.Table[j].Address;
                        m--;
                        j--;
                    }
                    si.Rows += dx;
                    m        = (ushort)(dx - 1);
                    j        = coOrder - 1;
                    for (i = 0; i < dx; i++)
                    {
                        if (m == p)
                        {
                            si.Table[m].Key     = key;
                            si.Table[m].Address = adr;
                            if (branch)
                            {
                                si.LinkChild(fle, adr, kyln, pgsz);
                            }
                            dx--;
                            if (m == 0)
                            {
                                break;
                            }
                            m--;
                        }
                        si.Table[m].Key     = Table[j].Key;
                        si.Table[m].Address = Table[j].Address;
                        if (branch)
                        {
                            si.LinkChild(fle, Table[j].Address, kyln, pgsz);
                        }
                        m--;
                        j--;
                    }
                    Rows -= dx;
                    if (p == coOrder + 1)
                    {
                        for (i = Rows; i > row; i--)
                        {
                            Table[i].Key     = Table[i - 1].Key;
                            Table[i].Address = Table[i - 1].Address;
                        }
                        Table[row].Key     = key;
                        Table[row].Address = adr;
                        if (branch)
                        {
                            LinkChild(fle, adr, kyln, pgsz);
                        }
                        Rows++;
                    }
                    LinkParent(fle, kyln, pgsz);
                    fle.Write(this);
                    fle.Write(si);
                    fle.Write(root);
                    return;
                }
            }

            si = new IdxPage(Parent, this.Buffer.Position, Next, kyln);
            IdxPage pa;
            ushort  mv = si.Rows = Rows = coMin;
            ushort  hp, wr;
            long    thad;
            string  skey;

            if (row >= coMin)
            {
                hp = (ushort)(row - coMin);
            }
            else
            {
                hp = coOrder;
            }
            wr = 0;
            for (i = 0; i < coMin; i++)
            {
                if (i == hp)
                {
                    si.Table[wr].Key     = key;
                    si.Table[wr].Address = adr;
                    wr++;
                    si.Rows++;
                }
                si.Table[wr].Key     = Table[mv].Key;
                si.Table[wr].Address = Table[mv].Address;
                wr++;
                mv++;
            }
            if (hp == coMin)
            {
                si.Table[coMin].Key     = key;
                si.Table[coMin].Address = adr;
                si.Rows++;
            }
            fle.Append(si, pgsz);
            Next = si.Buffer.Position;

            if (branch)
            {
                for (i = 0; i < si.Rows; i++)
                {
                    si.LinkChild(fle, si.Table[i].Address, kyln, pgsz);
                }
            }

            if (si.Rows == Rows)
            {
                for (i = Rows; i > row; i--)
                {
                    Table[i].Key     = Table[i - 1].Key;
                    Table[i].Address = Table[i - 1].Address;
                }
                Table[row].Key     = key;
                Table[row].Address = adr;
                if (branch)
                {
                    LinkChild(fle, adr, kyln, pgsz);
                }
                Rows++;
            }

            if (si.Next == 0)
            {
                if (!branch)
                {
                    root.Last = Next;
                }
                if (Parent == 0)
                {
                    pa                  = new IdxPage(0, 0, 0, kyln);
                    pa.Rows             = 2;
                    pa.Table[0].Key     = Table[Rows - 1].Key;
                    pa.Table[0].Address = si.Previous;
                    pa.Table[1].Key     = si.Table[si.Rows - 1].Key;
                    pa.Table[1].Address = Next;
                    fle.Append(pa, pgsz);
                    Parent = si.Parent = pa.Buffer.Position;
                    fle.Write(this);
                    fle.Write(si);
                    root.Root = Parent;
                    root.Levels++;
                    fle.Write(root);
                    return;
                }
            }
            else
            {
                IdxPage ss = new IdxPage(0, 0, 0, kyln);
                fle.Read(ss, si.Next, pgsz);
                ss.Previous = Next;
                fle.Write(ss);
            }

            LinkParent(fle, kyln, pgsz);
            fle.Write(this);

            skey = si.Table[si.Rows - 1].Key;
            thad = this.Buffer.Position;
            pa   = new IdxPage(0, 0, 0, kyln);
            fle.Read(pa, Parent, pgsz);
            for (j = 0; j < pa.Rows; j++)
            {
                if (pa.Table[j].Address == thad)
                {
                    pos.Row    = j;
                    pos.Result = IdxPos.coIxSmaller;
                    pa.Add(fle, skey, Next, true, root, pos);
                    return;
                }
            }
            throw new System.InvalidOperationException("MinBase: Index file " + fle.LoFile.Name + " corrupted, trying to add key.");
        }