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