1 |
dpavlin |
8 |
/* |
2 |
|
|
* integrate swish-e into PostgreSQL |
3 |
|
|
* |
4 |
|
|
* Dobrica Pavlinusic <dpavlin@rot13.org> 2005-02-18 |
5 |
|
|
* |
6 |
|
|
* TODO: |
7 |
|
|
* - check null input using PG_ARGISNULL before using PG_GETARG_xxxx |
8 |
|
|
* - support composite type arguments |
9 |
|
|
* |
10 |
|
|
* NOTES: |
11 |
|
|
* - clear structures with memset to support hash indexes (who whould like |
12 |
|
|
* to create hash index on table returned from function?) |
13 |
|
|
* |
14 |
dpavlin |
9 |
* Based on: |
15 |
|
|
* - C example from PostgreSQL documentation (BSD licence) |
16 |
|
|
* - swish-e example src/libtest.c (GPL) |
17 |
|
|
* - _textin/_textout from pgcurl.c (LGPL) |
18 |
|
|
* |
19 |
|
|
* This code is licenced under GPL |
20 |
dpavlin |
8 |
*/ |
21 |
|
|
|
22 |
|
|
#include "postgres.h" |
23 |
|
|
#include "fmgr.h" |
24 |
|
|
#include "funcapi.h" |
25 |
dpavlin |
9 |
#include "utils/builtins.h" |
26 |
dpavlin |
8 |
#include <swish-e.h> |
27 |
|
|
|
28 |
dpavlin |
9 |
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
29 |
|
|
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
30 |
dpavlin |
8 |
|
31 |
dpavlin |
9 |
|
32 |
dpavlin |
8 |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
33 |
|
|
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
34 |
|
|
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
35 |
|
|
|
36 |
|
|
/* define PostgreSQL v1 function */ |
37 |
|
|
PG_FUNCTION_INFO_V1(pgswish); |
38 |
|
|
Datum pgswish(PG_FUNCTION_ARGS) { |
39 |
|
|
|
40 |
|
|
FuncCallContext *funcctx; |
41 |
|
|
int call_cntr; |
42 |
|
|
int max_calls; |
43 |
|
|
TupleDesc tupdesc; |
44 |
|
|
TupleTableSlot *slot; |
45 |
|
|
AttInMetadata *attinmeta; |
46 |
|
|
SW_HANDLE swish_handle = NULL; /* Database handle */ |
47 |
|
|
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
48 |
|
|
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
49 |
dpavlin |
9 |
char *index_path; |
50 |
|
|
char *query; |
51 |
dpavlin |
8 |
|
52 |
|
|
/* stuff done only on the first call of the function */ |
53 |
|
|
if (SRF_IS_FIRSTCALL()) { |
54 |
|
|
MemoryContext oldcontext; |
55 |
|
|
|
56 |
dpavlin |
9 |
/* take arguments from function */ |
57 |
|
|
//index_path = _textout(PG_GETARG_TEXT_P(0)); |
58 |
|
|
index_path = _textout(PG_GETARG_TEXT_P(0)); |
59 |
|
|
query = _textout(PG_GETARG_TEXT_P(1)); |
60 |
|
|
|
61 |
dpavlin |
8 |
/* create a function context for cross-call persistence */ |
62 |
|
|
funcctx = SRF_FIRSTCALL_INIT(); |
63 |
|
|
|
64 |
|
|
/* switch to memory context appropriate for multiple function calls */ |
65 |
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
66 |
|
|
|
67 |
dpavlin |
9 |
|
68 |
dpavlin |
8 |
/* Send any errors or warnings to stderr (default is stdout) */ |
69 |
|
|
SwishErrorsToStderr(); |
70 |
|
|
|
71 |
dpavlin |
9 |
elog(INFO, "pgswish: SwishInit(%s)", index_path); |
72 |
dpavlin |
8 |
|
73 |
dpavlin |
9 |
swish_handle = SwishInit( index_path ); |
74 |
dpavlin |
8 |
|
75 |
|
|
if (! swish_handle) { |
76 |
dpavlin |
9 |
elog(ERROR, "pgswish: can't open %s", index_path); |
77 |
dpavlin |
8 |
SRF_RETURN_DONE(funcctx); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
81 |
|
|
/* set ranking scheme. default is 0 */ |
82 |
|
|
SwishRankScheme( swish_handle, 1 ); |
83 |
|
|
|
84 |
|
|
/* Check for errors after every call */ |
85 |
|
|
if ( SwishError( swish_handle ) ) |
86 |
|
|
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
87 |
|
|
|
88 |
dpavlin |
9 |
elog(INFO, "pgswish: SwishQuery(%s)", query); |
89 |
dpavlin |
8 |
/* Here's a short-cut to searching that creates a search object and searches at the same time */ |
90 |
dpavlin |
9 |
elog(INFO,"## FIXME: SwishQuery kills back-end?"); |
91 |
|
|
results = SwishQuery( swish_handle, query); |
92 |
|
|
elog(INFO,"## FIXME: no..."); |
93 |
dpavlin |
8 |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
94 |
|
|
|
95 |
|
|
/* total number of tuples to be returned */ |
96 |
|
|
funcctx->max_calls = SwishHits( results ); |
97 |
|
|
|
98 |
|
|
/* check if results exists */ |
99 |
|
|
if ( 0 == funcctx->max_calls ) |
100 |
dpavlin |
9 |
elog(INFO, "no results for: %s", query ); |
101 |
dpavlin |
8 |
|
102 |
dpavlin |
9 |
elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls); |
103 |
|
|
|
104 |
dpavlin |
8 |
/* Build a tuple description for a __pgswish tuple */ |
105 |
|
|
tupdesc = RelationNameGetTupleDesc("__pgswish"); |
106 |
|
|
|
107 |
|
|
/* allocate a slot for a tuple with this tupdesc */ |
108 |
|
|
slot = TupleDescGetSlot(tupdesc); |
109 |
|
|
|
110 |
|
|
/* assign slot to function context */ |
111 |
|
|
funcctx->slot = slot; |
112 |
|
|
|
113 |
|
|
/* |
114 |
|
|
* generate attribute metadata needed later to produce tuples from raw |
115 |
|
|
* C strings |
116 |
|
|
*/ |
117 |
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
118 |
|
|
funcctx->attinmeta = attinmeta; |
119 |
|
|
|
120 |
|
|
MemoryContextSwitchTo(oldcontext); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
/* stuff done on every call of the function */ |
124 |
|
|
funcctx = SRF_PERCALL_SETUP(); |
125 |
|
|
|
126 |
|
|
call_cntr = funcctx->call_cntr; |
127 |
|
|
max_calls = funcctx->max_calls; |
128 |
|
|
slot = funcctx->slot; |
129 |
|
|
attinmeta = funcctx->attinmeta; |
130 |
|
|
|
131 |
|
|
if (call_cntr < max_calls) { |
132 |
|
|
char **values; |
133 |
|
|
HeapTuple tuple; |
134 |
|
|
Datum result; |
135 |
|
|
|
136 |
|
|
if (0) { |
137 |
|
|
/* |
138 |
|
|
* Prepare a values array for storage in our slot. |
139 |
|
|
* This should be an array of C strings which will |
140 |
|
|
* be processed later by the type input functions. |
141 |
|
|
*/ |
142 |
dpavlin |
9 |
values = (char **) palloc(5 * sizeof(char *)); |
143 |
|
|
values[0] = _textout( SwishResultPropertyULong ( result, "swishrank" ) ); |
144 |
|
|
values[1] = _textout( SwishResultPropertyStr ( result, "swishdocpath" ) ); |
145 |
|
|
values[2] = _textout( SwishResultPropertyStr ( result, "swishtitle" ) ); |
146 |
|
|
values[3] = _textout( SwishResultPropertyStr ( result, "swishdocsize" ) ); |
147 |
|
|
values[4] = _textout( SwishResultPropertyStr ( result, "swishdbfile" ) ); |
148 |
dpavlin |
8 |
|
149 |
|
|
/* build a tuple */ |
150 |
|
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
151 |
|
|
|
152 |
|
|
/* make the tuple into a datum */ |
153 |
|
|
result = TupleGetDatum(slot, tuple); |
154 |
|
|
|
155 |
|
|
} |
156 |
|
|
/* clean up (this is not really necessary) */ |
157 |
|
|
|
158 |
|
|
SRF_RETURN_NEXT(funcctx, result); |
159 |
|
|
} else { |
160 |
|
|
/* free swish object and close */ |
161 |
|
|
Free_Search_Object( search ); |
162 |
|
|
SwishClose( swish_handle ); |
163 |
|
|
|
164 |
|
|
/* do when there is no more left */ |
165 |
|
|
SRF_RETURN_DONE(funcctx); |
166 |
|
|
} |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
/* |
170 |
|
|
* elog errors |
171 |
|
|
* |
172 |
|
|
*/ |
173 |
|
|
|
174 |
|
|
static void error_or_abort( SW_HANDLE swish_handle ) { |
175 |
|
|
if ( !SwishError( swish_handle ) ) |
176 |
|
|
return; |
177 |
|
|
|
178 |
|
|
/* print a message */ |
179 |
|
|
elog(ERROR, |
180 |
|
|
"pgswish error: Number [%d], Type [%s], Optional Message: [%s]\n", |
181 |
|
|
SwishError( swish_handle ), |
182 |
|
|
SwishErrorString( swish_handle ), |
183 |
|
|
SwishLastErrorMsg( swish_handle ) |
184 |
|
|
); |
185 |
|
|
if ( search ) Free_Search_Object( search ); |
186 |
|
|
SwishClose( swish_handle ); |
187 |
|
|
|
188 |
|
|
/* do when there is no more left */ |
189 |
|
|
} |
190 |
|
|
|