1 /* $Id: extract.c,v 1.120 2002-08-02 19:26:55 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)
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 if (zh->reg->keys.buf_used == 0)
504 /* the extraction process returned no information - the record
505 is probably empty - unless flagShowRecords is in use */
509 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
510 fname, recordOffset);
515 /* perform match if sysno not known and if match criteria is specified */
522 if (rGroup->recordId && *rGroup->recordId)
526 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
530 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
532 memcpy (sysno, rinfo+1, sizeof(*sysno));
536 logf (LOG_WARN, "Bad match criteria");
547 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
548 fname, recordOffset);
549 logf (LOG_WARN, "cannot delete record above (seems new)");
552 if (zh->records_processed < rGroup->fileVerboseLimit)
553 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
554 fname, recordOffset);
555 rec = rec_new (zh->reg->records);
559 recordAttr = rec_init_attr (zh->reg->zei, rec);
563 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
565 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
566 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
568 zh->records_inserted++;
572 /* record already exists */
573 struct recKeys delkeys;
575 rec = rec_get (zh->reg->records, *sysno);
578 recordAttr = rec_init_attr (zh->reg->zei, rec);
580 if (recordAttr->runNumber ==
581 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
583 yaz_log (LOG_LOG, "run number = %d", recordAttr->runNumber);
584 yaz_log (LOG_LOG, "skipped %s %s " PRINTF_OFF_T,
585 rGroup->recordType, fname, recordOffset);
586 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
591 delkeys.buf_used = rec->size[recInfo_delKeys];
592 delkeys.buf = rec->info[recInfo_delKeys];
593 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
594 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
597 /* record going to be deleted */
598 if (!delkeys.buf_used)
600 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
601 rGroup->recordType, fname, recordOffset);
602 logf (LOG_WARN, "cannot delete file above, storeKeys false");
606 if (zh->records_processed < rGroup->fileVerboseLimit)
607 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
608 rGroup->recordType, fname, recordOffset);
609 zh->records_deleted++;
611 dict_delete (zh->reg->matchDict, matchStr);
612 rec_del (zh->reg->records, &rec);
620 /* record going to be updated */
621 if (!delkeys.buf_used)
623 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
624 rGroup->recordType, fname, recordOffset);
625 logf (LOG_WARN, "cannot update file above, storeKeys false");
629 if (zh->records_processed < rGroup->fileVerboseLimit)
630 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
631 rGroup->recordType, fname, recordOffset);
632 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
633 zh->records_updated++;
637 /* update file type */
638 xfree (rec->info[recInfo_fileType]);
639 rec->info[recInfo_fileType] =
640 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
642 /* update filename */
643 xfree (rec->info[recInfo_filename]);
644 rec->info[recInfo_filename] =
645 rec_strdup (fname, &rec->size[recInfo_filename]);
647 /* update delete keys */
648 xfree (rec->info[recInfo_delKeys]);
649 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
652 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
653 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
654 zh->reg->keys.buf = NULL;
655 zh->reg->keys.buf_max = 0;
657 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
658 rec->size[recInfo_delKeys] = reckeys.buf_used;
659 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
660 rec->size[recInfo_delKeys]);
665 rec->info[recInfo_delKeys] = NULL;
666 rec->size[recInfo_delKeys] = 0;
669 /* save file size of original record */
670 zebraExplain_recordBytesIncrement (zh->reg->zei,
671 - recordAttr->recordSize);
672 recordAttr->recordSize = fi->file_moffset - recordOffset;
673 if (!recordAttr->recordSize)
674 recordAttr->recordSize = fi->file_max - recordOffset;
675 zebraExplain_recordBytesIncrement (zh->reg->zei,
676 recordAttr->recordSize);
678 /* set run-number for this record */
679 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
682 /* update store data */
683 xfree (rec->info[recInfo_storeData]);
684 if (rGroup->flagStoreData == 1)
686 rec->size[recInfo_storeData] = recordAttr->recordSize;
687 rec->info[recInfo_storeData] = (char *)
688 xmalloc (recordAttr->recordSize);
689 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
691 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
692 recordOffset, fname);
695 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
696 < recordAttr->recordSize)
698 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
699 recordAttr->recordSize, fname);
705 rec->info[recInfo_storeData] = NULL;
706 rec->size[recInfo_storeData] = 0;
708 /* update database name */
709 xfree (rec->info[recInfo_databaseName]);
710 rec->info[recInfo_databaseName] =
711 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
714 recordAttr->recordOffset = recordOffset;
716 /* commit this record */
717 rec_put (zh->reg->records, &rec);
722 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
723 const struct recordGroup *rGroupP, int deleteFlag)
731 struct recordGroup rGroupM;
732 struct recordGroup *rGroup = &rGroupM;
733 struct file_read_info *fi;
736 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
738 if (!rGroup->groupName || !*rGroup->groupName)
741 sprintf (gprefix, "%s.", rGroup->groupName);
743 logf (LOG_DEBUG, "fileExtract %s", fname);
745 /* determine file extension */
747 for (i = strlen(fname); --i >= 0; )
750 else if (fname[i] == '.')
752 strcpy (ext, fname+i+1);
755 /* determine file type - depending on extension */
756 if (!rGroup->recordType)
758 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
759 if (!(rGroup->recordType = res_get (zh->res, ext_res)))
761 sprintf (ext_res, "%srecordType", gprefix);
762 rGroup->recordType = res_get (zh->res, ext_res);
765 if (!rGroup->recordType)
767 if (zh->records_processed < rGroup->fileVerboseLimit)
768 logf (LOG_LOG, "? %s", fname);
771 if (!*rGroup->recordType)
774 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
777 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
781 /* determine match criteria */
782 if (!rGroup->recordId)
784 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
785 rGroup->recordId = res_get (zh->res, ext_res);
788 /* determine database name */
789 if (!rGroup->databaseName)
791 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
792 if (!(rGroup->databaseName = res_get (zh->res, ext_res)))
794 sprintf (ext_res, "%sdatabase", gprefix);
795 rGroup->databaseName = res_get (zh->res, ext_res);
798 if (!rGroup->databaseName)
799 rGroup->databaseName = "Default";
801 /* determine if explain database */
803 sprintf (ext_res, "%sexplainDatabase", gprefix);
804 rGroup->explainDatabase =
805 atoi (res_get_def (zh->res, ext_res, "0"));
807 /* announce database */
808 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
810 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName,
811 rGroup->explainDatabase))
815 if (rGroup->flagStoreData == -1)
818 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
819 if (!(sval = res_get (zh->res, ext_res)))
821 sprintf (ext_res, "%sstoreData", gprefix);
822 sval = res_get (zh->res, ext_res);
825 rGroup->flagStoreData = atoi (sval);
827 if (rGroup->flagStoreData == -1)
828 rGroup->flagStoreData = 0;
830 if (rGroup->flagStoreKeys == -1)
834 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
835 sval = res_get (zh->res, ext_res);
838 sprintf (ext_res, "%sstoreKeys", gprefix);
839 sval = res_get (zh->res, ext_res);
842 sval = res_get (zh->res, "storeKeys");
844 rGroup->flagStoreKeys = atoi (sval);
846 if (rGroup->flagStoreKeys == -1)
847 rGroup->flagStoreKeys = 0;
849 if (sysno && deleteFlag)
855 if (zh->path_reg && !yaz_is_abspath (fname))
857 strcpy (full_rep, zh->path_reg);
858 strcat (full_rep, "/");
859 strcat (full_rep, fname);
862 strcpy (full_rep, fname);
865 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
867 logf (LOG_WARN|LOG_ERRNO, "open %s", full_rep);
871 fi = file_read_start (fd);
875 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
876 recType, subType, clientData);
877 } while (r && !sysno && fi->file_more);
885 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
886 const char *buf, size_t buf_size,
887 const char *databaseName, int delete_flag,
888 int test_mode, int *sysno,
889 int store_keys, int store_data,
890 const char *match_criteria)
892 RecordAttr *recordAttr;
893 struct recExtractCtrl extractCtrl;
899 const char *fname = "<no file>";
901 long recordOffset = 0;
902 struct zebra_fetch_control fc;
905 fc.record_int_buf = buf;
906 fc.record_int_len = buf_size;
907 fc.record_int_pos = 0;
909 fc.record_offset = 0;
911 extractCtrl.offset = 0;
912 extractCtrl.readf = zebra_record_int_read;
913 extractCtrl.seekf = zebra_record_int_seek;
914 extractCtrl.tellf = zebra_record_int_tell;
915 extractCtrl.endf = zebra_record_int_end;
916 extractCtrl.fh = &fc;
918 /* announce database */
919 if (zebraExplain_curDatabase (zh->reg->zei, databaseName))
921 if (zebraExplain_newDatabase (zh->reg->zei, databaseName, 0))
925 recType_byName (zh->reg->recTypes, recordType, subType,
928 logf (LOG_WARN, "No such record type: %s", recordType);
932 zh->reg->keys.buf_used = 0;
933 zh->reg->keys.prevAttrUse = -1;
934 zh->reg->keys.prevAttrSet = -1;
935 zh->reg->keys.prevSeqNo = 0;
936 zh->reg->sortKeys = 0;
938 extractCtrl.subType = subType;
939 extractCtrl.init = extract_init;
940 extractCtrl.tokenAdd = extract_token_add;
941 extractCtrl.schemaAdd = extract_schema_add;
942 extractCtrl.dh = zh->reg->dh;
943 extractCtrl.handle = zh;
944 extractCtrl.zebra_maps = zh->reg->zebra_maps;
945 extractCtrl.flagShowRecords = 0;
946 for (i = 0; i<256; i++)
948 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
949 extractCtrl.seqno[i] = 1;
951 extractCtrl.seqno[i] = 0;
954 r = (*recType->extract)(clientData, &extractCtrl);
956 if (r == RECCTRL_EXTRACT_EOF)
958 else if (r == RECCTRL_EXTRACT_ERROR)
960 /* error occured during extraction ... */
962 yaz_log (LOG_WARN, "extract error");
964 if (rGroup->flagRw &&
965 zh->records_processed < rGroup->fileVerboseLimit)
967 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
968 fname, (long) recordOffset);
973 if (zh->reg->keys.buf_used == 0)
975 /* the extraction process returned no information - the record
976 is probably empty - unless flagShowRecords is in use */
979 logf (LOG_WARN, "No keys generated for record");
980 logf (LOG_WARN, " The file is probably empty");
990 logf (LOG_LOG, "delete %s %s %ld", recordType,
991 fname, (long) recordOffset);
992 logf (LOG_WARN, "cannot delete record above (seems new)");
995 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
996 (long) recordOffset);
997 rec = rec_new (zh->reg->records);
1001 recordAttr = rec_init_attr (zh->reg->zei, rec);
1005 dict_insert (zh->reg->matchDict, matchStr,
1006 sizeof(*sysno), sysno);
1008 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1009 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1013 /* record already exists */
1014 struct recKeys delkeys;
1016 rec = rec_get (zh->reg->records, *sysno);
1019 recordAttr = rec_init_attr (zh->reg->zei, rec);
1021 if (recordAttr->runNumber ==
1022 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
1024 logf (LOG_LOG, "skipped %s %s %ld", recordType,
1025 fname, (long) recordOffset);
1029 delkeys.buf_used = rec->size[recInfo_delKeys];
1030 delkeys.buf = rec->info[recInfo_delKeys];
1031 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
1032 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
1035 /* record going to be deleted */
1036 if (!delkeys.buf_used)
1038 logf (LOG_LOG, "delete %s %s %ld", recordType,
1039 fname, (long) recordOffset);
1040 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1044 logf (LOG_LOG, "delete %s %s %ld", recordType,
1045 fname, (long) recordOffset);
1048 dict_delete (matchDict, matchStr);
1050 rec_del (zh->reg->records, &rec);
1057 /* record going to be updated */
1058 if (!delkeys.buf_used)
1060 logf (LOG_LOG, "update %s %s %ld", recordType,
1061 fname, (long) recordOffset);
1062 logf (LOG_WARN, "cannot update file above, storeKeys false");
1066 logf (LOG_LOG, "update %s %s %ld", recordType,
1067 fname, (long) recordOffset);
1068 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1072 /* update file type */
1073 xfree (rec->info[recInfo_fileType]);
1074 rec->info[recInfo_fileType] =
1075 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1077 /* update filename */
1078 xfree (rec->info[recInfo_filename]);
1079 rec->info[recInfo_filename] =
1080 rec_strdup (fname, &rec->size[recInfo_filename]);
1082 /* update delete keys */
1083 xfree (rec->info[recInfo_delKeys]);
1084 if (zh->reg->keys.buf_used > 0 && store_keys == 1)
1086 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1087 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1088 zh->reg->keys.buf = NULL;
1089 zh->reg->keys.buf_max = 0;
1093 rec->info[recInfo_delKeys] = NULL;
1094 rec->size[recInfo_delKeys] = 0;
1097 /* save file size of original record */
1098 zebraExplain_recordBytesIncrement (zh->reg->zei,
1099 - recordAttr->recordSize);
1101 recordAttr->recordSize = fi->file_moffset - recordOffset;
1102 if (!recordAttr->recordSize)
1103 recordAttr->recordSize = fi->file_max - recordOffset;
1105 recordAttr->recordSize = buf_size;
1107 zebraExplain_recordBytesIncrement (zh->reg->zei,
1108 recordAttr->recordSize);
1110 /* set run-number for this record */
1111 recordAttr->runNumber =
1112 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1114 /* update store data */
1115 xfree (rec->info[recInfo_storeData]);
1116 if (store_data == 1)
1118 rec->size[recInfo_storeData] = recordAttr->recordSize;
1119 rec->info[recInfo_storeData] = (char *)
1120 xmalloc (recordAttr->recordSize);
1122 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1124 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1126 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1127 (long) recordOffset, fname);
1130 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1131 < recordAttr->recordSize)
1133 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1134 recordAttr->recordSize, fname);
1141 rec->info[recInfo_storeData] = NULL;
1142 rec->size[recInfo_storeData] = 0;
1144 /* update database name */
1145 xfree (rec->info[recInfo_databaseName]);
1146 rec->info[recInfo_databaseName] =
1147 rec_strdup (databaseName, &rec->size[recInfo_databaseName]);
1150 recordAttr->recordOffset = recordOffset;
1152 /* commit this record */
1153 rec_put (zh->reg->records, &rec);
1158 int explain_extract (void *handle, Record rec, data1_node *n)
1160 ZebraHandle zh = (ZebraHandle) handle;
1161 struct recExtractCtrl extractCtrl;
1164 if (zebraExplain_curDatabase (zh->reg->zei,
1165 rec->info[recInfo_databaseName]))
1168 if (zebraExplain_newDatabase (zh->reg->zei,
1169 rec->info[recInfo_databaseName], 0))
1173 zh->reg->keys.buf_used = 0;
1174 zh->reg->keys.prevAttrUse = -1;
1175 zh->reg->keys.prevAttrSet = -1;
1176 zh->reg->keys.prevSeqNo = 0;
1177 zh->reg->sortKeys = 0;
1179 extractCtrl.init = extract_init;
1180 extractCtrl.tokenAdd = extract_token_add;
1181 extractCtrl.schemaAdd = extract_schema_add;
1182 extractCtrl.dh = zh->reg->dh;
1183 for (i = 0; i<256; i++)
1184 extractCtrl.seqno[i] = 0;
1185 extractCtrl.zebra_maps = zh->reg->zebra_maps;
1186 extractCtrl.flagShowRecords = 0;
1187 extractCtrl.handle = handle;
1189 grs_extract_tree(&extractCtrl, n);
1191 if (rec->size[recInfo_delKeys])
1193 struct recKeys delkeys;
1194 struct sortKey *sortKeys = 0;
1196 delkeys.buf_used = rec->size[recInfo_delKeys];
1197 delkeys.buf = rec->info[recInfo_delKeys];
1198 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1199 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1201 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys);
1202 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1204 xfree (rec->info[recInfo_delKeys]);
1205 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1206 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1207 zh->reg->keys.buf = NULL;
1208 zh->reg->keys.buf_max = 0;
1212 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1213 int cmd, struct recKeys *reckeys)
1217 unsigned char attrSet = (unsigned char) -1;
1218 unsigned short attrUse = (unsigned short) -1;
1223 ZebraExplainInfo zei = zh->reg->zei;
1225 if (!zh->reg->key_buf)
1227 int mem = 8*1024*1024;
1228 zh->reg->key_buf = (char**) xmalloc (mem);
1229 zh->reg->ptr_top = mem/sizeof(char*);
1231 zh->reg->key_buf_used = 0;
1232 zh->reg->key_file_no = 0;
1234 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1235 while (off < reckeys->buf_used)
1237 const char *src = reckeys->buf + off;
1246 memcpy (&ch, src, sizeof(ch));
1252 memcpy (&attrSet, src, sizeof(attrSet));
1253 src += sizeof(attrSet);
1257 memcpy (&attrUse, src, sizeof(attrUse));
1258 src += sizeof(attrUse);
1261 if (zh->reg->key_buf_used + 1024 >
1262 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1263 extract_flushWriteKeys (zh);
1265 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1266 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1269 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1271 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1274 zh->reg->key_buf_used +=
1275 key_SU_encode (ch,((char*)zh->reg->key_buf) +
1276 zh->reg->key_buf_used);
1279 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1281 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1282 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1285 seqno += ((lead>>2) & 15)-1;
1288 memcpy (&seqno, src, sizeof(seqno));
1289 src += sizeof(seqno);
1293 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used, &key, sizeof(key));
1294 (zh->reg->key_buf_used) += sizeof(key);
1295 off = src - reckeys->buf;
1297 assert (off == reckeys->buf_used);
1300 void extract_flushWriteKeys (ZebraHandle zh)
1303 char out_fname[200];
1305 struct encode_info encode_info;
1306 int ptr_i = zh->reg->ptr_i;
1310 if (!zh->reg->key_buf || ptr_i <= 0)
1313 (zh->reg->key_file_no)++;
1314 logf (LOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1316 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1317 sizeof(char*), key_qsort_compare);
1318 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1320 if (!(outf = fopen (out_fname, "wb")))
1322 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1325 logf (LOG_LOG, "writing section %d", zh->reg->key_file_no);
1326 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1328 encode_key_init (&encode_info);
1329 encode_key_write (cp, &encode_info, outf);
1333 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1334 if (strcmp (cp, prevcp))
1336 encode_key_init (&encode_info);
1337 encode_key_write (cp, &encode_info, outf);
1341 encode_key_write (cp + strlen(cp), &encode_info, outf);
1344 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1345 extract_get_fname_tmp (out_fname, key_file_no);
1347 if (!(outf = fopen (out_fname, "wb")))
1349 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1352 logf (LOG_LOG, "writing section %d", key_file_no);
1354 prevcp = key_buf[ptr_top-i];
1356 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1358 key_y_len = strlen(prevcp)+1;
1360 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1361 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1363 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1364 sizeof(char*), key_y_compare);
1365 cp = key_buf[ptr_top-ptr_i];
1367 encode_key_init (&encode_info);
1368 encode_key_write (cp, &encode_info, outf);
1371 cp = key_buf[ptr_top-ptr_i];
1372 encode_key_write (cp+key_y_len, &encode_info, outf);
1376 prevcp = key_buf[ptr_top-ptr_i];
1381 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1384 logf (LOG_LOG, "finished section %d", zh->reg->key_file_no);
1386 zh->reg->key_buf_used = 0;
1389 void extract_add_index_string (RecWord *p, const char *string,
1393 unsigned char attrSet;
1394 unsigned short attrUse;
1397 int *pseqno = &p->seqno;
1398 ZebraHandle zh = p->extractCtrl->handle;
1399 ZebraExplainInfo zei = zh->reg->zei;
1400 struct recKeys *keys = &zh->reg->keys;
1402 if (keys->buf_used+1024 > keys->buf_max)
1406 b = (char *) xmalloc (keys->buf_max += 128000);
1407 if (keys->buf_used > 0)
1408 memcpy (b, keys->buf, keys->buf_used);
1412 dst = keys->buf + keys->buf_used;
1414 attrSet = p->attrSet;
1415 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1418 keys->prevAttrSet = attrSet;
1419 attrUse = p->attrUse;
1420 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1423 keys->prevAttrUse = attrUse;
1425 diff = 1 + *pseqno - keys->prevSeqNo;
1426 if (diff >= 1 && diff <= 15)
1427 lead |= (diff << 2);
1431 keys->prevSeqNo = *pseqno;
1438 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1441 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1442 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1443 attrSet, attrUse, ch);
1446 memcpy (dst, &ch, sizeof(ch));
1452 memcpy (dst, &attrSet, sizeof(attrSet));
1453 dst += sizeof(attrSet);
1457 memcpy (dst, &attrUse, sizeof(attrUse));
1458 dst += sizeof(attrUse);
1461 *dst++ = p->reg_type;
1462 memcpy (dst, string, length);
1468 memcpy (dst, pseqno, sizeof(*pseqno));
1469 dst += sizeof(*pseqno);
1471 keys->buf_used = dst - keys->buf;
1474 static void extract_add_sort_string (RecWord *p, const char *string,
1478 ZebraHandle zh = p->extractCtrl->handle;
1480 for (sk = zh->reg->sortKeys; sk; sk = sk->next)
1481 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1484 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1485 sk->next = zh->reg->sortKeys;
1486 zh->reg->sortKeys = sk;
1488 sk->string = (char *) xmalloc (length);
1489 sk->length = length;
1490 memcpy (sk->string, string, length);
1492 sk->attrSet = p->attrSet;
1493 sk->attrUse = p->attrUse;
1496 void extract_add_string (RecWord *p, const char *string, int length)
1498 assert (length > 0);
1499 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1500 extract_add_sort_string (p, string, length);
1502 extract_add_index_string (p, string, length);
1505 static void extract_add_incomplete_field (RecWord *p)
1507 const char *b = p->string;
1508 int remain = p->length;
1509 const char **map = 0;
1512 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1516 char buf[IT_MAX_WORD+1];
1520 while (map && *map && **map == *CHR_SPACE)
1522 remain = p->length - (b - p->string);
1524 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1531 while (map && *map && **map != *CHR_SPACE)
1533 const char *cp = *map;
1535 while (i < IT_MAX_WORD && *cp)
1537 remain = p->length - (b - p->string);
1539 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1545 extract_add_string (p, buf, i);
1550 static void extract_add_complete_field (RecWord *p)
1552 const char *b = p->string;
1553 char buf[IT_MAX_WORD+1];
1554 const char **map = 0;
1555 int i = 0, remain = p->length;
1558 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1560 while (remain > 0 && i < IT_MAX_WORD)
1562 while (map && *map && **map == *CHR_SPACE)
1564 remain = p->length - (b - p->string);
1566 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1573 if (i && i < IT_MAX_WORD)
1574 buf[i++] = *CHR_SPACE;
1575 while (map && *map && **map != *CHR_SPACE)
1577 const char *cp = *map;
1579 if (i >= IT_MAX_WORD)
1581 while (i < IT_MAX_WORD && *cp)
1583 remain = p->length - (b - p->string);
1585 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1593 extract_add_string (p, buf, i);
1596 void extract_token_add (RecWord *p)
1599 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1600 p->string, p->length)))
1602 p->string = wrbuf_buf(wrbuf);
1603 p->length = wrbuf_len(wrbuf);
1605 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1606 extract_add_complete_field (p);
1608 extract_add_incomplete_field(p);
1611 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1613 ZebraHandle zh = (ZebraHandle) (p->handle);
1614 zebraExplain_addSchema (zh->reg->zei, oid);
1617 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1618 int cmd, struct sortKey **skp)
1620 struct sortKey *sk = *skp;
1621 SortIdx sortIdx = zh->reg->sortIdx;
1623 sortIdx_sysno (sortIdx, sysno);
1626 struct sortKey *sk_next = sk->next;
1627 sortIdx_type (sortIdx, sk->attrUse);
1628 sortIdx_add (sortIdx, sk->string, sk->length);
1636 void encode_key_init (struct encode_info *i)
1643 char *encode_key_int (int d, char *bp)
1647 else if (d <= 16383)
1649 *bp++ = 64 + (d>>8);
1652 else if (d <= 4194303)
1654 *bp++ = 128 + (d>>16);
1655 *bp++ = (d>>8) & 255;
1660 *bp++ = 192 + (d>>24);
1661 *bp++ = (d>>16) & 255;
1662 *bp++ = (d>>8) & 255;
1668 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1673 while ((*bp++ = *k++))
1675 memcpy (&key, k+1, sizeof(struct it_key));
1676 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1677 if (i->sysno != key.sysno)
1679 i->sysno = key.sysno;
1682 else if (!i->seqno && !key.seqno && i->cmd == *k)
1684 bp = encode_key_int (key.seqno - i->seqno, bp);
1685 i->seqno = key.seqno;
1687 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1689 logf (LOG_FATAL|LOG_ERRNO, "fwrite");