Header And Logo

PostgreSQL
| The world's most advanced open source database.

dict.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * dict.c
00004  *      Standard interface to dictionary
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  *
00008  *
00009  * IDENTIFICATION
00010  *    src/backend/tsearch/dict.c
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #include "postgres.h"
00015 
00016 #include "catalog/pg_type.h"
00017 #include "tsearch/ts_cache.h"
00018 #include "tsearch/ts_utils.h"
00019 #include "utils/builtins.h"
00020 
00021 
00022 /*
00023  * Lexize one word by dictionary, mostly debug function
00024  */
00025 Datum
00026 ts_lexize(PG_FUNCTION_ARGS)
00027 {
00028     Oid         dictId = PG_GETARG_OID(0);
00029     text       *in = PG_GETARG_TEXT_P(1);
00030     ArrayType  *a;
00031     TSDictionaryCacheEntry *dict;
00032     TSLexeme   *res,
00033                *ptr;
00034     Datum      *da;
00035     DictSubState dstate = {false, false, NULL};
00036 
00037     dict = lookup_ts_dictionary_cache(dictId);
00038 
00039     res = (TSLexeme *) DatumGetPointer(FunctionCall4(&dict->lexize,
00040                                              PointerGetDatum(dict->dictData),
00041                                                 PointerGetDatum(VARDATA(in)),
00042                                        Int32GetDatum(VARSIZE(in) - VARHDRSZ),
00043                                                   PointerGetDatum(&dstate)));
00044 
00045     if (dstate.getnext)
00046     {
00047         dstate.isend = true;
00048         ptr = (TSLexeme *) DatumGetPointer(FunctionCall4(&dict->lexize,
00049                                              PointerGetDatum(dict->dictData),
00050                                                 PointerGetDatum(VARDATA(in)),
00051                                        Int32GetDatum(VARSIZE(in) - VARHDRSZ),
00052                                                   PointerGetDatum(&dstate)));
00053         if (ptr != NULL)
00054             res = ptr;
00055     }
00056 
00057     if (!res)
00058         PG_RETURN_NULL();
00059 
00060     ptr = res;
00061     while (ptr->lexeme)
00062         ptr++;
00063     da = (Datum *) palloc(sizeof(Datum) * (ptr - res));
00064     ptr = res;
00065     while (ptr->lexeme)
00066     {
00067         da[ptr - res] = CStringGetTextDatum(ptr->lexeme);
00068         ptr++;
00069     }
00070 
00071     a = construct_array(da,
00072                         ptr - res,
00073                         TEXTOID,
00074                         -1,
00075                         false,
00076                         'i');
00077 
00078     ptr = res;
00079     while (ptr->lexeme)
00080     {
00081         pfree(DatumGetPointer(da[ptr - res]));
00082         pfree(ptr->lexeme);
00083         ptr++;
00084     }
00085     pfree(res);
00086     pfree(da);
00087 
00088     PG_RETURN_POINTER(a);
00089 }