source: main/trunk/greenstone2/common-src/indexers/mg/jni/MGRetrieveWrapperImpl.c@ 37383

Last change on this file since 37383 was 37383, checked in by davidb, 14 months ago

Mac compiler is stricter about about function externs

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