1 /* $Id: extract.c,v 1.208 2006-05-09 11:31:37 marc 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)");
530 rec = rec_new (zh->reg->records);
534 if (zh->records_processed < zh->m_file_verbose_limit)
536 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
537 " " ZINT_FORMAT " %s" ,
539 fname, recordOffset, *sysno, matchStr);
541 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
544 fname, recordOffset, *sysno);
546 recordAttr = rec_init_attr (zh->reg->zei, rec);
547 recordAttr->staticrank = extractCtrl.staticrank;
551 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
552 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
553 sizeof(*sysno), sysno);
558 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
560 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
562 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
563 recordAttr->staticrank);
564 zh->records_inserted++;
568 /* record already exists */
569 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
572 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
574 struct sortKeys sortKeys;
577 rec = rec_get (zh->reg->records, *sysno);
580 recordAttr = rec_init_attr (zh->reg->zei, rec);
582 zebra_rec_keys_set_buf(delkeys,
583 rec->info[recInfo_delKeys],
584 rec->size[recInfo_delKeys],
588 zebra_rec_keys_set_buf(sortKeys,
589 rec->info[recInfo_sortKeys],
590 rec->size[recInfo_sortKeys],
592 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
594 sortKeys.buf_used = rec->size[recInfo_sortKeys];
595 sortKeys.buf = rec->info[recInfo_sortKeys];
596 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
599 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
600 recordAttr->staticrank); /* old values */
603 /* record going to be deleted */
604 if (zebra_rec_keys_empty(delkeys))
606 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T
608 zh->m_record_type, fname, recordOffset, *sysno);
609 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
613 if (zh->records_processed < zh->m_file_verbose_limit)
615 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
616 " " ZINT_FORMAT " %s" ,
618 fname, recordOffset, *sysno, matchStr);
620 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
623 fname, recordOffset, *sysno);
627 zh->records_deleted++;
630 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
631 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
633 rec_del (zh->reg->records, &rec);
641 /* flush new keys for sort&search etc */
642 if (zh->records_processed < zh->m_file_verbose_limit)
644 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
645 " " ZINT_FORMAT " %s" ,
647 fname, recordOffset, *sysno, matchStr);
649 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
652 fname, recordOffset, *sysno);
654 recordAttr->staticrank = extractCtrl.staticrank;
656 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
658 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
660 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
661 recordAttr->staticrank);
662 zh->records_updated++;
664 zebra_rec_keys_close(delkeys);
666 zebra_rec_keys_close(sortKeys);
669 /* update file type */
670 xfree (rec->info[recInfo_fileType]);
671 rec->info[recInfo_fileType] =
672 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
674 /* update filename */
675 xfree (rec->info[recInfo_filename]);
676 rec->info[recInfo_filename] =
677 rec_strdup (fname, &rec->size[recInfo_filename]);
679 /* update delete keys */
680 xfree (rec->info[recInfo_delKeys]);
681 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
683 zebra_rec_keys_get_buf(zh->reg->keys,
684 &rec->info[recInfo_delKeys],
685 &rec->size[recInfo_delKeys]);
689 rec->info[recInfo_delKeys] = NULL;
690 rec->size[recInfo_delKeys] = 0;
693 /* update sort keys */
694 xfree (rec->info[recInfo_sortKeys]);
697 zebra_rec_keys_get_buf(zh->reg->sortKeys,
698 &rec->info[recInfo_sortKeys],
699 &rec->size[recInfo_sortKeys]);
701 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
702 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
703 zh->reg->sortKeys.buf = NULL;
704 zh->reg->sortKeys.buf_max = 0;
707 /* save file size of original record */
708 zebraExplain_recordBytesIncrement (zh->reg->zei,
709 - recordAttr->recordSize);
710 recordAttr->recordSize = fi->file_moffset - recordOffset;
711 if (!recordAttr->recordSize)
712 recordAttr->recordSize = fi->file_max - recordOffset;
713 zebraExplain_recordBytesIncrement (zh->reg->zei,
714 recordAttr->recordSize);
716 /* set run-number for this record */
717 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
720 /* update store data */
721 xfree (rec->info[recInfo_storeData]);
722 if (zh->store_data_buf)
724 rec->size[recInfo_storeData] = zh->store_data_size;
725 rec->info[recInfo_storeData] = zh->store_data_buf;
726 zh->store_data_buf = 0;
728 else if (zh->m_store_data)
730 rec->size[recInfo_storeData] = recordAttr->recordSize;
731 rec->info[recInfo_storeData] = (char *)
732 xmalloc (recordAttr->recordSize);
733 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
735 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
736 recordOffset, fname);
739 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
740 < recordAttr->recordSize)
742 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
743 recordAttr->recordSize, fname);
749 rec->info[recInfo_storeData] = NULL;
750 rec->size[recInfo_storeData] = 0;
752 /* update database name */
753 xfree (rec->info[recInfo_databaseName]);
754 rec->info[recInfo_databaseName] =
755 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
758 recordAttr->recordOffset = recordOffset;
760 /* commit this record */
761 rec_put (zh->reg->records, &rec);
766 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
769 ZEBRA_RES r = ZEBRA_OK;
774 struct file_read_info *fi;
775 const char *original_record_type = 0;
777 void *recTypeClientData;
779 if (!zh->m_group || !*zh->m_group)
782 sprintf (gprefix, "%s.", zh->m_group);
784 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
786 /* determine file extension */
788 for (i = strlen(fname); --i >= 0; )
791 else if (fname[i] == '.')
793 strcpy (ext, fname+i+1);
796 /* determine file type - depending on extension */
797 original_record_type = zh->m_record_type;
798 if (!zh->m_record_type)
800 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
801 zh->m_record_type = res_get (zh->res, ext_res);
803 if (!zh->m_record_type)
805 if (zh->records_processed < zh->m_file_verbose_limit)
806 yaz_log (YLOG_LOG, "? %s", fname);
809 /* determine match criteria */
810 if (!zh->m_record_id)
812 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
813 zh->m_record_id = res_get (zh->res, ext_res);
817 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
818 &recTypeClientData)))
820 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
824 switch(recType->version)
829 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
831 if (sysno && deleteFlag)
837 if (zh->path_reg && !yaz_is_abspath (fname))
839 strcpy (full_rep, zh->path_reg);
840 strcat (full_rep, "/");
841 strcat (full_rep, fname);
844 strcpy (full_rep, fname);
846 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
848 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
849 zh->m_record_type = original_record_type;
853 fi = file_read_start (fd);
856 fi->file_moffset = fi->file_offset;
857 fi->file_more = 0; /* file_end not called (yet) */
858 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
859 recType, recTypeClientData);
861 { /* file_end has been called so reset offset .. */
862 fi->file_offset = fi->file_moffset;
863 lseek(fi->fd, fi->file_moffset, SEEK_SET);
877 zh->m_record_type = original_record_type;
882 If sysno is provided, then it's used to identify the reocord.
883 If not, and match_criteria is provided, then sysno is guessed
884 If not, and a record is provided, then sysno is got from there
887 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
888 const char *buf, size_t buf_size,
891 const char *recordType,
893 const char *match_criteria,
899 RecordAttr *recordAttr;
900 struct recExtractCtrl extractCtrl;
902 const char *matchStr = 0;
903 RecType recType = NULL;
906 long recordOffset = 0;
907 struct zebra_fetch_control fc;
908 const char *pr_fname = fname; /* filename to print .. */
909 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
912 pr_fname = "<no file>"; /* make it printable if file is omitted */
915 fc.record_int_buf = buf;
916 fc.record_int_len = buf_size;
917 fc.record_int_pos = 0;
919 fc.record_offset = 0;
921 extractCtrl.offset = 0;
922 extractCtrl.readf = zebra_record_int_read;
923 extractCtrl.seekf = zebra_record_int_seek;
924 extractCtrl.tellf = zebra_record_int_tell;
925 extractCtrl.endf = zebra_record_int_end;
926 extractCtrl.first_record = 1;
927 extractCtrl.fh = &fc;
929 zebra_rec_keys_reset(zh->reg->keys);
932 zebra_rec_keys_reset(zh->reg->sortKeys);
934 zh->reg->sortKeys.buf_used = 0;
936 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
938 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
939 zh->m_explain_database))
943 if (recordType && *recordType)
945 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
946 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
951 if (!(zh->m_record_type))
953 yaz_log (YLOG_WARN, "No such record type defined");
956 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
957 recType = recType_byName (zh->reg->recTypes, zh->res,
958 zh->m_record_type, &clientData);
959 recordType = zh->m_record_type;
964 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
968 extractCtrl.init = extract_init;
969 extractCtrl.tokenAdd = extract_token_add;
970 extractCtrl.schemaAdd = extract_schema_add;
971 extractCtrl.dh = zh->reg->dh;
972 extractCtrl.handle = zh;
973 extractCtrl.match_criteria[0] = '\0';
974 extractCtrl.staticrank = 0;
976 init_extractCtrl(zh, &extractCtrl);
978 extract_set_store_data_prepare(&extractCtrl);
980 r = (*recType->extract)(clientData, &extractCtrl);
982 if (r == RECCTRL_EXTRACT_EOF)
984 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
986 /* error occured during extraction ... */
987 yaz_log (YLOG_WARN, "extract error: generic");
990 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
992 /* error occured during extraction ... */
993 yaz_log (YLOG_WARN, "extract error: no such filter");
997 if (extractCtrl.match_criteria[0])
998 match_criteria = extractCtrl.match_criteria;
1004 if (match_criteria && *match_criteria) {
1005 matchStr = match_criteria;
1007 if (zh->m_record_id && *zh->m_record_id) {
1008 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1012 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1019 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1020 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1024 assert(*rinfo == sizeof(*sysno));
1025 memcpy (sysno, rinfo+1, sizeof(*sysno));
1029 if (zebra_rec_keys_empty(zh->reg->keys))
1031 /* the extraction process returned no information - the record
1032 is probably empty - unless flagShowRecords is in use */
1042 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1043 pr_fname, (long) recordOffset);
1044 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1048 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1049 (long) recordOffset);
1050 rec = rec_new (zh->reg->records);
1052 *sysno = rec->sysno;
1054 recordAttr = rec_init_attr (zh->reg->zei, rec);
1055 recordAttr->staticrank = extractCtrl.staticrank;
1059 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1060 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1061 sizeof(*sysno), sysno);
1066 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1068 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1072 print_rec_keys(zh, zh->reg->keys);
1074 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1075 recordAttr->staticrank);
1076 zh->records_inserted++;
1080 /* record already exists */
1081 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1083 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1085 struct sortKeys sortKeys;
1090 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1091 recordType, pr_fname, (long) recordOffset);
1096 rec = rec_get (zh->reg->records, *sysno);
1099 recordAttr = rec_init_attr (zh->reg->zei, rec);
1101 zebra_rec_keys_set_buf(delkeys,
1102 rec->info[recInfo_delKeys],
1103 rec->size[recInfo_delKeys],
1106 zebra_rec_keys_set_buf(sortKeys,
1107 rec->info[recInfo_sortKeys],
1108 rec->size[recInfo_sortKeys],
1111 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1112 sortKeys.buf = rec->info[recInfo_sortKeys];
1116 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1118 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1120 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1121 recordAttr->staticrank);
1124 /* record going to be deleted */
1125 if (zebra_rec_keys_empty(delkeys))
1127 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1128 pr_fname, (long) recordOffset);
1129 yaz_log (YLOG_WARN, "cannot delete file above, "
1130 "storeKeys false (3)");
1135 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1136 pr_fname, (long) recordOffset);
1137 zh->records_deleted++;
1140 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1141 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1143 rec_del (zh->reg->records, &rec);
1152 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1153 pr_fname, (long) recordOffset);
1154 recordAttr->staticrank = extractCtrl.staticrank;
1156 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1158 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1160 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1161 recordAttr->staticrank);
1162 zh->records_updated++;
1164 zebra_rec_keys_close(delkeys);
1166 zebra_rec_keys_close(sortKeys);
1169 /* update file type */
1170 xfree (rec->info[recInfo_fileType]);
1171 rec->info[recInfo_fileType] =
1172 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1174 /* update filename */
1175 xfree (rec->info[recInfo_filename]);
1176 rec->info[recInfo_filename] =
1177 rec_strdup (fname, &rec->size[recInfo_filename]);
1179 /* update delete keys */
1180 xfree (rec->info[recInfo_delKeys]);
1181 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1183 zebra_rec_keys_get_buf(zh->reg->keys,
1184 &rec->info[recInfo_delKeys],
1185 &rec->size[recInfo_delKeys]);
1189 rec->info[recInfo_delKeys] = NULL;
1190 rec->size[recInfo_delKeys] = 0;
1192 /* update sort keys */
1193 xfree (rec->info[recInfo_sortKeys]);
1196 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1197 &rec->info[recInfo_sortKeys],
1198 &rec->size[recInfo_sortKeys]);
1200 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1201 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1202 zh->reg->sortKeys.buf = NULL;
1203 zh->reg->sortKeys.buf_max = 0;
1206 /* save file size of original record */
1207 zebraExplain_recordBytesIncrement (zh->reg->zei,
1208 - recordAttr->recordSize);
1210 recordAttr->recordSize = fi->file_moffset - recordOffset;
1211 if (!recordAttr->recordSize)
1212 recordAttr->recordSize = fi->file_max - recordOffset;
1214 recordAttr->recordSize = buf_size;
1216 zebraExplain_recordBytesIncrement (zh->reg->zei,
1217 recordAttr->recordSize);
1219 /* set run-number for this record */
1220 recordAttr->runNumber =
1221 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1223 /* update store data */
1224 xfree (rec->info[recInfo_storeData]);
1226 /* update store data */
1227 if (zh->store_data_buf)
1229 rec->size[recInfo_storeData] = zh->store_data_size;
1230 rec->info[recInfo_storeData] = zh->store_data_buf;
1231 zh->store_data_buf = 0;
1233 else if (zh->m_store_data)
1235 rec->size[recInfo_storeData] = recordAttr->recordSize;
1236 rec->info[recInfo_storeData] = (char *)
1237 xmalloc (recordAttr->recordSize);
1238 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1242 rec->info[recInfo_storeData] = NULL;
1243 rec->size[recInfo_storeData] = 0;
1245 /* update database name */
1246 xfree (rec->info[recInfo_databaseName]);
1247 rec->info[recInfo_databaseName] =
1248 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1251 recordAttr->recordOffset = recordOffset;
1253 /* commit this record */
1254 rec_put (zh->reg->records, &rec);
1259 int explain_extract (void *handle, Record rec, data1_node *n)
1261 ZebraHandle zh = (ZebraHandle) handle;
1262 struct recExtractCtrl extractCtrl;
1264 if (zebraExplain_curDatabase (zh->reg->zei,
1265 rec->info[recInfo_databaseName]))
1268 if (zebraExplain_newDatabase (zh->reg->zei,
1269 rec->info[recInfo_databaseName], 0))
1273 zebra_rec_keys_reset(zh->reg->keys);
1276 zebra_rec_keys_reset(zh->reg->sortKeys);
1278 zh->reg->sortKeys.buf_used = 0;
1280 extractCtrl.init = extract_init;
1281 extractCtrl.tokenAdd = extract_token_add;
1282 extractCtrl.schemaAdd = extract_schema_add;
1283 extractCtrl.dh = zh->reg->dh;
1285 init_extractCtrl(zh, &extractCtrl);
1287 extractCtrl.flagShowRecords = 0;
1288 extractCtrl.match_criteria[0] = '\0';
1289 extractCtrl.staticrank = 0;
1290 extractCtrl.handle = handle;
1291 extractCtrl.first_record = 1;
1293 extract_set_store_data_prepare(&extractCtrl);
1296 grs_extract_tree(&extractCtrl, n);
1298 if (rec->size[recInfo_delKeys])
1300 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1303 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1305 struct sortKeys sortkeys;
1308 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1309 rec->size[recInfo_delKeys],
1311 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1312 zebra_rec_keys_close(delkeys);
1314 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1315 rec->size[recInfo_sortKeys],
1318 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1319 zebra_rec_keys_close(sortkeys);
1321 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1322 sortkeys.buf = rec->info[recInfo_sortKeys];
1323 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1326 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1328 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1330 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1333 xfree (rec->info[recInfo_delKeys]);
1334 zebra_rec_keys_get_buf(zh->reg->keys,
1335 &rec->info[recInfo_delKeys],
1336 &rec->size[recInfo_delKeys]);
1338 xfree (rec->info[recInfo_sortKeys]);
1340 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1341 &rec->info[recInfo_sortKeys],
1342 &rec->size[recInfo_sortKeys]);
1344 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1345 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1346 zh->reg->sortKeys.buf = NULL;
1347 zh->reg->sortKeys.buf_max = 0;
1353 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1355 zebra_rec_keys_t reckeys,
1358 ZebraExplainInfo zei = zh->reg->zei;
1360 if (!zh->reg->key_buf)
1362 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1365 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1368 /* FIXME: That "8" should be in a default settings include */
1369 /* not hard-coded here! -H */
1370 zh->reg->key_buf = (char**) xmalloc (mem);
1371 zh->reg->ptr_top = mem/sizeof(char*);
1373 zh->reg->key_buf_used = 0;
1374 zh->reg->key_file_no = 0;
1376 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1378 if (zebra_rec_keys_rewind(reckeys))
1382 struct it_key key_in;
1383 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1386 struct it_key key_out;
1387 zint *keyp = key_out.mem;
1389 assert(key_in.len == 4);
1391 /* check for buffer overflow */
1392 if (zh->reg->key_buf_used + 1024 >
1393 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1394 extract_flushWriteKeys (zh, 0);
1397 assert(zh->reg->ptr_i > 0);
1398 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1399 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1401 /* encode the ordinal value (field/use/attribute) .. */
1402 ch = (int) key_in.mem[0];
1403 zh->reg->key_buf_used +=
1404 key_SU_encode(ch, (char*)zh->reg->key_buf +
1405 zh->reg->key_buf_used);
1407 /* copy the 0-terminated stuff from str to output */
1408 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1409 zh->reg->key_buf_used += slen;
1410 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1412 /* the delete/insert indicator */
1413 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1415 if (zh->m_staticrank) /* rank config enabled ? */
1419 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1423 *keyp++ = staticrank;
1429 if (key_in.mem[1]) /* filter specified record ID */
1430 *keyp++ = key_in.mem[1];
1433 *keyp++ = key_in.mem[2]; /* section_id */
1434 *keyp++ = key_in.mem[3]; /* sequence .. */
1436 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1437 &key_out, sizeof(key_out));
1438 (zh->reg->key_buf_used) += sizeof(key_out);
1443 void extract_flushWriteKeys (ZebraHandle zh, int final)
1444 /* optimizing: if final=1, and no files written yet */
1445 /* push the keys directly to merge, sidestepping the */
1446 /* temp file altogether. Speeds small updates */
1449 char out_fname[200];
1451 struct encode_info encode_info;
1452 int ptr_i = zh->reg->ptr_i;
1457 if (!zh->reg->key_buf || ptr_i <= 0)
1459 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1460 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1461 yaz_log (YLOG_DEBUG, " buf=%p ",
1463 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1464 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1469 (zh->reg->key_file_no)++;
1470 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1471 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1472 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1474 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1475 sizeof(char*), key_qsort_compare);
1477 /* zebra.cfg: tempfiles:
1478 Y: always use temp files (old way)
1479 A: use temp files, if more than one (auto)
1480 = if this is both the last and the first
1481 N: never bother with temp files (new) */
1483 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1484 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1485 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1490 if ( ( temp_policy =='N' ) || /* always from memory */
1491 ( ( temp_policy =='A' ) && /* automatic */
1492 (zh->reg->key_file_no == 1) && /* this is first time */
1493 (final) ) ) /* and last (=only) time */
1494 { /* go directly from memory */
1495 zh->reg->key_file_no =0; /* signal not to read files */
1496 zebra_index_merge(zh);
1498 zh->reg->key_buf_used = 0;
1502 /* Not doing directly from memory, write into a temp file */
1503 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1505 if (!(outf = fopen (out_fname, "wb")))
1507 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1510 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1511 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1513 encode_key_init (&encode_info);
1514 encode_key_write (cp, &encode_info, outf);
1518 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1519 if (strcmp (cp, prevcp))
1521 encode_key_flush ( &encode_info, outf);
1522 encode_key_init (&encode_info);
1523 encode_key_write (cp, &encode_info, outf);
1527 encode_key_write (cp + strlen(cp), &encode_info, outf);
1529 encode_key_flush ( &encode_info, outf);
1531 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1532 extract_get_fname_tmp (out_fname, key_file_no);
1534 if (!(outf = fopen (out_fname, "wb")))
1536 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1539 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1541 prevcp = key_buf[ptr_top-i];
1543 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1545 key_y_len = strlen(prevcp)+1;
1547 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1548 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1550 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1551 sizeof(char*), key_y_compare);
1552 cp = key_buf[ptr_top-ptr_i];
1554 encode_key_init (&encode_info);
1555 encode_key_write (cp, &encode_info, outf);
1558 cp = key_buf[ptr_top-ptr_i];
1559 encode_key_write (cp+key_y_len, &encode_info, outf);
1561 encode_key_flush ( &encode_info, outf);
1564 prevcp = key_buf[ptr_top-ptr_i];
1569 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1572 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1574 zh->reg->key_buf_used = 0;
1577 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1578 zebra_rec_keys_t reckeys,
1579 zebra_snippets *snippets)
1581 NMEM nmem = nmem_create();
1582 if (zebra_rec_keys_rewind(reckeys))
1587 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1589 char dst_buf[IT_MAX_WORD];
1590 char *dst_term = dst_buf;
1593 assert(key.len <= 4 && key.len > 2);
1594 seqno = (int) key.mem[key.len-1];
1597 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1598 0/* db */, 0/* set */, 0/* use */,
1599 0 /* string_index */);
1601 zebra_term_untrans_iconv(zh, nmem, index_type,
1603 zebra_snippets_append(snippets, seqno, ord, dst_term);
1611 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1613 yaz_log(YLOG_LOG, "print_rec_keys");
1614 if (zebra_rec_keys_rewind(reckeys))
1619 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1621 char dst_buf[IT_MAX_WORD];
1625 assert(key.len <= 4 && key.len > 2);
1627 zebraExplain_lookup_ord(zh->reg->zei,
1628 key.mem[0], &index_type, &db, 0, 0, 0);
1630 seqno = (int) key.mem[key.len-1];
1632 zebra_term_untrans(zh, index_type, dst_buf, str);
1634 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1635 key.mem[0], seqno, dst_buf);
1640 void extract_add_index_string (RecWord *p, const char *str, int length)
1644 ZebraHandle zh = p->extractCtrl->handle;
1645 ZebraExplainInfo zei = zh->reg->zei;
1650 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1652 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1659 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
1660 p->attrSet, p->attrUse);
1662 ch = zebraExplain_add_attr_su(zei, p->index_type,
1663 p->attrSet, p->attrUse);
1668 key.mem[1] = p->record_id;
1669 key.mem[2] = p->section_id;
1670 key.mem[3] = p->seqno;
1679 for (i = 0; i<length && i < 20; i++)
1680 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1681 /* just for debugging .. */
1682 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1683 "record_id=%lld section_id=%lld seqno=%lld %s",
1684 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1689 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1693 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1697 ZebraHandle zh = p->extractCtrl->handle;
1698 ZebraExplainInfo zei = zh->reg->zei;
1703 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1705 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1713 key.mem[1] = p->record_id;
1714 key.mem[2] = p->section_id;
1715 key.mem[3] = p->seqno;
1717 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1720 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1722 ZebraHandle zh = p->extractCtrl->handle;
1723 struct sortKeys *sk = &zh->reg->sortKeys;
1726 while (off < sk->buf_used)
1730 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1731 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1732 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1734 if (p->attrSet == set && p->attrUse == use)
1737 assert (off == sk->buf_used);
1739 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1743 b = (char *) xmalloc (sk->buf_max += 128000);
1744 if (sk->buf_used > 0)
1745 memcpy (b, sk->buf, sk->buf_used);
1749 off += key_SU_encode(p->attrSet, sk->buf + off);
1750 off += key_SU_encode(p->attrUse, sk->buf + off);
1751 off += key_SU_encode(length, sk->buf + off);
1752 memcpy (sk->buf + off, str, length);
1753 sk->buf_used = off + length;
1757 void extract_add_string (RecWord *p, const char *string, int length)
1759 assert (length > 0);
1760 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1761 extract_add_sort_string (p, string, length);
1763 extract_add_index_string (p, string, length);
1766 static void extract_add_incomplete_field (RecWord *p)
1768 const char *b = p->term_buf;
1769 int remain = p->term_len;
1770 const char **map = 0;
1772 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1775 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1779 char buf[IT_MAX_WORD+1];
1783 while (map && *map && **map == *CHR_SPACE)
1785 remain = p->term_len - (b - p->term_buf);
1787 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1795 while (map && *map && **map != *CHR_SPACE)
1797 const char *cp = *map;
1799 while (i < IT_MAX_WORD && *cp)
1801 remain = p->term_len - (b - p->term_buf);
1803 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1809 extract_add_string (p, buf, i);
1814 static void extract_add_complete_field (RecWord *p)
1816 const char *b = p->term_buf;
1817 char buf[IT_MAX_WORD+1];
1818 const char **map = 0;
1819 int i = 0, remain = p->term_len;
1821 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1822 p->term_len, p->term_buf);
1825 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1827 while (remain > 0 && i < IT_MAX_WORD)
1829 while (map && *map && **map == *CHR_SPACE)
1831 remain = p->term_len - (b - p->term_buf);
1835 int first = i ? 0 : 1; /* first position */
1836 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1844 if (i && i < IT_MAX_WORD)
1845 buf[i++] = *CHR_SPACE;
1846 while (map && *map && **map != *CHR_SPACE)
1848 const char *cp = *map;
1850 if (**map == *CHR_CUT)
1856 if (i >= IT_MAX_WORD)
1858 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1859 while (i < IT_MAX_WORD && *cp)
1862 remain = p->term_len - (b - p->term_buf);
1865 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1874 extract_add_string (p, buf, i);
1877 void extract_token_add (RecWord *p)
1881 yaz_log (YLOG_LOG, "token_add "
1882 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1883 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1886 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1887 p->term_buf, p->term_len)))
1889 p->term_buf = wrbuf_buf(wrbuf);
1890 p->term_len = wrbuf_len(wrbuf);
1892 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1893 extract_add_complete_field (p);
1895 extract_add_incomplete_field(p);
1898 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1899 void *buf, size_t sz)
1901 ZebraHandle zh = (ZebraHandle) p->handle;
1903 xfree(zh->store_data_buf);
1904 zh->store_data_buf = 0;
1905 zh->store_data_size = 0;
1908 zh->store_data_buf = xmalloc(sz);
1909 zh->store_data_size = sz;
1910 memcpy(zh->store_data_buf, buf, sz);
1914 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1916 ZebraHandle zh = (ZebraHandle) p->handle;
1917 xfree(zh->store_data_buf);
1918 zh->store_data_buf = 0;
1919 zh->store_data_size = 0;
1920 p->setStoreData = extract_set_store_data_cb;
1923 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1925 ZebraHandle zh = (ZebraHandle) p->handle;
1926 zebraExplain_addSchema (zh->reg->zei, oid);
1930 #error not done yet with zebra_rec_keys_t
1931 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1932 int cmd, struct recKeys *reckeys)
1934 SortIdx sortIdx = zh->reg->sortIdx;
1935 void *decode_handle = iscz1_start();
1939 while (off < reckeys->buf_used)
1941 const char *src = reckeys->buf + off;
1943 char *dst = (char*) &key;
1945 iscz1_decode(decode_handle, &dst, &src);
1946 assert(key.len == 4);
1948 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
1950 sortIdx_type(sortIdx, ch);
1952 sortIdx_add(sortIdx, src, strlen(src));
1954 sortIdx_add(sortIdx, "", 1);
1959 off = src - reckeys->buf;
1961 assert (off == reckeys->buf_used);
1962 iscz1_stop(decode_handle);
1965 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1966 int cmd, struct sortKeys *sk)
1968 SortIdx sortIdx = zh->reg->sortIdx;
1971 sortIdx_sysno (sortIdx, sysno);
1973 while (off < sk->buf_used)
1977 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1978 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1979 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1981 sortIdx_type(sortIdx, use);
1983 sortIdx_add(sortIdx, sk->buf + off, slen);
1985 sortIdx_add(sortIdx, "", 1);
1991 void encode_key_init (struct encode_info *i)
2000 i->encode_handle = iscz1_start();
2001 i->decode_handle = iscz1_start();
2007 /* this is the old encode_key_write
2008 * may be deleted once we are confident that the new works
2011 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2014 char *bp = i->buf, *bp0;
2015 const char *src = (char *) &key;
2017 /* copy term to output buf */
2018 while ((*bp++ = *k++))
2020 /* and copy & align key so we can mangle */
2021 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
2025 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
2027 assert(key.mem[0] >= 0);
2030 iscz1_encode(i->encode_handle, &bp, &src);
2032 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
2033 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2035 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2044 const char *src = bp0+1;
2045 char *dst = (char*) &key2;
2046 iscz1_decode(i->decode_handle, &dst, &src);
2048 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
2050 assert(key2.mem[1]);
2055 void encode_key_flush (struct encode_info *i, FILE *outf)
2057 iscz1_stop(i->encode_handle);
2058 iscz1_stop(i->decode_handle);
2063 /* new encode_key_write
2064 * The idea is to buffer one more key, and compare them
2065 * If we are going to delete and insert the same key,
2066 * we may as well not bother. Should make a difference in
2067 * updates with small modifications (appending to a mbox)
2069 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2074 if (*k) /* first time for new key */
2077 while ((*bp++ = *k++))
2079 i->keylen= bp - i->buf -1;
2080 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2084 bp=i->buf + i->keylen;
2089 memcpy (&key, k+1, sizeof(struct it_key));
2090 if (0==i->prevsys) /* no previous filter, fill up */
2092 i->prevsys=key.sysno;
2093 i->prevseq=key.seqno;
2096 else if ( (i->prevsys==key.sysno) &&
2097 (i->prevseq==key.seqno) &&
2099 { /* same numbers, diff cmd, they cancel out */
2103 { /* different stuff, write previous, move buf */
2104 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2105 if (i->sysno != i->prevsys)
2107 i->sysno = i->prevsys;
2110 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2112 return; /* ??? Filters some sort of duplicates away */
2113 /* ??? Can this ever happen -H 15oct02 */
2115 bp = encode_key_int (i->prevseq - i->seqno, bp);
2116 i->seqno = i->prevseq;
2117 i->cmd = i->prevcmd;
2118 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2120 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2123 i->keylen=0; /* ok, it's written, forget it */
2124 i->prevsys=key.sysno;
2125 i->prevseq=key.seqno;
2130 void encode_key_flush (struct encode_info *i, FILE *outf)
2131 { /* flush the last key from i */
2132 char *bp =i->buf + i->keylen;
2135 return; /* nothing to flush */
2138 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2139 if (i->sysno != i->prevsys)
2141 i->sysno = i->prevsys;
2144 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2146 return; /* ??? Filters some sort of duplicates away */
2147 /* ??? Can this ever happen -H 15oct02 */
2149 bp = encode_key_int (i->prevseq - i->seqno, bp);
2150 i->seqno = i->prevseq;
2151 i->cmd = i->prevcmd;
2152 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2154 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2157 i->keylen=0; /* ok, it's written, forget it */
2158 i->prevsys=0; /* forget the values too */