package iskra import ( "sort" "git.smesh.lol/go-iskra/lattice" ) // Atom link generations and the query tolerances. const ( GenLegacy uint8 = 0 GenContexted uint8 = 1 DefaultSigmaArchaic = 64.0 DefaultSigmaDiscourse = 128.0 ) // diversityNearThreshold is the Gaussian factor below which a register // coordinate stops counting as independent support for a destination atom. const diversityNearThreshold = 0.25 // AtomIdxEntry is one cross-language atom link: a source atom and context // mapped to a destination atom and context, with the generation and register // coordinates it was observed under. type AtomIdxEntry struct { SrcLang uint8 DstLang uint8 RoleA uint8 RoleB uint8 Gen uint8 RArchaic uint8 RDiscourse uint8 Weight uint32 SrcAtom string ContextA string DstAtom string ContextB string } // AtomIdx is the atom-link table. type AtomIdx struct { Entries []AtomIdxEntry } // AtomLinkResult is the outcome of one lookup. type AtomLinkResult struct { DstAtom string DstRole int32 DstContext string Weight uint32 Generation uint8 Tier uint8 } // LookupKey is the lattice key of one legacy atom link, AtomLinkKey in // ingest_pattern.mx: srcLang | dstLang | 'L' | rArchaic | rDisc | src | dst. func LookupKey(srcLang, dstLang uint8, src, dst string, rArch, rDisc uint8) lattice.Key { buf := make([]byte, 0, 5+len(src)+len(dst)) buf = append(buf, srcLang, dstLang, 'L', rArch, rDisc) buf = append(buf, src...) buf = append(buf, dst...) return lattice.HashKey(buf) } // LinkForm is the surface form an atom link is stored under: src=dst. func LinkForm(src, dst string) string { return src + "=" + dst } // SplitLinkForm recovers the two atoms from a link form. func SplitLinkForm(form string) (src, dst string, ok bool) { for i := 0; i < len(form); i++ { if form[i] == '=' { return form[:i], form[i+1:], true } } return form, "", false } // BuildAtomIdx scans every record in the pragmatic branch and decodes its form // as an atom link. func BuildAtomIdx(t *Tree) *AtomIdx { idx := &AtomIdx{} entries, err := t.DB.BranchEntries(lattice.Bpragmatic) if err != nil { return idx } for _, e := range entries { src, dst, ok := SplitLinkForm(t.Form(e.RecIdx)) if !ok || src == "" || dst == "" { continue } m := t.Meta(e.RecIdx) if m == nil { continue } idx.Entries = append(idx.Entries, AtomIdxEntry{ // The pragmatic record has no destination-language field: the // stage tag is the source, and the destination is the other one of // the pair the ingest stored. // The pragmatic record has no destination-language field: the // stage tag is the source and the record holds src=dst, so every // record names its own direction. SrcLang: m.StageTag, DstLang: peerLang(m.StageTag), Gen: GenLegacy, RArchaic: m.Extra[2], RDiscourse: m.Extra[3], Weight: m.Count, SrcAtom: src, DstAtom: dst, }) } idx.Sort() return idx } // peerLang is the other language of a two-language pair. The only pair the // ingest writes is JA and EN (ja.mx LangJA 0x02 and LangEN 0x01), so a source // of one is a destination of the other and anything else names itself. func peerLang(l uint8) uint8 { switch l { case LangJA: return LangEN case LangEN: return LangJA } return l } // Sort orders the entries the way the sidecar does, source language then // source atom, so a lookup is a prefix scan. func (idx *AtomIdx) Sort() { sort.SliceStable(idx.Entries, func(i, j int) bool { a, b := &idx.Entries[i], &idx.Entries[j] if a.SrcLang != b.SrcLang { return a.SrcLang < b.SrcLang } if a.SrcAtom != b.SrcAtom { return a.SrcAtom < b.SrcAtom } if a.ContextA != b.ContextA { return a.ContextA < b.ContextA } if a.DstLang != b.DstLang { return a.DstLang < b.DstLang } return a.DstAtom < b.DstAtom }) } // FindBySrc returns the entries for one source atom. func (idx *AtomIdx) FindBySrc(srcLang uint8, srcAtom string) []AtomIdxEntry { lo := sort.Search(len(idx.Entries), func(i int) bool { e := &idx.Entries[i] if e.SrcLang != srcLang { return e.SrcLang > srcLang } return e.SrcAtom >= srcAtom }) var out []AtomIdxEntry for k := lo; k < len(idx.Entries); k++ { e := &idx.Entries[k] if e.SrcLang != srcLang || e.SrcAtom != srcAtom { break } out = append(out, *e) } return out }