-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq.c
More file actions
1261 lines (1184 loc) · 34.2 KB
/
Copy pathq.c
File metadata and controls
1261 lines (1184 loc) · 34.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2026 RayforceDB Team
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#define _GNU_SOURCE
/*
* Q IPC wire-format client core (see q.h). Language-neutral
*/
#include "q.h"
#include "table/sym.h" /* RAY_SYM_W64 */
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <netdb.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
/* Two helpers the rayforce core public header does not (yet) export. Defined
* here so q stays self-contained and binding-independent. */
/* Dict attr bit on ray_t->attrs (v2: a dict is RAY_LIST + this bit).
* Value must match src/mem/heap.h. */
#ifndef RAY_ATTR_DICT
#define RAY_ATTR_DICT 0x02
#endif
/* Byte width of a fixed-size scalar/vec element, for both atom (negative) and
* vec (positive) type codes. Returns 0 for variable-width/unknown types
* (RAY_LIST, RAY_SYM, RAY_STR, RAY_TABLE, RAY_DICT, ...). */
static inline size_t ray_scalar_elem_size(int8_t type) {
switch (type < 0 ? -type : type) {
case RAY_BOOL:
case RAY_U8:
return 1;
case RAY_I16:
return 2;
case RAY_I32:
case RAY_DATE:
case RAY_TIME:
case RAY_F32:
return 4;
case RAY_I64:
case RAY_F64:
case RAY_TIMESTAMP:
return 8;
case RAY_GUID:
return 16;
default:
return 0;
}
}
#define Q_KB 1 /* boolean */
#define Q_UU 2 /* guid (16B) */
#define Q_KG 4 /* byte */
#define Q_KH 5 /* short */
#define Q_KI 6 /* int */
#define Q_KJ 7 /* long */
#define Q_KE 8 /* real (f32) */
#define Q_KF 9 /* float (f64) */
#define Q_KC 10 /* char */
#define Q_KS 11 /* symbol */
#define Q_KP 12 /* timestamp */
#define Q_KM 13 /* month */
#define Q_KD 14 /* date */
#define Q_KZ 15 /* datetime */
#define Q_KN 16 /* timespan */
#define Q_KU 17 /* minute */
#define Q_KV 18 /* second */
#define Q_KT 19 /* time */
#define Q_XT 98 /* table */
#define Q_XD 99 /* dict */
#define Q_ERR (-128)
#define Q_MSG_SYNC 1
#define Q_MAX_BODY ((int64_t)256 << 20)
typedef struct {
uint8_t endianness;
uint8_t msgtype;
uint8_t compressed;
uint8_t reserved;
uint32_t size;
} q_header_t;
#define Q_LITTLE_ENDIAN 1
static void q_set_err(char *err, size_t errlen, const char *msg) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "%s", msg);
}
/* Build a v2 table from a RAY_SYM-vec of column ids and an array of column
* vectors. Retains each column internally; the caller keeps ownership of the
* inputs. (Local copy so q has no binding-specific dependencies.) */
static ray_t *q_build_table(const int64_t *col_ids, ray_t *const *cols,
int64_t ncols) {
ray_t *tbl = ray_table_new(ncols);
if (tbl == NULL)
return ray_error("table_new failed", NULL);
if (RAY_IS_ERR(tbl))
return tbl;
for (int64_t i = 0; i < ncols; i++) {
if (cols[i] == NULL) {
ray_release(tbl);
return ray_error("table column is null", NULL);
}
/* ray_table_add_col retains the column internally and may return a new
* pointer on realloc. */
ray_t *res = ray_table_add_col(tbl, col_ids[i], cols[i]);
if (res == NULL || RAY_IS_ERR(res)) {
ray_release(tbl);
return res ? res : ray_error("table_add_col failed", NULL);
}
tbl = res;
}
return tbl;
}
static ssize_t q_recv_all(int fd, void *buf, size_t n) {
size_t total = 0;
uint8_t *p = (uint8_t *)buf;
while (total < n) {
ssize_t r = recv(fd, p + total, n - total, 0);
if (r == 0)
return -1;
if (r < 0) {
if (errno == EINTR)
continue;
return -1;
}
total += (size_t)r;
}
return (ssize_t)total;
}
static ssize_t q_send_all(int fd, const void *buf, size_t n) {
size_t total = 0;
const uint8_t *p = (const uint8_t *)buf;
while (total < n) {
ssize_t r = send(fd, p + total, n - total, 0);
if (r <= 0) {
if (r < 0 && errno == EINTR)
continue;
return -1;
}
total += (size_t)r;
}
return (ssize_t)total;
}
/* Connect one address with an optional timeout (ms)
* Returns 0 on success, -1 on plain failure, -2 on timeout. */
static int q_connect_one(const struct addrinfo *p, int timeout_ms, int fd) {
if (timeout_ms <= 0)
return connect(fd, p->ai_addr, p->ai_addrlen) == 0 ? 0 : -1;
int flags = fcntl(fd, F_GETFL, 0);
if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
return -1;
int rc = connect(fd, p->ai_addr, p->ai_addrlen);
if (rc == 0) {
fcntl(fd, F_SETFL, flags);
return 0;
}
if (errno != EINPROGRESS)
return -1;
struct pollfd pfd = {.fd = fd, .events = POLLOUT};
int pr;
do {
pr = poll(&pfd, 1, timeout_ms);
} while (pr < 0 && errno == EINTR);
if (pr == 0)
return -2; /* timeout */
if (pr < 0)
return -1;
int soerr = 0;
socklen_t slen = sizeof soerr;
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &soerr, &slen) < 0 || soerr != 0)
return -1;
fcntl(fd, F_SETFL, flags); /* back to blocking */
return 0;
}
/* Open a TCP connection. */
static int q_open_socket(const char *host, int port, int timeout_ms,
int *timed_out) {
*timed_out = 0;
char service[16];
snprintf(service, sizeof(service), "%d", port);
struct addrinfo hints = {0};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *res = NULL;
if (getaddrinfo(host, service, &hints, &res) != 0 || res == NULL)
return -1;
int fd = -1;
int any_timeout = 0;
for (struct addrinfo *p = res; p != NULL; p = p->ai_next) {
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (fd < 0)
continue;
int rc = q_connect_one(p, timeout_ms, fd);
if (rc == 0)
break;
if (rc == -2)
any_timeout = 1;
close(fd);
fd = -1;
}
freeaddrinfo(res);
if (fd < 0)
*timed_out = any_timeout;
return fd;
}
/* Apply a send/recv timeout (ms) to a connected socket. */
static void q_set_timeout(int fd, int timeout_ms) {
if (timeout_ms <= 0)
return;
struct timeval tv = {.tv_sec = timeout_ms / 1000,
.tv_usec = (timeout_ms % 1000) * 1000};
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
}
static int8_t q_type_of(int8_t ray_type) {
int sign = (ray_type < 0) ? -1 : 1;
int t = (ray_type < 0) ? -ray_type : ray_type;
switch (t) {
case RAY_BOOL:
return (int8_t)(sign * Q_KB);
case RAY_U8:
return (int8_t)(sign * Q_KG);
case RAY_I16:
return (int8_t)(sign * Q_KH);
case RAY_I32:
return (int8_t)(sign * Q_KI);
case RAY_I64:
return (int8_t)(sign * Q_KJ);
case RAY_F32:
return (int8_t)(sign * Q_KE);
case RAY_F64:
return (int8_t)(sign * Q_KF);
case RAY_DATE:
return (int8_t)(sign * Q_KD);
case RAY_TIME:
return (int8_t)(sign * Q_KT);
case RAY_TIMESTAMP:
return (int8_t)(sign * Q_KP);
case RAY_GUID:
return (int8_t)(sign * Q_UU);
case RAY_SYM:
return (int8_t)(sign * Q_KS);
case RAY_STR:
return (int8_t)(sign * Q_KC);
case RAY_LIST:
return 0;
case RAY_TABLE:
return Q_XT;
case RAY_DICT:
return Q_XD;
case RAY_ERROR:
return Q_ERR;
default:
return 0;
}
}
static int64_t q_size_obj(ray_t *obj);
static int64_t q_ser_obj(uint8_t *buf, ray_t *obj);
static ray_t *q_des_obj(uint8_t **buf, int64_t *len);
/* Build a v2 table from RAY_SYM-vec of names and RAY_LIST of vectors. */
static ray_t *q_make_table(ray_t *keys, ray_t *vals) {
if (keys == NULL || vals == NULL || keys->type != RAY_SYM ||
vals->type != RAY_LIST || keys->len != vals->len) {
if (keys)
ray_release(keys);
if (vals)
ray_release(vals);
return ray_error("q: malformed table — expected (sym-vec, list)", NULL);
}
ray_t *tbl = q_build_table((const int64_t *)ray_data(keys),
(ray_t *const *)ray_data(vals), keys->len);
ray_release(keys);
ray_release(vals);
return tbl;
}
static int64_t q_size_obj(ray_t *obj) {
if (obj == NULL || obj == RAY_NULL_OBJ)
return 1 + 1 + 4; /* type + attrs + len(0) */
int8_t t = obj->type;
/* Atoms */
if (t < 0) {
int abs_t = -t;
switch (abs_t) {
case RAY_BOOL:
case RAY_U8:
return 1 + 1;
case RAY_I16:
return 1 + 2;
case RAY_I32:
case RAY_DATE:
case RAY_TIME:
case RAY_F32:
return 1 + 4;
case RAY_I64:
case RAY_TIMESTAMP:
case RAY_F64:
return 1 + 8;
case RAY_GUID:
return 1 + 16;
case RAY_SYM: {
ray_t *s = ray_sym_str(obj->i64);
int64_t n = s ? (int64_t)ray_str_len(s) : 0;
if (s)
ray_release(s);
return 1 + n + 1; /* null-terminated */
}
case RAY_STR: {
/* Single char goes as -KC atom; multi-char as KC vector. */
int64_t n = (int64_t)ray_str_len(obj);
if (n == 1)
return 1 + 1;
return 1 + 1 + 4 + n;
}
}
return 0;
}
/* Vectors / containers */
if (t == RAY_LIST) {
int64_t size = 1 + 1 + 4;
ray_t **elems = (ray_t **)ray_data(obj);
for (int64_t i = 0; i < obj->len; i++)
size += q_size_obj(elems[i]);
return size;
}
if (t == RAY_SYM) {
int64_t size = 1 + 1 + 4;
int64_t *ids = (int64_t *)ray_data(obj);
for (int64_t i = 0; i < obj->len; i++) {
ray_t *s = ray_sym_str(ids[i]);
size += s ? (int64_t)ray_str_len(s) + 1 : 1;
if (s)
ray_release(s);
}
return size;
}
if (t == RAY_TABLE) {
/* Wire form: XT byte + attrs(0) + XD marker + KS-vec of names +
* list-0 of column vectors. RAY_TABLE is opaque - extract via the
* public accessors instead of indexing ray_data directly. */
int64_t ncols = ray_table_ncols(obj);
int64_t names = 1 + 1 + 4; /* KS type + attrs + count */
for (int64_t i = 0; i < ncols; i++) {
ray_t *s = ray_sym_str(ray_table_col_name(obj, i));
names += s ? (int64_t)ray_str_len(s) + 1 : 1;
if (s)
ray_release(s);
}
int64_t cols = 1 + 1 + 4; /* list type + attrs + count */
for (int64_t i = 0; i < ncols; i++)
cols += q_size_obj(ray_table_get_col_idx(obj, i));
return 3 + names + cols; /* XT + attrs + XD */
}
if (t == RAY_ERROR) {
const char *msg = ray_err_code(obj);
int64_t n = msg ? (int64_t)strlen(msg) : 0;
return 1 + n + 1; /* type byte + msg + null */
}
if (t == RAY_NULL)
return 1 + 1 + 4;
if (t == RAY_STR) {
int64_t size = 1 + 1 + 4; /* list type + attrs + count */
for (int64_t i = 0; i < obj->len; i++) {
size_t slen = 0;
ray_str_vec_get(obj, i, &slen);
size += 1 + 1 + 4 + (int64_t)slen; /* KC vec header + chars */
}
return size;
}
int esz = (int)ray_scalar_elem_size(t);
if (esz == 0)
return 0;
return 1 + 1 + 4 + obj->len * esz;
}
static int64_t q_ser_obj(uint8_t *buf, ray_t *obj) {
uint8_t *start = buf;
if (obj == NULL || obj == RAY_NULL_OBJ) {
*buf++ = 101; /* identity / null */
*buf++ = 0;
return buf - start;
}
int8_t t = obj->type;
*buf++ = (uint8_t)q_type_of(t);
if (t < 0) {
int abs_t = -t;
switch (abs_t) {
case RAY_BOOL:
case RAY_U8:
*buf++ = obj->u8;
return buf - start;
case RAY_I16:
memcpy(buf, &obj->i16, 2);
return buf + 2 - start;
case RAY_I32:
case RAY_DATE:
case RAY_TIME:
memcpy(buf, &obj->i32, 4);
return buf + 4 - start;
case RAY_F32: {
float f = (float)obj->f64;
memcpy(buf, &f, 4);
return buf + 4 - start;
}
case RAY_I64:
case RAY_TIMESTAMP:
memcpy(buf, &obj->i64, 8);
return buf + 8 - start;
case RAY_F64:
memcpy(buf, &obj->f64, 8);
return buf + 8 - start;
case RAY_GUID:
/* GUID *vectors* store contiguous elements at ray_data and go through the
* generic vector path below. */
memcpy(buf, (const char *)obj->obj + sizeof(ray_t), 16);
return buf + 16 - start;
case RAY_SYM: {
ray_t *s = ray_sym_str(obj->i64);
size_t n = s ? ray_str_len(s) : 0;
if (s) {
memcpy(buf, ray_str_ptr(s), n);
ray_release(s);
}
buf[n] = 0;
return buf + n + 1 - start;
}
case RAY_STR: {
size_t n = ray_str_len(obj);
if (n == 1) {
*buf++ = (uint8_t)ray_str_ptr(obj)[0];
return buf - start;
}
/* RAY_STR atom (n>1) -> KC vector */
start[0] = (uint8_t)Q_KC;
*buf++ = 0; /* attrs */
uint32_t len32 = (uint32_t)n;
memcpy(buf, &len32, 4);
buf += 4;
memcpy(buf, ray_str_ptr(obj), n);
return buf + n - start;
}
}
return -1;
}
/* Containers/vectors */
if (t == RAY_LIST) {
*buf++ = 0;
uint32_t len32 = (uint32_t)obj->len;
memcpy(buf, &len32, 4);
buf += 4;
ray_t **elems = (ray_t **)ray_data(obj);
for (int64_t i = 0; i < obj->len; i++) {
int64_t r = q_ser_obj(buf, elems[i]);
if (r < 0)
return -1;
buf += r;
}
return buf - start;
}
if (t == RAY_SYM) {
*buf++ = 0;
uint32_t len32 = (uint32_t)obj->len;
memcpy(buf, &len32, 4);
buf += 4;
int64_t *ids = (int64_t *)ray_data(obj);
for (int64_t i = 0; i < obj->len; i++) {
ray_t *s = ray_sym_str(ids[i]);
size_t n = s ? ray_str_len(s) : 0;
if (s) {
memcpy(buf, ray_str_ptr(s), n);
ray_release(s);
}
buf[n] = 0;
buf += n + 1;
}
return buf - start;
}
if (t == RAY_TABLE) {
int64_t ncols = ray_table_ncols(obj);
*buf++ = 0; /* attrs */
*buf++ = (uint8_t)Q_XD; /* table is dict-of-cols on the wire */
/* keys: KS vector of column names */
*buf++ = (uint8_t)Q_KS;
*buf++ = 0; /* attrs */
uint32_t kn = (uint32_t)ncols;
memcpy(buf, &kn, 4);
buf += 4;
for (int64_t i = 0; i < ncols; i++) {
ray_t *s = ray_sym_str(ray_table_col_name(obj, i));
size_t n = s ? ray_str_len(s) : 0;
if (s) {
memcpy(buf, ray_str_ptr(s), n);
ray_release(s);
}
buf[n] = 0;
buf += n + 1;
}
/* values: general list of column vectors */
*buf++ = 0; /* list type */
*buf++ = 0; /* attrs */
memcpy(buf, &kn, 4);
buf += 4;
for (int64_t i = 0; i < ncols; i++) {
int64_t r = q_ser_obj(buf, ray_table_get_col_idx(obj, i));
if (r < 0)
return -1;
buf += r;
}
return buf - start;
}
/* RAY_DICT (type code 99) is never produced by v2; dicts present as
* RAY_LIST + RAY_ATTR_DICT and are serialized through the RAY_LIST path
* above (losing the dict-ness on the wire). */
if (t == RAY_ERROR) {
const char *msg = ray_err_code(obj);
size_t n = msg ? strlen(msg) : 0;
if (n)
memcpy(buf, msg, n);
buf[n] = 0;
return buf + n + 1 - start;
}
if (t == RAY_NULL) {
*buf++ = 0;
memset(buf, 0, 4);
return buf + 4 - start;
}
if (t == RAY_STR) {
start[0] = 0;
*buf++ = 0; /* attrs */
uint32_t len32 = (uint32_t)obj->len;
memcpy(buf, &len32, 4);
buf += 4;
for (int64_t i = 0; i < obj->len; i++) {
size_t slen = 0;
const char *s = ray_str_vec_get(obj, i, &slen);
*buf++ = (uint8_t)Q_KC; /* each element is a char vector */
*buf++ = 0; /* attrs */
uint32_t l = (uint32_t)slen;
memcpy(buf, &l, 4);
buf += 4;
if (slen)
memcpy(buf, s, slen);
buf += (int64_t)slen;
}
return buf - start;
}
int esz = (int)ray_scalar_elem_size(t);
if (esz == 0)
return -1;
*buf++ = 0;
uint32_t len32 = (uint32_t)obj->len;
memcpy(buf, &len32, 4);
buf += 4;
size_t n = (size_t)obj->len * esz;
memcpy(buf, ray_data(obj), n);
return buf + n - start;
}
#define Q_NEED(n) \
do { \
if (*len < (int64_t)(n)) \
return ray_error("q: buffer underflow", NULL); \
} while (0)
/* Decode a width-byte atom and re-tag as the requested ray_type */
static ray_t *q_des_atom_i(uint8_t **buf, int64_t *len, int8_t ray_type,
int width) {
Q_NEED(width);
ray_t *o = NULL;
switch (width) {
case 1:
o = ray_u8(**buf);
break;
case 2: {
int16_t v;
memcpy(&v, *buf, 2);
o = ray_i16(v);
break;
}
case 4: {
int32_t v;
memcpy(&v, *buf, 4);
o = ray_i32(v);
break;
}
case 8: {
int64_t v;
memcpy(&v, *buf, 8);
o = ray_i64(v);
break;
}
}
if (o)
o->type = -ray_type; /* tag with the requested ray atom type */
*buf += width;
*len -= width;
return o;
}
/* Read a q vector header: 1 byte attrs + 4 bytes int32 length.
* Advances *buf by 5 and decrements *len. Returns -1 on buffer underflow. */
static int q_read_vec_header(uint8_t **buf, int64_t *len, int32_t *out_n) {
if (*len < 5)
return -1;
(*buf)++;
*len -= 1; /* attrs */
memcpy(out_n, *buf, 4);
*buf += 4;
*len -= 4;
return 0;
}
static ray_t *q_des_vec_i(uint8_t **buf, int64_t *len, int8_t ray_type,
int width) {
int32_t n;
if (q_read_vec_header(buf, len, &n) < 0)
return ray_error("q: buffer underflow", NULL);
if (n < 0)
return ray_error("q: negative vector length", NULL);
int64_t bytes = (int64_t)n * width;
if (*len < bytes)
return ray_error("q: buffer underflow (vec body)", NULL);
ray_t *vec = ray_vec_new(ray_type, n);
if (vec == NULL || RAY_IS_ERR(vec)) {
if (vec)
ray_release(vec);
return ray_error("q: vector alloc failed", NULL);
}
memcpy(ray_data(vec), *buf, bytes);
vec->len = n;
*buf += bytes;
*len -= bytes;
return vec;
}
static inline int64_t q_kz_days_to_nanos(double days) {
if (isnan(days))
return NULL_I64;
return (int64_t)llround(days * 86400000.0) * 1000000LL;
}
static ray_t *q_des_obj(uint8_t **buf, int64_t *len) {
if (*len < 1)
return ray_error("q: buffer underflow (type)", NULL);
int8_t type = (int8_t)**buf;
(*buf)++;
(*len)--;
switch (type) {
/* Atoms (negative type). The deserializer reads raw bytes then re-tags. */
case -Q_KB:
return q_des_atom_i(buf, len, RAY_BOOL, 1);
case -Q_KG:
return q_des_atom_i(buf, len, RAY_U8, 1);
case -Q_KH:
return q_des_atom_i(buf, len, RAY_I16, 2);
case -Q_KI:
return q_des_atom_i(buf, len, RAY_I32, 4);
case -Q_KJ:
return q_des_atom_i(buf, len, RAY_I64, 8);
case -Q_KP:
case -Q_KN:
return q_des_atom_i(buf, len, RAY_TIMESTAMP, 8);
case -Q_KD:
case -Q_KM:
return q_des_atom_i(buf, len, RAY_DATE, 4);
case -Q_KT:
case -Q_KU:
case -Q_KV:
return q_des_atom_i(buf, len, RAY_TIME, 4);
case -Q_KZ: {
Q_NEED(8);
double d;
memcpy(&d, *buf, 8);
*buf += 8;
*len -= 8;
return ray_timestamp(q_kz_days_to_nanos(d));
}
case -Q_KE: {
Q_NEED(4);
float f;
memcpy(&f, *buf, 4);
*buf += 4;
*len -= 4;
return ray_f64((double)f);
}
case -Q_KF: {
Q_NEED(8);
double f;
memcpy(&f, *buf, 8);
*buf += 8;
*len -= 8;
return ray_f64(f);
}
case -Q_KC: {
Q_NEED(1);
char c = (char)**buf;
*buf += 1;
*len -= 1;
return ray_str(&c, 1);
}
case -Q_KS: {
int64_t n = 0;
while (n < *len && (*buf)[n] != '\0')
n++;
if (n >= *len)
return ray_error("q: symbol not null-terminated", NULL);
int64_t id = ray_sym_intern((const char *)*buf, (size_t)n);
*buf += n + 1;
*len -= n + 1;
return (id < 0) ? ray_error("q: symbol intern failed", NULL) : ray_sym(id);
}
case -Q_UU: {
Q_NEED(16);
ray_t *g = ray_guid(*buf);
*buf += 16;
*len -= 16;
return g;
}
/* Vectors */
case Q_KB:
return q_des_vec_i(buf, len, RAY_BOOL, 1);
case Q_KG:
return q_des_vec_i(buf, len, RAY_U8, 1);
case Q_KH:
return q_des_vec_i(buf, len, RAY_I16, 2);
case Q_KI:
return q_des_vec_i(buf, len, RAY_I32, 4);
case Q_KJ:
return q_des_vec_i(buf, len, RAY_I64, 8);
case Q_KP:
case Q_KN:
return q_des_vec_i(buf, len, RAY_TIMESTAMP, 8);
case Q_KD:
case Q_KM:
return q_des_vec_i(buf, len, RAY_DATE, 4);
case Q_KT:
case Q_KU:
case Q_KV:
return q_des_vec_i(buf, len, RAY_TIME, 4);
case Q_KZ: {
int32_t n;
if (q_read_vec_header(buf, len, &n) < 0)
return ray_error("q: buffer underflow", NULL);
if (n < 0)
return ray_error("q: negative datetime-vec length", NULL);
int64_t bytes = (int64_t)n * 8;
if (*len < bytes)
return ray_error("q: buffer underflow (datetime-vec)", NULL);
ray_t *vec = ray_vec_new(RAY_TIMESTAMP, n);
if (vec == NULL || RAY_IS_ERR(vec)) {
if (vec)
ray_release(vec);
return ray_error("q: vector alloc failed", NULL);
}
int64_t *out = (int64_t *)ray_data(vec);
for (int32_t i = 0; i < n; i++) {
double d;
memcpy(&d, *buf + i * 8, 8);
out[i] = q_kz_days_to_nanos(d);
}
vec->len = n;
*buf += bytes;
*len -= bytes;
return vec;
}
case Q_KE: {
/* Real (4-byte float) vector — convert to F64 vec for v2. */
int32_t n;
if (q_read_vec_header(buf, len, &n) < 0)
return ray_error("q: buffer underflow", NULL);
if (n < 0)
return ray_error("q: negative real-vec length", NULL);
int64_t bytes = (int64_t)n * 4;
if (*len < bytes)
return ray_error("q: buffer underflow (real-vec)", NULL);
ray_t *vec = ray_vec_new(RAY_F64, n);
if (vec == NULL || RAY_IS_ERR(vec)) {
if (vec)
ray_release(vec);
return ray_error("q: vector alloc failed", NULL);
}
double *out = (double *)ray_data(vec);
for (int32_t i = 0; i < n; i++) {
float f;
memcpy(&f, *buf + i * 4, 4);
out[i] = (double)f;
}
vec->len = n;
*buf += bytes;
*len -= bytes;
return vec;
}
case Q_KF:
return q_des_vec_i(buf, len, RAY_F64, 8);
case Q_UU:
return q_des_vec_i(buf, len, RAY_GUID, 16);
case Q_KC: {
/* KC vector → RAY_STR atom of length n */
int32_t n;
if (q_read_vec_header(buf, len, &n) < 0)
return ray_error("q: buffer underflow", NULL);
if (n < 0)
return ray_error("q: negative char-vec length", NULL);
if (*len < n)
return ray_error("q: buffer underflow (char-vec)", NULL);
ray_t *s = ray_str((const char *)*buf, (size_t)n);
*buf += n;
*len -= n;
return s;
}
case Q_KS: {
int32_t n;
if (q_read_vec_header(buf, len, &n) < 0)
return ray_error("q: buffer underflow", NULL);
if (n < 0)
return ray_error("q: negative symbol-vec length", NULL);
ray_t *vec = ray_sym_vec_new(RAY_SYM_W64, n);
if (vec == NULL || RAY_IS_ERR(vec)) {
if (vec)
ray_release(vec);
return ray_error("q: symbol vector alloc failed", NULL);
}
int64_t *ids = (int64_t *)ray_data(vec);
for (int32_t i = 0; i < n; i++) {
int64_t k = 0;
while (k < *len && (*buf)[k] != '\0')
k++;
if (k >= *len) {
ray_release(vec);
return ray_error("q: symbol not null-terminated in vec", NULL);
}
int64_t id = ray_sym_intern((const char *)*buf, (size_t)k);
if (id < 0) {
ray_release(vec);
return ray_error("q: symbol intern failed", NULL);
}
ids[i] = id;
*buf += k + 1;
*len -= k + 1;
}
vec->len = n;
return vec;
}
case 0: { /* general list */
int32_t n;
if (q_read_vec_header(buf, len, &n) < 0)
return ray_error("q: buffer underflow", NULL);
if (n < 0)
return ray_error("q: negative list length", NULL);
ray_t *list = ray_list_new(0);
for (int32_t i = 0; i < n; i++) {
ray_t *elem = q_des_obj(buf, len);
if (elem == NULL || RAY_IS_ERR(elem)) {
ray_release(list);
return elem ? elem : ray_error("q: list element decode failed", NULL);
}
list = ray_list_append(list, elem);
ray_release(elem); /* append retains its own ref; drop ours */
if (list == NULL || RAY_IS_ERR(list))
return list ? list : ray_error("q: list append failed", NULL);
}
return list;
}
case Q_XT: { /* table = attrs(0) + dict_marker(99) + keys + values */
Q_NEED(2);
(*buf) += 2;
*len -= 2;
ray_t *keys = q_des_obj(buf, len);
if (keys == NULL || RAY_IS_ERR(keys))
return keys;
ray_t *vals = q_des_obj(buf, len);
if (vals == NULL || RAY_IS_ERR(vals)) {
ray_release(keys);
return vals;
}
return q_make_table(keys, vals);
}
case Q_XD: { /* dict = keys + values; could be a keyed table */
ray_t *keys = q_des_obj(buf, len);
if (keys == NULL || RAY_IS_ERR(keys))
return keys;
ray_t *vals = q_des_obj(buf, len);
if (vals == NULL || RAY_IS_ERR(vals)) {
ray_release(keys);
return vals;
}
/* A plain dict (e.g. `a`b!1 2) becomes a native rayforce dict. A keyed
* table arrives as a dict whose key and value are both tables. Rayforce
* has no keyed-table type here, so keep that as a 2-element
* RAY_LIST + ATTR_DICT (key-table, value-table). */
if (keys->type == RAY_TABLE && vals->type == RAY_TABLE) {
ray_t *kt = ray_list_new(0);
kt = ray_list_append(kt, keys);
ray_release(keys); /* append retains; drop our ref */
kt = ray_list_append(kt, vals);
ray_release(vals);
if (kt && !RAY_IS_ERR(kt))
kt->attrs |= RAY_ATTR_DICT;
return kt;
}
return ray_dict_new(keys, vals); /* consumes both refs */
}
case Q_ERR: {
/* The error string is NUL-terminated on the wire. Ensure the terminator is
* within the remaining buffer before handing it to ray_error. */
const char *s = (const char *)*buf;
int64_t i = 0;
while (i < *len && s[i] != '\0')
i++;
if (i == *len)
return ray_error("q: malformed error frame", NULL);
*buf += i + 1;
*len -= i + 1;
/* Put the q error text in both the code (short, shown by ray_fmt) and the
* message (full), so bindings reading the message field get the whole
* string even though the displayed code is length-capped. */
return ray_error(s, "%s", s);
}
default:
return ray_error("q: unsupported wire type", NULL);
}
}
/* Decompression */
static int q_decompress(const uint8_t *src, int64_t src_len, uint8_t **out_buf,
int64_t *out_len) {
if (src_len < 4)
return -1;