2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.70 1997-07-01 13:00:42 adam
8 * Bug fix in routine searchRecordKey: uninitialized variables.
10 * Revision 1.69 1997/04/29 09:26:03 adam
11 * Bug fix: generic recordId handling didn't work for compressed internal
14 * Revision 1.68 1997/02/12 20:39:45 adam
15 * Implemented options -f <n> that limits the log to the first <n>
17 * Changed some log messages also.
19 * Revision 1.67 1996/11/15 15:02:14 adam
20 * Minor changes regarding logging.
22 * Revision 1.66 1996/11/14 09:52:21 adam
23 * Strings in record keys bound by IT_MAX_WORD.
25 * Revision 1.65 1996/11/14 08:57:56 adam
26 * Reduction of storeKeys area.
28 * Revision 1.64 1996/11/08 11:10:16 adam
29 * Buffers used during file match got bigger.
30 * Compressed ISAM support everywhere.
31 * Bug fixes regarding masking characters in queries.
32 * Redesigned Regexp-2 queries.
34 * Revision 1.63 1996/10/29 14:09:39 adam
35 * Use of cisam system - enabled if setting isamc is 1.
37 * Revision 1.62 1996/10/11 10:57:01 adam
38 * New module recctrl. Used to manage records (extract/retrieval).
39 * Several files have been moved to the recctrl sub directory.
41 * Revision 1.61 1996/06/06 12:08:37 quinn
42 * Added showRecord function
44 * Revision 1.60 1996/06/04 10:18:12 adam
45 * Search/scan uses character mapping module.
47 * Revision 1.59 1996/05/14 15:47:07 adam
48 * Cleanup of various buffer size entities.
50 * Revision 1.58 1996/05/14 06:16:38 adam
51 * Compact use/set bytes used in search service.
53 * Revision 1.57 1996/05/13 14:23:04 adam
54 * Work on compaction of set/use bytes in dictionary.
56 * Revision 1.56 1996/05/09 09:54:42 adam
57 * Server supports maps from one logical attributes to a list of physical
59 * The extraction process doesn't make space consuming 'any' keys.
61 * Revision 1.55 1996/05/09 07:28:55 quinn
62 * Work towards phrases and multiple registers
64 * Revision 1.54 1996/05/01 13:46:35 adam
65 * First work on multiple records in one file.
66 * New option, -offset, to the "unread" command in the filter module.
68 * Revision 1.53 1996/04/26 12:09:43 adam
69 * Added a few comments.
71 * Revision 1.52 1996/04/25 13:27:57 adam
72 * Function recordExtract modified so that files with no keys (possibly empty)
75 * Revision 1.51 1996/03/19 11:08:42 adam
76 * Bug fix: Log preamble wasn't always turned off after recordExtract.
78 * Revision 1.50 1996/02/12 18:45:36 adam
79 * New fileVerboseFlag in record group control.
81 * Revision 1.49 1996/02/05 12:29:57 adam
82 * Logging reduced a bit.
83 * The remaining running time is estimated during register merge.
85 * Revision 1.48 1996/02/01 20:53:26 adam
86 * The temporary per-record keys are compacted a little, and duplication
87 * of the per-records keys are avoided when they are saved in the record
90 * Revision 1.47 1996/01/17 14:57:48 adam
91 * Prototype changed for reader functions in extract/retrieve. File
92 * is identified by 'void *' instead of 'int.
94 * Revision 1.46 1995/12/15 14:57:16 adam
97 * Revision 1.45 1995/12/15 12:37:41 adam
98 * In addRecordKeyAny: Writes key only when attrSet != -1.
100 * Revision 1.44 1995/12/12 16:00:54 adam
101 * System call sync(2) used after update/commit.
102 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
105 * Revision 1.43 1995/12/11 09:12:46 adam
106 * The rec_get function returns NULL if record doesn't exist - will
107 * happen in the server if the result set records have been deleted since
108 * the creation of the set (i.e. the search).
109 * The server saves a result temporarily if it is 'volatile', i.e. the
110 * set is register dependent.
112 * Revision 1.42 1995/12/07 17:38:46 adam
113 * Work locking mechanisms for concurrent updates/commit.
115 * Revision 1.41 1995/12/06 16:06:42 adam
116 * Better diagnostics. Work on 'real' dictionary deletion.
118 * Revision 1.40 1995/12/05 16:57:40 adam
119 * More work on regular patterns.
121 * Revision 1.39 1995/12/05 13:20:18 adam
122 * Bug fix: file_read sometimes returned early EOF.
124 * Revision 1.38 1995/12/04 17:59:21 adam
125 * More work on regular expression conversion.
127 * Revision 1.37 1995/12/04 14:22:27 adam
128 * Extra arg to recType_byName.
129 * Started work on new regular expression parsed input to
130 * structured records.
132 * Revision 1.36 1995/11/30 08:34:29 adam
133 * Started work on commit facility.
134 * Changed a few malloc/free to xmalloc/xfree.
136 * Revision 1.35 1995/11/28 14:26:21 adam
137 * Bug fix: recordId with constant wasn't right.
138 * Bug fix: recordId dictionary entry wasn't deleted when needed.
140 * Revision 1.34 1995/11/28 09:09:38 adam
141 * Zebra config renamed.
142 * Use setting 'recordId' to identify record now.
143 * Bug fix in recindex.c: rec_release_blocks was invokeded even
144 * though the blocks were already released.
145 * File traversal properly deletes records when needed.
147 * Revision 1.33 1995/11/27 09:56:20 adam
148 * Record info elements better enumerated. Internal store of records.
150 * Revision 1.32 1995/11/25 10:24:05 adam
151 * More record fields - they are enumerated now.
152 * New options: flagStoreData flagStoreKey.
154 * Revision 1.31 1995/11/24 11:31:35 adam
155 * Commands add & del read filenames from stdin if source directory is
157 * Match criteria supports 'constant' strings.
159 * Revision 1.30 1995/11/22 17:19:16 adam
160 * Record management uses the bfile system.
162 * Revision 1.29 1995/11/21 15:01:14 adam
163 * New general match criteria implemented.
164 * New feature: document groups.
166 * Revision 1.28 1995/11/21 09:20:30 adam
167 * Yet more work on record match.
169 * Revision 1.27 1995/11/20 16:59:45 adam
170 * New update method: the 'old' keys are saved for each records.
172 * Revision 1.26 1995/11/20 11:56:24 adam
173 * Work on new traversal.
175 * Revision 1.25 1995/11/16 15:34:54 adam
176 * Uses new record management system in both indexer and server.
178 * Revision 1.24 1995/11/15 19:13:08 adam
179 * Work on record management.
181 * Revision 1.23 1995/10/27 14:00:10 adam
182 * Implemented detection of database availability.
184 * Revision 1.22 1995/10/17 18:02:07 adam
185 * New feature: databases. Implemented as prefix to words in dictionary.
187 * Revision 1.21 1995/10/10 12:24:38 adam
188 * Temporary sort files are compressed.
190 * Revision 1.20 1995/10/06 13:52:05 adam
191 * Bug fixes. Handler may abort further scanning.
193 * Revision 1.19 1995/10/04 12:55:16 adam
194 * Bug fix in ranked search. Use=Any keys inserted.
196 * Revision 1.18 1995/10/04 09:37:08 quinn
199 * Revision 1.17 1995/10/03 14:28:57 adam
200 * Buffered read in extract works.
202 * Revision 1.16 1995/10/03 14:28:45 adam
203 * Work on more effecient read handler in extract.
205 * Revision 1.15 1995/10/02 15:42:53 adam
206 * Extract uses file descriptors instead of FILE pointers.
208 * Revision 1.14 1995/10/02 15:29:13 adam
209 * More logging in file_extract.
211 * Revision 1.13 1995/09/29 14:01:39 adam
214 * Revision 1.12 1995/09/28 14:22:56 adam
215 * Sort uses smaller temporary files.
217 * Revision 1.11 1995/09/28 12:10:31 adam
218 * Bug fixes. Field prefix used in queries.
220 * Revision 1.10 1995/09/28 09:19:41 adam
221 * xfree/xmalloc used everywhere.
222 * Extract/retrieve method seems to work for text records.
224 * Revision 1.9 1995/09/27 12:22:28 adam
225 * More work on extract in record control.
226 * Field name is not in isam keys but in prefix in dictionary words.
228 * Revision 1.8 1995/09/14 07:48:22 adam
229 * Record control management.
231 * Revision 1.7 1995/09/11 13:09:32 adam
232 * More work on relevance feedback.
234 * Revision 1.6 1995/09/08 14:52:27 adam
235 * Minor changes. Dictionary is lower case now.
237 * Revision 1.5 1995/09/06 16:11:16 adam
238 * Option: only one word key per file.
240 * Revision 1.4 1995/09/05 15:28:39 adam
241 * More work on search engine.
243 * Revision 1.3 1995/09/04 12:33:41 adam
244 * Various cleanup. YAZ util used instead.
246 * Revision 1.2 1995/09/04 09:10:34 adam
247 * More work on index add/del/update.
248 * Merge sort implemented.
249 * Initial work on z39 server.
251 * Revision 1.1 1995/09/01 14:06:35 adam
252 * Split of work into more files.
265 static Dict matchDict;
267 static Records records = NULL;
269 static char **key_buf;
270 static size_t ptr_top;
272 static size_t key_buf_used;
273 static int key_file_no;
275 static int records_inserted = 0;
276 static int records_updated = 0;
277 static int records_deleted = 0;
278 static int records_processed = 0;
280 static ZebTargetInfo *zti = NULL;
282 static void logRecord (int showFlag)
286 if (showFlag || !(records_processed % 1000))
288 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
289 records_processed, records_inserted, records_updated,
294 void key_open (int mem)
297 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
300 key_buf = xmalloc (mem);
301 ptr_top = mem/sizeof(char*);
307 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
309 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
313 records = rec_open (1);
315 zti = zebTargetInfo_open (records, 1);
325 void encode_key_init (struct encode_info *i)
331 char *encode_key_int (int d, char *bp)
340 else if (d <= 4194303)
342 *bp++ = 128 + (d>>16);
343 *bp++ = (d>>8) & 255;
348 *bp++ = 192 + (d>>24);
349 *bp++ = (d>>16) & 255;
350 *bp++ = (d>>8) & 255;
356 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
361 while ((*bp++ = *k++))
363 memcpy (&key, k+1, sizeof(struct it_key));
364 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
365 if (i->sysno != key.sysno)
367 i->sysno = key.sysno;
370 bp = encode_key_int (key.seqno - i->seqno, bp);
371 i->seqno = key.seqno;
372 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
374 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
382 static int key_y_len;
384 static int key_y_compare (const void *p1, const void *p2)
388 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
389 *(char**) p2 + key_y_len + 1)))
391 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
394 static int key_x_compare (const void *p1, const void *p2)
396 return strcmp (*(char**) p1, *(char**) p2);
400 void key_flush (void)
405 struct encode_info encode_info;
414 logf (LOG_LOG, "sorting section %d", key_file_no);
416 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
417 getFnameTmp (out_fname, key_file_no);
419 if (!(outf = fopen (out_fname, "w")))
421 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
424 logf (LOG_LOG, "writing section %d", key_file_no);
425 prevcp = cp = key_buf[ptr_top-ptr_i];
427 encode_key_init (&encode_info);
428 encode_key_write (cp, &encode_info, outf);
431 cp = key_buf[ptr_top-ptr_i];
432 if (strcmp (cp, prevcp))
434 encode_key_init (&encode_info);
435 encode_key_write (cp, &encode_info, outf);
439 encode_key_write (cp + strlen(cp), &encode_info, outf);
442 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
443 getFnameTmp (out_fname, key_file_no);
445 if (!(outf = fopen (out_fname, "w")))
447 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
450 logf (LOG_LOG, "writing section %d", key_file_no);
452 prevcp = key_buf[ptr_top-i];
454 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
456 key_y_len = strlen(prevcp)+1;
458 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
459 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
461 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
462 sizeof(char*), key_y_compare);
463 cp = key_buf[ptr_top-ptr_i];
465 encode_key_init (&encode_info);
466 encode_key_write (cp, &encode_info, outf);
469 cp = key_buf[ptr_top-ptr_i];
470 encode_key_write (cp+key_y_len, &encode_info, outf);
474 prevcp = key_buf[ptr_top-ptr_i];
479 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
482 logf (LOG_LOG, "finished section %d", key_file_no);
492 zebTargetInfo_close (zti, 1);
494 rec_close (&records);
495 dict_close (matchDict);
501 static void wordInit (RecWord *p)
505 p->which = Word_String;
517 static void addRecordKey (const RecWord *p)
526 if (reckeys.buf_used+1024 > reckeys.buf_max)
530 b = xmalloc (reckeys.buf_max += 128000);
531 if (reckeys.buf_used > 0)
532 memcpy (b, reckeys.buf, reckeys.buf_used);
536 dst = reckeys.buf + reckeys.buf_used;
538 attrSet = p->attrSet;
539 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
542 reckeys.prevAttrSet = attrSet;
543 attrUse = p->attrUse;
544 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
547 reckeys.prevAttrUse = attrUse;
549 diff = 1 + p->seqno - reckeys.prevSeqNo;
550 if (diff >= 1 && diff <= 15)
555 reckeys.prevSeqNo = p->seqno;
561 memcpy (dst, &attrSet, sizeof(attrSet));
562 dst += sizeof(attrSet);
566 memcpy (dst, &attrUse, sizeof(attrUse));
567 dst += sizeof(attrUse);
580 for (i = 0; p->u.string[i] && i < IT_MAX_WORD-3; i++)
581 *dst++ = p->u.string[i];
586 memcpy (dst, &p->seqno, sizeof(p->seqno));
587 dst += sizeof(p->seqno);
589 reckeys.buf_used = dst - reckeys.buf;
592 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
593 const char *databaseName)
600 if (zebTargetInfo_curDatabase (zti, databaseName))
602 if (zebTargetInfo_newDatabase (zti, databaseName))
605 while (off < reckeys->buf_used)
607 const char *src = reckeys->buf + off;
615 memcpy (&attrSet, src, sizeof(attrSet));
616 src += sizeof(attrSet);
620 memcpy (&attrUse, src, sizeof(attrUse));
621 src += sizeof(attrUse);
623 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
626 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
628 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
630 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
632 ((char*) key_buf) [key_buf_used++] = ch;
634 ((char*)key_buf) [key_buf_used++] = *src++;
636 ((char*)key_buf) [key_buf_used++] = '\0';
637 ((char*) key_buf)[key_buf_used++] = cmd;
640 seqno += ((lead>>2) & 15)-1;
643 memcpy (&seqno, src, sizeof(seqno));
644 src += sizeof(seqno);
648 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
649 key_buf_used += sizeof(key);
650 off = src - reckeys->buf;
652 assert (off == reckeys->buf_used);
655 static const char **searchRecordKey (struct recKeys *reckeys,
656 int attrSetS, int attrUseS)
658 static const char *ws[32];
666 for (i = 0; i<32; i++)
669 while (off < reckeys->buf_used)
672 const char *src = reckeys->buf + off;
680 memcpy (&attrSet, src, sizeof(attrSet));
681 src += sizeof(attrSet);
685 memcpy (&attrUse, src, sizeof(attrUse));
686 src += sizeof(attrUse);
692 seqno += ((lead>>2) & 15)-1;
695 memcpy (&seqno, src, sizeof(seqno));
696 src += sizeof(seqno);
698 if (attrUseS == attrUse && attrSetS == attrSet)
705 woff = seqno - startSeq;
706 if (woff >= 0 && woff < 31)
710 off = src - reckeys->buf;
712 assert (off == reckeys->buf_used);
716 struct file_read_info {
724 static struct file_read_info *file_read_start (int fd)
726 struct file_read_info *fi = xmalloc (sizeof(*fi));
730 fi->file_moffset = 0;
734 static void file_read_stop (struct file_read_info *fi)
740 static off_t file_seek (void *handle, off_t offset)
742 struct file_read_info *p = handle;
743 p->file_offset = offset;
744 return lseek (p->fd, offset, SEEK_SET);
747 static int file_read (void *handle, char *buf, size_t count)
749 struct file_read_info *p = handle;
752 r = read (fd, buf, count);
756 if (p->file_offset > p->file_max)
757 p->file_max = p->file_offset;
762 static void file_begin (void *handle)
764 struct file_read_info *p = handle;
766 p->file_offset = p->file_moffset;
768 lseek (p->fd, p->file_moffset, SEEK_SET);
772 static void file_end (void *handle, off_t offset)
774 struct file_read_info *p = handle;
776 assert (p->file_more == 0);
778 p->file_moffset = offset;
781 static int atois (const char **s)
784 while ( (c=**s) >= '0' && c <= '9')
786 val = val*10 + c - '0';
792 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
796 static char dstBuf[2048];
798 const char *s = spec;
799 static const char **w;
804 while (*s == ' ' || *s == '\t')
811 int attrSet, attrUse;
815 attrSet = atois (&s);
818 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
819 spec, rGroup->groupName ? rGroup->groupName : "none");
823 attrUse = atois (&s);
824 w = searchRecordKey (reckeys, attrSet, attrUse);
829 for (i = 0; i<32; i++)
834 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
835 spec, rGroup->groupName ? rGroup->groupName : "none");
840 for (i = 0; i<32; i++)
841 if (matchFlag[i] && w[i])
853 logf (LOG_WARN, "Record didn't contain match"
854 " fields in (%d,%d)", attrSet, attrUse);
862 const char *spec_src = NULL;
863 const char *s1 = ++s;
864 while (*s1 && *s1 != ' ' && *s1 != '\t')
870 memcpy (special, s, spec_len);
871 special[spec_len] = '\0';
874 if (!strcmp (special, "group"))
875 spec_src = rGroup->groupName;
876 else if (!strcmp (special, "database"))
877 spec_src = rGroup->databaseName;
878 else if (!strcmp (special, "filename"))
880 else if (!strcmp (special, "type"))
881 spec_src = rGroup->recordType;
886 strcpy (dst, spec_src);
887 dst += strlen (spec_src);
890 else if (*s == '\"' || *s == '\'')
892 int stopMarker = *s++;
896 while (*s && *s != stopMarker)
899 tmpString[i++] = *s++;
904 strcpy (dst, tmpString);
905 dst += strlen (tmpString);
909 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
910 spec, rGroup->groupName ? rGroup->groupName : "none");
917 logf (LOG_WARN, "No match criteria for record %s in group %s",
918 fname, rGroup->groupName ? rGroup->groupName : "none");
924 struct recordLogInfo {
927 struct recordGroup *rGroup;
930 static void recordLogPreamble (int level, const char *msg, void *info)
932 struct recordLogInfo *p = info;
933 FILE *outf = log_file ();
937 fprintf (outf, "File %s, offset %d, type %s\n",
938 p->rGroup->recordType, p->recordOffset, p->fname);
939 log_event_start (NULL, NULL);
942 static int recordExtract (SYSNO *sysno, const char *fname,
943 struct recordGroup *rGroup, int deleteFlag,
944 struct file_read_info *fi, RecType recType,
947 struct recExtractCtrl extractCtrl;
951 off_t recordOffset = 0;
953 struct recordLogInfo logInfo;
957 /* we are going to read from a file, so prepare the extraction */
959 extractCtrl.subType = subType;
960 extractCtrl.init = wordInit;
961 extractCtrl.add = addRecordKey;
963 reckeys.buf_used = 0;
964 reckeys.prevAttrUse = -1;
965 reckeys.prevAttrSet = -1;
966 reckeys.prevSeqNo = 0;
968 recordOffset = fi->file_moffset;
969 extractCtrl.offset = recordOffset;
970 extractCtrl.readf = file_read;
971 extractCtrl.seekf = file_seek;
972 extractCtrl.endf = file_end;
973 extractCtrl.map_chrs_input = map_chrs_input;
974 extractCtrl.flagShowRecords = rGroup->flagShowRecords;
975 if (rGroup->flagShowRecords)
976 printf ("File: %s %ld\n", fname, (long) recordOffset);
978 logInfo.fname = fname;
979 logInfo.recordOffset = recordOffset;
980 logInfo.rGroup = rGroup;
981 log_event_start (recordLogPreamble, &logInfo);
983 r = (*recType->extract)(&extractCtrl);
985 log_event_start (NULL, NULL);
989 /* error occured during extraction ... */
990 if (!rGroup->flagShowRecords &&
991 records_processed < rGroup->fileVerboseLimit)
993 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
994 fname, (long) recordOffset, r);
998 if (reckeys.buf_used == 0)
1000 /* the extraction process returned no information - the record
1001 is probably empty - unless flagShowRecords is in use */
1002 if (rGroup->flagShowRecords)
1004 logf (LOG_WARN, "No keys generated for file %s", fname);
1005 logf (LOG_WARN, " The file is probably empty");
1010 /* perform match if sysno not known and if match criteria is specified */
1017 if (rGroup->recordId && *rGroup->recordId)
1021 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1025 rinfo = dict_lookup (matchDict, matchStr);
1027 memcpy (sysno, rinfo+1, sizeof(*sysno));
1031 logf (LOG_WARN, "Bad match criteria");
1042 logf (LOG_LOG, "Cannot delete new record");
1045 if (records_processed < rGroup->fileVerboseLimit)
1046 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1047 fname, (long) recordOffset);
1048 rec = rec_new (records);
1049 *sysno = rec->sysno;
1053 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1055 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1061 /* record already exists */
1062 struct recKeys delkeys;
1064 rec = rec_get (records, *sysno);
1066 delkeys.buf_used = rec->size[recInfo_delKeys];
1067 delkeys.buf = rec->info[recInfo_delKeys];
1068 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1071 /* record going to be deleted */
1072 if (!delkeys.buf_used)
1074 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1075 fname, (long) recordOffset);
1076 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1080 if (records_processed < rGroup->fileVerboseLimit)
1081 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1082 fname, (long) recordOffset);
1085 dict_delete (matchDict, matchStr);
1086 rec_del (records, &rec);
1093 /* record going to be updated */
1094 if (!delkeys.buf_used)
1096 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1097 fname, (long) recordOffset);
1098 logf (LOG_WARN, "cannot update file above, storeKeys false");
1102 if (records_processed < rGroup->fileVerboseLimit)
1103 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1104 fname, (long) recordOffset);
1105 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1110 /* update file type */
1111 xfree (rec->info[recInfo_fileType]);
1112 rec->info[recInfo_fileType] =
1113 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1115 /* update filename */
1116 xfree (rec->info[recInfo_filename]);
1117 rec->info[recInfo_filename] =
1118 rec_strdup (fname, &rec->size[recInfo_filename]);
1120 /* update delete keys */
1121 xfree (rec->info[recInfo_delKeys]);
1122 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1125 rec->size[recInfo_delKeys] = reckeys.buf_used;
1126 rec->info[recInfo_delKeys] = reckeys.buf;
1128 reckeys.buf_max = 0;
1130 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1131 rec->size[recInfo_delKeys] = reckeys.buf_used;
1132 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1133 rec->size[recInfo_delKeys]);
1138 rec->info[recInfo_delKeys] = NULL;
1139 rec->size[recInfo_delKeys] = 0;
1142 /* update store data */
1143 xfree (rec->info[recInfo_storeData]);
1144 if (rGroup->flagStoreData == 1)
1146 rec->size[recInfo_storeData] = fi->file_max;
1147 rec->info[recInfo_storeData] = xmalloc (fi->file_max);
1148 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1150 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1151 (long) recordOffset);
1154 if (read (fi->fd, rec->info[recInfo_storeData], fi->file_max)
1157 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1158 fi->file_max, fname);
1164 rec->info[recInfo_storeData] = NULL;
1165 rec->size[recInfo_storeData] = 0;
1167 /* update database name */
1168 xfree (rec->info[recInfo_databaseName]);
1169 rec->info[recInfo_databaseName] =
1170 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1173 xfree (rec->info[recInfo_offset]);
1175 rec->size[recInfo_offset] = sizeof(recordOffset);
1176 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1177 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1179 /* commit this record */
1180 rec_put (records, &rec);
1185 int fileExtract (SYSNO *sysno, const char *fname,
1186 const struct recordGroup *rGroupP, int deleteFlag)
1194 struct recordGroup rGroupM;
1195 struct recordGroup *rGroup = &rGroupM;
1196 struct file_read_info *fi;
1198 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1200 if (!rGroup->groupName || !*rGroup->groupName)
1203 sprintf (gprefix, "%s.", rGroup->groupName);
1205 logf (LOG_DEBUG, "fileExtract %s", fname);
1207 /* determine file extension */
1208 for (i = strlen(fname); --i >= 0; )
1209 if (fname[i] == '/')
1214 else if (fname[i] == '.')
1216 strcpy (ext, fname+i+1);
1219 /* determine file type - depending on extension */
1220 if (!rGroup->recordType)
1222 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1223 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1225 sprintf (ext_res, "%srecordType", gprefix);
1226 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1228 if (records_processed < rGroup->fileVerboseLimit)
1229 logf (LOG_LOG, "? %s", fname);
1234 if (!rGroup->recordType)
1236 if (records_processed < rGroup->fileVerboseLimit)
1237 logf (LOG_LOG, "? record %s", fname);
1240 if (!(recType = recType_byName (rGroup->recordType, subType)))
1242 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1246 /* determine match criteria */
1247 if (!rGroup->recordId)
1249 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1250 rGroup->recordId = res_get (common_resource, ext_res);
1253 /* determine database name */
1254 if (!rGroup->databaseName)
1256 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1257 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1259 sprintf (ext_res, "%sdatabase", gprefix);
1260 rGroup->databaseName = res_get (common_resource, ext_res);
1263 if (!rGroup->databaseName)
1264 rGroup->databaseName = "Default";
1266 if (rGroup->flagStoreData == -1)
1269 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1270 if (!(sval = res_get (common_resource, ext_res)))
1272 sprintf (ext_res, "%sstoreData", gprefix);
1273 sval = res_get (common_resource, ext_res);
1276 rGroup->flagStoreData = atoi (sval);
1278 if (rGroup->flagStoreData == -1)
1279 rGroup->flagStoreData = 0;
1281 if (rGroup->flagStoreKeys == -1)
1285 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1286 if (!(sval = res_get (common_resource, ext_res)))
1288 sprintf (ext_res, "%sstoreKeys", gprefix);
1289 sval = res_get (common_resource, ext_res);
1292 rGroup->flagStoreKeys = atoi (sval);
1294 if (rGroup->flagStoreKeys == -1)
1295 rGroup->flagStoreKeys = 0;
1297 if (sysno && deleteFlag)
1301 if ((fd = open (fname, O_RDONLY)) == -1)
1303 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1307 fi = file_read_start (fd);
1311 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1313 } while (r && !sysno && fi->file_more);
1314 file_read_stop (fi);