2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.106 2000-12-05 12:22:53 adam
8 * Termlist source implemented (so that we can index values of XML/SGML
11 * Revision 1.105 2000/12/05 10:01:44 adam
12 * Fixed bug regarding user-defined attribute sets.
14 * Revision 1.104 2000/09/05 14:04:05 adam
15 * Updates for prefix 'yaz_' for YAZ log functions.
17 * Revision 1.103 2000/05/18 12:01:36 adam
18 * System call times(2) used again. More 64-bit fixes.
20 * Revision 1.102 2000/05/15 15:32:33 adam
21 * Added 64 bit file input.
23 * Revision 1.101 2000/05/15 13:02:39 adam
26 * Revision 1.100 2000/03/20 19:08:36 adam
27 * Added remote record import using Z39.50 extended services and Segment
30 * Revision 1.99 2000/02/24 10:57:02 adam
31 * Sequence number incremented after each incomplete-field.
33 * Revision 1.98 1999/09/07 07:19:21 adam
34 * Work on character mapping. Implemented replace rules.
36 * Revision 1.97 1999/07/06 12:28:04 adam
37 * Updated record index structure. Format includes version ID. Compression
38 * algorithm ID is stored for each record block.
40 * Revision 1.96 1999/05/26 07:49:13 adam
43 * Revision 1.95 1999/05/21 12:00:17 adam
44 * Better diagnostics for extraction process.
46 * Revision 1.94 1999/05/20 12:57:18 adam
47 * Implemented TCL filter. Updated recctrl system.
49 * Revision 1.93 1999/05/15 14:36:38 adam
50 * Updated dictionary. Implemented "compression" of dictionary.
52 * Revision 1.92 1999/03/09 16:27:49 adam
53 * More work on SDRKit integration.
55 * Revision 1.91 1999/02/12 13:29:22 adam
56 * Implemented position-flag for registers.
58 * Revision 1.90 1999/02/02 14:50:52 adam
59 * Updated WIN32 code specific sections. Changed header.
61 * Revision 1.89 1998/10/28 10:54:38 adam
64 * Revision 1.88 1998/10/16 08:14:29 adam
65 * Updated record control system.
67 * Revision 1.87 1998/10/15 13:10:33 adam
68 * Fixed bug in Zebra that caused it to stop indexing when empty
71 * Revision 1.86 1998/10/13 20:33:53 adam
72 * Fixed one log message and change use ordinal to be an unsigned char.
74 * Revision 1.85 1998/09/22 10:03:41 adam
75 * Changed result sets to be persistent in the sense that they can
76 * be re-searched if needed.
77 * Fixed memory leak in rsm_or.
79 * Revision 1.84 1998/06/11 15:42:22 adam
80 * Changed the way use attributes are specified in the recordId
83 * Revision 1.83 1998/06/08 14:43:10 adam
84 * Added suport for EXPLAIN Proxy servers - added settings databasePath
85 * and explainDatabase to facilitate this. Increased maximum number
86 * of databases and attributes in one register.
88 * Revision 1.82 1998/05/20 10:12:15 adam
89 * Implemented automatic EXPLAIN database maintenance.
90 * Modified Zebra to work with ASN.1 compiled version of YAZ.
92 * Revision 1.81 1998/03/11 11:19:04 adam
93 * Changed the way sequence numbers are generated.
95 * Revision 1.80 1998/03/05 08:45:11 adam
96 * New result set model and modular ranking system. Moved towards
97 * descent server API. System information stored as "SGML" records.
99 * Revision 1.79 1998/02/17 10:32:52 adam
100 * Fixed bug: binary files weren't opened with flag b on NT.
102 * Revision 1.78 1998/02/10 12:03:05 adam
105 * Revision 1.77 1998/01/12 15:04:08 adam
106 * The test option (-s) only uses read-lock (and not write lock).
108 * Revision 1.76 1997/10/27 14:33:04 adam
109 * Moved towards generic character mapping depending on "structure"
110 * field in abstract syntax file. Fixed a few memory leaks. Fixed
111 * bug with negative integers when doing searches with relational
114 * Revision 1.75 1997/09/17 12:19:12 adam
115 * Zebra version corresponds to YAZ version 1.4.
116 * Changed Zebra server so that it doesn't depend on global common_resource.
118 * Revision 1.74 1997/09/09 13:38:06 adam
119 * Partial port to WIN95/NT.
121 * Revision 1.73 1997/09/04 13:57:20 adam
122 * New file extract/retrieve method tellf (added).
123 * Added O_BINARY for open calls.
125 * Revision 1.72 1997/07/15 16:32:29 adam
126 * Bug fix: Match handler didn't terminate the resulting string!
128 * Revision 1.71 1997/07/15 16:28:41 adam
129 * Bug fix: storeData didn't work with files with multiple records.
130 * Bug fix: fixed memory management with records; not really well
133 * Revision 1.70 1997/07/01 13:00:42 adam
134 * Bug fix in routine searchRecordKey: uninitialized variables.
136 * Revision 1.69 1997/04/29 09:26:03 adam
137 * Bug fix: generic recordId handling didn't work for compressed internal
140 * Revision 1.68 1997/02/12 20:39:45 adam
141 * Implemented options -f <n> that limits the log to the first <n>
143 * Changed some log messages also.
145 * Revision 1.67 1996/11/15 15:02:14 adam
146 * Minor changes regarding logging.
148 * Revision 1.66 1996/11/14 09:52:21 adam
149 * Strings in record keys bound by IT_MAX_WORD.
151 * Revision 1.65 1996/11/14 08:57:56 adam
152 * Reduction of storeKeys area.
154 * Revision 1.64 1996/11/08 11:10:16 adam
155 * Buffers used during file match got bigger.
156 * Compressed ISAM support everywhere.
157 * Bug fixes regarding masking characters in queries.
158 * Redesigned Regexp-2 queries.
160 * Revision 1.63 1996/10/29 14:09:39 adam
161 * Use of cisam system - enabled if setting isamc is 1.
163 * Revision 1.62 1996/10/11 10:57:01 adam
164 * New module recctrl. Used to manage records (extract/retrieval).
165 * Several files have been moved to the recctrl sub directory.
167 * Revision 1.61 1996/06/06 12:08:37 quinn
168 * Added showRecord function
170 * Revision 1.60 1996/06/04 10:18:12 adam
171 * Search/scan uses character mapping module.
173 * Revision 1.59 1996/05/14 15:47:07 adam
174 * Cleanup of various buffer size entities.
176 * Revision 1.58 1996/05/14 06:16:38 adam
177 * Compact use/set bytes used in search service.
179 * Revision 1.57 1996/05/13 14:23:04 adam
180 * Work on compaction of set/use bytes in dictionary.
182 * Revision 1.56 1996/05/09 09:54:42 adam
183 * Server supports maps from one logical attributes to a list of physical
185 * The extraction process doesn't make space consuming 'any' keys.
187 * Revision 1.55 1996/05/09 07:28:55 quinn
188 * Work towards phrases and multiple registers
190 * Revision 1.54 1996/05/01 13:46:35 adam
191 * First work on multiple records in one file.
192 * New option, -offset, to the "unread" command in the filter module.
194 * Revision 1.53 1996/04/26 12:09:43 adam
195 * Added a few comments.
197 * Revision 1.52 1996/04/25 13:27:57 adam
198 * Function recordExtract modified so that files with no keys (possibly empty)
201 * Revision 1.51 1996/03/19 11:08:42 adam
202 * Bug fix: Log preamble wasn't always turned off after recordExtract.
204 * Revision 1.50 1996/02/12 18:45:36 adam
205 * New fileVerboseFlag in record group control.
207 * Revision 1.49 1996/02/05 12:29:57 adam
208 * Logging reduced a bit.
209 * The remaining running time is estimated during register merge.
211 * Revision 1.48 1996/02/01 20:53:26 adam
212 * The temporary per-record keys are compacted a little, and duplication
213 * of the per-records keys are avoided when they are saved in the record
214 * information buffer.
216 * Revision 1.47 1996/01/17 14:57:48 adam
217 * Prototype changed for reader functions in extract/retrieve. File
218 * is identified by 'void *' instead of 'int.
220 * Revision 1.46 1995/12/15 14:57:16 adam
223 * Revision 1.45 1995/12/15 12:37:41 adam
224 * In addRecordKeyAny: Writes key only when attrSet != -1.
226 * Revision 1.44 1995/12/12 16:00:54 adam
227 * System call sync(2) used after update/commit.
228 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
231 * Revision 1.43 1995/12/11 09:12:46 adam
232 * The rec_get function returns NULL if record doesn't exist - will
233 * happen in the server if the result set records have been deleted since
234 * the creation of the set (i.e. the search).
235 * The server saves a result temporarily if it is 'volatile', i.e. the
236 * set is register dependent.
238 * Revision 1.42 1995/12/07 17:38:46 adam
239 * Work locking mechanisms for concurrent updates/commit.
241 * Revision 1.41 1995/12/06 16:06:42 adam
242 * Better diagnostics. Work on 'real' dictionary deletion.
244 * Revision 1.40 1995/12/05 16:57:40 adam
245 * More work on regular patterns.
247 * Revision 1.39 1995/12/05 13:20:18 adam
248 * Bug fix: file_read sometimes returned early EOF.
250 * Revision 1.38 1995/12/04 17:59:21 adam
251 * More work on regular expression conversion.
253 * Revision 1.37 1995/12/04 14:22:27 adam
254 * Extra arg to recType_byName.
255 * Started work on new regular expression parsed input to
256 * structured records.
258 * Revision 1.36 1995/11/30 08:34:29 adam
259 * Started work on commit facility.
260 * Changed a few malloc/free to xmalloc/xfree.
262 * Revision 1.35 1995/11/28 14:26:21 adam
263 * Bug fix: recordId with constant wasn't right.
264 * Bug fix: recordId dictionary entry wasn't deleted when needed.
266 * Revision 1.34 1995/11/28 09:09:38 adam
267 * Zebra config renamed.
268 * Use setting 'recordId' to identify record now.
269 * Bug fix in recindex.c: rec_release_blocks was invokeded even
270 * though the blocks were already released.
271 * File traversal properly deletes records when needed.
273 * Revision 1.33 1995/11/27 09:56:20 adam
274 * Record info elements better enumerated. Internal store of records.
276 * Revision 1.32 1995/11/25 10:24:05 adam
277 * More record fields - they are enumerated now.
278 * New options: flagStoreData flagStoreKey.
280 * Revision 1.31 1995/11/24 11:31:35 adam
281 * Commands add & del read filenames from stdin if source directory is
283 * Match criteria supports 'constant' strings.
285 * Revision 1.30 1995/11/22 17:19:16 adam
286 * Record management uses the bfile system.
288 * Revision 1.29 1995/11/21 15:01:14 adam
289 * New general match criteria implemented.
290 * New feature: document groups.
292 * Revision 1.28 1995/11/21 09:20:30 adam
293 * Yet more work on record match.
295 * Revision 1.27 1995/11/20 16:59:45 adam
296 * New update method: the 'old' keys are saved for each records.
298 * Revision 1.26 1995/11/20 11:56:24 adam
299 * Work on new traversal.
301 * Revision 1.25 1995/11/16 15:34:54 adam
302 * Uses new record management system in both indexer and server.
304 * Revision 1.24 1995/11/15 19:13:08 adam
305 * Work on record management.
307 * Revision 1.23 1995/10/27 14:00:10 adam
308 * Implemented detection of database availability.
310 * Revision 1.22 1995/10/17 18:02:07 adam
311 * New feature: databases. Implemented as prefix to words in dictionary.
313 * Revision 1.21 1995/10/10 12:24:38 adam
314 * Temporary sort files are compressed.
316 * Revision 1.20 1995/10/06 13:52:05 adam
317 * Bug fixes. Handler may abort further scanning.
319 * Revision 1.19 1995/10/04 12:55:16 adam
320 * Bug fix in ranked search. Use=Any keys inserted.
322 * Revision 1.18 1995/10/04 09:37:08 quinn
325 * Revision 1.17 1995/10/03 14:28:57 adam
326 * Buffered read in extract works.
328 * Revision 1.16 1995/10/03 14:28:45 adam
329 * Work on more effecient read handler in extract.
331 * Revision 1.15 1995/10/02 15:42:53 adam
332 * Extract uses file descriptors instead of FILE pointers.
334 * Revision 1.14 1995/10/02 15:29:13 adam
335 * More logging in file_extract.
337 * Revision 1.13 1995/09/29 14:01:39 adam
340 * Revision 1.12 1995/09/28 14:22:56 adam
341 * Sort uses smaller temporary files.
343 * Revision 1.11 1995/09/28 12:10:31 adam
344 * Bug fixes. Field prefix used in queries.
346 * Revision 1.10 1995/09/28 09:19:41 adam
347 * xfree/xmalloc used everywhere.
348 * Extract/retrieve method seems to work for text records.
350 * Revision 1.9 1995/09/27 12:22:28 adam
351 * More work on extract in record control.
352 * Field name is not in isam keys but in prefix in dictionary words.
354 * Revision 1.8 1995/09/14 07:48:22 adam
355 * Record control management.
357 * Revision 1.7 1995/09/11 13:09:32 adam
358 * More work on relevance feedback.
360 * Revision 1.6 1995/09/08 14:52:27 adam
361 * Minor changes. Dictionary is lower case now.
363 * Revision 1.5 1995/09/06 16:11:16 adam
364 * Option: only one word key per file.
366 * Revision 1.4 1995/09/05 15:28:39 adam
367 * More work on search engine.
369 * Revision 1.3 1995/09/04 12:33:41 adam
370 * Various cleanup. YAZ util used instead.
372 * Revision 1.2 1995/09/04 09:10:34 adam
373 * More work on index add/del/update.
374 * Merge sort implemented.
375 * Initial work on z39 server.
377 * Revision 1.1 1995/09/01 14:06:35 adam
378 * Split of work into more files.
397 #if _FILE_OFFSET_BITS == 64
398 #define PRINTF_OFF_T "%Ld"
400 #define PRINTF_OFF_T "%ld"
408 #include "zebrasdr.h"
411 static Dict matchDict;
413 static Records records = NULL;
414 static SortIdx sortIdx = NULL;
416 static char **key_buf;
417 static size_t ptr_top;
419 static size_t key_buf_used;
420 static int key_file_no;
422 static int records_inserted = 0;
423 static int records_updated = 0;
424 static int records_deleted = 0;
425 static int records_processed = 0;
427 static ZebraExplainInfo zti = NULL;
430 static void logRecord (int showFlag)
434 if (showFlag || !(records_processed % 1000))
436 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
437 records_processed, records_inserted, records_updated,
442 static int explain_extract (void *handle, Record drec, data1_node *n);
444 int key_open (struct recordGroup *rGroup, int mem)
446 BFiles bfs = rGroup->bfs;
447 int rw = rGroup->flagRw;
448 data1_handle dh = rGroup->dh;
449 char *recordCompression;
450 int record_compression = REC_COMPRESS_NONE;
452 mem = atoi(res_get_def (common_resource, "memMax", "16"))*1024*1024;
455 key_buf = (char **) xmalloc (mem);
456 ptr_top = mem/sizeof(char*);
462 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
464 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
468 recordCompression = res_get_def (common_resource,
469 "recordCompression", "none");
470 if (!strcmp (recordCompression, "none"))
471 record_compression = REC_COMPRESS_NONE;
472 if (!strcmp (recordCompression, "bzip2"))
473 record_compression = REC_COMPRESS_BZIP2;
474 records = rec_open (bfs, rw, record_compression);
477 dict_close (matchDict);
480 zti = zebraExplain_open (records, dh, common_resource,
481 rw, rGroup, explain_extract);
484 rec_close (&records);
485 dict_close (matchDict);
488 sortIdx = sortIdx_open (bfs, 1);
499 void encode_key_init (struct encode_info *i)
506 char *encode_key_int (int d, char *bp)
515 else if (d <= 4194303)
517 *bp++ = 128 + (d>>16);
518 *bp++ = (d>>8) & 255;
523 *bp++ = 192 + (d>>24);
524 *bp++ = (d>>16) & 255;
525 *bp++ = (d>>8) & 255;
531 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
536 while ((*bp++ = *k++))
538 memcpy (&key, k+1, sizeof(struct it_key));
539 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
540 if (i->sysno != key.sysno)
542 i->sysno = key.sysno;
545 else if (!i->seqno && !key.seqno && i->cmd == *k)
547 bp = encode_key_int (key.seqno - i->seqno, bp);
548 i->seqno = key.seqno;
550 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
552 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
560 static int key_y_len;
562 static int key_y_compare (const void *p1, const void *p2)
566 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
567 *(char**) p2 + key_y_len + 1)))
569 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
572 static int key_x_compare (const void *p1, const void *p2)
574 return strcmp (*(char**) p1, *(char**) p2);
578 void key_flush (void)
583 struct encode_info encode_info;
592 logf (LOG_LOG, "sorting section %d", key_file_no);
594 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
595 getFnameTmp (common_resource, out_fname, key_file_no);
597 if (!(outf = fopen (out_fname, "wb")))
599 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
602 logf (LOG_LOG, "writing section %d", key_file_no);
603 prevcp = cp = key_buf[ptr_top-ptr_i];
605 encode_key_init (&encode_info);
606 encode_key_write (cp, &encode_info, outf);
609 cp = key_buf[ptr_top-ptr_i];
610 if (strcmp (cp, prevcp))
612 encode_key_init (&encode_info);
613 encode_key_write (cp, &encode_info, outf);
617 encode_key_write (cp + strlen(cp), &encode_info, outf);
620 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
621 getFnameTmp (out_fname, key_file_no);
623 if (!(outf = fopen (out_fname, "wb")))
625 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
628 logf (LOG_LOG, "writing section %d", key_file_no);
630 prevcp = key_buf[ptr_top-i];
632 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
634 key_y_len = strlen(prevcp)+1;
636 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
637 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
639 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
640 sizeof(char*), key_y_compare);
641 cp = key_buf[ptr_top-ptr_i];
643 encode_key_init (&encode_info);
644 encode_key_write (cp, &encode_info, outf);
647 cp = key_buf[ptr_top-ptr_i];
648 encode_key_write (cp+key_y_len, &encode_info, outf);
652 prevcp = key_buf[ptr_top-ptr_i];
657 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
660 logf (LOG_LOG, "finished section %d", key_file_no);
665 int key_close (struct recordGroup *rGroup)
667 int rw = rGroup->flagRw;
669 zebraExplain_runNumberIncrement (zti, 1);
670 zebraExplain_close (zti, rw);
673 rec_close (&records);
674 dict_close (matchDict);
675 sortIdx_close (sortIdx);
681 static void wordInit (struct recExtractCtrl *p, RecWord *w)
683 w->zebra_maps = p->zebra_maps;
684 w->seqnos = p->seqno;
685 w->attrSet = VAL_BIB1;
691 static struct sortKey {
696 struct sortKey *next;
699 static struct recKeys {
708 static void addIndexString (RecWord *p, const char *string, int length)
711 unsigned char attrSet;
712 unsigned short attrUse;
715 int *pseqno = &p->seqnos[p->reg_type];
717 if (reckeys.buf_used+1024 > reckeys.buf_max)
721 b = (char *) xmalloc (reckeys.buf_max += 128000);
722 if (reckeys.buf_used > 0)
723 memcpy (b, reckeys.buf, reckeys.buf_used);
727 dst = reckeys.buf + reckeys.buf_used;
729 attrSet = p->attrSet;
730 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
733 reckeys.prevAttrSet = attrSet;
734 attrUse = p->attrUse;
735 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
738 reckeys.prevAttrUse = attrUse;
740 diff = 1 + *pseqno - reckeys.prevSeqNo;
741 if (diff >= 1 && diff <= 15)
746 reckeys.prevSeqNo = *pseqno;
753 int ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
756 ch = zebraExplain_addSU (zti, attrSet, attrUse);
759 memcpy (dst, &ch, sizeof(ch));
765 memcpy (dst, &attrSet, sizeof(attrSet));
766 dst += sizeof(attrSet);
770 memcpy (dst, &attrUse, sizeof(attrUse));
771 dst += sizeof(attrUse);
774 *dst++ = p->reg_type;
775 memcpy (dst, string, length);
781 memcpy (dst, pseqno, sizeof(*pseqno));
782 dst += sizeof(*pseqno);
784 reckeys.buf_used = dst - reckeys.buf;
789 static void addSortString (RecWord *p, const char *string, int length)
793 for (sk = sortKeys; sk; sk = sk->next)
794 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
797 sk = (struct sortKey *) xmalloc (sizeof(*sk));
801 sk->string = (char *) xmalloc (length);
803 memcpy (sk->string, string, length);
805 sk->attrSet = p->attrSet;
806 sk->attrUse = p->attrUse;
809 static void addString (RecWord *p, const char *string, int length)
812 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
813 addSortString (p, string, length);
815 addIndexString (p, string, length);
818 static void addIncompleteField (RecWord *p)
820 const char *b = p->string;
821 int remain = p->length;
822 const char **map = 0;
825 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
829 char buf[IT_MAX_WORD+1];
833 while (map && *map && **map == *CHR_SPACE)
835 remain = p->length - (b - p->string);
837 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
844 while (map && *map && **map != *CHR_SPACE)
846 const char *cp = *map;
848 while (i < IT_MAX_WORD && *cp)
850 remain = p->length - (b - p->string);
852 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
858 addString (p, buf, i);
860 (p->seqnos[p->reg_type])++; /* to separate this from next one */
863 static void addCompleteField (RecWord *p)
865 const char *b = p->string;
866 char buf[IT_MAX_WORD+1];
867 const char **map = 0;
868 int i = 0, remain = p->length;
871 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
873 while (remain > 0 && i < IT_MAX_WORD)
875 while (map && *map && **map == *CHR_SPACE)
877 remain = p->length - (b - p->string);
879 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
886 if (i && i < IT_MAX_WORD)
887 buf[i++] = *CHR_SPACE;
888 while (map && *map && **map != *CHR_SPACE)
890 const char *cp = *map;
892 if (i >= IT_MAX_WORD)
894 while (i < IT_MAX_WORD && *cp)
896 remain = p->length - (b - p->string);
898 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
906 addString (p, buf, i);
909 static void addRecordKey (RecWord *p)
912 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
913 p->string, p->length)))
915 p->string = wrbuf_buf(wrbuf);
916 p->length = wrbuf_len(wrbuf);
918 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
919 addCompleteField (p);
921 addIncompleteField(p);
924 static void flushSortKeys (SYSNO sysno, int cmd)
926 struct sortKey *sk = sortKeys;
928 sortIdx_sysno (sortIdx, sysno);
931 struct sortKey *sk_next = sk->next;
932 sortIdx_type (sortIdx, sk->attrUse);
933 sortIdx_add (sortIdx, sk->string, sk->length);
941 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
945 unsigned char attrSet = (unsigned char) -1;
946 unsigned short attrUse = (unsigned short) -1;
952 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
953 while (off < reckeys->buf_used)
955 const char *src = reckeys->buf + off;
964 memcpy (&ch, src, sizeof(ch));
970 memcpy (&attrSet, src, sizeof(attrSet));
971 src += sizeof(attrSet);
975 memcpy (&attrUse, src, sizeof(attrUse));
976 src += sizeof(attrUse);
979 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
983 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
987 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
990 ch = zebraExplain_addSU (zti, attrSet, attrUse);
991 yaz_log (LOG_LOG, "addSU cmd=%d set=%d use=%d SU=%d",
992 cmd, attrSet, attrUse, ch);
996 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
999 ((char*)key_buf) [key_buf_used++] = *src++;
1001 ((char*)key_buf) [key_buf_used++] = '\0';
1002 ((char*) key_buf)[key_buf_used++] = cmd;
1005 seqno += ((lead>>2) & 15)-1;
1008 memcpy (&seqno, src, sizeof(seqno));
1009 src += sizeof(seqno);
1013 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
1014 key_buf_used += sizeof(key);
1015 off = src - reckeys->buf;
1017 assert (off == reckeys->buf_used);
1020 static const char **searchRecordKey (struct recKeys *reckeys,
1021 int attrSetS, int attrUseS)
1023 static const char *ws[32];
1035 for (i = 0; i<32; i++)
1039 chS = zebraExplain_lookupSU (zti, attrSetS, attrUseS);
1043 while (off < reckeys->buf_used)
1046 const char *src = reckeys->buf + off;
1054 memcpy (&ch, src, sizeof(ch));
1060 memcpy (&attrSet, src, sizeof(attrSet));
1061 src += sizeof(attrSet);
1065 memcpy (&attrUse, src, sizeof(attrUse));
1066 src += sizeof(attrUse);
1073 seqno += ((lead>>2) & 15)-1;
1076 memcpy (&seqno, src, sizeof(seqno));
1077 src += sizeof(seqno);
1083 attrUseS == attrUse && attrSetS == attrSet
1092 woff = seqno - startSeq;
1093 if (woff >= 0 && woff < 31)
1097 off = src - reckeys->buf;
1099 assert (off == reckeys->buf_used);
1103 struct file_read_info {
1104 off_t file_max; /* maximum offset so far */
1105 off_t file_offset; /* current offset */
1106 off_t file_moffset; /* offset of rec/rec boundary */
1113 static struct file_read_info *file_read_start (int fd)
1115 struct file_read_info *fi = (struct file_read_info *)
1116 xmalloc (sizeof(*fi));
1120 fi->file_moffset = 0;
1126 static void file_read_stop (struct file_read_info *fi)
1131 static off_t file_seek (void *handle, off_t offset)
1133 struct file_read_info *p = (struct file_read_info *) handle;
1134 p->file_offset = offset;
1137 return lseek (p->fd, offset, SEEK_SET);
1140 static off_t file_tell (void *handle)
1142 struct file_read_info *p = (struct file_read_info *) handle;
1143 return p->file_offset;
1146 static int file_read (void *handle, char *buf, size_t count)
1148 struct file_read_info *p = (struct file_read_info *) handle;
1154 if (r > p->sdrmax - p->file_offset)
1155 r = p->sdrmax - p->file_offset;
1157 memcpy (buf, p->sdrbuf + p->file_offset, r);
1160 r = read (fd, buf, count);
1163 p->file_offset += r;
1164 if (p->file_offset > p->file_max)
1165 p->file_max = p->file_offset;
1170 static void file_begin (void *handle)
1172 struct file_read_info *p = (struct file_read_info *) handle;
1174 p->file_offset = p->file_moffset;
1175 if (!p->sdrbuf && p->file_moffset)
1176 lseek (p->fd, p->file_moffset, SEEK_SET);
1180 static void file_end (void *handle, off_t offset)
1182 struct file_read_info *p = (struct file_read_info *) handle;
1184 assert (p->file_more == 0);
1186 p->file_moffset = offset;
1189 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1190 const char *fname, const char *spec)
1192 static char dstBuf[2048];
1194 const char *s = spec;
1195 static const char **w;
1199 while (*s == ' ' || *s == '\t')
1205 char attset_str[64], attname_str[64];
1206 data1_attset *attset;
1209 int attSet = 1, attUse = 1;
1213 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1215 attset_str[i++] = *s;
1216 attset_str[i] = '\0';
1221 for (i = 0; *s && *s != ')'; s++)
1223 attname_str[i++] = *s;
1224 attname_str[i] = '\0';
1227 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1230 attSet = attset->reference;
1231 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1233 attUse = att->value;
1235 attUse = atoi (attname_str);
1237 w = searchRecordKey (reckeys, attSet, attUse);
1242 for (i = 0; i<32; i++)
1247 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1248 spec, rGroup->groupName ? rGroup->groupName : "none");
1253 for (i = 0; i<32; i++)
1254 if (matchFlag[i] && w[i])
1262 dst += strlen(w[i]);
1266 logf (LOG_WARN, "Record didn't contain match"
1267 " fields in (%s,%s)", attset_str, attname_str);
1275 const char *spec_src = NULL;
1276 const char *s1 = ++s;
1277 while (*s1 && *s1 != ' ' && *s1 != '\t')
1283 memcpy (special, s, spec_len);
1284 special[spec_len] = '\0';
1287 if (!strcmp (special, "group"))
1288 spec_src = rGroup->groupName;
1289 else if (!strcmp (special, "database"))
1290 spec_src = rGroup->databaseName;
1291 else if (!strcmp (special, "filename"))
1293 else if (!strcmp (special, "type"))
1294 spec_src = rGroup->recordType;
1299 strcpy (dst, spec_src);
1300 dst += strlen (spec_src);
1303 else if (*s == '\"' || *s == '\'')
1305 int stopMarker = *s++;
1309 while (*s && *s != stopMarker)
1312 tmpString[i++] = *s++;
1316 tmpString[i] = '\0';
1317 strcpy (dst, tmpString);
1318 dst += strlen (tmpString);
1322 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1323 spec, rGroup->groupName ? rGroup->groupName : "none");
1330 logf (LOG_WARN, "No match criteria for record %s in group %s",
1331 fname, rGroup->groupName ? rGroup->groupName : "none");
1338 struct recordLogInfo {
1341 struct recordGroup *rGroup;
1344 static void recordLogPreamble (int level, const char *msg, void *info)
1346 struct recordLogInfo *p = (struct recordLogInfo *) info;
1347 FILE *outf = yaz_log_file ();
1349 if (level & LOG_LOG)
1351 fprintf (outf, "File %s, offset %d, type %s\n",
1352 p->rGroup->recordType, p->recordOffset, p->fname);
1353 log_event_start (NULL, NULL);
1356 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1358 zebraExplain_addSchema (zti, oid);
1361 static int recordExtract (SYSNO *sysno, const char *fname,
1362 struct recordGroup *rGroup, int deleteFlag,
1363 struct file_read_info *fi,
1364 RecType recType, char *subType, void *clientData)
1366 RecordAttr *recordAttr;
1371 struct recordLogInfo logInfo;
1372 off_t recordOffset = 0;
1376 struct recExtractCtrl extractCtrl;
1378 /* we are going to read from a file, so prepare the extraction */
1381 reckeys.buf_used = 0;
1382 reckeys.prevAttrUse = -1;
1383 reckeys.prevAttrSet = -1;
1384 reckeys.prevSeqNo = 0;
1386 recordOffset = fi->file_moffset;
1387 extractCtrl.offset = fi->file_moffset;
1388 extractCtrl.readf = file_read;
1389 extractCtrl.seekf = file_seek;
1390 extractCtrl.tellf = file_tell;
1391 extractCtrl.endf = file_end;
1392 extractCtrl.fh = fi;
1393 extractCtrl.subType = subType;
1394 extractCtrl.init = wordInit;
1395 extractCtrl.tokenAdd = addRecordKey;
1396 extractCtrl.schemaAdd = addSchema;
1397 extractCtrl.dh = rGroup->dh;
1398 for (i = 0; i<256; i++)
1400 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1401 extractCtrl.seqno[i] = 1;
1403 extractCtrl.seqno[i] = 0;
1405 extractCtrl.zebra_maps = rGroup->zebra_maps;
1406 extractCtrl.flagShowRecords = !rGroup->flagRw;
1408 if (!rGroup->flagRw)
1409 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
1411 logInfo.fname = fname;
1412 logInfo.recordOffset = recordOffset;
1413 logInfo.rGroup = rGroup;
1414 log_event_start (recordLogPreamble, &logInfo);
1416 r = (*recType->extract)(clientData, &extractCtrl);
1418 log_event_start (NULL, NULL);
1420 if (r == RECCTRL_EXTRACT_EOF)
1422 else if (r == RECCTRL_EXTRACT_ERROR)
1424 /* error occured during extraction ... */
1425 if (rGroup->flagRw &&
1426 records_processed < rGroup->fileVerboseLimit)
1428 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
1429 fname, recordOffset);
1433 if (reckeys.buf_used == 0)
1435 /* the extraction process returned no information - the record
1436 is probably empty - unless flagShowRecords is in use */
1437 if (!rGroup->flagRw)
1440 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
1441 fname, recordOffset);
1446 /* perform match if sysno not known and if match criteria is specified */
1453 if (rGroup->recordId && *rGroup->recordId)
1457 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1461 rinfo = dict_lookup (matchDict, matchStr);
1463 memcpy (sysno, rinfo+1, sizeof(*sysno));
1467 logf (LOG_WARN, "Bad match criteria");
1478 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
1479 fname, recordOffset);
1480 logf (LOG_WARN, "cannot delete record above (seems new)");
1483 if (records_processed < rGroup->fileVerboseLimit)
1484 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
1485 fname, recordOffset);
1486 rec = rec_new (records);
1488 *sysno = rec->sysno;
1490 recordAttr = rec_init_attr (zti, rec);
1494 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1496 flushRecordKeys (*sysno, 1, &reckeys);
1497 flushSortKeys (*sysno, 1);
1503 /* record already exists */
1504 struct recKeys delkeys;
1506 rec = rec_get (records, *sysno);
1509 recordAttr = rec_init_attr (zti, rec);
1511 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1513 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
1514 fname, recordOffset);
1519 delkeys.buf_used = rec->size[recInfo_delKeys];
1520 delkeys.buf = rec->info[recInfo_delKeys];
1521 flushSortKeys (*sysno, 0);
1522 flushRecordKeys (*sysno, 0, &delkeys);
1525 /* record going to be deleted */
1526 if (!delkeys.buf_used)
1528 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1529 rGroup->recordType, fname, recordOffset);
1530 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1534 if (records_processed < rGroup->fileVerboseLimit)
1535 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1536 rGroup->recordType, fname, recordOffset);
1539 dict_delete (matchDict, matchStr);
1540 rec_del (records, &rec);
1548 /* record going to be updated */
1549 if (!delkeys.buf_used)
1551 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1552 rGroup->recordType, fname, recordOffset);
1553 logf (LOG_WARN, "cannot update file above, storeKeys false");
1557 if (records_processed < rGroup->fileVerboseLimit)
1558 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1559 rGroup->recordType, fname, recordOffset);
1560 flushRecordKeys (*sysno, 1, &reckeys);
1565 /* update file type */
1566 xfree (rec->info[recInfo_fileType]);
1567 rec->info[recInfo_fileType] =
1568 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1570 /* update filename */
1571 xfree (rec->info[recInfo_filename]);
1572 rec->info[recInfo_filename] =
1573 rec_strdup (fname, &rec->size[recInfo_filename]);
1575 /* update delete keys */
1576 xfree (rec->info[recInfo_delKeys]);
1577 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1580 rec->size[recInfo_delKeys] = reckeys.buf_used;
1581 rec->info[recInfo_delKeys] = reckeys.buf;
1583 reckeys.buf_max = 0;
1585 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1586 rec->size[recInfo_delKeys] = reckeys.buf_used;
1587 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1588 rec->size[recInfo_delKeys]);
1593 rec->info[recInfo_delKeys] = NULL;
1594 rec->size[recInfo_delKeys] = 0;
1597 /* save file size of original record */
1598 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1599 recordAttr->recordSize = fi->file_moffset - recordOffset;
1600 if (!recordAttr->recordSize)
1601 recordAttr->recordSize = fi->file_max - recordOffset;
1602 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1604 /* set run-number for this record */
1605 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1607 /* update store data */
1608 xfree (rec->info[recInfo_storeData]);
1609 if (rGroup->flagStoreData == 1)
1611 rec->size[recInfo_storeData] = recordAttr->recordSize;
1612 rec->info[recInfo_storeData] = (char *)
1613 xmalloc (recordAttr->recordSize);
1614 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1616 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
1617 recordOffset, fname);
1620 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1621 < recordAttr->recordSize)
1623 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1624 recordAttr->recordSize, fname);
1630 rec->info[recInfo_storeData] = NULL;
1631 rec->size[recInfo_storeData] = 0;
1633 /* update database name */
1634 xfree (rec->info[recInfo_databaseName]);
1635 rec->info[recInfo_databaseName] =
1636 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1639 recordAttr->recordOffset = recordOffset;
1641 /* commit this record */
1642 rec_put (records, &rec);
1647 int fileExtract (SYSNO *sysno, const char *fname,
1648 const struct recordGroup *rGroupP, int deleteFlag)
1656 struct recordGroup rGroupM;
1657 struct recordGroup *rGroup = &rGroupM;
1658 struct file_read_info *fi;
1661 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1663 if (!rGroup->groupName || !*rGroup->groupName)
1666 sprintf (gprefix, "%s.", rGroup->groupName);
1668 logf (LOG_DEBUG, "fileExtract %s", fname);
1670 /* determine file extension */
1672 for (i = strlen(fname); --i >= 0; )
1673 if (fname[i] == '/')
1675 else if (fname[i] == '.')
1677 strcpy (ext, fname+i+1);
1680 /* determine file type - depending on extension */
1681 if (!rGroup->recordType)
1683 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1684 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1686 sprintf (ext_res, "%srecordType", gprefix);
1687 rGroup->recordType = res_get (common_resource, ext_res);
1690 if (!rGroup->recordType)
1692 if (records_processed < rGroup->fileVerboseLimit)
1693 logf (LOG_LOG, "? %s", fname);
1696 if (!*rGroup->recordType)
1699 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1702 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1706 /* determine match criteria */
1707 if (!rGroup->recordId)
1709 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1710 rGroup->recordId = res_get (common_resource, ext_res);
1713 /* determine database name */
1714 if (!rGroup->databaseName)
1716 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1717 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1719 sprintf (ext_res, "%sdatabase", gprefix);
1720 rGroup->databaseName = res_get (common_resource, ext_res);
1723 if (!rGroup->databaseName)
1724 rGroup->databaseName = "Default";
1726 /* determine if explain database */
1728 sprintf (ext_res, "%sexplainDatabase", gprefix);
1729 rGroup->explainDatabase =
1730 atoi (res_get_def (common_resource, ext_res, "0"));
1732 /* announce database */
1733 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1735 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1736 rGroup->explainDatabase))
1740 if (rGroup->flagStoreData == -1)
1743 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1744 if (!(sval = res_get (common_resource, ext_res)))
1746 sprintf (ext_res, "%sstoreData", gprefix);
1747 sval = res_get (common_resource, ext_res);
1750 rGroup->flagStoreData = atoi (sval);
1752 if (rGroup->flagStoreData == -1)
1753 rGroup->flagStoreData = 0;
1755 if (rGroup->flagStoreKeys == -1)
1759 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1760 if (!(sval = res_get (common_resource, ext_res)))
1762 sprintf (ext_res, "%sstoreKeys", gprefix);
1763 sval = res_get (common_resource, ext_res);
1766 rGroup->flagStoreKeys = atoi (sval);
1768 if (rGroup->flagStoreKeys == -1)
1769 rGroup->flagStoreKeys = 0;
1771 if (sysno && deleteFlag)
1775 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1777 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1781 fi = file_read_start (fd);
1785 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1786 recType, subType, clientData);
1787 } while (r && !sysno && fi->file_more);
1788 file_read_stop (fi);
1794 static int explain_extract (void *handle, Record rec, data1_node *n)
1796 struct recordGroup *rGroup = (struct recordGroup*) handle;
1797 struct recExtractCtrl extractCtrl;
1800 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1802 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1806 reckeys.buf_used = 0;
1807 reckeys.prevAttrUse = -1;
1808 reckeys.prevAttrSet = -1;
1809 reckeys.prevSeqNo = 0;
1811 extractCtrl.init = wordInit;
1812 extractCtrl.tokenAdd = addRecordKey;
1813 extractCtrl.schemaAdd = addSchema;
1814 extractCtrl.dh = rGroup->dh;
1815 for (i = 0; i<256; i++)
1816 extractCtrl.seqno[i] = 0;
1817 extractCtrl.zebra_maps = rGroup->zebra_maps;
1818 extractCtrl.flagShowRecords = !rGroup->flagRw;
1820 grs_extract_tree(&extractCtrl, n);
1822 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1824 if (rec->size[recInfo_delKeys])
1826 struct recKeys delkeys;
1828 delkeys.buf_used = rec->size[recInfo_delKeys];
1829 delkeys.buf = rec->info[recInfo_delKeys];
1830 flushSortKeys (rec->sysno, 0);
1831 flushRecordKeys (rec->sysno, 0, &delkeys);
1833 flushRecordKeys (rec->sysno, 1, &reckeys);
1834 flushSortKeys (rec->sysno, 1);
1836 xfree (rec->info[recInfo_delKeys]);
1837 rec->size[recInfo_delKeys] = reckeys.buf_used;
1838 rec->info[recInfo_delKeys] = reckeys.buf;
1840 reckeys.buf_max = 0;