1 /* $Id: extract.c,v 1.121 2002-08-28 12:47:10 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
37 #if _FILE_OFFSET_BITS == 64
38 #define PRINTF_OFF_T "%Ld"
40 #define PRINTF_OFF_T "%ld"
43 static void shellsort(void *ar, int r, size_t s,
44 int (*cmp)(const void *a, const void *b))
49 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
50 13776, 4592, 1968, 861, 336,
51 112, 48, 21, 7, 3, 1 };
52 for ( k = 0; k < 16; k++)
53 for (h = incs[k], i = h; i < r; i++)
57 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
59 memcpy (a + s*j, a + s*(j-h), s);
67 static void logRecord (ZebraHandle zh)
69 ++zh->records_processed;
70 if (!(zh->records_processed % 1000))
72 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
73 zh->records_processed, zh->records_inserted, zh->records_updated,
78 static void extract_init (struct recExtractCtrl *p, RecWord *w)
80 w->zebra_maps = p->zebra_maps;
82 w->attrSet = VAL_BIB1;
88 static const char **searchRecordKey (ZebraHandle zh,
89 struct recKeys *reckeys,
90 int attrSetS, int attrUseS)
92 static const char *ws[32];
104 for (i = 0; i<32; i++)
108 chS = zebraExplain_lookupSU (zh->reg->zei, attrSetS, attrUseS);
112 while (off < reckeys->buf_used)
115 const char *src = reckeys->buf + off;
123 memcpy (&ch, src, sizeof(ch));
129 memcpy (&attrSet, src, sizeof(attrSet));
130 src += sizeof(attrSet);
134 memcpy (&attrUse, src, sizeof(attrUse));
135 src += sizeof(attrUse);
142 seqno += ((lead>>2) & 15)-1;
145 memcpy (&seqno, src, sizeof(seqno));
146 src += sizeof(seqno);
152 attrUseS == attrUse && attrSetS == attrSet
161 woff = seqno - startSeq;
162 if (woff >= 0 && woff < 31)
166 off = src - reckeys->buf;
168 assert (off == reckeys->buf_used);
172 struct file_read_info {
173 off_t file_max; /* maximum offset so far */
174 off_t file_offset; /* current offset */
175 off_t file_moffset; /* offset of rec/rec boundary */
182 static struct file_read_info *file_read_start (int fd)
184 struct file_read_info *fi = (struct file_read_info *)
185 xmalloc (sizeof(*fi));
189 fi->file_moffset = 0;
195 static void file_read_stop (struct file_read_info *fi)
200 static off_t file_seek (void *handle, off_t offset)
202 struct file_read_info *p = (struct file_read_info *) handle;
203 p->file_offset = offset;
206 return lseek (p->fd, offset, SEEK_SET);
209 static off_t file_tell (void *handle)
211 struct file_read_info *p = (struct file_read_info *) handle;
212 return p->file_offset;
215 static int file_read (void *handle, char *buf, size_t count)
217 struct file_read_info *p = (struct file_read_info *) handle;
223 if (r > p->sdrmax - p->file_offset)
224 r = p->sdrmax - p->file_offset;
226 memcpy (buf, p->sdrbuf + p->file_offset, r);
229 r = read (fd, buf, count);
233 if (p->file_offset > p->file_max)
234 p->file_max = p->file_offset;
239 static void file_begin (void *handle)
241 struct file_read_info *p = (struct file_read_info *) handle;
243 p->file_offset = p->file_moffset;
244 if (!p->sdrbuf && p->file_moffset)
245 lseek (p->fd, p->file_moffset, SEEK_SET);
249 static void file_end (void *handle, off_t offset)
251 struct file_read_info *p = (struct file_read_info *) handle;
253 assert (p->file_more == 0);
255 p->file_moffset = offset;
258 static char *fileMatchStr (ZebraHandle zh,
259 struct recKeys *reckeys, struct recordGroup *rGroup,
260 const char *fname, const char *spec)
262 static char dstBuf[2048]; /* static here ??? */
264 const char *s = spec;
265 static const char **w;
269 while (*s == ' ' || *s == '\t')
275 char attset_str[64], attname_str[64];
276 data1_attset *attset;
279 int attSet = 1, attUse = 1;
283 for (i = 0; *s && *s != ',' && *s != ')'; s++)
285 attset_str[i++] = *s;
286 attset_str[i] = '\0';
291 for (i = 0; *s && *s != ')'; s++)
293 attname_str[i++] = *s;
294 attname_str[i] = '\0';
297 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
300 attSet = attset->reference;
301 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
305 attUse = atoi (attname_str);
307 w = searchRecordKey (zh, reckeys, attSet, attUse);
312 for (i = 0; i<32; i++)
317 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
318 spec, rGroup->groupName ? rGroup->groupName : "none");
323 for (i = 0; i<32; i++)
324 if (matchFlag[i] && w[i])
336 logf (LOG_WARN, "Record didn't contain match"
337 " fields in (%s,%s)", attset_str, attname_str);
345 const char *spec_src = NULL;
346 const char *s1 = ++s;
347 while (*s1 && *s1 != ' ' && *s1 != '\t')
353 memcpy (special, s, spec_len);
354 special[spec_len] = '\0';
357 if (!strcmp (special, "group"))
358 spec_src = rGroup->groupName;
359 else if (!strcmp (special, "database"))
360 spec_src = rGroup->databaseName;
361 else if (!strcmp (special, "filename"))
363 else if (!strcmp (special, "type"))
364 spec_src = rGroup->recordType;
369 strcpy (dst, spec_src);
370 dst += strlen (spec_src);
373 else if (*s == '\"' || *s == '\'')
375 int stopMarker = *s++;
379 while (*s && *s != stopMarker)
382 tmpString[i++] = *s++;
387 strcpy (dst, tmpString);
388 dst += strlen (tmpString);
392 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
393 spec, rGroup->groupName ? rGroup->groupName : "none");
400 logf (LOG_WARN, "No match criteria for record %s in group %s",
401 fname, rGroup->groupName ? rGroup->groupName : "none");
408 struct recordLogInfo {
411 struct recordGroup *rGroup;
414 static void recordLogPreamble (int level, const char *msg, void *info)
416 struct recordLogInfo *p = (struct recordLogInfo *) info;
417 FILE *outf = yaz_log_file ();
421 fprintf (outf, "File %s, offset %d, type %s\n",
422 p->rGroup->recordType, p->recordOffset, p->fname);
423 log_event_start (NULL, NULL);
427 static int recordExtract (ZebraHandle zh,
428 SYSNO *sysno, const char *fname,
429 struct recordGroup *rGroup, int deleteFlag,
430 struct file_read_info *fi,
431 RecType recType, char *subType, void *clientData)
433 RecordAttr *recordAttr;
438 struct recordLogInfo logInfo;
439 off_t recordOffset = 0;
443 struct recExtractCtrl extractCtrl;
445 /* we are going to read from a file, so prepare the extraction */
448 zh->reg->keys.buf_used = 0;
449 zh->reg->keys.prevAttrUse = -1;
450 zh->reg->keys.prevAttrSet = -1;
451 zh->reg->keys.prevSeqNo = 0;
452 zh->reg->sortKeys = 0;
454 recordOffset = fi->file_moffset;
455 extractCtrl.offset = fi->file_moffset;
456 extractCtrl.readf = file_read;
457 extractCtrl.seekf = file_seek;
458 extractCtrl.tellf = file_tell;
459 extractCtrl.endf = file_end;
461 extractCtrl.subType = subType;
462 extractCtrl.init = extract_init;
463 extractCtrl.tokenAdd = extract_token_add;
464 extractCtrl.schemaAdd = extract_schema_add;
465 extractCtrl.dh = zh->reg->dh;
466 extractCtrl.handle = zh;
467 for (i = 0; i<256; i++)
469 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
470 extractCtrl.seqno[i] = 1;
472 extractCtrl.seqno[i] = 0;
474 extractCtrl.zebra_maps = zh->reg->zebra_maps;
475 extractCtrl.flagShowRecords = !rGroup->flagRw;
478 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
480 logInfo.fname = fname;
481 logInfo.recordOffset = recordOffset;
482 logInfo.rGroup = rGroup;
483 log_event_start (recordLogPreamble, &logInfo);
485 r = (*recType->extract)(clientData, &extractCtrl);
487 log_event_start (NULL, NULL);
489 if (r == RECCTRL_EXTRACT_EOF)
491 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
493 /* error occured during extraction ... */
494 if (rGroup->flagRw &&
495 zh->records_processed < rGroup->fileVerboseLimit)
497 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
498 fname, recordOffset);
502 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
504 /* error occured during extraction ... */
505 if (rGroup->flagRw &&
506 zh->records_processed < rGroup->fileVerboseLimit)
508 logf (LOG_WARN, "no filter for %s %s "
509 PRINTF_OFF_T, rGroup->recordType,
510 fname, recordOffset);
514 if (zh->reg->keys.buf_used == 0)
516 /* the extraction process returned no information - the record
517 is probably empty - unless flagShowRecords is in use */
521 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
522 fname, recordOffset);
527 /* perform match if sysno not known and if match criteria is specified */
534 if (rGroup->recordId && *rGroup->recordId)
538 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
542 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
544 memcpy (sysno, rinfo+1, sizeof(*sysno));
548 logf (LOG_WARN, "Bad match criteria");
559 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
560 fname, recordOffset);
561 logf (LOG_WARN, "cannot delete record above (seems new)");
564 if (zh->records_processed < rGroup->fileVerboseLimit)
565 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
566 fname, recordOffset);
567 rec = rec_new (zh->reg->records);
571 recordAttr = rec_init_attr (zh->reg->zei, rec);
575 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
577 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
578 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
580 zh->records_inserted++;
584 /* record already exists */
585 struct recKeys delkeys;
587 rec = rec_get (zh->reg->records, *sysno);
590 recordAttr = rec_init_attr (zh->reg->zei, rec);
592 if (recordAttr->runNumber ==
593 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
595 yaz_log (LOG_LOG, "run number = %d", recordAttr->runNumber);
596 yaz_log (LOG_LOG, "skipped %s %s " PRINTF_OFF_T,
597 rGroup->recordType, fname, recordOffset);
598 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
603 delkeys.buf_used = rec->size[recInfo_delKeys];
604 delkeys.buf = rec->info[recInfo_delKeys];
605 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
606 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
609 /* record going to be deleted */
610 if (!delkeys.buf_used)
612 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
613 rGroup->recordType, fname, recordOffset);
614 logf (LOG_WARN, "cannot delete file above, storeKeys false");
618 if (zh->records_processed < rGroup->fileVerboseLimit)
619 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
620 rGroup->recordType, fname, recordOffset);
621 zh->records_deleted++;
623 dict_delete (zh->reg->matchDict, matchStr);
624 rec_del (zh->reg->records, &rec);
632 /* record going to be updated */
633 if (!delkeys.buf_used)
635 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
636 rGroup->recordType, fname, recordOffset);
637 logf (LOG_WARN, "cannot update file above, storeKeys false");
641 if (zh->records_processed < rGroup->fileVerboseLimit)
642 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
643 rGroup->recordType, fname, recordOffset);
644 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
645 zh->records_updated++;
649 /* update file type */
650 xfree (rec->info[recInfo_fileType]);
651 rec->info[recInfo_fileType] =
652 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
654 /* update filename */
655 xfree (rec->info[recInfo_filename]);
656 rec->info[recInfo_filename] =
657 rec_strdup (fname, &rec->size[recInfo_filename]);
659 /* update delete keys */
660 xfree (rec->info[recInfo_delKeys]);
661 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
664 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
665 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
666 zh->reg->keys.buf = NULL;
667 zh->reg->keys.buf_max = 0;
669 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
670 rec->size[recInfo_delKeys] = reckeys.buf_used;
671 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
672 rec->size[recInfo_delKeys]);
677 rec->info[recInfo_delKeys] = NULL;
678 rec->size[recInfo_delKeys] = 0;
681 /* save file size of original record */
682 zebraExplain_recordBytesIncrement (zh->reg->zei,
683 - recordAttr->recordSize);
684 recordAttr->recordSize = fi->file_moffset - recordOffset;
685 if (!recordAttr->recordSize)
686 recordAttr->recordSize = fi->file_max - recordOffset;
687 zebraExplain_recordBytesIncrement (zh->reg->zei,
688 recordAttr->recordSize);
690 /* set run-number for this record */
691 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
694 /* update store data */
695 xfree (rec->info[recInfo_storeData]);
696 if (rGroup->flagStoreData == 1)
698 rec->size[recInfo_storeData] = recordAttr->recordSize;
699 rec->info[recInfo_storeData] = (char *)
700 xmalloc (recordAttr->recordSize);
701 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
703 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
704 recordOffset, fname);
707 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
708 < recordAttr->recordSize)
710 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
711 recordAttr->recordSize, fname);
717 rec->info[recInfo_storeData] = NULL;
718 rec->size[recInfo_storeData] = 0;
720 /* update database name */
721 xfree (rec->info[recInfo_databaseName]);
722 rec->info[recInfo_databaseName] =
723 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
726 recordAttr->recordOffset = recordOffset;
728 /* commit this record */
729 rec_put (zh->reg->records, &rec);
734 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
735 const struct recordGroup *rGroupP, int deleteFlag)
743 struct recordGroup rGroupM;
744 struct recordGroup *rGroup = &rGroupM;
745 struct file_read_info *fi;
748 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
750 if (!rGroup->groupName || !*rGroup->groupName)
753 sprintf (gprefix, "%s.", rGroup->groupName);
755 logf (LOG_DEBUG, "fileExtract %s", fname);
757 /* determine file extension */
759 for (i = strlen(fname); --i >= 0; )
762 else if (fname[i] == '.')
764 strcpy (ext, fname+i+1);
767 /* determine file type - depending on extension */
768 if (!rGroup->recordType)
770 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
771 if (!(rGroup->recordType = res_get (zh->res, ext_res)))
773 sprintf (ext_res, "%srecordType", gprefix);
774 rGroup->recordType = res_get (zh->res, ext_res);
777 if (!rGroup->recordType)
779 if (zh->records_processed < rGroup->fileVerboseLimit)
780 logf (LOG_LOG, "? %s", fname);
783 if (!*rGroup->recordType)
786 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
789 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
793 /* determine match criteria */
794 if (!rGroup->recordId)
796 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
797 rGroup->recordId = res_get (zh->res, ext_res);
800 /* determine database name */
801 if (!rGroup->databaseName)
803 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
804 if (!(rGroup->databaseName = res_get (zh->res, ext_res)))
806 sprintf (ext_res, "%sdatabase", gprefix);
807 rGroup->databaseName = res_get (zh->res, ext_res);
810 if (!rGroup->databaseName)
811 rGroup->databaseName = "Default";
813 /* determine if explain database */
815 sprintf (ext_res, "%sexplainDatabase", gprefix);
816 rGroup->explainDatabase =
817 atoi (res_get_def (zh->res, ext_res, "0"));
819 /* announce database */
820 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
822 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName,
823 rGroup->explainDatabase))
827 if (rGroup->flagStoreData == -1)
830 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
831 if (!(sval = res_get (zh->res, ext_res)))
833 sprintf (ext_res, "%sstoreData", gprefix);
834 sval = res_get (zh->res, ext_res);
837 rGroup->flagStoreData = atoi (sval);
839 if (rGroup->flagStoreData == -1)
840 rGroup->flagStoreData = 0;
842 if (rGroup->flagStoreKeys == -1)
846 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
847 sval = res_get (zh->res, ext_res);
850 sprintf (ext_res, "%sstoreKeys", gprefix);
851 sval = res_get (zh->res, ext_res);
854 sval = res_get (zh->res, "storeKeys");
856 rGroup->flagStoreKeys = atoi (sval);
858 if (rGroup->flagStoreKeys == -1)
859 rGroup->flagStoreKeys = 0;
861 if (sysno && deleteFlag)
867 if (zh->path_reg && !yaz_is_abspath (fname))
869 strcpy (full_rep, zh->path_reg);
870 strcat (full_rep, "/");
871 strcat (full_rep, fname);
874 strcpy (full_rep, fname);
877 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
879 logf (LOG_WARN|LOG_ERRNO, "open %s", full_rep);
883 fi = file_read_start (fd);
887 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
888 recType, subType, clientData);
889 } while (r && !sysno && fi->file_more);
897 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
898 const char *buf, size_t buf_size,
899 const char *databaseName, int delete_flag,
900 int test_mode, int *sysno,
901 int store_keys, int store_data,
902 const char *match_criteria)
904 RecordAttr *recordAttr;
905 struct recExtractCtrl extractCtrl;
911 const char *fname = "<no file>";
913 long recordOffset = 0;
914 struct zebra_fetch_control fc;
917 fc.record_int_buf = buf;
918 fc.record_int_len = buf_size;
919 fc.record_int_pos = 0;
921 fc.record_offset = 0;
923 extractCtrl.offset = 0;
924 extractCtrl.readf = zebra_record_int_read;
925 extractCtrl.seekf = zebra_record_int_seek;
926 extractCtrl.tellf = zebra_record_int_tell;
927 extractCtrl.endf = zebra_record_int_end;
928 extractCtrl.fh = &fc;
930 /* announce database */
931 if (zebraExplain_curDatabase (zh->reg->zei, databaseName))
933 if (zebraExplain_newDatabase (zh->reg->zei, databaseName, 0))
937 recType_byName (zh->reg->recTypes, recordType, subType,
940 logf (LOG_WARN, "No such record type: %s", recordType);
944 zh->reg->keys.buf_used = 0;
945 zh->reg->keys.prevAttrUse = -1;
946 zh->reg->keys.prevAttrSet = -1;
947 zh->reg->keys.prevSeqNo = 0;
948 zh->reg->sortKeys = 0;
950 extractCtrl.subType = subType;
951 extractCtrl.init = extract_init;
952 extractCtrl.tokenAdd = extract_token_add;
953 extractCtrl.schemaAdd = extract_schema_add;
954 extractCtrl.dh = zh->reg->dh;
955 extractCtrl.handle = zh;
956 extractCtrl.zebra_maps = zh->reg->zebra_maps;
957 extractCtrl.flagShowRecords = 0;
958 for (i = 0; i<256; i++)
960 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
961 extractCtrl.seqno[i] = 1;
963 extractCtrl.seqno[i] = 0;
966 r = (*recType->extract)(clientData, &extractCtrl);
968 if (r == RECCTRL_EXTRACT_EOF)
970 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
972 /* error occured during extraction ... */
973 yaz_log (LOG_WARN, "extract error: generic");
976 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
978 /* error occured during extraction ... */
979 yaz_log (LOG_WARN, "extract error: no such filter");
982 if (zh->reg->keys.buf_used == 0)
984 /* the extraction process returned no information - the record
985 is probably empty - unless flagShowRecords is in use */
988 logf (LOG_WARN, "No keys generated for record");
989 logf (LOG_WARN, " The file is probably empty");
999 logf (LOG_LOG, "delete %s %s %ld", recordType,
1000 fname, (long) recordOffset);
1001 logf (LOG_WARN, "cannot delete record above (seems new)");
1004 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
1005 (long) recordOffset);
1006 rec = rec_new (zh->reg->records);
1008 *sysno = rec->sysno;
1010 recordAttr = rec_init_attr (zh->reg->zei, rec);
1014 dict_insert (zh->reg->matchDict, matchStr,
1015 sizeof(*sysno), sysno);
1017 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1018 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1022 /* record already exists */
1023 struct recKeys delkeys;
1025 rec = rec_get (zh->reg->records, *sysno);
1028 recordAttr = rec_init_attr (zh->reg->zei, rec);
1030 if (recordAttr->runNumber ==
1031 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
1033 logf (LOG_LOG, "skipped %s %s %ld", recordType,
1034 fname, (long) recordOffset);
1038 delkeys.buf_used = rec->size[recInfo_delKeys];
1039 delkeys.buf = rec->info[recInfo_delKeys];
1040 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
1041 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
1044 /* record going to be deleted */
1045 if (!delkeys.buf_used)
1047 logf (LOG_LOG, "delete %s %s %ld", recordType,
1048 fname, (long) recordOffset);
1049 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1053 logf (LOG_LOG, "delete %s %s %ld", recordType,
1054 fname, (long) recordOffset);
1057 dict_delete (matchDict, matchStr);
1059 rec_del (zh->reg->records, &rec);
1066 /* record going to be updated */
1067 if (!delkeys.buf_used)
1069 logf (LOG_LOG, "update %s %s %ld", recordType,
1070 fname, (long) recordOffset);
1071 logf (LOG_WARN, "cannot update file above, storeKeys false");
1075 logf (LOG_LOG, "update %s %s %ld", recordType,
1076 fname, (long) recordOffset);
1077 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1081 /* update file type */
1082 xfree (rec->info[recInfo_fileType]);
1083 rec->info[recInfo_fileType] =
1084 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1086 /* update filename */
1087 xfree (rec->info[recInfo_filename]);
1088 rec->info[recInfo_filename] =
1089 rec_strdup (fname, &rec->size[recInfo_filename]);
1091 /* update delete keys */
1092 xfree (rec->info[recInfo_delKeys]);
1093 if (zh->reg->keys.buf_used > 0 && store_keys == 1)
1095 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1096 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1097 zh->reg->keys.buf = NULL;
1098 zh->reg->keys.buf_max = 0;
1102 rec->info[recInfo_delKeys] = NULL;
1103 rec->size[recInfo_delKeys] = 0;
1106 /* save file size of original record */
1107 zebraExplain_recordBytesIncrement (zh->reg->zei,
1108 - recordAttr->recordSize);
1110 recordAttr->recordSize = fi->file_moffset - recordOffset;
1111 if (!recordAttr->recordSize)
1112 recordAttr->recordSize = fi->file_max - recordOffset;
1114 recordAttr->recordSize = buf_size;
1116 zebraExplain_recordBytesIncrement (zh->reg->zei,
1117 recordAttr->recordSize);
1119 /* set run-number for this record */
1120 recordAttr->runNumber =
1121 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1123 /* update store data */
1124 xfree (rec->info[recInfo_storeData]);
1125 if (store_data == 1)
1127 rec->size[recInfo_storeData] = recordAttr->recordSize;
1128 rec->info[recInfo_storeData] = (char *)
1129 xmalloc (recordAttr->recordSize);
1131 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1133 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1135 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1136 (long) recordOffset, fname);
1139 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1140 < recordAttr->recordSize)
1142 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1143 recordAttr->recordSize, fname);
1150 rec->info[recInfo_storeData] = NULL;
1151 rec->size[recInfo_storeData] = 0;
1153 /* update database name */
1154 xfree (rec->info[recInfo_databaseName]);
1155 rec->info[recInfo_databaseName] =
1156 rec_strdup (databaseName, &rec->size[recInfo_databaseName]);
1159 recordAttr->recordOffset = recordOffset;
1161 /* commit this record */
1162 rec_put (zh->reg->records, &rec);
1167 int explain_extract (void *handle, Record rec, data1_node *n)
1169 ZebraHandle zh = (ZebraHandle) handle;
1170 struct recExtractCtrl extractCtrl;
1173 if (zebraExplain_curDatabase (zh->reg->zei,
1174 rec->info[recInfo_databaseName]))
1177 if (zebraExplain_newDatabase (zh->reg->zei,
1178 rec->info[recInfo_databaseName], 0))
1182 zh->reg->keys.buf_used = 0;
1183 zh->reg->keys.prevAttrUse = -1;
1184 zh->reg->keys.prevAttrSet = -1;
1185 zh->reg->keys.prevSeqNo = 0;
1186 zh->reg->sortKeys = 0;
1188 extractCtrl.init = extract_init;
1189 extractCtrl.tokenAdd = extract_token_add;
1190 extractCtrl.schemaAdd = extract_schema_add;
1191 extractCtrl.dh = zh->reg->dh;
1192 for (i = 0; i<256; i++)
1193 extractCtrl.seqno[i] = 0;
1194 extractCtrl.zebra_maps = zh->reg->zebra_maps;
1195 extractCtrl.flagShowRecords = 0;
1196 extractCtrl.handle = handle;
1198 grs_extract_tree(&extractCtrl, n);
1200 if (rec->size[recInfo_delKeys])
1202 struct recKeys delkeys;
1203 struct sortKey *sortKeys = 0;
1205 delkeys.buf_used = rec->size[recInfo_delKeys];
1206 delkeys.buf = rec->info[recInfo_delKeys];
1207 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1208 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1210 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys);
1211 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1213 xfree (rec->info[recInfo_delKeys]);
1214 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1215 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1216 zh->reg->keys.buf = NULL;
1217 zh->reg->keys.buf_max = 0;
1221 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1222 int cmd, struct recKeys *reckeys)
1226 unsigned char attrSet = (unsigned char) -1;
1227 unsigned short attrUse = (unsigned short) -1;
1232 ZebraExplainInfo zei = zh->reg->zei;
1234 if (!zh->reg->key_buf)
1236 int mem = 8*1024*1024;
1237 zh->reg->key_buf = (char**) xmalloc (mem);
1238 zh->reg->ptr_top = mem/sizeof(char*);
1240 zh->reg->key_buf_used = 0;
1241 zh->reg->key_file_no = 0;
1243 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1244 while (off < reckeys->buf_used)
1246 const char *src = reckeys->buf + off;
1255 memcpy (&ch, src, sizeof(ch));
1261 memcpy (&attrSet, src, sizeof(attrSet));
1262 src += sizeof(attrSet);
1266 memcpy (&attrUse, src, sizeof(attrUse));
1267 src += sizeof(attrUse);
1270 if (zh->reg->key_buf_used + 1024 >
1271 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1272 extract_flushWriteKeys (zh);
1274 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1275 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1278 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1280 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1283 zh->reg->key_buf_used +=
1284 key_SU_encode (ch,((char*)zh->reg->key_buf) +
1285 zh->reg->key_buf_used);
1288 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1290 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1291 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1294 seqno += ((lead>>2) & 15)-1;
1297 memcpy (&seqno, src, sizeof(seqno));
1298 src += sizeof(seqno);
1302 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used, &key, sizeof(key));
1303 (zh->reg->key_buf_used) += sizeof(key);
1304 off = src - reckeys->buf;
1306 assert (off == reckeys->buf_used);
1309 void extract_flushWriteKeys (ZebraHandle zh)
1312 char out_fname[200];
1314 struct encode_info encode_info;
1315 int ptr_i = zh->reg->ptr_i;
1319 if (!zh->reg->key_buf || ptr_i <= 0)
1322 (zh->reg->key_file_no)++;
1323 logf (LOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1325 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1326 sizeof(char*), key_qsort_compare);
1327 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1329 if (!(outf = fopen (out_fname, "wb")))
1331 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1334 logf (LOG_LOG, "writing section %d", zh->reg->key_file_no);
1335 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1337 encode_key_init (&encode_info);
1338 encode_key_write (cp, &encode_info, outf);
1342 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1343 if (strcmp (cp, prevcp))
1345 encode_key_init (&encode_info);
1346 encode_key_write (cp, &encode_info, outf);
1350 encode_key_write (cp + strlen(cp), &encode_info, outf);
1353 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1354 extract_get_fname_tmp (out_fname, key_file_no);
1356 if (!(outf = fopen (out_fname, "wb")))
1358 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1361 logf (LOG_LOG, "writing section %d", key_file_no);
1363 prevcp = key_buf[ptr_top-i];
1365 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1367 key_y_len = strlen(prevcp)+1;
1369 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1370 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1372 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1373 sizeof(char*), key_y_compare);
1374 cp = key_buf[ptr_top-ptr_i];
1376 encode_key_init (&encode_info);
1377 encode_key_write (cp, &encode_info, outf);
1380 cp = key_buf[ptr_top-ptr_i];
1381 encode_key_write (cp+key_y_len, &encode_info, outf);
1385 prevcp = key_buf[ptr_top-ptr_i];
1390 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1393 logf (LOG_LOG, "finished section %d", zh->reg->key_file_no);
1395 zh->reg->key_buf_used = 0;
1398 void extract_add_index_string (RecWord *p, const char *string,
1402 unsigned char attrSet;
1403 unsigned short attrUse;
1406 int *pseqno = &p->seqno;
1407 ZebraHandle zh = p->extractCtrl->handle;
1408 ZebraExplainInfo zei = zh->reg->zei;
1409 struct recKeys *keys = &zh->reg->keys;
1411 if (keys->buf_used+1024 > keys->buf_max)
1415 b = (char *) xmalloc (keys->buf_max += 128000);
1416 if (keys->buf_used > 0)
1417 memcpy (b, keys->buf, keys->buf_used);
1421 dst = keys->buf + keys->buf_used;
1423 attrSet = p->attrSet;
1424 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1427 keys->prevAttrSet = attrSet;
1428 attrUse = p->attrUse;
1429 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1432 keys->prevAttrUse = attrUse;
1434 diff = 1 + *pseqno - keys->prevSeqNo;
1435 if (diff >= 1 && diff <= 15)
1436 lead |= (diff << 2);
1440 keys->prevSeqNo = *pseqno;
1447 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1450 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1451 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1452 attrSet, attrUse, ch);
1455 memcpy (dst, &ch, sizeof(ch));
1461 memcpy (dst, &attrSet, sizeof(attrSet));
1462 dst += sizeof(attrSet);
1466 memcpy (dst, &attrUse, sizeof(attrUse));
1467 dst += sizeof(attrUse);
1470 *dst++ = p->reg_type;
1471 memcpy (dst, string, length);
1477 memcpy (dst, pseqno, sizeof(*pseqno));
1478 dst += sizeof(*pseqno);
1480 keys->buf_used = dst - keys->buf;
1483 static void extract_add_sort_string (RecWord *p, const char *string,
1487 ZebraHandle zh = p->extractCtrl->handle;
1489 for (sk = zh->reg->sortKeys; sk; sk = sk->next)
1490 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1493 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1494 sk->next = zh->reg->sortKeys;
1495 zh->reg->sortKeys = sk;
1497 sk->string = (char *) xmalloc (length);
1498 sk->length = length;
1499 memcpy (sk->string, string, length);
1501 sk->attrSet = p->attrSet;
1502 sk->attrUse = p->attrUse;
1505 void extract_add_string (RecWord *p, const char *string, int length)
1507 assert (length > 0);
1508 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1509 extract_add_sort_string (p, string, length);
1511 extract_add_index_string (p, string, length);
1514 static void extract_add_incomplete_field (RecWord *p)
1516 const char *b = p->string;
1517 int remain = p->length;
1518 const char **map = 0;
1521 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1525 char buf[IT_MAX_WORD+1];
1529 while (map && *map && **map == *CHR_SPACE)
1531 remain = p->length - (b - p->string);
1533 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1540 while (map && *map && **map != *CHR_SPACE)
1542 const char *cp = *map;
1544 while (i < IT_MAX_WORD && *cp)
1546 remain = p->length - (b - p->string);
1548 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1554 extract_add_string (p, buf, i);
1559 static void extract_add_complete_field (RecWord *p)
1561 const char *b = p->string;
1562 char buf[IT_MAX_WORD+1];
1563 const char **map = 0;
1564 int i = 0, remain = p->length;
1567 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1569 while (remain > 0 && i < IT_MAX_WORD)
1571 while (map && *map && **map == *CHR_SPACE)
1573 remain = p->length - (b - p->string);
1575 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1582 if (i && i < IT_MAX_WORD)
1583 buf[i++] = *CHR_SPACE;
1584 while (map && *map && **map != *CHR_SPACE)
1586 const char *cp = *map;
1588 if (i >= IT_MAX_WORD)
1590 while (i < IT_MAX_WORD && *cp)
1592 remain = p->length - (b - p->string);
1594 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1602 extract_add_string (p, buf, i);
1605 void extract_token_add (RecWord *p)
1608 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1609 p->string, p->length)))
1611 p->string = wrbuf_buf(wrbuf);
1612 p->length = wrbuf_len(wrbuf);
1614 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1615 extract_add_complete_field (p);
1617 extract_add_incomplete_field(p);
1620 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1622 ZebraHandle zh = (ZebraHandle) (p->handle);
1623 zebraExplain_addSchema (zh->reg->zei, oid);
1626 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1627 int cmd, struct sortKey **skp)
1629 struct sortKey *sk = *skp;
1630 SortIdx sortIdx = zh->reg->sortIdx;
1632 sortIdx_sysno (sortIdx, sysno);
1635 struct sortKey *sk_next = sk->next;
1636 sortIdx_type (sortIdx, sk->attrUse);
1637 sortIdx_add (sortIdx, sk->string, sk->length);
1645 void encode_key_init (struct encode_info *i)
1652 char *encode_key_int (int d, char *bp)
1656 else if (d <= 16383)
1658 *bp++ = 64 + (d>>8);
1661 else if (d <= 4194303)
1663 *bp++ = 128 + (d>>16);
1664 *bp++ = (d>>8) & 255;
1669 *bp++ = 192 + (d>>24);
1670 *bp++ = (d>>16) & 255;
1671 *bp++ = (d>>8) & 255;
1677 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1682 while ((*bp++ = *k++))
1684 memcpy (&key, k+1, sizeof(struct it_key));
1685 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1686 if (i->sysno != key.sysno)
1688 i->sysno = key.sysno;
1691 else if (!i->seqno && !key.seqno && i->cmd == *k)
1693 bp = encode_key_int (key.seqno - i->seqno, bp);
1694 i->seqno = key.seqno;
1696 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1698 logf (LOG_FATAL|LOG_ERRNO, "fwrite");