OSDN Git Service

Standard pgindent run for 8.1.
[pg-rex/syncrep.git] / contrib / tsearch2 / tsvector.c
index ea50421..cfed6e4 100644 (file)
@@ -50,9 +50,9 @@ Datum         tsvector_length(PG_FUNCTION_ARGS);
 static int
 comparePos(const void *a, const void *b)
 {
-       if (((WordEntryPos *) a)->pos == ((WordEntryPos *) b)->pos)
-               return 1;
-       return (((WordEntryPos *) a)->pos > ((WordEntryPos *) b)->pos) ? 1 : -1;
+       if (WEP_GETPOS(*(WordEntryPos *) a) == WEP_GETPOS(*(WordEntryPos *) b))
+               return 0;
+       return (WEP_GETPOS(*(WordEntryPos *) a) > WEP_GETPOS(*(WordEntryPos *) b)) ? 1 : -1;
 }
 
 static int
@@ -70,16 +70,15 @@ uniquePos(WordEntryPos * a, int4 l)
        ptr = a + 1;
        while (ptr - a < l)
        {
-               if (ptr->pos != res->pos)
+               if (WEP_GETPOS(*ptr) != WEP_GETPOS(*res))
                {
                        res++;
-                       res->pos = ptr->pos;
-                       res->weight = ptr->weight;
-                       if (res - a >= MAXNUMPOS - 1 || res->pos == MAXENTRYPOS - 1)
+                       *res = *ptr;
+                       if (res - a >= MAXNUMPOS - 1 || WEP_GETPOS(*res) == MAXENTRYPOS - 1)
                                break;
                }
-               else if (ptr->weight > res->weight)
-                       res->weight = ptr->weight;
+               else if (WEP_GETWEIGHT(*ptr) > WEP_GETWEIGHT(*res))
+                       WEP_SETWEIGHT(*res, WEP_GETWEIGHT(*ptr));
                ptr++;
        }
        return res + 1 - a;
@@ -310,7 +309,7 @@ gettoken_tsvector(TI_IN_STATE * state)
                }
                else if (state->state == INPOSINFO)
                {
-                       if (isdigit(*(state->prsbuf)))
+                       if (isdigit((unsigned char) *(state->prsbuf)))
                        {
                                if (state->alen == 0)
                                {
@@ -324,12 +323,12 @@ gettoken_tsvector(TI_IN_STATE * state)
                                        state->pos = (WordEntryPos *) repalloc(state->pos, sizeof(WordEntryPos) * state->alen);
                                }
                                (*(uint16 *) (state->pos))++;
-                               state->pos[*(uint16 *) (state->pos)].pos = LIMITPOS(atoi(state->prsbuf));
-                               if (state->pos[*(uint16 *) (state->pos)].pos == 0)
+                               WEP_SETPOS(state->pos[*(uint16 *) (state->pos)], LIMITPOS(atoi(state->prsbuf)));
+                               if (WEP_GETPOS(state->pos[*(uint16 *) (state->pos)]) == 0)
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
                                                         errmsg("wrong position info")));
-                               state->pos[*(uint16 *) (state->pos)].weight = 0;
+                               WEP_SETWEIGHT(state->pos[*(uint16 *) (state->pos)], 0);
                                state->state = WAITPOSDELIM;
                        }
                        else
@@ -343,39 +342,40 @@ gettoken_tsvector(TI_IN_STATE * state)
                                state->state = INPOSINFO;
                        else if (tolower(*(state->prsbuf)) == 'a' || *(state->prsbuf) == '*')
                        {
-                               if (state->pos[*(uint16 *) (state->pos)].weight)
+                               if (WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]))
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
                                                         errmsg("syntax error")));
-                               state->pos[*(uint16 *) (state->pos)].weight = 3;
+                               WEP_SETWEIGHT(state->pos[*(uint16 *) (state->pos)], 3);
                        }
                        else if (tolower(*(state->prsbuf)) == 'b')
                        {
-                               if (state->pos[*(uint16 *) (state->pos)].weight)
+                               if (WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]))
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
                                                         errmsg("syntax error")));
-                               state->pos[*(uint16 *) (state->pos)].weight = 2;
+                               WEP_SETWEIGHT(state->pos[*(uint16 *) (state->pos)], 2);
                        }
                        else if (tolower(*(state->prsbuf)) == 'c')
                        {
-                               if (state->pos[*(uint16 *) (state->pos)].weight)
+                               if (WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]))
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
                                                         errmsg("syntax error")));
