package lattice import "sort" // In-memory second and third layers (Meta, Lex, StringPool) and the adjacency // list (Adj), carried from go-ref. iskra does not use them: it keeps its meta // beside the .iskr in .meta and its strings in the 48-byte Record. These are // the only structures MESH stores that .iskr does not. // AllocMeta appends an empty MetaEntry and returns its index. func (t *Tree) AllocMeta() uint32 { t.Meta = append(t.Meta, MetaEntry{}) return uint32(len(t.Meta) - 1) } // AllocLex appends an empty LexEntry and returns its index. func (t *Tree) AllocLex() uint32 { t.Lex = append(t.Lex, LexEntry{}) return uint32(len(t.Lex) - 1) } // MetaAt returns the MetaEntry at idx; the caller has checked the bounds. func (t *Tree) MetaAt(idx uint32) *MetaEntry { return &t.Meta[idx] } // LexAt returns the LexEntry at idx. func (t *Tree) LexAt(idx uint32) *LexEntry { return &t.Lex[idx] } // PoolPtr returns the string pool for LexEntry.SetForm. func (t *Tree) PoolPtr() *[]byte { return &t.StringPool } // RegisterKey records a key's meta and lex indices for MESH's key map. func (t *Tree) RegisterKey(key Key, metaIdx, lexIdx uint32) { if t.KeyMeta == nil { t.KeyMeta = make(map[Key][2]uint32) } t.KeyMeta[key] = [2]uint32{metaIdx, lexIdx} } // LookupMetaByKey returns the meta and lex indices registered for a key. func (t *Tree) LookupMetaByKey(key Key) (metaIdx, lexIdx uint32, ok bool) { v, ok := t.KeyMeta[key] return v[0], v[1], ok } // SetCounts sets the block, meta and lex counts of a freshly loaded MESH tree. func (t *Tree) SetCounts(nodes, meta, lex uint32) { t.nCount = nodes t.MetaLen = meta t.LexLen = lex if t.MetaLen == 0 && len(t.Meta) > 0 { t.MetaLen = uint32(len(t.Meta)) } if t.LexLen == 0 && len(t.Lex) > 0 { t.LexLen = uint32(len(t.Lex)) } } // pendingAdj stages AddAdj calls before FinalizeAdj lays them out contiguously. func (t *Tree) AddAdj(srcMeta, tgtMeta uint32) { if t.pendingAdj == nil { t.pendingAdj = make(map[uint32][]uint32) } t.pendingAdj[srcMeta] = append(t.pendingAdj[srcMeta], tgtMeta) } // FinalizeAdj writes the staged adjacency block contiguously, source by source // in ascending order, and sets AdjOffset and AdjCount on every source entry. // go-ref iterated a map here, which orders the block differently on every run; // the order is part of the MESH bytes, so it is sorted. func (t *Tree) FinalizeAdj() { t.Adj = t.Adj[:0] for i := range t.Meta { t.Meta[i].AdjOffset = 0 t.Meta[i].AdjCount = 0 } srcs := make([]uint32, 0, len(t.pendingAdj)) for src := range t.pendingAdj { srcs = append(srcs, src) } sort.Slice(srcs, func(i, j int) bool { return srcs[i] < srcs[j] }) for _, src := range srcs { targets := t.pendingAdj[src] if int(src) >= len(t.Meta) { continue } meta := &t.Meta[src] meta.AdjOffset = uint32(len(t.Adj)) if len(targets) > 0xFFFF { panic("lattice: more than 65535 adjacency links for one entry") } meta.AdjCount = uint16(len(targets)) for _, tgt := range targets { t.Adj = append(t.Adj, AdjLink{TargetMeta: tgt}) } } t.pendingAdj = nil } // L3Size and MaxNodesInL3 are go-ref's cache-pressure thresholds, reported by // NeedsRepack. Nothing in iskra uses them. const ( L3Size = 4 << 20 MaxNodesInL3 = L3Size / NodeSize ) // NodeBytes is the in-memory block array size. func (t *Tree) NodeBytes() int { return int(t.nCount) * NodeSize } // NeedsRepack reports whether the block array exceeds the L3 threshold. func (t *Tree) NeedsRepack() bool { return t.NodeBytes() > L3Size } // NewMeshTree builds the tree a MESH file describes: one key space in block 0, // the three pools as read, and no record table, because MESH stores meta and // lex indices rather than records. func NewMeshTree(nodes []Node, meta []MetaEntry, lex []LexEntry, adj []AdjLink, pool []byte) *Tree { t := &Tree{ Nodes: nodes, Meta: meta, Lex: lex, Adj: adj, StringPool: pool, FreeHead: NullIndex, RecKey: make(map[uint32]Key), KeyMeta: make(map[Key][2]uint32), nCount: uint32(len(nodes)), MetaLen: uint32(len(meta)), LexLen: uint32(len(lex)), } if len(meta) > 0 || len(lex) > 0 { // MESH reserves index 0 of a populated pool as the null pointer. t.MetaLen = uint32(len(meta)) } return t } // EntryCount is the number of entries in a MESH-style tree: index 0 of every // pool is the null sentinel. func (t *Tree) EntryCount() int { if t.MetaLen <= 1 { return 0 } return int(t.MetaLen) - 1 } // NewMeshRootTree returns a tree with a single empty root block at index 0 and // no other branch roots: the shape MESH requires. func NewMeshRootTree() *Tree { t := &Tree{ Nodes: make([]Node, 1), FreeHead: NullIndex, nCount: 1, RecKey: make(map[uint32]Key), KeyMeta: make(map[Key][2]uint32), } initNodeChildren(&t.Nodes[0]) return t } // NewRecordPoolTree returns a tree with a single root at block 0, record index // 0 held as MESH's null pointer, and meta/lex pools starting at index 1. Entries // go in from record 1. func NewRecordPoolTree(capacity int) *Tree { t := NewMeshRootTree() t.Records = make([]Record, 1, capacity+1) t.Records[0].Link = [2]uint32{NullRec, NullRec} t.rCount = 1 t.Meta = make([]MetaEntry, 1) t.Lex = make([]LexEntry, 1) t.MetaLen = 1 t.LexLen = 1 return t }