/[pgestraier]/trunk/pgest.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/pgest.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 39 - (hide annotations)
Fri Jul 8 15:26:04 2005 UTC (18 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 8450 byte(s)
removed misleading INFO message (hint: it's not a bug)

1 dpavlin 1 /*
2     * integrate Hyper Estraier into PostgreSQL
3     *
4     * Dobrica Pavlinusic <dpavlin@rot13.org> 2005-05-19
5     *
6     * TODO:
7     * - all
8     *
9     * NOTES:
10     * - clear structures with memset to support hash indexes (who whould like
11     * to create hash index on table returned from function?)
12     * - number of returned rows is set by PostgreSQL evaluator, see:
13     * http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php
14     *
15     * Based on:
16     * - C example from PostgreSQL documentation (BSD licence)
17     * - example002.c from Hyper Estraier (GPL)
18     * - _textin/_textout from pgcurl.c (LGPL)
19     *
20     * This code is licenced under GPL
21     */
22    
23     #include "postgres.h"
24     #include "fmgr.h"
25     #include "funcapi.h"
26     #include "utils/builtins.h"
27     #include "utils/array.h"
28     #include "miscadmin.h"
29     #include <estraier.h>
30     #include <cabin.h>
31    
32     #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
33     #define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
34     #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
35     #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
36    
37 dpavlin 27 /* SortMem got renamed in PostgreSQL 8.0 */
38     #ifndef SortMem
39     #define SortMem 16 * 1024
40     #endif
41    
42 dpavlin 5 /* prototype */
43     char *attr2text(ESTDOC *doc, char *attr);
44 dpavlin 1
45    
46     /* work in progress */
47 dpavlin 19 PG_FUNCTION_INFO_V1(pgest_attr);
48     Datum pgest_attr(PG_FUNCTION_ARGS)
49 dpavlin 1 {
50 dpavlin 31 ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(6);
51 dpavlin 25 Oid attr_element_type = ARR_ELEMTYPE(attr_arr);
52     int attr_ndims = ARR_NDIM(attr_arr);
53     int *attr_dim_counts = ARR_DIMS(attr_arr);
54     int *attr_dim_lower_bounds = ARR_LBOUND(attr_arr);
55 dpavlin 19 int ncols = 0;
56     int nrows = 0;
57     int indx[MAXDIM];
58 dpavlin 25 int16 attr_len;
59     bool attr_byval;
60     char attr_align;
61 dpavlin 1 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
62     AttInMetadata *attinmeta;
63     TupleDesc tupdesc;
64 dpavlin 19 Tuplestorestate *tupstore = NULL;
65 dpavlin 1 HeapTuple tuple;
66     MemoryContext per_query_ctx;
67     MemoryContext oldcontext;
68     Datum dvalue;
69     char **values;
70 dpavlin 19 int rsinfo_ncols;
71 dpavlin 1 int i, j;
72 dpavlin 19 /* estvars */
73 dpavlin 25 ESTDB *db;
74     ESTCOND *cond;
75     ESTDOC *doc;
76     const CBLIST *texts;
77     int ecode, *est_result, resnum;
78     int limit = 0;
79     int offset = 0;
80    
81 dpavlin 19 char *index_path;
82     char *query;
83     char *attr;
84 dpavlin 31 char *order;
85 dpavlin 1
86 dpavlin 19
87     /* only allow 1D input array */
88 dpavlin 25 if (attr_ndims == 1)
89 dpavlin 19 {
90 dpavlin 25 ncols = attr_dim_counts[0];
91 dpavlin 19 }
92     else
93     ereport(ERROR,
94     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
95     errmsg("invalid input array"),
96     errdetail("Input array must have 1 dimension")));
97    
98 dpavlin 1 /* check to see if caller supports us returning a tuplestore */
99     if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
100     ereport(ERROR,
101     (errcode(ERRCODE_SYNTAX_ERROR),
102     errmsg("materialize mode required, but it is not " \
103     "allowed in this context")));
104    
105 dpavlin 19 /* get info about element type needed to construct the array */
106 dpavlin 25 get_typlenbyvalalign(attr_element_type, &attr_len, &attr_byval, &attr_align);
107 dpavlin 19
108 dpavlin 1 /* get the requested return tuple description */
109     tupdesc = rsinfo->expectedDesc;
110 dpavlin 19 rsinfo_ncols = tupdesc->natts;
111 dpavlin 1
112     /*
113     * The requested tuple description better match up with the array
114     * we were given.
115     */
116 dpavlin 19 if (rsinfo_ncols != ncols)
117     ereport(ERROR,
118     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
119     errmsg("invalid input array"),
120     errdetail("Number of elements in array must match number of query specified columns.")));
121    
122 dpavlin 1 /* OK, use it */
123     attinmeta = TupleDescGetAttInMetadata(tupdesc);
124    
125     /* Now go to work */
126     rsinfo->returnMode = SFRM_Materialize;
127    
128     per_query_ctx = fcinfo->flinfo->fn_mcxt;
129     oldcontext = MemoryContextSwitchTo(per_query_ctx);
130    
131     /* initialize our tuplestore */
132     tupstore = tuplestore_begin_heap(true, false, SortMem);
133    
134 dpavlin 19
135     /* take rest of arguments from function */
136    
137     /* index path */
138     if (PG_ARGISNULL(0)) {
139     ereport(ERROR,
140     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
141     errmsg("index path can't be null"),
142     errdetail("Index path must be valid full path to HyperEstraier index")));
143     }
144     index_path = _textout(PG_GETARG_TEXT_P(0));
145    
146     /* query string */
147 dpavlin 31 if (PG_ARGISNULL(1)) {
148 dpavlin 19 query = "";
149     } else {
150     query = _textout(PG_GETARG_TEXT_P(1));
151     }
152    
153     /* atribute filter */
154     if (PG_ARGISNULL(2)) {
155     attr = "";
156     } else {
157     attr = _textout(PG_GETARG_TEXT_P(2));
158     }
159 dpavlin 31
160     /* sort order */
161     if (PG_ARGISNULL(3)) {
162     order = "";
163     } else {
164     order = _textout(PG_GETARG_TEXT_P(3));
165     }
166 dpavlin 19
167 dpavlin 31
168 dpavlin 19 /* limit */
169 dpavlin 31 if (PG_ARGISNULL(4)) {
170 dpavlin 19 limit = 0;
171     } else {
172 dpavlin 31 limit = PG_GETARG_INT32(4);
173 dpavlin 19 }
174    
175     /* offset */
176 dpavlin 31 if (PG_ARGISNULL(5)) {
177 dpavlin 19 offset = 0;
178     } else {
179 dpavlin 31 offset = PG_GETARG_INT32(5);
180 dpavlin 19 }
181    
182    
183     /* open the database */
184     elog(DEBUG1, "pgest_attr: est_db_open(%s)", index_path);
185    
186     if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){
187     ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
188     errmsg("est_db_open: can't open %s: %d", index_path, ecode),
189     errdetail(est_err_msg(ecode))));
190     }
191    
192 dpavlin 20 elog(DEBUG1, "pgest_attr: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset);
193 dpavlin 19
194     /* create a search condition object */
195     if (!(cond = est_cond_new())) {
196     ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED),
197     errmsg("pgest_attr: est_cond_new failed")));
198     }
199    
200     /* set the search phrase to the search condition object */
201     if (! PG_ARGISNULL(1) && strlen(query) > 0)
202     est_cond_set_phrase(cond, query);
203    
204     /* minimum valid attribute length is 10: @a STREQ a */
205     if (! PG_ARGISNULL(2) && strlen(attr) >= 10) {
206 dpavlin 20 elog(DEBUG1,"est_cond_add_attr(%s)", attr);
207 dpavlin 19 est_cond_add_attr(cond, attr);
208     }
209    
210 dpavlin 31 /* set the search phrase to the search condition object */
211     if (! PG_ARGISNULL(3) && strlen(order) > 0) {
212     elog(DEBUG1,"est_cond_set_order(%s)", order);
213     est_cond_set_order(cond, order);
214     }
215    
216 dpavlin 38 if (limit) {
217     elog(DEBUG1,"est_cond_set_max(%d)", limit + offset);
218     est_cond_set_max(cond, limit + offset);
219     }
220    
221 dpavlin 19 /* get the result of search */
222     est_result = est_db_search(db, cond, &resnum, NULL);
223    
224     /* check if results exists */
225     if ( 0 == resnum ) {
226     elog(INFO, "pgest_attr: no results for: %s", query );
227     }
228    
229     /* total number of tuples to be returned */
230     if (limit && limit < resnum) {
231 dpavlin 31 nrows = limit;
232 dpavlin 19 } else {
233     nrows = resnum - offset;
234     }
235    
236    
237     elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query);
238    
239    
240 dpavlin 1 values = (char **) palloc(ncols * sizeof(char *));
241    
242     for (i = 0; i < nrows; i++)
243     {
244 dpavlin 19
245     /* get result from estraier */
246     if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) {
247     elog(INFO, "can't find result %d", i + offset);
248     } else {
249     elog(DEBUG1, "URI: %s\n Title: %s\n",
250     est_doc_attr(doc, "@uri"),
251     est_doc_attr(doc, "@title")
252     );
253     }
254    
255     /* iterate over results */
256 dpavlin 1 for (j = 0; j < ncols; j++)
257     {
258 dpavlin 19 bool isnull;
259    
260     /* array value of this position */
261 dpavlin 25 indx[0] = j + attr_dim_lower_bounds[0];
262 dpavlin 19
263 dpavlin 25 dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull);
264 dpavlin 19
265     if (!isnull && doc)
266     values[j] = DatumGetCString(
267     attr2text(doc,
268 dpavlin 20 (char *)DirectFunctionCall1(textout, dvalue)
269 dpavlin 19 ));
270     else
271     values[j] = NULL;
272 dpavlin 1 }
273     /* construct the tuple */
274     tuple = BuildTupleFromCStrings(attinmeta, values);
275    
276     /* now store it */
277     tuplestore_puttuple(tupstore, tuple);
278 dpavlin 19
279    
280     /* delete estraier document object */
281     est_doc_delete(doc);
282 dpavlin 1 }
283    
284     tuplestore_donestoring(tupstore);
285     rsinfo->setResult = tupstore;
286    
287     /*
288     * SFRM_Materialize mode expects us to return a NULL Datum. The actual
289     * tuples are in our tuplestore and passed back through
290     * rsinfo->setResult. rsinfo->setDesc is set to the tuple description
291     * that we actually used to build our tuples with, so the caller can
292     * verify we did what it was expecting.
293     */
294     rsinfo->setDesc = tupdesc;
295     MemoryContextSwitchTo(oldcontext);
296    
297 dpavlin 31 est_cond_delete(cond);
298    
299 dpavlin 19 if(!est_db_close(db, &ecode)){
300     ereport(ERROR, (errcode(ERRCODE_IO_ERROR),
301     errmsg("est_db_close: %d", ecode),
302     errdetail(est_err_msg(ecode))));
303     }
304    
305 dpavlin 1 return (Datum) 0;
306     }
307    
308    
309     /* make text var from attr */
310     char *attr2text(ESTDOC *doc, char *attr) {
311     char *val;
312     const char *attrval;
313     int len;
314 dpavlin 4 int attrlen;
315 dpavlin 1
316 dpavlin 2 elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);
317 dpavlin 1
318 dpavlin 4 if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) {
319     val = (char *) palloc(attrlen * sizeof(char));
320 dpavlin 1 } else {
321     return (Datum) NULL;
322     }
323    
324     len = strlen(attrval);
325 dpavlin 2 elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len);
326 dpavlin 1
327     len++;
328     len *= sizeof(char);
329    
330     elog(DEBUG2, "palloc(%d)", len);
331    
332     val = palloc(len);
333    
334     memset(val, 0, len);
335     strncpy(val, attrval, len);
336    
337     elog(DEBUG2, "val=%s", val);
338    
339     return val;
340     }
341    

  ViewVC Help
Powered by ViewVC 1.1.26