37 |
/* prototype */ |
/* prototype */ |
38 |
char *attr2text(ESTDOC *doc, char *attr); |
char *attr2text(ESTDOC *doc, char *attr); |
39 |
|
|
|
ESTDB *db; |
|
|
ESTCOND *cond; |
|
|
ESTDOC *doc; |
|
|
const CBLIST *texts; |
|
|
int ecode, *est_result, resnum, i, j; |
|
|
int limit = 0; |
|
|
int offset = 0; |
|
|
|
|
|
/* define PostgreSQL v1 function */ |
|
|
PG_FUNCTION_INFO_V1(pgest); |
|
|
Datum pgest(PG_FUNCTION_ARGS) { |
|
|
|
|
|
FuncCallContext *funcctx; |
|
|
int call_cntr; |
|
|
int max_calls; |
|
|
TupleDesc tupdesc; |
|
|
TupleTableSlot *slot; |
|
|
AttInMetadata *attinmeta; |
|
|
char *index_path; |
|
|
char *query; |
|
|
char *attr; |
|
|
|
|
|
/* stuff done only on the first call of the function */ |
|
|
if (SRF_IS_FIRSTCALL()) { |
|
|
MemoryContext oldcontext; |
|
|
|
|
|
/* create a function context for cross-call persistence */ |
|
|
funcctx = SRF_FIRSTCALL_INIT(); |
|
|
|
|
|
/* switch to memory context appropriate for multiple function calls */ |
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
|
|
/* take arguments from function */ |
|
|
|
|
|
/* index path */ |
|
|
if (PG_ARGISNULL(0)) { |
|
|
elog(ERROR, "index path can't be null"); |
|
|
SRF_RETURN_DONE(funcctx); |
|
|
} |
|
|
index_path = _textout(PG_GETARG_TEXT_P(0)); |
|
|
|
|
|
/* query string */ |
|
|
if (PG_ARGISNULL(0)) { |
|
|
query = ""; |
|
|
} else { |
|
|
query = _textout(PG_GETARG_TEXT_P(1)); |
|
|
} |
|
|
|
|
|
/* atribute filter */ |
|
|
if (PG_ARGISNULL(2)) { |
|
|
attr = ""; |
|
|
} else { |
|
|
attr = _textout(PG_GETARG_TEXT_P(2)); |
|
|
} |
|
|
|
|
|
/* limit */ |
|
|
if (PG_ARGISNULL(3)) { |
|
|
limit = 0; |
|
|
} else { |
|
|
limit = PG_GETARG_INT32(3); |
|
|
} |
|
|
|
|
|
/* offset */ |
|
|
if (PG_ARGISNULL(4)) { |
|
|
offset = 0; |
|
|
} else { |
|
|
offset = PG_GETARG_INT32(4); |
|
|
} |
|
|
|
|
|
|
|
|
/* open the database */ |
|
|
elog(DEBUG1, "pgest: est_db_open(%s)", index_path); |
|
|
|
|
|
if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){ |
|
|
elog(ERROR, "est_db_open: can't open %s [%d]: %s", index_path, ecode, est_err_msg(ecode)); |
|
|
SRF_RETURN_DONE(funcctx); |
|
|
} |
|
|
|
|
|
elog(INFO, "pgest: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset); |
|
|
|
|
|
/* create a search condition object */ |
|
|
if (!(cond = est_cond_new())) { |
|
|
elog(INFO, "pgest: est_cond_new failed"); |
|
|
SRF_RETURN_DONE(funcctx); |
|
|
} |
|
|
|
|
|
/* set the search phrase to the search condition object */ |
|
|
if (! PG_ARGISNULL(1) && strlen(query) > 0) |
|
|
est_cond_set_phrase(cond, query); |
|
|
|
|
|
/* minimum valid attribute length is 10: @a STREQ a */ |
|
|
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
|
|
elog(INFO,"est_cond_add_attr(%s)", attr); |
|
|
est_cond_add_attr(cond, attr); |
|
|
} |
|
|
|
|
|
/* get the result of search */ |
|
|
est_result = est_db_search(db, cond, &resnum, NULL); |
|
|
|
|
|
/* total number of tuples to be returned */ |
|
|
if (limit && limit < resnum) { |
|
|
funcctx->max_calls = limit - offset; |
|
|
} else { |
|
|
funcctx->max_calls = resnum - offset; |
|
|
} |
|
|
|
|
|
/* check if results exists */ |
|
|
if ( 0 == funcctx->max_calls ) |
|
|
elog(INFO, "pgest: no results for: %s", query ); |
|
|
|
|
|
elog(DEBUG1, "pgest: found %d hits for %s", resnum, query); |
|
|
|
|
|
/* Build a tuple description for a __pgest tuple */ |
|
|
tupdesc = RelationNameGetTupleDesc("__pgest"); |
|
|
|
|
|
/* allocate a slot for a tuple with this tupdesc */ |
|
|
slot = TupleDescGetSlot(tupdesc); |
|
|
|
|
|
/* assign slot to function context */ |
|
|
funcctx->slot = slot; |
|
|
|
|
|
/* |
|
|
* generate attribute metadata needed later to produce tuples from raw |
|
|
* C strings |
|
|
*/ |
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
|
|
funcctx->attinmeta = attinmeta; |
|
|
|
|
|
MemoryContextSwitchTo(oldcontext); |
|
|
|
|
|
elog(DEBUG1, "SRF_IS_FIRSTCALL done"); |
|
|
} |
|
|
|
|
|
/* stuff done on every call of the function */ |
|
|
funcctx = SRF_PERCALL_SETUP(); |
|
|
|
|
|
call_cntr = funcctx->call_cntr; |
|
|
max_calls = funcctx->max_calls; |
|
|
slot = funcctx->slot; |
|
|
attinmeta = funcctx->attinmeta; |
|
|
|
|
|
if (limit && call_cntr > limit - 1) { |
|
|
elog(INFO, "call_cntr: %d limit: %d", call_cntr, limit); |
|
|
SRF_RETURN_DONE(funcctx); |
|
|
} |
|
|
|
|
|
if (call_cntr < max_calls) { |
|
|
char **values; |
|
|
HeapTuple tuple; |
|
|
Datum result; |
|
|
|
|
|
elog(DEBUG1, "pgest: loop count %d", call_cntr); |
|
|
|
|
|
if (! est_result) { |
|
|
elog(ERROR, "pgest: no estraier results"); |
|
|
SRF_RETURN_DONE(funcctx); |
|
|
} |
|
|
|
|
|
/* |
|
|
* Prepare a values array for storage in our slot. |
|
|
* This should be an array of C strings which will |
|
|
* be processed later by the type input functions. |
|
|
*/ |
|
|
|
|
|
if (doc = est_db_get_doc(db, est_result[call_cntr + offset], 0)) { |
|
|
|
|
|
elog(DEBUG1, "URI: %s\n Title: %s\n", |
|
|
est_doc_attr(doc, "@uri"), |
|
|
est_doc_attr(doc, "@title") |
|
|
); |
|
|
|
|
|
values = (char **) palloc(4 * sizeof(char *)); |
|
|
|
|
|
// values[0] = (char *) palloc(strlen(_estval) * sizeof(char)); |
|
|
|
|
|
values[0] = (char *) attr2text(doc,"@id"); |
|
|
values[1] = (char *) attr2text(doc,"@uri"); |
|
|
values[2] = (char *) attr2text(doc,"@title"); |
|
|
values[3] = (char *) attr2text(doc,"@size"); |
|
|
|
|
|
/* destloy the document object */ |
|
|
elog(DEBUG2, "est_doc_delete"); |
|
|
est_doc_delete(doc); |
|
|
} else { |
|
|
elog(INFO, "no result from estraier"); |
|
|
values[0] = DatumGetCString( "" ); |
|
|
values[1] = DatumGetCString( "" ); |
|
|
values[2] = DatumGetCString( "" ); |
|
|
values[3] = DatumGetCString( "" ); |
|
|
} |
|
|
|
|
|
|
|
|
elog(DEBUG2, "build tuple"); |
|
|
/* build a tuple */ |
|
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
|
|
|
|
|
elog(DEBUG2, "make tuple into datum"); |
|
|
/* make the tuple into a datum */ |
|
|
result = TupleGetDatum(slot, tuple); |
|
|
|
|
|
elog(DEBUG2, "cleanup"); |
|
|
/* clean up ? */ |
|
|
/* |
|
|
pfree(values[0]); |
|
|
pfree(values[1]); |
|
|
pfree(values[2]); |
|
|
pfree(values[3]); |
|
|
pfree(values); |
|
|
*/ |
|
|
|
|
|
elog(DEBUG2, "cleanup over"); |
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, result); |
|
|
} else { |
|
|
elog(DEBUG1, "loop over"); |
|
|
|
|
|
if(!est_db_close(db, &ecode)){ |
|
|
elog(INFO, "est_db_close error: %s", est_err_msg(ecode)); |
|
|
} |
|
|
|
|
|
/* do when there is no more left */ |
|
|
SRF_RETURN_DONE(funcctx); |
|
|
} |
|
|
} |
|
40 |
|
|
41 |
/* work in progress */ |
/* work in progress */ |
42 |
PG_FUNCTION_INFO_V1(pgest2); |
PG_FUNCTION_INFO_V1(pgest_attr); |
43 |
Datum pgest2(PG_FUNCTION_ARGS) |
Datum pgest_attr(PG_FUNCTION_ARGS) |
44 |
{ |
{ |
45 |
int nrows = 3; |
ArrayType *attr_arr = PG_GETARG_ARRAYTYPE_P(5); |
46 |
int16 typlen; |
Oid attr_element_type = ARR_ELEMTYPE(attr_arr); |
47 |
bool typbyval; |
int attr_ndims = ARR_NDIM(attr_arr); |
48 |
char typalign; |
int *attr_dim_counts = ARR_DIMS(attr_arr); |
49 |
|
int *attr_dim_lower_bounds = ARR_LBOUND(attr_arr); |
50 |
|
int ncols = 0; |
51 |
|
int nrows = 0; |
52 |
|
int indx[MAXDIM]; |
53 |
|
int16 attr_len; |
54 |
|
bool attr_byval; |
55 |
|
char attr_align; |
56 |
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
57 |
AttInMetadata *attinmeta; |
AttInMetadata *attinmeta; |
58 |
TupleDesc tupdesc; |
TupleDesc tupdesc; |
59 |
Tuplestorestate *tupstore = NULL; |
Tuplestorestate *tupstore = NULL; |
60 |
HeapTuple tuple; |
HeapTuple tuple; |
61 |
MemoryContext per_query_ctx; |
MemoryContext per_query_ctx; |
62 |
MemoryContext oldcontext; |
MemoryContext oldcontext; |
63 |
Datum dvalue; |
Datum dvalue; |
64 |
char **values; |
char **values; |
65 |
int ncols; |
int rsinfo_ncols; |
66 |
int i, j; |
int i, j; |
67 |
|
/* estvars */ |
68 |
|
ESTDB *db; |
69 |
|
ESTCOND *cond; |
70 |
|
ESTDOC *doc; |
71 |
|
const CBLIST *texts; |
72 |
|
int ecode, *est_result, resnum; |
73 |
|
int limit = 0; |
74 |
|
int offset = 0; |
75 |
|
|
76 |
|
char *index_path; |
77 |
|
char *query; |
78 |
|
char *attr; |
79 |
|
|
80 |
|
|
81 |
|
/* only allow 1D input array */ |
82 |
|
if (attr_ndims == 1) |
83 |
|
{ |
84 |
|
ncols = attr_dim_counts[0]; |
85 |
|
} |
86 |
|
else |
87 |
|
ereport(ERROR, |
88 |
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
89 |
|
errmsg("invalid input array"), |
90 |
|
errdetail("Input array must have 1 dimension"))); |
91 |
|
|
92 |
/* check to see if caller supports us returning a tuplestore */ |
/* check to see if caller supports us returning a tuplestore */ |
93 |
if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) |
if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) |
94 |
ereport(ERROR, |
ereport(ERROR, |
96 |
errmsg("materialize mode required, but it is not " \ |
errmsg("materialize mode required, but it is not " \ |
97 |
"allowed in this context"))); |
"allowed in this context"))); |
98 |
|
|
99 |
|
/* get info about element type needed to construct the array */ |
100 |
|
get_typlenbyvalalign(attr_element_type, &attr_len, &attr_byval, &attr_align); |
101 |
|
|
102 |
/* get the requested return tuple description */ |
/* get the requested return tuple description */ |
103 |
tupdesc = rsinfo->expectedDesc; |
tupdesc = rsinfo->expectedDesc; |
104 |
ncols = tupdesc->natts; |
rsinfo_ncols = tupdesc->natts; |
105 |
|
|
106 |
/* |
/* |
107 |
* The requested tuple description better match up with the array |
* The requested tuple description better match up with the array |
108 |
* we were given. |
* we were given. |
109 |
*/ |
*/ |
110 |
|
if (rsinfo_ncols != ncols) |
111 |
|
ereport(ERROR, |
112 |
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
113 |
|
errmsg("invalid input array"), |
114 |
|
errdetail("Number of elements in array must match number of query specified columns."))); |
115 |
|
|
116 |
/* OK, use it */ |
/* OK, use it */ |
117 |
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
118 |
|
|
125 |
/* initialize our tuplestore */ |
/* initialize our tuplestore */ |
126 |
tupstore = tuplestore_begin_heap(true, false, SortMem); |
tupstore = tuplestore_begin_heap(true, false, SortMem); |
127 |
|
|
128 |
|
|
129 |
|
/* take rest of arguments from function */ |
130 |
|
|
131 |
|
/* index path */ |
132 |
|
if (PG_ARGISNULL(0)) { |
133 |
|
ereport(ERROR, |
134 |
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
135 |
|
errmsg("index path can't be null"), |
136 |
|
errdetail("Index path must be valid full path to HyperEstraier index"))); |
137 |
|
} |
138 |
|
index_path = _textout(PG_GETARG_TEXT_P(0)); |
139 |
|
|
140 |
|
/* query string */ |
141 |
|
if (PG_ARGISNULL(0)) { |
142 |
|
query = ""; |
143 |
|
} else { |
144 |
|
query = _textout(PG_GETARG_TEXT_P(1)); |
145 |
|
} |
146 |
|
|
147 |
|
/* atribute filter */ |
148 |
|
if (PG_ARGISNULL(2)) { |
149 |
|
attr = ""; |
150 |
|
} else { |
151 |
|
attr = _textout(PG_GETARG_TEXT_P(2)); |
152 |
|
} |
153 |
|
|
154 |
|
/* limit */ |
155 |
|
if (PG_ARGISNULL(3)) { |
156 |
|
limit = 0; |
157 |
|
} else { |
158 |
|
limit = PG_GETARG_INT32(3); |
159 |
|
} |
160 |
|
|
161 |
|
/* offset */ |
162 |
|
if (PG_ARGISNULL(4)) { |
163 |
|
offset = 0; |
164 |
|
} else { |
165 |
|
offset = PG_GETARG_INT32(4); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
/* open the database */ |
170 |
|
elog(DEBUG1, "pgest_attr: est_db_open(%s)", index_path); |
171 |
|
|
172 |
|
if(!(db = est_db_open(index_path, ESTDBREADER, &ecode))){ |
173 |
|
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
174 |
|
errmsg("est_db_open: can't open %s: %d", index_path, ecode), |
175 |
|
errdetail(est_err_msg(ecode)))); |
176 |
|
} |
177 |
|
|
178 |
|
elog(DEBUG1, "pgest_attr: query[%s] attr[%s] limit %d offset %d", query, (PG_ARGISNULL(2) ? "NULL" : attr), limit, offset); |
179 |
|
|
180 |
|
/* create a search condition object */ |
181 |
|
if (!(cond = est_cond_new())) { |
182 |
|
ereport(ERROR, (errcode(ERRCODE_QUERY_CANCELED), |
183 |
|
errmsg("pgest_attr: est_cond_new failed"))); |
184 |
|
} |
185 |
|
|
186 |
|
/* set the search phrase to the search condition object */ |
187 |
|
if (! PG_ARGISNULL(1) && strlen(query) > 0) |
188 |
|
est_cond_set_phrase(cond, query); |
189 |
|
|
190 |
|
/* minimum valid attribute length is 10: @a STREQ a */ |
191 |
|
if (! PG_ARGISNULL(2) && strlen(attr) >= 10) { |
192 |
|
elog(DEBUG1,"est_cond_add_attr(%s)", attr); |
193 |
|
est_cond_add_attr(cond, attr); |
194 |
|
} |
195 |
|
|
196 |
|
/* get the result of search */ |
197 |
|
est_result = est_db_search(db, cond, &resnum, NULL); |
198 |
|
|
199 |
|
/* check if results exists */ |
200 |
|
if ( 0 == resnum ) { |
201 |
|
elog(INFO, "pgest_attr: no results for: %s", query ); |
202 |
|
} |
203 |
|
|
204 |
|
/* total number of tuples to be returned */ |
205 |
|
if (limit && limit < resnum) { |
206 |
|
nrows = limit - offset; |
207 |
|
} else { |
208 |
|
nrows = resnum - offset; |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
elog(DEBUG1, "pgest_attr: found %d hits for %s", resnum, query); |
213 |
|
|
214 |
|
|
215 |
values = (char **) palloc(ncols * sizeof(char *)); |
values = (char **) palloc(ncols * sizeof(char *)); |
216 |
|
|
217 |
for (i = 0; i < nrows; i++) |
for (i = 0; i < nrows; i++) |
218 |
{ |
{ |
219 |
|
|
220 |
|
/* get result from estraier */ |
221 |
|
if (! ( doc = est_db_get_doc(db, est_result[i + offset], 0)) ) { |
222 |
|
elog(INFO, "can't find result %d", i + offset); |
223 |
|
} else { |
224 |
|
elog(DEBUG1, "URI: %s\n Title: %s\n", |
225 |
|
est_doc_attr(doc, "@uri"), |
226 |
|
est_doc_attr(doc, "@title") |
227 |
|
); |
228 |
|
} |
229 |
|
|
230 |
|
/* iterate over results */ |
231 |
for (j = 0; j < ncols; j++) |
for (j = 0; j < ncols; j++) |
232 |
{ |
{ |
233 |
values[j] = DatumGetCString( "foo" ); |
bool isnull; |
234 |
|
|
235 |
|
/* array value of this position */ |
236 |
|
indx[0] = j + attr_dim_lower_bounds[0]; |
237 |
|
|
238 |
|
dvalue = array_ref(attr_arr, attr_ndims, indx, -1, attr_len, attr_byval, attr_align, &isnull); |
239 |
|
|
240 |
|
if (!isnull && doc) |
241 |
|
values[j] = DatumGetCString( |
242 |
|
attr2text(doc, |
243 |
|
(char *)DirectFunctionCall1(textout, dvalue) |
244 |
|
)); |
245 |
|
else |
246 |
|
values[j] = NULL; |
247 |
} |
} |
248 |
/* construct the tuple */ |
/* construct the tuple */ |
249 |
tuple = BuildTupleFromCStrings(attinmeta, values); |
tuple = BuildTupleFromCStrings(attinmeta, values); |
250 |
|
|
251 |
/* now store it */ |
/* now store it */ |
252 |
tuplestore_puttuple(tupstore, tuple); |
tuplestore_puttuple(tupstore, tuple); |
253 |
|
|
254 |
|
|
255 |
|
/* delete estraier document object */ |
256 |
|
est_doc_delete(doc); |
257 |
} |
} |
258 |
|
|
259 |
tuplestore_donestoring(tupstore); |
tuplestore_donestoring(tupstore); |
269 |
rsinfo->setDesc = tupdesc; |
rsinfo->setDesc = tupdesc; |
270 |
MemoryContextSwitchTo(oldcontext); |
MemoryContextSwitchTo(oldcontext); |
271 |
|
|
272 |
|
if(!est_db_close(db, &ecode)){ |
273 |
|
ereport(ERROR, (errcode(ERRCODE_IO_ERROR), |
274 |
|
errmsg("est_db_close: %d", ecode), |
275 |
|
errdetail(est_err_msg(ecode)))); |
276 |
|
} |
277 |
|
|
278 |
return (Datum) 0; |
return (Datum) 0; |
279 |
} |
} |
280 |
|
|
312 |
return val; |
return val; |
313 |
} |
} |
314 |
|
|
|
/* make integer variable from property */ |
|
|
/* |
|
|
char *prop2int(SW_RESULT sw_res, char *propname) { |
|
|
char *val; |
|
|
unsigned long prop; |
|
|
int len; |
|
|
|
|
|
elog(DEBUG2, "prop2int(%s)", propname); |
|
|
|
|
|
prop = estResultPropertyULong( sw_res, propname ); |
|
|
if (error_or_abort( est_handle )) return NULL; |
|
|
|
|
|
elog(DEBUG1, "prop2int(%s) = %lu", propname, prop); |
|
|
|
|
|
len = 128 * sizeof(char); |
|
|
elog(DEBUG2, "palloc(%d)", len); |
|
|
|
|
|
val = palloc(len); |
|
|
memset(val, 0, len); |
|
|
|
|
|
snprintf(val, len, "%lu", prop); |
|
|
|
|
|
elog(DEBUG2, "val=%s", val); |
|
|
|
|
|
return val; |
|
|
} |
|
|
*/ |
|