1 /* $Id: extract.c,v 1.249 2007-02-06 09:34:56 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 static int log_level_extract = 0;
40 static int log_level_details = 0;
41 static int log_level_initialized = 0;
43 static void zebra_init_log_level(void)
45 if (!log_level_initialized)
47 log_level_initialized = 1;
49 log_level_extract = yaz_log_module_level("extract");
50 log_level_details = yaz_log_module_level("indexdetails");
54 static void extract_flush_record_keys(ZebraHandle zh, zint sysno,
55 int cmd, zebra_rec_keys_t reckeys,
57 static void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
58 int cmd, zebra_rec_keys_t skp);
59 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
60 static void extract_token_add (RecWord *p);
62 static void check_log_limit(ZebraHandle zh)
64 if (zh->records_processed + zh->records_skipped == zh->m_file_verbose_limit)
66 yaz_log(YLOG_LOG, "More than %d file log entries. Omitting rest",
67 zh->m_file_verbose_limit);
71 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,
80 zh->records_updated, zh->records_deleted);
84 static void extract_add_index_string (RecWord *p,
85 zinfo_index_category_t cat,
86 const char *str, int length);
88 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
90 static void extract_init(struct recExtractCtrl *p, RecWord *w)
93 w->index_name = "any";
101 static void searchRecordKey(ZebraHandle zh,
102 zebra_rec_keys_t reckeys,
103 const char *index_name,
104 const char **ws, int ws_length)
108 zinfo_index_category_t cat = zinfo_index_category_index;
110 for (i = 0; i<ws_length; i++)
114 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
116 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
118 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
123 if (zebra_rec_keys_rewind(reckeys))
130 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
132 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
134 seqno = key.mem[key.len-1];
136 if (key.mem[0] == ch)
142 woff = seqno - startSeq;
143 if (woff >= 0 && woff < ws_length)
150 #define FILE_MATCH_BLANK "\t "
152 static char *get_match_from_spec(ZebraHandle zh,
153 zebra_rec_keys_t reckeys,
154 const char *fname, const char *spec)
156 static char dstBuf[2048]; /* static here ??? */
158 const char *s = spec;
162 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
169 char attset_str[64], attname_str[64];
173 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
175 for (i = 0; *s && *s != ',' && *s != ')' &&
176 !strchr(FILE_MATCH_BLANK, *s); s++)
177 if (i+1 < sizeof(attset_str))
178 attset_str[i++] = *s;
179 attset_str[i] = '\0';
181 for (; strchr(FILE_MATCH_BLANK, *s); s++)
184 strcpy(attname_str, attset_str);
187 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
189 for (i = 0; *s && *s != ')' &&
190 !strchr(FILE_MATCH_BLANK, *s); s++)
191 if (i+1 < sizeof(attname_str))
192 attname_str[i++] = *s;
193 attname_str[i] = '\0';
196 searchRecordKey (zh, reckeys, attname_str, ws, 32);
200 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
201 spec, zh->m_group ? zh->m_group : "none");
206 for (i = 0; i<32; i++)
215 dst += strlen(ws[i]);
219 yaz_log (YLOG_WARN, "Record didn't contain match"
220 " fields in (%s,%s)", attset_str, attname_str);
228 const char *spec_src = NULL;
229 const char *s1 = ++s;
230 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
234 if (spec_len > sizeof(special)-1)
235 spec_len = sizeof(special)-1;
236 memcpy (special, s, spec_len);
237 special[spec_len] = '\0';
240 if (!strcmp (special, "group"))
241 spec_src = zh->m_group;
242 else if (!strcmp (special, "database"))
243 spec_src = zh->basenames[0];
244 else if (!strcmp (special, "filename")) {
247 else if (!strcmp (special, "type"))
248 spec_src = zh->m_record_type;
253 strcpy (dst, spec_src);
254 dst += strlen (spec_src);
257 else if (*s == '\"' || *s == '\'')
259 int stopMarker = *s++;
263 while (*s && *s != stopMarker)
265 if (i+1 < sizeof(tmpString))
266 tmpString[i++] = *s++;
271 strcpy (dst, tmpString);
272 dst += strlen (tmpString);
276 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
277 spec, zh->m_group ? zh->m_group : "none");
284 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
285 fname, zh->m_group ? zh->m_group : "none");
292 struct recordLogInfo {
295 struct recordGroup *rGroup;
298 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
301 for (i = 0; i<256; i++)
303 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
308 ctrl->flagShowRecords = !zh->m_flag_rw;
311 static void all_matches_add(struct recExtractCtrl *ctrl)
314 extract_init(ctrl, &word);
315 word.index_name = "_ALLRECORDS";
316 word.index_type = 'w';
318 extract_add_index_string (&word, zinfo_index_category_alwaysmatches,
322 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
325 ZEBRA_RES r = ZEBRA_OK;
330 struct file_read_info *fi = 0;
331 const char *original_record_type = 0;
333 void *recTypeClientData;
334 struct ZebraRecStream stream, *streamp;
336 zebra_init_log_level();
338 if (!zh->m_group || !*zh->m_group)
341 sprintf (gprefix, "%s.", zh->m_group);
343 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
345 /* determine file extension */
347 for (i = strlen(fname); --i >= 0; )
350 else if (fname[i] == '.')
352 strcpy (ext, fname+i+1);
355 /* determine file type - depending on extension */
356 original_record_type = zh->m_record_type;
357 if (!zh->m_record_type)
359 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
360 zh->m_record_type = res_get (zh->res, ext_res);
362 if (!zh->m_record_type)
365 if (zh->records_processed + zh->records_skipped
366 < zh->m_file_verbose_limit)
367 yaz_log (YLOG_LOG, "? %s", fname);
368 zh->records_skipped++;
371 /* determine match criteria */
372 if (!zh->m_record_id)
374 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
375 zh->m_record_id = res_get (zh->res, ext_res);
379 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
380 &recTypeClientData)))
382 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
386 switch(recType->version)
391 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
393 if (sysno && deleteFlag)
402 if (zh->path_reg && !yaz_is_abspath (fname))
404 strcpy (full_rep, zh->path_reg);
405 strcat (full_rep, "/");
406 strcat (full_rep, fname);
409 strcpy (full_rep, fname);
411 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
413 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
414 zh->m_record_type = original_record_type;
418 zebra_create_stream_fd(streamp, fd, 0);
423 r = zebra_extract_record_stream(zh, streamp,
428 0, /*match_criteria */
430 1, /* force_update */
431 1, /* allow_update */
432 recType, recTypeClientData, &more);
441 stream.destroy(streamp);
442 zh->m_record_type = original_record_type;
447 If sysno is provided, then it's used to identify the reocord.
448 If not, and match_criteria is provided, then sysno is guessed
449 If not, and a record is provided, then sysno is got from there
453 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
454 const char *buf, size_t buf_size,
457 const char *recordType,
459 const char *match_criteria,
464 struct ZebraRecStream stream;
470 if (recordType && *recordType)
472 yaz_log(log_level_extract,
473 "Record type explicitly specified: %s", recordType);
474 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
479 if (!(zh->m_record_type))
481 yaz_log (YLOG_WARN, "No such record type defined");
484 yaz_log(log_level_extract, "Get record type from rgroup: %s",
486 recType = recType_byName (zh->reg->recTypes, zh->res,
487 zh->m_record_type, &clientData);
488 recordType = zh->m_record_type;
493 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
497 zebra_create_stream_mem(&stream, buf, buf_size);
499 res = zebra_extract_record_stream(zh, &stream,
508 recType, clientData, &more);
509 stream.destroy(&stream);
514 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
515 struct ZebraRecStream *stream,
518 const char *recordType,
520 const char *match_criteria,
525 void *recTypeClientData,
530 RecordAttr *recordAttr;
531 struct recExtractCtrl extractCtrl;
533 const char *matchStr = 0;
535 off_t start_offset = 0, end_offset = 0;
536 const char *pr_fname = fname; /* filename to print .. */
537 int show_progress = zh->records_processed + zh->records_skipped
538 < zh->m_file_verbose_limit ? 1:0;
540 zebra_init_log_level();
543 pr_fname = "<no file>"; /* make it printable if file is omitted */
545 zebra_rec_keys_reset(zh->reg->keys);
546 zebra_rec_keys_reset(zh->reg->sortKeys);
548 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
550 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
551 zh->m_explain_database))
557 off_t null_offset = 0;
558 extractCtrl.stream = stream;
560 start_offset = stream->tellf(stream);
562 extractCtrl.first_record = start_offset ? 0 : 1;
564 stream->endf(stream, &null_offset);;
566 extractCtrl.init = extract_init;
567 extractCtrl.tokenAdd = extract_token_add;
568 extractCtrl.schemaAdd = extract_schema_add;
569 extractCtrl.dh = zh->reg->dh;
570 extractCtrl.handle = zh;
571 extractCtrl.match_criteria[0] = '\0';
572 extractCtrl.staticrank = 0;
574 init_extractCtrl(zh, &extractCtrl);
576 extract_set_store_data_prepare(&extractCtrl);
578 r = (*recType->extract)(recTypeClientData, &extractCtrl);
580 if (r == RECCTRL_EXTRACT_EOF)
582 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
584 /* error occured during extraction ... */
585 yaz_log (YLOG_WARN, "extract error: generic");
588 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
590 /* error occured during extraction ... */
591 yaz_log (YLOG_WARN, "extract error: no such filter");
595 all_matches_add(&extractCtrl);
597 if (extractCtrl.match_criteria[0])
598 match_criteria = extractCtrl.match_criteria;
601 end_offset = stream->endf(stream, 0);
604 end_offset = stream->tellf(stream);
606 stream->seekf(stream, end_offset);
616 if (match_criteria && *match_criteria) {
617 matchStr = match_criteria;
619 if (zh->m_record_id && *zh->m_record_id) {
620 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
624 yaz_log (YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
625 pr_fname, (zint) start_offset);
632 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
633 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
637 assert(*rinfo == sizeof(*sysno));
638 memcpy (sysno, rinfo+1, sizeof(*sysno));
642 if (zebra_rec_keys_empty(zh->reg->keys))
644 /* the extraction process returned no information - the record
645 is probably empty - unless flagShowRecords is in use */
655 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
656 pr_fname, (zint) start_offset);
657 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
661 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
662 (zint) start_offset);
663 rec = rec_new (zh->reg->records);
667 recordAttr = rec_init_attr (zh->reg->zei, rec);
668 if (extractCtrl.staticrank < 0)
670 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
671 extractCtrl.staticrank = 0;
673 recordAttr->staticrank = extractCtrl.staticrank;
677 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
678 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
679 sizeof(*sysno), sysno);
682 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
683 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
684 recordAttr->staticrank);
685 zh->records_inserted++;
689 /* record already exists */
690 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
691 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
694 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
695 recordType, pr_fname, (zint) start_offset);
700 rec = rec_get (zh->reg->records, *sysno);
703 recordAttr = rec_init_attr (zh->reg->zei, rec);
705 /* decrease total size */
706 zebraExplain_recordBytesIncrement (zh->reg->zei,
707 - recordAttr->recordSize);
709 zebra_rec_keys_set_buf(delkeys,
710 rec->info[recInfo_delKeys],
711 rec->size[recInfo_delKeys],
713 zebra_rec_keys_set_buf(sortKeys,
714 rec->info[recInfo_sortKeys],
715 rec->size[recInfo_sortKeys],
718 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
719 extract_flush_record_keys(zh, *sysno, 0, delkeys,
720 recordAttr->staticrank);
723 /* record going to be deleted */
724 if (zebra_rec_keys_empty(delkeys))
726 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
727 pr_fname, (zint) start_offset);
728 yaz_log(YLOG_WARN, "cannot delete file above, "
729 "storeKeys false (3)");
734 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
735 pr_fname, (zint) start_offset);
736 zh->records_deleted++;
739 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
740 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
742 rec_del (zh->reg->records, &rec);
751 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
752 pr_fname, (zint) start_offset);
753 recordAttr->staticrank = extractCtrl.staticrank;
754 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
755 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
756 recordAttr->staticrank);
757 zh->records_updated++;
759 zebra_rec_keys_close(delkeys);
760 zebra_rec_keys_close(sortKeys);
762 /* update file type */
763 xfree (rec->info[recInfo_fileType]);
764 rec->info[recInfo_fileType] =
765 rec_strdup (recordType, &rec->size[recInfo_fileType]);
767 /* update filename */
768 xfree (rec->info[recInfo_filename]);
769 rec->info[recInfo_filename] =
770 rec_strdup (fname, &rec->size[recInfo_filename]);
772 /* update delete keys */
773 xfree (rec->info[recInfo_delKeys]);
774 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
776 zebra_rec_keys_get_buf(zh->reg->keys,
777 &rec->info[recInfo_delKeys],
778 &rec->size[recInfo_delKeys]);
782 rec->info[recInfo_delKeys] = NULL;
783 rec->size[recInfo_delKeys] = 0;
785 /* update sort keys */
786 xfree (rec->info[recInfo_sortKeys]);
788 zebra_rec_keys_get_buf(zh->reg->sortKeys,
789 &rec->info[recInfo_sortKeys],
790 &rec->size[recInfo_sortKeys]);
794 recordAttr->recordSize = end_offset - start_offset;
795 zebraExplain_recordBytesIncrement(zh->reg->zei,
796 recordAttr->recordSize);
799 /* set run-number for this record */
800 recordAttr->runNumber =
801 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
803 /* update store data */
804 xfree (rec->info[recInfo_storeData]);
806 /* update store data */
807 if (zh->store_data_buf)
809 rec->size[recInfo_storeData] = zh->store_data_size;
810 rec->info[recInfo_storeData] = zh->store_data_buf;
811 zh->store_data_buf = 0;
812 recordAttr->recordSize = zh->store_data_size;
814 else if (zh->m_store_data)
816 off_t cur_offset = stream->tellf(stream);
818 rec->size[recInfo_storeData] = recordAttr->recordSize;
819 rec->info[recInfo_storeData] = (char *)
820 xmalloc (recordAttr->recordSize);
821 stream->seekf(stream, start_offset);
822 stream->readf(stream, rec->info[recInfo_storeData],
823 recordAttr->recordSize);
824 stream->seekf(stream, cur_offset);
828 rec->info[recInfo_storeData] = NULL;
829 rec->size[recInfo_storeData] = 0;
831 /* update database name */
832 xfree (rec->info[recInfo_databaseName]);
833 rec->info[recInfo_databaseName] =
834 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
837 recordAttr->recordOffset = start_offset;
839 /* commit this record */
840 rec_put (zh->reg->records, &rec);
845 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
847 ZebraHandle zh = (ZebraHandle) handle;
848 struct recExtractCtrl extractCtrl;
850 if (zebraExplain_curDatabase (zh->reg->zei,
851 rec->info[recInfo_databaseName]))
854 if (zebraExplain_newDatabase (zh->reg->zei,
855 rec->info[recInfo_databaseName], 0))
859 zebra_rec_keys_reset(zh->reg->keys);
860 zebra_rec_keys_reset(zh->reg->sortKeys);
862 extractCtrl.init = extract_init;
863 extractCtrl.tokenAdd = extract_token_add;
864 extractCtrl.schemaAdd = extract_schema_add;
865 extractCtrl.dh = zh->reg->dh;
867 init_extractCtrl(zh, &extractCtrl);
869 extractCtrl.flagShowRecords = 0;
870 extractCtrl.match_criteria[0] = '\0';
871 extractCtrl.staticrank = 0;
872 extractCtrl.handle = handle;
873 extractCtrl.first_record = 1;
875 extract_set_store_data_prepare(&extractCtrl);
878 grs_extract_tree(&extractCtrl, n);
880 if (rec->size[recInfo_delKeys])
882 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
884 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
886 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
887 rec->size[recInfo_delKeys],
889 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
890 zebra_rec_keys_close(delkeys);
892 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
893 rec->size[recInfo_sortKeys],
896 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
897 zebra_rec_keys_close(sortkeys);
899 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
900 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
902 xfree (rec->info[recInfo_delKeys]);
903 zebra_rec_keys_get_buf(zh->reg->keys,
904 &rec->info[recInfo_delKeys],
905 &rec->size[recInfo_delKeys]);
907 xfree (rec->info[recInfo_sortKeys]);
908 zebra_rec_keys_get_buf(zh->reg->sortKeys,
909 &rec->info[recInfo_sortKeys],
910 &rec->size[recInfo_sortKeys]);
914 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
915 zebra_rec_keys_t reckeys,
918 if (zebra_rec_keys_rewind(reckeys))
923 NMEM nmem = nmem_create();
925 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
927 char keystr[200]; /* room for zints to print */
929 int ord = CAST_ZINT_TO_INT(key.mem[0]);
931 const char *string_index;
933 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
934 0/* db */, &string_index);
936 zebra_term_untrans_iconv(zh, nmem, index_type,
939 for (i = 0; i<key.len; i++)
941 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
944 if (*str < CHR_BASE_CHAR)
947 char dst_buf[200]; /* room for special chars */
949 strcpy(dst_buf , "?");
951 if (!strcmp(str, ""))
952 strcpy(dst_buf, "alwaysmatches");
953 if (!strcmp(str, FIRST_IN_FIELD_STR))
954 strcpy(dst_buf, "firstinfield");
955 else if (!strcmp(str, CHR_UNKNOWN))
956 strcpy(dst_buf, "unknown");
957 else if (!strcmp(str, CHR_SPACE))
958 strcpy(dst_buf, "space");
960 for (i = 0; i<slen; i++)
962 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
964 yaz_log(level, "%s%c %s %s", keystr, index_type,
965 string_index, dst_buf);
969 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
970 string_index, dst_term);
978 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
979 zebra_rec_keys_t reckeys)
981 ZebraExplainInfo zei = zh->reg->zei;
985 struct ord_stat *next;
988 if (zebra_rec_keys_rewind(reckeys))
990 struct ord_stat *ord_list = 0;
994 struct it_key key_in;
995 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
997 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
999 for (p = ord_list; p ; p = p->next)
1007 p = xmalloc(sizeof(*p));
1018 struct ord_stat *p1 = p;
1021 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1023 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1030 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1031 zebra_rec_keys_t reckeys,
1034 ZebraExplainInfo zei = zh->reg->zei;
1036 extract_rec_keys_adjust(zh, cmd, reckeys);
1038 if (log_level_details)
1040 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1041 sysno, cmd ? "insert" : "delete");
1042 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1045 if (!zh->reg->key_block)
1047 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1048 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1049 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1050 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1052 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1054 if (zebra_rec_keys_rewind(reckeys))
1058 struct it_key key_in;
1059 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1061 key_block_write(zh->reg->key_block, sysno,
1062 &key_in, cmd, str, slen,
1063 staticrank, zh->m_staticrank);
1069 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1070 zebra_rec_keys_t reckeys,
1071 zebra_snippets *snippets)
1073 NMEM nmem = nmem_create();
1074 if (zebra_rec_keys_rewind(reckeys))
1079 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1086 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1087 seqno = key.mem[key.len-1];
1088 ord = CAST_ZINT_TO_INT(key.mem[0]);
1090 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1091 0/* db */, 0 /* string_index */);
1093 zebra_term_untrans_iconv(zh, nmem, index_type,
1095 zebra_snippets_append(snippets, seqno, ord, dst_term);
1103 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1105 yaz_log(YLOG_LOG, "print_rec_keys");
1106 if (zebra_rec_keys_rewind(reckeys))
1111 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1113 char dst_buf[IT_MAX_WORD];
1116 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1118 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1120 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1122 seqno = key.mem[key.len-1];
1124 zebra_term_untrans(zh, index_type, dst_buf, str);
1126 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1127 " term=%s", ord, seqno, dst_buf);
1132 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1133 const char *str, int length)
1136 ZebraHandle zh = p->extractCtrl->handle;
1137 ZebraExplainInfo zei = zh->reg->zei;
1140 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1142 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1146 key.mem[i++] = p->record_id;
1147 key.mem[i++] = p->section_id;
1149 if (zh->m_segment_indexing)
1150 key.mem[i++] = p->segment;
1151 key.mem[i++] = p->seqno;
1154 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1157 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1160 ZebraHandle zh = p->extractCtrl->handle;
1161 ZebraExplainInfo zei = zh->reg->zei;
1163 zinfo_index_category_t cat = zinfo_index_category_sort;
1165 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1167 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1170 key.mem[1] = p->record_id;
1172 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1175 static void extract_add_staticrank_string(RecWord *p,
1176 const char *str, int length)
1179 struct recExtractCtrl *ctrl = p->extractCtrl;
1181 if (length > sizeof(valz)-1)
1182 length = sizeof(valz)-1;
1184 memcpy(valz, str, length);
1185 valz[length] = '\0';
1186 ctrl->staticrank = atozint(valz);
1189 static void extract_add_string(RecWord *p, const char *string, int length)
1191 ZebraHandle zh = p->extractCtrl->handle;
1192 assert (length > 0);
1197 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1199 extract_add_index_string(p, zinfo_index_category_index,
1201 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1204 memcpy(&word, p, sizeof(word));
1207 extract_add_index_string(
1208 &word, zinfo_index_category_alwaysmatches, "", 0);
1211 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1213 extract_add_sort_string(p, string, length);
1215 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1217 extract_add_staticrank_string(p, string, length);
1221 static void extract_add_incomplete_field(RecWord *p)
1223 ZebraHandle zh = p->extractCtrl->handle;
1224 const char *b = p->term_buf;
1225 int remain = p->term_len;
1227 const char **map = 0;
1230 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1234 char buf[IT_MAX_WORD+1];
1238 while (map && *map && **map == *CHR_SPACE)
1240 remain = p->term_len - (b - p->term_buf);
1242 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1250 while (map && *map && **map != *CHR_SPACE)
1252 const char *cp = *map;
1254 while (i < IT_MAX_WORD && *cp)
1256 remain = p->term_len - (b - p->term_buf);
1258 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1268 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1270 /* first in field marker */
1271 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1275 extract_add_string (p, buf, i);
1280 static void extract_add_complete_field (RecWord *p)
1282 ZebraHandle zh = p->extractCtrl->handle;
1283 const char *b = p->term_buf;
1284 char buf[IT_MAX_WORD+1];
1285 const char **map = 0;
1286 int i = 0, remain = p->term_len;
1289 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1291 while (remain > 0 && i < IT_MAX_WORD)
1293 while (map && *map && **map == *CHR_SPACE)
1295 remain = p->term_len - (b - p->term_buf);
1299 int first = i ? 0 : 1; /* first position */
1300 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1308 if (i && i < IT_MAX_WORD)
1309 buf[i++] = *CHR_SPACE;
1310 while (map && *map && **map != *CHR_SPACE)
1312 const char *cp = *map;
1314 if (**map == *CHR_CUT)
1320 if (i >= IT_MAX_WORD)
1322 while (i < IT_MAX_WORD && *cp)
1325 remain = p->term_len - (b - p->term_buf);
1328 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1337 extract_add_string (p, buf, i);
1340 static void extract_token_add(RecWord *p)
1342 ZebraHandle zh = p->extractCtrl->handle;
1345 if (log_level_extract)
1347 yaz_log(log_level_extract, "extract_token_add "
1348 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1349 p->index_type, p->index_name,
1350 p->seqno, p->term_len, p->term_buf);
1352 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1353 p->term_buf, p->term_len)))
1355 p->term_buf = wrbuf_buf(wrbuf);
1356 p->term_len = wrbuf_len(wrbuf);
1358 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1359 extract_add_complete_field (p);
1361 extract_add_incomplete_field(p);
1364 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1365 void *buf, size_t sz)
1367 ZebraHandle zh = (ZebraHandle) p->handle;
1369 xfree(zh->store_data_buf);
1370 zh->store_data_buf = 0;
1371 zh->store_data_size = 0;
1374 zh->store_data_buf = xmalloc(sz);
1375 zh->store_data_size = sz;
1376 memcpy(zh->store_data_buf, buf, sz);
1380 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1382 ZebraHandle zh = (ZebraHandle) p->handle;
1383 xfree(zh->store_data_buf);
1384 zh->store_data_buf = 0;
1385 zh->store_data_size = 0;
1386 p->setStoreData = extract_set_store_data_cb;
1389 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1391 ZebraHandle zh = (ZebraHandle) p->handle;
1392 zebraExplain_addSchema (zh->reg->zei, oid);
1395 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1396 int cmd, zebra_rec_keys_t reckeys)
1399 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1401 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1404 if (zebra_rec_keys_rewind(reckeys))
1406 zebra_sort_index_t si = zh->reg->sort_index;
1409 struct it_key key_in;
1411 zebra_sort_sysno(si, sysno);
1413 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1415 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1417 zebra_sort_type(si, ord);
1419 zebra_sort_add(si, str, slen);
1421 zebra_sort_delete(si);
1429 * indent-tabs-mode: nil
1431 * vim: shiftwidth=4 tabstop=8 expandtab