2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: extract.c,v 1.113 2002-03-15 20:20:55 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 if (rec->size[recInfo_delKeys])
1157 struct recKeys delkeys;
1158 struct sortKey *sortKeys = 0;
1160 delkeys.buf_used = rec->size[recInfo_delKeys];
1161 delkeys.buf = rec->info[recInfo_delKeys];
1162 extract_flushSortKeys (zh, rec->sysno, 0, &sortKeys);
1163 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1165 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->keys);
1166 extract_flushSortKeys (zh, rec->sysno, 1, &zh->sortKeys);
1168 xfree (rec->info[recInfo_delKeys]);
1169 rec->size[recInfo_delKeys] = zh->keys.buf_used;
1170 rec->info[recInfo_delKeys] = zh->keys.buf;
1171 zh->keys.buf = NULL;
1172 zh->keys.buf_max = 0;
1176 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1177 int cmd, struct recKeys *reckeys)
1181 unsigned char attrSet = (unsigned char) -1;
1182 unsigned short attrUse = (unsigned short) -1;
1187 ZebraExplainInfo zei = zh->service->zei;
1191 int mem = 8*1024*1024;
1192 zh->key_buf = (char**) xmalloc (mem);
1193 zh->ptr_top = mem/sizeof(char*);
1195 zh->key_buf_used = 0;
1196 zh->key_file_no = 0;
1198 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1199 while (off < reckeys->buf_used)
1201 const char *src = reckeys->buf + off;
1210 memcpy (&ch, src, sizeof(ch));
1216 memcpy (&attrSet, src, sizeof(attrSet));
1217 src += sizeof(attrSet);
1221 memcpy (&attrUse, src, sizeof(attrUse));
1222 src += sizeof(attrUse);
1225 if (zh->key_buf_used + 1024 > (zh->ptr_top-zh->ptr_i)*sizeof(char*))
1226 extract_flushWriteKeys (zh);
1228 (zh->key_buf)[zh->ptr_top - zh->ptr_i] =
1229 (char*)zh->key_buf + zh->key_buf_used;
1232 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1234 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1238 key_SU_encode (ch,((char*)zh->key_buf) + zh->key_buf_used);
1241 ((char*)zh->key_buf) [(zh->key_buf_used)++] = *src++;
1243 ((char*)(zh->key_buf))[(zh->key_buf_used)++] = '\0';
1244 ((char*)(zh->key_buf))[(zh->key_buf_used)++] = cmd;
1247 seqno += ((lead>>2) & 15)-1;
1250 memcpy (&seqno, src, sizeof(seqno));
1251 src += sizeof(seqno);
1255 memcpy ((char*)zh->key_buf + zh->key_buf_used, &key, sizeof(key));
1256 (zh->key_buf_used) += sizeof(key);
1257 off = src - reckeys->buf;
1259 assert (off == reckeys->buf_used);
1262 void extract_flushWriteKeys (ZebraHandle zh)
1265 char out_fname[200];
1267 struct encode_info encode_info;
1268 int ptr_i = zh->ptr_i;
1272 if (!zh->key_buf || ptr_i <= 0)
1275 (zh->key_file_no)++;
1276 logf (LOG_LOG, "sorting section %d", (zh->key_file_no));
1278 qsort (zh->key_buf + zh->ptr_top - ptr_i, ptr_i, sizeof(char*),
1280 extract_get_fname_tmp (zh, out_fname, zh->key_file_no);
1282 if (!(outf = fopen (out_fname, "wb")))
1284 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1287 logf (LOG_LOG, "writing section %d", zh->key_file_no);
1288 prevcp = cp = (zh->key_buf)[zh->ptr_top - ptr_i];
1290 encode_key_init (&encode_info);
1291 encode_key_write (cp, &encode_info, outf);
1295 cp = (zh->key_buf)[zh->ptr_top - ptr_i];
1296 if (strcmp (cp, prevcp))
1298 encode_key_init (&encode_info);
1299 encode_key_write (cp, &encode_info, outf);
1303 encode_key_write (cp + strlen(cp), &encode_info, outf);
1306 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1307 extract_get_fname_tmp (out_fname, key_file_no);
1309 if (!(outf = fopen (out_fname, "wb")))
1311 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1314 logf (LOG_LOG, "writing section %d", key_file_no);
1316 prevcp = key_buf[ptr_top-i];
1318 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1320 key_y_len = strlen(prevcp)+1;
1322 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1323 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1325 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1326 sizeof(char*), key_y_compare);
1327 cp = key_buf[ptr_top-ptr_i];
1329 encode_key_init (&encode_info);
1330 encode_key_write (cp, &encode_info, outf);
1333 cp = key_buf[ptr_top-ptr_i];
1334 encode_key_write (cp+key_y_len, &encode_info, outf);
1338 prevcp = key_buf[ptr_top-ptr_i];
1343 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1346 logf (LOG_LOG, "finished section %d", zh->key_file_no);
1348 zh->key_buf_used = 0;
1351 void extract_add_index_string (RecWord *p, const char *string,
1355 unsigned char attrSet;
1356 unsigned short attrUse;
1359 int *pseqno = &p->seqnos[p->reg_type];
1360 ZebraHandle zh = p->extractCtrl->handle;
1361 ZebraExplainInfo zei = zh->service->zei;
1362 struct recKeys *keys = &zh->keys;
1364 if (keys->buf_used+1024 > keys->buf_max)
1368 b = (char *) xmalloc (keys->buf_max += 128000);
1369 if (keys->buf_used > 0)
1370 memcpy (b, keys->buf, keys->buf_used);
1374 dst = keys->buf + keys->buf_used;
1376 attrSet = p->attrSet;
1377 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1380 keys->prevAttrSet = attrSet;
1381 attrUse = p->attrUse;
1382 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1385 keys->prevAttrUse = attrUse;
1387 diff = 1 + *pseqno - keys->prevSeqNo;
1388 if (diff >= 1 && diff <= 15)
1389 lead |= (diff << 2);
1393 keys->prevSeqNo = *pseqno;
1400 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1403 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1404 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1405 attrSet, attrUse, ch);
1408 memcpy (dst, &ch, sizeof(ch));
1414 memcpy (dst, &attrSet, sizeof(attrSet));
1415 dst += sizeof(attrSet);
1419 memcpy (dst, &attrUse, sizeof(attrUse));
1420 dst += sizeof(attrUse);
1423 *dst++ = p->reg_type;
1424 memcpy (dst, string, length);
1430 memcpy (dst, pseqno, sizeof(*pseqno));
1431 dst += sizeof(*pseqno);
1433 keys->buf_used = dst - keys->buf;
1438 static void extract_add_sort_string (RecWord *p, const char *string,
1442 ZebraHandle zh = p->extractCtrl->handle;
1444 for (sk = zh->sortKeys; sk; sk = sk->next)
1445 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
1448 sk = (struct sortKey *) xmalloc (sizeof(*sk));
1449 sk->next = zh->sortKeys;
1452 sk->string = (char *) xmalloc (length);
1453 sk->length = length;
1454 memcpy (sk->string, string, length);
1456 sk->attrSet = p->attrSet;
1457 sk->attrUse = p->attrUse;
1460 void extract_add_string (RecWord *p, const char *string, int length)
1462 assert (length > 0);
1463 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1464 extract_add_sort_string (p, string, length);
1466 extract_add_index_string (p, string, length);
1469 static void extract_add_incomplete_field (RecWord *p)
1471 const char *b = p->string;
1472 int remain = p->length;
1473 const char **map = 0;
1476 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1480 char buf[IT_MAX_WORD+1];
1484 while (map && *map && **map == *CHR_SPACE)
1486 remain = p->length - (b - p->string);
1488 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1495 while (map && *map && **map != *CHR_SPACE)
1497 const char *cp = *map;
1499 while (i < IT_MAX_WORD && *cp)
1501 remain = p->length - (b - p->string);
1503 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1509 extract_add_string (p, buf, i);
1511 (p->seqnos[p->reg_type])++; /* to separate this from next one */
1514 static void extract_add_complete_field (RecWord *p)
1516 const char *b = p->string;
1517 char buf[IT_MAX_WORD+1];
1518 const char **map = 0;
1519 int i = 0, remain = p->length;
1522 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1524 while (remain > 0 && i < IT_MAX_WORD)
1526 while (map && *map && **map == *CHR_SPACE)
1528 remain = p->length - (b - p->string);
1530 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1537 if (i && i < IT_MAX_WORD)
1538 buf[i++] = *CHR_SPACE;
1539 while (map && *map && **map != *CHR_SPACE)
1541 const char *cp = *map;
1543 if (i >= IT_MAX_WORD)
1545 while (i < IT_MAX_WORD && *cp)
1547 remain = p->length - (b - p->string);
1549 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1557 extract_add_string (p, buf, i);
1560 void extract_token_add (RecWord *p)
1563 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1564 p->string, p->length)))
1566 p->string = wrbuf_buf(wrbuf);
1567 p->length = wrbuf_len(wrbuf);
1569 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1570 extract_add_complete_field (p);
1572 extract_add_incomplete_field(p);
1575 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1577 ZebraHandle zh = (ZebraHandle) (p->handle);
1578 zebraExplain_addSchema (zh->service->zei, oid);
1581 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1582 int cmd, struct sortKey **skp)
1584 struct sortKey *sk = *skp;
1585 SortIdx sortIdx = zh->service->sortIdx;
1587 sortIdx_sysno (sortIdx, sysno);
1590 struct sortKey *sk_next = sk->next;
1591 sortIdx_type (sortIdx, sk->attrUse);
1592 sortIdx_add (sortIdx, sk->string, sk->length);
1600 void encode_key_init (struct encode_info *i)
1607 char *encode_key_int (int d, char *bp)
1611 else if (d <= 16383)
1613 *bp++ = 64 + (d>>8);
1616 else if (d <= 4194303)
1618 *bp++ = 128 + (d>>16);
1619 *bp++ = (d>>8) & 255;
1624 *bp++ = 192 + (d>>24);
1625 *bp++ = (d>>16) & 255;
1626 *bp++ = (d>>8) & 255;
1632 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1637 while ((*bp++ = *k++))
1639 memcpy (&key, k+1, sizeof(struct it_key));
1640 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1641 if (i->sysno != key.sysno)
1643 i->sysno = key.sysno;
1646 else if (!i->seqno && !key.seqno && i->cmd == *k)
1648 bp = encode_key_int (key.seqno - i->seqno, bp);
1649 i->seqno = key.seqno;
1651 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1653 logf (LOG_FATAL|LOG_ERRNO, "fwrite");