- RecordAttr *recordAttr;
- int r;
- const char *matchStr = 0;
- SYSNO sysnotmp;
- Record rec;
- off_t recordOffset = 0;
- struct recExtractCtrl extractCtrl;
-
- /* announce database */
- if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
- {
- if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
- zh->m_explain_database))
- return 0;
- }
-
- if (fi->fd != -1)
- {
- /* we are going to read from a file, so prepare the extraction */
- zebra_rec_keys_reset(zh->reg->keys);
-
-#if NATTR
- zebra_rec_keys_reset(zh->reg->sortKeys);
-#else
- zh->reg->sortKeys.buf_used = 0;
-#endif
- recordOffset = fi->file_moffset;
- extractCtrl.handle = zh;
- extractCtrl.offset = fi->file_moffset;
- extractCtrl.readf = file_read;
- extractCtrl.seekf = file_seek;
- extractCtrl.tellf = file_tell;
- extractCtrl.endf = file_end;
- extractCtrl.fh = fi;
- extractCtrl.init = extract_init;
- extractCtrl.tokenAdd = extract_token_add;
- extractCtrl.schemaAdd = extract_schema_add;
- extractCtrl.dh = zh->reg->dh;
- extractCtrl.match_criteria[0] = '\0';
- extractCtrl.staticrank = 0;
-
- extractCtrl.first_record = fi->file_offset ? 0 : 1;
-
- extract_set_store_data_prepare(&extractCtrl);
-
- init_extractCtrl(zh, &extractCtrl);
-
- if (!zh->m_flag_rw)
- printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
- if (zh->m_flag_rw)
- {
- char msg[512];
- sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
- yaz_log_init_prefix2 (msg);
- }
-
- r = (*recType->extract)(recTypeClientData, &extractCtrl);
-
- yaz_log_init_prefix2 (0);
- if (r == RECCTRL_EXTRACT_EOF)
- return 0;
- else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
- {
- /* error occured during extraction ... */
- if (zh->m_flag_rw &&
- zh->records_processed < zh->m_file_verbose_limit)
- {
- yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
- fname, recordOffset);
- }
- return 0;
- }
- else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
- {
- /* error occured during extraction ... */
- if (zh->m_flag_rw &&
- zh->records_processed < zh->m_file_verbose_limit)
- {
- yaz_log (YLOG_WARN, "no filter for %s %s "
- PRINTF_OFF_T, zh->m_record_type,
- fname, recordOffset);
- }
- return 0;
- }
- if (extractCtrl.match_criteria[0])
- matchStr = extractCtrl.match_criteria;
- }
-
- /* perform match if sysno not known and if match criteria is specified */
- if (!sysno)
- {
- sysnotmp = 0;
- sysno = &sysnotmp;
-
- if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
- {
- matchStr = fileMatchStr (zh, zh->reg->keys, fname,
- zh->m_record_id);
- if (!matchStr)
- {
- yaz_log(YLOG_WARN, "Bad match criteria");
- return 0;
- }
- }
- if (matchStr)
- {
- char *rinfo = dict_lookup (zh->reg->matchDict, matchStr);
- if (rinfo)
- {
- assert(*rinfo == sizeof(*sysno));
- memcpy (sysno, rinfo+1, sizeof(*sysno));
- }
- }
- }
- if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
- {
- /* the extraction process returned no information - the record
- is probably empty - unless flagShowRecords is in use */
- if (!zh->m_flag_rw)
- return 1;
-
- if (zh->records_processed < zh->m_file_verbose_limit)
- yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
- fname, recordOffset);
- return 1;
- }
-
- if (! *sysno)
- {
- /* new record */
- if (deleteFlag)
- {
- yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
- fname, recordOffset);
- yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
- return 1;
- }
- if (zh->records_processed < zh->m_file_verbose_limit)
- yaz_log (YLOG_LOG, "add %s %s " PRINTF_OFF_T, zh->m_record_type,
- fname, recordOffset);
- rec = rec_new (zh->reg->records);
-
- *sysno = rec->sysno;
-
- recordAttr = rec_init_attr (zh->reg->zei, rec);
- recordAttr->staticrank = extractCtrl.staticrank;
-
- if (matchStr)
- {
- dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
- }
-#if NATTR
- extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
-#else
- extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
-#endif
- 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();
-
-#if NATTR
- zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
-#else
- struct sortKeys sortKeys;
-#endif
-
- 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);
-
-#if NATTR
- zebra_rec_keys_set_buf(sortKeys,
- rec->info[recInfo_sortKeys],
- rec->size[recInfo_sortKeys],
- 0);
- extract_flushSortKeys (zh, *sysno, 0, sortKeys);
-#else
- sortKeys.buf_used = rec->size[recInfo_sortKeys];
- sortKeys.buf = rec->info[recInfo_sortKeys];
- extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
-#endif
-
- extract_flushRecordKeys (zh, *sysno, 0, delkeys,
- recordAttr->staticrank); /* old values */
- if (deleteFlag)
- {
- /* record going to be deleted */
- if (zebra_rec_keys_empty(delkeys))
- {
- yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T,
- zh->m_record_type, fname, recordOffset);
- yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
- }
- else
- {
- if (zh->records_processed < zh->m_file_verbose_limit)
- yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T,
- zh->m_record_type, fname, recordOffset);
- zh->records_deleted++;
- if (matchStr)
- dict_delete (zh->reg->matchDict, matchStr);
- rec_del (zh->reg->records, &rec);
- }
- rec_rm (&rec);
- logRecord (zh);
- return 1;
- }
- else
- {
- /* flush new keys for sort&search etc */
- if (zh->records_processed < zh->m_file_verbose_limit)
- yaz_log (YLOG_LOG, "update %s %s " PRINTF_OFF_T,
- zh->m_record_type, fname, recordOffset);
- recordAttr->staticrank = extractCtrl.staticrank;
-#if NATTR
- extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
-#else
- extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
-#endif
- extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
- recordAttr->staticrank);
- zh->records_updated++;
- }
- zebra_rec_keys_close(delkeys);
-#if NATTR
- zebra_rec_keys_close(sortKeys);
-#endif
- }
- /* update file type */
- xfree (rec->info[recInfo_fileType]);
- rec->info[recInfo_fileType] =
- rec_strdup (zh->m_record_type, &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]);
-
-#if NATTR
- zebra_rec_keys_get_buf(zh->reg->sortKeys,
- &rec->info[recInfo_sortKeys],
- &rec->size[recInfo_sortKeys]);
-#else
- rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
- rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
- zh->reg->sortKeys.buf = NULL;
- zh->reg->sortKeys.buf_max = 0;
-#endif
-
- /* save file size of original record */
- zebraExplain_recordBytesIncrement (zh->reg->zei,
- - recordAttr->recordSize);
- recordAttr->recordSize = fi->file_moffset - recordOffset;
- if (!recordAttr->recordSize)
- recordAttr->recordSize = fi->file_max - recordOffset;
- 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]);
- 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)
- {
- rec->size[recInfo_storeData] = recordAttr->recordSize;
- rec->info[recInfo_storeData] = (char *)
- xmalloc (recordAttr->recordSize);
- if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
- {
- yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
- recordOffset, fname);
- exit (1);
- }
- if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
- < recordAttr->recordSize)
- {
- yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
- recordAttr->recordSize, fname);
- exit (1);
- }
- }
- else
- {
- rec->info[recInfo_storeData] = NULL;
- rec->size[recInfo_storeData] = 0;
- }
- /* update database name */
- xfree (rec->info[recInfo_databaseName]);
- rec->info[recInfo_databaseName] =
- rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
-
- /* update offset */
- recordAttr->recordOffset = recordOffset;
-
- /* commit this record */
- rec_put (zh->reg->records, &rec);
- logRecord (zh);
- return 1;