2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: extract.c,v 1.119 2002-05-07 11:05:19 adam Exp $
21 #if _FILE_OFFSET_BITS == 64
22 #define PRINTF_OFF_T "%Ld"
24 #define PRINTF_OFF_T "%ld"
27 static void shellsort(void *ar, int r, size_t s,
28 int (*cmp)(const void *a, const void *b))
33 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
34 13776, 4592, 1968, 861, 336,
35 112, 48, 21, 7, 3, 1 };
36 for ( k = 0; k < 16; k++)
37 for (h = incs[k], i = h; i < r; i++)
41 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
43 memcpy (a + s*j, a + s*(j-h), s);
51 static void logRecord (ZebraHandle zh)
53 ++zh->records_processed;
54 if (!(zh->records_processed % 1000))
56 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
57 zh->records_processed, zh->records_inserted, zh->records_updated,
62 static void extract_init (struct recExtractCtrl *p, RecWord *w)
64 w->zebra_maps = p->zebra_maps;
66 w->attrSet = VAL_BIB1;
72 static const char **searchRecordKey (ZebraHandle zh,
73 struct recKeys *reckeys,
74 int attrSetS, int attrUseS)
76 static const char *ws[32];
88 for (i = 0; i<32; i++)
92 chS = zebraExplain_lookupSU (zh->reg->zei, attrSetS, attrUseS);
96 while (off < reckeys->buf_used)
99 const char *src = reckeys->buf + off;
107 memcpy (&ch, src, sizeof(ch));
113 memcpy (&attrSet, src, sizeof(attrSet));
114 src += sizeof(attrSet);
118 memcpy (&attrUse, src, sizeof(attrUse));
119 src += sizeof(attrUse);
126 seqno += ((lead>>2) & 15)-1;
129 memcpy (&seqno, src, sizeof(seqno));
130 src += sizeof(seqno);
136 attrUseS == attrUse && attrSetS == attrSet
145 woff = seqno - startSeq;
146 if (woff >= 0 && woff < 31)
150 off = src - reckeys->buf;
152 assert (off == reckeys->buf_used);
156 struct file_read_info {
157 off_t file_max; /* maximum offset so far */
158 off_t file_offset; /* current offset */
159 off_t file_moffset; /* offset of rec/rec boundary */
166 static struct file_read_info *file_read_start (int fd)
168 struct file_read_info *fi = (struct file_read_info *)
169 xmalloc (sizeof(*fi));
173 fi->file_moffset = 0;
179 static void file_read_stop (struct file_read_info *fi)
184 static off_t file_seek (void *handle, off_t offset)
186 struct file_read_info *p = (struct file_read_info *) handle;
187 p->file_offset = offset;
190 return lseek (p->fd, offset, SEEK_SET);
193 static off_t file_tell (void *handle)
195 struct file_read_info *p = (struct file_read_info *) handle;
196 return p->file_offset;
199 static int file_read (void *handle, char *buf, size_t count)
201 struct file_read_info *p = (struct file_read_info *) handle;
207 if (r > p->sdrmax - p->file_offset)
208 r = p->sdrmax - p->file_offset;
210 memcpy (buf, p->sdrbuf + p->file_offset, r);
213 r = read (fd, buf, count);
217 if (p->file_offset > p->file_max)
218 p->file_max = p->file_offset;
223 static void file_begin (void *handle)
225 struct file_read_info *p = (struct file_read_info *) handle;
227 p->file_offset = p->file_moffset;
228 if (!p->sdrbuf && p->file_moffset)
229 lseek (p->fd, p->file_moffset, SEEK_SET);
233 static void file_end (void *handle, off_t offset)
235 struct file_read_info *p = (struct file_read_info *) handle;
237 assert (p->file_more == 0);
239 p->file_moffset = offset;
242 static char *fileMatchStr (ZebraHandle zh,
243 struct recKeys *reckeys, struct recordGroup *rGroup,
244 const char *fname, const char *spec)
246 static char dstBuf[2048]; /* static here ??? */
248 const char *s = spec;
249 static const char **w;
253 while (*s == ' ' || *s == '\t')
259 char attset_str[64], attname_str[64];
260 data1_attset *attset;
263 int attSet = 1, attUse = 1;
267 for (i = 0; *s && *s != ',' && *s != ')'; s++)
269 attset_str[i++] = *s;
270 attset_str[i] = '\0';
275 for (i = 0; *s && *s != ')'; s++)
277 attname_str[i++] = *s;
278 attname_str[i] = '\0';
281 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
284 attSet = attset->reference;
285 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
289 attUse = atoi (attname_str);
291 w = searchRecordKey (zh, reckeys, attSet, attUse);
296 for (i = 0; i<32; i++)
301 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
302 spec, rGroup->groupName ? rGroup->groupName : "none");
307 for (i = 0; i<32; i++)
308 if (matchFlag[i] && w[i])
320 logf (LOG_WARN, "Record didn't contain match"
321 " fields in (%s,%s)", attset_str, attname_str);
329 const char *spec_src = NULL;
330 const char *s1 = ++s;
331 while (*s1 && *s1 != ' ' && *s1 != '\t')
337 memcpy (special, s, spec_len);
338 special[spec_len] = '\0';
341 if (!strcmp (special, "group"))
342 spec_src = rGroup->groupName;
343 else if (!strcmp (special, "database"))
344 spec_src = rGroup->databaseName;
345 else if (!strcmp (special, "filename"))
347 else if (!strcmp (special, "type"))
348 spec_src = rGroup->recordType;
353 strcpy (dst, spec_src);
354 dst += strlen (spec_src);
357 else if (*s == '\"' || *s == '\'')
359 int stopMarker = *s++;
363 while (*s && *s != stopMarker)
366 tmpString[i++] = *s++;
371 strcpy (dst, tmpString);
372 dst += strlen (tmpString);
376 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
377 spec, rGroup->groupName ? rGroup->groupName : "none");
384 logf (LOG_WARN, "No match criteria for record %s in group %s",
385 fname, rGroup->groupName ? rGroup->groupName : "none");
392 struct recordLogInfo {
395 struct recordGroup *rGroup;
398 static void recordLogPreamble (int level, const char *msg, void *info)
400 struct recordLogInfo *p = (struct recordLogInfo *) info;
401 FILE *outf = yaz_log_file ();
405 fprintf (outf, "File %s, offset %d, type %s\n",
406 p->rGroup->recordType, p->recordOffset, p->fname);
407 log_event_start (NULL, NULL);
411 static int recordExtract (ZebraHandle zh,
412 SYSNO *sysno, const char *fname,
413 struct recordGroup *rGroup, int deleteFlag,
414 struct file_read_info *fi,
415 RecType recType, char *subType, void *clientData)
417 RecordAttr *recordAttr;
422 struct recordLogInfo logInfo;
423 off_t recordOffset = 0;
427 struct recExtractCtrl extractCtrl;
429 /* we are going to read from a file, so prepare the extraction */
432 zh->reg->keys.buf_used = 0;
433 zh->reg->keys.prevAttrUse = -1;
434 zh->reg->keys.prevAttrSet = -1;
435 zh->reg->keys.prevSeqNo = 0;
436 zh->reg->sortKeys = 0;
438 recordOffset = fi->file_moffset;
439 extractCtrl.offset = fi->file_moffset;
440 extractCtrl.readf = file_read;
441 extractCtrl.seekf = file_seek;
442 extractCtrl.tellf = file_tell;
443 extractCtrl.endf = file_end;
445 extractCtrl.subType = subType;
446 extractCtrl.init = extract_init;
447 extractCtrl.tokenAdd = extract_token_add;
448 extractCtrl.schemaAdd = extract_schema_add;
449 extractCtrl.dh = zh->reg->dh;
450 extractCtrl.handle = zh;
451 for (i = 0; i<256; i++)
453 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
454 extractCtrl.seqno[i] = 1;
456 extractCtrl.seqno[i] = 0;
458 extractCtrl.zebra_maps = zh->reg->zebra_maps;
459 extractCtrl.flagShowRecords = !rGroup->flagRw;
462 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
464 logInfo.fname = fname;
465 logInfo.recordOffset = recordOffset;
466 logInfo.rGroup = rGroup;
467 log_event_start (recordLogPreamble, &logInfo);
469 r = (*recType->extract)(clientData, &extractCtrl);
471 log_event_start (NULL, NULL);
473 if (r == RECCTRL_EXTRACT_EOF)
475 else if (r == RECCTRL_EXTRACT_ERROR)
477 /* error occured during extraction ... */
478 if (rGroup->flagRw &&
479 zh->records_processed < rGroup->fileVerboseLimit)
481 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
482 fname, recordOffset);
486 if (zh->reg->keys.buf_used == 0)
488 /* the extraction process returned no information - the record
489 is probably empty - unless flagShowRecords is in use */
493 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
494 fname, recordOffset);
499 /* perform match if sysno not known and if match criteria is specified */
506 if (rGroup->recordId && *rGroup->recordId)
510 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
514 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
516 memcpy (sysno, rinfo+1, sizeof(*sysno));
520 logf (LOG_WARN, "Bad match criteria");
531 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
532 fname, recordOffset);
533 logf (LOG_WARN, "cannot delete record above (seems new)");
536 if (zh->records_processed < rGroup->fileVerboseLimit)
537 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
538 fname, recordOffset);
539 rec = rec_new (zh->reg->records);
543 recordAttr = rec_init_attr (zh->reg->zei, rec);
547 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
549 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
550 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
552 zh->records_inserted++;
556 /* record already exists */
557 struct recKeys delkeys;
559 rec = rec_get (zh->reg->records, *sysno);
562 recordAttr = rec_init_attr (zh->reg->zei, rec);
564 if (recordAttr->runNumber ==
565 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
567 yaz_log (LOG_LOG, "run number = %d", recordAttr->runNumber);
568 yaz_log (LOG_LOG, "skipped %s %s " PRINTF_OFF_T,
569 rGroup->recordType, fname, recordOffset);
570 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
575 delkeys.buf_used = rec->size[recInfo_delKeys];
576 delkeys.buf = rec->info[recInfo_delKeys];
577 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
578 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
581 /* record going to be deleted */
582 if (!delkeys.buf_used)
584 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
585 rGroup->recordType, fname, recordOffset);
586 logf (LOG_WARN, "cannot delete file above, storeKeys false");
590 if (zh->records_processed < rGroup->fileVerboseLimit)
591 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
592 rGroup->recordType, fname, recordOffset);
593 zh->records_deleted++;
595 dict_delete (zh->reg->matchDict, matchStr);
596 rec_del (zh->reg->records, &rec);
604 /* record going to be updated */
605 if (!delkeys.buf_used)
607 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
608 rGroup->recordType, fname, recordOffset);
609 logf (LOG_WARN, "cannot update file above, storeKeys false");
613 if (zh->records_processed < rGroup->fileVerboseLimit)
614 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
615 rGroup->recordType, fname, recordOffset);
616 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
617 zh->records_updated++;
621 /* update file type */
622 xfree (rec->info[recInfo_fileType]);
623 rec->info[recInfo_fileType] =
624 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
626 /* update filename */
627 xfree (rec->info[recInfo_filename]);
628 rec->info[recInfo_filename] =
629 rec_strdup (fname, &rec->size[recInfo_filename]);
631 /* update delete keys */
632 xfree (rec->info[recInfo_delKeys]);
633 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
636 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
637 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
638 zh->reg->keys.buf = NULL;
639 zh->reg->keys.buf_max = 0;
641 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
642 rec->size[recInfo_delKeys] = reckeys.buf_used;
643 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
644 rec->size[recInfo_delKeys]);
649 rec->info[recInfo_delKeys] = NULL;
650 rec->size[recInfo_delKeys] = 0;
653 /* save file size of original record */
654 zebraExplain_recordBytesIncrement (zh->reg->zei,
655 - recordAttr->recordSize);
656 recordAttr->recordSize = fi->file_moffset - recordOffset;
657 if (!recordAttr->recordSize)
658 recordAttr->recordSize = fi->file_max - recordOffset;
659 zebraExplain_recordBytesIncrement (zh->reg->zei,
660 recordAttr->recordSize);
662 /* set run-number for this record */
663 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
666 /* update store data */
667 xfree (rec->info[recInfo_storeData]);
668 if (rGroup->flagStoreData == 1)
670 rec->size[recInfo_storeData] = recordAttr->recordSize;
671 rec->info[recInfo_storeData] = (char *)
672 xmalloc (recordAttr->recordSize);
673 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
675 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
676 recordOffset, fname);
679 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
680 < recordAttr->recordSize)
682 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
683 recordAttr->recordSize, fname);
689 rec->info[recInfo_storeData] = NULL;
690 rec->size[recInfo_storeData] = 0;
692 /* update database name */
693 xfree (rec->info[recInfo_databaseName]);
694 rec->info[recInfo_databaseName] =
695 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
698 recordAttr->recordOffset = recordOffset;
700 /* commit this record */
701 rec_put (zh->reg->records, &rec);
706 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
707 const struct recordGroup *rGroupP, int deleteFlag)
715 struct recordGroup rGroupM;
716 struct recordGroup *rGroup = &rGroupM;
717 struct file_read_info *fi;
720 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
722 if (!rGroup->groupName || !*rGroup->groupName)
725 sprintf (gprefix, "%s.", rGroup->groupName);
727 logf (LOG_DEBUG, "fileExtract %s", fname);
729 /* determine file extension */
731 for (i = strlen(fname); --i >= 0; )
734 else if (fname[i] == '.')
736 strcpy (ext, fname+i+1);
739 /* determine file type - depending on extension */
740 if (!rGroup->recordType)
742 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
743 if (!(rGroup->recordType = res_get (zh->res, ext_res)))
745 sprintf (ext_res, "%srecordType", gprefix);
746 rGroup->recordType = res_get (zh->res, ext_res);
749 if (!rGroup->recordType)
751 if (zh->records_processed < rGroup->fileVerboseLimit)
752 logf (LOG_LOG, "? %s", fname);
755 if (!*rGroup->recordType)
758 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
761 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
765 /* determine match criteria */
766 if (!rGroup->recordId)
768 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
769 rGroup->recordId = res_get (zh->res, ext_res);
772 /* determine database name */
773 if (!rGroup->databaseName)
775 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
776 if (!(rGroup->databaseName = res_get (zh->res, ext_res)))
778 sprintf (ext_res, "%sdatabase", gprefix);
779 rGroup->databaseName = res_get (zh->res, ext_res);
782 if (!rGroup->databaseName)
783 rGroup->databaseName = "Default";
785 /* determine if explain database */
787 sprintf (ext_res, "%sexplainDatabase", gprefix);
788 rGroup->explainDatabase =
789 atoi (res_get_def (zh->res, ext_res, "0"));
791 /* announce database */
792 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
794 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName,
795 rGroup->explainDatabase))
799 if (rGroup->flagStoreData == -1)
802 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
803 if (!(sval = res_get (zh->res, ext_res)))
805 sprintf (ext_res, "%sstoreData", gprefix);
806 sval = res_get (zh->res, ext_res);
809 rGroup->flagStoreData = atoi (sval);
811 if (rGroup->flagStoreData == -1)
812 rGroup->flagStoreData = 0;
814 if (rGroup->flagStoreKeys == -1)
818 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
819 sval = res_get (zh->res, ext_res);
822 sprintf (ext_res, "%sstoreKeys", gprefix);
823 sval = res_get (zh->res, ext_res);
826 sval = res_get (zh->res, "storeKeys");
828 rGroup->flagStoreKeys = atoi (sval);
830 if (rGroup->flagStoreKeys == -1)
831 rGroup->flagStoreKeys = 0;
833 if (sysno && deleteFlag)
839 if (zh->path_reg && !yaz_is_abspath (fname))
841 strcpy (full_rep, zh->path_reg);
842 strcat (full_rep, "/");
843 strcat (full_rep, fname);
846 strcpy (full_rep, fname);
849 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
851 logf (LOG_WARN|LOG_ERRNO, "open %s", full_rep);
855 fi = file_read_start (fd);
859 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
860 recType, subType, clientData);
861 } while (r && !sysno && fi->file_more);
869 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
870 const char *buf, size_t buf_size,
871 const char *databaseName, int delete_flag,
872 int test_mode, int *sysno,
873 int store_keys, int store_data,
874 const char *match_criteria)
876 RecordAttr *recordAttr;
877 struct recExtractCtrl extractCtrl;
883 const char *fname = "<no file>";
885 long recordOffset = 0;
886 struct zebra_fetch_control fc;
889 fc.record_int_buf = buf;
890 fc.record_int_len = buf_size;
891 fc.record_int_pos = 0;
893 fc.record_offset = 0;
895 extractCtrl.offset = 0;
896 extractCtrl.readf = zebra_record_int_read;
897 extractCtrl.seekf = zebra_record_int_seek;
898 extractCtrl.tellf = zebra_record_int_tell;
899 extractCtrl.endf = zebra_record_int_end;
900 extractCtrl.fh = &fc;
902 /* announce database */
903 if (zebraExplain_curDatabase (zh->reg->zei, databaseName))
905 if (zebraExplain_newDatabase (zh->reg->zei, databaseName, 0))
909 recType_byName (zh->reg->recTypes, recordType, subType,
912 logf (LOG_WARN, "No such record type: %s", recordType);
916 zh->reg->keys.buf_used = 0;
917 zh->reg->keys.prevAttrUse = -1;
918 zh->reg->keys.prevAttrSet = -1;
919 zh->reg->keys.prevSeqNo = 0;
920 zh->reg->sortKeys = 0;
922 extractCtrl.subType = subType;
923 extractCtrl.init = extract_init;
924 extractCtrl.tokenAdd = extract_token_add;
925 extractCtrl.schemaAdd = extract_schema_add;
926 extractCtrl.dh = zh->reg->dh;
927 extractCtrl.handle = zh;
928 extractCtrl.zebra_maps = zh->reg->zebra_maps;
929 extractCtrl.flagShowRecords = 0;
930 for (i = 0; i<256; i++)
932 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
933 extractCtrl.seqno[i] = 1;
935 extractCtrl.seqno[i] = 0;
938 r = (*recType->extract)(clientData, &extractCtrl);
940 if (r == RECCTRL_EXTRACT_EOF)
942 else if (r == RECCTRL_EXTRACT_ERROR)
944 /* error occured during extraction ... */
946 yaz_log (LOG_WARN, "extract error");
948 if (rGroup->flagRw &&
949 zh->records_processed < rGroup->fileVerboseLimit)
951 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
952 fname, (long) recordOffset);
957 if (zh->reg->keys.buf_used == 0)
959 /* the extraction process returned no information - the record
960 is probably empty - unless flagShowRecords is in use */
963 logf (LOG_WARN, "No keys generated for record");
964 logf (LOG_WARN, " The file is probably empty");
974 logf (LOG_LOG, "delete %s %s %ld", recordType,
975 fname, (long) recordOffset);
976 logf (LOG_WARN, "cannot delete record above (seems new)");
979 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
980 (long) recordOffset);
981 rec = rec_new (zh->reg->records);
985 recordAttr = rec_init_attr (zh->reg->zei, rec);
989 dict_insert (zh->reg->matchDict, matchStr,
990 sizeof(*sysno), sysno);
992 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
993 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
997 /* record already exists */
998 struct recKeys delkeys;
1000 rec = rec_get (zh->reg->records, *sysno);
1003 recordAttr = rec_init_attr (zh->reg->zei, rec);
1005 if (recordAttr->runNumber ==
1006 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
1008 logf (LOG_LOG, "skipped %s %s %ld", recordType,
1009 fname, (long) recordOffset);
1013 delkeys.buf_used = rec->size[recInfo_delKeys];
1014 delkeys.buf = rec->info[recInfo_delKeys];
1015 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
1016 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
1019 /* record going to be deleted */
1020 if (!delkeys.buf_used)
1022 logf (LOG_LOG, "delete %s %s %ld", recordType,
1023 fname, (long) recordOffset);
1024 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1028 logf (LOG_LOG, "delete %s %s %ld", recordType,
1029 fname, (long) recordOffset);
1032 dict_delete (matchDict, matchStr);
1034 rec_del (zh->reg->records, &rec);
1041 /* record going to be updated */
1042 if (!delkeys.buf_used)
1044 logf (LOG_LOG, "update %s %s %ld", recordType,
1045 fname, (long) recordOffset);
1046 logf (LOG_WARN, "cannot update file above, storeKeys false");
1050 logf (LOG_LOG, "update %s %s %ld", recordType,
1051 fname, (long) recordOffset);
1052 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1056 /* update file type */
1057 xfree (rec->info[recInfo_fileType]);
1058 rec->info[recInfo_fileType] =
1059 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1061 /* update filename */
1062 xfree (rec->info[recInfo_filename]);
1063 rec->info[recInfo_filename] =
1064 rec_strdup (fname, &rec->size[recInfo_filename]);
1066 /* update delete keys */
1067 xfree (rec->info[recInfo_delKeys]);
1068 if (zh->reg->keys.buf_used > 0 && store_keys == 1)
1070 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1071 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1072 zh->reg->keys.buf = NULL;
1073 zh->reg->keys.buf_max = 0;
1077 rec->info[recInfo_delKeys] = NULL;
1078 rec->size[recInfo_delKeys] = 0;
1081 /* save file size of original record */
1082 zebraExplain_recordBytesIncrement (zh->reg->zei,
1083 - recordAttr->recordSize);
1085 recordAttr->recordSize = fi->file_moffset - recordOffset;
1086 if (!recordAttr->recordSize)
1087 recordAttr->recordSize = fi->file_max - recordOffset;
1089 recordAttr->recordSize = buf_size;
1091 zebraExplain_recordBytesIncrement (zh->reg->zei,
1092 recordAttr->recordSize);
1094 /* set run-number for this record */
1095 recordAttr->runNumber =
1096 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1098 /* update store data */
1099 xfree (rec->info[recInfo_storeData]);
1100 if (store_data == 1)
1102 rec->size[recInfo_storeData] = recordAttr->recordSize;
1103 rec->info[recInfo_storeData] = (char *)
1104 xmalloc (recordAttr->recordSize);
1106 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1108 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1110 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1111 (long) recordOffset, fname);
1114 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1115 < recordAttr->recordSize)
1117 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1118 recordAttr->recordSize, 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 (databaseName, &rec->size[recInfo_databaseName]);
1134 recordAttr->recordOffset = recordOffset;
1136 /* commit this record */
1137 rec_put (zh->reg->records, &rec);
1142 int explain_extract (void *handle, Record rec, data1_node *n)
1144 ZebraHandle zh = (ZebraHandle) handle;
1145 struct recExtractCtrl extractCtrl;
1148 if (zebraExplain_curDatabase (zh->reg->zei,
1149 rec->info[recInfo_databaseName]))
1152 if (zebraExplain_newDatabase (zh->reg->zei,
1153 rec->info[recInfo_databaseName], 0))
1157 zh->reg->keys.buf_used = 0;
1158 zh->reg->keys.prevAttrUse = -1;
1159 zh->reg->keys.prevAttrSet = -1;
1160 zh->reg->keys.prevSeqNo = 0;
1161 zh->reg->sortKeys = 0;
1163 extractCtrl.init = extract_init;
1164 extractCtrl.tokenAdd = extract_token_add;
1165 extractCtrl.schemaAdd = extract_schema_add;
1166 extractCtrl.dh = zh->reg->dh;
1167 for (i = 0; i<256; i++)
1168 extractCtrl.seqno[i] = 0;
1169 extractCtrl.zebra_maps = zh->reg->zebra_maps;
1170 extractCtrl.flagShowRecords = 0;
1171 extractCtrl.handle = handle;
1173 grs_extract_tree(&extractCtrl, n);
1175 if (rec->size[recInfo_delKeys])
1177 struct recKeys delkeys;
1178 struct sortKey *sortKeys = 0;
1180 delkeys.buf_used = rec->size[recInfo_delKeys];
1181 delkeys.buf = rec->info[recInfo_delKeys];
1182 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1183 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1185 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys);
1186 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1188 xfree (rec->info[recInfo_delKeys]);
1189 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1190 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1191 zh->reg->keys.buf = NULL;
1192 zh->reg->keys.buf_max = 0;
1196 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1197 int cmd, struct recKeys *reckeys)
1201 unsigned char attrSet = (unsigned char) -1;
1202 unsigned short attrUse = (unsigned short) -1;
1207 ZebraExplainInfo zei = zh->reg->zei;
1209 if (!zh->reg->key_buf)
1211 int mem = 8*1024*1024;
1212 zh->reg->key_buf = (char**) xmalloc (mem);
1213 zh->reg->ptr_top = mem/sizeof(char*);
1215 zh->reg->key_buf_used = 0;
1216 zh->reg->key_file_no = 0;
1218 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1219 while (off < reckeys->buf_used)
1221 const char *src = reckeys->buf + off;
1230 memcpy (&ch, src, sizeof(ch));
1236 memcpy (&attrSet, src, sizeof(attrSet));
1237 src += sizeof(attrSet);
1241 memcpy (&attrUse, src, sizeof(attrUse));
1242 src += sizeof(attrUse);
1245 if (zh->reg->key_buf_used + 1024 >
1246 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1247 extract_flushWriteKeys (zh);
1249 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1250 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1253 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1255 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1258 zh->reg->key_buf_used +=
1259 key_SU_encode (ch,((char*)zh->reg->key_buf) +
1260 zh->reg->key_buf_used);
1263 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1265 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1266 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1269 seqno += ((lead>>2) & 15)-1;
1272 memcpy (&seqno, src, sizeof(seqno));
1273 src += sizeof(seqno);
1277 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used, &key, sizeof(key));
1278 (zh->reg->key_buf_used) += sizeof(key);
1279 off = src - reckeys->buf;
1281 assert (off == reckeys->buf_used);
1284 void extract_flushWriteKeys (ZebraHandle zh)
1287 char out_fname[200];
1289 struct encode_info encode_info;
1290 int ptr_i = zh->reg->ptr_i;
1294 if (!zh->reg->key_buf || ptr_i <= 0)
1297 (zh->reg->key_file_no)++;
1298 logf (LOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1300 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1301 sizeof(char*), key_qsort_compare);
1302 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1304 if (!(outf = fopen (out_fname, "wb")))
1306 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1309 logf (LOG_LOG, "writing section %d", zh->reg->key_file_no);
1310 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1312 encode_key_init (&encode_info);
1313 encode_key_write (cp, &encode_info, outf);
1317 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1318 if (strcmp (cp, prevcp))
1320 encode_key_init (&encode_info);
1321 encode_key_write (cp, &encode_info, outf);
1325 encode_key_write (cp + strlen(cp), &encode_info, outf);
1328 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1329 extract_get_fname_tmp (out_fname, key_file_no);
1331 if (!(outf = fopen (out_fname, "wb")))
1333 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1336 logf (LOG_LOG, "writing section %d", key_file_no);
1338 prevcp = key_buf[ptr_top-i];
1340 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1342 key_y_len = strlen(prevcp)+1;
1344 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1345 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1347 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1348 sizeof(char*), key_y_compare);
1349 cp = key_buf[ptr_top-ptr_i];
1351 encode_key_init (&encode_info);
1352 encode_key_write (cp, &encode_info, outf);
1355 cp = key_buf[ptr_top-ptr_i];
1356 encode_key_write (cp+key_y_len, &encode_info, outf);
1360 prevcp = key_buf[ptr_top-ptr_i];
1365 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1368 logf (LOG_LOG, "finished section %d", zh->reg->key_file_no);
1370 zh->reg->key_buf_used = 0;
1373 void extract_add_index_string (RecWord *p, const char *string,
1377 unsigned char attrSet;
1378 unsigned short attrUse;
1381 int *pseqno = &p->seqno;
1382 ZebraHandle zh = p->extractCtrl->handle;
1383 ZebraExplainInfo zei = zh->reg->zei;
1384 struct recKeys *keys = &zh->reg->keys;
1386 if (keys->buf_used+1024 > keys->buf_max)
1390 b = (char *) xmalloc (keys->buf_max += 128000);
1391 if (keys->buf_used > 0)
1392 memcpy (b, keys->buf, keys->buf_used);
1396 dst = keys->buf + keys->buf_used;
1398 attrSet = p->attrSet;
1399 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1402 keys->prevAttrSet = attrSet;
1403 attrUse = p->attrUse;
1404 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1407 keys->prevAttrUse = attrUse;
1409 diff = 1 + *pseqno - keys->prevSeqNo;
1410 if (diff >= 1 && diff <= 15)
1411 lead |= (diff << 2);
1415 keys->prevSeqNo = *pseqno;
1422 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1425 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1426 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1427 attrSet, attrUse, ch);
1430 memcpy (dst, &ch, sizeof(ch));
1436 memcpy (dst, &attrSet, sizeof(attrSet));
1437 dst += sizeof(attrSet);
1441 memcpy (dst, &attrUse, sizeof(attrUse));
1442 dst += sizeof(attrUse);
1445 *dst++ = p->reg_type;
1446 memcpy (dst, string, length);
1452 memcpy (dst, pseqno, sizeof(*pseqno));
1453 dst += sizeof(*pseqno);
1455 keys->buf_used = dst - keys->buf;
1458 static void extract_add_sort_string (RecWord *p, const char *string,
1462 ZebraHandle zh = p->extractCtrl->handle;
1464 for (sk = zh->reg->sortKeys; sk; sk = sk->next)
1465 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1468 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1469 sk->next = zh->reg->sortKeys;
1470 zh->reg->sortKeys = sk;
1472 sk->string = (char *) xmalloc (length);
1473 sk->length = length;
1474 memcpy (sk->string, string, length);
1476 sk->attrSet = p->attrSet;
1477 sk->attrUse = p->attrUse;
1480 void extract_add_string (RecWord *p, const char *string, int length)
1482 assert (length > 0);
1483 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1484 extract_add_sort_string (p, string, length);
1486 extract_add_index_string (p, string, length);
1489 static void extract_add_incomplete_field (RecWord *p)
1491 const char *b = p->string;
1492 int remain = p->length;
1493 const char **map = 0;
1496 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1500 char buf[IT_MAX_WORD+1];
1504 while (map && *map && **map == *CHR_SPACE)
1506 remain = p->length - (b - p->string);
1508 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1515 while (map && *map && **map != *CHR_SPACE)
1517 const char *cp = *map;
1519 while (i < IT_MAX_WORD && *cp)
1521 remain = p->length - (b - p->string);
1523 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1529 extract_add_string (p, buf, i);
1534 static void extract_add_complete_field (RecWord *p)
1536 const char *b = p->string;
1537 char buf[IT_MAX_WORD+1];
1538 const char **map = 0;
1539 int i = 0, remain = p->length;
1542 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1544 while (remain > 0 && i < IT_MAX_WORD)
1546 while (map && *map && **map == *CHR_SPACE)
1548 remain = p->length - (b - p->string);
1550 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1557 if (i && i < IT_MAX_WORD)
1558 buf[i++] = *CHR_SPACE;
1559 while (map && *map && **map != *CHR_SPACE)
1561 const char *cp = *map;
1563 if (i >= IT_MAX_WORD)
1565 while (i < IT_MAX_WORD && *cp)
1567 remain = p->length - (b - p->string);
1569 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1577 extract_add_string (p, buf, i);
1580 void extract_token_add (RecWord *p)
1583 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1584 p->string, p->length)))
1586 p->string = wrbuf_buf(wrbuf);
1587 p->length = wrbuf_len(wrbuf);
1589 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1590 extract_add_complete_field (p);
1592 extract_add_incomplete_field(p);
1595 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1597 ZebraHandle zh = (ZebraHandle) (p->handle);
1598 zebraExplain_addSchema (zh->reg->zei, oid);
1601 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1602 int cmd, struct sortKey **skp)
1604 struct sortKey *sk = *skp;
1605 SortIdx sortIdx = zh->reg->sortIdx;
1607 sortIdx_sysno (sortIdx, sysno);
1610 struct sortKey *sk_next = sk->next;
1611 sortIdx_type (sortIdx, sk->attrUse);
1612 sortIdx_add (sortIdx, sk->string, sk->length);
1620 void encode_key_init (struct encode_info *i)
1627 char *encode_key_int (int d, char *bp)
1631 else if (d <= 16383)
1633 *bp++ = 64 + (d>>8);
1636 else if (d <= 4194303)
1638 *bp++ = 128 + (d>>16);
1639 *bp++ = (d>>8) & 255;
1644 *bp++ = 192 + (d>>24);
1645 *bp++ = (d>>16) & 255;
1646 *bp++ = (d>>8) & 255;
1652 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1657 while ((*bp++ = *k++))
1659 memcpy (&key, k+1, sizeof(struct it_key));
1660 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1661 if (i->sysno != key.sysno)
1663 i->sysno = key.sysno;
1666 else if (!i->seqno && !key.seqno && i->cmd == *k)
1668 bp = encode_key_int (key.seqno - i->seqno, bp);
1669 i->seqno = key.seqno;
1671 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1673 logf (LOG_FATAL|LOG_ERRNO, "fwrite");