private Dictionary <string, IndexBlock> GetIndexDict(FileStream fileStream, TableBlock tableBlock)
        {
            Dictionary <string, IndexBlock> indexDict = new Dictionary <string, IndexBlock>();

            long       indexBlockHeadPos = tableBlock.IndexBlockHeadPos;
            IndexBlock currentIndexBlock = fileStream.ReadBlock <IndexBlock>(indexBlockHeadPos);

            tableBlock.IndexBlockHead = currentIndexBlock;

            currentIndexBlock.TryLoadNextObj(fileStream);// 此时的currentIndexBlock是索引块的头结点。
            while (currentIndexBlock.NextObj != null)
            {
                SkipListNodeBlock[] headNodes = GetHeadNodesOfSkipListNodeBlock(fileStream, currentIndexBlock.NextObj);
                currentIndexBlock.NextObj.SkipListHeadNodes = headNodes;
                SkipListNodeBlock tailNode = GetTailNodeOfSkipListNodeBlock(fileStream, currentIndexBlock.NextObj);
                currentIndexBlock.NextObj.SkipListTailNode = tailNode;
                foreach (SkipListNodeBlock headNode in currentIndexBlock.NextObj.SkipListHeadNodes)
                {
                    if (headNode.RightPos == tailNode.ThisPos)
                    {
                        headNode.RightObj = tailNode;
                    }
                }

                currentIndexBlock.NextObj.TryLoadNextObj(fileStream);

                indexDict.Add(currentIndexBlock.NextObj.BindMember, currentIndexBlock.NextObj);

                currentIndexBlock = currentIndexBlock.NextObj;
            }
            return(indexDict);
        }
        /// <summary>
        /// 删除数据库文件里的某个表及其所有索引、数据。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public bool DeleteTable <T>() where T : Table
        {
            Type type = typeof(T);

            if (!this.tableBlockDict.ContainsKey(type))
            {
                return(false);
            }

            // 删除表、索引和数据。
            FileStream  fs = this.fileStream;
            Transaction ts = this.transaction;

            ts.Begin();

            try
            {
                TableBlock table = this.tableBlockDict[type];
                ts.Delete(table);

                long       currentIndexPos = table.IndexBlockHeadPos;
                IndexBlock IndexHead       = fs.ReadBlock <IndexBlock>(currentIndexPos);// 此时指向IndexBlock头结点
                ts.Delete(IndexHead);
                // 删除数据块。
                {
                    IndexHead.TryLoadNextObj(fs);
                    IndexBlock currentIndex = IndexHead.NextObj;// 此时指向PK
                    ts.Delete(currentIndex);
                    DeleteDataBlocks(currentIndex, fs, ts);
                }
                // 删除索引块和skip list node块。
                {
                    IndexBlock currentIndex = IndexHead;
                    while (currentIndex.NextPos != 0)
                    {
                        currentIndex.TryLoadNextObj(fs);
                        ts.Delete(currentIndex.NextObj);

                        DeleteSkipListNodes(currentIndex, fs, ts);

                        currentIndex = currentIndex.NextObj;
                    }
                }

                this.tableBlockDict.Remove(type);
                this.tableIndexBlockDict.Remove(type);

                ts.Commit();

                return(true);
            }
            catch (Exception ex)
            {
                ts.Rollback();
                throw ex;
            }
        }
Example #3
0
        /// <summary>
        /// 把指定的表包含的所有索引的所有skip list node和key value信息都画到图片上。
        /// </summary>
        /// <param name="nextTable"></param>
        /// <param name="dir"></param>
        /// <param name="db"></param>
        private static void PrintTableBlock(TableBlock table, string dir, string prefix, FileDBContext db)
        {
            FileStream fs = db.GetFileStream();
            long       indexBlockHeadPos = table.IndexBlockHeadPos;
            IndexBlock indexBlockHead    = fs.ReadBlock <IndexBlock>(indexBlockHeadPos);
            IndexBlock previousIndex     = indexBlockHead;

            while (previousIndex.NextPos != 0)
            {
                previousIndex.TryLoadNextObj(fs);
                IndexBlock index = previousIndex.NextObj;
                PrintIndexBlock(table, index, dir, prefix, db);

                previousIndex = index;
            }
        }
Example #4
0
        //static readonly Brush indexBrush = new LinearGradientBrush(new Rectangle(0, 0, Consts.indexBlockLength, bmpHeight), Color.DarkGoldenrod, Color.LightGoldenrodYellow, 0f);

        private static void DrawIndex(FileDBContext db, Bitmap[] bmps)
        {
            TableBlock tableBlockHead = db.GetTableBlockHeadNode();
            TableBlock previousTable  = tableBlockHead;

            while (previousTable.NextPos != 0)
            {
                //previousTable.TryLoadNextObj(fs);
                TableBlock table = previousTable.NextObj;

                IndexBlock currentIndex = table.IndexBlockHead;
                while (currentIndex != null)
                {
                    long     index    = currentIndex.ThisPos.PagePos() / Consts.pageSize;
                    Bitmap   bmp      = bmps[index];
                    Graphics g        = Graphics.FromImage(bmp);
                    int      startPos = (int)(currentIndex.ThisPos - Consts.pageSize * index);
                    int      length   = currentIndex.ToBytes().Length;
                    Brush    brush    = new LinearGradientBrush(new Point(startPos, 0), new Point(startPos + length, 0), Color.DarkGray, Color.LightGray);
                    g.FillRectangle(brush, startPos, 1, length, bmpHeight - 1);
                    g.DrawRectangle(boundPen, startPos, 1, length, bmpHeight - 1);
                    g.Dispose();

                    currentIndex.TryLoadNextObj(db.GetFileStream());
                    currentIndex = currentIndex.NextObj;
                }
                //while (currentIndex.NextPos != 0)
                //{
                //    long index = currentIndex.NextObj.ThisPos.PagePos() / Consts.pageSize;
                //    Bitmap bmp = bmps[index];
                //    Graphics g = Graphics.FromImage(bmp);
                //    int startPos = (int)(currentIndex.NextObj.ThisPos - Consts.pageSize * index);
                //    int length = currentIndex.NextObj.ToBytes().Length;
                //    Brush brush = new LinearGradientBrush(new Point(startPos, 0), new Point(startPos + length, 0), Color.DarkGray, Color.LightGray);
                //    g.FillRectangle(brush, startPos, 1, length, bmpHeight - 1);
                //    g.DrawRectangle(boundPen, startPos, 1, length, bmpHeight - 1);
                //    g.Dispose();

                //    currentIndex = currentIndex.NextObj;
                //}

                previousTable = table;
            }
        }