TSQuery QTN2QT(QTNode *in) { TSQuery out; int len; int sumlen = 0, nnode = 0; QTN2QTState state; cntsize(in, &sumlen, &nnode); if (TSQUERY_TOO_BIG(nnode, sumlen)) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("tsquery is too large"))); len = COMPUTESIZE(nnode, sumlen); out = (TSQuery) palloc0(len); SET_VARSIZE(out, len); out->size = nnode; state.curitem = GETQUERY(out); state.operand = state.curoperand = GETOPERAND(out); fillQT(&state, in); return out; }
/* * Count the total length of operand string in tree, including '\0'- * terminators. */ static void cntsize(QTNode *in, int *sumlen, int *nnode) { /* since this function recurses, it could be driven to stack overflow. */ check_stack_depth(); *nnode += 1; if (in->valnode->type == QI_OPR) { int i; for (i = 0; i < in->nchild; i++) cntsize(in->child[i], sumlen, nnode); } else { *sumlen += in->valnode->qoperand.length + 1; } }
TSQuery QTN2QT(QTNode *in) { TSQuery out; int len; int sumlen = 0, nnode = 0; QTN2QTState state; cntsize(in, &sumlen, &nnode); len = COMPUTESIZE(nnode, sumlen); out = (TSQuery) palloc0(len); SET_VARSIZE(out, len); out->size = nnode; state.curitem = GETQUERY(out); state.operand = state.curoperand = GETOPERAND(out); fillQT(&state, in); return out; }