1 package lattice
2 3 import "unsafe"
4 5 // Node is 136 bytes on disk and in memory. 8×8B keys + 8×4B RecPtrs +
6 // 9×4B Children + Mult + Flags + 2B pad.
7 //
8 // Flags: bit 0 = free-list member (isDeleted in the moxie source), bits 1-3 =
9 // branch, bits 4-7 = key count 0-8.
10 //
11 // The free-list bit does not mean "this block is out of the tree". Freeing a
12 // block clears one parent edge pointing at it, but its own children stay linked
13 // to it and its keys and records stay in the file. None of the walks in this
14 // package reads the bit to decide liveness; a block is reached or it is not,
15 // and Header counts both cases separately.
16 type Node struct {
17 Keys [C]Key
18 RecPtrs [C]uint32
19 Children [C + 1]uint32
20 Mult uint8
21 Flags uint8
22 }
23 24 var _ [136 - int(unsafe.Sizeof(Node{}))]byte
25 var _ [int(unsafe.Sizeof(Node{})) - 136]byte
26 27 const (
28 flagFree uint8 = 0x01
29 flagBranchMask uint8 = 0x0E
30 )
31 32 // KeyCount is the count in the high nibble of Flags. It can exceed C in a
33 // corrupt block; callers that index with it clamp to C first.
34 func (n *Node) KeyCount() int { return int(n.Flags >> 4) }
35 36 // Count is KeyCount clamped to C.
37 func (n *Node) Count() int {
38 if k := n.KeyCount(); k <= C {
39 return k
40 }
41 return C
42 }
43 func (n *Node) setCount(k int) { n.Flags = (n.Flags & 0x0F) | uint8(k<<4) }
44 func (n *Node) IsDeleted() bool { return n.Flags&flagFree != 0 }
45 func (n *Node) setDeleted() { n.Flags |= flagFree }
46 func (n *Node) clearDeleted() { n.Flags &^= flagFree }
47 func (n *Node) GetBranch() Branch { return Branch((n.Flags & flagBranchMask) >> 1) }
48 func (n *Node) SetBranch(b Branch) { n.Flags = (n.Flags &^ flagBranchMask) | uint8(b<<1) }
49 func (n *Node) IsLeaf() bool { return n.Children[0] == NullIndex }
50 51 func initNodeChildren(n *Node) {
52 for i := range n.Children {
53 n.Children[i] = NullIndex
54 }
55 for i := range n.RecPtrs {
56 n.RecPtrs[i] = NullRec
57 }
58 }
59 60 // Record is 48 bytes. Cross-branch links live here, not in nodes, so they
61 // survive B-tree splits. Inline holds up to 24 bytes of payload.
62 type Record struct {
63 DataFile uint32
64 DataOff uint32
65 DataLen uint32
66 Link [2]uint32
67 Branch uint8
68 Inline [24]byte
69 }
70 71 var _ [48 - int(unsafe.Sizeof(Record{}))]byte
72 var _ [int(unsafe.Sizeof(Record{})) - 48]byte
73 74 func (r *Record) IsInline() bool { return r.DataFile == 0 && r.DataLen <= 24 }
75 76 func (r *Record) SetInline(data []byte) {
77 r.DataFile = 0
78 r.DataOff = 0
79 r.DataLen = uint32(len(data))
80 var zero [24]byte
81 r.Inline = zero
82 copy(r.Inline[:], data)
83 }
84 85 func (r *Record) SetOverflow(file, off, length uint32) {
86 r.DataFile = file
87 r.DataOff = off
88 r.DataLen = length
89 }
90 91 // InlineData returns the inline payload. A record whose DataLen exceeds 24 is
92 // backed by the payload pool and yields nil, exactly as in the moxie source.
93 func (r *Record) InlineData() []byte {
94 if r.DataLen > 24 {
95 return nil
96 }
97 return r.Inline[:r.DataLen]
98 }
99 100 // PendingExp is one queued expansion propagation, journaled in the .iskr file
101 // after the records. iskradb/compact.mx declares it and propagates it, but no
102 // moxie code path that ships constructs one: Insert never appends, so the
103 // journal is always empty in files written by the shipped code.
104 type PendingExp struct {
105 Branch Branch
106 Depth uint16
107 Factor uint8
108 }
109 110 var _ [6 - int(unsafe.Sizeof(PendingExp{}))]byte
111 var _ [int(unsafe.Sizeof(PendingExp{})) - 6]byte
112