1 /* $Id: extract.c,v 1.210 2006-05-10 12:31:08 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
39 #if _FILE_OFFSET_BITS == 64
40 #define PRINTF_OFF_T "%Ld"
42 #define PRINTF_OFF_T "%ld"
45 #define USE_SHELLSORT 0
48 static void shellsort(void *ar, int r, size_t s,
49 int (*cmp)(const void *a, const void *b))
54 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
55 13776, 4592, 1968, 861, 336,
56 112, 48, 21, 7, 3, 1 };
57 for ( k = 0; k < 16; k++)
58 for (h = incs[k], i = h; i < r; i++)
62 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
64 memcpy (a + s*j, a + s*(j-h), s);
72 static void logRecord (ZebraHandle zh)
74 ++zh->records_processed;
75 if (!(zh->records_processed % 1000))
77 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
78 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
79 zh->records_processed, zh->records_inserted, zh->records_updated,
84 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
86 static void extract_init (struct recExtractCtrl *p, RecWord *w)
88 w->zebra_maps = p->zebra_maps;
92 w->attrSet = VAL_BIB1;
102 static void searchRecordKey(ZebraHandle zh,
103 zebra_rec_keys_t reckeys,
104 int attrSetS, int attrUseS,
105 const char **ws, int ws_length)
110 for (i = 0; i<ws_length; i++)
113 ch = zebraExplain_lookup_attr_su_any_index(zh->reg->zei,
118 if (zebra_rec_keys_rewind(reckeys))
125 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
127 assert(key.len <= 4 && key.len > 2);
129 seqno = key.mem[key.len-1];
131 if (key.mem[0] == ch)
137 woff = seqno - startSeq;
138 if (woff >= 0 && woff < ws_length)
145 struct file_read_info {
146 off_t file_max; /* maximum offset so far */
147 off_t file_offset; /* current offset */
148 off_t file_moffset; /* offset of rec/rec boundary */
153 static struct file_read_info *file_read_start (int fd)
155 struct file_read_info *fi = (struct file_read_info *)
156 xmalloc (sizeof(*fi));
160 fi->file_moffset = 0;
166 static void file_read_stop (struct file_read_info *fi)
171 static off_t file_seek (void *handle, off_t offset)
173 struct file_read_info *p = (struct file_read_info *) handle;
174 p->file_offset = offset;
175 return lseek (p->fd, offset, SEEK_SET);
178 static off_t file_tell (void *handle)
180 struct file_read_info *p = (struct file_read_info *) handle;
181 return p->file_offset;
184 static int file_read (void *handle, char *buf, size_t count)
186 struct file_read_info *p = (struct file_read_info *) handle;
189 r = read (fd, buf, count);
193 if (p->file_offset > p->file_max)
194 p->file_max = p->file_offset;
199 static void file_end (void *handle, off_t offset)
201 struct file_read_info *p = (struct file_read_info *) handle;
203 if (offset != p->file_moffset)
205 p->file_moffset = offset;
210 #define FILE_MATCH_BLANK "\t "
212 static char *fileMatchStr (ZebraHandle zh,
213 zebra_rec_keys_t reckeys,
214 const char *fname, const char *spec)
216 static char dstBuf[2048]; /* static here ??? */
218 const char *s = spec;
222 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
229 char attset_str[64], attname_str[64];
230 data1_attset *attset;
232 int attSet = 1, attUse = 1;
235 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
237 for (i = 0; *s && *s != ',' && *s != ')' &&
238 !strchr(FILE_MATCH_BLANK, *s); s++)
239 if (i+1 < sizeof(attset_str))
240 attset_str[i++] = *s;
241 attset_str[i] = '\0';
243 for (; strchr(FILE_MATCH_BLANK, *s); s++)
247 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
249 for (i = 0; *s && *s != ')' &&
250 !strchr(FILE_MATCH_BLANK, *s); s++)
251 if (i+1 < sizeof(attname_str))
252 attname_str[i++] = *s;
253 attname_str[i] = '\0';
256 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
259 attSet = attset->reference;
260 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
264 attUse = atoi (attname_str);
266 searchRecordKey (zh, reckeys, attSet, attUse, ws, 32);
270 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
271 spec, zh->m_group ? zh->m_group : "none");
276 for (i = 0; i<32; i++)
285 dst += strlen(ws[i]);
289 yaz_log (YLOG_WARN, "Record didn't contain match"
290 " fields in (%s,%s)", attset_str, attname_str);
298 const char *spec_src = NULL;
299 const char *s1 = ++s;
300 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
304 if (spec_len > sizeof(special)-1)
305 spec_len = sizeof(special)-1;
306 memcpy (special, s, spec_len);
307 special[spec_len] = '\0';
310 if (!strcmp (special, "group"))
311 spec_src = zh->m_group;
312 else if (!strcmp (special, "database"))
313 spec_src = zh->basenames[0];
314 else if (!strcmp (special, "filename")) {
317 else if (!strcmp (special, "type"))
318 spec_src = zh->m_record_type;
323 strcpy (dst, spec_src);
324 dst += strlen (spec_src);
327 else if (*s == '\"' || *s == '\'')
329 int stopMarker = *s++;
333 while (*s && *s != stopMarker)
335 if (i+1 < sizeof(tmpString))
336 tmpString[i++] = *s++;
341 strcpy (dst, tmpString);
342 dst += strlen (tmpString);
346 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
347 spec, zh->m_group ? zh->m_group : "none");
354 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
355 fname, zh->m_group ? zh->m_group : "none");
362 struct recordLogInfo {
365 struct recordGroup *rGroup;
368 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
371 for (i = 0; i<256; i++)
373 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
378 ctrl->zebra_maps = zh->reg->zebra_maps;
379 ctrl->flagShowRecords = !zh->m_flag_rw;
382 static ZEBRA_RES file_extract_record(ZebraHandle zh,
383 SYSNO *sysno, const char *fname,
385 struct file_read_info *fi,
388 void *recTypeClientData)
390 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)
537 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
538 " " ZINT_FORMAT " %s" ,
540 fname, recordOffset, *sysno, matchStr);
542 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
545 fname, recordOffset, *sysno);
547 recordAttr = rec_init_attr (zh->reg->zei, rec);
548 recordAttr->staticrank = extractCtrl.staticrank;
552 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
553 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
554 sizeof(*sysno), sysno);
559 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
561 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
563 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
564 recordAttr->staticrank);
565 zh->records_inserted++;
569 /* record already exists */
570 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
573 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
575 struct sortKeys sortKeys;
578 rec = rec_get (zh->reg->records, *sysno);
581 recordAttr = rec_init_attr (zh->reg->zei, rec);
583 zebra_rec_keys_set_buf(delkeys,
584 rec->info[recInfo_delKeys],
585 rec->size[recInfo_delKeys],
589 zebra_rec_keys_set_buf(sortKeys,
590 rec->info[recInfo_sortKeys],
591 rec->size[recInfo_sortKeys],
593 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
595 sortKeys.buf_used = rec->size[recInfo_sortKeys];
596 sortKeys.buf = rec->info[recInfo_sortKeys];
597 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
600 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
601 recordAttr->staticrank); /* old values */
604 /* record going to be deleted */
605 if (zebra_rec_keys_empty(delkeys))
607 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T
609 zh->m_record_type, fname, recordOffset, *sysno);
610 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
614 if (zh->records_processed < zh->m_file_verbose_limit)
617 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
618 " " ZINT_FORMAT " %s" ,
620 fname, recordOffset, *sysno, matchStr);
622 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
625 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)
645 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
646 " " ZINT_FORMAT " %s" ,
648 fname, recordOffset, *sysno, matchStr);
650 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
653 fname, recordOffset, *sysno);
655 recordAttr->staticrank = extractCtrl.staticrank;
657 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
659 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
661 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
662 recordAttr->staticrank);
663 zh->records_updated++;
665 zebra_rec_keys_close(delkeys);
667 zebra_rec_keys_close(sortKeys);
670 /* update file type */
671 xfree (rec->info[recInfo_fileType]);
672 rec->info[recInfo_fileType] =
673 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
675 /* update filename */
676 xfree (rec->info[recInfo_filename]);
677 rec->info[recInfo_filename] =
678 rec_strdup (fname, &rec->size[recInfo_filename]);
680 /* update delete keys */
681 xfree (rec->info[recInfo_delKeys]);
682 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
684 zebra_rec_keys_get_buf(zh->reg->keys,
685 &rec->info[recInfo_delKeys],
686 &rec->size[recInfo_delKeys]);
690 rec->info[recInfo_delKeys] = NULL;
691 rec->size[recInfo_delKeys] = 0;
694 /* update sort keys */
695 xfree (rec->info[recInfo_sortKeys]);
698 zebra_rec_keys_get_buf(zh->reg->sortKeys,
699 &rec->info[recInfo_sortKeys],
700 &rec->size[recInfo_sortKeys]);
702 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
703 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
704 zh->reg->sortKeys.buf = NULL;
705 zh->reg->sortKeys.buf_max = 0;
708 /* save file size of original record */
709 zebraExplain_recordBytesIncrement (zh->reg->zei,
710 - recordAttr->recordSize);
711 recordAttr->recordSize = fi->file_moffset - recordOffset;
712 if (!recordAttr->recordSize)
713 recordAttr->recordSize = fi->file_max - recordOffset;
714 zebraExplain_recordBytesIncrement (zh->reg->zei,
715 recordAttr->recordSize);
717 /* set run-number for this record */
718 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
721 /* update store data */
722 xfree (rec->info[recInfo_storeData]);
723 if (zh->store_data_buf)
725 rec->size[recInfo_storeData] = zh->store_data_size;
726 rec->info[recInfo_storeData] = zh->store_data_buf;
727 zh->store_data_buf = 0;
729 else if (zh->m_store_data)
731 rec->size[recInfo_storeData] = recordAttr->recordSize;
732 rec->info[recInfo_storeData] = (char *)
733 xmalloc (recordAttr->recordSize);
734 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
736 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
737 recordOffset, fname);
740 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
741 < recordAttr->recordSize)
743 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
744 recordAttr->recordSize, fname);
750 rec->info[recInfo_storeData] = NULL;
751 rec->size[recInfo_storeData] = 0;
753 /* update database name */
754 xfree (rec->info[recInfo_databaseName]);
755 rec->info[recInfo_databaseName] =
756 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
759 recordAttr->recordOffset = recordOffset;
761 /* commit this record */
762 rec_put (zh->reg->records, &rec);
767 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
770 ZEBRA_RES r = ZEBRA_OK;
775 struct file_read_info *fi;
776 const char *original_record_type = 0;
778 void *recTypeClientData;
780 if (!zh->m_group || !*zh->m_group)
783 sprintf (gprefix, "%s.", zh->m_group);
785 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
787 /* determine file extension */
789 for (i = strlen(fname); --i >= 0; )
792 else if (fname[i] == '.')
794 strcpy (ext, fname+i+1);
797 /* determine file type - depending on extension */
798 original_record_type = zh->m_record_type;
799 if (!zh->m_record_type)
801 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
802 zh->m_record_type = res_get (zh->res, ext_res);
804 if (!zh->m_record_type)
806 if (zh->records_processed < zh->m_file_verbose_limit)
807 yaz_log (YLOG_LOG, "? %s", fname);
810 /* determine match criteria */
811 if (!zh->m_record_id)
813 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
814 zh->m_record_id = res_get (zh->res, ext_res);
818 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
819 &recTypeClientData)))
821 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
825 switch(recType->version)
830 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
832 if (sysno && deleteFlag)
838 if (zh->path_reg && !yaz_is_abspath (fname))
840 strcpy (full_rep, zh->path_reg);
841 strcat (full_rep, "/");
842 strcat (full_rep, fname);
845 strcpy (full_rep, fname);
847 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
849 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
850 zh->m_record_type = original_record_type;
854 fi = file_read_start (fd);
857 fi->file_moffset = fi->file_offset;
858 fi->file_more = 0; /* file_end not called (yet) */
859 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
860 recType, recTypeClientData);
862 { /* file_end has been called so reset offset .. */
863 fi->file_offset = fi->file_moffset;
864 lseek(fi->fd, fi->file_moffset, SEEK_SET);
878 zh->m_record_type = original_record_type;
883 If sysno is provided, then it's used to identify the reocord.
884 If not, and match_criteria is provided, then sysno is guessed
885 If not, and a record is provided, then sysno is got from there
888 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
889 const char *buf, size_t buf_size,
892 const char *recordType,
894 const char *match_criteria,
900 RecordAttr *recordAttr;
901 struct recExtractCtrl extractCtrl;
903 const char *matchStr = 0;
904 RecType recType = NULL;
907 long recordOffset = 0;
908 struct zebra_fetch_control fc;
909 const char *pr_fname = fname; /* filename to print .. */
910 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
913 pr_fname = "<no file>"; /* make it printable if file is omitted */
916 fc.record_int_buf = buf;
917 fc.record_int_len = buf_size;
918 fc.record_int_pos = 0;
920 fc.record_offset = 0;
922 extractCtrl.offset = 0;
923 extractCtrl.readf = zebra_record_int_read;
924 extractCtrl.seekf = zebra_record_int_seek;
925 extractCtrl.tellf = zebra_record_int_tell;
926 extractCtrl.endf = zebra_record_int_end;
927 extractCtrl.first_record = 1;
928 extractCtrl.fh = &fc;
930 zebra_rec_keys_reset(zh->reg->keys);
933 zebra_rec_keys_reset(zh->reg->sortKeys);
935 zh->reg->sortKeys.buf_used = 0;
937 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
939 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
940 zh->m_explain_database))
944 if (recordType && *recordType)
946 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
947 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
952 if (!(zh->m_record_type))
954 yaz_log (YLOG_WARN, "No such record type defined");
957 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
958 recType = recType_byName (zh->reg->recTypes, zh->res,
959 zh->m_record_type, &clientData);
960 recordType = zh->m_record_type;
965 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
969 extractCtrl.init = extract_init;
970 extractCtrl.tokenAdd = extract_token_add;
971 extractCtrl.schemaAdd = extract_schema_add;
972 extractCtrl.dh = zh->reg->dh;
973 extractCtrl.handle = zh;
974 extractCtrl.match_criteria[0] = '\0';
975 extractCtrl.staticrank = 0;
977 init_extractCtrl(zh, &extractCtrl);
979 extract_set_store_data_prepare(&extractCtrl);
981 r = (*recType->extract)(clientData, &extractCtrl);
983 if (r == RECCTRL_EXTRACT_EOF)
985 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
987 /* error occured during extraction ... */
988 yaz_log (YLOG_WARN, "extract error: generic");
991 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
993 /* error occured during extraction ... */
994 yaz_log (YLOG_WARN, "extract error: no such filter");
998 if (extractCtrl.match_criteria[0])
999 match_criteria = extractCtrl.match_criteria;
1005 if (match_criteria && *match_criteria) {
1006 matchStr = match_criteria;
1008 if (zh->m_record_id && *zh->m_record_id) {
1009 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1013 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1020 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1021 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1025 assert(*rinfo == sizeof(*sysno));
1026 memcpy (sysno, rinfo+1, sizeof(*sysno));
1030 if (zebra_rec_keys_empty(zh->reg->keys))
1032 /* the extraction process returned no information - the record
1033 is probably empty - unless flagShowRecords is in use */
1043 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1044 pr_fname, (long) recordOffset);
1045 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1049 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1050 (long) recordOffset);
1051 rec = rec_new (zh->reg->records);
1053 *sysno = rec->sysno;
1055 recordAttr = rec_init_attr (zh->reg->zei, rec);
1056 recordAttr->staticrank = extractCtrl.staticrank;
1060 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1061 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1062 sizeof(*sysno), sysno);
1067 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1069 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1073 print_rec_keys(zh, zh->reg->keys);
1075 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1076 recordAttr->staticrank);
1077 zh->records_inserted++;
1081 /* record already exists */
1082 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1084 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1086 struct sortKeys sortKeys;
1091 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1092 recordType, pr_fname, (long) recordOffset);
1097 rec = rec_get (zh->reg->records, *sysno);
1100 recordAttr = rec_init_attr (zh->reg->zei, rec);
1102 zebra_rec_keys_set_buf(delkeys,
1103 rec->info[recInfo_delKeys],
1104 rec->size[recInfo_delKeys],
1107 zebra_rec_keys_set_buf(sortKeys,
1108 rec->info[recInfo_sortKeys],
1109 rec->size[recInfo_sortKeys],
1112 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1113 sortKeys.buf = rec->info[recInfo_sortKeys];
1117 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1119 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1121 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1122 recordAttr->staticrank);
1125 /* record going to be deleted */
1126 if (zebra_rec_keys_empty(delkeys))
1128 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1129 pr_fname, (long) recordOffset);
1130 yaz_log (YLOG_WARN, "cannot delete file above, "
1131 "storeKeys false (3)");
1136 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1137 pr_fname, (long) recordOffset);
1138 zh->records_deleted++;
1141 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1142 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1144 rec_del (zh->reg->records, &rec);
1153 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1154 pr_fname, (long) recordOffset);
1155 recordAttr->staticrank = extractCtrl.staticrank;
1157 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1159 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1161 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1162 recordAttr->staticrank);
1163 zh->records_updated++;
1165 zebra_rec_keys_close(delkeys);
1167 zebra_rec_keys_close(sortKeys);
1170 /* update file type */
1171 xfree (rec->info[recInfo_fileType]);
1172 rec->info[recInfo_fileType] =
1173 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1175 /* update filename */
1176 xfree (rec->info[recInfo_filename]);
1177 rec->info[recInfo_filename] =
1178 rec_strdup (fname, &rec->size[recInfo_filename]);
1180 /* update delete keys */
1181 xfree (rec->info[recInfo_delKeys]);
1182 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1184 zebra_rec_keys_get_buf(zh->reg->keys,
1185 &rec->info[recInfo_delKeys],
1186 &rec->size[recInfo_delKeys]);
1190 rec->info[recInfo_delKeys] = NULL;
1191 rec->size[recInfo_delKeys] = 0;
1193 /* update sort keys */
1194 xfree (rec->info[recInfo_sortKeys]);
1197 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1198 &rec->info[recInfo_sortKeys],
1199 &rec->size[recInfo_sortKeys]);
1201 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1202 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1203 zh->reg->sortKeys.buf = NULL;
1204 zh->reg->sortKeys.buf_max = 0;
1207 /* save file size of original record */
1208 zebraExplain_recordBytesIncrement (zh->reg->zei,
1209 - recordAttr->recordSize);
1211 recordAttr->recordSize = fi->file_moffset - recordOffset;
1212 if (!recordAttr->recordSize)
1213 recordAttr->recordSize = fi->file_max - recordOffset;
1215 recordAttr->recordSize = buf_size;
1217 zebraExplain_recordBytesIncrement (zh->reg->zei,
1218 recordAttr->recordSize);
1220 /* set run-number for this record */
1221 recordAttr->runNumber =
1222 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1224 /* update store data */
1225 xfree (rec->info[recInfo_storeData]);
1227 /* update store data */
1228 if (zh->store_data_buf)
1230 rec->size[recInfo_storeData] = zh->store_data_size;
1231 rec->info[recInfo_storeData] = zh->store_data_buf;
1232 zh->store_data_buf = 0;
1234 else if (zh->m_store_data)
1236 rec->size[recInfo_storeData] = recordAttr->recordSize;
1237 rec->info[recInfo_storeData] = (char *)
1238 xmalloc (recordAttr->recordSize);
1239 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1243 rec->info[recInfo_storeData] = NULL;
1244 rec->size[recInfo_storeData] = 0;
1246 /* update database name */
1247 xfree (rec->info[recInfo_databaseName]);
1248 rec->info[recInfo_databaseName] =
1249 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1252 recordAttr->recordOffset = recordOffset;
1254 /* commit this record */
1255 rec_put (zh->reg->records, &rec);
1260 int explain_extract (void *handle, Record rec, data1_node *n)
1262 ZebraHandle zh = (ZebraHandle) handle;
1263 struct recExtractCtrl extractCtrl;
1265 if (zebraExplain_curDatabase (zh->reg->zei,
1266 rec->info[recInfo_databaseName]))
1269 if (zebraExplain_newDatabase (zh->reg->zei,
1270 rec->info[recInfo_databaseName], 0))
1274 zebra_rec_keys_reset(zh->reg->keys);
1277 zebra_rec_keys_reset(zh->reg->sortKeys);
1279 zh->reg->sortKeys.buf_used = 0;
1281 extractCtrl.init = extract_init;
1282 extractCtrl.tokenAdd = extract_token_add;
1283 extractCtrl.schemaAdd = extract_schema_add;
1284 extractCtrl.dh = zh->reg->dh;
1286 init_extractCtrl(zh, &extractCtrl);
1288 extractCtrl.flagShowRecords = 0;
1289 extractCtrl.match_criteria[0] = '\0';
1290 extractCtrl.staticrank = 0;
1291 extractCtrl.handle = handle;
1292 extractCtrl.first_record = 1;
1294 extract_set_store_data_prepare(&extractCtrl);
1297 grs_extract_tree(&extractCtrl, n);
1299 if (rec->size[recInfo_delKeys])
1301 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1304 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1306 struct sortKeys sortkeys;
1309 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1310 rec->size[recInfo_delKeys],
1312 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1313 zebra_rec_keys_close(delkeys);
1315 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1316 rec->size[recInfo_sortKeys],
1319 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1320 zebra_rec_keys_close(sortkeys);
1322 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1323 sortkeys.buf = rec->info[recInfo_sortKeys];
1324 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1327 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1329 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1331 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1334 xfree (rec->info[recInfo_delKeys]);
1335 zebra_rec_keys_get_buf(zh->reg->keys,
1336 &rec->info[recInfo_delKeys],
1337 &rec->size[recInfo_delKeys]);
1339 xfree (rec->info[recInfo_sortKeys]);
1341 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1342 &rec->info[recInfo_sortKeys],
1343 &rec->size[recInfo_sortKeys]);
1345 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1346 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1347 zh->reg->sortKeys.buf = NULL;
1348 zh->reg->sortKeys.buf_max = 0;
1354 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1355 zebra_rec_keys_t reckeys)
1357 ZebraExplainInfo zei = zh->reg->zei;
1361 struct ord_stat *next;
1364 if (zebra_rec_keys_rewind(reckeys))
1366 struct ord_stat *ord_list = 0;
1370 struct it_key key_in;
1371 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1373 int ord = key_in.mem[0];
1375 for (p = ord_list; p ; p = p->next)
1383 p = xmalloc(sizeof(*p));
1394 struct ord_stat *p1 = p;
1397 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1399 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1406 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1408 zebra_rec_keys_t reckeys,
1411 ZebraExplainInfo zei = zh->reg->zei;
1413 extract_rec_keys_adjust(zh, cmd, reckeys);
1415 if (!zh->reg->key_buf)
1417 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1420 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1423 /* FIXME: That "8" should be in a default settings include */
1424 /* not hard-coded here! -H */
1425 zh->reg->key_buf = (char**) xmalloc (mem);
1426 zh->reg->ptr_top = mem/sizeof(char*);
1428 zh->reg->key_buf_used = 0;
1429 zh->reg->key_file_no = 0;
1431 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1433 if (zebra_rec_keys_rewind(reckeys))
1437 struct it_key key_in;
1438 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1441 struct it_key key_out;
1442 zint *keyp = key_out.mem;
1444 assert(key_in.len == 4);
1446 /* check for buffer overflow */
1447 if (zh->reg->key_buf_used + 1024 >
1448 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1449 extract_flushWriteKeys (zh, 0);
1452 assert(zh->reg->ptr_i > 0);
1453 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1454 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1456 /* encode the ordinal value (field/use/attribute) .. */
1457 ch = (int) key_in.mem[0];
1458 zh->reg->key_buf_used +=
1459 key_SU_encode(ch, (char*)zh->reg->key_buf +
1460 zh->reg->key_buf_used);
1462 /* copy the 0-terminated stuff from str to output */
1463 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1464 zh->reg->key_buf_used += slen;
1465 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1467 /* the delete/insert indicator */
1468 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1470 if (zh->m_staticrank) /* rank config enabled ? */
1474 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1478 *keyp++ = staticrank;
1484 if (key_in.mem[1]) /* filter specified record ID */
1485 *keyp++ = key_in.mem[1];
1488 *keyp++ = key_in.mem[2]; /* section_id */
1489 *keyp++ = key_in.mem[3]; /* sequence .. */
1491 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1492 &key_out, sizeof(key_out));
1493 (zh->reg->key_buf_used) += sizeof(key_out);
1498 void extract_flushWriteKeys (ZebraHandle zh, int final)
1499 /* optimizing: if final=1, and no files written yet */
1500 /* push the keys directly to merge, sidestepping the */
1501 /* temp file altogether. Speeds small updates */
1504 char out_fname[200];
1506 struct encode_info encode_info;
1507 int ptr_i = zh->reg->ptr_i;
1512 if (!zh->reg->key_buf || ptr_i <= 0)
1514 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1515 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1516 yaz_log (YLOG_DEBUG, " buf=%p ",
1518 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1519 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1524 (zh->reg->key_file_no)++;
1525 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1526 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1527 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1529 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1530 sizeof(char*), key_qsort_compare);
1532 /* zebra.cfg: tempfiles:
1533 Y: always use temp files (old way)
1534 A: use temp files, if more than one (auto)
1535 = if this is both the last and the first
1536 N: never bother with temp files (new) */
1538 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1539 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1540 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1545 if ( ( temp_policy =='N' ) || /* always from memory */
1546 ( ( temp_policy =='A' ) && /* automatic */
1547 (zh->reg->key_file_no == 1) && /* this is first time */
1548 (final) ) ) /* and last (=only) time */
1549 { /* go directly from memory */
1550 zh->reg->key_file_no =0; /* signal not to read files */
1551 zebra_index_merge(zh);
1553 zh->reg->key_buf_used = 0;
1557 /* Not doing directly from memory, write into a temp file */
1558 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1560 if (!(outf = fopen (out_fname, "wb")))
1562 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1565 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1566 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1568 encode_key_init (&encode_info);
1569 encode_key_write (cp, &encode_info, outf);
1573 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1574 if (strcmp (cp, prevcp))
1576 encode_key_flush ( &encode_info, outf);
1577 encode_key_init (&encode_info);
1578 encode_key_write (cp, &encode_info, outf);
1582 encode_key_write (cp + strlen(cp), &encode_info, outf);
1584 encode_key_flush ( &encode_info, outf);
1586 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1587 extract_get_fname_tmp (out_fname, key_file_no);
1589 if (!(outf = fopen (out_fname, "wb")))
1591 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1594 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1596 prevcp = key_buf[ptr_top-i];
1598 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1600 key_y_len = strlen(prevcp)+1;
1602 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1603 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1605 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1606 sizeof(char*), key_y_compare);
1607 cp = key_buf[ptr_top-ptr_i];
1609 encode_key_init (&encode_info);
1610 encode_key_write (cp, &encode_info, outf);
1613 cp = key_buf[ptr_top-ptr_i];
1614 encode_key_write (cp+key_y_len, &encode_info, outf);
1616 encode_key_flush ( &encode_info, outf);
1619 prevcp = key_buf[ptr_top-ptr_i];
1624 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1627 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1629 zh->reg->key_buf_used = 0;
1632 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1633 zebra_rec_keys_t reckeys,
1634 zebra_snippets *snippets)
1636 NMEM nmem = nmem_create();
1637 if (zebra_rec_keys_rewind(reckeys))
1642 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1644 char dst_buf[IT_MAX_WORD];
1645 char *dst_term = dst_buf;
1648 assert(key.len <= 4 && key.len > 2);
1649 seqno = (int) key.mem[key.len-1];
1652 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1653 0/* db */, 0/* set */, 0/* use */,
1654 0 /* string_index */);
1656 zebra_term_untrans_iconv(zh, nmem, index_type,
1658 zebra_snippets_append(snippets, seqno, ord, dst_term);
1666 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1668 yaz_log(YLOG_LOG, "print_rec_keys");
1669 if (zebra_rec_keys_rewind(reckeys))
1674 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1676 char dst_buf[IT_MAX_WORD];
1680 assert(key.len <= 4 && key.len > 2);
1682 zebraExplain_lookup_ord(zh->reg->zei,
1683 key.mem[0], &index_type, &db, 0, 0, 0);
1685 seqno = (int) key.mem[key.len-1];
1687 zebra_term_untrans(zh, index_type, dst_buf, str);
1689 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1690 key.mem[0], seqno, dst_buf);
1695 void extract_add_index_string (RecWord *p, const char *str, int length)
1699 ZebraHandle zh = p->extractCtrl->handle;
1700 ZebraExplainInfo zei = zh->reg->zei;
1705 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1707 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1714 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
1715 p->attrSet, p->attrUse);
1717 ch = zebraExplain_add_attr_su(zei, p->index_type,
1718 p->attrSet, p->attrUse);
1723 key.mem[1] = p->record_id;
1724 key.mem[2] = p->section_id;
1725 key.mem[3] = p->seqno;
1734 for (i = 0; i<length && i < 20; i++)
1735 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1736 /* just for debugging .. */
1737 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1738 "record_id=%lld section_id=%lld seqno=%lld %s",
1739 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1744 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1748 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1752 ZebraHandle zh = p->extractCtrl->handle;
1753 ZebraExplainInfo zei = zh->reg->zei;
1758 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1760 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1768 key.mem[1] = p->record_id;
1769 key.mem[2] = p->section_id;
1770 key.mem[3] = p->seqno;
1772 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1775 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1777 ZebraHandle zh = p->extractCtrl->handle;
1778 struct sortKeys *sk = &zh->reg->sortKeys;
1781 while (off < sk->buf_used)
1785 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1786 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1787 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1789 if (p->attrSet == set && p->attrUse == use)
1792 assert (off == sk->buf_used);
1794 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1798 b = (char *) xmalloc (sk->buf_max += 128000);
1799 if (sk->buf_used > 0)
1800 memcpy (b, sk->buf, sk->buf_used);
1804 off += key_SU_encode(p->attrSet, sk->buf + off);
1805 off += key_SU_encode(p->attrUse, sk->buf + off);
1806 off += key_SU_encode(length, sk->buf + off);
1807 memcpy (sk->buf + off, str, length);
1808 sk->buf_used = off + length;
1812 void extract_add_string (RecWord *p, const char *string, int length)
1814 assert (length > 0);
1815 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1816 extract_add_sort_string (p, string, length);
1818 extract_add_index_string (p, string, length);
1821 static void extract_add_incomplete_field (RecWord *p)
1823 const char *b = p->term_buf;
1824 int remain = p->term_len;
1825 const char **map = 0;
1827 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1830 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1834 char buf[IT_MAX_WORD+1];
1838 while (map && *map && **map == *CHR_SPACE)
1840 remain = p->term_len - (b - p->term_buf);
1842 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1850 while (map && *map && **map != *CHR_SPACE)
1852 const char *cp = *map;
1854 while (i < IT_MAX_WORD && *cp)
1856 remain = p->term_len - (b - p->term_buf);
1858 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1864 extract_add_string (p, buf, i);
1869 static void extract_add_complete_field (RecWord *p)
1871 const char *b = p->term_buf;
1872 char buf[IT_MAX_WORD+1];
1873 const char **map = 0;
1874 int i = 0, remain = p->term_len;
1876 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1877 p->term_len, p->term_buf);
1880 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1882 while (remain > 0 && i < IT_MAX_WORD)
1884 while (map && *map && **map == *CHR_SPACE)
1886 remain = p->term_len - (b - p->term_buf);
1890 int first = i ? 0 : 1; /* first position */
1891 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1899 if (i && i < IT_MAX_WORD)
1900 buf[i++] = *CHR_SPACE;
1901 while (map && *map && **map != *CHR_SPACE)
1903 const char *cp = *map;
1905 if (**map == *CHR_CUT)
1911 if (i >= IT_MAX_WORD)
1913 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1914 while (i < IT_MAX_WORD && *cp)
1917 remain = p->term_len - (b - p->term_buf);
1920 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1929 extract_add_string (p, buf, i);
1932 void extract_token_add (RecWord *p)
1936 yaz_log (YLOG_LOG, "token_add "
1937 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1938 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1941 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1942 p->term_buf, p->term_len)))
1944 p->term_buf = wrbuf_buf(wrbuf);
1945 p->term_len = wrbuf_len(wrbuf);
1947 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1948 extract_add_complete_field (p);
1950 extract_add_incomplete_field(p);
1953 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1954 void *buf, size_t sz)
1956 ZebraHandle zh = (ZebraHandle) p->handle;
1958 xfree(zh->store_data_buf);
1959 zh->store_data_buf = 0;
1960 zh->store_data_size = 0;
1963 zh->store_data_buf = xmalloc(sz);
1964 zh->store_data_size = sz;
1965 memcpy(zh->store_data_buf, buf, sz);
1969 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1971 ZebraHandle zh = (ZebraHandle) p->handle;
1972 xfree(zh->store_data_buf);
1973 zh->store_data_buf = 0;
1974 zh->store_data_size = 0;
1975 p->setStoreData = extract_set_store_data_cb;
1978 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1980 ZebraHandle zh = (ZebraHandle) p->handle;
1981 zebraExplain_addSchema (zh->reg->zei, oid);
1985 #error not done yet with zebra_rec_keys_t
1986 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1987 int cmd, struct recKeys *reckeys)
1989 SortIdx sortIdx = zh->reg->sortIdx;
1990 void *decode_handle = iscz1_start();
1994 while (off < reckeys->buf_used)
1996 const char *src = reckeys->buf + off;
1998 char *dst = (char*) &key;
2000 iscz1_decode(decode_handle, &dst, &src);
2001 assert(key.len == 4);
2003 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
2005 sortIdx_type(sortIdx, ch);
2007 sortIdx_add(sortIdx, src, strlen(src));
2009 sortIdx_add(sortIdx, "", 1);
2014 off = src - reckeys->buf;
2016 assert (off == reckeys->buf_used);
2017 iscz1_stop(decode_handle);
2020 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
2021 int cmd, struct sortKeys *sk)
2023 SortIdx sortIdx = zh->reg->sortIdx;
2026 sortIdx_sysno (sortIdx, sysno);
2028 while (off < sk->buf_used)
2032 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
2033 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
2034 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
2036 sortIdx_type(sortIdx, use);
2038 sortIdx_add(sortIdx, sk->buf + off, slen);
2040 sortIdx_add(sortIdx, "", 1);
2046 void encode_key_init (struct encode_info *i)
2055 i->encode_handle = iscz1_start();
2056 i->decode_handle = iscz1_start();
2062 /* this is the old encode_key_write
2063 * may be deleted once we are confident that the new works
2066 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2069 char *bp = i->buf, *bp0;
2070 const char *src = (char *) &key;
2072 /* copy term to output buf */
2073 while ((*bp++ = *k++))
2075 /* and copy & align key so we can mangle */
2076 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
2080 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
2082 assert(key.mem[0] >= 0);
2085 iscz1_encode(i->encode_handle, &bp, &src);
2087 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
2088 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2090 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2099 const char *src = bp0+1;
2100 char *dst = (char*) &key2;
2101 iscz1_decode(i->decode_handle, &dst, &src);
2103 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
2105 assert(key2.mem[1]);
2110 void encode_key_flush (struct encode_info *i, FILE *outf)
2112 iscz1_stop(i->encode_handle);
2113 iscz1_stop(i->decode_handle);
2118 /* new encode_key_write
2119 * The idea is to buffer one more key, and compare them
2120 * If we are going to delete and insert the same key,
2121 * we may as well not bother. Should make a difference in
2122 * updates with small modifications (appending to a mbox)
2124 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2129 if (*k) /* first time for new key */
2132 while ((*bp++ = *k++))
2134 i->keylen= bp - i->buf -1;
2135 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2139 bp=i->buf + i->keylen;
2144 memcpy (&key, k+1, sizeof(struct it_key));
2145 if (0==i->prevsys) /* no previous filter, fill up */
2147 i->prevsys=key.sysno;
2148 i->prevseq=key.seqno;
2151 else if ( (i->prevsys==key.sysno) &&
2152 (i->prevseq==key.seqno) &&
2154 { /* same numbers, diff cmd, they cancel out */
2158 { /* different stuff, write previous, move buf */
2159 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2160 if (i->sysno != i->prevsys)
2162 i->sysno = i->prevsys;
2165 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2167 return; /* ??? Filters some sort of duplicates away */
2168 /* ??? Can this ever happen -H 15oct02 */
2170 bp = encode_key_int (i->prevseq - i->seqno, bp);
2171 i->seqno = i->prevseq;
2172 i->cmd = i->prevcmd;
2173 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2175 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2178 i->keylen=0; /* ok, it's written, forget it */
2179 i->prevsys=key.sysno;
2180 i->prevseq=key.seqno;
2185 void encode_key_flush (struct encode_info *i, FILE *outf)
2186 { /* flush the last key from i */
2187 char *bp =i->buf + i->keylen;
2190 return; /* nothing to flush */
2193 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2194 if (i->sysno != i->prevsys)
2196 i->sysno = i->prevsys;
2199 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2201 return; /* ??? Filters some sort of duplicates away */
2202 /* ??? Can this ever happen -H 15oct02 */
2204 bp = encode_key_int (i->prevseq - i->seqno, bp);
2205 i->seqno = i->prevseq;
2206 i->cmd = i->prevcmd;
2207 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2209 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2212 i->keylen=0; /* ok, it's written, forget it */
2213 i->prevsys=0; /* forget the values too */
2220 * indent-tabs-mode: nil
2222 * vim: shiftwidth=4 tabstop=8 expandtab