1 /* $Id: extract.c,v 1.206 2006-03-30 09:52:15 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 int 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 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
740 struct file_read_info *fi;
741 const char *original_record_type = 0;
743 void *recTypeClientData;
745 if (!zh->m_group || !*zh->m_group)
748 sprintf (gprefix, "%s.", zh->m_group);
750 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
752 /* determine file extension */
754 for (i = strlen(fname); --i >= 0; )
757 else if (fname[i] == '.')
759 strcpy (ext, fname+i+1);
762 /* determine file type - depending on extension */
763 original_record_type = zh->m_record_type;
764 if (!zh->m_record_type)
766 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
767 zh->m_record_type = res_get (zh->res, ext_res);
769 if (!zh->m_record_type)
771 if (zh->records_processed < zh->m_file_verbose_limit)
772 yaz_log (YLOG_LOG, "? %s", fname);
775 /* determine match criteria */
776 if (!zh->m_record_id)
778 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
779 zh->m_record_id = res_get (zh->res, ext_res);
783 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
784 &recTypeClientData)))
786 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
790 switch(recType->version)
795 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
797 if (sysno && deleteFlag)
803 if (zh->path_reg && !yaz_is_abspath (fname))
805 strcpy (full_rep, zh->path_reg);
806 strcat (full_rep, "/");
807 strcat (full_rep, fname);
810 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);
837 zh->m_record_type = original_record_type;
842 If sysno is provided, then it's used to identify the reocord.
843 If not, and match_criteria is provided, then sysno is guessed
844 If not, and a record is provided, then sysno is got from there
847 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
848 const char *buf, size_t buf_size,
851 const char *recordType,
853 const char *match_criteria,
859 RecordAttr *recordAttr;
860 struct recExtractCtrl extractCtrl;
862 const char *matchStr = 0;
863 RecType recType = NULL;
866 long recordOffset = 0;
867 struct zebra_fetch_control fc;
868 const char *pr_fname = fname; /* filename to print .. */
869 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
872 pr_fname = "<no file>"; /* make it printable if file is omitted */
875 fc.record_int_buf = buf;
876 fc.record_int_len = buf_size;
877 fc.record_int_pos = 0;
879 fc.record_offset = 0;
881 extractCtrl.offset = 0;
882 extractCtrl.readf = zebra_record_int_read;
883 extractCtrl.seekf = zebra_record_int_seek;
884 extractCtrl.tellf = zebra_record_int_tell;
885 extractCtrl.endf = zebra_record_int_end;
886 extractCtrl.first_record = 1;
887 extractCtrl.fh = &fc;
889 zebra_rec_keys_reset(zh->reg->keys);
892 zebra_rec_keys_reset(zh->reg->sortKeys);
894 zh->reg->sortKeys.buf_used = 0;
896 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
898 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
899 zh->m_explain_database))
903 if (recordType && *recordType)
905 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
906 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
911 if (!(zh->m_record_type))
913 yaz_log (YLOG_WARN, "No such record type defined");
916 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
917 recType = recType_byName (zh->reg->recTypes, zh->res,
918 zh->m_record_type, &clientData);
919 recordType = zh->m_record_type;
924 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
928 extractCtrl.init = extract_init;
929 extractCtrl.tokenAdd = extract_token_add;
930 extractCtrl.schemaAdd = extract_schema_add;
931 extractCtrl.dh = zh->reg->dh;
932 extractCtrl.handle = zh;
933 extractCtrl.match_criteria[0] = '\0';
934 extractCtrl.staticrank = 0;
936 init_extractCtrl(zh, &extractCtrl);
938 extract_set_store_data_prepare(&extractCtrl);
940 r = (*recType->extract)(clientData, &extractCtrl);
942 if (r == RECCTRL_EXTRACT_EOF)
944 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
946 /* error occured during extraction ... */
947 yaz_log (YLOG_WARN, "extract error: generic");
950 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
952 /* error occured during extraction ... */
953 yaz_log (YLOG_WARN, "extract error: no such filter");
957 if (extractCtrl.match_criteria[0])
958 match_criteria = extractCtrl.match_criteria;
964 if (match_criteria && *match_criteria) {
965 matchStr = match_criteria;
967 if (zh->m_record_id && *zh->m_record_id) {
968 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
972 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
979 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
980 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
984 assert(*rinfo == sizeof(*sysno));
985 memcpy (sysno, rinfo+1, sizeof(*sysno));
989 if (zebra_rec_keys_empty(zh->reg->keys))
991 /* the extraction process returned no information - the record
992 is probably empty - unless flagShowRecords is in use */
1002 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1003 pr_fname, (long) recordOffset);
1004 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1008 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1009 (long) recordOffset);
1010 rec = rec_new (zh->reg->records);
1012 *sysno = rec->sysno;
1014 recordAttr = rec_init_attr (zh->reg->zei, rec);
1015 recordAttr->staticrank = extractCtrl.staticrank;
1019 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1020 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1021 sizeof(*sysno), sysno);
1024 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1026 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1030 print_rec_keys(zh, zh->reg->keys);
1032 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1033 recordAttr->staticrank);
1034 zh->records_inserted++;
1038 /* record already exists */
1039 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1041 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1043 struct sortKeys sortKeys;
1048 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1049 recordType, pr_fname, (long) recordOffset);
1054 rec = rec_get (zh->reg->records, *sysno);
1057 recordAttr = rec_init_attr (zh->reg->zei, rec);
1059 zebra_rec_keys_set_buf(delkeys,
1060 rec->info[recInfo_delKeys],
1061 rec->size[recInfo_delKeys],
1064 zebra_rec_keys_set_buf(sortKeys,
1065 rec->info[recInfo_sortKeys],
1066 rec->size[recInfo_sortKeys],
1069 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1070 sortKeys.buf = rec->info[recInfo_sortKeys];
1074 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1076 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1078 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1079 recordAttr->staticrank);
1082 /* record going to be deleted */
1083 if (zebra_rec_keys_empty(delkeys))
1085 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1086 pr_fname, (long) recordOffset);
1087 yaz_log (YLOG_WARN, "cannot delete file above, "
1088 "storeKeys false (3)");
1093 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1094 pr_fname, (long) recordOffset);
1095 zh->records_deleted++;
1098 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1099 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1101 rec_del (zh->reg->records, &rec);
1110 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1111 pr_fname, (long) recordOffset);
1112 recordAttr->staticrank = extractCtrl.staticrank;
1114 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1116 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1118 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1119 recordAttr->staticrank);
1120 zh->records_updated++;
1122 zebra_rec_keys_close(delkeys);
1124 zebra_rec_keys_close(sortKeys);
1127 /* update file type */
1128 xfree (rec->info[recInfo_fileType]);
1129 rec->info[recInfo_fileType] =
1130 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1132 /* update filename */
1133 xfree (rec->info[recInfo_filename]);
1134 rec->info[recInfo_filename] =
1135 rec_strdup (fname, &rec->size[recInfo_filename]);
1137 /* update delete keys */
1138 xfree (rec->info[recInfo_delKeys]);
1139 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1141 zebra_rec_keys_get_buf(zh->reg->keys,
1142 &rec->info[recInfo_delKeys],
1143 &rec->size[recInfo_delKeys]);
1147 rec->info[recInfo_delKeys] = NULL;
1148 rec->size[recInfo_delKeys] = 0;
1150 /* update sort keys */
1151 xfree (rec->info[recInfo_sortKeys]);
1154 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1155 &rec->info[recInfo_sortKeys],
1156 &rec->size[recInfo_sortKeys]);
1158 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1159 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1160 zh->reg->sortKeys.buf = NULL;
1161 zh->reg->sortKeys.buf_max = 0;
1164 /* save file size of original record */
1165 zebraExplain_recordBytesIncrement (zh->reg->zei,
1166 - recordAttr->recordSize);
1168 recordAttr->recordSize = fi->file_moffset - recordOffset;
1169 if (!recordAttr->recordSize)
1170 recordAttr->recordSize = fi->file_max - recordOffset;
1172 recordAttr->recordSize = buf_size;
1174 zebraExplain_recordBytesIncrement (zh->reg->zei,
1175 recordAttr->recordSize);
1177 /* set run-number for this record */
1178 recordAttr->runNumber =
1179 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1181 /* update store data */
1182 xfree (rec->info[recInfo_storeData]);
1184 /* update store data */
1185 if (zh->store_data_buf)
1187 rec->size[recInfo_storeData] = zh->store_data_size;
1188 rec->info[recInfo_storeData] = zh->store_data_buf;
1189 zh->store_data_buf = 0;
1191 else if (zh->m_store_data)
1193 rec->size[recInfo_storeData] = recordAttr->recordSize;
1194 rec->info[recInfo_storeData] = (char *)
1195 xmalloc (recordAttr->recordSize);
1196 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1200 rec->info[recInfo_storeData] = NULL;
1201 rec->size[recInfo_storeData] = 0;
1203 /* update database name */
1204 xfree (rec->info[recInfo_databaseName]);
1205 rec->info[recInfo_databaseName] =
1206 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1209 recordAttr->recordOffset = recordOffset;
1211 /* commit this record */
1212 rec_put (zh->reg->records, &rec);
1217 int explain_extract (void *handle, Record rec, data1_node *n)
1219 ZebraHandle zh = (ZebraHandle) handle;
1220 struct recExtractCtrl extractCtrl;
1222 if (zebraExplain_curDatabase (zh->reg->zei,
1223 rec->info[recInfo_databaseName]))
1226 if (zebraExplain_newDatabase (zh->reg->zei,
1227 rec->info[recInfo_databaseName], 0))
1231 zebra_rec_keys_reset(zh->reg->keys);
1234 zebra_rec_keys_reset(zh->reg->sortKeys);
1236 zh->reg->sortKeys.buf_used = 0;
1238 extractCtrl.init = extract_init;
1239 extractCtrl.tokenAdd = extract_token_add;
1240 extractCtrl.schemaAdd = extract_schema_add;
1241 extractCtrl.dh = zh->reg->dh;
1243 init_extractCtrl(zh, &extractCtrl);
1245 extractCtrl.flagShowRecords = 0;
1246 extractCtrl.match_criteria[0] = '\0';
1247 extractCtrl.staticrank = 0;
1248 extractCtrl.handle = handle;
1249 extractCtrl.first_record = 1;
1251 extract_set_store_data_prepare(&extractCtrl);
1254 grs_extract_tree(&extractCtrl, n);
1256 if (rec->size[recInfo_delKeys])
1258 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1261 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1263 struct sortKeys sortkeys;
1266 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1267 rec->size[recInfo_delKeys],
1269 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1270 zebra_rec_keys_close(delkeys);
1272 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1273 rec->size[recInfo_sortKeys],
1276 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1277 zebra_rec_keys_close(sortkeys);
1279 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1280 sortkeys.buf = rec->info[recInfo_sortKeys];
1281 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1284 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1286 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1288 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1291 xfree (rec->info[recInfo_delKeys]);
1292 zebra_rec_keys_get_buf(zh->reg->keys,
1293 &rec->info[recInfo_delKeys],
1294 &rec->size[recInfo_delKeys]);
1296 xfree (rec->info[recInfo_sortKeys]);
1298 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1299 &rec->info[recInfo_sortKeys],
1300 &rec->size[recInfo_sortKeys]);
1302 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1303 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1304 zh->reg->sortKeys.buf = NULL;
1305 zh->reg->sortKeys.buf_max = 0;
1311 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1313 zebra_rec_keys_t reckeys,
1316 ZebraExplainInfo zei = zh->reg->zei;
1318 if (!zh->reg->key_buf)
1320 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1323 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1326 /* FIXME: That "8" should be in a default settings include */
1327 /* not hard-coded here! -H */
1328 zh->reg->key_buf = (char**) xmalloc (mem);
1329 zh->reg->ptr_top = mem/sizeof(char*);
1331 zh->reg->key_buf_used = 0;
1332 zh->reg->key_file_no = 0;
1334 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1336 if (zebra_rec_keys_rewind(reckeys))
1340 struct it_key key_in;
1341 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1344 struct it_key key_out;
1345 zint *keyp = key_out.mem;
1347 assert(key_in.len == 4);
1349 /* check for buffer overflow */
1350 if (zh->reg->key_buf_used + 1024 >
1351 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1352 extract_flushWriteKeys (zh, 0);
1355 assert(zh->reg->ptr_i > 0);
1356 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1357 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1359 /* encode the ordinal value (field/use/attribute) .. */
1360 ch = (int) key_in.mem[0];
1361 zh->reg->key_buf_used +=
1362 key_SU_encode(ch, (char*)zh->reg->key_buf +
1363 zh->reg->key_buf_used);
1365 /* copy the 0-terminated stuff from str to output */
1366 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1367 zh->reg->key_buf_used += slen;
1368 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1370 /* the delete/insert indicator */
1371 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1373 if (zh->m_staticrank) /* rank config enabled ? */
1377 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1381 *keyp++ = staticrank;
1387 if (key_in.mem[1]) /* filter specified record ID */
1388 *keyp++ = key_in.mem[1];
1391 *keyp++ = key_in.mem[2]; /* section_id */
1392 *keyp++ = key_in.mem[3]; /* sequence .. */
1394 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1395 &key_out, sizeof(key_out));
1396 (zh->reg->key_buf_used) += sizeof(key_out);
1401 void extract_flushWriteKeys (ZebraHandle zh, int final)
1402 /* optimizing: if final=1, and no files written yet */
1403 /* push the keys directly to merge, sidestepping the */
1404 /* temp file altogether. Speeds small updates */
1407 char out_fname[200];
1409 struct encode_info encode_info;
1410 int ptr_i = zh->reg->ptr_i;
1415 if (!zh->reg->key_buf || ptr_i <= 0)
1417 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1418 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1419 yaz_log (YLOG_DEBUG, " buf=%p ",
1421 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1422 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1427 (zh->reg->key_file_no)++;
1428 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1429 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1430 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1432 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1433 sizeof(char*), key_qsort_compare);
1435 /* zebra.cfg: tempfiles:
1436 Y: always use temp files (old way)
1437 A: use temp files, if more than one (auto)
1438 = if this is both the last and the first
1439 N: never bother with temp files (new) */
1441 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1442 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1443 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1448 if ( ( temp_policy =='N' ) || /* always from memory */
1449 ( ( temp_policy =='A' ) && /* automatic */
1450 (zh->reg->key_file_no == 1) && /* this is first time */
1451 (final) ) ) /* and last (=only) time */
1452 { /* go directly from memory */
1453 zh->reg->key_file_no =0; /* signal not to read files */
1454 zebra_index_merge(zh);
1456 zh->reg->key_buf_used = 0;
1460 /* Not doing directly from memory, write into a temp file */
1461 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1463 if (!(outf = fopen (out_fname, "wb")))
1465 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1468 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1469 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1471 encode_key_init (&encode_info);
1472 encode_key_write (cp, &encode_info, outf);
1476 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1477 if (strcmp (cp, prevcp))
1479 encode_key_flush ( &encode_info, outf);
1480 encode_key_init (&encode_info);
1481 encode_key_write (cp, &encode_info, outf);
1485 encode_key_write (cp + strlen(cp), &encode_info, outf);
1487 encode_key_flush ( &encode_info, outf);
1489 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1490 extract_get_fname_tmp (out_fname, key_file_no);
1492 if (!(outf = fopen (out_fname, "wb")))
1494 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1497 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1499 prevcp = key_buf[ptr_top-i];
1501 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1503 key_y_len = strlen(prevcp)+1;
1505 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1506 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1508 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1509 sizeof(char*), key_y_compare);
1510 cp = key_buf[ptr_top-ptr_i];
1512 encode_key_init (&encode_info);
1513 encode_key_write (cp, &encode_info, outf);
1516 cp = key_buf[ptr_top-ptr_i];
1517 encode_key_write (cp+key_y_len, &encode_info, outf);
1519 encode_key_flush ( &encode_info, outf);
1522 prevcp = key_buf[ptr_top-ptr_i];
1527 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1530 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1532 zh->reg->key_buf_used = 0;
1535 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1536 zebra_rec_keys_t reckeys,
1537 zebra_snippets *snippets)
1539 NMEM nmem = nmem_create();
1540 if (zebra_rec_keys_rewind(reckeys))
1545 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1547 char dst_buf[IT_MAX_WORD];
1548 char *dst_term = dst_buf;
1551 assert(key.len <= 4 && key.len > 2);
1552 seqno = (int) key.mem[key.len-1];
1555 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1556 0/* db */, 0/* set */, 0/* use */,
1557 0 /* string index */);
1559 zebra_term_untrans_iconv(zh, nmem, index_type,
1561 zebra_snippets_append(snippets, seqno, ord, dst_term);
1569 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1571 yaz_log(YLOG_LOG, "print_rec_keys");
1572 if (zebra_rec_keys_rewind(reckeys))
1577 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1579 char dst_buf[IT_MAX_WORD];
1583 assert(key.len <= 4 && key.len > 2);
1585 zebraExplain_lookup_ord(zh->reg->zei,
1586 key.mem[0], &index_type, &db, 0, 0, 0);
1588 seqno = (int) key.mem[key.len-1];
1590 zebra_term_untrans(zh, index_type, dst_buf, str);
1592 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1593 key.mem[0], seqno, dst_buf);
1598 void extract_add_index_string (RecWord *p, const char *str, int length)
1602 ZebraHandle zh = p->extractCtrl->handle;
1603 ZebraExplainInfo zei = zh->reg->zei;
1608 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1610 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1617 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
1618 p->attrSet, p->attrUse);
1620 ch = zebraExplain_add_attr_su(zei, p->index_type,
1621 p->attrSet, p->attrUse);
1626 key.mem[1] = p->record_id;
1627 key.mem[2] = p->section_id;
1628 key.mem[3] = p->seqno;
1637 for (i = 0; i<length && i < 20; i++)
1638 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1639 /* just for debugging .. */
1640 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1641 "record_id=%lld section_id=%lld seqno=%lld %s",
1642 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1647 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1651 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1655 ZebraHandle zh = p->extractCtrl->handle;
1656 ZebraExplainInfo zei = zh->reg->zei;
1661 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1663 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1671 key.mem[1] = p->record_id;
1672 key.mem[2] = p->section_id;
1673 key.mem[3] = p->seqno;
1675 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1678 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1680 ZebraHandle zh = p->extractCtrl->handle;
1681 struct sortKeys *sk = &zh->reg->sortKeys;
1684 while (off < sk->buf_used)
1688 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1689 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1690 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1692 if (p->attrSet == set && p->attrUse == use)
1695 assert (off == sk->buf_used);
1697 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1701 b = (char *) xmalloc (sk->buf_max += 128000);
1702 if (sk->buf_used > 0)
1703 memcpy (b, sk->buf, sk->buf_used);
1707 off += key_SU_encode(p->attrSet, sk->buf + off);
1708 off += key_SU_encode(p->attrUse, sk->buf + off);
1709 off += key_SU_encode(length, sk->buf + off);
1710 memcpy (sk->buf + off, str, length);
1711 sk->buf_used = off + length;
1715 void extract_add_string (RecWord *p, const char *string, int length)
1717 assert (length > 0);
1718 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1719 extract_add_sort_string (p, string, length);
1721 extract_add_index_string (p, string, length);
1724 static void extract_add_incomplete_field (RecWord *p)
1726 const char *b = p->term_buf;
1727 int remain = p->term_len;
1728 const char **map = 0;
1730 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1733 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1737 char buf[IT_MAX_WORD+1];
1741 while (map && *map && **map == *CHR_SPACE)
1743 remain = p->term_len - (b - p->term_buf);
1745 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1753 while (map && *map && **map != *CHR_SPACE)
1755 const char *cp = *map;
1757 while (i < IT_MAX_WORD && *cp)
1759 remain = p->term_len - (b - p->term_buf);
1761 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1767 extract_add_string (p, buf, i);
1772 static void extract_add_complete_field (RecWord *p)
1774 const char *b = p->term_buf;
1775 char buf[IT_MAX_WORD+1];
1776 const char **map = 0;
1777 int i = 0, remain = p->term_len;
1779 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1780 p->term_len, p->term_buf);
1783 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1785 while (remain > 0 && i < IT_MAX_WORD)
1787 while (map && *map && **map == *CHR_SPACE)
1789 remain = p->term_len - (b - p->term_buf);
1793 int first = i ? 0 : 1; /* first position */
1794 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1802 if (i && i < IT_MAX_WORD)
1803 buf[i++] = *CHR_SPACE;
1804 while (map && *map && **map != *CHR_SPACE)
1806 const char *cp = *map;
1808 if (**map == *CHR_CUT)
1814 if (i >= IT_MAX_WORD)
1816 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1817 while (i < IT_MAX_WORD && *cp)
1820 remain = p->term_len - (b - p->term_buf);
1823 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1832 extract_add_string (p, buf, i);
1835 void extract_token_add (RecWord *p)
1839 yaz_log (YLOG_LOG, "token_add "
1840 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1841 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1844 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1845 p->term_buf, p->term_len)))
1847 p->term_buf = wrbuf_buf(wrbuf);
1848 p->term_len = wrbuf_len(wrbuf);
1850 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1851 extract_add_complete_field (p);
1853 extract_add_incomplete_field(p);
1856 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1857 void *buf, size_t sz)
1859 ZebraHandle zh = (ZebraHandle) p->handle;
1861 xfree(zh->store_data_buf);
1862 zh->store_data_buf = 0;
1863 zh->store_data_size = 0;
1866 zh->store_data_buf = xmalloc(sz);
1867 zh->store_data_size = sz;
1868 memcpy(zh->store_data_buf, buf, sz);
1872 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1874 ZebraHandle zh = (ZebraHandle) p->handle;
1875 xfree(zh->store_data_buf);
1876 zh->store_data_buf = 0;
1877 zh->store_data_size = 0;
1878 p->setStoreData = extract_set_store_data_cb;
1881 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1883 ZebraHandle zh = (ZebraHandle) p->handle;
1884 zebraExplain_addSchema (zh->reg->zei, oid);
1888 #error not done yet with zebra_rec_keys_t
1889 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1890 int cmd, struct recKeys *reckeys)
1892 SortIdx sortIdx = zh->reg->sortIdx;
1893 void *decode_handle = iscz1_start();
1897 while (off < reckeys->buf_used)
1899 const char *src = reckeys->buf + off;
1901 char *dst = (char*) &key;
1903 iscz1_decode(decode_handle, &dst, &src);
1904 assert(key.len == 4);
1906 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
1908 sortIdx_type(sortIdx, ch);
1910 sortIdx_add(sortIdx, src, strlen(src));
1912 sortIdx_add(sortIdx, "", 1);
1917 off = src - reckeys->buf;
1919 assert (off == reckeys->buf_used);
1920 iscz1_stop(decode_handle);
1923 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1924 int cmd, struct sortKeys *sk)
1926 SortIdx sortIdx = zh->reg->sortIdx;
1929 sortIdx_sysno (sortIdx, sysno);
1931 while (off < sk->buf_used)
1935 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1936 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1937 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1939 sortIdx_type(sortIdx, use);
1941 sortIdx_add(sortIdx, sk->buf + off, slen);
1943 sortIdx_add(sortIdx, "", 1);
1949 void encode_key_init (struct encode_info *i)
1958 i->encode_handle = iscz1_start();
1959 i->decode_handle = iscz1_start();
1965 /* this is the old encode_key_write
1966 * may be deleted once we are confident that the new works
1969 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1972 char *bp = i->buf, *bp0;
1973 const char *src = (char *) &key;
1975 /* copy term to output buf */
1976 while ((*bp++ = *k++))
1978 /* and copy & align key so we can mangle */
1979 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
1983 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
1985 assert(key.mem[0] >= 0);
1988 iscz1_encode(i->encode_handle, &bp, &src);
1990 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
1991 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1993 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2002 const char *src = bp0+1;
2003 char *dst = (char*) &key2;
2004 iscz1_decode(i->decode_handle, &dst, &src);
2006 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
2008 assert(key2.mem[1]);
2013 void encode_key_flush (struct encode_info *i, FILE *outf)
2015 iscz1_stop(i->encode_handle);
2016 iscz1_stop(i->decode_handle);
2021 /* new encode_key_write
2022 * The idea is to buffer one more key, and compare them
2023 * If we are going to delete and insert the same key,
2024 * we may as well not bother. Should make a difference in
2025 * updates with small modifications (appending to a mbox)
2027 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2032 if (*k) /* first time for new key */
2035 while ((*bp++ = *k++))
2037 i->keylen= bp - i->buf -1;
2038 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2042 bp=i->buf + i->keylen;
2047 memcpy (&key, k+1, sizeof(struct it_key));
2048 if (0==i->prevsys) /* no previous filter, fill up */
2050 i->prevsys=key.sysno;
2051 i->prevseq=key.seqno;
2054 else if ( (i->prevsys==key.sysno) &&
2055 (i->prevseq==key.seqno) &&
2057 { /* same numbers, diff cmd, they cancel out */
2061 { /* different stuff, write previous, move buf */
2062 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2063 if (i->sysno != i->prevsys)
2065 i->sysno = i->prevsys;
2068 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2070 return; /* ??? Filters some sort of duplicates away */
2071 /* ??? Can this ever happen -H 15oct02 */
2073 bp = encode_key_int (i->prevseq - i->seqno, bp);
2074 i->seqno = i->prevseq;
2075 i->cmd = i->prevcmd;
2076 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2078 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2081 i->keylen=0; /* ok, it's written, forget it */
2082 i->prevsys=key.sysno;
2083 i->prevseq=key.seqno;
2088 void encode_key_flush (struct encode_info *i, FILE *outf)
2089 { /* flush the last key from i */
2090 char *bp =i->buf + i->keylen;
2093 return; /* nothing to flush */
2096 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2097 if (i->sysno != i->prevsys)
2099 i->sysno = i->prevsys;
2102 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2104 return; /* ??? Filters some sort of duplicates away */
2105 /* ??? Can this ever happen -H 15oct02 */
2107 bp = encode_key_int (i->prevseq - i->seqno, bp);
2108 i->seqno = i->prevseq;
2109 i->cmd = i->prevcmd;
2110 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2112 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2115 i->keylen=0; /* ok, it's written, forget it */
2116 i->prevsys=0; /* forget the values too */