|
|
@@ -167,6 +167,28 @@ func btLeafPos(n *btNode, key []byte) int {
|
|
|
return cnt
|
|
|
}
|
|
|
|
|
|
+// btLeftmostKey 获取子树最左叶的第一个 key(用于 promoKey)
|
|
|
+func btLeftmostKey(handle page.HandleDb, nodePID uint32) []byte {
|
|
|
+ cur := nodePID
|
|
|
+ for {
|
|
|
+ n := btLoad(handle, cur)
|
|
|
+ if n == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ if n.isLeaf() {
|
|
|
+ if n.info.Count == 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ e := btEntry(n, 0)
|
|
|
+ key := make([]byte, 8) // 主键固定8字节
|
|
|
+ copy(key, btKey(e))
|
|
|
+ return key
|
|
|
+ }
|
|
|
+ // 非叶节点,继续向左下遍历
|
|
|
+ cur = btPID(btEntry(n, 0))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// ======================== B+ 鏍戞悳绱?========================
|
|
|
|
|
|
func BTreePKSearch(rootPID uint32, key []byte, handle page.HandleDb) (uint32, bool) {
|
|
|
@@ -396,8 +418,8 @@ func BTreePKInsert(rootPID uint32, key []byte, dpID uint32, handle page.HandleDb
|
|
|
btSave(handle, nl)
|
|
|
}
|
|
|
}
|
|
|
- pk := make([]byte, 8)
|
|
|
- copy(pk, rl.entries[:8])
|
|
|
+ // 使用右子树最左叶的第一个 key 作为 promoKey
|
|
|
+ pk := btLeftmostKey(handle, rl.info.Hdr.PageID)
|
|
|
nr, err := btPromote(handle, rootPID, path, n.info.Hdr.PageID, pk, rl.info.Hdr.PageID)
|
|
|
return nr, 0, false, err
|
|
|
}
|