2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.102 2000-05-15 15:32:33 adam
8 * Added 64 bit file input.
10 * Revision 1.101 2000/05/15 13:02:39 adam
13 * Revision 1.100 2000/03/20 19:08:36 adam
14 * Added remote record import using Z39.50 extended services and Segment
17 * Revision 1.99 2000/02/24 10:57:02 adam
18 * Sequence number incremented after each incomplete-field.
20 * Revision 1.98 1999/09/07 07:19:21 adam
21 * Work on character mapping. Implemented replace rules.
23 * Revision 1.97 1999/07/06 12:28:04 adam
24 * Updated record index structure. Format includes version ID. Compression
25 * algorithm ID is stored for each record block.
27 * Revision 1.96 1999/05/26 07:49:13 adam
30 * Revision 1.95 1999/05/21 12:00:17 adam
31 * Better diagnostics for extraction process.
33 * Revision 1.94 1999/05/20 12:57:18 adam
34 * Implemented TCL filter. Updated recctrl system.
36 * Revision 1.93 1999/05/15 14:36:38 adam
37 * Updated dictionary. Implemented "compression" of dictionary.
39 * Revision 1.92 1999/03/09 16:27:49 adam
40 * More work on SDRKit integration.
42 * Revision 1.91 1999/02/12 13:29:22 adam
43 * Implemented position-flag for registers.
45 * Revision 1.90 1999/02/02 14:50:52 adam
46 * Updated WIN32 code specific sections. Changed header.
48 * Revision 1.89 1998/10/28 10:54:38 adam
51 * Revision 1.88 1998/10/16 08:14:29 adam
52 * Updated record control system.
54 * Revision 1.87 1998/10/15 13:10:33 adam
55 * Fixed bug in Zebra that caused it to stop indexing when empty
58 * Revision 1.86 1998/10/13 20:33:53 adam
59 * Fixed one log message and change use ordinal to be an unsigned char.
61 * Revision 1.85 1998/09/22 10:03:41 adam
62 * Changed result sets to be persistent in the sense that they can
63 * be re-searched if needed.
64 * Fixed memory leak in rsm_or.
66 * Revision 1.84 1998/06/11 15:42:22 adam
67 * Changed the way use attributes are specified in the recordId
70 * Revision 1.83 1998/06/08 14:43:10 adam
71 * Added suport for EXPLAIN Proxy servers - added settings databasePath
72 * and explainDatabase to facilitate this. Increased maximum number
73 * of databases and attributes in one register.
75 * Revision 1.82 1998/05/20 10:12:15 adam
76 * Implemented automatic EXPLAIN database maintenance.
77 * Modified Zebra to work with ASN.1 compiled version of YAZ.
79 * Revision 1.81 1998/03/11 11:19:04 adam
80 * Changed the way sequence numbers are generated.
82 * Revision 1.80 1998/03/05 08:45:11 adam
83 * New result set model and modular ranking system. Moved towards
84 * descent server API. System information stored as "SGML" records.
86 * Revision 1.79 1998/02/17 10:32:52 adam
87 * Fixed bug: binary files weren't opened with flag b on NT.
89 * Revision 1.78 1998/02/10 12:03:05 adam
92 * Revision 1.77 1998/01/12 15:04:08 adam
93 * The test option (-s) only uses read-lock (and not write lock).
95 * Revision 1.76 1997/10/27 14:33:04 adam
96 * Moved towards generic character mapping depending on "structure"
97 * field in abstract syntax file. Fixed a few memory leaks. Fixed
98 * bug with negative integers when doing searches with relational
101 * Revision 1.75 1997/09/17 12:19:12 adam
102 * Zebra version corresponds to YAZ version 1.4.
103 * Changed Zebra server so that it doesn't depend on global common_resource.
105 * Revision 1.74 1997/09/09 13:38:06 adam
106 * Partial port to WIN95/NT.
108 * Revision 1.73 1997/09/04 13:57:20 adam
109 * New file extract/retrieve method tellf (added).
110 * Added O_BINARY for open calls.
112 * Revision 1.72 1997/07/15 16:32:29 adam
113 * Bug fix: Match handler didn't terminate the resulting string!
115 * Revision 1.71 1997/07/15 16:28:41 adam
116 * Bug fix: storeData didn't work with files with multiple records.
117 * Bug fix: fixed memory management with records; not really well
120 * Revision 1.70 1997/07/01 13:00:42 adam
121 * Bug fix in routine searchRecordKey: uninitialized variables.
123 * Revision 1.69 1997/04/29 09:26:03 adam
124 * Bug fix: generic recordId handling didn't work for compressed internal
127 * Revision 1.68 1997/02/12 20:39:45 adam
128 * Implemented options -f <n> that limits the log to the first <n>
130 * Changed some log messages also.
132 * Revision 1.67 1996/11/15 15:02:14 adam
133 * Minor changes regarding logging.
135 * Revision 1.66 1996/11/14 09:52:21 adam
136 * Strings in record keys bound by IT_MAX_WORD.
138 * Revision 1.65 1996/11/14 08:57:56 adam
139 * Reduction of storeKeys area.
141 * Revision 1.64 1996/11/08 11:10:16 adam
142 * Buffers used during file match got bigger.
143 * Compressed ISAM support everywhere.
144 * Bug fixes regarding masking characters in queries.
145 * Redesigned Regexp-2 queries.
147 * Revision 1.63 1996/10/29 14:09:39 adam
148 * Use of cisam system - enabled if setting isamc is 1.
150 * Revision 1.62 1996/10/11 10:57:01 adam
151 * New module recctrl. Used to manage records (extract/retrieval).
152 * Several files have been moved to the recctrl sub directory.
154 * Revision 1.61 1996/06/06 12:08:37 quinn
155 * Added showRecord function
157 * Revision 1.60 1996/06/04 10:18:12 adam
158 * Search/scan uses character mapping module.
160 * Revision 1.59 1996/05/14 15:47:07 adam
161 * Cleanup of various buffer size entities.
163 * Revision 1.58 1996/05/14 06:16:38 adam
164 * Compact use/set bytes used in search service.
166 * Revision 1.57 1996/05/13 14:23:04 adam
167 * Work on compaction of set/use bytes in dictionary.
169 * Revision 1.56 1996/05/09 09:54:42 adam
170 * Server supports maps from one logical attributes to a list of physical
172 * The extraction process doesn't make space consuming 'any' keys.
174 * Revision 1.55 1996/05/09 07:28:55 quinn
175 * Work towards phrases and multiple registers
177 * Revision 1.54 1996/05/01 13:46:35 adam
178 * First work on multiple records in one file.
179 * New option, -offset, to the "unread" command in the filter module.
181 * Revision 1.53 1996/04/26 12:09:43 adam
182 * Added a few comments.
184 * Revision 1.52 1996/04/25 13:27:57 adam
185 * Function recordExtract modified so that files with no keys (possibly empty)
188 * Revision 1.51 1996/03/19 11:08:42 adam
189 * Bug fix: Log preamble wasn't always turned off after recordExtract.
191 * Revision 1.50 1996/02/12 18:45:36 adam
192 * New fileVerboseFlag in record group control.
194 * Revision 1.49 1996/02/05 12:29:57 adam
195 * Logging reduced a bit.
196 * The remaining running time is estimated during register merge.
198 * Revision 1.48 1996/02/01 20:53:26 adam
199 * The temporary per-record keys are compacted a little, and duplication
200 * of the per-records keys are avoided when they are saved in the record
201 * information buffer.
203 * Revision 1.47 1996/01/17 14:57:48 adam
204 * Prototype changed for reader functions in extract/retrieve. File
205 * is identified by 'void *' instead of 'int.
207 * Revision 1.46 1995/12/15 14:57:16 adam
210 * Revision 1.45 1995/12/15 12:37:41 adam
211 * In addRecordKeyAny: Writes key only when attrSet != -1.
213 * Revision 1.44 1995/12/12 16:00:54 adam
214 * System call sync(2) used after update/commit.
215 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
218 * Revision 1.43 1995/12/11 09:12:46 adam
219 * The rec_get function returns NULL if record doesn't exist - will
220 * happen in the server if the result set records have been deleted since
221 * the creation of the set (i.e. the search).
222 * The server saves a result temporarily if it is 'volatile', i.e. the
223 * set is register dependent.
225 * Revision 1.42 1995/12/07 17:38:46 adam
226 * Work locking mechanisms for concurrent updates/commit.
228 * Revision 1.41 1995/12/06 16:06:42 adam
229 * Better diagnostics. Work on 'real' dictionary deletion.
231 * Revision 1.40 1995/12/05 16:57:40 adam
232 * More work on regular patterns.
234 * Revision 1.39 1995/12/05 13:20:18 adam
235 * Bug fix: file_read sometimes returned early EOF.
237 * Revision 1.38 1995/12/04 17:59:21 adam
238 * More work on regular expression conversion.
240 * Revision 1.37 1995/12/04 14:22:27 adam
241 * Extra arg to recType_byName.
242 * Started work on new regular expression parsed input to
243 * structured records.
245 * Revision 1.36 1995/11/30 08:34:29 adam
246 * Started work on commit facility.
247 * Changed a few malloc/free to xmalloc/xfree.
249 * Revision 1.35 1995/11/28 14:26:21 adam
250 * Bug fix: recordId with constant wasn't right.
251 * Bug fix: recordId dictionary entry wasn't deleted when needed.
253 * Revision 1.34 1995/11/28 09:09:38 adam
254 * Zebra config renamed.
255 * Use setting 'recordId' to identify record now.
256 * Bug fix in recindex.c: rec_release_blocks was invokeded even
257 * though the blocks were already released.
258 * File traversal properly deletes records when needed.
260 * Revision 1.33 1995/11/27 09:56:20 adam
261 * Record info elements better enumerated. Internal store of records.
263 * Revision 1.32 1995/11/25 10:24:05 adam
264 * More record fields - they are enumerated now.
265 * New options: flagStoreData flagStoreKey.
267 * Revision 1.31 1995/11/24 11:31:35 adam
268 * Commands add & del read filenames from stdin if source directory is
270 * Match criteria supports 'constant' strings.
272 * Revision 1.30 1995/11/22 17:19:16 adam
273 * Record management uses the bfile system.
275 * Revision 1.29 1995/11/21 15:01:14 adam
276 * New general match criteria implemented.
277 * New feature: document groups.
279 * Revision 1.28 1995/11/21 09:20:30 adam
280 * Yet more work on record match.
282 * Revision 1.27 1995/11/20 16:59:45 adam
283 * New update method: the 'old' keys are saved for each records.
285 * Revision 1.26 1995/11/20 11:56:24 adam
286 * Work on new traversal.
288 * Revision 1.25 1995/11/16 15:34:54 adam
289 * Uses new record management system in both indexer and server.
291 * Revision 1.24 1995/11/15 19:13:08 adam
292 * Work on record management.
294 * Revision 1.23 1995/10/27 14:00:10 adam
295 * Implemented detection of database availability.
297 * Revision 1.22 1995/10/17 18:02:07 adam
298 * New feature: databases. Implemented as prefix to words in dictionary.
300 * Revision 1.21 1995/10/10 12:24:38 adam
301 * Temporary sort files are compressed.
303 * Revision 1.20 1995/10/06 13:52:05 adam
304 * Bug fixes. Handler may abort further scanning.
306 * Revision 1.19 1995/10/04 12:55:16 adam
307 * Bug fix in ranked search. Use=Any keys inserted.
309 * Revision 1.18 1995/10/04 09:37:08 quinn
312 * Revision 1.17 1995/10/03 14:28:57 adam
313 * Buffered read in extract works.
315 * Revision 1.16 1995/10/03 14:28:45 adam
316 * Work on more effecient read handler in extract.
318 * Revision 1.15 1995/10/02 15:42:53 adam
319 * Extract uses file descriptors instead of FILE pointers.
321 * Revision 1.14 1995/10/02 15:29:13 adam
322 * More logging in file_extract.
324 * Revision 1.13 1995/09/29 14:01:39 adam
327 * Revision 1.12 1995/09/28 14:22:56 adam
328 * Sort uses smaller temporary files.
330 * Revision 1.11 1995/09/28 12:10:31 adam
331 * Bug fixes. Field prefix used in queries.
333 * Revision 1.10 1995/09/28 09:19:41 adam
334 * xfree/xmalloc used everywhere.
335 * Extract/retrieve method seems to work for text records.
337 * Revision 1.9 1995/09/27 12:22:28 adam
338 * More work on extract in record control.
339 * Field name is not in isam keys but in prefix in dictionary words.
341 * Revision 1.8 1995/09/14 07:48:22 adam
342 * Record control management.
344 * Revision 1.7 1995/09/11 13:09:32 adam
345 * More work on relevance feedback.
347 * Revision 1.6 1995/09/08 14:52:27 adam
348 * Minor changes. Dictionary is lower case now.
350 * Revision 1.5 1995/09/06 16:11:16 adam
351 * Option: only one word key per file.
353 * Revision 1.4 1995/09/05 15:28:39 adam
354 * More work on search engine.
356 * Revision 1.3 1995/09/04 12:33:41 adam
357 * Various cleanup. YAZ util used instead.
359 * Revision 1.2 1995/09/04 09:10:34 adam
360 * More work on index add/del/update.
361 * Merge sort implemented.
362 * Initial work on z39 server.
364 * Revision 1.1 1995/09/01 14:06:35 adam
365 * Split of work into more files.
384 #if _FILE_OFFSET_BITS == 64
385 #define PRINTF_OFF_T "%Ld"
387 #define PRINTF_OFF_T "%ld"
395 #include "zebrasdr.h"
398 static Dict matchDict;
400 static Records records = NULL;
401 static SortIdx sortIdx = NULL;
403 static char **key_buf;
404 static size_t ptr_top;
406 static size_t key_buf_used;
407 static int key_file_no;
409 static int records_inserted = 0;
410 static int records_updated = 0;
411 static int records_deleted = 0;
412 static int records_processed = 0;
414 static ZebraExplainInfo zti = NULL;
416 static void logRecord (int showFlag)
420 if (showFlag || !(records_processed % 1000))
422 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
423 records_processed, records_inserted, records_updated,
428 static int explain_extract (void *handle, Record drec, data1_node *n);
430 int key_open (struct recordGroup *rGroup, int mem)
432 BFiles bfs = rGroup->bfs;
433 int rw = rGroup->flagRw;
434 data1_handle dh = rGroup->dh;
435 char *recordCompression;
436 int record_compression = REC_COMPRESS_NONE;
438 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
441 key_buf = (char **) xmalloc (mem);
442 ptr_top = mem/sizeof(char*);
448 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
450 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
454 recordCompression = res_get_def (common_resource,
455 "recordCompression", "none");
456 if (!strcmp (recordCompression, "none"))
457 record_compression = REC_COMPRESS_NONE;
458 if (!strcmp (recordCompression, "bzip2"))
459 record_compression = REC_COMPRESS_BZIP2;
460 records = rec_open (bfs, rw, record_compression);
463 dict_close (matchDict);
466 zti = zebraExplain_open (records, dh, common_resource,
467 rw, rGroup, explain_extract);
470 rec_close (&records);
471 dict_close (matchDict);
474 sortIdx = sortIdx_open (bfs, 1);
485 void encode_key_init (struct encode_info *i)
492 char *encode_key_int (int d, char *bp)
501 else if (d <= 4194303)
503 *bp++ = 128 + (d>>16);
504 *bp++ = (d>>8) & 255;
509 *bp++ = 192 + (d>>24);
510 *bp++ = (d>>16) & 255;
511 *bp++ = (d>>8) & 255;
517 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
522 while ((*bp++ = *k++))
524 memcpy (&key, k+1, sizeof(struct it_key));
525 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
526 if (i->sysno != key.sysno)
528 i->sysno = key.sysno;
531 else if (!i->seqno && !key.seqno && i->cmd == *k)
533 bp = encode_key_int (key.seqno - i->seqno, bp);
534 i->seqno = key.seqno;
536 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
538 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
546 static int key_y_len;
548 static int key_y_compare (const void *p1, const void *p2)
552 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
553 *(char**) p2 + key_y_len + 1)))
555 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
558 static int key_x_compare (const void *p1, const void *p2)
560 return strcmp (*(char**) p1, *(char**) p2);
564 void key_flush (void)
569 struct encode_info encode_info;
578 logf (LOG_LOG, "sorting section %d", key_file_no);
580 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
581 getFnameTmp (common_resource, out_fname, key_file_no);
583 if (!(outf = fopen (out_fname, "wb")))
585 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
588 logf (LOG_LOG, "writing section %d", key_file_no);
589 prevcp = cp = key_buf[ptr_top-ptr_i];
591 encode_key_init (&encode_info);
592 encode_key_write (cp, &encode_info, outf);
595 cp = key_buf[ptr_top-ptr_i];
596 if (strcmp (cp, prevcp))
598 encode_key_init (&encode_info);
599 encode_key_write (cp, &encode_info, outf);
603 encode_key_write (cp + strlen(cp), &encode_info, outf);
606 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
607 getFnameTmp (out_fname, key_file_no);
609 if (!(outf = fopen (out_fname, "wb")))
611 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
614 logf (LOG_LOG, "writing section %d", key_file_no);
616 prevcp = key_buf[ptr_top-i];
618 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
620 key_y_len = strlen(prevcp)+1;
622 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
623 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
625 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
626 sizeof(char*), key_y_compare);
627 cp = key_buf[ptr_top-ptr_i];
629 encode_key_init (&encode_info);
630 encode_key_write (cp, &encode_info, outf);
633 cp = key_buf[ptr_top-ptr_i];
634 encode_key_write (cp+key_y_len, &encode_info, outf);
638 prevcp = key_buf[ptr_top-ptr_i];
643 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
646 logf (LOG_LOG, "finished section %d", key_file_no);
651 int key_close (struct recordGroup *rGroup)
653 int rw = rGroup->flagRw;
655 zebraExplain_runNumberIncrement (zti, 1);
656 zebraExplain_close (zti, rw);
659 rec_close (&records);
660 dict_close (matchDict);
661 sortIdx_close (sortIdx);
667 static void wordInit (struct recExtractCtrl *p, RecWord *w)
669 w->zebra_maps = p->zebra_maps;
670 w->seqnos = p->seqno;
671 w->attrSet = VAL_BIB1;
677 static struct sortKey {
682 struct sortKey *next;
685 static struct recKeys {
694 static void addIndexString (RecWord *p, const char *string, int length)
697 unsigned char attrSet;
698 unsigned short attrUse;
701 int *pseqno = &p->seqnos[p->reg_type];
703 if (reckeys.buf_used+1024 > reckeys.buf_max)
707 b = (char *) xmalloc (reckeys.buf_max += 128000);
708 if (reckeys.buf_used > 0)
709 memcpy (b, reckeys.buf, reckeys.buf_used);
713 dst = reckeys.buf + reckeys.buf_used;
715 attrSet = p->attrSet;
716 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
719 reckeys.prevAttrSet = attrSet;
720 attrUse = p->attrUse;
721 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
724 reckeys.prevAttrUse = attrUse;
726 diff = 1 + *pseqno - reckeys.prevSeqNo;
727 if (diff >= 1 && diff <= 15)
732 reckeys.prevSeqNo = *pseqno;
738 memcpy (dst, &attrSet, sizeof(attrSet));
739 dst += sizeof(attrSet);
743 memcpy (dst, &attrUse, sizeof(attrUse));
744 dst += sizeof(attrUse);
746 *dst++ = p->reg_type;
747 memcpy (dst, string, length);
753 memcpy (dst, pseqno, sizeof(*pseqno));
754 dst += sizeof(*pseqno);
756 reckeys.buf_used = dst - reckeys.buf;
761 static void addSortString (RecWord *p, const char *string, int length)
765 for (sk = sortKeys; sk; sk = sk->next)
766 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
769 sk = (struct sortKey *) xmalloc (sizeof(*sk));
773 sk->string = (char *) xmalloc (length);
775 memcpy (sk->string, string, length);
777 sk->attrSet = p->attrSet;
778 sk->attrUse = p->attrUse;
781 static void addString (RecWord *p, const char *string, int length)
784 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
785 addSortString (p, string, length);
787 addIndexString (p, string, length);
790 static void addIncompleteField (RecWord *p)
792 const char *b = p->string;
793 int remain = p->length;
794 const char **map = 0;
797 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
801 char buf[IT_MAX_WORD+1];
805 while (map && *map && **map == *CHR_SPACE)
807 remain = p->length - (b - p->string);
809 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
816 while (map && *map && **map != *CHR_SPACE)
818 const char *cp = *map;
820 while (i < IT_MAX_WORD && *cp)
822 remain = p->length - (b - p->string);
824 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
830 addString (p, buf, i);
832 (p->seqnos[p->reg_type])++; /* to separate this from next one */
835 static void addCompleteField (RecWord *p)
837 const char *b = p->string;
838 char buf[IT_MAX_WORD+1];
839 const char **map = 0;
840 int i = 0, remain = p->length;
843 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
845 while (remain > 0 && i < IT_MAX_WORD)
847 while (map && *map && **map == *CHR_SPACE)
849 remain = p->length - (b - p->string);
851 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
858 if (i && i < IT_MAX_WORD)
859 buf[i++] = *CHR_SPACE;
860 while (map && *map && **map != *CHR_SPACE)
862 const char *cp = *map;
864 if (i >= IT_MAX_WORD)
866 while (i < IT_MAX_WORD && *cp)
868 remain = p->length - (b - p->string);
870 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
878 addString (p, buf, i);
881 static void addRecordKey (RecWord *p)
884 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
885 p->string, p->length)))
887 p->string = wrbuf_buf(wrbuf);
888 p->length = wrbuf_len(wrbuf);
890 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
891 addCompleteField (p);
893 addIncompleteField(p);
896 static void flushSortKeys (SYSNO sysno, int cmd)
898 struct sortKey *sk = sortKeys;
900 sortIdx_sysno (sortIdx, sysno);
903 struct sortKey *sk_next = sk->next;
904 sortIdx_type (sortIdx, sk->attrUse);
905 sortIdx_add (sortIdx, sk->string, sk->length);
913 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
915 unsigned char attrSet = (unsigned char) -1;
916 unsigned short attrUse = (unsigned short) -1;
920 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
921 while (off < reckeys->buf_used)
923 const char *src = reckeys->buf + off;
931 memcpy (&attrSet, src, sizeof(attrSet));
932 src += sizeof(attrSet);
936 memcpy (&attrUse, src, sizeof(attrUse));
937 src += sizeof(attrUse);
939 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
942 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
944 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
946 ch = zebraExplain_addSU (zti, attrSet, attrUse);
948 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
951 ((char*)key_buf) [key_buf_used++] = *src++;
953 ((char*)key_buf) [key_buf_used++] = '\0';
954 ((char*) key_buf)[key_buf_used++] = cmd;
957 seqno += ((lead>>2) & 15)-1;
960 memcpy (&seqno, src, sizeof(seqno));
961 src += sizeof(seqno);
965 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
966 key_buf_used += sizeof(key);
967 off = src - reckeys->buf;
969 assert (off == reckeys->buf_used);
972 static const char **searchRecordKey (struct recKeys *reckeys,
973 int attrSetS, int attrUseS)
975 static const char *ws[32];
983 for (i = 0; i<32; i++)
986 while (off < reckeys->buf_used)
989 const char *src = reckeys->buf + off;
997 memcpy (&attrSet, src, sizeof(attrSet));
998 src += sizeof(attrSet);
1002 memcpy (&attrUse, src, sizeof(attrUse));
1003 src += sizeof(attrUse);
1009 seqno += ((lead>>2) & 15)-1;
1012 memcpy (&seqno, src, sizeof(seqno));
1013 src += sizeof(seqno);
1015 if (attrUseS == attrUse && attrSetS == attrSet)
1022 woff = seqno - startSeq;
1023 if (woff >= 0 && woff < 31)
1027 off = src - reckeys->buf;
1029 assert (off == reckeys->buf_used);
1033 struct file_read_info {
1034 off_t file_max; /* maximum offset so far */
1035 off_t file_offset; /* current offset */
1036 off_t file_moffset; /* offset of rec/rec boundary */
1043 static struct file_read_info *file_read_start (int fd)
1045 struct file_read_info *fi = (struct file_read_info *)
1046 xmalloc (sizeof(*fi));
1050 fi->file_moffset = 0;
1056 static void file_read_stop (struct file_read_info *fi)
1061 static off_t file_seek (void *handle, off_t offset)
1063 struct file_read_info *p = (struct file_read_info *) handle;
1064 p->file_offset = offset;
1067 return lseek (p->fd, offset, SEEK_SET);
1070 static off_t file_tell (void *handle)
1072 struct file_read_info *p = (struct file_read_info *) handle;
1073 return p->file_offset;
1076 static int file_read (void *handle, char *buf, size_t count)
1078 struct file_read_info *p = (struct file_read_info *) handle;
1084 if (r > p->sdrmax - p->file_offset)
1085 r = p->sdrmax - p->file_offset;
1087 memcpy (buf, p->sdrbuf + p->file_offset, r);
1090 r = read (fd, buf, count);
1093 p->file_offset += r;
1094 if (p->file_offset > p->file_max)
1095 p->file_max = p->file_offset;
1100 static void file_begin (void *handle)
1102 struct file_read_info *p = (struct file_read_info *) handle;
1104 p->file_offset = p->file_moffset;
1105 if (!p->sdrbuf && p->file_moffset)
1106 lseek (p->fd, p->file_moffset, SEEK_SET);
1110 static void file_end (void *handle, off_t offset)
1112 struct file_read_info *p = (struct file_read_info *) handle;
1114 assert (p->file_more == 0);
1116 p->file_moffset = offset;
1119 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1120 const char *fname, const char *spec)
1122 static char dstBuf[2048];
1124 const char *s = spec;
1125 static const char **w;
1129 while (*s == ' ' || *s == '\t')
1135 char attset_str[64], attname_str[64];
1136 data1_attset *attset;
1139 int attSet = 1, attUse = 1;
1143 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1145 attset_str[i++] = *s;
1146 attset_str[i] = '\0';
1151 for (i = 0; *s && *s != ')'; s++)
1153 attname_str[i++] = *s;
1154 attname_str[i] = '\0';
1157 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1160 attSet = attset->reference;
1161 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1163 attUse = att->value;
1165 attUse = atoi (attname_str);
1167 w = searchRecordKey (reckeys, attSet, attUse);
1172 for (i = 0; i<32; i++)
1177 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1178 spec, rGroup->groupName ? rGroup->groupName : "none");
1183 for (i = 0; i<32; i++)
1184 if (matchFlag[i] && w[i])
1192 dst += strlen(w[i]);
1196 logf (LOG_WARN, "Record didn't contain match"
1197 " fields in (%s,%s)", attset_str, attname_str);
1205 const char *spec_src = NULL;
1206 const char *s1 = ++s;
1207 while (*s1 && *s1 != ' ' && *s1 != '\t')
1213 memcpy (special, s, spec_len);
1214 special[spec_len] = '\0';
1217 if (!strcmp (special, "group"))
1218 spec_src = rGroup->groupName;
1219 else if (!strcmp (special, "database"))
1220 spec_src = rGroup->databaseName;
1221 else if (!strcmp (special, "filename"))
1223 else if (!strcmp (special, "type"))
1224 spec_src = rGroup->recordType;
1229 strcpy (dst, spec_src);
1230 dst += strlen (spec_src);
1233 else if (*s == '\"' || *s == '\'')
1235 int stopMarker = *s++;
1239 while (*s && *s != stopMarker)
1242 tmpString[i++] = *s++;
1246 tmpString[i] = '\0';
1247 strcpy (dst, tmpString);
1248 dst += strlen (tmpString);
1252 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1253 spec, rGroup->groupName ? rGroup->groupName : "none");
1260 logf (LOG_WARN, "No match criteria for record %s in group %s",
1261 fname, rGroup->groupName ? rGroup->groupName : "none");
1268 struct recordLogInfo {
1271 struct recordGroup *rGroup;
1274 static void recordLogPreamble (int level, const char *msg, void *info)
1276 struct recordLogInfo *p = (struct recordLogInfo *) info;
1277 FILE *outf = log_file ();
1279 if (level & LOG_LOG)
1281 fprintf (outf, "File %s, offset %d, type %s\n",
1282 p->rGroup->recordType, p->recordOffset, p->fname);
1283 log_event_start (NULL, NULL);
1286 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1288 zebraExplain_addSchema (zti, oid);
1291 static int recordExtract (SYSNO *sysno, const char *fname,
1292 struct recordGroup *rGroup, int deleteFlag,
1293 struct file_read_info *fi,
1294 RecType recType, char *subType, void *clientData)
1296 RecordAttr *recordAttr;
1301 struct recordLogInfo logInfo;
1302 off_t recordOffset = 0;
1306 struct recExtractCtrl extractCtrl;
1308 /* we are going to read from a file, so prepare the extraction */
1311 reckeys.buf_used = 0;
1312 reckeys.prevAttrUse = -1;
1313 reckeys.prevAttrSet = -1;
1314 reckeys.prevSeqNo = 0;
1316 recordOffset = fi->file_moffset;
1317 extractCtrl.offset = fi->file_moffset;
1318 extractCtrl.readf = file_read;
1319 extractCtrl.seekf = file_seek;
1320 extractCtrl.tellf = file_tell;
1321 extractCtrl.endf = file_end;
1322 extractCtrl.fh = fi;
1323 extractCtrl.subType = subType;
1324 extractCtrl.init = wordInit;
1325 extractCtrl.tokenAdd = addRecordKey;
1326 extractCtrl.schemaAdd = addSchema;
1327 extractCtrl.dh = rGroup->dh;
1328 for (i = 0; i<256; i++)
1330 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1331 extractCtrl.seqno[i] = 1;
1333 extractCtrl.seqno[i] = 0;
1335 extractCtrl.zebra_maps = rGroup->zebra_maps;
1336 extractCtrl.flagShowRecords = !rGroup->flagRw;
1338 if (!rGroup->flagRw)
1339 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
1341 logInfo.fname = fname;
1342 logInfo.recordOffset = recordOffset;
1343 logInfo.rGroup = rGroup;
1344 log_event_start (recordLogPreamble, &logInfo);
1346 r = (*recType->extract)(clientData, &extractCtrl);
1348 log_event_start (NULL, NULL);
1350 if (r == RECCTRL_EXTRACT_EOF)
1352 else if (r == RECCTRL_EXTRACT_ERROR)
1354 /* error occured during extraction ... */
1355 if (rGroup->flagRw &&
1356 records_processed < rGroup->fileVerboseLimit)
1358 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
1359 fname, recordOffset);
1363 if (reckeys.buf_used == 0)
1365 /* the extraction process returned no information - the record
1366 is probably empty - unless flagShowRecords is in use */
1367 if (!rGroup->flagRw)
1370 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
1371 fname, recordOffset);
1376 /* perform match if sysno not known and if match criteria is specified */
1383 if (rGroup->recordId && *rGroup->recordId)
1387 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1391 rinfo = dict_lookup (matchDict, matchStr);
1393 memcpy (sysno, rinfo+1, sizeof(*sysno));
1397 logf (LOG_WARN, "Bad match criteria");
1408 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
1409 fname, recordOffset);
1410 logf (LOG_WARN, "cannot delete record above (seems new)");
1413 if (records_processed < rGroup->fileVerboseLimit)
1414 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
1415 fname, recordOffset);
1416 rec = rec_new (records);
1418 *sysno = rec->sysno;
1420 recordAttr = rec_init_attr (zti, rec);
1424 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1426 flushRecordKeys (*sysno, 1, &reckeys);
1427 flushSortKeys (*sysno, 1);
1433 /* record already exists */
1434 struct recKeys delkeys;
1436 rec = rec_get (records, *sysno);
1439 recordAttr = rec_init_attr (zti, rec);
1441 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1443 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
1444 fname, recordOffset);
1449 delkeys.buf_used = rec->size[recInfo_delKeys];
1450 delkeys.buf = rec->info[recInfo_delKeys];
1451 flushSortKeys (*sysno, 0);
1452 flushRecordKeys (*sysno, 0, &delkeys);
1455 /* record going to be deleted */
1456 if (!delkeys.buf_used)
1458 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1459 rGroup->recordType, fname, recordOffset);
1460 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1464 if (records_processed < rGroup->fileVerboseLimit)
1465 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
1466 rGroup->recordType, fname, recordOffset);
1469 dict_delete (matchDict, matchStr);
1470 rec_del (records, &rec);
1478 /* record going to be updated */
1479 if (!delkeys.buf_used)
1481 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1482 rGroup->recordType, fname, recordOffset);
1483 logf (LOG_WARN, "cannot update file above, storeKeys false");
1487 if (records_processed < rGroup->fileVerboseLimit)
1488 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
1489 rGroup->recordType, fname, recordOffset);
1490 flushRecordKeys (*sysno, 1, &reckeys);
1495 /* update file type */
1496 xfree (rec->info[recInfo_fileType]);
1497 rec->info[recInfo_fileType] =
1498 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1500 /* update filename */
1501 xfree (rec->info[recInfo_filename]);
1502 rec->info[recInfo_filename] =
1503 rec_strdup (fname, &rec->size[recInfo_filename]);
1505 /* update delete keys */
1506 xfree (rec->info[recInfo_delKeys]);
1507 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1510 rec->size[recInfo_delKeys] = reckeys.buf_used;
1511 rec->info[recInfo_delKeys] = reckeys.buf;
1513 reckeys.buf_max = 0;
1515 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1516 rec->size[recInfo_delKeys] = reckeys.buf_used;
1517 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1518 rec->size[recInfo_delKeys]);
1523 rec->info[recInfo_delKeys] = NULL;
1524 rec->size[recInfo_delKeys] = 0;
1527 /* save file size of original record */
1528 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1529 recordAttr->recordSize = fi->file_moffset - recordOffset;
1530 if (!recordAttr->recordSize)
1531 recordAttr->recordSize = fi->file_max - recordOffset;
1532 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1534 /* set run-number for this record */
1535 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1537 /* update store data */
1538 xfree (rec->info[recInfo_storeData]);
1539 if (rGroup->flagStoreData == 1)
1541 rec->size[recInfo_storeData] = recordAttr->recordSize;
1542 rec->info[recInfo_storeData] = (char *)
1543 xmalloc (recordAttr->recordSize);
1544 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1546 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
1547 recordOffset, fname);
1550 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1551 < recordAttr->recordSize)
1553 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1554 recordAttr->recordSize, fname);
1560 rec->info[recInfo_storeData] = NULL;
1561 rec->size[recInfo_storeData] = 0;
1563 /* update database name */
1564 xfree (rec->info[recInfo_databaseName]);
1565 rec->info[recInfo_databaseName] =
1566 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1569 recordAttr->recordOffset = recordOffset;
1571 /* commit this record */
1572 rec_put (records, &rec);
1577 int fileExtract (SYSNO *sysno, const char *fname,
1578 const struct recordGroup *rGroupP, int deleteFlag)
1586 struct recordGroup rGroupM;
1587 struct recordGroup *rGroup = &rGroupM;
1588 struct file_read_info *fi;
1591 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1593 if (!rGroup->groupName || !*rGroup->groupName)
1596 sprintf (gprefix, "%s.", rGroup->groupName);
1598 logf (LOG_DEBUG, "fileExtract %s", fname);
1600 /* determine file extension */
1602 for (i = strlen(fname); --i >= 0; )
1603 if (fname[i] == '/')
1605 else if (fname[i] == '.')
1607 strcpy (ext, fname+i+1);
1610 /* determine file type - depending on extension */
1611 if (!rGroup->recordType)
1613 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1614 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1616 sprintf (ext_res, "%srecordType", gprefix);
1617 rGroup->recordType = res_get (common_resource, ext_res);
1620 if (!rGroup->recordType)
1622 if (records_processed < rGroup->fileVerboseLimit)
1623 logf (LOG_LOG, "? %s", fname);
1626 if (!*rGroup->recordType)
1629 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1632 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1636 /* determine match criteria */
1637 if (!rGroup->recordId)
1639 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1640 rGroup->recordId = res_get (common_resource, ext_res);
1643 /* determine database name */
1644 if (!rGroup->databaseName)
1646 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1647 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1649 sprintf (ext_res, "%sdatabase", gprefix);
1650 rGroup->databaseName = res_get (common_resource, ext_res);
1653 if (!rGroup->databaseName)
1654 rGroup->databaseName = "Default";
1656 /* determine if explain database */
1658 sprintf (ext_res, "%sexplainDatabase", gprefix);
1659 rGroup->explainDatabase =
1660 atoi (res_get_def (common_resource, ext_res, "0"));
1662 /* announce database */
1663 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1665 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1666 rGroup->explainDatabase))
1670 if (rGroup->flagStoreData == -1)
1673 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1674 if (!(sval = res_get (common_resource, ext_res)))
1676 sprintf (ext_res, "%sstoreData", gprefix);
1677 sval = res_get (common_resource, ext_res);
1680 rGroup->flagStoreData = atoi (sval);
1682 if (rGroup->flagStoreData == -1)
1683 rGroup->flagStoreData = 0;
1685 if (rGroup->flagStoreKeys == -1)
1689 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1690 if (!(sval = res_get (common_resource, ext_res)))
1692 sprintf (ext_res, "%sstoreKeys", gprefix);
1693 sval = res_get (common_resource, ext_res);
1696 rGroup->flagStoreKeys = atoi (sval);
1698 if (rGroup->flagStoreKeys == -1)
1699 rGroup->flagStoreKeys = 0;
1701 if (sysno && deleteFlag)
1705 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1707 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1711 fi = file_read_start (fd);
1715 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1716 recType, subType, clientData);
1717 } while (r && !sysno && fi->file_more);
1718 file_read_stop (fi);
1724 static int explain_extract (void *handle, Record rec, data1_node *n)
1726 struct recordGroup *rGroup = (struct recordGroup*) handle;
1727 struct recExtractCtrl extractCtrl;
1730 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1732 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1736 reckeys.buf_used = 0;
1737 reckeys.prevAttrUse = -1;
1738 reckeys.prevAttrSet = -1;
1739 reckeys.prevSeqNo = 0;
1741 extractCtrl.init = wordInit;
1742 extractCtrl.tokenAdd = addRecordKey;
1743 extractCtrl.schemaAdd = addSchema;
1744 extractCtrl.dh = rGroup->dh;
1745 for (i = 0; i<256; i++)
1746 extractCtrl.seqno[i] = 0;
1747 extractCtrl.zebra_maps = rGroup->zebra_maps;
1748 extractCtrl.flagShowRecords = !rGroup->flagRw;
1750 grs_extract_tree(&extractCtrl, n);
1752 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1754 if (rec->size[recInfo_delKeys])
1756 struct recKeys delkeys;
1758 delkeys.buf_used = rec->size[recInfo_delKeys];
1759 delkeys.buf = rec->info[recInfo_delKeys];
1760 flushSortKeys (rec->sysno, 0);
1761 flushRecordKeys (rec->sysno, 0, &delkeys);
1763 flushRecordKeys (rec->sysno, 1, &reckeys);
1764 flushSortKeys (rec->sysno, 1);
1766 xfree (rec->info[recInfo_delKeys]);
1767 rec->size[recInfo_delKeys] = reckeys.buf_used;
1768 rec->info[recInfo_delKeys] = reckeys.buf;
1770 reckeys.buf_max = 0;