-                               state->pos[*(uint16 *) (state->pos)].weight = 1;
+                               WEP_SETWEIGHT(state->pos[*(uint16 *) (state->pos)], 1);
                        }
                        else if (tolower(*(state->prsbuf)) == 'd')
                        {
-                               if (state->pos[*(uint16 *) (state->pos)].weight)
+                               if (WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]))
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
                                                         errmsg("syntax error")));
-                               state->pos[*(uint16 *) (state->pos)].weight = 0;
+                               WEP_SETWEIGHT(state->pos[*(uint16 *) (state->pos)], 0);
                        }
-                       else if (isspace(*(state->prsbuf)) || *(state->prsbuf) == '\0')
+                       else if (isspace((unsigned char) *(state->prsbuf)) ||
+                                        *(state->prsbuf) == '\0')
                                return 1;
-                       else if (!isdigit(*(state->prsbuf)))
+                       else if (!isdigit((unsigned char) *(state->prsbuf)))
                                ereport(ERROR,
                                                (errcode(ERRCODE_SYNTAX_ERROR),
                                                 errmsg("syntax error")));
@@ -404,6 +404,7 @@ tsvector_in(PG_FUNCTION_ARGS)
        int4            i,
                                buflen = 256;
 
+       SET_FUNCOID();
        state.prsbuf = buf;
        state.len = 32;
        state.word = (char *) palloc(state.len);
@@ -451,6 +452,8 @@ tsvector_in(PG_FUNCTION_ARGS)
 
        if (len > 0)
                len = uniqueentry(arr, len, tmpbuf, &buflen);
+       else
+               buflen = 0;
        totallen = CALCDATASIZE(len, buflen);
        in = (tsvector *) palloc(totallen);
        memset(in, 0, totallen);
@@ -536,9 +539,9 @@ tsvector_out(PG_FUNCTION_ARGS)
                        wptr = POSDATAPTR(out, ptr);
                        while (pp)
                        {
-                               sprintf(curout, "%d", wptr->pos);
+                               sprintf(curout, "%d", WEP_GETPOS(*wptr));
                                curout = strchr(curout, '\0');
-                               switch (wptr->weight)
+                               switch (WEP_GETWEIGHT(*wptr))
                                {
                                        case 3:
                                                *curout++ = 'A';
@@ -570,24 +573,24 @@ tsvector_out(PG_FUNCTION_ARGS)
 static int
 compareWORD(const void *a, const void *b)
 {
-       if (((WORD *) a)->len == ((WORD *) b)->len)
+       if (((TSWORD *) a)->len == ((TSWORD *) b)->len)
        {
                int                     res = strncmp(
-                                                                 ((WORD *) a)->word,
-                                                                 ((WORD *) b)->word,
-                                                                 ((WORD *) b)->len);
+                                                                 ((TSWORD *) a)->word,
+                                                                 ((TSWORD *) b)->word,
+                                                                 ((TSWORD *) b)->len);
 
                if (res == 0)
-                       return (((WORD *) a)->pos.pos > ((WORD *) b)->pos.pos) ? 1 : -1;
+                       return (((TSWORD *) a)->pos.pos > ((TSWORD *) b)->pos.pos) ? 1 : -1;
                return res;
        }
-       return (((WORD *) a)->len > ((WORD *) b)->len) ? 1 : -1;
+       return (((TSWORD *) a)->len > ((TSWORD *) b)->len) ? 1 : -1;
 }
 
 static int
-uniqueWORD(WORD * a, int4 l)
+uniqueWORD(TSWORD * a, int4 l)
 {
-       WORD       *ptr,
+       TSWORD     *ptr,
                           *res;
        int                     tmppos;
 
@@ -604,7 +607,7 @@ uniqueWORD(WORD * a, int4 l)
        res = a;
        ptr = a + 1;
 
-       qsort((void *) a, l, sizeof(WORD), compareWORD);
+       qsort((void *) a, l, sizeof(TSWORD), compareWORD);
        tmppos = LIMITPOS(a->pos.pos);
        a->alen = 2;
        a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen);
@@ -635,7 +638,8 @@ uniqueWORD(WORD * a, int4 l)
                                        res->alen *= 2;
                                        res->pos.apos = (uint16 *) repalloc(res->pos.apos, sizeof(uint16) * res->alen);
                                }
