1 /* $Id: extract.c,v 1.207 2006-04-05 02:11:44 adam Exp $
2 Copyright (C) 1995-2005
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 RecordAttr *recordAttr;
392 const char *matchStr = 0;
395 off_t recordOffset = 0;
396 struct recExtractCtrl extractCtrl;
398 /* announce database */
399 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
401 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
402 zh->m_explain_database))
408 /* we are going to read from a file, so prepare the extraction */
409 zebra_rec_keys_reset(zh->reg->keys);
412 zebra_rec_keys_reset(zh->reg->sortKeys);
414 zh->reg->sortKeys.buf_used = 0;
416 recordOffset = fi->file_moffset;
417 extractCtrl.handle = zh;
418 extractCtrl.offset = fi->file_moffset;
419 extractCtrl.readf = file_read;
420 extractCtrl.seekf = file_seek;
421 extractCtrl.tellf = file_tell;
422 extractCtrl.endf = file_end;
424 extractCtrl.init = extract_init;
425 extractCtrl.tokenAdd = extract_token_add;
426 extractCtrl.schemaAdd = extract_schema_add;
427 extractCtrl.dh = zh->reg->dh;
428 extractCtrl.match_criteria[0] = '\0';
429 extractCtrl.staticrank = 0;
431 extractCtrl.first_record = fi->file_offset ? 0 : 1;
433 extract_set_store_data_prepare(&extractCtrl);
435 init_extractCtrl(zh, &extractCtrl);
438 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
442 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
443 yaz_log_init_prefix2 (msg);
446 r = (*recType->extract)(recTypeClientData, &extractCtrl);
448 yaz_log_init_prefix2 (0);
449 if (r == RECCTRL_EXTRACT_EOF)
451 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
453 /* error occured during extraction ... */
455 zh->records_processed < zh->m_file_verbose_limit)
457 yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
458 fname, recordOffset);
462 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
464 /* error occured during extraction ... */
466 zh->records_processed < zh->m_file_verbose_limit)
468 yaz_log (YLOG_WARN, "no filter for %s %s "
469 PRINTF_OFF_T, zh->m_record_type,
470 fname, recordOffset);
474 if (extractCtrl.match_criteria[0])
475 matchStr = extractCtrl.match_criteria;
478 /* perform match if sysno not known and if match criteria is specified */
484 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
486 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
490 yaz_log(YLOG_WARN, "Bad match criteria");
496 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
497 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
501 assert(*rinfo == sizeof(*sysno));
502 memcpy (sysno, rinfo+1, sizeof(*sysno));
506 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
508 /* the extraction process returned no information - the record
509 is probably empty - unless flagShowRecords is in use */
513 if (zh->records_processed < zh->m_file_verbose_limit)
514 yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
515 fname, recordOffset);
524 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
525 fname, recordOffset);
526 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
529 if (zh->records_processed < zh->m_file_verbose_limit)
530 yaz_log (YLOG_LOG, "add %s %s " PRINTF_OFF_T, zh->m_record_type,
531 fname, recordOffset);
532 rec = rec_new (zh->reg->records);
536 recordAttr = rec_init_attr (zh->reg->zei, rec);
537 recordAttr->staticrank = extractCtrl.staticrank;
541 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
542 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
543 sizeof(*sysno), sysno);
546 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
548 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
550 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
551 recordAttr->staticrank);
552 zh->records_inserted++;
556 /* record already exists */
557 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
560 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
562 struct sortKeys sortKeys;
565 rec = rec_get (zh->reg->records, *sysno);
568 recordAttr = rec_init_attr (zh->reg->zei, rec);
570 zebra_rec_keys_set_buf(delkeys,
571 rec->info[recInfo_delKeys],
572 rec->size[recInfo_delKeys],
576 zebra_rec_keys_set_buf(sortKeys,
577 rec->info[recInfo_sortKeys],
578 rec->size[recInfo_sortKeys],
580 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
582 sortKeys.buf_used = rec->size[recInfo_sortKeys];
583 sortKeys.buf = rec->info[recInfo_sortKeys];
584 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
587 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
588 recordAttr->staticrank); /* old values */
591 /* record going to be deleted */
592 if (zebra_rec_keys_empty(delkeys))
594 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T,
595 zh->m_record_type, fname, recordOffset);
596 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
600 if (zh->records_processed < zh->m_file_verbose_limit)
601 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T,
602 zh->m_record_type, fname, recordOffset);
603 zh->records_deleted++;
606 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
607 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
609 rec_del (zh->reg->records, &rec);
617 /* flush new keys for sort&search etc */
618 if (zh->records_processed < zh->m_file_verbose_limit)
619 yaz_log (YLOG_LOG, "update %s %s " PRINTF_OFF_T,
620 zh->m_record_type, fname, recordOffset);
621 recordAttr->staticrank = extractCtrl.staticrank;
623 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
625 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
627 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
628 recordAttr->staticrank);
629 zh->records_updated++;
631 zebra_rec_keys_close(delkeys);
633 zebra_rec_keys_close(sortKeys);
636 /* update file type */
637 xfree (rec->info[recInfo_fileType]);
638 rec->info[recInfo_fileType] =
639 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
641 /* update filename */
642 xfree (rec->info[recInfo_filename]);
643 rec->info[recInfo_filename] =
644 rec_strdup (fname, &rec->size[recInfo_filename]);
646 /* update delete keys */
647 xfree (rec->info[recInfo_delKeys]);
648 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
650 zebra_rec_keys_get_buf(zh->reg->keys,
651 &rec->info[recInfo_delKeys],
652 &rec->size[recInfo_delKeys]);
656 rec->info[recInfo_delKeys] = NULL;
657 rec->size[recInfo_delKeys] = 0;
660 /* update sort keys */
661 xfree (rec->info[recInfo_sortKeys]);
664 zebra_rec_keys_get_buf(zh->reg->sortKeys,
665 &rec->info[recInfo_sortKeys],
666 &rec->size[recInfo_sortKeys]);
668 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
669 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
670 zh->reg->sortKeys.buf = NULL;
671 zh->reg->sortKeys.buf_max = 0;
674 /* save file size of original record */
675 zebraExplain_recordBytesIncrement (zh->reg->zei,
676 - recordAttr->recordSize);
677 recordAttr->recordSize = fi->file_moffset - recordOffset;
678 if (!recordAttr->recordSize)
679 recordAttr->recordSize = fi->file_max - recordOffset;
680 zebraExplain_recordBytesIncrement (zh->reg->zei,
681 recordAttr->recordSize);
683 /* set run-number for this record */
684 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
687 /* update store data */
688 xfree (rec->info[recInfo_storeData]);
689 if (zh->store_data_buf)
691 rec->size[recInfo_storeData] = zh->store_data_size;
692 rec->info[recInfo_storeData] = zh->store_data_buf;
693 zh->store_data_buf = 0;
695 else if (zh->m_store_data)
697 rec->size[recInfo_storeData] = recordAttr->recordSize;
698 rec->info[recInfo_storeData] = (char *)
699 xmalloc (recordAttr->recordSize);
700 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
702 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
703 recordOffset, fname);
706 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
707 < recordAttr->recordSize)
709 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
710 recordAttr->recordSize, fname);
716 rec->info[recInfo_storeData] = NULL;
717 rec->size[recInfo_storeData] = 0;
719 /* update database name */
720 xfree (rec->info[recInfo_databaseName]);
721 rec->info[recInfo_databaseName] =
722 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
725 recordAttr->recordOffset = recordOffset;
727 /* commit this record */
728 rec_put (zh->reg->records, &rec);
733 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
736 ZEBRA_RES r = ZEBRA_OK;
741 struct file_read_info *fi;
742 const char *original_record_type = 0;
744 void *recTypeClientData;
746 if (!zh->m_group || !*zh->m_group)
749 sprintf (gprefix, "%s.", zh->m_group);
751 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
753 /* determine file extension */
755 for (i = strlen(fname); --i >= 0; )
758 else if (fname[i] == '.')
760 strcpy (ext, fname+i+1);
763 /* determine file type - depending on extension */
764 original_record_type = zh->m_record_type;
765 if (!zh->m_record_type)
767 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
768 zh->m_record_type = res_get (zh->res, ext_res);
770 if (!zh->m_record_type)
772 if (zh->records_processed < zh->m_file_verbose_limit)
773 yaz_log (YLOG_LOG, "? %s", fname);
776 /* determine match criteria */
777 if (!zh->m_record_id)
779 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
780 zh->m_record_id = res_get (zh->res, ext_res);
784 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
785 &recTypeClientData)))
787 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
791 switch(recType->version)
796 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
798 if (sysno && deleteFlag)
804 if (zh->path_reg && !yaz_is_abspath (fname))
806 strcpy (full_rep, zh->path_reg);
807 strcat (full_rep, "/");
808 strcat (full_rep, fname);
811 strcpy (full_rep, fname);
813 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
815 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
816 zh->m_record_type = original_record_type;
820 fi = file_read_start (fd);
823 fi->file_moffset = fi->file_offset;
824 fi->file_more = 0; /* file_end not called (yet) */
825 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
826 recType, recTypeClientData);
828 { /* file_end has been called so reset offset .. */
829 fi->file_offset = fi->file_moffset;
830 lseek(fi->fd, fi->file_moffset, SEEK_SET);
844 zh->m_record_type = original_record_type;
849 If sysno is provided, then it's used to identify the reocord.
850 If not, and match_criteria is provided, then sysno is guessed
851 If not, and a record is provided, then sysno is got from there
854 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
855 const char *buf, size_t buf_size,
858 const char *recordType,
860 const char *match_criteria,
866 RecordAttr *recordAttr;
867 struct recExtractCtrl extractCtrl;
869 const char *matchStr = 0;
870 RecType recType = NULL;
873 long recordOffset = 0;
874 struct zebra_fetch_control fc;
875 const char *pr_fname = fname; /* filename to print .. */
876 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
879 pr_fname = "<no file>"; /* make it printable if file is omitted */
882 fc.record_int_buf = buf;
883 fc.record_int_len = buf_size;
884 fc.record_int_pos = 0;
886 fc.record_offset = 0;
888 extractCtrl.offset = 0;
889 extractCtrl.readf = zebra_record_int_read;
890 extractCtrl.seekf = zebra_record_int_seek;
891 extractCtrl.tellf = zebra_record_int_tell;
892 extractCtrl.endf = zebra_record_int_end;
893 extractCtrl.first_record = 1;
894 extractCtrl.fh = &fc;
896 zebra_rec_keys_reset(zh->reg->keys);
899 zebra_rec_keys_reset(zh->reg->sortKeys);
901 zh->reg->sortKeys.buf_used = 0;
903 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
905 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
906 zh->m_explain_database))
910 if (recordType && *recordType)
912 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
913 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
918 if (!(zh->m_record_type))
920 yaz_log (YLOG_WARN, "No such record type defined");
923 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
924 recType = recType_byName (zh->reg->recTypes, zh->res,
925 zh->m_record_type, &clientData);
926 recordType = zh->m_record_type;
931 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
935 extractCtrl.init = extract_init;
936 extractCtrl.tokenAdd = extract_token_add;
937 extractCtrl.schemaAdd = extract_schema_add;
938 extractCtrl.dh = zh->reg->dh;
939 extractCtrl.handle = zh;
940 extractCtrl.match_criteria[0] = '\0';
941 extractCtrl.staticrank = 0;
943 init_extractCtrl(zh, &extractCtrl);
945 extract_set_store_data_prepare(&extractCtrl);
947 r = (*recType->extract)(clientData, &extractCtrl);
949 if (r == RECCTRL_EXTRACT_EOF)
951 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
953 /* error occured during extraction ... */
954 yaz_log (YLOG_WARN, "extract error: generic");
957 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
959 /* error occured during extraction ... */
960 yaz_log (YLOG_WARN, "extract error: no such filter");
964 if (extractCtrl.match_criteria[0])
965 match_criteria = extractCtrl.match_criteria;
971 if (match_criteria && *match_criteria) {
972 matchStr = match_criteria;
974 if (zh->m_record_id && *zh->m_record_id) {
975 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
979 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
986 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
987 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
991 assert(*rinfo == sizeof(*sysno));
992 memcpy (sysno, rinfo+1, sizeof(*sysno));
996 if (zebra_rec_keys_empty(zh->reg->keys))
998 /* the extraction process returned no information - the record
999 is probably empty - unless flagShowRecords is in use */
1009 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1010 pr_fname, (long) recordOffset);
1011 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1015 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1016 (long) recordOffset);
1017 rec = rec_new (zh->reg->records);
1019 *sysno = rec->sysno;
1021 recordAttr = rec_init_attr (zh->reg->zei, rec);
1022 recordAttr->staticrank = extractCtrl.staticrank;
1026 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1027 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1028 sizeof(*sysno), sysno);
1031 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1033 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1037 print_rec_keys(zh, zh->reg->keys);
1039 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1040 recordAttr->staticrank);
1041 zh->records_inserted++;
1045 /* record already exists */
1046 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1048 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1050 struct sortKeys sortKeys;
1055 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1056 recordType, pr_fname, (long) recordOffset);
1061 rec = rec_get (zh->reg->records, *sysno);
1064 recordAttr = rec_init_attr (zh->reg->zei, rec);
1066 zebra_rec_keys_set_buf(delkeys,
1067 rec->info[recInfo_delKeys],
1068 rec->size[recInfo_delKeys],
1071 zebra_rec_keys_set_buf(sortKeys,
1072 rec->info[recInfo_sortKeys],
1073 rec->size[recInfo_sortKeys],
1076 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1077 sortKeys.buf = rec->info[recInfo_sortKeys];
1081 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1083 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1085 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1086 recordAttr->staticrank);
1089 /* record going to be deleted */
1090 if (zebra_rec_keys_empty(delkeys))
1092 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1093 pr_fname, (long) recordOffset);
1094 yaz_log (YLOG_WARN, "cannot delete file above, "
1095 "storeKeys false (3)");
1100 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1101 pr_fname, (long) recordOffset);
1102 zh->records_deleted++;
1105 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1106 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1108 rec_del (zh->reg->records, &rec);
1117 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1118 pr_fname, (long) recordOffset);
1119 recordAttr->staticrank = extractCtrl.staticrank;
1121 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1123 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1125 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1126 recordAttr->staticrank);
1127 zh->records_updated++;
1129 zebra_rec_keys_close(delkeys);
1131 zebra_rec_keys_close(sortKeys);
1134 /* update file type */
1135 xfree (rec->info[recInfo_fileType]);
1136 rec->info[recInfo_fileType] =
1137 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1139 /* update filename */
1140 xfree (rec->info[recInfo_filename]);
1141 rec->info[recInfo_filename] =
1142 rec_strdup (fname, &rec->size[recInfo_filename]);
1144 /* update delete keys */
1145 xfree (rec->info[recInfo_delKeys]);
1146 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1148 zebra_rec_keys_get_buf(zh->reg->keys,
1149 &rec->info[recInfo_delKeys],
1150 &rec->size[recInfo_delKeys]);
1154 rec->info[recInfo_delKeys] = NULL;
1155 rec->size[recInfo_delKeys] = 0;
1157 /* update sort keys */
1158 xfree (rec->info[recInfo_sortKeys]);
1161 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1162 &rec->info[recInfo_sortKeys],
1163 &rec->size[recInfo_sortKeys]);
1165 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1166 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1167 zh->reg->sortKeys.buf = NULL;
1168 zh->reg->sortKeys.buf_max = 0;
1171 /* save file size of original record */
1172 zebraExplain_recordBytesIncrement (zh->reg->zei,
1173 - recordAttr->recordSize);
1175 recordAttr->recordSize = fi->file_moffset - recordOffset;
1176 if (!recordAttr->recordSize)
1177 recordAttr->recordSize = fi->file_max - recordOffset;
1179 recordAttr->recordSize = buf_size;
1181 zebraExplain_recordBytesIncrement (zh->reg->zei,
1182 recordAttr->recordSize);
1184 /* set run-number for this record */
1185 recordAttr->runNumber =
1186 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1188 /* update store data */
1189 xfree (rec->info[recInfo_storeData]);
1191 /* update store data */
1192 if (zh->store_data_buf)
1194 rec->size[recInfo_storeData] = zh->store_data_size;
1195 rec->info[recInfo_storeData] = zh->store_data_buf;
1196 zh->store_data_buf = 0;
1198 else if (zh->m_store_data)
1200 rec->size[recInfo_storeData] = recordAttr->recordSize;
1201 rec->info[recInfo_storeData] = (char *)
1202 xmalloc (recordAttr->recordSize);
1203 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1207 rec->info[recInfo_storeData] = NULL;
1208 rec->size[recInfo_storeData] = 0;
1210 /* update database name */
1211 xfree (rec->info[recInfo_databaseName]);
1212 rec->info[recInfo_databaseName] =
1213 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1216 recordAttr->recordOffset = recordOffset;
1218 /* commit this record */
1219 rec_put (zh->reg->records, &rec);
1224 int explain_extract (void *handle, Record rec, data1_node *n)
1226 ZebraHandle zh = (ZebraHandle) handle;
1227 struct recExtractCtrl extractCtrl;
1229 if (zebraExplain_curDatabase (zh->reg->zei,
1230 rec->info[recInfo_databaseName]))
1233 if (zebraExplain_newDatabase (zh->reg->zei,
1234 rec->info[recInfo_databaseName], 0))
1238 zebra_rec_keys_reset(zh->reg->keys);
1241 zebra_rec_keys_reset(zh->reg->sortKeys);
1243 zh->reg->sortKeys.buf_used = 0;
1245 extractCtrl.init = extract_init;
1246 extractCtrl.tokenAdd = extract_token_add;
1247 extractCtrl.schemaAdd = extract_schema_add;
1248 extractCtrl.dh = zh->reg->dh;
1250 init_extractCtrl(zh, &extractCtrl);
1252 extractCtrl.flagShowRecords = 0;
1253 extractCtrl.match_criteria[0] = '\0';
1254 extractCtrl.staticrank = 0;
1255 extractCtrl.handle = handle;
1256 extractCtrl.first_record = 1;
1258 extract_set_store_data_prepare(&extractCtrl);
1261 grs_extract_tree(&extractCtrl, n);
1263 if (rec->size[recInfo_delKeys])
1265 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1268 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1270 struct sortKeys sortkeys;
1273 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1274 rec->size[recInfo_delKeys],
1276 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1277 zebra_rec_keys_close(delkeys);
1279 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1280 rec->size[recInfo_sortKeys],
1283 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1284 zebra_rec_keys_close(sortkeys);
1286 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1287 sortkeys.buf = rec->info[recInfo_sortKeys];
1288 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1291 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1293 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1295 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1298 xfree (rec->info[recInfo_delKeys]);
1299 zebra_rec_keys_get_buf(zh->reg->keys,
1300 &rec->info[recInfo_delKeys],
1301 &rec->size[recInfo_delKeys]);
1303 xfree (rec->info[recInfo_sortKeys]);
1305 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1306 &rec->info[recInfo_sortKeys],
1307 &rec->size[recInfo_sortKeys]);
1309 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1310 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1311 zh->reg->sortKeys.buf = NULL;
1312 zh->reg->sortKeys.buf_max = 0;
1318 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1320 zebra_rec_keys_t reckeys,
1323 ZebraExplainInfo zei = zh->reg->zei;
1325 if (!zh->reg->key_buf)
1327 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1330 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1333 /* FIXME: That "8" should be in a default settings include */
1334 /* not hard-coded here! -H */
1335 zh->reg->key_buf = (char**) xmalloc (mem);
1336 zh->reg->ptr_top = mem/sizeof(char*);
1338 zh->reg->key_buf_used = 0;
1339 zh->reg->key_file_no = 0;
1341 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1343 if (zebra_rec_keys_rewind(reckeys))
1347 struct it_key key_in;
1348 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1351 struct it_key key_out;
1352 zint *keyp = key_out.mem;
1354 assert(key_in.len == 4);
1356 /* check for buffer overflow */
1357 if (zh->reg->key_buf_used + 1024 >
1358 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1359 extract_flushWriteKeys (zh, 0);
1362 assert(zh->reg->ptr_i > 0);
1363 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1364 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1366 /* encode the ordinal value (field/use/attribute) .. */
1367 ch = (int) key_in.mem[0];
1368 zh->reg->key_buf_used +=
1369 key_SU_encode(ch, (char*)zh->reg->key_buf +
1370 zh->reg->key_buf_used);
1372 /* copy the 0-terminated stuff from str to output */
1373 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1374 zh->reg->key_buf_used += slen;
1375 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1377 /* the delete/insert indicator */
1378 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1380 if (zh->m_staticrank) /* rank config enabled ? */
1384 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1388 *keyp++ = staticrank;
1394 if (key_in.mem[1]) /* filter specified record ID */
1395 *keyp++ = key_in.mem[1];
1398 *keyp++ = key_in.mem[2]; /* section_id */
1399 *keyp++ = key_in.mem[3]; /* sequence .. */
1401 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1402 &key_out, sizeof(key_out));
1403 (zh->reg->key_buf_used) += sizeof(key_out);
1408 void extract_flushWriteKeys (ZebraHandle zh, int final)
1409 /* optimizing: if final=1, and no files written yet */
1410 /* push the keys directly to merge, sidestepping the */
1411 /* temp file altogether. Speeds small updates */
1414 char out_fname[200];
1416 struct encode_info encode_info;
1417 int ptr_i = zh->reg->ptr_i;
1422 if (!zh->reg->key_buf || ptr_i <= 0)
1424 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1425 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1426 yaz_log (YLOG_DEBUG, " buf=%p ",
1428 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1429 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1434 (zh->reg->key_file_no)++;
1435 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1436 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1437 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1439 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1440 sizeof(char*), key_qsort_compare);
1442 /* zebra.cfg: tempfiles:
1443 Y: always use temp files (old way)
1444 A: use temp files, if more than one (auto)
1445 = if this is both the last and the first
1446 N: never bother with temp files (new) */
1448 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1449 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1450 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1455 if ( ( temp_policy =='N' ) || /* always from memory */
1456 ( ( temp_policy =='A' ) && /* automatic */
1457 (zh->reg->key_file_no == 1) && /* this is first time */
1458 (final) ) ) /* and last (=only) time */
1459 { /* go directly from memory */
1460 zh->reg->key_file_no =0; /* signal not to read files */
1461 zebra_index_merge(zh);
1463 zh->reg->key_buf_used = 0;
1467 /* Not doing directly from memory, write into a temp file */
1468 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1470 if (!(outf = fopen (out_fname, "wb")))
1472 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1475 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1476 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1478 encode_key_init (&encode_info);
1479 encode_key_write (cp, &encode_info, outf);
1483 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1484 if (strcmp (cp, prevcp))
1486 encode_key_flush ( &encode_info, outf);
1487 encode_key_init (&encode_info);
1488 encode_key_write (cp, &encode_info, outf);
1492 encode_key_write (cp + strlen(cp), &encode_info, outf);
1494 encode_key_flush ( &encode_info, outf);
1496 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1497 extract_get_fname_tmp (out_fname, key_file_no);
1499 if (!(outf = fopen (out_fname, "wb")))
1501 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1504 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1506 prevcp = key_buf[ptr_top-i];
1508 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1510 key_y_len = strlen(prevcp)+1;
1512 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1513 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1515 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1516 sizeof(char*), key_y_compare);
1517 cp = key_buf[ptr_top-ptr_i];
1519 encode_key_init (&encode_info);
1520 encode_key_write (cp, &encode_info, outf);
1523 cp = key_buf[ptr_top-ptr_i];
1524 encode_key_write (cp+key_y_len, &encode_info, outf);
1526 encode_key_flush ( &encode_info, outf);
1529 prevcp = key_buf[ptr_top-ptr_i];
1534 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1537 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1539 zh->reg->key_buf_used = 0;
1542 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1543 zebra_rec_keys_t reckeys,
1544 zebra_snippets *snippets)
1546 NMEM nmem = nmem_create();
1547 if (zebra_rec_keys_rewind(reckeys))
1552 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1554 char dst_buf[IT_MAX_WORD];
1555 char *dst_term = dst_buf;
1558 assert(key.len <= 4 && key.len > 2);
1559 seqno = (int) key.mem[key.len-1];
1562 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1563 0/* db */, 0/* set */, 0/* use */,
1564 0 /* string_index */);
1566 zebra_term_untrans_iconv(zh, nmem, index_type,
1568 zebra_snippets_append(snippets, seqno, ord, dst_term);
1576 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1578 yaz_log(YLOG_LOG, "print_rec_keys");
1579 if (zebra_rec_keys_rewind(reckeys))
1584 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1586 char dst_buf[IT_MAX_WORD];
1590 assert(key.len <= 4 && key.len > 2);
1592 zebraExplain_lookup_ord(zh->reg->zei,
1593 key.mem[0], &index_type, &db, 0, 0, 0);
1595 seqno = (int) key.mem[key.len-1];
1597 zebra_term_untrans(zh, index_type, dst_buf, str);
1599 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1600 key.mem[0], seqno, dst_buf);
1605 void extract_add_index_string (RecWord *p, const char *str, int length)
1609 ZebraHandle zh = p->extractCtrl->handle;
1610 ZebraExplainInfo zei = zh->reg->zei;
1615 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1617 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1624 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
1625 p->attrSet, p->attrUse);
1627 ch = zebraExplain_add_attr_su(zei, p->index_type,
1628 p->attrSet, p->attrUse);
1633 key.mem[1] = p->record_id;
1634 key.mem[2] = p->section_id;
1635 key.mem[3] = p->seqno;
1644 for (i = 0; i<length && i < 20; i++)
1645 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1646 /* just for debugging .. */
1647 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1648 "record_id=%lld section_id=%lld seqno=%lld %s",
1649 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1654 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1658 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1662 ZebraHandle zh = p->extractCtrl->handle;
1663 ZebraExplainInfo zei = zh->reg->zei;
1668 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1670 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1678 key.mem[1] = p->record_id;
1679 key.mem[2] = p->section_id;
1680 key.mem[3] = p->seqno;
1682 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1685 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1687 ZebraHandle zh = p->extractCtrl->handle;
1688 struct sortKeys *sk = &zh->reg->sortKeys;
1691 while (off < sk->buf_used)
1695 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1696 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1697 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1699 if (p->attrSet == set && p->attrUse == use)
1702 assert (off == sk->buf_used);
1704 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1708 b = (char *) xmalloc (sk->buf_max += 128000);
1709 if (sk->buf_used > 0)
1710 memcpy (b, sk->buf, sk->buf_used);
1714 off += key_SU_encode(p->attrSet, sk->buf + off);
1715 off += key_SU_encode(p->attrUse, sk->buf + off);
1716 off += key_SU_encode(length, sk->buf + off);
1717 memcpy (sk->buf + off, str, length);
1718 sk->buf_used = off + length;
1722 void extract_add_string (RecWord *p, const char *string, int length)
1724 assert (length > 0);
1725 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1726 extract_add_sort_string (p, string, length);
1728 extract_add_index_string (p, string, length);
1731 static void extract_add_incomplete_field (RecWord *p)
1733 const char *b = p->term_buf;
1734 int remain = p->term_len;
1735 const char **map = 0;
1737 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1740 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1744 char buf[IT_MAX_WORD+1];
1748 while (map && *map && **map == *CHR_SPACE)
1750 remain = p->term_len - (b - p->term_buf);
1752 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1760 while (map && *map && **map != *CHR_SPACE)
1762 const char *cp = *map;
1764 while (i < IT_MAX_WORD && *cp)
1766 remain = p->term_len - (b - p->term_buf);
1768 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1774 extract_add_string (p, buf, i);
1779 static void extract_add_complete_field (RecWord *p)
1781 const char *b = p->term_buf;
1782 char buf[IT_MAX_WORD+1];
1783 const char **map = 0;
1784 int i = 0, remain = p->term_len;
1786 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1787 p->term_len, p->term_buf);
1790 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1792 while (remain > 0 && i < IT_MAX_WORD)
1794 while (map && *map && **map == *CHR_SPACE)
1796 remain = p->term_len - (b - p->term_buf);
1800 int first = i ? 0 : 1; /* first position */
1801 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1809 if (i && i < IT_MAX_WORD)
1810 buf[i++] = *CHR_SPACE;
1811 while (map && *map && **map != *CHR_SPACE)
1813 const char *cp = *map;
1815 if (**map == *CHR_CUT)
1821 if (i >= IT_MAX_WORD)
1823 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1824 while (i < IT_MAX_WORD && *cp)
1827 remain = p->term_len - (b - p->term_buf);
1830 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1839 extract_add_string (p, buf, i);
1842 void extract_token_add (RecWord *p)
1846 yaz_log (YLOG_LOG, "token_add "
1847 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1848 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1851 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1852 p->term_buf, p->term_len)))
1854 p->term_buf = wrbuf_buf(wrbuf);
1855 p->term_len = wrbuf_len(wrbuf);
1857 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1858 extract_add_complete_field (p);
1860 extract_add_incomplete_field(p);
1863 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1864 void *buf, size_t sz)
1866 ZebraHandle zh = (ZebraHandle) p->handle;
1868 xfree(zh->store_data_buf);
1869 zh->store_data_buf = 0;
1870 zh->store_data_size = 0;
1873 zh->store_data_buf = xmalloc(sz);
1874 zh->store_data_size = sz;
1875 memcpy(zh->store_data_buf, buf, sz);
1879 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1881 ZebraHandle zh = (ZebraHandle) p->handle;
1882 xfree(zh->store_data_buf);
1883 zh->store_data_buf = 0;
1884 zh->store_data_size = 0;
1885 p->setStoreData = extract_set_store_data_cb;
1888 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1890 ZebraHandle zh = (ZebraHandle) p->handle;
1891 zebraExplain_addSchema (zh->reg->zei, oid);
1895 #error not done yet with zebra_rec_keys_t
1896 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1897 int cmd, struct recKeys *reckeys)
1899 SortIdx sortIdx = zh->reg->sortIdx;
1900 void *decode_handle = iscz1_start();
1904 while (off < reckeys->buf_used)
1906 const char *src = reckeys->buf + off;
1908 char *dst = (char*) &key;
1910 iscz1_decode(decode_handle, &dst, &src);
1911 assert(key.len == 4);
1913 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
1915 sortIdx_type(sortIdx, ch);
1917 sortIdx_add(sortIdx, src, strlen(src));
1919 sortIdx_add(sortIdx, "", 1);
1924 off = src - reckeys->buf;
1926 assert (off == reckeys->buf_used);
1927 iscz1_stop(decode_handle);
1930 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1931 int cmd, struct sortKeys *sk)
1933 SortIdx sortIdx = zh->reg->sortIdx;
1936 sortIdx_sysno (sortIdx, sysno);
1938 while (off < sk->buf_used)
1942 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1943 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1944 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1946 sortIdx_type(sortIdx, use);
1948 sortIdx_add(sortIdx, sk->buf + off, slen);
1950 sortIdx_add(sortIdx, "", 1);
1956 void encode_key_init (struct encode_info *i)
1965 i->encode_handle = iscz1_start();
1966 i->decode_handle = iscz1_start();
1972 /* this is the old encode_key_write
1973 * may be deleted once we are confident that the new works
1976 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1979 char *bp = i->buf, *bp0;
1980 const char *src = (char *) &key;
1982 /* copy term to output buf */
1983 while ((*bp++ = *k++))
1985 /* and copy & align key so we can mangle */
1986 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
1990 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
1992 assert(key.mem[0] >= 0);
1995 iscz1_encode(i->encode_handle, &bp, &src);
1997 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
1998 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2000 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2009 const char *src = bp0+1;
2010 char *dst = (char*) &key2;
2011 iscz1_decode(i->decode_handle, &dst, &src);
2013 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
2015 assert(key2.mem[1]);
2020 void encode_key_flush (struct encode_info *i, FILE *outf)
2022 iscz1_stop(i->encode_handle);
2023 iscz1_stop(i->decode_handle);
2028 /* new encode_key_write
2029 * The idea is to buffer one more key, and compare them
2030 * If we are going to delete and insert the same key,
2031 * we may as well not bother. Should make a difference in
2032 * updates with small modifications (appending to a mbox)
2034 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2039 if (*k) /* first time for new key */
2042 while ((*bp++ = *k++))
2044 i->keylen= bp - i->buf -1;
2045 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2049 bp=i->buf + i->keylen;
2054 memcpy (&key, k+1, sizeof(struct it_key));
2055 if (0==i->prevsys) /* no previous filter, fill up */
2057 i->prevsys=key.sysno;
2058 i->prevseq=key.seqno;
2061 else if ( (i->prevsys==key.sysno) &&
2062 (i->prevseq==key.seqno) &&
2064 { /* same numbers, diff cmd, they cancel out */
2068 { /* different stuff, write previous, move buf */
2069 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2070 if (i->sysno != i->prevsys)
2072 i->sysno = i->prevsys;
2075 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2077 return; /* ??? Filters some sort of duplicates away */
2078 /* ??? Can this ever happen -H 15oct02 */
2080 bp = encode_key_int (i->prevseq - i->seqno, bp);
2081 i->seqno = i->prevseq;
2082 i->cmd = i->prevcmd;
2083 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2085 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2088 i->keylen=0; /* ok, it's written, forget it */
2089 i->prevsys=key.sysno;
2090 i->prevseq=key.seqno;
2095 void encode_key_flush (struct encode_info *i, FILE *outf)
2096 { /* flush the last key from i */
2097 char *bp =i->buf + i->keylen;
2100 return; /* nothing to flush */
2103 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2104 if (i->sysno != i->prevsys)
2106 i->sysno = i->prevsys;
2109 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2111 return; /* ??? Filters some sort of duplicates away */
2112 /* ??? Can this ever happen -H 15oct02 */
2114 bp = encode_key_int (i->prevseq - i->seqno, bp);
2115 i->seqno = i->prevseq;
2116 i->cmd = i->prevcmd;
2117 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2119 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2122 i->keylen=0; /* ok, it's written, forget it */
2123 i->prevsys=0; /* forget the values too */