2 * Copyright (C) 1994-2001, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.109 2001-10-15 19:53:43 adam
8 * POSIX thread updates. First work on term sets.
10 * Revision 1.108 2001/06/14 11:44:56 adam
11 * Bug fix: default storeKeys setting wasn't read when group was specified.
13 * Revision 1.107 2001/05/28 13:58:48 adam
14 * Call flushSortKeys when record is skipped to fix bad re-use of
15 * sort keys to whatever next record that comes in.
17 * Revision 1.106 2000/12/05 12:22:53 adam
18 * Termlist source implemented (so that we can index values of XML/SGML
21 * Revision 1.105 2000/12/05 10:01:44 adam
22 * Fixed bug regarding user-defined attribute sets.
24 * Revision 1.104 2000/09/05 14:04:05 adam
25 * Updates for prefix 'yaz_' for YAZ log functions.
27 * Revision 1.103 2000/05/18 12:01:36 adam
28 * System call times(2) used again. More 64-bit fixes.
30 * Revision 1.102 2000/05/15 15:32:33 adam
31 * Added 64 bit file input.
33 * Revision 1.101 2000/05/15 13:02:39 adam
36 * Revision 1.100 2000/03/20 19:08:36 adam
37 * Added remote record import using Z39.50 extended services and Segment
40 * Revision 1.99 2000/02/24 10:57:02 adam
41 * Sequence number incremented after each incomplete-field.
43 * Revision 1.98 1999/09/07 07:19:21 adam
44 * Work on character mapping. Implemented replace rules.
46 * Revision 1.97 1999/07/06 12:28:04 adam
47 * Updated record index structure. Format includes version ID. Compression
48 * algorithm ID is stored for each record block.
50 * Revision 1.96 1999/05/26 07:49:13 adam
53 * Revision 1.95 1999/05/21 12:00:17 adam
54 * Better diagnostics for extraction process.
56 * Revision 1.94 1999/05/20 12:57:18 adam
57 * Implemented TCL filter. Updated recctrl system.
59 * Revision 1.93 1999/05/15 14:36:38 adam
60 * Updated dictionary. Implemented "compression" of dictionary.
62 * Revision 1.92 1999/03/09 16:27:49 adam
63 * More work on SDRKit integration.
65 * Revision 1.91 1999/02/12 13:29:22 adam
66 * Implemented position-flag for registers.
68 * Revision 1.90 1999/02/02 14:50:52 adam
69 * Updated WIN32 code specific sections. Changed header.
71 * Revision 1.89 1998/10/28 10:54:38 adam
74 * Revision 1.88 1998/10/16 08:14:29 adam
75 * Updated record control system.
77 * Revision 1.87 1998/10/15 13:10:33 adam
78 * Fixed bug in Zebra that caused it to stop indexing when empty
81 * Revision 1.86 1998/10/13 20:33:53 adam
82 * Fixed one log message and change use ordinal to be an unsigned char.
84 * Revision 1.85 1998/09/22 10:03:41 adam
85 * Changed result sets to be persistent in the sense that they can
86 * be re-searched if needed.
87 * Fixed memory leak in rsm_or.
89 * Revision 1.84 1998/06/11 15:42:22 adam
90 * Changed the way use attributes are specified in the recordId
93 * Revision 1.83 1998/06/08 14:43:10 adam
94 * Added suport for EXPLAIN Proxy servers - added settings databasePath
95 * and explainDatabase to facilitate this. Increased maximum number
96 * of databases and attributes in one register.
98 * Revision 1.82 1998/05/20 10:12:15 adam
99 * Implemented automatic EXPLAIN database maintenance.
100 * Modified Zebra to work with ASN.1 compiled version of YAZ.
102 * Revision 1.81 1998/03/11 11:19:04 adam
103 * Changed the way sequence numbers are generated.
105 * Revision 1.80 1998/03/05 08:45:11 adam
106 * New result set model and modular ranking system. Moved towards
107 * descent server API. System information stored as "SGML" records.
109 * Revision 1.79 1998/02/17 10:32:52 adam
110 * Fixed bug: binary files weren't opened with flag b on NT.
112 * Revision 1.78 1998/02/10 12:03:05 adam
115 * Revision 1.77 1998/01/12 15:04:08 adam
116 * The test option (-s) only uses read-lock (and not write lock).
118 * Revision 1.76 1997/10/27 14:33:04 adam
119 * Moved towards generic character mapping depending on "structure"
120 * field in abstract syntax file. Fixed a few memory leaks. Fixed
121 * bug with negative integers when doing searches with relational
124 * Revision 1.75 1997/09/17 12:19:12 adam
125 * Zebra version corresponds to YAZ version 1.4.
126 * Changed Zebra server so that it doesn't depend on global common_resource.
128 * Revision 1.74 1997/09/09 13:38:06 adam
129 * Partial port to WIN95/NT.
131 * Revision 1.73 1997/09/04 13:57:20 adam
132 * New file extract/retrieve method tellf (added).
133 * Added O_BINARY for open calls.
135 * Revision 1.72 1997/07/15 16:32:29 adam
136 * Bug fix: Match handler didn't terminate the resulting string!
138 * Revision 1.71 1997/07/15 16:28:41 adam
139 * Bug fix: storeData didn't work with files with multiple records.
140 * Bug fix: fixed memory management with records; not really well
143 * Revision 1.70 1997/07/01 13:00:42 adam
144 * Bug fix in routine searchRecordKey: uninitialized variables.
146 * Revision 1.69 1997/04/29 09:26:03 adam
147 * Bug fix: generic recordId handling didn't work for compressed internal
150 * Revision 1.68 1997/02/12 20:39:45 adam
151 * Implemented options -f <n> that limits the log to the first <n>
153 * Changed some log messages also.
155 * Revision 1.67 1996/11/15 15:02:14 adam
156 * Minor changes regarding logging.
158 * Revision 1.66 1996/11/14 09:52:21 adam
159 * Strings in record keys bound by IT_MAX_WORD.
161 * Revision 1.65 1996/11/14 08:57:56 adam
162 * Reduction of storeKeys area.
164 * Revision 1.64 1996/11/08 11:10:16 adam
165 * Buffers used during file match got bigger.
166 * Compressed ISAM support everywhere.
167 * Bug fixes regarding masking characters in queries.
168 * Redesigned Regexp-2 queries.
170 * Revision 1.63 1996/10/29 14:09:39 adam
171 * Use of cisam system - enabled if setting isamc is 1.
173 * Revision 1.62 1996/10/11 10:57:01 adam
174 * New module recctrl. Used to manage records (extract/retrieval).
175 * Several files have been moved to the recctrl sub directory.
177 * Revision 1.61 1996/06/06 12:08:37 quinn
178 * Added showRecord function
180 * Revision 1.60 1996/06/04 10:18:12 adam
181 * Search/scan uses character mapping module.
183 * Revision 1.59 1996/05/14 15:47:07 adam
184 * Cleanup of various buffer size entities.
186 * Revision 1.58 1996/05/14 06:16:38 adam
187 * Compact use/set bytes used in search service.
189 * Revision 1.57 1996/05/13 14:23:04 adam
190 * Work on compaction of set/use bytes in dictionary.
192 * Revision 1.56 1996/05/09 09:54:42 adam
193 * Server supports maps from one logical attributes to a list of physical
195 * The extraction process doesn't make space consuming 'any' keys.
197 * Revision 1.55 1996/05/09 07:28:55 quinn
198 * Work towards phrases and multiple registers
200 * Revision 1.54 1996/05/01 13:46:35 adam
201 * First work on multiple records in one file.
202 * New option, -offset, to the "unread" command in the filter module.
204 * Revision 1.53 1996/04/26 12:09:43 adam
205 * Added a few comments.
207 * Revision 1.52 1996/04/25 13:27:57 adam
208 * Function recordExtract modified so that files with no keys (possibly empty)
211 * Revision 1.51 1996/03/19 11:08:42 adam
212 * Bug fix: Log preamble wasn't always turned off after recordExtract.
214 * Revision 1.50 1996/02/12 18:45:36 adam
215 * New fileVerboseFlag in record group control.
217 * Revision 1.49 1996/02/05 12:29:57 adam
218 * Logging reduced a bit.
219 * The remaining running time is estimated during register merge.
221 * Revision 1.48 1996/02/01 20:53:26 adam
222 * The temporary per-record keys are compacted a little, and duplication
223 * of the per-records keys are avoided when they are saved in the record
224 * information buffer.
226 * Revision 1.47 1996/01/17 14:57:48 adam
227 * Prototype changed for reader functions in extract/retrieve. File
228 * is identified by 'void *' instead of 'int.
230 * Revision 1.46 1995/12/15 14:57:16 adam
233 * Revision 1.45 1995/12/15 12:37:41 adam
234 * In addRecordKeyAny: Writes key only when attrSet != -1.
236 * Revision 1.44 1995/12/12 16:00:54 adam
237 * System call sync(2) used after update/commit.
238 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
241 * Revision 1.43 1995/12/11 09:12:46 adam
242 * The rec_get function returns NULL if record doesn't exist - will
243 * happen in the server if the result set records have been deleted since
244 * the creation of the set (i.e. the search).
245 * The server saves a result temporarily if it is 'volatile', i.e. the
246 * set is register dependent.
248 * Revision 1.42 1995/12/07 17:38:46 adam
249 * Work locking mechanisms for concurrent updates/commit.
251 * Revision 1.41 1995/12/06 16:06:42 adam
252 * Better diagnostics. Work on 'real' dictionary deletion.
254 * Revision 1.40 1995/12/05 16:57:40 adam
255 * More work on regular patterns.
257 * Revision 1.39 1995/12/05 13:20:18 adam
258 * Bug fix: file_read sometimes returned early EOF.
260 * Revision 1.38 1995/12/04 17:59:21 adam
261 * More work on regular expression conversion.
263 * Revision 1.37 1995/12/04 14:22:27 adam
264 * Extra arg to recType_byName.
265 * Started work on new regular expression parsed input to
266 * structured records.
268 * Revision 1.36 1995/11/30 08:34:29 adam
269 * Started work on commit facility.
270 * Changed a few malloc/free to xmalloc/xfree.
272 * Revision 1.35 1995/11/28 14:26:21 adam
273 * Bug fix: recordId with constant wasn't right.
274 * Bug fix: recordId dictionary entry wasn't deleted when needed.
276 * Revision 1.34 1995/11/28 09:09:38 adam
277 * Zebra config renamed.
278 * Use setting 'recordId' to identify record now.
279 * Bug fix in recindex.c: rec_release_blocks was invokeded even
280 * though the blocks were already released.
281 * File traversal properly deletes records when needed.
283 * Revision 1.33 1995/11/27 09:56:20 adam
284 * Record info elements better enumerated. Internal store of records.
286 * Revision 1.32 1995/11/25 10:24:05 adam
287 * More record fields - they are enumerated now.
288 * New options: flagStoreData flagStoreKey.
290 * Revision 1.31 1995/11/24 11:31:35 adam
291 * Commands add & del read filenames from stdin if source directory is
293 * Match criteria supports 'constant' strings.
295 * Revision 1.30 1995/11/22 17:19:16 adam
296 * Record management uses the bfile system.
298 * Revision 1.29 1995/11/21 15:01:14 adam
299 * New general match criteria implemented.
300 * New feature: document groups.
302 * Revision 1.28 1995/11/21 09:20:30 adam
303 * Yet more work on record match.
305 * Revision 1.27 1995/11/20 16:59:45 adam
306 * New update method: the 'old' keys are saved for each records.
308 * Revision 1.26 1995/11/20 11:56:24 adam
309 * Work on new traversal.
311 * Revision 1.25 1995/11/16 15:34:54 adam
312 * Uses new record management system in both indexer and server.
314 * Revision 1.24 1995/11/15 19:13:08 adam
315 * Work on record management.
317 * Revision 1.23 1995/10/27 14:00:10 adam
318 * Implemented detection of database availability.
320 * Revision 1.22 1995/10/17 18:02:07 adam
321 * New feature: databases. Implemented as prefix to words in dictionary.
323 * Revision 1.21 1995/10/10 12:24:38 adam
324 * Temporary sort files are compressed.
326 * Revision 1.20 1995/10/06 13:52:05 adam
327 * Bug fixes. Handler may abort further scanning.
329 * Revision 1.19 1995/10/04 12:55:16 adam
330 * Bug fix in ranked search. Use=Any keys inserted.
332 * Revision 1.18 1995/10/04 09:37:08 quinn
335 * Revision 1.17 1995/10/03 14:28:57 adam
336 * Buffered read in extract works.
338 * Revision 1.16 1995/10/03 14:28:45 adam
339 * Work on more effecient read handler in extract.
341 * Revision 1.15 1995/10/02 15:42:53 adam
342 * Extract uses file descriptors instead of FILE pointers.
344 * Revision 1.14 1995/10/02 15:29:13 adam
345 * More logging in file_extract.
347 * Revision 1.13 1995/09/29 14:01:39 adam
350 * Revision 1.12 1995/09/28 14:22:56 adam
351 * Sort uses smaller temporary files.
353 * Revision 1.11 1995/09/28 12:10:31 adam
354 * Bug fixes. Field prefix used in queries.
356 * Revision 1.10 1995/09/28 09:19:41 adam
357 * xfree/xmalloc used everywhere.
358 * Extract/retrieve method seems to work for text records.
360 * Revision 1.9 1995/09/27 12:22:28 adam
361 * More work on extract in record control.
362 * Field name is not in isam keys but in prefix in dictionary words.
364 * Revision 1.8 1995/09/14 07:48:22 adam
365 * Record control management.
367 * Revision 1.7 1995/09/11 13:09:32 adam
368 * More work on relevance feedback.
370 * Revision 1.6 1995/09/08 14:52:27 adam
371 * Minor changes. Dictionary is lower case now.
373 * Revision 1.5 1995/09/06 16:11:16 adam
374 * Option: only one word key per file.
376 * Revision 1.4 1995/09/05 15:28:39 adam
377 * More work on search engine.
379 * Revision 1.3 1995/09/04 12:33:41 adam
380 * Various cleanup. YAZ util used instead.
382 * Revision 1.2 1995/09/04 09:10:34 adam
383 * More work on index add/del/update.
384 * Merge sort implemented.
385 * Initial work on z39 server.
387 * Revision 1.1 1995/09/01 14:06:35 adam
388 * Split of work into more files.
407 #if _FILE_OFFSET_BITS == 64
408 #define PRINTF_OFF_T "%Ld"
410 #define PRINTF_OFF_T "%ld"
418 #include "zebrasdr.h"
421 static Dict matchDict;
423 static Records records = NULL;
424 static SortIdx sortIdx = NULL;
426 static char **key_buf;
427 static size_t ptr_top;
429 static size_t key_buf_used;
430 static int key_file_no;
432 static int records_inserted = 0;
433 static int records_updated = 0;
434 static int records_deleted = 0;
435 static int records_processed = 0;
437 static ZebraExplainInfo zti = NULL;
440 static void logRecord (int showFlag)
444 if (showFlag || !(records_processed % 1000))
446 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
447 records_processed, records_inserted, records_updated,
452 static int explain_extract (void *handle, Record drec, data1_node *n);
454 int key_open (struct recordGroup *rGroup, int mem)
456 BFiles bfs = rGroup->bfs;
457 int rw = rGroup->flagRw;
458 data1_handle dh = rGroup->dh;
459 char *recordCompression;
460 int record_compression = REC_COMPRESS_NONE;
462 mem = atoi(res_get_def (common_resource, "memMax", "16"))*1024*1024;
465 key_buf = (char **) xmalloc (mem);
466 ptr_top = mem/sizeof(char*);
472 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
474 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
478 recordCompression = res_get_def (common_resource,
479 "recordCompression", "none");
480 if (!strcmp (recordCompression, "none"))
481 record_compression = REC_COMPRESS_NONE;
482 if (!strcmp (recordCompression, "bzip2"))
483 record_compression = REC_COMPRESS_BZIP2;
484 records = rec_open (bfs, rw, record_compression);
487 dict_close (matchDict);
490 zti = zebraExplain_open (records, dh, common_resource,
491 rw, rGroup, explain_extract);
494 rec_close (&records);
495 dict_close (matchDict);
498 sortIdx = sortIdx_open (bfs, 1);
509 void encode_key_init (struct encode_info *i)
516 char *encode_key_int (int d, char *bp)
525 else if (d <= 4194303)
527 *bp++ = 128 + (d>>16);
528 *bp++ = (d>>8) & 255;
533 *bp++ = 192 + (d>>24);
534 *bp++ = (d>>16) & 255;
535 *bp++ = (d>>8) & 255;
541 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
546 while ((*bp++ = *k++))
548 memcpy (&key, k+1, sizeof(struct it_key));
549 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
550 if (i->sysno != key.sysno)
552 i->sysno = key.sysno;
555 else if (!i->seqno && !key.seqno && i->cmd == *k)
557 bp = encode_key_int (key.seqno - i->seqno, bp);
558 i->seqno = key.seqno;
560 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
562 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
570 static int key_y_len;
572 static int key_y_compare (const void *p1, const void *p2)
576 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
577 *(char**) p2 + key_y_len + 1)))
579 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
582 static int key_x_compare (const void *p1, const void *p2)
584 return strcmp (*(char**) p1, *(char**) p2);
588 void key_flush (void)
593 struct encode_info encode_info;
602 logf (LOG_LOG, "sorting section %d", key_file_no);
604 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
605 getFnameTmp (common_resource, out_fname, key_file_no);
607 if (!(outf = fopen (out_fname, "wb")))
609 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
612 logf (LOG_LOG, "writing section %d", key_file_no);
613 prevcp = cp = key_buf[ptr_top-ptr_i];
615 encode_key_init (&encode_info);
616 encode_key_write (cp, &encode_info, outf);
619 cp = key_buf[ptr_top-ptr_i];
620 if (strcmp (cp, prevcp))
622 encode_key_init (&encode_info);
623 encode_key_write (cp, &encode_info, outf);
627 encode_key_write (cp + strlen(cp), &encode_info, outf);
630 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
631 getFnameTmp (out_fname, key_file_no);
633 if (!(outf = fopen (out_fname, "wb")))
635 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
638 logf (LOG_LOG, "writing section %d", key_file_no);
640 prevcp = key_buf[ptr_top-i];
642 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
644 key_y_len = strlen(prevcp)+1;
646 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
647 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
649 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
650 sizeof(char*), key_y_compare);
651 cp = key_buf[ptr_top-ptr_i];
653 encode_key_init (&encode_info);
654 encode_key_write (cp, &encode_info, outf);
657 cp = key_buf[ptr_top-ptr_i];
658 encode_key_write (cp+key_y_len, &encode_info, outf);
662 prevcp = key_buf[ptr_top-ptr_i];
667 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
670 logf (LOG_LOG, "finished section %d", key_file_no);
675 int key_close (struct recordGroup *rGroup)
677 int rw = rGroup->flagRw;
679 zebraExplain_runNumberIncrement (zti, 1);
680 zebraExplain_close (zti, rw);
683 rec_close (&records);
684 dict_close (matchDict);
685 sortIdx_close (sortIdx);
691 static void wordInit (struct recExtractCtrl *p, RecWord *w)
693 w->zebra_maps = p->zebra_maps;
694 w->seqnos = p->seqno;
695 w->attrSet = VAL_BIB1;
701 static struct sortKey {
706 struct sortKey *next;
709 static struct recKeys {
718 static void addIndexString (RecWord *p, const char *string, int length)
721 unsigned char attrSet;
722 unsigned short attrUse;
725 int *pseqno = &p->seqnos[p->reg_type];
727 if (reckeys.buf_used+1024 > reckeys.buf_max)
731 b = (char *) xmalloc (reckeys.buf_max += 128000);
732 if (reckeys.buf_used > 0)
733 memcpy (b, reckeys.buf, reckeys.buf_used);
737 dst = reckeys.buf + reckeys.buf_used;
739 attrSet = p->attrSet;
740 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
743 reckeys.prevAttrSet = attrSet;
744 attrUse = p->attrUse;
745 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
748 reckeys.prevAttrUse = attrUse;
750 diff = 1 + *pseqno - reckeys.prevSeqNo;
751 if (diff >= 1 && diff <= 15)
756 reckeys.prevSeqNo = *pseqno;
763 int ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
766 ch = zebraExplain_addSU (zti, attrSet, attrUse);
769 memcpy (dst, &ch, sizeof(ch));
775 memcpy (dst, &attrSet, sizeof(attrSet));
776 dst += sizeof(attrSet);
780 memcpy (dst, &attrUse, sizeof(attrUse));
781 dst += sizeof(attrUse);
784 *dst++ = p->reg_type;
785 memcpy (dst, string, length);
791 memcpy (dst, pseqno, sizeof(*pseqno));
792 dst += sizeof(*pseqno);
794 reckeys.buf_used = dst - reckeys.buf;
799 static void addSortString (RecWord *p, const char *string, int length)
803 for (sk = sortKeys; sk; sk = sk->next)
804 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
807 sk = (struct sortKey *) xmalloc (sizeof(*sk));
811 sk->string = (char *) xmalloc (length);
813 memcpy (sk->string, string, length);
815 sk->attrSet = p->attrSet;
816 sk->attrUse = p->attrUse;
819 static void addString (RecWord *p, const char *string, int length)
822 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
823 addSortString (p, string, length);
825 addIndexString (p, string, length);
828 static void addIncompleteField (RecWord *p)
830 const char *b = p->string;
831 int remain = p->length;
832 const char **map = 0;
835 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
839 char buf[IT_MAX_WORD+1];
843 while (map && *map && **map == *CHR_SPACE)
845 remain = p->length - (b - p->string);
847 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
854 while (map && *map && **map != *CHR_SPACE)
856 const char *cp = *map;
858 while (i < IT_MAX_WORD && *cp)
860 remain = p->length - (b - p->string);
862 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
868 addString (p, buf, i);
870 (p->seqnos[p->reg_type])++; /* to separate this from next one */
873 static void addCompleteField (RecWord *p)
875 const char *b = p->string;
876 char buf[IT_MAX_WORD+1];
877 const char **map = 0;
878 int i = 0, remain = p->length;
881 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
883 while (remain > 0 && i < IT_MAX_WORD)
885 while (map && *map && **map == *CHR_SPACE)
887 remain = p->length - (b - p->string);
889 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
896 if (i && i < IT_MAX_WORD)
897 buf[i++] = *CHR_SPACE;
898 while (map && *map && **map != *CHR_SPACE)
900 const char *cp = *map;
902 if (i >= IT_MAX_WORD)
904 while (i < IT_MAX_WORD && *cp)
906 remain = p->length - (b - p->string);
908 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
916 addString (p, buf, i);
919 static void addRecordKey (RecWord *p)
922 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
923 p->string, p->length)))
925 p->string = wrbuf_buf(wrbuf);
926 p->length = wrbuf_len(wrbuf);
928 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
929 addCompleteField (p);
931 addIncompleteField(p);
934 static void flushSortKeys (SYSNO sysno, int cmd)
936 struct sortKey *sk = sortKeys;
938 sortIdx_sysno (sortIdx, sysno);
941 struct sortKey *sk_next = sk->next;
944 sortIdx_type (sortIdx, sk->attrUse);
945 sortIdx_add (sortIdx, sk->string, sk->length);
954 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
958 unsigned char attrSet = (unsigned char) -1;
959 unsigned short attrUse = (unsigned short) -1;
965 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
966 while (off < reckeys->buf_used)
968 const char *src = reckeys->buf + off;
977 memcpy (&ch, src, sizeof(ch));
983 memcpy (&attrSet, src, sizeof(attrSet));
984 src += sizeof(attrSet);
988 memcpy (&attrUse, src, sizeof(attrUse));
989 src += sizeof(attrUse);
992 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
996 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
1000 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
1003 ch = zebraExplain_addSU (zti, attrSet, attrUse);
1004 yaz_log (LOG_LOG, "addSU cmd=%d set=%d use=%d SU=%d",
1005 cmd, attrSet, attrUse, ch);
1009 key_buf_used += key_SU_encode (ch, ((char*)key_buf) + key_buf_used);
1012 ((char*)key_buf) [key_buf_used++] = *src++;
1014 ((char*)key_buf) [key_buf_used++] = '\0';
1015 ((char*) key_buf)[key_buf_used++] = cmd;
1018 seqno += ((lead>>2) & 15)-1;
1021 memcpy (&seqno, src, sizeof(seqno));
1022 src += sizeof(seqno);
1026 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
1027 key_buf_used += sizeof(key);
1028 off = src - reckeys->buf;
1030 assert (off == reckeys->buf_used);
1033 static const char **searchRecordKey (struct recKeys *reckeys,
1034 int attrSetS, int attrUseS)
1036 static const char *ws[32];
1048 for (i = 0; i<32; i++)
1052 chS = zebraExplain_lookupSU (zti, attrSetS, attrUseS);
1056 while (off < reckeys->buf_used)
1059 const char *src = reckeys->buf + off;
1067 memcpy (&ch, src, sizeof(ch));
1073 memcpy (&attrSet, src, sizeof(attrSet));
1074 src += sizeof(attrSet);
1078 memcpy (&attrUse, src, sizeof(attrUse));
1079 src += sizeof(attrUse);
1086 seqno += ((lead>>2) & 15)-1;
1089 memcpy (&seqno, src, sizeof(seqno));
1090 src += sizeof(seqno);
1096 attrUseS == attrUse && attrSetS == attrSet
1105 woff = seqno - startSeq;
1106 if (woff >= 0 && woff < 31)
1110 off = src - reckeys->buf;
1112 assert (off == reckeys->buf_used);
1116 struct file_read_info {
1117 off_t file_max; /* maximum offset so far */
1118 off_t file_offset; /* current offset */
1119 off_t file_moffset; /* offset of rec/rec boundary */
1126 static struct file_read_info *file_read_start (int fd)
1128 struct file_read_info *fi = (struct file_read_info *)
1129 xmalloc (sizeof(*fi));
1133 fi->file_moffset = 0;
1139 static void file_read_stop (struct file_read_info *fi)
1144 static off_t file_seek (void *handle, off_t offset)
1146 struct file_read_info *p = (struct file_read_info *) handle;
1147 p->file_offset = offset;
1150 return lseek (p->fd, offset, SEEK_SET);
1153 static off_t file_tell (void *handle)
1155 struct file_read_info *p = (struct file_read_info *) handle;
1156 return p->file_offset;
1159 static int file_read (void *handle, char *buf, size_t count)
1161 struct file_read_info *p = (struct file_read_info *) handle;
1167 if (r > p->sdrmax - p->file_offset)
1168 r = p->sdrmax - p->file_offset;
1170 memcpy (buf, p->sdrbuf + p->file_offset, r);
1173 r = read (fd, buf, count);
1176 p->file_offset += r;
1177 if (p->file_offset > p->file_max)
1178 p->file_max = p->file_offset;
1183 static void file_begin (void *handle)
1185 struct file_read_info *p = (struct file_read_info *) handle;
1187 p->file_offset = p->file_moffset;
1188 if (!p->sdrbuf && p->file_moffset)
1189 lseek (p->fd, p->file_moffset, SEEK_SET);
1193 static void file_end (void *handle, off_t offset)
1195 struct file_read_info *p = (struct file_read_info *) handle;
1197 assert (p->file_more == 0);
1199 p->file_moffset = offset;
1202 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1203 const char *fname, const char *spec)
1205 static char dstBuf[2048];
1207 const char *s = spec;
1208 static const char **w;
1212 while (*s == ' ' || *s == '\t')
1218 char attset_str[64], attname_str[64];
1219 data1_attset *attset;
1222 int attSet = 1, attUse = 1;
1226 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1228 attset_str[i++] = *s;
1229 attset_str[i] = '\0';
1234 for (i = 0; *s && *s != ')'; s++)
1236 attname_str[i++] = *s;
1237 attname_str[i] = '\0';
1240 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1243 attSet = attset->reference;
1244 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1246 attUse = att->value;
1248 attUse = atoi (attname_str);
1250 w = searchRecordKey (reckeys, attSet, attUse);
1255 for (i = 0; i<32; i++)
1260 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1261 spec, rGroup->groupName ? rGroup->groupName : "none");
1266 for (i = 0; i<32; i++)
1267 if (matchFlag[i] && w[i])
1275 dst += strlen(w[i]);
1279 logf (LOG_WARN, "Record didn't contain match"
1280 " fields in (%s,%s)", attset_str, attname_str);
1288 const char *spec_src = NULL;
1289 const char *s1 = ++s;
1290 while (*s1 && *s1 != ' ' && *s1 != '\t')
1296 memcpy (special, s, spec_len);
1297 special[spec_len] = '\0';
1300 if (!strcmp (special, "group"))
1301 spec_src = rGroup->groupName;
1302 else if (!strcmp (special, "database"))
1303 spec_src = rGroup->databaseName;
1304 else if (!strcmp (special, "filename"))
1306 else if (!strcmp (special, "type"))
1307 spec_src = rGroup->recordType;
1312 strcpy (dst, spec_src);
1313 dst += strlen (spec_src);
1316 else if (*s == '\"' || *s == '\'')
1318 int stopMarker = *s++;
1322 while (*s && *s != stopMarker)
1325 tmpString[i++] = *s++;
1329 tmpString[i] = '\0';
1330 strcpy (dst, tmpString);
1331 dst += strlen (tmpString);
1335 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1336 spec, rGroup->groupName ? rGroup->groupName : "none");
1343 logf (LOG_WARN, "No match criteria for record %s in group %s",
1344 fname, rGroup->groupName ? rGroup->groupName : "none");
1351 struct recordLogInfo {
1354 struct recordGroup *rGroup;
1357 static void recordLogPreamble (int level, const char *msg, void *info)
1359 struct recordLogInfo *p = (struct recordLogInfo *) info;
1360 FILE *outf = yaz_log_file ();
1362 if (level & LOG_LOG)
1364 fprintf (outf, "File %s, offset %d, type %s\n",
1365 p->rGroup->recordType, p->recordOffset, p->fname);
1366 log_event_start (NULL, NULL);
1369 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1371 zebraExplain_addSchema (zti, oid);
1374 static int recordExtract (SYSNO *sysno, const char *fname,
1375 struct recordGroup *rGroup, int deleteFlag,
1376 struct file_read_info *fi,
1377 RecType recType, char *subType, void *clientData)
1379 RecordAttr *recordAttr;
1384 struct recordLogInfo logInfo;
1385 off_t recordOffset = 0;
1389 struct recExtractCtrl extractCtrl;
1391 /* we are going to read from a file, so prepare the extraction */
1394 reckeys.buf_used = 0;
1395 reckeys.prevAttrUse = -1;
1396 reckeys.prevAttrSet = -1;
1397 reckeys.prevSeqNo = 0;
1399 recordOffset = fi->file_moffset;
1400 extractCtrl.offset = fi->file_moffset;
1401 extractCtrl.readf = file_read;
1402 extractCtrl.seekf = file_seek;
1403 extractCtrl.tellf = file_tell;
1404 extractCtrl.endf = file_end;
1405 extractCtrl.fh = fi;
1406 extractCtrl.subType = subType;
1407 extractCtrl.init = wordInit;
1408 extractCtrl.tokenAdd = addRecordKey;
1409 extractCtrl.schemaAdd = addSchema;
1410 extractCtrl.dh = rGroup->dh;
1411 for (i = 0; i<256; i++)
1413 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1414 extractCtrl.seqno[i] = 1;
1416 extractCtrl.seqno[i] = 0;
1418 extractCtrl.zebra_maps = rGroup->zebra_maps;
1419 extractCtrl.flagShowRecords = !rGroup->flagRw;
1421 if (!rGroup->flagRw)
1422 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
1424 logInfo.fname = fname;
1425 logInfo.recordOffset = recordOffset;
1426 logInfo.rGroup = rGroup;
1427 log_event_start (recordLogPreamble, &logInfo);
1429 r = (*recType->extract)(clientData, &extractCtrl);
1431 log_event_start (NULL, NULL);
1433 if (r == RECCTRL_EXTRACT_EOF)
1435 else if (r == RECCTRL_EXTRACT_ERROR)
1437 /* error occured during extraction ... */
1438 if (rGroup->flagRw &&
1439 records_processed < rGroup->fileVerboseLimit)
1441 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
1442 fname, recordOffset);
1446 if (reckeys.buf_used == 0)
1448 /* the extraction process returned no information - the record
1449 is probably empty - unless flagShowRecords is in use */
1450 if (!rGroup->flagRw)
1453 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
1454 fname, recordOffset);
1459 /* perform match if sysno not known and if match criteria is specified */
1466 if (rGroup->recordId && *rGroup->recordId)
1470 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1474 rinfo = dict_lookup (matchDict, matchStr);
1476 memcpy (sysno, rinfo+1, sizeof(*sysno));
1480 logf (LOG_WARN, "Bad match criteria");
1491 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
1492 fname, recordOffset);
1493 logf (LOG_WARN, "cannot delete record above (seems new)");
1496 if (records_processed < rGroup->fileVerboseLimit)
1497 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
1498 fname, recordOffset);
1499 rec = rec_new (records);
1501 *sysno = rec->sysno;
1503 recordAttr = rec_init_attr (zti, rec);
1507 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1509 flushRecordKeys (*sysno, 1, &reckeys);
1510 flushSortKeys (*sysno, 1);
1516 /* record already exists */
1517 struct recKeys delkeys;
1519 rec = rec_get (records, *sysno);
1522 recordAttr = rec_init_attr (zti, rec);
1524 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1526 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
1527 fname, recordOffset);
1528 flushSortKeys (*sysno, -1);
1533 delkeys.buf_used = rec->size[recInfo_delKeys];
1534 delkeys.buf = rec->info[recInfo_delKeys];
1535 flushSortKeys (*sysno, 0);
1536 flushRecordKeys (*sysno, 0, &delkeys);
1539 /* record going to be deleted */
1540 if (!delkeys.buf_used)
1542 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1543 rGroup->recordType, fname, recordOffset);
1544 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1548 if (records_processed < rGroup->fileVerboseLimit)
1549 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1550 rGroup->recordType, fname, recordOffset);
1553 dict_delete (matchDict, matchStr);
1554 rec_del (records, &rec);
1562 /* record going to be updated */
1563 if (!delkeys.buf_used)
1565 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1566 rGroup->recordType, fname, recordOffset);
1567 logf (LOG_WARN, "cannot update file above, storeKeys false");
1571 if (records_processed < rGroup->fileVerboseLimit)
1572 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1573 rGroup->recordType, fname, recordOffset);
1574 flushRecordKeys (*sysno, 1, &reckeys);
1579 /* update file type */
1580 xfree (rec->info[recInfo_fileType]);
1581 rec->info[recInfo_fileType] =
1582 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1584 /* update filename */
1585 xfree (rec->info[recInfo_filename]);
1586 rec->info[recInfo_filename] =
1587 rec_strdup (fname, &rec->size[recInfo_filename]);
1589 /* update delete keys */
1590 xfree (rec->info[recInfo_delKeys]);
1591 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1594 rec->size[recInfo_delKeys] = reckeys.buf_used;
1595 rec->info[recInfo_delKeys] = reckeys.buf;
1597 reckeys.buf_max = 0;
1599 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1600 rec->size[recInfo_delKeys] = reckeys.buf_used;
1601 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1602 rec->size[recInfo_delKeys]);
1607 rec->info[recInfo_delKeys] = NULL;
1608 rec->size[recInfo_delKeys] = 0;
1611 /* save file size of original record */
1612 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1613 recordAttr->recordSize = fi->file_moffset - recordOffset;
1614 if (!recordAttr->recordSize)
1615 recordAttr->recordSize = fi->file_max - recordOffset;
1616 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1618 /* set run-number for this record */
1619 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1621 /* update store data */
1622 xfree (rec->info[recInfo_storeData]);
1623 if (rGroup->flagStoreData == 1)
1625 rec->size[recInfo_storeData] = recordAttr->recordSize;
1626 rec->info[recInfo_storeData] = (char *)
1627 xmalloc (recordAttr->recordSize);
1628 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1630 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
1631 recordOffset, fname);
1634 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1635 < recordAttr->recordSize)
1637 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1638 recordAttr->recordSize, fname);
1644 rec->info[recInfo_storeData] = NULL;
1645 rec->size[recInfo_storeData] = 0;
1647 /* update database name */
1648 xfree (rec->info[recInfo_databaseName]);
1649 rec->info[recInfo_databaseName] =
1650 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1653 recordAttr->recordOffset = recordOffset;
1655 /* commit this record */
1656 rec_put (records, &rec);
1661 int fileExtract (SYSNO *sysno, const char *fname,
1662 const struct recordGroup *rGroupP, int deleteFlag)
1670 struct recordGroup rGroupM;
1671 struct recordGroup *rGroup = &rGroupM;
1672 struct file_read_info *fi;
1675 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1677 if (!rGroup->groupName || !*rGroup->groupName)
1680 sprintf (gprefix, "%s.", rGroup->groupName);
1682 logf (LOG_DEBUG, "fileExtract %s", fname);
1684 /* determine file extension */
1686 for (i = strlen(fname); --i >= 0; )
1687 if (fname[i] == '/')
1689 else if (fname[i] == '.')
1691 strcpy (ext, fname+i+1);
1694 /* determine file type - depending on extension */
1695 if (!rGroup->recordType)
1697 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1698 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1700 sprintf (ext_res, "%srecordType", gprefix);
1701 rGroup->recordType = res_get (common_resource, ext_res);
1704 if (!rGroup->recordType)
1706 if (records_processed < rGroup->fileVerboseLimit)
1707 logf (LOG_LOG, "? %s", fname);
1710 if (!*rGroup->recordType)
1713 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1716 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1720 /* determine match criteria */
1721 if (!rGroup->recordId)
1723 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1724 rGroup->recordId = res_get (common_resource, ext_res);
1727 /* determine database name */
1728 if (!rGroup->databaseName)
1730 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1731 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1733 sprintf (ext_res, "%sdatabase", gprefix);
1734 rGroup->databaseName = res_get (common_resource, ext_res);
1737 if (!rGroup->databaseName)
1738 rGroup->databaseName = "Default";
1740 /* determine if explain database */
1742 sprintf (ext_res, "%sexplainDatabase", gprefix);
1743 rGroup->explainDatabase =
1744 atoi (res_get_def (common_resource, ext_res, "0"));
1746 /* announce database */
1747 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1749 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1750 rGroup->explainDatabase))
1754 if (rGroup->flagStoreData == -1)
1757 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1758 if (!(sval = res_get (common_resource, ext_res)))
1760 sprintf (ext_res, "%sstoreData", gprefix);
1761 sval = res_get (common_resource, ext_res);
1764 rGroup->flagStoreData = atoi (sval);
1766 if (rGroup->flagStoreData == -1)
1767 rGroup->flagStoreData = 0;
1769 if (rGroup->flagStoreKeys == -1)
1773 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1774 sval = res_get (common_resource, ext_res);
1777 sprintf (ext_res, "%sstoreKeys", gprefix);
1778 sval = res_get (common_resource, ext_res);
1781 sval = res_get (common_resource, "storeKeys");
1783 rGroup->flagStoreKeys = atoi (sval);
1785 if (rGroup->flagStoreKeys == -1)
1786 rGroup->flagStoreKeys = 0;
1788 if (sysno && deleteFlag)
1792 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1794 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1798 fi = file_read_start (fd);
1802 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1803 recType, subType, clientData);
1804 } while (r && !sysno && fi->file_more);
1805 file_read_stop (fi);
1811 static int explain_extract (void *handle, Record rec, data1_node *n)
1813 struct recordGroup *rGroup = (struct recordGroup*) handle;
1814 struct recExtractCtrl extractCtrl;
1817 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1819 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1823 reckeys.buf_used = 0;
1824 reckeys.prevAttrUse = -1;
1825 reckeys.prevAttrSet = -1;
1826 reckeys.prevSeqNo = 0;
1828 extractCtrl.init = wordInit;
1829 extractCtrl.tokenAdd = addRecordKey;
1830 extractCtrl.schemaAdd = addSchema;
1831 extractCtrl.dh = rGroup->dh;
1832 for (i = 0; i<256; i++)
1833 extractCtrl.seqno[i] = 0;
1834 extractCtrl.zebra_maps = rGroup->zebra_maps;
1835 extractCtrl.flagShowRecords = !rGroup->flagRw;
1837 grs_extract_tree(&extractCtrl, n);
1839 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1841 if (rec->size[recInfo_delKeys])
1843 struct recKeys delkeys;
1845 delkeys.buf_used = rec->size[recInfo_delKeys];
1846 delkeys.buf = rec->info[recInfo_delKeys];
1847 flushSortKeys (rec->sysno, 0);
1848 flushRecordKeys (rec->sysno, 0, &delkeys);
1850 flushRecordKeys (rec->sysno, 1, &reckeys);
1851 flushSortKeys (rec->sysno, 1);
1853 xfree (rec->info[recInfo_delKeys]);
1854 rec->size[recInfo_delKeys] = reckeys.buf_used;
1855 rec->info[recInfo_delKeys] = reckeys.buf;
1857 reckeys.buf_max = 0;