-                               if ( res->pos.apos[0]==0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos) ) { 
+                               if (res->pos.apos[0] == 0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
+                               {
                                        res->pos.apos[res->pos.apos[0] + 1] = LIMITPOS(ptr->pos.pos);
                                        res->pos.apos[0]++;
                                }
@@ -699,8 +703,8 @@ makevalue(PRSTEXT * prs)
                        wptr = POSDATAPTR(in, ptr);
                        for (j = 0; j < *(uint16 *) cur; j++)
                        {
-                               wptr[j].weight = 0;
-                               wptr[j].pos = prs->words[i].pos.apos[j + 1];
+                               WEP_SETWEIGHT(wptr[j], 0);
+                               WEP_SETPOS(wptr[j], prs->words[i].pos.apos[j + 1]);
                        }
                        cur += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos);
                        pfree(prs->words[i].pos.apos);
@@ -720,12 +724,15 @@ to_tsvector(PG_FUNCTION_ARGS)
        text       *in = PG_GETARG_TEXT_P(1);
        PRSTEXT         prs;
        tsvector   *out = NULL;
-       TSCfgInfo  *cfg = findcfg(PG_GETARG_INT32(0));
+       TSCfgInfo  *cfg;
+
+       SET_FUNCOID();
+       cfg = findcfg(PG_GETARG_INT32(0));
 
        prs.lenwords = 32;
        prs.curwords = 0;
        prs.pos = 0;
-       prs.words = (WORD *) palloc(sizeof(WORD) * prs.lenwords);
+       prs.words = (TSWORD *) palloc(sizeof(TSWORD) * prs.lenwords);
 
        parsetext_v2(cfg, &prs, VARDATA(in), VARSIZE(in) - VARHDRSZ);
        PG_FREE_IF_COPY(in, 1);
@@ -746,12 +753,15 @@ Datum
 to_tsvector_name(PG_FUNCTION_ARGS)
 {
        text       *cfg = PG_GETARG_TEXT_P(0);
-       Datum           res = DirectFunctionCall3(
-                                                                                 to_tsvector,
-                                                                                 Int32GetDatum(name2id_cfg(cfg)),
-                                                                                 PG_GETARG_DATUM(1),
-                                                                                 (Datum) 0
-       );
+       Datum           res;
+
+       SET_FUNCOID();
+       res = DirectFunctionCall3(
+                                                         to_tsvector,
+                                                         Int32GetDatum(name2id_cfg(cfg)),
+                                                         PG_GETARG_DATUM(1),
+                                                         (Datum) 0
+               );
 
        PG_FREE_IF_COPY(cfg, 0);
        PG_RETURN_DATUM(res);
@@ -760,12 +770,15 @@ to_tsvector_name(PG_FUNCTION_ARGS)
 Datum
 to_tsvector_current(PG_FUNCTION_ARGS)
 {
-       Datum           res = DirectFunctionCall3(
-                                                                                 to_tsvector,
-                                                                                 Int32GetDatum(get_currcfg()),
-                                                                                 PG_GETARG_DATUM(0),
-                                                                                 (Datum) 0
-       );
+       Datum           res;
+
+       SET_FUNCOID();
+       res = DirectFunctionCall3(
+                                                         to_tsvector,
+                                                         Int32GetDatum(get_currcfg()),
+                                                         PG_GETARG_DATUM(0),
+                                                         (Datum) 0
+               );
 
        PG_RETURN_DATUM(res);
 }
@@ -776,10 +789,10 @@ findFunc(char *fname)
        FuncCandidateList clist,
                                ptr;
        Oid                     funcid = InvalidOid;
-       List       *names = makeList1(makeString(fname));
+       List       *names = list_make1(makeString(fname));
 
        ptr = clist = FuncnameGetCandidates(names, 1);
-       freeList(names);
+       list_free(names);
 
        if (!ptr)
                return funcid;
@@ -806,12 +819,15 @@ tsearch2(PG_FUNCTION_ARGS)
        Trigger    *trigger;
        Relation        rel;
        HeapTuple       rettuple = NULL;
-       TSCfgInfo  *cfg = findcfg(get_currcfg());
        int                     numidxattr,
                                i;
        PRSTEXT         prs;
        Datum           datum = (Datum) 0;
        Oid                     funcoid = InvalidOid;
+       TSCfgInfo  *cfg;
+
+       SET_FUNCOID();
+       cfg = findcfg(get_currcfg());
 
        if (!CALLED_AS_TRIGGER(fcinfo))
                /* internal error */
@@ -850,7 +866,7 @@ tsearch2(PG_FUNCTION_ARGS)
        prs.lenwords = 32;
        prs.curwords = 0;
        prs.pos = 0;
-       prs.words = (WORD *) palloc(sizeof(WORD) * prs.lenwords);
+       prs.words = (TSWORD *) palloc(sizeof(TSWORD) * prs.lenwords);
 
        /* find all words in indexable column */
        for (i = 1; i < trigger->tgnargs; i++)
@@ -890,9 +906,9 @@ tsearch2(PG_FUNCTION_ARGS)
                if (funcoid != InvalidOid)
                {
                        text       *txttmp = (text *) DatumGetPointer(OidFunctionCall1(
-                                                                                                                                funcoid,
-                                                                                        PointerGetDatum(txt_toasted)
-                                                                                                                                         ));
+                                                                                                                                        funcoid,
+                                                                                                PointerGetDatum(txt_toasted)
+                                                                                                                                                  ));
 
                        txt = (text *) DatumGetPointer(PG_DETOAST_DATUM(PointerGetDatum(txttmp)));
                        if (txt == txttmp)
@@ -932,3 +948,101 @@ tsearch2(PG_FUNCTION_ARGS)
 
        return PointerGetDatum(rettuple);
 }
