1
0
mirror of https://github.com/fumiama/fumidb.git synced 2026-06-06 01:00:32 +08:00
Files
fumidb/api/index.md
2022-04-26 15:58:02 +08:00

85 lines
6.1 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.

# 索引格式
## 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+树建立索引,每个节点大小为`4096`字节,最多可有`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 │ next node ptr │
└───────────────────┴───────────────────┘
4088 4096
```
## int64/double
使用B+树建立索引,每个节点大小为`4096`字节,最多可有`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 │ next node ptr │
└───────────────────┴───────────────────┘
4088 4096
```
## string
先将其哈希为int64再按int64进行查找。冲突时根据string表项附带存储的[下一个哈希相同的数据项的指针(uint64)](/api/types.md#字符串)进行遍历。