2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.71 1997-07-15 16:28:41 adam
8 * Bug fix: storeData didn't work with files with multiple records.
9 * Bug fix: fixed memory management with records; not really well
12 * Revision 1.70 1997/07/01 13:00:42 adam
13 * Bug fix in routine searchRecordKey: uninitialized variables.
15 * Revision 1.69 1997/04/29 09:26:03 adam
16 * Bug fix: generic recordId handling didn't work for compressed internal
19 * Revision 1.68 1997/02/12 20:39:45 adam
20 * Implemented options -f <n> that limits the log to the first <n>
22 * Changed some log messages also.
24 * Revision 1.67 1996/11/15 15:02:14 adam
25 * Minor changes regarding logging.
27 * Revision 1.66 1996/11/14 09:52:21 adam
28 * Strings in record keys bound by IT_MAX_WORD.
30 * Revision 1.65 1996/11/14 08:57:56 adam
31 * Reduction of storeKeys area.
33 * Revision 1.64 1996/11/08 11:10:16 adam
34 * Buffers used during file match got bigger.
35 * Compressed ISAM support everywhere.
36 * Bug fixes regarding masking characters in queries.
37 * Redesigned Regexp-2 queries.
39 * Revision 1.63 1996/10/29 14:09:39 adam
40 * Use of cisam system - enabled if setting isamc is 1.
42 * Revision 1.62 1996/10/11 10:57:01 adam
43 * New module recctrl. Used to manage records (extract/retrieval).
44 * Several files have been moved to the recctrl sub directory.
46 * Revision 1.61 1996/06/06 12:08:37 quinn
47 * Added showRecord function
49 * Revision 1.60 1996/06/04 10:18:12 adam
50 * Search/scan uses character mapping module.
52 * Revision 1.59 1996/05/14 15:47:07 adam
53 * Cleanup of various buffer size entities.
55 * Revision 1.58 1996/05/14 06:16:38 adam
56 * Compact use/set bytes used in search service.
58 * Revision 1.57 1996/05/13 14:23:04 adam
59 * Work on compaction of set/use bytes in dictionary.
61 * Revision 1.56 1996/05/09 09:54:42 adam
62 * Server supports maps from one logical attributes to a list of physical
64 * The extraction process doesn't make space consuming 'any' keys.
66 * Revision 1.55 1996/05/09 07:28:55 quinn
67 * Work towards phrases and multiple registers
69 * Revision 1.54 1996/05/01 13:46:35 adam
70 * First work on multiple records in one file.
71 * New option, -offset, to the "unread" command in the filter module.
73 * Revision 1.53 1996/04/26 12:09:43 adam
74 * Added a few comments.
76 * Revision 1.52 1996/04/25 13:27:57 adam
77 * Function recordExtract modified so that files with no keys (possibly empty)
80 * Revision 1.51 1996/03/19 11:08:42 adam
81 * Bug fix: Log preamble wasn't always turned off after recordExtract.
83 * Revision 1.50 1996/02/12 18:45:36 adam
84 * New fileVerboseFlag in record group control.
86 * Revision 1.49 1996/02/05 12:29:57 adam
87 * Logging reduced a bit.
88 * The remaining running time is estimated during register merge.
90 * Revision 1.48 1996/02/01 20:53:26 adam
91 * The temporary per-record keys are compacted a little, and duplication
92 * of the per-records keys are avoided when they are saved in the record
95 * Revision 1.47 1996/01/17 14:57:48 adam
96 * Prototype changed for reader functions in extract/retrieve. File
97 * is identified by 'void *' instead of 'int.
99 * Revision 1.46 1995/12/15 14:57:16 adam
102 * Revision 1.45 1995/12/15 12:37:41 adam
103 * In addRecordKeyAny: Writes key only when attrSet != -1.
105 * Revision 1.44 1995/12/12 16:00:54 adam
106 * System call sync(2) used after update/commit.
107 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
110 * Revision 1.43 1995/12/11 09:12:46 adam
111 * The rec_get function returns NULL if record doesn't exist - will
112 * happen in the server if the result set records have been deleted since
113 * the creation of the set (i.e. the search).
114 * The server saves a result temporarily if it is 'volatile', i.e. the
115 * set is register dependent.
117 * Revision 1.42 1995/12/07 17:38:46 adam
118 * Work locking mechanisms for concurrent updates/commit.
120 * Revision 1.41 1995/12/06 16:06:42 adam
121 * Better diagnostics. Work on 'real' dictionary deletion.
123 * Revision 1.40 1995/12/05 16:57:40 adam
124 * More work on regular patterns.
126 * Revision 1.39 1995/12/05 13:20:18 adam
127 * Bug fix: file_read sometimes returned early EOF.
129 * Revision 1.38 1995/12/04 17:59:21 adam
130 * More work on regular expression conversion.
132 * Revision 1.37 1995/12/04 14:22:27 adam
133 * Extra arg to recType_byName.
134 * Started work on new regular expression parsed input to
135 * structured records.
137 * Revision 1.36 1995/11/30 08:34:29 adam
138 * Started work on commit facility.
139 * Changed a few malloc/free to xmalloc/xfree.
141 * Revision 1.35 1995/11/28 14:26:21 adam
142 * Bug fix: recordId with constant wasn't right.
143 * Bug fix: recordId dictionary entry wasn't deleted when needed.
145 * Revision 1.34 1995/11/28 09:09:38 adam
146 * Zebra config renamed.
147 * Use setting 'recordId' to identify record now.
148 * Bug fix in recindex.c: rec_release_blocks was invokeded even
149 * though the blocks were already released.
150 * File traversal properly deletes records when needed.
152 * Revision 1.33 1995/11/27 09:56:20 adam
153 * Record info elements better enumerated. Internal store of records.
155 * Revision 1.32 1995/11/25 10:24:05 adam
156 * More record fields - they are enumerated now.
157 * New options: flagStoreData flagStoreKey.
159 * Revision 1.31 1995/11/24 11:31:35 adam
160 * Commands add & del read filenames from stdin if source directory is
162 * Match criteria supports 'constant' strings.
164 * Revision 1.30 1995/11/22 17:19:16 adam
165 * Record management uses the bfile system.
167 * Revision 1.29 1995/11/21 15:01:14 adam
168 * New general match criteria implemented.
169 * New feature: document groups.
171 * Revision 1.28 1995/11/21 09:20:30 adam
172 * Yet more work on record match.
174 * Revision 1.27 1995/11/20 16:59:45 adam
175 * New update method: the 'old' keys are saved for each records.
177 * Revision 1.26 1995/11/20 11:56:24 adam
178 * Work on new traversal.
180 * Revision 1.25 1995/11/16 15:34:54 adam
181 * Uses new record management system in both indexer and server.
183 * Revision 1.24 1995/11/15 19:13:08 adam
184 * Work on record management.
186 * Revision 1.23 1995/10/27 14:00:10 adam
187 * Implemented detection of database availability.
189 * Revision 1.22 1995/10/17 18:02:07 adam
190 * New feature: databases. Implemented as prefix to words in dictionary.
192 * Revision 1.21 1995/10/10 12:24:38 adam
193 * Temporary sort files are compressed.
195 * Revision 1.20 1995/10/06 13:52:05 adam
196 * Bug fixes. Handler may abort further scanning.
198 * Revision 1.19 1995/10/04 12:55:16 adam
199 * Bug fix in ranked search. Use=Any keys inserted.
201 * Revision 1.18 1995/10/04 09:37:08 quinn
204 * Revision 1.17 1995/10/03 14:28:57 adam
205 * Buffered read in extract works.
207 * Revision 1.16 1995/10/03 14:28:45 adam
208 * Work on more effecient read handler in extract.
210 * Revision 1.15 1995/10/02 15:42:53 adam
211 * Extract uses file descriptors instead of FILE pointers.
213 * Revision 1.14 1995/10/02 15:29:13 adam
214 * More logging in file_extract.
216 * Revision 1.13 1995/09/29 14:01:39 adam
219 * Revision 1.12 1995/09/28 14:22:56 adam
220 * Sort uses smaller temporary files.
222 * Revision 1.11 1995/09/28 12:10:31 adam
223 * Bug fixes. Field prefix used in queries.
225 * Revision 1.10 1995/09/28 09:19:41 adam
226 * xfree/xmalloc used everywhere.
227 * Extract/retrieve method seems to work for text records.
229 * Revision 1.9 1995/09/27 12:22:28 adam
230 * More work on extract in record control.
231 * Field name is not in isam keys but in prefix in dictionary words.
233 * Revision 1.8 1995/09/14 07:48:22 adam
234 * Record control management.
236 * Revision 1.7 1995/09/11 13:09:32 adam
237 * More work on relevance feedback.
239 * Revision 1.6 1995/09/08 14:52:27 adam
240 * Minor changes. Dictionary is lower case now.
242 * Revision 1.5 1995/09/06 16:11:16 adam
243 * Option: only one word key per file.
245 * Revision 1.4 1995/09/05 15:28:39 adam
246 * More work on search engine.
248 * Revision 1.3 1995/09/04 12:33:41 adam
249 * Various cleanup. YAZ util used instead.
251 * Revision 1.2 1995/09/04 09:10:34 adam
252 * More work on index add/del/update.
253 * Merge sort implemented.
254 * Initial work on z39 server.
256 * Revision 1.1 1995/09/01 14:06:35 adam
257 * Split of work into more files.
270 static Dict matchDict;
272 static Records records = NULL;
274 static char **key_buf;
275 static size_t ptr_top;
277 static size_t key_buf_used;
278 static int key_file_no;
280 static int records_inserted = 0;
281 static int records_updated = 0;
282 static int records_deleted = 0;
283 static int records_processed = 0;
285 static ZebTargetInfo *zti = NULL;
287 static void logRecord (int showFlag)
291 if (showFlag || !(records_processed % 1000))
293 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
294 records_processed, records_inserted, records_updated,
299 void key_open (int mem)
302 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
305 key_buf = xmalloc (mem);
306 ptr_top = mem/sizeof(char*);
312 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
314 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
318 records = rec_open (1);
320 zti = zebTargetInfo_open (records, 1);
330 void encode_key_init (struct encode_info *i)
336 char *encode_key_int (int d, char *bp)
345 else if (d <= 4194303)
347 *bp++ = 128 + (d>>16);
348 *bp++ = (d>>8) & 255;
353 *bp++ = 192 + (d>>24);
354 *bp++ = (d>>16) & 255;
355 *bp++ = (d>>8) & 255;
361 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
366 while ((*bp++ = *k++))
368 memcpy (&key, k+1, sizeof(struct it_key));
369 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
370 if (i->sysno != key.sysno)
372 i->sysno = key.sysno;
375 bp = encode_key_int (key.seqno - i->seqno, bp);
376 i->seqno = key.seqno;
377 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
379 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
387 static int key_y_len;
389 static int key_y_compare (const void *p1, const void *p2)
393 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
394 *(char**) p2 + key_y_len + 1)))
396 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
399 static int key_x_compare (const void *p1, const void *p2)
401 return strcmp (*(char**) p1, *(char**) p2);
405 void key_flush (void)
410 struct encode_info encode_info;
419 logf (LOG_LOG, "sorting section %d", key_file_no);
421 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
422 getFnameTmp (out_fname, key_file_no);
424 if (!(outf = fopen (out_fname, "w")))
426 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
429 logf (LOG_LOG, "writing section %d", key_file_no);
430 prevcp = cp = key_buf[ptr_top-ptr_i];
432 encode_key_init (&encode_info);
433 encode_key_write (cp, &encode_info, outf);
436 cp = key_buf[ptr_top-ptr_i];
437 if (strcmp (cp, prevcp))
439 encode_key_init (&encode_info);
440 encode_key_write (cp, &encode_info, outf);
444 encode_key_write (cp + strlen(cp), &encode_info, outf);
447 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
448 getFnameTmp (out_fname, key_file_no);
450 if (!(outf = fopen (out_fname, "w")))
452 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
455 logf (LOG_LOG, "writing section %d", key_file_no);
457 prevcp = key_buf[ptr_top-i];
459 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
461 key_y_len = strlen(prevcp)+1;
463 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
464 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
466 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
467 sizeof(char*), key_y_compare);
468 cp = key_buf[ptr_top-ptr_i];
470 encode_key_init (&encode_info);
471 encode_key_write (cp, &encode_info, outf);
474 cp = key_buf[ptr_top-ptr_i];
475 encode_key_write (cp+key_y_len, &encode_info, outf);
479 prevcp = key_buf[ptr_top-ptr_i];
484 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
487 logf (LOG_LOG, "finished section %d", key_file_no);
497 zebTargetInfo_close (zti, 1);
499 rec_close (&records);
500 dict_close (matchDict);
506 static void wordInit (RecWord *p)
510 p->which = Word_String;
522 static void addRecordKey (const RecWord *p)
531 if (reckeys.buf_used+1024 > reckeys.buf_max)
535 b = xmalloc (reckeys.buf_max += 128000);
536 if (reckeys.buf_used > 0)
537 memcpy (b, reckeys.buf, reckeys.buf_used);
541 dst = reckeys.buf + reckeys.buf_used;
543 attrSet = p->attrSet;
544 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
547 reckeys.prevAttrSet = attrSet;
548 attrUse = p->attrUse;
549 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
552 reckeys.prevAttrUse = attrUse;
554 diff = 1 + p->seqno - reckeys.prevSeqNo;
555 if (diff >= 1 && diff <= 15)
560 reckeys.prevSeqNo = p->seqno;
566 memcpy (dst, &attrSet, sizeof(attrSet));
567 dst += sizeof(attrSet);
571 memcpy (dst, &attrUse, sizeof(attrUse));
572 dst += sizeof(attrUse);
585 for (i = 0; p->u.string[i] && i < IT_MAX_WORD-3; i++)
586 *dst++ = p->u.string[i];
591 memcpy (dst, &p->seqno, sizeof(p->seqno));
592 dst += sizeof(p->seqno);
594 reckeys.buf_used = dst - reckeys.buf;
597 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
598 const char *databaseName)
605 if (zebTargetInfo_curDatabase (zti, databaseName))
607 if (zebTargetInfo_newDatabase (zti, databaseName))
610 while (off < reckeys->buf_used)
612 const char *src = reckeys->buf + off;
620 memcpy (&attrSet, src, sizeof(attrSet));
621 src += sizeof(attrSet);
625 memcpy (&attrUse, src, sizeof(attrUse));
626 src += sizeof(attrUse);
628 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
631 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
633 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
635 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
637 ((char*) key_buf) [key_buf_used++] = ch;
639 ((char*)key_buf) [key_buf_used++] = *src++;
641 ((char*)key_buf) [key_buf_used++] = '\0';
642 ((char*) key_buf)[key_buf_used++] = cmd;
645 seqno += ((lead>>2) & 15)-1;
648 memcpy (&seqno, src, sizeof(seqno));
649 src += sizeof(seqno);
653 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
654 key_buf_used += sizeof(key);
655 off = src - reckeys->buf;
657 assert (off == reckeys->buf_used);
660 static const char **searchRecordKey (struct recKeys *reckeys,
661 int attrSetS, int attrUseS)
663 static const char *ws[32];
671 for (i = 0; i<32; i++)
674 while (off < reckeys->buf_used)
677 const char *src = reckeys->buf + off;
685 memcpy (&attrSet, src, sizeof(attrSet));
686 src += sizeof(attrSet);
690 memcpy (&attrUse, src, sizeof(attrUse));
691 src += sizeof(attrUse);
697 seqno += ((lead>>2) & 15)-1;
700 memcpy (&seqno, src, sizeof(seqno));
701 src += sizeof(seqno);
703 if (attrUseS == attrUse && attrSetS == attrSet)
710 woff = seqno - startSeq;
711 if (woff >= 0 && woff < 31)
715 off = src - reckeys->buf;
717 assert (off == reckeys->buf_used);
721 struct file_read_info {
729 static struct file_read_info *file_read_start (int fd)
731 struct file_read_info *fi = xmalloc (sizeof(*fi));
735 fi->file_moffset = 0;
739 static void file_read_stop (struct file_read_info *fi)
745 static off_t file_seek (void *handle, off_t offset)
747 struct file_read_info *p = handle;
748 p->file_offset = offset;
749 return lseek (p->fd, offset, SEEK_SET);
752 static int file_read (void *handle, char *buf, size_t count)
754 struct file_read_info *p = handle;
757 r = read (fd, buf, count);
761 if (p->file_offset > p->file_max)
762 p->file_max = p->file_offset;
767 static void file_begin (void *handle)
769 struct file_read_info *p = handle;
771 p->file_offset = p->file_moffset;
773 lseek (p->fd, p->file_moffset, SEEK_SET);
777 static void file_end (void *handle, off_t offset)
779 struct file_read_info *p = handle;
781 assert (p->file_more == 0);
783 p->file_moffset = offset;
786 static int atois (const char **s)
789 while ( (c=**s) >= '0' && c <= '9')
791 val = val*10 + c - '0';
797 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
801 static char dstBuf[2048];
803 const char *s = spec;
804 static const char **w;
809 while (*s == ' ' || *s == '\t')
816 int attrSet, attrUse;
820 attrSet = atois (&s);
823 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
824 spec, rGroup->groupName ? rGroup->groupName : "none");
828 attrUse = atois (&s);
829 w = searchRecordKey (reckeys, attrSet, attrUse);
834 for (i = 0; i<32; i++)
839 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
840 spec, rGroup->groupName ? rGroup->groupName : "none");
845 for (i = 0; i<32; i++)
846 if (matchFlag[i] && w[i])
858 logf (LOG_WARN, "Record didn't contain match"
859 " fields in (%d,%d)", attrSet, attrUse);
867 const char *spec_src = NULL;
868 const char *s1 = ++s;
869 while (*s1 && *s1 != ' ' && *s1 != '\t')
875 memcpy (special, s, spec_len);
876 special[spec_len] = '\0';
879 if (!strcmp (special, "group"))
880 spec_src = rGroup->groupName;
881 else if (!strcmp (special, "database"))
882 spec_src = rGroup->databaseName;
883 else if (!strcmp (special, "filename"))
885 else if (!strcmp (special, "type"))
886 spec_src = rGroup->recordType;
891 strcpy (dst, spec_src);
892 dst += strlen (spec_src);
895 else if (*s == '\"' || *s == '\'')
897 int stopMarker = *s++;
901 while (*s && *s != stopMarker)
904 tmpString[i++] = *s++;
909 strcpy (dst, tmpString);
910 dst += strlen (tmpString);
914 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
915 spec, rGroup->groupName ? rGroup->groupName : "none");
922 logf (LOG_WARN, "No match criteria for record %s in group %s",
923 fname, rGroup->groupName ? rGroup->groupName : "none");
929 struct recordLogInfo {
932 struct recordGroup *rGroup;
935 static void recordLogPreamble (int level, const char *msg, void *info)
937 struct recordLogInfo *p = info;
938 FILE *outf = log_file ();
942 fprintf (outf, "File %s, offset %d, type %s\n",
943 p->rGroup->recordType, p->recordOffset, p->fname);
944 log_event_start (NULL, NULL);
947 static int recordExtract (SYSNO *sysno, const char *fname,
948 struct recordGroup *rGroup, int deleteFlag,
949 struct file_read_info *fi, RecType recType,
952 struct recExtractCtrl extractCtrl;
956 off_t recordOffset = 0;
958 struct recordLogInfo logInfo;
962 /* we are going to read from a file, so prepare the extraction */
964 extractCtrl.subType = subType;
965 extractCtrl.init = wordInit;
966 extractCtrl.add = addRecordKey;
968 reckeys.buf_used = 0;
969 reckeys.prevAttrUse = -1;
970 reckeys.prevAttrSet = -1;
971 reckeys.prevSeqNo = 0;
973 recordOffset = fi->file_moffset;
974 extractCtrl.offset = recordOffset;
975 extractCtrl.readf = file_read;
976 extractCtrl.seekf = file_seek;
977 extractCtrl.endf = file_end;
978 extractCtrl.map_chrs_input = map_chrs_input;
979 extractCtrl.flagShowRecords = rGroup->flagShowRecords;
980 if (rGroup->flagShowRecords)
981 printf ("File: %s %ld\n", fname, (long) recordOffset);
983 logInfo.fname = fname;
984 logInfo.recordOffset = recordOffset;
985 logInfo.rGroup = rGroup;
986 log_event_start (recordLogPreamble, &logInfo);
988 r = (*recType->extract)(&extractCtrl);
990 log_event_start (NULL, NULL);
994 /* error occured during extraction ... */
995 if (!rGroup->flagShowRecords &&
996 records_processed < rGroup->fileVerboseLimit)
998 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
999 fname, (long) recordOffset, r);
1003 if (reckeys.buf_used == 0)
1005 /* the extraction process returned no information - the record
1006 is probably empty - unless flagShowRecords is in use */
1007 if (rGroup->flagShowRecords)
1009 logf (LOG_WARN, "No keys generated for file %s", fname);
1010 logf (LOG_WARN, " The file is probably empty");
1015 /* perform match if sysno not known and if match criteria is specified */
1022 if (rGroup->recordId && *rGroup->recordId)
1026 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1030 rinfo = dict_lookup (matchDict, matchStr);
1032 memcpy (sysno, rinfo+1, sizeof(*sysno));
1036 logf (LOG_WARN, "Bad match criteria");
1047 logf (LOG_LOG, "Cannot delete new record");
1050 if (records_processed < rGroup->fileVerboseLimit)
1051 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1052 fname, (long) recordOffset);
1053 rec = rec_new (records);
1054 *sysno = rec->sysno;
1058 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1060 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1066 /* record already exists */
1067 struct recKeys delkeys;
1069 rec = rec_get (records, *sysno);
1071 delkeys.buf_used = rec->size[recInfo_delKeys];
1072 delkeys.buf = rec->info[recInfo_delKeys];
1073 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1076 /* record going to be deleted */
1077 if (!delkeys.buf_used)
1079 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1080 fname, (long) recordOffset);
1081 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1085 if (records_processed < rGroup->fileVerboseLimit)
1086 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1087 fname, (long) recordOffset);
1090 dict_delete (matchDict, matchStr);
1091 rec_del (records, &rec);
1098 /* record going to be updated */
1099 if (!delkeys.buf_used)
1101 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1102 fname, (long) recordOffset);
1103 logf (LOG_WARN, "cannot update file above, storeKeys false");
1107 if (records_processed < rGroup->fileVerboseLimit)
1108 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1109 fname, (long) recordOffset);
1110 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1115 /* update file type */
1116 xfree (rec->info[recInfo_fileType]);
1117 rec->info[recInfo_fileType] =
1118 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1120 /* update filename */
1121 xfree (rec->info[recInfo_filename]);
1122 rec->info[recInfo_filename] =
1123 rec_strdup (fname, &rec->size[recInfo_filename]);
1125 /* update delete keys */
1126 xfree (rec->info[recInfo_delKeys]);
1127 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1130 rec->size[recInfo_delKeys] = reckeys.buf_used;
1131 rec->info[recInfo_delKeys] = reckeys.buf;
1133 reckeys.buf_max = 0;
1135 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1136 rec->size[recInfo_delKeys] = reckeys.buf_used;
1137 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1138 rec->size[recInfo_delKeys]);
1143 rec->info[recInfo_delKeys] = NULL;
1144 rec->size[recInfo_delKeys] = 0;
1147 /* update store data */
1148 xfree (rec->info[recInfo_storeData]);
1149 if (rGroup->flagStoreData == 1)
1151 int size = fi->file_moffset - recordOffset;
1153 size = fi->file_max - recordOffset;
1154 rec->size[recInfo_storeData] = size;
1155 rec->info[recInfo_storeData] = xmalloc (size);
1156 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1158 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1159 (long) recordOffset);
1162 if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1164 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1165 fi->file_max, fname);
1171 rec->info[recInfo_storeData] = NULL;
1172 rec->size[recInfo_storeData] = 0;
1174 /* update database name */
1175 xfree (rec->info[recInfo_databaseName]);
1176 rec->info[recInfo_databaseName] =
1177 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1180 xfree (rec->info[recInfo_offset]);
1182 rec->size[recInfo_offset] = sizeof(recordOffset);
1183 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1184 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1186 /* commit this record */
1187 rec_put (records, &rec);
1192 int fileExtract (SYSNO *sysno, const char *fname,
1193 const struct recordGroup *rGroupP, int deleteFlag)
1201 struct recordGroup rGroupM;
1202 struct recordGroup *rGroup = &rGroupM;
1203 struct file_read_info *fi;
1205 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1207 if (!rGroup->groupName || !*rGroup->groupName)
1210 sprintf (gprefix, "%s.", rGroup->groupName);
1212 logf (LOG_DEBUG, "fileExtract %s", fname);
1214 /* determine file extension */
1215 for (i = strlen(fname); --i >= 0; )
1216 if (fname[i] == '/')
1221 else if (fname[i] == '.')
1223 strcpy (ext, fname+i+1);
1226 /* determine file type - depending on extension */
1227 if (!rGroup->recordType)
1229 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1230 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1232 sprintf (ext_res, "%srecordType", gprefix);
1233 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1235 if (records_processed < rGroup->fileVerboseLimit)
1236 logf (LOG_LOG, "? %s", fname);
1241 if (!rGroup->recordType)
1243 if (records_processed < rGroup->fileVerboseLimit)
1244 logf (LOG_LOG, "? record %s", fname);
1247 if (!(recType = recType_byName (rGroup->recordType, subType)))
1249 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1253 /* determine match criteria */
1254 if (!rGroup->recordId)
1256 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1257 rGroup->recordId = res_get (common_resource, ext_res);
1260 /* determine database name */
1261 if (!rGroup->databaseName)
1263 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1264 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1266 sprintf (ext_res, "%sdatabase", gprefix);
1267 rGroup->databaseName = res_get (common_resource, ext_res);
1270 if (!rGroup->databaseName)
1271 rGroup->databaseName = "Default";
1273 if (rGroup->flagStoreData == -1)
1276 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1277 if (!(sval = res_get (common_resource, ext_res)))
1279 sprintf (ext_res, "%sstoreData", gprefix);
1280 sval = res_get (common_resource, ext_res);
1283 rGroup->flagStoreData = atoi (sval);
1285 if (rGroup->flagStoreData == -1)
1286 rGroup->flagStoreData = 0;
1288 if (rGroup->flagStoreKeys == -1)
1292 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1293 if (!(sval = res_get (common_resource, ext_res)))
1295 sprintf (ext_res, "%sstoreKeys", gprefix);
1296 sval = res_get (common_resource, ext_res);
1299 rGroup->flagStoreKeys = atoi (sval);
1301 if (rGroup->flagStoreKeys == -1)
1302 rGroup->flagStoreKeys = 0;
1304 if (sysno && deleteFlag)
1308 if ((fd = open (fname, O_RDONLY)) == -1)
1310 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1314 fi = file_read_start (fd);
1318 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1320 } while (r && !sysno && fi->file_more);
1321 file_read_stop (fi);