2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: extract.c,v 1.111 2002-02-20 23:07:54 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;
430 recordOffset = fi->file_moffset;
431 extractCtrl.offset = fi->file_moffset;
432 extractCtrl.readf = file_read;
433 extractCtrl.seekf = file_seek;
434 extractCtrl.tellf = file_tell;
435 extractCtrl.endf = file_end;
437 extractCtrl.subType = subType;
438 extractCtrl.init = extract_init;
439 extractCtrl.tokenAdd = extract_token_add;
440 extractCtrl.schemaAdd = extract_schema_add;
441 extractCtrl.dh = zh->service->dh;
442 extractCtrl.handle = zh;
443 for (i = 0; i<256; i++)
445 if (zebra_maps_is_positioned(zh->service->zebra_maps, i))
446 extractCtrl.seqno[i] = 1;
448 extractCtrl.seqno[i] = 0;
450 extractCtrl.zebra_maps = zh->service->zebra_maps;
451 extractCtrl.flagShowRecords = !rGroup->flagRw;
454 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
456 logInfo.fname = fname;
457 logInfo.recordOffset = recordOffset;
458 logInfo.rGroup = rGroup;
459 log_event_start (recordLogPreamble, &logInfo);
461 r = (*recType->extract)(clientData, &extractCtrl);
463 log_event_start (NULL, NULL);
465 if (r == RECCTRL_EXTRACT_EOF)
467 else if (r == RECCTRL_EXTRACT_ERROR)
469 /* error occured during extraction ... */
470 if (rGroup->flagRw &&
471 records_processed < rGroup->fileVerboseLimit)
473 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
474 fname, recordOffset);
478 if (zh->keys.buf_used == 0)
480 /* the extraction process returned no information - the record
481 is probably empty - unless flagShowRecords is in use */
485 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
486 fname, recordOffset);
491 /* perform match if sysno not known and if match criteria is specified */
498 if (rGroup->recordId && *rGroup->recordId)
502 matchStr = fileMatchStr (zh, &zh->keys, rGroup, fname,
506 rinfo = dict_lookup (zh->service->matchDict, matchStr);
508 memcpy (sysno, rinfo+1, sizeof(*sysno));
512 logf (LOG_WARN, "Bad match criteria");
523 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
524 fname, recordOffset);
525 logf (LOG_WARN, "cannot delete record above (seems new)");
528 if (records_processed < rGroup->fileVerboseLimit)
529 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
530 fname, recordOffset);
531 rec = rec_new (zh->service->records);
535 recordAttr = rec_init_attr (zh->service->zei, rec);
539 dict_insert (zh->service->matchDict, matchStr, sizeof(*sysno), sysno);
541 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
542 extract_flushSortKeys (zh, *sysno, 1, &zh->sortKeys);
548 /* record already exists */
549 struct recKeys delkeys;
551 rec = rec_get (zh->service->records, *sysno);
554 recordAttr = rec_init_attr (zh->service->zei, rec);
556 if (recordAttr->runNumber ==
557 zebraExplain_runNumberIncrement (zh->service->zei, 0))
559 logf (LOG_LOG, "skipped %s %s " PRINTF_OFF_T, rGroup->recordType,
560 fname, recordOffset);
561 extract_flushSortKeys (zh, *sysno, -1, &zh->sortKeys);
566 delkeys.buf_used = rec->size[recInfo_delKeys];
567 delkeys.buf = rec->info[recInfo_delKeys];
568 extract_flushSortKeys (zh, *sysno, 0, &zh->sortKeys);
569 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
572 /* record going to be deleted */
573 if (!delkeys.buf_used)
575 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
576 rGroup->recordType, fname, recordOffset);
577 logf (LOG_WARN, "cannot delete file above, storeKeys false");
581 if (records_processed < rGroup->fileVerboseLimit)
582 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
583 rGroup->recordType, fname, recordOffset);
586 dict_delete (zh->service->matchDict, matchStr);
587 rec_del (zh->service->records, &rec);
595 /* record going to be updated */
596 if (!delkeys.buf_used)
598 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
599 rGroup->recordType, fname, recordOffset);
600 logf (LOG_WARN, "cannot update file above, storeKeys false");
604 if (records_processed < rGroup->fileVerboseLimit)
605 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
606 rGroup->recordType, fname, recordOffset);
607 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
612 /* update file type */
613 xfree (rec->info[recInfo_fileType]);
614 rec->info[recInfo_fileType] =
615 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
617 /* update filename */
618 xfree (rec->info[recInfo_filename]);
619 rec->info[recInfo_filename] =
620 rec_strdup (fname, &rec->size[recInfo_filename]);
622 /* update delete keys */
623 xfree (rec->info[recInfo_delKeys]);
624 if (zh->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
627 rec->size[recInfo_delKeys] = zh->keys.buf_used;
628 rec->info[recInfo_delKeys] = zh->keys.buf;
630 zh->keys.buf_max = 0;
632 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
633 rec->size[recInfo_delKeys] = reckeys.buf_used;
634 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
635 rec->size[recInfo_delKeys]);
640 rec->info[recInfo_delKeys] = NULL;
641 rec->size[recInfo_delKeys] = 0;
644 /* save file size of original record */
645 zebraExplain_recordBytesIncrement (zh->service->zei,
646 - recordAttr->recordSize);
647 recordAttr->recordSize = fi->file_moffset - recordOffset;
648 if (!recordAttr->recordSize)
649 recordAttr->recordSize = fi->file_max - recordOffset;
650 zebraExplain_recordBytesIncrement (zh->service->zei,
651 recordAttr->recordSize);
653 /* set run-number for this record */
654 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->service->zei,
657 /* update store data */
658 xfree (rec->info[recInfo_storeData]);
659 if (rGroup->flagStoreData == 1)
661 rec->size[recInfo_storeData] = recordAttr->recordSize;
662 rec->info[recInfo_storeData] = (char *)
663 xmalloc (recordAttr->recordSize);
664 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
666 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
667 recordOffset, fname);
670 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
671 < recordAttr->recordSize)
673 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
674 recordAttr->recordSize, fname);
680 rec->info[recInfo_storeData] = NULL;
681 rec->size[recInfo_storeData] = 0;
683 /* update database name */
684 xfree (rec->info[recInfo_databaseName]);
685 rec->info[recInfo_databaseName] =
686 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
689 recordAttr->recordOffset = recordOffset;
691 /* commit this record */
692 rec_put (zh->service->records, &rec);
697 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
698 const struct recordGroup *rGroupP, int deleteFlag)
706 struct recordGroup rGroupM;
707 struct recordGroup *rGroup = &rGroupM;
708 struct file_read_info *fi;
711 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
713 if (!rGroup->groupName || !*rGroup->groupName)
716 sprintf (gprefix, "%s.", rGroup->groupName);
718 logf (LOG_DEBUG, "fileExtract %s", fname);
720 /* determine file extension */
722 for (i = strlen(fname); --i >= 0; )
725 else if (fname[i] == '.')
727 strcpy (ext, fname+i+1);
730 /* determine file type - depending on extension */
731 if (!rGroup->recordType)
733 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
734 if (!(rGroup->recordType = res_get (zh->service->res, ext_res)))
736 sprintf (ext_res, "%srecordType", gprefix);
737 rGroup->recordType = res_get (zh->service->res, ext_res);
740 if (!rGroup->recordType)
742 if (records_processed < rGroup->fileVerboseLimit)
743 logf (LOG_LOG, "? %s", fname);
746 if (!*rGroup->recordType)
749 recType_byName (zh->service->recTypes, rGroup->recordType, subType,
752 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
756 /* determine match criteria */
757 if (!rGroup->recordId)
759 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
760 rGroup->recordId = res_get (zh->service->res, ext_res);
763 /* determine database name */
764 if (!rGroup->databaseName)
766 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
767 if (!(rGroup->databaseName = res_get (zh->service->res, ext_res)))
769 sprintf (ext_res, "%sdatabase", gprefix);
770 rGroup->databaseName = res_get (zh->service->res, ext_res);
773 if (!rGroup->databaseName)
774 rGroup->databaseName = "Default";
776 /* determine if explain database */
778 sprintf (ext_res, "%sexplainDatabase", gprefix);
779 rGroup->explainDatabase =
780 atoi (res_get_def (zh->service->res, ext_res, "0"));
782 /* announce database */
783 if (zebraExplain_curDatabase (zh->service->zei, rGroup->databaseName))
785 if (zebraExplain_newDatabase (zh->service->zei, rGroup->databaseName,
786 rGroup->explainDatabase))
790 if (rGroup->flagStoreData == -1)
793 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
794 if (!(sval = res_get (zh->service->res, ext_res)))
796 sprintf (ext_res, "%sstoreData", gprefix);
797 sval = res_get (zh->service->res, ext_res);
800 rGroup->flagStoreData = atoi (sval);
802 if (rGroup->flagStoreData == -1)
803 rGroup->flagStoreData = 0;
805 if (rGroup->flagStoreKeys == -1)
809 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
810 sval = res_get (zh->service->res, ext_res);
813 sprintf (ext_res, "%sstoreKeys", gprefix);
814 sval = res_get (zh->service->res, ext_res);
817 sval = res_get (zh->service->res, "storeKeys");
819 rGroup->flagStoreKeys = atoi (sval);
821 if (rGroup->flagStoreKeys == -1)
822 rGroup->flagStoreKeys = 0;
824 if (sysno && deleteFlag)
828 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
830 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
834 fi = file_read_start (fd);
838 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
839 recType, subType, clientData);
840 } while (r && !sysno && fi->file_more);
848 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
849 const char *buf, size_t buf_size,
850 const char *databaseName, int delete_flag,
851 int test_mode, int *sysno,
852 int store_keys, int store_data,
853 const char *match_criteria)
855 RecordAttr *recordAttr;
856 struct recExtractCtrl extractCtrl;
862 const char *fname = "<no file>";
864 long recordOffset = 0;
865 struct zebra_fetch_control fc;
868 fc.record_int_buf = buf;
869 fc.record_int_len = buf_size;
870 fc.record_int_pos = 0;
872 fc.record_offset = 0;
874 extractCtrl.offset = 0;
875 extractCtrl.readf = zebra_record_int_read;
876 extractCtrl.seekf = zebra_record_int_seek;
877 extractCtrl.tellf = zebra_record_int_tell;
878 extractCtrl.endf = zebra_record_int_end;
879 extractCtrl.fh = &fc;
881 /* announce database */
882 if (zebraExplain_curDatabase (zh->service->zei, databaseName))
884 if (zebraExplain_newDatabase (zh->service->zei, databaseName, 0))
888 recType_byName (zh->service->recTypes, recordType, subType,
891 logf (LOG_WARN, "No such record type: %s", recordType);
895 zh->keys.buf_used = 0;
896 zh->keys.prevAttrUse = -1;
897 zh->keys.prevAttrSet = -1;
898 zh->keys.prevSeqNo = 0;
901 extractCtrl.subType = subType;
902 extractCtrl.init = extract_init;
903 extractCtrl.tokenAdd = extract_token_add;
904 extractCtrl.schemaAdd = extract_schema_add;
905 extractCtrl.dh = zh->service->dh;
906 extractCtrl.handle = zh;
907 extractCtrl.zebra_maps = zh->service->zebra_maps;
908 extractCtrl.flagShowRecords = 0;
909 for (i = 0; i<256; i++)
911 if (zebra_maps_is_positioned(zh->service->zebra_maps, i))
912 extractCtrl.seqno[i] = 1;
914 extractCtrl.seqno[i] = 0;
917 r = (*recType->extract)(clientData, &extractCtrl);
919 if (r == RECCTRL_EXTRACT_EOF)
921 else if (r == RECCTRL_EXTRACT_ERROR)
923 /* error occured during extraction ... */
925 yaz_log (LOG_WARN, "extract error");
927 if (rGroup->flagRw &&
928 records_processed < rGroup->fileVerboseLimit)
930 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
931 fname, (long) recordOffset);
936 if (zh->keys.buf_used == 0)
938 /* the extraction process returned no information - the record
939 is probably empty - unless flagShowRecords is in use */
942 logf (LOG_WARN, "No keys generated for record");
943 logf (LOG_WARN, " The file is probably empty");
953 logf (LOG_LOG, "delete %s %s %ld", recordType,
954 fname, (long) recordOffset);
955 logf (LOG_WARN, "cannot delete record above (seems new)");
958 logf (LOG_LOG, "add %s %s %ld", recordType, fname,
959 (long) recordOffset);
960 rec = rec_new (zh->service->records);
964 recordAttr = rec_init_attr (zh->service->zei, rec);
968 dict_insert (zh->service->matchDict, matchStr,
969 sizeof(*sysno), sysno);
971 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
972 extract_flushSortKeys (zh, *sysno, 1, &zh->sortKeys);
976 /* record already exists */
977 struct recKeys delkeys;
979 rec = rec_get (zh->service->records, *sysno);
982 recordAttr = rec_init_attr (zh->service->zei, rec);
984 if (recordAttr->runNumber ==
985 zebraExplain_runNumberIncrement (zh->service->zei, 0))
987 logf (LOG_LOG, "skipped %s %s %ld", recordType,
988 fname, (long) recordOffset);
992 delkeys.buf_used = rec->size[recInfo_delKeys];
993 delkeys.buf = rec->info[recInfo_delKeys];
994 extract_flushSortKeys (zh, *sysno, 0, &zh->sortKeys);
995 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
998 /* record going to be deleted */
999 if (!delkeys.buf_used)
1001 logf (LOG_LOG, "delete %s %s %ld", recordType,
1002 fname, (long) recordOffset);
1003 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1007 logf (LOG_LOG, "delete %s %s %ld", recordType,
1008 fname, (long) recordOffset);
1011 dict_delete (matchDict, matchStr);
1013 rec_del (zh->service->records, &rec);
1020 /* record going to be updated */
1021 if (!delkeys.buf_used)
1023 logf (LOG_LOG, "update %s %s %ld", recordType,
1024 fname, (long) recordOffset);
1025 logf (LOG_WARN, "cannot update file above, storeKeys false");
1029 logf (LOG_LOG, "update %s %s %ld", recordType,
1030 fname, (long) recordOffset);
1031 extract_flushRecordKeys (zh, *sysno, 1, &zh->keys);
1035 /* update file type */
1036 xfree (rec->info[recInfo_fileType]);
1037 rec->info[recInfo_fileType] =
1038 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1040 /* update filename */
1041 xfree (rec->info[recInfo_filename]);
1042 rec->info[recInfo_filename] =
1043 rec_strdup (fname, &rec->size[recInfo_filename]);
1045 /* update delete keys */
1046 xfree (rec->info[recInfo_delKeys]);
1047 if (zh->keys.buf_used > 0 && store_keys == 1)
1049 rec->size[recInfo_delKeys] = zh->keys.buf_used;
1050 rec->info[recInfo_delKeys] = zh->keys.buf;
1051 zh->keys.buf = NULL;
1052 zh->keys.buf_max = 0;
1056 rec->info[recInfo_delKeys] = NULL;
1057 rec->size[recInfo_delKeys] = 0;
1060 /* save file size of original record */
1061 zebraExplain_recordBytesIncrement (zh->service->zei,
1062 - recordAttr->recordSize);
1064 recordAttr->recordSize = fi->file_moffset - recordOffset;
1065 if (!recordAttr->recordSize)
1066 recordAttr->recordSize = fi->file_max - recordOffset;
1068 recordAttr->recordSize = buf_size;
1070 zebraExplain_recordBytesIncrement (zh->service->zei,
1071 recordAttr->recordSize);
1073 /* set run-number for this record */
1074 recordAttr->runNumber =
1075 zebraExplain_runNumberIncrement (zh->service->zei, 0);
1077 /* update store data */
1078 xfree (rec->info[recInfo_storeData]);
1079 if (store_data == 1)
1081 rec->size[recInfo_storeData] = recordAttr->recordSize;
1082 rec->info[recInfo_storeData] = (char *)
1083 xmalloc (recordAttr->recordSize);
1085 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1087 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1089 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1090 (long) recordOffset, fname);
1093 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1094 < recordAttr->recordSize)
1096 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1097 recordAttr->recordSize, fname);
1104 rec->info[recInfo_storeData] = NULL;
1105 rec->size[recInfo_storeData] = 0;
1107 /* update database name */
1108 xfree (rec->info[recInfo_databaseName]);
1109 rec->info[recInfo_databaseName] =
1110 rec_strdup (databaseName, &rec->size[recInfo_databaseName]);
1113 recordAttr->recordOffset = recordOffset;
1115 /* commit this record */
1116 rec_put (zh->service->records, &rec);
1121 int explain_extract (void *handle, Record rec, data1_node *n)
1123 ZebraHandle zh = (ZebraHandle) handle;
1124 struct recExtractCtrl extractCtrl;
1127 if (zebraExplain_curDatabase (zh->service->zei,
1128 rec->info[recInfo_databaseName]))
1131 if (zebraExplain_newDatabase (zh->service->zei,
1132 rec->info[recInfo_databaseName], 0))
1136 zh->keys.buf_used = 0;
1137 zh->keys.prevAttrUse = -1;
1138 zh->keys.prevAttrSet = -1;
1139 zh->keys.prevSeqNo = 0;
1142 extractCtrl.init = extract_init;
1143 extractCtrl.tokenAdd = extract_token_add;
1144 extractCtrl.schemaAdd = extract_schema_add;
1145 extractCtrl.dh = zh->service->dh;
1146 for (i = 0; i<256; i++)
1147 extractCtrl.seqno[i] = 0;
1148 extractCtrl.zebra_maps = zh->service->zebra_maps;
1149 extractCtrl.flagShowRecords = 0;
1150 extractCtrl.handle = handle;
1152 grs_extract_tree(&extractCtrl, n);
1154 logf (LOG_LOG, "flush explain record, sysno=%d", rec->sysno);
1156 if (rec->size[recInfo_delKeys])
1158 struct recKeys delkeys;
1159 struct sortKey *sortKeys = 0;
1161 delkeys.buf_used = rec->size[recInfo_delKeys];
1162 delkeys.buf = rec->info[recInfo_delKeys];
1163 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1164 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1166 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->keys);
1167 extract_flushSortKeys (zh, rec->sysno, 1, &zh->sortKeys);
1169 xfree (rec->info[recInfo_delKeys]);
1170 rec->size[recInfo_delKeys] = zh->keys.buf_used;
1171 rec->info[recInfo_delKeys] = zh->keys.buf;
1172 zh->keys.buf = NULL;
1173 zh->keys.buf_max = 0;
1177 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1178 int cmd, struct recKeys *reckeys)
1182 unsigned char attrSet = (unsigned char) -1;
1183 unsigned short attrUse = (unsigned short) -1;
1188 ZebraExplainInfo zei = zh->service->zei;
1192 int mem = 8*1024*1024;
1193 zh->key_buf = (char**) xmalloc (mem);
1194 zh->ptr_top = mem/sizeof(char*);
1196 zh->key_buf_used = 0;
1197 zh->key_file_no = 0;
1199 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1200 while (off < reckeys->buf_used)
1202 const char *src = reckeys->buf + off;
1211 memcpy (&ch, src, sizeof(ch));
1217 memcpy (&attrSet, src, sizeof(attrSet));
1218 src += sizeof(attrSet);
1222 memcpy (&attrUse, src, sizeof(attrUse));
1223 src += sizeof(attrUse);
1226 if (zh->key_buf_used + 1024 > (zh->ptr_top-zh->ptr_i)*sizeof(char*))
1227 extract_flushWriteKeys (zh);
1229 (zh->key_buf)[zh->ptr_top - zh->ptr_i] =
1230 (char*)zh->key_buf + zh->key_buf_used;
1233 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1235 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1239 key_SU_encode (ch,((char*)zh->key_buf) + zh->key_buf_used);
1242 ((char*)zh->key_buf) [(zh->key_buf_used)++] = *src++;
1244 ((char*)(zh->key_buf))[(zh->key_buf_used)++] = '\0';
1245 ((char*)(zh->key_buf))[(zh->key_buf_used)++] = cmd;
1248 seqno += ((lead>>2) & 15)-1;
1251 memcpy (&seqno, src, sizeof(seqno));
1252 src += sizeof(seqno);
1256 memcpy ((char*)zh->key_buf + zh->key_buf_used, &key, sizeof(key));
1257 (zh->key_buf_used) += sizeof(key);
1258 off = src - reckeys->buf;
1260 assert (off == reckeys->buf_used);
1263 void extract_flushWriteKeys (ZebraHandle zh)
1266 char out_fname[200];
1268 struct encode_info encode_info;
1269 int ptr_i = zh->ptr_i;
1273 if (!zh->key_buf || ptr_i <= 0)
1276 (zh->key_file_no)++;
1277 logf (LOG_LOG, "sorting section %d", (zh->key_file_no));
1279 qsort (zh->key_buf + zh->ptr_top - ptr_i, ptr_i, sizeof(char*),
1281 extract_get_fname_tmp (zh, out_fname, zh->key_file_no);
1283 if (!(outf = fopen (out_fname, "wb")))
1285 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1288 logf (LOG_LOG, "writing section %d", zh->key_file_no);
1289 prevcp = cp = (zh->key_buf)[zh->ptr_top - ptr_i];
1291 encode_key_init (&encode_info);
1292 encode_key_write (cp, &encode_info, outf);
1296 cp = (zh->key_buf)[zh->ptr_top - ptr_i];
1297 if (strcmp (cp, prevcp))
1299 encode_key_init (&encode_info);
1300 encode_key_write (cp, &encode_info, outf);
1304 encode_key_write (cp + strlen(cp), &encode_info, outf);
1307 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1308 extract_get_fname_tmp (out_fname, key_file_no);
1310 if (!(outf = fopen (out_fname, "wb")))
1312 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1315 logf (LOG_LOG, "writing section %d", key_file_no);
1317 prevcp = key_buf[ptr_top-i];
1319 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1321 key_y_len = strlen(prevcp)+1;
1323 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1324 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1326 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1327 sizeof(char*), key_y_compare);
1328 cp = key_buf[ptr_top-ptr_i];
1330 encode_key_init (&encode_info);
1331 encode_key_write (cp, &encode_info, outf);
1334 cp = key_buf[ptr_top-ptr_i];
1335 encode_key_write (cp+key_y_len, &encode_info, outf);
1339 prevcp = key_buf[ptr_top-ptr_i];
1344 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1347 logf (LOG_LOG, "finished section %d", zh->key_file_no);
1349 zh->key_buf_used = 0;
1352 void extract_add_index_string (RecWord *p, const char *string,
1356 unsigned char attrSet;
1357 unsigned short attrUse;
1360 int *pseqno = &p->seqnos[p->reg_type];
1361 ZebraHandle zh = p->extractCtrl->handle;
1362 ZebraExplainInfo zei = zh->service->zei;
1363 struct recKeys *keys = &zh->keys;
1365 if (keys->buf_used+1024 > keys->buf_max)
1369 b = (char *) xmalloc (keys->buf_max += 128000);
1370 if (keys->buf_used > 0)
1371 memcpy (b, keys->buf, keys->buf_used);
1375 dst = keys->buf + keys->buf_used;
1377 attrSet = p->attrSet;
1378 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1381 keys->prevAttrSet = attrSet;
1382 attrUse = p->attrUse;
1383 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1386 keys->prevAttrUse = attrUse;
1388 diff = 1 + *pseqno - keys->prevSeqNo;
1389 if (diff >= 1 && diff <= 15)
1390 lead |= (diff << 2);
1394 keys->prevSeqNo = *pseqno;
1401 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1404 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1405 yaz_log (LOG_LOG, "addSU set=%d use=%d SU=%d",
1406 attrSet, attrUse, ch);
1409 memcpy (dst, &ch, sizeof(ch));
1415 memcpy (dst, &attrSet, sizeof(attrSet));
1416 dst += sizeof(attrSet);
1420 memcpy (dst, &attrUse, sizeof(attrUse));
1421 dst += sizeof(attrUse);
1424 *dst++ = p->reg_type;
1425 memcpy (dst, string, length);
1431 memcpy (dst, pseqno, sizeof(*pseqno));
1432 dst += sizeof(*pseqno);
1434 keys->buf_used = dst - keys->buf;
1439 static void extract_add_sort_string (RecWord *p, const char *string,
1443 ZebraHandle zh = p->extractCtrl->handle;
1445 for (sk = zh->sortKeys; sk; sk = sk->next)
1446 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1449 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1450 sk->next = zh->sortKeys;
1453 sk->string = (char *) xmalloc (length);
1454 sk->length = length;
1455 memcpy (sk->string, string, length);
1457 sk->attrSet = p->attrSet;
1458 sk->attrUse = p->attrUse;
1461 void extract_add_string (RecWord *p, const char *string, int length)
1463 assert (length > 0);
1464 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1465 extract_add_sort_string (p, string, length);
1467 extract_add_index_string (p, string, length);
1470 static void extract_add_incomplete_field (RecWord *p)
1472 const char *b = p->string;
1473 int remain = p->length;
1474 const char **map = 0;
1477 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1481 char buf[IT_MAX_WORD+1];
1485 while (map && *map && **map == *CHR_SPACE)
1487 remain = p->length - (b - p->string);
1489 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1496 while (map && *map && **map != *CHR_SPACE)
1498 const char *cp = *map;
1500 while (i < IT_MAX_WORD && *cp)
1502 remain = p->length - (b - p->string);
1504 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1510 extract_add_string (p, buf, i);
1512 (p->seqnos[p->reg_type])++; /* to separate this from next one */
1515 static void extract_add_complete_field (RecWord *p)
1517 const char *b = p->string;
1518 char buf[IT_MAX_WORD+1];
1519 const char **map = 0;
1520 int i = 0, remain = p->length;
1523 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1525 while (remain > 0 && i < IT_MAX_WORD)
1527 while (map && *map && **map == *CHR_SPACE)
1529 remain = p->length - (b - p->string);
1531 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1538 if (i && i < IT_MAX_WORD)
1539 buf[i++] = *CHR_SPACE;
1540 while (map && *map && **map != *CHR_SPACE)
1542 const char *cp = *map;
1544 if (i >= IT_MAX_WORD)
1546 while (i < IT_MAX_WORD && *cp)
1548 remain = p->length - (b - p->string);
1550 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1558 extract_add_string (p, buf, i);
1561 void extract_token_add (RecWord *p)
1564 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1565 p->string, p->length)))
1567 p->string = wrbuf_buf(wrbuf);
1568 p->length = wrbuf_len(wrbuf);
1570 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1571 extract_add_complete_field (p);
1573 extract_add_incomplete_field(p);
1576 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1578 ZebraHandle zh = (ZebraHandle) (p->handle);
1579 zebraExplain_addSchema (zh->service->zei, oid);
1582 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1583 int cmd, struct sortKey **skp)
1585 struct sortKey *sk = *skp;
1586 SortIdx sortIdx = zh->service->sortIdx;
1588 sortIdx_sysno (sortIdx, sysno);
1591 struct sortKey *sk_next = sk->next;
1592 sortIdx_type (sortIdx, sk->attrUse);
1593 sortIdx_add (sortIdx, sk->string, sk->length);
1598 yaz_log (LOG_LOG, "extract_flushSortKeys");
1602 void encode_key_init (struct encode_info *i)
1609 char *encode_key_int (int d, char *bp)
1613 else if (d <= 16383)
1615 *bp++ = 64 + (d>>8);
1618 else if (d <= 4194303)
1620 *bp++ = 128 + (d>>16);
1621 *bp++ = (d>>8) & 255;
1626 *bp++ = 192 + (d>>24);
1627 *bp++ = (d>>16) & 255;
1628 *bp++ = (d>>8) & 255;
1634 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1639 while ((*bp++ = *k++))
1641 memcpy (&key, k+1, sizeof(struct it_key));
1642 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1643 if (i->sysno != key.sysno)
1645 i->sysno = key.sysno;
1648 else if (!i->seqno && !key.seqno && i->cmd == *k)
1650 bp = encode_key_int (key.seqno - i->seqno, bp);
1651 i->seqno = key.seqno;
1653 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1655 logf (LOG_FATAL|LOG_ERRNO, "fwrite");