1 | /*
|
---|
2 | * MGRetrieveWrapperImpl.c
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 |
|
---|
21 | #include <jni.h>
|
---|
22 | #ifdef __MINGW32__
|
---|
23 |
|
---|
24 | /* Cross compiling for Windows
|
---|
25 | Want the type definitions in *win32* version of jni_md.h but
|
---|
26 | this then leads to C-mangled style functions which we *don't*
|
---|
27 | want. The following achieves this */
|
---|
28 |
|
---|
29 | #undef JNIEXPORT
|
---|
30 | #undef JNIIMPORT
|
---|
31 | #undef JNICALL
|
---|
32 |
|
---|
33 | #define JNIEXPORT
|
---|
34 | #define JNIIMPORT
|
---|
35 | #define JNICALL
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include "MGWrapperImpl.h"
|
---|
39 |
|
---|
40 | #include "org_greenstone_mg_MGRetrieveWrapper.h"
|
---|
41 |
|
---|
42 | #include "backend.h"
|
---|
43 | #include "environment.h"
|
---|
44 | #include "text_get.h"
|
---|
45 | #include "stemmer.h"
|
---|
46 |
|
---|
47 | #define MAX_INDEXES_CACHED 3
|
---|
48 |
|
---|
49 | /* copied from mgquery, needed to convert paragraph numbers to document numbers
|
---|
50 | for greenstone */
|
---|
51 | #if defined(PARADOCNUM) || defined(NZDL)
|
---|
52 | static int GetDocNumFromParaNum(query_data *qd, int paranum) {
|
---|
53 | int Documents = qd->td->cth.num_of_docs;
|
---|
54 | int *Paragraph = qd->paragraph;
|
---|
55 | int low = 1, high = Documents;
|
---|
56 | int mid = (low+high)/2;
|
---|
57 |
|
---|
58 | while ((mid = (low+high)/2) >=1 && mid <= Documents)
|
---|
59 | {
|
---|
60 | if (paranum > Paragraph[mid])
|
---|
61 | low = mid+1;
|
---|
62 | else if (paranum <= Paragraph[mid-1])
|
---|
63 | high = mid-1;
|
---|
64 | else
|
---|
65 | return mid;
|
---|
66 | }
|
---|
67 | FatalError(1, "Bad paragraph number.\n");
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 | #endif
|
---|
71 |
|
---|
72 |
|
---|
73 | /*********************************************
|
---|
74 | initialisation stuff
|
---|
75 | *********************************************/
|
---|
76 |
|
---|
77 | /* cached ids for java stuff */
|
---|
78 | jfieldID FID_mg_data = NULL; /* MGWrapperData */
|
---|
79 | jfieldID FID_query_result = NULL; /* MGQueryResult */
|
---|
80 | jmethodID MID_addDoc = NULL; /* MGQueryResult.addDoc() */
|
---|
81 | jmethodID MID_addTerm = NULL; /* MGQueryResult.addTerm() */
|
---|
82 | jmethodID MID_addEquivTerm = NULL; /* MGQueryResult.addEquivTerm() */
|
---|
83 | jmethodID MID_setTotalDocs = NULL; /* MGQueryResult.setTotalDocs() */
|
---|
84 | jmethodID MID_clearResult = NULL; /* MGQueryResult.clear() */
|
---|
85 |
|
---|
86 |
|
---|
87 | /*******************************************
|
---|
88 | Index caching
|
---|
89 | *******************************************/
|
---|
90 |
|
---|
91 | query_data* cached_indexes[MAX_INDEXES_CACHED] = { NULL };
|
---|
92 |
|
---|
93 |
|
---|
94 | /* Get the index data necessary to perform a query or document retrieval */
|
---|
95 | query_data*
|
---|
96 | loadIndexData(char* base_dir, char* index_path, char* text_path)
|
---|
97 | {
|
---|
98 | char* index_path_name;
|
---|
99 | char* text_path_name;
|
---|
100 | query_data* qd;
|
---|
101 | int i = 0;
|
---|
102 |
|
---|
103 | /* Form the path name of the desired indexes */
|
---|
104 | index_path_name = (char*) malloc(strlen(base_dir) + strlen(index_path) + 1);
|
---|
105 | assert(index_path_name != NULL);
|
---|
106 | strcpy(index_path_name, base_dir);
|
---|
107 | strcat(index_path_name, index_path);
|
---|
108 | printf("Index pathname: %s\n", index_path_name);
|
---|
109 |
|
---|
110 | text_path_name = (char*) malloc(strlen(base_dir) + strlen(text_path) + 1);
|
---|
111 | assert(text_path_name != NULL);
|
---|
112 | strcpy(text_path_name, base_dir);
|
---|
113 | strcat(text_path_name, text_path);
|
---|
114 | printf("Text pathname: %s\n", text_path_name);
|
---|
115 |
|
---|
116 | /* Search through the cached indexes for the desired one */
|
---|
117 | while (i < MAX_INDEXES_CACHED && cached_indexes[i] != NULL) {
|
---|
118 | printf("(Cached) Pathname: %s\n", cached_indexes[i]->pathname);
|
---|
119 | printf("(Cached) Textpathname: %s\n", cached_indexes[i]->textpathname);
|
---|
120 |
|
---|
121 | /* Check if the index has already been loaded */
|
---|
122 | if ((strcmp(index_path_name, cached_indexes[i]->pathname) == 0) &&
|
---|
123 | (strcmp(text_path_name, cached_indexes[i]->textpathname) == 0)) {
|
---|
124 | /* Index has already been loaded and cached, so return it */
|
---|
125 | printf("Found index!\n");
|
---|
126 | free(index_path_name);
|
---|
127 | free(text_path_name);
|
---|
128 | return cached_indexes[i];
|
---|
129 | }
|
---|
130 |
|
---|
131 | i++;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* Text strings no longer needed */
|
---|
135 | free(index_path_name);
|
---|
136 | free(text_path_name);
|
---|
137 |
|
---|
138 | /* The index is not cached, so load it now */
|
---|
139 | qd = InitQuerySystem(base_dir, index_path, text_path, NULL);
|
---|
140 | if (!qd) {
|
---|
141 | printf("Error: Could not InitQuerySystem()...\n");
|
---|
142 | return NULL;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* The index loaded OK, so cache it */
|
---|
146 | /* This could be a little more sophisticated, eg. replace least frequently used index */
|
---|
147 | if (i >= MAX_INDEXES_CACHED)
|
---|
148 | i = MAX_INDEXES_CACHED - 1;
|
---|
149 |
|
---|
150 | /* Free the index being replaced */
|
---|
151 | if (cached_indexes[i] != NULL)
|
---|
152 | FinishQuerySystem(cached_indexes[i]);
|
---|
153 |
|
---|
154 | /* Cache the loaded index, and return it */
|
---|
155 | cached_indexes[i] = qd;
|
---|
156 | return cached_indexes[i];
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Class: org_greenstone_mg_MGRetrieveWrapper
|
---|
161 | * Method: initIDs
|
---|
162 | * Signature: ()V
|
---|
163 | */
|
---|
164 | JNIEXPORT void JNICALL
|
---|
165 | Java_org_greenstone_mg_MGRetrieveWrapper_initIDs(JNIEnv *j_env, jclass j_cls)
|
---|
166 | {
|
---|
167 | /* a long-"J" */
|
---|
168 | FID_mg_data = (*j_env)->GetFieldID(j_env, j_cls, "mg_data_ptr_", "J");
|
---|
169 | assert(FID_mg_data != NULL);
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Class: org_greenstone_mg_MGRetrieveWrapper
|
---|
175 | * Method: initCSide
|
---|
176 | * Signature: ()Z
|
---|
177 | */
|
---|
178 | JNIEXPORT jboolean JNICALL Java_org_greenstone_mg_MGRetrieveWrapper_initCSide
|
---|
179 | (JNIEnv *j_env, jobject j_obj)
|
---|
180 | {
|
---|
181 | /* Allocate a MGWrapperData object to store query parameters */
|
---|
182 | MGWrapperData* data = (MGWrapperData*) malloc(sizeof(MGWrapperData));
|
---|
183 | assert(data != NULL);
|
---|
184 |
|
---|
185 | /* Set default values - no stemming, no case-folding, boolean OR queries */
|
---|
186 | data->defaultStemMethod = 0;
|
---|
187 | data->defaultBoolCombine = 0;
|
---|
188 |
|
---|
189 | /* Allocate a QueryInfo object to store more query parameters */
|
---|
190 | data->queryInfo = (QueryInfo*) malloc(sizeof(QueryInfo));
|
---|
191 | assert(data->queryInfo != NULL);
|
---|
192 |
|
---|
193 | /* Set default values - 50 documents max, return term freqs, sort by rank */
|
---|
194 | data->queryInfo->index = NULL;
|
---|
195 | data->queryInfo->maxDocs = 50;
|
---|
196 | data->queryInfo->needTermFreqs = 1;
|
---|
197 |
|
---|
198 | /* Save the object on the Java side */
|
---|
199 | (*j_env)->SetLongField(j_env, j_obj, FID_mg_data, (long) data);
|
---|
200 |
|
---|
201 | /* Initialise MG environment variables */
|
---|
202 | InitEnv();
|
---|
203 | SetEnv("expert", "true", NULL);
|
---|
204 | SetEnv("mode", "docnums", NULL);
|
---|
205 |
|
---|
206 | return 1; /* true - no errors */
|
---|
207 | }
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * Class: org_greenstone_mg_MGRetrieveWrapper
|
---|
211 | * Method: unloadIndexData
|
---|
212 | * Signature: ()Z
|
---|
213 | */
|
---|
214 | JNIEXPORT jboolean JNICALL Java_org_greenstone_mg_MGRetrieveWrapper_unloadIndexData
|
---|
215 | (JNIEnv* j_env, jobject j_obj)
|
---|
216 | {
|
---|
217 | /* Free all the loaded indexes */
|
---|
218 | int i = 0;
|
---|
219 | while (i < MAX_INDEXES_CACHED && cached_indexes[i] != NULL) {
|
---|
220 | FinishQuerySystem(cached_indexes[i]);
|
---|
221 | cached_indexes[i] = NULL;
|
---|
222 | i++;
|
---|
223 | }
|
---|
224 |
|
---|
225 | return 1; /* true - no errors */
|
---|
226 | }
|
---|
227 | /* Choose MG index to search */
|
---|
228 | JNIEXPORT void JNICALL
|
---|
229 | Java_org_greenstone_mg_MGRetrieveWrapper_setIndex(JNIEnv *j_env, jobject j_obj,
|
---|
230 | jstring j_index)
|
---|
231 | {
|
---|
232 | MGWrapperData* data = (MGWrapperData*)(intptr_t) (*j_env)->GetLongField(j_env, j_obj, FID_mg_data);
|
---|
233 |
|
---|
234 | /* Get the index name as a C string */
|
---|
235 | const char* index = (*j_env)->GetStringUTFChars(j_env, j_index, NULL);
|
---|
236 | assert(index != NULL);
|
---|
237 | printf("Choosing index %s...\n", index);
|
---|
238 |
|
---|
239 | /* Free the previous index name */
|
---|
240 | if (data->queryInfo->index)
|
---|
241 | free(data->queryInfo->index);
|
---|
242 |
|
---|
243 | /* Allocate memory for the index name, and fill it */
|
---|
244 | data->queryInfo->index = (char*) malloc(strlen(index) + 1);
|
---|
245 | assert(data->queryInfo->index != NULL);
|
---|
246 | strcpy(data->queryInfo->index, index);
|
---|
247 |
|
---|
248 | /* Release the index string */
|
---|
249 | (*j_env)->ReleaseStringUTFChars(j_env, j_index, index);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * Class: org_greenstone_mg_MGRetrieveWrapper
|
---|
254 | * Method: getDocument
|
---|
255 | * Signature: (Ljava/lang/String;Ljava/lang/String;J)Ljava/lang/String;
|
---|
256 | */
|
---|
257 | JNIEXPORT jstring JNICALL Java_org_greenstone_mg_MGRetrieveWrapper_getDocument
|
---|
258 | (JNIEnv *j_env, jobject j_obj,
|
---|
259 | jstring j_base_dir, jstring j_text_path,
|
---|
260 | jlong j_docnum)
|
---|
261 | {
|
---|
262 | MGWrapperData* data = (MGWrapperData*)(intptr_t) (*j_env)->GetLongField(j_env, j_obj, FID_mg_data);
|
---|
263 |
|
---|
264 | char* index_path;
|
---|
265 | const char* base_dir;
|
---|
266 | const char* text_path;
|
---|
267 | query_data* qd;
|
---|
268 |
|
---|
269 | mg_u_long pos, len;
|
---|
270 | u_char* c_buffer = NULL;
|
---|
271 | u_char* uc_buffer = NULL;
|
---|
272 | int ULen;
|
---|
273 |
|
---|
274 | jstring result;
|
---|
275 |
|
---|
276 | /* Make sure an index has been specified */
|
---|
277 | index_path = data->queryInfo->index;
|
---|
278 | assert(index_path != NULL);
|
---|
279 |
|
---|
280 | /* Obtain C versions of the two string parameters */
|
---|
281 | base_dir = (*j_env)->GetStringUTFChars(j_env, j_base_dir, NULL);
|
---|
282 | if (base_dir == NULL) {
|
---|
283 | return NULL;
|
---|
284 | }
|
---|
285 | text_path = (*j_env)->GetStringUTFChars(j_env, j_text_path, NULL);
|
---|
286 | if (text_path == NULL) {
|
---|
287 | (*j_env)->ReleaseStringUTFChars(j_env, j_base_dir, base_dir);
|
---|
288 | return NULL;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /* Load the appropriate index for satisfying this request */
|
---|
292 | printf("Document retrieval, index path: %s\n", index_path);
|
---|
293 | qd = loadIndexData((char*) base_dir, (char*) index_path, (char*) text_path);
|
---|
294 |
|
---|
295 | /* The C text strings are no longer needed */
|
---|
296 | (*j_env)->ReleaseStringUTFChars(j_env, j_base_dir, base_dir);
|
---|
297 | (*j_env)->ReleaseStringUTFChars(j_env, j_text_path, text_path);
|
---|
298 |
|
---|
299 | /* Check that the index was loaded successfully */
|
---|
300 | if (qd==NULL) {
|
---|
301 | return NULL;
|
---|
302 | }
|
---|
303 | /*assert(qd != NULL);*/
|
---|
304 |
|
---|
305 | /* Get the document position and length in the text file */
|
---|
306 | printf("Fetching document number %d...\n", (mg_u_long) j_docnum);
|
---|
307 | FetchDocStart(qd, (mg_u_long) j_docnum, &pos, &len);
|
---|
308 | printf("Fetched document start. Pos: %d, Len: %d\n", pos, len);
|
---|
309 |
|
---|
310 | /* Allocate memory for the document text (from mg/src/text/mgquery.c:RawDocOutput()) */
|
---|
311 | c_buffer = (u_char*) malloc(len);
|
---|
312 | assert(c_buffer != NULL);
|
---|
313 | uc_buffer = (u_char*) malloc((int) (qd->td->cth.ratio * 1.01 * len) + 100);
|
---|
314 | assert(uc_buffer != NULL);
|
---|
315 |
|
---|
316 | /* Seek to the correct position in the file and read the document text */
|
---|
317 | Fseek (qd->td->TextFile, pos, 0);
|
---|
318 | Fread (c_buffer, 1, len, qd->td->TextFile);
|
---|
319 |
|
---|
320 | /* Decompress the document text into another buffer, and terminate it */
|
---|
321 | DecodeText (qd->cd, c_buffer, len, uc_buffer, &ULen);
|
---|
322 | uc_buffer[ULen] = '\0';
|
---|
323 |
|
---|
324 | /* Load the document text into a Java string */
|
---|
325 | result = (*j_env)->NewStringUTF(j_env, uc_buffer);
|
---|
326 | assert(result != NULL);
|
---|
327 |
|
---|
328 | /* Free C buffers */
|
---|
329 | free(c_buffer);
|
---|
330 | free(uc_buffer);
|
---|
331 |
|
---|
332 | /* Return the document text */
|
---|
333 | return result;
|
---|
334 | }
|
---|
335 |
|
---|