Esempio n. 1
0
        public static UserRow DeSerialize(byte[] bytes)
        {
            UserRow user = new UserRow();

            user.id       = BitConverter.ToInt32(bytes, 0);
            user.username = Encoding.ASCII.GetString(bytes, 4, 32).Trim('\0');
            user.email    = Encoding.ASCII.GetString(bytes, 36, 255).Trim('\0');
            return(user);
        }
Esempio n. 2
0
        public static UserTable Open(string filePath)
        {
            var       pager = Pager.PagerOpen(filePath);
            UserTable table = new UserTable();
            UserRow   row   = new UserRow();

            table.Pager        = pager;
            table.MaxRowNumber = (int)pager.FileLength / row.RowSize;
            return(table);
        }
Esempio n. 3
0
        /// <summary>
        /// 保存到磁盘
        /// </summary>
        public void Save()
        {
            UserRow userRow    = new UserRow();
            var     pageNumber = MaxRowNumber / userRow.RowPerPage;

            for (int i = 0; i < pageNumber; i++)
            {
                Pager.Flush(i, Pager.PageSize);
            }

            var additionRowsNumber = MaxRowNumber % userRow.RowPerPage;

            if (additionRowsNumber > 0)
            {
                Pager.Flush(pageNumber, additionRowsNumber * userRow.RowSize);
            }
        }
Esempio n. 4
0
        public void Insert(UserRow row)
        {
            var tableMaxRows = Pager.TableMaxPages * row.RowPerPage;

            if (MaxRowNumber >= tableMaxRows)
            {
                Console.WriteLine("Table Full");
                return;
            }

            var    bytes  = row.Serialize();
            Cursor cursor = Cursor.TableEnd(this);

            cursor.CursorValue(out var page, out var offset);
            Array.Copy(bytes, 0, page, offset, row.RowSize);
            MaxRowNumber++;
        }
Esempio n. 5
0
        public void Select()
        {
            UserRow userRow = new UserRow();

            Console.WriteLine("Count:" + (MaxRowNumber));
            Console.WriteLine("id\tusername\temail");

            Cursor cursor = Cursor.TableStart(this);

            while (!cursor.IsEndOfTable)
            {
                cursor.CursorValue(out var page, out var offset);
                byte[] rowData = new byte[userRow.RowSize];
                Array.Copy(page, offset, rowData, 0, userRow.RowSize);

                var row = UserRow.DeSerialize(rowData);
                Console.WriteLine($"{row.id}\t{row.username}\t{row.email}");
                cursor.CursorAdvance();
            }
        }