1 #include "ejdb2_internal.h"
2 #include <iowow/iwutils.h>
3
4 // ---------------------------------------------------------------------------
5
6 // fixme: code duplication below
jbi_jbl_fill_ikey(JBIDX idx,JBL jbv,IWKV_val * ikey,char numbuf[static IWNUMBUF_SIZE])7 void jbi_jbl_fill_ikey(JBIDX idx, JBL jbv, IWKV_val *ikey, char numbuf[static IWNUMBUF_SIZE]) {
8 int64_t *llv = (void*) numbuf;
9 jbl_type_t jbvt = jbl_type(jbv);
10 ejdb_idx_mode_t itype = (idx->mode & ~(EJDB_IDX_UNIQUE));
11 ikey->size = 0;
12 ikey->data = 0;
13
14 switch (itype) {
15 case EJDB_IDX_STR:
16 switch (jbvt) {
17 case JBV_STR:
18 ikey->size = jbl_size(jbv);
19 ikey->data = (void*) jbl_get_str(jbv);
20 break;
21 case JBV_I64:
22 ikey->size = (size_t) iwitoa(jbl_get_i64(jbv), numbuf, IWNUMBUF_SIZE);
23 ikey->data = numbuf;
24 break;
25 case JBV_BOOL:
26 if (jbl_get_i32(jbv)) {
27 ikey->size = sizeof("true");
28 ikey->data = "true";
29 } else {
30 ikey->size = sizeof("false");
31 ikey->data = "false";
32 }
33 break;
34 case JBV_F64:
35 iwjson_ftoa(jbl_get_f64(jbv), numbuf, &ikey->size);
36 ikey->data = numbuf;
37 break;
38 default:
39 break;
40 }
41 break;
42 case EJDB_IDX_I64:
43 ikey->size = sizeof(*llv);
44 ikey->data = llv;
45 switch (jbvt) {
46 case JBV_I64:
47 case JBV_F64:
48 case JBV_BOOL:
49 *llv = jbl_get_i64(jbv);
50 break;
51 case JBV_STR:
52 *llv = iwatoi(jbl_get_str(jbv));
53 break;
54 default:
55 ikey->size = 0;
56 ikey->data = 0;
57 break;
58 }
59 break;
60 case EJDB_IDX_F64:
61 ikey->data = numbuf;
62 switch (jbvt) {
63 case JBV_F64:
64 case JBV_I64:
65 case JBV_BOOL:
66 iwjson_ftoa(jbl_get_f64(jbv), numbuf, &ikey->size);
67 break;
68 case JBV_STR:
69 iwjson_ftoa(iwatof(jbl_get_str(jbv)), numbuf, &ikey->size);
70 break;
71 default:
72 ikey->size = 0; // -V1048
73 ikey->data = 0;
74 break;
75 }
76 break;
77 default:
78 break;
79 }
80 }
81
jbi_jqval_fill_ikey(JBIDX idx,const JQVAL * jqval,IWKV_val * ikey,char numbuf[static IWNUMBUF_SIZE])82 void jbi_jqval_fill_ikey(JBIDX idx, const JQVAL *jqval, IWKV_val *ikey, char numbuf[static IWNUMBUF_SIZE]) {
83 int64_t *llv = (void*) numbuf;
84 ikey->size = 0;
85 ikey->data = numbuf;
86 ejdb_idx_mode_t itype = (idx->mode & ~(EJDB_IDX_UNIQUE));
87 jqval_type_t jqvt = jqval->type;
88
89 switch (itype) {
90 case EJDB_IDX_STR:
91 switch (jqvt) {
92 case JQVAL_STR:
93 ikey->size = strlen(jqval->vstr);
94 ikey->data = (void*) jqval->vstr;
95 break;
96 case JQVAL_I64:
97 ikey->size = (size_t) iwitoa(jqval->vi64, numbuf, IWNUMBUF_SIZE);
98 break;
99 case JQVAL_BOOL:
100 if (jqval->vbool) {
101 ikey->size = sizeof("true");
102 ikey->data = "true";
103 } else {
104 ikey->size = sizeof("false");
105 ikey->data = "false";
106 }
107 break;
108 case JQVAL_F64:
109 iwjson_ftoa(jqval->vf64, numbuf, &ikey->size);
110 break;
111 default:
112 break;
113 }
114 break;
115 case EJDB_IDX_I64:
116 ikey->size = sizeof(*llv);
117 switch (jqvt) {
118 case JQVAL_I64:
119 *llv = jqval->vi64;
120 break;
121 case JQVAL_F64:
122 *llv = (int64_t) jqval->vf64;
123 break;
124 case JQVAL_BOOL:
125 *llv = jqval->vbool;
126 break;
127 case JQVAL_STR:
128 *llv = iwatoi(jqval->vstr);
129 break;
130 default:
131 ikey->data = 0;
132 break;
133 }
134 break;
135 case EJDB_IDX_F64:
136 switch (jqvt) {
137 case JQVAL_F64:
138 iwjson_ftoa(jqval->vf64, numbuf, &ikey->size);
139 break;
140 case JQVAL_I64:
141 iwjson_ftoa(jqval->vi64, numbuf, &ikey->size);
142 break;
143 case JQVAL_BOOL:
144 iwjson_ftoa(jqval->vbool, numbuf, &ikey->size);
145 break;
146 case JQVAL_STR:
147 iwjson_ftoa(iwatof(jqval->vstr), numbuf, &ikey->size);
148 break;
149 default:
150 ikey->data = 0;
151 break;
152 }
153 break;
154 default:
155 break;
156 }
157 }
158
jbi_node_fill_ikey(JBIDX idx,JBL_NODE node,IWKV_val * ikey,char numbuf[static IWNUMBUF_SIZE])159 void jbi_node_fill_ikey(JBIDX idx, JBL_NODE node, IWKV_val *ikey, char numbuf[static IWNUMBUF_SIZE]) {
160 int64_t *llv = (void*) numbuf;
161 ikey->size = 0;
162 ikey->data = numbuf;
163 ejdb_idx_mode_t itype = (idx->mode & ~(EJDB_IDX_UNIQUE));
164 jbl_type_t jbvt = node->type;
165
166 switch (itype) {
167 case EJDB_IDX_STR:
168 switch (jbvt) {
169 case JBV_STR:
170 ikey->size = (size_t) node->vsize;
171 ikey->data = (void*) node->vptr;
172 break;
173 case JBV_I64:
174 ikey->size = (size_t) iwitoa(node->vi64, numbuf, IWNUMBUF_SIZE);
175 break;
176 case JBV_BOOL:
177 if (node->vbool) {
178 ikey->size = sizeof("true");
179 ikey->data = "true";
180 } else {
181 ikey->size = sizeof("false");
182 ikey->data = "false";
183 }
184 break;
185 case JBV_F64:
186 iwjson_ftoa(node->vf64, numbuf, &ikey->size);
187 break;
188 default:
189 break;
190 }
191 break;
192 case EJDB_IDX_I64:
193 ikey->size = sizeof(*llv);
194 switch (jbvt) {
195 case JBV_I64:
196 *llv = node->vi64;
197 break;
198 case JBV_F64:
199 *llv = (int64_t) node->vf64;
200 break;
201 case JBV_BOOL:
202 *llv = node->vbool;
203 break;
204 case JBV_STR:
205 *llv = iwatoi(node->vptr);
206 break;
207 default:
208 ikey->size = 0;
209 ikey->data = 0;
210 break;
211 }
212 break;
213 case EJDB_IDX_F64:
214 switch (jbvt) {
215 case JBV_F64:
216 iwjson_ftoa(node->vf64, numbuf, &ikey->size);
217 break;
218 case JBV_I64:
219 iwjson_ftoa(node->vi64, numbuf, &ikey->size);
220 break;
221 case JBV_BOOL:
222 iwjson_ftoa(node->vbool, numbuf, &ikey->size);
223 break;
224 case JBV_STR:
225 iwjson_ftoa(iwatof(node->vptr), numbuf, &ikey->size);
226 break;
227 default:
228 ikey->data = 0;
229 break;
230 }
231 break;
232 default:
233 break;
234 }
235 }
236
jbi_node_expr_matched(JQP_AUX * aux,JBIDX idx,IWKV_cursor cur,JQP_EXPR * expr,iwrc * rcp)237 bool jbi_node_expr_matched(JQP_AUX *aux, JBIDX idx, IWKV_cursor cur, JQP_EXPR *expr, iwrc *rcp) {
238 size_t sz;
239 char skey[1024];
240 char *kbuf = skey;
241 bool ret = false;
242 iwrc rc = 0;
243
244 if (!(idx->mode & (EJDB_IDX_STR | EJDB_IDX_I64 | EJDB_IDX_F64))) {
245 return false;
246 }
247 JQVAL lv, *rv = jql_unit_to_jqval(aux, expr->right, &rc);
248 RCGO(rc, finish);
249
250 rc = iwkv_cursor_copy_key(cur, kbuf, sizeof(skey) - 1, &sz, 0);
251 RCGO(rc, finish);
252 if (sz > sizeof(skey) - 1) {
253 kbuf = malloc(sz);
254 if (!kbuf) {
255 rc = iwrc_set_errno(IW_ERROR_ALLOC, errno);
256 goto finish;
257 }
258 rc = iwkv_cursor_copy_key(cur, kbuf, sizeof(skey) - 1, &sz, 0);
259 RCGO(rc, finish);
260 }
261 if (idx->mode & EJDB_IDX_STR) {
262 kbuf[sz] = '\0';
263 lv.type = JQVAL_STR;
264 lv.vstr = kbuf;
265 } else if (idx->mode & EJDB_IDX_I64) {
266 memcpy(&lv.vi64, kbuf, sizeof(lv.vi64));
267 lv.type = JQVAL_I64;
268 } else if (idx->mode & EJDB_IDX_F64) {
269 kbuf[sz] = '\0';
270 lv.type = JQVAL_F64;
271 lv.vf64 = (double) iwatof(kbuf);
272 }
273
274 ret = jql_match_jqval_pair(aux, &lv, expr->op, rv, &rc);
275
276 finish:
277 if (kbuf != skey) {
278 free(kbuf);
279 }
280 *rcp = rc;
281 return ret;
282 }
283