2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.73 1997-09-04 13:57:20 adam
8 * New file extract/retrieve method tellf (added).
9 * Added O_BINARY for open calls.
11 * Revision 1.72 1997/07/15 16:32:29 adam
12 * Bug fix: Match handler didn't terminate the resulting string!
14 * Revision 1.71 1997/07/15 16:28:41 adam
15 * Bug fix: storeData didn't work with files with multiple records.
16 * Bug fix: fixed memory management with records; not really well
19 * Revision 1.70 1997/07/01 13:00:42 adam
20 * Bug fix in routine searchRecordKey: uninitialized variables.
22 * Revision 1.69 1997/04/29 09:26:03 adam
23 * Bug fix: generic recordId handling didn't work for compressed internal
26 * Revision 1.68 1997/02/12 20:39:45 adam
27 * Implemented options -f <n> that limits the log to the first <n>
29 * Changed some log messages also.
31 * Revision 1.67 1996/11/15 15:02:14 adam
32 * Minor changes regarding logging.
34 * Revision 1.66 1996/11/14 09:52:21 adam
35 * Strings in record keys bound by IT_MAX_WORD.
37 * Revision 1.65 1996/11/14 08:57:56 adam
38 * Reduction of storeKeys area.
40 * Revision 1.64 1996/11/08 11:10:16 adam
41 * Buffers used during file match got bigger.
42 * Compressed ISAM support everywhere.
43 * Bug fixes regarding masking characters in queries.
44 * Redesigned Regexp-2 queries.
46 * Revision 1.63 1996/10/29 14:09:39 adam
47 * Use of cisam system - enabled if setting isamc is 1.
49 * Revision 1.62 1996/10/11 10:57:01 adam
50 * New module recctrl. Used to manage records (extract/retrieval).
51 * Several files have been moved to the recctrl sub directory.
53 * Revision 1.61 1996/06/06 12:08:37 quinn
54 * Added showRecord function
56 * Revision 1.60 1996/06/04 10:18:12 adam
57 * Search/scan uses character mapping module.
59 * Revision 1.59 1996/05/14 15:47:07 adam
60 * Cleanup of various buffer size entities.
62 * Revision 1.58 1996/05/14 06:16:38 adam
63 * Compact use/set bytes used in search service.
65 * Revision 1.57 1996/05/13 14:23:04 adam
66 * Work on compaction of set/use bytes in dictionary.
68 * Revision 1.56 1996/05/09 09:54:42 adam
69 * Server supports maps from one logical attributes to a list of physical
71 * The extraction process doesn't make space consuming 'any' keys.
73 * Revision 1.55 1996/05/09 07:28:55 quinn
74 * Work towards phrases and multiple registers
76 * Revision 1.54 1996/05/01 13:46:35 adam
77 * First work on multiple records in one file.
78 * New option, -offset, to the "unread" command in the filter module.
80 * Revision 1.53 1996/04/26 12:09:43 adam
81 * Added a few comments.
83 * Revision 1.52 1996/04/25 13:27:57 adam
84 * Function recordExtract modified so that files with no keys (possibly empty)
87 * Revision 1.51 1996/03/19 11:08:42 adam
88 * Bug fix: Log preamble wasn't always turned off after recordExtract.
90 * Revision 1.50 1996/02/12 18:45:36 adam
91 * New fileVerboseFlag in record group control.
93 * Revision 1.49 1996/02/05 12:29:57 adam
94 * Logging reduced a bit.
95 * The remaining running time is estimated during register merge.
97 * Revision 1.48 1996/02/01 20:53:26 adam
98 * The temporary per-record keys are compacted a little, and duplication
99 * of the per-records keys are avoided when they are saved in the record
100 * information buffer.
102 * Revision 1.47 1996/01/17 14:57:48 adam
103 * Prototype changed for reader functions in extract/retrieve. File
104 * is identified by 'void *' instead of 'int.
106 * Revision 1.46 1995/12/15 14:57:16 adam
109 * Revision 1.45 1995/12/15 12:37:41 adam
110 * In addRecordKeyAny: Writes key only when attrSet != -1.
112 * Revision 1.44 1995/12/12 16:00:54 adam
113 * System call sync(2) used after update/commit.
114 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
117 * Revision 1.43 1995/12/11 09:12:46 adam
118 * The rec_get function returns NULL if record doesn't exist - will
119 * happen in the server if the result set records have been deleted since
120 * the creation of the set (i.e. the search).
121 * The server saves a result temporarily if it is 'volatile', i.e. the
122 * set is register dependent.
124 * Revision 1.42 1995/12/07 17:38:46 adam
125 * Work locking mechanisms for concurrent updates/commit.
127 * Revision 1.41 1995/12/06 16:06:42 adam
128 * Better diagnostics. Work on 'real' dictionary deletion.
130 * Revision 1.40 1995/12/05 16:57:40 adam
131 * More work on regular patterns.
133 * Revision 1.39 1995/12/05 13:20:18 adam
134 * Bug fix: file_read sometimes returned early EOF.
136 * Revision 1.38 1995/12/04 17:59:21 adam
137 * More work on regular expression conversion.
139 * Revision 1.37 1995/12/04 14:22:27 adam
140 * Extra arg to recType_byName.
141 * Started work on new regular expression parsed input to
142 * structured records.
144 * Revision 1.36 1995/11/30 08:34:29 adam
145 * Started work on commit facility.
146 * Changed a few malloc/free to xmalloc/xfree.
148 * Revision 1.35 1995/11/28 14:26:21 adam
149 * Bug fix: recordId with constant wasn't right.
150 * Bug fix: recordId dictionary entry wasn't deleted when needed.
152 * Revision 1.34 1995/11/28 09:09:38 adam
153 * Zebra config renamed.
154 * Use setting 'recordId' to identify record now.
155 * Bug fix in recindex.c: rec_release_blocks was invokeded even
156 * though the blocks were already released.
157 * File traversal properly deletes records when needed.
159 * Revision 1.33 1995/11/27 09:56:20 adam
160 * Record info elements better enumerated. Internal store of records.
162 * Revision 1.32 1995/11/25 10:24:05 adam
163 * More record fields - they are enumerated now.
164 * New options: flagStoreData flagStoreKey.
166 * Revision 1.31 1995/11/24 11:31:35 adam
167 * Commands add & del read filenames from stdin if source directory is
169 * Match criteria supports 'constant' strings.
171 * Revision 1.30 1995/11/22 17:19:16 adam
172 * Record management uses the bfile system.
174 * Revision 1.29 1995/11/21 15:01:14 adam
175 * New general match criteria implemented.
176 * New feature: document groups.
178 * Revision 1.28 1995/11/21 09:20:30 adam
179 * Yet more work on record match.
181 * Revision 1.27 1995/11/20 16:59:45 adam
182 * New update method: the 'old' keys are saved for each records.
184 * Revision 1.26 1995/11/20 11:56:24 adam
185 * Work on new traversal.
187 * Revision 1.25 1995/11/16 15:34:54 adam
188 * Uses new record management system in both indexer and server.
190 * Revision 1.24 1995/11/15 19:13:08 adam
191 * Work on record management.
193 * Revision 1.23 1995/10/27 14:00:10 adam
194 * Implemented detection of database availability.
196 * Revision 1.22 1995/10/17 18:02:07 adam
197 * New feature: databases. Implemented as prefix to words in dictionary.
199 * Revision 1.21 1995/10/10 12:24:38 adam
200 * Temporary sort files are compressed.
202 * Revision 1.20 1995/10/06 13:52:05 adam
203 * Bug fixes. Handler may abort further scanning.
205 * Revision 1.19 1995/10/04 12:55:16 adam
206 * Bug fix in ranked search. Use=Any keys inserted.
208 * Revision 1.18 1995/10/04 09:37:08 quinn
211 * Revision 1.17 1995/10/03 14:28:57 adam
212 * Buffered read in extract works.
214 * Revision 1.16 1995/10/03 14:28:45 adam
215 * Work on more effecient read handler in extract.
217 * Revision 1.15 1995/10/02 15:42:53 adam
218 * Extract uses file descriptors instead of FILE pointers.
220 * Revision 1.14 1995/10/02 15:29:13 adam
221 * More logging in file_extract.
223 * Revision 1.13 1995/09/29 14:01:39 adam
226 * Revision 1.12 1995/09/28 14:22:56 adam
227 * Sort uses smaller temporary files.
229 * Revision 1.11 1995/09/28 12:10:31 adam
230 * Bug fixes. Field prefix used in queries.
232 * Revision 1.10 1995/09/28 09:19:41 adam
233 * xfree/xmalloc used everywhere.
234 * Extract/retrieve method seems to work for text records.
236 * Revision 1.9 1995/09/27 12:22:28 adam
237 * More work on extract in record control.
238 * Field name is not in isam keys but in prefix in dictionary words.
240 * Revision 1.8 1995/09/14 07:48:22 adam
241 * Record control management.
243 * Revision 1.7 1995/09/11 13:09:32 adam
244 * More work on relevance feedback.
246 * Revision 1.6 1995/09/08 14:52:27 adam
247 * Minor changes. Dictionary is lower case now.
249 * Revision 1.5 1995/09/06 16:11:16 adam
250 * Option: only one word key per file.
252 * Revision 1.4 1995/09/05 15:28:39 adam
253 * More work on search engine.
255 * Revision 1.3 1995/09/04 12:33:41 adam
256 * Various cleanup. YAZ util used instead.
258 * Revision 1.2 1995/09/04 09:10:34 adam
259 * More work on index add/del/update.
260 * Merge sort implemented.
261 * Initial work on z39 server.
263 * Revision 1.1 1995/09/01 14:06:35 adam
264 * Split of work into more files.
277 static Dict matchDict;
279 static Records records = NULL;
281 static char **key_buf;
282 static size_t ptr_top;
284 static size_t key_buf_used;
285 static int key_file_no;
287 static int records_inserted = 0;
288 static int records_updated = 0;
289 static int records_deleted = 0;
290 static int records_processed = 0;
292 static ZebTargetInfo *zti = NULL;
294 static void logRecord (int showFlag)
298 if (showFlag || !(records_processed % 1000))
300 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
301 records_processed, records_inserted, records_updated,
306 void key_open (int mem)
309 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
312 key_buf = xmalloc (mem);
313 ptr_top = mem/sizeof(char*);
319 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
321 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
325 records = rec_open (1);
327 zti = zebTargetInfo_open (records, 1);
337 void encode_key_init (struct encode_info *i)
343 char *encode_key_int (int d, char *bp)
352 else if (d <= 4194303)
354 *bp++ = 128 + (d>>16);
355 *bp++ = (d>>8) & 255;
360 *bp++ = 192 + (d>>24);
361 *bp++ = (d>>16) & 255;
362 *bp++ = (d>>8) & 255;
368 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
373 while ((*bp++ = *k++))
375 memcpy (&key, k+1, sizeof(struct it_key));
376 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
377 if (i->sysno != key.sysno)
379 i->sysno = key.sysno;
382 bp = encode_key_int (key.seqno - i->seqno, bp);
383 i->seqno = key.seqno;
384 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
386 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
394 static int key_y_len;
396 static int key_y_compare (const void *p1, const void *p2)
400 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
401 *(char**) p2 + key_y_len + 1)))
403 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
406 static int key_x_compare (const void *p1, const void *p2)
408 return strcmp (*(char**) p1, *(char**) p2);
412 void key_flush (void)
417 struct encode_info encode_info;
426 logf (LOG_LOG, "sorting section %d", key_file_no);
428 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
429 getFnameTmp (out_fname, key_file_no);
431 if (!(outf = fopen (out_fname, "w")))
433 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
436 logf (LOG_LOG, "writing section %d", key_file_no);
437 prevcp = cp = key_buf[ptr_top-ptr_i];
439 encode_key_init (&encode_info);
440 encode_key_write (cp, &encode_info, outf);
443 cp = key_buf[ptr_top-ptr_i];
444 if (strcmp (cp, prevcp))
446 encode_key_init (&encode_info);
447 encode_key_write (cp, &encode_info, outf);
451 encode_key_write (cp + strlen(cp), &encode_info, outf);
454 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
455 getFnameTmp (out_fname, key_file_no);
457 if (!(outf = fopen (out_fname, "w")))
459 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
462 logf (LOG_LOG, "writing section %d", key_file_no);
464 prevcp = key_buf[ptr_top-i];
466 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
468 key_y_len = strlen(prevcp)+1;
470 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
471 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
473 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
474 sizeof(char*), key_y_compare);
475 cp = key_buf[ptr_top-ptr_i];
477 encode_key_init (&encode_info);
478 encode_key_write (cp, &encode_info, outf);
481 cp = key_buf[ptr_top-ptr_i];
482 encode_key_write (cp+key_y_len, &encode_info, outf);
486 prevcp = key_buf[ptr_top-ptr_i];
491 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
494 logf (LOG_LOG, "finished section %d", key_file_no);
504 zebTargetInfo_close (zti, 1);
506 rec_close (&records);
507 dict_close (matchDict);
513 static void wordInit (RecWord *p)
517 p->which = Word_String;
529 static void addRecordKey (const RecWord *p)
538 if (reckeys.buf_used+1024 > reckeys.buf_max)
542 b = xmalloc (reckeys.buf_max += 128000);
543 if (reckeys.buf_used > 0)
544 memcpy (b, reckeys.buf, reckeys.buf_used);
548 dst = reckeys.buf + reckeys.buf_used;
550 attrSet = p->attrSet;
551 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
554 reckeys.prevAttrSet = attrSet;
555 attrUse = p->attrUse;
556 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
559 reckeys.prevAttrUse = attrUse;
561 diff = 1 + p->seqno - reckeys.prevSeqNo;
562 if (diff >= 1 && diff <= 15)
567 reckeys.prevSeqNo = p->seqno;
573 memcpy (dst, &attrSet, sizeof(attrSet));
574 dst += sizeof(attrSet);
578 memcpy (dst, &attrUse, sizeof(attrUse));
579 dst += sizeof(attrUse);
592 for (i = 0; p->u.string[i] && i < IT_MAX_WORD-3; i++)
593 *dst++ = p->u.string[i];
598 memcpy (dst, &p->seqno, sizeof(p->seqno));
599 dst += sizeof(p->seqno);
601 reckeys.buf_used = dst - reckeys.buf;
604 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
605 const char *databaseName)
612 if (zebTargetInfo_curDatabase (zti, databaseName))
614 if (zebTargetInfo_newDatabase (zti, databaseName))
617 while (off < reckeys->buf_used)
619 const char *src = reckeys->buf + off;
627 memcpy (&attrSet, src, sizeof(attrSet));
628 src += sizeof(attrSet);
632 memcpy (&attrUse, src, sizeof(attrUse));
633 src += sizeof(attrUse);
635 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
638 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
640 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
642 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
644 ((char*) key_buf) [key_buf_used++] = ch;
646 ((char*)key_buf) [key_buf_used++] = *src++;
648 ((char*)key_buf) [key_buf_used++] = '\0';
649 ((char*) key_buf)[key_buf_used++] = cmd;
652 seqno += ((lead>>2) & 15)-1;
655 memcpy (&seqno, src, sizeof(seqno));
656 src += sizeof(seqno);
660 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
661 key_buf_used += sizeof(key);
662 off = src - reckeys->buf;
664 assert (off == reckeys->buf_used);
667 static const char **searchRecordKey (struct recKeys *reckeys,
668 int attrSetS, int attrUseS)
670 static const char *ws[32];
678 for (i = 0; i<32; i++)
681 while (off < reckeys->buf_used)
684 const char *src = reckeys->buf + off;
692 memcpy (&attrSet, src, sizeof(attrSet));
693 src += sizeof(attrSet);
697 memcpy (&attrUse, src, sizeof(attrUse));
698 src += sizeof(attrUse);
704 seqno += ((lead>>2) & 15)-1;
707 memcpy (&seqno, src, sizeof(seqno));
708 src += sizeof(seqno);
710 if (attrUseS == attrUse && attrSetS == attrSet)
717 woff = seqno - startSeq;
718 if (woff >= 0 && woff < 31)
722 off = src - reckeys->buf;
724 assert (off == reckeys->buf_used);
728 struct file_read_info {
736 static struct file_read_info *file_read_start (int fd)
738 struct file_read_info *fi = xmalloc (sizeof(*fi));
742 fi->file_moffset = 0;
746 static void file_read_stop (struct file_read_info *fi)
752 static off_t file_seek (void *handle, off_t offset)
754 struct file_read_info *p = handle;
755 p->file_offset = offset;
756 return lseek (p->fd, offset, SEEK_SET);
759 static off_t file_tell (void *handle)
761 struct file_read_info *p = handle;
762 return p->file_offset;
765 static int file_read (void *handle, char *buf, size_t count)
767 struct file_read_info *p = handle;
770 r = read (fd, buf, count);
774 if (p->file_offset > p->file_max)
775 p->file_max = p->file_offset;
780 static void file_begin (void *handle)
782 struct file_read_info *p = handle;
784 p->file_offset = p->file_moffset;
786 lseek (p->fd, p->file_moffset, SEEK_SET);
790 static void file_end (void *handle, off_t offset)
792 struct file_read_info *p = handle;
794 assert (p->file_more == 0);
796 p->file_moffset = offset;
799 static int atois (const char **s)
802 while ( (c=**s) >= '0' && c <= '9')
804 val = val*10 + c - '0';
810 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
814 static char dstBuf[2048];
816 const char *s = spec;
817 static const char **w;
822 while (*s == ' ' || *s == '\t')
829 int attrSet, attrUse;
833 attrSet = atois (&s);
836 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
837 spec, rGroup->groupName ? rGroup->groupName : "none");
841 attrUse = atois (&s);
842 w = searchRecordKey (reckeys, attrSet, attrUse);
847 for (i = 0; i<32; i++)
852 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
853 spec, rGroup->groupName ? rGroup->groupName : "none");
858 for (i = 0; i<32; i++)
859 if (matchFlag[i] && w[i])
871 logf (LOG_WARN, "Record didn't contain match"
872 " fields in (%d,%d)", attrSet, attrUse);
880 const char *spec_src = NULL;
881 const char *s1 = ++s;
882 while (*s1 && *s1 != ' ' && *s1 != '\t')
888 memcpy (special, s, spec_len);
889 special[spec_len] = '\0';
892 if (!strcmp (special, "group"))
893 spec_src = rGroup->groupName;
894 else if (!strcmp (special, "database"))
895 spec_src = rGroup->databaseName;
896 else if (!strcmp (special, "filename"))
898 else if (!strcmp (special, "type"))
899 spec_src = rGroup->recordType;
904 strcpy (dst, spec_src);
905 dst += strlen (spec_src);
908 else if (*s == '\"' || *s == '\'')
910 int stopMarker = *s++;
914 while (*s && *s != stopMarker)
917 tmpString[i++] = *s++;
922 strcpy (dst, tmpString);
923 dst += strlen (tmpString);
927 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
928 spec, rGroup->groupName ? rGroup->groupName : "none");
935 logf (LOG_WARN, "No match criteria for record %s in group %s",
936 fname, rGroup->groupName ? rGroup->groupName : "none");
943 struct recordLogInfo {
946 struct recordGroup *rGroup;
949 static void recordLogPreamble (int level, const char *msg, void *info)
951 struct recordLogInfo *p = info;
952 FILE *outf = log_file ();
956 fprintf (outf, "File %s, offset %d, type %s\n",
957 p->rGroup->recordType, p->recordOffset, p->fname);
958 log_event_start (NULL, NULL);
961 static int recordExtract (SYSNO *sysno, const char *fname,
962 struct recordGroup *rGroup, int deleteFlag,
963 struct file_read_info *fi, RecType recType,
966 struct recExtractCtrl extractCtrl;
970 off_t recordOffset = 0;
972 struct recordLogInfo logInfo;
976 /* we are going to read from a file, so prepare the extraction */
978 extractCtrl.subType = subType;
979 extractCtrl.init = wordInit;
980 extractCtrl.add = addRecordKey;
982 reckeys.buf_used = 0;
983 reckeys.prevAttrUse = -1;
984 reckeys.prevAttrSet = -1;
985 reckeys.prevSeqNo = 0;
987 recordOffset = fi->file_moffset;
988 extractCtrl.offset = recordOffset;
989 extractCtrl.readf = file_read;
990 extractCtrl.seekf = file_seek;
991 extractCtrl.tellf = file_tell;
992 extractCtrl.endf = file_end;
993 extractCtrl.map_chrs_input = map_chrs_input;
994 extractCtrl.flagShowRecords = rGroup->flagShowRecords;
995 if (rGroup->flagShowRecords)
996 printf ("File: %s %ld\n", fname, (long) recordOffset);
998 logInfo.fname = fname;
999 logInfo.recordOffset = recordOffset;
1000 logInfo.rGroup = rGroup;
1001 log_event_start (recordLogPreamble, &logInfo);
1003 r = (*recType->extract)(&extractCtrl);
1005 log_event_start (NULL, NULL);
1009 /* error occured during extraction ... */
1010 if (!rGroup->flagShowRecords &&
1011 records_processed < rGroup->fileVerboseLimit)
1013 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1014 fname, (long) recordOffset, r);
1018 if (reckeys.buf_used == 0)
1020 /* the extraction process returned no information - the record
1021 is probably empty - unless flagShowRecords is in use */
1022 if (rGroup->flagShowRecords)
1024 logf (LOG_WARN, "No keys generated for file %s", fname);
1025 logf (LOG_WARN, " The file is probably empty");
1030 /* perform match if sysno not known and if match criteria is specified */
1037 if (rGroup->recordId && *rGroup->recordId)
1041 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1045 rinfo = dict_lookup (matchDict, matchStr);
1047 memcpy (sysno, rinfo+1, sizeof(*sysno));
1051 logf (LOG_WARN, "Bad match criteria");
1062 logf (LOG_LOG, "Cannot delete new record");
1065 if (records_processed < rGroup->fileVerboseLimit)
1066 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1067 fname, (long) recordOffset);
1068 rec = rec_new (records);
1069 *sysno = rec->sysno;
1073 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1075 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1081 /* record already exists */
1082 struct recKeys delkeys;
1084 rec = rec_get (records, *sysno);
1086 delkeys.buf_used = rec->size[recInfo_delKeys];
1087 delkeys.buf = rec->info[recInfo_delKeys];
1088 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1091 /* record going to be deleted */
1092 if (!delkeys.buf_used)
1094 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1095 fname, (long) recordOffset);
1096 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1100 if (records_processed < rGroup->fileVerboseLimit)
1101 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1102 fname, (long) recordOffset);
1105 dict_delete (matchDict, matchStr);
1106 rec_del (records, &rec);
1113 /* record going to be updated */
1114 if (!delkeys.buf_used)
1116 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1117 fname, (long) recordOffset);
1118 logf (LOG_WARN, "cannot update file above, storeKeys false");
1122 if (records_processed < rGroup->fileVerboseLimit)
1123 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1124 fname, (long) recordOffset);
1125 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1130 /* update file type */
1131 xfree (rec->info[recInfo_fileType]);
1132 rec->info[recInfo_fileType] =
1133 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1135 /* update filename */
1136 xfree (rec->info[recInfo_filename]);
1137 rec->info[recInfo_filename] =
1138 rec_strdup (fname, &rec->size[recInfo_filename]);
1140 /* update delete keys */
1141 xfree (rec->info[recInfo_delKeys]);
1142 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1145 rec->size[recInfo_delKeys] = reckeys.buf_used;
1146 rec->info[recInfo_delKeys] = reckeys.buf;
1148 reckeys.buf_max = 0;
1150 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1151 rec->size[recInfo_delKeys] = reckeys.buf_used;
1152 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1153 rec->size[recInfo_delKeys]);
1158 rec->info[recInfo_delKeys] = NULL;
1159 rec->size[recInfo_delKeys] = 0;
1162 /* update store data */
1163 xfree (rec->info[recInfo_storeData]);
1164 if (rGroup->flagStoreData == 1)
1166 int size = fi->file_moffset - recordOffset;
1168 size = fi->file_max - recordOffset;
1169 rec->size[recInfo_storeData] = size;
1170 rec->info[recInfo_storeData] = xmalloc (size);
1171 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1173 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1174 (long) recordOffset);
1177 if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1179 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1180 fi->file_max, fname);
1186 rec->info[recInfo_storeData] = NULL;
1187 rec->size[recInfo_storeData] = 0;
1189 /* update database name */
1190 xfree (rec->info[recInfo_databaseName]);
1191 rec->info[recInfo_databaseName] =
1192 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1195 xfree (rec->info[recInfo_offset]);
1197 rec->size[recInfo_offset] = sizeof(recordOffset);
1198 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1199 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1201 /* commit this record */
1202 rec_put (records, &rec);
1207 int fileExtract (SYSNO *sysno, const char *fname,
1208 const struct recordGroup *rGroupP, int deleteFlag)
1216 struct recordGroup rGroupM;
1217 struct recordGroup *rGroup = &rGroupM;
1218 struct file_read_info *fi;
1220 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1222 if (!rGroup->groupName || !*rGroup->groupName)
1225 sprintf (gprefix, "%s.", rGroup->groupName);
1227 logf (LOG_DEBUG, "fileExtract %s", fname);
1229 /* determine file extension */
1230 for (i = strlen(fname); --i >= 0; )
1231 if (fname[i] == '/')
1236 else if (fname[i] == '.')
1238 strcpy (ext, fname+i+1);
1241 /* determine file type - depending on extension */
1242 if (!rGroup->recordType)
1244 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1245 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1247 sprintf (ext_res, "%srecordType", gprefix);
1248 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1250 if (records_processed < rGroup->fileVerboseLimit)
1251 logf (LOG_LOG, "? %s", fname);
1256 if (!rGroup->recordType)
1258 if (records_processed < rGroup->fileVerboseLimit)
1259 logf (LOG_LOG, "? record %s", fname);
1262 if (!(recType = recType_byName (rGroup->recordType, subType)))
1264 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1268 /* determine match criteria */
1269 if (!rGroup->recordId)
1271 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1272 rGroup->recordId = res_get (common_resource, ext_res);
1275 /* determine database name */
1276 if (!rGroup->databaseName)
1278 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1279 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1281 sprintf (ext_res, "%sdatabase", gprefix);
1282 rGroup->databaseName = res_get (common_resource, ext_res);
1285 if (!rGroup->databaseName)
1286 rGroup->databaseName = "Default";
1288 if (rGroup->flagStoreData == -1)
1291 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1292 if (!(sval = res_get (common_resource, ext_res)))
1294 sprintf (ext_res, "%sstoreData", gprefix);
1295 sval = res_get (common_resource, ext_res);
1298 rGroup->flagStoreData = atoi (sval);
1300 if (rGroup->flagStoreData == -1)
1301 rGroup->flagStoreData = 0;
1303 if (rGroup->flagStoreKeys == -1)
1307 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1308 if (!(sval = res_get (common_resource, ext_res)))
1310 sprintf (ext_res, "%sstoreKeys", gprefix);
1311 sval = res_get (common_resource, ext_res);
1314 rGroup->flagStoreKeys = atoi (sval);
1316 if (rGroup->flagStoreKeys == -1)
1317 rGroup->flagStoreKeys = 0;
1319 if (sysno && deleteFlag)
1323 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1325 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1329 fi = file_read_start (fd);
1333 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1335 } while (r && !sysno && fi->file_more);
1336 file_read_stop (fi);