11 |
* - clear structures with memset to support hash indexes (who whould like |
* - clear structures with memset to support hash indexes (who whould like |
12 |
* to create hash index on table returned from function?) |
* to create hash index on table returned from function?) |
13 |
* |
* |
14 |
* Based on C example from PostgreSQL documentation and swish-e |
* Based on: |
15 |
* src/libtest.c, so licence is GPL |
* - 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 |
*/ |
*/ |
21 |
|
|
22 |
#include "postgres.h" |
#include "postgres.h" |
23 |
#include "fmgr.h" |
#include "fmgr.h" |
24 |
#include "funcapi.h" |
#include "funcapi.h" |
25 |
|
#include "utils/builtins.h" |
26 |
#include <swish-e.h> |
#include <swish-e.h> |
27 |
|
|
28 |
|
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
29 |
|
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
30 |
|
|
31 |
|
|
32 |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
33 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
46 |
SW_HANDLE swish_handle = NULL; /* Database handle */ |
SW_HANDLE swish_handle = NULL; /* Database handle */ |
47 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
48 |
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
49 |
|
char *index_path; |
50 |
|
char *query; |
51 |
|
|
52 |
/* stuff done only on the first call of the function */ |
/* stuff done only on the first call of the function */ |
53 |
if (SRF_IS_FIRSTCALL()) { |
if (SRF_IS_FIRSTCALL()) { |
54 |
MemoryContext oldcontext; |
MemoryContext oldcontext; |
55 |
|
|
56 |
|
/* 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 |
/* create a function context for cross-call persistence */ |
/* create a function context for cross-call persistence */ |
62 |
funcctx = SRF_FIRSTCALL_INIT(); |
funcctx = SRF_FIRSTCALL_INIT(); |
63 |
|
|
64 |
/* switch to memory context appropriate for multiple function calls */ |
/* switch to memory context appropriate for multiple function calls */ |
65 |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
66 |
|
|
67 |
|
|
68 |
/* Send any errors or warnings to stderr (default is stdout) */ |
/* Send any errors or warnings to stderr (default is stdout) */ |
69 |
SwishErrorsToStderr(); |
SwishErrorsToStderr(); |
70 |
|
|
71 |
elog(INFO, "pgswish opening %s, query %s", |
elog(INFO, "pgswish: SwishInit(%s)", index_path); |
|
PG_GETARG_TEXT_P(0), |
|
|
PG_GETARG_TEXT_P(1) |
|
|
); |
|
72 |
|
|
73 |
swish_handle = SwishInit( (char *)PG_GETARG_TEXT_P(0) ); |
swish_handle = SwishInit( index_path ); |
74 |
|
|
75 |
if (! swish_handle) { |
if (! swish_handle) { |
76 |
elog(ERROR, "pgswish: can't open %s", PG_GETARG_TEXT_P(0)); |
elog(ERROR, "pgswish: can't open %s", index_path); |
77 |
SRF_RETURN_DONE(funcctx); |
SRF_RETURN_DONE(funcctx); |
78 |
} |
} |
79 |
|
|
85 |
if ( SwishError( swish_handle ) ) |
if ( SwishError( swish_handle ) ) |
86 |
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
87 |
|
|
88 |
|
elog(INFO, "pgswish: SwishQuery(%s)", query); |
89 |
/* Here's a short-cut to searching that creates a search object and searches at the same time */ |
/* Here's a short-cut to searching that creates a search object and searches at the same time */ |
90 |
results = SwishQuery( swish_handle, (char *)PG_GETARG_TEXT_P(1) ); |
elog(INFO,"## FIXME: SwishQuery kills back-end?"); |
91 |
|
results = SwishQuery( swish_handle, query); |
92 |
|
elog(INFO,"## FIXME: no..."); |
93 |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
94 |
|
|
95 |
/* total number of tuples to be returned */ |
/* total number of tuples to be returned */ |
97 |
|
|
98 |
/* check if results exists */ |
/* check if results exists */ |
99 |
if ( 0 == funcctx->max_calls ) |
if ( 0 == funcctx->max_calls ) |
100 |
elog(INFO, "no results for: %s", PG_GETARG_TEXT_P(1) ); |
elog(INFO, "no results for: %s", query ); |
101 |
|
|
102 |
|
elog(INFO, "pgswish: SwishHits = %d", funcctx->max_calls); |
103 |
|
|
104 |
/* Build a tuple description for a __pgswish tuple */ |
/* Build a tuple description for a __pgswish tuple */ |
105 |
tupdesc = RelationNameGetTupleDesc("__pgswish"); |
tupdesc = RelationNameGetTupleDesc("__pgswish"); |
139 |
* This should be an array of C strings which will |
* This should be an array of C strings which will |
140 |
* be processed later by the type input functions. |
* be processed later by the type input functions. |
141 |
*/ |
*/ |
142 |
values = (void **) palloc(5 * sizeof(void *)); |
values = (char **) palloc(5 * sizeof(char *)); |
143 |
values[0] = (long *) SwishResultPropertyULong ( result, "swishrank" ), |
values[0] = _textout( SwishResultPropertyULong ( result, "swishrank" ) ); |
144 |
values[1] = (char *) SwishResultPropertyStr ( result, "swishdocpath" ), |
values[1] = _textout( SwishResultPropertyStr ( result, "swishdocpath" ) ); |
145 |
values[2] = (char *) SwishResultPropertyStr ( result, "swishtitle" ), |
values[2] = _textout( SwishResultPropertyStr ( result, "swishtitle" ) ); |
146 |
values[3] = (long *) SwishResultPropertyStr ( result, "swishdocsize" ), |
values[3] = _textout( SwishResultPropertyStr ( result, "swishdocsize" ) ); |
147 |
values[4] = (char *) SwishResultPropertyStr ( result, "swishdbfile" ), |
values[4] = _textout( SwishResultPropertyStr ( result, "swishdbfile" ) ); |
148 |
|
|
149 |
/* build a tuple */ |
/* build a tuple */ |
150 |
tuple = BuildTupleFromCStrings(attinmeta, values); |
tuple = BuildTupleFromCStrings(attinmeta, values); |