+void zebra_admin_import_begin (ZebraHandle zh, const char *database)
+{
+ zebra_begin_trans (zh);
+ xfree (zh->admin_databaseName);
+ zh->admin_databaseName = xstrdup(database);
+}
+
+void zebra_admin_import_end (ZebraHandle zh)
+{
+ zebra_end_trans (zh);
+}
+
+void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
+{
+ int sysno;
+ int i;
+ for (i = 0; i<segment->num_segmentRecords; i++)
+ {
+ Z_NamePlusRecord *npr = segment->segmentRecords[i];
+ const char *databaseName = npr->databaseName;
+
+ if (!databaseName)
+ databaseName = zh->admin_databaseName;
+ printf ("--------------%d--------------------\n", i);
+ if (npr->which == Z_NamePlusRecord_intermediateFragment)
+ {
+ Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
+ if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
+ {
+ Odr_oct *oct = fragment->u.notExternallyTagged;
+ printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
+ oct->buf);
+
+ sysno = 0;
+ extract_rec_in_mem (zh, "grs.sgml",
+ oct->buf, oct->len,
+ databaseName,
+ 0 /* delete_flag */,
+ 0 /* test_mode */,
+ &sysno /* sysno */,
+ 1 /* store_keys */,
+ 1 /* store_data */,
+ 0 /* match criteria */);
+ }
+ }
+ }
+}
+
+void zebra_admin_create (ZebraHandle zh, const char *database)
+{
+ ZebraService zs;
+
+ zebra_begin_trans (zh);
+
+ zs = zh->service;
+ /* announce database */
+ if (zebraExplain_newDatabase (zh->reg->zei, database, 0
+ /* explainDatabase */))
+ {
+ zh->errCode = 224;
+ zh->errString = "Database already exist";
+ }
+ zebra_end_trans (zh);
+}
+
+int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
+ const char *input_str, int input_len,
+ char *output_str, int output_len)
+{
+ WRBUF wrbuf;
+ if (!zh->reg->zebra_maps)
+ return -1;
+ wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
+ input_str, input_len);
+ if (!wrbuf)
+ return -2;
+ if (wrbuf_len(wrbuf) >= output_len)
+ return -3;
+ if (wrbuf_len(wrbuf))
+ memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
+ output_str[wrbuf_len(wrbuf)] = '\0';
+ return wrbuf_len(wrbuf);
+}
+
+
+void zebra_set_state (ZebraHandle zh, int val, int seqno)
+{
+ char state_fname[256];
+ char *fname;
+ long p = getpid();
+ FILE *f;
+
+ sprintf (state_fname, "state.%s.LCK", zh->reg_name);
+ fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
+ f = fopen (fname, "w");
+
+ yaz_log (LOG_LOG, "%c %d %ld", val, seqno, p);
+ fprintf (f, "%c %d %ld\n", val, seqno, p);
+ fclose (f);
+ xfree (fname);
+}
+
+void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
+{
+ char state_fname[256];
+ char *fname;
+ FILE *f;
+
+ sprintf (state_fname, "state.%s.LCK", zh->reg_name);
+ fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
+ f = fopen (fname, "r");
+ *val = 'o';
+ *seqno = 0;
+
+ if (f)
+ {
+ fscanf (f, "%c %d", val, seqno);
+ fclose (f);
+ }
+ xfree (fname);
+}
+
+int zebra_begin_read (ZebraHandle zh)
+{
+ int dirty = 0;
+ char val;
+ int seqno;
+
+ assert (zh->res);
+
+ (zh->trans_no)++;
+
+ if (zh->trans_no != 1)
+ {
+ zebra_flush_reg (zh);
+ return 0;
+ }
+#if HAVE_SYS_TIMES_H
+ times (&zh->tms1);
+#endif
+ if (!zh->res)
+ {
+ (zh->trans_no)--;
+ zh->errCode = 109;
+ return -1;
+ }
+ if (!zh->lock_normal || !zh->lock_shadow)
+ {
+ (zh->trans_no)--;
+ zh->errCode = 2;
+ return -1;
+ }
+ zebra_get_state (zh, &val, &seqno);
+ if (val == 'd')
+ val = 'o';
+
+ if (!zh->reg)
+ dirty = 1;
+ else if (seqno != zh->reg->seqno)
+ {
+ yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
+ seqno, zh->reg->seqno);
+ dirty = 1;
+ }
+ else if (zh->reg->last_val != val)
+ {
+ yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
+ val, zh->reg->last_val);
+ dirty = 1;
+ }
+ if (!dirty)
+ return 0;
+
+ if (val == 'c')
+ zebra_lock_r (zh->lock_shadow);
+ else
+ zebra_lock_r (zh->lock_normal);
+
+ if (zh->reg)
+ zebra_register_close (zh->service, zh->reg);
+ zh->reg = zebra_register_open (zh->service, zh->reg_name,
+ 0, val == 'c' ? 1 : 0,
+ zh->res, zh->path_reg);
+ if (!zh->reg)
+ {
+ zh->errCode = 109;
+ return -1;
+ }
+ zh->reg->last_val = val;
+ zh->reg->seqno = seqno;
+
+ return 0;
+}
+
+void zebra_end_read (ZebraHandle zh)