interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / door / ev2log.om

416 lines · 7260 bytes Raw Blame History
1note door/ev2log.om: the event log. evload reads <data>events/log once (at the first page of the stream
2note after a start or a reload), checks every line (seq, field count, the sha256 chain) and indexes
3note it; a line that does not check switches the stream off (503, fail closed) and nothing is appended
4note to a log that did not check. evemit puts one event in the batch of the current pass unless its
5note key (kind, rid, source, x) is already in the log; evflush sorts the batch by time and appends it.
6global gevtb ulong 16
7global gevh2 uchar 32
8fn evload to void
9put gevok 0 2
10evmkdirs
11put gevn 0 0
12put gevoff 0 0
13for i ulong 32
14put gevprv i 0
15end
16for i ulong len gevset
17put gevset i 0
18end
19evpath tevlog
20fd = fopen fpth 0 get fpthl 0 524288 0
21if lt fd 0
22if eq fd -2
23put gevok 0 1
24end
25ret
26end
27pos = ulong 0
28ok = true
29for it ulong 1000000
30r = pread int fd gevrb 0 len gevrb long pos
31if lt r 0
32ok = false
33break
34end
35if eq r 0
36break
37end
38u = evscan ulong r pos
39if lt u 0
40ok = false
41break
42end
43if eq u 0
44evtamper
45ok = false
46break
47end
48pos = add pos ulong u
49end
50x = fclose int fd
51if ok
52put gevok 0 1
53end
54end
55fn evtamper to void
56evgb
57evgt tevlbad
58evgn add get gevn 0 1
59evgout
60end
61note evscan n pos: the complete lines of gevrb[0, n) (read at file offset pos); returns the bytes they
62note take, -1 when a line does not check
63fn evscan n ulong pos ulong to long
64p = ulong 0
65for it ulong 2097152
66if ge p n
67break
68end
69e = p
70for k ulong 2097152
71if ge e n
72break
73end
74if eq get gevrb e 10
75break
76end
77e = add e 1
78end
79if ge e n
80break
81end
82if not evline p e
83evtamper
84ret -1
85end
86k = get gevn 0
87put gevoff k add pos p
88put gevn 0 add k 1
89put gevoff add k 1 add pos add e 1
90p = add e 1
91end
92ret long p
93end
94note evline a e: check the line gevrb[a, e) as line gevn+1, then chain it and add its key
95fn evline a ulong e ulong to bool
96n = sub e a
97if or ge n EVLMAX ge get gevn 0 EVMAX
98ret false
99end
100ln = new uchar 8192 0
101for i ulong n bound 8192
102put ln i get gevrb add a i
103end
104ret evlchk part ln 0 n
105end
106fn evlchk ln view uchar * to bool
107if not evtabs ln
108ret false
109end
110seq = evfnum ln 0
111if ne seq add get gevn 0 1
112ret false
113end
114pv = add get gevtb 9 1
115if ne sub len ln pv 64
116ret false
117end
118for i ulong 32
119b = ulong get gevprv i
120if ne get ln add pv mul i 2 evhexc shr b 4
121ret false
122end
123if ne get ln add add pv mul i 2 1 evhexc and b 15
124ret false
125end
126end
127evclr
128put gevk 0 evfnum ln 2
129evfcopy ln 4 1
130evfcopy ln 5 2
131evfcopy ln 8 3
132evput evkey
133evsha ln
134for i ulong 32
135put gevprv i get gevh2 i
136end
137ret true
138end
139note evtabs ln: the positions of the 10 tabs of a line into gevtb[0..9]; false for another count
140fn evtabs ln view uchar * to bool
141k = ulong 0
142for i ulong len ln bound 8192
143if eq get ln i 9
144if ge k 10
145ret false
146end
147put gevtb k i
148k = add k 1
149end
150end
151ret eq k 10
152end
153note evfa f / evfe f: start and end of field f (0..10) of the line whose tabs are in gevtb
154fn evfa f ulong to ulong
155if eq f 0
156ret 0
157end
158ret add get gevtb sub f 1 1
159end
160fn evfe f ulong ln ulong to ulong
161if ge f 10
162ret ln
163end
164ret get gevtb f
165end
166note evfnum ln f: the decimal number in field f (at most 19 digits), 0 for anything else
167fn evfnum ln view uchar * f ulong to ulong
168a = evfa f
169e = evfe f len ln
170if or eq a e gt sub e a 19
171ret 0
172end
173v = ulong 0
174for i ulong sub e a bound 19
175c = get ln add a i
176if not evdig c
177ret 0
178end
179v = add mul v 10 ulong sub c 48
180end
181ret v
182end
183note evfcopy ln f w: field f into the event field w (1 rid, 2 source, 3 x)
184fn evfcopy ln view uchar * f ulong w ulong to void
185a = evfa f
186e = evfe f len ln
187for i ulong sub e a bound 8192
188c = get ln add a i
189if eq w 1
190evridc c
191end
192if eq w 2
193evsrcc c
194end
195if eq w 3
196evxc c
197end
198end
199end
200note ---- the batch
201fn evbc c uchar to void
202n = get gevbatl 0
203if lt n len gevbat
204put gevbat n c
205put gevbatl 0 add n 1
206end
207end
208fn evbnum v ulong to void
209d = new uchar 24 0
210k = evdec v d
211for j ulong k bound 24
212evbc get d j
213end
214end
215note evemit: the event built in the gev* fields joins the batch, unless its key is known
216fn evemit to void
217if or eq get gevk 0 0 eq get gevt 0 0
218ret
219end
220k = evkey
221if evhas k
222ret
223end
224if or ge get gevbn 0 EVBMAX gt add get gevbatl 0 4096 len gevbat
225flog tevlbfl
226evflush
227if ne get gevok 0 1
228ret
229end
230end
231evput k
232evsubend
233b = get gevbn 0
234put gevbo b get gevbatl 0
235put gevbt b get gevt 0
236evbnum get gevt 0
237evbc 9
238evbnum get gevk 0
239evbc 9
240for i ulong get gevactl 0 bound 128
241evbc get gevact i
242end
243evbc 9
244for i ulong get gevridl 0 bound 64
245evbc get gevrid i
246end
247evbc 9
248for i ulong get gevsrcl 0 bound 128
249evbc get gevsrc i
250end
251evbc 9
252for i ulong get gevpthl 0 bound 1024
253evbc get gevpth i
254end
255evbc 9
256for i ulong get gevjsl 0 bound 1024
257evbc get gevjs i
258end
259evbc 9
260for i ulong get gevxl 0 bound 192
261evbc get gevx i
262end
263evbc 9
264for i ulong get gevsubl 0 bound 512
265evbc get gevsub i
266end
267put gevbl b sub get gevbatl 0 get gevbo b
268put gevbn 0 add b 1
269put gevpe 0 add get gevpe 0 1
270end
271note evsort: gevbi = the batch indices ordered by time, equal times in batch order (bottom-up merge)
272fn evsort to void
273n = get gevbn 0
274for i ulong n bound 32768
275put gevbi i i
276end
277w = ulong 1
278for pass ulong 20
279if ge w n
280break
281end
282lo = ulong 0
283for it ulong 32768
284if ge lo n
285break
286end
287mid = add lo w
288if gt mid n
289mid = n
290end
291hi = add lo mul w 2
292if gt hi n
293hi = n
294end
295evmerge lo mid hi
296lo = hi
297end
298for i ulong n bound 32768
299put gevbi i get gevbj i
300end
301w = mul w 2
302end
303end
304fn evmerge lo ulong mid ulong hi ulong to void
305i = lo
306j = mid
307for k ulong sub hi lo bound 32768
308if evtakel i mid j hi
309put gevbj add lo k get gevbi i
310i = add i 1
311else
312put gevbj add lo k get gevbi j
313j = add j 1
314end
315end
316end
317note evtakel: the next merged entry comes from the left run (equal times keep batch order)
318fn evtakel i ulong mid ulong j ulong hi ulong to bool
319if ge i mid
320ret false
321end
322if ge j hi
323ret true
324end
325ret le get gevbt get gevbi i get gevbt get gevbi j
326end
327note evflush: append the batch in time order, each line with its seq and the chain, then fsync once
328fn evflush to void
329n = get gevbn 0
330if eq n 0
331ret
332end
333evsort
334evpath tevlog
335fd = fopen fpth 0 get fpthl 0 525377 384
336if lt fd 0
337evwfail
338ret
339end
340ok = true
341for q ulong n bound 32768
342if ge get gevn 0 EVMAX
343break
344end
345b = get gevbi q
346if not evwline int fd b
347ok = false
348break
349end
350end
351x = fsync int fd
352y = fclose int fd
353if not ok
354evwfail
355end
356put gevbn 0 0
357put gevbatl 0 0
358end
359fn evwfail to void
360put gevok 0 2
361flog tevlwr
362end
363note evwline fd b: write batch entry b as line gevn+1 (built in gevln, hashed, written, then indexed)
364fn evwline fd int b ulong to bool
365l = evwbuild b
366if ge l sub EVLMAX 1
367ret false
368end
369ret evwout fd l
370end
371fn evwbuild b ulong to ulong
372put gevlnl 0 0
373evlnum add get gevn 0 1
374evlc 9
375o = get gevbo b
376for i ulong get gevbl b bound 8192
377evlc get gevbat add o i
378end
379evlc 9
380for i ulong 32
381v = ulong get gevprv i
382evlc evhexc shr v 4
383evlc evhexc and v 15
384end
385ret get gevlnl 0
386end
387note evsha ln: sha256 of ln into gevh2
388fn evsha ln view uchar * to void
389c = new ulong 40 0
390h = new uchar 32 0
391sha256_init c
392sha256_update c ln
393sha256_final c h
394for i ulong 32
395put gevh2 i get h i
396end
397end
398fn evwout fd int l ulong to bool
399ln = new uchar 8192 0
400for i ulong l bound 8192
401put ln i get gevln i
402end
403evsha part ln 0 l
404put gevln l 10
405w = write fd gevln 0 add l 1
406if ne w long add l 1
407ret false
408end
409for i ulong 32
410put gevprv i get gevh2 i
411end
412k = get gevn 0
413put gevn 0 add k 1
414put gevoff add k 1 add get gevoff k add l 1
415ret true
416end