Skip to content

Commit 4aee6a6

Browse files
committed
continue refactoring
1 parent ff4809f commit 4aee6a6

5 files changed

+90
-67
lines changed

jsquery.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ struct JsQueryItem {
8080
JsQueryItem **elems;
8181
} array;
8282
};
83-
8483
};
8584

8685
typedef struct JsQueryItemR {
@@ -109,7 +108,8 @@ typedef struct JsQueryItemR {
109108

110109
} JsQueryItemR;
111110

112-
extern void jsqInit(JsQueryItemR *v, char *base, int32 pos);
111+
extern void jsqInit(JsQueryItemR *v, JsQuery *js);
112+
extern void jsqInitByBuffer(JsQueryItemR *v, char *base, int32 pos);
113113
extern bool jsqGetNext(JsQueryItemR *v, JsQueryItemR *a);
114114
extern void jsqGetArg(JsQueryItemR *v, JsQueryItemR *a);
115115
extern void jsqGetLeftArg(JsQueryItemR *v, JsQueryItemR *a);

jsquery_constr.c

+54-40
Original file line numberDiff line numberDiff line change
@@ -20,77 +20,84 @@
2020
#include "jsquery.h"
2121

2222
static int32
23-
copyJsQuery(StringInfo buf, char *jqBase, int32 jqPos)
23+
copyJsQuery(StringInfo buf, JsQueryItemR *jsq)
2424
{
25-
int32 resPos = buf->len - VARHDRSZ; /* position from begining of jsquery data */
26-
JsQueryItemType type;
27-
int32 nextPos, chld, next;
25+
JsQueryItemR elem;
26+
int32 next, chld;
27+
int32 resPos = buf->len - VARHDRSZ; /* position from begining of jsquery data */
2828

2929
check_stack_depth();
3030

31-
jqPos = readJsQueryHeader(jqBase, jqPos, &type, &nextPos);
32-
33-
appendStringInfoChar(buf, (char)type);
31+
appendStringInfoChar(buf, (char)jsq->type);
3432
alignStringInfoInt(buf);
3533

36-
next = (nextPos > 0) ? buf->len : 0;;
34+
next = (jsqGetNext(jsq, NULL)) ? buf->len : 0;
3735
appendBinaryStringInfo(buf, (char*)&next /* fake value */, sizeof(next));
3836

39-
switch(type)
37+
switch(jsq->type)
4038
{
4139
case jqiKey:
4240
case jqiString:
4341
{
44-
int32 len;
42+
int32 len;
43+
char *s;
4544

46-
read_int32(len, jqBase, jqPos);
45+
s = jsqGetString(jsq, &len);
4746
appendBinaryStringInfo(buf, (char*)&len, sizeof(len));
48-
appendBinaryStringInfo(buf, jqBase + jqPos, len + 1 /* \0 */);
47+
appendBinaryStringInfo(buf, s, len + 1 /* \0 */);
4948
}
5049
break;
5150
case jqiNumeric:
52-
appendBinaryStringInfo(buf, jqBase + jqPos, VARSIZE(jqBase + jqPos));
51+
{
52+
Numeric n = jsqGetNumeric(jsq);
53+
54+
appendBinaryStringInfo(buf, (char*)n, VARSIZE_ANY(n));
55+
}
5356
break;
5457
case jqiBool:
55-
appendBinaryStringInfo(buf, jqBase + jqPos, 1);
58+
{
59+
bool v = jsqGetBool(jsq);
60+
61+
appendBinaryStringInfo(buf, (char*)&v, 1);
62+
}
5663
break;
5764
case jqiArray:
5865
{
59-
int32 i, nelems, arrayStart, *arrayPosIn;
66+
int32 i, arrayStart;
6067

61-
read_int32(nelems, jqBase, jqPos);
62-
appendBinaryStringInfo(buf, (char*)&nelems /* fake value */, sizeof(nelems));
68+
appendBinaryStringInfo(buf, (char*)&jsq->array.nelems,
69+
sizeof(jsq->array.nelems));
6370

6471
arrayStart = buf->len;
65-
arrayPosIn = (int32*)(jqBase + jqPos);
6672

6773
/* reserve place for "pointers" to array's elements */
68-
for(i=0; i<nelems; i++)
74+
for(i=0; i<jsq->array.nelems; i++)
6975
appendBinaryStringInfo(buf, (char*)&i /* fake value */, sizeof(i));
7076

71-
for(i=0; i<nelems; i++)
77+
while(jsqIterateArray(jsq, &elem))
7278
{
73-
chld = copyJsQuery(buf, jqBase, arrayPosIn[i]);
79+
chld = copyJsQuery(buf, &elem);
7480
*(int32*)(buf->data + arrayStart + i * sizeof(i)) = chld;
81+
i++;
7582
}
7683
}
7784
break;
7885
case jqiAnd:
7986
case jqiOr:
8087
{
81-
int32 leftIn, rightIn, leftOut, rightOut;
88+
int32 leftOut, rightOut;
8289

8390
leftOut = buf->len;
8491
appendBinaryStringInfo(buf, (char*)&leftOut /* fake value */, sizeof(leftOut));
8592
rightOut = buf->len;
8693
appendBinaryStringInfo(buf, (char*)&rightOut /* fake value */, sizeof(rightOut));
8794

88-
read_int32(leftIn, jqBase, jqPos);
89-
chld = copyJsQuery(buf, jqBase, leftIn);
95+
jsqGetLeftArg(jsq, &elem);
96+
chld = copyJsQuery(buf, &elem);
9097
*(int32*)(buf->data + leftOut) = chld;
9198

92-
read_int32(rightIn, jqBase, jqPos);
93-
chld = copyJsQuery(buf, jqBase, rightIn);
99+
jsqGetRightArg(jsq, &elem);
100+
chld = copyJsQuery(buf, &elem);
94101
*(int32*)(buf->data + rightOut) = chld;
95102
}
96103
break;
@@ -105,13 +112,12 @@ copyJsQuery(StringInfo buf, char *jqBase, int32 jqPos)
105112
case jqiOverlap:
106113
case jqiNot:
107114
{
108-
int32 argIn, argOut;
115+
int32 argOut = buf->len;
109116

110-
argOut = buf->len;
111117
appendBinaryStringInfo(buf, (char*)&argOut /* fake value */, sizeof(argOut));
112118

113-
read_int32(argIn, jqBase, jqPos);
114-
chld = copyJsQuery(buf, jqBase, argIn);
119+
jsqGetArg(jsq, &elem);
120+
chld = copyJsQuery(buf, &elem);
115121
*(int32*)(buf->data + argOut) = chld;
116122
}
117123
break;
@@ -122,11 +128,11 @@ copyJsQuery(StringInfo buf, char *jqBase, int32 jqPos)
122128
case jqiAnyKey:
123129
break;
124130
default:
125-
elog(ERROR, "Unknown JsQueryItem type: %d", type);
131+
elog(ERROR, "Unknown JsQueryItem type: %d", jsq->type);
126132
}
127133

128-
if (nextPos)
129-
*(int32*)(buf->data + next) = copyJsQuery(buf, jqBase, nextPos);
134+
if (jsqGetNext(jsq, &elem))
135+
*(int32*)(buf->data + next) = copyJsQuery(buf, &elem);
130136

131137
return resPos;
132138
}
@@ -137,27 +143,32 @@ joinJsQuery(JsQueryItemType type, JsQuery *jq1, JsQuery *jq2)
137143
JsQuery *out;
138144
StringInfoData buf;
139145
int32 left, right, chld;
146+
JsQueryItemR v;
140147

141148
initStringInfo(&buf);
142149
enlargeStringInfo(&buf, VARSIZE_ANY(jq1) + VARSIZE_ANY(jq2) + 4 * sizeof(int32) + VARHDRSZ);
143150

144151
appendStringInfoSpaces(&buf, VARHDRSZ);
145152

153+
/* form jqiAnd/jqiOr header */
146154
appendStringInfoChar(&buf, (char)type);
147155
alignStringInfoInt(&buf);
148156

149-
/* next */
150-
chld = 0;
157+
/* nextPos field of header*/
158+
chld = 0; /* actual value, not a fake */
151159
appendBinaryStringInfo(&buf, (char*)&chld, sizeof(chld));
152160

153161
left = buf.len;
154162
appendBinaryStringInfo(&buf, (char*)&left /* fake value */, sizeof(left));
155163
right = buf.len;
156164
appendBinaryStringInfo(&buf, (char*)&right /* fake value */, sizeof(right));
157165

158-
chld = copyJsQuery(&buf, VARDATA(jq1), 0);
166+
/* dump left and right subtree */
167+
jsqInit(&v, jq1);
168+
chld = copyJsQuery(&buf, &v);
159169
*(int32*)(buf.data + left) = chld;
160-
chld = copyJsQuery(&buf, VARDATA(jq2), 0);
170+
jsqInit(&v, jq2);
171+
chld = copyJsQuery(&buf, &v);
161172
*(int32*)(buf.data + right) = chld;
162173

163174
out = (JsQuery*)buf.data;
@@ -206,23 +217,26 @@ jsquery_not(PG_FUNCTION_ARGS)
206217
JsQuery *out;
207218
StringInfoData buf;
208219
int32 arg, chld;
220+
JsQueryItemR v;
209221

210222
initStringInfo(&buf);
211223
enlargeStringInfo(&buf, VARSIZE_ANY(jq) + 4 * sizeof(int32) + VARHDRSZ);
212224

213225
appendStringInfoSpaces(&buf, VARHDRSZ);
214226

227+
/* form jsquery header */
215228
appendStringInfoChar(&buf, (char)jqiNot);
216229
alignStringInfoInt(&buf);
217230

218-
/* next */
219-
chld = 0;
231+
/* nextPos field of header*/
232+
chld = 0; /* actual value, not a fake */
220233
appendBinaryStringInfo(&buf, (char*)&chld, sizeof(chld));
221234

222235
arg = buf.len;
223236
appendBinaryStringInfo(&buf, (char*)&arg /* fake value */, sizeof(arg));
224237

225-
chld = copyJsQuery(&buf, VARDATA(jq), 0);
238+
jsqInit(&v, jq);
239+
chld = copyJsQuery(&buf, &v);
226240
*(int32*)(buf.data + arg) = chld;
227241

228242
out = (JsQuery*)buf.data;

jsquery_io.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ printJsQueryItem(StringInfo buf, JsQueryItemR *v, bool inKey, bool printBrackete
228228
case jqiKey:
229229
if (inKey)
230230
appendStringInfoChar(buf, '.');
231+
/* follow next */
231232
case jqiString:
232233
escape_json(buf, jsqGetString(v, NULL));
233234
break;
@@ -331,7 +332,7 @@ jsquery_out(PG_FUNCTION_ARGS)
331332
initStringInfo(&buf);
332333
enlargeStringInfo(&buf, VARSIZE(in) /* estimation */);
333334

334-
jsqInit(&v, VARDATA(in), 0);
335+
jsqInit(&v, in);
335336
printJsQueryItem(&buf, &v, false, true);
336337

337338
PG_RETURN_CSTRING(buf.data);

jsquery_op.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ executeExpr(JsQueryItemR *jsq, int32 op, JsonbValue *jb)
273273
/*
274274
* read arg type
275275
*/
276-
Assert(jsq->nextPos == 0);
276+
Assert(jsqGetNext(jsq, NULL) == false);
277277
Assert(jsq->type == jqiAny || jsq->type == jqiString || jsq->type == jqiNumeric ||
278278
jsq->type == jqiNull || jsq->type == jqiBool || jsq->type == jqiArray);
279279

@@ -448,7 +448,7 @@ jsquery_json_exec(PG_FUNCTION_ARGS)
448448
jbv.val.binary.data = &jb->root;
449449
jbv.val.binary.len = VARSIZE_ANY_EXHDR(jb);
450450

451-
jsqInit(&jsq, VARDATA(jq), 0);
451+
jsqInit(&jsq, jq);
452452

453453
res = recursiveExecute(&jsq, &jbv);
454454

@@ -472,7 +472,7 @@ json_jsquery_exec(PG_FUNCTION_ARGS)
472472
jbv.val.binary.data = &jb->root;
473473
jbv.val.binary.len = VARSIZE_ANY_EXHDR(jb);
474474

475-
jsqInit(&jsq, VARDATA(jq), 0);
475+
jsqInit(&jsq, jq);
476476

477477
res = recursiveExecute(&jsq, &jbv);
478478

@@ -591,8 +591,8 @@ jsquery_cmp(PG_FUNCTION_ARGS)
591591
int32 res;
592592
JsQueryItemR v1, v2;
593593

594-
jsqInit(&v1, VARDATA(jq1), 0);
595-
jsqInit(&v2, VARDATA(jq2), 0);
594+
jsqInit(&v1, jq1);
595+
jsqInit(&v2, jq2);
596596

597597
res = compareJsQuery(&v1, &v2);
598598

@@ -611,8 +611,8 @@ jsquery_lt(PG_FUNCTION_ARGS)
611611
int32 res;
612612
JsQueryItemR v1, v2;
613613

614-
jsqInit(&v1, VARDATA(jq1), 0);
615-
jsqInit(&v2, VARDATA(jq2), 0);
614+
jsqInit(&v1, jq1);
615+
jsqInit(&v2, jq2);
616616

617617
res = compareJsQuery(&v1, &v2);
618618

@@ -631,8 +631,8 @@ jsquery_le(PG_FUNCTION_ARGS)
631631
int32 res;
632632
JsQueryItemR v1, v2;
633633

634-
jsqInit(&v1, VARDATA(jq1), 0);
635-
jsqInit(&v2, VARDATA(jq2), 0);
634+
jsqInit(&v1, jq1);
635+
jsqInit(&v2, jq2);
636636

637637
res = compareJsQuery(&v1, &v2);
638638

@@ -651,8 +651,8 @@ jsquery_eq(PG_FUNCTION_ARGS)
651651
int32 res;
652652
JsQueryItemR v1, v2;
653653

654-
jsqInit(&v1, VARDATA(jq1), 0);
655-
jsqInit(&v2, VARDATA(jq2), 0);
654+
jsqInit(&v1, jq1);
655+
jsqInit(&v2, jq2);
656656

657657
res = compareJsQuery(&v1, &v2);
658658

@@ -671,8 +671,8 @@ jsquery_ne(PG_FUNCTION_ARGS)
671671
int32 res;
672672
JsQueryItemR v1, v2;
673673

674-
jsqInit(&v1, VARDATA(jq1), 0);
675-
jsqInit(&v2, VARDATA(jq2), 0);
674+
jsqInit(&v1, jq1);
675+
jsqInit(&v2, jq2);
676676

677677
res = compareJsQuery(&v1, &v2);
678678

@@ -691,8 +691,8 @@ jsquery_ge(PG_FUNCTION_ARGS)
691691
int32 res;
692692
JsQueryItemR v1, v2;
693693

694-
jsqInit(&v1, VARDATA(jq1), 0);
695-
jsqInit(&v2, VARDATA(jq2), 0);
694+
jsqInit(&v1, jq1);
695+
jsqInit(&v2, jq2);
696696

697697
res = compareJsQuery(&v1, &v2);
698698

@@ -711,8 +711,8 @@ jsquery_gt(PG_FUNCTION_ARGS)
711711
int32 res;
712712
JsQueryItemR v1, v2;
713713

714-
jsqInit(&v1, VARDATA(jq1), 0);
715-
jsqInit(&v2, VARDATA(jq2), 0);
714+
jsqInit(&v1, jq1);
715+
jsqInit(&v2, jq2);
716716

717717
res = compareJsQuery(&v1, &v2);
718718

@@ -805,7 +805,7 @@ jsquery_hash(PG_FUNCTION_ARGS)
805805
pg_crc32 res;
806806

807807
INIT_CRC32(res);
808-
jsqInit(&v, VARDATA(jq), 0);
808+
jsqInit(&v, jq);
809809
hashJsQuery(&v, &res);
810810
FIN_CRC32(res);
811811

0 commit comments

Comments
 (0)