1
0
mirror of https://github.com/fumiama/fumidb.git synced 2026-06-20 03:50:25 +08:00
This commit is contained in:
源文雨
2022-04-25 23:29:39 +08:00
parent 5bdac9ba46
commit 1b2da23ffb
5 changed files with 208 additions and 0 deletions

84
api/index.md Normal file
View File

@@ -0,0 +1,84 @@
# 索引格式
## int8
> 查找速度为O(1)
由于总条目仅有256条因此直接存储256个uint64的位置指针指示该条目在文件的偏移。
> 下面每格1字节
```
┌─────────┬─────────┬─────────┬─────────┐
│ ptr 000 │ ptr 001 │ ptr ... │ ptr 255 │
└─────────┴─────────┴─────────┴─────────┘
```
## int16
> 查找速度为
> - 无该表项O(1)
> - 有该表项O(logn)
由于总条目仅有65536条因此使用位图索引+位图+顺序链表进行查找定位。
### 位图与位图索引
每一位代表一个槽位为0表示当前为空为1表示当前已有值按顺序排列。
> 下面每格1字节
```
┌────────┬────────┬────────┬────────┐
│00100011│00000000│11001010│11000110│
└────────┴────────┴────────┴────────┘
```
每256位32字节为一组生成8位1字节位图索引插在该组最前。该值指示在这256个槽位中有多少个已被填充。特别地如果256个槽位均被填满索引也为`0`因此还需要额外判断其对应位图是全空还是全满。只要有一处不为0而位图索引为0即可判定这256个槽位全满。
> 下面每格1字节
```
┌────────┬────────┬────────┬────────┐
│ 30 │ No.000 │ No.... │ No.255 │
└────────┴────────┴────────┴────────┘
```
### 顺序链表
根据位图和位图索引可以很方便地计算出当前值在顺序链表上的位置。顺序链表以256个uint64的位置指针为单位分配当装满后分配一块新的空间并将新链表开头的位置指针记录在旧链表开头。当没有下一个节点时开头置0。特别地当删除表项时节点数有可能减少。此时并不归还多余节点所占空间也不对节点头部指针做任何改动而是将其保留以备后用。
> 下面每格8字节
```
┌────────┬────────┬────────┬────────┐
│next ptr│ ptr000 │ ptr... │ ptr255 │
└────────┴────────┴────────┴────────┘
```
## int32/float
使用B+树建立索引,每个节点大小为`4088`字节,可对齐到`4096`字节以方便`mmap`,最多可有`n=341`个扇出,`340`个值;最少则有`170`个值(根节点不遵守最少值规则)。
> 下面每格4字节
```
0 8 12 20
┌───────────────────┬─────────┬───────────────────┬─────────┐
0│ pointer 001 │ key 001 │ pointer 002 │ key 002 │
├───────────────────┼─────────┼───────────────────┼─────────┤
24│ pointer 003 │ key 003 │ pointer 004 │ key 004 │
├───────────────────┼─────────┼───────────────────┼─────────┤
48│ pointer 005 │ key 005 │ pointer 006 │ key 006 │
├───────────────────┼─────────┼───────────────────┼─────────┤
xxx│ pointer xxx │ key xxx │ pointer xxx │ key xxx │
├───────────────────┼─────────┼───────────────────┼─────────┤
4056│ pointer 339 │ key 339 │ pointer 340 │ key 340 │
├───────────────────┼─────────┴─────────┬─────────┴─────────┘
4080│ pointer 341 │ unused field │
└───────────────────┴───────────────────┘
4088 4096
```
## int64/double
使用B+树建立索引,每个节点大小为`4088`字节,可对齐到`4096`字节以方便`mmap`,最多可有`n=256`个扇出,`255`个值;最少则有`128`个值(根节点不遵守最少值规则)。
> 下面每格8字节
```
0 8
┌───────────────────┬───────────────────┐
0│ pointer 001 │ key 001 │
├───────────────────┼───────────────────┤
16│ pointer 002 │ key 002 │
├───────────────────┼───────────────────┤
32│ pointer 003 │ key 003 │
├───────────────────┼───────────────────┤
xxx│ pointer xxx │ key xxx │
├───────────────────┼───────────────────┤
4064│ pointer 255 │ key 255 │
├───────────────────┼───────────────────┤
4080│ pointer 256 │ unused field │
└───────────────────┴───────────────────┘
4088 4096
```
## string
先将其哈希为int64再按int64进行查找。冲突时根据string表项附带存储的[下一个哈希相同的数据项的指针(uint64)](/api/types.md#字符串)进行遍历。