interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / protocol / synapse / map / src / 40-xerj.om

302 lines · 6251 bytes Raw Blame History
1note ======== xerj (Elasticsearch-compatible) over seed/net/http.om ========
2note One connection per request to SXIP:SXPORT (default 127.0.0.1:9200). Request bodies are written into
3note gout after any pending output and cut off again before the request is sent. SXST: 0 untried, 1 up,
4note 2 down (a connect or protocol failure; later calls skip xerj).
5
6text tget "GET"
7text tpost "POST"
8text tputm "PUT"
9text tdelm "DELETE"
10text thost "Host"
11text thostv "127.0.0.1"
12text tctype "Content-Type"
13text tctjson "application/json"
14text tctnd "application/x-ndjson"
15text tsrchs "/_search?filter_path=hits.hits._id,hits.hits._score"
16text tbulks "/_bulk?refresh=true&filter_path=errors"
17text tq1 "{\"size\":"
18text tq2 ",\"_source\":false,\"query\":{\"bool\":{\"must\":{\"semantic\":{\"field\":\"text\",\"query\":"
19text tq3 "}},\"filter\":{\"term\":{\"kind\":\""
20text tq4 "\"}}}}}"
21text tkrepov "repo"
22text tkskill "skill"
23text tmapping "{\"mappings\":{\"properties\":{\"kind\":{\"type\":\"keyword\"},\"name\":{\"type\":\"keyword\"},\"text\":{\"type\":\"semantic_text\"}}}}"
24
25note sm_xtarget: "/" index sfx into tg; returns its length, or -36 when it does not fit
26fn sm_xtarget tg edit uchar 1024 sfx view uchar * to long
27io2 = get gst SIDXO
28il = get gst SIDXL
29if gt add add il len sfx 1 len tg
30ret -36
31end
32put tg 0 47
33for i ulong il
34put tg add i 1 get gpool add io2 i
35end
36for i ulong len sfx
37put tg add add il 1 i get sfx i
38end
39ret long add add il 1 len sfx
40end
41
42note sm_xresp: read the response status and body of connection cs; the body lands in gfile (length
43note SFLEN). Returns the HTTP status, or a negative code; the connection is closed either way.
44fn sm_xresp cs edit ulong 320 io edit uchar 131072 to long
45d = new uchar 1048576 0
46st = hrsprd cs io
47if lt st 0
48c = hclose cs
49ret st
50end
51b = hbodyall cs io d
52c = hclose cs
53if lt b 0
54ret b
55end
56for i ulong ulong b
57put gfile i get d i
58end
59put gst SFLEN ulong b
60ret st
61end
62
63note sm_xsend: end the headers, write gout[bs, bs + n) as the body and finish the request on cs
64fn sm_xsend cs edit ulong 320 io edit uchar 131072 r long bs ulong n ulong to long
65q = hfe r hhend cs io 0 n
66for i ulong n
67q = hfe q hputb cs io get gout add bs i
68end
69ret hfe q hwfin cs io
70end
71
72note sm_xdial: connect to SXIP:SXPORT (3 s timeout); the descriptor, or a negative code
73fn sm_xdial to long
74a = new uchar 16 0
75r = haddr4 a uint get gst SXIP get gst SXPORT
76ret hdial a 3000000000
77end
78
79note sm_xhttp: method m on "/" index sfx with body gout[bs, SOUT); the response body lands in gfile
80note (length SFLEN). Returns the HTTP status, or a negative code.
81fn sm_xhttp m view uchar * sfx view uchar * bs ulong to long
82cs = new ulong 320 0
83io = new uchar 131072 0
84tg = new uchar 1024 0
85n = sub get gst SOUT bs
86put gst SOUT bs
87if eq get gst SXST 2
88ret -111
89end
90tn = sm_xtarget tg sfx
91if lt tn 0
92ret tn
93end
94fd = sm_xdial
95if lt fd 0
96put gst SXST 2
97ret fd
98end
99r = hinit cs io int fd 0
100hsettmo cs 30000000000 30000000000
101r = hfe r hreqst cs io m
102r = hfe r hreqtg cs io part tg 0 ulong tn
103r = hfe r hhname cs io thost
104r = hfe r hhval cs io thostv
105if gt n 0
106r = hfe r hhname cs io tctype
107if eq get gst SCTYP 1
108r = hfe r hhval cs io tctnd
109else
110r = hfe r hhval cs io tctjson
111end
112end
113r = sm_xsend cs io r bs n
114if lt r 0
115c = hclose cs
116put gst SXST 2
117ret r
118end
119st = sm_xresp cs io
120if lt st 0
121put gst SXST 2
122ret st
123end
124put gst SXST 1
125ret st
126end
127
128note sm_xsearch: semantic search of query slice (qo, qn) over documents of kind (0 repo, 1 skill);
129note hits into gxr. Returns 0, or a negative code (xerj down, error status, malformed answer).
130fn sm_xsearch qo ulong qn ulong kind ulong size ulong to long
131put gst SNXR 0
132if eq qn 0
133ret 0
134end
135bs = get gst SOUT
136sm_ot tq1
137sm_on size
138sm_ot tq2
139sm_oq qo qn
140sm_ot tq3
141if eq kind 0
142sm_ot tkrepov
143else
144sm_ot tkskill
145end
146sm_ot tq4
147put gst SCTYP 0
148st = sm_xhttp tpost tsrchs bs
149if lt st 0
150ret st
151end
152if ne st 200
153ret -71
154end
155ret sm_jload 6 0
156end
157
158text tbx1 "{\"index\":{\"_id\":\"repo:"
159text tbx2 "}}\n{\"kind\":\"repo\",\"name\":"
160text tbx3 ",\"text\":"
161text tbxsep ": "
162text tbxlay ". Layer: "
163text tbxtop ". Topics: "
164text tbs1 "{\"index\":{\"_id\":\"skill:"
165text tbs2 "}}\n{\"kind\":\"skill\",\"name\":"
166
167note sm_xdoc: one bulk action line and document for record r
168fn sm_xdoc r ulong to void
169sm_ot tbx1
170ro = sm_fo r FREPO
171rl = sm_fl r FREPO
172sm_otext ro rl
173sm_ob 34
174sm_ot tbx2
175sm_oq sm_fo r FREPO sm_fl r FREPO
176sm_ot tbx3
177sm_ob 34
178sm_otext sm_fo r FREPO sm_fl r FREPO
179sm_otext2 tbxsep
180sm_otext sm_fo r FPUR sm_fl r FPUR
181if gt sm_fl r FLAY 0
182sm_otext2 tbxlay
183sm_otext sm_fo r FLAY sm_fl r FLAY
184end
185if gt sm_fl r FTOP 0
186sm_otext2 tbxtop
187sm_otext sm_fo r FTOP sm_fl r FTOP
188end
189sm_ob 34
190sm_ob 125
191sm_ob 10
192end
193
194fn sm_otext o ulong n ulong to void
195for i ulong n
196sm_oesc get gpool add o i
197end
198end
199
200fn sm_otext2 t view uchar * to void
201for i ulong len t
202sm_oesc get t i
203end
204end
205
206fn sm_xskill s ulong to void
207no = get gsk mul s 4
208nl = get gsk add mul s 4 1
209sm_ot tbs1
210sm_otext no nl
211sm_ob 34
212sm_ot tbs2
213sm_oq no nl
214sm_ot tbx3
215sm_ob 34
216sm_otext no nl
217sm_otext2 tbxsep
218sm_otext get gsk add mul s 4 2 get gsk add mul s 4 3
219sm_ob 34
220sm_ob 125
221sm_ob 10
222end
223
224note sm_xindex: drop and recreate the index, then bulk-load records and skills. Returns 0 or negative.
225fn sm_xindex to long
226bs = get gst SOUT
227st = sm_xhttp tdelm part tempty 0 0 bs
228if lt st 0
229ret st
230end
231sm_ot tmapping
232put gst SCTYP 0
233st = sm_xhttp tputm part tempty 0 0 bs
234if lt st 0
235ret st
236end
237if ne st 200
238sm_xerrbody
239ret -71
240end
241for r ulong get gst SNREC
242sm_xdoc r
243end
244for s ulong get gst SNSKL
245sm_xskill s
246end
247put gst SCTYP 1
248st = sm_xhttp tpost tbulks bs
249if lt st 0
250ret st
251end
252if ne st 200
253sm_xerrbody
254ret -71
255end
256if not sm_xnoerr
257sm_xerrbody
258ret -71
259end
260ret 0
261end
262
263text tnoerr "\"errors\":false"
264
265note sm_xnoerr: the bulk reply's top-level "errors" is false (it precedes "items" in the reply)
266fn sm_xnoerr to bool
267n = get gst SFLEN
268m = len tnoerr
269if lt n m
270ret false
271end
272for i ulong add sub n m 1
273if sm_xnoat i
274ret true
275end
276if eq get gfile i 91
277ret false
278end
279end
280ret false
281end
282
283fn sm_xnoat i ulong to bool
284for j ulong len tnoerr
285if ne get gfile add i j get tnoerr j
286ret false
287end
288end
289ret true
290end
291
292text txerr "synapse-map: xerj answered: "
293
294fn sm_xerrbody to void
295sm_err txerr
296n = get gst SFLEN
297if gt n 400
298n = 400
299end
300k = write 2 gfile 0 n
301sm_errnl
302end