1
0
mirror of https://github.com/fumiama/fumidb.git synced 2026-06-05 00:32:44 +08:00
Files
fumidb/api/table.md
2022-05-04 13:48:57 +08:00

5.3 KiB
Raw Permalink Blame History

数据表格式

表头

如下所示,加上文件中附加的ptr of next table,表头永远是4k对齐的且长度不超过一页其中行类型列表的No.1自动成为主键,强制应用unique+nonnull类型修饰符;data blocks可以为任意数据,如索引,表项等。增加时直接添加或重用已删除区块,修改时直接覆盖,删除时直接在索引中移除该项,将块首地址附加到已删除块的链表即可。

┌──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┐
│  0 - 7   │  8 - 15  │ 16 -- 23 │ 24 -- 31 │ 32 -- 39 │ 40 -- 47 │ 48 -- 55 │ 56 -- 63 │
├──────────┴──────────┼──────────┴──────────┴──────────┴──────────┴──────────┴──────────┤
│  table name length  │     name     of     the     table     ( variable   length )     │
├─────────────────────┼──────────┬──────────┬──────────┬──────────┬──────────┬──────────┤
│                     │ type  of │ type  of │ type  of │ type  of │ type  of │ type  of │
│   table row length  │   row    │   row    │   row    │   row    │   row    │   row    │
│                     │   No.1   │   No.2   │   No.3   │   No.4   │   No...  │   No.N   │
├─────────────────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┤
│                index pointer of pk  ( this pointer will never be zero )               │
├───────────────────────────────────────────────────────────────────────────────────────┤
│      index pointer of row  No.2 ( if it's zero, there is no index for this row  )     │
├───────────────────────────────────────────────────────────────────────────────────────┤
│     index pointer of row  No... ( if it's zero, there is no index for this row  )     │
├───────────────────────────────────────────────────────────────────────────────────────┤
│      index pointer of row  No.N ( if it's zero, there is no index for this row  )     │
├───────────────────────────────────────────────────────────────────────────────────────┤
│                  index pointer of first foreign key  ( if available )                 │
├───────────────────────────────────────────────────────────────────────────────────────┤
│                  index pointer of second foreign key ( if available )                 │
├───────────────────────────────────────────────────────────────────────────────────────┤
│                                     data blocks ...                                   │
└───────────────────────────────────────────────────────────────────────────────────────┘

数据区块

索引

区块长度固定,但是不同索引类型有所不同 详见索引格式

表项

实现时为了性能考虑,建表时限制数据栏数小于128

区块长度固定,为8+len(row 1)+len(row 2)+...+len(row N)字节

由于存在可变长的 string 和 binary不同区块长度有可能不同

为方便遍历数据表项以uint64的指针开头代表下一项的地址。接下来按照数据类型中规定的存储格式依次附加第一项、第二项直到第N项的值。

表项的增加

优先附加到上一个表项末尾。如无法实现,则从空区块选取一个,或附加到整个文件末尾。

表项的删除

需要更新未被使用的对齐部分