2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.99 2000-02-24 10:57:02 adam
8 * Sequence number incremented after each incomplete-field.
10 * Revision 1.98 1999/09/07 07:19:21 adam
11 * Work on character mapping. Implemented replace rules.
13 * Revision 1.97 1999/07/06 12:28:04 adam
14 * Updated record index structure. Format includes version ID. Compression
15 * algorithm ID is stored for each record block.
17 * Revision 1.96 1999/05/26 07:49:13 adam
20 * Revision 1.95 1999/05/21 12:00:17 adam
21 * Better diagnostics for extraction process.
23 * Revision 1.94 1999/05/20 12:57:18 adam
24 * Implemented TCL filter. Updated recctrl system.
26 * Revision 1.93 1999/05/15 14:36:38 adam
27 * Updated dictionary. Implemented "compression" of dictionary.
29 * Revision 1.92 1999/03/09 16:27:49 adam
30 * More work on SDRKit integration.
32 * Revision 1.91 1999/02/12 13:29:22 adam
33 * Implemented position-flag for registers.
35 * Revision 1.90 1999/02/02 14:50:52 adam
36 * Updated WIN32 code specific sections. Changed header.
38 * Revision 1.89 1998/10/28 10:54:38 adam
41 * Revision 1.88 1998/10/16 08:14:29 adam
42 * Updated record control system.
44 * Revision 1.87 1998/10/15 13:10:33 adam
45 * Fixed bug in Zebra that caused it to stop indexing when empty
48 * Revision 1.86 1998/10/13 20:33:53 adam
49 * Fixed one log message and change use ordinal to be an unsigned char.
51 * Revision 1.85 1998/09/22 10:03:41 adam
52 * Changed result sets to be persistent in the sense that they can
53 * be re-searched if needed.
54 * Fixed memory leak in rsm_or.
56 * Revision 1.84 1998/06/11 15:42:22 adam
57 * Changed the way use attributes are specified in the recordId
60 * Revision 1.83 1998/06/08 14:43:10 adam
61 * Added suport for EXPLAIN Proxy servers - added settings databasePath
62 * and explainDatabase to facilitate this. Increased maximum number
63 * of databases and attributes in one register.
65 * Revision 1.82 1998/05/20 10:12:15 adam
66 * Implemented automatic EXPLAIN database maintenance.
67 * Modified Zebra to work with ASN.1 compiled version of YAZ.
69 * Revision 1.81 1998/03/11 11:19:04 adam
70 * Changed the way sequence numbers are generated.
72 * Revision 1.80 1998/03/05 08:45:11 adam
73 * New result set model and modular ranking system. Moved towards
74 * descent server API. System information stored as "SGML" records.
76 * Revision 1.79 1998/02/17 10:32:52 adam
77 * Fixed bug: binary files weren't opened with flag b on NT.
79 * Revision 1.78 1998/02/10 12:03:05 adam
82 * Revision 1.77 1998/01/12 15:04:08 adam
83 * The test option (-s) only uses read-lock (and not write lock).
85 * Revision 1.76 1997/10/27 14:33:04 adam
86 * Moved towards generic character mapping depending on "structure"
87 * field in abstract syntax file. Fixed a few memory leaks. Fixed
88 * bug with negative integers when doing searches with relational
91 * Revision 1.75 1997/09/17 12:19:12 adam
92 * Zebra version corresponds to YAZ version 1.4.
93 * Changed Zebra server so that it doesn't depend on global common_resource.
95 * Revision 1.74 1997/09/09 13:38:06 adam
96 * Partial port to WIN95/NT.
98 * Revision 1.73 1997/09/04 13:57:20 adam
99 * New file extract/retrieve method tellf (added).
100 * Added O_BINARY for open calls.
102 * Revision 1.72 1997/07/15 16:32:29 adam
103 * Bug fix: Match handler didn't terminate the resulting string!
105 * Revision 1.71 1997/07/15 16:28:41 adam
106 * Bug fix: storeData didn't work with files with multiple records.
107 * Bug fix: fixed memory management with records; not really well
110 * Revision 1.70 1997/07/01 13:00:42 adam
111 * Bug fix in routine searchRecordKey: uninitialized variables.
113 * Revision 1.69 1997/04/29 09:26:03 adam
114 * Bug fix: generic recordId handling didn't work for compressed internal
117 * Revision 1.68 1997/02/12 20:39:45 adam
118 * Implemented options -f <n> that limits the log to the first <n>
120 * Changed some log messages also.
122 * Revision 1.67 1996/11/15 15:02:14 adam
123 * Minor changes regarding logging.
125 * Revision 1.66 1996/11/14 09:52:21 adam
126 * Strings in record keys bound by IT_MAX_WORD.
128 * Revision 1.65 1996/11/14 08:57:56 adam
129 * Reduction of storeKeys area.
131 * Revision 1.64 1996/11/08 11:10:16 adam
132 * Buffers used during file match got bigger.
133 * Compressed ISAM support everywhere.
134 * Bug fixes regarding masking characters in queries.
135 * Redesigned Regexp-2 queries.
137 * Revision 1.63 1996/10/29 14:09:39 adam
138 * Use of cisam system - enabled if setting isamc is 1.
140 * Revision 1.62 1996/10/11 10:57:01 adam
141 * New module recctrl. Used to manage records (extract/retrieval).
142 * Several files have been moved to the recctrl sub directory.
144 * Revision 1.61 1996/06/06 12:08:37 quinn
145 * Added showRecord function
147 * Revision 1.60 1996/06/04 10:18:12 adam
148 * Search/scan uses character mapping module.
150 * Revision 1.59 1996/05/14 15:47:07 adam
151 * Cleanup of various buffer size entities.
153 * Revision 1.58 1996/05/14 06:16:38 adam
154 * Compact use/set bytes used in search service.
156 * Revision 1.57 1996/05/13 14:23:04 adam
157 * Work on compaction of set/use bytes in dictionary.
159 * Revision 1.56 1996/05/09 09:54:42 adam
160 * Server supports maps from one logical attributes to a list of physical
162 * The extraction process doesn't make space consuming 'any' keys.
164 * Revision 1.55 1996/05/09 07:28:55 quinn
165 * Work towards phrases and multiple registers
167 * Revision 1.54 1996/05/01 13:46:35 adam
168 * First work on multiple records in one file.
169 * New option, -offset, to the "unread" command in the filter module.
171 * Revision 1.53 1996/04/26 12:09:43 adam
172 * Added a few comments.
174 * Revision 1.52 1996/04/25 13:27:57 adam
175 * Function recordExtract modified so that files with no keys (possibly empty)
178 * Revision 1.51 1996/03/19 11:08:42 adam
179 * Bug fix: Log preamble wasn't always turned off after recordExtract.
181 * Revision 1.50 1996/02/12 18:45:36 adam
182 * New fileVerboseFlag in record group control.
184 * Revision 1.49 1996/02/05 12:29:57 adam
185 * Logging reduced a bit.
186 * The remaining running time is estimated during register merge.
188 * Revision 1.48 1996/02/01 20:53:26 adam
189 * The temporary per-record keys are compacted a little, and duplication
190 * of the per-records keys are avoided when they are saved in the record
191 * information buffer.
193 * Revision 1.47 1996/01/17 14:57:48 adam
194 * Prototype changed for reader functions in extract/retrieve. File
195 * is identified by 'void *' instead of 'int.
197 * Revision 1.46 1995/12/15 14:57:16 adam
200 * Revision 1.45 1995/12/15 12:37:41 adam
201 * In addRecordKeyAny: Writes key only when attrSet != -1.
203 * Revision 1.44 1995/12/12 16:00:54 adam
204 * System call sync(2) used after update/commit.
205 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
208 * Revision 1.43 1995/12/11 09:12:46 adam
209 * The rec_get function returns NULL if record doesn't exist - will
210 * happen in the server if the result set records have been deleted since
211 * the creation of the set (i.e. the search).
212 * The server saves a result temporarily if it is 'volatile', i.e. the
213 * set is register dependent.
215 * Revision 1.42 1995/12/07 17:38:46 adam
216 * Work locking mechanisms for concurrent updates/commit.
218 * Revision 1.41 1995/12/06 16:06:42 adam
219 * Better diagnostics. Work on 'real' dictionary deletion.
221 * Revision 1.40 1995/12/05 16:57:40 adam
222 * More work on regular patterns.
224 * Revision 1.39 1995/12/05 13:20:18 adam
225 * Bug fix: file_read sometimes returned early EOF.
227 * Revision 1.38 1995/12/04 17:59:21 adam
228 * More work on regular expression conversion.
230 * Revision 1.37 1995/12/04 14:22:27 adam
231 * Extra arg to recType_byName.
232 * Started work on new regular expression parsed input to
233 * structured records.
235 * Revision 1.36 1995/11/30 08:34:29 adam
236 * Started work on commit facility.
237 * Changed a few malloc/free to xmalloc/xfree.
239 * Revision 1.35 1995/11/28 14:26:21 adam
240 * Bug fix: recordId with constant wasn't right.
241 * Bug fix: recordId dictionary entry wasn't deleted when needed.
243 * Revision 1.34 1995/11/28 09:09:38 adam
244 * Zebra config renamed.
245 * Use setting 'recordId' to identify record now.
246 * Bug fix in recindex.c: rec_release_blocks was invokeded even
247 * though the blocks were already released.
248 * File traversal properly deletes records when needed.
250 * Revision 1.33 1995/11/27 09:56:20 adam
251 * Record info elements better enumerated. Internal store of records.
253 * Revision 1.32 1995/11/25 10:24:05 adam
254 * More record fields - they are enumerated now.
255 * New options: flagStoreData flagStoreKey.
257 * Revision 1.31 1995/11/24 11:31:35 adam
258 * Commands add & del read filenames from stdin if source directory is
260 * Match criteria supports 'constant' strings.
262 * Revision 1.30 1995/11/22 17:19:16 adam
263 * Record management uses the bfile system.
265 * Revision 1.29 1995/11/21 15:01:14 adam
266 * New general match criteria implemented.
267 * New feature: document groups.
269 * Revision 1.28 1995/11/21 09:20:30 adam
270 * Yet more work on record match.
272 * Revision 1.27 1995/11/20 16:59:45 adam
273 * New update method: the 'old' keys are saved for each records.
275 * Revision 1.26 1995/11/20 11:56:24 adam
276 * Work on new traversal.
278 * Revision 1.25 1995/11/16 15:34:54 adam
279 * Uses new record management system in both indexer and server.
281 * Revision 1.24 1995/11/15 19:13:08 adam
282 * Work on record management.
284 * Revision 1.23 1995/10/27 14:00:10 adam
285 * Implemented detection of database availability.
287 * Revision 1.22 1995/10/17 18:02:07 adam
288 * New feature: databases. Implemented as prefix to words in dictionary.
290 * Revision 1.21 1995/10/10 12:24:38 adam
291 * Temporary sort files are compressed.
293 * Revision 1.20 1995/10/06 13:52:05 adam
294 * Bug fixes. Handler may abort further scanning.
296 * Revision 1.19 1995/10/04 12:55:16 adam
297 * Bug fix in ranked search. Use=Any keys inserted.
299 * Revision 1.18 1995/10/04 09:37:08 quinn
302 * Revision 1.17 1995/10/03 14:28:57 adam
303 * Buffered read in extract works.
305 * Revision 1.16 1995/10/03 14:28:45 adam
306 * Work on more effecient read handler in extract.
308 * Revision 1.15 1995/10/02 15:42:53 adam
309 * Extract uses file descriptors instead of FILE pointers.
311 * Revision 1.14 1995/10/02 15:29:13 adam
312 * More logging in file_extract.
314 * Revision 1.13 1995/09/29 14:01:39 adam
317 * Revision 1.12 1995/09/28 14:22:56 adam
318 * Sort uses smaller temporary files.
320 * Revision 1.11 1995/09/28 12:10:31 adam
321 * Bug fixes. Field prefix used in queries.
323 * Revision 1.10 1995/09/28 09:19:41 adam
324 * xfree/xmalloc used everywhere.
325 * Extract/retrieve method seems to work for text records.
327 * Revision 1.9 1995/09/27 12:22:28 adam
328 * More work on extract in record control.
329 * Field name is not in isam keys but in prefix in dictionary words.
331 * Revision 1.8 1995/09/14 07:48:22 adam
332 * Record control management.
334 * Revision 1.7 1995/09/11 13:09:32 adam
335 * More work on relevance feedback.
337 * Revision 1.6 1995/09/08 14:52:27 adam
338 * Minor changes. Dictionary is lower case now.
340 * Revision 1.5 1995/09/06 16:11:16 adam
341 * Option: only one word key per file.
343 * Revision 1.4 1995/09/05 15:28:39 adam
344 * More work on search engine.
346 * Revision 1.3 1995/09/04 12:33:41 adam
347 * Various cleanup. YAZ util used instead.
349 * Revision 1.2 1995/09/04 09:10:34 adam
350 * More work on index add/del/update.
351 * Merge sort implemented.
352 * Initial work on z39 server.
354 * Revision 1.1 1995/09/01 14:06:35 adam
355 * Split of work into more files.
379 #include "zebrasdr.h"
382 static Dict matchDict;
384 static Records records = NULL;
385 static SortIdx sortIdx = NULL;
387 static char **key_buf;
388 static size_t ptr_top;
390 static size_t key_buf_used;
391 static int key_file_no;
393 static int records_inserted = 0;
394 static int records_updated = 0;
395 static int records_deleted = 0;
396 static int records_processed = 0;
398 static ZebraExplainInfo zti = NULL;
400 static void logRecord (int showFlag)
404 if (showFlag || !(records_processed % 1000))
406 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
407 records_processed, records_inserted, records_updated,
412 static int explain_extract (void *handle, Record drec, data1_node *n);
414 int key_open (struct recordGroup *rGroup, int mem)
416 BFiles bfs = rGroup->bfs;
417 int rw = rGroup->flagRw;
418 data1_handle dh = rGroup->dh;
419 char *recordCompression;
420 int record_compression = REC_COMPRESS_NONE;
422 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
425 key_buf = (char **) xmalloc (mem);
426 ptr_top = mem/sizeof(char*);
432 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
434 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
438 recordCompression = res_get_def (common_resource,
439 "recordCompression", "none");
440 if (!strcmp (recordCompression, "none"))
441 record_compression = REC_COMPRESS_NONE;
442 if (!strcmp (recordCompression, "bzip2"))
443 record_compression = REC_COMPRESS_BZIP2;
444 records = rec_open (bfs, rw, record_compression);
447 dict_close (matchDict);
450 zti = zebraExplain_open (records, dh, common_resource,
451 rw, rGroup, explain_extract);
454 rec_close (&records);
455 dict_close (matchDict);
458 sortIdx = sortIdx_open (bfs, 1);
469 void encode_key_init (struct encode_info *i)
476 char *encode_key_int (int d, char *bp)
485 else if (d <= 4194303)
487 *bp++ = 128 + (d>>16);
488 *bp++ = (d>>8) & 255;
493 *bp++ = 192 + (d>>24);
494 *bp++ = (d>>16) & 255;
495 *bp++ = (d>>8) & 255;
501 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
506 while ((*bp++ = *k++))
508 memcpy (&key, k+1, sizeof(struct it_key));
509 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
510 if (i->sysno != key.sysno)
512 i->sysno = key.sysno;
515 else if (!i->seqno && !key.seqno && i->cmd == *k)
517 bp = encode_key_int (key.seqno - i->seqno, bp);
518 i->seqno = key.seqno;
520 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
522 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
530 static int key_y_len;
532 static int key_y_compare (const void *p1, const void *p2)
536 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
537 *(char**) p2 + key_y_len + 1)))
539 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
542 static int key_x_compare (const void *p1, const void *p2)
544 return strcmp (*(char**) p1, *(char**) p2);
548 void key_flush (void)
553 struct encode_info encode_info;
562 logf (LOG_LOG, "sorting section %d", key_file_no);
564 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
565 getFnameTmp (out_fname, key_file_no);
567 if (!(outf = fopen (out_fname, "wb")))
569 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
572 logf (LOG_LOG, "writing section %d", key_file_no);
573 prevcp = cp = key_buf[ptr_top-ptr_i];
575 encode_key_init (&encode_info);
576 encode_key_write (cp, &encode_info, outf);
579 cp = key_buf[ptr_top-ptr_i];
580 if (strcmp (cp, prevcp))
582 encode_key_init (&encode_info);
583 encode_key_write (cp, &encode_info, outf);
587 encode_key_write (cp + strlen(cp), &encode_info, outf);
590 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
591 getFnameTmp (out_fname, key_file_no);
593 if (!(outf = fopen (out_fname, "wb")))
595 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
598 logf (LOG_LOG, "writing section %d", key_file_no);
600 prevcp = key_buf[ptr_top-i];
602 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
604 key_y_len = strlen(prevcp)+1;
606 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
607 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
609 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
610 sizeof(char*), key_y_compare);
611 cp = key_buf[ptr_top-ptr_i];
613 encode_key_init (&encode_info);
614 encode_key_write (cp, &encode_info, outf);
617 cp = key_buf[ptr_top-ptr_i];
618 encode_key_write (cp+key_y_len, &encode_info, outf);
622 prevcp = key_buf[ptr_top-ptr_i];
627 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
630 logf (LOG_LOG, "finished section %d", key_file_no);
635 int key_close (struct recordGroup *rGroup)
637 int rw = rGroup->flagRw;
639 zebraExplain_runNumberIncrement (zti, 1);
640 zebraExplain_close (zti, rw, 0);
643 rec_close (&records);
644 dict_close (matchDict);
645 sortIdx_close (sortIdx);
651 static void wordInit (struct recExtractCtrl *p, RecWord *w)
653 w->zebra_maps = p->zebra_maps;
654 w->seqnos = p->seqno;
655 w->attrSet = VAL_BIB1;
661 static struct sortKey {
666 struct sortKey *next;
669 static struct recKeys {
678 static void addIndexString (RecWord *p, const char *string, int length)
681 unsigned char attrSet;
682 unsigned short attrUse;
685 int *pseqno = &p->seqnos[p->reg_type];
687 if (reckeys.buf_used+1024 > reckeys.buf_max)
691 b = (char *) xmalloc (reckeys.buf_max += 128000);
692 if (reckeys.buf_used > 0)
693 memcpy (b, reckeys.buf, reckeys.buf_used);
697 dst = reckeys.buf + reckeys.buf_used;
699 attrSet = p->attrSet;
700 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
703 reckeys.prevAttrSet = attrSet;
704 attrUse = p->attrUse;
705 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
708 reckeys.prevAttrUse = attrUse;
710 diff = 1 + *pseqno - reckeys.prevSeqNo;
711 if (diff >= 1 && diff <= 15)
716 reckeys.prevSeqNo = *pseqno;
722 memcpy (dst, &attrSet, sizeof(attrSet));
723 dst += sizeof(attrSet);
727 memcpy (dst, &attrUse, sizeof(attrUse));
728 dst += sizeof(attrUse);
730 *dst++ = p->reg_type;
731 memcpy (dst, string, length);
737 memcpy (dst, pseqno, sizeof(*pseqno));
738 dst += sizeof(*pseqno);
740 reckeys.buf_used = dst - reckeys.buf;
745 static void addSortString (RecWord *p, const char *string, int length)
749 for (sk = sortKeys; sk; sk = sk->next)
750 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
753 sk = (struct sortKey *) xmalloc (sizeof(*sk));
757 sk->string = (char *) xmalloc (length);
759 memcpy (sk->string, string, length);
761 sk->attrSet = p->attrSet;
762 sk->attrUse = p->attrUse;
765 static void addString (RecWord *p, const char *string, int length)
768 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
769 addSortString (p, string, length);
771 addIndexString (p, string, length);
774 static void addIncompleteField (RecWord *p)
776 const char *b = p->string;
777 int remain = p->length;
778 const char **map = 0;
781 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
785 char buf[IT_MAX_WORD+1];
789 while (map && *map && **map == *CHR_SPACE)
791 remain = p->length - (b - p->string);
793 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
800 while (map && *map && **map != *CHR_SPACE)
802 const char *cp = *map;
804 while (i < IT_MAX_WORD && *cp)
806 remain = p->length - (b - p->string);
808 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
814 addString (p, buf, i);
816 (p->seqnos[p->reg_type])++; /* to separate this from next one */
819 static void addCompleteField (RecWord *p)
821 const char *b = p->string;
822 char buf[IT_MAX_WORD+1];
823 const char **map = 0;
824 int i = 0, remain = p->length;
827 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
829 while (remain > 0 && i < IT_MAX_WORD)
831 while (map && *map && **map == *CHR_SPACE)
833 remain = p->length - (b - p->string);
835 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
842 if (i && i < IT_MAX_WORD)
843 buf[i++] = *CHR_SPACE;
844 while (map && *map && **map != *CHR_SPACE)
846 const char *cp = *map;
848 if (i >= IT_MAX_WORD)
850 while (i < IT_MAX_WORD && *cp)
852 remain = p->length - (b - p->string);
854 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
862 addString (p, buf, i);
865 static void addRecordKey (RecWord *p)
868 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
869 p->string, p->length)))
871 p->string = wrbuf_buf(wrbuf);
872 p->length = wrbuf_len(wrbuf);
874 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
875 addCompleteField (p);
877 addIncompleteField(p);
880 static void flushSortKeys (SYSNO sysno, int cmd)
882 struct sortKey *sk = sortKeys;
884 sortIdx_sysno (sortIdx, sysno);
887 struct sortKey *sk_next = sk->next;
888 sortIdx_type (sortIdx, sk->attrUse);
889 sortIdx_add (sortIdx, sk->string, sk->length);
897 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
899 unsigned char attrSet = (unsigned char) -1;
900 unsigned short attrUse = (unsigned short) -1;
904 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
905 while (off < reckeys->buf_used)
907 const char *src = reckeys->buf + off;
915 memcpy (&attrSet, src, sizeof(attrSet));
916 src += sizeof(attrSet);
920 memcpy (&attrUse, src, sizeof(attrUse));
921 src += sizeof(attrUse);
923 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
926 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
928 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
930 ch = zebraExplain_addSU (zti, attrSet, attrUse);
932 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
935 ((char*)key_buf) [key_buf_used++] = *src++;
937 ((char*)key_buf) [key_buf_used++] = '\0';
938 ((char*) key_buf)[key_buf_used++] = cmd;
941 seqno += ((lead>>2) & 15)-1;
944 memcpy (&seqno, src, sizeof(seqno));
945 src += sizeof(seqno);
949 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
950 key_buf_used += sizeof(key);
951 off = src - reckeys->buf;
953 assert (off == reckeys->buf_used);
956 static const char **searchRecordKey (struct recKeys *reckeys,
957 int attrSetS, int attrUseS)
959 static const char *ws[32];
967 for (i = 0; i<32; i++)
970 while (off < reckeys->buf_used)
973 const char *src = reckeys->buf + off;
981 memcpy (&attrSet, src, sizeof(attrSet));
982 src += sizeof(attrSet);
986 memcpy (&attrUse, src, sizeof(attrUse));
987 src += sizeof(attrUse);
993 seqno += ((lead>>2) & 15)-1;
996 memcpy (&seqno, src, sizeof(seqno));
997 src += sizeof(seqno);
999 if (attrUseS == attrUse && attrSetS == attrSet)
1006 woff = seqno - startSeq;
1007 if (woff >= 0 && woff < 31)
1011 off = src - reckeys->buf;
1013 assert (off == reckeys->buf_used);
1017 struct file_read_info {
1018 off_t file_max; /* maximum offset so far */
1019 off_t file_offset; /* current offset */
1020 off_t file_moffset; /* offset of rec/rec boundary */
1027 static struct file_read_info *file_read_start (int fd)
1029 struct file_read_info *fi = (struct file_read_info *)
1030 xmalloc (sizeof(*fi));
1034 fi->file_moffset = 0;
1040 static void file_read_stop (struct file_read_info *fi)
1045 static off_t file_seek (void *handle, off_t offset)
1047 struct file_read_info *p = (struct file_read_info *) handle;
1048 p->file_offset = offset;
1051 return lseek (p->fd, offset, SEEK_SET);
1054 static off_t file_tell (void *handle)
1056 struct file_read_info *p = (struct file_read_info *) handle;
1057 return p->file_offset;
1060 static int file_read (void *handle, char *buf, size_t count)
1062 struct file_read_info *p = (struct file_read_info *) handle;
1068 if (r > p->sdrmax - p->file_offset)
1069 r = p->sdrmax - p->file_offset;
1071 memcpy (buf, p->sdrbuf + p->file_offset, r);
1074 r = read (fd, buf, count);
1077 p->file_offset += r;
1078 if (p->file_offset > p->file_max)
1079 p->file_max = p->file_offset;
1084 static void file_begin (void *handle)
1086 struct file_read_info *p = (struct file_read_info *) handle;
1088 p->file_offset = p->file_moffset;
1089 if (!p->sdrbuf && p->file_moffset)
1090 lseek (p->fd, p->file_moffset, SEEK_SET);
1094 static void file_end (void *handle, off_t offset)
1096 struct file_read_info *p = (struct file_read_info *) handle;
1098 assert (p->file_more == 0);
1100 p->file_moffset = offset;
1103 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1104 const char *fname, const char *spec)
1106 static char dstBuf[2048];
1108 const char *s = spec;
1109 static const char **w;
1113 while (*s == ' ' || *s == '\t')
1119 char attset_str[64], attname_str[64];
1120 data1_attset *attset;
1123 int attSet = 1, attUse = 1;
1127 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1129 attset_str[i++] = *s;
1130 attset_str[i] = '\0';
1135 for (i = 0; *s && *s != ')'; s++)
1137 attname_str[i++] = *s;
1138 attname_str[i] = '\0';
1141 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1144 attSet = attset->reference;
1145 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1147 attUse = att->value;
1149 attUse = atoi (attname_str);
1151 w = searchRecordKey (reckeys, attSet, attUse);
1156 for (i = 0; i<32; i++)
1161 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1162 spec, rGroup->groupName ? rGroup->groupName : "none");
1167 for (i = 0; i<32; i++)
1168 if (matchFlag[i] && w[i])
1176 dst += strlen(w[i]);
1180 logf (LOG_WARN, "Record didn't contain match"
1181 " fields in (%s,%s)", attset_str, attname_str);
1189 const char *spec_src = NULL;
1190 const char *s1 = ++s;
1191 while (*s1 && *s1 != ' ' && *s1 != '\t')
1197 memcpy (special, s, spec_len);
1198 special[spec_len] = '\0';
1201 if (!strcmp (special, "group"))
1202 spec_src = rGroup->groupName;
1203 else if (!strcmp (special, "database"))
1204 spec_src = rGroup->databaseName;
1205 else if (!strcmp (special, "filename"))
1207 else if (!strcmp (special, "type"))
1208 spec_src = rGroup->recordType;
1213 strcpy (dst, spec_src);
1214 dst += strlen (spec_src);
1217 else if (*s == '\"' || *s == '\'')
1219 int stopMarker = *s++;
1223 while (*s && *s != stopMarker)
1226 tmpString[i++] = *s++;
1230 tmpString[i] = '\0';
1231 strcpy (dst, tmpString);
1232 dst += strlen (tmpString);
1236 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1237 spec, rGroup->groupName ? rGroup->groupName : "none");
1244 logf (LOG_WARN, "No match criteria for record %s in group %s",
1245 fname, rGroup->groupName ? rGroup->groupName : "none");
1252 struct recordLogInfo {
1255 struct recordGroup *rGroup;
1258 static void recordLogPreamble (int level, const char *msg, void *info)
1260 struct recordLogInfo *p = (struct recordLogInfo *) info;
1261 FILE *outf = log_file ();
1263 if (level & LOG_LOG)
1265 fprintf (outf, "File %s, offset %d, type %s\n",
1266 p->rGroup->recordType, p->recordOffset, p->fname);
1267 log_event_start (NULL, NULL);
1270 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1272 zebraExplain_addSchema (zti, oid);
1275 static int recordExtract (SYSNO *sysno, const char *fname,
1276 struct recordGroup *rGroup, int deleteFlag,
1277 struct file_read_info *fi,
1278 RecType recType, char *subType, void *clientData)
1280 RecordAttr *recordAttr;
1285 struct recordLogInfo logInfo;
1286 off_t recordOffset = 0;
1290 struct recExtractCtrl extractCtrl;
1292 /* we are going to read from a file, so prepare the extraction */
1295 reckeys.buf_used = 0;
1296 reckeys.prevAttrUse = -1;
1297 reckeys.prevAttrSet = -1;
1298 reckeys.prevSeqNo = 0;
1300 recordOffset = fi->file_moffset;
1301 extractCtrl.offset = fi->file_moffset;
1302 extractCtrl.readf = file_read;
1303 extractCtrl.seekf = file_seek;
1304 extractCtrl.tellf = file_tell;
1305 extractCtrl.endf = file_end;
1306 extractCtrl.fh = fi;
1307 extractCtrl.subType = subType;
1308 extractCtrl.init = wordInit;
1309 extractCtrl.tokenAdd = addRecordKey;
1310 extractCtrl.schemaAdd = addSchema;
1311 extractCtrl.dh = rGroup->dh;
1312 for (i = 0; i<256; i++)
1314 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1315 extractCtrl.seqno[i] = 1;
1317 extractCtrl.seqno[i] = 0;
1319 extractCtrl.zebra_maps = rGroup->zebra_maps;
1320 extractCtrl.flagShowRecords = !rGroup->flagRw;
1322 if (!rGroup->flagRw)
1323 printf ("File: %s %ld\n", fname, (long) recordOffset);
1325 logInfo.fname = fname;
1326 logInfo.recordOffset = recordOffset;
1327 logInfo.rGroup = rGroup;
1328 log_event_start (recordLogPreamble, &logInfo);
1330 r = (*recType->extract)(clientData, &extractCtrl);
1332 log_event_start (NULL, NULL);
1334 if (r == RECCTRL_EXTRACT_EOF)
1336 else if (r == RECCTRL_EXTRACT_ERROR)
1338 /* error occured during extraction ... */
1339 if (rGroup->flagRw &&
1340 records_processed < rGroup->fileVerboseLimit)
1342 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
1343 fname, (long) recordOffset);
1347 if (reckeys.buf_used == 0)
1349 /* the extraction process returned no information - the record
1350 is probably empty - unless flagShowRecords is in use */
1351 if (!rGroup->flagRw)
1353 logf (LOG_WARN, "No keys generated for file %s", fname);
1354 logf (LOG_WARN, " The file is probably empty");
1359 /* perform match if sysno not known and if match criteria is specified */
1366 if (rGroup->recordId && *rGroup->recordId)
1370 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1374 rinfo = dict_lookup (matchDict, matchStr);
1376 memcpy (sysno, rinfo+1, sizeof(*sysno));
1380 logf (LOG_WARN, "Bad match criteria");
1391 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1392 fname, (long) recordOffset);
1393 logf (LOG_WARN, "cannot delete record above (seems new)");
1396 if (records_processed < rGroup->fileVerboseLimit)
1397 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1398 fname, (long) recordOffset);
1399 rec = rec_new (records);
1401 *sysno = rec->sysno;
1403 recordAttr = rec_init_attr (zti, rec);
1407 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1409 flushRecordKeys (*sysno, 1, &reckeys);
1410 flushSortKeys (*sysno, 1);
1416 /* record already exists */
1417 struct recKeys delkeys;
1419 rec = rec_get (records, *sysno);
1422 recordAttr = rec_init_attr (zti, rec);
1424 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1426 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1427 fname, (long) recordOffset);
1432 delkeys.buf_used = rec->size[recInfo_delKeys];
1433 delkeys.buf = rec->info[recInfo_delKeys];
1434 flushSortKeys (*sysno, 0);
1435 flushRecordKeys (*sysno, 0, &delkeys);
1438 /* record going to be deleted */
1439 if (!delkeys.buf_used)
1441 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1442 fname, (long) recordOffset);
1443 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1447 if (records_processed < rGroup->fileVerboseLimit)
1448 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1449 fname, (long) recordOffset);
1452 dict_delete (matchDict, matchStr);
1453 rec_del (records, &rec);
1461 /* record going to be updated */
1462 if (!delkeys.buf_used)
1464 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1465 fname, (long) recordOffset);
1466 logf (LOG_WARN, "cannot update file above, storeKeys false");
1470 if (records_processed < rGroup->fileVerboseLimit)
1471 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1472 fname, (long) recordOffset);
1473 flushRecordKeys (*sysno, 1, &reckeys);
1478 /* update file type */
1479 xfree (rec->info[recInfo_fileType]);
1480 rec->info[recInfo_fileType] =
1481 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1483 /* update filename */
1484 xfree (rec->info[recInfo_filename]);
1485 rec->info[recInfo_filename] =
1486 rec_strdup (fname, &rec->size[recInfo_filename]);
1488 /* update delete keys */
1489 xfree (rec->info[recInfo_delKeys]);
1490 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1493 rec->size[recInfo_delKeys] = reckeys.buf_used;
1494 rec->info[recInfo_delKeys] = reckeys.buf;
1496 reckeys.buf_max = 0;
1498 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1499 rec->size[recInfo_delKeys] = reckeys.buf_used;
1500 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1501 rec->size[recInfo_delKeys]);
1506 rec->info[recInfo_delKeys] = NULL;
1507 rec->size[recInfo_delKeys] = 0;
1510 /* save file size of original record */
1511 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1512 recordAttr->recordSize = fi->file_moffset - recordOffset;
1513 if (!recordAttr->recordSize)
1514 recordAttr->recordSize = fi->file_max - recordOffset;
1515 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1517 /* set run-number for this record */
1518 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1520 /* update store data */
1521 xfree (rec->info[recInfo_storeData]);
1522 if (rGroup->flagStoreData == 1)
1524 rec->size[recInfo_storeData] = recordAttr->recordSize;
1525 rec->info[recInfo_storeData] = (char *)
1526 xmalloc (recordAttr->recordSize);
1527 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1529 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1530 (long) recordOffset, fname);
1533 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1534 < recordAttr->recordSize)
1536 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1537 recordAttr->recordSize, fname);
1543 rec->info[recInfo_storeData] = NULL;
1544 rec->size[recInfo_storeData] = 0;
1546 /* update database name */
1547 xfree (rec->info[recInfo_databaseName]);
1548 rec->info[recInfo_databaseName] =
1549 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1552 recordAttr->recordOffset = recordOffset;
1554 /* commit this record */
1555 rec_put (records, &rec);
1560 int fileExtract (SYSNO *sysno, const char *fname,
1561 const struct recordGroup *rGroupP, int deleteFlag)
1569 struct recordGroup rGroupM;
1570 struct recordGroup *rGroup = &rGroupM;
1571 struct file_read_info *fi;
1574 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1576 if (!rGroup->groupName || !*rGroup->groupName)
1579 sprintf (gprefix, "%s.", rGroup->groupName);
1581 logf (LOG_DEBUG, "fileExtract %s", fname);
1583 /* determine file extension */
1585 for (i = strlen(fname); --i >= 0; )
1586 if (fname[i] == '/')
1588 else if (fname[i] == '.')
1590 strcpy (ext, fname+i+1);
1593 /* determine file type - depending on extension */
1594 if (!rGroup->recordType)
1596 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1597 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1599 sprintf (ext_res, "%srecordType", gprefix);
1600 rGroup->recordType = res_get (common_resource, ext_res);
1603 if (!rGroup->recordType)
1605 if (records_processed < rGroup->fileVerboseLimit)
1606 logf (LOG_LOG, "? %s", fname);
1609 if (!*rGroup->recordType)
1612 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1615 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1619 /* determine match criteria */
1620 if (!rGroup->recordId)
1622 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1623 rGroup->recordId = res_get (common_resource, ext_res);
1626 /* determine database name */
1627 if (!rGroup->databaseName)
1629 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1630 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1632 sprintf (ext_res, "%sdatabase", gprefix);
1633 rGroup->databaseName = res_get (common_resource, ext_res);
1636 if (!rGroup->databaseName)
1637 rGroup->databaseName = "Default";
1639 /* determine if explain database */
1641 sprintf (ext_res, "%sexplainDatabase", gprefix);
1642 rGroup->explainDatabase =
1643 atoi (res_get_def (common_resource, ext_res, "0"));
1645 /* announce database */
1646 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1648 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1649 rGroup->explainDatabase))
1653 if (rGroup->flagStoreData == -1)
1656 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1657 if (!(sval = res_get (common_resource, ext_res)))
1659 sprintf (ext_res, "%sstoreData", gprefix);
1660 sval = res_get (common_resource, ext_res);
1663 rGroup->flagStoreData = atoi (sval);
1665 if (rGroup->flagStoreData == -1)
1666 rGroup->flagStoreData = 0;
1668 if (rGroup->flagStoreKeys == -1)
1672 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1673 if (!(sval = res_get (common_resource, ext_res)))
1675 sprintf (ext_res, "%sstoreKeys", gprefix);
1676 sval = res_get (common_resource, ext_res);
1679 rGroup->flagStoreKeys = atoi (sval);
1681 if (rGroup->flagStoreKeys == -1)
1682 rGroup->flagStoreKeys = 0;
1688 char xname[128], *xp;
1690 strncpy (xname, fname, 127);
1691 if (!(xp = strchr (xname, '.')))
1694 if (strcmp (xp+1, "sdr.bits"))
1697 h = zebraSdr_open (xname);
1700 logf (LOG_WARN, "sdr open %s", xname);
1709 segmentno = zebraSdr_segment (h, 0);
1710 sprintf (sdr_name, "%%%s.%d", xname, segmentno);
1716 r = zebraSdr_read (h, &buf);
1721 fi = file_read_start (0);
1727 r = recordExtract (sysno, sdr_name, rGroup, deleteFlag, fi,
1729 } while (r && !sysno && fi->file_more);
1730 file_read_stop (fi);
1737 if (sysno && deleteFlag)
1741 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1743 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1747 fi = file_read_start (fd);
1751 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1752 recType, subType, clientData);
1753 } while (r && !sysno && fi->file_more);
1754 file_read_stop (fi);
1760 static int explain_extract (void *handle, Record rec, data1_node *n)
1762 struct recordGroup *rGroup = (struct recordGroup*) handle;
1763 struct recExtractCtrl extractCtrl;
1766 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1768 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1772 reckeys.buf_used = 0;
1773 reckeys.prevAttrUse = -1;
1774 reckeys.prevAttrSet = -1;
1775 reckeys.prevSeqNo = 0;
1777 extractCtrl.init = wordInit;
1778 extractCtrl.tokenAdd = addRecordKey;
1779 extractCtrl.schemaAdd = addSchema;
1780 extractCtrl.dh = rGroup->dh;
1781 for (i = 0; i<256; i++)
1782 extractCtrl.seqno[i] = 0;
1783 extractCtrl.zebra_maps = rGroup->zebra_maps;
1784 extractCtrl.flagShowRecords = !rGroup->flagRw;
1786 grs_extract_tree(&extractCtrl, n);
1788 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1790 if (rec->size[recInfo_delKeys])
1792 struct recKeys delkeys;
1794 delkeys.buf_used = rec->size[recInfo_delKeys];
1795 delkeys.buf = rec->info[recInfo_delKeys];
1796 flushSortKeys (rec->sysno, 0);
1797 flushRecordKeys (rec->sysno, 0, &delkeys);
1799 flushRecordKeys (rec->sysno, 1, &reckeys);
1800 flushSortKeys (rec->sysno, 1);
1802 xfree (rec->info[recInfo_delKeys]);
1803 rec->size[recInfo_delKeys] = reckeys.buf_used;
1804 rec->info[recInfo_delKeys] = reckeys.buf;
1806 reckeys.buf_max = 0;