Skip to content

Commit

Permalink
B tree
Browse files Browse the repository at this point in the history
  • Loading branch information
zhonghong committed May 11, 2024
1 parent a75107f commit b2ebca7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
71 changes: 71 additions & 0 deletions bTree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package bluedb

import "encoding/binary"

// BNode
// | type | nkeys | pointers | offsets | key-values
// | 2B | 2B | nkeys * 8B | nkeys * 2B | ...
// This is the format of the KV pair. Lengths followed by data.
// | klen | vlen | key | val |
// | 2B | 2B | ... | ... |
type BNode struct {
data []byte
}

type BTree struct {
// 根节点指针
root uint64

// 获取BNode
get func(uint64) BNode
// 分配一个新页
new func(BNode) uint64
// 删除页
del func(BNode)
}

// 页配置
const (
HEADER = 4
BTREE_PAGE_SIZE = 4096
BTREE_MAX_KEY_LEN = 1000
BTREE_MAX_VALUE_LEN = 3000
)

func init() {
nodeMax := HEADER + 8 + 2 + 4 + BTREE_MAX_KEY_LEN + BTREE_MAX_VALUE_LEN
assert(nodeMax <= BTREE_PAGE_SIZE)
}

func assert(expression bool) {
if !expression {
panic("校验不通过")
}
}

// header
func (node *BNode) dataType() uint16 {
return binary.LittleEndian.Uint16(node.data)
}

func (node *BNode) keyCount() uint16 {
return binary.LittleEndian.Uint16(node.data[2:4])
}

func (node *BNode) setHeader(dataType uint16, keyCount uint16) {
binary.LittleEndian.PutUint16(node.data[0:2], dataType)
binary.LittleEndian.PutUint16(node.data[2:4], keyCount)
}

// pointer
func (node *BNode) getPointer(idNum uint16) uint64 {
assert(idNum < node.keyCount())
pos := HEADER + 8*idNum
return binary.LittleEndian.Uint64(node.data[pos:])
}

func (node *BNode) setPointer(idNum uint16, val uint64) {
assert(idNum < node.keyCount())
pos := HEADER + 8*idNum
binary.LittleEndian.PutUint64(node.data[pos:], val)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module bluedb

go 1.19

0 comments on commit b2ebca7

Please sign in to comment.