+static void zebra_select_register(ZebraHandle zh, const char *new_reg)
+{
+ ASSERTZH;
+ zh->errCode = 0;
+ if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
+ return;
+ if (!zh->res)
+ {
+ assert(zh->reg == 0);
+ assert(*zh->reg_name == 0);
+ }
+ else
+ {
+ if (zh->reg)
+ {
+ resultSetInvalidate(zh);
+ zebra_register_close(zh->service, zh->reg);
+ zh->reg = 0;
+ }
+ zebra_close_res(zh);
+ }
+ xfree(zh->reg_name);
+ zh->reg_name = xstrdup(new_reg);
+
+ 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]);
+ }