10 |
* NOTES: |
* NOTES: |
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 |
|
* - number of returned rows is set by PostgreSQL evaluator, see: |
14 |
|
* http://archives.postgresql.org/pgsql-hackers/2005-02/msg00546.php |
15 |
* |
* |
16 |
* Based on C example from PostgreSQL documentation and swish-e |
* Based on: |
17 |
* src/libtest.c, so licence is GPL |
* - C example from PostgreSQL documentation (BSD licence) |
18 |
|
* - swish-e example src/libtest.c (GPL) |
19 |
|
* - _textin/_textout from pgcurl.c (LGPL) |
20 |
|
* |
21 |
|
* This code is licenced under GPL |
22 |
*/ |
*/ |
23 |
|
|
24 |
#include "postgres.h" |
#include "postgres.h" |
25 |
#include "fmgr.h" |
#include "fmgr.h" |
26 |
#include "funcapi.h" |
#include "funcapi.h" |
27 |
|
#include "utils/builtins.h" |
28 |
#include <swish-e.h> |
#include <swish-e.h> |
29 |
|
|
30 |
|
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) |
31 |
|
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) |
32 |
|
|
33 |
|
|
34 |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
35 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
48 |
SW_HANDLE swish_handle = NULL; /* Database handle */ |
SW_HANDLE swish_handle = NULL; /* Database handle */ |
49 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
50 |
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
SW_RESULTS results = NULL; /* results handle -- holds list of results */ |
51 |
|
char *index_path; |
52 |
|
char *query; |
53 |
|
|
54 |
/* stuff done only on the first call of the function */ |
/* stuff done only on the first call of the function */ |
55 |
if (SRF_IS_FIRSTCALL()) { |
if (SRF_IS_FIRSTCALL()) { |
56 |
MemoryContext oldcontext; |
MemoryContext oldcontext; |
57 |
|
|
58 |
|
/* take arguments from function */ |
59 |
|
//index_path = _textout(PG_GETARG_TEXT_P(0)); |
60 |
|
index_path = _textout(PG_GETARG_TEXT_P(0)); |
61 |
|
query = _textout(PG_GETARG_TEXT_P(1)); |
62 |
|
|
63 |
/* create a function context for cross-call persistence */ |
/* create a function context for cross-call persistence */ |
64 |
funcctx = SRF_FIRSTCALL_INIT(); |
funcctx = SRF_FIRSTCALL_INIT(); |
65 |
|
|
66 |
/* switch to memory context appropriate for multiple function calls */ |
/* switch to memory context appropriate for multiple function calls */ |
67 |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
68 |
|
|
69 |
|
|
70 |
/* Send any errors or warnings to stderr (default is stdout) */ |
/* Send any errors or warnings to stderr (default is stdout) */ |
71 |
SwishErrorsToStderr(); |
SwishErrorsToStderr(); |
72 |
|
|
73 |
elog(INFO, "pgswish opening %s, query %s", |
elog(INFO, "pgswish: SwishInit(%s)", index_path); |
|
PG_GETARG_TEXT_P(0), |
|
|
PG_GETARG_TEXT_P(1) |
|
|
); |
|
74 |
|
|
75 |
swish_handle = SwishInit( (char *)PG_GETARG_TEXT_P(0) ); |
swish_handle = SwishInit( index_path ); |
76 |
|
|
77 |
if (! swish_handle) { |
if (! swish_handle) { |
78 |
elog(ERROR, "pgswish: can't open %s", PG_GETARG_TEXT_P(0)); |
elog(ERROR, "pgswish: can't open %s", index_path); |
79 |
SRF_RETURN_DONE(funcctx); |
SRF_RETURN_DONE(funcctx); |
80 |
} |
} |
81 |
|
|
82 |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
if ( SwishError( swish_handle ) ) error_or_abort( swish_handle ); |
83 |
/* set ranking scheme. default is 0 */ |
/* set ranking scheme. default is 0 */ |
84 |
SwishRankScheme( swish_handle, 1 ); |
SwishRankScheme( swish_handle, 0 ); |
85 |
|
|
86 |
/* Check for errors after every call */ |
/* Check for errors after every call */ |
87 |
if ( SwishError( swish_handle ) ) |
if ( SwishError( swish_handle ) ) |
88 |
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
error_or_abort( swish_handle ); /* print an error or abort -- see below */ |
89 |
|
|
90 |
|
elog(INFO, "pgswish: SwishQuery(%s)", query); |
91 |
/* 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 */ |
92 |
results = SwishQuery( swish_handle, (char *)PG_GETARG_TEXT_P(1) ); |
results = SwishQuery( swish_handle, query); |
|
|
|
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); |