interplane.Get Omega
Read in your language

cortex-rs

Pure compiled native Rust memory engine with vector search and SQLite WAL

Brought by drakestapleton

branch main Branches Tags History

cortex-rs / om / api2.om

583 lines · 9751 bytes Raw Blame History
1note api2.om: handler bodies (search, recall, get, traverse, writes) on the store.
2global rsl ulong 64
3global rsb f64 64
4global rsk uchar 64
5global nres ulong 1
6global qtr uchar 4194304
7global qtrl ulong 1
8global qem f32 768
9global qemf ulong 1
10text r_res "{\"results\":["
11text r_tail "],\"degraded\":[],\"elapsedMs\":"
12text r_lex ",\"lexicalScore\":"
13text r_gss ",\"graphScore\":0.0,\"semanticScore\":0.0,\"score\":"
14text r_ent "{\"entity\":"
15text r_fs "},\"finalScore\":"
16text r_cs ",\"componentScores\":{\"lexical\":"
17text r_sem ",\"semantic\":"
18text r_cr ",\"confidenceRecency\":0.95,\"graph\":0.0}}"
19text r_claims "{\"claims\":["
20text r_cend "]}"
21text e_lkcx "{\"error\":\"LIKE or GLOB pattern too complex\"}"
22global lkerr uchar 1
23
24note ---- result list ----
25fn res_has e ulong to bool
26for i ulong get nres 0
27if eq get rsl i e
28ret true
29end
30end
31ret false
32end
33fn res_add e ulong k uchar b f64 to void
34n = get nres 0
35if ge n len rsl
36ret
37end
38put rsl n e
39put rsk n k
40put rsb n b
41put nres 0 add n 1
42end
43
44note ---- FTS phase: run the MATCH text in qq for space d; fills the result list ----
45note (fts_search: qerr <> 0 means SQLite raised an error; cortex-rs run_fts then yields None = 0 rows)
46note qok is set for the FTS rows of space d only (fts_search reads it for no other row)
47fn cx_fts_run d ulong lim ulong to void
48e = fts_gfirst d
49for a ulong 4611686018427387904
50if eq e NONE
51break
52end
53v = uchar 0
54if eq get eret e 0
55v = 1
56end
57put qok e v
58e = fts_gnext e
59end
60put qgsel 0 d
61put qlim 0 lim
62n = fts_search
63put nres 0 0
64for i ulong n
65res_add get qrd i 0 get qrb i
66end
67end
68note qq = clean_q (qtr without double quotes) inside double quotes
69fn qq_clean to void
70put qq 0 34
71o = ulong 1
72for i ulong get qtrl 0
73c = get qtr i
74if ne c 34
75put qq o c
76o = add o 1
77end
78end
79put qq o 34
80put qql 0 add o 1
81end
82
83note ---- search ----
84fn api_search to ulong
85sp = get ap A_SPACE
86if eq sp NONE
87sp = stage_txt tdefsp
88end
89if not scope_ok sp
90js_raw e403
91ret 403
92end
93lim = get ap A_LIMIT
94if eq lim NONE
95lim = 12
96end
97if gt lim 50
98lim = 50
99end
100put nres 0 0
101put lkerr 0 0
102d = dict_find sp
103if ne d NONE
104search_in d lim
105end
106if eq get lkerr 0 1
107js_raw e_lkcx
108ret 500
109end
110js_raw r_res
111for i ulong get nres 0
112if gt i 0
113js_byte 44
114end
115o_search_row i
116end
117js_raw r_tail
118o_elapsed
119js_byte 125
120ret 200
121end
122fn search_in d ulong lim ulong to void
123q = get ap A_Q
124n = ulong 0
125if ne q NONE
126n = rf_len q
127o = rf_off q
128for i ulong n
129put jrs i hsg add o i
130end
131end
132ru_trim 0 n
133tro = get jtro 0
134tl = get jtrl 0
135for i ulong tl
136put qtr i get jrs add tro i
137end
138put qtrl 0 tl
139if eq tl 0
140search_list d lim
141ret
142end
143note the live binary returns rows only from the quoted-phrase FTS attempt: its operator query and
144note its AND-joined query never contribute (spec/ORACLE-OBSERVED.md)
145qq_clean
146cx_fts_run d lim
147if lt get nres 0 lim
148search_like d lim
149end
150if gt get nres 0 lim
151put nres 0 lim
152end
153end
154note substring fallback: LIKE '%q%' on name or content, scan order, LIMIT lim rows, new ones appended
155note likeFunc raises "LIKE or GLOB pattern too complex" on the first row it tests (lkerr = 1)
156fn search_like d ulong lim ulong to void
157n = get qtrl 0
158put qlp 0 37
159for i ulong n
160put qlp add i 1 get qtr i
161end
162put qlp add n 1 37
163put qlpl 0 add n 2
164lok = like_set
165lit = like_lit
166eo_range d
167a = get eor 0
168b = get eor 1
169got = ulong 0
170k = a
171while and lt k b lt got lim bound 999999
172e = get eord k
173if eq get eret e 0
174if not lok
175put lkerr 0 1
176ret
177end
178if like_ent e lit
179got = add got 1
180if not res_has e
181res_add e 1 0.0
182end
183end
184end
185k = add k 1
186end
187end
188note e's name or content LIKE the pattern (lit: like_lit held, like_find answers)
189fn like_ent e ulong lit bool to bool
190cx_lkname get enm e
191if cx_lkone lit
192ret true
193end
194cx_lkcont get ect e
195ret cx_lkone lit
196end
197note the name string r into qct/qctl
198fn cx_lkname r ulong to void
199o = rf_off r
200n = rf_len r
201i = ulong 0
202for a ulong 4611686018427387904
203if ge i n
204break
205end
206put qct i hsg add o i
207i = add i 1
208end
209put qctl 0 n
210end
211note the content string c into qct/qctl (hc packs eight bytes per word, low byte first)
212fn cx_lkcont c ulong to void
213o = rf_off c
214n = rf_len c
215k = ulong 0
216for a ulong 4611686018427387904
217if ge k n
218break
219end
220w = add o k
221x = get hc shr w 3
222s = mul and w 7 8
223put qct k uchar and shr x s 255
224k = add k 1
225end
226put qctl 0 n
227end
228fn cx_lkone lit bool to bool
229if lit
230ret like_find
231end
232ret like_test
233end
234note empty query: newest first by created_at (stable on scan order), LIMIT lim
235fn search_list d ulong lim ulong to void
236eo_range d
237a = get eor 0
238b = get eor 1
239for k ulong sub b a
240e = get eord add a k
241if eq get eret e 0
242list_insert e lim
243end
244end
245end
246fn list_insert e ulong lim ulong to void
247n = get nres 0
248p = n
249while gt p 0 bound 64
250if ge hs_cmp get eca get rsl sub p 1 get eca e 0
251break
252end
253p = sub p 1
254end
255if ge p lim
256ret
257end
258m = n
259if ge m lim
260m = sub lim 1
261end
262i = m
263while gt i p bound 64
264put rsl i get rsl sub i 1
265put rsk i 2
266i = sub i 1
267end
268put rsl p e
269put rsk p 2
270if lt n lim
271put nres 0 add n 1
272end
273end
274fn o_search_row i ulong to void
275e = get rsl i
276k = get rsk i
277s = f64 0.4
278if eq k 0
279s = div 1.0 add 1.0 fabs get rsb i
280end
281if eq k 1
282s = 0.7
283end
284o_entity e
285js_raw r_lex
286js_f64 bitcast ulong s
287js_raw r_gss
288js_f64 bitcast ulong s
289js_byte 125
290end
291fn o_elapsed to void
292t = sub ulong monons get ap A_T0
293ms = mul div f64 t 1000000000.0 1000.0
294js_f64 bitcast ulong ms
295end
296
297note ---- recall ----
298global rcf f64 64
299global rcl f64 64
300global rcs f64 64
301fn api_recall to ulong
302sp = get ap A_SPACE
303if eq sp NONE
304sp = stage_txt tdefsp
305end
306if not scope_ok sp
307js_raw e403
308ret 403
309end
310lim = get ap A_LIMIT
311if eq lim NONE
312lim = 5
313end
314if gt lim 20
315lim = 20
316end
317enc_fetch_q
318put nres 0 0
319d = dict_find sp
320if ne d NONE
321recall_in d lim
322end
323js_raw r_res
324for i ulong get nres 0
325if gt i 0
326js_byte 44
327end
328js_raw r_ent
329o_entity get rsl i
330js_raw r_fs
331js_f64 bitcast ulong get rcf i
332js_raw r_cs
333js_f64 bitcast ulong get rcl i
334js_raw r_sem
335js_f64 bitcast ulong get rcs i
336js_raw r_cr
337end
338js_raw r_tail
339o_elapsed
340js_byte 125
341ret 200
342end
343fn recall_in d ulong lim ulong to void
344q = get ap A_Q
345o = rf_off q
346n = rf_len q
347for i ulong n
348put jrq i hsg add o i
349end
350put jrql 0 n
351ru_prep
352eo_range d
353a = get eor 0
354b = get eor 1
355for k ulong sub b a
356e = get eord add a k
357if eq get eret e 0
358recall_one e lim
359end
360end
361end
362fn recall_one e ulong lim ulong to void
363sem = f64 0.0
364if and eq get qemf 0 1 ne get eem e NONE
365sem = cos_em e
366end
367lex = recall_lex e
368fin = lex
369if eq get qemf 0 1
370fin = add mul 0.6 sem mul 0.4 lex
371end
372recall_insert e fin lex sem lim
373end
374fn recall_lex e ulong to f64
375r = get enm e
376o = rf_off r
377n = rf_len r
378for i ulong n
379put jrs i hsg add o i
380end
381put jrsl 0 n
382if ru_contains
383ret 0.9
384end
385c = get ect e
386o = rf_off c
387n = rf_len c
388for i ulong n
389put jrs i hc_get add o i
390end
391put jrsl 0 n
392if ru_contains
393ret 0.6
394end
395ret 0.0
396end
397note stable top-k: a new item goes after every kept item with a score >= its own
398fn recall_insert e ulong fin f64 lex f64 sem f64 lim ulong to void
399n = get nres 0
400p = n
401while gt p 0 bound 64
402if ge get rcf sub p 1 fin
403break
404end
405p = sub p 1
406end
407if ge p lim
408ret
409end
410m = n
411if ge m lim
412m = sub lim 1
413end
414i = m
415while gt i p bound 64
416j = sub i 1
417put rsl i get rsl j
418put rcf i get rcf j
419put rcl i get rcl j
420put rcs i get rcs j
421i = j
422end
423put rsl p e
424put rcf p fin
425put rcl p lex
426put rcs p sem
427if lt n lim
428put nres 0 add n 1
429end
430end
431note cosine similarity in f32 exactly as embeddings.rs (sequential sums, sqrt each, one divide)
432fn cos_em e ulong to f64
433r = get eem e
434if ne rf_len r 3072
435ret 0.0
436end
437o = rf_off r
438dot = f32 0.0
439na = f32 0.0
440nb = f32 0.0
441for i ulong 768
442a = get qem i
443b = em_at add o mul i 4
444dot = add dot mul a b
445na = add na mul a a
446nb = add nb mul b b
447end
448den = mul fsqrt na fsqrt nb
449if gt den 0.0
450ret f64 div dot den
451end
452ret 0.0
453end
454note little-endian f32 at hc byte offset o
455fn em_at o ulong to f32
456v = or or ulong hc_get o shl ulong hc_get add o 1 8 or shl ulong hc_get add o 2 16 shl ulong hc_get add o 3 24
457ret bitcast f32 uint v
458end
459
460note ---- get ----
461fn api_get to ulong
462t = get ap A_ID
463if eq t NONE
464t = get ap A_NAME
465end
466if or eq t NONE eq rf_len t 0
467js_raw e400id
468ret 400
469end
470sp = get ap A_SPACE
471if eq sp NONE
472sp = stage_txt tdefsp
473end
474if not scope_ok sp
475js_raw e403
476ret 403
477end
478d = dict_find sp
479if eq d NONE
480js_raw e404
481ret 404
482end
483eo_range d
484a = get eor 0
485b = get eor 1
486for k ulong sub b a
487e = get eord add a k
488if eq get eret e 0
489if or hs_eq get eid e t hs_eq get enm e t
490o_entity e
491js_byte 125
492ret 200
493end
494end
495end
496js_raw e404
497ret 404
498end
499
500note ---- traverse ----
501text k_csub ",\"subjectEntityId\":"
502text k_cprd ",\"predicate\":"
503text k_cobj ",\"objectEntityId\":"
504text k_clit ",\"literalValue\":"
505text k_cf0 "{\"confidence\":"
506text k_cid ",\"id\":"
507fn api_traverse to ulong
508s = get ap A_SUBJ
509sp = get ap A_SPACE
510if eq sp NONE
511sp = stage_txt tdefsp
512end
513if not scope_ok sp
514js_raw e403
515ret 403
516end
517d = dict_find sp
518js_raw r_claims
519k = ulong 0
520if ne d NONE
521for c ulong get nc 0
522if cl_in c d
523if hs_eq get csub c s
524k = o_claim_sep c k
525end
526end
527end
528for c ulong get nc 0
529if cl_in c d
530if and hs_eq get cobj c s not hs_eq get csub c s
531k = o_claim_sep c k
532end
533end
534end
535end
536js_raw r_cend
537ret 200
538end
539fn cl_in c ulong d ulong to bool
540ret and eq get cret c 0 eq get csl c d
541end
542fn o_claim_sep c ulong k ulong to ulong
543if gt k 0
544js_byte 44
545end
546o_claim c
547ret add k 1
548end
549note json!({"claims": claims}) turns each Claim into a Value: keys come out sorted (BTreeMap)
550fn o_claim c ulong to void
551js_raw k_cf0
552js_f64 bitcast ulong get ccf c
553js_raw k_ca
554o_hs get cca c
555js_raw k_cid
556o_hs get cid c
557js_raw k_clit
558l = get clit c
559if eq l NONE
560js_raw tnull
561else
562o_canon l tnull
563end
564js_raw k_md
565o_canon get cmd c tobj0
566js_raw k_cobj
567o_hs get cobj c
568js_raw k_cprd
569o_hs get cprd c
570js_raw k_ret
571if eq get cret c 0
572js_raw tfalse
573else
574js_raw ttrue
575end
576js_raw k_spid
577o_hs get csid c
578js_raw k_spsl
579o_hs get dref get csl c
580js_raw k_csub
581o_hs get csub c
582js_byte 125
583end