2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.83 1998-06-08 14:43:10 adam
8 * Added suport for EXPLAIN Proxy servers - added settings databasePath
9 * and explainDatabase to facilitate this. Increased maximum number
10 * of databases and attributes in one register.
12 * Revision 1.82 1998/05/20 10:12:15 adam
13 * Implemented automatic EXPLAIN database maintenance.
14 * Modified Zebra to work with ASN.1 compiled version of YAZ.
16 * Revision 1.81 1998/03/11 11:19:04 adam
17 * Changed the way sequence numbers are generated.
19 * Revision 1.80 1998/03/05 08:45:11 adam
20 * New result set model and modular ranking system. Moved towards
21 * descent server API. System information stored as "SGML" records.
23 * Revision 1.79 1998/02/17 10:32:52 adam
24 * Fixed bug: binary files weren't opened with flag b on NT.
26 * Revision 1.78 1998/02/10 12:03:05 adam
29 * Revision 1.77 1998/01/12 15:04:08 adam
30 * The test option (-s) only uses read-lock (and not write lock).
32 * Revision 1.76 1997/10/27 14:33:04 adam
33 * Moved towards generic character mapping depending on "structure"
34 * field in abstract syntax file. Fixed a few memory leaks. Fixed
35 * bug with negative integers when doing searches with relational
38 * Revision 1.75 1997/09/17 12:19:12 adam
39 * Zebra version corresponds to YAZ version 1.4.
40 * Changed Zebra server so that it doesn't depend on global common_resource.
42 * Revision 1.74 1997/09/09 13:38:06 adam
43 * Partial port to WIN95/NT.
45 * Revision 1.73 1997/09/04 13:57:20 adam
46 * New file extract/retrieve method tellf (added).
47 * Added O_BINARY for open calls.
49 * Revision 1.72 1997/07/15 16:32:29 adam
50 * Bug fix: Match handler didn't terminate the resulting string!
52 * Revision 1.71 1997/07/15 16:28:41 adam
53 * Bug fix: storeData didn't work with files with multiple records.
54 * Bug fix: fixed memory management with records; not really well
57 * Revision 1.70 1997/07/01 13:00:42 adam
58 * Bug fix in routine searchRecordKey: uninitialized variables.
60 * Revision 1.69 1997/04/29 09:26:03 adam
61 * Bug fix: generic recordId handling didn't work for compressed internal
64 * Revision 1.68 1997/02/12 20:39:45 adam
65 * Implemented options -f <n> that limits the log to the first <n>
67 * Changed some log messages also.
69 * Revision 1.67 1996/11/15 15:02:14 adam
70 * Minor changes regarding logging.
72 * Revision 1.66 1996/11/14 09:52:21 adam
73 * Strings in record keys bound by IT_MAX_WORD.
75 * Revision 1.65 1996/11/14 08:57:56 adam
76 * Reduction of storeKeys area.
78 * Revision 1.64 1996/11/08 11:10:16 adam
79 * Buffers used during file match got bigger.
80 * Compressed ISAM support everywhere.
81 * Bug fixes regarding masking characters in queries.
82 * Redesigned Regexp-2 queries.
84 * Revision 1.63 1996/10/29 14:09:39 adam
85 * Use of cisam system - enabled if setting isamc is 1.
87 * Revision 1.62 1996/10/11 10:57:01 adam
88 * New module recctrl. Used to manage records (extract/retrieval).
89 * Several files have been moved to the recctrl sub directory.
91 * Revision 1.61 1996/06/06 12:08:37 quinn
92 * Added showRecord function
94 * Revision 1.60 1996/06/04 10:18:12 adam
95 * Search/scan uses character mapping module.
97 * Revision 1.59 1996/05/14 15:47:07 adam
98 * Cleanup of various buffer size entities.
100 * Revision 1.58 1996/05/14 06:16:38 adam
101 * Compact use/set bytes used in search service.
103 * Revision 1.57 1996/05/13 14:23:04 adam
104 * Work on compaction of set/use bytes in dictionary.
106 * Revision 1.56 1996/05/09 09:54:42 adam
107 * Server supports maps from one logical attributes to a list of physical
109 * The extraction process doesn't make space consuming 'any' keys.
111 * Revision 1.55 1996/05/09 07:28:55 quinn
112 * Work towards phrases and multiple registers
114 * Revision 1.54 1996/05/01 13:46:35 adam
115 * First work on multiple records in one file.
116 * New option, -offset, to the "unread" command in the filter module.
118 * Revision 1.53 1996/04/26 12:09:43 adam
119 * Added a few comments.
121 * Revision 1.52 1996/04/25 13:27:57 adam
122 * Function recordExtract modified so that files with no keys (possibly empty)
125 * Revision 1.51 1996/03/19 11:08:42 adam
126 * Bug fix: Log preamble wasn't always turned off after recordExtract.
128 * Revision 1.50 1996/02/12 18:45:36 adam
129 * New fileVerboseFlag in record group control.
131 * Revision 1.49 1996/02/05 12:29:57 adam
132 * Logging reduced a bit.
133 * The remaining running time is estimated during register merge.
135 * Revision 1.48 1996/02/01 20:53:26 adam
136 * The temporary per-record keys are compacted a little, and duplication
137 * of the per-records keys are avoided when they are saved in the record
138 * information buffer.
140 * Revision 1.47 1996/01/17 14:57:48 adam
141 * Prototype changed for reader functions in extract/retrieve. File
142 * is identified by 'void *' instead of 'int.
144 * Revision 1.46 1995/12/15 14:57:16 adam
147 * Revision 1.45 1995/12/15 12:37:41 adam
148 * In addRecordKeyAny: Writes key only when attrSet != -1.
150 * Revision 1.44 1995/12/12 16:00:54 adam
151 * System call sync(2) used after update/commit.
152 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
155 * Revision 1.43 1995/12/11 09:12:46 adam
156 * The rec_get function returns NULL if record doesn't exist - will
157 * happen in the server if the result set records have been deleted since
158 * the creation of the set (i.e. the search).
159 * The server saves a result temporarily if it is 'volatile', i.e. the
160 * set is register dependent.
162 * Revision 1.42 1995/12/07 17:38:46 adam
163 * Work locking mechanisms for concurrent updates/commit.
165 * Revision 1.41 1995/12/06 16:06:42 adam
166 * Better diagnostics. Work on 'real' dictionary deletion.
168 * Revision 1.40 1995/12/05 16:57:40 adam
169 * More work on regular patterns.
171 * Revision 1.39 1995/12/05 13:20:18 adam
172 * Bug fix: file_read sometimes returned early EOF.
174 * Revision 1.38 1995/12/04 17:59:21 adam
175 * More work on regular expression conversion.
177 * Revision 1.37 1995/12/04 14:22:27 adam
178 * Extra arg to recType_byName.
179 * Started work on new regular expression parsed input to
180 * structured records.
182 * Revision 1.36 1995/11/30 08:34:29 adam
183 * Started work on commit facility.
184 * Changed a few malloc/free to xmalloc/xfree.
186 * Revision 1.35 1995/11/28 14:26:21 adam
187 * Bug fix: recordId with constant wasn't right.
188 * Bug fix: recordId dictionary entry wasn't deleted when needed.
190 * Revision 1.34 1995/11/28 09:09:38 adam
191 * Zebra config renamed.
192 * Use setting 'recordId' to identify record now.
193 * Bug fix in recindex.c: rec_release_blocks was invokeded even
194 * though the blocks were already released.
195 * File traversal properly deletes records when needed.
197 * Revision 1.33 1995/11/27 09:56:20 adam
198 * Record info elements better enumerated. Internal store of records.
200 * Revision 1.32 1995/11/25 10:24:05 adam
201 * More record fields - they are enumerated now.
202 * New options: flagStoreData flagStoreKey.
204 * Revision 1.31 1995/11/24 11:31:35 adam
205 * Commands add & del read filenames from stdin if source directory is
207 * Match criteria supports 'constant' strings.
209 * Revision 1.30 1995/11/22 17:19:16 adam
210 * Record management uses the bfile system.
212 * Revision 1.29 1995/11/21 15:01:14 adam
213 * New general match criteria implemented.
214 * New feature: document groups.
216 * Revision 1.28 1995/11/21 09:20:30 adam
217 * Yet more work on record match.
219 * Revision 1.27 1995/11/20 16:59:45 adam
220 * New update method: the 'old' keys are saved for each records.
222 * Revision 1.26 1995/11/20 11:56:24 adam
223 * Work on new traversal.
225 * Revision 1.25 1995/11/16 15:34:54 adam
226 * Uses new record management system in both indexer and server.
228 * Revision 1.24 1995/11/15 19:13:08 adam
229 * Work on record management.
231 * Revision 1.23 1995/10/27 14:00:10 adam
232 * Implemented detection of database availability.
234 * Revision 1.22 1995/10/17 18:02:07 adam
235 * New feature: databases. Implemented as prefix to words in dictionary.
237 * Revision 1.21 1995/10/10 12:24:38 adam
238 * Temporary sort files are compressed.
240 * Revision 1.20 1995/10/06 13:52:05 adam
241 * Bug fixes. Handler may abort further scanning.
243 * Revision 1.19 1995/10/04 12:55:16 adam
244 * Bug fix in ranked search. Use=Any keys inserted.
246 * Revision 1.18 1995/10/04 09:37:08 quinn
249 * Revision 1.17 1995/10/03 14:28:57 adam
250 * Buffered read in extract works.
252 * Revision 1.16 1995/10/03 14:28:45 adam
253 * Work on more effecient read handler in extract.
255 * Revision 1.15 1995/10/02 15:42:53 adam
256 * Extract uses file descriptors instead of FILE pointers.
258 * Revision 1.14 1995/10/02 15:29:13 adam
259 * More logging in file_extract.
261 * Revision 1.13 1995/09/29 14:01:39 adam
264 * Revision 1.12 1995/09/28 14:22:56 adam
265 * Sort uses smaller temporary files.
267 * Revision 1.11 1995/09/28 12:10:31 adam
268 * Bug fixes. Field prefix used in queries.
270 * Revision 1.10 1995/09/28 09:19:41 adam
271 * xfree/xmalloc used everywhere.
272 * Extract/retrieve method seems to work for text records.
274 * Revision 1.9 1995/09/27 12:22:28 adam
275 * More work on extract in record control.
276 * Field name is not in isam keys but in prefix in dictionary words.
278 * Revision 1.8 1995/09/14 07:48:22 adam
279 * Record control management.
281 * Revision 1.7 1995/09/11 13:09:32 adam
282 * More work on relevance feedback.
284 * Revision 1.6 1995/09/08 14:52:27 adam
285 * Minor changes. Dictionary is lower case now.
287 * Revision 1.5 1995/09/06 16:11:16 adam
288 * Option: only one word key per file.
290 * Revision 1.4 1995/09/05 15:28:39 adam
291 * More work on search engine.
293 * Revision 1.3 1995/09/04 12:33:41 adam
294 * Various cleanup. YAZ util used instead.
296 * Revision 1.2 1995/09/04 09:10:34 adam
297 * More work on index add/del/update.
298 * Merge sort implemented.
299 * Initial work on z39 server.
301 * Revision 1.1 1995/09/01 14:06:35 adam
302 * Split of work into more files.
321 static Dict matchDict;
323 static Records records = NULL;
324 static SortIdx sortIdx = NULL;
326 static char **key_buf;
327 static size_t ptr_top;
329 static size_t key_buf_used;
330 static int key_file_no;
332 static int records_inserted = 0;
333 static int records_updated = 0;
334 static int records_deleted = 0;
335 static int records_processed = 0;
337 static ZebraExplainInfo zti = NULL;
339 static void logRecord (int showFlag)
343 if (showFlag || !(records_processed % 1000))
345 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
346 records_processed, records_inserted, records_updated,
351 static int explain_extract (void *handle, Record drec, data1_node *n);
353 int key_open (struct recordGroup *rGroup, int mem)
355 BFiles bfs = rGroup->bfs;
356 int rw = rGroup->flagRw;
357 data1_handle dh = rGroup->dh;
359 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
362 key_buf = xmalloc (mem);
363 ptr_top = mem/sizeof(char*);
369 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
371 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
375 records = rec_open (bfs, rw);
378 dict_close (matchDict);
381 zti = zebraExplain_open (records, dh, common_resource,
382 rw, rGroup, explain_extract);
385 rec_close (&records);
386 dict_close (matchDict);
389 sortIdx = sortIdx_open (bfs, 1);
399 void encode_key_init (struct encode_info *i)
405 char *encode_key_int (int d, char *bp)
414 else if (d <= 4194303)
416 *bp++ = 128 + (d>>16);
417 *bp++ = (d>>8) & 255;
422 *bp++ = 192 + (d>>24);
423 *bp++ = (d>>16) & 255;
424 *bp++ = (d>>8) & 255;
430 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
435 while ((*bp++ = *k++))
437 memcpy (&key, k+1, sizeof(struct it_key));
438 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
439 if (i->sysno != key.sysno)
441 i->sysno = key.sysno;
444 bp = encode_key_int (key.seqno - i->seqno, bp);
445 i->seqno = key.seqno;
446 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
448 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
456 static int key_y_len;
458 static int key_y_compare (const void *p1, const void *p2)
462 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
463 *(char**) p2 + key_y_len + 1)))
465 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
468 static int key_x_compare (const void *p1, const void *p2)
470 return strcmp (*(char**) p1, *(char**) p2);
474 void key_flush (void)
479 struct encode_info encode_info;
488 logf (LOG_LOG, "sorting section %d", key_file_no);
490 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
491 getFnameTmp (out_fname, key_file_no);
493 if (!(outf = fopen (out_fname, "wb")))
495 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
498 logf (LOG_LOG, "writing section %d", key_file_no);
499 prevcp = cp = key_buf[ptr_top-ptr_i];
501 encode_key_init (&encode_info);
502 encode_key_write (cp, &encode_info, outf);
505 cp = key_buf[ptr_top-ptr_i];
506 if (strcmp (cp, prevcp))
508 encode_key_init (&encode_info);
509 encode_key_write (cp, &encode_info, outf);
513 encode_key_write (cp + strlen(cp), &encode_info, outf);
516 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
517 getFnameTmp (out_fname, key_file_no);
519 if (!(outf = fopen (out_fname, "wb")))
521 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
524 logf (LOG_LOG, "writing section %d", key_file_no);
526 prevcp = key_buf[ptr_top-i];
528 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
530 key_y_len = strlen(prevcp)+1;
532 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
533 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
535 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
536 sizeof(char*), key_y_compare);
537 cp = key_buf[ptr_top-ptr_i];
539 encode_key_init (&encode_info);
540 encode_key_write (cp, &encode_info, outf);
543 cp = key_buf[ptr_top-ptr_i];
544 encode_key_write (cp+key_y_len, &encode_info, outf);
548 prevcp = key_buf[ptr_top-ptr_i];
553 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
556 logf (LOG_LOG, "finished section %d", key_file_no);
561 int key_close (struct recordGroup *rGroup)
563 int rw = rGroup->flagRw;
565 zebraExplain_runNumberIncrement (zti, 1);
566 zebraExplain_close (zti, rw, 0);
569 rec_close (&records);
570 dict_close (matchDict);
571 sortIdx_close (sortIdx);
577 static void wordInit (struct recExtractCtrl *p, RecWord *w)
579 w->zebra_maps = p->zebra_maps;
580 w->seqnos = p->seqno;
581 w->attrSet = VAL_BIB1;
586 static struct sortKey {
591 struct sortKey *next;
594 static struct recKeys {
603 static void addIndexString (RecWord *p, const char *string, int length)
610 int *pseqno = &p->seqnos[p->reg_type];
612 if (reckeys.buf_used+1024 > reckeys.buf_max)
616 b = xmalloc (reckeys.buf_max += 128000);
617 if (reckeys.buf_used > 0)
618 memcpy (b, reckeys.buf, reckeys.buf_used);
622 dst = reckeys.buf + reckeys.buf_used;
624 attrSet = p->attrSet;
625 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
628 reckeys.prevAttrSet = attrSet;
629 attrUse = p->attrUse;
630 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
633 reckeys.prevAttrUse = attrUse;
635 diff = 1 + *pseqno - reckeys.prevSeqNo;
636 if (diff >= 1 && diff <= 15)
641 reckeys.prevSeqNo = *pseqno;
647 memcpy (dst, &attrSet, sizeof(attrSet));
648 dst += sizeof(attrSet);
652 memcpy (dst, &attrUse, sizeof(attrUse));
653 dst += sizeof(attrUse);
655 *dst++ = p->reg_type;
656 memcpy (dst, string, length);
662 memcpy (dst, pseqno, sizeof(*pseqno));
663 dst += sizeof(*pseqno);
665 reckeys.buf_used = dst - reckeys.buf;
669 static void addSortString (RecWord *p, const char *string, int length)
673 for (sk = sortKeys; sk; sk = sk->next)
674 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
677 sk = xmalloc (sizeof(*sk));
681 sk->string = xmalloc (p->length);
682 sk->length = p->length;
683 memcpy (sk->string, p->string, p->length);
684 sk->attrSet = p->attrSet;
685 sk->attrUse = p->attrUse;
688 static void addString (RecWord *p, const char *string, int length)
691 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
692 addSortString (p, string, length);
694 addIndexString (p, string, length);
697 static void addIncompleteField (RecWord *p)
699 const char *b = p->string;
700 int remain = p->length;
701 const char **map = 0;
704 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
708 char buf[IT_MAX_WORD+1];
712 while (map && *map && **map == *CHR_SPACE)
714 remain = p->length - (b - p->string);
716 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
723 while (map && *map && **map != *CHR_SPACE)
725 const char *cp = *map;
727 while (i < IT_MAX_WORD && *cp)
729 remain = p->length - (b - p->string);
731 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
737 addString (p, buf, i);
741 static void addCompleteField (RecWord *p)
743 const char *b = p->string;
744 char buf[IT_MAX_WORD+1];
745 const char **map = 0;
746 int i = 0, remain = p->length;
749 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
751 while (remain > 0 && i < IT_MAX_WORD)
753 while (map && *map && **map == *CHR_SPACE)
755 remain = p->length - (b - p->string);
757 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
764 if (i && i < IT_MAX_WORD)
765 buf[i++] = *CHR_SPACE;
766 while (map && *map && **map != *CHR_SPACE)
768 const char *cp = *map;
770 if (i >= IT_MAX_WORD)
772 while (i < IT_MAX_WORD && *cp)
774 remain = p->length - (b - p->string);
776 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
784 addString (p, buf, i);
787 static void addRecordKey (RecWord *p)
789 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
790 addCompleteField (p);
792 addIncompleteField(p);
795 static void flushSortKeys (SYSNO sysno, int cmd)
797 struct sortKey *sk = sortKeys;
799 sortIdx_sysno (sortIdx, sysno);
802 struct sortKey *sk_next = sk->next;
803 sortIdx_type (sortIdx, sk->attrUse);
804 sortIdx_add (sortIdx, sk->string, sk->length);
812 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
819 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
820 while (off < reckeys->buf_used)
822 const char *src = reckeys->buf + off;
830 memcpy (&attrSet, src, sizeof(attrSet));
831 src += sizeof(attrSet);
835 memcpy (&attrUse, src, sizeof(attrUse));
836 src += sizeof(attrUse);
838 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
841 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
843 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
845 ch = zebraExplain_addSU (zti, attrSet, attrUse);
847 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
850 ((char*)key_buf) [key_buf_used++] = *src++;
852 ((char*)key_buf) [key_buf_used++] = '\0';
853 ((char*) key_buf)[key_buf_used++] = cmd;
856 seqno += ((lead>>2) & 15)-1;
859 memcpy (&seqno, src, sizeof(seqno));
860 src += sizeof(seqno);
864 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
865 key_buf_used += sizeof(key);
866 off = src - reckeys->buf;
868 assert (off == reckeys->buf_used);
871 static const char **searchRecordKey (struct recKeys *reckeys,
872 int attrSetS, int attrUseS)
874 static const char *ws[32];
882 for (i = 0; i<32; i++)
885 while (off < reckeys->buf_used)
888 const char *src = reckeys->buf + off;
896 memcpy (&attrSet, src, sizeof(attrSet));
897 src += sizeof(attrSet);
901 memcpy (&attrUse, src, sizeof(attrUse));
902 src += sizeof(attrUse);
908 seqno += ((lead>>2) & 15)-1;
911 memcpy (&seqno, src, sizeof(seqno));
912 src += sizeof(seqno);
914 if (attrUseS == attrUse && attrSetS == attrSet)
921 woff = seqno - startSeq;
922 if (woff >= 0 && woff < 31)
926 off = src - reckeys->buf;
928 assert (off == reckeys->buf_used);
932 struct file_read_info {
940 static struct file_read_info *file_read_start (int fd)
942 struct file_read_info *fi = xmalloc (sizeof(*fi));
946 fi->file_moffset = 0;
950 static void file_read_stop (struct file_read_info *fi)
955 static off_t file_seek (void *handle, off_t offset)
957 struct file_read_info *p = handle;
958 p->file_offset = offset;
959 return lseek (p->fd, offset, SEEK_SET);
962 static off_t file_tell (void *handle)
964 struct file_read_info *p = handle;
965 return p->file_offset;
968 static int file_read (void *handle, char *buf, size_t count)
970 struct file_read_info *p = handle;
973 r = read (fd, buf, count);
977 if (p->file_offset > p->file_max)
978 p->file_max = p->file_offset;
983 static void file_begin (void *handle)
985 struct file_read_info *p = handle;
987 p->file_offset = p->file_moffset;
989 lseek (p->fd, p->file_moffset, SEEK_SET);
993 static void file_end (void *handle, off_t offset)
995 struct file_read_info *p = handle;
997 assert (p->file_more == 0);
999 p->file_moffset = offset;
1002 static int atois (const char **s)
1005 while ( (c=**s) >= '0' && c <= '9')
1007 val = val*10 + c - '0';
1013 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1017 static char dstBuf[2048];
1019 const char *s = spec;
1020 static const char **w;
1025 while (*s == ' ' || *s == '\t')
1032 int attrSet, attrUse;
1036 attrSet = atois (&s);
1039 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
1040 spec, rGroup->groupName ? rGroup->groupName : "none");
1044 attrUse = atois (&s);
1045 w = searchRecordKey (reckeys, attrSet, attrUse);
1050 for (i = 0; i<32; i++)
1055 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1056 spec, rGroup->groupName ? rGroup->groupName : "none");
1061 for (i = 0; i<32; i++)
1062 if (matchFlag[i] && w[i])
1070 dst += strlen(w[i]);
1074 logf (LOG_WARN, "Record didn't contain match"
1075 " fields in (%d,%d)", attrSet, attrUse);
1083 const char *spec_src = NULL;
1084 const char *s1 = ++s;
1085 while (*s1 && *s1 != ' ' && *s1 != '\t')
1091 memcpy (special, s, spec_len);
1092 special[spec_len] = '\0';
1095 if (!strcmp (special, "group"))
1096 spec_src = rGroup->groupName;
1097 else if (!strcmp (special, "database"))
1098 spec_src = rGroup->databaseName;
1099 else if (!strcmp (special, "filename"))
1101 else if (!strcmp (special, "type"))
1102 spec_src = rGroup->recordType;
1107 strcpy (dst, spec_src);
1108 dst += strlen (spec_src);
1111 else if (*s == '\"' || *s == '\'')
1113 int stopMarker = *s++;
1117 while (*s && *s != stopMarker)
1120 tmpString[i++] = *s++;
1124 tmpString[i] = '\0';
1125 strcpy (dst, tmpString);
1126 dst += strlen (tmpString);
1130 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1131 spec, rGroup->groupName ? rGroup->groupName : "none");
1138 logf (LOG_WARN, "No match criteria for record %s in group %s",
1139 fname, rGroup->groupName ? rGroup->groupName : "none");
1146 struct recordLogInfo {
1149 struct recordGroup *rGroup;
1152 static void recordLogPreamble (int level, const char *msg, void *info)
1154 struct recordLogInfo *p = info;
1155 FILE *outf = log_file ();
1157 if (level & LOG_LOG)
1159 fprintf (outf, "File %s, offset %d, type %s\n",
1160 p->rGroup->recordType, p->recordOffset, p->fname);
1161 log_event_start (NULL, NULL);
1164 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1166 zebraExplain_addSchema (zti, oid);
1169 static int recordExtract (SYSNO *sysno, const char *fname,
1170 struct recordGroup *rGroup, int deleteFlag,
1171 struct file_read_info *fi,
1172 RecType recType, char *subType)
1174 RecordAttr *recordAttr;
1179 struct recordLogInfo logInfo;
1180 off_t recordOffset = 0;
1184 struct recExtractCtrl extractCtrl;
1186 /* we are going to read from a file, so prepare the extraction */
1189 reckeys.buf_used = 0;
1190 reckeys.prevAttrUse = -1;
1191 reckeys.prevAttrSet = -1;
1192 reckeys.prevSeqNo = 0;
1194 recordOffset = fi->file_moffset;
1195 extractCtrl.offset = fi->file_moffset;
1196 extractCtrl.readf = file_read;
1197 extractCtrl.seekf = file_seek;
1198 extractCtrl.tellf = file_tell;
1199 extractCtrl.endf = file_end;
1200 extractCtrl.fh = fi;
1201 extractCtrl.subType = subType;
1202 extractCtrl.init = wordInit;
1203 extractCtrl.addWord = addRecordKey;
1204 extractCtrl.addSchema = addSchema;
1205 extractCtrl.dh = rGroup->dh;
1206 for (i = 0; i<256; i++)
1207 extractCtrl.seqno[i] = 0;
1208 extractCtrl.zebra_maps = rGroup->zebra_maps;
1209 extractCtrl.flagShowRecords = !rGroup->flagRw;
1211 if (!rGroup->flagRw)
1212 printf ("File: %s %ld\n", fname, (long) recordOffset);
1214 logInfo.fname = fname;
1215 logInfo.recordOffset = recordOffset;
1216 logInfo.rGroup = rGroup;
1217 log_event_start (recordLogPreamble, &logInfo);
1219 r = (*recType->extract)(&extractCtrl);
1221 log_event_start (NULL, NULL);
1225 /* error occured during extraction ... */
1226 if (rGroup->flagRw &&
1227 records_processed < rGroup->fileVerboseLimit)
1229 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1230 fname, (long) recordOffset, r);
1234 if (reckeys.buf_used == 0)
1236 /* the extraction process returned no information - the record
1237 is probably empty - unless flagShowRecords is in use */
1238 if (!rGroup->flagRw)
1240 logf (LOG_WARN, "No keys generated for file %s", fname);
1241 logf (LOG_WARN, " The file is probably empty");
1246 /* perform match if sysno not known and if match criteria is specified */
1253 if (rGroup->recordId && *rGroup->recordId)
1257 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1261 rinfo = dict_lookup (matchDict, matchStr);
1263 memcpy (sysno, rinfo+1, sizeof(*sysno));
1267 logf (LOG_WARN, "Bad match criteria");
1278 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1279 fname, (long) recordOffset);
1280 logf (LOG_WARN, "cannot delete record above (seems new)");
1283 if (records_processed < rGroup->fileVerboseLimit)
1284 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1285 fname, (long) recordOffset);
1286 rec = rec_new (records);
1288 *sysno = rec->sysno;
1290 recordAttr = rec_init_attr (zti, rec);
1294 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1296 flushRecordKeys (*sysno, 1, &reckeys);
1297 flushSortKeys (*sysno, 1);
1303 /* record already exists */
1304 struct recKeys delkeys;
1306 rec = rec_get (records, *sysno);
1309 recordAttr = rec_init_attr (zti, rec);
1311 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1313 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1314 fname, (long) recordOffset);
1319 delkeys.buf_used = rec->size[recInfo_delKeys];
1320 delkeys.buf = rec->info[recInfo_delKeys];
1321 flushSortKeys (*sysno, 0);
1322 flushRecordKeys (*sysno, 0, &delkeys);
1325 /* record going to be deleted */
1326 if (!delkeys.buf_used)
1328 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1329 fname, (long) recordOffset);
1330 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1334 if (records_processed < rGroup->fileVerboseLimit)
1335 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1336 fname, (long) recordOffset);
1339 dict_delete (matchDict, matchStr);
1340 rec_del (records, &rec);
1348 /* record going to be updated */
1349 if (!delkeys.buf_used)
1351 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1352 fname, (long) recordOffset);
1353 logf (LOG_WARN, "cannot update file above, storeKeys false");
1357 if (records_processed < rGroup->fileVerboseLimit)
1358 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1359 fname, (long) recordOffset);
1360 flushRecordKeys (*sysno, 1, &reckeys);
1365 /* update file type */
1366 xfree (rec->info[recInfo_fileType]);
1367 rec->info[recInfo_fileType] =
1368 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1370 /* update filename */
1371 xfree (rec->info[recInfo_filename]);
1372 rec->info[recInfo_filename] =
1373 rec_strdup (fname, &rec->size[recInfo_filename]);
1375 /* update delete keys */
1376 xfree (rec->info[recInfo_delKeys]);
1377 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1380 rec->size[recInfo_delKeys] = reckeys.buf_used;
1381 rec->info[recInfo_delKeys] = reckeys.buf;
1383 reckeys.buf_max = 0;
1385 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1386 rec->size[recInfo_delKeys] = reckeys.buf_used;
1387 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1388 rec->size[recInfo_delKeys]);
1393 rec->info[recInfo_delKeys] = NULL;
1394 rec->size[recInfo_delKeys] = 0;
1397 /* save file size of original record */
1398 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1399 recordAttr->recordSize = fi->file_moffset - recordOffset;
1400 if (!recordAttr->recordSize)
1401 recordAttr->recordSize = fi->file_max - recordOffset;
1402 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1404 /* set run-number for this record */
1405 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1407 /* update store data */
1408 xfree (rec->info[recInfo_storeData]);
1409 if (rGroup->flagStoreData == 1)
1411 rec->size[recInfo_storeData] = recordAttr->recordSize;
1412 rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
1413 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1415 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1416 (long) recordOffset);
1419 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1420 < recordAttr->recordSize)
1422 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1423 recordAttr->recordSize, fname);
1429 rec->info[recInfo_storeData] = NULL;
1430 rec->size[recInfo_storeData] = 0;
1432 /* update database name */
1433 xfree (rec->info[recInfo_databaseName]);
1434 rec->info[recInfo_databaseName] =
1435 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1438 recordAttr->recordOffset = recordOffset;
1440 /* commit this record */
1441 rec_put (records, &rec);
1446 int fileExtract (SYSNO *sysno, const char *fname,
1447 const struct recordGroup *rGroupP, int deleteFlag)
1455 struct recordGroup rGroupM;
1456 struct recordGroup *rGroup = &rGroupM;
1457 struct file_read_info *fi;
1459 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1461 if (!rGroup->groupName || !*rGroup->groupName)
1464 sprintf (gprefix, "%s.", rGroup->groupName);
1466 logf (LOG_DEBUG, "fileExtract %s", fname);
1468 /* determine file extension */
1470 for (i = strlen(fname); --i >= 0; )
1471 if (fname[i] == '/')
1473 else if (fname[i] == '.')
1475 strcpy (ext, fname+i+1);
1478 /* determine file type - depending on extension */
1479 if (!rGroup->recordType)
1481 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1482 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1484 sprintf (ext_res, "%srecordType", gprefix);
1485 rGroup->recordType = res_get (common_resource, ext_res);
1488 if (!rGroup->recordType)
1490 if (records_processed < rGroup->fileVerboseLimit)
1491 logf (LOG_LOG, "? %s", fname);
1494 if (!*rGroup->recordType)
1496 if (!(recType = recType_byName (rGroup->recordType, subType)))
1498 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1502 /* determine match criteria */
1503 if (!rGroup->recordId)
1505 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1506 rGroup->recordId = res_get (common_resource, ext_res);
1509 /* determine database name */
1510 if (!rGroup->databaseName)
1512 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1513 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1515 sprintf (ext_res, "%sdatabase", gprefix);
1516 rGroup->databaseName = res_get (common_resource, ext_res);
1519 if (!rGroup->databaseName)
1520 rGroup->databaseName = "Default";
1522 /* determine if explain database */
1524 sprintf (ext_res, "%sexplainDatabase", gprefix);
1525 rGroup->explainDatabase =
1526 atoi (res_get_def (common_resource, ext_res, "0"));
1528 /* announce database */
1529 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1531 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1532 rGroup->explainDatabase))
1536 if (rGroup->flagStoreData == -1)
1539 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1540 if (!(sval = res_get (common_resource, ext_res)))
1542 sprintf (ext_res, "%sstoreData", gprefix);
1543 sval = res_get (common_resource, ext_res);
1546 rGroup->flagStoreData = atoi (sval);
1548 if (rGroup->flagStoreData == -1)
1549 rGroup->flagStoreData = 0;
1551 if (rGroup->flagStoreKeys == -1)
1555 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1556 if (!(sval = res_get (common_resource, ext_res)))
1558 sprintf (ext_res, "%sstoreKeys", gprefix);
1559 sval = res_get (common_resource, ext_res);
1562 rGroup->flagStoreKeys = atoi (sval);
1564 if (rGroup->flagStoreKeys == -1)
1565 rGroup->flagStoreKeys = 0;
1567 if (sysno && deleteFlag)
1571 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1573 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1577 fi = file_read_start (fd);
1581 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1583 } while (r && !sysno && fi->file_more);
1584 file_read_stop (fi);
1590 static int explain_extract (void *handle, Record rec, data1_node *n)
1592 struct recordGroup *rGroup = (struct recordGroup*) handle;
1593 struct recExtractCtrl extractCtrl;
1596 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1598 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1602 reckeys.buf_used = 0;
1603 reckeys.prevAttrUse = -1;
1604 reckeys.prevAttrSet = -1;
1605 reckeys.prevSeqNo = 0;
1607 extractCtrl.init = wordInit;
1608 extractCtrl.addWord = addRecordKey;
1609 extractCtrl.addSchema = addSchema;
1610 extractCtrl.dh = rGroup->dh;
1611 for (i = 0; i<256; i++)
1612 extractCtrl.seqno[i] = 0;
1613 extractCtrl.zebra_maps = rGroup->zebra_maps;
1614 extractCtrl.flagShowRecords = !rGroup->flagRw;
1616 grs_extract_tree(&extractCtrl, n);
1618 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1620 if (rec->size[recInfo_delKeys])
1622 struct recKeys delkeys;
1624 delkeys.buf_used = rec->size[recInfo_delKeys];
1625 delkeys.buf = rec->info[recInfo_delKeys];
1626 flushSortKeys (rec->sysno, 0);
1627 flushRecordKeys (rec->sysno, 0, &delkeys);
1629 flushRecordKeys (rec->sysno, 1, &reckeys);
1630 flushSortKeys (rec->sysno, 1);
1632 xfree (rec->info[recInfo_delKeys]);
1633 rec->size[recInfo_delKeys] = reckeys.buf_used;
1634 rec->info[recInfo_delKeys] = reckeys.buf;
1636 reckeys.buf_max = 0;