interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / om / api3.om

407 lines · 7097 bytes Raw Blame History
1note api3.om: writes (entity upsert, claim upsert, retract, hard delete) and receipts.
2text r_rec1 "{\"recorded\":true,\"receipt\":{\"id\":"
3text r_rec2 ",\"operation\":"
4text r_rec3 ",\"targetType\":"
5text r_rec4 ",\"targetId\":"
6text r_rec5 ",\"committedAt\":"
7text t_eup "entity.upsert"
8text t_cup "claim.upsert"
9text t_entity "entity"
10text t_claim "claim"
11text t_retr ".retract"
12text t_colsp ": "
13text tobj0e "}}"
14text r_del "{\"deleted\":"
15
16note now as an RFC 3339 string (chrono to_rfc3339, AutoSi), staged in hs
17fn stage_now to ulong
18num_rfc realns false
19ret stage_jtx
20end
21fn stage_uuid to ulong
22ok = num_uuid false
23ret stage_jtx
24end
25fn stage_jtx to ulong
26n = get jtxl 0
27o = get hst 0
28for i ulong n
29hsp add o i get jtx i
30end
31put hst 0 add o n
32ret rf_hs o n
33end
34
35note ensure_space: the space slot for slug r (staged), creating it (uuid, now, next rowid) when missing
36fn ensure_space r ulong to ulong
37d = dict_intern r
38s = sp_of d
39if ne s NONE
40ret s
41end
42idr = stage_uuid
43ca = stage_now
44st_commit
45row = add get stv 3 1
46s = sp_add row idr d ca
47if not and lg_space s lg_end
48trap 9
49end
50ret s
51end
52
53note receipt JSON for an operation text pair (op1 + op2), target type text and target id ref
54fn o_receipt op1 view uchar * tt view uchar * tid ulong now ulong to void
55idr = stage_uuid
56js_raw r_rec1
57o_hs idr
58js_raw r_rec2
59js_byte 34
60js_raw op1
61js_byte 34
62js_raw r_rec3
63js_esc_txt tt
64js_raw r_rec4
65o_hs tid
66js_raw r_rec5
67o_hs now
68js_raw tobj0e
69end
70
71note ---- entity upsert (db.rs upsert_entity) ----
72fn api_write to ulong
73k = get ap A_KIND
74if eq k 1
75ret api_upsert
76end
77if eq k 2
78ret api_claim
79end
80ret api_retract
81end
82fn api_upsert to ulong
83sp = get ap A_SPACE
84if eq sp NONE
85sp = stage_txt tdefsp
86end
87if not scope_ok sp
88js_raw e403
89ret 403
90end
91note Company Brain entries are append-only (brain.om): the generic write may not revise one
92if bn_guard sp
93ret 409
94end
95enc_fetch_w
96s = ensure_space sp
97d = sdi_of s
98now = stage_now
99e = mnm_find d get ap A_NAME
100rev = long 1
101if ne e NONE
102rev = add get erev e 1
103else
104e = upsert_slot s d now
105end
106st_commit
107upsert_apply e rev
108if not and lg_entity e lg_end
109trap 9
110end
111o_receipt t_eup t_entity get eid e now
112ret 201
113end
114fn sdi_of s ulong to ulong
115ret get sdi s
116end
117note no row with this name: the id (given or new) decides; ON CONFLICT(id) updates that row
118fn upsert_slot s ulong d ulong now ulong to ulong
119idr = get ap A_ID
120if eq idr NONE
121idr = stage_uuid
122end
123e = mid_find idr
124if ne e NONE
125ret e
126end
127e = get nent 0
128if ge e EMAX
129trap 9
130end
131put nent 0 add e 1
132put erow e add get stv 0 1
133put stv 0 get erow e
134put eid e idr
135put esid e get sid s
136put esl e d
137put eca e now
138put eret e 2
139put efts e 0
140put eem e NONE
141mid_add e
142ret e
143end
144note write the input fields into slot e (keys change, so unlink first), then FTS delete + insert
145fn upsert_apply e ulong rev long to void
146if lt get eret e 2
147ent_unlink e
148end
149put ety e dict_intern get ap A_TYPE
150put enm e get ap A_NAME
151put ect e get ap A_CONT
152put eal e get ap A_ALIAS
153put eav e 1
154put emd e get ap A_META
155put ecf e bitcast f64 get ap A_CONF
156put erev e rev
157put evf e get ap A_VF
158put evt e get ap A_VT
159m = get ap A_EMB
160if ne m NONE
161put eem e m
162else
163put eem e hc_from_disk get eem e
164st_commit_hc
165end
166put eret e 0
167ent_link e
168st_fts_drop e
169st_fts_new e
170end
171fn st_commit_hc to void
172if gt get hcn 0 get hct 0
173put hct 0 get hcn 0
174end
175end
176
177note ---- claim upsert (db.rs upsert_claim) ----
178fn api_claim to ulong
179sp = get ap A_SPACE
180if eq sp NONE
181sp = stage_txt tdefsp
182end
183if not scope_ok sp
184js_raw e403
185ret 403
186end
187s = ensure_space sp
188idr = get ap A_ID
189if eq idr NONE
190idr = stage_uuid
191end
192now = stage_now
193c = mcl_find idr
194if eq c NONE
195c = claim_new s idr now
196end
197st_commit
198put cprd c get ap A_PRED
199put cobj c get ap A_OBJ
200put clit c get ap A_LIT
201put ccf c bitcast f64 get ap A_CONF
202put cmd c get ap A_META
203put cret c 0
204if not and lg_claim c lg_end
205trap 9
206end
207o_receipt t_cup t_claim get cid c now
208ret 201
209end
210fn claim_new s ulong idr ulong now ulong to ulong
211c = get nc 0
212if ge c CMAX
213trap 9
214end
215put nc 0 add c 1
216put crow c add get stv 2 1
217put stv 2 get crow c
218put cid c idr
219put csid c get sid s
220put csl c get sdi s
221put csub c get ap A_SUBJ
222put cca c now
223mcl_add c
224ret c
225end
226
227note ---- retract (db.rs retract_target) ----
228fn api_retract to ulong
229bn_stale
230tt = get ap A_TT
231tid = get ap A_TID
232now = stage_now
233if hs_eqtxt tt t_entity
234e = mid_find tid
235if ne e NONE
236if not scope_ok get dref get esl e
237js_raw e403
238ret 403
239end
240put eret e 1
241st_fts_drop e
242st_commit
243if not and lg_idop OP_ERET tid lg_end
244trap 9
245end
246else
247if not scope_ok NONE
248js_raw e403
249ret 403
250end
251end
252end
253if hs_eqtxt tt t_claim
254c = mcl_find tid
255if ne c NONE
256if not scope_ok get dref get csl c
257js_raw e403
258ret 403
259end
260put cret c 1
261st_commit
262if not and lg_idop OP_CRET tid lg_end
263trap 9
264end
265else
266if not scope_ok NONE
267js_raw e403
268ret 403
269end
270end
271end
272st_commit
273o_receipt_rt tt tid now
274ret 201
275end
276note receipt whose operation is "<targetType>.retract" for any target type text
277fn o_receipt_rt tt ulong tid ulong now ulong to void
278idr = stage_uuid
279js_raw r_rec1
280o_hs idr
281js_raw r_rec2
282n = rf_len tt
283o = rf_off tt
284for i ulong n
285put jes i hsg add o i
286end
287for i ulong len t_retr
288put jes add n i get t_retr i
289end
290js_esc_buf 0 add n len t_retr
291js_raw r_rec3
292o_hs tt
293js_raw r_rec4
294o_hs tid
295js_raw r_rec5
296o_hs now
297js_raw tobj0e
298end
299
300note ---- hard delete (extension, not in cortex-rs): POST /api/cortex/delete ----
301note {"targetType":"entity","targetId":ID} or {"space":SLUG}; data leaves the disk by compaction
302fn api_delete to ulong
303bn_stale
304sp = get ap A_SPACE
305n = ulong 0
306if ne sp NONE
307if not scope_ok sp
308js_raw e403
309ret 403
310end
311d = dict_find sp
312if ne d NONE
313n = del_count_space d
314st_kill_space d
315if not and lg_idop OP_SDEL sp lg_end
316trap 9
317end
318end
319else
320e = mid_find get ap A_TID
321if ne e NONE
322if not scope_ok get dref get esl e
323js_raw e403
324ret 403
325end
326st_kill e
327n = 1
328if not and lg_idop OP_EDEL get ap A_TID lg_end
329trap 9
330end
331end
332end
333st_commit
334if gt n 0
335c = lg_compact
336if ne c 0
337trap 9
338end
339end
340js_raw r_del
341js_u64 n
342js_byte 125
343ret 200
344end
345fn del_count_space d ulong to ulong
346n = ulong 0
347for e ulong get nent 0
348if and eq get esl e d lt get eret e 2
349n = add n 1
350end
351end
352ret n
353end
354
355note ---- scoped tokens (extension): --tokens FILE, lines "TOKEN SPACE[,SPACE...]" ----
356note tks: token count; tko/tkl token bytes in tkb; tsa/tsn the token's first space and count in tsp
357global tkb uchar 1048576
358global tks ulong 1
359global tko ulong 4096
360global tkl ulong 4096
361global tsa ulong 4096
362global tsn ulong 4096
363global tsp ulong 65536
364global tspn ulong 1
365fn scope_check o ulong n ulong to ulong
366for t ulong get tks 0
367if auth_eq2 o n t
368put ap A_SPSET t
369ret 0
370end
371end
372ret 401
373end
374fn auth_eq2 o ulong n ulong t ulong to bool
375l = get tkl t
376b = get tko t
377d = ulong 0
378if ne n l
379d = 1
380end
381k = n
382if gt k l
383k = l
384end
385for i ulong k
386x = ulong get jrs add o i
387d = or d xor x ulong get tkb add b i
388end
389ret eq d 0
390end
391note true when the request may touch space slug r (NONE = not named: refused for scoped tokens)
392fn scope_ok r ulong to bool
393t = get ap A_SPSET
394if eq t NONE
395ret true
396end
397if eq r NONE
398ret false
399end
400a = get tsa t
401for i ulong get tsn t
402if hs_eq get tsp add a i r
403ret true
404end
405end
406ret false
407end