2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: extract.c,v 1.112 2002-03-14 18:41:59 adam Exp $
24 #if _FILE_OFFSET_BITS == 64
25 #define PRINTF_OFF_T "%Ld"
27 #define PRINTF_OFF_T "%ld"
38 static int records_inserted = 0;
39 static int records_updated = 0;
40 static int records_deleted = 0;
41 static int records_processed = 0;
43 static void logRecord (int showFlag)
47 if (showFlag || !(records_processed % 1000))
49 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
50 records_processed, records_inserted, records_updated,
55 static void extract_init (struct recExtractCtrl *p, RecWord *w)
57 w->zebra_maps = p->zebra_maps;
59 w->attrSet = VAL_BIB1;
65 static const char **searchRecordKey (ZebraHandle zh,
66 struct recKeys *reckeys,
67 int attrSetS, int attrUseS)
69 static const char *ws[32];
81 for (i = 0; i<32; i++)
85 chS = zebraExplain_lookupSU (zh->service->zei, attrSetS, attrUseS);
89 while (off < reckeys->buf_used)
92 const char *src = reckeys->buf + off;
100 memcpy (&ch, src, sizeof(ch));
106 memcpy (&attrSet, src, sizeof(attrSet));
107 src += sizeof(attrSet);
111 memcpy (&attrUse, src, sizeof(attrUse));
112 src += sizeof(attrUse);
119 seqno += ((lead>>2) & 15)-1;
122 memcpy (&seqno, src, sizeof(seqno));
123 src += sizeof(seqno);
129 attrUseS == attrUse && attrSetS == attrSet
138 woff = seqno - startSeq;
139 if (woff >= 0 && woff < 31)
143 off = src - reckeys->buf;
145 assert (off == reckeys->buf_used);
149 struct file_read_info {
150 off_t file_max; /* maximum offset so far */
151 off_t file_offset; /* current offset */
152 off_t file_moffset; /* offset of rec/rec boundary */
159 static struct file_read_info *file_read_start (int fd)
161 struct file_read_info *fi = (struct file_read_info *)
162 xmalloc (sizeof(*fi));
166 fi->file_moffset = 0;
172 static void file_read_stop (struct file_read_info *fi)
177 static off_t file_seek (void *handle, off_t offset)
179 struct file_read_info *p = (struct file_read_info *) handle;
180 p->file_offset = offset;
183 return lseek (p->fd, offset, SEEK_SET);
186 static off_t file_tell (void *handle)
188 struct file_read_info *p = (struct file_read_info *) handle;
189 return p->file_offset;
192 static int file_read (void *handle, char *buf, size_t count)
194 struct file_read_info *p = (struct file_read_info *) handle;
200 if (r > p->sdrmax - p->file_offset)
201 r = p->sdrmax - p->file_offset;
203 memcpy (buf, p->sdrbuf + p->file_offset, r);
206 r = read (fd, buf, count);
210 if (p->file_offset > p->file_max)
211 p->file_max = p->file_offset;
216 static void file_begin (void *handle)
218 struct file_read_info *p = (struct file_read_info *) handle;
220 p->file_offset = p->file_moffset;
221 if (!p->sdrbuf && p->file_moffset)
222 lseek (p->fd, p->file_moffset, SEEK_SET);
226 static void file_end (void *handle, off_t offset)
228 struct file_read_info *p = (struct file_read_info *) handle;
230 assert (p->file_more == 0);
232 p->file_moffset = offset;
235 static char *fileMatchStr (ZebraHandle zh,
236 struct recKeys *reckeys, struct recordGroup *rGroup,
237 const char *fname, const char *spec)
239 static char dstBuf[2048]; /* static here ??? */
241 const char *s = spec;
242 static const char **w;
246 while (*s == ' ' || *s == '\t')
252 char attset_str[64], attname_str[64];
253 data1_attset *attset;
256 int attSet = 1, attUse = 1;
260 for (i = 0; *s && *s != ',' && *s != ')'; s++)
262 attset_str[i++] = *s;
263 attset_str[i] = '\0';
268 for (i = 0; *s && *s != ')'; s++)
270 attname_str[i++] = *s;
271 attname_str[i] = '\0';
274 if ((attset = data1_get_attset (zh->service->dh, attset_str)))
277 attSet = attset->reference;
278 att = data1_getattbyname(zh->service->dh, attset, attname_str);
282 attUse = atoi (attname_str);
284 w = searchRecordKey (zh, reckeys, attSet, attUse);
289 for (i = 0; i<32; i++)
294 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
295 spec, rGroup->groupName ? rGroup->groupName : "none");
300 for (i = 0; i<32; i++)
301 if (matchFlag[i] && w[i])
313 logf (LOG_WARN, "Record didn't contain match"
314 " fields in (%s,%s)", attset_str, attname_str);
322 const char *spec_src = NULL;
323 const char *s1 = ++s;
324 while (*s1 && *s1 != ' ' && *s1 != '\t')
330 memcpy (special, s, spec_len);
331 special[spec_len] = '\0';
334 if (!strcmp (special, "group"))
335 spec_src = rGroup->groupName;
336 else if (!strcmp (special, "database"))
337 spec_src = rGroup->databaseName;
338 else if (!strcmp (special, "filename"))
340 else if (!strcmp (special, "type"))
341 spec_src = rGroup->recordType;
346 strcpy (dst, spec_src);
347 dst += strlen (spec_src);
350 else if (*s == '\"' || *s == '\'')
352 int stopMarker = *s++;
356 while (*s && *s != stopMarker)
359 tmpString[i++] = *s++;
364 strcpy (dst, tmpString);
365 dst += strlen (tmpString);
369 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
370 spec, rGroup->groupName ? rGroup->groupName : "none");
377 logf (LOG_WARN, "No match criteria for record %s in group %s",
378 fname, rGroup->groupName ? rGroup->groupName : "none");
385 struct recordLogInfo {
388 struct recordGroup *rGroup;
391 static void recordLogPreamble (int level, const char *msg, void *info)
393 struct recordLogInfo *p = (struct recordLogInfo *) info;
394 FILE *outf = yaz_log_file ();
398 fprintf (outf, "File %s, offset %d, type %s\n",
399 p->rGroup->recordType, p->recordOffset, p->fname);
400 log_event_start (NULL, NULL);
404 static int recordExtract (ZebraHandle zh,
405 SYSNO *sysno, const char *fname,
406 struct recordGroup *rGroup, int deleteFlag,
407 struct file_read_info *fi,
408 RecType recType, char *subType, void *clientData)
410 RecordAttr *recordAttr;
415 struct recordLogInfo logInfo;
416 off_t recordOffset = 0;
420 struct recExtractCtrl extractCtrl;
422 /* we are going to read from a file, so prepare the extraction */
425 zh->keys.buf_used = 0;
426 zh->keys.prevAttrUse = -1;
427 zh->keys.prevAttrSet = -1;
428 zh->keys.prevSeqNo = 0;
431 recordOffset = fi->file_moffset;
432 extractCtrl.offset = fi->file_moffset;
433 extractCtrl.readf = file_read;
434 extractCtrl.seekf = file_seek;
435 extractCtrl.tellf = file_tell;
436 extractCtrl.endf = file_end;
438 extractCtrl.subType = subType;
439 extractCtrl.init = extract_init;
440 extractCtrl.tokenAdd = extract_token_add;
441 extractCtrl.schemaAdd = extract_schema_add;
442 extractCtrl.dh = zh->service->dh;
443 extractCtrl.handle = zh;
444 for (i = 0; i<256; i++)
446 if (zebra_maps_is_positioned(zh->service->zebra_maps, i))
447 extractCtrl.seqno[i] = 1;
449 extractCtrl.seqno[i] = 0;
451 extractCtrl.zebra_maps = zh->service->zebra_maps;
452 extractCtrl.flagShowRecords = !rGroup->flagRw;
455 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
457 logInfo.fname = fname;
458 logInfo.recordOffset = recordOffset;
459 logInfo.rGroup = rGroup;
460 log_event_start (recordLogPreamble, &logInfo);
462 r = (*recType->extract)(clientData, &extractCtrl);
464 log_event_start (NULL, NULL);
466 if (r == RECCTRL_EXTRACT_EOF)
468 else if (r == RECCTRL_EXTRACT_ERROR)
470 /* error occured during extraction ... */
471 if (rGroup->flagRw &&
472 records_processed < rGroup->fileVerboseLimit)
474 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
475 fname, recordOffset);
479 if (zh->keys.buf_used == 0)
481 /* the extraction process returned no information - the record
482 is probably empty - unless flagShowRecords is in use */
486 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
487 fname, recordOffset);
492 /* perform match if sysno not known and if match criteria is specified */
499 if (rGroup->recordId && *rGroup->recordId)
503 matchStr = fileMatchStr (zh, &zh->keys, rGroup, fname,
507 rinfo = dict_lookup (zh->service->matchDict, matchStr);
509 memcpy (sysno, rinfo+1, sizeof(*sysno));
513 logf (LOG_WARN, "Bad match criteria");
524 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
525 fname, recordOffset);
526 logf (LOG_WARN, "cannot delete record above (seems new)");
529 if (records_processed < rGroup->fileVerboseLimit)
530 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
531 fname, recordOffset);
532 rec = rec_new (zh->service->records);
536 recordAttr = rec_init_attr (zh->service->zei, rec);
540 dict_insert (zh->service->matchDict, matchStr, sizeof(*sysno), sysno);
542 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
543 extract_flushSortKeys (zh, *sysno, 1, &zh->sortKeys);
549 /* record already exists */
550 struct recKeys delkeys;
552 rec = rec_get (zh->service->records, *sysno);
555 recordAttr = rec_init_attr (zh->service->zei, rec);
557 if (recordAttr->runNumber ==
558 zebraExplain_runNumberIncrement (zh->service->zei, 0))
560 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
561 fname, recordOffset);
562 extract_flushSortKeys (zh, *sysno, -1, &zh->sortKeys);
567 delkeys.buf_used = rec->size[recInfo_delKeys];
568 delkeys.buf = rec->info[recInfo_delKeys];
569 extract_flushSortKeys (zh, *sysno, 0, &zh->sortKeys);
570 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
573 /* record going to be deleted */
574 if (!delkeys.buf_used)
576 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
577 rGroup->recordType, fname, recordOffset);
578 logf (LOG_WARN, "cannot delete file above, storeKeys false");
582 if (records_processed < rGroup->fileVerboseLimit)
583 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
584 rGroup->recordType, fname, recordOffset);
587 dict_delete (zh->service->matchDict, matchStr);
588 rec_del (zh->service->records, &rec);
596 /* record going to be updated */
597 if (!delkeys.buf_used)
599 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
600 rGroup->recordType, fname, recordOffset);
601 logf (LOG_WARN, "cannot update file above, storeKeys false");
605 if (records_processed < rGroup->fileVerboseLimit)
606 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
607 rGroup->recordType, fname, recordOffset);
608 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
613 /* update file type */
614 xfree (rec->info[recInfo_fileType]);
615 rec->info[recInfo_fileType] =
616 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
618 /* update filename */
619 xfree (rec->info[recInfo_filename]);
620 rec->info[recInfo_filename] =
621 rec_strdup (fname, &rec->size[recInfo_filename]);
623 /* update delete keys */
624 xfree (rec->info[recInfo_delKeys]);
625 if (zh->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
628 rec->size[recInfo_delKeys] = zh->keys.buf_used;
629 rec->info[recInfo_delKeys] = zh->keys.buf;
631 zh->keys.buf_max = 0;
633 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
634 rec->size[recInfo_delKeys] = reckeys.buf_used;
635 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
636 rec->size[recInfo_delKeys]);
641 rec->info[recInfo_delKeys] = NULL;
642 rec->size[recInfo_delKeys] = 0;
645 /* save file size of original record */
646 zebraExplain_recordBytesIncrement (zh->service->zei,
647 - recordAttr->recordSize);
648 recordAttr->recordSize = fi->file_moffset - recordOffset;
649 if (!recordAttr->recordSize)
650 recordAttr->recordSize = fi->file_max - recordOffset;
651 zebraExplain_recordBytesIncrement (zh->service->zei,
652 recordAttr->recordSize);
654 /* set run-number for this record */
655 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->service->zei,
658 /* update store data */
659 xfree (rec->info[recInfo_storeData]);
660 if (rGroup->flagStoreData == 1)
662 rec->size[recInfo_storeData] = recordAttr->recordSize;
663 rec->info[recInfo_storeData] = (char *)
664 xmalloc (recordAttr->recordSize);
665 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
667 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
668 recordOffset, fname);
671 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
672 < recordAttr->recordSize)
674 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
675 recordAttr->recordSize, fname);
681 rec->info[recInfo_storeData] = NULL;
682 rec->size[recInfo_storeData] = 0;
684 /* update database name */
685 xfree (rec->info[recInfo_databaseName]);
686 rec->info[recInfo_databaseName] =
687 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
690 recordAttr->recordOffset = recordOffset;
692 /* commit this record */
693 rec_put (zh->service->records, &rec);
698 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
699 const struct recordGroup *rGroupP, int deleteFlag)
707 struct recordGroup rGroupM;
708 struct recordGroup *rGroup = &rGroupM;
709 struct file_read_info *fi;
712 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
714 if (!rGroup->groupName || !*rGroup->groupName)
717 sprintf (gprefix, "%s.", rGroup->groupName);
719 logf (LOG_DEBUG, "fileExtract %s", fname);
721 /* determine file extension */
723 for (i = strlen(fname); --i >= 0; )
726 else if (fname[i] == '.')
728 strcpy (ext, fname+i+1);
731 /* determine file type - depending on extension */
732 if (!rGroup->recordType)
734 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
735 if (!(rGroup->recordType = res_get (zh->service->res, ext_res)))
737 sprintf (ext_res, "%srecordType", gprefix);
738 rGroup->recordType = res_get (zh->service->res, ext_res);
741 if (!rGroup->recordType)
743 if (records_processed < rGroup->fileVerboseLimit)
744 logf (LOG_LOG, "? %s", fname);
747 if (!*rGroup->recordType)
750 recType_byName (zh->service->recTypes, rGroup->recordType, subType,
753 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
757 /* determine match criteria */
758 if (!rGroup->recordId)
760 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
761 rGroup->recordId = res_get (zh->service->res, ext_res);
764 /* determine database name */
765 if (!rGroup->databaseName)
767 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
768 if (!(rGroup->databaseName = res_get (zh->service->res, ext_res)))
770 sprintf (ext_res, "%sdatabase", gprefix);
771 rGroup->databaseName = res_get (zh->service->res, ext_res);
774 if (!rGroup->databaseName)
775 rGroup->databaseName = "Default";
777 /* determine if explain database */
779 sprintf (ext_res, "%sexplainDatabase", gprefix);
780 rGroup->explainDatabase =
781 atoi (res_get_def (zh->service->res, ext_res, "0"));
783 /* announce database */
784 if (zebraExplain_curDatabase (zh->service->zei, rGroup->databaseName))
786 if (zebraExplain_newDatabase (zh->service->zei, rGroup->databaseName,
787 rGroup->explainDatabase))
791 if (rGroup->flagStoreData == -1)
794 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
795 if (!(sval = res_get (zh->service->res, ext_res)))
797 sprintf (ext_res, "%sstoreData", gprefix);
798 sval = res_get (zh->service->res, ext_res);
801 rGroup->flagStoreData = atoi (sval);
803 if (rGroup->flagStoreData == -1)
804 rGroup->flagStoreData = 0;
806 if (rGroup->flagStoreKeys == -1)
810 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
811 sval = res_get (zh->service->res, ext_res);
814 sprintf (ext_res, "%sstoreKeys", gprefix);
815 sval = res_get (zh->service->res, ext_res);
818 sval = res_get (zh->service->res, "storeKeys");
820 rGroup->flagStoreKeys = atoi (sval);
822 if (rGroup->flagStoreKeys == -1)
823 rGroup->flagStoreKeys = 0;
825 if (sysno && deleteFlag)
829 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
831 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
835 fi = file_read_start (fd);
839 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
840 recType, subType, clientData);
841 } while (r && !sysno && fi->file_more);
849 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
850 const char *buf, size_t buf_size,
851 const char *databaseName, int delete_flag,
852 int test_mode, int *sysno,
853 int store_keys, int store_data,
854 const char *match_criteria)
856 RecordAttr *recordAttr;
857 struct recExtractCtrl extractCtrl;
863 const char *fname = "<no file>";
865 long recordOffset = 0;
866 struct zebra_fetch_control fc;
869 fc.record_int_buf = buf;
870 fc.record_int_len = buf_size;
871 fc.record_int_pos = 0;
873 fc.record_offset = 0;
875 extractCtrl.offset = 0;
876 extractCtrl.readf = zebra_record_int_read;
877 extractCtrl.seekf = zebra_record_int_seek;
878 extractCtrl.tellf = zebra_record_int_tell;
879 extractCtrl.endf = zebra_record_int_end;
880 extractCtrl.fh = &fc;
882 /* announce database */
883 if (zebraExplain_curDatabase (zh->service->zei, databaseName))
885 if (zebraExplain_newDatabase (zh->service->zei, databaseName, 0))
889 recType_byName (zh->service->recTypes, recordType, subType,
892 logf (LOG_WARN, "No such record type: %s", recordType);
896 zh->keys.buf_used = 0;
897 zh->keys.prevAttrUse = -1;
898 zh->keys.prevAttrSet = -1;
899 zh->keys.prevSeqNo = 0;
902 extractCtrl.subType = subType;
903 extractCtrl.init = extract_init;
904 extractCtrl.tokenAdd = extract_token_add;
905 extractCtrl.schemaAdd = extract_schema_add;
906 extractCtrl.dh = zh->service->dh;
907 extractCtrl.handle = zh;
908 extractCtrl.zebra_maps = zh->service->zebra_maps;
909 extractCtrl.flagShowRecords = 0;
910 for (i = 0; i<256; i++)
912 if (zebra_maps_is_positioned(zh->service->zebra_maps, i))
913 extractCtrl.seqno[i] = 1;
915 extractCtrl.seqno[i] = 0;
918 r = (*recType->extract)(clientData, &extractCtrl);
920 if (r == RECCTRL_EXTRACT_EOF)
922 else if (r == RECCTRL_EXTRACT_ERROR)
924 /* error occured during extraction ... */
926 yaz_log (LOG_WARN, "extract error");
928 if (rGroup->flagRw &&
929 records_processed < rGroup->fileVerboseLimit)
931 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
932 fname, (long) recordOffset);
937 if (zh->keys.buf_used == 0)
939 /* the extraction process returned no information - the record
940 is probably empty - unless flagShowRecords is in use */
943 logf (LOG_WARN, "No keys generated for record");
944 logf (LOG_WARN, " The file is probably empty");
954 logf (LOG_LOG, "delete %s %s %ld", recordType,
955 fname, (long) recordOffset);
956 logf (LOG_WARN, "cannot delete record above (seems new)");
959 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
960 (long) recordOffset);
961 rec = rec_new (zh->service->records);
965 recordAttr = rec_init_attr (zh->service->zei, rec);
969 dict_insert (zh->service->matchDict, matchStr,
970 sizeof(*sysno), sysno);
972 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
973 extract_flushSortKeys (zh, *sysno, 1, &zh->sortKeys);
977 /* record already exists */
978 struct recKeys delkeys;
980 rec = rec_get (zh->service->records, *sysno);
983 recordAttr = rec_init_attr (zh->service->zei, rec);
985 if (recordAttr->runNumber ==
986 zebraExplain_runNumberIncrement (zh->service->zei, 0))
988 logf (LOG_LOG, "skipped %s %s %ld", recordType,
989 fname, (long) recordOffset);
993 delkeys.buf_used = rec->size[recInfo_delKeys];
994 delkeys.buf = rec->info[recInfo_delKeys];
995 extract_flushSortKeys (zh, *sysno, 0, &zh->sortKeys);
996 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
999 /* record going to be deleted */
1000 if (!delkeys.buf_used)
1002 logf (LOG_LOG, "delete %s %s %ld", recordType,
1003 fname, (long) recordOffset);
1004 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1008 logf (LOG_LOG, "delete %s %s %ld", recordType,
1009 fname, (long) recordOffset);
1012 dict_delete (matchDict, matchStr);
1014 rec_del (zh->service->records, &rec);
1021 /* record going to be updated */
1022 if (!delkeys.buf_used)
1024 logf (LOG_LOG, "update %s %s %ld", recordType,
1025 fname, (long) recordOffset);
1026 logf (LOG_WARN, "cannot update file above, storeKeys false");
1030 logf (LOG_LOG, "update %s %s %ld", recordType,
1031 fname, (long) recordOffset);
1032 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
1036 /* update file type */
1037 xfree (rec->info[recInfo_fileType]);
1038 rec->info[recInfo_fileType] =
1039 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1041 /* update filename */
1042 xfree (rec->info[recInfo_filename]);
1043 rec->info[recInfo_filename] =
1044 rec_strdup (fname, &rec->size[recInfo_filename]);
1046 /* update delete keys */
1047 xfree (rec->info[recInfo_delKeys]);
1048 if (zh->keys.buf_used > 0 && store_keys == 1)
1050 rec->size[recInfo_delKeys] = zh->keys.buf_used;
1051 rec->info[recInfo_delKeys] = zh->keys.buf;
1052 zh->keys.buf = NULL;
1053 zh->keys.buf_max = 0;
1057 rec->info[recInfo_delKeys] = NULL;
1058 rec->size[recInfo_delKeys] = 0;
1061 /* save file size of original record */
1062 zebraExplain_recordBytesIncrement (zh->service->zei,
1063 - recordAttr->recordSize);
1065 recordAttr->recordSize = fi->file_moffset - recordOffset;
1066 if (!recordAttr->recordSize)
1067 recordAttr->recordSize = fi->file_max - recordOffset;
1069 recordAttr->recordSize = buf_size;
1071 zebraExplain_recordBytesIncrement (zh->service->zei,
1072 recordAttr->recordSize);
1074 /* set run-number for this record */
1075 recordAttr->runNumber =
1076 zebraExplain_runNumberIncrement (zh->service->zei, 0);
1078 /* update store data */
1079 xfree (rec->info[recInfo_storeData]);
1080 if (store_data == 1)
1082 rec->size[recInfo_storeData] = recordAttr->recordSize;
1083 rec->info[recInfo_storeData] = (char *)
1084 xmalloc (recordAttr->recordSize);
1086 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1088 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1090 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1091 (long) recordOffset, fname);
1094 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1095 < recordAttr->recordSize)
1097 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1098 recordAttr->recordSize, fname);
1105 rec->info[recInfo_storeData] = NULL;
1106 rec->size[recInfo_storeData] = 0;
1108 /* update database name */
1109 xfree (rec->info[recInfo_databaseName]);
1110 rec->info[recInfo_databaseName] =
1111 rec_strdup (databaseName, &rec->size[recInfo_databaseName]);
1114 recordAttr->recordOffset = recordOffset;
1116 /* commit this record */
1117 rec_put (zh->service->records, &rec);
1122 int explain_extract (void *handle, Record rec, data1_node *n)
1124 ZebraHandle zh = (ZebraHandle) handle;
1125 struct recExtractCtrl extractCtrl;
1128 if (zebraExplain_curDatabase (zh->service->zei,
1129 rec->info[recInfo_databaseName]))
1132 if (zebraExplain_newDatabase (zh->service->zei,
1133 rec->info[recInfo_databaseName], 0))
1137 zh->keys.buf_used = 0;
1138 zh->keys.prevAttrUse = -1;
1139 zh->keys.prevAttrSet = -1;
1140 zh->keys.prevSeqNo = 0;
1143 extractCtrl.init = extract_init;
1144 extractCtrl.tokenAdd = extract_token_add;
1145 extractCtrl.schemaAdd = extract_schema_add;
1146 extractCtrl.dh = zh->service->dh;
1147 for (i = 0; i<256; i++)
1148 extractCtrl.seqno[i] = 0;
1149 extractCtrl.zebra_maps = zh->service->zebra_maps;
1150 extractCtrl.flagShowRecords = 0;
1151 extractCtrl.handle = handle;
1153 grs_extract_tree(&extractCtrl, n);
1155 logf (LOG_LOG, "flush explain record, sysno=%d", rec->sysno);
1157 if (rec->size[recInfo_delKeys])
1159 struct recKeys delkeys;
1160 struct sortKey *sortKeys = 0;
1162 delkeys.buf_used = rec->size[recInfo_delKeys];
1163 delkeys.buf = rec->info[recInfo_delKeys];
1164 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1165 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1167 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->keys);
1168 extract_flushSortKeys (zh, rec->sysno, 1, &zh->sortKeys);
1170 xfree (rec->info[recInfo_delKeys]);
1171 rec->size[recInfo_delKeys] = zh->keys.buf_used;
1172 rec->info[recInfo_delKeys] = zh->keys.buf;
1173 zh->keys.buf = NULL;
1174 zh->keys.buf_max = 0;
1178 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1179 int cmd, struct recKeys *reckeys)
1183 unsigned char attrSet = (unsigned char) -1;
1184 unsigned short attrUse = (unsigned short) -1;
1189 ZebraExplainInfo zei = zh->service->zei;
1193 int mem = 8*1024*1024;
1194 zh->key_buf = (char**) xmalloc (mem);
1195 zh->ptr_top = mem/sizeof(char*);
1197 zh->key_buf_used = 0;
1198 zh->key_file_no = 0;
1200 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1201 while (off < reckeys->buf_used)
1203 const char *src = reckeys->buf + off;
1212 memcpy (&ch, src, sizeof(ch));
1218 memcpy (&attrSet, src, sizeof(attrSet));
1219 src += sizeof(attrSet);
1223 memcpy (&attrUse, src, sizeof(attrUse));
1224 src += sizeof(attrUse);
1227 if (zh->key_buf_used + 1024 > (zh->ptr_top-zh->ptr_i)*sizeof(char*))
1228 extract_flushWriteKeys (zh);
1230 (zh->key_buf)[zh->ptr_top - zh->ptr_i] =
1231 (char*)zh->key_buf + zh->key_buf_used;
1234 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1236 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1240 key_SU_encode (ch,((char*)zh->key_buf) + zh->key_buf_used);
1243 ((char*)zh->key_buf) [(zh->key_buf_used)++] = *src++;
1245 ((char*)(zh->key_buf))[(zh->key_buf_used)++] = '\0';
1246 ((char*)(zh->key_buf))[(zh->key_buf_used)++] = cmd;
1249 seqno += ((lead>>2) & 15)-1;
1252 memcpy (&seqno, src, sizeof(seqno));
1253 src += sizeof(seqno);
1257 memcpy ((char*)zh->key_buf + zh->key_buf_used, &key, sizeof(key));
1258 (zh->key_buf_used) += sizeof(key);
1259 off = src - reckeys->buf;
1261 assert (off == reckeys->buf_used);
1264 void extract_flushWriteKeys (ZebraHandle zh)
1267 char out_fname[200];
1269 struct encode_info encode_info;
1270 int ptr_i = zh->ptr_i;
1274 if (!zh->key_buf || ptr_i <= 0)
1277 (zh->key_file_no)++;
1278 logf (LOG_LOG, "sorting section %d", (zh->key_file_no));
1280 qsort (zh->key_buf + zh->ptr_top - ptr_i, ptr_i, sizeof(char*),
1282 extract_get_fname_tmp (zh, out_fname, zh->key_file_no);
1284 if (!(outf = fopen (out_fname, "wb")))
1286 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1289 logf (LOG_LOG, "writing section %d", zh->key_file_no);
1290 prevcp = cp = (zh->key_buf)[zh->ptr_top - ptr_i];
1292 encode_key_init (&encode_info);
1293 encode_key_write (cp, &encode_info, outf);
1297 cp = (zh->key_buf)[zh->ptr_top - ptr_i];
1298 if (strcmp (cp, prevcp))
1300 encode_key_init (&encode_info);
1301 encode_key_write (cp, &encode_info, outf);
1305 encode_key_write (cp + strlen(cp), &encode_info, outf);
1308 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1309 extract_get_fname_tmp (out_fname, key_file_no);
1311 if (!(outf = fopen (out_fname, "wb")))
1313 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1316 logf (LOG_LOG, "writing section %d", key_file_no);
1318 prevcp = key_buf[ptr_top-i];
1320 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1322 key_y_len = strlen(prevcp)+1;
1324 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1325 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1327 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1328 sizeof(char*), key_y_compare);
1329 cp = key_buf[ptr_top-ptr_i];
1331 encode_key_init (&encode_info);
1332 encode_key_write (cp, &encode_info, outf);
1335 cp = key_buf[ptr_top-ptr_i];
1336 encode_key_write (cp+key_y_len, &encode_info, outf);
1340 prevcp = key_buf[ptr_top-ptr_i];
1345 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1348 logf (LOG_LOG, "finished section %d", zh->key_file_no);
1350 zh->key_buf_used = 0;
1353 void extract_add_index_string (RecWord *p, const char *string,
1357 unsigned char attrSet;
1358 unsigned short attrUse;
1361 int *pseqno = &p->seqnos[p->reg_type];
1362 ZebraHandle zh = p->extractCtrl->handle;
1363 ZebraExplainInfo zei = zh->service->zei;
1364 struct recKeys *keys = &zh->keys;
1366 if (keys->buf_used+1024 > keys->buf_max)
1370 b = (char *) xmalloc (keys->buf_max += 128000);
1371 if (keys->buf_used > 0)
1372 memcpy (b, keys->buf, keys->buf_used);
1376 dst = keys->buf + keys->buf_used;
1378 attrSet = p->attrSet;
1379 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1382 keys->prevAttrSet = attrSet;
1383 attrUse = p->attrUse;
1384 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1387 keys->prevAttrUse = attrUse;
1389 diff = 1 + *pseqno - keys->prevSeqNo;
1390 if (diff >= 1 && diff <= 15)
1391 lead |= (diff << 2);
1395 keys->prevSeqNo = *pseqno;
1402 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1405 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1406 yaz_log (LOG_LOG, "addSU set=%d use=%d SU=%d",
1407 attrSet, attrUse, ch);
1410 memcpy (dst, &ch, sizeof(ch));
1416 memcpy (dst, &attrSet, sizeof(attrSet));
1417 dst += sizeof(attrSet);
1421 memcpy (dst, &attrUse, sizeof(attrUse));
1422 dst += sizeof(attrUse);
1425 *dst++ = p->reg_type;
1426 memcpy (dst, string, length);
1432 memcpy (dst, pseqno, sizeof(*pseqno));
1433 dst += sizeof(*pseqno);
1435 keys->buf_used = dst - keys->buf;
1440 static void extract_add_sort_string (RecWord *p, const char *string,
1444 ZebraHandle zh = p->extractCtrl->handle;
1446 for (sk = zh->sortKeys; sk; sk = sk->next)
1447 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1450 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1451 sk->next = zh->sortKeys;
1454 sk->string = (char *) xmalloc (length);
1455 sk->length = length;
1456 memcpy (sk->string, string, length);
1458 sk->attrSet = p->attrSet;
1459 sk->attrUse = p->attrUse;
1462 void extract_add_string (RecWord *p, const char *string, int length)
1464 assert (length > 0);
1465 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1466 extract_add_sort_string (p, string, length);
1468 extract_add_index_string (p, string, length);
1471 static void extract_add_incomplete_field (RecWord *p)
1473 const char *b = p->string;
1474 int remain = p->length;
1475 const char **map = 0;
1478 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1482 char buf[IT_MAX_WORD+1];
1486 while (map && *map && **map == *CHR_SPACE)
1488 remain = p->length - (b - p->string);
1490 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1497 while (map && *map && **map != *CHR_SPACE)
1499 const char *cp = *map;
1501 while (i < IT_MAX_WORD && *cp)
1503 remain = p->length - (b - p->string);
1505 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1511 extract_add_string (p, buf, i);
1513 (p->seqnos[p->reg_type])++; /* to separate this from next one */
1516 static void extract_add_complete_field (RecWord *p)
1518 const char *b = p->string;
1519 char buf[IT_MAX_WORD+1];
1520 const char **map = 0;
1521 int i = 0, remain = p->length;
1524 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1526 while (remain > 0 && i < IT_MAX_WORD)
1528 while (map && *map && **map == *CHR_SPACE)
1530 remain = p->length - (b - p->string);
1532 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1539 if (i && i < IT_MAX_WORD)
1540 buf[i++] = *CHR_SPACE;
1541 while (map && *map && **map != *CHR_SPACE)
1543 const char *cp = *map;
1545 if (i >= IT_MAX_WORD)
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,
1559 extract_add_string (p, buf, i);
1562 void extract_token_add (RecWord *p)
1565 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1566 p->string, p->length)))
1568 p->string = wrbuf_buf(wrbuf);
1569 p->length = wrbuf_len(wrbuf);
1571 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1572 extract_add_complete_field (p);
1574 extract_add_incomplete_field(p);
1577 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1579 ZebraHandle zh = (ZebraHandle) (p->handle);
1580 zebraExplain_addSchema (zh->service->zei, oid);
1583 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1584 int cmd, struct sortKey **skp)
1586 struct sortKey *sk = *skp;
1587 SortIdx sortIdx = zh->service->sortIdx;
1589 sortIdx_sysno (sortIdx, sysno);
1592 struct sortKey *sk_next = sk->next;
1593 sortIdx_type (sortIdx, sk->attrUse);
1594 sortIdx_add (sortIdx, sk->string, sk->length);
1599 yaz_log (LOG_LOG, "extract_flushSortKeys");
1603 void encode_key_init (struct encode_info *i)
1610 char *encode_key_int (int d, char *bp)
1614 else if (d <= 16383)
1616 *bp++ = 64 + (d>>8);
1619 else if (d <= 4194303)
1621 *bp++ = 128 + (d>>16);
1622 *bp++ = (d>>8) & 255;
1627 *bp++ = 192 + (d>>24);
1628 *bp++ = (d>>16) & 255;
1629 *bp++ = (d>>8) & 255;
1635 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1640 while ((*bp++ = *k++))
1642 memcpy (&key, k+1, sizeof(struct it_key));
1643 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1644 if (i->sysno != key.sysno)
1646 i->sysno = key.sysno;
1649 else if (!i->seqno && !key.seqno && i->cmd == *k)
1651 bp = encode_key_int (key.seqno - i->seqno, bp);
1652 i->seqno = key.seqno;
1654 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1656 logf (LOG_FATAL|LOG_ERRNO, "fwrite");