1 /* $Id: extract.c,v 1.122 2002-08-29 08:47:08 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 #define USE_SHELLSORT 0
46 static void shellsort(void *ar, int r, size_t s,
47 int (*cmp)(const void *a, const void *b))
52 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
53 13776, 4592, 1968, 861, 336,
54 112, 48, 21, 7, 3, 1 };
55 for ( k = 0; k < 16; k++)
56 for (h = incs[k], i = h; i < r; i++)
60 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
62 memcpy (a + s*j, a + s*(j-h), s);
70 static void logRecord (ZebraHandle zh)
72 ++zh->records_processed;
73 if (!(zh->records_processed % 1000))
75 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
76 zh->records_processed, zh->records_inserted, zh->records_updated,
81 static void extract_init (struct recExtractCtrl *p, RecWord *w)
83 w->zebra_maps = p->zebra_maps;
85 w->attrSet = VAL_BIB1;
91 static const char **searchRecordKey (ZebraHandle zh,
92 struct recKeys *reckeys,
93 int attrSetS, int attrUseS)
95 static const char *ws[32];
107 for (i = 0; i<32; i++)
111 chS = zebraExplain_lookupSU (zh->reg->zei, attrSetS, attrUseS);
115 while (off < reckeys->buf_used)
118 const char *src = reckeys->buf + off;
126 memcpy (&ch, src, sizeof(ch));
132 memcpy (&attrSet, src, sizeof(attrSet));
133 src += sizeof(attrSet);
137 memcpy (&attrUse, src, sizeof(attrUse));
138 src += sizeof(attrUse);
145 seqno += ((lead>>2) & 15)-1;
148 memcpy (&seqno, src, sizeof(seqno));
149 src += sizeof(seqno);
155 attrUseS == attrUse && attrSetS == attrSet
164 woff = seqno - startSeq;
165 if (woff >= 0 && woff < 31)
169 off = src - reckeys->buf;
171 assert (off == reckeys->buf_used);
175 struct file_read_info {
176 off_t file_max; /* maximum offset so far */
177 off_t file_offset; /* current offset */
178 off_t file_moffset; /* offset of rec/rec boundary */
185 static struct file_read_info *file_read_start (int fd)
187 struct file_read_info *fi = (struct file_read_info *)
188 xmalloc (sizeof(*fi));
192 fi->file_moffset = 0;
198 static void file_read_stop (struct file_read_info *fi)
203 static off_t file_seek (void *handle, off_t offset)
205 struct file_read_info *p = (struct file_read_info *) handle;
206 p->file_offset = offset;
209 return lseek (p->fd, offset, SEEK_SET);
212 static off_t file_tell (void *handle)
214 struct file_read_info *p = (struct file_read_info *) handle;
215 return p->file_offset;
218 static int file_read (void *handle, char *buf, size_t count)
220 struct file_read_info *p = (struct file_read_info *) handle;
226 if (r > p->sdrmax - p->file_offset)
227 r = p->sdrmax - p->file_offset;
229 memcpy (buf, p->sdrbuf + p->file_offset, r);
232 r = read (fd, buf, count);
236 if (p->file_offset > p->file_max)
237 p->file_max = p->file_offset;
242 static void file_begin (void *handle)
244 struct file_read_info *p = (struct file_read_info *) handle;
246 p->file_offset = p->file_moffset;
247 if (!p->sdrbuf && p->file_moffset)
248 lseek (p->fd, p->file_moffset, SEEK_SET);
252 static void file_end (void *handle, off_t offset)
254 struct file_read_info *p = (struct file_read_info *) handle;
256 assert (p->file_more == 0);
258 p->file_moffset = offset;
261 static char *fileMatchStr (ZebraHandle zh,
262 struct recKeys *reckeys, struct recordGroup *rGroup,
263 const char *fname, const char *spec)
265 static char dstBuf[2048]; /* static here ??? */
267 const char *s = spec;
268 static const char **w;
272 while (*s == ' ' || *s == '\t')
278 char attset_str[64], attname_str[64];
279 data1_attset *attset;
282 int attSet = 1, attUse = 1;
286 for (i = 0; *s && *s != ',' && *s != ')'; s++)
288 attset_str[i++] = *s;
289 attset_str[i] = '\0';
294 for (i = 0; *s && *s != ')'; s++)
296 attname_str[i++] = *s;
297 attname_str[i] = '\0';
300 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
303 attSet = attset->reference;
304 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
308 attUse = atoi (attname_str);
310 w = searchRecordKey (zh, reckeys, attSet, attUse);
315 for (i = 0; i<32; i++)
320 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
321 spec, rGroup->groupName ? rGroup->groupName : "none");
326 for (i = 0; i<32; i++)
327 if (matchFlag[i] && w[i])
339 logf (LOG_WARN, "Record didn't contain match"
340 " fields in (%s,%s)", attset_str, attname_str);
348 const char *spec_src = NULL;
349 const char *s1 = ++s;
350 while (*s1 && *s1 != ' ' && *s1 != '\t')
356 memcpy (special, s, spec_len);
357 special[spec_len] = '\0';
360 if (!strcmp (special, "group"))
361 spec_src = rGroup->groupName;
362 else if (!strcmp (special, "database"))
363 spec_src = rGroup->databaseName;
364 else if (!strcmp (special, "filename"))
366 else if (!strcmp (special, "type"))
367 spec_src = rGroup->recordType;
372 strcpy (dst, spec_src);
373 dst += strlen (spec_src);
376 else if (*s == '\"' || *s == '\'')
378 int stopMarker = *s++;
382 while (*s && *s != stopMarker)
385 tmpString[i++] = *s++;
390 strcpy (dst, tmpString);
391 dst += strlen (tmpString);
395 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
396 spec, rGroup->groupName ? rGroup->groupName : "none");
403 logf (LOG_WARN, "No match criteria for record %s in group %s",
404 fname, rGroup->groupName ? rGroup->groupName : "none");
411 struct recordLogInfo {
414 struct recordGroup *rGroup;
417 static void recordLogPreamble (int level, const char *msg, void *info)
419 struct recordLogInfo *p = (struct recordLogInfo *) info;
420 FILE *outf = yaz_log_file ();
424 fprintf (outf, "File %s, offset %d, type %s\n",
425 p->fname, p->recordOffset, p->rGroup->recordType);
426 log_event_start (NULL, NULL);
430 static int recordExtract (ZebraHandle zh,
431 SYSNO *sysno, const char *fname,
432 struct recordGroup *rGroup, int deleteFlag,
433 struct file_read_info *fi,
434 RecType recType, char *subType, void *clientData)
436 RecordAttr *recordAttr;
441 struct recordLogInfo logInfo;
442 off_t recordOffset = 0;
446 struct recExtractCtrl extractCtrl;
448 /* we are going to read from a file, so prepare the extraction */
451 zh->reg->keys.buf_used = 0;
452 zh->reg->keys.prevAttrUse = -1;
453 zh->reg->keys.prevAttrSet = -1;
454 zh->reg->keys.prevSeqNo = 0;
455 zh->reg->sortKeys = 0;
457 recordOffset = fi->file_moffset;
458 extractCtrl.offset = fi->file_moffset;
459 extractCtrl.readf = file_read;
460 extractCtrl.seekf = file_seek;
461 extractCtrl.tellf = file_tell;
462 extractCtrl.endf = file_end;
464 extractCtrl.subType = subType;
465 extractCtrl.init = extract_init;
466 extractCtrl.tokenAdd = extract_token_add;
467 extractCtrl.schemaAdd = extract_schema_add;
468 extractCtrl.dh = zh->reg->dh;
469 extractCtrl.handle = zh;
470 for (i = 0; i<256; i++)
472 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
473 extractCtrl.seqno[i] = 1;
475 extractCtrl.seqno[i] = 0;
477 extractCtrl.zebra_maps = zh->reg->zebra_maps;
478 extractCtrl.flagShowRecords = !rGroup->flagRw;
481 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
483 logInfo.fname = fname;
484 logInfo.recordOffset = recordOffset;
485 logInfo.rGroup = rGroup;
486 log_event_start (recordLogPreamble, &logInfo);
488 r = (*recType->extract)(clientData, &extractCtrl);
490 log_event_start (NULL, NULL);
492 if (r == RECCTRL_EXTRACT_EOF)
494 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
496 /* error occured during extraction ... */
497 if (rGroup->flagRw &&
498 zh->records_processed < rGroup->fileVerboseLimit)
500 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
501 fname, recordOffset);
505 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
507 /* error occured during extraction ... */
508 if (rGroup->flagRw &&
509 zh->records_processed < rGroup->fileVerboseLimit)
511 logf (LOG_WARN, "no filter for %s %s "
512 PRINTF_OFF_T, rGroup->recordType,
513 fname, recordOffset);
517 if (zh->reg->keys.buf_used == 0)
519 /* the extraction process returned no information - the record
520 is probably empty - unless flagShowRecords is in use */
524 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
525 fname, recordOffset);
530 /* perform match if sysno not known and if match criteria is specified */
537 if (rGroup->recordId && *rGroup->recordId)
541 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
545 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
547 memcpy (sysno, rinfo+1, sizeof(*sysno));
551 logf (LOG_WARN, "Bad match criteria");
562 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
563 fname, recordOffset);
564 logf (LOG_WARN, "cannot delete record above (seems new)");
567 if (zh->records_processed < rGroup->fileVerboseLimit)
568 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
569 fname, recordOffset);
570 rec = rec_new (zh->reg->records);
574 recordAttr = rec_init_attr (zh->reg->zei, rec);
578 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
580 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
581 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
583 zh->records_inserted++;
587 /* record already exists */
588 struct recKeys delkeys;
590 rec = rec_get (zh->reg->records, *sysno);
593 recordAttr = rec_init_attr (zh->reg->zei, rec);
595 if (recordAttr->runNumber ==
596 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
598 yaz_log (LOG_LOG, "run number = %d", recordAttr->runNumber);
599 yaz_log (LOG_LOG, "skipped %s %s " PRINTF_OFF_T,
600 rGroup->recordType, fname, recordOffset);
601 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
606 delkeys.buf_used = rec->size[recInfo_delKeys];
607 delkeys.buf = rec->info[recInfo_delKeys];
608 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
609 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
612 /* record going to be deleted */
613 if (!delkeys.buf_used)
615 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
616 rGroup->recordType, fname, recordOffset);
617 logf (LOG_WARN, "cannot delete file above, storeKeys false");
621 if (zh->records_processed < rGroup->fileVerboseLimit)
622 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
623 rGroup->recordType, fname, recordOffset);
624 zh->records_deleted++;
626 dict_delete (zh->reg->matchDict, matchStr);
627 rec_del (zh->reg->records, &rec);
635 /* record going to be updated */
636 if (!delkeys.buf_used)
638 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
639 rGroup->recordType, fname, recordOffset);
640 logf (LOG_WARN, "cannot update file above, storeKeys false");
644 if (zh->records_processed < rGroup->fileVerboseLimit)
645 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
646 rGroup->recordType, fname, recordOffset);
647 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
648 zh->records_updated++;
652 /* update file type */
653 xfree (rec->info[recInfo_fileType]);
654 rec->info[recInfo_fileType] =
655 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
657 /* update filename */
658 xfree (rec->info[recInfo_filename]);
659 rec->info[recInfo_filename] =
660 rec_strdup (fname, &rec->size[recInfo_filename]);
662 /* update delete keys */
663 xfree (rec->info[recInfo_delKeys]);
664 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
667 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
668 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
669 zh->reg->keys.buf = NULL;
670 zh->reg->keys.buf_max = 0;
672 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
673 rec->size[recInfo_delKeys] = reckeys.buf_used;
674 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
675 rec->size[recInfo_delKeys]);
680 rec->info[recInfo_delKeys] = NULL;
681 rec->size[recInfo_delKeys] = 0;
684 /* save file size of original record */
685 zebraExplain_recordBytesIncrement (zh->reg->zei,
686 - recordAttr->recordSize);
687 recordAttr->recordSize = fi->file_moffset - recordOffset;
688 if (!recordAttr->recordSize)
689 recordAttr->recordSize = fi->file_max - recordOffset;
690 zebraExplain_recordBytesIncrement (zh->reg->zei,
691 recordAttr->recordSize);
693 /* set run-number for this record */
694 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
697 /* update store data */
698 xfree (rec->info[recInfo_storeData]);
699 if (rGroup->flagStoreData == 1)
701 rec->size[recInfo_storeData] = recordAttr->recordSize;
702 rec->info[recInfo_storeData] = (char *)
703 xmalloc (recordAttr->recordSize);
704 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
706 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
707 recordOffset, fname);
710 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
711 < recordAttr->recordSize)
713 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
714 recordAttr->recordSize, fname);
720 rec->info[recInfo_storeData] = NULL;
721 rec->size[recInfo_storeData] = 0;
723 /* update database name */
724 xfree (rec->info[recInfo_databaseName]);
725 rec->info[recInfo_databaseName] =
726 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
729 recordAttr->recordOffset = recordOffset;
731 /* commit this record */
732 rec_put (zh->reg->records, &rec);
737 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
738 const struct recordGroup *rGroupP, int deleteFlag)
746 struct recordGroup rGroupM;
747 struct recordGroup *rGroup = &rGroupM;
748 struct file_read_info *fi;
751 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
753 if (!rGroup->groupName || !*rGroup->groupName)
756 sprintf (gprefix, "%s.", rGroup->groupName);
758 logf (LOG_DEBUG, "fileExtract %s", fname);
760 /* determine file extension */
762 for (i = strlen(fname); --i >= 0; )
765 else if (fname[i] == '.')
767 strcpy (ext, fname+i+1);
770 /* determine file type - depending on extension */
771 if (!rGroup->recordType)
773 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
774 if (!(rGroup->recordType = res_get (zh->res, ext_res)))
776 sprintf (ext_res, "%srecordType", gprefix);
777 rGroup->recordType = res_get (zh->res, ext_res);
780 if (!rGroup->recordType)
782 if (zh->records_processed < rGroup->fileVerboseLimit)
783 logf (LOG_LOG, "? %s", fname);
786 if (!*rGroup->recordType)
789 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
792 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
796 /* determine match criteria */
797 if (!rGroup->recordId)
799 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
800 rGroup->recordId = res_get (zh->res, ext_res);
803 /* determine database name */
804 if (!rGroup->databaseName)
806 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
807 if (!(rGroup->databaseName = res_get (zh->res, ext_res)))
809 sprintf (ext_res, "%sdatabase", gprefix);
810 rGroup->databaseName = res_get (zh->res, ext_res);
813 if (!rGroup->databaseName)
814 rGroup->databaseName = "Default";
816 /* determine if explain database */
818 sprintf (ext_res, "%sexplainDatabase", gprefix);
819 rGroup->explainDatabase =
820 atoi (res_get_def (zh->res, ext_res, "0"));
822 /* announce database */
823 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
825 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName,
826 rGroup->explainDatabase))
830 if (rGroup->flagStoreData == -1)
833 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
834 if (!(sval = res_get (zh->res, ext_res)))
836 sprintf (ext_res, "%sstoreData", gprefix);
837 sval = res_get (zh->res, ext_res);
840 rGroup->flagStoreData = atoi (sval);
842 if (rGroup->flagStoreData == -1)
843 rGroup->flagStoreData = 0;
845 if (rGroup->flagStoreKeys == -1)
849 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
850 sval = res_get (zh->res, ext_res);
853 sprintf (ext_res, "%sstoreKeys", gprefix);
854 sval = res_get (zh->res, ext_res);
857 sval = res_get (zh->res, "storeKeys");
859 rGroup->flagStoreKeys = atoi (sval);
861 if (rGroup->flagStoreKeys == -1)
862 rGroup->flagStoreKeys = 0;
864 if (sysno && deleteFlag)
870 if (zh->path_reg && !yaz_is_abspath (fname))
872 strcpy (full_rep, zh->path_reg);
873 strcat (full_rep, "/");
874 strcat (full_rep, fname);
877 strcpy (full_rep, fname);
880 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
882 logf (LOG_WARN|LOG_ERRNO, "open %s", full_rep);
886 fi = file_read_start (fd);
890 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
891 recType, subType, clientData);
892 } while (r && !sysno && fi->file_more);
900 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
901 const char *buf, size_t buf_size,
902 const char *databaseName, int delete_flag,
903 int test_mode, int *sysno,
904 int store_keys, int store_data,
905 const char *match_criteria)
907 RecordAttr *recordAttr;
908 struct recExtractCtrl extractCtrl;
914 const char *fname = "<no file>";
916 long recordOffset = 0;
917 struct zebra_fetch_control fc;
920 fc.record_int_buf = buf;
921 fc.record_int_len = buf_size;
922 fc.record_int_pos = 0;
924 fc.record_offset = 0;
926 extractCtrl.offset = 0;
927 extractCtrl.readf = zebra_record_int_read;
928 extractCtrl.seekf = zebra_record_int_seek;
929 extractCtrl.tellf = zebra_record_int_tell;
930 extractCtrl.endf = zebra_record_int_end;
931 extractCtrl.fh = &fc;
933 /* announce database */
934 if (zebraExplain_curDatabase (zh->reg->zei, databaseName))
936 if (zebraExplain_newDatabase (zh->reg->zei, databaseName, 0))
940 recType_byName (zh->reg->recTypes, recordType, subType,
943 logf (LOG_WARN, "No such record type: %s", recordType);
947 zh->reg->keys.buf_used = 0;
948 zh->reg->keys.prevAttrUse = -1;
949 zh->reg->keys.prevAttrSet = -1;
950 zh->reg->keys.prevSeqNo = 0;
951 zh->reg->sortKeys = 0;
953 extractCtrl.subType = subType;
954 extractCtrl.init = extract_init;
955 extractCtrl.tokenAdd = extract_token_add;
956 extractCtrl.schemaAdd = extract_schema_add;
957 extractCtrl.dh = zh->reg->dh;
958 extractCtrl.handle = zh;
959 extractCtrl.zebra_maps = zh->reg->zebra_maps;
960 extractCtrl.flagShowRecords = 0;
961 for (i = 0; i<256; i++)
963 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
964 extractCtrl.seqno[i] = 1;
966 extractCtrl.seqno[i] = 0;
969 r = (*recType->extract)(clientData, &extractCtrl);
971 if (r == RECCTRL_EXTRACT_EOF)
973 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
975 /* error occured during extraction ... */
976 yaz_log (LOG_WARN, "extract error: generic");
979 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
981 /* error occured during extraction ... */
982 yaz_log (LOG_WARN, "extract error: no such filter");
985 if (zh->reg->keys.buf_used == 0)
987 /* the extraction process returned no information - the record
988 is probably empty - unless flagShowRecords is in use */
991 logf (LOG_WARN, "No keys generated for record");
992 logf (LOG_WARN, " The file is probably empty");
1002 logf (LOG_LOG, "delete %s %s %ld", recordType,
1003 fname, (long) recordOffset);
1004 logf (LOG_WARN, "cannot delete record above (seems new)");
1007 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
1008 (long) recordOffset);
1009 rec = rec_new (zh->reg->records);
1011 *sysno = rec->sysno;
1013 recordAttr = rec_init_attr (zh->reg->zei, rec);
1017 dict_insert (zh->reg->matchDict, matchStr,
1018 sizeof(*sysno), sysno);
1020 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1021 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1025 /* record already exists */
1026 struct recKeys delkeys;
1028 rec = rec_get (zh->reg->records, *sysno);
1031 recordAttr = rec_init_attr (zh->reg->zei, rec);
1033 if (recordAttr->runNumber ==
1034 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
1036 logf (LOG_LOG, "skipped %s %s %ld", recordType,
1037 fname, (long) recordOffset);
1041 delkeys.buf_used = rec->size[recInfo_delKeys];
1042 delkeys.buf = rec->info[recInfo_delKeys];
1043 extract_flushSortKeys (zh, *sysno, 0, &zh->reg->sortKeys);
1044 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
1047 /* record going to be deleted */
1048 if (!delkeys.buf_used)
1050 logf (LOG_LOG, "delete %s %s %ld", recordType,
1051 fname, (long) recordOffset);
1052 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1056 logf (LOG_LOG, "delete %s %s %ld", recordType,
1057 fname, (long) recordOffset);
1060 dict_delete (matchDict, matchStr);
1062 rec_del (zh->reg->records, &rec);
1069 /* record going to be updated */
1070 if (!delkeys.buf_used)
1072 logf (LOG_LOG, "update %s %s %ld", recordType,
1073 fname, (long) recordOffset);
1074 logf (LOG_WARN, "cannot update file above, storeKeys false");
1078 logf (LOG_LOG, "update %s %s %ld", recordType,
1079 fname, (long) recordOffset);
1080 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1084 /* update file type */
1085 xfree (rec->info[recInfo_fileType]);
1086 rec->info[recInfo_fileType] =
1087 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1089 /* update filename */
1090 xfree (rec->info[recInfo_filename]);
1091 rec->info[recInfo_filename] =
1092 rec_strdup (fname, &rec->size[recInfo_filename]);
1094 /* update delete keys */
1095 xfree (rec->info[recInfo_delKeys]);
1096 if (zh->reg->keys.buf_used > 0 && store_keys == 1)
1098 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1099 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1100 zh->reg->keys.buf = NULL;
1101 zh->reg->keys.buf_max = 0;
1105 rec->info[recInfo_delKeys] = NULL;
1106 rec->size[recInfo_delKeys] = 0;
1109 /* save file size of original record */
1110 zebraExplain_recordBytesIncrement (zh->reg->zei,
1111 - recordAttr->recordSize);
1113 recordAttr->recordSize = fi->file_moffset - recordOffset;
1114 if (!recordAttr->recordSize)
1115 recordAttr->recordSize = fi->file_max - recordOffset;
1117 recordAttr->recordSize = buf_size;
1119 zebraExplain_recordBytesIncrement (zh->reg->zei,
1120 recordAttr->recordSize);
1122 /* set run-number for this record */
1123 recordAttr->runNumber =
1124 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1126 /* update store data */
1127 xfree (rec->info[recInfo_storeData]);
1128 if (store_data == 1)
1130 rec->size[recInfo_storeData] = recordAttr->recordSize;
1131 rec->info[recInfo_storeData] = (char *)
1132 xmalloc (recordAttr->recordSize);
1134 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1136 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1138 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1139 (long) recordOffset, fname);
1142 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1143 < recordAttr->recordSize)
1145 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1146 recordAttr->recordSize, fname);
1153 rec->info[recInfo_storeData] = NULL;
1154 rec->size[recInfo_storeData] = 0;
1156 /* update database name */
1157 xfree (rec->info[recInfo_databaseName]);
1158 rec->info[recInfo_databaseName] =
1159 rec_strdup (databaseName, &rec->size[recInfo_databaseName]);
1162 recordAttr->recordOffset = recordOffset;
1164 /* commit this record */
1165 rec_put (zh->reg->records, &rec);
1170 int explain_extract (void *handle, Record rec, data1_node *n)
1172 ZebraHandle zh = (ZebraHandle) handle;
1173 struct recExtractCtrl extractCtrl;
1176 if (zebraExplain_curDatabase (zh->reg->zei,
1177 rec->info[recInfo_databaseName]))
1180 if (zebraExplain_newDatabase (zh->reg->zei,
1181 rec->info[recInfo_databaseName], 0))
1185 zh->reg->keys.buf_used = 0;
1186 zh->reg->keys.prevAttrUse = -1;
1187 zh->reg->keys.prevAttrSet = -1;
1188 zh->reg->keys.prevSeqNo = 0;
1189 zh->reg->sortKeys = 0;
1191 extractCtrl.init = extract_init;
1192 extractCtrl.tokenAdd = extract_token_add;
1193 extractCtrl.schemaAdd = extract_schema_add;
1194 extractCtrl.dh = zh->reg->dh;
1195 for (i = 0; i<256; i++)
1196 extractCtrl.seqno[i] = 0;
1197 extractCtrl.zebra_maps = zh->reg->zebra_maps;
1198 extractCtrl.flagShowRecords = 0;
1199 extractCtrl.handle = handle;
1201 grs_extract_tree(&extractCtrl, n);
1203 if (rec->size[recInfo_delKeys])
1205 struct recKeys delkeys;
1206 struct sortKey *sortKeys = 0;
1208 delkeys.buf_used = rec->size[recInfo_delKeys];
1209 delkeys.buf = rec->info[recInfo_delKeys];
1210 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1211 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1213 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys);
1214 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1216 xfree (rec->info[recInfo_delKeys]);
1217 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1218 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1219 zh->reg->keys.buf = NULL;
1220 zh->reg->keys.buf_max = 0;
1224 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1225 int cmd, struct recKeys *reckeys)
1229 unsigned char attrSet = (unsigned char) -1;
1230 unsigned short attrUse = (unsigned short) -1;
1235 ZebraExplainInfo zei = zh->reg->zei;
1237 if (!zh->reg->key_buf)
1239 int mem = 8*1024*1024;
1240 zh->reg->key_buf = (char**) xmalloc (mem);
1241 zh->reg->ptr_top = mem/sizeof(char*);
1243 zh->reg->key_buf_used = 0;
1244 zh->reg->key_file_no = 0;
1246 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1247 while (off < reckeys->buf_used)
1249 const char *src = reckeys->buf + off;
1258 memcpy (&ch, src, sizeof(ch));
1264 memcpy (&attrSet, src, sizeof(attrSet));
1265 src += sizeof(attrSet);
1269 memcpy (&attrUse, src, sizeof(attrUse));
1270 src += sizeof(attrUse);
1273 if (zh->reg->key_buf_used + 1024 >
1274 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1275 extract_flushWriteKeys (zh);
1277 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1278 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1281 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1283 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1286 zh->reg->key_buf_used +=
1287 key_SU_encode (ch,((char*)zh->reg->key_buf) +
1288 zh->reg->key_buf_used);
1291 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1293 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1294 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1297 seqno += ((lead>>2) & 15)-1;
1300 memcpy (&seqno, src, sizeof(seqno));
1301 src += sizeof(seqno);
1305 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used, &key, sizeof(key));
1306 (zh->reg->key_buf_used) += sizeof(key);
1307 off = src - reckeys->buf;
1309 assert (off == reckeys->buf_used);
1312 void extract_flushWriteKeys (ZebraHandle zh)
1315 char out_fname[200];
1317 struct encode_info encode_info;
1318 int ptr_i = zh->reg->ptr_i;
1322 if (!zh->reg->key_buf || ptr_i <= 0)
1325 (zh->reg->key_file_no)++;
1326 logf (LOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1328 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1329 sizeof(char*), key_qsort_compare);
1330 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1332 if (!(outf = fopen (out_fname, "wb")))
1334 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1337 logf (LOG_LOG, "writing section %d", zh->reg->key_file_no);
1338 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1340 encode_key_init (&encode_info);
1341 encode_key_write (cp, &encode_info, outf);
1345 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1346 if (strcmp (cp, prevcp))
1348 encode_key_init (&encode_info);
1349 encode_key_write (cp, &encode_info, outf);
1353 encode_key_write (cp + strlen(cp), &encode_info, outf);
1356 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1357 extract_get_fname_tmp (out_fname, key_file_no);
1359 if (!(outf = fopen (out_fname, "wb")))
1361 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1364 logf (LOG_LOG, "writing section %d", key_file_no);
1366 prevcp = key_buf[ptr_top-i];
1368 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1370 key_y_len = strlen(prevcp)+1;
1372 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1373 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1375 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1376 sizeof(char*), key_y_compare);
1377 cp = key_buf[ptr_top-ptr_i];
1379 encode_key_init (&encode_info);
1380 encode_key_write (cp, &encode_info, outf);
1383 cp = key_buf[ptr_top-ptr_i];
1384 encode_key_write (cp+key_y_len, &encode_info, outf);
1388 prevcp = key_buf[ptr_top-ptr_i];
1393 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1396 logf (LOG_LOG, "finished section %d", zh->reg->key_file_no);
1398 zh->reg->key_buf_used = 0;
1401 void extract_add_index_string (RecWord *p, const char *string,
1405 unsigned char attrSet;
1406 unsigned short attrUse;
1409 int *pseqno = &p->seqno;
1410 ZebraHandle zh = p->extractCtrl->handle;
1411 ZebraExplainInfo zei = zh->reg->zei;
1412 struct recKeys *keys = &zh->reg->keys;
1414 if (keys->buf_used+1024 > keys->buf_max)
1418 b = (char *) xmalloc (keys->buf_max += 128000);
1419 if (keys->buf_used > 0)
1420 memcpy (b, keys->buf, keys->buf_used);
1424 dst = keys->buf + keys->buf_used;
1426 attrSet = p->attrSet;
1427 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1430 keys->prevAttrSet = attrSet;
1431 attrUse = p->attrUse;
1432 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1435 keys->prevAttrUse = attrUse;
1437 diff = 1 + *pseqno - keys->prevSeqNo;
1438 if (diff >= 1 && diff <= 15)
1439 lead |= (diff << 2);
1443 keys->prevSeqNo = *pseqno;
1450 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1453 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1454 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1455 attrSet, attrUse, ch);
1458 memcpy (dst, &ch, sizeof(ch));
1464 memcpy (dst, &attrSet, sizeof(attrSet));
1465 dst += sizeof(attrSet);
1469 memcpy (dst, &attrUse, sizeof(attrUse));
1470 dst += sizeof(attrUse);
1473 *dst++ = p->reg_type;
1474 memcpy (dst, string, length);
1480 memcpy (dst, pseqno, sizeof(*pseqno));
1481 dst += sizeof(*pseqno);
1483 keys->buf_used = dst - keys->buf;
1486 static void extract_add_sort_string (RecWord *p, const char *string,
1490 ZebraHandle zh = p->extractCtrl->handle;
1492 for (sk = zh->reg->sortKeys; sk; sk = sk->next)
1493 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1496 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1497 sk->next = zh->reg->sortKeys;
1498 zh->reg->sortKeys = sk;
1500 sk->string = (char *) xmalloc (length);
1501 sk->length = length;
1502 memcpy (sk->string, string, length);
1504 sk->attrSet = p->attrSet;
1505 sk->attrUse = p->attrUse;
1508 void extract_add_string (RecWord *p, const char *string, int length)
1510 assert (length > 0);
1511 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1512 extract_add_sort_string (p, string, length);
1514 extract_add_index_string (p, string, length);
1517 static void extract_add_incomplete_field (RecWord *p)
1519 const char *b = p->string;
1520 int remain = p->length;
1521 const char **map = 0;
1524 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1528 char buf[IT_MAX_WORD+1];
1532 while (map && *map && **map == *CHR_SPACE)
1534 remain = p->length - (b - p->string);
1536 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1543 while (map && *map && **map != *CHR_SPACE)
1545 const char *cp = *map;
1547 while (i < IT_MAX_WORD && *cp)
1549 remain = p->length - (b - p->string);
1551 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1557 extract_add_string (p, buf, i);
1562 static void extract_add_complete_field (RecWord *p)
1564 const char *b = p->string;
1565 char buf[IT_MAX_WORD+1];
1566 const char **map = 0;
1567 int i = 0, remain = p->length;
1570 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1572 while (remain > 0 && i < IT_MAX_WORD)
1574 while (map && *map && **map == *CHR_SPACE)
1576 remain = p->length - (b - p->string);
1578 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1585 if (i && i < IT_MAX_WORD)
1586 buf[i++] = *CHR_SPACE;
1587 while (map && *map && **map != *CHR_SPACE)
1589 const char *cp = *map;
1591 if (i >= IT_MAX_WORD)
1593 while (i < IT_MAX_WORD && *cp)
1595 remain = p->length - (b - p->string);
1597 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1605 extract_add_string (p, buf, i);
1608 void extract_token_add (RecWord *p)
1611 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1612 p->string, p->length)))
1614 p->string = wrbuf_buf(wrbuf);
1615 p->length = wrbuf_len(wrbuf);
1617 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1618 extract_add_complete_field (p);
1620 extract_add_incomplete_field(p);
1623 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1625 ZebraHandle zh = (ZebraHandle) (p->handle);
1626 zebraExplain_addSchema (zh->reg->zei, oid);
1629 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1630 int cmd, struct sortKey **skp)
1632 struct sortKey *sk = *skp;
1633 SortIdx sortIdx = zh->reg->sortIdx;
1635 sortIdx_sysno (sortIdx, sysno);
1638 struct sortKey *sk_next = sk->next;
1639 sortIdx_type (sortIdx, sk->attrUse);
1640 sortIdx_add (sortIdx, sk->string, sk->length);
1648 void encode_key_init (struct encode_info *i)
1655 char *encode_key_int (int d, char *bp)
1659 else if (d <= 16383)
1661 *bp++ = 64 + (d>>8);
1664 else if (d <= 4194303)
1666 *bp++ = 128 + (d>>16);
1667 *bp++ = (d>>8) & 255;
1672 *bp++ = 192 + (d>>24);
1673 *bp++ = (d>>16) & 255;
1674 *bp++ = (d>>8) & 255;
1680 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1685 while ((*bp++ = *k++))
1687 memcpy (&key, k+1, sizeof(struct it_key));
1688 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1689 if (i->sysno != key.sysno)
1691 i->sysno = key.sysno;
1694 else if (!i->seqno && !key.seqno && i->cmd == *k)
1696 bp = encode_key_int (key.seqno - i->seqno, bp);
1697 i->seqno = key.seqno;
1699 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1701 logf (LOG_FATAL|LOG_ERRNO, "fwrite");