+ yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
+ zh->m_record_type = original_record_type;
+ return ZEBRA_FAIL;
+ }
+ streamp = &stream;
+ zebra_create_stream_fd(streamp, fd, 0);
+ }
+ r = zebra_extract_records_stream(zh, streamp,
+ deleteFlag ?
+ action_delete : action_update,
+ 0, /* tst_mode */
+ zh->m_record_type,
+ sysno,
+ 0, /*match_criteria */
+ fname,
+ recType, recTypeClientData);
+ if (streamp)
+ stream.destroy(streamp);
+ zh->m_record_type = original_record_type;
+ return r;
+}
+
+/*
+ If sysno is provided, then it's used to identify the reocord.
+ If not, and match_criteria is provided, then sysno is guessed
+ If not, and a record is provided, then sysno is got from there
+
+ */
+
+ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
+ const char *buf, size_t buf_size,
+ enum zebra_recctrl_action_t action,
+ int test_mode,
+ const char *recordType,
+ zint *sysno,
+ const char *match_criteria,
+ const char *fname)
+{
+ struct ZebraRecStream stream;
+ ZEBRA_RES res;
+ void *clientData;
+ RecType recType = 0;
+
+ if (recordType && *recordType)
+ {
+ yaz_log(log_level_extract,
+ "Record type explicitly specified: %s", recordType);
+ recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
+ &clientData);
+ }
+ else
+ {
+ if (!(zh->m_record_type))
+ {
+ yaz_log (YLOG_WARN, "No such record type defined");
+ return ZEBRA_FAIL;
+ }
+ 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;
+ 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 (1)
+ {
+ WRBUF w = wrbuf_hex_str(matchStr);
+ yaz_log(YLOG_LOG, "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)");
+ }