+ xfree(zh->path_reg);
+ zh->path_reg = 0;
+ if (zh->service->path_root)
+ {
+ zh->path_reg = xmalloc(strlen(zh->service->path_root) +
+ strlen(zh->reg_name) + 3);
+ strcpy(zh->path_reg, zh->service->path_root);
+ if (*zh->reg_name)
+ {
+ strcat(zh->path_reg, "/");
+ strcat(zh->path_reg, zh->reg_name);
+ }
+ }
+ zebra_open_res(zh);
+
+ if (zh->lock_normal)
+ zebra_lock_destroy(zh->lock_normal);
+ zh->lock_normal = 0;
+
+ if (zh->lock_shadow)
+ zebra_lock_destroy(zh->lock_shadow);
+ zh->lock_shadow = 0;
+
+ if (zh->res)
+ {
+ char fname[512];
+ const char *lock_area = res_get(zh->res, "lockDir");
+
+ if (!lock_area && zh->path_reg)
+ res_set(zh->res, "lockDir", zh->path_reg);
+ sprintf(fname, "norm.%s.LCK", zh->reg_name);
+ zh->lock_normal =
+ zebra_lock_create(res_get(zh->res, "lockDir"), fname);
+
+ sprintf(fname, "shadow.%s.LCK", zh->reg_name);
+ zh->lock_shadow =
+ zebra_lock_create(res_get(zh->res, "lockDir"), fname);
+
+ if (!zh->lock_normal || !zh->lock_shadow)
+ {
+ if (zh->lock_normal)
+ {
+ zebra_lock_destroy(zh->lock_normal);
+ zh->lock_normal = 0;
+ }
+ if (zh->lock_shadow)
+ {
+ zebra_lock_destroy(zh->lock_shadow);
+ zh->lock_shadow = 0;
+ }
+ zebra_close_res(zh);
+ }
+ }
+ if (zh->res)
+ {
+ int approx = 0;
+ if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
+ zebra_set_approx_limit(zh, approx);
+ }
+ if (zh->res)
+ {
+ if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
+ yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
+ }
+ if (zh->res)
+ {
+ if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
+ ZEBRA_OK)
+ {
+ yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
+ zh->m_segment_indexing);
+ }
+ }
+}
+
+void map_basenames_func(void *vp, const char *name, const char *value)
+{
+ struct map_baseinfo *p = (struct map_baseinfo *) vp;
+ int i, no;
+ char fromdb[128], todb[8][128];
+
+ assert(value);
+ assert(name);
+ assert(vp);
+
+ no =
+ sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
+ fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
+ todb[5], todb[6], todb[7]);
+ if (no < 2)
+ return ;
+ no--;
+ for (i = 0; i<p->num_bases; i++)
+ if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
+ {
+ p->basenames[i] = 0;
+ for (i = 0; i < no; i++)
+ {
+ if (p->new_num_bases == p->new_num_max)
+ return;
+ p->new_basenames[(p->new_num_bases)++] =
+ nmem_strdup(p->mem, todb[i]);
+ }
+ return;
+ }
+}
+
+int zebra_select_default_database(ZebraHandle zh)
+{
+ if (!zh->res)
+ {
+ /* no database has been selected - so we select based on
+ resource setting (including group)
+ */
+ const char *group = res_get(zh->session_res, "group");
+ const char *v = res_get_prefix(zh->session_res,
+ "database", group, "Default");
+ return zebra_select_database(zh, v);
+ }
+ return 0;
+}
+
+void map_basenames(ZebraHandle zh, ODR stream,
+ int *num_bases, char ***basenames)
+{
+ struct map_baseinfo info;
+ struct map_baseinfo *p = &info;
+ int i;
+ ASSERTZH;
+ yaz_log(log_level, "map_basenames ");
+ assert(stream);
+
+ info.zh = zh;
+
+ info.num_bases = *num_bases;
+ info.basenames = *basenames;
+ info.new_num_max = 128;
+ info.new_num_bases = 0;
+ info.new_basenames = (char **)
+ odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
+ info.mem = stream->mem;
+
+ res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
+
+ for (i = 0; i<p->num_bases; i++)
+ if (p->basenames[i] && p->new_num_bases < p->new_num_max)
+ {
+ p->new_basenames[(p->new_num_bases)++] =
+ nmem_strdup(p->mem, p->basenames[i]);
+ }
+ *num_bases = info.new_num_bases;
+ *basenames = info.new_basenames;
+ for (i = 0; i<*num_bases; i++)
+ yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
+}
+
+ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
+{
+ ZEBRA_CHECK_HANDLE(zh);
+
+ yaz_log(log_level, "zebra_select_database %s",basename);
+ assert(basename);
+ return zebra_select_databases(zh, 1, &basename);
+}
+
+ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
+ const char **basenames)
+{
+ int i;
+ const char *cp;
+ int len = 0;
+ char *new_reg = 0;
+
+ ZEBRA_CHECK_HANDLE(zh);
+ assert(basenames);
+
+ yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
+ num_bases,basenames[0]);
+ zh->errCode = 0;
+
+ if (num_bases < 1)
+ {
+ zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
+ return ZEBRA_FAIL;
+ }
+
+ /* Check if the user has access to all databases (Seb) */
+ /* You could argue that this should happen later, after we have
+ * determined that the database(s) exist. */
+ if (zh->dbaccesslist) {
+ for (i = 0; i < num_bases; i++) {
+ const char *db = basenames[i];
+ char *p, *pp;
+ for (p = zh->dbaccesslist; p && *p; p = pp) {
+ int len;
+ if ((pp = strchr(p, '+'))) {
+ len = pp - p;
+ pp++;
+ }
+ else
+ len = strlen(p);
+ if (len == strlen(db) && !strncmp(db, p, len))
+ break;
+ }
+ if (!p) {
+ zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
+ return ZEBRA_FAIL;
+ }
+ }
+ }
+
+ for (i = 0; i < zh->num_basenames; i++)
+ xfree(zh->basenames[i]);
+ xfree(zh->basenames);
+
+ zh->num_basenames = num_bases;
+ zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
+ for (i = 0; i < zh->num_basenames; i++)
+ zh->basenames[i] = xstrdup(basenames[i]);
+
+ cp = strrchr(basenames[0], '/');
+ if (cp)
+ {
+ len = cp - basenames[0];
+ new_reg = xmalloc(len + 1);
+ memcpy(new_reg, basenames[0], len);
+ new_reg[len] = '\0';
+ }
+ else
+ new_reg = xstrdup("");
+ for (i = 1; i<num_bases; i++)
+ {
+ const char *cp1;
+
+ cp1 = strrchr(basenames[i], '/');
+ if (cp)
+ {
+ if (!cp1)
+ {
+ zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
+ return -1;
+ }
+ if (len != cp1 - basenames[i] ||
+ memcmp(basenames[i], new_reg, len))
+ {
+ zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
+ return -1;
+ }
+ }
+ else
+ {
+ if (cp1)
+ {
+ zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
+ return ZEBRA_FAIL;
+ }
+ }
+ }
+ zebra_select_register(zh, new_reg);
+ xfree(new_reg);
+ if (!zh->res)
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ return ZEBRA_FAIL;
+ }
+ if (!zh->lock_normal || !zh->lock_shadow)
+ {
+ zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ return ZEBRA_FAIL;
+ }
+ return ZEBRA_OK;
+}
+
+ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
+{
+ if (approx_limit == 0)
+ approx_limit = DEFAULT_APPROX_LIMIT;
+ zh->approx_limit = approx_limit;
+ return ZEBRA_OK;
+}
+
+void zebra_set_partial_result(ZebraHandle zh)
+{
+ zh->partial_result = 1;
+}
+
+
+ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
+ int (*f)(void *client_data),
+ void *client_data)
+{
+ zh->break_handler_func = f;
+ zh->break_handler_data = client_data;
+ return ZEBRA_OK;
+}
+
+ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
+ const char *setname, zint *hits,
+ int *estimated_hit_count,
+ int *partial_resultset)
+{
+ ZEBRA_RES r;
+
+ ZEBRA_CHECK_HANDLE(zh);
+
+ assert(o);
+ assert(query);
+ assert(hits);
+ assert(setname);
+ yaz_log(log_level, "zebra_search_rpn");
+
+ zh->partial_result = 0;
+
+ if (zebra_begin_read(zh) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+
+ r = resultSetAddRPN(zh, odr_extract_mem(o), query,
+ zh->num_basenames, zh->basenames, setname,
+ hits, estimated_hit_count);
+
+ *partial_resultset = zh->partial_result;
+ zebra_end_read(zh);
+ return r;
+}
+
+ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
+ const char *setname, zint *hits)
+{
+ int estimated_hit_count;
+ int partial_resultset;
+ return zebra_search_RPN_x(zh, o, query, setname, hits,
+ &estimated_hit_count,
+ &partial_resultset);
+}
+
+ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
+ const char *setname,
+ Z_RecordComposition *comp,
+ const Odr_oid *input_format, int num_recs,
+ ZebraRetrievalRecord *recs)
+{
+ ZebraMetaRecord *poset;
+ int i;
+ ZEBRA_RES ret = ZEBRA_OK;
+ zint *pos_array;
+
+ ZEBRA_CHECK_HANDLE(zh);
+ assert(stream);
+ assert(setname);
+ assert(recs);
+ assert(num_recs>0);
+
+ yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
+
+ if (!zh->res)
+ {
+ zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
+ setname);
+ return ZEBRA_FAIL;
+ }
+
+ if (zebra_begin_read(zh) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+
+ pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));