1 /* $Id: extract.c,v 1.256 2007-04-25 08:22:01 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 /* 1 if we use eliminitate identical delete/insert keys */
44 /* eventually this the 0-case code will be removed */
47 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
48 zebra_rec_keys_t ins_keys,
50 zebra_rec_keys_t del_keys,
53 static void zebra_init_log_level(void)
55 if (!log_level_initialized)
57 log_level_initialized = 1;
59 log_level_extract = yaz_log_module_level("extract");
60 log_level_details = yaz_log_module_level("indexdetails");
64 static void extract_flush_record_keys(ZebraHandle zh, zint sysno,
65 int cmd, zebra_rec_keys_t reckeys,
67 static void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
68 int cmd, zebra_rec_keys_t skp);
69 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
70 static void extract_token_add (RecWord *p);
72 static void check_log_limit(ZebraHandle zh)
74 if (zh->records_processed + zh->records_skipped == zh->m_file_verbose_limit)
76 yaz_log(YLOG_LOG, "More than %d file log entries. Omitting rest",
77 zh->m_file_verbose_limit);
81 static void logRecord (ZebraHandle zh)
84 ++zh->records_processed;
85 if (!(zh->records_processed % 1000))
87 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
88 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
89 zh->records_processed, zh->records_inserted,
90 zh->records_updated, zh->records_deleted);
94 static void extract_add_index_string (RecWord *p,
95 zinfo_index_category_t cat,
96 const char *str, int length);
98 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
100 static void extract_init(struct recExtractCtrl *p, RecWord *w)
103 w->index_name = "any";
111 static void searchRecordKey(ZebraHandle zh,
112 zebra_rec_keys_t reckeys,
113 const char *index_name,
114 const char **ws, int ws_length)
118 zinfo_index_category_t cat = zinfo_index_category_index;
120 for (i = 0; i<ws_length; i++)
124 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
126 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
128 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
133 if (zebra_rec_keys_rewind(reckeys))
140 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
142 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
144 seqno = key.mem[key.len-1];
146 if (key.mem[0] == ch)
152 woff = seqno - startSeq;
153 if (woff >= 0 && woff < ws_length)
160 #define FILE_MATCH_BLANK "\t "
162 static char *get_match_from_spec(ZebraHandle zh,
163 zebra_rec_keys_t reckeys,
164 const char *fname, const char *spec)
166 static char dstBuf[2048]; /* static here ??? */
168 const char *s = spec;
172 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
179 char attset_str[64], attname_str[64];
183 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
185 for (i = 0; *s && *s != ',' && *s != ')' &&
186 !strchr(FILE_MATCH_BLANK, *s); s++)
187 if (i+1 < sizeof(attset_str))
188 attset_str[i++] = *s;
189 attset_str[i] = '\0';
191 for (; strchr(FILE_MATCH_BLANK, *s); s++)
194 strcpy(attname_str, attset_str);
197 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
199 for (i = 0; *s && *s != ')' &&
200 !strchr(FILE_MATCH_BLANK, *s); s++)
201 if (i+1 < sizeof(attname_str))
202 attname_str[i++] = *s;
203 attname_str[i] = '\0';
206 searchRecordKey (zh, reckeys, attname_str, ws, 32);
210 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
211 spec, zh->m_group ? zh->m_group : "none");
216 for (i = 0; i<32; i++)
225 dst += strlen(ws[i]);
229 yaz_log (YLOG_WARN, "Record didn't contain match"
230 " fields in (%s,%s)", attset_str, attname_str);
238 const char *spec_src = NULL;
239 const char *s1 = ++s;
240 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
244 if (spec_len > sizeof(special)-1)
245 spec_len = sizeof(special)-1;
246 memcpy (special, s, spec_len);
247 special[spec_len] = '\0';
250 if (!strcmp (special, "group"))
251 spec_src = zh->m_group;
252 else if (!strcmp (special, "database"))
253 spec_src = zh->basenames[0];
254 else if (!strcmp (special, "filename")) {
257 else if (!strcmp (special, "type"))
258 spec_src = zh->m_record_type;
263 strcpy (dst, spec_src);
264 dst += strlen (spec_src);
267 else if (*s == '\"' || *s == '\'')
269 int stopMarker = *s++;
273 while (*s && *s != stopMarker)
275 if (i+1 < sizeof(tmpString))
276 tmpString[i++] = *s++;
281 strcpy (dst, tmpString);
282 dst += strlen (tmpString);
286 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
287 spec, zh->m_group ? zh->m_group : "none");
294 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
295 fname, zh->m_group ? zh->m_group : "none");
302 struct recordLogInfo {
305 struct recordGroup *rGroup;
308 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
311 for (i = 0; i<256; i++)
313 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
318 ctrl->flagShowRecords = !zh->m_flag_rw;
321 static void all_matches_add(struct recExtractCtrl *ctrl)
324 extract_init(ctrl, &word);
325 word.index_name = "_ALLRECORDS";
326 word.index_type = 'w';
328 extract_add_index_string (&word, zinfo_index_category_alwaysmatches,
332 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
333 struct ZebraRecStream *stream,
334 enum zebra_recctrl_action_t action,
336 const char *recordType,
338 const char *match_criteria,
341 void *recTypeClientData);
344 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
347 ZEBRA_RES r = ZEBRA_OK;
352 struct file_read_info *fi = 0;
353 const char *original_record_type = 0;
355 void *recTypeClientData;
356 struct ZebraRecStream stream, *streamp;
358 zebra_init_log_level();
360 if (!zh->m_group || !*zh->m_group)
363 sprintf (gprefix, "%s.", zh->m_group);
365 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
367 /* determine file extension */
369 for (i = strlen(fname); --i >= 0; )
372 else if (fname[i] == '.')
374 strcpy (ext, fname+i+1);
377 /* determine file type - depending on extension */
378 original_record_type = zh->m_record_type;
379 if (!zh->m_record_type)
381 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
382 zh->m_record_type = res_get (zh->res, ext_res);
384 if (!zh->m_record_type)
387 if (zh->records_processed + zh->records_skipped
388 < zh->m_file_verbose_limit)
389 yaz_log (YLOG_LOG, "? %s", fname);
390 zh->records_skipped++;
393 /* determine match criteria */
394 if (!zh->m_record_id)
396 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
397 zh->m_record_id = res_get (zh->res, ext_res);
401 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
402 &recTypeClientData)))
404 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
408 switch(recType->version)
413 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
415 if (sysno && deleteFlag)
424 if (zh->path_reg && !yaz_is_abspath (fname))
426 strcpy (full_rep, zh->path_reg);
427 strcat (full_rep, "/");
428 strcat (full_rep, fname);
431 strcpy (full_rep, fname);
433 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
435 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
436 zh->m_record_type = original_record_type;
440 zebra_create_stream_fd(streamp, fd, 0);
442 r = zebra_extract_records_stream(zh, streamp,
444 action_delete : action_update,
448 0, /*match_criteria */
450 recType, recTypeClientData);
452 stream.destroy(streamp);
453 zh->m_record_type = original_record_type;
458 If sysno is provided, then it's used to identify the reocord.
459 If not, and match_criteria is provided, then sysno is guessed
460 If not, and a record is provided, then sysno is got from there
464 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
465 const char *buf, size_t buf_size,
466 enum zebra_recctrl_action_t action,
468 const char *recordType,
470 const char *match_criteria,
473 struct ZebraRecStream stream;
478 if (recordType && *recordType)
480 yaz_log(log_level_extract,
481 "Record type explicitly specified: %s", recordType);
482 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
487 if (!(zh->m_record_type))
489 yaz_log (YLOG_WARN, "No such record type defined");
492 yaz_log(log_level_extract, "Get record type from rgroup: %s",
494 recType = recType_byName (zh->reg->recTypes, zh->res,
495 zh->m_record_type, &clientData);
496 recordType = zh->m_record_type;
501 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
505 zebra_create_stream_mem(&stream, buf, buf_size);
507 res = zebra_extract_records_stream(zh, &stream,
514 recType, clientData);
515 stream.destroy(&stream);
519 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
520 struct ZebraRecStream *stream,
521 enum zebra_recctrl_action_t action,
523 const char *recordType,
525 const char *match_criteria,
528 void *recTypeClientData)
530 ZEBRA_RES res = ZEBRA_OK;
534 res = zebra_extract_record_stream(zh, stream,
541 recType, recTypeClientData, &more);
556 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
557 struct ZebraRecStream *stream,
558 enum zebra_recctrl_action_t action,
560 const char *recordType,
562 const char *match_criteria,
565 void *recTypeClientData,
570 RecordAttr *recordAttr;
571 struct recExtractCtrl extractCtrl;
573 const char *matchStr = 0;
575 off_t start_offset = 0, end_offset = 0;
576 const char *pr_fname = fname; /* filename to print .. */
577 int show_progress = zh->records_processed + zh->records_skipped
578 < zh->m_file_verbose_limit ? 1:0;
580 zebra_init_log_level();
583 pr_fname = "<no file>"; /* make it printable if file is omitted */
585 zebra_rec_keys_reset(zh->reg->keys);
586 zebra_rec_keys_reset(zh->reg->sortKeys);
588 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
590 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
591 zh->m_explain_database))
597 off_t null_offset = 0;
598 extractCtrl.stream = stream;
600 start_offset = stream->tellf(stream);
602 extractCtrl.first_record = start_offset ? 0 : 1;
604 stream->endf(stream, &null_offset);;
606 extractCtrl.init = extract_init;
607 extractCtrl.tokenAdd = extract_token_add;
608 extractCtrl.schemaAdd = extract_schema_add;
609 extractCtrl.dh = zh->reg->dh;
610 extractCtrl.handle = zh;
611 extractCtrl.match_criteria[0] = '\0';
612 extractCtrl.staticrank = 0;
613 extractCtrl.action = action;
615 init_extractCtrl(zh, &extractCtrl);
617 extract_set_store_data_prepare(&extractCtrl);
619 r = (*recType->extract)(recTypeClientData, &extractCtrl);
621 if (action == action_update)
623 action = extractCtrl.action;
628 case RECCTRL_EXTRACT_EOF:
630 case RECCTRL_EXTRACT_ERROR_GENERIC:
631 /* error occured during extraction ... */
632 yaz_log (YLOG_WARN, "extract error: generic");
634 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
635 /* error occured during extraction ... */
636 yaz_log (YLOG_WARN, "extract error: no such filter");
638 case RECCTRL_EXTRACT_SKIP:
640 yaz_log (YLOG_LOG, "skip %s %s " ZINT_FORMAT,
641 recordType, pr_fname, (zint) start_offset);
644 end_offset = stream->endf(stream, 0);
646 stream->seekf(stream, end_offset);
649 case RECCTRL_EXTRACT_OK:
652 yaz_log (YLOG_WARN, "extract error: unknown error: %d", r);
655 end_offset = stream->endf(stream, 0);
657 stream->seekf(stream, end_offset);
659 end_offset = stream->tellf(stream);
661 all_matches_add(&extractCtrl);
663 if (extractCtrl.match_criteria[0])
664 match_criteria = extractCtrl.match_criteria;
672 if (match_criteria && *match_criteria) {
673 matchStr = match_criteria;
675 if (zh->m_record_id && *zh->m_record_id) {
676 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
680 yaz_log (YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
681 pr_fname, (zint) start_offset);
688 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
689 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
693 assert(*rinfo == sizeof(*sysno));
694 memcpy (sysno, rinfo+1, sizeof(*sysno));
698 if (zebra_rec_keys_empty(zh->reg->keys))
700 /* the extraction process returned no information - the record
701 is probably empty - unless flagShowRecords is in use */
709 if (action == action_delete)
711 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
712 pr_fname, (zint) start_offset);
713 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
716 else if (action == action_replace)
718 yaz_log (YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
719 pr_fname, (zint) start_offset);
720 yaz_log (YLOG_WARN, "cannot update record above (seems new)");
724 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
725 (zint) start_offset);
726 rec = rec_new (zh->reg->records);
730 recordAttr = rec_init_attr (zh->reg->zei, rec);
731 if (extractCtrl.staticrank < 0)
733 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
734 extractCtrl.staticrank = 0;
739 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
740 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
741 sizeof(*sysno), sysno);
744 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
746 extract_flush_record_keys2(zh, *sysno,
747 zh->reg->keys, extractCtrl.staticrank,
748 0, recordAttr->staticrank);
750 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
751 extractCtrl.staticrank);
753 recordAttr->staticrank = extractCtrl.staticrank;
754 zh->records_inserted++;
758 /* record already exists */
759 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
760 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
761 if (action == action_insert)
763 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
764 recordType, pr_fname, (zint) start_offset);
769 rec = rec_get (zh->reg->records, *sysno);
772 recordAttr = rec_init_attr (zh->reg->zei, rec);
774 /* decrease total size */
775 zebraExplain_recordBytesIncrement (zh->reg->zei,
776 - recordAttr->recordSize);
778 zebra_rec_keys_set_buf(delkeys,
779 rec->info[recInfo_delKeys],
780 rec->size[recInfo_delKeys],
782 zebra_rec_keys_set_buf(sortKeys,
783 rec->info[recInfo_sortKeys],
784 rec->size[recInfo_sortKeys],
787 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
789 extract_flush_record_keys(zh, *sysno, 0, delkeys,
790 recordAttr->staticrank);
792 if (action == action_delete)
794 /* record going to be deleted */
796 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
797 delkeys, recordAttr->staticrank);
799 if (zebra_rec_keys_empty(delkeys))
801 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
802 pr_fname, (zint) start_offset);
803 yaz_log(YLOG_WARN, "cannot delete file above, "
804 "storeKeys false (3)");
809 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
810 pr_fname, (zint) start_offset);
811 zh->records_deleted++;
814 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
815 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
817 rec_del (zh->reg->records, &rec);
819 zebra_rec_keys_close(delkeys);
820 zebra_rec_keys_close(sortKeys);
826 { /* update or special_update */
828 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
829 pr_fname, (zint) start_offset);
830 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
833 extract_flush_record_keys2(zh, *sysno,
834 zh->reg->keys, extractCtrl.staticrank,
835 delkeys, recordAttr->staticrank);
837 extract_flush_record_keys(zh, *sysno, 1,
838 zh->reg->keys, extractCtrl.staticrank);
840 recordAttr->staticrank = extractCtrl.staticrank;
841 zh->records_updated++;
843 zebra_rec_keys_close(delkeys);
844 zebra_rec_keys_close(sortKeys);
846 /* update file type */
847 xfree (rec->info[recInfo_fileType]);
848 rec->info[recInfo_fileType] =
849 rec_strdup (recordType, &rec->size[recInfo_fileType]);
851 /* update filename */
852 xfree (rec->info[recInfo_filename]);
853 rec->info[recInfo_filename] =
854 rec_strdup (fname, &rec->size[recInfo_filename]);
856 /* update delete keys */
857 xfree (rec->info[recInfo_delKeys]);
858 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
860 zebra_rec_keys_get_buf(zh->reg->keys,
861 &rec->info[recInfo_delKeys],
862 &rec->size[recInfo_delKeys]);
866 rec->info[recInfo_delKeys] = NULL;
867 rec->size[recInfo_delKeys] = 0;
869 /* update sort keys */
870 xfree (rec->info[recInfo_sortKeys]);
872 zebra_rec_keys_get_buf(zh->reg->sortKeys,
873 &rec->info[recInfo_sortKeys],
874 &rec->size[recInfo_sortKeys]);
878 recordAttr->recordSize = end_offset - start_offset;
879 zebraExplain_recordBytesIncrement(zh->reg->zei,
880 recordAttr->recordSize);
883 /* set run-number for this record */
884 recordAttr->runNumber =
885 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
887 /* update store data */
888 xfree (rec->info[recInfo_storeData]);
890 /* update store data */
891 if (zh->store_data_buf)
893 rec->size[recInfo_storeData] = zh->store_data_size;
894 rec->info[recInfo_storeData] = zh->store_data_buf;
895 zh->store_data_buf = 0;
896 recordAttr->recordSize = zh->store_data_size;
898 else if (zh->m_store_data)
900 off_t cur_offset = stream->tellf(stream);
902 rec->size[recInfo_storeData] = recordAttr->recordSize;
903 rec->info[recInfo_storeData] = (char *)
904 xmalloc (recordAttr->recordSize);
905 stream->seekf(stream, start_offset);
906 stream->readf(stream, rec->info[recInfo_storeData],
907 recordAttr->recordSize);
908 stream->seekf(stream, cur_offset);
912 rec->info[recInfo_storeData] = NULL;
913 rec->size[recInfo_storeData] = 0;
915 /* update database name */
916 xfree (rec->info[recInfo_databaseName]);
917 rec->info[recInfo_databaseName] =
918 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
921 recordAttr->recordOffset = start_offset;
923 /* commit this record */
924 rec_put (zh->reg->records, &rec);
929 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
931 ZebraHandle zh = (ZebraHandle) handle;
932 struct recExtractCtrl extractCtrl;
934 if (zebraExplain_curDatabase (zh->reg->zei,
935 rec->info[recInfo_databaseName]))
938 if (zebraExplain_newDatabase (zh->reg->zei,
939 rec->info[recInfo_databaseName], 0))
943 zebra_rec_keys_reset(zh->reg->keys);
944 zebra_rec_keys_reset(zh->reg->sortKeys);
946 extractCtrl.init = extract_init;
947 extractCtrl.tokenAdd = extract_token_add;
948 extractCtrl.schemaAdd = extract_schema_add;
949 extractCtrl.dh = zh->reg->dh;
951 init_extractCtrl(zh, &extractCtrl);
953 extractCtrl.flagShowRecords = 0;
954 extractCtrl.match_criteria[0] = '\0';
955 extractCtrl.staticrank = 0;
956 extractCtrl.action = action_update;
958 extractCtrl.handle = handle;
959 extractCtrl.first_record = 1;
961 extract_set_store_data_prepare(&extractCtrl);
964 grs_extract_tree(&extractCtrl, n);
966 if (rec->size[recInfo_delKeys])
968 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
970 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
972 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
973 rec->size[recInfo_delKeys],
976 extract_flush_record_keys2(zh, rec->sysno,
977 zh->reg->keys, 0, delkeys, 0);
979 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
980 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
982 zebra_rec_keys_close(delkeys);
984 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
985 rec->size[recInfo_sortKeys],
988 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
989 zebra_rec_keys_close(sortkeys);
994 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
996 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
999 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1001 xfree (rec->info[recInfo_delKeys]);
1002 zebra_rec_keys_get_buf(zh->reg->keys,
1003 &rec->info[recInfo_delKeys],
1004 &rec->size[recInfo_delKeys]);
1006 xfree (rec->info[recInfo_sortKeys]);
1007 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1008 &rec->info[recInfo_sortKeys],
1009 &rec->size[recInfo_sortKeys]);
1013 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1014 zebra_rec_keys_t reckeys,
1017 if (zebra_rec_keys_rewind(reckeys))
1022 NMEM nmem = nmem_create();
1024 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1026 char keystr[200]; /* room for zints to print */
1028 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1030 const char *string_index;
1032 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1033 0/* db */, &string_index);
1035 zebra_term_untrans_iconv(zh, nmem, index_type,
1038 for (i = 0; i<key.len; i++)
1040 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1043 if (*str < CHR_BASE_CHAR)
1046 char dst_buf[200]; /* room for special chars */
1048 strcpy(dst_buf , "?");
1050 if (!strcmp(str, ""))
1051 strcpy(dst_buf, "alwaysmatches");
1052 if (!strcmp(str, FIRST_IN_FIELD_STR))
1053 strcpy(dst_buf, "firstinfield");
1054 else if (!strcmp(str, CHR_UNKNOWN))
1055 strcpy(dst_buf, "unknown");
1056 else if (!strcmp(str, CHR_SPACE))
1057 strcpy(dst_buf, "space");
1059 for (i = 0; i<slen; i++)
1061 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1063 yaz_log(level, "%s%c %s %s", keystr, index_type,
1064 string_index, dst_buf);
1068 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
1069 string_index, dst_term);
1077 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1078 zebra_rec_keys_t reckeys)
1080 ZebraExplainInfo zei = zh->reg->zei;
1084 struct ord_stat *next;
1087 if (zebra_rec_keys_rewind(reckeys))
1089 struct ord_stat *ord_list = 0;
1093 struct it_key key_in;
1094 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1096 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1098 for (p = ord_list; p ; p = p->next)
1106 p = xmalloc(sizeof(*p));
1117 struct ord_stat *p1 = p;
1120 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1122 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1129 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1130 zebra_rec_keys_t ins_keys, zint ins_rank,
1131 zebra_rec_keys_t del_keys, zint del_rank)
1133 ZebraExplainInfo zei = zh->reg->zei;
1137 if (!zh->reg->key_block)
1139 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1140 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1141 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1142 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1147 extract_rec_keys_adjust(zh, 1, ins_keys);
1149 zebraExplain_recordCountIncrement (zei, 1);
1150 zebra_rec_keys_rewind(ins_keys);
1154 extract_rec_keys_adjust(zh, 0, del_keys);
1156 zebraExplain_recordCountIncrement (zei, -1);
1157 zebra_rec_keys_rewind(del_keys);
1163 const char *del_str;
1164 struct it_key del_key_in;
1168 const char *ins_str;
1169 struct it_key ins_key_in;
1173 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1176 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1179 if (del && ins && ins_rank == del_rank
1180 && !key_compare(&del_key_in, &ins_key_in)
1181 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1191 key_block_write(zh->reg->key_block, sysno,
1192 &del_key_in, 0, del_str, del_slen,
1193 del_rank, zh->m_staticrank);
1195 key_block_write(zh->reg->key_block, sysno,
1196 &ins_key_in, 1, ins_str, ins_slen,
1197 ins_rank, zh->m_staticrank);
1199 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1202 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1203 zebra_rec_keys_t reckeys,
1206 ZebraExplainInfo zei = zh->reg->zei;
1208 extract_rec_keys_adjust(zh, cmd, reckeys);
1210 if (log_level_details)
1212 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1213 sysno, cmd ? "insert" : "delete");
1214 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1217 if (!zh->reg->key_block)
1219 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1220 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1221 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1222 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1224 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1227 yaz_log(YLOG_LOG, "sysno=" ZINT_FORMAT " cmd=%d", sysno, cmd);
1228 print_rec_keys(zh, reckeys);
1230 if (zebra_rec_keys_rewind(reckeys))
1234 struct it_key key_in;
1235 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1237 key_block_write(zh->reg->key_block, sysno,
1238 &key_in, cmd, str, slen,
1239 staticrank, zh->m_staticrank);
1244 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1245 zebra_rec_keys_t reckeys,
1246 zebra_snippets *snippets)
1248 NMEM nmem = nmem_create();
1249 if (zebra_rec_keys_rewind(reckeys))
1254 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1261 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1262 seqno = key.mem[key.len-1];
1263 ord = CAST_ZINT_TO_INT(key.mem[0]);
1265 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1266 0/* db */, 0 /* string_index */);
1268 zebra_term_untrans_iconv(zh, nmem, index_type,
1270 zebra_snippets_append(snippets, seqno, ord, dst_term);
1278 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1280 yaz_log(YLOG_LOG, "print_rec_keys");
1281 if (zebra_rec_keys_rewind(reckeys))
1286 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1288 char dst_buf[IT_MAX_WORD];
1291 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1293 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1295 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1297 seqno = key.mem[key.len-1];
1299 zebra_term_untrans(zh, index_type, dst_buf, str);
1301 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1302 " term=%s", ord, seqno, dst_buf);
1307 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1308 const char *str, int length)
1311 ZebraHandle zh = p->extractCtrl->handle;
1312 ZebraExplainInfo zei = zh->reg->zei;
1315 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1317 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1321 key.mem[i++] = p->record_id;
1322 key.mem[i++] = p->section_id;
1324 if (zh->m_segment_indexing)
1325 key.mem[i++] = p->segment;
1326 key.mem[i++] = p->seqno;
1329 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1332 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1335 ZebraHandle zh = p->extractCtrl->handle;
1336 ZebraExplainInfo zei = zh->reg->zei;
1338 zinfo_index_category_t cat = zinfo_index_category_sort;
1340 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1342 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1345 key.mem[1] = p->record_id;
1347 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1350 static void extract_add_staticrank_string(RecWord *p,
1351 const char *str, int length)
1354 struct recExtractCtrl *ctrl = p->extractCtrl;
1356 if (length > sizeof(valz)-1)
1357 length = sizeof(valz)-1;
1359 memcpy(valz, str, length);
1360 valz[length] = '\0';
1361 ctrl->staticrank = atozint(valz);
1364 static void extract_add_string(RecWord *p, const char *string, int length)
1366 ZebraHandle zh = p->extractCtrl->handle;
1367 assert (length > 0);
1372 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1374 extract_add_index_string(p, zinfo_index_category_index,
1376 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1379 memcpy(&word, p, sizeof(word));
1382 extract_add_index_string(
1383 &word, zinfo_index_category_alwaysmatches, "", 0);
1386 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1388 extract_add_sort_string(p, string, length);
1390 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1392 extract_add_staticrank_string(p, string, length);
1396 static void extract_add_incomplete_field(RecWord *p)
1398 ZebraHandle zh = p->extractCtrl->handle;
1399 const char *b = p->term_buf;
1400 int remain = p->term_len;
1402 const char **map = 0;
1405 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1409 char buf[IT_MAX_WORD+1];
1413 while (map && *map && **map == *CHR_SPACE)
1415 remain = p->term_len - (b - p->term_buf);
1417 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1425 while (map && *map && **map != *CHR_SPACE)
1427 const char *cp = *map;
1429 while (i < IT_MAX_WORD && *cp)
1431 remain = p->term_len - (b - p->term_buf);
1433 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1443 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1445 /* first in field marker */
1446 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1450 extract_add_string (p, buf, i);
1455 static void extract_add_complete_field (RecWord *p)
1457 ZebraHandle zh = p->extractCtrl->handle;
1458 const char *b = p->term_buf;
1459 char buf[IT_MAX_WORD+1];
1460 const char **map = 0;
1461 int i = 0, remain = p->term_len;
1464 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1466 while (remain > 0 && i < IT_MAX_WORD)
1468 while (map && *map && **map == *CHR_SPACE)
1470 remain = p->term_len - (b - p->term_buf);
1474 int first = i ? 0 : 1; /* first position */
1475 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1483 if (i && i < IT_MAX_WORD)
1484 buf[i++] = *CHR_SPACE;
1485 while (map && *map && **map != *CHR_SPACE)
1487 const char *cp = *map;
1489 if (**map == *CHR_CUT)
1495 if (i >= IT_MAX_WORD)
1497 while (i < IT_MAX_WORD && *cp)
1500 remain = p->term_len - (b - p->term_buf);
1503 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1512 extract_add_string (p, buf, i);
1515 static void extract_token_add(RecWord *p)
1517 ZebraHandle zh = p->extractCtrl->handle;
1520 if (log_level_details)
1522 yaz_log(log_level_details, "extract_token_add "
1523 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1524 p->index_type, p->index_name,
1525 p->seqno, p->term_len, p->term_buf);
1527 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1528 p->term_buf, p->term_len)))
1530 p->term_buf = wrbuf_buf(wrbuf);
1531 p->term_len = wrbuf_len(wrbuf);
1533 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1534 extract_add_complete_field (p);
1536 extract_add_incomplete_field(p);
1539 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1540 void *buf, size_t sz)
1542 ZebraHandle zh = (ZebraHandle) p->handle;
1544 xfree(zh->store_data_buf);
1545 zh->store_data_buf = 0;
1546 zh->store_data_size = 0;
1549 zh->store_data_buf = xmalloc(sz);
1550 zh->store_data_size = sz;
1551 memcpy(zh->store_data_buf, buf, sz);
1555 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1557 ZebraHandle zh = (ZebraHandle) p->handle;
1558 xfree(zh->store_data_buf);
1559 zh->store_data_buf = 0;
1560 zh->store_data_size = 0;
1561 p->setStoreData = extract_set_store_data_cb;
1564 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1566 ZebraHandle zh = (ZebraHandle) p->handle;
1567 zebraExplain_addSchema (zh->reg->zei, oid);
1570 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1571 int cmd, zebra_rec_keys_t reckeys)
1574 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1576 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1579 if (zebra_rec_keys_rewind(reckeys))
1581 zebra_sort_index_t si = zh->reg->sort_index;
1584 struct it_key key_in;
1586 zebra_sort_sysno(si, sysno);
1588 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1590 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1592 zebra_sort_type(si, ord);
1594 zebra_sort_add(si, str, slen);
1596 zebra_sort_delete(si);
1604 * indent-tabs-mode: nil
1606 * vim: shiftwidth=4 tabstop=8 expandtab