2 * Copyright (C) 1994-2001, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.108 2001-06-14 11:44:56 adam
8 * Bug fix: default storeKeys setting wasn't read when group was specified.
10 * Revision 1.107 2001/05/28 13:58:48 adam
11 * Call flushSortKeys when record is skipped to fix bad re-use of
12 * sort keys to whatever next record that comes in.
14 * Revision 1.106 2000/12/05 12:22:53 adam
15 * Termlist source implemented (so that we can index values of XML/SGML
18 * Revision 1.105 2000/12/05 10:01:44 adam
19 * Fixed bug regarding user-defined attribute sets.
21 * Revision 1.104 2000/09/05 14:04:05 adam
22 * Updates for prefix 'yaz_' for YAZ log functions.
24 * Revision 1.103 2000/05/18 12:01:36 adam
25 * System call times(2) used again. More 64-bit fixes.
27 * Revision 1.102 2000/05/15 15:32:33 adam
28 * Added 64 bit file input.
30 * Revision 1.101 2000/05/15 13:02:39 adam
33 * Revision 1.100 2000/03/20 19:08:36 adam
34 * Added remote record import using Z39.50 extended services and Segment
37 * Revision 1.99 2000/02/24 10:57:02 adam
38 * Sequence number incremented after each incomplete-field.
40 * Revision 1.98 1999/09/07 07:19:21 adam
41 * Work on character mapping. Implemented replace rules.
43 * Revision 1.97 1999/07/06 12:28:04 adam
44 * Updated record index structure. Format includes version ID. Compression
45 * algorithm ID is stored for each record block.
47 * Revision 1.96 1999/05/26 07:49:13 adam
50 * Revision 1.95 1999/05/21 12:00:17 adam
51 * Better diagnostics for extraction process.
53 * Revision 1.94 1999/05/20 12:57:18 adam
54 * Implemented TCL filter. Updated recctrl system.
56 * Revision 1.93 1999/05/15 14:36:38 adam
57 * Updated dictionary. Implemented "compression" of dictionary.
59 * Revision 1.92 1999/03/09 16:27:49 adam
60 * More work on SDRKit integration.
62 * Revision 1.91 1999/02/12 13:29:22 adam
63 * Implemented position-flag for registers.
65 * Revision 1.90 1999/02/02 14:50:52 adam
66 * Updated WIN32 code specific sections. Changed header.
68 * Revision 1.89 1998/10/28 10:54:38 adam
71 * Revision 1.88 1998/10/16 08:14:29 adam
72 * Updated record control system.
74 * Revision 1.87 1998/10/15 13:10:33 adam
75 * Fixed bug in Zebra that caused it to stop indexing when empty
78 * Revision 1.86 1998/10/13 20:33:53 adam
79 * Fixed one log message and change use ordinal to be an unsigned char.
81 * Revision 1.85 1998/09/22 10:03:41 adam
82 * Changed result sets to be persistent in the sense that they can
83 * be re-searched if needed.
84 * Fixed memory leak in rsm_or.
86 * Revision 1.84 1998/06/11 15:42:22 adam
87 * Changed the way use attributes are specified in the recordId
90 * Revision 1.83 1998/06/08 14:43:10 adam
91 * Added suport for EXPLAIN Proxy servers - added settings databasePath
92 * and explainDatabase to facilitate this. Increased maximum number
93 * of databases and attributes in one register.
95 * Revision 1.82 1998/05/20 10:12:15 adam
96 * Implemented automatic EXPLAIN database maintenance.
97 * Modified Zebra to work with ASN.1 compiled version of YAZ.
99 * Revision 1.81 1998/03/11 11:19:04 adam
100 * Changed the way sequence numbers are generated.
102 * Revision 1.80 1998/03/05 08:45:11 adam
103 * New result set model and modular ranking system. Moved towards
104 * descent server API. System information stored as "SGML" records.
106 * Revision 1.79 1998/02/17 10:32:52 adam
107 * Fixed bug: binary files weren't opened with flag b on NT.
109 * Revision 1.78 1998/02/10 12:03:05 adam
112 * Revision 1.77 1998/01/12 15:04:08 adam
113 * The test option (-s) only uses read-lock (and not write lock).
115 * Revision 1.76 1997/10/27 14:33:04 adam
116 * Moved towards generic character mapping depending on "structure"
117 * field in abstract syntax file. Fixed a few memory leaks. Fixed
118 * bug with negative integers when doing searches with relational
121 * Revision 1.75 1997/09/17 12:19:12 adam
122 * Zebra version corresponds to YAZ version 1.4.
123 * Changed Zebra server so that it doesn't depend on global common_resource.
125 * Revision 1.74 1997/09/09 13:38:06 adam
126 * Partial port to WIN95/NT.
128 * Revision 1.73 1997/09/04 13:57:20 adam
129 * New file extract/retrieve method tellf (added).
130 * Added O_BINARY for open calls.
132 * Revision 1.72 1997/07/15 16:32:29 adam
133 * Bug fix: Match handler didn't terminate the resulting string!
135 * Revision 1.71 1997/07/15 16:28:41 adam
136 * Bug fix: storeData didn't work with files with multiple records.
137 * Bug fix: fixed memory management with records; not really well
140 * Revision 1.70 1997/07/01 13:00:42 adam
141 * Bug fix in routine searchRecordKey: uninitialized variables.
143 * Revision 1.69 1997/04/29 09:26:03 adam
144 * Bug fix: generic recordId handling didn't work for compressed internal
147 * Revision 1.68 1997/02/12 20:39:45 adam
148 * Implemented options -f <n> that limits the log to the first <n>
150 * Changed some log messages also.
152 * Revision 1.67 1996/11/15 15:02:14 adam
153 * Minor changes regarding logging.
155 * Revision 1.66 1996/11/14 09:52:21 adam
156 * Strings in record keys bound by IT_MAX_WORD.
158 * Revision 1.65 1996/11/14 08:57:56 adam
159 * Reduction of storeKeys area.
161 * Revision 1.64 1996/11/08 11:10:16 adam
162 * Buffers used during file match got bigger.
163 * Compressed ISAM support everywhere.
164 * Bug fixes regarding masking characters in queries.
165 * Redesigned Regexp-2 queries.
167 * Revision 1.63 1996/10/29 14:09:39 adam
168 * Use of cisam system - enabled if setting isamc is 1.
170 * Revision 1.62 1996/10/11 10:57:01 adam
171 * New module recctrl. Used to manage records (extract/retrieval).
172 * Several files have been moved to the recctrl sub directory.
174 * Revision 1.61 1996/06/06 12:08:37 quinn
175 * Added showRecord function
177 * Revision 1.60 1996/06/04 10:18:12 adam
178 * Search/scan uses character mapping module.
180 * Revision 1.59 1996/05/14 15:47:07 adam
181 * Cleanup of various buffer size entities.
183 * Revision 1.58 1996/05/14 06:16:38 adam
184 * Compact use/set bytes used in search service.
186 * Revision 1.57 1996/05/13 14:23:04 adam
187 * Work on compaction of set/use bytes in dictionary.
189 * Revision 1.56 1996/05/09 09:54:42 adam
190 * Server supports maps from one logical attributes to a list of physical
192 * The extraction process doesn't make space consuming 'any' keys.
194 * Revision 1.55 1996/05/09 07:28:55 quinn
195 * Work towards phrases and multiple registers
197 * Revision 1.54 1996/05/01 13:46:35 adam
198 * First work on multiple records in one file.
199 * New option, -offset, to the "unread" command in the filter module.
201 * Revision 1.53 1996/04/26 12:09:43 adam
202 * Added a few comments.
204 * Revision 1.52 1996/04/25 13:27:57 adam
205 * Function recordExtract modified so that files with no keys (possibly empty)
208 * Revision 1.51 1996/03/19 11:08:42 adam
209 * Bug fix: Log preamble wasn't always turned off after recordExtract.
211 * Revision 1.50 1996/02/12 18:45:36 adam
212 * New fileVerboseFlag in record group control.
214 * Revision 1.49 1996/02/05 12:29:57 adam
215 * Logging reduced a bit.
216 * The remaining running time is estimated during register merge.
218 * Revision 1.48 1996/02/01 20:53:26 adam
219 * The temporary per-record keys are compacted a little, and duplication
220 * of the per-records keys are avoided when they are saved in the record
221 * information buffer.
223 * Revision 1.47 1996/01/17 14:57:48 adam
224 * Prototype changed for reader functions in extract/retrieve. File
225 * is identified by 'void *' instead of 'int.
227 * Revision 1.46 1995/12/15 14:57:16 adam
230 * Revision 1.45 1995/12/15 12:37:41 adam
231 * In addRecordKeyAny: Writes key only when attrSet != -1.
233 * Revision 1.44 1995/12/12 16:00:54 adam
234 * System call sync(2) used after update/commit.
235 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
238 * Revision 1.43 1995/12/11 09:12:46 adam
239 * The rec_get function returns NULL if record doesn't exist - will
240 * happen in the server if the result set records have been deleted since
241 * the creation of the set (i.e. the search).
242 * The server saves a result temporarily if it is 'volatile', i.e. the
243 * set is register dependent.
245 * Revision 1.42 1995/12/07 17:38:46 adam
246 * Work locking mechanisms for concurrent updates/commit.
248 * Revision 1.41 1995/12/06 16:06:42 adam
249 * Better diagnostics. Work on 'real' dictionary deletion.
251 * Revision 1.40 1995/12/05 16:57:40 adam
252 * More work on regular patterns.
254 * Revision 1.39 1995/12/05 13:20:18 adam
255 * Bug fix: file_read sometimes returned early EOF.
257 * Revision 1.38 1995/12/04 17:59:21 adam
258 * More work on regular expression conversion.
260 * Revision 1.37 1995/12/04 14:22:27 adam
261 * Extra arg to recType_byName.
262 * Started work on new regular expression parsed input to
263 * structured records.
265 * Revision 1.36 1995/11/30 08:34:29 adam
266 * Started work on commit facility.
267 * Changed a few malloc/free to xmalloc/xfree.
269 * Revision 1.35 1995/11/28 14:26:21 adam
270 * Bug fix: recordId with constant wasn't right.
271 * Bug fix: recordId dictionary entry wasn't deleted when needed.
273 * Revision 1.34 1995/11/28 09:09:38 adam
274 * Zebra config renamed.
275 * Use setting 'recordId' to identify record now.
276 * Bug fix in recindex.c: rec_release_blocks was invokeded even
277 * though the blocks were already released.
278 * File traversal properly deletes records when needed.
280 * Revision 1.33 1995/11/27 09:56:20 adam
281 * Record info elements better enumerated. Internal store of records.
283 * Revision 1.32 1995/11/25 10:24:05 adam
284 * More record fields - they are enumerated now.
285 * New options: flagStoreData flagStoreKey.
287 * Revision 1.31 1995/11/24 11:31:35 adam
288 * Commands add & del read filenames from stdin if source directory is
290 * Match criteria supports 'constant' strings.
292 * Revision 1.30 1995/11/22 17:19:16 adam
293 * Record management uses the bfile system.
295 * Revision 1.29 1995/11/21 15:01:14 adam
296 * New general match criteria implemented.
297 * New feature: document groups.
299 * Revision 1.28 1995/11/21 09:20:30 adam
300 * Yet more work on record match.
302 * Revision 1.27 1995/11/20 16:59:45 adam
303 * New update method: the 'old' keys are saved for each records.
305 * Revision 1.26 1995/11/20 11:56:24 adam
306 * Work on new traversal.
308 * Revision 1.25 1995/11/16 15:34:54 adam
309 * Uses new record management system in both indexer and server.
311 * Revision 1.24 1995/11/15 19:13:08 adam
312 * Work on record management.
314 * Revision 1.23 1995/10/27 14:00:10 adam
315 * Implemented detection of database availability.
317 * Revision 1.22 1995/10/17 18:02:07 adam
318 * New feature: databases. Implemented as prefix to words in dictionary.
320 * Revision 1.21 1995/10/10 12:24:38 adam
321 * Temporary sort files are compressed.
323 * Revision 1.20 1995/10/06 13:52:05 adam
324 * Bug fixes. Handler may abort further scanning.
326 * Revision 1.19 1995/10/04 12:55:16 adam
327 * Bug fix in ranked search. Use=Any keys inserted.
329 * Revision 1.18 1995/10/04 09:37:08 quinn
332 * Revision 1.17 1995/10/03 14:28:57 adam
333 * Buffered read in extract works.
335 * Revision 1.16 1995/10/03 14:28:45 adam
336 * Work on more effecient read handler in extract.
338 * Revision 1.15 1995/10/02 15:42:53 adam
339 * Extract uses file descriptors instead of FILE pointers.
341 * Revision 1.14 1995/10/02 15:29:13 adam
342 * More logging in file_extract.
344 * Revision 1.13 1995/09/29 14:01:39 adam
347 * Revision 1.12 1995/09/28 14:22:56 adam
348 * Sort uses smaller temporary files.
350 * Revision 1.11 1995/09/28 12:10:31 adam
351 * Bug fixes. Field prefix used in queries.
353 * Revision 1.10 1995/09/28 09:19:41 adam
354 * xfree/xmalloc used everywhere.
355 * Extract/retrieve method seems to work for text records.
357 * Revision 1.9 1995/09/27 12:22:28 adam
358 * More work on extract in record control.
359 * Field name is not in isam keys but in prefix in dictionary words.
361 * Revision 1.8 1995/09/14 07:48:22 adam
362 * Record control management.
364 * Revision 1.7 1995/09/11 13:09:32 adam
365 * More work on relevance feedback.
367 * Revision 1.6 1995/09/08 14:52:27 adam
368 * Minor changes. Dictionary is lower case now.
370 * Revision 1.5 1995/09/06 16:11:16 adam
371 * Option: only one word key per file.
373 * Revision 1.4 1995/09/05 15:28:39 adam
374 * More work on search engine.
376 * Revision 1.3 1995/09/04 12:33:41 adam
377 * Various cleanup. YAZ util used instead.
379 * Revision 1.2 1995/09/04 09:10:34 adam
380 * More work on index add/del/update.
381 * Merge sort implemented.
382 * Initial work on z39 server.
384 * Revision 1.1 1995/09/01 14:06:35 adam
385 * Split of work into more files.
404 #if _FILE_OFFSET_BITS == 64
405 #define PRINTF_OFF_T "%Ld"
407 #define PRINTF_OFF_T "%ld"
415 #include "zebrasdr.h"
418 static Dict matchDict;
420 static Records records = NULL;
421 static SortIdx sortIdx = NULL;
423 static char **key_buf;
424 static size_t ptr_top;
426 static size_t key_buf_used;
427 static int key_file_no;
429 static int records_inserted = 0;
430 static int records_updated = 0;
431 static int records_deleted = 0;
432 static int records_processed = 0;
434 static ZebraExplainInfo zti = NULL;
437 static void logRecord (int showFlag)
441 if (showFlag || !(records_processed % 1000))
443 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
444 records_processed, records_inserted, records_updated,
449 static int explain_extract (void *handle, Record drec, data1_node *n);
451 int key_open (struct recordGroup *rGroup, int mem)
453 BFiles bfs = rGroup->bfs;
454 int rw = rGroup->flagRw;
455 data1_handle dh = rGroup->dh;
456 char *recordCompression;
457 int record_compression = REC_COMPRESS_NONE;
459 mem = atoi(res_get_def (common_resource, "memMax", "16"))*1024*1024;
462 key_buf = (char **) xmalloc (mem);
463 ptr_top = mem/sizeof(char*);
469 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
471 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
475 recordCompression = res_get_def (common_resource,
476 "recordCompression", "none");
477 if (!strcmp (recordCompression, "none"))
478 record_compression = REC_COMPRESS_NONE;
479 if (!strcmp (recordCompression, "bzip2"))
480 record_compression = REC_COMPRESS_BZIP2;
481 records = rec_open (bfs, rw, record_compression);
484 dict_close (matchDict);
487 zti = zebraExplain_open (records, dh, common_resource,
488 rw, rGroup, explain_extract);
491 rec_close (&records);
492 dict_close (matchDict);
495 sortIdx = sortIdx_open (bfs, 1);
506 void encode_key_init (struct encode_info *i)
513 char *encode_key_int (int d, char *bp)
522 else if (d <= 4194303)
524 *bp++ = 128 + (d>>16);
525 *bp++ = (d>>8) & 255;
530 *bp++ = 192 + (d>>24);
531 *bp++ = (d>>16) & 255;
532 *bp++ = (d>>8) & 255;
538 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
543 while ((*bp++ = *k++))
545 memcpy (&key, k+1, sizeof(struct it_key));
546 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
547 if (i->sysno != key.sysno)
549 i->sysno = key.sysno;
552 else if (!i->seqno && !key.seqno && i->cmd == *k)
554 bp = encode_key_int (key.seqno - i->seqno, bp);
555 i->seqno = key.seqno;
557 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
559 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
567 static int key_y_len;
569 static int key_y_compare (const void *p1, const void *p2)
573 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
574 *(char**) p2 + key_y_len + 1)))
576 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
579 static int key_x_compare (const void *p1, const void *p2)
581 return strcmp (*(char**) p1, *(char**) p2);
585 void key_flush (void)
590 struct encode_info encode_info;
599 logf (LOG_LOG, "sorting section %d", key_file_no);
601 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
602 getFnameTmp (common_resource, out_fname, key_file_no);
604 if (!(outf = fopen (out_fname, "wb")))
606 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
609 logf (LOG_LOG, "writing section %d", key_file_no);
610 prevcp = cp = key_buf[ptr_top-ptr_i];
612 encode_key_init (&encode_info);
613 encode_key_write (cp, &encode_info, outf);
616 cp = key_buf[ptr_top-ptr_i];
617 if (strcmp (cp, prevcp))
619 encode_key_init (&encode_info);
620 encode_key_write (cp, &encode_info, outf);
624 encode_key_write (cp + strlen(cp), &encode_info, outf);
627 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
628 getFnameTmp (out_fname, key_file_no);
630 if (!(outf = fopen (out_fname, "wb")))
632 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
635 logf (LOG_LOG, "writing section %d", key_file_no);
637 prevcp = key_buf[ptr_top-i];
639 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
641 key_y_len = strlen(prevcp)+1;
643 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
644 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
646 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
647 sizeof(char*), key_y_compare);
648 cp = key_buf[ptr_top-ptr_i];
650 encode_key_init (&encode_info);
651 encode_key_write (cp, &encode_info, outf);
654 cp = key_buf[ptr_top-ptr_i];
655 encode_key_write (cp+key_y_len, &encode_info, outf);
659 prevcp = key_buf[ptr_top-ptr_i];
664 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
667 logf (LOG_LOG, "finished section %d", key_file_no);
672 int key_close (struct recordGroup *rGroup)
674 int rw = rGroup->flagRw;
676 zebraExplain_runNumberIncrement (zti, 1);
677 zebraExplain_close (zti, rw);
680 rec_close (&records);
681 dict_close (matchDict);
682 sortIdx_close (sortIdx);
688 static void wordInit (struct recExtractCtrl *p, RecWord *w)
690 w->zebra_maps = p->zebra_maps;
691 w->seqnos = p->seqno;
692 w->attrSet = VAL_BIB1;
698 static struct sortKey {
703 struct sortKey *next;
706 static struct recKeys {
715 static void addIndexString (RecWord *p, const char *string, int length)
718 unsigned char attrSet;
719 unsigned short attrUse;
722 int *pseqno = &p->seqnos[p->reg_type];
724 if (reckeys.buf_used+1024 > reckeys.buf_max)
728 b = (char *) xmalloc (reckeys.buf_max += 128000);
729 if (reckeys.buf_used > 0)
730 memcpy (b, reckeys.buf, reckeys.buf_used);
734 dst = reckeys.buf + reckeys.buf_used;
736 attrSet = p->attrSet;
737 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
740 reckeys.prevAttrSet = attrSet;
741 attrUse = p->attrUse;
742 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
745 reckeys.prevAttrUse = attrUse;
747 diff = 1 + *pseqno - reckeys.prevSeqNo;
748 if (diff >= 1 && diff <= 15)
753 reckeys.prevSeqNo = *pseqno;
760 int ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
763 ch = zebraExplain_addSU (zti, attrSet, attrUse);
766 memcpy (dst, &ch, sizeof(ch));
772 memcpy (dst, &attrSet, sizeof(attrSet));
773 dst += sizeof(attrSet);
777 memcpy (dst, &attrUse, sizeof(attrUse));
778 dst += sizeof(attrUse);
781 *dst++ = p->reg_type;
782 memcpy (dst, string, length);
788 memcpy (dst, pseqno, sizeof(*pseqno));
789 dst += sizeof(*pseqno);
791 reckeys.buf_used = dst - reckeys.buf;
796 static void addSortString (RecWord *p, const char *string, int length)
800 for (sk = sortKeys; sk; sk = sk->next)
801 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
804 sk = (struct sortKey *) xmalloc (sizeof(*sk));
808 sk->string = (char *) xmalloc (length);
810 memcpy (sk->string, string, length);
812 sk->attrSet = p->attrSet;
813 sk->attrUse = p->attrUse;
816 static void addString (RecWord *p, const char *string, int length)
819 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
820 addSortString (p, string, length);
822 addIndexString (p, string, length);
825 static void addIncompleteField (RecWord *p)
827 const char *b = p->string;
828 int remain = p->length;
829 const char **map = 0;
832 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
836 char buf[IT_MAX_WORD+1];
840 while (map && *map && **map == *CHR_SPACE)
842 remain = p->length - (b - p->string);
844 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
851 while (map && *map && **map != *CHR_SPACE)
853 const char *cp = *map;
855 while (i < IT_MAX_WORD && *cp)
857 remain = p->length - (b - p->string);
859 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
865 addString (p, buf, i);
867 (p->seqnos[p->reg_type])++; /* to separate this from next one */
870 static void addCompleteField (RecWord *p)
872 const char *b = p->string;
873 char buf[IT_MAX_WORD+1];
874 const char **map = 0;
875 int i = 0, remain = p->length;
878 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
880 while (remain > 0 && i < IT_MAX_WORD)
882 while (map && *map && **map == *CHR_SPACE)
884 remain = p->length - (b - p->string);
886 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
893 if (i && i < IT_MAX_WORD)
894 buf[i++] = *CHR_SPACE;
895 while (map && *map && **map != *CHR_SPACE)
897 const char *cp = *map;
899 if (i >= IT_MAX_WORD)
901 while (i < IT_MAX_WORD && *cp)
903 remain = p->length - (b - p->string);
905 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
913 addString (p, buf, i);
916 static void addRecordKey (RecWord *p)
919 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
920 p->string, p->length)))
922 p->string = wrbuf_buf(wrbuf);
923 p->length = wrbuf_len(wrbuf);
925 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
926 addCompleteField (p);
928 addIncompleteField(p);
931 static void flushSortKeys (SYSNO sysno, int cmd)
933 struct sortKey *sk = sortKeys;
935 sortIdx_sysno (sortIdx, sysno);
938 struct sortKey *sk_next = sk->next;
941 sortIdx_type (sortIdx, sk->attrUse);
942 sortIdx_add (sortIdx, sk->string, sk->length);
951 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
955 unsigned char attrSet = (unsigned char) -1;
956 unsigned short attrUse = (unsigned short) -1;
962 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
963 while (off < reckeys->buf_used)
965 const char *src = reckeys->buf + off;
974 memcpy (&ch, src, sizeof(ch));
980 memcpy (&attrSet, src, sizeof(attrSet));
981 src += sizeof(attrSet);
985 memcpy (&attrUse, src, sizeof(attrUse));
986 src += sizeof(attrUse);
989 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
993 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
997 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
1000 ch = zebraExplain_addSU (zti, attrSet, attrUse);
1001 yaz_log (LOG_LOG, "addSU cmd=%d set=%d use=%d SU=%d",
1002 cmd, attrSet, attrUse, ch);
1006 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
1009 ((char*)key_buf) [key_buf_used++] = *src++;
1011 ((char*)key_buf) [key_buf_used++] = '\0';
1012 ((char*) key_buf)[key_buf_used++] = cmd;
1015 seqno += ((lead>>2) & 15)-1;
1018 memcpy (&seqno, src, sizeof(seqno));
1019 src += sizeof(seqno);
1023 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
1024 key_buf_used += sizeof(key);
1025 off = src - reckeys->buf;
1027 assert (off == reckeys->buf_used);
1030 static const char **searchRecordKey (struct recKeys *reckeys,
1031 int attrSetS, int attrUseS)
1033 static const char *ws[32];
1045 for (i = 0; i<32; i++)
1049 chS = zebraExplain_lookupSU (zti, attrSetS, attrUseS);
1053 while (off < reckeys->buf_used)
1056 const char *src = reckeys->buf + off;
1064 memcpy (&ch, src, sizeof(ch));
1070 memcpy (&attrSet, src, sizeof(attrSet));
1071 src += sizeof(attrSet);
1075 memcpy (&attrUse, src, sizeof(attrUse));
1076 src += sizeof(attrUse);
1083 seqno += ((lead>>2) & 15)-1;
1086 memcpy (&seqno, src, sizeof(seqno));
1087 src += sizeof(seqno);
1093 attrUseS == attrUse && attrSetS == attrSet
1102 woff = seqno - startSeq;
1103 if (woff >= 0 && woff < 31)
1107 off = src - reckeys->buf;
1109 assert (off == reckeys->buf_used);
1113 struct file_read_info {
1114 off_t file_max; /* maximum offset so far */
1115 off_t file_offset; /* current offset */
1116 off_t file_moffset; /* offset of rec/rec boundary */
1123 static struct file_read_info *file_read_start (int fd)
1125 struct file_read_info *fi = (struct file_read_info *)
1126 xmalloc (sizeof(*fi));
1130 fi->file_moffset = 0;
1136 static void file_read_stop (struct file_read_info *fi)
1141 static off_t file_seek (void *handle, off_t offset)
1143 struct file_read_info *p = (struct file_read_info *) handle;
1144 p->file_offset = offset;
1147 return lseek (p->fd, offset, SEEK_SET);
1150 static off_t file_tell (void *handle)
1152 struct file_read_info *p = (struct file_read_info *) handle;
1153 return p->file_offset;
1156 static int file_read (void *handle, char *buf, size_t count)
1158 struct file_read_info *p = (struct file_read_info *) handle;
1164 if (r > p->sdrmax - p->file_offset)
1165 r = p->sdrmax - p->file_offset;
1167 memcpy (buf, p->sdrbuf + p->file_offset, r);
1170 r = read (fd, buf, count);
1173 p->file_offset += r;
1174 if (p->file_offset > p->file_max)
1175 p->file_max = p->file_offset;
1180 static void file_begin (void *handle)
1182 struct file_read_info *p = (struct file_read_info *) handle;
1184 p->file_offset = p->file_moffset;
1185 if (!p->sdrbuf && p->file_moffset)
1186 lseek (p->fd, p->file_moffset, SEEK_SET);
1190 static void file_end (void *handle, off_t offset)
1192 struct file_read_info *p = (struct file_read_info *) handle;
1194 assert (p->file_more == 0);
1196 p->file_moffset = offset;
1199 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1200 const char *fname, const char *spec)
1202 static char dstBuf[2048];
1204 const char *s = spec;
1205 static const char **w;
1209 while (*s == ' ' || *s == '\t')
1215 char attset_str[64], attname_str[64];
1216 data1_attset *attset;
1219 int attSet = 1, attUse = 1;
1223 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1225 attset_str[i++] = *s;
1226 attset_str[i] = '\0';
1231 for (i = 0; *s && *s != ')'; s++)
1233 attname_str[i++] = *s;
1234 attname_str[i] = '\0';
1237 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1240 attSet = attset->reference;
1241 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1243 attUse = att->value;
1245 attUse = atoi (attname_str);
1247 w = searchRecordKey (reckeys, attSet, attUse);
1252 for (i = 0; i<32; i++)
1257 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1258 spec, rGroup->groupName ? rGroup->groupName : "none");
1263 for (i = 0; i<32; i++)
1264 if (matchFlag[i] && w[i])
1272 dst += strlen(w[i]);
1276 logf (LOG_WARN, "Record didn't contain match"
1277 " fields in (%s,%s)", attset_str, attname_str);
1285 const char *spec_src = NULL;
1286 const char *s1 = ++s;
1287 while (*s1 && *s1 != ' ' && *s1 != '\t')
1293 memcpy (special, s, spec_len);
1294 special[spec_len] = '\0';
1297 if (!strcmp (special, "group"))
1298 spec_src = rGroup->groupName;
1299 else if (!strcmp (special, "database"))
1300 spec_src = rGroup->databaseName;
1301 else if (!strcmp (special, "filename"))
1303 else if (!strcmp (special, "type"))
1304 spec_src = rGroup->recordType;
1309 strcpy (dst, spec_src);
1310 dst += strlen (spec_src);
1313 else if (*s == '\"' || *s == '\'')
1315 int stopMarker = *s++;
1319 while (*s && *s != stopMarker)
1322 tmpString[i++] = *s++;
1326 tmpString[i] = '\0';
1327 strcpy (dst, tmpString);
1328 dst += strlen (tmpString);
1332 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1333 spec, rGroup->groupName ? rGroup->groupName : "none");
1340 logf (LOG_WARN, "No match criteria for record %s in group %s",
1341 fname, rGroup->groupName ? rGroup->groupName : "none");
1348 struct recordLogInfo {
1351 struct recordGroup *rGroup;
1354 static void recordLogPreamble (int level, const char *msg, void *info)
1356 struct recordLogInfo *p = (struct recordLogInfo *) info;
1357 FILE *outf = yaz_log_file ();
1359 if (level & LOG_LOG)
1361 fprintf (outf, "File %s, offset %d, type %s\n",
1362 p->rGroup->recordType, p->recordOffset, p->fname);
1363 log_event_start (NULL, NULL);
1366 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1368 zebraExplain_addSchema (zti, oid);
1371 static int recordExtract (SYSNO *sysno, const char *fname,
1372 struct recordGroup *rGroup, int deleteFlag,
1373 struct file_read_info *fi,
1374 RecType recType, char *subType, void *clientData)
1376 RecordAttr *recordAttr;
1381 struct recordLogInfo logInfo;
1382 off_t recordOffset = 0;
1386 struct recExtractCtrl extractCtrl;
1388 /* we are going to read from a file, so prepare the extraction */
1391 reckeys.buf_used = 0;
1392 reckeys.prevAttrUse = -1;
1393 reckeys.prevAttrSet = -1;
1394 reckeys.prevSeqNo = 0;
1396 recordOffset = fi->file_moffset;
1397 extractCtrl.offset = fi->file_moffset;
1398 extractCtrl.readf = file_read;
1399 extractCtrl.seekf = file_seek;
1400 extractCtrl.tellf = file_tell;
1401 extractCtrl.endf = file_end;
1402 extractCtrl.fh = fi;
1403 extractCtrl.subType = subType;
1404 extractCtrl.init = wordInit;
1405 extractCtrl.tokenAdd = addRecordKey;
1406 extractCtrl.schemaAdd = addSchema;
1407 extractCtrl.dh = rGroup->dh;
1408 for (i = 0; i<256; i++)
1410 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1411 extractCtrl.seqno[i] = 1;
1413 extractCtrl.seqno[i] = 0;
1415 extractCtrl.zebra_maps = rGroup->zebra_maps;
1416 extractCtrl.flagShowRecords = !rGroup->flagRw;
1418 if (!rGroup->flagRw)
1419 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
1421 logInfo.fname = fname;
1422 logInfo.recordOffset = recordOffset;
1423 logInfo.rGroup = rGroup;
1424 log_event_start (recordLogPreamble, &logInfo);
1426 r = (*recType->extract)(clientData, &extractCtrl);
1428 log_event_start (NULL, NULL);
1430 if (r == RECCTRL_EXTRACT_EOF)
1432 else if (r == RECCTRL_EXTRACT_ERROR)
1434 /* error occured during extraction ... */
1435 if (rGroup->flagRw &&
1436 records_processed < rGroup->fileVerboseLimit)
1438 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
1439 fname, recordOffset);
1443 if (reckeys.buf_used == 0)
1445 /* the extraction process returned no information - the record
1446 is probably empty - unless flagShowRecords is in use */
1447 if (!rGroup->flagRw)
1450 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
1451 fname, recordOffset);
1456 /* perform match if sysno not known and if match criteria is specified */
1463 if (rGroup->recordId && *rGroup->recordId)
1467 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1471 rinfo = dict_lookup (matchDict, matchStr);
1473 memcpy (sysno, rinfo+1, sizeof(*sysno));
1477 logf (LOG_WARN, "Bad match criteria");
1488 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
1489 fname, recordOffset);
1490 logf (LOG_WARN, "cannot delete record above (seems new)");
1493 if (records_processed < rGroup->fileVerboseLimit)
1494 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
1495 fname, recordOffset);
1496 rec = rec_new (records);
1498 *sysno = rec->sysno;
1500 recordAttr = rec_init_attr (zti, rec);
1504 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1506 flushRecordKeys (*sysno, 1, &reckeys);
1507 flushSortKeys (*sysno, 1);
1513 /* record already exists */
1514 struct recKeys delkeys;
1516 rec = rec_get (records, *sysno);
1519 recordAttr = rec_init_attr (zti, rec);
1521 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1523 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
1524 fname, recordOffset);
1525 flushSortKeys (*sysno, -1);
1530 delkeys.buf_used = rec->size[recInfo_delKeys];
1531 delkeys.buf = rec->info[recInfo_delKeys];
1532 flushSortKeys (*sysno, 0);
1533 flushRecordKeys (*sysno, 0, &delkeys);
1536 /* record going to be deleted */
1537 if (!delkeys.buf_used)
1539 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1540 rGroup->recordType, fname, recordOffset);
1541 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1545 if (records_processed < rGroup->fileVerboseLimit)
1546 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1547 rGroup->recordType, fname, recordOffset);
1550 dict_delete (matchDict, matchStr);
1551 rec_del (records, &rec);
1559 /* record going to be updated */
1560 if (!delkeys.buf_used)
1562 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1563 rGroup->recordType, fname, recordOffset);
1564 logf (LOG_WARN, "cannot update file above, storeKeys false");
1568 if (records_processed < rGroup->fileVerboseLimit)
1569 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1570 rGroup->recordType, fname, recordOffset);
1571 flushRecordKeys (*sysno, 1, &reckeys);
1576 /* update file type */
1577 xfree (rec->info[recInfo_fileType]);
1578 rec->info[recInfo_fileType] =
1579 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1581 /* update filename */
1582 xfree (rec->info[recInfo_filename]);
1583 rec->info[recInfo_filename] =
1584 rec_strdup (fname, &rec->size[recInfo_filename]);
1586 /* update delete keys */
1587 xfree (rec->info[recInfo_delKeys]);
1588 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1591 rec->size[recInfo_delKeys] = reckeys.buf_used;
1592 rec->info[recInfo_delKeys] = reckeys.buf;
1594 reckeys.buf_max = 0;
1596 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1597 rec->size[recInfo_delKeys] = reckeys.buf_used;
1598 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1599 rec->size[recInfo_delKeys]);
1604 rec->info[recInfo_delKeys] = NULL;
1605 rec->size[recInfo_delKeys] = 0;
1608 /* save file size of original record */
1609 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1610 recordAttr->recordSize = fi->file_moffset - recordOffset;
1611 if (!recordAttr->recordSize)
1612 recordAttr->recordSize = fi->file_max - recordOffset;
1613 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1615 /* set run-number for this record */
1616 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1618 /* update store data */
1619 xfree (rec->info[recInfo_storeData]);
1620 if (rGroup->flagStoreData == 1)
1622 rec->size[recInfo_storeData] = recordAttr->recordSize;
1623 rec->info[recInfo_storeData] = (char *)
1624 xmalloc (recordAttr->recordSize);
1625 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1627 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
1628 recordOffset, fname);
1631 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1632 < recordAttr->recordSize)
1634 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1635 recordAttr->recordSize, fname);
1641 rec->info[recInfo_storeData] = NULL;
1642 rec->size[recInfo_storeData] = 0;
1644 /* update database name */
1645 xfree (rec->info[recInfo_databaseName]);
1646 rec->info[recInfo_databaseName] =
1647 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1650 recordAttr->recordOffset = recordOffset;
1652 /* commit this record */
1653 rec_put (records, &rec);
1658 int fileExtract (SYSNO *sysno, const char *fname,
1659 const struct recordGroup *rGroupP, int deleteFlag)
1667 struct recordGroup rGroupM;
1668 struct recordGroup *rGroup = &rGroupM;
1669 struct file_read_info *fi;
1672 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1674 if (!rGroup->groupName || !*rGroup->groupName)
1677 sprintf (gprefix, "%s.", rGroup->groupName);
1679 logf (LOG_DEBUG, "fileExtract %s", fname);
1681 /* determine file extension */
1683 for (i = strlen(fname); --i >= 0; )
1684 if (fname[i] == '/')
1686 else if (fname[i] == '.')
1688 strcpy (ext, fname+i+1);
1691 /* determine file type - depending on extension */
1692 if (!rGroup->recordType)
1694 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1695 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1697 sprintf (ext_res, "%srecordType", gprefix);
1698 rGroup->recordType = res_get (common_resource, ext_res);
1701 if (!rGroup->recordType)
1703 if (records_processed < rGroup->fileVerboseLimit)
1704 logf (LOG_LOG, "? %s", fname);
1707 if (!*rGroup->recordType)
1710 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1713 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1717 /* determine match criteria */
1718 if (!rGroup->recordId)
1720 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1721 rGroup->recordId = res_get (common_resource, ext_res);
1724 /* determine database name */
1725 if (!rGroup->databaseName)
1727 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1728 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1730 sprintf (ext_res, "%sdatabase", gprefix);
1731 rGroup->databaseName = res_get (common_resource, ext_res);
1734 if (!rGroup->databaseName)
1735 rGroup->databaseName = "Default";
1737 /* determine if explain database */
1739 sprintf (ext_res, "%sexplainDatabase", gprefix);
1740 rGroup->explainDatabase =
1741 atoi (res_get_def (common_resource, ext_res, "0"));
1743 /* announce database */
1744 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1746 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1747 rGroup->explainDatabase))
1751 if (rGroup->flagStoreData == -1)
1754 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1755 if (!(sval = res_get (common_resource, ext_res)))
1757 sprintf (ext_res, "%sstoreData", gprefix);
1758 sval = res_get (common_resource, ext_res);
1761 rGroup->flagStoreData = atoi (sval);
1763 if (rGroup->flagStoreData == -1)
1764 rGroup->flagStoreData = 0;
1766 if (rGroup->flagStoreKeys == -1)
1770 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1771 sval = res_get (common_resource, ext_res);
1774 sprintf (ext_res, "%sstoreKeys", gprefix);
1775 sval = res_get (common_resource, ext_res);
1778 sval = res_get (common_resource, "storeKeys");
1780 rGroup->flagStoreKeys = atoi (sval);
1782 if (rGroup->flagStoreKeys == -1)
1783 rGroup->flagStoreKeys = 0;
1785 if (sysno && deleteFlag)
1789 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1791 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1795 fi = file_read_start (fd);
1799 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1800 recType, subType, clientData);
1801 } while (r && !sysno && fi->file_more);
1802 file_read_stop (fi);
1808 static int explain_extract (void *handle, Record rec, data1_node *n)
1810 struct recordGroup *rGroup = (struct recordGroup*) handle;
1811 struct recExtractCtrl extractCtrl;
1814 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1816 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1820 reckeys.buf_used = 0;
1821 reckeys.prevAttrUse = -1;
1822 reckeys.prevAttrSet = -1;
1823 reckeys.prevSeqNo = 0;
1825 extractCtrl.init = wordInit;
1826 extractCtrl.tokenAdd = addRecordKey;
1827 extractCtrl.schemaAdd = addSchema;
1828 extractCtrl.dh = rGroup->dh;
1829 for (i = 0; i<256; i++)
1830 extractCtrl.seqno[i] = 0;
1831 extractCtrl.zebra_maps = rGroup->zebra_maps;
1832 extractCtrl.flagShowRecords = !rGroup->flagRw;
1834 grs_extract_tree(&extractCtrl, n);
1836 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1838 if (rec->size[recInfo_delKeys])
1840 struct recKeys delkeys;
1842 delkeys.buf_used = rec->size[recInfo_delKeys];
1843 delkeys.buf = rec->info[recInfo_delKeys];
1844 flushSortKeys (rec->sysno, 0);
1845 flushRecordKeys (rec->sysno, 0, &delkeys);
1847 flushRecordKeys (rec->sysno, 1, &reckeys);
1848 flushSortKeys (rec->sysno, 1);
1850 xfree (rec->info[recInfo_delKeys]);
1851 rec->size[recInfo_delKeys] = reckeys.buf_used;
1852 rec->info[recInfo_delKeys] = reckeys.buf;
1854 reckeys.buf_max = 0;