1
0
mirror of https://github.com/fumiama/fumidb.git synced 2026-06-06 01:00:32 +08:00
Files
fumidb/api/table.md
源文雨 fae3641fab edit doc
2022-05-03 21:33:00 +08:00

42 lines
5.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 数据表格式
## 表头
如下所示,加上文件中附加的`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 ... │
└───────────────────────────────────────────────────────────────────────────────────────┘
```
## 数据区块
### 索引
> 区块长度固定,但是不同索引类型有所不同
详见[索引格式](/api/index.md)。
### 表项
> 区块长度固定,为`8+len(row 1)+len(row 2)+...+len(row N)`字节
> 由于存在可变长的 string 和 binary不同区块长度有可能不同
为方便遍历数据表项以uint64的指针开头代表下一项的地址。接下来按照[数据类型](/api/types.md)中规定的存储格式依次附加第一项、第二项直到第N项的值。
#### 表项的增加
优先附加到上一个表项末尾。如无法实现,则从空区块选取一个,或附加到整个文件末尾。
#### 表项的删除
需要更新[未被使用的对齐部分](/api/dbfile.md#空闲块)