+ yaz_log(log_level_extract, "Get record type from rgroup: %s",
+ zh->m_record_type);
+ recType = recType_byName(zh->reg->recTypes, zh->res,
+ zh->m_record_type, &clientData);
+ recordType = zh->m_record_type;
+ }
+
+ if (!recType)
+ {
+ yaz_log(YLOG_WARN, "No such record type: %s", recordType);
+ return ZEBRA_FAIL;
+ }
+
+ zebra_create_stream_mem(&stream, buf, buf_size);
+
+ res = zebra_extract_records_stream(zh, &stream,
+ action,
+ test_mode,
+ recordType,
+ sysno,
+ match_criteria,
+ fname,
+ recType, clientData);
+ stream.destroy(&stream);
+ return res;
+}
+
+ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
+ struct ZebraRecStream *stream,
+ enum zebra_recctrl_action_t action,
+ int test_mode,
+ const char *recordType,
+ zint *sysno,
+ const char *match_criteria,
+ const char *fname,
+ RecType recType,
+ void *recTypeClientData)
+{
+ ZEBRA_RES res = ZEBRA_OK;
+ while (1)
+ {
+ int more = 0;
+ res = zebra_extract_record_stream(zh, stream,
+ action,
+ test_mode,
+ recordType,
+ sysno,
+ match_criteria,
+ fname,
+ recType, recTypeClientData, &more);
+ if (!more)
+ {
+ res = ZEBRA_OK;
+ break;
+ }
+ if (res != ZEBRA_OK)
+ break;
+ if (sysno)
+ break;
+ }
+ return res;
+}
+
+
+static WRBUF wrbuf_hex_str(const char *cstr)
+{
+ size_t i;
+ WRBUF w = wrbuf_alloc();
+ for (i = 0; cstr[i]; i++)
+ {
+ if (cstr[i] < ' ' || cstr[i] > 126)
+ wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
+ else
+ wrbuf_putc(w, cstr[i]);
+ }
+ return w;
+}
+
+ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
+ struct ZebraRecStream *stream,
+ enum zebra_recctrl_action_t action,
+ int test_mode,
+ const char *recordType,
+ zint *sysno,
+ const char *match_criteria,
+ const char *fname,
+ RecType recType,
+ void *recTypeClientData,
+ int *more)
+
+{
+ zint sysno0 = 0;
+ RecordAttr *recordAttr;
+ struct recExtractCtrl extractCtrl;
+ int r;
+ const char *matchStr = 0;
+ Record rec;
+ off_t start_offset = 0, end_offset = 0;
+ const char *pr_fname = fname; /* filename to print .. */
+ int show_progress = zh->records_processed + zh->records_skipped
+ < zh->m_file_verbose_limit ? 1:0;
+
+ zebra_init_log_level();
+
+ if (!pr_fname)
+ pr_fname = "<no file>"; /* make it printable if file is omitted */
+
+ zebra_rec_keys_reset(zh->reg->keys);
+ zebra_rec_keys_reset(zh->reg->sortKeys);
+
+ if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
+ {
+ if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
+ zh->m_explain_database))
+ return ZEBRA_FAIL;
+ }
+
+ if (stream)
+ {
+ off_t null_offset = 0;
+ extractCtrl.stream = stream;
+
+ start_offset = stream->tellf(stream);
+
+ extractCtrl.first_record = start_offset ? 0 : 1;
+
+ stream->endf(stream, &null_offset);;
+
+ extractCtrl.init = extract_init;
+ if (zh->reg->index_types)
+ {
+ extractCtrl.tokenAdd = extract_token_add2;
+ }
+ else
+ {
+ extractCtrl.tokenAdd = extract_token_add;
+ }
+ extractCtrl.schemaAdd = extract_schema_add;
+ extractCtrl.dh = zh->reg->dh;
+ extractCtrl.handle = zh;
+ extractCtrl.match_criteria[0] = '\0';
+ extractCtrl.staticrank = 0;
+ extractCtrl.action = action;
+
+ init_extractCtrl(zh, &extractCtrl);
+
+ extract_set_store_data_prepare(&extractCtrl);
+
+ r = (*recType->extract)(recTypeClientData, &extractCtrl);
+
+ if (action == action_update)
+ {
+ action = extractCtrl.action;
+ }
+
+ switch (r)
+ {
+ case RECCTRL_EXTRACT_EOF:
+ return ZEBRA_FAIL;
+ case RECCTRL_EXTRACT_ERROR_GENERIC:
+ /* error occured during extraction ... */
+ yaz_log(YLOG_WARN, "extract error: generic");
+ return ZEBRA_FAIL;
+ case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
+ /* error occured during extraction ... */
+ yaz_log(YLOG_WARN, "extract error: no such filter");
+ return ZEBRA_FAIL;
+ case RECCTRL_EXTRACT_SKIP:
+ if (show_progress)
+ yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
+ recordType, pr_fname, (zint) start_offset);
+ *more = 1;
+
+ end_offset = stream->endf(stream, 0);
+ if (end_offset)
+ stream->seekf(stream, end_offset);
+
+ return ZEBRA_OK;
+ case RECCTRL_EXTRACT_OK:
+ break;
+ default:
+ yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
+ return ZEBRA_FAIL;
+ }
+ end_offset = stream->endf(stream, 0);
+ if (end_offset)
+ stream->seekf(stream, end_offset);
+ else
+ end_offset = stream->tellf(stream);
+
+ all_matches_add(&extractCtrl);
+
+ if (extractCtrl.match_criteria[0])
+ match_criteria = extractCtrl.match_criteria;
+ }
+
+ *more = 1;
+ if (!sysno)
+ {
+ sysno = &sysno0;
+
+ if (match_criteria && *match_criteria) {
+ matchStr = match_criteria;
+ } else {
+ if (zh->m_record_id && *zh->m_record_id) {
+ matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
+ zh->m_record_id);
+ if (!matchStr)
+ {
+ yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
+ pr_fname, (zint) start_offset);
+ return ZEBRA_FAIL;
+ }
+ }
+ }
+ if (matchStr)
+ {
+ int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
+ char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
+ matchStr);
+
+
+ if (log_level_extract)
+ {
+ WRBUF w = wrbuf_hex_str(matchStr);
+ yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
+ wrbuf_destroy(w);
+ }
+ if (rinfo)
+ {
+ assert(*rinfo == sizeof(*sysno));
+ memcpy(sysno, rinfo+1, sizeof(*sysno));
+ }
+ }
+ }
+ if (zebra_rec_keys_empty(zh->reg->keys))
+ {
+ /* the extraction process returned no information - the record
+ is probably empty - unless flagShowRecords is in use */
+ if (test_mode)
+ return ZEBRA_OK;
+ }
+
+ if (! *sysno)
+ {
+ /* new record */
+ if (action == action_delete)
+ {
+ yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
+ pr_fname, (zint) start_offset);
+ yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
+ return ZEBRA_FAIL;
+ }
+ else if (action == action_replace)
+ {
+ yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
+ pr_fname, (zint) start_offset);
+ yaz_log(YLOG_WARN, "cannot update record above (seems new)");
+ return ZEBRA_FAIL;
+ }
+ if (show_progress)
+ yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
+ (zint) start_offset);
+ rec = rec_new(zh->reg->records);
+
+ *sysno = rec->sysno;
+
+ recordAttr = rec_init_attr(zh->reg->zei, rec);
+ if (extractCtrl.staticrank < 0)
+ {
+ yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
+ extractCtrl.staticrank = 0;
+ }
+
+ if (matchStr)
+ {
+ int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
+ dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
+ sizeof(*sysno), sysno);
+ }
+
+ extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
+#if FLUSH2
+ extract_flush_record_keys2(zh, *sysno,
+ zh->reg->keys, extractCtrl.staticrank,
+ 0, recordAttr->staticrank);
+#else
+ extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
+ extractCtrl.staticrank);
+#endif
+ recordAttr->staticrank = extractCtrl.staticrank;
+ zh->records_inserted++;
+ }
+ else
+ {
+ /* record already exists */
+ zebra_rec_keys_t delkeys = zebra_rec_keys_open();
+ zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
+ if (action == action_insert)
+ {
+ yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
+ recordType, pr_fname, (zint) start_offset);
+ logRecord(zh);
+ return ZEBRA_FAIL;
+ }
+
+ rec = rec_get(zh->reg->records, *sysno);
+ assert(rec);
+
+ recordAttr = rec_init_attr(zh->reg->zei, rec);
+
+ /* decrease total size */
+ zebraExplain_recordBytesIncrement(zh->reg->zei,
+ - recordAttr->recordSize);
+
+ zebra_rec_keys_set_buf(delkeys,
+ rec->info[recInfo_delKeys],
+ rec->size[recInfo_delKeys],
+ 0);
+ zebra_rec_keys_set_buf(sortKeys,
+ rec->info[recInfo_sortKeys],
+ rec->size[recInfo_sortKeys],
+ 0);
+
+ extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
+#if !FLUSH2
+ extract_flush_record_keys(zh, *sysno, 0, delkeys,
+ recordAttr->staticrank);
+#endif
+ if (action == action_delete)
+ {
+ /* record going to be deleted */
+#if FLUSH2
+ extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
+ delkeys, recordAttr->staticrank);
+#endif
+ if (zebra_rec_keys_empty(delkeys))
+ {
+ yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
+ pr_fname, (zint) start_offset);
+ yaz_log(YLOG_WARN, "cannot delete file above, "
+ "storeKeys false (3)");
+ }
+ else
+ {
+ if (show_progress)
+ yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
+ pr_fname, (zint) start_offset);
+ zh->records_deleted++;
+ if (matchStr)
+ {
+ int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
+ dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
+ }
+ rec_del(zh->reg->records, &rec);
+ }
+ zebra_rec_keys_close(delkeys);
+ zebra_rec_keys_close(sortKeys);
+ rec_free(&rec);
+ logRecord(zh);
+ return ZEBRA_OK;
+ }
+ else
+ { /* update or special_update */
+ if (show_progress)
+ yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
+ pr_fname, (zint) start_offset);
+ extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
+
+#if FLUSH2
+ extract_flush_record_keys2(zh, *sysno,
+ zh->reg->keys, extractCtrl.staticrank,
+ delkeys, recordAttr->staticrank);
+#else
+ extract_flush_record_keys(zh, *sysno, 1,
+ zh->reg->keys, extractCtrl.staticrank);
+#endif
+ recordAttr->staticrank = extractCtrl.staticrank;
+ zh->records_updated++;
+ }
+ zebra_rec_keys_close(delkeys);
+ zebra_rec_keys_close(sortKeys);
+ }
+ /* update file type */
+ xfree(rec->info[recInfo_fileType]);
+ rec->info[recInfo_fileType] =
+ rec_strdup(recordType, &rec->size[recInfo_fileType]);
+
+ /* update filename */
+ xfree(rec->info[recInfo_filename]);
+ rec->info[recInfo_filename] =
+ rec_strdup(fname, &rec->size[recInfo_filename]);
+
+ /* update delete keys */
+ xfree(rec->info[recInfo_delKeys]);
+ if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
+ {
+ zebra_rec_keys_get_buf(zh->reg->keys,
+ &rec->info[recInfo_delKeys],
+ &rec->size[recInfo_delKeys]);
+ }
+ else
+ {
+ rec->info[recInfo_delKeys] = NULL;
+ rec->size[recInfo_delKeys] = 0;
+ }
+ /* update sort keys */
+ xfree(rec->info[recInfo_sortKeys]);
+
+ zebra_rec_keys_get_buf(zh->reg->sortKeys,
+ &rec->info[recInfo_sortKeys],
+ &rec->size[recInfo_sortKeys]);
+
+ if (stream)
+ {
+ recordAttr->recordSize = end_offset - start_offset;
+ zebraExplain_recordBytesIncrement(zh->reg->zei,
+ recordAttr->recordSize);
+ }
+
+ /* set run-number for this record */
+ recordAttr->runNumber =
+ zebraExplain_runNumberIncrement(zh->reg->zei, 0);
+
+ /* update store data */
+ xfree(rec->info[recInfo_storeData]);
+
+ /* update store data */
+ if (zh->store_data_buf)
+ {
+ rec->size[recInfo_storeData] = zh->store_data_size;
+ rec->info[recInfo_storeData] = zh->store_data_buf;
+ zh->store_data_buf = 0;
+ recordAttr->recordSize = zh->store_data_size;
+ }
+ else if (zh->m_store_data)
+ {
+ off_t cur_offset = stream->tellf(stream);
+
+ rec->size[recInfo_storeData] = recordAttr->recordSize;
+ rec->info[recInfo_storeData] = (char *)
+ xmalloc(recordAttr->recordSize);
+ stream->seekf(stream, start_offset);
+ stream->readf(stream, rec->info[recInfo_storeData],
+ recordAttr->recordSize);
+ stream->seekf(stream, cur_offset);