1 /* $Id: extract.c,v 1.211 2006-05-10 13:54:01 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
39 #if _FILE_OFFSET_BITS == 64
40 #define PRINTF_OFF_T "%Ld"
42 #define PRINTF_OFF_T "%ld"
45 #define USE_SHELLSORT 0
48 static void shellsort(void *ar, int r, size_t s,
49 int (*cmp)(const void *a, const void *b))
54 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
55 13776, 4592, 1968, 861, 336,
56 112, 48, 21, 7, 3, 1 };
57 for ( k = 0; k < 16; k++)
58 for (h = incs[k], i = h; i < r; i++)
62 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
64 memcpy (a + s*j, a + s*(j-h), s);
72 static void logRecord (ZebraHandle zh)
74 ++zh->records_processed;
75 if (!(zh->records_processed % 1000))
77 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
78 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
79 zh->records_processed, zh->records_inserted, zh->records_updated,
84 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
86 static void extract_init (struct recExtractCtrl *p, RecWord *w)
88 w->zebra_maps = p->zebra_maps;
92 w->attrSet = VAL_BIB1;
102 static void searchRecordKey(ZebraHandle zh,
103 zebra_rec_keys_t reckeys,
104 int attrSetS, int attrUseS,
105 const char **ws, int ws_length)
110 for (i = 0; i<ws_length; i++)
113 ch = zebraExplain_lookup_attr_su_any_index(zh->reg->zei,
118 if (zebra_rec_keys_rewind(reckeys))
125 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
127 assert(key.len <= 4 && key.len > 2);
129 seqno = key.mem[key.len-1];
131 if (key.mem[0] == ch)
137 woff = seqno - startSeq;
138 if (woff >= 0 && woff < ws_length)
145 struct file_read_info {
146 off_t file_max; /* maximum offset so far */
147 off_t file_offset; /* current offset */
148 off_t file_moffset; /* offset of rec/rec boundary */
153 static struct file_read_info *file_read_start (int fd)
155 struct file_read_info *fi = (struct file_read_info *)
156 xmalloc (sizeof(*fi));
160 fi->file_moffset = 0;
166 static void file_read_stop (struct file_read_info *fi)
171 static off_t file_seek (void *handle, off_t offset)
173 struct file_read_info *p = (struct file_read_info *) handle;
174 p->file_offset = offset;
175 return lseek (p->fd, offset, SEEK_SET);
178 static off_t file_tell (void *handle)
180 struct file_read_info *p = (struct file_read_info *) handle;
181 return p->file_offset;
184 static int file_read (void *handle, char *buf, size_t count)
186 struct file_read_info *p = (struct file_read_info *) handle;
189 r = read (fd, buf, count);
193 if (p->file_offset > p->file_max)
194 p->file_max = p->file_offset;
199 static void file_end (void *handle, off_t offset)
201 struct file_read_info *p = (struct file_read_info *) handle;
203 if (offset != p->file_moffset)
205 p->file_moffset = offset;
210 #define FILE_MATCH_BLANK "\t "
212 static char *fileMatchStr (ZebraHandle zh,
213 zebra_rec_keys_t reckeys,
214 const char *fname, const char *spec)
216 static char dstBuf[2048]; /* static here ??? */
218 const char *s = spec;
222 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
229 char attset_str[64], attname_str[64];
230 data1_attset *attset;
232 int attSet = 1, attUse = 1;
235 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
237 for (i = 0; *s && *s != ',' && *s != ')' &&
238 !strchr(FILE_MATCH_BLANK, *s); s++)
239 if (i+1 < sizeof(attset_str))
240 attset_str[i++] = *s;
241 attset_str[i] = '\0';
243 for (; strchr(FILE_MATCH_BLANK, *s); s++)
247 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
249 for (i = 0; *s && *s != ')' &&
250 !strchr(FILE_MATCH_BLANK, *s); s++)
251 if (i+1 < sizeof(attname_str))
252 attname_str[i++] = *s;
253 attname_str[i] = '\0';
256 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
259 attSet = attset->reference;
260 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
264 attUse = atoi (attname_str);
266 searchRecordKey (zh, reckeys, attSet, attUse, ws, 32);
270 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
271 spec, zh->m_group ? zh->m_group : "none");
276 for (i = 0; i<32; i++)
285 dst += strlen(ws[i]);
289 yaz_log (YLOG_WARN, "Record didn't contain match"
290 " fields in (%s,%s)", attset_str, attname_str);
298 const char *spec_src = NULL;
299 const char *s1 = ++s;
300 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
304 if (spec_len > sizeof(special)-1)
305 spec_len = sizeof(special)-1;
306 memcpy (special, s, spec_len);
307 special[spec_len] = '\0';
310 if (!strcmp (special, "group"))
311 spec_src = zh->m_group;
312 else if (!strcmp (special, "database"))
313 spec_src = zh->basenames[0];
314 else if (!strcmp (special, "filename")) {
317 else if (!strcmp (special, "type"))
318 spec_src = zh->m_record_type;
323 strcpy (dst, spec_src);
324 dst += strlen (spec_src);
327 else if (*s == '\"' || *s == '\'')
329 int stopMarker = *s++;
333 while (*s && *s != stopMarker)
335 if (i+1 < sizeof(tmpString))
336 tmpString[i++] = *s++;
341 strcpy (dst, tmpString);
342 dst += strlen (tmpString);
346 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
347 spec, zh->m_group ? zh->m_group : "none");
354 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
355 fname, zh->m_group ? zh->m_group : "none");
362 struct recordLogInfo {
365 struct recordGroup *rGroup;
368 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
371 for (i = 0; i<256; i++)
373 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
378 ctrl->zebra_maps = zh->reg->zebra_maps;
379 ctrl->flagShowRecords = !zh->m_flag_rw;
382 static ZEBRA_RES file_extract_record(ZebraHandle zh,
383 SYSNO *sysno, const char *fname,
385 struct file_read_info *fi,
388 void *recTypeClientData)
390 const char *match_str_to_print = "";
391 RecordAttr *recordAttr;
393 const char *matchStr = 0;
396 off_t recordOffset = 0;
397 struct recExtractCtrl extractCtrl;
399 /* announce database */
400 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
402 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
403 zh->m_explain_database))
409 /* we are going to read from a file, so prepare the extraction */
410 zebra_rec_keys_reset(zh->reg->keys);
413 zebra_rec_keys_reset(zh->reg->sortKeys);
415 zh->reg->sortKeys.buf_used = 0;
417 recordOffset = fi->file_moffset;
418 extractCtrl.handle = zh;
419 extractCtrl.offset = fi->file_moffset;
420 extractCtrl.readf = file_read;
421 extractCtrl.seekf = file_seek;
422 extractCtrl.tellf = file_tell;
423 extractCtrl.endf = file_end;
425 extractCtrl.init = extract_init;
426 extractCtrl.tokenAdd = extract_token_add;
427 extractCtrl.schemaAdd = extract_schema_add;
428 extractCtrl.dh = zh->reg->dh;
429 extractCtrl.match_criteria[0] = '\0';
430 extractCtrl.staticrank = 0;
432 extractCtrl.first_record = fi->file_offset ? 0 : 1;
434 extract_set_store_data_prepare(&extractCtrl);
436 init_extractCtrl(zh, &extractCtrl);
439 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
443 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
444 yaz_log_init_prefix2 (msg);
447 r = (*recType->extract)(recTypeClientData, &extractCtrl);
449 yaz_log_init_prefix2 (0);
450 if (r == RECCTRL_EXTRACT_EOF)
452 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
454 /* error occured during extraction ... */
456 zh->records_processed < zh->m_file_verbose_limit)
458 yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
459 fname, recordOffset);
463 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
465 /* error occured during extraction ... */
467 zh->records_processed < zh->m_file_verbose_limit)
469 yaz_log (YLOG_WARN, "no filter for %s %s "
470 PRINTF_OFF_T, zh->m_record_type,
471 fname, recordOffset);
475 if (extractCtrl.match_criteria[0])
476 matchStr = extractCtrl.match_criteria;
479 /* if matchStr is set now - we assume it's printable .
480 For internal matchStr (see below) we don't print */
482 match_str_to_print = matchStr;
484 /* perform internal match if sysno not known and if match criteria is
491 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
493 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
497 yaz_log(YLOG_WARN, "Bad match criteria");
503 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
504 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
508 assert(*rinfo == sizeof(*sysno));
509 memcpy (sysno, rinfo+1, sizeof(*sysno));
513 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
515 /* the extraction process returned no information - the record
516 is probably empty - unless flagShowRecords is in use */
520 if (zh->records_processed < zh->m_file_verbose_limit)
521 yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
522 fname, recordOffset);
531 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
532 fname, recordOffset);
533 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
537 rec = rec_new (zh->reg->records);
541 if (zh->records_processed < zh->m_file_verbose_limit)
543 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
544 " " ZINT_FORMAT " %s" ,
546 fname, recordOffset, *sysno, match_str_to_print);
548 recordAttr = rec_init_attr (zh->reg->zei, rec);
549 recordAttr->staticrank = extractCtrl.staticrank;
553 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
554 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
555 sizeof(*sysno), sysno);
560 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
562 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
564 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
565 recordAttr->staticrank);
566 zh->records_inserted++;
570 /* record already exists */
571 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
574 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
576 struct sortKeys sortKeys;
579 rec = rec_get (zh->reg->records, *sysno);
582 recordAttr = rec_init_attr (zh->reg->zei, rec);
584 zebra_rec_keys_set_buf(delkeys,
585 rec->info[recInfo_delKeys],
586 rec->size[recInfo_delKeys],
590 zebra_rec_keys_set_buf(sortKeys,
591 rec->info[recInfo_sortKeys],
592 rec->size[recInfo_sortKeys],
594 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
596 sortKeys.buf_used = rec->size[recInfo_sortKeys];
597 sortKeys.buf = rec->info[recInfo_sortKeys];
598 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
601 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
602 recordAttr->staticrank); /* old values */
605 /* record going to be deleted */
606 if (zebra_rec_keys_empty(delkeys))
608 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T
610 zh->m_record_type, fname, recordOffset, *sysno);
611 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
615 if (zh->records_processed < zh->m_file_verbose_limit)
617 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
618 " " ZINT_FORMAT " %s" ,
620 fname, recordOffset, *sysno, match_str_to_print);
622 zh->records_deleted++;
625 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
626 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
628 rec_del (zh->reg->records, &rec);
636 /* flush new keys for sort&search etc */
637 if (zh->records_processed < zh->m_file_verbose_limit)
639 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
640 " " ZINT_FORMAT " %s" ,
642 fname, recordOffset, *sysno, match_str_to_print);
644 recordAttr->staticrank = extractCtrl.staticrank;
646 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
648 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
650 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
651 recordAttr->staticrank);
652 zh->records_updated++;
654 zebra_rec_keys_close(delkeys);
656 zebra_rec_keys_close(sortKeys);
659 /* update file type */
660 xfree (rec->info[recInfo_fileType]);
661 rec->info[recInfo_fileType] =
662 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
664 /* update filename */
665 xfree (rec->info[recInfo_filename]);
666 rec->info[recInfo_filename] =
667 rec_strdup (fname, &rec->size[recInfo_filename]);
669 /* update delete keys */
670 xfree (rec->info[recInfo_delKeys]);
671 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
673 zebra_rec_keys_get_buf(zh->reg->keys,
674 &rec->info[recInfo_delKeys],
675 &rec->size[recInfo_delKeys]);
679 rec->info[recInfo_delKeys] = NULL;
680 rec->size[recInfo_delKeys] = 0;
683 /* update sort keys */
684 xfree (rec->info[recInfo_sortKeys]);
687 zebra_rec_keys_get_buf(zh->reg->sortKeys,
688 &rec->info[recInfo_sortKeys],
689 &rec->size[recInfo_sortKeys]);
691 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
692 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
693 zh->reg->sortKeys.buf = NULL;
694 zh->reg->sortKeys.buf_max = 0;
697 /* save file size of original record */
698 zebraExplain_recordBytesIncrement (zh->reg->zei,
699 - recordAttr->recordSize);
700 recordAttr->recordSize = fi->file_moffset - recordOffset;
701 if (!recordAttr->recordSize)
702 recordAttr->recordSize = fi->file_max - recordOffset;
703 zebraExplain_recordBytesIncrement (zh->reg->zei,
704 recordAttr->recordSize);
706 /* set run-number for this record */
707 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
710 /* update store data */
711 xfree (rec->info[recInfo_storeData]);
712 if (zh->store_data_buf)
714 rec->size[recInfo_storeData] = zh->store_data_size;
715 rec->info[recInfo_storeData] = zh->store_data_buf;
716 zh->store_data_buf = 0;
718 else if (zh->m_store_data)
720 rec->size[recInfo_storeData] = recordAttr->recordSize;
721 rec->info[recInfo_storeData] = (char *)
722 xmalloc (recordAttr->recordSize);
723 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
725 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
726 recordOffset, fname);
729 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
730 < recordAttr->recordSize)
732 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
733 recordAttr->recordSize, fname);
739 rec->info[recInfo_storeData] = NULL;
740 rec->size[recInfo_storeData] = 0;
742 /* update database name */
743 xfree (rec->info[recInfo_databaseName]);
744 rec->info[recInfo_databaseName] =
745 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
748 recordAttr->recordOffset = recordOffset;
750 /* commit this record */
751 rec_put (zh->reg->records, &rec);
756 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
759 ZEBRA_RES r = ZEBRA_OK;
764 struct file_read_info *fi;
765 const char *original_record_type = 0;
767 void *recTypeClientData;
769 if (!zh->m_group || !*zh->m_group)
772 sprintf (gprefix, "%s.", zh->m_group);
774 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
776 /* determine file extension */
778 for (i = strlen(fname); --i >= 0; )
781 else if (fname[i] == '.')
783 strcpy (ext, fname+i+1);
786 /* determine file type - depending on extension */
787 original_record_type = zh->m_record_type;
788 if (!zh->m_record_type)
790 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
791 zh->m_record_type = res_get (zh->res, ext_res);
793 if (!zh->m_record_type)
795 if (zh->records_processed < zh->m_file_verbose_limit)
796 yaz_log (YLOG_LOG, "? %s", fname);
799 /* determine match criteria */
800 if (!zh->m_record_id)
802 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
803 zh->m_record_id = res_get (zh->res, ext_res);
807 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
808 &recTypeClientData)))
810 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
814 switch(recType->version)
819 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
821 if (sysno && deleteFlag)
827 if (zh->path_reg && !yaz_is_abspath (fname))
829 strcpy (full_rep, zh->path_reg);
830 strcat (full_rep, "/");
831 strcat (full_rep, fname);
834 strcpy (full_rep, fname);
836 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
838 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
839 zh->m_record_type = original_record_type;
843 fi = file_read_start (fd);
846 fi->file_moffset = fi->file_offset;
847 fi->file_more = 0; /* file_end not called (yet) */
848 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
849 recType, recTypeClientData);
851 { /* file_end has been called so reset offset .. */
852 fi->file_offset = fi->file_moffset;
853 lseek(fi->fd, fi->file_moffset, SEEK_SET);
867 zh->m_record_type = original_record_type;
872 If sysno is provided, then it's used to identify the reocord.
873 If not, and match_criteria is provided, then sysno is guessed
874 If not, and a record is provided, then sysno is got from there
877 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
878 const char *buf, size_t buf_size,
881 const char *recordType,
883 const char *match_criteria,
889 RecordAttr *recordAttr;
890 struct recExtractCtrl extractCtrl;
892 const char *matchStr = 0;
893 RecType recType = NULL;
896 long recordOffset = 0;
897 struct zebra_fetch_control fc;
898 const char *pr_fname = fname; /* filename to print .. */
899 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
902 pr_fname = "<no file>"; /* make it printable if file is omitted */
905 fc.record_int_buf = buf;
906 fc.record_int_len = buf_size;
907 fc.record_int_pos = 0;
909 fc.record_offset = 0;
911 extractCtrl.offset = 0;
912 extractCtrl.readf = zebra_record_int_read;
913 extractCtrl.seekf = zebra_record_int_seek;
914 extractCtrl.tellf = zebra_record_int_tell;
915 extractCtrl.endf = zebra_record_int_end;
916 extractCtrl.first_record = 1;
917 extractCtrl.fh = &fc;
919 zebra_rec_keys_reset(zh->reg->keys);
922 zebra_rec_keys_reset(zh->reg->sortKeys);
924 zh->reg->sortKeys.buf_used = 0;
926 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
928 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
929 zh->m_explain_database))
933 if (recordType && *recordType)
935 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
936 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
941 if (!(zh->m_record_type))
943 yaz_log (YLOG_WARN, "No such record type defined");
946 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
947 recType = recType_byName (zh->reg->recTypes, zh->res,
948 zh->m_record_type, &clientData);
949 recordType = zh->m_record_type;
954 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
958 extractCtrl.init = extract_init;
959 extractCtrl.tokenAdd = extract_token_add;
960 extractCtrl.schemaAdd = extract_schema_add;
961 extractCtrl.dh = zh->reg->dh;
962 extractCtrl.handle = zh;
963 extractCtrl.match_criteria[0] = '\0';
964 extractCtrl.staticrank = 0;
966 init_extractCtrl(zh, &extractCtrl);
968 extract_set_store_data_prepare(&extractCtrl);
970 r = (*recType->extract)(clientData, &extractCtrl);
972 if (r == RECCTRL_EXTRACT_EOF)
974 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
976 /* error occured during extraction ... */
977 yaz_log (YLOG_WARN, "extract error: generic");
980 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
982 /* error occured during extraction ... */
983 yaz_log (YLOG_WARN, "extract error: no such filter");
987 if (extractCtrl.match_criteria[0])
988 match_criteria = extractCtrl.match_criteria;
994 if (match_criteria && *match_criteria) {
995 matchStr = match_criteria;
997 if (zh->m_record_id && *zh->m_record_id) {
998 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1002 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1009 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1010 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1014 assert(*rinfo == sizeof(*sysno));
1015 memcpy (sysno, rinfo+1, sizeof(*sysno));
1019 if (zebra_rec_keys_empty(zh->reg->keys))
1021 /* the extraction process returned no information - the record
1022 is probably empty - unless flagShowRecords is in use */
1032 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1033 pr_fname, (long) recordOffset);
1034 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1038 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1039 (long) recordOffset);
1040 rec = rec_new (zh->reg->records);
1042 *sysno = rec->sysno;
1044 recordAttr = rec_init_attr (zh->reg->zei, rec);
1045 recordAttr->staticrank = extractCtrl.staticrank;
1049 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1050 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1051 sizeof(*sysno), sysno);
1056 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1058 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1062 print_rec_keys(zh, zh->reg->keys);
1064 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1065 recordAttr->staticrank);
1066 zh->records_inserted++;
1070 /* record already exists */
1071 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1073 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1075 struct sortKeys sortKeys;
1080 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1081 recordType, pr_fname, (long) recordOffset);
1086 rec = rec_get (zh->reg->records, *sysno);
1089 recordAttr = rec_init_attr (zh->reg->zei, rec);
1091 zebra_rec_keys_set_buf(delkeys,
1092 rec->info[recInfo_delKeys],
1093 rec->size[recInfo_delKeys],
1096 zebra_rec_keys_set_buf(sortKeys,
1097 rec->info[recInfo_sortKeys],
1098 rec->size[recInfo_sortKeys],
1101 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1102 sortKeys.buf = rec->info[recInfo_sortKeys];
1106 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1108 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1110 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1111 recordAttr->staticrank);
1114 /* record going to be deleted */
1115 if (zebra_rec_keys_empty(delkeys))
1117 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1118 pr_fname, (long) recordOffset);
1119 yaz_log (YLOG_WARN, "cannot delete file above, "
1120 "storeKeys false (3)");
1125 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1126 pr_fname, (long) recordOffset);
1127 zh->records_deleted++;
1130 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1131 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1133 rec_del (zh->reg->records, &rec);
1142 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1143 pr_fname, (long) recordOffset);
1144 recordAttr->staticrank = extractCtrl.staticrank;
1146 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1148 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1150 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1151 recordAttr->staticrank);
1152 zh->records_updated++;
1154 zebra_rec_keys_close(delkeys);
1156 zebra_rec_keys_close(sortKeys);
1159 /* update file type */
1160 xfree (rec->info[recInfo_fileType]);
1161 rec->info[recInfo_fileType] =
1162 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1164 /* update filename */
1165 xfree (rec->info[recInfo_filename]);
1166 rec->info[recInfo_filename] =
1167 rec_strdup (fname, &rec->size[recInfo_filename]);
1169 /* update delete keys */
1170 xfree (rec->info[recInfo_delKeys]);
1171 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1173 zebra_rec_keys_get_buf(zh->reg->keys,
1174 &rec->info[recInfo_delKeys],
1175 &rec->size[recInfo_delKeys]);
1179 rec->info[recInfo_delKeys] = NULL;
1180 rec->size[recInfo_delKeys] = 0;
1182 /* update sort keys */
1183 xfree (rec->info[recInfo_sortKeys]);
1186 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1187 &rec->info[recInfo_sortKeys],
1188 &rec->size[recInfo_sortKeys]);
1190 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1191 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1192 zh->reg->sortKeys.buf = NULL;
1193 zh->reg->sortKeys.buf_max = 0;
1196 /* save file size of original record */
1197 zebraExplain_recordBytesIncrement (zh->reg->zei,
1198 - recordAttr->recordSize);
1200 recordAttr->recordSize = fi->file_moffset - recordOffset;
1201 if (!recordAttr->recordSize)
1202 recordAttr->recordSize = fi->file_max - recordOffset;
1204 recordAttr->recordSize = buf_size;
1206 zebraExplain_recordBytesIncrement (zh->reg->zei,
1207 recordAttr->recordSize);
1209 /* set run-number for this record */
1210 recordAttr->runNumber =
1211 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1213 /* update store data */
1214 xfree (rec->info[recInfo_storeData]);
1216 /* update store data */
1217 if (zh->store_data_buf)
1219 rec->size[recInfo_storeData] = zh->store_data_size;
1220 rec->info[recInfo_storeData] = zh->store_data_buf;
1221 zh->store_data_buf = 0;
1223 else if (zh->m_store_data)
1225 rec->size[recInfo_storeData] = recordAttr->recordSize;
1226 rec->info[recInfo_storeData] = (char *)
1227 xmalloc (recordAttr->recordSize);
1228 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1232 rec->info[recInfo_storeData] = NULL;
1233 rec->size[recInfo_storeData] = 0;
1235 /* update database name */
1236 xfree (rec->info[recInfo_databaseName]);
1237 rec->info[recInfo_databaseName] =
1238 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1241 recordAttr->recordOffset = recordOffset;
1243 /* commit this record */
1244 rec_put (zh->reg->records, &rec);
1249 int explain_extract (void *handle, Record rec, data1_node *n)
1251 ZebraHandle zh = (ZebraHandle) handle;
1252 struct recExtractCtrl extractCtrl;
1254 if (zebraExplain_curDatabase (zh->reg->zei,
1255 rec->info[recInfo_databaseName]))
1258 if (zebraExplain_newDatabase (zh->reg->zei,
1259 rec->info[recInfo_databaseName], 0))
1263 zebra_rec_keys_reset(zh->reg->keys);
1266 zebra_rec_keys_reset(zh->reg->sortKeys);
1268 zh->reg->sortKeys.buf_used = 0;
1270 extractCtrl.init = extract_init;
1271 extractCtrl.tokenAdd = extract_token_add;
1272 extractCtrl.schemaAdd = extract_schema_add;
1273 extractCtrl.dh = zh->reg->dh;
1275 init_extractCtrl(zh, &extractCtrl);
1277 extractCtrl.flagShowRecords = 0;
1278 extractCtrl.match_criteria[0] = '\0';
1279 extractCtrl.staticrank = 0;
1280 extractCtrl.handle = handle;
1281 extractCtrl.first_record = 1;
1283 extract_set_store_data_prepare(&extractCtrl);
1286 grs_extract_tree(&extractCtrl, n);
1288 if (rec->size[recInfo_delKeys])
1290 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1293 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1295 struct sortKeys sortkeys;
1298 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1299 rec->size[recInfo_delKeys],
1301 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1302 zebra_rec_keys_close(delkeys);
1304 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1305 rec->size[recInfo_sortKeys],
1308 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1309 zebra_rec_keys_close(sortkeys);
1311 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1312 sortkeys.buf = rec->info[recInfo_sortKeys];
1313 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1316 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1318 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1320 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1323 xfree (rec->info[recInfo_delKeys]);
1324 zebra_rec_keys_get_buf(zh->reg->keys,
1325 &rec->info[recInfo_delKeys],
1326 &rec->size[recInfo_delKeys]);
1328 xfree (rec->info[recInfo_sortKeys]);
1330 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1331 &rec->info[recInfo_sortKeys],
1332 &rec->size[recInfo_sortKeys]);
1334 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1335 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1336 zh->reg->sortKeys.buf = NULL;
1337 zh->reg->sortKeys.buf_max = 0;
1343 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1344 zebra_rec_keys_t reckeys)
1346 ZebraExplainInfo zei = zh->reg->zei;
1350 struct ord_stat *next;
1353 if (zebra_rec_keys_rewind(reckeys))
1355 struct ord_stat *ord_list = 0;
1359 struct it_key key_in;
1360 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1362 int ord = key_in.mem[0];
1364 for (p = ord_list; p ; p = p->next)
1372 p = xmalloc(sizeof(*p));
1383 struct ord_stat *p1 = p;
1386 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1388 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1395 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1397 zebra_rec_keys_t reckeys,
1400 ZebraExplainInfo zei = zh->reg->zei;
1402 extract_rec_keys_adjust(zh, cmd, reckeys);
1404 if (!zh->reg->key_buf)
1406 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1409 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1412 /* FIXME: That "8" should be in a default settings include */
1413 /* not hard-coded here! -H */
1414 zh->reg->key_buf = (char**) xmalloc (mem);
1415 zh->reg->ptr_top = mem/sizeof(char*);
1417 zh->reg->key_buf_used = 0;
1418 zh->reg->key_file_no = 0;
1420 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1422 if (zebra_rec_keys_rewind(reckeys))
1426 struct it_key key_in;
1427 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1430 struct it_key key_out;
1431 zint *keyp = key_out.mem;
1433 assert(key_in.len == 4);
1435 /* check for buffer overflow */
1436 if (zh->reg->key_buf_used + 1024 >
1437 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1438 extract_flushWriteKeys (zh, 0);
1441 assert(zh->reg->ptr_i > 0);
1442 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1443 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1445 /* encode the ordinal value (field/use/attribute) .. */
1446 ch = (int) key_in.mem[0];
1447 zh->reg->key_buf_used +=
1448 key_SU_encode(ch, (char*)zh->reg->key_buf +
1449 zh->reg->key_buf_used);
1451 /* copy the 0-terminated stuff from str to output */
1452 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1453 zh->reg->key_buf_used += slen;
1454 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1456 /* the delete/insert indicator */
1457 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1459 if (zh->m_staticrank) /* rank config enabled ? */
1463 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1467 *keyp++ = staticrank;
1473 if (key_in.mem[1]) /* filter specified record ID */
1474 *keyp++ = key_in.mem[1];
1477 *keyp++ = key_in.mem[2]; /* section_id */
1478 *keyp++ = key_in.mem[3]; /* sequence .. */
1480 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1481 &key_out, sizeof(key_out));
1482 (zh->reg->key_buf_used) += sizeof(key_out);
1487 void extract_flushWriteKeys (ZebraHandle zh, int final)
1488 /* optimizing: if final=1, and no files written yet */
1489 /* push the keys directly to merge, sidestepping the */
1490 /* temp file altogether. Speeds small updates */
1493 char out_fname[200];
1495 struct encode_info encode_info;
1496 int ptr_i = zh->reg->ptr_i;
1501 if (!zh->reg->key_buf || ptr_i <= 0)
1503 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1504 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1505 yaz_log (YLOG_DEBUG, " buf=%p ",
1507 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1508 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1513 (zh->reg->key_file_no)++;
1514 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1515 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1516 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1518 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1519 sizeof(char*), key_qsort_compare);
1521 /* zebra.cfg: tempfiles:
1522 Y: always use temp files (old way)
1523 A: use temp files, if more than one (auto)
1524 = if this is both the last and the first
1525 N: never bother with temp files (new) */
1527 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1528 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1529 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1534 if ( ( temp_policy =='N' ) || /* always from memory */
1535 ( ( temp_policy =='A' ) && /* automatic */
1536 (zh->reg->key_file_no == 1) && /* this is first time */
1537 (final) ) ) /* and last (=only) time */
1538 { /* go directly from memory */
1539 zh->reg->key_file_no =0; /* signal not to read files */
1540 zebra_index_merge(zh);
1542 zh->reg->key_buf_used = 0;
1546 /* Not doing directly from memory, write into a temp file */
1547 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1549 if (!(outf = fopen (out_fname, "wb")))
1551 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1554 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1555 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1557 encode_key_init (&encode_info);
1558 encode_key_write (cp, &encode_info, outf);
1562 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1563 if (strcmp (cp, prevcp))
1565 encode_key_flush ( &encode_info, outf);
1566 encode_key_init (&encode_info);
1567 encode_key_write (cp, &encode_info, outf);
1571 encode_key_write (cp + strlen(cp), &encode_info, outf);
1573 encode_key_flush ( &encode_info, outf);
1575 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1576 extract_get_fname_tmp (out_fname, key_file_no);
1578 if (!(outf = fopen (out_fname, "wb")))
1580 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1583 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1585 prevcp = key_buf[ptr_top-i];
1587 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1589 key_y_len = strlen(prevcp)+1;
1591 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1592 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1594 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1595 sizeof(char*), key_y_compare);
1596 cp = key_buf[ptr_top-ptr_i];
1598 encode_key_init (&encode_info);
1599 encode_key_write (cp, &encode_info, outf);
1602 cp = key_buf[ptr_top-ptr_i];
1603 encode_key_write (cp+key_y_len, &encode_info, outf);
1605 encode_key_flush ( &encode_info, outf);
1608 prevcp = key_buf[ptr_top-ptr_i];
1613 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1616 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1618 zh->reg->key_buf_used = 0;
1621 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1622 zebra_rec_keys_t reckeys,
1623 zebra_snippets *snippets)
1625 NMEM nmem = nmem_create();
1626 if (zebra_rec_keys_rewind(reckeys))
1631 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1633 char dst_buf[IT_MAX_WORD];
1634 char *dst_term = dst_buf;
1637 assert(key.len <= 4 && key.len > 2);
1638 seqno = (int) key.mem[key.len-1];
1641 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1642 0/* db */, 0/* set */, 0/* use */,
1643 0 /* string_index */);
1645 zebra_term_untrans_iconv(zh, nmem, index_type,
1647 zebra_snippets_append(snippets, seqno, ord, dst_term);
1655 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1657 yaz_log(YLOG_LOG, "print_rec_keys");
1658 if (zebra_rec_keys_rewind(reckeys))
1663 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1665 char dst_buf[IT_MAX_WORD];
1669 assert(key.len <= 4 && key.len > 2);
1671 zebraExplain_lookup_ord(zh->reg->zei,
1672 key.mem[0], &index_type, &db, 0, 0, 0);
1674 seqno = (int) key.mem[key.len-1];
1676 zebra_term_untrans(zh, index_type, dst_buf, str);
1678 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1679 key.mem[0], seqno, dst_buf);
1684 void extract_add_index_string (RecWord *p, const char *str, int length)
1688 ZebraHandle zh = p->extractCtrl->handle;
1689 ZebraExplainInfo zei = zh->reg->zei;
1694 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1696 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1703 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
1704 p->attrSet, p->attrUse);
1706 ch = zebraExplain_add_attr_su(zei, p->index_type,
1707 p->attrSet, p->attrUse);
1712 key.mem[1] = p->record_id;
1713 key.mem[2] = p->section_id;
1714 key.mem[3] = p->seqno;
1723 for (i = 0; i<length && i < 20; i++)
1724 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1725 /* just for debugging .. */
1726 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1727 "record_id=%lld section_id=%lld seqno=%lld %s",
1728 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1733 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1737 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1741 ZebraHandle zh = p->extractCtrl->handle;
1742 ZebraExplainInfo zei = zh->reg->zei;
1747 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1749 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1757 key.mem[1] = p->record_id;
1758 key.mem[2] = p->section_id;
1759 key.mem[3] = p->seqno;
1761 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1764 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1766 ZebraHandle zh = p->extractCtrl->handle;
1767 struct sortKeys *sk = &zh->reg->sortKeys;
1770 while (off < sk->buf_used)
1774 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1775 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1776 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1778 if (p->attrSet == set && p->attrUse == use)
1781 assert (off == sk->buf_used);
1783 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1787 b = (char *) xmalloc (sk->buf_max += 128000);
1788 if (sk->buf_used > 0)
1789 memcpy (b, sk->buf, sk->buf_used);
1793 off += key_SU_encode(p->attrSet, sk->buf + off);
1794 off += key_SU_encode(p->attrUse, sk->buf + off);
1795 off += key_SU_encode(length, sk->buf + off);
1796 memcpy (sk->buf + off, str, length);
1797 sk->buf_used = off + length;
1801 void extract_add_string (RecWord *p, const char *string, int length)
1803 assert (length > 0);
1804 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1805 extract_add_sort_string (p, string, length);
1807 extract_add_index_string (p, string, length);
1810 static void extract_add_incomplete_field (RecWord *p)
1812 const char *b = p->term_buf;
1813 int remain = p->term_len;
1814 const char **map = 0;
1816 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1819 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1823 char buf[IT_MAX_WORD+1];
1827 while (map && *map && **map == *CHR_SPACE)
1829 remain = p->term_len - (b - p->term_buf);
1831 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1839 while (map && *map && **map != *CHR_SPACE)
1841 const char *cp = *map;
1843 while (i < IT_MAX_WORD && *cp)
1845 remain = p->term_len - (b - p->term_buf);
1847 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1853 extract_add_string (p, buf, i);
1858 static void extract_add_complete_field (RecWord *p)
1860 const char *b = p->term_buf;
1861 char buf[IT_MAX_WORD+1];
1862 const char **map = 0;
1863 int i = 0, remain = p->term_len;
1865 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1866 p->term_len, p->term_buf);
1869 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1871 while (remain > 0 && i < IT_MAX_WORD)
1873 while (map && *map && **map == *CHR_SPACE)
1875 remain = p->term_len - (b - p->term_buf);
1879 int first = i ? 0 : 1; /* first position */
1880 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1888 if (i && i < IT_MAX_WORD)
1889 buf[i++] = *CHR_SPACE;
1890 while (map && *map && **map != *CHR_SPACE)
1892 const char *cp = *map;
1894 if (**map == *CHR_CUT)
1900 if (i >= IT_MAX_WORD)
1902 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1903 while (i < IT_MAX_WORD && *cp)
1906 remain = p->term_len - (b - p->term_buf);
1909 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1918 extract_add_string (p, buf, i);
1921 void extract_token_add (RecWord *p)
1925 yaz_log (YLOG_LOG, "token_add "
1926 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1927 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1930 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1931 p->term_buf, p->term_len)))
1933 p->term_buf = wrbuf_buf(wrbuf);
1934 p->term_len = wrbuf_len(wrbuf);
1936 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1937 extract_add_complete_field (p);
1939 extract_add_incomplete_field(p);
1942 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1943 void *buf, size_t sz)
1945 ZebraHandle zh = (ZebraHandle) p->handle;
1947 xfree(zh->store_data_buf);
1948 zh->store_data_buf = 0;
1949 zh->store_data_size = 0;
1952 zh->store_data_buf = xmalloc(sz);
1953 zh->store_data_size = sz;
1954 memcpy(zh->store_data_buf, buf, sz);
1958 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1960 ZebraHandle zh = (ZebraHandle) p->handle;
1961 xfree(zh->store_data_buf);
1962 zh->store_data_buf = 0;
1963 zh->store_data_size = 0;
1964 p->setStoreData = extract_set_store_data_cb;
1967 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1969 ZebraHandle zh = (ZebraHandle) p->handle;
1970 zebraExplain_addSchema (zh->reg->zei, oid);
1974 #error not done yet with zebra_rec_keys_t
1975 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1976 int cmd, struct recKeys *reckeys)
1978 SortIdx sortIdx = zh->reg->sortIdx;
1979 void *decode_handle = iscz1_start();
1983 while (off < reckeys->buf_used)
1985 const char *src = reckeys->buf + off;
1987 char *dst = (char*) &key;
1989 iscz1_decode(decode_handle, &dst, &src);
1990 assert(key.len == 4);
1992 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
1994 sortIdx_type(sortIdx, ch);
1996 sortIdx_add(sortIdx, src, strlen(src));
1998 sortIdx_add(sortIdx, "", 1);
2003 off = src - reckeys->buf;
2005 assert (off == reckeys->buf_used);
2006 iscz1_stop(decode_handle);
2009 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
2010 int cmd, struct sortKeys *sk)
2012 SortIdx sortIdx = zh->reg->sortIdx;
2015 sortIdx_sysno (sortIdx, sysno);
2017 while (off < sk->buf_used)
2021 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
2022 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
2023 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
2025 sortIdx_type(sortIdx, use);
2027 sortIdx_add(sortIdx, sk->buf + off, slen);
2029 sortIdx_add(sortIdx, "", 1);
2035 void encode_key_init (struct encode_info *i)
2044 i->encode_handle = iscz1_start();
2045 i->decode_handle = iscz1_start();
2051 /* this is the old encode_key_write
2052 * may be deleted once we are confident that the new works
2055 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2058 char *bp = i->buf, *bp0;
2059 const char *src = (char *) &key;
2061 /* copy term to output buf */
2062 while ((*bp++ = *k++))
2064 /* and copy & align key so we can mangle */
2065 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
2069 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
2071 assert(key.mem[0] >= 0);
2074 iscz1_encode(i->encode_handle, &bp, &src);
2076 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
2077 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2079 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2088 const char *src = bp0+1;
2089 char *dst = (char*) &key2;
2090 iscz1_decode(i->decode_handle, &dst, &src);
2092 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
2094 assert(key2.mem[1]);
2099 void encode_key_flush (struct encode_info *i, FILE *outf)
2101 iscz1_stop(i->encode_handle);
2102 iscz1_stop(i->decode_handle);
2107 /* new encode_key_write
2108 * The idea is to buffer one more key, and compare them
2109 * If we are going to delete and insert the same key,
2110 * we may as well not bother. Should make a difference in
2111 * updates with small modifications (appending to a mbox)
2113 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2118 if (*k) /* first time for new key */
2121 while ((*bp++ = *k++))
2123 i->keylen= bp - i->buf -1;
2124 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2128 bp=i->buf + i->keylen;
2133 memcpy (&key, k+1, sizeof(struct it_key));
2134 if (0==i->prevsys) /* no previous filter, fill up */
2136 i->prevsys=key.sysno;
2137 i->prevseq=key.seqno;
2140 else if ( (i->prevsys==key.sysno) &&
2141 (i->prevseq==key.seqno) &&
2143 { /* same numbers, diff cmd, they cancel out */
2147 { /* different stuff, write previous, move buf */
2148 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2149 if (i->sysno != i->prevsys)
2151 i->sysno = i->prevsys;
2154 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2156 return; /* ??? Filters some sort of duplicates away */
2157 /* ??? Can this ever happen -H 15oct02 */
2159 bp = encode_key_int (i->prevseq - i->seqno, bp);
2160 i->seqno = i->prevseq;
2161 i->cmd = i->prevcmd;
2162 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2164 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2167 i->keylen=0; /* ok, it's written, forget it */
2168 i->prevsys=key.sysno;
2169 i->prevseq=key.seqno;
2174 void encode_key_flush (struct encode_info *i, FILE *outf)
2175 { /* flush the last key from i */
2176 char *bp =i->buf + i->keylen;
2179 return; /* nothing to flush */
2182 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2183 if (i->sysno != i->prevsys)
2185 i->sysno = i->prevsys;
2188 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2190 return; /* ??? Filters some sort of duplicates away */
2191 /* ??? Can this ever happen -H 15oct02 */
2193 bp = encode_key_int (i->prevseq - i->seqno, bp);
2194 i->seqno = i->prevseq;
2195 i->cmd = i->prevcmd;
2196 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2198 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2201 i->keylen=0; /* ok, it's written, forget it */
2202 i->prevsys=0; /* forget the values too */
2209 * indent-tabs-mode: nil
2211 * vim: shiftwidth=4 tabstop=8 expandtab