2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.91 1999-02-12 13:29:22 adam
8 * Implemented position-flag for registers.
10 * Revision 1.90 1999/02/02 14:50:52 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.89 1998/10/28 10:54:38 adam
16 * Revision 1.88 1998/10/16 08:14:29 adam
17 * Updated record control system.
19 * Revision 1.87 1998/10/15 13:10:33 adam
20 * Fixed bug in Zebra that caused it to stop indexing when empty
23 * Revision 1.86 1998/10/13 20:33:53 adam
24 * Fixed one log message and change use ordinal to be an unsigned char.
26 * Revision 1.85 1998/09/22 10:03:41 adam
27 * Changed result sets to be persistent in the sense that they can
28 * be re-searched if needed.
29 * Fixed memory leak in rsm_or.
31 * Revision 1.84 1998/06/11 15:42:22 adam
32 * Changed the way use attributes are specified in the recordId
35 * Revision 1.83 1998/06/08 14:43:10 adam
36 * Added suport for EXPLAIN Proxy servers - added settings databasePath
37 * and explainDatabase to facilitate this. Increased maximum number
38 * of databases and attributes in one register.
40 * Revision 1.82 1998/05/20 10:12:15 adam
41 * Implemented automatic EXPLAIN database maintenance.
42 * Modified Zebra to work with ASN.1 compiled version of YAZ.
44 * Revision 1.81 1998/03/11 11:19:04 adam
45 * Changed the way sequence numbers are generated.
47 * Revision 1.80 1998/03/05 08:45:11 adam
48 * New result set model and modular ranking system. Moved towards
49 * descent server API. System information stored as "SGML" records.
51 * Revision 1.79 1998/02/17 10:32:52 adam
52 * Fixed bug: binary files weren't opened with flag b on NT.
54 * Revision 1.78 1998/02/10 12:03:05 adam
57 * Revision 1.77 1998/01/12 15:04:08 adam
58 * The test option (-s) only uses read-lock (and not write lock).
60 * Revision 1.76 1997/10/27 14:33:04 adam
61 * Moved towards generic character mapping depending on "structure"
62 * field in abstract syntax file. Fixed a few memory leaks. Fixed
63 * bug with negative integers when doing searches with relational
66 * Revision 1.75 1997/09/17 12:19:12 adam
67 * Zebra version corresponds to YAZ version 1.4.
68 * Changed Zebra server so that it doesn't depend on global common_resource.
70 * Revision 1.74 1997/09/09 13:38:06 adam
71 * Partial port to WIN95/NT.
73 * Revision 1.73 1997/09/04 13:57:20 adam
74 * New file extract/retrieve method tellf (added).
75 * Added O_BINARY for open calls.
77 * Revision 1.72 1997/07/15 16:32:29 adam
78 * Bug fix: Match handler didn't terminate the resulting string!
80 * Revision 1.71 1997/07/15 16:28:41 adam
81 * Bug fix: storeData didn't work with files with multiple records.
82 * Bug fix: fixed memory management with records; not really well
85 * Revision 1.70 1997/07/01 13:00:42 adam
86 * Bug fix in routine searchRecordKey: uninitialized variables.
88 * Revision 1.69 1997/04/29 09:26:03 adam
89 * Bug fix: generic recordId handling didn't work for compressed internal
92 * Revision 1.68 1997/02/12 20:39:45 adam
93 * Implemented options -f <n> that limits the log to the first <n>
95 * Changed some log messages also.
97 * Revision 1.67 1996/11/15 15:02:14 adam
98 * Minor changes regarding logging.
100 * Revision 1.66 1996/11/14 09:52:21 adam
101 * Strings in record keys bound by IT_MAX_WORD.
103 * Revision 1.65 1996/11/14 08:57:56 adam
104 * Reduction of storeKeys area.
106 * Revision 1.64 1996/11/08 11:10:16 adam
107 * Buffers used during file match got bigger.
108 * Compressed ISAM support everywhere.
109 * Bug fixes regarding masking characters in queries.
110 * Redesigned Regexp-2 queries.
112 * Revision 1.63 1996/10/29 14:09:39 adam
113 * Use of cisam system - enabled if setting isamc is 1.
115 * Revision 1.62 1996/10/11 10:57:01 adam
116 * New module recctrl. Used to manage records (extract/retrieval).
117 * Several files have been moved to the recctrl sub directory.
119 * Revision 1.61 1996/06/06 12:08:37 quinn
120 * Added showRecord function
122 * Revision 1.60 1996/06/04 10:18:12 adam
123 * Search/scan uses character mapping module.
125 * Revision 1.59 1996/05/14 15:47:07 adam
126 * Cleanup of various buffer size entities.
128 * Revision 1.58 1996/05/14 06:16:38 adam
129 * Compact use/set bytes used in search service.
131 * Revision 1.57 1996/05/13 14:23:04 adam
132 * Work on compaction of set/use bytes in dictionary.
134 * Revision 1.56 1996/05/09 09:54:42 adam
135 * Server supports maps from one logical attributes to a list of physical
137 * The extraction process doesn't make space consuming 'any' keys.
139 * Revision 1.55 1996/05/09 07:28:55 quinn
140 * Work towards phrases and multiple registers
142 * Revision 1.54 1996/05/01 13:46:35 adam
143 * First work on multiple records in one file.
144 * New option, -offset, to the "unread" command in the filter module.
146 * Revision 1.53 1996/04/26 12:09:43 adam
147 * Added a few comments.
149 * Revision 1.52 1996/04/25 13:27:57 adam
150 * Function recordExtract modified so that files with no keys (possibly empty)
153 * Revision 1.51 1996/03/19 11:08:42 adam
154 * Bug fix: Log preamble wasn't always turned off after recordExtract.
156 * Revision 1.50 1996/02/12 18:45:36 adam
157 * New fileVerboseFlag in record group control.
159 * Revision 1.49 1996/02/05 12:29:57 adam
160 * Logging reduced a bit.
161 * The remaining running time is estimated during register merge.
163 * Revision 1.48 1996/02/01 20:53:26 adam
164 * The temporary per-record keys are compacted a little, and duplication
165 * of the per-records keys are avoided when they are saved in the record
166 * information buffer.
168 * Revision 1.47 1996/01/17 14:57:48 adam
169 * Prototype changed for reader functions in extract/retrieve. File
170 * is identified by 'void *' instead of 'int.
172 * Revision 1.46 1995/12/15 14:57:16 adam
175 * Revision 1.45 1995/12/15 12:37:41 adam
176 * In addRecordKeyAny: Writes key only when attrSet != -1.
178 * Revision 1.44 1995/12/12 16:00:54 adam
179 * System call sync(2) used after update/commit.
180 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
183 * Revision 1.43 1995/12/11 09:12:46 adam
184 * The rec_get function returns NULL if record doesn't exist - will
185 * happen in the server if the result set records have been deleted since
186 * the creation of the set (i.e. the search).
187 * The server saves a result temporarily if it is 'volatile', i.e. the
188 * set is register dependent.
190 * Revision 1.42 1995/12/07 17:38:46 adam
191 * Work locking mechanisms for concurrent updates/commit.
193 * Revision 1.41 1995/12/06 16:06:42 adam
194 * Better diagnostics. Work on 'real' dictionary deletion.
196 * Revision 1.40 1995/12/05 16:57:40 adam
197 * More work on regular patterns.
199 * Revision 1.39 1995/12/05 13:20:18 adam
200 * Bug fix: file_read sometimes returned early EOF.
202 * Revision 1.38 1995/12/04 17:59:21 adam
203 * More work on regular expression conversion.
205 * Revision 1.37 1995/12/04 14:22:27 adam
206 * Extra arg to recType_byName.
207 * Started work on new regular expression parsed input to
208 * structured records.
210 * Revision 1.36 1995/11/30 08:34:29 adam
211 * Started work on commit facility.
212 * Changed a few malloc/free to xmalloc/xfree.
214 * Revision 1.35 1995/11/28 14:26:21 adam
215 * Bug fix: recordId with constant wasn't right.
216 * Bug fix: recordId dictionary entry wasn't deleted when needed.
218 * Revision 1.34 1995/11/28 09:09:38 adam
219 * Zebra config renamed.
220 * Use setting 'recordId' to identify record now.
221 * Bug fix in recindex.c: rec_release_blocks was invokeded even
222 * though the blocks were already released.
223 * File traversal properly deletes records when needed.
225 * Revision 1.33 1995/11/27 09:56:20 adam
226 * Record info elements better enumerated. Internal store of records.
228 * Revision 1.32 1995/11/25 10:24:05 adam
229 * More record fields - they are enumerated now.
230 * New options: flagStoreData flagStoreKey.
232 * Revision 1.31 1995/11/24 11:31:35 adam
233 * Commands add & del read filenames from stdin if source directory is
235 * Match criteria supports 'constant' strings.
237 * Revision 1.30 1995/11/22 17:19:16 adam
238 * Record management uses the bfile system.
240 * Revision 1.29 1995/11/21 15:01:14 adam
241 * New general match criteria implemented.
242 * New feature: document groups.
244 * Revision 1.28 1995/11/21 09:20:30 adam
245 * Yet more work on record match.
247 * Revision 1.27 1995/11/20 16:59:45 adam
248 * New update method: the 'old' keys are saved for each records.
250 * Revision 1.26 1995/11/20 11:56:24 adam
251 * Work on new traversal.
253 * Revision 1.25 1995/11/16 15:34:54 adam
254 * Uses new record management system in both indexer and server.
256 * Revision 1.24 1995/11/15 19:13:08 adam
257 * Work on record management.
259 * Revision 1.23 1995/10/27 14:00:10 adam
260 * Implemented detection of database availability.
262 * Revision 1.22 1995/10/17 18:02:07 adam
263 * New feature: databases. Implemented as prefix to words in dictionary.
265 * Revision 1.21 1995/10/10 12:24:38 adam
266 * Temporary sort files are compressed.
268 * Revision 1.20 1995/10/06 13:52:05 adam
269 * Bug fixes. Handler may abort further scanning.
271 * Revision 1.19 1995/10/04 12:55:16 adam
272 * Bug fix in ranked search. Use=Any keys inserted.
274 * Revision 1.18 1995/10/04 09:37:08 quinn
277 * Revision 1.17 1995/10/03 14:28:57 adam
278 * Buffered read in extract works.
280 * Revision 1.16 1995/10/03 14:28:45 adam
281 * Work on more effecient read handler in extract.
283 * Revision 1.15 1995/10/02 15:42:53 adam
284 * Extract uses file descriptors instead of FILE pointers.
286 * Revision 1.14 1995/10/02 15:29:13 adam
287 * More logging in file_extract.
289 * Revision 1.13 1995/09/29 14:01:39 adam
292 * Revision 1.12 1995/09/28 14:22:56 adam
293 * Sort uses smaller temporary files.
295 * Revision 1.11 1995/09/28 12:10:31 adam
296 * Bug fixes. Field prefix used in queries.
298 * Revision 1.10 1995/09/28 09:19:41 adam
299 * xfree/xmalloc used everywhere.
300 * Extract/retrieve method seems to work for text records.
302 * Revision 1.9 1995/09/27 12:22:28 adam
303 * More work on extract in record control.
304 * Field name is not in isam keys but in prefix in dictionary words.
306 * Revision 1.8 1995/09/14 07:48:22 adam
307 * Record control management.
309 * Revision 1.7 1995/09/11 13:09:32 adam
310 * More work on relevance feedback.
312 * Revision 1.6 1995/09/08 14:52:27 adam
313 * Minor changes. Dictionary is lower case now.
315 * Revision 1.5 1995/09/06 16:11:16 adam
316 * Option: only one word key per file.
318 * Revision 1.4 1995/09/05 15:28:39 adam
319 * More work on search engine.
321 * Revision 1.3 1995/09/04 12:33:41 adam
322 * Various cleanup. YAZ util used instead.
324 * Revision 1.2 1995/09/04 09:10:34 adam
325 * More work on index add/del/update.
326 * Merge sort implemented.
327 * Initial work on z39 server.
329 * Revision 1.1 1995/09/01 14:06:35 adam
330 * Split of work into more files.
354 #include "zebrasdr.h"
357 static Dict matchDict;
359 static Records records = NULL;
360 static SortIdx sortIdx = NULL;
362 static char **key_buf;
363 static size_t ptr_top;
365 static size_t key_buf_used;
366 static int key_file_no;
368 static int records_inserted = 0;
369 static int records_updated = 0;
370 static int records_deleted = 0;
371 static int records_processed = 0;
373 static ZebraExplainInfo zti = NULL;
375 static void logRecord (int showFlag)
379 if (showFlag || !(records_processed % 1000))
381 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
382 records_processed, records_inserted, records_updated,
387 static int explain_extract (void *handle, Record drec, data1_node *n);
389 int key_open (struct recordGroup *rGroup, int mem)
391 BFiles bfs = rGroup->bfs;
392 int rw = rGroup->flagRw;
393 data1_handle dh = rGroup->dh;
395 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
398 key_buf = xmalloc (mem);
399 ptr_top = mem/sizeof(char*);
405 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
407 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
411 records = rec_open (bfs, rw);
414 dict_close (matchDict);
417 zti = zebraExplain_open (records, dh, common_resource,
418 rw, rGroup, explain_extract);
421 rec_close (&records);
422 dict_close (matchDict);
425 sortIdx = sortIdx_open (bfs, 1);
436 void encode_key_init (struct encode_info *i)
443 char *encode_key_int (int d, char *bp)
452 else if (d <= 4194303)
454 *bp++ = 128 + (d>>16);
455 *bp++ = (d>>8) & 255;
460 *bp++ = 192 + (d>>24);
461 *bp++ = (d>>16) & 255;
462 *bp++ = (d>>8) & 255;
468 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
473 while ((*bp++ = *k++))
475 memcpy (&key, k+1, sizeof(struct it_key));
476 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
477 if (i->sysno != key.sysno)
479 i->sysno = key.sysno;
482 else if (!i->seqno && !key.seqno && i->cmd == *k)
484 bp = encode_key_int (key.seqno - i->seqno, bp);
485 i->seqno = key.seqno;
487 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
489 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
497 static int key_y_len;
499 static int key_y_compare (const void *p1, const void *p2)
503 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
504 *(char**) p2 + key_y_len + 1)))
506 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
509 static int key_x_compare (const void *p1, const void *p2)
511 return strcmp (*(char**) p1, *(char**) p2);
515 void key_flush (void)
520 struct encode_info encode_info;
529 logf (LOG_LOG, "sorting section %d", key_file_no);
531 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
532 getFnameTmp (out_fname, key_file_no);
534 if (!(outf = fopen (out_fname, "wb")))
536 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
539 logf (LOG_LOG, "writing section %d", key_file_no);
540 prevcp = cp = key_buf[ptr_top-ptr_i];
542 encode_key_init (&encode_info);
543 encode_key_write (cp, &encode_info, outf);
546 cp = key_buf[ptr_top-ptr_i];
547 if (strcmp (cp, prevcp))
549 encode_key_init (&encode_info);
550 encode_key_write (cp, &encode_info, outf);
554 encode_key_write (cp + strlen(cp), &encode_info, outf);
557 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
558 getFnameTmp (out_fname, key_file_no);
560 if (!(outf = fopen (out_fname, "wb")))
562 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
565 logf (LOG_LOG, "writing section %d", key_file_no);
567 prevcp = key_buf[ptr_top-i];
569 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
571 key_y_len = strlen(prevcp)+1;
573 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
574 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
576 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
577 sizeof(char*), key_y_compare);
578 cp = key_buf[ptr_top-ptr_i];
580 encode_key_init (&encode_info);
581 encode_key_write (cp, &encode_info, outf);
584 cp = key_buf[ptr_top-ptr_i];
585 encode_key_write (cp+key_y_len, &encode_info, outf);
589 prevcp = key_buf[ptr_top-ptr_i];
594 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
597 logf (LOG_LOG, "finished section %d", key_file_no);
602 int key_close (struct recordGroup *rGroup)
604 int rw = rGroup->flagRw;
606 zebraExplain_runNumberIncrement (zti, 1);
607 zebraExplain_close (zti, rw, 0);
610 rec_close (&records);
611 dict_close (matchDict);
612 sortIdx_close (sortIdx);
618 static void wordInit (struct recExtractCtrl *p, RecWord *w)
620 w->zebra_maps = p->zebra_maps;
621 w->seqnos = p->seqno;
622 w->attrSet = VAL_BIB1;
627 static struct sortKey {
632 struct sortKey *next;
635 static struct recKeys {
644 static void addIndexString (RecWord *p, const char *string, int length)
647 unsigned char attrSet;
648 unsigned short attrUse;
651 int *pseqno = &p->seqnos[p->reg_type];
653 if (reckeys.buf_used+1024 > reckeys.buf_max)
657 b = xmalloc (reckeys.buf_max += 128000);
658 if (reckeys.buf_used > 0)
659 memcpy (b, reckeys.buf, reckeys.buf_used);
663 dst = reckeys.buf + reckeys.buf_used;
665 attrSet = p->attrSet;
666 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
669 reckeys.prevAttrSet = attrSet;
670 attrUse = p->attrUse;
671 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
674 reckeys.prevAttrUse = attrUse;
676 diff = 1 + *pseqno - reckeys.prevSeqNo;
677 if (diff >= 1 && diff <= 15)
682 reckeys.prevSeqNo = *pseqno;
688 memcpy (dst, &attrSet, sizeof(attrSet));
689 dst += sizeof(attrSet);
693 memcpy (dst, &attrUse, sizeof(attrUse));
694 dst += sizeof(attrUse);
696 *dst++ = p->reg_type;
697 memcpy (dst, string, length);
703 memcpy (dst, pseqno, sizeof(*pseqno));
704 dst += sizeof(*pseqno);
706 reckeys.buf_used = dst - reckeys.buf;
711 static void addSortString (RecWord *p, const char *string, int length)
715 for (sk = sortKeys; sk; sk = sk->next)
716 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
719 sk = xmalloc (sizeof(*sk));
723 sk->string = xmalloc (length);
725 memcpy (sk->string, string, length);
727 sk->attrSet = p->attrSet;
728 sk->attrUse = p->attrUse;
731 static void addString (RecWord *p, const char *string, int length)
734 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
735 addSortString (p, string, length);
737 addIndexString (p, string, length);
740 static void addIncompleteField (RecWord *p)
742 const char *b = p->string;
743 int remain = p->length;
744 const char **map = 0;
747 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
751 char buf[IT_MAX_WORD+1];
755 while (map && *map && **map == *CHR_SPACE)
757 remain = p->length - (b - p->string);
759 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
766 while (map && *map && **map != *CHR_SPACE)
768 const char *cp = *map;
770 while (i < IT_MAX_WORD && *cp)
772 remain = p->length - (b - p->string);
774 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
780 addString (p, buf, i);
784 static void addCompleteField (RecWord *p)
786 const char *b = p->string;
787 char buf[IT_MAX_WORD+1];
788 const char **map = 0;
789 int i = 0, remain = p->length;
792 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
794 while (remain > 0 && i < IT_MAX_WORD)
796 while (map && *map && **map == *CHR_SPACE)
798 remain = p->length - (b - p->string);
800 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
807 if (i && i < IT_MAX_WORD)
808 buf[i++] = *CHR_SPACE;
809 while (map && *map && **map != *CHR_SPACE)
811 const char *cp = *map;
813 if (i >= IT_MAX_WORD)
815 while (i < IT_MAX_WORD && *cp)
817 remain = p->length - (b - p->string);
819 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
827 addString (p, buf, i);
830 static void addRecordKey (RecWord *p)
832 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
833 addCompleteField (p);
835 addIncompleteField(p);
838 static void flushSortKeys (SYSNO sysno, int cmd)
840 struct sortKey *sk = sortKeys;
842 sortIdx_sysno (sortIdx, sysno);
845 struct sortKey *sk_next = sk->next;
846 sortIdx_type (sortIdx, sk->attrUse);
847 sortIdx_add (sortIdx, sk->string, sk->length);
855 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
857 unsigned char attrSet = (unsigned char) -1;
858 unsigned short attrUse = (unsigned short) -1;
862 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
863 while (off < reckeys->buf_used)
865 const char *src = reckeys->buf + off;
873 memcpy (&attrSet, src, sizeof(attrSet));
874 src += sizeof(attrSet);
878 memcpy (&attrUse, src, sizeof(attrUse));
879 src += sizeof(attrUse);
881 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
884 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
886 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
888 ch = zebraExplain_addSU (zti, attrSet, attrUse);
890 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
893 ((char*)key_buf) [key_buf_used++] = *src++;
895 ((char*)key_buf) [key_buf_used++] = '\0';
896 ((char*) key_buf)[key_buf_used++] = cmd;
899 seqno += ((lead>>2) & 15)-1;
902 memcpy (&seqno, src, sizeof(seqno));
903 src += sizeof(seqno);
907 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
908 key_buf_used += sizeof(key);
909 off = src - reckeys->buf;
911 assert (off == reckeys->buf_used);
914 static const char **searchRecordKey (struct recKeys *reckeys,
915 int attrSetS, int attrUseS)
917 static const char *ws[32];
925 for (i = 0; i<32; i++)
928 while (off < reckeys->buf_used)
931 const char *src = reckeys->buf + off;
939 memcpy (&attrSet, src, sizeof(attrSet));
940 src += sizeof(attrSet);
944 memcpy (&attrUse, src, sizeof(attrUse));
945 src += sizeof(attrUse);
951 seqno += ((lead>>2) & 15)-1;
954 memcpy (&seqno, src, sizeof(seqno));
955 src += sizeof(seqno);
957 if (attrUseS == attrUse && attrSetS == attrSet)
964 woff = seqno - startSeq;
965 if (woff >= 0 && woff < 31)
969 off = src - reckeys->buf;
971 assert (off == reckeys->buf_used);
975 struct file_read_info {
976 off_t file_max; /* maximum offset so far */
977 off_t file_offset; /* current offset */
978 off_t file_moffset; /* offset of rec/rec boundary */
985 static struct file_read_info *file_read_start (int fd)
987 struct file_read_info *fi = xmalloc (sizeof(*fi));
991 fi->file_moffset = 0;
997 static void file_read_stop (struct file_read_info *fi)
1002 static off_t file_seek (void *handle, off_t offset)
1004 struct file_read_info *p = handle;
1005 p->file_offset = offset;
1008 return lseek (p->fd, offset, SEEK_SET);
1011 static off_t file_tell (void *handle)
1013 struct file_read_info *p = handle;
1014 return p->file_offset;
1017 static int file_read (void *handle, char *buf, size_t count)
1019 struct file_read_info *p = handle;
1025 if (r > p->sdrmax - p->file_offset)
1026 r = p->sdrmax - p->file_offset;
1028 memcpy (buf, p->sdrbuf + p->file_offset, r);
1031 r = read (fd, buf, count);
1034 p->file_offset += r;
1035 if (p->file_offset > p->file_max)
1036 p->file_max = p->file_offset;
1041 static void file_begin (void *handle)
1043 struct file_read_info *p = handle;
1045 p->file_offset = p->file_moffset;
1046 if (!p->sdrbuf && p->file_moffset)
1047 lseek (p->fd, p->file_moffset, SEEK_SET);
1051 static void file_end (void *handle, off_t offset)
1053 struct file_read_info *p = handle;
1055 assert (p->file_more == 0);
1057 p->file_moffset = offset;
1060 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1061 const char *fname, const char *spec)
1063 static char dstBuf[2048];
1065 const char *s = spec;
1066 static const char **w;
1070 while (*s == ' ' || *s == '\t')
1076 char attset_str[64], attname_str[64];
1077 data1_attset *attset;
1080 int attSet = 1, attUse = 1;
1084 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1086 attset_str[i++] = *s;
1087 attset_str[i] = '\0';
1092 for (i = 0; *s && *s != ')'; s++)
1094 attname_str[i++] = *s;
1095 attname_str[i] = '\0';
1098 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1101 attSet = attset->reference;
1102 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1104 attUse = att->value;
1106 attUse = atoi (attname_str);
1108 w = searchRecordKey (reckeys, attSet, attUse);
1113 for (i = 0; i<32; i++)
1118 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1119 spec, rGroup->groupName ? rGroup->groupName : "none");
1124 for (i = 0; i<32; i++)
1125 if (matchFlag[i] && w[i])
1133 dst += strlen(w[i]);
1137 logf (LOG_WARN, "Record didn't contain match"
1138 " fields in (%s,%s)", attset_str, attname_str);
1146 const char *spec_src = NULL;
1147 const char *s1 = ++s;
1148 while (*s1 && *s1 != ' ' && *s1 != '\t')
1154 memcpy (special, s, spec_len);
1155 special[spec_len] = '\0';
1158 if (!strcmp (special, "group"))
1159 spec_src = rGroup->groupName;
1160 else if (!strcmp (special, "database"))
1161 spec_src = rGroup->databaseName;
1162 else if (!strcmp (special, "filename"))
1164 else if (!strcmp (special, "type"))
1165 spec_src = rGroup->recordType;
1170 strcpy (dst, spec_src);
1171 dst += strlen (spec_src);
1174 else if (*s == '\"' || *s == '\'')
1176 int stopMarker = *s++;
1180 while (*s && *s != stopMarker)
1183 tmpString[i++] = *s++;
1187 tmpString[i] = '\0';
1188 strcpy (dst, tmpString);
1189 dst += strlen (tmpString);
1193 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1194 spec, rGroup->groupName ? rGroup->groupName : "none");
1201 logf (LOG_WARN, "No match criteria for record %s in group %s",
1202 fname, rGroup->groupName ? rGroup->groupName : "none");
1209 struct recordLogInfo {
1212 struct recordGroup *rGroup;
1215 static void recordLogPreamble (int level, const char *msg, void *info)
1217 struct recordLogInfo *p = info;
1218 FILE *outf = log_file ();
1220 if (level & LOG_LOG)
1222 fprintf (outf, "File %s, offset %d, type %s\n",
1223 p->rGroup->recordType, p->recordOffset, p->fname);
1224 log_event_start (NULL, NULL);
1227 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1229 zebraExplain_addSchema (zti, oid);
1232 static int recordExtract (SYSNO *sysno, const char *fname,
1233 struct recordGroup *rGroup, int deleteFlag,
1234 struct file_read_info *fi,
1235 RecType recType, char *subType)
1237 RecordAttr *recordAttr;
1242 struct recordLogInfo logInfo;
1243 off_t recordOffset = 0;
1247 struct recExtractCtrl extractCtrl;
1249 /* we are going to read from a file, so prepare the extraction */
1252 reckeys.buf_used = 0;
1253 reckeys.prevAttrUse = -1;
1254 reckeys.prevAttrSet = -1;
1255 reckeys.prevSeqNo = 0;
1257 recordOffset = fi->file_moffset;
1258 extractCtrl.offset = fi->file_moffset;
1259 extractCtrl.readf = file_read;
1260 extractCtrl.seekf = file_seek;
1261 extractCtrl.tellf = file_tell;
1262 extractCtrl.endf = file_end;
1263 extractCtrl.fh = fi;
1264 extractCtrl.subType = subType;
1265 extractCtrl.init = wordInit;
1266 extractCtrl.addWord = addRecordKey;
1267 extractCtrl.addSchema = addSchema;
1268 extractCtrl.dh = rGroup->dh;
1269 for (i = 0; i<256; i++)
1271 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1272 extractCtrl.seqno[i] = 1;
1274 extractCtrl.seqno[i] = 0;
1276 extractCtrl.zebra_maps = rGroup->zebra_maps;
1277 extractCtrl.flagShowRecords = !rGroup->flagRw;
1279 if (!rGroup->flagRw)
1280 printf ("File: %s %ld\n", fname, (long) recordOffset);
1282 logInfo.fname = fname;
1283 logInfo.recordOffset = recordOffset;
1284 logInfo.rGroup = rGroup;
1285 log_event_start (recordLogPreamble, &logInfo);
1287 r = (*recType->extract)(&extractCtrl);
1289 log_event_start (NULL, NULL);
1293 /* error occured during extraction ... */
1294 if (rGroup->flagRw &&
1295 records_processed < rGroup->fileVerboseLimit)
1297 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1298 fname, (long) recordOffset, r);
1302 if (reckeys.buf_used == 0)
1304 /* the extraction process returned no information - the record
1305 is probably empty - unless flagShowRecords is in use */
1306 if (!rGroup->flagRw)
1308 logf (LOG_WARN, "No keys generated for file %s", fname);
1309 logf (LOG_WARN, " The file is probably empty");
1314 /* perform match if sysno not known and if match criteria is specified */
1321 if (rGroup->recordId && *rGroup->recordId)
1325 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1329 rinfo = dict_lookup (matchDict, matchStr);
1331 memcpy (sysno, rinfo+1, sizeof(*sysno));
1335 logf (LOG_WARN, "Bad match criteria");
1346 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1347 fname, (long) recordOffset);
1348 logf (LOG_WARN, "cannot delete record above (seems new)");
1351 if (records_processed < rGroup->fileVerboseLimit)
1352 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1353 fname, (long) recordOffset);
1354 rec = rec_new (records);
1356 *sysno = rec->sysno;
1358 recordAttr = rec_init_attr (zti, rec);
1362 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1364 flushRecordKeys (*sysno, 1, &reckeys);
1365 flushSortKeys (*sysno, 1);
1371 /* record already exists */
1372 struct recKeys delkeys;
1374 rec = rec_get (records, *sysno);
1377 recordAttr = rec_init_attr (zti, rec);
1379 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1381 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1382 fname, (long) recordOffset);
1387 delkeys.buf_used = rec->size[recInfo_delKeys];
1388 delkeys.buf = rec->info[recInfo_delKeys];
1389 flushSortKeys (*sysno, 0);
1390 flushRecordKeys (*sysno, 0, &delkeys);
1393 /* record going to be deleted */
1394 if (!delkeys.buf_used)
1396 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1397 fname, (long) recordOffset);
1398 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1402 if (records_processed < rGroup->fileVerboseLimit)
1403 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1404 fname, (long) recordOffset);
1407 dict_delete (matchDict, matchStr);
1408 rec_del (records, &rec);
1416 /* record going to be updated */
1417 if (!delkeys.buf_used)
1419 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1420 fname, (long) recordOffset);
1421 logf (LOG_WARN, "cannot update file above, storeKeys false");
1425 if (records_processed < rGroup->fileVerboseLimit)
1426 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1427 fname, (long) recordOffset);
1428 flushRecordKeys (*sysno, 1, &reckeys);
1433 /* update file type */
1434 xfree (rec->info[recInfo_fileType]);
1435 rec->info[recInfo_fileType] =
1436 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1438 /* update filename */
1439 xfree (rec->info[recInfo_filename]);
1440 rec->info[recInfo_filename] =
1441 rec_strdup (fname, &rec->size[recInfo_filename]);
1443 /* update delete keys */
1444 xfree (rec->info[recInfo_delKeys]);
1445 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1448 rec->size[recInfo_delKeys] = reckeys.buf_used;
1449 rec->info[recInfo_delKeys] = reckeys.buf;
1451 reckeys.buf_max = 0;
1453 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1454 rec->size[recInfo_delKeys] = reckeys.buf_used;
1455 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1456 rec->size[recInfo_delKeys]);
1461 rec->info[recInfo_delKeys] = NULL;
1462 rec->size[recInfo_delKeys] = 0;
1465 /* save file size of original record */
1466 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1467 recordAttr->recordSize = fi->file_moffset - recordOffset;
1468 if (!recordAttr->recordSize)
1469 recordAttr->recordSize = fi->file_max - recordOffset;
1470 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1472 /* set run-number for this record */
1473 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1475 /* update store data */
1476 xfree (rec->info[recInfo_storeData]);
1477 if (rGroup->flagStoreData == 1)
1479 rec->size[recInfo_storeData] = recordAttr->recordSize;
1480 rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
1481 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1483 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1484 (long) recordOffset, fname);
1487 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1488 < recordAttr->recordSize)
1490 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1491 recordAttr->recordSize, fname);
1497 rec->info[recInfo_storeData] = NULL;
1498 rec->size[recInfo_storeData] = 0;
1500 /* update database name */
1501 xfree (rec->info[recInfo_databaseName]);
1502 rec->info[recInfo_databaseName] =
1503 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1506 recordAttr->recordOffset = recordOffset;
1508 /* commit this record */
1509 rec_put (records, &rec);
1514 int fileExtract (SYSNO *sysno, const char *fname,
1515 const struct recordGroup *rGroupP, int deleteFlag)
1523 struct recordGroup rGroupM;
1524 struct recordGroup *rGroup = &rGroupM;
1525 struct file_read_info *fi;
1527 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1529 if (!rGroup->groupName || !*rGroup->groupName)
1532 sprintf (gprefix, "%s.", rGroup->groupName);
1534 logf (LOG_DEBUG, "fileExtract %s", fname);
1536 /* determine file extension */
1538 for (i = strlen(fname); --i >= 0; )
1539 if (fname[i] == '/')
1541 else if (fname[i] == '.')
1543 strcpy (ext, fname+i+1);
1546 /* determine file type - depending on extension */
1547 if (!rGroup->recordType)
1549 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1550 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1552 sprintf (ext_res, "%srecordType", gprefix);
1553 rGroup->recordType = res_get (common_resource, ext_res);
1556 if (!rGroup->recordType)
1558 if (records_processed < rGroup->fileVerboseLimit)
1559 logf (LOG_LOG, "? %s", fname);
1562 if (!*rGroup->recordType)
1565 recType_byName (rGroup->recTypes, rGroup->recordType, subType)))
1567 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1571 /* determine match criteria */
1572 if (!rGroup->recordId)
1574 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1575 rGroup->recordId = res_get (common_resource, ext_res);
1578 /* determine database name */
1579 if (!rGroup->databaseName)
1581 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1582 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1584 sprintf (ext_res, "%sdatabase", gprefix);
1585 rGroup->databaseName = res_get (common_resource, ext_res);
1588 if (!rGroup->databaseName)
1589 rGroup->databaseName = "Default";
1591 /* determine if explain database */
1593 sprintf (ext_res, "%sexplainDatabase", gprefix);
1594 rGroup->explainDatabase =
1595 atoi (res_get_def (common_resource, ext_res, "0"));
1597 /* announce database */
1598 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1600 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1601 rGroup->explainDatabase))
1605 if (rGroup->flagStoreData == -1)
1608 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1609 if (!(sval = res_get (common_resource, ext_res)))
1611 sprintf (ext_res, "%sstoreData", gprefix);
1612 sval = res_get (common_resource, ext_res);
1615 rGroup->flagStoreData = atoi (sval);
1617 if (rGroup->flagStoreData == -1)
1618 rGroup->flagStoreData = 0;
1620 if (rGroup->flagStoreKeys == -1)
1624 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1625 if (!(sval = res_get (common_resource, ext_res)))
1627 sprintf (ext_res, "%sstoreKeys", gprefix);
1628 sval = res_get (common_resource, ext_res);
1631 rGroup->flagStoreKeys = atoi (sval);
1633 if (rGroup->flagStoreKeys == -1)
1634 rGroup->flagStoreKeys = 0;
1640 char xname[128], *xp;
1642 strncpy (xname, fname, 127);
1643 if ((xp = strchr (xname, '.')))
1646 h = zebraSdr_open (xname);
1649 logf (LOG_WARN, "sdr open %s", xname);
1658 segmentno = zebraSdr_segment (h, 0);
1659 sprintf (sdr_name, "%%%s.%d", xname, segmentno);
1660 logf (LOG_LOG, "SDR: %s", sdr_name);
1666 r = zebraSdr_read (h, &buf);
1671 fi = file_read_start (0);
1677 r = recordExtract (sysno, sdr_name, rGroup, deleteFlag, fi,
1679 } while (r && !sysno && fi->file_more);
1680 file_read_stop (fi);
1687 if (sysno && deleteFlag)
1691 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1693 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1697 fi = file_read_start (fd);
1701 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1703 } while (r && !sysno && fi->file_more);
1704 file_read_stop (fi);
1710 static int explain_extract (void *handle, Record rec, data1_node *n)
1712 struct recordGroup *rGroup = (struct recordGroup*) handle;
1713 struct recExtractCtrl extractCtrl;
1716 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1718 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1722 reckeys.buf_used = 0;
1723 reckeys.prevAttrUse = -1;
1724 reckeys.prevAttrSet = -1;
1725 reckeys.prevSeqNo = 0;
1727 extractCtrl.init = wordInit;
1728 extractCtrl.addWord = addRecordKey;
1729 extractCtrl.addSchema = addSchema;
1730 extractCtrl.dh = rGroup->dh;
1731 for (i = 0; i<256; i++)
1732 extractCtrl.seqno[i] = 0;
1733 extractCtrl.zebra_maps = rGroup->zebra_maps;
1734 extractCtrl.flagShowRecords = !rGroup->flagRw;
1736 grs_extract_tree(&extractCtrl, n);
1738 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1740 if (rec->size[recInfo_delKeys])
1742 struct recKeys delkeys;
1744 delkeys.buf_used = rec->size[recInfo_delKeys];
1745 delkeys.buf = rec->info[recInfo_delKeys];
1746 flushSortKeys (rec->sysno, 0);
1747 flushRecordKeys (rec->sysno, 0, &delkeys);
1749 flushRecordKeys (rec->sysno, 1, &reckeys);
1750 flushSortKeys (rec->sysno, 1);
1752 xfree (rec->info[recInfo_delKeys]);
1753 rec->size[recInfo_delKeys] = reckeys.buf_used;
1754 rec->info[recInfo_delKeys] = reckeys.buf;
1756 reckeys.buf_max = 0;