1 |
|
2 |
/* |
3 |
-- This module does metaname handling for swish-e |
4 |
-- |
5 |
** This program and library is free software; you can redistribute it and/or |
6 |
** modify it under the terms of the GNU (Library) General Public License |
7 |
** as published by the Free Software Foundation; either version 2 |
8 |
** of the License, or 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 (Library) General Public License for more details. |
14 |
** |
15 |
** You should have received a copy of the GNU (Library) General Public License |
16 |
** along with this program; if not, write to the Free Software |
17 |
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
|
19 |
|
20 |
-- 2001-02-12 rasc minor changes, concering the tolower problem |
21 |
(unsigned char) problem!!! |
22 |
|
23 |
*/ |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
#include "swish.h" |
29 |
#include "mem.h" |
30 |
#include "merge.h" |
31 |
#include "string.h" |
32 |
#include "docprop.h" |
33 |
#include "metanames.h" |
34 |
#include "dump.h" |
35 |
#include "error.h" |
36 |
|
37 |
typedef struct |
38 |
{ |
39 |
char *metaName; |
40 |
int metaType; /* see metanames.h for values. All values must be "ored" */ |
41 |
} |
42 |
defaultMetaNames; |
43 |
|
44 |
|
45 |
/************************************************************************** |
46 |
* List of *internal* meta names |
47 |
* |
48 |
* Note: |
49 |
* Removing any of these will prevent access for result output |
50 |
* Removing any of the "real" meta names will also prevent storing |
51 |
* of the data in the property file. |
52 |
* That is, they may be commented out and then selected in the |
53 |
* configuration file as needed. |
54 |
* Hard to imagine not wanting the doc path! |
55 |
* |
56 |
***************************************************************************/ |
57 |
|
58 |
|
59 |
static defaultMetaNames SwishDefaultMetaNames[] = { |
60 |
|
61 |
/* This is the default meta ID ( number 1 ) that plain text is stored as */ |
62 |
{ AUTOPROPERTY_DEFAULT, META_INDEX }, /* REQUIRED */ |
63 |
|
64 |
|
65 |
/* These are the "internal" meta names generated at search time they are all required */ |
66 |
{ AUTOPROPERTY_REC_COUNT, META_PROP | META_INTERNAL | META_NUMBER }, |
67 |
{ AUTOPROPERTY_RESULT_RANK, META_PROP | META_INTERNAL | META_NUMBER }, |
68 |
{ AUTOPROPERTY_FILENUM, META_PROP | META_INTERNAL | META_NUMBER }, |
69 |
{ AUTOPROPERTY_INDEXFILE, META_PROP | META_INTERNAL | META_STRING }, |
70 |
|
71 |
/* These meta names "real" meta names that are available by default */ |
72 |
/* These can be commented out (e.g. to save disk space) and added back in with PropertyNames */ |
73 |
{ AUTOPROPERTY_DOCPATH, META_PROP | META_STRING }, |
74 |
{ AUTOPROPERTY_TITLE, META_PROP | META_STRING | META_IGNORE_CASE }, |
75 |
{ AUTOPROPERTY_DOCSIZE, META_PROP | META_NUMBER}, |
76 |
{ AUTOPROPERTY_LASTMODIFIED, META_PROP | META_DATE}, |
77 |
// { AUTOPROPERTY_SUMMARY, META_PROP | META_STRING}, |
78 |
// { AUTOPROPERTY_STARTPOS, META_PROP | META_NUMBER}, // should be added only if LST is selected |
79 |
}; |
80 |
|
81 |
/* Add the Internal swish metanames to the index file structure */ |
82 |
void add_default_metanames(IndexFILE * indexf) |
83 |
{ |
84 |
int i; |
85 |
|
86 |
for (i = 0; i < sizeof(SwishDefaultMetaNames) / sizeof(SwishDefaultMetaNames[0]); i++) |
87 |
addMetaEntry(&indexf->header, SwishDefaultMetaNames[i].metaName, SwishDefaultMetaNames[i].metaType, 0); |
88 |
} |
89 |
|
90 |
|
91 |
|
92 |
/************************************************************************** |
93 |
* These next routines add a new property/metaname to the list |
94 |
* |
95 |
* |
96 |
***************************************************************************/ |
97 |
|
98 |
|
99 |
|
100 |
/* Add an entry to the metaEntryArray if one doesn't already exist */ |
101 |
|
102 |
|
103 |
struct metaEntry *addMetaEntry(INDEXDATAHEADER *header, char *metaname, int metaType, int metaID) |
104 |
{ |
105 |
struct metaEntry *tmpEntry = NULL; |
106 |
char *metaWord; |
107 |
|
108 |
if (metaname == NULL || metaname[0] == '\0') |
109 |
progerr("internal error - called addMetaEntry without a name"); |
110 |
|
111 |
|
112 |
metaWord = estrdup( metaname ); |
113 |
strtolower(metaWord); |
114 |
|
115 |
|
116 |
/* See if there is a previous metaname with the same name */ |
117 |
// tmpEntry = metaType & META_PROP |
118 |
// ? getPropNameByName(header, metaWord) |
119 |
// : getMetaNameByName(header, metaWord); |
120 |
|
121 |
|
122 |
if (!tmpEntry) /* metaName not found - Create a new one */ |
123 |
tmpEntry = addNewMetaEntry( header, metaWord, metaType, metaID); |
124 |
|
125 |
else |
126 |
/* This allows adding Numeric or Date onto an existing property. */ |
127 |
/* Probably not needed */ |
128 |
tmpEntry->metaType |= metaType; |
129 |
|
130 |
|
131 |
efree( metaWord ); |
132 |
|
133 |
return tmpEntry; |
134 |
|
135 |
} |
136 |
|
137 |
static struct metaEntry *create_meta_entry( char *name ) |
138 |
{ |
139 |
struct metaEntry *newEntry = (struct metaEntry *) emalloc(sizeof(struct metaEntry)); |
140 |
|
141 |
memset(newEntry, 0, sizeof(struct metaEntry)); |
142 |
newEntry->metaName = (char *) estrdup( name ); |
143 |
return newEntry; |
144 |
} |
145 |
|
146 |
|
147 |
|
148 |
struct metaEntry *addNewMetaEntry(INDEXDATAHEADER *header, char *metaWord, int metaType, int metaID) |
149 |
{ |
150 |
int metaCounter = header->metaCounter; |
151 |
struct metaEntry *newEntry; |
152 |
struct metaEntry **metaEntryArray = header->metaEntryArray; |
153 |
newEntry = create_meta_entry( metaWord ); |
154 |
|
155 |
newEntry->metaType = metaType; |
156 |
|
157 |
/* If metaID is 0 asign a value using metaCounter */ |
158 |
/* Loaded stored metanames from index specifically sets the metaID */ |
159 |
|
160 |
newEntry->metaID = metaID ? metaID : metaCounter + 1; |
161 |
|
162 |
|
163 |
/* Create or enlarge the array, as needed */ |
164 |
if(! metaEntryArray) |
165 |
{ |
166 |
metaEntryArray = (struct metaEntry **) emalloc(sizeof(struct metaEntry *)); |
167 |
metaCounter = 0; |
168 |
} |
169 |
else |
170 |
metaEntryArray = (struct metaEntry **) erealloc(metaEntryArray,(metaCounter + 1) * sizeof(struct metaEntry *)); |
171 |
|
172 |
|
173 |
/* And save it in the array */ |
174 |
metaEntryArray[metaCounter++] = newEntry; |
175 |
|
176 |
/* Now update the header */ |
177 |
header->metaCounter = metaCounter; |
178 |
header->metaEntryArray = metaEntryArray; |
179 |
|
180 |
return newEntry; |
181 |
} |
182 |
|
183 |
/************************************************************************** |
184 |
* Clear in_tag flags on all metanames |
185 |
* The flags are used for indexing |
186 |
* |
187 |
***************************************************************************/ |
188 |
|
189 |
|
190 |
/** Lookup META_INDEX -- these only return meta names, not properties **/ |
191 |
|
192 |
void ClearInMetaFlags(INDEXDATAHEADER * header) |
193 |
{ |
194 |
int i; |
195 |
|
196 |
for (i = 0; i < header->metaCounter; i++) |
197 |
header->metaEntryArray[i]->in_tag = 0; |
198 |
} |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
/************************************************************************** |
204 |
* Initialize the property mapping array |
205 |
* Used to get the property seek pointers from the index file |
206 |
* |
207 |
* THIS IS TEMPORARY until I break up the metanames and properties |
208 |
* |
209 |
* This just creates two arrays to map metaIDs between property index numbers. |
210 |
* |
211 |
***************************************************************************/ |
212 |
|
213 |
void init_property_list(INDEXDATAHEADER *header) |
214 |
{ |
215 |
int i; |
216 |
|
217 |
/* only needs to be called one time */ |
218 |
if ( header->property_count ) |
219 |
return; |
220 |
|
221 |
if ( header->propIDX_to_metaID ) |
222 |
progerr("Called init_property_list with non-null header->propIDX_to_metaID"); |
223 |
|
224 |
if ( !header->metaCounter ) |
225 |
{ |
226 |
header->property_count = -1; |
227 |
return; |
228 |
} |
229 |
|
230 |
|
231 |
header->propIDX_to_metaID = emalloc( (1 + header->metaCounter) * sizeof( int ) ); |
232 |
header->metaID_to_PropIDX = emalloc( (1 + header->metaCounter) * sizeof( int ) ); |
233 |
|
234 |
for (i = 0; i < header->metaCounter; i++) |
235 |
{ |
236 |
if (is_meta_property(header->metaEntryArray[i]) && !header->metaEntryArray[i]->alias && !is_meta_internal(header->metaEntryArray[i]) ) |
237 |
{ |
238 |
header->metaID_to_PropIDX[header->metaEntryArray[i]->metaID] = header->property_count; |
239 |
header->propIDX_to_metaID[header->property_count++] = header->metaEntryArray[i]->metaID; |
240 |
} |
241 |
else |
242 |
header->metaID_to_PropIDX[header->metaEntryArray[i]->metaID] = -1; |
243 |
} |
244 |
|
245 |
if ( !header->property_count ) |
246 |
header->property_count = -1; |
247 |
} |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
/************************************************************************** |
253 |
* These routines lookup either a property or a metaname |
254 |
* by its ID or name |
255 |
* |
256 |
* The routines only look at either properites or metanames |
257 |
* |
258 |
* Note: probably could save a bit by just saying that if not META_PROP then |
259 |
* it's a a meta index entry. In otherwords, the type flag of zero could mean |
260 |
* META_INDEX, otherwise it's a PROPERTY. $$$ todo... |
261 |
* |
262 |
* |
263 |
***************************************************************************/ |
264 |
|
265 |
|
266 |
/** Lookup META_INDEX -- these only return meta names, not properties **/ |
267 |
|
268 |
struct metaEntry *getMetaNameByNameNoAlias(INDEXDATAHEADER * header, char *word) |
269 |
{ |
270 |
int i; |
271 |
|
272 |
for (i = 0; i < header->metaCounter; i++) |
273 |
if (is_meta_index(header->metaEntryArray[i]) && !strcasecmp(header->metaEntryArray[i]->metaName, word)) |
274 |
return header->metaEntryArray[i]; |
275 |
|
276 |
return NULL; |
277 |
} |
278 |
|
279 |
|
280 |
/* Returns the structure associated with the metaName if it exists |
281 |
* Requests for Aliased names returns the base meta entry, not the alias meta entry. |
282 |
* Note that on a alias it checks the *alias*'s type, so it must match. |
283 |
*/ |
284 |
|
285 |
struct metaEntry *getMetaNameByName(INDEXDATAHEADER * header, char *word) |
286 |
{ |
287 |
int i; |
288 |
|
289 |
for (i = 0; i < header->metaCounter; i++) |
290 |
if (is_meta_index(header->metaEntryArray[i]) && !strcasecmp(header->metaEntryArray[i]->metaName, word)) |
291 |
return header->metaEntryArray[i]->alias |
292 |
? getMetaNameByID( header, header->metaEntryArray[i]->alias ) |
293 |
: header->metaEntryArray[i]; |
294 |
|
295 |
return NULL; |
296 |
} |
297 |
|
298 |
|
299 |
/* Returns the structure associated with the metaName ID if it exists |
300 |
*/ |
301 |
|
302 |
struct metaEntry *getMetaNameByID(INDEXDATAHEADER *header, int number) |
303 |
{ |
304 |
int i; |
305 |
|
306 |
for (i = 0; i < header->metaCounter; i++) |
307 |
{ |
308 |
if (is_meta_index(header->metaEntryArray[i]) && number == header->metaEntryArray[i]->metaID) |
309 |
return header->metaEntryArray[i]; |
310 |
} |
311 |
return NULL; |
312 |
} |
313 |
|
314 |
|
315 |
|
316 |
/** Lookup META_PROP -- these only return properties **/ |
317 |
|
318 |
struct metaEntry *getPropNameByNameNoAlias(INDEXDATAHEADER * header, char *word) |
319 |
{ |
320 |
int i; |
321 |
|
322 |
for (i = 0; i < header->metaCounter; i++) |
323 |
if (is_meta_property(header->metaEntryArray[i]) && !strcasecmp(header->metaEntryArray[i]->metaName, word)) |
324 |
return header->metaEntryArray[i]; |
325 |
|
326 |
return NULL; |
327 |
} |
328 |
|
329 |
|
330 |
/* Returns the structure associated with the metaName if it exists |
331 |
* Requests for Aliased names returns the base meta entry, not the alias meta entry. |
332 |
* Note that on a alias it checks the *alias*'s type, so it must match. |
333 |
*/ |
334 |
|
335 |
struct metaEntry *getPropNameByName(INDEXDATAHEADER * header, char *word) |
336 |
{ |
337 |
int i; |
338 |
|
339 |
|
340 |
for (i = 0; i < header->metaCounter; i++) |
341 |
if (is_meta_property(header->metaEntryArray[i]) && !strcasecmp(header->metaEntryArray[i]->metaName, word)) |
342 |
return header->metaEntryArray[i]->alias |
343 |
? getPropNameByID( header, header->metaEntryArray[i]->alias ) |
344 |
: header->metaEntryArray[i]; |
345 |
|
346 |
return NULL; |
347 |
} |
348 |
|
349 |
|
350 |
/* Returns the structure associated with the metaName ID if it exists |
351 |
*/ |
352 |
|
353 |
struct metaEntry *getPropNameByID(INDEXDATAHEADER *header, int number) |
354 |
{ |
355 |
int i; |
356 |
|
357 |
for (i = 0; i < header->metaCounter; i++) |
358 |
{ |
359 |
if (is_meta_property(header->metaEntryArray[i]) && number == header->metaEntryArray[i]->metaID) |
360 |
return header->metaEntryArray[i]; |
361 |
} |
362 |
|
363 |
return NULL; |
364 |
} |
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
/* This is really used to check for seeing which internal metaname is being requested */ |
371 |
|
372 |
int is_meta_entry( struct metaEntry *meta_entry, char *name ) |
373 |
{ |
374 |
return strcasecmp( meta_entry->metaName, name ) == 0; |
375 |
} |
376 |
|
377 |
|
378 |
/************************************************************************** |
379 |
* Free list of MetaEntry's |
380 |
* |
381 |
***************************************************************************/ |
382 |
|
383 |
|
384 |
|
385 |
/* Free meta entries for an index file */ |
386 |
|
387 |
void freeMetaEntries( INDEXDATAHEADER *header ) |
388 |
{ |
389 |
int i; |
390 |
|
391 |
/* Make sure there are meta names assigned */ |
392 |
if ( !header->metaCounter ) |
393 |
return; |
394 |
|
395 |
|
396 |
/* should the elements be set to NULL? */ |
397 |
for( i = 0; i < header->metaCounter; i++ ) |
398 |
{ |
399 |
struct metaEntry *meta = header->metaEntryArray[i]; |
400 |
|
401 |
efree( meta->metaName ); |
402 |
|
403 |
#ifndef USE_BTREE |
404 |
if ( meta->sorted_data) |
405 |
efree( meta->sorted_data ); |
406 |
#endif |
407 |
|
408 |
if ( meta->inPropRange ) |
409 |
efree( meta->inPropRange); |
410 |
|
411 |
if ( meta->loPropRange ) |
412 |
freeProperty( meta->loPropRange ); |
413 |
|
414 |
if ( meta->hiPropRange ) |
415 |
freeProperty( meta->hiPropRange ); |
416 |
|
417 |
if ( meta->extractpath_default ) |
418 |
efree( meta->extractpath_default ); |
419 |
|
420 |
|
421 |
efree( meta ); |
422 |
} |
423 |
|
424 |
/* And free the pointer to the list */ |
425 |
efree( header->metaEntryArray); |
426 |
header->metaEntryArray = NULL; |
427 |
header->metaCounter = 0; |
428 |
} |
429 |
|
430 |
|
431 |
/************************************************************************** |
432 |
* Check if should bump word position on this meta name |
433 |
* |
434 |
***************************************************************************/ |
435 |
|
436 |
|
437 |
int isDontBumpMetaName( struct swline *tmplist, char *tag) |
438 |
{ |
439 |
char *tmptag; |
440 |
|
441 |
if (!tmplist) return 0; |
442 |
if (strcmp(tmplist->line,"*")==0) return 1; |
443 |
|
444 |
tmptag=estrdup(tag); |
445 |
tmptag=strtolower(tmptag); |
446 |
while(tmplist) |
447 |
{ |
448 |
|
449 |
if( strcasecmp(tmptag,tmplist->line)==0 ) |
450 |
{ |
451 |
efree(tmptag); |
452 |
return 1; |
453 |
} |
454 |
tmplist=tmplist->next; |
455 |
} |
456 |
efree(tmptag); |
457 |
return 0; |
458 |
|
459 |
} |
460 |
|
461 |
|
462 |
|