atomidx.go raw

   1  package iskra
   2  
   3  import (
   4  	"sort"
   5  
   6  	"git.smesh.lol/go-iskra/lattice"
   7  )
   8  
   9  // Atom link generations and the query tolerances.
  10  const (
  11  	GenLegacy    uint8 = 0
  12  	GenContexted uint8 = 1
  13  
  14  	DefaultSigmaArchaic   = 64.0
  15  	DefaultSigmaDiscourse = 128.0
  16  )
  17  
  18  // diversityNearThreshold is the Gaussian factor below which a register
  19  // coordinate stops counting as independent support for a destination atom.
  20  const diversityNearThreshold = 0.25
  21  
  22  // AtomIdxEntry is one cross-language atom link: a source atom and context
  23  // mapped to a destination atom and context, with the generation and register
  24  // coordinates it was observed under.
  25  type AtomIdxEntry struct {
  26  	SrcLang    uint8
  27  	DstLang    uint8
  28  	RoleA      uint8
  29  	RoleB      uint8
  30  	Gen        uint8
  31  	RArchaic   uint8
  32  	RDiscourse uint8
  33  	Weight     uint32
  34  	SrcAtom    string
  35  	ContextA   string
  36  	DstAtom    string
  37  	ContextB   string
  38  }
  39  
  40  // AtomIdx is the atom-link table.
  41  type AtomIdx struct {
  42  	Entries []AtomIdxEntry
  43  }
  44  
  45  // AtomLinkResult is the outcome of one lookup.
  46  type AtomLinkResult struct {
  47  	DstAtom    string
  48  	DstRole    int32
  49  	DstContext string
  50  	Weight     uint32
  51  	Generation uint8
  52  	Tier       uint8
  53  }
  54  
  55  // LookupKey is the lattice key of one legacy atom link, AtomLinkKey in
  56  // ingest_pattern.mx: srcLang | dstLang | 'L' | rArchaic | rDisc | src | dst.
  57  func LookupKey(srcLang, dstLang uint8, src, dst string, rArch, rDisc uint8) lattice.Key {
  58  	buf := make([]byte, 0, 5+len(src)+len(dst))
  59  	buf = append(buf, srcLang, dstLang, 'L', rArch, rDisc)
  60  	buf = append(buf, src...)
  61  	buf = append(buf, dst...)
  62  	return lattice.HashKey(buf)
  63  }
  64  
  65  // LinkForm is the surface form an atom link is stored under: src=dst.
  66  func LinkForm(src, dst string) string { return src + "=" + dst }
  67  
  68  // SplitLinkForm recovers the two atoms from a link form.
  69  func SplitLinkForm(form string) (src, dst string, ok bool) {
  70  	for i := 0; i < len(form); i++ {
  71  		if form[i] == '=' {
  72  			return form[:i], form[i+1:], true
  73  		}
  74  	}
  75  	return form, "", false
  76  }
  77  
  78  // BuildAtomIdx scans every record in the pragmatic branch and decodes its form
  79  // as an atom link.
  80  func BuildAtomIdx(t *Tree) *AtomIdx {
  81  	idx := &AtomIdx{}
  82  	entries, err := t.DB.BranchEntries(lattice.Bpragmatic)
  83  	if err != nil {
  84  		return idx
  85  	}
  86  	for _, e := range entries {
  87  		src, dst, ok := SplitLinkForm(t.Form(e.RecIdx))
  88  		if !ok || src == "" || dst == "" {
  89  			continue
  90  		}
  91  		m := t.Meta(e.RecIdx)
  92  		if m == nil {
  93  			continue
  94  		}
  95  		idx.Entries = append(idx.Entries, AtomIdxEntry{
  96  			// The pragmatic record has no destination-language field: the
  97  			// stage tag is the source, and the destination is the other one of
  98  			// the pair the ingest stored.
  99  			// The pragmatic record has no destination-language field: the
 100  			// stage tag is the source and the record holds src=dst, so every
 101  			// record names its own direction.
 102  			SrcLang:    m.StageTag,
 103  			DstLang:    peerLang(m.StageTag),
 104  			Gen:        GenLegacy,
 105  			RArchaic:   m.Extra[2],
 106  			RDiscourse: m.Extra[3],
 107  			Weight:     m.Count,
 108  			SrcAtom:    src,
 109  			DstAtom:    dst,
 110  		})
 111  	}
 112  	idx.Sort()
 113  	return idx
 114  }
 115  
 116  // peerLang is the other language of a two-language pair. The only pair the
 117  // ingest writes is JA and EN (ja.mx LangJA 0x02 and LangEN 0x01), so a source
 118  // of one is a destination of the other and anything else names itself.
 119  func peerLang(l uint8) uint8 {
 120  	switch l {
 121  	case LangJA:
 122  		return LangEN
 123  	case LangEN:
 124  		return LangJA
 125  	}
 126  	return l
 127  }
 128  
 129  // Sort orders the entries the way the sidecar does, source language then
 130  // source atom, so a lookup is a prefix scan.
 131  func (idx *AtomIdx) Sort() {
 132  	sort.SliceStable(idx.Entries, func(i, j int) bool {
 133  		a, b := &idx.Entries[i], &idx.Entries[j]
 134  		if a.SrcLang != b.SrcLang {
 135  			return a.SrcLang < b.SrcLang
 136  		}
 137  		if a.SrcAtom != b.SrcAtom {
 138  			return a.SrcAtom < b.SrcAtom
 139  		}
 140  		if a.ContextA != b.ContextA {
 141  			return a.ContextA < b.ContextA
 142  		}
 143  		if a.DstLang != b.DstLang {
 144  			return a.DstLang < b.DstLang
 145  		}
 146  		return a.DstAtom < b.DstAtom
 147  	})
 148  }
 149  
 150  // FindBySrc returns the entries for one source atom.
 151  func (idx *AtomIdx) FindBySrc(srcLang uint8, srcAtom string) []AtomIdxEntry {
 152  
 153  	lo := sort.Search(len(idx.Entries), func(i int) bool {
 154  		e := &idx.Entries[i]
 155  		if e.SrcLang != srcLang {
 156  			return e.SrcLang > srcLang
 157  		}
 158  		return e.SrcAtom >= srcAtom
 159  	})
 160  	var out []AtomIdxEntry
 161  	for k := lo; k < len(idx.Entries); k++ {
 162  		e := &idx.Entries[k]
 163  		if e.SrcLang != srcLang || e.SrcAtom != srcAtom {
 164  			break
 165  		}
 166  		out = append(out, *e)
 167  	}
 168  	return out
 169  }
 170