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