/[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 9 - (hide annotations)
Sun May 22 21:18:11 2005 UTC (18 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 9402 byte(s)
support query in which only attribute is specified

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 5 /* prototype */
38     char *attr2text(ESTDOC *doc, char *attr);
39 dpavlin 1
40     ESTDB *db;
41     ESTCOND *cond;
42     ESTDOC *doc;
43     const CBLIST *texts;
44     int ecode, *est_result, resnum, i, j;
45 dpavlin 3 int limit = 0;
46     int offset = 0;
47 dpavlin 1
48     /* define PostgreSQL v1 function */
49     PG_FUNCTION_INFO_V1(pgest);
50     Datum pgest(PG_FUNCTION_ARGS) {
51    
52     FuncCallContext *funcctx;
53     int call_cntr;
54     int max_calls;
55     TupleDesc tupdesc;
56     TupleTableSlot *slot;
57     AttInMetadata *attinmeta;
58     char *index_path;
59     char *query;
60 dpavlin 5 char *attr;
61 dpavlin 1
62     /* stuff done only on the first call of the function */
63     if (SRF_IS_FIRSTCALL()) {
64     MemoryContext oldcontext;
65    
66     /* take arguments from function */
67     //index_path = _textout(PG_GETARG_TEXT_P(0));
68     index_path = _textout(PG_GETARG_TEXT_P(0));
69     query = _textout(PG_GETARG_TEXT_P(1));
70 dpavlin 5 attr = _textout(PG_GETARG_TEXT_P(2));
71     limit = PG_GETARG_INT32(3);
72     offset = PG_GETARG_INT32(4);
73 dpavlin 1
74     /* create a function context for cross-call persistence */
75     funcctx = SRF_FIRSTCALL_INIT();
76    
77     /* switch to memory context appropriate for multiple function calls */
78     oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
79    
80     /* open the database */
81     elog(DEBUG1, "pgest: est_db_open(%s)", index_path);
82    
83     if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){
84     elog(ERROR, "est_db_open: can't open %s [%d]: %s", index_path, ecode, est_err_msg(ecode));
85     SRF_RETURN_DONE(funcctx);
86     }
87    
88 dpavlin 5 elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, attr, limit, offset);
89 dpavlin 1
90     /* create a search condition object */
91     if (!(cond = est_cond_new())) {
92     elog(INFO, "pgest: est_cond_new failed");
93     SRF_RETURN_DONE(funcctx);
94     }
95    
96     /* set the search phrase to the search condition object */
97 dpavlin 9 if (strlen(query) > 0)
98     est_cond_set_phrase(cond, query);
99 dpavlin 1
100 dpavlin 5 /* minimum valid attribute length is 10: @a STREQ a */
101     if (attr != NULL && strlen(attr) >= 10) {
102     elog(INFO,"est_cond_add_attr(%s)", attr);
103     est_cond_add_attr(cond, attr);
104     }
105    
106 dpavlin 1 /* get the result of search */
107     est_result = est_db_search(db, cond, &resnum, NULL);
108    
109     /* total number of tuples to be returned */
110 dpavlin 5 if (limit && limit < resnum) {
111     funcctx->max_calls = limit - offset;
112     } else {
113     funcctx->max_calls = resnum - offset;
114     }
115 dpavlin 1
116     /* check if results exists */
117     if ( 0 == funcctx->max_calls )
118     elog(INFO, "pgest: no results for: %s", query );
119    
120 dpavlin 5 elog(DEBUG1, "pgest: found %d hits for %s", resnum, query);
121 dpavlin 1
122     /* Build a tuple description for a __pgest tuple */
123     tupdesc = RelationNameGetTupleDesc("__pgest");
124    
125     /* allocate a slot for a tuple with this tupdesc */
126     slot = TupleDescGetSlot(tupdesc);
127    
128     /* assign slot to function context */
129     funcctx->slot = slot;
130    
131     /*
132     * generate attribute metadata needed later to produce tuples from raw
133     * C strings
134     */
135     attinmeta = TupleDescGetAttInMetadata(tupdesc);
136     funcctx->attinmeta = attinmeta;
137    
138     MemoryContextSwitchTo(oldcontext);
139    
140 dpavlin 2 elog(DEBUG1, "SRF_IS_FIRSTCALL done");
141 dpavlin 1 }
142    
143     /* stuff done on every call of the function */
144     funcctx = SRF_PERCALL_SETUP();
145    
146     call_cntr = funcctx->call_cntr;
147     max_calls = funcctx->max_calls;
148     slot = funcctx->slot;
149     attinmeta = funcctx->attinmeta;
150 dpavlin 3
151     if (limit && call_cntr > limit - 1) {
152     elog(INFO, "call_cntr: %d limit: %d", call_cntr, limit);
153     SRF_RETURN_DONE(funcctx);
154     }
155    
156 dpavlin 1 if (call_cntr < max_calls) {
157     char **values;
158     HeapTuple tuple;
159     Datum result;
160    
161 dpavlin 2 elog(DEBUG1, "pgest: loop count %d", call_cntr);
162 dpavlin 1
163     if (! est_result) {
164     elog(ERROR, "pgest: no estraier results");
165     SRF_RETURN_DONE(funcctx);
166     }
167    
168     /*
169     * Prepare a values array for storage in our slot.
170     * This should be an array of C strings which will
171     * be processed later by the type input functions.
172     */
173    
174 dpavlin 3 if (doc = est_db_get_doc(db, est_result[call_cntr + offset], 0)) {
175 dpavlin 1
176 dpavlin 2 elog(DEBUG1, "URI: %s\n Title: %s\n",
177 dpavlin 1 est_doc_attr(doc, "@uri"),
178     est_doc_attr(doc, "@title")
179     );
180    
181     values = (char **) palloc(4 * sizeof(char *));
182    
183     // values[0] = (char *) palloc(strlen(_estval) * sizeof(char));
184    
185 dpavlin 5 values[0] = (char *) attr2text(doc,"@id");
186     values[1] = (char *) attr2text(doc,"@uri");
187     values[2] = (char *) attr2text(doc,"@title");
188     values[3] = (char *) attr2text(doc,"@type");
189 dpavlin 1
190     /* destloy the document object */
191     elog(DEBUG2, "est_doc_delete");
192     est_doc_delete(doc);
193     } else {
194     elog(INFO, "no result from estraier");
195 dpavlin 7 values[0] = DatumGetCString( "" );
196     values[1] = DatumGetCString( "" );
197     values[2] = DatumGetCString( "" );
198     values[3] = DatumGetCString( "" );
199 dpavlin 1 }
200    
201    
202     elog(DEBUG2, "build tuple");
203     /* build a tuple */
204     tuple = BuildTupleFromCStrings(attinmeta, values);
205    
206     elog(DEBUG2, "make tuple into datum");
207     /* make the tuple into a datum */
208     result = TupleGetDatum(slot, tuple);
209    
210     elog(DEBUG2, "cleanup");
211     /* clean up ? */
212     /*
213     pfree(values[0]);
214     pfree(values[1]);
215     pfree(values[2]);
216     pfree(values[3]);
217     pfree(values);
218     */
219    
220     elog(DEBUG2, "cleanup over");
221    
222     SRF_RETURN_NEXT(funcctx, result);
223     } else {
224 dpavlin 2 elog(DEBUG1, "loop over");
225 dpavlin 1
226     if(!est_db_close(db, &ecode)){
227     elog(INFO, "est_db_close error: %s", est_err_msg(ecode));
228     }
229    
230     /* do when there is no more left */
231     SRF_RETURN_DONE(funcctx);
232     }
233     }
234    
235     /* work in progress */
236     PG_FUNCTION_INFO_V1(pgest2);
237     Datum pgest2(PG_FUNCTION_ARGS)
238     {
239     int nrows = 3;
240     int16 typlen;
241     bool typbyval;
242     char typalign;
243     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
244     AttInMetadata *attinmeta;
245     TupleDesc tupdesc;
246     Tuplestorestate *tupstore = NULL;
247     HeapTuple tuple;
248     MemoryContext per_query_ctx;
249     MemoryContext oldcontext;
250     Datum dvalue;
251     char **values;
252     int ncols;
253     int i, j;
254    
255     /* check to see if caller supports us returning a tuplestore */
256     if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
257     ereport(ERROR,
258     (errcode(ERRCODE_SYNTAX_ERROR),
259     errmsg("materialize mode required, but it is not " \
260     "allowed in this context")));
261    
262     /* get the requested return tuple description */
263     tupdesc = rsinfo->expectedDesc;
264     ncols = tupdesc->natts;
265    
266     /*
267     * The requested tuple description better match up with the array
268     * we were given.
269     */
270     /* OK, use it */
271     attinmeta = TupleDescGetAttInMetadata(tupdesc);
272    
273     /* Now go to work */
274     rsinfo->returnMode = SFRM_Materialize;
275    
276     per_query_ctx = fcinfo->flinfo->fn_mcxt;
277     oldcontext = MemoryContextSwitchTo(per_query_ctx);
278    
279     /* initialize our tuplestore */
280     tupstore = tuplestore_begin_heap(true, false, SortMem);
281    
282     values = (char **) palloc(ncols * sizeof(char *));
283    
284     for (i = 0; i < nrows; i++)
285     {
286     for (j = 0; j < ncols; j++)
287     {
288     values[j] = DatumGetCString( "foo" );
289     }
290     /* construct the tuple */
291     tuple = BuildTupleFromCStrings(attinmeta, values);
292    
293     /* now store it */
294     tuplestore_puttuple(tupstore, tuple);
295     }
296    
297     tuplestore_donestoring(tupstore);
298     rsinfo->setResult = tupstore;
299    
300     /*
301     * SFRM_Materialize mode expects us to return a NULL Datum. The actual
302     * tuples are in our tuplestore and passed back through
303     * rsinfo->setResult. rsinfo->setDesc is set to the tuple description
304     * that we actually used to build our tuples with, so the caller can
305     * verify we did what it was expecting.
306     */
307     rsinfo->setDesc = tupdesc;
308     MemoryContextSwitchTo(oldcontext);
309    
310     return (Datum) 0;
311     }
312    
313    
314     /* make text var from attr */
315     char *attr2text(ESTDOC *doc, char *attr) {
316     char *val;
317     const char *attrval;
318     int len;
319 dpavlin 4 int attrlen;
320 dpavlin 1
321 dpavlin 2 elog(DEBUG1, "doc: %08x, attr: %s", doc, attr);
322 dpavlin 1
323 dpavlin 4 if ( (attrval = est_doc_attr(doc, attr)) && (attrlen = strlen(attrval)) ) {
324     val = (char *) palloc(attrlen * sizeof(char));
325 dpavlin 1 } else {
326     return (Datum) NULL;
327     }
328    
329     len = strlen(attrval);
330 dpavlin 2 elog(DEBUG1, "attr2text(%s) = '%s' %d bytes", attr, attrval, len);
331 dpavlin 1
332     len++;
333     len *= sizeof(char);
334    
335     elog(DEBUG2, "palloc(%d)", len);
336    
337     val = palloc(len);
338    
339     memset(val, 0, len);
340     strncpy(val, attrval, len);
341    
342     elog(DEBUG2, "val=%s", val);
343    
344     return val;
345     }
346    
347     /* make integer variable from property */
348     /*
349     char *prop2int(SW_RESULT sw_res, char *propname) {
350     char *val;
351     unsigned long prop;
352     int len;
353    
354     elog(DEBUG2, "prop2int(%s)", propname);
355    
356     prop = estResultPropertyULong( sw_res, propname );
357     if (error_or_abort( est_handle )) return NULL;
358    
359     elog(DEBUG1, "prop2int(%s) = %lu", propname, prop);
360    
361     len = 128 * sizeof(char);
362     elog(DEBUG2, "palloc(%d)", len);
363    
364     val = palloc(len);
365     memset(val, 0, len);
366    
367     snprintf(val, len, "%lu", prop);
368    
369     elog(DEBUG2, "val=%s", val);
370    
371     return val;
372     }
373     */

  ViewVC Help
Powered by ViewVC 1.1.26