2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: extract.c,v 1.117 2002-04-15 14:05:43 adam Exp $
21 #if _FILE_OFFSET_BITS == 64
22 #define PRINTF_OFF_T "%Ld"
24 #define PRINTF_OFF_T "%ld"
27 static void logRecord (ZebraHandle zh)
29 ++zh->records_processed;
30 if (!(zh->records_processed % 1000))
32 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
33 zh->records_processed, zh->records_inserted, zh->records_updated,
38 static void extract_init (struct recExtractCtrl *p, RecWord *w)
40 w->zebra_maps = p->zebra_maps;
42 w->attrSet = VAL_BIB1;
48 static const char **searchRecordKey (ZebraHandle zh,
49 struct recKeys *reckeys,
50 int attrSetS, int attrUseS)
52 static const char *ws[32];
64 for (i = 0; i<32; i++)
68 chS = zebraExplain_lookupSU (zh->reg->zei, attrSetS, attrUseS);
72 while (off < reckeys->buf_used)
75 const char *src = reckeys->buf + off;
83 memcpy (&ch, src, sizeof(ch));
89 memcpy (&attrSet, src, sizeof(attrSet));
90 src += sizeof(attrSet);
94 memcpy (&attrUse, src, sizeof(attrUse));
95 src += sizeof(attrUse);
102 seqno += ((lead>>2) & 15)-1;
105 memcpy (&seqno, src, sizeof(seqno));
106 src += sizeof(seqno);
112 attrUseS == attrUse && attrSetS == attrSet
121 woff = seqno - startSeq;
122 if (woff >= 0 && woff < 31)
126 off = src - reckeys->buf;
128 assert (off == reckeys->buf_used);
132 struct file_read_info {
133 off_t file_max; /* maximum offset so far */
134 off_t file_offset; /* current offset */
135 off_t file_moffset; /* offset of rec/rec boundary */
142 static struct file_read_info *file_read_start (int fd)
144 struct file_read_info *fi = (struct file_read_info *)
145 xmalloc (sizeof(*fi));
149 fi->file_moffset = 0;
155 static void file_read_stop (struct file_read_info *fi)
160 static off_t file_seek (void *handle, off_t offset)
162 struct file_read_info *p = (struct file_read_info *) handle;
163 p->file_offset = offset;
166 return lseek (p->fd, offset, SEEK_SET);
169 static off_t file_tell (void *handle)
171 struct file_read_info *p = (struct file_read_info *) handle;
172 return p->file_offset;
175 static int file_read (void *handle, char *buf, size_t count)
177 struct file_read_info *p = (struct file_read_info *) handle;
183 if (r > p->sdrmax - p->file_offset)
184 r = p->sdrmax - p->file_offset;
186 memcpy (buf, p->sdrbuf + p->file_offset, r);
189 r = read (fd, buf, count);
193 if (p->file_offset > p->file_max)
194 p->file_max = p->file_offset;
199 static void file_begin (void *handle)
201 struct file_read_info *p = (struct file_read_info *) handle;
203 p->file_offset = p->file_moffset;
204 if (!p->sdrbuf && p->file_moffset)
205 lseek (p->fd, p->file_moffset, SEEK_SET);
209 static void file_end (void *handle, off_t offset)
211 struct file_read_info *p = (struct file_read_info *) handle;
213 assert (p->file_more == 0);
215 p->file_moffset = offset;
218 static char *fileMatchStr (ZebraHandle zh,
219 struct recKeys *reckeys, struct recordGroup *rGroup,
220 const char *fname, const char *spec)
222 static char dstBuf[2048]; /* static here ??? */
224 const char *s = spec;
225 static const char **w;
229 while (*s == ' ' || *s == '\t')
235 char attset_str[64], attname_str[64];
236 data1_attset *attset;
239 int attSet = 1, attUse = 1;
243 for (i = 0; *s && *s != ',' && *s != ')'; s++)
245 attset_str[i++] = *s;
246 attset_str[i] = '\0';
251 for (i = 0; *s && *s != ')'; s++)
253 attname_str[i++] = *s;
254 attname_str[i] = '\0';
257 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
260 attSet = attset->reference;
261 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
265 attUse = atoi (attname_str);
267 w = searchRecordKey (zh, reckeys, attSet, attUse);
272 for (i = 0; i<32; i++)
277 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
278 spec, rGroup->groupName ? rGroup->groupName : "none");
283 for (i = 0; i<32; i++)
284 if (matchFlag[i] && w[i])
296 logf (LOG_WARN, "Record didn't contain match"
297 " fields in (%s,%s)", attset_str, attname_str);
305 const char *spec_src = NULL;
306 const char *s1 = ++s;
307 while (*s1 && *s1 != ' ' && *s1 != '\t')
313 memcpy (special, s, spec_len);
314 special[spec_len] = '\0';
317 if (!strcmp (special, "group"))
318 spec_src = rGroup->groupName;
319 else if (!strcmp (special, "database"))
320 spec_src = rGroup->databaseName;
321 else if (!strcmp (special, "filename"))
323 else if (!strcmp (special, "type"))
324 spec_src = rGroup->recordType;
329 strcpy (dst, spec_src);
330 dst += strlen (spec_src);
333 else if (*s == '\"' || *s == '\'')
335 int stopMarker = *s++;
339 while (*s && *s != stopMarker)
342 tmpString[i++] = *s++;
347 strcpy (dst, tmpString);
348 dst += strlen (tmpString);
352 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
353 spec, rGroup->groupName ? rGroup->groupName : "none");
360 logf (LOG_WARN, "No match criteria for record %s in group %s",
361 fname, rGroup->groupName ? rGroup->groupName : "none");
368 struct recordLogInfo {
371 struct recordGroup *rGroup;
374 static void recordLogPreamble (int level, const char *msg, void *info)
376 struct recordLogInfo *p = (struct recordLogInfo *) info;
377 FILE *outf = yaz_log_file ();
381 fprintf (outf, "File %s, offset %d, type %s\n",
382 p->rGroup->recordType, p->recordOffset, p->fname);
383 log_event_start (NULL, NULL);
387 static int recordExtract (ZebraHandle zh,
388 SYSNO *sysno, const char *fname,
389 struct recordGroup *rGroup, int deleteFlag,
390 struct file_read_info *fi,
391 RecType recType, char *subType, void *clientData)
393 RecordAttr *recordAttr;
398 struct recordLogInfo logInfo;
399 off_t recordOffset = 0;
403 struct recExtractCtrl extractCtrl;
405 /* we are going to read from a file, so prepare the extraction */
408 zh->reg->keys.buf_used = 0;
409 zh->reg->keys.prevAttrUse = -1;
410 zh->reg->keys.prevAttrSet = -1;
411 zh->reg->keys.prevSeqNo = 0;
412 zh->reg->sortKeys = 0;
414 recordOffset = fi->file_moffset;
415 extractCtrl.offset = fi->file_moffset;
416 extractCtrl.readf = file_read;
417 extractCtrl.seekf = file_seek;
418 extractCtrl.tellf = file_tell;
419 extractCtrl.endf = file_end;
421 extractCtrl.subType = subType;
422 extractCtrl.init = extract_init;
423 extractCtrl.tokenAdd = extract_token_add;
424 extractCtrl.schemaAdd = extract_schema_add;
425 extractCtrl.dh = zh->reg->dh;
426 extractCtrl.handle = zh;
427 for (i = 0; i<256; i++)
429 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
430 extractCtrl.seqno[i] = 1;
432 extractCtrl.seqno[i] = 0;
434 extractCtrl.zebra_maps = zh->reg->zebra_maps;
435 extractCtrl.flagShowRecords = !rGroup->flagRw;
438 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
440 logInfo.fname = fname;
441 logInfo.recordOffset = recordOffset;
442 logInfo.rGroup = rGroup;
443 log_event_start (recordLogPreamble, &logInfo);
445 r = (*recType->extract)(clientData, &extractCtrl);
447 log_event_start (NULL, NULL);
449 if (r == RECCTRL_EXTRACT_EOF)
451 else if (r == RECCTRL_EXTRACT_ERROR)
453 /* error occured during extraction ... */
454 if (rGroup->flagRw &&
455 zh->records_processed < rGroup->fileVerboseLimit)
457 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
458 fname, recordOffset);
462 if (zh->reg->keys.buf_used == 0)
464 /* the extraction process returned no information - the record
465 is probably empty - unless flagShowRecords is in use */
469 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
470 fname, recordOffset);
475 /* perform match if sysno not known and if match criteria is specified */
482 if (rGroup->recordId && *rGroup->recordId)
486 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
490 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
492 memcpy (sysno, rinfo+1, sizeof(*sysno));
496 logf (LOG_WARN, "Bad match criteria");
507 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
508 fname, recordOffset);
509 logf (LOG_WARN, "cannot delete record above (seems new)");
512 if (zh->records_processed < rGroup->fileVerboseLimit)
513 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
514 fname, recordOffset);
515 rec = rec_new (zh->reg->records);
519 recordAttr = rec_init_attr (zh->reg->zei, rec);
523 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
525 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
526 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
528 zh->records_inserted++;
532 /* record already exists */
533 struct recKeys delkeys;
535 rec = rec_get (zh->reg->records, *sysno);
538 recordAttr = rec_init_attr (zh->reg->zei, rec);
540 if (recordAttr->runNumber ==
541 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
543 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
544 fname, recordOffset);
545 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
550 delkeys.buf_used = rec->size[recInfo_delKeys];
551 delkeys.buf = rec->info[recInfo_delKeys];
552 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
553 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
556 /* record going to be deleted */
557 if (!delkeys.buf_used)
559 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
560 rGroup->recordType, fname, recordOffset);
561 logf (LOG_WARN, "cannot delete file above, storeKeys false");
565 if (zh->records_processed < rGroup->fileVerboseLimit)
566 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
567 rGroup->recordType, fname, recordOffset);
568 zh->records_deleted++;
570 dict_delete (zh->reg->matchDict, matchStr);
571 rec_del (zh->reg->records, &rec);
579 /* record going to be updated */
580 if (!delkeys.buf_used)
582 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
583 rGroup->recordType, fname, recordOffset);
584 logf (LOG_WARN, "cannot update file above, storeKeys false");
588 if (zh->records_processed < rGroup->fileVerboseLimit)
589 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
590 rGroup->recordType, fname, recordOffset);
591 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
592 zh->records_updated++;
596 /* update file type */
597 xfree (rec->info[recInfo_fileType]);
598 rec->info[recInfo_fileType] =
599 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
601 /* update filename */
602 xfree (rec->info[recInfo_filename]);
603 rec->info[recInfo_filename] =
604 rec_strdup (fname, &rec->size[recInfo_filename]);
606 /* update delete keys */
607 xfree (rec->info[recInfo_delKeys]);
608 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
611 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
612 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
613 zh->reg->keys.buf = NULL;
614 zh->reg->keys.buf_max = 0;
616 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
617 rec->size[recInfo_delKeys] = reckeys.buf_used;
618 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
619 rec->size[recInfo_delKeys]);
624 rec->info[recInfo_delKeys] = NULL;
625 rec->size[recInfo_delKeys] = 0;
628 /* save file size of original record */
629 zebraExplain_recordBytesIncrement (zh->reg->zei,
630 - recordAttr->recordSize);
631 recordAttr->recordSize = fi->file_moffset - recordOffset;
632 if (!recordAttr->recordSize)
633 recordAttr->recordSize = fi->file_max - recordOffset;
634 zebraExplain_recordBytesIncrement (zh->reg->zei,
635 recordAttr->recordSize);
637 /* set run-number for this record */
638 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
641 /* update store data */
642 xfree (rec->info[recInfo_storeData]);
643 if (rGroup->flagStoreData == 1)
645 rec->size[recInfo_storeData] = recordAttr->recordSize;
646 rec->info[recInfo_storeData] = (char *)
647 xmalloc (recordAttr->recordSize);
648 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
650 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
651 recordOffset, fname);
654 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
655 < recordAttr->recordSize)
657 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
658 recordAttr->recordSize, fname);
664 rec->info[recInfo_storeData] = NULL;
665 rec->size[recInfo_storeData] = 0;
667 /* update database name */
668 xfree (rec->info[recInfo_databaseName]);
669 rec->info[recInfo_databaseName] =
670 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
673 recordAttr->recordOffset = recordOffset;
675 /* commit this record */
676 rec_put (zh->reg->records, &rec);
681 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
682 const struct recordGroup *rGroupP, int deleteFlag)
690 struct recordGroup rGroupM;
691 struct recordGroup *rGroup = &rGroupM;
692 struct file_read_info *fi;
695 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
697 if (!rGroup->groupName || !*rGroup->groupName)
700 sprintf (gprefix, "%s.", rGroup->groupName);
702 logf (LOG_DEBUG, "fileExtract %s", fname);
704 /* determine file extension */
706 for (i = strlen(fname); --i >= 0; )
709 else if (fname[i] == '.')
711 strcpy (ext, fname+i+1);
714 /* determine file type - depending on extension */
715 if (!rGroup->recordType)
717 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
718 if (!(rGroup->recordType = res_get (zh->res, ext_res)))
720 sprintf (ext_res, "%srecordType", gprefix);
721 rGroup->recordType = res_get (zh->res, ext_res);
724 if (!rGroup->recordType)
726 if (zh->records_processed < rGroup->fileVerboseLimit)
727 logf (LOG_LOG, "? %s", fname);
730 if (!*rGroup->recordType)
733 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
736 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
740 /* determine match criteria */
741 if (!rGroup->recordId)
743 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
744 rGroup->recordId = res_get (zh->res, ext_res);
747 /* determine database name */
748 if (!rGroup->databaseName)
750 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
751 if (!(rGroup->databaseName = res_get (zh->res, ext_res)))
753 sprintf (ext_res, "%sdatabase", gprefix);
754 rGroup->databaseName = res_get (zh->res, ext_res);
757 if (!rGroup->databaseName)
758 rGroup->databaseName = "Default";
760 /* determine if explain database */
762 sprintf (ext_res, "%sexplainDatabase", gprefix);
763 rGroup->explainDatabase =
764 atoi (res_get_def (zh->res, ext_res, "0"));
766 /* announce database */
767 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
769 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName,
770 rGroup->explainDatabase))
774 if (rGroup->flagStoreData == -1)
777 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
778 if (!(sval = res_get (zh->res, ext_res)))
780 sprintf (ext_res, "%sstoreData", gprefix);
781 sval = res_get (zh->res, ext_res);
784 rGroup->flagStoreData = atoi (sval);
786 if (rGroup->flagStoreData == -1)
787 rGroup->flagStoreData = 0;
789 if (rGroup->flagStoreKeys == -1)
793 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
794 sval = res_get (zh->res, ext_res);
797 sprintf (ext_res, "%sstoreKeys", gprefix);
798 sval = res_get (zh->res, ext_res);
801 sval = res_get (zh->res, "storeKeys");
803 rGroup->flagStoreKeys = atoi (sval);
805 if (rGroup->flagStoreKeys == -1)
806 rGroup->flagStoreKeys = 0;
808 if (sysno && deleteFlag)
814 if (zh->path_reg && !yaz_is_abspath (fname))
816 strcpy (full_rep, zh->path_reg);
817 strcat (full_rep, "/");
818 strcat (full_rep, fname);
821 strcpy (full_rep, fname);
824 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
826 logf (LOG_WARN|LOG_ERRNO, "open %s", full_rep);
830 fi = file_read_start (fd);
834 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
835 recType, subType, clientData);
836 } while (r && !sysno && fi->file_more);
844 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
845 const char *buf, size_t buf_size,
846 const char *databaseName, int delete_flag,
847 int test_mode, int *sysno,
848 int store_keys, int store_data,
849 const char *match_criteria)
851 RecordAttr *recordAttr;
852 struct recExtractCtrl extractCtrl;
858 const char *fname = "<no file>";
860 long recordOffset = 0;
861 struct zebra_fetch_control fc;
864 fc.record_int_buf = buf;
865 fc.record_int_len = buf_size;
866 fc.record_int_pos = 0;
868 fc.record_offset = 0;
870 extractCtrl.offset = 0;
871 extractCtrl.readf = zebra_record_int_read;
872 extractCtrl.seekf = zebra_record_int_seek;
873 extractCtrl.tellf = zebra_record_int_tell;
874 extractCtrl.endf = zebra_record_int_end;
875 extractCtrl.fh = &fc;
877 /* announce database */
878 if (zebraExplain_curDatabase (zh->reg->zei, databaseName))
880 if (zebraExplain_newDatabase (zh->reg->zei, databaseName, 0))
884 recType_byName (zh->reg->recTypes, recordType, subType,
887 logf (LOG_WARN, "No such record type: %s", recordType);
891 zh->reg->keys.buf_used = 0;
892 zh->reg->keys.prevAttrUse = -1;
893 zh->reg->keys.prevAttrSet = -1;
894 zh->reg->keys.prevSeqNo = 0;
895 zh->reg->sortKeys = 0;
897 extractCtrl.subType = subType;
898 extractCtrl.init = extract_init;
899 extractCtrl.tokenAdd = extract_token_add;
900 extractCtrl.schemaAdd = extract_schema_add;
901 extractCtrl.dh = zh->reg->dh;
902 extractCtrl.handle = zh;
903 extractCtrl.zebra_maps = zh->reg->zebra_maps;
904 extractCtrl.flagShowRecords = 0;
905 for (i = 0; i<256; i++)
907 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
908 extractCtrl.seqno[i] = 1;
910 extractCtrl.seqno[i] = 0;
913 r = (*recType->extract)(clientData, &extractCtrl);
915 if (r == RECCTRL_EXTRACT_EOF)
917 else if (r == RECCTRL_EXTRACT_ERROR)
919 /* error occured during extraction ... */
921 yaz_log (LOG_WARN, "extract error");
923 if (rGroup->flagRw &&
924 zh->records_processed < rGroup->fileVerboseLimit)
926 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
927 fname, (long) recordOffset);
932 if (zh->reg->keys.buf_used == 0)
934 /* the extraction process returned no information - the record
935 is probably empty - unless flagShowRecords is in use */
938 logf (LOG_WARN, "No keys generated for record");
939 logf (LOG_WARN, " The file is probably empty");
949 logf (LOG_LOG, "delete %s %s %ld", recordType,
950 fname, (long) recordOffset);
951 logf (LOG_WARN, "cannot delete record above (seems new)");
954 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
955 (long) recordOffset);
956 rec = rec_new (zh->reg->records);
960 recordAttr = rec_init_attr (zh->reg->zei, rec);
964 dict_insert (zh->reg->matchDict, matchStr,
965 sizeof(*sysno), sysno);
967 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
968 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
972 /* record already exists */
973 struct recKeys delkeys;
975 rec = rec_get (zh->reg->records, *sysno);
978 recordAttr = rec_init_attr (zh->reg->zei, rec);
980 if (recordAttr->runNumber ==
981 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
983 logf (LOG_LOG, "skipped %s %s %ld", recordType,
984 fname, (long) recordOffset);
988 delkeys.buf_used = rec->size[recInfo_delKeys];
989 delkeys.buf = rec->info[recInfo_delKeys];
990 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
991 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
994 /* record going to be deleted */
995 if (!delkeys.buf_used)
997 logf (LOG_LOG, "delete %s %s %ld", recordType,
998 fname, (long) recordOffset);
999 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1003 logf (LOG_LOG, "delete %s %s %ld", recordType,
1004 fname, (long) recordOffset);
1007 dict_delete (matchDict, matchStr);
1009 rec_del (zh->reg->records, &rec);
1016 /* record going to be updated */
1017 if (!delkeys.buf_used)
1019 logf (LOG_LOG, "update %s %s %ld", recordType,
1020 fname, (long) recordOffset);
1021 logf (LOG_WARN, "cannot update file above, storeKeys false");
1025 logf (LOG_LOG, "update %s %s %ld", recordType,
1026 fname, (long) recordOffset);
1027 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1031 /* update file type */
1032 xfree (rec->info[recInfo_fileType]);
1033 rec->info[recInfo_fileType] =
1034 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1036 /* update filename */
1037 xfree (rec->info[recInfo_filename]);
1038 rec->info[recInfo_filename] =
1039 rec_strdup (fname, &rec->size[recInfo_filename]);
1041 /* update delete keys */
1042 xfree (rec->info[recInfo_delKeys]);
1043 if (zh->reg->keys.buf_used > 0 && store_keys == 1)
1045 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1046 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1047 zh->reg->keys.buf = NULL;
1048 zh->reg->keys.buf_max = 0;
1052 rec->info[recInfo_delKeys] = NULL;
1053 rec->size[recInfo_delKeys] = 0;
1056 /* save file size of original record */
1057 zebraExplain_recordBytesIncrement (zh->reg->zei,
1058 - recordAttr->recordSize);
1060 recordAttr->recordSize = fi->file_moffset - recordOffset;
1061 if (!recordAttr->recordSize)
1062 recordAttr->recordSize = fi->file_max - recordOffset;
1064 recordAttr->recordSize = buf_size;
1066 zebraExplain_recordBytesIncrement (zh->reg->zei,
1067 recordAttr->recordSize);
1069 /* set run-number for this record */
1070 recordAttr->runNumber =
1071 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1073 /* update store data */
1074 xfree (rec->info[recInfo_storeData]);
1075 if (store_data == 1)
1077 rec->size[recInfo_storeData] = recordAttr->recordSize;
1078 rec->info[recInfo_storeData] = (char *)
1079 xmalloc (recordAttr->recordSize);
1081 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1083 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1085 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1086 (long) recordOffset, fname);
1089 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1090 < recordAttr->recordSize)
1092 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1093 recordAttr->recordSize, fname);
1100 rec->info[recInfo_storeData] = NULL;
1101 rec->size[recInfo_storeData] = 0;
1103 /* update database name */
1104 xfree (rec->info[recInfo_databaseName]);
1105 rec->info[recInfo_databaseName] =
1106 rec_strdup (databaseName, &rec->size[recInfo_databaseName]);
1109 recordAttr->recordOffset = recordOffset;
1111 /* commit this record */
1112 rec_put (zh->reg->records, &rec);
1117 int explain_extract (void *handle, Record rec, data1_node *n)
1119 ZebraHandle zh = (ZebraHandle) handle;
1120 struct recExtractCtrl extractCtrl;
1123 if (zebraExplain_curDatabase (zh->reg->zei,
1124 rec->info[recInfo_databaseName]))
1127 if (zebraExplain_newDatabase (zh->reg->zei,
1128 rec->info[recInfo_databaseName], 0))
1132 zh->reg->keys.buf_used = 0;
1133 zh->reg->keys.prevAttrUse = -1;
1134 zh->reg->keys.prevAttrSet = -1;
1135 zh->reg->keys.prevSeqNo = 0;
1136 zh->reg->sortKeys = 0;
1138 extractCtrl.init = extract_init;
1139 extractCtrl.tokenAdd = extract_token_add;
1140 extractCtrl.schemaAdd = extract_schema_add;
1141 extractCtrl.dh = zh->reg->dh;
1142 for (i = 0; i<256; i++)
1143 extractCtrl.seqno[i] = 0;
1144 extractCtrl.zebra_maps = zh->reg->zebra_maps;
1145 extractCtrl.flagShowRecords = 0;
1146 extractCtrl.handle = handle;
1148 grs_extract_tree(&extractCtrl, n);
1150 if (rec->size[recInfo_delKeys])
1152 struct recKeys delkeys;
1153 struct sortKey *sortKeys = 0;
1155 delkeys.buf_used = rec->size[recInfo_delKeys];
1156 delkeys.buf = rec->info[recInfo_delKeys];
1157 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1158 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1160 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys);
1161 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1163 xfree (rec->info[recInfo_delKeys]);
1164 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1165 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1166 zh->reg->keys.buf = NULL;
1167 zh->reg->keys.buf_max = 0;
1171 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1172 int cmd, struct recKeys *reckeys)
1176 unsigned char attrSet = (unsigned char) -1;
1177 unsigned short attrUse = (unsigned short) -1;
1182 ZebraExplainInfo zei = zh->reg->zei;
1184 if (!zh->reg->key_buf)
1186 int mem = 8*1024*1024;
1187 zh->reg->key_buf = (char**) xmalloc (mem);
1188 zh->reg->ptr_top = mem/sizeof(char*);
1190 zh->reg->key_buf_used = 0;
1191 zh->reg->key_file_no = 0;
1193 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1194 while (off < reckeys->buf_used)
1196 const char *src = reckeys->buf + off;
1205 memcpy (&ch, src, sizeof(ch));
1211 memcpy (&attrSet, src, sizeof(attrSet));
1212 src += sizeof(attrSet);
1216 memcpy (&attrUse, src, sizeof(attrUse));
1217 src += sizeof(attrUse);
1220 if (zh->reg->key_buf_used + 1024 >
1221 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1222 extract_flushWriteKeys (zh);
1224 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1225 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1228 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1230 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1233 zh->reg->key_buf_used +=
1234 key_SU_encode (ch,((char*)zh->reg->key_buf) +
1235 zh->reg->key_buf_used);
1238 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1240 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1241 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1244 seqno += ((lead>>2) & 15)-1;
1247 memcpy (&seqno, src, sizeof(seqno));
1248 src += sizeof(seqno);
1252 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used, &key, sizeof(key));
1253 (zh->reg->key_buf_used) += sizeof(key);
1254 off = src - reckeys->buf;
1256 assert (off == reckeys->buf_used);
1259 void extract_flushWriteKeys (ZebraHandle zh)
1262 char out_fname[200];
1264 struct encode_info encode_info;
1265 int ptr_i = zh->reg->ptr_i;
1269 if (!zh->reg->key_buf || ptr_i <= 0)
1272 (zh->reg->key_file_no)++;
1273 logf (LOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1275 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i, sizeof(char*),
1277 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1279 if (!(outf = fopen (out_fname, "wb")))
1281 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1284 logf (LOG_LOG, "writing section %d", zh->reg->key_file_no);
1285 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1287 encode_key_init (&encode_info);
1288 encode_key_write (cp, &encode_info, outf);
1292 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1293 if (strcmp (cp, prevcp))
1295 encode_key_init (&encode_info);
1296 encode_key_write (cp, &encode_info, outf);
1300 encode_key_write (cp + strlen(cp), &encode_info, outf);
1303 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1304 extract_get_fname_tmp (out_fname, key_file_no);
1306 if (!(outf = fopen (out_fname, "wb")))
1308 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1311 logf (LOG_LOG, "writing section %d", key_file_no);
1313 prevcp = key_buf[ptr_top-i];
1315 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1317 key_y_len = strlen(prevcp)+1;
1319 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1320 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1322 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1323 sizeof(char*), key_y_compare);
1324 cp = key_buf[ptr_top-ptr_i];
1326 encode_key_init (&encode_info);
1327 encode_key_write (cp, &encode_info, outf);
1330 cp = key_buf[ptr_top-ptr_i];
1331 encode_key_write (cp+key_y_len, &encode_info, outf);
1335 prevcp = key_buf[ptr_top-ptr_i];
1340 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1343 logf (LOG_LOG, "finished section %d", zh->reg->key_file_no);
1345 zh->reg->key_buf_used = 0;
1348 void extract_add_index_string (RecWord *p, const char *string,
1352 unsigned char attrSet;
1353 unsigned short attrUse;
1356 int *pseqno = &p->seqno;
1357 ZebraHandle zh = p->extractCtrl->handle;
1358 ZebraExplainInfo zei = zh->reg->zei;
1359 struct recKeys *keys = &zh->reg->keys;
1361 if (keys->buf_used+1024 > keys->buf_max)
1365 b = (char *) xmalloc (keys->buf_max += 128000);
1366 if (keys->buf_used > 0)
1367 memcpy (b, keys->buf, keys->buf_used);
1371 dst = keys->buf + keys->buf_used;
1373 attrSet = p->attrSet;
1374 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1377 keys->prevAttrSet = attrSet;
1378 attrUse = p->attrUse;
1379 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1382 keys->prevAttrUse = attrUse;
1384 diff = 1 + *pseqno - keys->prevSeqNo;
1385 if (diff >= 1 && diff <= 15)
1386 lead |= (diff << 2);
1390 keys->prevSeqNo = *pseqno;
1397 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1400 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1401 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1402 attrSet, attrUse, ch);
1405 memcpy (dst, &ch, sizeof(ch));
1411 memcpy (dst, &attrSet, sizeof(attrSet));
1412 dst += sizeof(attrSet);
1416 memcpy (dst, &attrUse, sizeof(attrUse));
1417 dst += sizeof(attrUse);
1420 *dst++ = p->reg_type;
1421 memcpy (dst, string, length);
1427 memcpy (dst, pseqno, sizeof(*pseqno));
1428 dst += sizeof(*pseqno);
1430 keys->buf_used = dst - keys->buf;
1433 static void extract_add_sort_string (RecWord *p, const char *string,
1437 ZebraHandle zh = p->extractCtrl->handle;
1439 for (sk = zh->reg->sortKeys; sk; sk = sk->next)
1440 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1443 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1444 sk->next = zh->reg->sortKeys;
1445 zh->reg->sortKeys = sk;
1447 sk->string = (char *) xmalloc (length);
1448 sk->length = length;
1449 memcpy (sk->string, string, length);
1451 sk->attrSet = p->attrSet;
1452 sk->attrUse = p->attrUse;
1455 void extract_add_string (RecWord *p, const char *string, int length)
1457 assert (length > 0);
1458 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1459 extract_add_sort_string (p, string, length);
1461 extract_add_index_string (p, string, length);
1464 static void extract_add_incomplete_field (RecWord *p)
1466 const char *b = p->string;
1467 int remain = p->length;
1468 const char **map = 0;
1471 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1475 char buf[IT_MAX_WORD+1];
1479 while (map && *map && **map == *CHR_SPACE)
1481 remain = p->length - (b - p->string);
1483 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1490 while (map && *map && **map != *CHR_SPACE)
1492 const char *cp = *map;
1494 while (i < IT_MAX_WORD && *cp)
1496 remain = p->length - (b - p->string);
1498 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1504 extract_add_string (p, buf, i);
1509 static void extract_add_complete_field (RecWord *p)
1511 const char *b = p->string;
1512 char buf[IT_MAX_WORD+1];
1513 const char **map = 0;
1514 int i = 0, remain = p->length;
1517 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1519 while (remain > 0 && i < IT_MAX_WORD)
1521 while (map && *map && **map == *CHR_SPACE)
1523 remain = p->length - (b - p->string);
1525 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1532 if (i && i < IT_MAX_WORD)
1533 buf[i++] = *CHR_SPACE;
1534 while (map && *map && **map != *CHR_SPACE)
1536 const char *cp = *map;
1538 if (i >= IT_MAX_WORD)
1540 while (i < IT_MAX_WORD && *cp)
1542 remain = p->length - (b - p->string);
1544 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1552 extract_add_string (p, buf, i);
1555 void extract_token_add (RecWord *p)
1558 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1559 p->string, p->length)))
1561 p->string = wrbuf_buf(wrbuf);
1562 p->length = wrbuf_len(wrbuf);
1564 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1565 extract_add_complete_field (p);
1567 extract_add_incomplete_field(p);
1570 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1572 ZebraHandle zh = (ZebraHandle) (p->handle);
1573 zebraExplain_addSchema (zh->reg->zei, oid);
1576 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1577 int cmd, struct sortKey **skp)
1579 struct sortKey *sk = *skp;
1580 SortIdx sortIdx = zh->reg->sortIdx;
1582 sortIdx_sysno (sortIdx, sysno);
1585 struct sortKey *sk_next = sk->next;
1586 sortIdx_type (sortIdx, sk->attrUse);
1587 sortIdx_add (sortIdx, sk->string, sk->length);
1595 void encode_key_init (struct encode_info *i)
1602 char *encode_key_int (int d, char *bp)
1606 else if (d <= 16383)
1608 *bp++ = 64 + (d>>8);
1611 else if (d <= 4194303)
1613 *bp++ = 128 + (d>>16);
1614 *bp++ = (d>>8) & 255;
1619 *bp++ = 192 + (d>>24);
1620 *bp++ = (d>>16) & 255;
1621 *bp++ = (d>>8) & 255;
1627 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1632 while ((*bp++ = *k++))
1634 memcpy (&key, k+1, sizeof(struct it_key));
1635 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1636 if (i->sysno != key.sysno)
1638 i->sysno = key.sysno;
1641 else if (!i->seqno && !key.seqno && i->cmd == *k)
1643 bp = encode_key_int (key.seqno - i->seqno, bp);
1644 i->seqno = key.seqno;
1646 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1648 logf (LOG_FATAL|LOG_ERRNO, "fwrite");