2017-08-08 23:58:59 +05:30
|
|
|
(library
|
|
|
|
(btree)
|
|
|
|
|
2017-08-11 20:11:57 +05:30
|
|
|
(export btree-value-type
|
|
|
|
btree-dev
|
|
|
|
btree-root
|
|
|
|
btree-open
|
2017-08-08 23:58:59 +05:30
|
|
|
btree-lookup
|
2017-08-10 19:37:20 +05:30
|
|
|
btree-each
|
|
|
|
le64-type)
|
2017-08-08 23:58:59 +05:30
|
|
|
|
|
|
|
(import (block-io)
|
|
|
|
(chezscheme)
|
|
|
|
(binary-format)
|
|
|
|
(list-utils))
|
|
|
|
|
|
|
|
;;; Unlike the kernel or c++ versions, I'm going to leave it to the hiogher
|
|
|
|
;;; levels to handle multi level btrees.
|
2017-08-11 20:11:57 +05:30
|
|
|
(binary-format node-header
|
2017-08-08 23:58:59 +05:30
|
|
|
(csum le32)
|
|
|
|
(flags le32)
|
|
|
|
(blocknr le64)
|
|
|
|
(nr-entries le32)
|
|
|
|
(max-entries le32)
|
|
|
|
(value-size le32)
|
|
|
|
(padding le32))
|
|
|
|
|
2017-08-11 20:11:57 +05:30
|
|
|
;;; (unpacker bv offset)
|
2017-08-08 23:58:59 +05:30
|
|
|
(define-record-type value-type (fields size unpacker))
|
|
|
|
|
|
|
|
(define (max-entries vt)
|
|
|
|
(/ (- metadata-block-size node-header-size)
|
2017-08-11 20:11:57 +05:30
|
|
|
(+ (size-type le64)
|
2017-08-08 23:58:59 +05:30
|
|
|
(value-type-size vt))))
|
|
|
|
|
|
|
|
(define (key-offset index)
|
2017-08-11 20:11:57 +05:30
|
|
|
(+ node-header-size (* (size-type le64) index)))
|
2017-08-08 23:58:59 +05:30
|
|
|
|
2017-08-11 20:11:57 +05:30
|
|
|
(define (value-base header)
|
2017-08-08 23:58:59 +05:30
|
|
|
(+ node-header-size
|
2017-08-11 20:11:57 +05:30
|
|
|
(* (node-header-max-entries header)
|
|
|
|
(size-type le64))))
|
2017-08-08 23:58:59 +05:30
|
|
|
|
2017-08-11 20:11:57 +05:30
|
|
|
(define (value-offset header vt index)
|
|
|
|
(+ (value-base header)
|
2017-08-08 23:58:59 +05:30
|
|
|
(* (value-type-size vt) index)))
|
|
|
|
|
|
|
|
(define-record-type btree
|
|
|
|
(fields value-type dev root))
|
|
|
|
|
|
|
|
(define (btree-open vt dev root)
|
2017-08-11 20:11:57 +05:30
|
|
|
(make-btree vt dev root))
|
2017-08-08 23:58:59 +05:30
|
|
|
|
|
|
|
(define le64-type
|
2017-08-11 20:11:57 +05:30
|
|
|
(make-value-type (size-type le64)
|
2017-08-08 23:58:59 +05:30
|
|
|
(lambda (bv offset)
|
|
|
|
(unpack-type bv offset le64))))
|
|
|
|
|
|
|
|
(define (internal-node? header)
|
|
|
|
(bitwise-bit-set? 0 (node-header-flags header)))
|
|
|
|
|
|
|
|
(define (leaf-node? header)
|
|
|
|
(bitwise-bit-set? 1 (node-header-flags header)))
|
|
|
|
|
|
|
|
(define (key-at node index)
|
2017-08-11 20:11:57 +05:30
|
|
|
(unpack-type node (key-offset index) le64))
|
2017-08-08 23:58:59 +05:30
|
|
|
|
2017-08-11 20:11:57 +05:30
|
|
|
(define (value-at header node index vt)
|
|
|
|
((value-type-unpacker vt) node (value-offset header vt index)))
|
2017-08-08 23:58:59 +05:30
|
|
|
|
|
|
|
;;; Performs a binary search looking for the key and returns the index of the
|
|
|
|
;;; lower bound.
|
|
|
|
(define (lower-bound node header key)
|
2017-08-10 19:37:20 +05:30
|
|
|
(let loop ((lo 0) (hi (node-header-nr-entries header)))
|
|
|
|
(if (<= (- hi lo) 1)
|
2017-08-08 23:58:59 +05:30
|
|
|
lo
|
|
|
|
(let* ((mid (+ lo (/ (- hi lo) 2)))
|
2017-08-10 19:37:20 +05:30
|
|
|
(k (key-at node mid)))
|
2017-08-08 23:58:59 +05:30
|
|
|
(cond
|
|
|
|
((= key k) mid)
|
|
|
|
((< k key) (loop mid hi))
|
2017-08-10 19:37:20 +05:30
|
|
|
(else (loop lo mid)))))))
|
2017-08-08 23:58:59 +05:30
|
|
|
|
|
|
|
;;;;----------------------------------------------
|
|
|
|
;;;; Lookup
|
|
|
|
;;;;----------------------------------------------
|
|
|
|
|
|
|
|
(define (btree-lookup tree key default)
|
|
|
|
(let ((dev (btree-dev tree))
|
|
|
|
(vt (btree-value-type tree)))
|
|
|
|
|
|
|
|
(define (lookup root fail-k)
|
|
|
|
(let loop ((root root))
|
|
|
|
(let* ((node (read-block dev root))
|
2017-08-11 20:11:57 +05:30
|
|
|
(header (node-header-unpack node 0))
|
|
|
|
(index (lower-bound node header key)))
|
2017-08-08 23:58:59 +05:30
|
|
|
(if (internal-node? header)
|
2017-08-11 20:11:57 +05:30
|
|
|
(loop (value-at header node index le64-type))
|
2017-08-08 23:58:59 +05:30
|
|
|
(if (= key (key-at node index))
|
2017-08-11 20:11:57 +05:30
|
|
|
(value-at header node index vt)
|
2017-08-08 23:58:59 +05:30
|
|
|
(fail-k default))))))
|
|
|
|
|
|
|
|
(call/cc
|
|
|
|
(lambda (fail-k)
|
|
|
|
(lookup (btree-root tree) fail-k)))))
|
|
|
|
|
|
|
|
;;;;----------------------------------------------
|
|
|
|
;;;; Walking the btree
|
|
|
|
;;;;----------------------------------------------
|
|
|
|
|
|
|
|
;;; Calls (fn key value) on every entry of the btree.
|
|
|
|
(define (btree-each tree fn)
|
|
|
|
(let ((vt (btree-value-type tree)))
|
|
|
|
|
|
|
|
(define (visit-leaf node header)
|
|
|
|
(let loop ((index 0))
|
|
|
|
(when (< index (node-header-nr-entries header))
|
2017-08-11 20:11:57 +05:30
|
|
|
(fn (key-at node index) (value-at header node index vt))
|
2017-08-08 23:58:59 +05:30
|
|
|
(loop (+ 1 index)))))
|
|
|
|
|
|
|
|
(define (visit-internal node header)
|
|
|
|
(let loop ((index 0))
|
|
|
|
(when (< index (node-header-nr-entries header))
|
2017-08-11 20:11:57 +05:30
|
|
|
(visit-node (value-at header node index le64-type))
|
2017-08-08 23:58:59 +05:30
|
|
|
(loop (+ 1 index)))))
|
|
|
|
|
|
|
|
(define (visit-node root)
|
2017-08-11 20:11:57 +05:30
|
|
|
(let* ((node (read-block (btree-dev tree) root))
|
|
|
|
(header (node-header-unpack node 0)))
|
2017-08-08 23:58:59 +05:30
|
|
|
((if (internal-node? header) visit-internal visit-leaf) node header)))
|
|
|
|
|
2017-08-11 20:11:57 +05:30
|
|
|
(visit-node (btree-root tree)))))
|
2017-08-08 23:58:59 +05:30
|
|
|
|