-
Notifications
You must be signed in to change notification settings - Fork 49
/
jsquery_support.c
267 lines (232 loc) · 4.61 KB
/
jsquery_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
/*-------------------------------------------------------------------------
*
* jsquery_support.c
* Functions and operations to support jsquery
*
* Copyright (c) 2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2017-2021, Postgres Professional
* Author: Teodor Sigaev <teodor@sigaev.ru>
*
* IDENTIFICATION
* contrib/jsquery/jsquery_support.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "jsquery.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
alignStringInfoInt(StringInfo buf)
{
switch(INTALIGN(buf->len) - buf->len)
{
case 3:
appendStringInfoCharMacro(buf, 0);
/* fall through */
case 2:
appendStringInfoCharMacro(buf, 0);
/* fall through */
case 1:
appendStringInfoCharMacro(buf, 0);
/* fall through */
default:
break;
}
}
void
jsqInit(JsQueryItem *v, JsQuery *js)
{
jsqInitByBuffer(v, VARDATA(js), 0);
}
void
jsqInitByBuffer(JsQueryItem *v, char *base, int32 pos)
{
v->base = base;
read_byte(v->type, base, pos);
v->hint = v->type & JSQ_HINT_MASK;
v->type &= ~JSQ_HINT_MASK;
switch(INTALIGN(pos) - pos)
{
case 3: pos++; /* fall through */
case 2: pos++; /* fall through */
case 1: pos++; /* fall through */
default: break;
}
read_int32(v->nextPos, base, pos);
switch(v->type)
{
case jqiNull:
case jqiCurrent:
case jqiLength:
case jqiAny:
case jqiAnyArray:
case jqiAnyKey:
case jqiAll:
case jqiAllArray:
case jqiAllKey:
break;
case jqiIndexArray:
read_int32(v->arrayIndex, base, pos);
break;
case jqiKey:
case jqiString:
read_int32(v->value.datalen, base, pos);
/* fall through */
/* follow next */
case jqiNumeric:
case jqiBool:
case jqiIs:
v->value.data = base + pos;
break;
case jqiArray:
read_int32(v->array.nelems, base, pos);
v->array.current = 0;
v->array.arrayPtr = (int32*)(base + pos);
break;
case jqiAnd:
case jqiOr:
read_int32(v->args.left, base, pos);
read_int32(v->args.right, base, pos);
break;
case jqiEqual:
case jqiLess:
case jqiGreater:
case jqiLessOrEqual:
case jqiGreaterOrEqual:
case jqiContains:
case jqiContained:
case jqiOverlap:
case jqiIn:
case jqiNot:
case jqiFilter:
read_int32(v->arg, base, pos);
break;
default:
abort();
elog(ERROR, "Unknown type: %d", v->type);
}
}
void
jsqGetArg(JsQueryItem *v, JsQueryItem *a)
{
Assert(
v->type == jqiEqual ||
v->type == jqiLess ||
v->type == jqiGreater ||
v->type == jqiLessOrEqual ||
v->type == jqiGreaterOrEqual ||
v->type == jqiContains ||
v->type == jqiContained ||
v->type == jqiOverlap ||
v->type == jqiFilter ||
v->type == jqiIn ||
v->type == jqiNot
);
jsqInitByBuffer(a, v->base, v->arg);
}
bool
jsqGetNext(JsQueryItem *v, JsQueryItem *a)
{
if (v->nextPos > 0)
{
Assert(
v->type == jqiKey ||
v->type == jqiAny ||
v->type == jqiIndexArray ||
v->type == jqiAnyArray ||
v->type == jqiAnyKey ||
v->type == jqiAll ||
v->type == jqiAllArray ||
v->type == jqiAllKey ||
v->type == jqiCurrent ||
v->type == jqiFilter ||
v->type == jqiLength
);
if (a)
jsqInitByBuffer(a, v->base, v->nextPos);
return true;
}
return false;
}
void
jsqGetLeftArg(JsQueryItem *v, JsQueryItem *a)
{
Assert(
v->type == jqiAnd ||
v->type == jqiOr
);
jsqInitByBuffer(a, v->base, v->args.left);
}
void
jsqGetRightArg(JsQueryItem *v, JsQueryItem *a)
{
Assert(
v->type == jqiAnd ||
v->type == jqiOr
);
jsqInitByBuffer(a, v->base, v->args.right);
}
bool
jsqGetBool(JsQueryItem *v)
{
Assert(v->type == jqiBool);
return (bool)*v->value.data;
}
Numeric
jsqGetNumeric(JsQueryItem *v)
{
Assert(v->type == jqiNumeric);
return (Numeric)v->value.data;
}
int32
jsqGetIsType(JsQueryItem *v)
{
Assert(v->type == jqiIs);
return (int32)*v->value.data;
}
char*
jsqGetString(JsQueryItem *v, int32 *len)
{
Assert(
v->type == jqiKey ||
v->type == jqiString
);
if (len)
*len = v->value.datalen;
return v->value.data;
}
void
jsqIterateInit(JsQueryItem *v)
{
Assert(v->type == jqiArray);
v->array.current = 0;
}
bool
jsqIterateArray(JsQueryItem *v, JsQueryItem *e)
{
Assert(v->type == jqiArray);
if (v->array.current < v->array.nelems)
{
jsqInitByBuffer(e, v->base, v->array.arrayPtr[v->array.current]);
v->array.current++;
return true;
}
else
{
return false;
}
}
void
jsqIterateDestroy(JsQueryItem *v)
{
Assert(v->type == jqiArray);
Assert(v->array.current <= v->array.nelems);
v->array.current++;
}