/[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 38 - (hide annotations)
Fri Jul 8 12:47:49 2005 UTC (18 years, 9 months ago) by dpavlin
File MIME type: text/plain
File size: 8553 byte(s)
added est_cond_set_max to return just minumum needed resutls from Hyper Estraier

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 38 elog(INFO, "This is probably a bug in limit implementation. Please report it to dpavlin@rot13.org");
233 dpavlin 19 } else {
234     nrows = resnum - offset;
235     }
236    
237    
238     elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query);
239    
240    
241 dpavlin 1 values = (char **) palloc(ncols * sizeof(char *));
242    
243     for (i = 0; i < nrows; i++)
244     {
245 dpavlin 19
246     /* get result from estraier */
247     if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) {
248     elog(INFO, "can't find result %d", i + offset);
249     } else {
250     elog(DEBUG1, "URI: %s\n Title: %s\n",
251     est_doc_attr(doc, "@uri"),
252     est_doc_attr(doc, "@title")
253     );
254     }
255    
256     /* iterate over results */
257 dpavlin 1 for (j = 0; j < ncols; j++)
258     {
259 dpavlin 19 bool isnull;
260    
261     /* array value of this position */
262 dpavlin 25 indx[0] = j + attr_dim_lower_bounds[0];
263 dpavlin 19
264 dpavlin 25 dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull);
265 dpavlin 19
266     if (!isnull && doc)
267     values[j] = DatumGetCString(
268     attr2text(doc,
269 dpavlin 20 (char *)DirectFunctionCall1(textout, dvalue)
270 dpavlin 19 ));
271     else
272     values[j] = NULL;
273 dpavlin 1 }
274     /* construct the tuple */
275     tuple = BuildTupleFromCStrings(attinmeta, values);
276    
277     /* now store it */
278     tuplestore_puttuple(tupstore, tuple);
279 dpavlin 19
280    
281     /* delete estraier document object */
282     est_doc_delete(doc);
283 dpavlin 1 }
284    
285     tuplestore_donestoring(tupstore);
286     rsinfo->setResult = tupstore;
287    
288     /*
289     * SFRM_Materialize mode expects us to return a NULL Datum. The actual
290     * tuples are in our tuplestore and passed back through
291     * rsinfo->setResult. rsinfo->setDesc is set to the tuple description
292     * that we actually used to build our tuples with, so the caller can
293     * verify we did what it was expecting.
294     */
295     rsinfo->setDesc = tupdesc;
296     MemoryContextSwitchTo(oldcontext);
297    
298 dpavlin 31 est_cond_delete(cond);
299    
300 dpavlin 19 if(!est_db_close(db, &ecode)){
301     ereport(ERROR, (errcode(ERRCODE_IO_ERROR),
302     errmsg("est_db_close: %d", ecode),
303     errdetail(est_err_msg(ecode))));
304     }
305    
306 dpavlin 1 return (Datum) 0;
307     }
308    
309    
310     /* make text var from attr */
311     char *attr2text(ESTDOC *doc, char *attr) {
312     char *val;
313     const char *attrval;
314     int len;
315 dpavlin 4 int attrlen;
316 dpavlin 1
317 dpavlin 2 elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);
318 dpavlin 1
319 dpavlin 4 if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) {
320     val = (char *) palloc(attrlen * sizeof(char));
321 dpavlin 1 } else {
322     return (Datum) NULL;
323     }
324    
325     len = strlen(attrval);
326 dpavlin 2 elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len);
327 dpavlin 1
328     len++;
329     len *= sizeof(char);
330    
331     elog(DEBUG2, "palloc(%d)", len);
332    
333     val = palloc(len);
334    
335     memset(val, 0, len);
336     strncpy(val, attrval, len);
337    
338     elog(DEBUG2, "val=%s", val);
339    
340     return val;
341     }
342    

  ViewVC Help
Powered by ViewVC 1.1.26