package lattice import "unsafe" // Node is 136 bytes on disk and in memory. 8×8B keys + 8×4B RecPtrs + // 9×4B Children + Mult + Flags + 2B pad. // // Flags: bit 0 = free-list member (isDeleted in the moxie source), bits 1-3 = // branch, bits 4-7 = key count 0-8. // // The free-list bit does not mean "this block is out of the tree". Freeing a // block clears one parent edge pointing at it, but its own children stay linked // to it and its keys and records stay in the file. None of the walks in this // package reads the bit to decide liveness; a block is reached or it is not, // and Header counts both cases separately. type Node struct { Keys [C]Key RecPtrs [C]uint32 Children [C + 1]uint32 Mult uint8 Flags uint8 } var _ [136 - int(unsafe.Sizeof(Node{}))]byte var _ [int(unsafe.Sizeof(Node{})) - 136]byte const ( flagFree uint8 = 0x01 flagBranchMask uint8 = 0x0E ) // KeyCount is the count in the high nibble of Flags. It can exceed C in a // corrupt block; callers that index with it clamp to C first. func (n *Node) KeyCount() int { return int(n.Flags >> 4) } // Count is KeyCount clamped to C. func (n *Node) Count() int { if k := n.KeyCount(); k <= C { return k } return C } func (n *Node) setCount(k int) { n.Flags = (n.Flags & 0x0F) | uint8(k<<4) } func (n *Node) IsDeleted() bool { return n.Flags&flagFree != 0 } func (n *Node) setDeleted() { n.Flags |= flagFree } func (n *Node) clearDeleted() { n.Flags &^= flagFree } func (n *Node) GetBranch() Branch { return Branch((n.Flags & flagBranchMask) >> 1) } func (n *Node) SetBranch(b Branch) { n.Flags = (n.Flags &^ flagBranchMask) | uint8(b<<1) } func (n *Node) IsLeaf() bool { return n.Children[0] == NullIndex } func initNodeChildren(n *Node) { for i := range n.Children { n.Children[i] = NullIndex } for i := range n.RecPtrs { n.RecPtrs[i] = NullRec } } // Record is 48 bytes. Cross-branch links live here, not in nodes, so they // survive B-tree splits. Inline holds up to 24 bytes of payload. type Record struct { DataFile uint32 DataOff uint32 DataLen uint32 Link [2]uint32 Branch uint8 Inline [24]byte } var _ [48 - int(unsafe.Sizeof(Record{}))]byte var _ [int(unsafe.Sizeof(Record{})) - 48]byte func (r *Record) IsInline() bool { return r.DataFile == 0 && r.DataLen <= 24 } func (r *Record) SetInline(data []byte) { r.DataFile = 0 r.DataOff = 0 r.DataLen = uint32(len(data)) var zero [24]byte r.Inline = zero copy(r.Inline[:], data) } func (r *Record) SetOverflow(file, off, length uint32) { r.DataFile = file r.DataOff = off r.DataLen = length } // InlineData returns the inline payload. A record whose DataLen exceeds 24 is // backed by the payload pool and yields nil, exactly as in the moxie source. func (r *Record) InlineData() []byte { if r.DataLen > 24 { return nil } return r.Inline[:r.DataLen] } // PendingExp is one queued expansion propagation, journaled in the .iskr file // after the records. iskradb/compact.mx declares it and propagates it, but no // moxie code path that ships constructs one: Insert never appends, so the // journal is always empty in files written by the shipped code. type PendingExp struct { Branch Branch Depth uint16 Factor uint8 } var _ [6 - int(unsafe.Sizeof(PendingExp{}))]byte var _ [int(unsafe.Sizeof(PendingExp{})) - 6]byte