2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.65 1996-11-14 08:57:56 adam
8 * Reduction of storeKeys area.
10 * Revision 1.64 1996/11/08 11:10:16 adam
11 * Buffers used during file match got bigger.
12 * Compressed ISAM support everywhere.
13 * Bug fixes regarding masking characters in queries.
14 * Redesigned Regexp-2 queries.
16 * Revision 1.63 1996/10/29 14:09:39 adam
17 * Use of cisam system - enabled if setting isamc is 1.
19 * Revision 1.62 1996/10/11 10:57:01 adam
20 * New module recctrl. Used to manage records (extract/retrieval).
21 * Several files have been moved to the recctrl sub directory.
23 * Revision 1.61 1996/06/06 12:08:37 quinn
24 * Added showRecord function
26 * Revision 1.60 1996/06/04 10:18:12 adam
27 * Search/scan uses character mapping module.
29 * Revision 1.59 1996/05/14 15:47:07 adam
30 * Cleanup of various buffer size entities.
32 * Revision 1.58 1996/05/14 06:16:38 adam
33 * Compact use/set bytes used in search service.
35 * Revision 1.57 1996/05/13 14:23:04 adam
36 * Work on compaction of set/use bytes in dictionary.
38 * Revision 1.56 1996/05/09 09:54:42 adam
39 * Server supports maps from one logical attributes to a list of physical
41 * The extraction process doesn't make space consuming 'any' keys.
43 * Revision 1.55 1996/05/09 07:28:55 quinn
44 * Work towards phrases and multiple registers
46 * Revision 1.54 1996/05/01 13:46:35 adam
47 * First work on multiple records in one file.
48 * New option, -offset, to the "unread" command in the filter module.
50 * Revision 1.53 1996/04/26 12:09:43 adam
51 * Added a few comments.
53 * Revision 1.52 1996/04/25 13:27:57 adam
54 * Function recordExtract modified so that files with no keys (possibly empty)
57 * Revision 1.51 1996/03/19 11:08:42 adam
58 * Bug fix: Log preamble wasn't always turned off after recordExtract.
60 * Revision 1.50 1996/02/12 18:45:36 adam
61 * New fileVerboseFlag in record group control.
63 * Revision 1.49 1996/02/05 12:29:57 adam
64 * Logging reduced a bit.
65 * The remaining running time is estimated during register merge.
67 * Revision 1.48 1996/02/01 20:53:26 adam
68 * The temporary per-record keys are compacted a little, and duplication
69 * of the per-records keys are avoided when they are saved in the record
72 * Revision 1.47 1996/01/17 14:57:48 adam
73 * Prototype changed for reader functions in extract/retrieve. File
74 * is identified by 'void *' instead of 'int.
76 * Revision 1.46 1995/12/15 14:57:16 adam
79 * Revision 1.45 1995/12/15 12:37:41 adam
80 * In addRecordKeyAny: Writes key only when attrSet != -1.
82 * Revision 1.44 1995/12/12 16:00:54 adam
83 * System call sync(2) used after update/commit.
84 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
87 * Revision 1.43 1995/12/11 09:12:46 adam
88 * The rec_get function returns NULL if record doesn't exist - will
89 * happen in the server if the result set records have been deleted since
90 * the creation of the set (i.e. the search).
91 * The server saves a result temporarily if it is 'volatile', i.e. the
92 * set is register dependent.
94 * Revision 1.42 1995/12/07 17:38:46 adam
95 * Work locking mechanisms for concurrent updates/commit.
97 * Revision 1.41 1995/12/06 16:06:42 adam
98 * Better diagnostics. Work on 'real' dictionary deletion.
100 * Revision 1.40 1995/12/05 16:57:40 adam
101 * More work on regular patterns.
103 * Revision 1.39 1995/12/05 13:20:18 adam
104 * Bug fix: file_read sometimes returned early EOF.
106 * Revision 1.38 1995/12/04 17:59:21 adam
107 * More work on regular expression conversion.
109 * Revision 1.37 1995/12/04 14:22:27 adam
110 * Extra arg to recType_byName.
111 * Started work on new regular expression parsed input to
112 * structured records.
114 * Revision 1.36 1995/11/30 08:34:29 adam
115 * Started work on commit facility.
116 * Changed a few malloc/free to xmalloc/xfree.
118 * Revision 1.35 1995/11/28 14:26:21 adam
119 * Bug fix: recordId with constant wasn't right.
120 * Bug fix: recordId dictionary entry wasn't deleted when needed.
122 * Revision 1.34 1995/11/28 09:09:38 adam
123 * Zebra config renamed.
124 * Use setting 'recordId' to identify record now.
125 * Bug fix in recindex.c: rec_release_blocks was invokeded even
126 * though the blocks were already released.
127 * File traversal properly deletes records when needed.
129 * Revision 1.33 1995/11/27 09:56:20 adam
130 * Record info elements better enumerated. Internal store of records.
132 * Revision 1.32 1995/11/25 10:24:05 adam
133 * More record fields - they are enumerated now.
134 * New options: flagStoreData flagStoreKey.
136 * Revision 1.31 1995/11/24 11:31:35 adam
137 * Commands add & del read filenames from stdin if source directory is
139 * Match criteria supports 'constant' strings.
141 * Revision 1.30 1995/11/22 17:19:16 adam
142 * Record management uses the bfile system.
144 * Revision 1.29 1995/11/21 15:01:14 adam
145 * New general match criteria implemented.
146 * New feature: document groups.
148 * Revision 1.28 1995/11/21 09:20:30 adam
149 * Yet more work on record match.
151 * Revision 1.27 1995/11/20 16:59:45 adam
152 * New update method: the 'old' keys are saved for each records.
154 * Revision 1.26 1995/11/20 11:56:24 adam
155 * Work on new traversal.
157 * Revision 1.25 1995/11/16 15:34:54 adam
158 * Uses new record management system in both indexer and server.
160 * Revision 1.24 1995/11/15 19:13:08 adam
161 * Work on record management.
163 * Revision 1.23 1995/10/27 14:00:10 adam
164 * Implemented detection of database availability.
166 * Revision 1.22 1995/10/17 18:02:07 adam
167 * New feature: databases. Implemented as prefix to words in dictionary.
169 * Revision 1.21 1995/10/10 12:24:38 adam
170 * Temporary sort files are compressed.
172 * Revision 1.20 1995/10/06 13:52:05 adam
173 * Bug fixes. Handler may abort further scanning.
175 * Revision 1.19 1995/10/04 12:55:16 adam
176 * Bug fix in ranked search. Use=Any keys inserted.
178 * Revision 1.18 1995/10/04 09:37:08 quinn
181 * Revision 1.17 1995/10/03 14:28:57 adam
182 * Buffered read in extract works.
184 * Revision 1.16 1995/10/03 14:28:45 adam
185 * Work on more effecient read handler in extract.
187 * Revision 1.15 1995/10/02 15:42:53 adam
188 * Extract uses file descriptors instead of FILE pointers.
190 * Revision 1.14 1995/10/02 15:29:13 adam
191 * More logging in file_extract.
193 * Revision 1.13 1995/09/29 14:01:39 adam
196 * Revision 1.12 1995/09/28 14:22:56 adam
197 * Sort uses smaller temporary files.
199 * Revision 1.11 1995/09/28 12:10:31 adam
200 * Bug fixes. Field prefix used in queries.
202 * Revision 1.10 1995/09/28 09:19:41 adam
203 * xfree/xmalloc used everywhere.
204 * Extract/retrieve method seems to work for text records.
206 * Revision 1.9 1995/09/27 12:22:28 adam
207 * More work on extract in record control.
208 * Field name is not in isam keys but in prefix in dictionary words.
210 * Revision 1.8 1995/09/14 07:48:22 adam
211 * Record control management.
213 * Revision 1.7 1995/09/11 13:09:32 adam
214 * More work on relevance feedback.
216 * Revision 1.6 1995/09/08 14:52:27 adam
217 * Minor changes. Dictionary is lower case now.
219 * Revision 1.5 1995/09/06 16:11:16 adam
220 * Option: only one word key per file.
222 * Revision 1.4 1995/09/05 15:28:39 adam
223 * More work on search engine.
225 * Revision 1.3 1995/09/04 12:33:41 adam
226 * Various cleanup. YAZ util used instead.
228 * Revision 1.2 1995/09/04 09:10:34 adam
229 * More work on index add/del/update.
230 * Merge sort implemented.
231 * Initial work on z39 server.
233 * Revision 1.1 1995/09/01 14:06:35 adam
234 * Split of work into more files.
247 static Dict matchDict;
249 static Records records = NULL;
251 static char **key_buf;
252 static size_t ptr_top;
254 static size_t key_buf_used;
255 static int key_file_no;
257 static int records_inserted = 0;
258 static int records_updated = 0;
259 static int records_deleted = 0;
260 static int records_processed = 0;
262 static ZebTargetInfo *zti = NULL;
264 static void logRecord (int showFlag)
268 if (showFlag || !(records_processed % 1000))
270 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
271 records_processed, records_inserted, records_updated,
276 void key_open (int mem)
279 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
282 key_buf = xmalloc (mem);
283 ptr_top = mem/sizeof(char*);
289 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
291 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
295 records = rec_open (1);
297 zti = zebTargetInfo_open (records, 1);
307 void encode_key_init (struct encode_info *i)
313 char *encode_key_int (int d, char *bp)
322 else if (d <= 4194303)
324 *bp++ = 128 + (d>>16);
325 *bp++ = (d>>8) & 255;
330 *bp++ = 192 + (d>>24);
331 *bp++ = (d>>16) & 255;
332 *bp++ = (d>>8) & 255;
338 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
343 while ((*bp++ = *k++))
345 memcpy (&key, k+1, sizeof(struct it_key));
346 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
347 if (i->sysno != key.sysno)
349 i->sysno = key.sysno;
352 bp = encode_key_int (key.seqno - i->seqno, bp);
353 i->seqno = key.seqno;
354 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
356 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
364 static int key_y_len;
366 static int key_y_compare (const void *p1, const void *p2)
370 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
371 *(char**) p2 + key_y_len + 1)))
373 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
376 static int key_x_compare (const void *p1, const void *p2)
378 return strcmp (*(char**) p1, *(char**) p2);
382 void key_flush (void)
387 struct encode_info encode_info;
396 logf (LOG_LOG, "sorting section %d", key_file_no);
398 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
399 getFnameTmp (out_fname, key_file_no);
401 if (!(outf = fopen (out_fname, "w")))
403 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
406 logf (LOG_LOG, "writing section %d", key_file_no);
407 prevcp = cp = key_buf[ptr_top-ptr_i];
409 encode_key_init (&encode_info);
410 encode_key_write (cp, &encode_info, outf);
413 cp = key_buf[ptr_top-ptr_i];
414 if (strcmp (cp, prevcp))
416 encode_key_init (&encode_info);
417 encode_key_write (cp, &encode_info, outf);
421 encode_key_write (cp + strlen(cp), &encode_info, outf);
424 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_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);
434 prevcp = key_buf[ptr_top-i];
436 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
438 key_y_len = strlen(prevcp)+1;
440 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
441 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
443 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
444 sizeof(char*), key_y_compare);
445 cp = key_buf[ptr_top-ptr_i];
447 encode_key_init (&encode_info);
448 encode_key_write (cp, &encode_info, outf);
451 cp = key_buf[ptr_top-ptr_i];
452 encode_key_write (cp+key_y_len, &encode_info, outf);
456 prevcp = key_buf[ptr_top-ptr_i];
461 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
464 logf (LOG_LOG, "finished section %d", key_file_no);
474 zebTargetInfo_close (zti, 1);
476 rec_close (&records);
477 dict_close (matchDict);
483 static void wordInit (RecWord *p)
487 p->which = Word_String;
499 static void addRecordKey (const RecWord *p)
508 if (reckeys.buf_used+1024 > reckeys.buf_max)
512 b = xmalloc (reckeys.buf_max += 128000);
513 if (reckeys.buf_used > 0)
514 memcpy (b, reckeys.buf, reckeys.buf_used);
518 dst = reckeys.buf + reckeys.buf_used;
520 attrSet = p->attrSet;
521 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
524 reckeys.prevAttrSet = attrSet;
525 attrUse = p->attrUse;
526 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
529 reckeys.prevAttrUse = attrUse;
531 diff = 1 + p->seqno - reckeys.prevSeqNo;
532 if (diff >= 1 && diff <= 15)
537 reckeys.prevSeqNo = p->seqno;
543 memcpy (dst, &attrSet, sizeof(attrSet));
544 dst += sizeof(attrSet);
548 memcpy (dst, &attrUse, sizeof(attrUse));
549 dst += sizeof(attrUse);
562 for (i = 0; p->u.string[i]; i++)
563 *dst++ = p->u.string[i];
568 memcpy (dst, &p->seqno, sizeof(p->seqno));
569 dst += sizeof(p->seqno);
571 reckeys.buf_used = dst - reckeys.buf;
574 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
575 const char *databaseName)
582 if (zebTargetInfo_curDatabase (zti, databaseName))
584 if (zebTargetInfo_newDatabase (zti, databaseName))
587 while (off < reckeys->buf_used)
589 const char *src = reckeys->buf + off;
597 memcpy (&attrSet, src, sizeof(attrSet));
598 src += sizeof(attrSet);
602 memcpy (&attrUse, src, sizeof(attrUse));
603 src += sizeof(attrUse);
605 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
608 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
610 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
612 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
614 ((char*) key_buf) [key_buf_used++] = ch;
616 ((char*)key_buf) [key_buf_used++] = *src++;
618 ((char*)key_buf) [key_buf_used++] = '\0';
619 ((char*) key_buf)[key_buf_used++] = cmd;
622 seqno += ((lead>>2) & 15)-1;
625 memcpy (&seqno, src, sizeof(seqno));
626 src += sizeof(seqno);
630 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
631 key_buf_used += sizeof(key);
632 off = src - reckeys->buf;
634 assert (off == reckeys->buf_used);
637 static const char **searchRecordKey (struct recKeys *reckeys,
638 int attrSetS, int attrUseS)
640 static const char *ws[32];
645 for (i = 0; i<32; i++)
648 while (off < reckeys->buf_used)
650 const char *src = reckeys->buf + off;
656 memcpy (&attrSet, src, sizeof(attrSet));
657 src += sizeof(attrSet);
659 memcpy (&attrUse, src, sizeof(attrUse));
660 src += sizeof(attrUse);
666 memcpy (&seqno, src, sizeof(seqno));
667 src += sizeof(seqno);
670 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
672 if (attrUseS == attrUse && attrSetS == attrSet)
679 woff = seqno - startSeq;
680 if (woff >= 0 && woff < 31)
684 off = src - reckeys->buf;
686 assert (off == reckeys->buf_used);
690 struct file_read_info {
698 static struct file_read_info *file_read_start (int fd)
700 struct file_read_info *fi = xmalloc (sizeof(*fi));
704 fi->file_moffset = 0;
708 static void file_read_stop (struct file_read_info *fi)
714 static off_t file_seek (void *handle, off_t offset)
716 struct file_read_info *p = handle;
717 p->file_offset = offset;
718 return lseek (p->fd, offset, SEEK_SET);
721 static int file_read (void *handle, char *buf, size_t count)
723 struct file_read_info *p = handle;
726 r = read (fd, buf, count);
730 if (p->file_offset > p->file_max)
731 p->file_max = p->file_offset;
736 static void file_begin (void *handle)
738 struct file_read_info *p = handle;
740 p->file_offset = p->file_moffset;
742 lseek (p->fd, p->file_moffset, SEEK_SET);
746 static void file_end (void *handle, off_t offset)
748 struct file_read_info *p = handle;
750 assert (p->file_more == 0);
752 p->file_moffset = offset;
755 static int atois (const char **s)
758 while ( (c=**s) >= '0' && c <= '9')
760 val = val*10 + c - '0';
766 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
770 static char dstBuf[2048];
772 const char *s = spec;
773 static const char **w;
778 while (*s == ' ' || *s == '\t')
785 int attrSet, attrUse;
789 attrSet = atois (&s);
792 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
793 spec, rGroup->groupName ? rGroup->groupName : "none");
797 attrUse = atois (&s);
798 w = searchRecordKey (reckeys, attrSet, attrUse);
803 for (i = 0; i<32; i++)
808 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
809 spec, rGroup->groupName ? rGroup->groupName : "none");
814 for (i = 0; i<32; i++)
815 if (matchFlag[i] && w[i])
827 logf (LOG_WARN, "Record didn't contain match"
828 " fields in (%d,%d)", attrSet, attrUse);
836 const char *spec_src = NULL;
837 const char *s1 = ++s;
838 while (*s1 && *s1 != ' ' && *s1 != '\t')
844 memcpy (special, s, spec_len);
845 special[spec_len] = '\0';
848 if (!strcmp (special, "group"))
849 spec_src = rGroup->groupName;
850 else if (!strcmp (special, "database"))
851 spec_src = rGroup->databaseName;
852 else if (!strcmp (special, "filename"))
854 else if (!strcmp (special, "type"))
855 spec_src = rGroup->recordType;
860 strcpy (dst, spec_src);
861 dst += strlen (spec_src);
864 else if (*s == '\"' || *s == '\'')
866 int stopMarker = *s++;
870 while (*s && *s != stopMarker)
873 tmpString[i++] = *s++;
878 strcpy (dst, tmpString);
879 dst += strlen (tmpString);
883 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
884 spec, rGroup->groupName ? rGroup->groupName : "none");
891 logf (LOG_WARN, "No match criteria for record %s in group %s",
892 fname, rGroup->groupName ? rGroup->groupName : "none");
898 struct recordLogInfo {
901 struct recordGroup *rGroup;
904 static void recordLogPreamble (int level, const char *msg, void *info)
906 struct recordLogInfo *p = info;
907 FILE *outf = log_file ();
912 fprintf (outf, "%s of ", p->op);
913 fprintf (outf, "%s type %s\n", p->rGroup->recordType, p->fname);
914 log_event_start (NULL, NULL);
917 static int recordExtract (SYSNO *sysno, const char *fname,
918 struct recordGroup *rGroup, int deleteFlag,
919 struct file_read_info *fi, RecType recType,
922 struct recExtractCtrl extractCtrl;
926 off_t recordOffset = 0;
928 struct recordLogInfo logInfo;
930 logInfo.fname = fname;
932 logInfo.rGroup = rGroup;
933 log_event_start (recordLogPreamble, &logInfo);
937 /* we are going to read from a file, so prepare the extraction */
939 extractCtrl.subType = subType;
940 extractCtrl.init = wordInit;
941 extractCtrl.add = addRecordKey;
943 reckeys.buf_used = 0;
944 reckeys.prevAttrUse = -1;
945 reckeys.prevAttrSet = -1;
946 reckeys.prevSeqNo = 0;
948 recordOffset = fi->file_moffset;
949 extractCtrl.offset = recordOffset;
950 extractCtrl.readf = file_read;
951 extractCtrl.seekf = file_seek;
952 extractCtrl.endf = file_end;
953 extractCtrl.map_chrs_input = map_chrs_input;
954 extractCtrl.flagShowRecords = rGroup->flagShowRecords;
955 r = (*recType->extract)(&extractCtrl);
959 /* error occured during extraction ... */
960 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
963 if (reckeys.buf_used == 0)
965 /* the extraction process returned no information - the record
967 logf (LOG_WARN, "Empty file %s", fname);
972 /* perform match if sysno not known and if match criteria is specified */
979 if (rGroup->recordId && *rGroup->recordId)
983 matchStr = fileMatchStr (&reckeys, rGroup, fname,
987 rinfo = dict_lookup (matchDict, matchStr);
989 memcpy (sysno, rinfo+1, sizeof(*sysno));
993 logf (LOG_WARN, "Bad match criteria");
1004 logf (LOG_LOG, "Cannot delete new record");
1008 if (rGroup->fileVerboseFlag)
1009 logf (LOG_LOG, "add %s %s+%ld", rGroup->recordType,
1010 fname, (long) recordOffset);
1011 rec = rec_new (records);
1012 *sysno = rec->sysno;
1016 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1018 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1024 /* record already exists */
1025 struct recKeys delkeys;
1027 rec = rec_get (records, *sysno);
1029 delkeys.buf_used = rec->size[recInfo_delKeys];
1030 delkeys.buf = rec->info[recInfo_delKeys];
1031 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1034 /* record going to be deleted */
1035 logInfo.op = "delete";
1036 if (!delkeys.buf_used)
1038 logf (LOG_WARN, "cannot delete; storeKeys false");
1042 if (rGroup->fileVerboseFlag)
1043 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1044 fname, (long) recordOffset);
1047 dict_delete (matchDict, matchStr);
1048 rec_del (records, &rec);
1055 /* record going to be updated */
1056 logInfo.op = "update";
1057 if (!delkeys.buf_used)
1059 logf (LOG_WARN, "cannot update; storeKeys false");
1063 if (rGroup->fileVerboseFlag)
1064 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1065 fname, (long) recordOffset);
1066 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1071 /* update file type */
1072 xfree (rec->info[recInfo_fileType]);
1073 rec->info[recInfo_fileType] =
1074 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1076 /* update filename */
1077 xfree (rec->info[recInfo_filename]);
1078 rec->info[recInfo_filename] =
1079 rec_strdup (fname, &rec->size[recInfo_filename]);
1081 /* update delete keys */
1082 xfree (rec->info[recInfo_delKeys]);
1083 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1086 rec->size[recInfo_delKeys] = reckeys.buf_used;
1087 rec->info[recInfo_delKeys] = reckeys.buf;
1089 reckeys.buf_max = 0;
1091 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1092 rec->size[recInfo_delKeys] = reckeys.buf_used;
1093 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1094 rec->size[recInfo_delKeys]);
1099 rec->info[recInfo_delKeys] = NULL;
1100 rec->size[recInfo_delKeys] = 0;
1103 /* update store data */
1104 xfree (rec->info[recInfo_storeData]);
1105 if (rGroup->flagStoreData == 1)
1107 rec->size[recInfo_storeData] = fi->file_max;
1108 rec->info[recInfo_storeData] = xmalloc (fi->file_max);
1109 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1111 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1112 (long) recordOffset);
1115 if (read (fi->fd, rec->info[recInfo_storeData], fi->file_max)
1118 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1119 fi->file_max, fname);
1125 rec->info[recInfo_storeData] = NULL;
1126 rec->size[recInfo_storeData] = 0;
1128 /* update database name */
1129 xfree (rec->info[recInfo_databaseName]);
1130 rec->info[recInfo_databaseName] =
1131 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1134 xfree (rec->info[recInfo_offset]);
1136 rec->size[recInfo_offset] = sizeof(recordOffset);
1137 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1138 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1140 /* commit this record */
1141 rec_put (records, &rec);
1146 int fileExtract (SYSNO *sysno, const char *fname,
1147 const struct recordGroup *rGroupP, int deleteFlag)
1155 struct recordGroup rGroupM;
1156 struct recordGroup *rGroup = &rGroupM;
1157 struct file_read_info *fi;
1159 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1161 if (!rGroup->groupName || !*rGroup->groupName)
1164 sprintf (gprefix, "%s.", rGroup->groupName);
1166 logf (LOG_DEBUG, "fileExtract %s", fname);
1168 /* determine file extension */
1169 for (i = strlen(fname); --i >= 0; )
1170 if (fname[i] == '/')
1175 else if (fname[i] == '.')
1177 strcpy (ext, fname+i+1);
1180 /* determine file type - depending on extension */
1181 if (!rGroup->recordType)
1183 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1184 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1186 sprintf (ext_res, "%srecordType", gprefix);
1187 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1189 if (rGroup->fileVerboseFlag)
1190 logf (LOG_LOG, "? %s", fname);
1195 if (!rGroup->recordType)
1197 if (rGroup->fileVerboseFlag)
1198 logf (LOG_LOG, "? record %s", fname);
1201 if (!(recType = recType_byName (rGroup->recordType, subType)))
1203 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1207 /* determine match criteria */
1208 if (!rGroup->recordId)
1210 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1211 rGroup->recordId = res_get (common_resource, ext_res);
1214 /* determine database name */
1215 if (!rGroup->databaseName)
1217 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1218 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1220 sprintf (ext_res, "%sdatabase", gprefix);
1221 rGroup->databaseName = res_get (common_resource, ext_res);
1224 if (!rGroup->databaseName)
1225 rGroup->databaseName = "Default";
1227 if (rGroup->flagStoreData == -1)
1230 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1231 if (!(sval = res_get (common_resource, ext_res)))
1233 sprintf (ext_res, "%sstoreData", gprefix);
1234 sval = res_get (common_resource, ext_res);
1237 rGroup->flagStoreData = atoi (sval);
1239 if (rGroup->flagStoreData == -1)
1240 rGroup->flagStoreData = 0;
1242 if (rGroup->flagStoreKeys == -1)
1246 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1247 if (!(sval = res_get (common_resource, ext_res)))
1249 sprintf (ext_res, "%sstoreKeys", gprefix);
1250 sval = res_get (common_resource, ext_res);
1253 rGroup->flagStoreKeys = atoi (sval);
1255 if (rGroup->flagStoreKeys == -1)
1256 rGroup->flagStoreKeys = 0;
1258 if (sysno && deleteFlag)
1262 if ((fd = open (fname, O_RDONLY)) == -1)
1264 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1268 fi = file_read_start (fd);
1272 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1274 } while (r && !sysno && fi->file_more);
1275 log_event_start (NULL, NULL);
1276 file_read_stop (fi);