-
Notifications
You must be signed in to change notification settings - Fork 8
/
fhirpath_support.c
307 lines (266 loc) · 6.03 KB
/
fhirpath_support.c
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
#include "postgres.h"
#include "miscadmin.h"
#include "fhirpath.h"
#define read_byte(v, b, p) do { \
(v) = *(uint8*)((b) + (p)); \
(p) += 1; \
} while(0) \
#define read_int32(v, b, p) do { \
(v) = *(uint32*)((b) + (p)); \
(p) += sizeof(int32); \
} while(0) \
void dumpit(char *buf, int32 len);
void
dumpit(char *buf, int32 len) {
FILE* f = fopen("/tmp/dump","wb");
if(f)
fwrite(buf,1, len,f);
fclose(f);
}
/* This function passed parser AST create binary string */
/* - pack fhirpath into linear memory (storage representation) */
int
serializeFhirpathParseItem(StringInfo buf, FhirpathParseItem *item)
{
int32 pos = buf->len - VARHDRSZ; /* position from begining of fhirpath data */
int32 next = 0;
int32 chld;
/* we recursive check stack */
check_stack_depth();
/* write item type */
appendStringInfoChar(buf, (char)(item->type));
/* align ???*/
alignStringInfoInt(buf);
next = (item->next) ? buf->len : 0;
/*write next field */
appendBinaryStringInfo(buf, (char*)&next /* fake value */, sizeof(next));
switch(item->type) {
case fpEqual:
case fpOr:
case fpPipe:
/* elog(INFO, "pipe"); */
{
int32 left, right;
left = buf->len;
appendBinaryStringInfo(buf, (char*)&left /* fake value */, sizeof(left));
right = buf->len;
appendBinaryStringInfo(buf, (char*)&right /* fake value */, sizeof(right));
chld = serializeFhirpathParseItem(buf, item->args.left);
*(int32*)(buf->data + left) = chld;
chld = serializeFhirpathParseItem(buf, item->args.right);
*(int32*)(buf->data + right) = chld;
}
break;
case fpExists:
break;
case fpValues:
break;
case fpKey:
case fpString:
case fpResourceType:
/* elog(INFO, "serialize key: %s [%d]", item->string.val, item->string.len); */
/* write length field*/
appendBinaryStringInfo(buf, (char*)&item->string.len, sizeof(item->string.len));
/* write string content */
appendBinaryStringInfo(buf, item->string.val, item->string.len);
appendStringInfoChar(buf, '\0');
break;
case fpNull:
/* elog(INFO, "null"); */
break;
default:
elog(ERROR, "Serialize error: unknown type: %d", item->type);
}
if (item->next)
*(int32*)(buf->data + next) =serializeFhirpathParseItem(buf, item->next);
return pos;
}
void
alignStringInfoInt(StringInfo buf)
{
switch(INTALIGN(buf->len) - buf->len)
{
case 3:
appendStringInfoCharMacro(buf, 0);
case 2:
appendStringInfoCharMacro(buf, 0);
case 1:
appendStringInfoCharMacro(buf, 0);
default:
break;
}
}
void
fpInit(FhirpathItem *v, Fhirpath *js)
{
fpInitByBuffer(v, VARDATA(js), 0);
}
void
fpInitByBuffer(FhirpathItem *v, char *base, int32 pos)
{
v->base = base;
read_byte(v->type, base, pos);
switch(INTALIGN(pos) - pos)
{
case 3: pos++;
case 2: pos++;
case 1: pos++;
default: break;
}
read_int32(v->nextPos, base, pos);
switch(v->type)
{
case fpNull:
break;
case fpExists:
break;
case fpValues:
break;
case fpKey:
case fpResourceType:
case fpString:
read_int32(v->value.datalen, base, pos);
v->value.data = base + pos;
break;
case fpEqual:
case fpOr:
case fpPipe:
read_int32(v->args.left, base, pos);
read_int32(v->args.right, base, pos);
break;
default:
elog(ERROR, "Init fhirpath: unknown type %d", v->type);
abort();
}
}
void
fpGetArg(FhirpathItem *v, FhirpathItem *a)
{
fpInitByBuffer(a, v->base, v->arg);
}
bool
fpGetNext(FhirpathItem *v, FhirpathItem *a)
{
if (v->nextPos > 0)
{
if (a)
fpInitByBuffer(a, v->base, v->nextPos);
return true;
}
return false;
}
void
fpGetLeftArg(FhirpathItem *v, FhirpathItem *a)
{
fpInitByBuffer(a, v->base, v->args.left);
}
void
fpGetRightArg(FhirpathItem *v, FhirpathItem *a)
{
fpInitByBuffer(a, v->base, v->args.right);
}
bool
fpGetBool(FhirpathItem *v)
{
return (bool)*v->value.data;
}
Numeric
fpGetNumeric(FhirpathItem *v)
{
return (Numeric)v->value.data;
}
int32
fpGetIsType(FhirpathItem *v)
{
return (int32)*v->value.data;
}
char*
fpGetString(FhirpathItem *v, int32 *len)
{
Assert(
v->type == fpKey ||
v->type == fpString
);
if (len)
*len = v->value.datalen;
return v->value.data;
}
void
fpIterateInit(FhirpathItem *v)
{
v->array.current = 0;
}
bool
fpIterateArray(FhirpathItem *v, FhirpathItem *e)
{
if (v->array.current < v->array.nelems)
{
fpInitByBuffer(e, v->base, v->array.arrayPtr[v->array.current]);
v->array.current++;
return true;
}
else
{
return false;
}
}
/* This function passed parser AST create binary string */
/* - pack fhirpath into linear memory (storage representation) */
void
printFhirpathItem(StringInfo buf, FhirpathItem *v, bool inKey)
{
FhirpathItem elem;
check_stack_depth();
switch(v->type)
{
case fpNull:
break;
case fpKey:
/* elog(INFO, "print fpKey %s", fpGetString(v, NULL)); */
appendStringInfoChar(buf, '.');
appendStringInfoString(buf, fpGetString(v, NULL));
break;
case fpResourceType:
/* elog(INFO, "print fpKey %s", fpGetString(v, NULL)); */
appendStringInfoString(buf, fpGetString(v, NULL));
break;
case fpString:
elog(INFO, "print fpString not impl");
/* escape_json(buf, fpGetString(v, NULL)); */
/* appendStringInfoString(buf, fpGetString(v, NULL)); */
break;
case fpPipe:
fpGetLeftArg(v, &elem);
printFhirpathItem(buf, &elem, false);
appendStringInfoString(buf, " | ");
fpGetRightArg(v, &elem);
printFhirpathItem(buf, &elem, false);
break;
case fpExists:
appendStringInfoString(buf, ".exists()");
break;
case fpOr:
fpGetLeftArg(v, &elem);
printFhirpathItem(buf, &elem, false);
appendStringInfoString(buf, " OR ");
fpGetRightArg(v, &elem);
printFhirpathItem(buf, &elem, false);
break;
case fpEqual:
fpGetLeftArg(v, &elem);
appendStringInfoString(buf, ".where(");
appendStringInfoString(buf, fpGetString(&elem, NULL));
appendStringInfoString(buf, "=");
fpGetRightArg(v, &elem);
appendStringInfoString(buf, fpGetString(&elem, NULL));
appendStringInfoString(buf, ")");
break;
case fpValues:
appendStringInfoString(buf, ".vals()");
break;
default:
elog(ERROR, "Print: unknown type: %d", v->type);
}
if (fpGetNext(v, &elem))
printFhirpathItem(buf, &elem, true);
}