2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.101 2000-05-15 13:02:39 adam
10 * Revision 1.100 2000/03/20 19:08:36 adam
11 * Added remote record import using Z39.50 extended services and Segment
14 * Revision 1.99 2000/02/24 10:57:02 adam
15 * Sequence number incremented after each incomplete-field.
17 * Revision 1.98 1999/09/07 07:19:21 adam
18 * Work on character mapping. Implemented replace rules.
20 * Revision 1.97 1999/07/06 12:28:04 adam
21 * Updated record index structure. Format includes version ID. Compression
22 * algorithm ID is stored for each record block.
24 * Revision 1.96 1999/05/26 07:49:13 adam
27 * Revision 1.95 1999/05/21 12:00:17 adam
28 * Better diagnostics for extraction process.
30 * Revision 1.94 1999/05/20 12:57:18 adam
31 * Implemented TCL filter. Updated recctrl system.
33 * Revision 1.93 1999/05/15 14:36:38 adam
34 * Updated dictionary. Implemented "compression" of dictionary.
36 * Revision 1.92 1999/03/09 16:27:49 adam
37 * More work on SDRKit integration.
39 * Revision 1.91 1999/02/12 13:29:22 adam
40 * Implemented position-flag for registers.
42 * Revision 1.90 1999/02/02 14:50:52 adam
43 * Updated WIN32 code specific sections. Changed header.
45 * Revision 1.89 1998/10/28 10:54:38 adam
48 * Revision 1.88 1998/10/16 08:14:29 adam
49 * Updated record control system.
51 * Revision 1.87 1998/10/15 13:10:33 adam
52 * Fixed bug in Zebra that caused it to stop indexing when empty
55 * Revision 1.86 1998/10/13 20:33:53 adam
56 * Fixed one log message and change use ordinal to be an unsigned char.
58 * Revision 1.85 1998/09/22 10:03:41 adam
59 * Changed result sets to be persistent in the sense that they can
60 * be re-searched if needed.
61 * Fixed memory leak in rsm_or.
63 * Revision 1.84 1998/06/11 15:42:22 adam
64 * Changed the way use attributes are specified in the recordId
67 * Revision 1.83 1998/06/08 14:43:10 adam
68 * Added suport for EXPLAIN Proxy servers - added settings databasePath
69 * and explainDatabase to facilitate this. Increased maximum number
70 * of databases and attributes in one register.
72 * Revision 1.82 1998/05/20 10:12:15 adam
73 * Implemented automatic EXPLAIN database maintenance.
74 * Modified Zebra to work with ASN.1 compiled version of YAZ.
76 * Revision 1.81 1998/03/11 11:19:04 adam
77 * Changed the way sequence numbers are generated.
79 * Revision 1.80 1998/03/05 08:45:11 adam
80 * New result set model and modular ranking system. Moved towards
81 * descent server API. System information stored as "SGML" records.
83 * Revision 1.79 1998/02/17 10:32:52 adam
84 * Fixed bug: binary files weren't opened with flag b on NT.
86 * Revision 1.78 1998/02/10 12:03:05 adam
89 * Revision 1.77 1998/01/12 15:04:08 adam
90 * The test option (-s) only uses read-lock (and not write lock).
92 * Revision 1.76 1997/10/27 14:33:04 adam
93 * Moved towards generic character mapping depending on "structure"
94 * field in abstract syntax file. Fixed a few memory leaks. Fixed
95 * bug with negative integers when doing searches with relational
98 * Revision 1.75 1997/09/17 12:19:12 adam
99 * Zebra version corresponds to YAZ version 1.4.
100 * Changed Zebra server so that it doesn't depend on global common_resource.
102 * Revision 1.74 1997/09/09 13:38:06 adam
103 * Partial port to WIN95/NT.
105 * Revision 1.73 1997/09/04 13:57:20 adam
106 * New file extract/retrieve method tellf (added).
107 * Added O_BINARY for open calls.
109 * Revision 1.72 1997/07/15 16:32:29 adam
110 * Bug fix: Match handler didn't terminate the resulting string!
112 * Revision 1.71 1997/07/15 16:28:41 adam
113 * Bug fix: storeData didn't work with files with multiple records.
114 * Bug fix: fixed memory management with records; not really well
117 * Revision 1.70 1997/07/01 13:00:42 adam
118 * Bug fix in routine searchRecordKey: uninitialized variables.
120 * Revision 1.69 1997/04/29 09:26:03 adam
121 * Bug fix: generic recordId handling didn't work for compressed internal
124 * Revision 1.68 1997/02/12 20:39:45 adam
125 * Implemented options -f <n> that limits the log to the first <n>
127 * Changed some log messages also.
129 * Revision 1.67 1996/11/15 15:02:14 adam
130 * Minor changes regarding logging.
132 * Revision 1.66 1996/11/14 09:52:21 adam
133 * Strings in record keys bound by IT_MAX_WORD.
135 * Revision 1.65 1996/11/14 08:57:56 adam
136 * Reduction of storeKeys area.
138 * Revision 1.64 1996/11/08 11:10:16 adam
139 * Buffers used during file match got bigger.
140 * Compressed ISAM support everywhere.
141 * Bug fixes regarding masking characters in queries.
142 * Redesigned Regexp-2 queries.
144 * Revision 1.63 1996/10/29 14:09:39 adam
145 * Use of cisam system - enabled if setting isamc is 1.
147 * Revision 1.62 1996/10/11 10:57:01 adam
148 * New module recctrl. Used to manage records (extract/retrieval).
149 * Several files have been moved to the recctrl sub directory.
151 * Revision 1.61 1996/06/06 12:08:37 quinn
152 * Added showRecord function
154 * Revision 1.60 1996/06/04 10:18:12 adam
155 * Search/scan uses character mapping module.
157 * Revision 1.59 1996/05/14 15:47:07 adam
158 * Cleanup of various buffer size entities.
160 * Revision 1.58 1996/05/14 06:16:38 adam
161 * Compact use/set bytes used in search service.
163 * Revision 1.57 1996/05/13 14:23:04 adam
164 * Work on compaction of set/use bytes in dictionary.
166 * Revision 1.56 1996/05/09 09:54:42 adam
167 * Server supports maps from one logical attributes to a list of physical
169 * The extraction process doesn't make space consuming 'any' keys.
171 * Revision 1.55 1996/05/09 07:28:55 quinn
172 * Work towards phrases and multiple registers
174 * Revision 1.54 1996/05/01 13:46:35 adam
175 * First work on multiple records in one file.
176 * New option, -offset, to the "unread" command in the filter module.
178 * Revision 1.53 1996/04/26 12:09:43 adam
179 * Added a few comments.
181 * Revision 1.52 1996/04/25 13:27:57 adam
182 * Function recordExtract modified so that files with no keys (possibly empty)
185 * Revision 1.51 1996/03/19 11:08:42 adam
186 * Bug fix: Log preamble wasn't always turned off after recordExtract.
188 * Revision 1.50 1996/02/12 18:45:36 adam
189 * New fileVerboseFlag in record group control.
191 * Revision 1.49 1996/02/05 12:29:57 adam
192 * Logging reduced a bit.
193 * The remaining running time is estimated during register merge.
195 * Revision 1.48 1996/02/01 20:53:26 adam
196 * The temporary per-record keys are compacted a little, and duplication
197 * of the per-records keys are avoided when they are saved in the record
198 * information buffer.
200 * Revision 1.47 1996/01/17 14:57:48 adam
201 * Prototype changed for reader functions in extract/retrieve. File
202 * is identified by 'void *' instead of 'int.
204 * Revision 1.46 1995/12/15 14:57:16 adam
207 * Revision 1.45 1995/12/15 12:37:41 adam
208 * In addRecordKeyAny: Writes key only when attrSet != -1.
210 * Revision 1.44 1995/12/12 16:00:54 adam
211 * System call sync(2) used after update/commit.
212 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
215 * Revision 1.43 1995/12/11 09:12:46 adam
216 * The rec_get function returns NULL if record doesn't exist - will
217 * happen in the server if the result set records have been deleted since
218 * the creation of the set (i.e. the search).
219 * The server saves a result temporarily if it is 'volatile', i.e. the
220 * set is register dependent.
222 * Revision 1.42 1995/12/07 17:38:46 adam
223 * Work locking mechanisms for concurrent updates/commit.
225 * Revision 1.41 1995/12/06 16:06:42 adam
226 * Better diagnostics. Work on 'real' dictionary deletion.
228 * Revision 1.40 1995/12/05 16:57:40 adam
229 * More work on regular patterns.
231 * Revision 1.39 1995/12/05 13:20:18 adam
232 * Bug fix: file_read sometimes returned early EOF.
234 * Revision 1.38 1995/12/04 17:59:21 adam
235 * More work on regular expression conversion.
237 * Revision 1.37 1995/12/04 14:22:27 adam
238 * Extra arg to recType_byName.
239 * Started work on new regular expression parsed input to
240 * structured records.
242 * Revision 1.36 1995/11/30 08:34:29 adam
243 * Started work on commit facility.
244 * Changed a few malloc/free to xmalloc/xfree.
246 * Revision 1.35 1995/11/28 14:26:21 adam
247 * Bug fix: recordId with constant wasn't right.
248 * Bug fix: recordId dictionary entry wasn't deleted when needed.
250 * Revision 1.34 1995/11/28 09:09:38 adam
251 * Zebra config renamed.
252 * Use setting 'recordId' to identify record now.
253 * Bug fix in recindex.c: rec_release_blocks was invokeded even
254 * though the blocks were already released.
255 * File traversal properly deletes records when needed.
257 * Revision 1.33 1995/11/27 09:56:20 adam
258 * Record info elements better enumerated. Internal store of records.
260 * Revision 1.32 1995/11/25 10:24:05 adam
261 * More record fields - they are enumerated now.
262 * New options: flagStoreData flagStoreKey.
264 * Revision 1.31 1995/11/24 11:31:35 adam
265 * Commands add & del read filenames from stdin if source directory is
267 * Match criteria supports 'constant' strings.
269 * Revision 1.30 1995/11/22 17:19:16 adam
270 * Record management uses the bfile system.
272 * Revision 1.29 1995/11/21 15:01:14 adam
273 * New general match criteria implemented.
274 * New feature: document groups.
276 * Revision 1.28 1995/11/21 09:20:30 adam
277 * Yet more work on record match.
279 * Revision 1.27 1995/11/20 16:59:45 adam
280 * New update method: the 'old' keys are saved for each records.
282 * Revision 1.26 1995/11/20 11:56:24 adam
283 * Work on new traversal.
285 * Revision 1.25 1995/11/16 15:34:54 adam
286 * Uses new record management system in both indexer and server.
288 * Revision 1.24 1995/11/15 19:13:08 adam
289 * Work on record management.
291 * Revision 1.23 1995/10/27 14:00:10 adam
292 * Implemented detection of database availability.
294 * Revision 1.22 1995/10/17 18:02:07 adam
295 * New feature: databases. Implemented as prefix to words in dictionary.
297 * Revision 1.21 1995/10/10 12:24:38 adam
298 * Temporary sort files are compressed.
300 * Revision 1.20 1995/10/06 13:52:05 adam
301 * Bug fixes. Handler may abort further scanning.
303 * Revision 1.19 1995/10/04 12:55:16 adam
304 * Bug fix in ranked search. Use=Any keys inserted.
306 * Revision 1.18 1995/10/04 09:37:08 quinn
309 * Revision 1.17 1995/10/03 14:28:57 adam
310 * Buffered read in extract works.
312 * Revision 1.16 1995/10/03 14:28:45 adam
313 * Work on more effecient read handler in extract.
315 * Revision 1.15 1995/10/02 15:42:53 adam
316 * Extract uses file descriptors instead of FILE pointers.
318 * Revision 1.14 1995/10/02 15:29:13 adam
319 * More logging in file_extract.
321 * Revision 1.13 1995/09/29 14:01:39 adam
324 * Revision 1.12 1995/09/28 14:22:56 adam
325 * Sort uses smaller temporary files.
327 * Revision 1.11 1995/09/28 12:10:31 adam
328 * Bug fixes. Field prefix used in queries.
330 * Revision 1.10 1995/09/28 09:19:41 adam
331 * xfree/xmalloc used everywhere.
332 * Extract/retrieve method seems to work for text records.
334 * Revision 1.9 1995/09/27 12:22:28 adam
335 * More work on extract in record control.
336 * Field name is not in isam keys but in prefix in dictionary words.
338 * Revision 1.8 1995/09/14 07:48:22 adam
339 * Record control management.
341 * Revision 1.7 1995/09/11 13:09:32 adam
342 * More work on relevance feedback.
344 * Revision 1.6 1995/09/08 14:52:27 adam
345 * Minor changes. Dictionary is lower case now.
347 * Revision 1.5 1995/09/06 16:11:16 adam
348 * Option: only one word key per file.
350 * Revision 1.4 1995/09/05 15:28:39 adam
351 * More work on search engine.
353 * Revision 1.3 1995/09/04 12:33:41 adam
354 * Various cleanup. YAZ util used instead.
356 * Revision 1.2 1995/09/04 09:10:34 adam
357 * More work on index add/del/update.
358 * Merge sort implemented.
359 * Initial work on z39 server.
361 * Revision 1.1 1995/09/01 14:06:35 adam
362 * Split of work into more files.
386 #include "zebrasdr.h"
389 static Dict matchDict;
391 static Records records = NULL;
392 static SortIdx sortIdx = NULL;
394 static char **key_buf;
395 static size_t ptr_top;
397 static size_t key_buf_used;
398 static int key_file_no;
400 static int records_inserted = 0;
401 static int records_updated = 0;
402 static int records_deleted = 0;
403 static int records_processed = 0;
405 static ZebraExplainInfo zti = NULL;
407 static void logRecord (int showFlag)
411 if (showFlag || !(records_processed % 1000))
413 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
414 records_processed, records_inserted, records_updated,
419 static int explain_extract (void *handle, Record drec, data1_node *n);
421 int key_open (struct recordGroup *rGroup, int mem)
423 BFiles bfs = rGroup->bfs;
424 int rw = rGroup->flagRw;
425 data1_handle dh = rGroup->dh;
426 char *recordCompression;
427 int record_compression = REC_COMPRESS_NONE;
429 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
432 key_buf = (char **) xmalloc (mem);
433 ptr_top = mem/sizeof(char*);
439 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
441 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
445 recordCompression = res_get_def (common_resource,
446 "recordCompression", "none");
447 if (!strcmp (recordCompression, "none"))
448 record_compression = REC_COMPRESS_NONE;
449 if (!strcmp (recordCompression, "bzip2"))
450 record_compression = REC_COMPRESS_BZIP2;
451 records = rec_open (bfs, rw, record_compression);
454 dict_close (matchDict);
457 zti = zebraExplain_open (records, dh, common_resource,
458 rw, rGroup, explain_extract);
461 rec_close (&records);
462 dict_close (matchDict);
465 sortIdx = sortIdx_open (bfs, 1);
476 void encode_key_init (struct encode_info *i)
483 char *encode_key_int (int d, char *bp)
492 else if (d <= 4194303)
494 *bp++ = 128 + (d>>16);
495 *bp++ = (d>>8) & 255;
500 *bp++ = 192 + (d>>24);
501 *bp++ = (d>>16) & 255;
502 *bp++ = (d>>8) & 255;
508 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
513 while ((*bp++ = *k++))
515 memcpy (&key, k+1, sizeof(struct it_key));
516 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
517 if (i->sysno != key.sysno)
519 i->sysno = key.sysno;
522 else if (!i->seqno && !key.seqno && i->cmd == *k)
524 bp = encode_key_int (key.seqno - i->seqno, bp);
525 i->seqno = key.seqno;
527 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
529 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
537 static int key_y_len;
539 static int key_y_compare (const void *p1, const void *p2)
543 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
544 *(char**) p2 + key_y_len + 1)))
546 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
549 static int key_x_compare (const void *p1, const void *p2)
551 return strcmp (*(char**) p1, *(char**) p2);
555 void key_flush (void)
560 struct encode_info encode_info;
569 logf (LOG_LOG, "sorting section %d", key_file_no);
571 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
572 getFnameTmp (common_resource, out_fname, key_file_no);
574 if (!(outf = fopen (out_fname, "wb")))
576 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
579 logf (LOG_LOG, "writing section %d", key_file_no);
580 prevcp = cp = key_buf[ptr_top-ptr_i];
582 encode_key_init (&encode_info);
583 encode_key_write (cp, &encode_info, outf);
586 cp = key_buf[ptr_top-ptr_i];
587 if (strcmp (cp, prevcp))
589 encode_key_init (&encode_info);
590 encode_key_write (cp, &encode_info, outf);
594 encode_key_write (cp + strlen(cp), &encode_info, outf);
597 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
598 getFnameTmp (out_fname, key_file_no);
600 if (!(outf = fopen (out_fname, "wb")))
602 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
605 logf (LOG_LOG, "writing section %d", key_file_no);
607 prevcp = key_buf[ptr_top-i];
609 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
611 key_y_len = strlen(prevcp)+1;
613 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
614 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
616 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
617 sizeof(char*), key_y_compare);
618 cp = key_buf[ptr_top-ptr_i];
620 encode_key_init (&encode_info);
621 encode_key_write (cp, &encode_info, outf);
624 cp = key_buf[ptr_top-ptr_i];
625 encode_key_write (cp+key_y_len, &encode_info, outf);
629 prevcp = key_buf[ptr_top-ptr_i];
634 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
637 logf (LOG_LOG, "finished section %d", key_file_no);
642 int key_close (struct recordGroup *rGroup)
644 int rw = rGroup->flagRw;
646 zebraExplain_runNumberIncrement (zti, 1);
647 zebraExplain_close (zti, rw);
650 rec_close (&records);
651 dict_close (matchDict);
652 sortIdx_close (sortIdx);
658 static void wordInit (struct recExtractCtrl *p, RecWord *w)
660 w->zebra_maps = p->zebra_maps;
661 w->seqnos = p->seqno;
662 w->attrSet = VAL_BIB1;
668 static struct sortKey {
673 struct sortKey *next;
676 static struct recKeys {
685 static void addIndexString (RecWord *p, const char *string, int length)
688 unsigned char attrSet;
689 unsigned short attrUse;
692 int *pseqno = &p->seqnos[p->reg_type];
694 if (reckeys.buf_used+1024 > reckeys.buf_max)
698 b = (char *) xmalloc (reckeys.buf_max += 128000);
699 if (reckeys.buf_used > 0)
700 memcpy (b, reckeys.buf, reckeys.buf_used);
704 dst = reckeys.buf + reckeys.buf_used;
706 attrSet = p->attrSet;
707 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
710 reckeys.prevAttrSet = attrSet;
711 attrUse = p->attrUse;
712 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
715 reckeys.prevAttrUse = attrUse;
717 diff = 1 + *pseqno - reckeys.prevSeqNo;
718 if (diff >= 1 && diff <= 15)
723 reckeys.prevSeqNo = *pseqno;
729 memcpy (dst, &attrSet, sizeof(attrSet));
730 dst += sizeof(attrSet);
734 memcpy (dst, &attrUse, sizeof(attrUse));
735 dst += sizeof(attrUse);
737 *dst++ = p->reg_type;
738 memcpy (dst, string, length);
744 memcpy (dst, pseqno, sizeof(*pseqno));
745 dst += sizeof(*pseqno);
747 reckeys.buf_used = dst - reckeys.buf;
752 static void addSortString (RecWord *p, const char *string, int length)
756 for (sk = sortKeys; sk; sk = sk->next)
757 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
760 sk = (struct sortKey *) xmalloc (sizeof(*sk));
764 sk->string = (char *) xmalloc (length);
766 memcpy (sk->string, string, length);
768 sk->attrSet = p->attrSet;
769 sk->attrUse = p->attrUse;
772 static void addString (RecWord *p, const char *string, int length)
775 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
776 addSortString (p, string, length);
778 addIndexString (p, string, length);
781 static void addIncompleteField (RecWord *p)
783 const char *b = p->string;
784 int remain = p->length;
785 const char **map = 0;
788 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
792 char buf[IT_MAX_WORD+1];
796 while (map && *map && **map == *CHR_SPACE)
798 remain = p->length - (b - p->string);
800 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
807 while (map && *map && **map != *CHR_SPACE)
809 const char *cp = *map;
811 while (i < IT_MAX_WORD && *cp)
813 remain = p->length - (b - p->string);
815 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
821 addString (p, buf, i);
823 (p->seqnos[p->reg_type])++; /* to separate this from next one */
826 static void addCompleteField (RecWord *p)
828 const char *b = p->string;
829 char buf[IT_MAX_WORD+1];
830 const char **map = 0;
831 int i = 0, remain = p->length;
834 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
836 while (remain > 0 && i < IT_MAX_WORD)
838 while (map && *map && **map == *CHR_SPACE)
840 remain = p->length - (b - p->string);
842 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
849 if (i && i < IT_MAX_WORD)
850 buf[i++] = *CHR_SPACE;
851 while (map && *map && **map != *CHR_SPACE)
853 const char *cp = *map;
855 if (i >= IT_MAX_WORD)
857 while (i < IT_MAX_WORD && *cp)
859 remain = p->length - (b - p->string);
861 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
869 addString (p, buf, i);
872 static void addRecordKey (RecWord *p)
875 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
876 p->string, p->length)))
878 p->string = wrbuf_buf(wrbuf);
879 p->length = wrbuf_len(wrbuf);
881 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
882 addCompleteField (p);
884 addIncompleteField(p);
887 static void flushSortKeys (SYSNO sysno, int cmd)
889 struct sortKey *sk = sortKeys;
891 sortIdx_sysno (sortIdx, sysno);
894 struct sortKey *sk_next = sk->next;
895 sortIdx_type (sortIdx, sk->attrUse);
896 sortIdx_add (sortIdx, sk->string, sk->length);
904 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
906 unsigned char attrSet = (unsigned char) -1;
907 unsigned short attrUse = (unsigned short) -1;
911 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
912 while (off < reckeys->buf_used)
914 const char *src = reckeys->buf + off;
922 memcpy (&attrSet, src, sizeof(attrSet));
923 src += sizeof(attrSet);
927 memcpy (&attrUse, src, sizeof(attrUse));
928 src += sizeof(attrUse);
930 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
933 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
935 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
937 ch = zebraExplain_addSU (zti, attrSet, attrUse);
939 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
942 ((char*)key_buf) [key_buf_used++] = *src++;
944 ((char*)key_buf) [key_buf_used++] = '\0';
945 ((char*) key_buf)[key_buf_used++] = cmd;
948 seqno += ((lead>>2) & 15)-1;
951 memcpy (&seqno, src, sizeof(seqno));
952 src += sizeof(seqno);
956 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
957 key_buf_used += sizeof(key);
958 off = src - reckeys->buf;
960 assert (off == reckeys->buf_used);
963 static const char **searchRecordKey (struct recKeys *reckeys,
964 int attrSetS, int attrUseS)
966 static const char *ws[32];
974 for (i = 0; i<32; i++)
977 while (off < reckeys->buf_used)
980 const char *src = reckeys->buf + off;
988 memcpy (&attrSet, src, sizeof(attrSet));
989 src += sizeof(attrSet);
993 memcpy (&attrUse, src, sizeof(attrUse));
994 src += sizeof(attrUse);
1000 seqno += ((lead>>2) & 15)-1;
1003 memcpy (&seqno, src, sizeof(seqno));
1004 src += sizeof(seqno);
1006 if (attrUseS == attrUse && attrSetS == attrSet)
1013 woff = seqno - startSeq;
1014 if (woff >= 0 && woff < 31)
1018 off = src - reckeys->buf;
1020 assert (off == reckeys->buf_used);
1024 struct file_read_info {
1025 off_t file_max; /* maximum offset so far */
1026 off_t file_offset; /* current offset */
1027 off_t file_moffset; /* offset of rec/rec boundary */
1034 static struct file_read_info *file_read_start (int fd)
1036 struct file_read_info *fi = (struct file_read_info *)
1037 xmalloc (sizeof(*fi));
1041 fi->file_moffset = 0;
1047 static void file_read_stop (struct file_read_info *fi)
1052 static off_t file_seek (void *handle, off_t offset)
1054 struct file_read_info *p = (struct file_read_info *) handle;
1055 p->file_offset = offset;
1058 return lseek (p->fd, offset, SEEK_SET);
1061 static off_t file_tell (void *handle)
1063 struct file_read_info *p = (struct file_read_info *) handle;
1064 return p->file_offset;
1067 static int file_read (void *handle, char *buf, size_t count)
1069 struct file_read_info *p = (struct file_read_info *) handle;
1075 if (r > p->sdrmax - p->file_offset)
1076 r = p->sdrmax - p->file_offset;
1078 memcpy (buf, p->sdrbuf + p->file_offset, r);
1081 r = read (fd, buf, count);
1084 p->file_offset += r;
1085 if (p->file_offset > p->file_max)
1086 p->file_max = p->file_offset;
1091 static void file_begin (void *handle)
1093 struct file_read_info *p = (struct file_read_info *) handle;
1095 p->file_offset = p->file_moffset;
1096 if (!p->sdrbuf && p->file_moffset)
1097 lseek (p->fd, p->file_moffset, SEEK_SET);
1101 static void file_end (void *handle, off_t offset)
1103 struct file_read_info *p = (struct file_read_info *) handle;
1105 assert (p->file_more == 0);
1107 p->file_moffset = offset;
1110 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1111 const char *fname, const char *spec)
1113 static char dstBuf[2048];
1115 const char *s = spec;
1116 static const char **w;
1120 while (*s == ' ' || *s == '\t')
1126 char attset_str[64], attname_str[64];
1127 data1_attset *attset;
1130 int attSet = 1, attUse = 1;
1134 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1136 attset_str[i++] = *s;
1137 attset_str[i] = '\0';
1142 for (i = 0; *s && *s != ')'; s++)
1144 attname_str[i++] = *s;
1145 attname_str[i] = '\0';
1148 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1151 attSet = attset->reference;
1152 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1154 attUse = att->value;
1156 attUse = atoi (attname_str);
1158 w = searchRecordKey (reckeys, attSet, attUse);
1163 for (i = 0; i<32; i++)
1168 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1169 spec, rGroup->groupName ? rGroup->groupName : "none");
1174 for (i = 0; i<32; i++)
1175 if (matchFlag[i] && w[i])
1183 dst += strlen(w[i]);
1187 logf (LOG_WARN, "Record didn't contain match"
1188 " fields in (%s,%s)", attset_str, attname_str);
1196 const char *spec_src = NULL;
1197 const char *s1 = ++s;
1198 while (*s1 && *s1 != ' ' && *s1 != '\t')
1204 memcpy (special, s, spec_len);
1205 special[spec_len] = '\0';
1208 if (!strcmp (special, "group"))
1209 spec_src = rGroup->groupName;
1210 else if (!strcmp (special, "database"))
1211 spec_src = rGroup->databaseName;
1212 else if (!strcmp (special, "filename"))
1214 else if (!strcmp (special, "type"))
1215 spec_src = rGroup->recordType;
1220 strcpy (dst, spec_src);
1221 dst += strlen (spec_src);
1224 else if (*s == '\"' || *s == '\'')
1226 int stopMarker = *s++;
1230 while (*s && *s != stopMarker)
1233 tmpString[i++] = *s++;
1237 tmpString[i] = '\0';
1238 strcpy (dst, tmpString);
1239 dst += strlen (tmpString);
1243 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1244 spec, rGroup->groupName ? rGroup->groupName : "none");
1251 logf (LOG_WARN, "No match criteria for record %s in group %s",
1252 fname, rGroup->groupName ? rGroup->groupName : "none");
1259 struct recordLogInfo {
1262 struct recordGroup *rGroup;
1265 static void recordLogPreamble (int level, const char *msg, void *info)
1267 struct recordLogInfo *p = (struct recordLogInfo *) info;
1268 FILE *outf = log_file ();
1270 if (level & LOG_LOG)
1272 fprintf (outf, "File %s, offset %d, type %s\n",
1273 p->rGroup->recordType, p->recordOffset, p->fname);
1274 log_event_start (NULL, NULL);
1277 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1279 zebraExplain_addSchema (zti, oid);
1282 static int recordExtract (SYSNO *sysno, const char *fname,
1283 struct recordGroup *rGroup, int deleteFlag,
1284 struct file_read_info *fi,
1285 RecType recType, char *subType, void *clientData)
1287 RecordAttr *recordAttr;
1292 struct recordLogInfo logInfo;
1293 off_t recordOffset = 0;
1297 struct recExtractCtrl extractCtrl;
1299 /* we are going to read from a file, so prepare the extraction */
1302 reckeys.buf_used = 0;
1303 reckeys.prevAttrUse = -1;
1304 reckeys.prevAttrSet = -1;
1305 reckeys.prevSeqNo = 0;
1307 recordOffset = fi->file_moffset;
1308 extractCtrl.offset = fi->file_moffset;
1309 extractCtrl.readf = file_read;
1310 extractCtrl.seekf = file_seek;
1311 extractCtrl.tellf = file_tell;
1312 extractCtrl.endf = file_end;
1313 extractCtrl.fh = fi;
1314 extractCtrl.subType = subType;
1315 extractCtrl.init = wordInit;
1316 extractCtrl.tokenAdd = addRecordKey;
1317 extractCtrl.schemaAdd = addSchema;
1318 extractCtrl.dh = rGroup->dh;
1319 for (i = 0; i<256; i++)
1321 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1322 extractCtrl.seqno[i] = 1;
1324 extractCtrl.seqno[i] = 0;
1326 extractCtrl.zebra_maps = rGroup->zebra_maps;
1327 extractCtrl.flagShowRecords = !rGroup->flagRw;
1329 if (!rGroup->flagRw)
1330 printf ("File: %s %ld\n", fname, (long) recordOffset);
1332 logInfo.fname = fname;
1333 logInfo.recordOffset = recordOffset;
1334 logInfo.rGroup = rGroup;
1335 log_event_start (recordLogPreamble, &logInfo);
1337 r = (*recType->extract)(clientData, &extractCtrl);
1339 log_event_start (NULL, NULL);
1341 if (r == RECCTRL_EXTRACT_EOF)
1343 else if (r == RECCTRL_EXTRACT_ERROR)
1345 /* error occured during extraction ... */
1346 if (rGroup->flagRw &&
1347 records_processed < rGroup->fileVerboseLimit)
1349 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
1350 fname, (long) recordOffset);
1354 if (reckeys.buf_used == 0)
1356 /* the extraction process returned no information - the record
1357 is probably empty - unless flagShowRecords is in use */
1358 if (!rGroup->flagRw)
1361 logf (LOG_WARN, "empty %s %s %ld", rGroup->recordType,
1362 fname, (long) recordOffset);
1367 /* perform match if sysno not known and if match criteria is specified */
1374 if (rGroup->recordId && *rGroup->recordId)
1378 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1382 rinfo = dict_lookup (matchDict, matchStr);
1384 memcpy (sysno, rinfo+1, sizeof(*sysno));
1388 logf (LOG_WARN, "Bad match criteria");
1399 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1400 fname, (long) recordOffset);
1401 logf (LOG_WARN, "cannot delete record above (seems new)");
1404 if (records_processed < rGroup->fileVerboseLimit)
1405 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1406 fname, (long) recordOffset);
1407 rec = rec_new (records);
1409 *sysno = rec->sysno;
1411 recordAttr = rec_init_attr (zti, rec);
1415 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1417 flushRecordKeys (*sysno, 1, &reckeys);
1418 flushSortKeys (*sysno, 1);
1424 /* record already exists */
1425 struct recKeys delkeys;
1427 rec = rec_get (records, *sysno);
1430 recordAttr = rec_init_attr (zti, rec);
1432 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1434 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1435 fname, (long) recordOffset);
1440 delkeys.buf_used = rec->size[recInfo_delKeys];
1441 delkeys.buf = rec->info[recInfo_delKeys];
1442 flushSortKeys (*sysno, 0);
1443 flushRecordKeys (*sysno, 0, &delkeys);
1446 /* record going to be deleted */
1447 if (!delkeys.buf_used)
1449 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1450 fname, (long) recordOffset);
1451 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1455 if (records_processed < rGroup->fileVerboseLimit)
1456 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1457 fname, (long) recordOffset);
1460 dict_delete (matchDict, matchStr);
1461 rec_del (records, &rec);
1469 /* record going to be updated */
1470 if (!delkeys.buf_used)
1472 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1473 fname, (long) recordOffset);
1474 logf (LOG_WARN, "cannot update file above, storeKeys false");
1478 if (records_processed < rGroup->fileVerboseLimit)
1479 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1480 fname, (long) recordOffset);
1481 flushRecordKeys (*sysno, 1, &reckeys);
1486 /* update file type */
1487 xfree (rec->info[recInfo_fileType]);
1488 rec->info[recInfo_fileType] =
1489 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1491 /* update filename */
1492 xfree (rec->info[recInfo_filename]);
1493 rec->info[recInfo_filename] =
1494 rec_strdup (fname, &rec->size[recInfo_filename]);
1496 /* update delete keys */
1497 xfree (rec->info[recInfo_delKeys]);
1498 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1501 rec->size[recInfo_delKeys] = reckeys.buf_used;
1502 rec->info[recInfo_delKeys] = reckeys.buf;
1504 reckeys.buf_max = 0;
1506 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1507 rec->size[recInfo_delKeys] = reckeys.buf_used;
1508 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1509 rec->size[recInfo_delKeys]);
1514 rec->info[recInfo_delKeys] = NULL;
1515 rec->size[recInfo_delKeys] = 0;
1518 /* save file size of original record */
1519 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1520 recordAttr->recordSize = fi->file_moffset - recordOffset;
1521 if (!recordAttr->recordSize)
1522 recordAttr->recordSize = fi->file_max - recordOffset;
1523 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1525 /* set run-number for this record */
1526 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1528 /* update store data */
1529 xfree (rec->info[recInfo_storeData]);
1530 if (rGroup->flagStoreData == 1)
1532 rec->size[recInfo_storeData] = recordAttr->recordSize;
1533 rec->info[recInfo_storeData] = (char *)
1534 xmalloc (recordAttr->recordSize);
1535 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1537 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1538 (long) recordOffset, fname);
1541 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1542 < recordAttr->recordSize)
1544 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1545 recordAttr->recordSize, fname);
1551 rec->info[recInfo_storeData] = NULL;
1552 rec->size[recInfo_storeData] = 0;
1554 /* update database name */
1555 xfree (rec->info[recInfo_databaseName]);
1556 rec->info[recInfo_databaseName] =
1557 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1560 recordAttr->recordOffset = recordOffset;
1562 /* commit this record */
1563 rec_put (records, &rec);
1568 int fileExtract (SYSNO *sysno, const char *fname,
1569 const struct recordGroup *rGroupP, int deleteFlag)
1577 struct recordGroup rGroupM;
1578 struct recordGroup *rGroup = &rGroupM;
1579 struct file_read_info *fi;
1582 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1584 if (!rGroup->groupName || !*rGroup->groupName)
1587 sprintf (gprefix, "%s.", rGroup->groupName);
1589 logf (LOG_DEBUG, "fileExtract %s", fname);
1591 /* determine file extension */
1593 for (i = strlen(fname); --i >= 0; )
1594 if (fname[i] == '/')
1596 else if (fname[i] == '.')
1598 strcpy (ext, fname+i+1);
1601 /* determine file type - depending on extension */
1602 if (!rGroup->recordType)
1604 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1605 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1607 sprintf (ext_res, "%srecordType", gprefix);
1608 rGroup->recordType = res_get (common_resource, ext_res);
1611 if (!rGroup->recordType)
1613 if (records_processed < rGroup->fileVerboseLimit)
1614 logf (LOG_LOG, "? %s", fname);
1617 if (!*rGroup->recordType)
1620 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1623 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1627 /* determine match criteria */
1628 if (!rGroup->recordId)
1630 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1631 rGroup->recordId = res_get (common_resource, ext_res);
1634 /* determine database name */
1635 if (!rGroup->databaseName)
1637 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1638 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1640 sprintf (ext_res, "%sdatabase", gprefix);
1641 rGroup->databaseName = res_get (common_resource, ext_res);
1644 if (!rGroup->databaseName)
1645 rGroup->databaseName = "Default";
1647 /* determine if explain database */
1649 sprintf (ext_res, "%sexplainDatabase", gprefix);
1650 rGroup->explainDatabase =
1651 atoi (res_get_def (common_resource, ext_res, "0"));
1653 /* announce database */
1654 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1656 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1657 rGroup->explainDatabase))
1661 if (rGroup->flagStoreData == -1)
1664 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1665 if (!(sval = res_get (common_resource, ext_res)))
1667 sprintf (ext_res, "%sstoreData", gprefix);
1668 sval = res_get (common_resource, ext_res);
1671 rGroup->flagStoreData = atoi (sval);
1673 if (rGroup->flagStoreData == -1)
1674 rGroup->flagStoreData = 0;
1676 if (rGroup->flagStoreKeys == -1)
1680 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1681 if (!(sval = res_get (common_resource, ext_res)))
1683 sprintf (ext_res, "%sstoreKeys", gprefix);
1684 sval = res_get (common_resource, ext_res);
1687 rGroup->flagStoreKeys = atoi (sval);
1689 if (rGroup->flagStoreKeys == -1)
1690 rGroup->flagStoreKeys = 0;
1692 if (sysno && deleteFlag)
1696 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1698 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1702 fi = file_read_start (fd);
1706 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1707 recType, subType, clientData);
1708 } while (r && !sysno && fi->file_more);
1709 file_read_stop (fi);
1715 static int explain_extract (void *handle, Record rec, data1_node *n)
1717 struct recordGroup *rGroup = (struct recordGroup*) handle;
1718 struct recExtractCtrl extractCtrl;
1721 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1723 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1727 reckeys.buf_used = 0;
1728 reckeys.prevAttrUse = -1;
1729 reckeys.prevAttrSet = -1;
1730 reckeys.prevSeqNo = 0;
1732 extractCtrl.init = wordInit;
1733 extractCtrl.tokenAdd = addRecordKey;
1734 extractCtrl.schemaAdd = addSchema;
1735 extractCtrl.dh = rGroup->dh;
1736 for (i = 0; i<256; i++)
1737 extractCtrl.seqno[i] = 0;
1738 extractCtrl.zebra_maps = rGroup->zebra_maps;
1739 extractCtrl.flagShowRecords = !rGroup->flagRw;
1741 grs_extract_tree(&extractCtrl, n);
1743 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1745 if (rec->size[recInfo_delKeys])
1747 struct recKeys delkeys;
1749 delkeys.buf_used = rec->size[recInfo_delKeys];
1750 delkeys.buf = rec->info[recInfo_delKeys];
1751 flushSortKeys (rec->sysno, 0);
1752 flushRecordKeys (rec->sysno, 0, &delkeys);
1754 flushRecordKeys (rec->sysno, 1, &reckeys);
1755 flushSortKeys (rec->sysno, 1);
1757 xfree (rec->info[recInfo_delKeys]);
1758 rec->size[recInfo_delKeys] = reckeys.buf_used;
1759 rec->info[recInfo_delKeys] = reckeys.buf;
1761 reckeys.buf_max = 0;