2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.84 1998-06-11 15:42:22 adam
8 * Changed the way use attributes are specified in the recordId
11 * Revision 1.83 1998/06/08 14:43:10 adam
12 * Added suport for EXPLAIN Proxy servers - added settings databasePath
13 * and explainDatabase to facilitate this. Increased maximum number
14 * of databases and attributes in one register.
16 * Revision 1.82 1998/05/20 10:12:15 adam
17 * Implemented automatic EXPLAIN database maintenance.
18 * Modified Zebra to work with ASN.1 compiled version of YAZ.
20 * Revision 1.81 1998/03/11 11:19:04 adam
21 * Changed the way sequence numbers are generated.
23 * Revision 1.80 1998/03/05 08:45:11 adam
24 * New result set model and modular ranking system. Moved towards
25 * descent server API. System information stored as "SGML" records.
27 * Revision 1.79 1998/02/17 10:32:52 adam
28 * Fixed bug: binary files weren't opened with flag b on NT.
30 * Revision 1.78 1998/02/10 12:03:05 adam
33 * Revision 1.77 1998/01/12 15:04:08 adam
34 * The test option (-s) only uses read-lock (and not write lock).
36 * Revision 1.76 1997/10/27 14:33:04 adam
37 * Moved towards generic character mapping depending on "structure"
38 * field in abstract syntax file. Fixed a few memory leaks. Fixed
39 * bug with negative integers when doing searches with relational
42 * Revision 1.75 1997/09/17 12:19:12 adam
43 * Zebra version corresponds to YAZ version 1.4.
44 * Changed Zebra server so that it doesn't depend on global common_resource.
46 * Revision 1.74 1997/09/09 13:38:06 adam
47 * Partial port to WIN95/NT.
49 * Revision 1.73 1997/09/04 13:57:20 adam
50 * New file extract/retrieve method tellf (added).
51 * Added O_BINARY for open calls.
53 * Revision 1.72 1997/07/15 16:32:29 adam
54 * Bug fix: Match handler didn't terminate the resulting string!
56 * Revision 1.71 1997/07/15 16:28:41 adam
57 * Bug fix: storeData didn't work with files with multiple records.
58 * Bug fix: fixed memory management with records; not really well
61 * Revision 1.70 1997/07/01 13:00:42 adam
62 * Bug fix in routine searchRecordKey: uninitialized variables.
64 * Revision 1.69 1997/04/29 09:26:03 adam
65 * Bug fix: generic recordId handling didn't work for compressed internal
68 * Revision 1.68 1997/02/12 20:39:45 adam
69 * Implemented options -f <n> that limits the log to the first <n>
71 * Changed some log messages also.
73 * Revision 1.67 1996/11/15 15:02:14 adam
74 * Minor changes regarding logging.
76 * Revision 1.66 1996/11/14 09:52:21 adam
77 * Strings in record keys bound by IT_MAX_WORD.
79 * Revision 1.65 1996/11/14 08:57:56 adam
80 * Reduction of storeKeys area.
82 * Revision 1.64 1996/11/08 11:10:16 adam
83 * Buffers used during file match got bigger.
84 * Compressed ISAM support everywhere.
85 * Bug fixes regarding masking characters in queries.
86 * Redesigned Regexp-2 queries.
88 * Revision 1.63 1996/10/29 14:09:39 adam
89 * Use of cisam system - enabled if setting isamc is 1.
91 * Revision 1.62 1996/10/11 10:57:01 adam
92 * New module recctrl. Used to manage records (extract/retrieval).
93 * Several files have been moved to the recctrl sub directory.
95 * Revision 1.61 1996/06/06 12:08:37 quinn
96 * Added showRecord function
98 * Revision 1.60 1996/06/04 10:18:12 adam
99 * Search/scan uses character mapping module.
101 * Revision 1.59 1996/05/14 15:47:07 adam
102 * Cleanup of various buffer size entities.
104 * Revision 1.58 1996/05/14 06:16:38 adam
105 * Compact use/set bytes used in search service.
107 * Revision 1.57 1996/05/13 14:23:04 adam
108 * Work on compaction of set/use bytes in dictionary.
110 * Revision 1.56 1996/05/09 09:54:42 adam
111 * Server supports maps from one logical attributes to a list of physical
113 * The extraction process doesn't make space consuming 'any' keys.
115 * Revision 1.55 1996/05/09 07:28:55 quinn
116 * Work towards phrases and multiple registers
118 * Revision 1.54 1996/05/01 13:46:35 adam
119 * First work on multiple records in one file.
120 * New option, -offset, to the "unread" command in the filter module.
122 * Revision 1.53 1996/04/26 12:09:43 adam
123 * Added a few comments.
125 * Revision 1.52 1996/04/25 13:27:57 adam
126 * Function recordExtract modified so that files with no keys (possibly empty)
129 * Revision 1.51 1996/03/19 11:08:42 adam
130 * Bug fix: Log preamble wasn't always turned off after recordExtract.
132 * Revision 1.50 1996/02/12 18:45:36 adam
133 * New fileVerboseFlag in record group control.
135 * Revision 1.49 1996/02/05 12:29:57 adam
136 * Logging reduced a bit.
137 * The remaining running time is estimated during register merge.
139 * Revision 1.48 1996/02/01 20:53:26 adam
140 * The temporary per-record keys are compacted a little, and duplication
141 * of the per-records keys are avoided when they are saved in the record
142 * information buffer.
144 * Revision 1.47 1996/01/17 14:57:48 adam
145 * Prototype changed for reader functions in extract/retrieve. File
146 * is identified by 'void *' instead of 'int.
148 * Revision 1.46 1995/12/15 14:57:16 adam
151 * Revision 1.45 1995/12/15 12:37:41 adam
152 * In addRecordKeyAny: Writes key only when attrSet != -1.
154 * Revision 1.44 1995/12/12 16:00:54 adam
155 * System call sync(2) used after update/commit.
156 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
159 * Revision 1.43 1995/12/11 09:12:46 adam
160 * The rec_get function returns NULL if record doesn't exist - will
161 * happen in the server if the result set records have been deleted since
162 * the creation of the set (i.e. the search).
163 * The server saves a result temporarily if it is 'volatile', i.e. the
164 * set is register dependent.
166 * Revision 1.42 1995/12/07 17:38:46 adam
167 * Work locking mechanisms for concurrent updates/commit.
169 * Revision 1.41 1995/12/06 16:06:42 adam
170 * Better diagnostics. Work on 'real' dictionary deletion.
172 * Revision 1.40 1995/12/05 16:57:40 adam
173 * More work on regular patterns.
175 * Revision 1.39 1995/12/05 13:20:18 adam
176 * Bug fix: file_read sometimes returned early EOF.
178 * Revision 1.38 1995/12/04 17:59:21 adam
179 * More work on regular expression conversion.
181 * Revision 1.37 1995/12/04 14:22:27 adam
182 * Extra arg to recType_byName.
183 * Started work on new regular expression parsed input to
184 * structured records.
186 * Revision 1.36 1995/11/30 08:34:29 adam
187 * Started work on commit facility.
188 * Changed a few malloc/free to xmalloc/xfree.
190 * Revision 1.35 1995/11/28 14:26:21 adam
191 * Bug fix: recordId with constant wasn't right.
192 * Bug fix: recordId dictionary entry wasn't deleted when needed.
194 * Revision 1.34 1995/11/28 09:09:38 adam
195 * Zebra config renamed.
196 * Use setting 'recordId' to identify record now.
197 * Bug fix in recindex.c: rec_release_blocks was invokeded even
198 * though the blocks were already released.
199 * File traversal properly deletes records when needed.
201 * Revision 1.33 1995/11/27 09:56:20 adam
202 * Record info elements better enumerated. Internal store of records.
204 * Revision 1.32 1995/11/25 10:24:05 adam
205 * More record fields - they are enumerated now.
206 * New options: flagStoreData flagStoreKey.
208 * Revision 1.31 1995/11/24 11:31:35 adam
209 * Commands add & del read filenames from stdin if source directory is
211 * Match criteria supports 'constant' strings.
213 * Revision 1.30 1995/11/22 17:19:16 adam
214 * Record management uses the bfile system.
216 * Revision 1.29 1995/11/21 15:01:14 adam
217 * New general match criteria implemented.
218 * New feature: document groups.
220 * Revision 1.28 1995/11/21 09:20:30 adam
221 * Yet more work on record match.
223 * Revision 1.27 1995/11/20 16:59:45 adam
224 * New update method: the 'old' keys are saved for each records.
226 * Revision 1.26 1995/11/20 11:56:24 adam
227 * Work on new traversal.
229 * Revision 1.25 1995/11/16 15:34:54 adam
230 * Uses new record management system in both indexer and server.
232 * Revision 1.24 1995/11/15 19:13:08 adam
233 * Work on record management.
235 * Revision 1.23 1995/10/27 14:00:10 adam
236 * Implemented detection of database availability.
238 * Revision 1.22 1995/10/17 18:02:07 adam
239 * New feature: databases. Implemented as prefix to words in dictionary.
241 * Revision 1.21 1995/10/10 12:24:38 adam
242 * Temporary sort files are compressed.
244 * Revision 1.20 1995/10/06 13:52:05 adam
245 * Bug fixes. Handler may abort further scanning.
247 * Revision 1.19 1995/10/04 12:55:16 adam
248 * Bug fix in ranked search. Use=Any keys inserted.
250 * Revision 1.18 1995/10/04 09:37:08 quinn
253 * Revision 1.17 1995/10/03 14:28:57 adam
254 * Buffered read in extract works.
256 * Revision 1.16 1995/10/03 14:28:45 adam
257 * Work on more effecient read handler in extract.
259 * Revision 1.15 1995/10/02 15:42:53 adam
260 * Extract uses file descriptors instead of FILE pointers.
262 * Revision 1.14 1995/10/02 15:29:13 adam
263 * More logging in file_extract.
265 * Revision 1.13 1995/09/29 14:01:39 adam
268 * Revision 1.12 1995/09/28 14:22:56 adam
269 * Sort uses smaller temporary files.
271 * Revision 1.11 1995/09/28 12:10:31 adam
272 * Bug fixes. Field prefix used in queries.
274 * Revision 1.10 1995/09/28 09:19:41 adam
275 * xfree/xmalloc used everywhere.
276 * Extract/retrieve method seems to work for text records.
278 * Revision 1.9 1995/09/27 12:22:28 adam
279 * More work on extract in record control.
280 * Field name is not in isam keys but in prefix in dictionary words.
282 * Revision 1.8 1995/09/14 07:48:22 adam
283 * Record control management.
285 * Revision 1.7 1995/09/11 13:09:32 adam
286 * More work on relevance feedback.
288 * Revision 1.6 1995/09/08 14:52:27 adam
289 * Minor changes. Dictionary is lower case now.
291 * Revision 1.5 1995/09/06 16:11:16 adam
292 * Option: only one word key per file.
294 * Revision 1.4 1995/09/05 15:28:39 adam
295 * More work on search engine.
297 * Revision 1.3 1995/09/04 12:33:41 adam
298 * Various cleanup. YAZ util used instead.
300 * Revision 1.2 1995/09/04 09:10:34 adam
301 * More work on index add/del/update.
302 * Merge sort implemented.
303 * Initial work on z39 server.
305 * Revision 1.1 1995/09/01 14:06:35 adam
306 * Split of work into more files.
325 static Dict matchDict;
327 static Records records = NULL;
328 static SortIdx sortIdx = NULL;
330 static char **key_buf;
331 static size_t ptr_top;
333 static size_t key_buf_used;
334 static int key_file_no;
336 static int records_inserted = 0;
337 static int records_updated = 0;
338 static int records_deleted = 0;
339 static int records_processed = 0;
341 static ZebraExplainInfo zti = NULL;
343 static void logRecord (int showFlag)
347 if (showFlag || !(records_processed % 1000))
349 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
350 records_processed, records_inserted, records_updated,
355 static int explain_extract (void *handle, Record drec, data1_node *n);
357 int key_open (struct recordGroup *rGroup, int mem)
359 BFiles bfs = rGroup->bfs;
360 int rw = rGroup->flagRw;
361 data1_handle dh = rGroup->dh;
363 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
366 key_buf = xmalloc (mem);
367 ptr_top = mem/sizeof(char*);
373 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
375 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
379 records = rec_open (bfs, rw);
382 dict_close (matchDict);
385 zti = zebraExplain_open (records, dh, common_resource,
386 rw, rGroup, explain_extract);
389 rec_close (&records);
390 dict_close (matchDict);
393 sortIdx = sortIdx_open (bfs, 1);
403 void encode_key_init (struct encode_info *i)
409 char *encode_key_int (int d, char *bp)
418 else if (d <= 4194303)
420 *bp++ = 128 + (d>>16);
421 *bp++ = (d>>8) & 255;
426 *bp++ = 192 + (d>>24);
427 *bp++ = (d>>16) & 255;
428 *bp++ = (d>>8) & 255;
434 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
439 while ((*bp++ = *k++))
441 memcpy (&key, k+1, sizeof(struct it_key));
442 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
443 if (i->sysno != key.sysno)
445 i->sysno = key.sysno;
448 bp = encode_key_int (key.seqno - i->seqno, bp);
449 i->seqno = key.seqno;
450 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
452 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
460 static int key_y_len;
462 static int key_y_compare (const void *p1, const void *p2)
466 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
467 *(char**) p2 + key_y_len + 1)))
469 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
472 static int key_x_compare (const void *p1, const void *p2)
474 return strcmp (*(char**) p1, *(char**) p2);
478 void key_flush (void)
483 struct encode_info encode_info;
492 logf (LOG_LOG, "sorting section %d", key_file_no);
494 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
495 getFnameTmp (out_fname, key_file_no);
497 if (!(outf = fopen (out_fname, "wb")))
499 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
502 logf (LOG_LOG, "writing section %d", key_file_no);
503 prevcp = cp = key_buf[ptr_top-ptr_i];
505 encode_key_init (&encode_info);
506 encode_key_write (cp, &encode_info, outf);
509 cp = key_buf[ptr_top-ptr_i];
510 if (strcmp (cp, prevcp))
512 encode_key_init (&encode_info);
513 encode_key_write (cp, &encode_info, outf);
517 encode_key_write (cp + strlen(cp), &encode_info, outf);
520 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
521 getFnameTmp (out_fname, key_file_no);
523 if (!(outf = fopen (out_fname, "wb")))
525 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
528 logf (LOG_LOG, "writing section %d", key_file_no);
530 prevcp = key_buf[ptr_top-i];
532 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
534 key_y_len = strlen(prevcp)+1;
536 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
537 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
539 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
540 sizeof(char*), key_y_compare);
541 cp = key_buf[ptr_top-ptr_i];
543 encode_key_init (&encode_info);
544 encode_key_write (cp, &encode_info, outf);
547 cp = key_buf[ptr_top-ptr_i];
548 encode_key_write (cp+key_y_len, &encode_info, outf);
552 prevcp = key_buf[ptr_top-ptr_i];
557 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
560 logf (LOG_LOG, "finished section %d", key_file_no);
565 int key_close (struct recordGroup *rGroup)
567 int rw = rGroup->flagRw;
569 zebraExplain_runNumberIncrement (zti, 1);
570 zebraExplain_close (zti, rw, 0);
573 rec_close (&records);
574 dict_close (matchDict);
575 sortIdx_close (sortIdx);
581 static void wordInit (struct recExtractCtrl *p, RecWord *w)
583 w->zebra_maps = p->zebra_maps;
584 w->seqnos = p->seqno;
585 w->attrSet = VAL_BIB1;
590 static struct sortKey {
595 struct sortKey *next;
598 static struct recKeys {
607 static void addIndexString (RecWord *p, const char *string, int length)
614 int *pseqno = &p->seqnos[p->reg_type];
616 if (reckeys.buf_used+1024 > reckeys.buf_max)
620 b = xmalloc (reckeys.buf_max += 128000);
621 if (reckeys.buf_used > 0)
622 memcpy (b, reckeys.buf, reckeys.buf_used);
626 dst = reckeys.buf + reckeys.buf_used;
628 attrSet = p->attrSet;
629 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
632 reckeys.prevAttrSet = attrSet;
633 attrUse = p->attrUse;
634 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
637 reckeys.prevAttrUse = attrUse;
639 diff = 1 + *pseqno - reckeys.prevSeqNo;
640 if (diff >= 1 && diff <= 15)
645 reckeys.prevSeqNo = *pseqno;
651 memcpy (dst, &attrSet, sizeof(attrSet));
652 dst += sizeof(attrSet);
656 memcpy (dst, &attrUse, sizeof(attrUse));
657 dst += sizeof(attrUse);
659 *dst++ = p->reg_type;
660 memcpy (dst, string, length);
666 memcpy (dst, pseqno, sizeof(*pseqno));
667 dst += sizeof(*pseqno);
669 reckeys.buf_used = dst - reckeys.buf;
673 static void addSortString (RecWord *p, const char *string, int length)
677 for (sk = sortKeys; sk; sk = sk->next)
678 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
681 sk = xmalloc (sizeof(*sk));
685 sk->string = xmalloc (p->length);
686 sk->length = p->length;
687 memcpy (sk->string, p->string, p->length);
688 sk->attrSet = p->attrSet;
689 sk->attrUse = p->attrUse;
692 static void addString (RecWord *p, const char *string, int length)
695 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
696 addSortString (p, string, length);
698 addIndexString (p, string, length);
701 static void addIncompleteField (RecWord *p)
703 const char *b = p->string;
704 int remain = p->length;
705 const char **map = 0;
708 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
712 char buf[IT_MAX_WORD+1];
716 while (map && *map && **map == *CHR_SPACE)
718 remain = p->length - (b - p->string);
720 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
727 while (map && *map && **map != *CHR_SPACE)
729 const char *cp = *map;
731 while (i < IT_MAX_WORD && *cp)
733 remain = p->length - (b - p->string);
735 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
741 addString (p, buf, i);
745 static void addCompleteField (RecWord *p)
747 const char *b = p->string;
748 char buf[IT_MAX_WORD+1];
749 const char **map = 0;
750 int i = 0, remain = p->length;
753 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
755 while (remain > 0 && i < IT_MAX_WORD)
757 while (map && *map && **map == *CHR_SPACE)
759 remain = p->length - (b - p->string);
761 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
768 if (i && i < IT_MAX_WORD)
769 buf[i++] = *CHR_SPACE;
770 while (map && *map && **map != *CHR_SPACE)
772 const char *cp = *map;
774 if (i >= IT_MAX_WORD)
776 while (i < IT_MAX_WORD && *cp)
778 remain = p->length - (b - p->string);
780 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
788 addString (p, buf, i);
791 static void addRecordKey (RecWord *p)
793 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
794 addCompleteField (p);
796 addIncompleteField(p);
799 static void flushSortKeys (SYSNO sysno, int cmd)
801 struct sortKey *sk = sortKeys;
803 sortIdx_sysno (sortIdx, sysno);
806 struct sortKey *sk_next = sk->next;
807 sortIdx_type (sortIdx, sk->attrUse);
808 sortIdx_add (sortIdx, sk->string, sk->length);
816 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
823 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
824 while (off < reckeys->buf_used)
826 const char *src = reckeys->buf + off;
834 memcpy (&attrSet, src, sizeof(attrSet));
835 src += sizeof(attrSet);
839 memcpy (&attrUse, src, sizeof(attrUse));
840 src += sizeof(attrUse);
842 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
845 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
847 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
849 ch = zebraExplain_addSU (zti, attrSet, attrUse);
851 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
854 ((char*)key_buf) [key_buf_used++] = *src++;
856 ((char*)key_buf) [key_buf_used++] = '\0';
857 ((char*) key_buf)[key_buf_used++] = cmd;
860 seqno += ((lead>>2) & 15)-1;
863 memcpy (&seqno, src, sizeof(seqno));
864 src += sizeof(seqno);
868 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
869 key_buf_used += sizeof(key);
870 off = src - reckeys->buf;
872 assert (off == reckeys->buf_used);
875 static const char **searchRecordKey (struct recKeys *reckeys,
876 int attrSetS, int attrUseS)
878 static const char *ws[32];
886 for (i = 0; i<32; i++)
889 while (off < reckeys->buf_used)
892 const char *src = reckeys->buf + off;
900 memcpy (&attrSet, src, sizeof(attrSet));
901 src += sizeof(attrSet);
905 memcpy (&attrUse, src, sizeof(attrUse));
906 src += sizeof(attrUse);
912 seqno += ((lead>>2) & 15)-1;
915 memcpy (&seqno, src, sizeof(seqno));
916 src += sizeof(seqno);
918 if (attrUseS == attrUse && attrSetS == attrSet)
925 woff = seqno - startSeq;
926 if (woff >= 0 && woff < 31)
930 off = src - reckeys->buf;
932 assert (off == reckeys->buf_used);
936 struct file_read_info {
944 static struct file_read_info *file_read_start (int fd)
946 struct file_read_info *fi = xmalloc (sizeof(*fi));
950 fi->file_moffset = 0;
954 static void file_read_stop (struct file_read_info *fi)
959 static off_t file_seek (void *handle, off_t offset)
961 struct file_read_info *p = handle;
962 p->file_offset = offset;
963 return lseek (p->fd, offset, SEEK_SET);
966 static off_t file_tell (void *handle)
968 struct file_read_info *p = handle;
969 return p->file_offset;
972 static int file_read (void *handle, char *buf, size_t count)
974 struct file_read_info *p = handle;
977 r = read (fd, buf, count);
981 if (p->file_offset > p->file_max)
982 p->file_max = p->file_offset;
987 static void file_begin (void *handle)
989 struct file_read_info *p = handle;
991 p->file_offset = p->file_moffset;
993 lseek (p->fd, p->file_moffset, SEEK_SET);
997 static void file_end (void *handle, off_t offset)
999 struct file_read_info *p = handle;
1001 assert (p->file_more == 0);
1003 p->file_moffset = offset;
1006 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1007 const char *fname, const char *spec)
1009 static char dstBuf[2048];
1011 const char *s = spec;
1012 static const char **w;
1016 while (*s == ' ' || *s == '\t')
1022 char attset_str[64], attname_str[64];
1023 data1_attset *attset;
1026 int attSet = 1, attUse = 1;
1030 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1032 attset_str[i++] = *s;
1033 attset_str[i] = '\0';
1038 for (i = 0; *s && *s != ')'; s++)
1040 attname_str[i++] = *s;
1041 attname_str[i] = '\0';
1044 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1047 attSet = attset->reference;
1048 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1050 attUse = att->value;
1052 attUse = atoi (attname_str);
1054 w = searchRecordKey (reckeys, attSet, attUse);
1059 for (i = 0; i<32; i++)
1064 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1065 spec, rGroup->groupName ? rGroup->groupName : "none");
1070 for (i = 0; i<32; i++)
1071 if (matchFlag[i] && w[i])
1079 dst += strlen(w[i]);
1083 logf (LOG_WARN, "Record didn't contain match"
1084 " fields in (%s,%s)", attset_str, attname_str);
1092 const char *spec_src = NULL;
1093 const char *s1 = ++s;
1094 while (*s1 && *s1 != ' ' && *s1 != '\t')
1100 memcpy (special, s, spec_len);
1101 special[spec_len] = '\0';
1104 if (!strcmp (special, "group"))
1105 spec_src = rGroup->groupName;
1106 else if (!strcmp (special, "database"))
1107 spec_src = rGroup->databaseName;
1108 else if (!strcmp (special, "filename"))
1110 else if (!strcmp (special, "type"))
1111 spec_src = rGroup->recordType;
1116 strcpy (dst, spec_src);
1117 dst += strlen (spec_src);
1120 else if (*s == '\"' || *s == '\'')
1122 int stopMarker = *s++;
1126 while (*s && *s != stopMarker)
1129 tmpString[i++] = *s++;
1133 tmpString[i] = '\0';
1134 strcpy (dst, tmpString);
1135 dst += strlen (tmpString);
1139 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1140 spec, rGroup->groupName ? rGroup->groupName : "none");
1147 logf (LOG_WARN, "No match criteria for record %s in group %s",
1148 fname, rGroup->groupName ? rGroup->groupName : "none");
1155 struct recordLogInfo {
1158 struct recordGroup *rGroup;
1161 static void recordLogPreamble (int level, const char *msg, void *info)
1163 struct recordLogInfo *p = info;
1164 FILE *outf = log_file ();
1166 if (level & LOG_LOG)
1168 fprintf (outf, "File %s, offset %d, type %s\n",
1169 p->rGroup->recordType, p->recordOffset, p->fname);
1170 log_event_start (NULL, NULL);
1173 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1175 zebraExplain_addSchema (zti, oid);
1178 static int recordExtract (SYSNO *sysno, const char *fname,
1179 struct recordGroup *rGroup, int deleteFlag,
1180 struct file_read_info *fi,
1181 RecType recType, char *subType)
1183 RecordAttr *recordAttr;
1188 struct recordLogInfo logInfo;
1189 off_t recordOffset = 0;
1193 struct recExtractCtrl extractCtrl;
1195 /* we are going to read from a file, so prepare the extraction */
1198 reckeys.buf_used = 0;
1199 reckeys.prevAttrUse = -1;
1200 reckeys.prevAttrSet = -1;
1201 reckeys.prevSeqNo = 0;
1203 recordOffset = fi->file_moffset;
1204 extractCtrl.offset = fi->file_moffset;
1205 extractCtrl.readf = file_read;
1206 extractCtrl.seekf = file_seek;
1207 extractCtrl.tellf = file_tell;
1208 extractCtrl.endf = file_end;
1209 extractCtrl.fh = fi;
1210 extractCtrl.subType = subType;
1211 extractCtrl.init = wordInit;
1212 extractCtrl.addWord = addRecordKey;
1213 extractCtrl.addSchema = addSchema;
1214 extractCtrl.dh = rGroup->dh;
1215 for (i = 0; i<256; i++)
1216 extractCtrl.seqno[i] = 0;
1217 extractCtrl.zebra_maps = rGroup->zebra_maps;
1218 extractCtrl.flagShowRecords = !rGroup->flagRw;
1220 if (!rGroup->flagRw)
1221 printf ("File: %s %ld\n", fname, (long) recordOffset);
1223 logInfo.fname = fname;
1224 logInfo.recordOffset = recordOffset;
1225 logInfo.rGroup = rGroup;
1226 log_event_start (recordLogPreamble, &logInfo);
1228 r = (*recType->extract)(&extractCtrl);
1230 log_event_start (NULL, NULL);
1234 /* error occured during extraction ... */
1235 if (rGroup->flagRw &&
1236 records_processed < rGroup->fileVerboseLimit)
1238 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1239 fname, (long) recordOffset, r);
1243 if (reckeys.buf_used == 0)
1245 /* the extraction process returned no information - the record
1246 is probably empty - unless flagShowRecords is in use */
1247 if (!rGroup->flagRw)
1249 logf (LOG_WARN, "No keys generated for file %s", fname);
1250 logf (LOG_WARN, " The file is probably empty");
1255 /* perform match if sysno not known and if match criteria is specified */
1262 if (rGroup->recordId && *rGroup->recordId)
1266 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1270 rinfo = dict_lookup (matchDict, matchStr);
1272 memcpy (sysno, rinfo+1, sizeof(*sysno));
1276 logf (LOG_WARN, "Bad match criteria");
1287 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1288 fname, (long) recordOffset);
1289 logf (LOG_WARN, "cannot delete record above (seems new)");
1292 if (records_processed < rGroup->fileVerboseLimit)
1293 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1294 fname, (long) recordOffset);
1295 rec = rec_new (records);
1297 *sysno = rec->sysno;
1299 recordAttr = rec_init_attr (zti, rec);
1303 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1305 flushRecordKeys (*sysno, 1, &reckeys);
1306 flushSortKeys (*sysno, 1);
1312 /* record already exists */
1313 struct recKeys delkeys;
1315 rec = rec_get (records, *sysno);
1318 recordAttr = rec_init_attr (zti, rec);
1320 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1322 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1323 fname, (long) recordOffset);
1328 delkeys.buf_used = rec->size[recInfo_delKeys];
1329 delkeys.buf = rec->info[recInfo_delKeys];
1330 flushSortKeys (*sysno, 0);
1331 flushRecordKeys (*sysno, 0, &delkeys);
1334 /* record going to be deleted */
1335 if (!delkeys.buf_used)
1337 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1338 fname, (long) recordOffset);
1339 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1343 if (records_processed < rGroup->fileVerboseLimit)
1344 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1345 fname, (long) recordOffset);
1348 dict_delete (matchDict, matchStr);
1349 rec_del (records, &rec);
1357 /* record going to be updated */
1358 if (!delkeys.buf_used)
1360 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1361 fname, (long) recordOffset);
1362 logf (LOG_WARN, "cannot update file above, storeKeys false");
1366 if (records_processed < rGroup->fileVerboseLimit)
1367 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1368 fname, (long) recordOffset);
1369 flushRecordKeys (*sysno, 1, &reckeys);
1374 /* update file type */
1375 xfree (rec->info[recInfo_fileType]);
1376 rec->info[recInfo_fileType] =
1377 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1379 /* update filename */
1380 xfree (rec->info[recInfo_filename]);
1381 rec->info[recInfo_filename] =
1382 rec_strdup (fname, &rec->size[recInfo_filename]);
1384 /* update delete keys */
1385 xfree (rec->info[recInfo_delKeys]);
1386 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1389 rec->size[recInfo_delKeys] = reckeys.buf_used;
1390 rec->info[recInfo_delKeys] = reckeys.buf;
1392 reckeys.buf_max = 0;
1394 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1395 rec->size[recInfo_delKeys] = reckeys.buf_used;
1396 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1397 rec->size[recInfo_delKeys]);
1402 rec->info[recInfo_delKeys] = NULL;
1403 rec->size[recInfo_delKeys] = 0;
1406 /* save file size of original record */
1407 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1408 recordAttr->recordSize = fi->file_moffset - recordOffset;
1409 if (!recordAttr->recordSize)
1410 recordAttr->recordSize = fi->file_max - recordOffset;
1411 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1413 /* set run-number for this record */
1414 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1416 /* update store data */
1417 xfree (rec->info[recInfo_storeData]);
1418 if (rGroup->flagStoreData == 1)
1420 rec->size[recInfo_storeData] = recordAttr->recordSize;
1421 rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
1422 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1424 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1425 (long) recordOffset);
1428 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1429 < recordAttr->recordSize)
1431 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1432 recordAttr->recordSize, fname);
1438 rec->info[recInfo_storeData] = NULL;
1439 rec->size[recInfo_storeData] = 0;
1441 /* update database name */
1442 xfree (rec->info[recInfo_databaseName]);
1443 rec->info[recInfo_databaseName] =
1444 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1447 recordAttr->recordOffset = recordOffset;
1449 /* commit this record */
1450 rec_put (records, &rec);
1455 int fileExtract (SYSNO *sysno, const char *fname,
1456 const struct recordGroup *rGroupP, int deleteFlag)
1464 struct recordGroup rGroupM;
1465 struct recordGroup *rGroup = &rGroupM;
1466 struct file_read_info *fi;
1468 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1470 if (!rGroup->groupName || !*rGroup->groupName)
1473 sprintf (gprefix, "%s.", rGroup->groupName);
1475 logf (LOG_DEBUG, "fileExtract %s", fname);
1477 /* determine file extension */
1479 for (i = strlen(fname); --i >= 0; )
1480 if (fname[i] == '/')
1482 else if (fname[i] == '.')
1484 strcpy (ext, fname+i+1);
1487 /* determine file type - depending on extension */
1488 if (!rGroup->recordType)
1490 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1491 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1493 sprintf (ext_res, "%srecordType", gprefix);
1494 rGroup->recordType = res_get (common_resource, ext_res);
1497 if (!rGroup->recordType)
1499 if (records_processed < rGroup->fileVerboseLimit)
1500 logf (LOG_LOG, "? %s", fname);
1503 if (!*rGroup->recordType)
1505 if (!(recType = recType_byName (rGroup->recordType, subType)))
1507 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1511 /* determine match criteria */
1512 if (!rGroup->recordId)
1514 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1515 rGroup->recordId = res_get (common_resource, ext_res);
1518 /* determine database name */
1519 if (!rGroup->databaseName)
1521 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1522 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1524 sprintf (ext_res, "%sdatabase", gprefix);
1525 rGroup->databaseName = res_get (common_resource, ext_res);
1528 if (!rGroup->databaseName)
1529 rGroup->databaseName = "Default";
1531 /* determine if explain database */
1533 sprintf (ext_res, "%sexplainDatabase", gprefix);
1534 rGroup->explainDatabase =
1535 atoi (res_get_def (common_resource, ext_res, "0"));
1537 /* announce database */
1538 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1540 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1541 rGroup->explainDatabase))
1545 if (rGroup->flagStoreData == -1)
1548 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1549 if (!(sval = res_get (common_resource, ext_res)))
1551 sprintf (ext_res, "%sstoreData", gprefix);
1552 sval = res_get (common_resource, ext_res);
1555 rGroup->flagStoreData = atoi (sval);
1557 if (rGroup->flagStoreData == -1)
1558 rGroup->flagStoreData = 0;
1560 if (rGroup->flagStoreKeys == -1)
1564 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1565 if (!(sval = res_get (common_resource, ext_res)))
1567 sprintf (ext_res, "%sstoreKeys", gprefix);
1568 sval = res_get (common_resource, ext_res);
1571 rGroup->flagStoreKeys = atoi (sval);
1573 if (rGroup->flagStoreKeys == -1)
1574 rGroup->flagStoreKeys = 0;
1576 if (sysno && deleteFlag)
1580 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1582 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1586 fi = file_read_start (fd);
1590 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1592 } while (r && !sysno && fi->file_more);
1593 file_read_stop (fi);
1599 static int explain_extract (void *handle, Record rec, data1_node *n)
1601 struct recordGroup *rGroup = (struct recordGroup*) handle;
1602 struct recExtractCtrl extractCtrl;
1605 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1607 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1611 reckeys.buf_used = 0;
1612 reckeys.prevAttrUse = -1;
1613 reckeys.prevAttrSet = -1;
1614 reckeys.prevSeqNo = 0;
1616 extractCtrl.init = wordInit;
1617 extractCtrl.addWord = addRecordKey;
1618 extractCtrl.addSchema = addSchema;
1619 extractCtrl.dh = rGroup->dh;
1620 for (i = 0; i<256; i++)
1621 extractCtrl.seqno[i] = 0;
1622 extractCtrl.zebra_maps = rGroup->zebra_maps;
1623 extractCtrl.flagShowRecords = !rGroup->flagRw;
1625 grs_extract_tree(&extractCtrl, n);
1627 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1629 if (rec->size[recInfo_delKeys])
1631 struct recKeys delkeys;
1633 delkeys.buf_used = rec->size[recInfo_delKeys];
1634 delkeys.buf = rec->info[recInfo_delKeys];
1635 flushSortKeys (rec->sysno, 0);
1636 flushRecordKeys (rec->sysno, 0, &delkeys);
1638 flushRecordKeys (rec->sysno, 1, &reckeys);
1639 flushSortKeys (rec->sysno, 1);
1641 xfree (rec->info[recInfo_delKeys]);
1642 rec->size[recInfo_delKeys] = reckeys.buf_used;
1643 rec->info[recInfo_delKeys] = reckeys.buf;
1645 reckeys.buf_max = 0;