1 /* $Id: extract.c,v 1.212 2006-05-10 14:13:45 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
40 #define PRINTF_OFF_T "%I64d"
43 #if SIZEOF_OFF_T == SIZEOF_LONG_LONG
44 #define PRINTF_OFF_T "%lld"
46 #define PRINTF_OFF_T "%ld"
52 #define USE_SHELLSORT 0
55 static void shellsort(void *ar, int r, size_t s,
56 int (*cmp)(const void *a, const void *b))
61 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
62 13776, 4592, 1968, 861, 336,
63 112, 48, 21, 7, 3, 1 };
64 for ( k = 0; k < 16; k++)
65 for (h = incs[k], i = h; i < r; i++)
69 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
71 memcpy (a + s*j, a + s*(j-h), s);
79 static void logRecord (ZebraHandle zh)
81 ++zh->records_processed;
82 if (!(zh->records_processed % 1000))
84 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
85 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
86 zh->records_processed, zh->records_inserted, zh->records_updated,
91 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
93 static void extract_init (struct recExtractCtrl *p, RecWord *w)
95 w->zebra_maps = p->zebra_maps;
99 w->attrSet = VAL_BIB1;
109 static void searchRecordKey(ZebraHandle zh,
110 zebra_rec_keys_t reckeys,
111 int attrSetS, int attrUseS,
112 const char **ws, int ws_length)
117 for (i = 0; i<ws_length; i++)
120 ch = zebraExplain_lookup_attr_su_any_index(zh->reg->zei,
125 if (zebra_rec_keys_rewind(reckeys))
132 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
134 assert(key.len <= 4 && key.len > 2);
136 seqno = key.mem[key.len-1];
138 if (key.mem[0] == ch)
144 woff = seqno - startSeq;
145 if (woff >= 0 && woff < ws_length)
152 struct file_read_info {
153 off_t file_max; /* maximum offset so far */
154 off_t file_offset; /* current offset */
155 off_t file_moffset; /* offset of rec/rec boundary */
160 static struct file_read_info *file_read_start (int fd)
162 struct file_read_info *fi = (struct file_read_info *)
163 xmalloc (sizeof(*fi));
167 fi->file_moffset = 0;
173 static void file_read_stop (struct file_read_info *fi)
178 static off_t file_seek (void *handle, off_t offset)
180 struct file_read_info *p = (struct file_read_info *) handle;
181 p->file_offset = offset;
182 return lseek (p->fd, offset, SEEK_SET);
185 static off_t file_tell (void *handle)
187 struct file_read_info *p = (struct file_read_info *) handle;
188 return p->file_offset;
191 static int file_read (void *handle, char *buf, size_t count)
193 struct file_read_info *p = (struct file_read_info *) handle;
196 r = read (fd, buf, count);
200 if (p->file_offset > p->file_max)
201 p->file_max = p->file_offset;
206 static void file_end (void *handle, off_t offset)
208 struct file_read_info *p = (struct file_read_info *) handle;
210 if (offset != p->file_moffset)
212 p->file_moffset = offset;
217 #define FILE_MATCH_BLANK "\t "
219 static char *fileMatchStr (ZebraHandle zh,
220 zebra_rec_keys_t reckeys,
221 const char *fname, const char *spec)
223 static char dstBuf[2048]; /* static here ??? */
225 const char *s = spec;
229 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
236 char attset_str[64], attname_str[64];
237 data1_attset *attset;
239 int attSet = 1, attUse = 1;
242 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
244 for (i = 0; *s && *s != ',' && *s != ')' &&
245 !strchr(FILE_MATCH_BLANK, *s); s++)
246 if (i+1 < sizeof(attset_str))
247 attset_str[i++] = *s;
248 attset_str[i] = '\0';
250 for (; strchr(FILE_MATCH_BLANK, *s); s++)
254 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
256 for (i = 0; *s && *s != ')' &&
257 !strchr(FILE_MATCH_BLANK, *s); s++)
258 if (i+1 < sizeof(attname_str))
259 attname_str[i++] = *s;
260 attname_str[i] = '\0';
263 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
266 attSet = attset->reference;
267 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
271 attUse = atoi (attname_str);
273 searchRecordKey (zh, reckeys, attSet, attUse, ws, 32);
277 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
278 spec, zh->m_group ? zh->m_group : "none");
283 for (i = 0; i<32; i++)
292 dst += strlen(ws[i]);
296 yaz_log (YLOG_WARN, "Record didn't contain match"
297 " fields in (%s,%s)", attset_str, attname_str);
305 const char *spec_src = NULL;
306 const char *s1 = ++s;
307 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
311 if (spec_len > sizeof(special)-1)
312 spec_len = sizeof(special)-1;
313 memcpy (special, s, spec_len);
314 special[spec_len] = '\0';
317 if (!strcmp (special, "group"))
318 spec_src = zh->m_group;
319 else if (!strcmp (special, "database"))
320 spec_src = zh->basenames[0];
321 else if (!strcmp (special, "filename")) {
324 else if (!strcmp (special, "type"))
325 spec_src = zh->m_record_type;
330 strcpy (dst, spec_src);
331 dst += strlen (spec_src);
334 else if (*s == '\"' || *s == '\'')
336 int stopMarker = *s++;
340 while (*s && *s != stopMarker)
342 if (i+1 < sizeof(tmpString))
343 tmpString[i++] = *s++;
348 strcpy (dst, tmpString);
349 dst += strlen (tmpString);
353 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
354 spec, zh->m_group ? zh->m_group : "none");
361 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
362 fname, zh->m_group ? zh->m_group : "none");
369 struct recordLogInfo {
372 struct recordGroup *rGroup;
375 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
378 for (i = 0; i<256; i++)
380 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
385 ctrl->zebra_maps = zh->reg->zebra_maps;
386 ctrl->flagShowRecords = !zh->m_flag_rw;
389 static ZEBRA_RES file_extract_record(ZebraHandle zh,
390 SYSNO *sysno, const char *fname,
392 struct file_read_info *fi,
395 void *recTypeClientData)
397 const char *match_str_to_print = "";
398 RecordAttr *recordAttr;
400 const char *matchStr = 0;
403 off_t recordOffset = 0;
404 struct recExtractCtrl extractCtrl;
406 /* announce database */
407 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
409 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
410 zh->m_explain_database))
416 /* we are going to read from a file, so prepare the extraction */
417 zebra_rec_keys_reset(zh->reg->keys);
420 zebra_rec_keys_reset(zh->reg->sortKeys);
422 zh->reg->sortKeys.buf_used = 0;
424 recordOffset = fi->file_moffset;
425 extractCtrl.handle = zh;
426 extractCtrl.offset = fi->file_moffset;
427 extractCtrl.readf = file_read;
428 extractCtrl.seekf = file_seek;
429 extractCtrl.tellf = file_tell;
430 extractCtrl.endf = file_end;
432 extractCtrl.init = extract_init;
433 extractCtrl.tokenAdd = extract_token_add;
434 extractCtrl.schemaAdd = extract_schema_add;
435 extractCtrl.dh = zh->reg->dh;
436 extractCtrl.match_criteria[0] = '\0';
437 extractCtrl.staticrank = 0;
439 extractCtrl.first_record = fi->file_offset ? 0 : 1;
441 extract_set_store_data_prepare(&extractCtrl);
443 init_extractCtrl(zh, &extractCtrl);
446 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
450 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
451 yaz_log_init_prefix2 (msg);
454 r = (*recType->extract)(recTypeClientData, &extractCtrl);
456 yaz_log_init_prefix2 (0);
457 if (r == RECCTRL_EXTRACT_EOF)
459 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
461 /* error occured during extraction ... */
463 zh->records_processed < zh->m_file_verbose_limit)
465 yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
466 fname, recordOffset);
470 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
472 /* error occured during extraction ... */
474 zh->records_processed < zh->m_file_verbose_limit)
476 yaz_log (YLOG_WARN, "no filter for %s %s "
477 PRINTF_OFF_T, zh->m_record_type,
478 fname, recordOffset);
482 if (extractCtrl.match_criteria[0])
483 matchStr = extractCtrl.match_criteria;
486 /* if matchStr is set now - we assume it's printable .
487 For internal matchStr (see below) we don't print */
489 match_str_to_print = matchStr;
491 /* perform internal match if sysno not known and if match criteria is
498 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
500 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
504 yaz_log(YLOG_WARN, "Bad match criteria");
510 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
511 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
515 assert(*rinfo == sizeof(*sysno));
516 memcpy (sysno, rinfo+1, sizeof(*sysno));
520 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
522 /* the extraction process returned no information - the record
523 is probably empty - unless flagShowRecords is in use */
527 if (zh->records_processed < zh->m_file_verbose_limit)
528 yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
529 fname, recordOffset);
538 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
539 fname, recordOffset);
540 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
544 rec = rec_new (zh->reg->records);
548 if (zh->records_processed < zh->m_file_verbose_limit)
550 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
551 " " ZINT_FORMAT " %s" ,
553 fname, recordOffset, *sysno, match_str_to_print);
555 recordAttr = rec_init_attr (zh->reg->zei, rec);
556 recordAttr->staticrank = extractCtrl.staticrank;
560 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
561 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
562 sizeof(*sysno), sysno);
567 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
569 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
571 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
572 recordAttr->staticrank);
573 zh->records_inserted++;
577 /* record already exists */
578 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
581 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
583 struct sortKeys sortKeys;
586 rec = rec_get (zh->reg->records, *sysno);
589 recordAttr = rec_init_attr (zh->reg->zei, rec);
591 zebra_rec_keys_set_buf(delkeys,
592 rec->info[recInfo_delKeys],
593 rec->size[recInfo_delKeys],
597 zebra_rec_keys_set_buf(sortKeys,
598 rec->info[recInfo_sortKeys],
599 rec->size[recInfo_sortKeys],
601 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
603 sortKeys.buf_used = rec->size[recInfo_sortKeys];
604 sortKeys.buf = rec->info[recInfo_sortKeys];
605 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
608 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
609 recordAttr->staticrank); /* old values */
612 /* record going to be deleted */
613 if (zebra_rec_keys_empty(delkeys))
615 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T
617 zh->m_record_type, fname, recordOffset, *sysno);
618 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
622 if (zh->records_processed < zh->m_file_verbose_limit)
624 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
625 " " ZINT_FORMAT " %s" ,
627 fname, recordOffset, *sysno, match_str_to_print);
629 zh->records_deleted++;
632 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
633 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
635 rec_del (zh->reg->records, &rec);
643 /* flush new keys for sort&search etc */
644 if (zh->records_processed < zh->m_file_verbose_limit)
646 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
647 " " ZINT_FORMAT " %s" ,
649 fname, recordOffset, *sysno, match_str_to_print);
651 recordAttr->staticrank = extractCtrl.staticrank;
653 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
655 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
657 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
658 recordAttr->staticrank);
659 zh->records_updated++;
661 zebra_rec_keys_close(delkeys);
663 zebra_rec_keys_close(sortKeys);
666 /* update file type */
667 xfree (rec->info[recInfo_fileType]);
668 rec->info[recInfo_fileType] =
669 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
671 /* update filename */
672 xfree (rec->info[recInfo_filename]);
673 rec->info[recInfo_filename] =
674 rec_strdup (fname, &rec->size[recInfo_filename]);
676 /* update delete keys */
677 xfree (rec->info[recInfo_delKeys]);
678 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
680 zebra_rec_keys_get_buf(zh->reg->keys,
681 &rec->info[recInfo_delKeys],
682 &rec->size[recInfo_delKeys]);
686 rec->info[recInfo_delKeys] = NULL;
687 rec->size[recInfo_delKeys] = 0;
690 /* update sort keys */
691 xfree (rec->info[recInfo_sortKeys]);
694 zebra_rec_keys_get_buf(zh->reg->sortKeys,
695 &rec->info[recInfo_sortKeys],
696 &rec->size[recInfo_sortKeys]);
698 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
699 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
700 zh->reg->sortKeys.buf = NULL;
701 zh->reg->sortKeys.buf_max = 0;
704 /* save file size of original record */
705 zebraExplain_recordBytesIncrement (zh->reg->zei,
706 - recordAttr->recordSize);
707 recordAttr->recordSize = fi->file_moffset - recordOffset;
708 if (!recordAttr->recordSize)
709 recordAttr->recordSize = fi->file_max - recordOffset;
710 zebraExplain_recordBytesIncrement (zh->reg->zei,
711 recordAttr->recordSize);
713 /* set run-number for this record */
714 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
717 /* update store data */
718 xfree (rec->info[recInfo_storeData]);
719 if (zh->store_data_buf)
721 rec->size[recInfo_storeData] = zh->store_data_size;
722 rec->info[recInfo_storeData] = zh->store_data_buf;
723 zh->store_data_buf = 0;
725 else if (zh->m_store_data)
727 rec->size[recInfo_storeData] = recordAttr->recordSize;
728 rec->info[recInfo_storeData] = (char *)
729 xmalloc (recordAttr->recordSize);
730 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
732 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
733 recordOffset, fname);
736 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
737 < recordAttr->recordSize)
739 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
740 recordAttr->recordSize, fname);
746 rec->info[recInfo_storeData] = NULL;
747 rec->size[recInfo_storeData] = 0;
749 /* update database name */
750 xfree (rec->info[recInfo_databaseName]);
751 rec->info[recInfo_databaseName] =
752 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
755 recordAttr->recordOffset = recordOffset;
757 /* commit this record */
758 rec_put (zh->reg->records, &rec);
763 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
766 ZEBRA_RES r = ZEBRA_OK;
771 struct file_read_info *fi;
772 const char *original_record_type = 0;
774 void *recTypeClientData;
776 if (!zh->m_group || !*zh->m_group)
779 sprintf (gprefix, "%s.", zh->m_group);
781 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
783 /* determine file extension */
785 for (i = strlen(fname); --i >= 0; )
788 else if (fname[i] == '.')
790 strcpy (ext, fname+i+1);
793 /* determine file type - depending on extension */
794 original_record_type = zh->m_record_type;
795 if (!zh->m_record_type)
797 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
798 zh->m_record_type = res_get (zh->res, ext_res);
800 if (!zh->m_record_type)
802 if (zh->records_processed < zh->m_file_verbose_limit)
803 yaz_log (YLOG_LOG, "? %s", fname);
806 /* determine match criteria */
807 if (!zh->m_record_id)
809 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
810 zh->m_record_id = res_get (zh->res, ext_res);
814 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
815 &recTypeClientData)))
817 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
821 switch(recType->version)
826 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
828 if (sysno && deleteFlag)
834 if (zh->path_reg && !yaz_is_abspath (fname))
836 strcpy (full_rep, zh->path_reg);
837 strcat (full_rep, "/");
838 strcat (full_rep, fname);
841 strcpy (full_rep, fname);
843 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
845 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
846 zh->m_record_type = original_record_type;
850 fi = file_read_start (fd);
853 fi->file_moffset = fi->file_offset;
854 fi->file_more = 0; /* file_end not called (yet) */
855 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
856 recType, recTypeClientData);
858 { /* file_end has been called so reset offset .. */
859 fi->file_offset = fi->file_moffset;
860 lseek(fi->fd, fi->file_moffset, SEEK_SET);
874 zh->m_record_type = original_record_type;
879 If sysno is provided, then it's used to identify the reocord.
880 If not, and match_criteria is provided, then sysno is guessed
881 If not, and a record is provided, then sysno is got from there
884 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
885 const char *buf, size_t buf_size,
888 const char *recordType,
890 const char *match_criteria,
896 RecordAttr *recordAttr;
897 struct recExtractCtrl extractCtrl;
899 const char *matchStr = 0;
900 RecType recType = NULL;
903 long recordOffset = 0;
904 struct zebra_fetch_control fc;
905 const char *pr_fname = fname; /* filename to print .. */
906 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
909 pr_fname = "<no file>"; /* make it printable if file is omitted */
912 fc.record_int_buf = buf;
913 fc.record_int_len = buf_size;
914 fc.record_int_pos = 0;
916 fc.record_offset = 0;
918 extractCtrl.offset = 0;
919 extractCtrl.readf = zebra_record_int_read;
920 extractCtrl.seekf = zebra_record_int_seek;
921 extractCtrl.tellf = zebra_record_int_tell;
922 extractCtrl.endf = zebra_record_int_end;
923 extractCtrl.first_record = 1;
924 extractCtrl.fh = &fc;
926 zebra_rec_keys_reset(zh->reg->keys);
929 zebra_rec_keys_reset(zh->reg->sortKeys);
931 zh->reg->sortKeys.buf_used = 0;
933 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
935 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
936 zh->m_explain_database))
940 if (recordType && *recordType)
942 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
943 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
948 if (!(zh->m_record_type))
950 yaz_log (YLOG_WARN, "No such record type defined");
953 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
954 recType = recType_byName (zh->reg->recTypes, zh->res,
955 zh->m_record_type, &clientData);
956 recordType = zh->m_record_type;
961 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
965 extractCtrl.init = extract_init;
966 extractCtrl.tokenAdd = extract_token_add;
967 extractCtrl.schemaAdd = extract_schema_add;
968 extractCtrl.dh = zh->reg->dh;
969 extractCtrl.handle = zh;
970 extractCtrl.match_criteria[0] = '\0';
971 extractCtrl.staticrank = 0;
973 init_extractCtrl(zh, &extractCtrl);
975 extract_set_store_data_prepare(&extractCtrl);
977 r = (*recType->extract)(clientData, &extractCtrl);
979 if (r == RECCTRL_EXTRACT_EOF)
981 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
983 /* error occured during extraction ... */
984 yaz_log (YLOG_WARN, "extract error: generic");
987 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
989 /* error occured during extraction ... */
990 yaz_log (YLOG_WARN, "extract error: no such filter");
994 if (extractCtrl.match_criteria[0])
995 match_criteria = extractCtrl.match_criteria;
1001 if (match_criteria && *match_criteria) {
1002 matchStr = match_criteria;
1004 if (zh->m_record_id && *zh->m_record_id) {
1005 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1009 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1016 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1017 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1021 assert(*rinfo == sizeof(*sysno));
1022 memcpy (sysno, rinfo+1, sizeof(*sysno));
1026 if (zebra_rec_keys_empty(zh->reg->keys))
1028 /* the extraction process returned no information - the record
1029 is probably empty - unless flagShowRecords is in use */
1039 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1040 pr_fname, (long) recordOffset);
1041 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1045 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1046 (long) recordOffset);
1047 rec = rec_new (zh->reg->records);
1049 *sysno = rec->sysno;
1051 recordAttr = rec_init_attr (zh->reg->zei, rec);
1052 recordAttr->staticrank = extractCtrl.staticrank;
1056 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1057 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1058 sizeof(*sysno), sysno);
1063 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1065 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1069 print_rec_keys(zh, zh->reg->keys);
1071 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1072 recordAttr->staticrank);
1073 zh->records_inserted++;
1077 /* record already exists */
1078 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1080 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1082 struct sortKeys sortKeys;
1087 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1088 recordType, pr_fname, (long) recordOffset);
1093 rec = rec_get (zh->reg->records, *sysno);
1096 recordAttr = rec_init_attr (zh->reg->zei, rec);
1098 zebra_rec_keys_set_buf(delkeys,
1099 rec->info[recInfo_delKeys],
1100 rec->size[recInfo_delKeys],
1103 zebra_rec_keys_set_buf(sortKeys,
1104 rec->info[recInfo_sortKeys],
1105 rec->size[recInfo_sortKeys],
1108 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1109 sortKeys.buf = rec->info[recInfo_sortKeys];
1113 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1115 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1117 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1118 recordAttr->staticrank);
1121 /* record going to be deleted */
1122 if (zebra_rec_keys_empty(delkeys))
1124 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1125 pr_fname, (long) recordOffset);
1126 yaz_log (YLOG_WARN, "cannot delete file above, "
1127 "storeKeys false (3)");
1132 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1133 pr_fname, (long) recordOffset);
1134 zh->records_deleted++;
1137 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1138 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1140 rec_del (zh->reg->records, &rec);
1149 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1150 pr_fname, (long) recordOffset);
1151 recordAttr->staticrank = extractCtrl.staticrank;
1153 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1155 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1157 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1158 recordAttr->staticrank);
1159 zh->records_updated++;
1161 zebra_rec_keys_close(delkeys);
1163 zebra_rec_keys_close(sortKeys);
1166 /* update file type */
1167 xfree (rec->info[recInfo_fileType]);
1168 rec->info[recInfo_fileType] =
1169 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1171 /* update filename */
1172 xfree (rec->info[recInfo_filename]);
1173 rec->info[recInfo_filename] =
1174 rec_strdup (fname, &rec->size[recInfo_filename]);
1176 /* update delete keys */
1177 xfree (rec->info[recInfo_delKeys]);
1178 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1180 zebra_rec_keys_get_buf(zh->reg->keys,
1181 &rec->info[recInfo_delKeys],
1182 &rec->size[recInfo_delKeys]);
1186 rec->info[recInfo_delKeys] = NULL;
1187 rec->size[recInfo_delKeys] = 0;
1189 /* update sort keys */
1190 xfree (rec->info[recInfo_sortKeys]);
1193 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1194 &rec->info[recInfo_sortKeys],
1195 &rec->size[recInfo_sortKeys]);
1197 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1198 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1199 zh->reg->sortKeys.buf = NULL;
1200 zh->reg->sortKeys.buf_max = 0;
1203 /* save file size of original record */
1204 zebraExplain_recordBytesIncrement (zh->reg->zei,
1205 - recordAttr->recordSize);
1207 recordAttr->recordSize = fi->file_moffset - recordOffset;
1208 if (!recordAttr->recordSize)
1209 recordAttr->recordSize = fi->file_max - recordOffset;
1211 recordAttr->recordSize = buf_size;
1213 zebraExplain_recordBytesIncrement (zh->reg->zei,
1214 recordAttr->recordSize);
1216 /* set run-number for this record */
1217 recordAttr->runNumber =
1218 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1220 /* update store data */
1221 xfree (rec->info[recInfo_storeData]);
1223 /* update store data */
1224 if (zh->store_data_buf)
1226 rec->size[recInfo_storeData] = zh->store_data_size;
1227 rec->info[recInfo_storeData] = zh->store_data_buf;
1228 zh->store_data_buf = 0;
1230 else if (zh->m_store_data)
1232 rec->size[recInfo_storeData] = recordAttr->recordSize;
1233 rec->info[recInfo_storeData] = (char *)
1234 xmalloc (recordAttr->recordSize);
1235 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1239 rec->info[recInfo_storeData] = NULL;
1240 rec->size[recInfo_storeData] = 0;
1242 /* update database name */
1243 xfree (rec->info[recInfo_databaseName]);
1244 rec->info[recInfo_databaseName] =
1245 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1248 recordAttr->recordOffset = recordOffset;
1250 /* commit this record */
1251 rec_put (zh->reg->records, &rec);
1256 int explain_extract (void *handle, Record rec, data1_node *n)
1258 ZebraHandle zh = (ZebraHandle) handle;
1259 struct recExtractCtrl extractCtrl;
1261 if (zebraExplain_curDatabase (zh->reg->zei,
1262 rec->info[recInfo_databaseName]))
1265 if (zebraExplain_newDatabase (zh->reg->zei,
1266 rec->info[recInfo_databaseName], 0))
1270 zebra_rec_keys_reset(zh->reg->keys);
1273 zebra_rec_keys_reset(zh->reg->sortKeys);
1275 zh->reg->sortKeys.buf_used = 0;
1277 extractCtrl.init = extract_init;
1278 extractCtrl.tokenAdd = extract_token_add;
1279 extractCtrl.schemaAdd = extract_schema_add;
1280 extractCtrl.dh = zh->reg->dh;
1282 init_extractCtrl(zh, &extractCtrl);
1284 extractCtrl.flagShowRecords = 0;
1285 extractCtrl.match_criteria[0] = '\0';
1286 extractCtrl.staticrank = 0;
1287 extractCtrl.handle = handle;
1288 extractCtrl.first_record = 1;
1290 extract_set_store_data_prepare(&extractCtrl);
1293 grs_extract_tree(&extractCtrl, n);
1295 if (rec->size[recInfo_delKeys])
1297 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1300 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1302 struct sortKeys sortkeys;
1305 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1306 rec->size[recInfo_delKeys],
1308 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1309 zebra_rec_keys_close(delkeys);
1311 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1312 rec->size[recInfo_sortKeys],
1315 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1316 zebra_rec_keys_close(sortkeys);
1318 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1319 sortkeys.buf = rec->info[recInfo_sortKeys];
1320 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1323 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1325 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1327 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1330 xfree (rec->info[recInfo_delKeys]);
1331 zebra_rec_keys_get_buf(zh->reg->keys,
1332 &rec->info[recInfo_delKeys],
1333 &rec->size[recInfo_delKeys]);
1335 xfree (rec->info[recInfo_sortKeys]);
1337 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1338 &rec->info[recInfo_sortKeys],
1339 &rec->size[recInfo_sortKeys]);
1341 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1342 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1343 zh->reg->sortKeys.buf = NULL;
1344 zh->reg->sortKeys.buf_max = 0;
1350 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1351 zebra_rec_keys_t reckeys)
1353 ZebraExplainInfo zei = zh->reg->zei;
1357 struct ord_stat *next;
1360 if (zebra_rec_keys_rewind(reckeys))
1362 struct ord_stat *ord_list = 0;
1366 struct it_key key_in;
1367 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1369 int ord = key_in.mem[0];
1371 for (p = ord_list; p ; p = p->next)
1379 p = xmalloc(sizeof(*p));
1390 struct ord_stat *p1 = p;
1393 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1395 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1402 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1404 zebra_rec_keys_t reckeys,
1407 ZebraExplainInfo zei = zh->reg->zei;
1409 extract_rec_keys_adjust(zh, cmd, reckeys);
1411 if (!zh->reg->key_buf)
1413 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1416 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1419 /* FIXME: That "8" should be in a default settings include */
1420 /* not hard-coded here! -H */
1421 zh->reg->key_buf = (char**) xmalloc (mem);
1422 zh->reg->ptr_top = mem/sizeof(char*);
1424 zh->reg->key_buf_used = 0;
1425 zh->reg->key_file_no = 0;
1427 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1429 if (zebra_rec_keys_rewind(reckeys))
1433 struct it_key key_in;
1434 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1437 struct it_key key_out;
1438 zint *keyp = key_out.mem;
1440 assert(key_in.len == 4);
1442 /* check for buffer overflow */
1443 if (zh->reg->key_buf_used + 1024 >
1444 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1445 extract_flushWriteKeys (zh, 0);
1448 assert(zh->reg->ptr_i > 0);
1449 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1450 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1452 /* encode the ordinal value (field/use/attribute) .. */
1453 ch = (int) key_in.mem[0];
1454 zh->reg->key_buf_used +=
1455 key_SU_encode(ch, (char*)zh->reg->key_buf +
1456 zh->reg->key_buf_used);
1458 /* copy the 0-terminated stuff from str to output */
1459 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1460 zh->reg->key_buf_used += slen;
1461 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1463 /* the delete/insert indicator */
1464 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1466 if (zh->m_staticrank) /* rank config enabled ? */
1470 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1474 *keyp++ = staticrank;
1480 if (key_in.mem[1]) /* filter specified record ID */
1481 *keyp++ = key_in.mem[1];
1484 *keyp++ = key_in.mem[2]; /* section_id */
1485 *keyp++ = key_in.mem[3]; /* sequence .. */
1487 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1488 &key_out, sizeof(key_out));
1489 (zh->reg->key_buf_used) += sizeof(key_out);
1494 void extract_flushWriteKeys (ZebraHandle zh, int final)
1495 /* optimizing: if final=1, and no files written yet */
1496 /* push the keys directly to merge, sidestepping the */
1497 /* temp file altogether. Speeds small updates */
1500 char out_fname[200];
1502 struct encode_info encode_info;
1503 int ptr_i = zh->reg->ptr_i;
1508 if (!zh->reg->key_buf || ptr_i <= 0)
1510 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1511 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1512 yaz_log (YLOG_DEBUG, " buf=%p ",
1514 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1515 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1520 (zh->reg->key_file_no)++;
1521 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1522 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1523 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1525 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1526 sizeof(char*), key_qsort_compare);
1528 /* zebra.cfg: tempfiles:
1529 Y: always use temp files (old way)
1530 A: use temp files, if more than one (auto)
1531 = if this is both the last and the first
1532 N: never bother with temp files (new) */
1534 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1535 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1536 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1541 if ( ( temp_policy =='N' ) || /* always from memory */
1542 ( ( temp_policy =='A' ) && /* automatic */
1543 (zh->reg->key_file_no == 1) && /* this is first time */
1544 (final) ) ) /* and last (=only) time */
1545 { /* go directly from memory */
1546 zh->reg->key_file_no =0; /* signal not to read files */
1547 zebra_index_merge(zh);
1549 zh->reg->key_buf_used = 0;
1553 /* Not doing directly from memory, write into a temp file */
1554 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1556 if (!(outf = fopen (out_fname, "wb")))
1558 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1561 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1562 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1564 encode_key_init (&encode_info);
1565 encode_key_write (cp, &encode_info, outf);
1569 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1570 if (strcmp (cp, prevcp))
1572 encode_key_flush ( &encode_info, outf);
1573 encode_key_init (&encode_info);
1574 encode_key_write (cp, &encode_info, outf);
1578 encode_key_write (cp + strlen(cp), &encode_info, outf);
1580 encode_key_flush ( &encode_info, outf);
1582 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1583 extract_get_fname_tmp (out_fname, key_file_no);
1585 if (!(outf = fopen (out_fname, "wb")))
1587 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1590 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1592 prevcp = key_buf[ptr_top-i];
1594 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1596 key_y_len = strlen(prevcp)+1;
1598 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1599 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1601 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1602 sizeof(char*), key_y_compare);
1603 cp = key_buf[ptr_top-ptr_i];
1605 encode_key_init (&encode_info);
1606 encode_key_write (cp, &encode_info, outf);
1609 cp = key_buf[ptr_top-ptr_i];
1610 encode_key_write (cp+key_y_len, &encode_info, outf);
1612 encode_key_flush ( &encode_info, outf);
1615 prevcp = key_buf[ptr_top-ptr_i];
1620 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1623 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1625 zh->reg->key_buf_used = 0;
1628 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1629 zebra_rec_keys_t reckeys,
1630 zebra_snippets *snippets)
1632 NMEM nmem = nmem_create();
1633 if (zebra_rec_keys_rewind(reckeys))
1638 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1640 char dst_buf[IT_MAX_WORD];
1641 char *dst_term = dst_buf;
1644 assert(key.len <= 4 && key.len > 2);
1645 seqno = (int) key.mem[key.len-1];
1648 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1649 0/* db */, 0/* set */, 0/* use */,
1650 0 /* string_index */);
1652 zebra_term_untrans_iconv(zh, nmem, index_type,
1654 zebra_snippets_append(snippets, seqno, ord, dst_term);
1662 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1664 yaz_log(YLOG_LOG, "print_rec_keys");
1665 if (zebra_rec_keys_rewind(reckeys))
1670 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1672 char dst_buf[IT_MAX_WORD];
1676 assert(key.len <= 4 && key.len > 2);
1678 zebraExplain_lookup_ord(zh->reg->zei,
1679 key.mem[0], &index_type, &db, 0, 0, 0);
1681 seqno = (int) key.mem[key.len-1];
1683 zebra_term_untrans(zh, index_type, dst_buf, str);
1685 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1686 key.mem[0], seqno, dst_buf);
1691 void extract_add_index_string (RecWord *p, const char *str, int length)
1695 ZebraHandle zh = p->extractCtrl->handle;
1696 ZebraExplainInfo zei = zh->reg->zei;
1701 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1703 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1710 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
1711 p->attrSet, p->attrUse);
1713 ch = zebraExplain_add_attr_su(zei, p->index_type,
1714 p->attrSet, p->attrUse);
1719 key.mem[1] = p->record_id;
1720 key.mem[2] = p->section_id;
1721 key.mem[3] = p->seqno;
1730 for (i = 0; i<length && i < 20; i++)
1731 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1732 /* just for debugging .. */
1733 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1734 "record_id=%lld section_id=%lld seqno=%lld %s",
1735 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1740 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1744 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1748 ZebraHandle zh = p->extractCtrl->handle;
1749 ZebraExplainInfo zei = zh->reg->zei;
1754 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1756 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1764 key.mem[1] = p->record_id;
1765 key.mem[2] = p->section_id;
1766 key.mem[3] = p->seqno;
1768 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1771 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1773 ZebraHandle zh = p->extractCtrl->handle;
1774 struct sortKeys *sk = &zh->reg->sortKeys;
1777 while (off < sk->buf_used)
1781 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1782 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1783 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1785 if (p->attrSet == set && p->attrUse == use)
1788 assert (off == sk->buf_used);
1790 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1794 b = (char *) xmalloc (sk->buf_max += 128000);
1795 if (sk->buf_used > 0)
1796 memcpy (b, sk->buf, sk->buf_used);
1800 off += key_SU_encode(p->attrSet, sk->buf + off);
1801 off += key_SU_encode(p->attrUse, sk->buf + off);
1802 off += key_SU_encode(length, sk->buf + off);
1803 memcpy (sk->buf + off, str, length);
1804 sk->buf_used = off + length;
1808 void extract_add_string (RecWord *p, const char *string, int length)
1810 assert (length > 0);
1811 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1812 extract_add_sort_string (p, string, length);
1814 extract_add_index_string (p, string, length);
1817 static void extract_add_incomplete_field (RecWord *p)
1819 const char *b = p->term_buf;
1820 int remain = p->term_len;
1821 const char **map = 0;
1823 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1826 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1830 char buf[IT_MAX_WORD+1];
1834 while (map && *map && **map == *CHR_SPACE)
1836 remain = p->term_len - (b - p->term_buf);
1838 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1846 while (map && *map && **map != *CHR_SPACE)
1848 const char *cp = *map;
1850 while (i < IT_MAX_WORD && *cp)
1852 remain = p->term_len - (b - p->term_buf);
1854 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1860 extract_add_string (p, buf, i);
1865 static void extract_add_complete_field (RecWord *p)
1867 const char *b = p->term_buf;
1868 char buf[IT_MAX_WORD+1];
1869 const char **map = 0;
1870 int i = 0, remain = p->term_len;
1872 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1873 p->term_len, p->term_buf);
1876 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1878 while (remain > 0 && i < IT_MAX_WORD)
1880 while (map && *map && **map == *CHR_SPACE)
1882 remain = p->term_len - (b - p->term_buf);
1886 int first = i ? 0 : 1; /* first position */
1887 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1895 if (i && i < IT_MAX_WORD)
1896 buf[i++] = *CHR_SPACE;
1897 while (map && *map && **map != *CHR_SPACE)
1899 const char *cp = *map;
1901 if (**map == *CHR_CUT)
1907 if (i >= IT_MAX_WORD)
1909 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1910 while (i < IT_MAX_WORD && *cp)
1913 remain = p->term_len - (b - p->term_buf);
1916 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1925 extract_add_string (p, buf, i);
1928 void extract_token_add (RecWord *p)
1932 yaz_log (YLOG_LOG, "token_add "
1933 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1934 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1937 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1938 p->term_buf, p->term_len)))
1940 p->term_buf = wrbuf_buf(wrbuf);
1941 p->term_len = wrbuf_len(wrbuf);
1943 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1944 extract_add_complete_field (p);
1946 extract_add_incomplete_field(p);
1949 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1950 void *buf, size_t sz)
1952 ZebraHandle zh = (ZebraHandle) p->handle;
1954 xfree(zh->store_data_buf);
1955 zh->store_data_buf = 0;
1956 zh->store_data_size = 0;
1959 zh->store_data_buf = xmalloc(sz);
1960 zh->store_data_size = sz;
1961 memcpy(zh->store_data_buf, buf, sz);
1965 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1967 ZebraHandle zh = (ZebraHandle) p->handle;
1968 xfree(zh->store_data_buf);
1969 zh->store_data_buf = 0;
1970 zh->store_data_size = 0;
1971 p->setStoreData = extract_set_store_data_cb;
1974 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1976 ZebraHandle zh = (ZebraHandle) p->handle;
1977 zebraExplain_addSchema (zh->reg->zei, oid);
1981 #error not done yet with zebra_rec_keys_t
1982 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1983 int cmd, struct recKeys *reckeys)
1985 SortIdx sortIdx = zh->reg->sortIdx;
1986 void *decode_handle = iscz1_start();
1990 while (off < reckeys->buf_used)
1992 const char *src = reckeys->buf + off;
1994 char *dst = (char*) &key;
1996 iscz1_decode(decode_handle, &dst, &src);
1997 assert(key.len == 4);
1999 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
2001 sortIdx_type(sortIdx, ch);
2003 sortIdx_add(sortIdx, src, strlen(src));
2005 sortIdx_add(sortIdx, "", 1);
2010 off = src - reckeys->buf;
2012 assert (off == reckeys->buf_used);
2013 iscz1_stop(decode_handle);
2016 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
2017 int cmd, struct sortKeys *sk)
2019 SortIdx sortIdx = zh->reg->sortIdx;
2022 sortIdx_sysno (sortIdx, sysno);
2024 while (off < sk->buf_used)
2028 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
2029 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
2030 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
2032 sortIdx_type(sortIdx, use);
2034 sortIdx_add(sortIdx, sk->buf + off, slen);
2036 sortIdx_add(sortIdx, "", 1);
2042 void encode_key_init (struct encode_info *i)
2051 i->encode_handle = iscz1_start();
2052 i->decode_handle = iscz1_start();
2058 /* this is the old encode_key_write
2059 * may be deleted once we are confident that the new works
2062 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2065 char *bp = i->buf, *bp0;
2066 const char *src = (char *) &key;
2068 /* copy term to output buf */
2069 while ((*bp++ = *k++))
2071 /* and copy & align key so we can mangle */
2072 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
2076 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
2078 assert(key.mem[0] >= 0);
2081 iscz1_encode(i->encode_handle, &bp, &src);
2083 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
2084 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2086 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2095 const char *src = bp0+1;
2096 char *dst = (char*) &key2;
2097 iscz1_decode(i->decode_handle, &dst, &src);
2099 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
2101 assert(key2.mem[1]);
2106 void encode_key_flush (struct encode_info *i, FILE *outf)
2108 iscz1_stop(i->encode_handle);
2109 iscz1_stop(i->decode_handle);
2114 /* new encode_key_write
2115 * The idea is to buffer one more key, and compare them
2116 * If we are going to delete and insert the same key,
2117 * we may as well not bother. Should make a difference in
2118 * updates with small modifications (appending to a mbox)
2120 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2125 if (*k) /* first time for new key */
2128 while ((*bp++ = *k++))
2130 i->keylen= bp - i->buf -1;
2131 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2135 bp=i->buf + i->keylen;
2140 memcpy (&key, k+1, sizeof(struct it_key));
2141 if (0==i->prevsys) /* no previous filter, fill up */
2143 i->prevsys=key.sysno;
2144 i->prevseq=key.seqno;
2147 else if ( (i->prevsys==key.sysno) &&
2148 (i->prevseq==key.seqno) &&
2150 { /* same numbers, diff cmd, they cancel out */
2154 { /* different stuff, write previous, move buf */
2155 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2156 if (i->sysno != i->prevsys)
2158 i->sysno = i->prevsys;
2161 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2163 return; /* ??? Filters some sort of duplicates away */
2164 /* ??? Can this ever happen -H 15oct02 */
2166 bp = encode_key_int (i->prevseq - i->seqno, bp);
2167 i->seqno = i->prevseq;
2168 i->cmd = i->prevcmd;
2169 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2171 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2174 i->keylen=0; /* ok, it's written, forget it */
2175 i->prevsys=key.sysno;
2176 i->prevseq=key.seqno;
2181 void encode_key_flush (struct encode_info *i, FILE *outf)
2182 { /* flush the last key from i */
2183 char *bp =i->buf + i->keylen;
2186 return; /* nothing to flush */
2189 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2190 if (i->sysno != i->prevsys)
2192 i->sysno = i->prevsys;
2195 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2197 return; /* ??? Filters some sort of duplicates away */
2198 /* ??? Can this ever happen -H 15oct02 */
2200 bp = encode_key_int (i->prevseq - i->seqno, bp);
2201 i->seqno = i->prevseq;
2202 i->cmd = i->prevcmd;
2203 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2205 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2208 i->keylen=0; /* ok, it's written, forget it */
2209 i->prevsys=0; /* forget the values too */
2216 * indent-tabs-mode: nil
2218 * vim: shiftwidth=4 tabstop=8 expandtab