-/* $Id: zebraapi.c,v 1.117 2004-01-22 15:40:25 heikki Exp $
+/* $Id: zebraapi.c,v 1.134 2004-10-01 15:36:15 heikki Exp $
Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
Index Data Aps
#include <assert.h>
#include <stdio.h>
+#include <limits.h>
#ifdef WIN32
#include <io.h>
#include <process.h>
#include <yaz/sortspec.h>
#include "index.h"
#include <charmap.h>
-#include "zebraapi.h"
+#include <idzebra/api.h>
/* simple asserts to validate the most essential input args */
#define ASSERTZH assert(zh && zh->service)
#define ASSERTZHRES assert(zh && zh->service && zh->res)
#define ASSERTZS assert(zs)
-/* A simple log macro */
-/* don't break with older yazen that lack log_app2 */
-#ifdef LOG_APP2
#define LOG_API LOG_APP2
-#else
-#define LOG_API LOG_DEBUG
-#endif
static Res zebra_open_res (ZebraHandle zh);
static void zebra_close_res (ZebraHandle zh);
zh->errCode = 0;
zh->errString = 0;
zh->res = 0;
+ zh->user_perm = 0;
zh->reg_name = xstrdup ("");
zh->path_reg = 0;
return zh;
}
-ZebraService zebra_start (const char *configName, Res def_res, Res over_res)
+ZebraService zebra_start (const char *configName)
+{
+ return zebra_start_res(configName, 0, 0);
+}
+
+ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
{
Res res;
- yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
+ yaz_log(LOG_API,"zebra_start %s",configName);
if ((res = res_open (configName, def_res, over_res)))
{
res_get (zh->global_res, "passwd"));
}
zh->path_root = res_get (zh->global_res, "root");
+ zh->nmem = nmem_create();
+ zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
return zh;
}
return 0;
}
+void zebra_filter_info(ZebraService zs, void *cd,
+ void (*cb)(void *cd, const char *name))
+{
+ recTypeClass_info(zs->record_classes, cd, cb);
+}
void zebra_pidfname(ZebraService zs, char *path)
{
strcat(path, "zebrasrv.pid");
}
+Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
+ int compact_flag, Res res)
+{
+ int page_size = 4096;
+ char resource_str[200];
+ const char *v;
+ sprintf (resource_str, "dict.%.100s.pagesize", name);
+
+ v = res_get(res, resource_str);
+ if (v)
+ {
+ page_size = atoi(v);
+ yaz_log(LOG_LOG, "Using custom dictionary page size %d for %s",
+ page_size, name);
+ }
+ return dict_open(bfs, name, cache, rw, compact_flag, page_size);
+}
+
+
static
struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
int rw, int useshadow, Res res,
assert (res);
- yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
+ yaz_log (LOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
reg->dh = data1_createx (DATA1_FLAG_XML);
getcwd(cwd, sizeof(cwd)-1);
profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
- yaz_log(LOG_LOG, "profilePath=%s cwd=%s", profilePath, cwd);
+ yaz_log(LOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
data1_set_tabpath (reg->dh, profilePath);
data1_set_tabroot (reg->dh, reg_path);
- reg->recTypes = recTypes_init (reg->dh);
- recTypes_default_handlers (reg->recTypes);
+ reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
reg->zebra_maps = zebra_maps_open (res, reg_path);
reg->rank_classes = NULL;
reg->keys.buf_max = 0;
reg->keys.buf = 0;
+ reg->keys.codec_handle = iscz1_start();
+
reg->sortKeys.buf = 0;
reg->sortKeys.buf_max = 0;
reg->sortIdx = 0;
reg->isams = 0;
reg->matchDict = 0;
- reg->isam = 0;
reg->isamc = 0;
- reg->isamd = 0;
reg->isamb = 0;
reg->zei = 0;
reg->matchDict = 0;
}
if (rw)
{
- reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
+ reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
}
- if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
+ if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
{
logf (LOG_WARN, "dict_open");
return 0;
return 0;
}
}
- 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)))
- {
- logf (LOG_WARN, "is_open");
- return 0;
- }
- }
if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
{
struct ISAMC_M_s isamc_m;
return 0;
}
}
- if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
- {
- struct ISAMD_M_s isamd_m;
-
- if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
- rw, key_isamd_m(res, &isamd_m))))
- {
- logf (LOG_WARN, "isamd_open");
- return 0;
- }
- }
if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
{
struct ISAMC_M_s isamc_m;
static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
{
ASSERTZS;
- yaz_log(LOG_LOG|LOG_API, "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)
sortIdx_close (reg->sortIdx);
if (reg->isams)
isams_close (reg->isams);
- if (reg->isam)
- is_close (reg->isam);
if (reg->isamc)
isc_close (reg->isamc);
- if (reg->isamd)
- isamd_close (reg->isamd);
if (reg->isamb)
isamb_close (reg->isamb);
rec_close (®->records);
xfree (reg->sortKeys.buf);
xfree (reg->keys.buf);
-
+ if (reg->keys.codec_handle)
+ iscz1_stop(reg->keys.codec_handle);
xfree (reg->key_buf);
xfree (reg->name);
xfree (reg);
{
if (!zs)
return 0;
- yaz_log (LOG_LOG|LOG_API, "zebra_stop");
+ yaz_log (LOG_API, "zebra_stop");
while (zs->sessions)
{
if (zs->passwd_db)
passwd_db_close (zs->passwd_db);
+ recTypeClass_destroy(zs->record_classes);
+ nmem_destroy(zs->nmem);
res_close (zs->global_res);
xfree (zs->configName);
xfree (zs);
}
zebra_mutex_cond_unlock (&zs->session_lock);
xfree (zh->reg_name);
+ xfree (zh->user_perm);
zh->service=0; /* more likely to trigger an assert */
xfree (zh->path_reg);
xfree (zh);
*num_bases = info.new_num_bases;
*basenames = info.new_basenames;
for (i = 0; i<*num_bases; i++)
- logf (LOG_LOG, "base %s", (*basenames)[i]);
+ logf (LOG_DEBUG, "base %s", (*basenames)[i]);
}
int zebra_select_database (ZebraHandle zh, const char *basename)
int zebra_search_RPN (ZebraHandle zh, ODR o,
Z_RPNQuery *query, const char *setname, int *hits)
{
+ const char *max;
+ zint maxhits;
ASSERTZH;
+ assert(query);
+ assert(hits);
+ assert(setname);
yaz_log(LOG_API,"zebra_search_rpn");
zh->errCode=0;
zh->hits = 0;
zh->num_basenames, zh->basenames, setname);
zebra_end_read (zh);
-
- *hits = zh->hits;
+ max = res_get (zh->res, "maxhits");
+ if (max)
+ maxhits=atoi(max);
+ else {
+ int i=0;
+ maxhits=INT_MAX;
+ while (maxhits>100) { maxhits/=10; i++;}
+ while (i--) maxhits *= 10;
+ }
+ if (zh->hits > maxhits) { /* too large for yaz to handle */
+ logf(LOG_DEBUG,"limiting hits to "ZINT_FORMAT, maxhits);
+ *hits=maxhits; /* round it down to two digits, to look like rounded */
+ }
+ else
+ *hits = zh->hits;
return 0;
}
return Z_DeleteStatus_systemProblemAtTarget;
switch (function)
{
- case Z_DeleteRequest_list:
+ case Z_DeleteResultSetRequest_list:
resultSetDestroy (zh, num_setnames, setnames, statuses);
break;
- case Z_DeleteRequest_all:
+ case Z_DeleteResultSetRequest_all:
resultSetDestroy (zh, -1, 0, statuses);
break;
}
int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
{
+ const char *p;
+ char u[40];
ZebraService zs;
+
ASSERTZH;
- yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
zh->errCode=0;
zs= zh->service;
+
+ sprintf(u, "perm.%.30s", user ? user : "anonymous");
+ p = res_get(zs->global_res, u);
+ xfree (zh->user_perm);
+ zh->user_perm = xstrdup(p ? p : "r");
+
+ /* users that don't require a password .. */
+ if (zh->user_perm && strchr(zh->user_perm, 'a'))
+ return 0;
+
if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
- {
- logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
return 0;
- }
-
- logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
return 1;
}
zh->errCode=0;
if (zebra_select_database(zh, database))
return 1;
- zebra_begin_trans (zh, 1);
+ if (zebra_begin_trans (zh, 1))
+ return 1;
return 0;
}
int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
{
- int sysno;
+ SYSNO sysno;
int i;
ASSERTZH;
yaz_log(LOG_API,"zebra_admin_import_segment");
/* 3 = delete. Fail if does not exist */
/* 4 = update. Insert/replace */
{
- int sysno = 0;
+ SYSNO sysno = 0;
char *rinfo = 0;
char recid_z[256];
ASSERTZH;
memcpy (recid_z, recid_buf, recid_len);
recid_z[recid_len] = 0;
- zebra_begin_trans(zh,1);
+ if (zebra_begin_trans(zh, 1))
+ return -1;
rinfo = dict_lookup (zh->reg->matchDict, recid_z);
if (rinfo)
}
ASSERTZHRES;
yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
+
+ if (zh->user_perm)
+ {
+ if (rw && !strchr(zh->user_perm, 'w'))
+ {
+ zh->errCode = 223;
+ zh->errString = 0;
+ return -1;
+ }
+ }
+
assert (zh->res);
if (rw)
{
(zh->trans_no++);
if (zh->trans_w_no)
+ {
+ read_res_for_transaction(zh);
return 0;
+ }
if (zh->trans_no != 1)
{
zh->errCode = 2;
zebra_get_state (zh, &val, &seqno);
if (val == 'c')
{
- yaz_log (LOG_LOG, "previous transaction didn't finish commit");
+ yaz_log (LOG_WARN, "previous transaction didn't finish commit");
zebra_unlock (zh->lock_shadow);
zebra_unlock (zh->lock_normal);
zebra_commit (zh);
{
BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
zh->path_reg);
- yaz_log (LOG_LOG, "previous transaction didn't reach commit");
+ yaz_log (LOG_WARN, "previous transaction didn't reach commit");
bf_commitClean (bfs, rval);
bfs_destroy (bfs);
}
dirty = 1;
else if (seqno != zh->reg->seqno)
{
- yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
+ yaz_log (LOG_DEBUG, "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",
+ yaz_log (LOG_DEBUG, "reopen last cur/old %d/%d",
val, zh->reg->last_val);
dirty = 1;
}
zh->trans_no--;
zh->trans_w_no = 0;
- yaz_log (LOG_LOG, "zebra_end_trans");
+ yaz_log (LOG_DEBUG, "zebra_end_trans");
rval = res_get (zh->res, "shadow");
zebraExplain_runNumberIncrement (zh->reg->zei, 1);
zebra_register_close (zh->service, zh->reg);
zh->reg = 0;
- yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
+ yaz_log (LOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
+ ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
zh->records_processed, zh->records_inserted,
zh->records_updated, zh->records_deleted);
if (val != 'd')
{
BFiles bfs = bfs_create (rval, zh->path_reg);
- yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
+ yaz_log (LOG_DEBUG, "deleting shadow val=%c", val);
bf_commitClean (bfs, rval);
bfs_destroy (bfs);
}
{
ASSERTZH;
zh->errCode=0;
- logf (LOG_LOG|LOG_API, "updating %s", path);
+ logf (LOG_API, "updating %s", path);
repositoryUpdate (zh, path);
return zh->errCode;
}
{
ASSERTZH;
zh->errCode=0;
- logf (LOG_LOG|LOG_API, "deleting %s", path);
+ logf (LOG_API, "deleting %s", path);
repositoryDelete (zh, path);
return zh->errCode;
}
{
zebra_set_state (zh, 'c', seqno);
- logf (LOG_LOG, "commit start");
+ logf (LOG_DEBUG, "commit start");
bf_commitExec (bfs);
#ifndef WIN32
sync ();
#endif
}
- logf (LOG_LOG, "commit clean");
+ logf (LOG_DEBUG, "commit clean");
bf_commitClean (bfs, rval);
seqno++;
zebra_set_state (zh, 'o', seqno);
bfs = bfs_create (res_get (zh->service->global_res, "register"),
zh->path_reg);
+ if (!bfs)
+ return -1;
if (rval && *rval)
bf_cache (bfs, rval);
int zebra_add_record(ZebraHandle zh,
const char *buf, int buf_size)
{
- int sysno = 0;
+ SYSNO sysno = 0;
return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
}
int zebra_insert_record (ZebraHandle zh,
const char *recordType,
- int *sysno, const char *match, const char *fname,
- const char *buf, int buf_size)
+ SYSNO *sysno, const char *match, const char *fname,
+ const char *buf, int buf_size, int force_update)
{
int res;
- yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
+ yaz_log(LOG_API, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
if (buf_size < 1) buf_size = strlen(buf);
- zebra_begin_trans(zh, 1);
+ if (zebra_begin_trans(zh, 1))
+ return 1;
res = buffer_extract_record (zh, buf, buf_size,
0, /* delete_flag */
0, /* test_mode */
int zebra_update_record (ZebraHandle zh,
const char *recordType,
- int* sysno, const char *match, const char *fname,
+ SYSNO* sysno, const char *match, const char *fname,
const char *buf, int buf_size,
int force_update)
{
int res;
- yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
+ yaz_log(LOG_API, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
if (buf_size < 1) buf_size = strlen(buf);
- zebra_begin_trans(zh, 1);
+ if (zebra_begin_trans(zh, 1))
+ return 1;
res = buffer_extract_record (zh, buf, buf_size,
0, /* delete_flag */
0, /* test_mode */
int zebra_delete_record (ZebraHandle zh,
const char *recordType,
- int *sysno, const char *match, const char *fname,
+ SYSNO *sysno, const char *match, const char *fname,
const char *buf, int buf_size,
int force_update)
{
int res;
- yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
+ yaz_log(LOG_API, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
if (buf_size < 1) buf_size = strlen(buf);
- zebra_begin_trans(zh, 1);
+ if (zebra_begin_trans(zh, 1))
+ return 1;
res = buffer_extract_record (zh, buf, buf_size,
1, /* delete_flag */
0, /* test_mode */
/* ---------------------------------------------------------------------------
Sort - a simplified interface, with optional read locks.
- FIXME - This is a horrible name, will conflict with half the applications
*/
int zebra_sort_by_specstr (ZebraHandle zh,
ODR stream,
zebra_end_read(zh);
return sort_status;
}
+