+ SYSNO sysno0 = 0;
+ RecordAttr *recordAttr;
+ struct recExtractCtrl extractCtrl;
+ int r;
+ const char *matchStr = 0;
+ Record rec;
+ off_t start_offset = 0;
+ const char *pr_fname = fname; /* filename to print .. */
+ int show_progress = zh->records_processed < 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;
+
+
+ init_extractCtrl(zh, &extractCtrl);
+
+ extract_set_store_data_prepare(&extractCtrl);
+
+ r = (*recType->extract)(recTypeClientData, &extractCtrl);
+
+ if (r == RECCTRL_EXTRACT_EOF)
+ return ZEBRA_FAIL;
+ else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
+ {
+ /* error occured during extraction ... */
+ yaz_log (YLOG_WARN, "extract error: generic");
+ return ZEBRA_FAIL;
+ }
+ else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
+ {
+ /* error occured during extraction ... */
+ yaz_log (YLOG_WARN, "extract error: no such filter");
+ return ZEBRA_FAIL;
+ }
+
+ all_matches_add(&extractCtrl);
+
+ if (extractCtrl.match_criteria[0])
+ match_criteria = extractCtrl.match_criteria;
+ }
+ if (!sysno) {
+
+ sysno = &sysno0;
+
+ if (match_criteria && *match_criteria) {
+ matchStr = match_criteria;
+ } else {
+ if (zh->m_record_id && *zh->m_record_id) {
+ matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
+ zh->m_record_id);
+ if (!matchStr)
+ {
+ yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
+ 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 (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 (delete_flag)
+ {
+ 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;
+ }
+ 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);
+ recordAttr->staticrank = extractCtrl.staticrank;
+
+ 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_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
+ extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
+ recordAttr->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 (!allow_update)
+ {
+ 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);
+
+ 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_flushSortKeys (zh, *sysno, 0, sortKeys);
+ extract_flushRecordKeys (zh, *sysno, 0, delkeys,
+ recordAttr->staticrank);
+ if (delete_flag)
+ {
+ /* record going to be deleted */
+ 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);
+ }
+ rec_rm (&rec);
+ logRecord(zh);
+ return ZEBRA_OK;
+ }
+ else
+ {
+ if (show_progress)
+ yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
+ pr_fname, (zint) ZINT_FORMAT);
+ recordAttr->staticrank = extractCtrl.staticrank;
+ extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
+ extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
+ recordAttr->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]);
+
+ /* save file size of original record */
+ zebraExplain_recordBytesIncrement (zh->reg->zei,
+ - recordAttr->recordSize);
+ if (stream)
+ {
+ off_t end_offset = stream->endf(stream, 0);
+
+ if (!end_offset)
+ end_offset = stream->tellf(stream);
+ else
+ stream->seekf(stream, end_offset);
+
+ 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;
+ }
+ else if (zh->m_store_data)
+ {
+ off_t cur_offset = stream->tellf(stream);