2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.81 1998-03-11 11:19:04 adam
8 * Changed the way sequence numbers are generated.
10 * Revision 1.80 1998/03/05 08:45:11 adam
11 * New result set model and modular ranking system. Moved towards
12 * descent server API. System information stored as "SGML" records.
14 * Revision 1.79 1998/02/17 10:32:52 adam
15 * Fixed bug: binary files weren't opened with flag b on NT.
17 * Revision 1.78 1998/02/10 12:03:05 adam
20 * Revision 1.77 1998/01/12 15:04:08 adam
21 * The test option (-s) only uses read-lock (and not write lock).
23 * Revision 1.76 1997/10/27 14:33:04 adam
24 * Moved towards generic character mapping depending on "structure"
25 * field in abstract syntax file. Fixed a few memory leaks. Fixed
26 * bug with negative integers when doing searches with relational
29 * Revision 1.75 1997/09/17 12:19:12 adam
30 * Zebra version corresponds to YAZ version 1.4.
31 * Changed Zebra server so that it doesn't depend on global common_resource.
33 * Revision 1.74 1997/09/09 13:38:06 adam
34 * Partial port to WIN95/NT.
36 * Revision 1.73 1997/09/04 13:57:20 adam
37 * New file extract/retrieve method tellf (added).
38 * Added O_BINARY for open calls.
40 * Revision 1.72 1997/07/15 16:32:29 adam
41 * Bug fix: Match handler didn't terminate the resulting string!
43 * Revision 1.71 1997/07/15 16:28:41 adam
44 * Bug fix: storeData didn't work with files with multiple records.
45 * Bug fix: fixed memory management with records; not really well
48 * Revision 1.70 1997/07/01 13:00:42 adam
49 * Bug fix in routine searchRecordKey: uninitialized variables.
51 * Revision 1.69 1997/04/29 09:26:03 adam
52 * Bug fix: generic recordId handling didn't work for compressed internal
55 * Revision 1.68 1997/02/12 20:39:45 adam
56 * Implemented options -f <n> that limits the log to the first <n>
58 * Changed some log messages also.
60 * Revision 1.67 1996/11/15 15:02:14 adam
61 * Minor changes regarding logging.
63 * Revision 1.66 1996/11/14 09:52:21 adam
64 * Strings in record keys bound by IT_MAX_WORD.
66 * Revision 1.65 1996/11/14 08:57:56 adam
67 * Reduction of storeKeys area.
69 * Revision 1.64 1996/11/08 11:10:16 adam
70 * Buffers used during file match got bigger.
71 * Compressed ISAM support everywhere.
72 * Bug fixes regarding masking characters in queries.
73 * Redesigned Regexp-2 queries.
75 * Revision 1.63 1996/10/29 14:09:39 adam
76 * Use of cisam system - enabled if setting isamc is 1.
78 * Revision 1.62 1996/10/11 10:57:01 adam
79 * New module recctrl. Used to manage records (extract/retrieval).
80 * Several files have been moved to the recctrl sub directory.
82 * Revision 1.61 1996/06/06 12:08:37 quinn
83 * Added showRecord function
85 * Revision 1.60 1996/06/04 10:18:12 adam
86 * Search/scan uses character mapping module.
88 * Revision 1.59 1996/05/14 15:47:07 adam
89 * Cleanup of various buffer size entities.
91 * Revision 1.58 1996/05/14 06:16:38 adam
92 * Compact use/set bytes used in search service.
94 * Revision 1.57 1996/05/13 14:23:04 adam
95 * Work on compaction of set/use bytes in dictionary.
97 * Revision 1.56 1996/05/09 09:54:42 adam
98 * Server supports maps from one logical attributes to a list of physical
100 * The extraction process doesn't make space consuming 'any' keys.
102 * Revision 1.55 1996/05/09 07:28:55 quinn
103 * Work towards phrases and multiple registers
105 * Revision 1.54 1996/05/01 13:46:35 adam
106 * First work on multiple records in one file.
107 * New option, -offset, to the "unread" command in the filter module.
109 * Revision 1.53 1996/04/26 12:09:43 adam
110 * Added a few comments.
112 * Revision 1.52 1996/04/25 13:27:57 adam
113 * Function recordExtract modified so that files with no keys (possibly empty)
116 * Revision 1.51 1996/03/19 11:08:42 adam
117 * Bug fix: Log preamble wasn't always turned off after recordExtract.
119 * Revision 1.50 1996/02/12 18:45:36 adam
120 * New fileVerboseFlag in record group control.
122 * Revision 1.49 1996/02/05 12:29:57 adam
123 * Logging reduced a bit.
124 * The remaining running time is estimated during register merge.
126 * Revision 1.48 1996/02/01 20:53:26 adam
127 * The temporary per-record keys are compacted a little, and duplication
128 * of the per-records keys are avoided when they are saved in the record
129 * information buffer.
131 * Revision 1.47 1996/01/17 14:57:48 adam
132 * Prototype changed for reader functions in extract/retrieve. File
133 * is identified by 'void *' instead of 'int.
135 * Revision 1.46 1995/12/15 14:57:16 adam
138 * Revision 1.45 1995/12/15 12:37:41 adam
139 * In addRecordKeyAny: Writes key only when attrSet != -1.
141 * Revision 1.44 1995/12/12 16:00:54 adam
142 * System call sync(2) used after update/commit.
143 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
146 * Revision 1.43 1995/12/11 09:12:46 adam
147 * The rec_get function returns NULL if record doesn't exist - will
148 * happen in the server if the result set records have been deleted since
149 * the creation of the set (i.e. the search).
150 * The server saves a result temporarily if it is 'volatile', i.e. the
151 * set is register dependent.
153 * Revision 1.42 1995/12/07 17:38:46 adam
154 * Work locking mechanisms for concurrent updates/commit.
156 * Revision 1.41 1995/12/06 16:06:42 adam
157 * Better diagnostics. Work on 'real' dictionary deletion.
159 * Revision 1.40 1995/12/05 16:57:40 adam
160 * More work on regular patterns.
162 * Revision 1.39 1995/12/05 13:20:18 adam
163 * Bug fix: file_read sometimes returned early EOF.
165 * Revision 1.38 1995/12/04 17:59:21 adam
166 * More work on regular expression conversion.
168 * Revision 1.37 1995/12/04 14:22:27 adam
169 * Extra arg to recType_byName.
170 * Started work on new regular expression parsed input to
171 * structured records.
173 * Revision 1.36 1995/11/30 08:34:29 adam
174 * Started work on commit facility.
175 * Changed a few malloc/free to xmalloc/xfree.
177 * Revision 1.35 1995/11/28 14:26:21 adam
178 * Bug fix: recordId with constant wasn't right.
179 * Bug fix: recordId dictionary entry wasn't deleted when needed.
181 * Revision 1.34 1995/11/28 09:09:38 adam
182 * Zebra config renamed.
183 * Use setting 'recordId' to identify record now.
184 * Bug fix in recindex.c: rec_release_blocks was invokeded even
185 * though the blocks were already released.
186 * File traversal properly deletes records when needed.
188 * Revision 1.33 1995/11/27 09:56:20 adam
189 * Record info elements better enumerated. Internal store of records.
191 * Revision 1.32 1995/11/25 10:24:05 adam
192 * More record fields - they are enumerated now.
193 * New options: flagStoreData flagStoreKey.
195 * Revision 1.31 1995/11/24 11:31:35 adam
196 * Commands add & del read filenames from stdin if source directory is
198 * Match criteria supports 'constant' strings.
200 * Revision 1.30 1995/11/22 17:19:16 adam
201 * Record management uses the bfile system.
203 * Revision 1.29 1995/11/21 15:01:14 adam
204 * New general match criteria implemented.
205 * New feature: document groups.
207 * Revision 1.28 1995/11/21 09:20:30 adam
208 * Yet more work on record match.
210 * Revision 1.27 1995/11/20 16:59:45 adam
211 * New update method: the 'old' keys are saved for each records.
213 * Revision 1.26 1995/11/20 11:56:24 adam
214 * Work on new traversal.
216 * Revision 1.25 1995/11/16 15:34:54 adam
217 * Uses new record management system in both indexer and server.
219 * Revision 1.24 1995/11/15 19:13:08 adam
220 * Work on record management.
222 * Revision 1.23 1995/10/27 14:00:10 adam
223 * Implemented detection of database availability.
225 * Revision 1.22 1995/10/17 18:02:07 adam
226 * New feature: databases. Implemented as prefix to words in dictionary.
228 * Revision 1.21 1995/10/10 12:24:38 adam
229 * Temporary sort files are compressed.
231 * Revision 1.20 1995/10/06 13:52:05 adam
232 * Bug fixes. Handler may abort further scanning.
234 * Revision 1.19 1995/10/04 12:55:16 adam
235 * Bug fix in ranked search. Use=Any keys inserted.
237 * Revision 1.18 1995/10/04 09:37:08 quinn
240 * Revision 1.17 1995/10/03 14:28:57 adam
241 * Buffered read in extract works.
243 * Revision 1.16 1995/10/03 14:28:45 adam
244 * Work on more effecient read handler in extract.
246 * Revision 1.15 1995/10/02 15:42:53 adam
247 * Extract uses file descriptors instead of FILE pointers.
249 * Revision 1.14 1995/10/02 15:29:13 adam
250 * More logging in file_extract.
252 * Revision 1.13 1995/09/29 14:01:39 adam
255 * Revision 1.12 1995/09/28 14:22:56 adam
256 * Sort uses smaller temporary files.
258 * Revision 1.11 1995/09/28 12:10:31 adam
259 * Bug fixes. Field prefix used in queries.
261 * Revision 1.10 1995/09/28 09:19:41 adam
262 * xfree/xmalloc used everywhere.
263 * Extract/retrieve method seems to work for text records.
265 * Revision 1.9 1995/09/27 12:22:28 adam
266 * More work on extract in record control.
267 * Field name is not in isam keys but in prefix in dictionary words.
269 * Revision 1.8 1995/09/14 07:48:22 adam
270 * Record control management.
272 * Revision 1.7 1995/09/11 13:09:32 adam
273 * More work on relevance feedback.
275 * Revision 1.6 1995/09/08 14:52:27 adam
276 * Minor changes. Dictionary is lower case now.
278 * Revision 1.5 1995/09/06 16:11:16 adam
279 * Option: only one word key per file.
281 * Revision 1.4 1995/09/05 15:28:39 adam
282 * More work on search engine.
284 * Revision 1.3 1995/09/04 12:33:41 adam
285 * Various cleanup. YAZ util used instead.
287 * Revision 1.2 1995/09/04 09:10:34 adam
288 * More work on index add/del/update.
289 * Merge sort implemented.
290 * Initial work on z39 server.
292 * Revision 1.1 1995/09/01 14:06:35 adam
293 * Split of work into more files.
312 static Dict matchDict;
314 static Records records = NULL;
315 static SortIdx sortIdx = NULL;
317 static char **key_buf;
318 static size_t ptr_top;
320 static size_t key_buf_used;
321 static int key_file_no;
323 static int records_inserted = 0;
324 static int records_updated = 0;
325 static int records_deleted = 0;
326 static int records_processed = 0;
328 static ZebraExplainInfo zti = NULL;
330 static void logRecord (int showFlag)
334 if (showFlag || !(records_processed % 1000))
336 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
337 records_processed, records_inserted, records_updated,
342 int key_open (BFiles bfs, int mem, int rw, data1_handle dh)
345 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
348 key_buf = xmalloc (mem);
349 ptr_top = mem/sizeof(char*);
355 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
357 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
361 records = rec_open (bfs, rw);
364 dict_close (matchDict);
367 zti = zebraExplain_open (records, dh, rw);
370 rec_close (&records);
371 dict_close (matchDict);
374 sortIdx = sortIdx_open (bfs, 1);
384 void encode_key_init (struct encode_info *i)
390 char *encode_key_int (int d, char *bp)
399 else if (d <= 4194303)
401 *bp++ = 128 + (d>>16);
402 *bp++ = (d>>8) & 255;
407 *bp++ = 192 + (d>>24);
408 *bp++ = (d>>16) & 255;
409 *bp++ = (d>>8) & 255;
415 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
420 while ((*bp++ = *k++))
422 memcpy (&key, k+1, sizeof(struct it_key));
423 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
424 if (i->sysno != key.sysno)
426 i->sysno = key.sysno;
429 bp = encode_key_int (key.seqno - i->seqno, bp);
430 i->seqno = key.seqno;
431 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
433 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
441 static int key_y_len;
443 static int key_y_compare (const void *p1, const void *p2)
447 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
448 *(char**) p2 + key_y_len + 1)))
450 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
453 static int key_x_compare (const void *p1, const void *p2)
455 return strcmp (*(char**) p1, *(char**) p2);
459 void key_flush (void)
464 struct encode_info encode_info;
473 logf (LOG_LOG, "sorting section %d", key_file_no);
475 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
476 getFnameTmp (out_fname, key_file_no);
478 if (!(outf = fopen (out_fname, "wb")))
480 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
483 logf (LOG_LOG, "writing section %d", key_file_no);
484 prevcp = cp = key_buf[ptr_top-ptr_i];
486 encode_key_init (&encode_info);
487 encode_key_write (cp, &encode_info, outf);
490 cp = key_buf[ptr_top-ptr_i];
491 if (strcmp (cp, prevcp))
493 encode_key_init (&encode_info);
494 encode_key_write (cp, &encode_info, outf);
498 encode_key_write (cp + strlen(cp), &encode_info, outf);
501 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
502 getFnameTmp (out_fname, key_file_no);
504 if (!(outf = fopen (out_fname, "wb")))
506 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
509 logf (LOG_LOG, "writing section %d", key_file_no);
511 prevcp = key_buf[ptr_top-i];
513 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
515 key_y_len = strlen(prevcp)+1;
517 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
518 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
520 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
521 sizeof(char*), key_y_compare);
522 cp = key_buf[ptr_top-ptr_i];
524 encode_key_init (&encode_info);
525 encode_key_write (cp, &encode_info, outf);
528 cp = key_buf[ptr_top-ptr_i];
529 encode_key_write (cp+key_y_len, &encode_info, outf);
533 prevcp = key_buf[ptr_top-ptr_i];
538 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
541 logf (LOG_LOG, "finished section %d", key_file_no);
546 int key_close (int rw)
551 zebraExplain_runNumberIncrement (zti, 1);
552 zebraExplain_close (zti, rw);
553 rec_close (&records);
554 dict_close (matchDict);
555 sortIdx_close (sortIdx);
561 static void wordInit (struct recExtractCtrl *p, RecWord *w)
563 w->zebra_maps = p->zebra_maps;
564 w->seqnos = p->seqno;
570 static struct sortKey {
575 struct sortKey *next;
578 static struct recKeys {
587 static void addIndexString (RecWord *p, const char *string, int length)
594 int *pseqno = &p->seqnos[p->reg_type];
596 if (reckeys.buf_used+1024 > reckeys.buf_max)
600 b = xmalloc (reckeys.buf_max += 128000);
601 if (reckeys.buf_used > 0)
602 memcpy (b, reckeys.buf, reckeys.buf_used);
606 dst = reckeys.buf + reckeys.buf_used;
608 attrSet = p->attrSet;
609 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
612 reckeys.prevAttrSet = attrSet;
613 attrUse = p->attrUse;
614 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
617 reckeys.prevAttrUse = attrUse;
619 diff = 1 + *pseqno - reckeys.prevSeqNo;
620 if (diff >= 1 && diff <= 15)
625 reckeys.prevSeqNo = *pseqno;
631 memcpy (dst, &attrSet, sizeof(attrSet));
632 dst += sizeof(attrSet);
636 memcpy (dst, &attrUse, sizeof(attrUse));
637 dst += sizeof(attrUse);
639 *dst++ = p->reg_type;
640 memcpy (dst, string, length);
646 memcpy (dst, pseqno, sizeof(*pseqno));
647 dst += sizeof(*pseqno);
649 reckeys.buf_used = dst - reckeys.buf;
653 static void addSortString (RecWord *p, const char *string, int length)
657 for (sk = sortKeys; sk; sk = sk->next)
658 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
661 sk = xmalloc (sizeof(*sk));
665 sk->string = xmalloc (p->length);
666 sk->length = p->length;
667 memcpy (sk->string, p->string, p->length);
668 sk->attrSet = p->attrSet;
669 sk->attrUse = p->attrUse;
672 static void addString (RecWord *p, const char *string, int length)
675 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
676 addSortString (p, string, length);
678 addIndexString (p, string, length);
681 static void addIncompleteField (RecWord *p)
683 const char *b = p->string;
684 int remain = p->length;
685 const char **map = 0;
688 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
692 char buf[IT_MAX_WORD+1];
696 while (map && *map && **map == *CHR_SPACE)
698 remain = p->length - (b - p->string);
700 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
707 while (map && *map && **map != *CHR_SPACE)
709 const char *cp = *map;
711 while (i < IT_MAX_WORD && *cp)
713 remain = p->length - (b - p->string);
715 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
721 addString (p, buf, i);
725 static void addCompleteField (RecWord *p)
727 const char *b = p->string;
728 char buf[IT_MAX_WORD+1];
729 const char **map = 0;
730 int i = 0, remain = p->length;
733 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
735 while (remain > 0 && i < IT_MAX_WORD)
737 while (map && *map && **map == *CHR_SPACE)
739 remain = p->length - (b - p->string);
741 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
748 if (i && i < IT_MAX_WORD)
749 buf[i++] = *CHR_SPACE;
750 while (map && *map && **map != *CHR_SPACE)
752 const char *cp = *map;
754 if (i >= IT_MAX_WORD)
756 while (i < IT_MAX_WORD && *cp)
758 remain = p->length - (b - p->string);
760 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
768 addString (p, buf, i);
771 static void addRecordKey (RecWord *p)
773 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
774 addCompleteField (p);
776 addIncompleteField(p);
779 static void flushSortKeys (SYSNO sysno, int cmd)
781 struct sortKey *sk = sortKeys;
783 sortIdx_sysno (sortIdx, sysno);
786 struct sortKey *sk_next = sk->next;
787 sortIdx_type (sortIdx, sk->attrUse);
788 sortIdx_add (sortIdx, sk->string, sk->length);
796 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
797 const char *databaseName)
804 if (zebraExplain_curDatabase (zti, databaseName))
806 if (zebraExplain_newDatabase (zti, databaseName))
809 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
810 while (off < reckeys->buf_used)
812 const char *src = reckeys->buf + off;
820 memcpy (&attrSet, src, sizeof(attrSet));
821 src += sizeof(attrSet);
825 memcpy (&attrUse, src, sizeof(attrUse));
826 src += sizeof(attrUse);
828 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
831 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
833 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
835 ch = zebraExplain_addSU (zti, attrSet, attrUse);
837 ((char*) key_buf) [key_buf_used++] = ch;
839 ((char*)key_buf) [key_buf_used++] = *src++;
841 ((char*)key_buf) [key_buf_used++] = '\0';
842 ((char*) key_buf)[key_buf_used++] = cmd;
845 seqno += ((lead>>2) & 15)-1;
848 memcpy (&seqno, src, sizeof(seqno));
849 src += sizeof(seqno);
853 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
854 key_buf_used += sizeof(key);
855 off = src - reckeys->buf;
857 assert (off == reckeys->buf_used);
860 static const char **searchRecordKey (struct recKeys *reckeys,
861 int attrSetS, int attrUseS)
863 static const char *ws[32];
871 for (i = 0; i<32; i++)
874 while (off < reckeys->buf_used)
877 const char *src = reckeys->buf + off;
885 memcpy (&attrSet, src, sizeof(attrSet));
886 src += sizeof(attrSet);
890 memcpy (&attrUse, src, sizeof(attrUse));
891 src += sizeof(attrUse);
897 seqno += ((lead>>2) & 15)-1;
900 memcpy (&seqno, src, sizeof(seqno));
901 src += sizeof(seqno);
903 if (attrUseS == attrUse && attrSetS == attrSet)
910 woff = seqno - startSeq;
911 if (woff >= 0 && woff < 31)
915 off = src - reckeys->buf;
917 assert (off == reckeys->buf_used);
921 struct file_read_info {
929 static struct file_read_info *file_read_start (int fd)
931 struct file_read_info *fi = xmalloc (sizeof(*fi));
935 fi->file_moffset = 0;
939 static void file_read_stop (struct file_read_info *fi)
945 static off_t file_seek (void *handle, off_t offset)
947 struct file_read_info *p = handle;
948 p->file_offset = offset;
949 return lseek (p->fd, offset, SEEK_SET);
952 static off_t file_tell (void *handle)
954 struct file_read_info *p = handle;
955 return p->file_offset;
958 static int file_read (void *handle, char *buf, size_t count)
960 struct file_read_info *p = handle;
963 r = read (fd, buf, count);
967 if (p->file_offset > p->file_max)
968 p->file_max = p->file_offset;
973 static void file_begin (void *handle)
975 struct file_read_info *p = handle;
977 p->file_offset = p->file_moffset;
979 lseek (p->fd, p->file_moffset, SEEK_SET);
983 static void file_end (void *handle, off_t offset)
985 struct file_read_info *p = handle;
987 assert (p->file_more == 0);
989 p->file_moffset = offset;
992 static int atois (const char **s)
995 while ( (c=**s) >= '0' && c <= '9')
997 val = val*10 + c - '0';
1003 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1007 static char dstBuf[2048];
1009 const char *s = spec;
1010 static const char **w;
1015 while (*s == ' ' || *s == '\t')
1022 int attrSet, attrUse;
1026 attrSet = atois (&s);
1029 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
1030 spec, rGroup->groupName ? rGroup->groupName : "none");
1034 attrUse = atois (&s);
1035 w = searchRecordKey (reckeys, attrSet, attrUse);
1040 for (i = 0; i<32; i++)
1045 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1046 spec, rGroup->groupName ? rGroup->groupName : "none");
1051 for (i = 0; i<32; i++)
1052 if (matchFlag[i] && w[i])
1060 dst += strlen(w[i]);
1064 logf (LOG_WARN, "Record didn't contain match"
1065 " fields in (%d,%d)", attrSet, attrUse);
1073 const char *spec_src = NULL;
1074 const char *s1 = ++s;
1075 while (*s1 && *s1 != ' ' && *s1 != '\t')
1081 memcpy (special, s, spec_len);
1082 special[spec_len] = '\0';
1085 if (!strcmp (special, "group"))
1086 spec_src = rGroup->groupName;
1087 else if (!strcmp (special, "database"))
1088 spec_src = rGroup->databaseName;
1089 else if (!strcmp (special, "filename"))
1091 else if (!strcmp (special, "type"))
1092 spec_src = rGroup->recordType;
1097 strcpy (dst, spec_src);
1098 dst += strlen (spec_src);
1101 else if (*s == '\"' || *s == '\'')
1103 int stopMarker = *s++;
1107 while (*s && *s != stopMarker)
1110 tmpString[i++] = *s++;
1114 tmpString[i] = '\0';
1115 strcpy (dst, tmpString);
1116 dst += strlen (tmpString);
1120 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1121 spec, rGroup->groupName ? rGroup->groupName : "none");
1128 logf (LOG_WARN, "No match criteria for record %s in group %s",
1129 fname, rGroup->groupName ? rGroup->groupName : "none");
1136 struct recordLogInfo {
1139 struct recordGroup *rGroup;
1142 static void recordLogPreamble (int level, const char *msg, void *info)
1144 struct recordLogInfo *p = info;
1145 FILE *outf = log_file ();
1147 if (level & LOG_LOG)
1149 fprintf (outf, "File %s, offset %d, type %s\n",
1150 p->rGroup->recordType, p->recordOffset, p->fname);
1151 log_event_start (NULL, NULL);
1154 static int recordExtract (SYSNO *sysno, const char *fname,
1155 struct recordGroup *rGroup, int deleteFlag,
1156 struct file_read_info *fi, RecType recType,
1159 struct recExtractCtrl extractCtrl;
1160 RecordAttr *recordAttr;
1164 off_t recordOffset = 0;
1166 struct recordLogInfo logInfo;
1171 /* we are going to read from a file, so prepare the extraction */
1172 extractCtrl.fh = fi;
1173 extractCtrl.subType = subType;
1174 extractCtrl.init = wordInit;
1175 extractCtrl.add = addRecordKey;
1176 extractCtrl.dh = rGroup->dh;
1178 reckeys.buf_used = 0;
1179 reckeys.prevAttrUse = -1;
1180 reckeys.prevAttrSet = -1;
1181 reckeys.prevSeqNo = 0;
1183 for (i = 0; i<256; i++)
1184 extractCtrl.seqno[i] = 0;
1185 recordOffset = fi->file_moffset;
1186 extractCtrl.offset = recordOffset;
1187 extractCtrl.readf = file_read;
1188 extractCtrl.seekf = file_seek;
1189 extractCtrl.tellf = file_tell;
1190 extractCtrl.endf = file_end;
1191 extractCtrl.zebra_maps = rGroup->zebra_maps;
1192 extractCtrl.flagShowRecords = !rGroup->flagRw;
1193 if (!rGroup->flagRw)
1194 printf ("File: %s %ld\n", fname, (long) recordOffset);
1196 logInfo.fname = fname;
1197 logInfo.recordOffset = recordOffset;
1198 logInfo.rGroup = rGroup;
1199 log_event_start (recordLogPreamble, &logInfo);
1201 r = (*recType->extract)(&extractCtrl);
1203 log_event_start (NULL, NULL);
1207 /* error occured during extraction ... */
1208 if (rGroup->flagRw &&
1209 records_processed < rGroup->fileVerboseLimit)
1211 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1212 fname, (long) recordOffset, r);
1216 if (reckeys.buf_used == 0)
1218 /* the extraction process returned no information - the record
1219 is probably empty - unless flagShowRecords is in use */
1220 if (!rGroup->flagRw)
1222 logf (LOG_WARN, "No keys generated for file %s", fname);
1223 logf (LOG_WARN, " The file is probably empty");
1228 /* perform match if sysno not known and if match criteria is specified */
1235 if (rGroup->recordId && *rGroup->recordId)
1239 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1243 rinfo = dict_lookup (matchDict, matchStr);
1245 memcpy (sysno, rinfo+1, sizeof(*sysno));
1249 logf (LOG_WARN, "Bad match criteria");
1260 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1261 fname, (long) recordOffset);
1262 logf (LOG_WARN, "cannot delete record above (seems new)");
1265 if (records_processed < rGroup->fileVerboseLimit)
1266 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1267 fname, (long) recordOffset);
1268 rec = rec_new (records);
1270 *sysno = rec->sysno;
1272 recordAttr = rec_init_attr (zti, rec);
1276 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1278 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1279 flushSortKeys (*sysno, 1);
1285 /* record already exists */
1286 struct recKeys delkeys;
1288 rec = rec_get (records, *sysno);
1291 recordAttr = rec_init_attr (zti, rec);
1293 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1295 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1296 fname, (long) recordOffset);
1301 delkeys.buf_used = rec->size[recInfo_delKeys];
1302 delkeys.buf = rec->info[recInfo_delKeys];
1303 flushSortKeys (*sysno, 0);
1304 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1307 /* record going to be deleted */
1308 if (!delkeys.buf_used)
1310 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1311 fname, (long) recordOffset);
1312 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1316 if (records_processed < rGroup->fileVerboseLimit)
1317 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1318 fname, (long) recordOffset);
1321 dict_delete (matchDict, matchStr);
1322 rec_del (records, &rec);
1330 /* record going to be updated */
1331 if (!delkeys.buf_used)
1333 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1334 fname, (long) recordOffset);
1335 logf (LOG_WARN, "cannot update file above, storeKeys false");
1339 if (records_processed < rGroup->fileVerboseLimit)
1340 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1341 fname, (long) recordOffset);
1342 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1347 /* update file type */
1348 xfree (rec->info[recInfo_fileType]);
1349 rec->info[recInfo_fileType] =
1350 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1352 /* update filename */
1353 xfree (rec->info[recInfo_filename]);
1354 rec->info[recInfo_filename] =
1355 rec_strdup (fname, &rec->size[recInfo_filename]);
1357 /* update delete keys */
1358 xfree (rec->info[recInfo_delKeys]);
1359 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1362 rec->size[recInfo_delKeys] = reckeys.buf_used;
1363 rec->info[recInfo_delKeys] = reckeys.buf;
1365 reckeys.buf_max = 0;
1367 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1368 rec->size[recInfo_delKeys] = reckeys.buf_used;
1369 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1370 rec->size[recInfo_delKeys]);
1375 rec->info[recInfo_delKeys] = NULL;
1376 rec->size[recInfo_delKeys] = 0;
1379 /* save file size of original record */
1380 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1381 recordAttr->recordSize = fi->file_moffset - recordOffset;
1382 if (!recordAttr->recordSize)
1383 recordAttr->recordSize = fi->file_max - recordOffset;
1384 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1386 /* set run-number for this record */
1387 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1389 /* update store data */
1390 xfree (rec->info[recInfo_storeData]);
1391 if (rGroup->flagStoreData == 1)
1393 rec->size[recInfo_storeData] = recordAttr->recordSize;
1394 rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
1395 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1397 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1398 (long) recordOffset);
1401 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1402 < recordAttr->recordSize)
1404 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1405 recordAttr->recordSize, fname);
1411 rec->info[recInfo_storeData] = NULL;
1412 rec->size[recInfo_storeData] = 0;
1414 /* update database name */
1415 xfree (rec->info[recInfo_databaseName]);
1416 rec->info[recInfo_databaseName] =
1417 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1420 recordAttr->recordOffset = recordOffset;
1422 /* commit this record */
1423 rec_put (records, &rec);
1428 int fileExtract (SYSNO *sysno, const char *fname,
1429 const struct recordGroup *rGroupP, int deleteFlag)
1437 struct recordGroup rGroupM;
1438 struct recordGroup *rGroup = &rGroupM;
1439 struct file_read_info *fi;
1441 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1443 if (!rGroup->groupName || !*rGroup->groupName)
1446 sprintf (gprefix, "%s.", rGroup->groupName);
1448 logf (LOG_DEBUG, "fileExtract %s", fname);
1450 /* determine file extension */
1451 for (i = strlen(fname); --i >= 0; )
1452 if (fname[i] == '/')
1457 else if (fname[i] == '.')
1459 strcpy (ext, fname+i+1);
1462 /* determine file type - depending on extension */
1463 if (!rGroup->recordType)
1465 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1466 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1468 sprintf (ext_res, "%srecordType", gprefix);
1469 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1471 if (records_processed < rGroup->fileVerboseLimit)
1472 logf (LOG_LOG, "? %s", fname);
1477 if (!rGroup->recordType)
1479 if (records_processed < rGroup->fileVerboseLimit)
1480 logf (LOG_LOG, "? record %s", fname);
1483 if (!(recType = recType_byName (rGroup->recordType, subType)))
1485 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1489 /* determine match criteria */
1490 if (!rGroup->recordId)
1492 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1493 rGroup->recordId = res_get (common_resource, ext_res);
1496 /* determine database name */
1497 if (!rGroup->databaseName)
1499 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1500 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1502 sprintf (ext_res, "%sdatabase", gprefix);
1503 rGroup->databaseName = res_get (common_resource, ext_res);
1506 if (!rGroup->databaseName)
1507 rGroup->databaseName = "Default";
1509 if (rGroup->flagStoreData == -1)
1512 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1513 if (!(sval = res_get (common_resource, ext_res)))
1515 sprintf (ext_res, "%sstoreData", gprefix);
1516 sval = res_get (common_resource, ext_res);
1519 rGroup->flagStoreData = atoi (sval);
1521 if (rGroup->flagStoreData == -1)
1522 rGroup->flagStoreData = 0;
1524 if (rGroup->flagStoreKeys == -1)
1528 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1529 if (!(sval = res_get (common_resource, ext_res)))
1531 sprintf (ext_res, "%sstoreKeys", gprefix);
1532 sval = res_get (common_resource, ext_res);
1535 rGroup->flagStoreKeys = atoi (sval);
1537 if (rGroup->flagStoreKeys == -1)
1538 rGroup->flagStoreKeys = 0;
1540 if (sysno && deleteFlag)
1544 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1546 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1550 fi = file_read_start (fd);
1554 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1556 } while (r && !sysno && fi->file_more);
1557 file_read_stop (fi);