+
+static int
+silly_cmp_tsvector(const tsvector * a, const tsvector * b)
+{
+       if (a->len < b->len)
+               return -1;
+       else if (a->len > b->len)
+               return 1;
+       else if (a->size < b->size)
+               return -1;
+       else if (a->size > b->size)
+               return 1;
+       else
+       {
+               unsigned char *aptr = (unsigned char *) (a->data) + DATAHDRSIZE;
+               unsigned char *bptr = (unsigned char *) (b->data) + DATAHDRSIZE;
+
+               while (aptr - ((unsigned char *) (a->data)) < a->len)
+               {
+                       if (*aptr != *bptr)
+                               return (*aptr < *bptr) ? -1 : 1;
+                       aptr++;
+                       bptr++;
+               }
+       }
+       return 0;
+}
+
+PG_FUNCTION_INFO_V1(tsvector_cmp);
+PG_FUNCTION_INFO_V1(tsvector_lt);
+PG_FUNCTION_INFO_V1(tsvector_le);
+PG_FUNCTION_INFO_V1(tsvector_eq);
+PG_FUNCTION_INFO_V1(tsvector_ne);
+PG_FUNCTION_INFO_V1(tsvector_ge);
+PG_FUNCTION_INFO_V1(tsvector_gt);
+Datum          tsvector_cmp(PG_FUNCTION_ARGS);
+Datum          tsvector_lt(PG_FUNCTION_ARGS);
+Datum          tsvector_le(PG_FUNCTION_ARGS);
+Datum          tsvector_eq(PG_FUNCTION_ARGS);
+Datum          tsvector_ne(PG_FUNCTION_ARGS);
+Datum          tsvector_ge(PG_FUNCTION_ARGS);
+Datum          tsvector_gt(PG_FUNCTION_ARGS);
+
+#define RUNCMP                                                                         \
+tsvector *a               = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));\
+tsvector *b               = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));\
+int res = silly_cmp_tsvector(a,b);                                                     \
+PG_FREE_IF_COPY(a,0);                                                                  \
+PG_FREE_IF_COPY(b,1);                                                                  \
+
+Datum
+tsvector_cmp(PG_FUNCTION_ARGS)
+{
+       RUNCMP
+               PG_RETURN_INT32(res);
+}
+
+Datum
+tsvector_lt(PG_FUNCTION_ARGS)
+{
+       RUNCMP
+               PG_RETURN_BOOL((res < 0) ? true : false);
+}
+
+Datum
+tsvector_le(PG_FUNCTION_ARGS)
+{
+       RUNCMP
+               PG_RETURN_BOOL((res <= 0) ? true : false);
+}
+
+Datum
+tsvector_eq(PG_FUNCTION_ARGS)
+{
+       RUNCMP
+               PG_RETURN_BOOL((res == 0) ? true : false);
+}
+
+Datum
+tsvector_ge(PG_FUNCTION_ARGS)
+{
+       RUNCMP
+               PG_RETURN_BOOL((res >= 0) ? true : false);
+}
+
+Datum
+tsvector_gt(PG_FUNCTION_ARGS)
+{
+       RUNCMP
+               PG_RETURN_BOOL((res > 0) ? true : false);
+}
+
+Datum
+tsvector_ne(PG_FUNCTION_ARGS)
+{
+       RUNCMP
+               PG_RETURN_BOOL((res != 0) ? true : false);
+}