2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.72 1997-07-15 16:32:29 adam
8 * Bug fix: Match handler didn't terminate the resulting string!
10 * Revision 1.71 1997/07/15 16:28:41 adam
11 * Bug fix: storeData didn't work with files with multiple records.
12 * Bug fix: fixed memory management with records; not really well
15 * Revision 1.70 1997/07/01 13:00:42 adam
16 * Bug fix in routine searchRecordKey: uninitialized variables.
18 * Revision 1.69 1997/04/29 09:26:03 adam
19 * Bug fix: generic recordId handling didn't work for compressed internal
22 * Revision 1.68 1997/02/12 20:39:45 adam
23 * Implemented options -f <n> that limits the log to the first <n>
25 * Changed some log messages also.
27 * Revision 1.67 1996/11/15 15:02:14 adam
28 * Minor changes regarding logging.
30 * Revision 1.66 1996/11/14 09:52:21 adam
31 * Strings in record keys bound by IT_MAX_WORD.
33 * Revision 1.65 1996/11/14 08:57:56 adam
34 * Reduction of storeKeys area.
36 * Revision 1.64 1996/11/08 11:10:16 adam
37 * Buffers used during file match got bigger.
38 * Compressed ISAM support everywhere.
39 * Bug fixes regarding masking characters in queries.
40 * Redesigned Regexp-2 queries.
42 * Revision 1.63 1996/10/29 14:09:39 adam
43 * Use of cisam system - enabled if setting isamc is 1.
45 * Revision 1.62 1996/10/11 10:57:01 adam
46 * New module recctrl. Used to manage records (extract/retrieval).
47 * Several files have been moved to the recctrl sub directory.
49 * Revision 1.61 1996/06/06 12:08:37 quinn
50 * Added showRecord function
52 * Revision 1.60 1996/06/04 10:18:12 adam
53 * Search/scan uses character mapping module.
55 * Revision 1.59 1996/05/14 15:47:07 adam
56 * Cleanup of various buffer size entities.
58 * Revision 1.58 1996/05/14 06:16:38 adam
59 * Compact use/set bytes used in search service.
61 * Revision 1.57 1996/05/13 14:23:04 adam
62 * Work on compaction of set/use bytes in dictionary.
64 * Revision 1.56 1996/05/09 09:54:42 adam
65 * Server supports maps from one logical attributes to a list of physical
67 * The extraction process doesn't make space consuming 'any' keys.
69 * Revision 1.55 1996/05/09 07:28:55 quinn
70 * Work towards phrases and multiple registers
72 * Revision 1.54 1996/05/01 13:46:35 adam
73 * First work on multiple records in one file.
74 * New option, -offset, to the "unread" command in the filter module.
76 * Revision 1.53 1996/04/26 12:09:43 adam
77 * Added a few comments.
79 * Revision 1.52 1996/04/25 13:27:57 adam
80 * Function recordExtract modified so that files with no keys (possibly empty)
83 * Revision 1.51 1996/03/19 11:08:42 adam
84 * Bug fix: Log preamble wasn't always turned off after recordExtract.
86 * Revision 1.50 1996/02/12 18:45:36 adam
87 * New fileVerboseFlag in record group control.
89 * Revision 1.49 1996/02/05 12:29:57 adam
90 * Logging reduced a bit.
91 * The remaining running time is estimated during register merge.
93 * Revision 1.48 1996/02/01 20:53:26 adam
94 * The temporary per-record keys are compacted a little, and duplication
95 * of the per-records keys are avoided when they are saved in the record
98 * Revision 1.47 1996/01/17 14:57:48 adam
99 * Prototype changed for reader functions in extract/retrieve. File
100 * is identified by 'void *' instead of 'int.
102 * Revision 1.46 1995/12/15 14:57:16 adam
105 * Revision 1.45 1995/12/15 12:37:41 adam
106 * In addRecordKeyAny: Writes key only when attrSet != -1.
108 * Revision 1.44 1995/12/12 16:00:54 adam
109 * System call sync(2) used after update/commit.
110 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
113 * Revision 1.43 1995/12/11 09:12:46 adam
114 * The rec_get function returns NULL if record doesn't exist - will
115 * happen in the server if the result set records have been deleted since
116 * the creation of the set (i.e. the search).
117 * The server saves a result temporarily if it is 'volatile', i.e. the
118 * set is register dependent.
120 * Revision 1.42 1995/12/07 17:38:46 adam
121 * Work locking mechanisms for concurrent updates/commit.
123 * Revision 1.41 1995/12/06 16:06:42 adam
124 * Better diagnostics. Work on 'real' dictionary deletion.
126 * Revision 1.40 1995/12/05 16:57:40 adam
127 * More work on regular patterns.
129 * Revision 1.39 1995/12/05 13:20:18 adam
130 * Bug fix: file_read sometimes returned early EOF.
132 * Revision 1.38 1995/12/04 17:59:21 adam
133 * More work on regular expression conversion.
135 * Revision 1.37 1995/12/04 14:22:27 adam
136 * Extra arg to recType_byName.
137 * Started work on new regular expression parsed input to
138 * structured records.
140 * Revision 1.36 1995/11/30 08:34:29 adam
141 * Started work on commit facility.
142 * Changed a few malloc/free to xmalloc/xfree.
144 * Revision 1.35 1995/11/28 14:26:21 adam
145 * Bug fix: recordId with constant wasn't right.
146 * Bug fix: recordId dictionary entry wasn't deleted when needed.
148 * Revision 1.34 1995/11/28 09:09:38 adam
149 * Zebra config renamed.
150 * Use setting 'recordId' to identify record now.
151 * Bug fix in recindex.c: rec_release_blocks was invokeded even
152 * though the blocks were already released.
153 * File traversal properly deletes records when needed.
155 * Revision 1.33 1995/11/27 09:56:20 adam
156 * Record info elements better enumerated. Internal store of records.
158 * Revision 1.32 1995/11/25 10:24:05 adam
159 * More record fields - they are enumerated now.
160 * New options: flagStoreData flagStoreKey.
162 * Revision 1.31 1995/11/24 11:31:35 adam
163 * Commands add & del read filenames from stdin if source directory is
165 * Match criteria supports 'constant' strings.
167 * Revision 1.30 1995/11/22 17:19:16 adam
168 * Record management uses the bfile system.
170 * Revision 1.29 1995/11/21 15:01:14 adam
171 * New general match criteria implemented.
172 * New feature: document groups.
174 * Revision 1.28 1995/11/21 09:20:30 adam
175 * Yet more work on record match.
177 * Revision 1.27 1995/11/20 16:59:45 adam
178 * New update method: the 'old' keys are saved for each records.
180 * Revision 1.26 1995/11/20 11:56:24 adam
181 * Work on new traversal.
183 * Revision 1.25 1995/11/16 15:34:54 adam
184 * Uses new record management system in both indexer and server.
186 * Revision 1.24 1995/11/15 19:13:08 adam
187 * Work on record management.
189 * Revision 1.23 1995/10/27 14:00:10 adam
190 * Implemented detection of database availability.
192 * Revision 1.22 1995/10/17 18:02:07 adam
193 * New feature: databases. Implemented as prefix to words in dictionary.
195 * Revision 1.21 1995/10/10 12:24:38 adam
196 * Temporary sort files are compressed.
198 * Revision 1.20 1995/10/06 13:52:05 adam
199 * Bug fixes. Handler may abort further scanning.
201 * Revision 1.19 1995/10/04 12:55:16 adam
202 * Bug fix in ranked search. Use=Any keys inserted.
204 * Revision 1.18 1995/10/04 09:37:08 quinn
207 * Revision 1.17 1995/10/03 14:28:57 adam
208 * Buffered read in extract works.
210 * Revision 1.16 1995/10/03 14:28:45 adam
211 * Work on more effecient read handler in extract.
213 * Revision 1.15 1995/10/02 15:42:53 adam
214 * Extract uses file descriptors instead of FILE pointers.
216 * Revision 1.14 1995/10/02 15:29:13 adam
217 * More logging in file_extract.
219 * Revision 1.13 1995/09/29 14:01:39 adam
222 * Revision 1.12 1995/09/28 14:22:56 adam
223 * Sort uses smaller temporary files.
225 * Revision 1.11 1995/09/28 12:10:31 adam
226 * Bug fixes. Field prefix used in queries.
228 * Revision 1.10 1995/09/28 09:19:41 adam
229 * xfree/xmalloc used everywhere.
230 * Extract/retrieve method seems to work for text records.
232 * Revision 1.9 1995/09/27 12:22:28 adam
233 * More work on extract in record control.
234 * Field name is not in isam keys but in prefix in dictionary words.
236 * Revision 1.8 1995/09/14 07:48:22 adam
237 * Record control management.
239 * Revision 1.7 1995/09/11 13:09:32 adam
240 * More work on relevance feedback.
242 * Revision 1.6 1995/09/08 14:52:27 adam
243 * Minor changes. Dictionary is lower case now.
245 * Revision 1.5 1995/09/06 16:11:16 adam
246 * Option: only one word key per file.
248 * Revision 1.4 1995/09/05 15:28:39 adam
249 * More work on search engine.
251 * Revision 1.3 1995/09/04 12:33:41 adam
252 * Various cleanup. YAZ util used instead.
254 * Revision 1.2 1995/09/04 09:10:34 adam
255 * More work on index add/del/update.
256 * Merge sort implemented.
257 * Initial work on z39 server.
259 * Revision 1.1 1995/09/01 14:06:35 adam
260 * Split of work into more files.
273 static Dict matchDict;
275 static Records records = NULL;
277 static char **key_buf;
278 static size_t ptr_top;
280 static size_t key_buf_used;
281 static int key_file_no;
283 static int records_inserted = 0;
284 static int records_updated = 0;
285 static int records_deleted = 0;
286 static int records_processed = 0;
288 static ZebTargetInfo *zti = NULL;
290 static void logRecord (int showFlag)
294 if (showFlag || !(records_processed % 1000))
296 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
297 records_processed, records_inserted, records_updated,
302 void key_open (int mem)
305 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
308 key_buf = xmalloc (mem);
309 ptr_top = mem/sizeof(char*);
315 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
317 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
321 records = rec_open (1);
323 zti = zebTargetInfo_open (records, 1);
333 void encode_key_init (struct encode_info *i)
339 char *encode_key_int (int d, char *bp)
348 else if (d <= 4194303)
350 *bp++ = 128 + (d>>16);
351 *bp++ = (d>>8) & 255;
356 *bp++ = 192 + (d>>24);
357 *bp++ = (d>>16) & 255;
358 *bp++ = (d>>8) & 255;
364 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
369 while ((*bp++ = *k++))
371 memcpy (&key, k+1, sizeof(struct it_key));
372 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
373 if (i->sysno != key.sysno)
375 i->sysno = key.sysno;
378 bp = encode_key_int (key.seqno - i->seqno, bp);
379 i->seqno = key.seqno;
380 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
382 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
390 static int key_y_len;
392 static int key_y_compare (const void *p1, const void *p2)
396 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
397 *(char**) p2 + key_y_len + 1)))
399 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
402 static int key_x_compare (const void *p1, const void *p2)
404 return strcmp (*(char**) p1, *(char**) p2);
408 void key_flush (void)
413 struct encode_info encode_info;
422 logf (LOG_LOG, "sorting section %d", key_file_no);
424 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
425 getFnameTmp (out_fname, key_file_no);
427 if (!(outf = fopen (out_fname, "w")))
429 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
432 logf (LOG_LOG, "writing section %d", key_file_no);
433 prevcp = cp = key_buf[ptr_top-ptr_i];
435 encode_key_init (&encode_info);
436 encode_key_write (cp, &encode_info, outf);
439 cp = key_buf[ptr_top-ptr_i];
440 if (strcmp (cp, prevcp))
442 encode_key_init (&encode_info);
443 encode_key_write (cp, &encode_info, outf);
447 encode_key_write (cp + strlen(cp), &encode_info, outf);
450 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
451 getFnameTmp (out_fname, key_file_no);
453 if (!(outf = fopen (out_fname, "w")))
455 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
458 logf (LOG_LOG, "writing section %d", key_file_no);
460 prevcp = key_buf[ptr_top-i];
462 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
464 key_y_len = strlen(prevcp)+1;
466 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
467 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
469 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
470 sizeof(char*), key_y_compare);
471 cp = key_buf[ptr_top-ptr_i];
473 encode_key_init (&encode_info);
474 encode_key_write (cp, &encode_info, outf);
477 cp = key_buf[ptr_top-ptr_i];
478 encode_key_write (cp+key_y_len, &encode_info, outf);
482 prevcp = key_buf[ptr_top-ptr_i];
487 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
490 logf (LOG_LOG, "finished section %d", key_file_no);
500 zebTargetInfo_close (zti, 1);
502 rec_close (&records);
503 dict_close (matchDict);
509 static void wordInit (RecWord *p)
513 p->which = Word_String;
525 static void addRecordKey (const RecWord *p)
534 if (reckeys.buf_used+1024 > reckeys.buf_max)
538 b = xmalloc (reckeys.buf_max += 128000);
539 if (reckeys.buf_used > 0)
540 memcpy (b, reckeys.buf, reckeys.buf_used);
544 dst = reckeys.buf + reckeys.buf_used;
546 attrSet = p->attrSet;
547 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
550 reckeys.prevAttrSet = attrSet;
551 attrUse = p->attrUse;
552 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
555 reckeys.prevAttrUse = attrUse;
557 diff = 1 + p->seqno - reckeys.prevSeqNo;
558 if (diff >= 1 && diff <= 15)
563 reckeys.prevSeqNo = p->seqno;
569 memcpy (dst, &attrSet, sizeof(attrSet));
570 dst += sizeof(attrSet);
574 memcpy (dst, &attrUse, sizeof(attrUse));
575 dst += sizeof(attrUse);
588 for (i = 0; p->u.string[i] && i < IT_MAX_WORD-3; i++)
589 *dst++ = p->u.string[i];
594 memcpy (dst, &p->seqno, sizeof(p->seqno));
595 dst += sizeof(p->seqno);
597 reckeys.buf_used = dst - reckeys.buf;
600 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
601 const char *databaseName)
608 if (zebTargetInfo_curDatabase (zti, databaseName))
610 if (zebTargetInfo_newDatabase (zti, databaseName))
613 while (off < reckeys->buf_used)
615 const char *src = reckeys->buf + off;
623 memcpy (&attrSet, src, sizeof(attrSet));
624 src += sizeof(attrSet);
628 memcpy (&attrUse, src, sizeof(attrUse));
629 src += sizeof(attrUse);
631 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
634 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
636 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
638 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
640 ((char*) key_buf) [key_buf_used++] = ch;
642 ((char*)key_buf) [key_buf_used++] = *src++;
644 ((char*)key_buf) [key_buf_used++] = '\0';
645 ((char*) key_buf)[key_buf_used++] = cmd;
648 seqno += ((lead>>2) & 15)-1;
651 memcpy (&seqno, src, sizeof(seqno));
652 src += sizeof(seqno);
656 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
657 key_buf_used += sizeof(key);
658 off = src - reckeys->buf;
660 assert (off == reckeys->buf_used);
663 static const char **searchRecordKey (struct recKeys *reckeys,
664 int attrSetS, int attrUseS)
666 static const char *ws[32];
674 for (i = 0; i<32; i++)
677 while (off < reckeys->buf_used)
680 const char *src = reckeys->buf + off;
688 memcpy (&attrSet, src, sizeof(attrSet));
689 src += sizeof(attrSet);
693 memcpy (&attrUse, src, sizeof(attrUse));
694 src += sizeof(attrUse);
700 seqno += ((lead>>2) & 15)-1;
703 memcpy (&seqno, src, sizeof(seqno));
704 src += sizeof(seqno);
706 if (attrUseS == attrUse && attrSetS == attrSet)
713 woff = seqno - startSeq;
714 if (woff >= 0 && woff < 31)
718 off = src - reckeys->buf;
720 assert (off == reckeys->buf_used);
724 struct file_read_info {
732 static struct file_read_info *file_read_start (int fd)
734 struct file_read_info *fi = xmalloc (sizeof(*fi));
738 fi->file_moffset = 0;
742 static void file_read_stop (struct file_read_info *fi)
748 static off_t file_seek (void *handle, off_t offset)
750 struct file_read_info *p = handle;
751 p->file_offset = offset;
752 return lseek (p->fd, offset, SEEK_SET);
755 static int file_read (void *handle, char *buf, size_t count)
757 struct file_read_info *p = handle;
760 r = read (fd, buf, count);
764 if (p->file_offset > p->file_max)
765 p->file_max = p->file_offset;
770 static void file_begin (void *handle)
772 struct file_read_info *p = handle;
774 p->file_offset = p->file_moffset;
776 lseek (p->fd, p->file_moffset, SEEK_SET);
780 static void file_end (void *handle, off_t offset)
782 struct file_read_info *p = handle;
784 assert (p->file_more == 0);
786 p->file_moffset = offset;
789 static int atois (const char **s)
792 while ( (c=**s) >= '0' && c <= '9')
794 val = val*10 + c - '0';
800 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
804 static char dstBuf[2048];
806 const char *s = spec;
807 static const char **w;
812 while (*s == ' ' || *s == '\t')
819 int attrSet, attrUse;
823 attrSet = atois (&s);
826 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
827 spec, rGroup->groupName ? rGroup->groupName : "none");
831 attrUse = atois (&s);
832 w = searchRecordKey (reckeys, attrSet, attrUse);
837 for (i = 0; i<32; i++)
842 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
843 spec, rGroup->groupName ? rGroup->groupName : "none");
848 for (i = 0; i<32; i++)
849 if (matchFlag[i] && w[i])
861 logf (LOG_WARN, "Record didn't contain match"
862 " fields in (%d,%d)", attrSet, attrUse);
870 const char *spec_src = NULL;
871 const char *s1 = ++s;
872 while (*s1 && *s1 != ' ' && *s1 != '\t')
878 memcpy (special, s, spec_len);
879 special[spec_len] = '\0';
882 if (!strcmp (special, "group"))
883 spec_src = rGroup->groupName;
884 else if (!strcmp (special, "database"))
885 spec_src = rGroup->databaseName;
886 else if (!strcmp (special, "filename"))
888 else if (!strcmp (special, "type"))
889 spec_src = rGroup->recordType;
894 strcpy (dst, spec_src);
895 dst += strlen (spec_src);
898 else if (*s == '\"' || *s == '\'')
900 int stopMarker = *s++;
904 while (*s && *s != stopMarker)
907 tmpString[i++] = *s++;
912 strcpy (dst, tmpString);
913 dst += strlen (tmpString);
917 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
918 spec, rGroup->groupName ? rGroup->groupName : "none");
925 logf (LOG_WARN, "No match criteria for record %s in group %s",
926 fname, rGroup->groupName ? rGroup->groupName : "none");
933 struct recordLogInfo {
936 struct recordGroup *rGroup;
939 static void recordLogPreamble (int level, const char *msg, void *info)
941 struct recordLogInfo *p = info;
942 FILE *outf = log_file ();
946 fprintf (outf, "File %s, offset %d, type %s\n",
947 p->rGroup->recordType, p->recordOffset, p->fname);
948 log_event_start (NULL, NULL);
951 static int recordExtract (SYSNO *sysno, const char *fname,
952 struct recordGroup *rGroup, int deleteFlag,
953 struct file_read_info *fi, RecType recType,
956 struct recExtractCtrl extractCtrl;
960 off_t recordOffset = 0;
962 struct recordLogInfo logInfo;
966 /* we are going to read from a file, so prepare the extraction */
968 extractCtrl.subType = subType;
969 extractCtrl.init = wordInit;
970 extractCtrl.add = addRecordKey;
972 reckeys.buf_used = 0;
973 reckeys.prevAttrUse = -1;
974 reckeys.prevAttrSet = -1;
975 reckeys.prevSeqNo = 0;
977 recordOffset = fi->file_moffset;
978 extractCtrl.offset = recordOffset;
979 extractCtrl.readf = file_read;
980 extractCtrl.seekf = file_seek;
981 extractCtrl.endf = file_end;
982 extractCtrl.map_chrs_input = map_chrs_input;
983 extractCtrl.flagShowRecords = rGroup->flagShowRecords;
984 if (rGroup->flagShowRecords)
985 printf ("File: %s %ld\n", fname, (long) recordOffset);
987 logInfo.fname = fname;
988 logInfo.recordOffset = recordOffset;
989 logInfo.rGroup = rGroup;
990 log_event_start (recordLogPreamble, &logInfo);
992 r = (*recType->extract)(&extractCtrl);
994 log_event_start (NULL, NULL);
998 /* error occured during extraction ... */
999 if (!rGroup->flagShowRecords &&
1000 records_processed < rGroup->fileVerboseLimit)
1002 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1003 fname, (long) recordOffset, r);
1007 if (reckeys.buf_used == 0)
1009 /* the extraction process returned no information - the record
1010 is probably empty - unless flagShowRecords is in use */
1011 if (rGroup->flagShowRecords)
1013 logf (LOG_WARN, "No keys generated for file %s", fname);
1014 logf (LOG_WARN, " The file is probably empty");
1019 /* perform match if sysno not known and if match criteria is specified */
1026 if (rGroup->recordId && *rGroup->recordId)
1030 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1034 rinfo = dict_lookup (matchDict, matchStr);
1036 memcpy (sysno, rinfo+1, sizeof(*sysno));
1040 logf (LOG_WARN, "Bad match criteria");
1051 logf (LOG_LOG, "Cannot delete new record");
1054 if (records_processed < rGroup->fileVerboseLimit)
1055 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1056 fname, (long) recordOffset);
1057 rec = rec_new (records);
1058 *sysno = rec->sysno;
1062 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1064 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1070 /* record already exists */
1071 struct recKeys delkeys;
1073 rec = rec_get (records, *sysno);
1075 delkeys.buf_used = rec->size[recInfo_delKeys];
1076 delkeys.buf = rec->info[recInfo_delKeys];
1077 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1080 /* record going to be deleted */
1081 if (!delkeys.buf_used)
1083 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1084 fname, (long) recordOffset);
1085 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1089 if (records_processed < rGroup->fileVerboseLimit)
1090 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1091 fname, (long) recordOffset);
1094 dict_delete (matchDict, matchStr);
1095 rec_del (records, &rec);
1102 /* record going to be updated */
1103 if (!delkeys.buf_used)
1105 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1106 fname, (long) recordOffset);
1107 logf (LOG_WARN, "cannot update file above, storeKeys false");
1111 if (records_processed < rGroup->fileVerboseLimit)
1112 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1113 fname, (long) recordOffset);
1114 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1119 /* update file type */
1120 xfree (rec->info[recInfo_fileType]);
1121 rec->info[recInfo_fileType] =
1122 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1124 /* update filename */
1125 xfree (rec->info[recInfo_filename]);
1126 rec->info[recInfo_filename] =
1127 rec_strdup (fname, &rec->size[recInfo_filename]);
1129 /* update delete keys */
1130 xfree (rec->info[recInfo_delKeys]);
1131 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1134 rec->size[recInfo_delKeys] = reckeys.buf_used;
1135 rec->info[recInfo_delKeys] = reckeys.buf;
1137 reckeys.buf_max = 0;
1139 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1140 rec->size[recInfo_delKeys] = reckeys.buf_used;
1141 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1142 rec->size[recInfo_delKeys]);
1147 rec->info[recInfo_delKeys] = NULL;
1148 rec->size[recInfo_delKeys] = 0;
1151 /* update store data */
1152 xfree (rec->info[recInfo_storeData]);
1153 if (rGroup->flagStoreData == 1)
1155 int size = fi->file_moffset - recordOffset;
1157 size = fi->file_max - recordOffset;
1158 rec->size[recInfo_storeData] = size;
1159 rec->info[recInfo_storeData] = xmalloc (size);
1160 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1162 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1163 (long) recordOffset);
1166 if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1168 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1169 fi->file_max, fname);
1175 rec->info[recInfo_storeData] = NULL;
1176 rec->size[recInfo_storeData] = 0;
1178 /* update database name */
1179 xfree (rec->info[recInfo_databaseName]);
1180 rec->info[recInfo_databaseName] =
1181 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1184 xfree (rec->info[recInfo_offset]);
1186 rec->size[recInfo_offset] = sizeof(recordOffset);
1187 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1188 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1190 /* commit this record */
1191 rec_put (records, &rec);
1196 int fileExtract (SYSNO *sysno, const char *fname,
1197 const struct recordGroup *rGroupP, int deleteFlag)
1205 struct recordGroup rGroupM;
1206 struct recordGroup *rGroup = &rGroupM;
1207 struct file_read_info *fi;
1209 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1211 if (!rGroup->groupName || !*rGroup->groupName)
1214 sprintf (gprefix, "%s.", rGroup->groupName);
1216 logf (LOG_DEBUG, "fileExtract %s", fname);
1218 /* determine file extension */
1219 for (i = strlen(fname); --i >= 0; )
1220 if (fname[i] == '/')
1225 else if (fname[i] == '.')
1227 strcpy (ext, fname+i+1);
1230 /* determine file type - depending on extension */
1231 if (!rGroup->recordType)
1233 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1234 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1236 sprintf (ext_res, "%srecordType", gprefix);
1237 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1239 if (records_processed < rGroup->fileVerboseLimit)
1240 logf (LOG_LOG, "? %s", fname);
1245 if (!rGroup->recordType)
1247 if (records_processed < rGroup->fileVerboseLimit)
1248 logf (LOG_LOG, "? record %s", fname);
1251 if (!(recType = recType_byName (rGroup->recordType, subType)))
1253 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1257 /* determine match criteria */
1258 if (!rGroup->recordId)
1260 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1261 rGroup->recordId = res_get (common_resource, ext_res);
1264 /* determine database name */
1265 if (!rGroup->databaseName)
1267 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1268 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1270 sprintf (ext_res, "%sdatabase", gprefix);
1271 rGroup->databaseName = res_get (common_resource, ext_res);
1274 if (!rGroup->databaseName)
1275 rGroup->databaseName = "Default";
1277 if (rGroup->flagStoreData == -1)
1280 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1281 if (!(sval = res_get (common_resource, ext_res)))
1283 sprintf (ext_res, "%sstoreData", gprefix);
1284 sval = res_get (common_resource, ext_res);
1287 rGroup->flagStoreData = atoi (sval);
1289 if (rGroup->flagStoreData == -1)
1290 rGroup->flagStoreData = 0;
1292 if (rGroup->flagStoreKeys == -1)
1296 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1297 if (!(sval = res_get (common_resource, ext_res)))
1299 sprintf (ext_res, "%sstoreKeys", gprefix);
1300 sval = res_get (common_resource, ext_res);
1303 rGroup->flagStoreKeys = atoi (sval);
1305 if (rGroup->flagStoreKeys == -1)
1306 rGroup->flagStoreKeys = 0;
1308 if (sysno && deleteFlag)
1312 if ((fd = open (fname, O_RDONLY)) == -1)
1314 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1318 fi = file_read_start (fd);
1322 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1324 } while (r && !sysno && fi->file_more);
1325 file_read_stop (fi);