1 package iskra
2 3 import "git.smesh.lol/iskradb/lattice"
4 5 // SubLattice is a domain-scoped view of an iskradb tree.
6 // All keys in a sub-lattice share the same domain byte, ensuring
7 // non-collision with other domains in the same physical tree.
8 type SubLattice struct {
9 Tree *lattice.Tree
10 Pool []byte
11 Domain uint8
12 Reg *Registry
13 }
14 15 // NewSubLattice creates a SubLattice view over an existing tree.
16 func NewSubLattice(tree *lattice.Tree, pool []byte, domain uint8, reg *Registry) SubLattice {
17 return SubLattice{Tree: tree, Pool: pool, Domain: domain, Reg: reg}
18 }
19 20 // MakeKey returns the key for (word, coord) in this sub-lattice's domain.
21 // The word is normalized via the registered KeyNormalizer for the domain.
22 func (sl SubLattice) MakeKey(coord uint64, word string) lattice.Key {
23 return MakeKey(sl.Domain, coord, sl.Reg.NormalizeKey(sl.Domain, word))
24 }
25 26 // Translate finds the best match for word in dstDomain by:
27 // 1. Looking up word in src sub-lattice at coord
28 // 2. Following Record.Link[0] to the primary cross-domain translation
29 // 3. Reading the target form from the dst sub-lattice record
30 //
31 // coord should encode the semantic/morph context of word in the source domain.
32 // RelaxCoord is applied if the exact coord is not found.
33 func Translate(src, dst SubLattice, word string, coord uint64) string {
34 norm := src.Reg.NormalizeKey(src.Domain, word)
35 branches := [3]lattice.Branch{lattice.Bnoun, lattice.Bverb, lattice.Bmodifier}
36 37 for _, c := range RelaxCoord(coord) {
38 key := MakeKey(src.Domain, c, norm)
39 for _, b := range branches {
40 ri := src.Tree.LookupRecIdx(b, key)
41 if ri == lattice.NullRec {
42 continue
43 }
44 rec := src.Tree.GetRecord(ri)
45 if rec == nil || rec.Link[0] == lattice.NullRec {
46 continue
47 }
48 dst_rec := dst.Tree.GetRecord(rec.Link[0])
49 if dst_rec == nil {
50 continue
51 }
52 n := int32(dst_rec.Inline[23])
53 if n > 0 && n <= 23 {
54 return string(dst_rec.Inline[:n])
55 }
56 if dst_rec.DataFile == 1 && dst_rec.DataLen > 0 {
57 end := dst_rec.DataOff + dst_rec.DataLen
58 if int32(end) <= len(dst.Pool) {
59 return string(dst.Pool[dst_rec.DataOff:end])
60 }
61 }
62 }
63 }
64 return ""
65 }
66 67 // Compose registers src as a sub-lattice within dst's tree by sharing the
68 // physical tree. Since domain bytes differ, keys cannot collide.
69 // Both sub-lattices must use the same physical *lattice.Tree.
70 // This is a conceptual operation - the tree is already shared if both
71 // SubLattices were created from the same tree pointer.
72 func Compose(a, b SubLattice) bool {
73 return a.Tree == b.Tree
74 }
75