2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: extract.c,v 1.116 2002-04-13 18:16:43 adam Exp $
21 #if _FILE_OFFSET_BITS == 64
22 #define PRINTF_OFF_T "%Ld"
24 #define PRINTF_OFF_T "%ld"
27 static int records_inserted = 0;
28 static int records_updated = 0;
29 static int records_deleted = 0;
30 static int records_processed = 0;
32 static void logRecord (int showFlag)
36 if (showFlag || !(records_processed % 1000))
38 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
39 records_processed, records_inserted, records_updated,
44 static void extract_init (struct recExtractCtrl *p, RecWord *w)
46 w->zebra_maps = p->zebra_maps;
48 w->attrSet = VAL_BIB1;
54 static const char **searchRecordKey (ZebraHandle zh,
55 struct recKeys *reckeys,
56 int attrSetS, int attrUseS)
58 static const char *ws[32];
70 for (i = 0; i<32; i++)
74 chS = zebraExplain_lookupSU (zh->reg->zei, attrSetS, attrUseS);
78 while (off < reckeys->buf_used)
81 const char *src = reckeys->buf + off;
89 memcpy (&ch, src, sizeof(ch));
95 memcpy (&attrSet, src, sizeof(attrSet));
96 src += sizeof(attrSet);
100 memcpy (&attrUse, src, sizeof(attrUse));
101 src += sizeof(attrUse);
108 seqno += ((lead>>2) & 15)-1;
111 memcpy (&seqno, src, sizeof(seqno));
112 src += sizeof(seqno);
118 attrUseS == attrUse && attrSetS == attrSet
127 woff = seqno - startSeq;
128 if (woff >= 0 && woff < 31)
132 off = src - reckeys->buf;
134 assert (off == reckeys->buf_used);
138 struct file_read_info {
139 off_t file_max; /* maximum offset so far */
140 off_t file_offset; /* current offset */
141 off_t file_moffset; /* offset of rec/rec boundary */
148 static struct file_read_info *file_read_start (int fd)
150 struct file_read_info *fi = (struct file_read_info *)
151 xmalloc (sizeof(*fi));
155 fi->file_moffset = 0;
161 static void file_read_stop (struct file_read_info *fi)
166 static off_t file_seek (void *handle, off_t offset)
168 struct file_read_info *p = (struct file_read_info *) handle;
169 p->file_offset = offset;
172 return lseek (p->fd, offset, SEEK_SET);
175 static off_t file_tell (void *handle)
177 struct file_read_info *p = (struct file_read_info *) handle;
178 return p->file_offset;
181 static int file_read (void *handle, char *buf, size_t count)
183 struct file_read_info *p = (struct file_read_info *) handle;
189 if (r > p->sdrmax - p->file_offset)
190 r = p->sdrmax - p->file_offset;
192 memcpy (buf, p->sdrbuf + p->file_offset, r);
195 r = read (fd, buf, count);
199 if (p->file_offset > p->file_max)
200 p->file_max = p->file_offset;
205 static void file_begin (void *handle)
207 struct file_read_info *p = (struct file_read_info *) handle;
209 p->file_offset = p->file_moffset;
210 if (!p->sdrbuf && p->file_moffset)
211 lseek (p->fd, p->file_moffset, SEEK_SET);
215 static void file_end (void *handle, off_t offset)
217 struct file_read_info *p = (struct file_read_info *) handle;
219 assert (p->file_more == 0);
221 p->file_moffset = offset;
224 static char *fileMatchStr (ZebraHandle zh,
225 struct recKeys *reckeys, struct recordGroup *rGroup,
226 const char *fname, const char *spec)
228 static char dstBuf[2048]; /* static here ??? */
230 const char *s = spec;
231 static const char **w;
235 while (*s == ' ' || *s == '\t')
241 char attset_str[64], attname_str[64];
242 data1_attset *attset;
245 int attSet = 1, attUse = 1;
249 for (i = 0; *s && *s != ',' && *s != ')'; s++)
251 attset_str[i++] = *s;
252 attset_str[i] = '\0';
257 for (i = 0; *s && *s != ')'; s++)
259 attname_str[i++] = *s;
260 attname_str[i] = '\0';
263 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
266 attSet = attset->reference;
267 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
271 attUse = atoi (attname_str);
273 w = searchRecordKey (zh, reckeys, attSet, attUse);
278 for (i = 0; i<32; i++)
283 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
284 spec, rGroup->groupName ? rGroup->groupName : "none");
289 for (i = 0; i<32; i++)
290 if (matchFlag[i] && w[i])
302 logf (LOG_WARN, "Record didn't contain match"
303 " fields in (%s,%s)", attset_str, attname_str);
311 const char *spec_src = NULL;
312 const char *s1 = ++s;
313 while (*s1 && *s1 != ' ' && *s1 != '\t')
319 memcpy (special, s, spec_len);
320 special[spec_len] = '\0';
323 if (!strcmp (special, "group"))
324 spec_src = rGroup->groupName;
325 else if (!strcmp (special, "database"))
326 spec_src = rGroup->databaseName;
327 else if (!strcmp (special, "filename"))
329 else if (!strcmp (special, "type"))
330 spec_src = rGroup->recordType;
335 strcpy (dst, spec_src);
336 dst += strlen (spec_src);
339 else if (*s == '\"' || *s == '\'')
341 int stopMarker = *s++;
345 while (*s && *s != stopMarker)
348 tmpString[i++] = *s++;
353 strcpy (dst, tmpString);
354 dst += strlen (tmpString);
358 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
359 spec, rGroup->groupName ? rGroup->groupName : "none");
366 logf (LOG_WARN, "No match criteria for record %s in group %s",
367 fname, rGroup->groupName ? rGroup->groupName : "none");
374 struct recordLogInfo {
377 struct recordGroup *rGroup;
380 static void recordLogPreamble (int level, const char *msg, void *info)
382 struct recordLogInfo *p = (struct recordLogInfo *) info;
383 FILE *outf = yaz_log_file ();
387 fprintf (outf, "File %s, offset %d, type %s\n",
388 p->rGroup->recordType, p->recordOffset, p->fname);
389 log_event_start (NULL, NULL);
393 static int recordExtract (ZebraHandle zh,
394 SYSNO *sysno, const char *fname,
395 struct recordGroup *rGroup, int deleteFlag,
396 struct file_read_info *fi,
397 RecType recType, char *subType, void *clientData)
399 RecordAttr *recordAttr;
404 struct recordLogInfo logInfo;
405 off_t recordOffset = 0;
409 struct recExtractCtrl extractCtrl;
411 /* we are going to read from a file, so prepare the extraction */
414 zh->reg->keys.buf_used = 0;
415 zh->reg->keys.prevAttrUse = -1;
416 zh->reg->keys.prevAttrSet = -1;
417 zh->reg->keys.prevSeqNo = 0;
418 zh->reg->sortKeys = 0;
420 recordOffset = fi->file_moffset;
421 extractCtrl.offset = fi->file_moffset;
422 extractCtrl.readf = file_read;
423 extractCtrl.seekf = file_seek;
424 extractCtrl.tellf = file_tell;
425 extractCtrl.endf = file_end;
427 extractCtrl.subType = subType;
428 extractCtrl.init = extract_init;
429 extractCtrl.tokenAdd = extract_token_add;
430 extractCtrl.schemaAdd = extract_schema_add;
431 extractCtrl.dh = zh->reg->dh;
432 extractCtrl.handle = zh;
433 for (i = 0; i<256; i++)
435 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
436 extractCtrl.seqno[i] = 1;
438 extractCtrl.seqno[i] = 0;
440 extractCtrl.zebra_maps = zh->reg->zebra_maps;
441 extractCtrl.flagShowRecords = !rGroup->flagRw;
444 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
446 logInfo.fname = fname;
447 logInfo.recordOffset = recordOffset;
448 logInfo.rGroup = rGroup;
449 log_event_start (recordLogPreamble, &logInfo);
451 r = (*recType->extract)(clientData, &extractCtrl);
453 log_event_start (NULL, NULL);
455 if (r == RECCTRL_EXTRACT_EOF)
457 else if (r == RECCTRL_EXTRACT_ERROR)
459 /* error occured during extraction ... */
460 if (rGroup->flagRw &&
461 records_processed < rGroup->fileVerboseLimit)
463 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
464 fname, recordOffset);
468 if (zh->reg->keys.buf_used == 0)
470 /* the extraction process returned no information - the record
471 is probably empty - unless flagShowRecords is in use */
475 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
476 fname, recordOffset);
481 /* perform match if sysno not known and if match criteria is specified */
488 if (rGroup->recordId && *rGroup->recordId)
492 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
496 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
498 memcpy (sysno, rinfo+1, sizeof(*sysno));
502 logf (LOG_WARN, "Bad match criteria");
513 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
514 fname, recordOffset);
515 logf (LOG_WARN, "cannot delete record above (seems new)");
518 if (records_processed < rGroup->fileVerboseLimit)
519 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
520 fname, recordOffset);
521 rec = rec_new (zh->reg->records);
525 recordAttr = rec_init_attr (zh->reg->zei, rec);
529 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
531 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
532 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
538 /* record already exists */
539 struct recKeys delkeys;
541 rec = rec_get (zh->reg->records, *sysno);
544 recordAttr = rec_init_attr (zh->reg->zei, rec);
546 if (recordAttr->runNumber ==
547 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
549 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
550 fname, recordOffset);
551 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
556 delkeys.buf_used = rec->size[recInfo_delKeys];
557 delkeys.buf = rec->info[recInfo_delKeys];
558 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
559 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
562 /* record going to be deleted */
563 if (!delkeys.buf_used)
565 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
566 rGroup->recordType, fname, recordOffset);
567 logf (LOG_WARN, "cannot delete file above, storeKeys false");
571 if (records_processed < rGroup->fileVerboseLimit)
572 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
573 rGroup->recordType, fname, recordOffset);
576 dict_delete (zh->reg->matchDict, matchStr);
577 rec_del (zh->reg->records, &rec);
585 /* record going to be updated */
586 if (!delkeys.buf_used)
588 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
589 rGroup->recordType, fname, recordOffset);
590 logf (LOG_WARN, "cannot update file above, storeKeys false");
594 if (records_processed < rGroup->fileVerboseLimit)
595 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
596 rGroup->recordType, fname, recordOffset);
597 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
602 /* update file type */
603 xfree (rec->info[recInfo_fileType]);
604 rec->info[recInfo_fileType] =
605 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
607 /* update filename */
608 xfree (rec->info[recInfo_filename]);
609 rec->info[recInfo_filename] =
610 rec_strdup (fname, &rec->size[recInfo_filename]);
612 /* update delete keys */
613 xfree (rec->info[recInfo_delKeys]);
614 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
617 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
618 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
619 zh->reg->keys.buf = NULL;
620 zh->reg->keys.buf_max = 0;
622 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
623 rec->size[recInfo_delKeys] = reckeys.buf_used;
624 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
625 rec->size[recInfo_delKeys]);
630 rec->info[recInfo_delKeys] = NULL;
631 rec->size[recInfo_delKeys] = 0;
634 /* save file size of original record */
635 zebraExplain_recordBytesIncrement (zh->reg->zei,
636 - recordAttr->recordSize);
637 recordAttr->recordSize = fi->file_moffset - recordOffset;
638 if (!recordAttr->recordSize)
639 recordAttr->recordSize = fi->file_max - recordOffset;
640 zebraExplain_recordBytesIncrement (zh->reg->zei,
641 recordAttr->recordSize);
643 /* set run-number for this record */
644 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
647 /* update store data */
648 xfree (rec->info[recInfo_storeData]);
649 if (rGroup->flagStoreData == 1)
651 rec->size[recInfo_storeData] = recordAttr->recordSize;
652 rec->info[recInfo_storeData] = (char *)
653 xmalloc (recordAttr->recordSize);
654 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
656 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
657 recordOffset, fname);
660 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
661 < recordAttr->recordSize)
663 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
664 recordAttr->recordSize, fname);
670 rec->info[recInfo_storeData] = NULL;
671 rec->size[recInfo_storeData] = 0;
673 /* update database name */
674 xfree (rec->info[recInfo_databaseName]);
675 rec->info[recInfo_databaseName] =
676 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
679 recordAttr->recordOffset = recordOffset;
681 /* commit this record */
682 rec_put (zh->reg->records, &rec);
687 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
688 const struct recordGroup *rGroupP, int deleteFlag)
696 struct recordGroup rGroupM;
697 struct recordGroup *rGroup = &rGroupM;
698 struct file_read_info *fi;
701 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
703 if (!rGroup->groupName || !*rGroup->groupName)
706 sprintf (gprefix, "%s.", rGroup->groupName);
708 logf (LOG_DEBUG, "fileExtract %s", fname);
710 /* determine file extension */
712 for (i = strlen(fname); --i >= 0; )
715 else if (fname[i] == '.')
717 strcpy (ext, fname+i+1);
720 /* determine file type - depending on extension */
721 if (!rGroup->recordType)
723 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
724 if (!(rGroup->recordType = res_get (zh->res, ext_res)))
726 sprintf (ext_res, "%srecordType", gprefix);
727 rGroup->recordType = res_get (zh->res, ext_res);
730 if (!rGroup->recordType)
732 if (records_processed < rGroup->fileVerboseLimit)
733 logf (LOG_LOG, "? %s", fname);
736 if (!*rGroup->recordType)
739 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
742 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
746 /* determine match criteria */
747 if (!rGroup->recordId)
749 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
750 rGroup->recordId = res_get (zh->res, ext_res);
753 /* determine database name */
754 if (!rGroup->databaseName)
756 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
757 if (!(rGroup->databaseName = res_get (zh->res, ext_res)))
759 sprintf (ext_res, "%sdatabase", gprefix);
760 rGroup->databaseName = res_get (zh->res, ext_res);
763 if (!rGroup->databaseName)
764 rGroup->databaseName = "Default";
766 /* determine if explain database */
768 sprintf (ext_res, "%sexplainDatabase", gprefix);
769 rGroup->explainDatabase =
770 atoi (res_get_def (zh->res, ext_res, "0"));
772 /* announce database */
773 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
775 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName,
776 rGroup->explainDatabase))
780 if (rGroup->flagStoreData == -1)
783 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
784 if (!(sval = res_get (zh->res, ext_res)))
786 sprintf (ext_res, "%sstoreData", gprefix);
787 sval = res_get (zh->res, ext_res);
790 rGroup->flagStoreData = atoi (sval);
792 if (rGroup->flagStoreData == -1)
793 rGroup->flagStoreData = 0;
795 if (rGroup->flagStoreKeys == -1)
799 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
800 sval = res_get (zh->res, ext_res);
803 sprintf (ext_res, "%sstoreKeys", gprefix);
804 sval = res_get (zh->res, ext_res);
807 sval = res_get (zh->res, "storeKeys");
809 rGroup->flagStoreKeys = atoi (sval);
811 if (rGroup->flagStoreKeys == -1)
812 rGroup->flagStoreKeys = 0;
814 if (sysno && deleteFlag)
820 if (zh->path_reg && !yaz_is_abspath (fname))
822 strcpy (full_rep, zh->path_reg);
823 strcat (full_rep, "/");
824 strcat (full_rep, fname);
827 strcpy (full_rep, fname);
830 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
832 logf (LOG_WARN|LOG_ERRNO, "open %s", full_rep);
836 fi = file_read_start (fd);
840 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
841 recType, subType, clientData);
842 } while (r && !sysno && fi->file_more);
850 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
851 const char *buf, size_t buf_size,
852 const char *databaseName, int delete_flag,
853 int test_mode, int *sysno,
854 int store_keys, int store_data,
855 const char *match_criteria)
857 RecordAttr *recordAttr;
858 struct recExtractCtrl extractCtrl;
864 const char *fname = "<no file>";
866 long recordOffset = 0;
867 struct zebra_fetch_control fc;
870 fc.record_int_buf = buf;
871 fc.record_int_len = buf_size;
872 fc.record_int_pos = 0;
874 fc.record_offset = 0;
876 extractCtrl.offset = 0;
877 extractCtrl.readf = zebra_record_int_read;
878 extractCtrl.seekf = zebra_record_int_seek;
879 extractCtrl.tellf = zebra_record_int_tell;
880 extractCtrl.endf = zebra_record_int_end;
881 extractCtrl.fh = &fc;
883 /* announce database */
884 if (zebraExplain_curDatabase (zh->reg->zei, databaseName))
886 if (zebraExplain_newDatabase (zh->reg->zei, databaseName, 0))
890 recType_byName (zh->reg->recTypes, recordType, subType,
893 logf (LOG_WARN, "No such record type: %s", recordType);
897 zh->reg->keys.buf_used = 0;
898 zh->reg->keys.prevAttrUse = -1;
899 zh->reg->keys.prevAttrSet = -1;
900 zh->reg->keys.prevSeqNo = 0;
901 zh->reg->sortKeys = 0;
903 extractCtrl.subType = subType;
904 extractCtrl.init = extract_init;
905 extractCtrl.tokenAdd = extract_token_add;
906 extractCtrl.schemaAdd = extract_schema_add;
907 extractCtrl.dh = zh->reg->dh;
908 extractCtrl.handle = zh;
909 extractCtrl.zebra_maps = zh->reg->zebra_maps;
910 extractCtrl.flagShowRecords = 0;
911 for (i = 0; i<256; i++)
913 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
914 extractCtrl.seqno[i] = 1;
916 extractCtrl.seqno[i] = 0;
919 r = (*recType->extract)(clientData, &extractCtrl);
921 if (r == RECCTRL_EXTRACT_EOF)
923 else if (r == RECCTRL_EXTRACT_ERROR)
925 /* error occured during extraction ... */
927 yaz_log (LOG_WARN, "extract error");
929 if (rGroup->flagRw &&
930 records_processed < rGroup->fileVerboseLimit)
932 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
933 fname, (long) recordOffset);
938 if (zh->reg->keys.buf_used == 0)
940 /* the extraction process returned no information - the record
941 is probably empty - unless flagShowRecords is in use */
944 logf (LOG_WARN, "No keys generated for record");
945 logf (LOG_WARN, " The file is probably empty");
955 logf (LOG_LOG, "delete %s %s %ld", recordType,
956 fname, (long) recordOffset);
957 logf (LOG_WARN, "cannot delete record above (seems new)");
960 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
961 (long) recordOffset);
962 rec = rec_new (zh->reg->records);
966 recordAttr = rec_init_attr (zh->reg->zei, rec);
970 dict_insert (zh->reg->matchDict, matchStr,
971 sizeof(*sysno), sysno);
973 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
974 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
978 /* record already exists */
979 struct recKeys delkeys;
981 rec = rec_get (zh->reg->records, *sysno);
984 recordAttr = rec_init_attr (zh->reg->zei, rec);
986 if (recordAttr->runNumber ==
987 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
989 logf (LOG_LOG, "skipped %s %s %ld", recordType,
990 fname, (long) recordOffset);
994 delkeys.buf_used = rec->size[recInfo_delKeys];
995 delkeys.buf = rec->info[recInfo_delKeys];
996 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
997 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
1000 /* record going to be deleted */
1001 if (!delkeys.buf_used)
1003 logf (LOG_LOG, "delete %s %s %ld", recordType,
1004 fname, (long) recordOffset);
1005 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1009 logf (LOG_LOG, "delete %s %s %ld", recordType,
1010 fname, (long) recordOffset);
1013 dict_delete (matchDict, matchStr);
1015 rec_del (zh->reg->records, &rec);
1022 /* record going to be updated */
1023 if (!delkeys.buf_used)
1025 logf (LOG_LOG, "update %s %s %ld", recordType,
1026 fname, (long) recordOffset);
1027 logf (LOG_WARN, "cannot update file above, storeKeys false");
1031 logf (LOG_LOG, "update %s %s %ld", recordType,
1032 fname, (long) recordOffset);
1033 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1037 /* update file type */
1038 xfree (rec->info[recInfo_fileType]);
1039 rec->info[recInfo_fileType] =
1040 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1042 /* update filename */
1043 xfree (rec->info[recInfo_filename]);
1044 rec->info[recInfo_filename] =
1045 rec_strdup (fname, &rec->size[recInfo_filename]);
1047 /* update delete keys */
1048 xfree (rec->info[recInfo_delKeys]);
1049 if (zh->reg->keys.buf_used > 0 && store_keys == 1)
1051 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1052 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1053 zh->reg->keys.buf = NULL;
1054 zh->reg->keys.buf_max = 0;
1058 rec->info[recInfo_delKeys] = NULL;
1059 rec->size[recInfo_delKeys] = 0;
1062 /* save file size of original record */
1063 zebraExplain_recordBytesIncrement (zh->reg->zei,
1064 - recordAttr->recordSize);
1066 recordAttr->recordSize = fi->file_moffset - recordOffset;
1067 if (!recordAttr->recordSize)
1068 recordAttr->recordSize = fi->file_max - recordOffset;
1070 recordAttr->recordSize = buf_size;
1072 zebraExplain_recordBytesIncrement (zh->reg->zei,
1073 recordAttr->recordSize);
1075 /* set run-number for this record */
1076 recordAttr->runNumber =
1077 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1079 /* update store data */
1080 xfree (rec->info[recInfo_storeData]);
1081 if (store_data == 1)
1083 rec->size[recInfo_storeData] = recordAttr->recordSize;
1084 rec->info[recInfo_storeData] = (char *)
1085 xmalloc (recordAttr->recordSize);
1087 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1089 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1091 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1092 (long) recordOffset, fname);
1095 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1096 < recordAttr->recordSize)
1098 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1099 recordAttr->recordSize, fname);
1106 rec->info[recInfo_storeData] = NULL;
1107 rec->size[recInfo_storeData] = 0;
1109 /* update database name */
1110 xfree (rec->info[recInfo_databaseName]);
1111 rec->info[recInfo_databaseName] =
1112 rec_strdup (databaseName, &rec->size[recInfo_databaseName]);
1115 recordAttr->recordOffset = recordOffset;
1117 /* commit this record */
1118 rec_put (zh->reg->records, &rec);
1123 int explain_extract (void *handle, Record rec, data1_node *n)
1125 ZebraHandle zh = (ZebraHandle) handle;
1126 struct recExtractCtrl extractCtrl;
1129 if (zebraExplain_curDatabase (zh->reg->zei,
1130 rec->info[recInfo_databaseName]))
1133 if (zebraExplain_newDatabase (zh->reg->zei,
1134 rec->info[recInfo_databaseName], 0))
1138 zh->reg->keys.buf_used = 0;
1139 zh->reg->keys.prevAttrUse = -1;
1140 zh->reg->keys.prevAttrSet = -1;
1141 zh->reg->keys.prevSeqNo = 0;
1142 zh->reg->sortKeys = 0;
1144 extractCtrl.init = extract_init;
1145 extractCtrl.tokenAdd = extract_token_add;
1146 extractCtrl.schemaAdd = extract_schema_add;
1147 extractCtrl.dh = zh->reg->dh;
1148 for (i = 0; i<256; i++)
1149 extractCtrl.seqno[i] = 0;
1150 extractCtrl.zebra_maps = zh->reg->zebra_maps;
1151 extractCtrl.flagShowRecords = 0;
1152 extractCtrl.handle = handle;
1154 grs_extract_tree(&extractCtrl, n);
1156 if (rec->size[recInfo_delKeys])
1158 struct recKeys delkeys;
1159 struct sortKey *sortKeys = 0;
1161 delkeys.buf_used = rec->size[recInfo_delKeys];
1162 delkeys.buf = rec->info[recInfo_delKeys];
1163 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1164 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1166 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys);
1167 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1169 xfree (rec->info[recInfo_delKeys]);
1170 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1171 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1172 zh->reg->keys.buf = NULL;
1173 zh->reg->keys.buf_max = 0;
1177 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1178 int cmd, struct recKeys *reckeys)
1182 unsigned char attrSet = (unsigned char) -1;
1183 unsigned short attrUse = (unsigned short) -1;
1188 ZebraExplainInfo zei = zh->reg->zei;
1190 if (!zh->reg->key_buf)
1192 int mem = 8*1024*1024;
1193 zh->reg->key_buf = (char**) xmalloc (mem);
1194 zh->reg->ptr_top = mem/sizeof(char*);
1196 zh->reg->key_buf_used = 0;
1197 zh->reg->key_file_no = 0;
1199 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1200 while (off < reckeys->buf_used)
1202 const char *src = reckeys->buf + off;
1211 memcpy (&ch, src, sizeof(ch));
1217 memcpy (&attrSet, src, sizeof(attrSet));
1218 src += sizeof(attrSet);
1222 memcpy (&attrUse, src, sizeof(attrUse));
1223 src += sizeof(attrUse);
1226 if (zh->reg->key_buf_used + 1024 >
1227 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1228 extract_flushWriteKeys (zh);
1230 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1231 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1234 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1236 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1239 zh->reg->key_buf_used +=
1240 key_SU_encode (ch,((char*)zh->reg->key_buf) +
1241 zh->reg->key_buf_used);
1244 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1246 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1247 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1250 seqno += ((lead>>2) & 15)-1;
1253 memcpy (&seqno, src, sizeof(seqno));
1254 src += sizeof(seqno);
1258 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used, &key, sizeof(key));
1259 (zh->reg->key_buf_used) += sizeof(key);
1260 off = src - reckeys->buf;
1262 assert (off == reckeys->buf_used);
1265 void extract_flushWriteKeys (ZebraHandle zh)
1268 char out_fname[200];
1270 struct encode_info encode_info;
1271 int ptr_i = zh->reg->ptr_i;
1275 if (!zh->reg->key_buf || ptr_i <= 0)
1278 (zh->reg->key_file_no)++;
1279 logf (LOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1281 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i, sizeof(char*),
1283 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1285 if (!(outf = fopen (out_fname, "wb")))
1287 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1290 logf (LOG_LOG, "writing section %d", zh->reg->key_file_no);
1291 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1293 encode_key_init (&encode_info);
1294 encode_key_write (cp, &encode_info, outf);
1298 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1299 if (strcmp (cp, prevcp))
1301 encode_key_init (&encode_info);
1302 encode_key_write (cp, &encode_info, outf);
1306 encode_key_write (cp + strlen(cp), &encode_info, outf);
1309 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1310 extract_get_fname_tmp (out_fname, key_file_no);
1312 if (!(outf = fopen (out_fname, "wb")))
1314 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1317 logf (LOG_LOG, "writing section %d", key_file_no);
1319 prevcp = key_buf[ptr_top-i];
1321 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1323 key_y_len = strlen(prevcp)+1;
1325 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1326 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1328 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1329 sizeof(char*), key_y_compare);
1330 cp = key_buf[ptr_top-ptr_i];
1332 encode_key_init (&encode_info);
1333 encode_key_write (cp, &encode_info, outf);
1336 cp = key_buf[ptr_top-ptr_i];
1337 encode_key_write (cp+key_y_len, &encode_info, outf);
1341 prevcp = key_buf[ptr_top-ptr_i];
1346 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1349 logf (LOG_LOG, "finished section %d", zh->reg->key_file_no);
1351 zh->reg->key_buf_used = 0;
1354 void extract_add_index_string (RecWord *p, const char *string,
1358 unsigned char attrSet;
1359 unsigned short attrUse;
1362 int *pseqno = &p->seqno;
1363 ZebraHandle zh = p->extractCtrl->handle;
1364 ZebraExplainInfo zei = zh->reg->zei;
1365 struct recKeys *keys = &zh->reg->keys;
1367 if (keys->buf_used+1024 > keys->buf_max)
1371 b = (char *) xmalloc (keys->buf_max += 128000);
1372 if (keys->buf_used > 0)
1373 memcpy (b, keys->buf, keys->buf_used);
1377 dst = keys->buf + keys->buf_used;
1379 attrSet = p->attrSet;
1380 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1383 keys->prevAttrSet = attrSet;
1384 attrUse = p->attrUse;
1385 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1388 keys->prevAttrUse = attrUse;
1390 diff = 1 + *pseqno - keys->prevSeqNo;
1391 if (diff >= 1 && diff <= 15)
1392 lead |= (diff << 2);
1396 keys->prevSeqNo = *pseqno;
1403 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1406 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1407 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1408 attrSet, attrUse, ch);
1411 memcpy (dst, &ch, sizeof(ch));
1417 memcpy (dst, &attrSet, sizeof(attrSet));
1418 dst += sizeof(attrSet);
1422 memcpy (dst, &attrUse, sizeof(attrUse));
1423 dst += sizeof(attrUse);
1426 *dst++ = p->reg_type;
1427 memcpy (dst, string, length);
1433 memcpy (dst, pseqno, sizeof(*pseqno));
1434 dst += sizeof(*pseqno);
1436 keys->buf_used = dst - keys->buf;
1439 static void extract_add_sort_string (RecWord *p, const char *string,
1443 ZebraHandle zh = p->extractCtrl->handle;
1445 for (sk = zh->reg->sortKeys; sk; sk = sk->next)
1446 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1449 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1450 sk->next = zh->reg->sortKeys;
1451 zh->reg->sortKeys = sk;
1453 sk->string = (char *) xmalloc (length);
1454 sk->length = length;
1455 memcpy (sk->string, string, length);
1457 sk->attrSet = p->attrSet;
1458 sk->attrUse = p->attrUse;
1461 void extract_add_string (RecWord *p, const char *string, int length)
1463 assert (length > 0);
1464 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1465 extract_add_sort_string (p, string, length);
1467 extract_add_index_string (p, string, length);
1470 static void extract_add_incomplete_field (RecWord *p)
1472 const char *b = p->string;
1473 int remain = p->length;
1474 const char **map = 0;
1477 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1481 char buf[IT_MAX_WORD+1];
1485 while (map && *map && **map == *CHR_SPACE)
1487 remain = p->length - (b - p->string);
1489 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1496 while (map && *map && **map != *CHR_SPACE)
1498 const char *cp = *map;
1500 while (i < IT_MAX_WORD && *cp)
1502 remain = p->length - (b - p->string);
1504 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1510 extract_add_string (p, buf, i);
1515 static void extract_add_complete_field (RecWord *p)
1517 const char *b = p->string;
1518 char buf[IT_MAX_WORD+1];
1519 const char **map = 0;
1520 int i = 0, remain = p->length;
1523 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1525 while (remain > 0 && i < IT_MAX_WORD)
1527 while (map && *map && **map == *CHR_SPACE)
1529 remain = p->length - (b - p->string);
1531 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1538 if (i && i < IT_MAX_WORD)
1539 buf[i++] = *CHR_SPACE;
1540 while (map && *map && **map != *CHR_SPACE)
1542 const char *cp = *map;
1544 if (i >= IT_MAX_WORD)
1546 while (i < IT_MAX_WORD && *cp)
1548 remain = p->length - (b - p->string);
1550 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1558 extract_add_string (p, buf, i);
1561 void extract_token_add (RecWord *p)
1564 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1565 p->string, p->length)))
1567 p->string = wrbuf_buf(wrbuf);
1568 p->length = wrbuf_len(wrbuf);
1570 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1571 extract_add_complete_field (p);
1573 extract_add_incomplete_field(p);
1576 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1578 ZebraHandle zh = (ZebraHandle) (p->handle);
1579 zebraExplain_addSchema (zh->reg->zei, oid);
1582 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1583 int cmd, struct sortKey **skp)
1585 struct sortKey *sk = *skp;
1586 SortIdx sortIdx = zh->reg->sortIdx;
1588 sortIdx_sysno (sortIdx, sysno);
1591 struct sortKey *sk_next = sk->next;
1592 sortIdx_type (sortIdx, sk->attrUse);
1593 sortIdx_add (sortIdx, sk->string, sk->length);
1601 void encode_key_init (struct encode_info *i)
1608 char *encode_key_int (int d, char *bp)
1612 else if (d <= 16383)
1614 *bp++ = 64 + (d>>8);
1617 else if (d <= 4194303)
1619 *bp++ = 128 + (d>>16);
1620 *bp++ = (d>>8) & 255;
1625 *bp++ = 192 + (d>>24);
1626 *bp++ = (d>>16) & 255;
1627 *bp++ = (d>>8) & 255;
1633 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1638 while ((*bp++ = *k++))
1640 memcpy (&key, k+1, sizeof(struct it_key));
1641 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1642 if (i->sysno != key.sysno)
1644 i->sysno = key.sysno;
1647 else if (!i->seqno && !key.seqno && i->cmd == *k)
1649 bp = encode_key_int (key.seqno - i->seqno, bp);
1650 i->seqno = key.seqno;
1652 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1654 logf (LOG_FATAL|LOG_ERRNO, "fwrite");