+ (zh->trans_no++);
+ if (zh->trans_no != 1)
+ {
+ return;
+ }
+
+ yaz_log (LOG_LOG, "zebra_begin_trans");
+
+ zh->records_inserted = 0;
+ zh->records_updated = 0;
+ zh->records_deleted = 0;
+ zh->records_processed = 0;
+
+#if HAVE_SYS_TIMES_H
+ times (&zh->tms1);
+#endif
+
+ /* lock */
+ if (zh->shadow_enable)
+ rval = res_get (zh->res, "shadow");
+
+ for (pass = 0; pass < 2; pass++)
+ {
+ if (rval)
+ {
+ zebra_lock_r (zh->lock_normal);
+ zebra_lock_w (zh->lock_shadow);
+ }
+ else
+ {
+ zebra_lock_w (zh->lock_normal);
+ zebra_lock_w (zh->lock_shadow);
+ }
+
+ zebra_get_state (zh, &val, &seqno);
+ if (val == 'c')
+ {
+ yaz_log (LOG_LOG, "previous transaction didn't finish commit");
+ zebra_unlock (zh->lock_shadow);
+ zebra_unlock (zh->lock_normal);
+ zebra_commit (zh);
+ continue;
+ }
+ else if (val == 'd')
+ {
+ if (rval)
+ {
+ BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
+ zh->path_reg);
+ yaz_log (LOG_LOG, "previous transaction didn't reach commit");
+ bf_commitClean (bfs, rval);
+ bfs_destroy (bfs);
+ }
+ else
+ {
+ yaz_log (LOG_WARN, "your previous transaction didn't finish");
+ }
+ }
+ break;
+ }
+ if (pass == 2)
+ {
+ yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
+ abort();
+ return;
+ }
+ zebra_set_state (zh, 'd', seqno);
+
+ zh->reg = zebra_register_open (zh->service, zh->reg_name,
+ 1, rval ? 1 : 0, zh->res,
+ zh->path_reg);
+
+ zh->reg->seqno = seqno;
+}
+
+void zebra_end_trans (ZebraHandle zh)
+{
+ char val;
+ int seqno;
+ const char *rval;
+
+ zh->trans_no--;
+ if (zh->trans_no != 0)
+ return;
+
+ yaz_log (LOG_LOG, "zebra_end_trans");
+ rval = res_get (zh->res, "shadow");
+
+ zebraExplain_runNumberIncrement (zh->reg->zei, 1);
+
+ zebra_flush_reg (zh);
+
+ zebra_register_close (zh->service, zh->reg);
+ zh->reg = 0;
+
+
+ yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
+ zh->records_processed, zh->records_inserted,
+ zh->records_updated, zh->records_deleted);
+
+ zebra_get_state (zh, &val, &seqno);
+ if (val != 'd')
+ {
+ BFiles bfs = bfs_create (rval, zh->path_reg);
+ yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
+ bf_commitClean (bfs, rval);
+ bfs_destroy (bfs);
+ }
+ if (!rval)
+ seqno++;
+ zebra_set_state (zh, 'o', seqno);
+
+ zebra_unlock (zh->lock_shadow);
+ zebra_unlock (zh->lock_normal);
+
+#if HAVE_SYS_TIMES_H
+ times (&zh->tms2);
+ logf (LOG_LOG, "user/system: %ld/%ld",
+ (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
+ (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
+
+#endif
+}
+
+void zebra_repository_update (ZebraHandle zh)
+{
+ zebra_begin_trans (zh);
+ logf (LOG_LOG, "updating %s", zh->rGroup.path);
+ repositoryUpdate (zh);
+ zebra_end_trans (zh);
+}
+
+void zebra_repository_delete (ZebraHandle zh)
+{
+ logf (LOG_LOG, "deleting %s", zh->rGroup.path);
+ repositoryDelete (zh);
+}
+
+void zebra_repository_show (ZebraHandle zh)
+{
+ repositoryShow (zh);
+}
+
+int zebra_commit (ZebraHandle zh)
+{
+ int seqno;
+ char val;
+ const char *rval;
+ BFiles bfs;
+
+ if (!zh->res)
+ {
+ zh->errCode = 109;
+ return -1;
+ }
+ rval = res_get (zh->res, "shadow");
+ if (!rval)
+ {
+ logf (LOG_WARN, "Cannot perform commit");
+ logf (LOG_WARN, "No shadow area defined");
+ return 0;
+ }
+
+ zebra_lock_w (zh->lock_normal);
+ zebra_lock_r (zh->lock_shadow);
+
+ bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
+
+ zebra_get_state (zh, &val, &seqno);
+
+ if (rval && *rval)
+ bf_cache (bfs, rval);
+ if (bf_commitExists (bfs))
+ {
+ zebra_set_state (zh, 'c', seqno);
+
+ logf (LOG_LOG, "commit start");
+ bf_commitExec (bfs);
+#ifndef WIN32
+ sync ();
+#endif
+ logf (LOG_LOG, "commit clean");
+ bf_commitClean (bfs, rval);
+ seqno++;
+ zebra_set_state (zh, 'o', seqno);
+ }
+ else
+ {
+ logf (LOG_LOG, "nothing to commit");
+ }
+ bfs_destroy (bfs);
+
+ zebra_unlock (zh->lock_shadow);
+ zebra_unlock (zh->lock_normal);
+ return 0;
+}
+
+int zebra_init (ZebraHandle zh)
+{
+ const char *rval;
+ BFiles bfs = 0;
+
+ if (!zh->res)
+ {
+ zh->errCode = 109;
+ return -1;
+ }
+ rval = res_get (zh->res, "shadow");
+
+ bfs = bfs_create (res_get (zh->service->global_res, "register"),
+ zh->path_reg);
+ if (rval && *rval)
+ bf_cache (bfs, rval);
+
+ bf_reset (bfs);
+ bfs_destroy (bfs);
+ zebra_set_state (zh, 'o', 0);
+ return 0;
+}
+
+int zebra_compact (ZebraHandle zh)
+{
+ BFiles bfs;
+ if (!zh->res)
+ {
+ zh->errCode = 109;
+ return -1;
+ }
+ bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
+ inv_compact (bfs);
+ bfs_destroy (bfs);
+ return 0;
+}
+
+int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
+{
+ int sysno = 0;
+ zebra_begin_trans (zh);
+ extract_rec_in_mem (zh, "grs.sgml",
+ buf, len,
+ "Default", /* database */
+ 0 /* delete_flag */,
+ 0 /* test_mode */,
+ &sysno /* sysno */,
+ 1 /* store_keys */,
+ 1 /* store_data */,
+ 0 /* match criteria */);
+ zebra_end_trans (zh);
+ return sysno;
+}
+
+void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
+{
+ memcpy (&zh->rGroup, rg, sizeof(*rg));
+}
+
+void zebra_result (ZebraHandle zh, int *code, char **addinfo)
+{
+ *code = zh->errCode;
+ *addinfo = zh->errString;
+}
+
+void zebra_shadow_enable (ZebraHandle zh, int value)
+{
+ zh->shadow_enable = value;
+}
+
+int zebra_record_encoding (ZebraHandle zh, const char *encoding)
+{
+ xfree (zh->record_encoding);
+ zh->record_encoding = xstrdup (encoding);
+ return 0;