2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.76 1997-10-27 14:33:04 adam
8 * Moved towards generic character mapping depending on "structure"
9 * field in abstract syntax file. Fixed a few memory leaks. Fixed
10 * bug with negative integers when doing searches with relational
13 * Revision 1.75 1997/09/17 12:19:12 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.74 1997/09/09 13:38:06 adam
18 * Partial port to WIN95/NT.
20 * Revision 1.73 1997/09/04 13:57:20 adam
21 * New file extract/retrieve method tellf (added).
22 * Added O_BINARY for open calls.
24 * Revision 1.72 1997/07/15 16:32:29 adam
25 * Bug fix: Match handler didn't terminate the resulting string!
27 * Revision 1.71 1997/07/15 16:28:41 adam
28 * Bug fix: storeData didn't work with files with multiple records.
29 * Bug fix: fixed memory management with records; not really well
32 * Revision 1.70 1997/07/01 13:00:42 adam
33 * Bug fix in routine searchRecordKey: uninitialized variables.
35 * Revision 1.69 1997/04/29 09:26:03 adam
36 * Bug fix: generic recordId handling didn't work for compressed internal
39 * Revision 1.68 1997/02/12 20:39:45 adam
40 * Implemented options -f <n> that limits the log to the first <n>
42 * Changed some log messages also.
44 * Revision 1.67 1996/11/15 15:02:14 adam
45 * Minor changes regarding logging.
47 * Revision 1.66 1996/11/14 09:52:21 adam
48 * Strings in record keys bound by IT_MAX_WORD.
50 * Revision 1.65 1996/11/14 08:57:56 adam
51 * Reduction of storeKeys area.
53 * Revision 1.64 1996/11/08 11:10:16 adam
54 * Buffers used during file match got bigger.
55 * Compressed ISAM support everywhere.
56 * Bug fixes regarding masking characters in queries.
57 * Redesigned Regexp-2 queries.
59 * Revision 1.63 1996/10/29 14:09:39 adam
60 * Use of cisam system - enabled if setting isamc is 1.
62 * Revision 1.62 1996/10/11 10:57:01 adam
63 * New module recctrl. Used to manage records (extract/retrieval).
64 * Several files have been moved to the recctrl sub directory.
66 * Revision 1.61 1996/06/06 12:08:37 quinn
67 * Added showRecord function
69 * Revision 1.60 1996/06/04 10:18:12 adam
70 * Search/scan uses character mapping module.
72 * Revision 1.59 1996/05/14 15:47:07 adam
73 * Cleanup of various buffer size entities.
75 * Revision 1.58 1996/05/14 06:16:38 adam
76 * Compact use/set bytes used in search service.
78 * Revision 1.57 1996/05/13 14:23:04 adam
79 * Work on compaction of set/use bytes in dictionary.
81 * Revision 1.56 1996/05/09 09:54:42 adam
82 * Server supports maps from one logical attributes to a list of physical
84 * The extraction process doesn't make space consuming 'any' keys.
86 * Revision 1.55 1996/05/09 07:28:55 quinn
87 * Work towards phrases and multiple registers
89 * Revision 1.54 1996/05/01 13:46:35 adam
90 * First work on multiple records in one file.
91 * New option, -offset, to the "unread" command in the filter module.
93 * Revision 1.53 1996/04/26 12:09:43 adam
94 * Added a few comments.
96 * Revision 1.52 1996/04/25 13:27:57 adam
97 * Function recordExtract modified so that files with no keys (possibly empty)
100 * Revision 1.51 1996/03/19 11:08:42 adam
101 * Bug fix: Log preamble wasn't always turned off after recordExtract.
103 * Revision 1.50 1996/02/12 18:45:36 adam
104 * New fileVerboseFlag in record group control.
106 * Revision 1.49 1996/02/05 12:29:57 adam
107 * Logging reduced a bit.
108 * The remaining running time is estimated during register merge.
110 * Revision 1.48 1996/02/01 20:53:26 adam
111 * The temporary per-record keys are compacted a little, and duplication
112 * of the per-records keys are avoided when they are saved in the record
113 * information buffer.
115 * Revision 1.47 1996/01/17 14:57:48 adam
116 * Prototype changed for reader functions in extract/retrieve. File
117 * is identified by 'void *' instead of 'int.
119 * Revision 1.46 1995/12/15 14:57:16 adam
122 * Revision 1.45 1995/12/15 12:37:41 adam
123 * In addRecordKeyAny: Writes key only when attrSet != -1.
125 * Revision 1.44 1995/12/12 16:00:54 adam
126 * System call sync(2) used after update/commit.
127 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
130 * Revision 1.43 1995/12/11 09:12:46 adam
131 * The rec_get function returns NULL if record doesn't exist - will
132 * happen in the server if the result set records have been deleted since
133 * the creation of the set (i.e. the search).
134 * The server saves a result temporarily if it is 'volatile', i.e. the
135 * set is register dependent.
137 * Revision 1.42 1995/12/07 17:38:46 adam
138 * Work locking mechanisms for concurrent updates/commit.
140 * Revision 1.41 1995/12/06 16:06:42 adam
141 * Better diagnostics. Work on 'real' dictionary deletion.
143 * Revision 1.40 1995/12/05 16:57:40 adam
144 * More work on regular patterns.
146 * Revision 1.39 1995/12/05 13:20:18 adam
147 * Bug fix: file_read sometimes returned early EOF.
149 * Revision 1.38 1995/12/04 17:59:21 adam
150 * More work on regular expression conversion.
152 * Revision 1.37 1995/12/04 14:22:27 adam
153 * Extra arg to recType_byName.
154 * Started work on new regular expression parsed input to
155 * structured records.
157 * Revision 1.36 1995/11/30 08:34:29 adam
158 * Started work on commit facility.
159 * Changed a few malloc/free to xmalloc/xfree.
161 * Revision 1.35 1995/11/28 14:26:21 adam
162 * Bug fix: recordId with constant wasn't right.
163 * Bug fix: recordId dictionary entry wasn't deleted when needed.
165 * Revision 1.34 1995/11/28 09:09:38 adam
166 * Zebra config renamed.
167 * Use setting 'recordId' to identify record now.
168 * Bug fix in recindex.c: rec_release_blocks was invokeded even
169 * though the blocks were already released.
170 * File traversal properly deletes records when needed.
172 * Revision 1.33 1995/11/27 09:56:20 adam
173 * Record info elements better enumerated. Internal store of records.
175 * Revision 1.32 1995/11/25 10:24:05 adam
176 * More record fields - they are enumerated now.
177 * New options: flagStoreData flagStoreKey.
179 * Revision 1.31 1995/11/24 11:31:35 adam
180 * Commands add & del read filenames from stdin if source directory is
182 * Match criteria supports 'constant' strings.
184 * Revision 1.30 1995/11/22 17:19:16 adam
185 * Record management uses the bfile system.
187 * Revision 1.29 1995/11/21 15:01:14 adam
188 * New general match criteria implemented.
189 * New feature: document groups.
191 * Revision 1.28 1995/11/21 09:20:30 adam
192 * Yet more work on record match.
194 * Revision 1.27 1995/11/20 16:59:45 adam
195 * New update method: the 'old' keys are saved for each records.
197 * Revision 1.26 1995/11/20 11:56:24 adam
198 * Work on new traversal.
200 * Revision 1.25 1995/11/16 15:34:54 adam
201 * Uses new record management system in both indexer and server.
203 * Revision 1.24 1995/11/15 19:13:08 adam
204 * Work on record management.
206 * Revision 1.23 1995/10/27 14:00:10 adam
207 * Implemented detection of database availability.
209 * Revision 1.22 1995/10/17 18:02:07 adam
210 * New feature: databases. Implemented as prefix to words in dictionary.
212 * Revision 1.21 1995/10/10 12:24:38 adam
213 * Temporary sort files are compressed.
215 * Revision 1.20 1995/10/06 13:52:05 adam
216 * Bug fixes. Handler may abort further scanning.
218 * Revision 1.19 1995/10/04 12:55:16 adam
219 * Bug fix in ranked search. Use=Any keys inserted.
221 * Revision 1.18 1995/10/04 09:37:08 quinn
224 * Revision 1.17 1995/10/03 14:28:57 adam
225 * Buffered read in extract works.
227 * Revision 1.16 1995/10/03 14:28:45 adam
228 * Work on more effecient read handler in extract.
230 * Revision 1.15 1995/10/02 15:42:53 adam
231 * Extract uses file descriptors instead of FILE pointers.
233 * Revision 1.14 1995/10/02 15:29:13 adam
234 * More logging in file_extract.
236 * Revision 1.13 1995/09/29 14:01:39 adam
239 * Revision 1.12 1995/09/28 14:22:56 adam
240 * Sort uses smaller temporary files.
242 * Revision 1.11 1995/09/28 12:10:31 adam
243 * Bug fixes. Field prefix used in queries.
245 * Revision 1.10 1995/09/28 09:19:41 adam
246 * xfree/xmalloc used everywhere.
247 * Extract/retrieve method seems to work for text records.
249 * Revision 1.9 1995/09/27 12:22:28 adam
250 * More work on extract in record control.
251 * Field name is not in isam keys but in prefix in dictionary words.
253 * Revision 1.8 1995/09/14 07:48:22 adam
254 * Record control management.
256 * Revision 1.7 1995/09/11 13:09:32 adam
257 * More work on relevance feedback.
259 * Revision 1.6 1995/09/08 14:52:27 adam
260 * Minor changes. Dictionary is lower case now.
262 * Revision 1.5 1995/09/06 16:11:16 adam
263 * Option: only one word key per file.
265 * Revision 1.4 1995/09/05 15:28:39 adam
266 * More work on search engine.
268 * Revision 1.3 1995/09/04 12:33:41 adam
269 * Various cleanup. YAZ util used instead.
271 * Revision 1.2 1995/09/04 09:10:34 adam
272 * More work on index add/del/update.
273 * Merge sort implemented.
274 * Initial work on z39 server.
276 * Revision 1.1 1995/09/01 14:06:35 adam
277 * Split of work into more files.
294 static Dict matchDict;
296 static Records records = NULL;
298 static char **key_buf;
299 static size_t ptr_top;
301 static size_t key_buf_used;
302 static int key_file_no;
304 static int records_inserted = 0;
305 static int records_updated = 0;
306 static int records_deleted = 0;
307 static int records_processed = 0;
309 static ZebTargetInfo *zti = NULL;
311 static void logRecord (int showFlag)
315 if (showFlag || !(records_processed % 1000))
317 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
318 records_processed, records_inserted, records_updated,
323 void key_open (BFiles bfs, int mem)
326 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
329 key_buf = xmalloc (mem);
330 ptr_top = mem/sizeof(char*);
336 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, 1)))
338 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
342 records = rec_open (bfs, 1);
344 zti = zebTargetInfo_open (records, 1);
354 void encode_key_init (struct encode_info *i)
360 char *encode_key_int (int d, char *bp)
369 else if (d <= 4194303)
371 *bp++ = 128 + (d>>16);
372 *bp++ = (d>>8) & 255;
377 *bp++ = 192 + (d>>24);
378 *bp++ = (d>>16) & 255;
379 *bp++ = (d>>8) & 255;
385 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
390 while ((*bp++ = *k++))
392 memcpy (&key, k+1, sizeof(struct it_key));
393 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
394 if (i->sysno != key.sysno)
396 i->sysno = key.sysno;
399 bp = encode_key_int (key.seqno - i->seqno, bp);
400 i->seqno = key.seqno;
401 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
403 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
411 static int key_y_len;
413 static int key_y_compare (const void *p1, const void *p2)
417 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
418 *(char**) p2 + key_y_len + 1)))
420 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
423 static int key_x_compare (const void *p1, const void *p2)
425 return strcmp (*(char**) p1, *(char**) p2);
429 void key_flush (void)
434 struct encode_info encode_info;
443 logf (LOG_LOG, "sorting section %d", key_file_no);
445 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
446 getFnameTmp (out_fname, key_file_no);
448 if (!(outf = fopen (out_fname, "w")))
450 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
453 logf (LOG_LOG, "writing section %d", key_file_no);
454 prevcp = cp = key_buf[ptr_top-ptr_i];
456 encode_key_init (&encode_info);
457 encode_key_write (cp, &encode_info, outf);
460 cp = key_buf[ptr_top-ptr_i];
461 if (strcmp (cp, prevcp))
463 encode_key_init (&encode_info);
464 encode_key_write (cp, &encode_info, outf);
468 encode_key_write (cp + strlen(cp), &encode_info, outf);
471 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
472 getFnameTmp (out_fname, key_file_no);
474 if (!(outf = fopen (out_fname, "w")))
476 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
479 logf (LOG_LOG, "writing section %d", key_file_no);
481 prevcp = key_buf[ptr_top-i];
483 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
485 key_y_len = strlen(prevcp)+1;
487 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
488 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
490 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
491 sizeof(char*), key_y_compare);
492 cp = key_buf[ptr_top-ptr_i];
494 encode_key_init (&encode_info);
495 encode_key_write (cp, &encode_info, outf);
498 cp = key_buf[ptr_top-ptr_i];
499 encode_key_write (cp+key_y_len, &encode_info, outf);
503 prevcp = key_buf[ptr_top-ptr_i];
508 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
511 logf (LOG_LOG, "finished section %d", key_file_no);
521 zebTargetInfo_close (zti, 1);
523 rec_close (&records);
524 dict_close (matchDict);
530 static void wordInit (RecWord *p)
546 static void addRecordKey (const RecWord *p)
555 if (reckeys.buf_used+1024 > reckeys.buf_max)
559 b = xmalloc (reckeys.buf_max += 128000);
560 if (reckeys.buf_used > 0)
561 memcpy (b, reckeys.buf, reckeys.buf_used);
565 dst = reckeys.buf + reckeys.buf_used;
567 attrSet = p->attrSet;
568 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
571 reckeys.prevAttrSet = attrSet;
572 attrUse = p->attrUse;
573 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
576 reckeys.prevAttrUse = attrUse;
578 diff = 1 + p->seqno - reckeys.prevSeqNo;
579 if (diff >= 1 && diff <= 15)
584 reckeys.prevSeqNo = p->seqno;
590 memcpy (dst, &attrSet, sizeof(attrSet));
591 dst += sizeof(attrSet);
595 memcpy (dst, &attrUse, sizeof(attrUse));
596 dst += sizeof(attrUse);
598 *dst++ = p->reg_type;
599 for (i = 0; p->string[i] && i < IT_MAX_WORD-3; i++)
600 *dst++ = p->string[i];
605 memcpy (dst, &p->seqno, sizeof(p->seqno));
606 dst += sizeof(p->seqno);
608 reckeys.buf_used = dst - reckeys.buf;
611 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
612 const char *databaseName)
619 if (zebTargetInfo_curDatabase (zti, databaseName))
621 if (zebTargetInfo_newDatabase (zti, databaseName))
624 while (off < reckeys->buf_used)
626 const char *src = reckeys->buf + off;
634 memcpy (&attrSet, src, sizeof(attrSet));
635 src += sizeof(attrSet);
639 memcpy (&attrUse, src, sizeof(attrUse));
640 src += sizeof(attrUse);
642 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
645 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
647 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
649 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
651 ((char*) key_buf) [key_buf_used++] = ch;
653 ((char*)key_buf) [key_buf_used++] = *src++;
655 ((char*)key_buf) [key_buf_used++] = '\0';
656 ((char*) key_buf)[key_buf_used++] = cmd;
659 seqno += ((lead>>2) & 15)-1;
662 memcpy (&seqno, src, sizeof(seqno));
663 src += sizeof(seqno);
667 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
668 key_buf_used += sizeof(key);
669 off = src - reckeys->buf;
671 assert (off == reckeys->buf_used);
674 static const char **searchRecordKey (struct recKeys *reckeys,
675 int attrSetS, int attrUseS)
677 static const char *ws[32];
685 for (i = 0; i<32; i++)
688 while (off < reckeys->buf_used)
691 const char *src = reckeys->buf + off;
699 memcpy (&attrSet, src, sizeof(attrSet));
700 src += sizeof(attrSet);
704 memcpy (&attrUse, src, sizeof(attrUse));
705 src += sizeof(attrUse);
711 seqno += ((lead>>2) & 15)-1;
714 memcpy (&seqno, src, sizeof(seqno));
715 src += sizeof(seqno);
717 if (attrUseS == attrUse && attrSetS == attrSet)
724 woff = seqno - startSeq;
725 if (woff >= 0 && woff < 31)
729 off = src - reckeys->buf;
731 assert (off == reckeys->buf_used);
735 struct file_read_info {
743 static struct file_read_info *file_read_start (int fd)
745 struct file_read_info *fi = xmalloc (sizeof(*fi));
749 fi->file_moffset = 0;
753 static void file_read_stop (struct file_read_info *fi)
759 static off_t file_seek (void *handle, off_t offset)
761 struct file_read_info *p = handle;
762 p->file_offset = offset;
763 return lseek (p->fd, offset, SEEK_SET);
766 static off_t file_tell (void *handle)
768 struct file_read_info *p = handle;
769 return p->file_offset;
772 static int file_read (void *handle, char *buf, size_t count)
774 struct file_read_info *p = handle;
777 r = read (fd, buf, count);
781 if (p->file_offset > p->file_max)
782 p->file_max = p->file_offset;
787 static void file_begin (void *handle)
789 struct file_read_info *p = handle;
791 p->file_offset = p->file_moffset;
793 lseek (p->fd, p->file_moffset, SEEK_SET);
797 static void file_end (void *handle, off_t offset)
799 struct file_read_info *p = handle;
801 assert (p->file_more == 0);
803 p->file_moffset = offset;
806 static int atois (const char **s)
809 while ( (c=**s) >= '0' && c <= '9')
811 val = val*10 + c - '0';
817 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
821 static char dstBuf[2048];
823 const char *s = spec;
824 static const char **w;
829 while (*s == ' ' || *s == '\t')
836 int attrSet, attrUse;
840 attrSet = atois (&s);
843 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
844 spec, rGroup->groupName ? rGroup->groupName : "none");
848 attrUse = atois (&s);
849 w = searchRecordKey (reckeys, attrSet, attrUse);
854 for (i = 0; i<32; i++)
859 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
860 spec, rGroup->groupName ? rGroup->groupName : "none");
865 for (i = 0; i<32; i++)
866 if (matchFlag[i] && w[i])
878 logf (LOG_WARN, "Record didn't contain match"
879 " fields in (%d,%d)", attrSet, attrUse);
887 const char *spec_src = NULL;
888 const char *s1 = ++s;
889 while (*s1 && *s1 != ' ' && *s1 != '\t')
895 memcpy (special, s, spec_len);
896 special[spec_len] = '\0';
899 if (!strcmp (special, "group"))
900 spec_src = rGroup->groupName;
901 else if (!strcmp (special, "database"))
902 spec_src = rGroup->databaseName;
903 else if (!strcmp (special, "filename"))
905 else if (!strcmp (special, "type"))
906 spec_src = rGroup->recordType;
911 strcpy (dst, spec_src);
912 dst += strlen (spec_src);
915 else if (*s == '\"' || *s == '\'')
917 int stopMarker = *s++;
921 while (*s && *s != stopMarker)
924 tmpString[i++] = *s++;
929 strcpy (dst, tmpString);
930 dst += strlen (tmpString);
934 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
935 spec, rGroup->groupName ? rGroup->groupName : "none");
942 logf (LOG_WARN, "No match criteria for record %s in group %s",
943 fname, rGroup->groupName ? rGroup->groupName : "none");
950 struct recordLogInfo {
953 struct recordGroup *rGroup;
956 static void recordLogPreamble (int level, const char *msg, void *info)
958 struct recordLogInfo *p = info;
959 FILE *outf = log_file ();
963 fprintf (outf, "File %s, offset %d, type %s\n",
964 p->rGroup->recordType, p->recordOffset, p->fname);
965 log_event_start (NULL, NULL);
968 static int recordExtract (SYSNO *sysno, const char *fname,
969 struct recordGroup *rGroup, int deleteFlag,
970 struct file_read_info *fi, RecType recType,
973 struct recExtractCtrl extractCtrl;
977 off_t recordOffset = 0;
979 struct recordLogInfo logInfo;
983 /* we are going to read from a file, so prepare the extraction */
985 extractCtrl.subType = subType;
986 extractCtrl.init = wordInit;
987 extractCtrl.add = addRecordKey;
988 extractCtrl.dh = rGroup->dh;
990 reckeys.buf_used = 0;
991 reckeys.prevAttrUse = -1;
992 reckeys.prevAttrSet = -1;
993 reckeys.prevSeqNo = 0;
995 recordOffset = fi->file_moffset;
996 extractCtrl.offset = recordOffset;
997 extractCtrl.readf = file_read;
998 extractCtrl.seekf = file_seek;
999 extractCtrl.tellf = file_tell;
1000 extractCtrl.endf = file_end;
1001 extractCtrl.zebra_maps = rGroup->zebra_maps;
1002 extractCtrl.flagShowRecords = rGroup->flagShowRecords;
1003 if (rGroup->flagShowRecords)
1004 printf ("File: %s %ld\n", fname, (long) recordOffset);
1006 logInfo.fname = fname;
1007 logInfo.recordOffset = recordOffset;
1008 logInfo.rGroup = rGroup;
1009 log_event_start (recordLogPreamble, &logInfo);
1011 r = (*recType->extract)(&extractCtrl);
1013 log_event_start (NULL, NULL);
1017 /* error occured during extraction ... */
1018 if (!rGroup->flagShowRecords &&
1019 records_processed < rGroup->fileVerboseLimit)
1021 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1022 fname, (long) recordOffset, r);
1026 if (reckeys.buf_used == 0)
1028 /* the extraction process returned no information - the record
1029 is probably empty - unless flagShowRecords is in use */
1030 if (rGroup->flagShowRecords)
1032 logf (LOG_WARN, "No keys generated for file %s", fname);
1033 logf (LOG_WARN, " The file is probably empty");
1038 /* perform match if sysno not known and if match criteria is specified */
1045 if (rGroup->recordId && *rGroup->recordId)
1049 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1053 rinfo = dict_lookup (matchDict, matchStr);
1055 memcpy (sysno, rinfo+1, sizeof(*sysno));
1059 logf (LOG_WARN, "Bad match criteria");
1070 logf (LOG_LOG, "Cannot delete new record");
1073 if (records_processed < rGroup->fileVerboseLimit)
1074 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1075 fname, (long) recordOffset);
1076 rec = rec_new (records);
1077 *sysno = rec->sysno;
1081 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1083 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1089 /* record already exists */
1090 struct recKeys delkeys;
1092 rec = rec_get (records, *sysno);
1094 delkeys.buf_used = rec->size[recInfo_delKeys];
1095 delkeys.buf = rec->info[recInfo_delKeys];
1096 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1099 /* record going to be deleted */
1100 if (!delkeys.buf_used)
1102 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1103 fname, (long) recordOffset);
1104 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1108 if (records_processed < rGroup->fileVerboseLimit)
1109 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1110 fname, (long) recordOffset);
1113 dict_delete (matchDict, matchStr);
1114 rec_del (records, &rec);
1121 /* record going to be updated */
1122 if (!delkeys.buf_used)
1124 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1125 fname, (long) recordOffset);
1126 logf (LOG_WARN, "cannot update file above, storeKeys false");
1130 if (records_processed < rGroup->fileVerboseLimit)
1131 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1132 fname, (long) recordOffset);
1133 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1138 /* update file type */
1139 xfree (rec->info[recInfo_fileType]);
1140 rec->info[recInfo_fileType] =
1141 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1143 /* update filename */
1144 xfree (rec->info[recInfo_filename]);
1145 rec->info[recInfo_filename] =
1146 rec_strdup (fname, &rec->size[recInfo_filename]);
1148 /* update delete keys */
1149 xfree (rec->info[recInfo_delKeys]);
1150 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1153 rec->size[recInfo_delKeys] = reckeys.buf_used;
1154 rec->info[recInfo_delKeys] = reckeys.buf;
1156 reckeys.buf_max = 0;
1158 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1159 rec->size[recInfo_delKeys] = reckeys.buf_used;
1160 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1161 rec->size[recInfo_delKeys]);
1166 rec->info[recInfo_delKeys] = NULL;
1167 rec->size[recInfo_delKeys] = 0;
1170 /* update store data */
1171 xfree (rec->info[recInfo_storeData]);
1172 if (rGroup->flagStoreData == 1)
1174 int size = fi->file_moffset - recordOffset;
1176 size = fi->file_max - recordOffset;
1177 rec->size[recInfo_storeData] = size;
1178 rec->info[recInfo_storeData] = xmalloc (size);
1179 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1181 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1182 (long) recordOffset);
1185 if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1187 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1188 fi->file_max, fname);
1194 rec->info[recInfo_storeData] = NULL;
1195 rec->size[recInfo_storeData] = 0;
1197 /* update database name */
1198 xfree (rec->info[recInfo_databaseName]);
1199 rec->info[recInfo_databaseName] =
1200 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1203 xfree (rec->info[recInfo_offset]);
1205 rec->size[recInfo_offset] = sizeof(recordOffset);
1206 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1207 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1209 /* commit this record */
1210 rec_put (records, &rec);
1215 int fileExtract (SYSNO *sysno, const char *fname,
1216 const struct recordGroup *rGroupP, int deleteFlag)
1224 struct recordGroup rGroupM;
1225 struct recordGroup *rGroup = &rGroupM;
1226 struct file_read_info *fi;
1228 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1230 if (!rGroup->groupName || !*rGroup->groupName)
1233 sprintf (gprefix, "%s.", rGroup->groupName);
1235 logf (LOG_DEBUG, "fileExtract %s", fname);
1237 /* determine file extension */
1238 for (i = strlen(fname); --i >= 0; )
1239 if (fname[i] == '/')
1244 else if (fname[i] == '.')
1246 strcpy (ext, fname+i+1);
1249 /* determine file type - depending on extension */
1250 if (!rGroup->recordType)
1252 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1253 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1255 sprintf (ext_res, "%srecordType", gprefix);
1256 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1258 if (records_processed < rGroup->fileVerboseLimit)
1259 logf (LOG_LOG, "? %s", fname);
1264 if (!rGroup->recordType)
1266 if (records_processed < rGroup->fileVerboseLimit)
1267 logf (LOG_LOG, "? record %s", fname);
1270 if (!(recType = recType_byName (rGroup->recordType, subType)))
1272 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1276 /* determine match criteria */
1277 if (!rGroup->recordId)
1279 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1280 rGroup->recordId = res_get (common_resource, ext_res);
1283 /* determine database name */
1284 if (!rGroup->databaseName)
1286 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1287 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1289 sprintf (ext_res, "%sdatabase", gprefix);
1290 rGroup->databaseName = res_get (common_resource, ext_res);
1293 if (!rGroup->databaseName)
1294 rGroup->databaseName = "Default";
1296 if (rGroup->flagStoreData == -1)
1299 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1300 if (!(sval = res_get (common_resource, ext_res)))
1302 sprintf (ext_res, "%sstoreData", gprefix);
1303 sval = res_get (common_resource, ext_res);
1306 rGroup->flagStoreData = atoi (sval);
1308 if (rGroup->flagStoreData == -1)
1309 rGroup->flagStoreData = 0;
1311 if (rGroup->flagStoreKeys == -1)
1315 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1316 if (!(sval = res_get (common_resource, ext_res)))
1318 sprintf (ext_res, "%sstoreKeys", gprefix);
1319 sval = res_get (common_resource, ext_res);
1322 rGroup->flagStoreKeys = atoi (sval);
1324 if (rGroup->flagStoreKeys == -1)
1325 rGroup->flagStoreKeys = 0;
1327 if (sysno && deleteFlag)
1331 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1333 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1337 fi = file_read_start (fd);
1341 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1343 } while (r && !sysno && fi->file_more);
1344 file_read_stop (fi);