-/*
- * Copyright (C) 1995-2002, Index Data
- * All rights reserved.
- *
- * $Id: zebraapi.c,v 1.55 2002-04-15 13:48:11 adam Exp $
- */
+/* $Id: zebraapi.c,v 1.81 2003-01-15 07:26:40 oleg Exp $
+ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
+ Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Zebra; see the file LICENSE.zebra. If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
#include <assert.h>
#include <stdio.h>
static void zebra_flush_reg (ZebraHandle zh)
{
- zebraExplain_flush (zh->reg->zei, 1, zh);
+ zebraExplain_flush (zh->reg->zei, zh);
extract_flushWriteKeys (zh);
zebra_index_merge (zh);
const char *reg_path);
static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
-static int zebra_begin_read (ZebraHandle zh);
-static void zebra_end_read (ZebraHandle zh);
-
ZebraHandle zebra_open (ZebraService zs)
{
ZebraHandle zh;
+ const char *default_encoding;
if (!zs)
return 0;
zh = (ZebraHandle) xmalloc (sizeof(*zh));
- yaz_log (LOG_LOG, "zebra_open zs=%p returns %p", zs, zh);
+ yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
zh->service = zs;
zh->reg = 0; /* no register attached yet */
zh->destroyed = 0;
zh->errCode = 0;
zh->errString = 0;
- zh->res = 0;
+ zh->res = 0;
zh->reg_name = xstrdup ("");
zh->path_reg = 0;
zh->shadow_enable = 1;
+ default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
+ zh->record_encoding = xstrdup (default_encoding);
+
+ zh->iconv_to_utf8 =
+ yaz_iconv_open ("UTF-8", default_encoding);
+ if (zh->iconv_to_utf8 == 0)
+ yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
+ default_encoding);
+ zh->iconv_from_utf8 =
+ yaz_iconv_open (default_encoding, "UTF-8");
+ if (zh->iconv_to_utf8 == 0)
+ yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
+ default_encoding);
+
zebra_mutex_cond_lock (&zs->session_lock);
zh->next = zs->sessions;
yaz_log (LOG_LOG, "zebra_start %s", configName);
- if (!(res = res_open (configName, 0)))
- yaz_log (LOG_WARN, "Cannot read resources `%s'", configName);
- else
+ if ((res = res_open (configName, 0)))
{
ZebraService zh = xmalloc (sizeof(*zh));
- yaz_log (LOG_LOG, "Read resources `%s'", configName);
+ yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
zh->global_res = res;
zh->configName = xstrdup(configName);
assert (res);
- yaz_log (LOG_LOG, "zebra_register_open rw = %d useshadow=%d p=%p",
+ yaz_log (LOG_DEBUG, "zebra_register_open rw = %d useshadow=%d p=%p",
rw, useshadow, reg);
- reg->dh = data1_create ();
+ reg->dh = data1_createx (DATA1_FLAG_XML);
if (!reg->dh)
return 0;
reg->bfs = bfs_create (res_get (res, "register"), reg_path);
}
if (useshadow)
bf_cache (reg->bfs, res_get (res, "shadow"));
- data1_set_tabpath (reg->dh, res_get(res, "profilePath"));
+ data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
+ DEFAULT_PROFILE_PATH));
data1_set_tabroot (reg->dh, reg_path);
reg->recTypes = recTypes_init (reg->dh);
recTypes_default_handlers (reg->recTypes);
reg->isam = 0;
reg->isamc = 0;
reg->isamd = 0;
+ reg->isamb = 0;
reg->zei = 0;
reg->matchDict = 0;
+ reg->key_file_no = 0;
zebraRankInstall (reg, rank1_class);
return 0;
}
}
- else if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
+ if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
{
if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
sizeof (struct it_key), res)))
return 0;
}
}
- else if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
+ if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
{
struct ISAMC_M_s isamc_m;
if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
return 0;
}
}
- else if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
+ if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
{
struct ISAMD_M_s isamd_m;
return 0;
}
}
+ if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
+ {
+ struct ISAMC_M_s isamc_m;
+
+ if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
+ rw, key_isamc_m(res, &isamc_m), 0)))
+ {
+ logf (LOG_WARN, "isamb_open");
+ return 0;
+ }
+ }
+ if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
+ {
+ struct ISAMC_M_s isamc_m;
+
+ if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
+ rw, key_isamc_m(res, &isamc_m), 1)))
+ {
+ logf (LOG_WARN, "isamb_open");
+ return 0;
+ }
+ }
+ if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
+ {
+ struct ISAMC_M_s isamc_m;
+
+ if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
+ rw, key_isamc_m(res, &isamc_m), -1)))
+ {
+ logf (LOG_WARN, "isamb_open");
+ return 0;
+ }
+ }
reg->zei = zebraExplain_open (reg->records, reg->dh,
res, rw, reg,
explain_extract);
return 0;
}
reg->active = 2;
- yaz_log (LOG_LOG, "zebra_register_open ok p=%p", reg);
+ yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
return reg;
}
static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
{
- yaz_log(LOG_LOG, "zebra_register_close p=%p", reg);
+ yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
reg->stop_flag = 0;
zebra_chdir (zs);
if (reg->records)
{
- zebraExplain_close (reg->zei, 0);
+ zebraExplain_close (reg->zei);
dict_close (reg->dict);
if (reg->matchDict)
dict_close (reg->matchDict);
isc_close (reg->isamc);
if (reg->isamd)
isamd_close (reg->isamd);
+ if (reg->isamb)
+ isamb_close (reg->isamb);
rec_close (®->records);
}
xfree (reg->key_buf);
xfree (reg->name);
xfree (reg);
- yaz_log (LOG_LOG, "zebra_register_close 2");
+ yaz_log(LOG_DEBUG, "zebra_register_close 2");
}
void zebra_stop(ZebraService zs)
return ;
yaz_log (LOG_LOG, "zebra_stop");
- zebra_mutex_cond_lock (&zs->session_lock);
while (zs->sessions)
{
zebra_close (zs->sessions);
}
- zebra_mutex_cond_unlock (&zs->session_lock);
-
zebra_mutex_cond_destroy (&zs->session_lock);
if (zs->passwd_db)
return;
zs = zh->service;
- yaz_log (LOG_LOG, "zebra_close zh=%p", zh);
+ yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
if (!zh)
return ;
resultSetDestroy (zh, -1, 0, 0);
zebra_register_close (zh->service, zh->reg);
zebra_close_res (zh);
+ xfree (zh->record_encoding);
+
+ if (zh->iconv_to_utf8 != 0)
+ yaz_iconv_close (zh->iconv_to_utf8);
+ if (zh->iconv_from_utf8 != 0)
+ yaz_iconv_close (zh->iconv_from_utf8);
+
xfree (zh->admin_databaseName);
zebra_mutex_cond_lock (&zs->session_lock);
zebra_lock_destroy (zh->lock_normal);
}
sp = &(*sp)->next;
}
-// if (!zs->sessions && zs->stop_flag)
-// zebra_register_deactivate(zs);
zebra_mutex_cond_unlock (&zs->session_lock);
xfree (zh->reg_name);
xfree (zh);
- yaz_log (LOG_LOG, "zebra_close zh=%p end", zh);}
+}
struct map_baseinfo {
ZebraHandle zh;
return ;
no--;
for (i = 0; i<p->num_bases; i++)
- if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
+ if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
{
p->basenames[i] = 0;
for (i = 0; i < no; i++)
zh->errCode = 109;
return -1;
}
+ if (!zh->lock_normal || !zh->lock_shadow)
+ {
+ zh->errCode = 2;
+ return -1;
+ }
return 0;
}
zh->errString = odr_strdup (stream, setname);
return;
}
+
+ zh->errCode = 0;
if (zebra_begin_read (zh))
return;
return 1;
}
-void zebra_admin_import_begin (ZebraHandle zh, const char *database)
+void zebra_admin_import_begin (ZebraHandle zh, const char *database,
+ const char *record_type)
{
+ if (zebra_select_database(zh, database))
+ return;
zebra_begin_trans (zh);
xfree (zh->admin_databaseName);
zh->admin_databaseName = xstrdup(database);
}
}
+int zebra_admin_exchange_record (ZebraHandle zh,
+ const char *database,
+ const char *rec_buf,
+ size_t rec_len,
+ const char *recid_buf, size_t recid_len,
+ int action)
+{
+ int sysno = 0;
+ char *rinfo = 0;
+ char recid_z[256];
+
+ if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
+ return -1;
+ memcpy (recid_z, recid_buf, recid_len);
+ recid_z[recid_len] = 0;
+
+ rinfo = dict_lookup (zh->reg->matchDict, recid_z);
+ if (rinfo)
+ {
+ if (action == 1) /* fail if insert */
+ return -1;
+ memcpy (&sysno, rinfo+1, sizeof(sysno));
+ }
+ else
+ {
+ if (action == 2 || action == 3) /* fail if delete or update */
+ return -1;
+ }
+ extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
+ action == 3 ? 1 : 0 /* delete flag */,
+ 0, &sysno, 1, 1, 0);
+ if (action == 1)
+ {
+ dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
+ }
+ else if (action == 3)
+ {
+ dict_delete (zh->reg->matchDict, recid_z);
+ }
+ return 0;
+}
+
void zebra_admin_create (ZebraHandle zh, const char *database)
{
ZebraService zs;
+ if (zebra_select_database (zh, database))
+ return;
zebra_begin_trans (zh);
zs = zh->service;
/* explainDatabase */))
{
zh->errCode = 224;
- zh->errString = "Database already exist";
+ zh->errString = "database already exist";
}
zebra_end_trans (zh);
}
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);
+ yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
fprintf (f, "%c %d %ld\n", val, seqno, p);
fclose (f);
xfree (fname);
xfree (fname);
}
-static int zebra_begin_read (ZebraHandle zh)
+int zebra_begin_read (ZebraHandle zh)
{
int dirty = 0;
char val;
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';
return 0;
}
-static void zebra_end_read (ZebraHandle zh)
+void zebra_end_read (ZebraHandle zh)
{
(zh->trans_no)--;
if (zh->trans_no != 0)
return;
+#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
+
zebra_unlock (zh->lock_normal);
zebra_unlock (zh->lock_shadow);
}
}
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
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')
{
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)
zebra_set_state (zh, 'c', seqno);
logf (LOG_LOG, "commit start");
-#ifndef WIN32
- sleep (2);
-#endif
bf_commitExec (bfs);
#ifndef WIN32
sync ();
zh->shadow_enable = value;
}
+int zebra_record_encoding (ZebraHandle zh, const char *encoding)
+{
+ xfree (zh->record_encoding);
+
+ /*
+ * Fixme!
+ */
+
+ if (zh->iconv_to_utf8 != 0)
+ yaz_iconv_close(zh->iconv_to_utf8);
+ if (zh->iconv_from_utf8 != 0)
+ yaz_iconv_close(zh->iconv_from_utf8);
+
+ zh->record_encoding = xstrdup (encoding);
+
+ logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
+
+ zh->iconv_to_utf8 =
+ yaz_iconv_open ("UTF-8", encoding);
+ if (zh->iconv_to_utf8 == 0)
+ yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
+ zh->iconv_from_utf8 =
+ yaz_iconv_open (encoding, "UTF-8");
+ if (zh->iconv_to_utf8 == 0)
+ yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
+
+ return 0;
+}
+
+void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
+{
+ res_put(zh->res, name, value);
+}
+
+const char *zebra_get_resource(ZebraHandle zh,
+ const char *name, const char *defaultvalue)
+{
+ return res_get_def( zh->res, name, (char *)defaultvalue);
+}