1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <yaz/diagbib1.h>
33 #include <yaz/pquery.h>
34 #include <yaz/sortspec.h>
39 #include <idzebra/api.h>
40 #include <yaz/oid_db.h>
42 #define DEFAULT_APPROX_LIMIT 2000000000
44 /* simple asserts to validate the most essential input args */
45 #define ASSERTZH assert(zh && zh->service)
46 #define ASSERTZHRES assert(zh && zh->service && zh->res)
47 #define ASSERTZS assert(zs)
49 static int log_level = 0;
50 static int log_level_initialized = 0;
52 static void zebra_open_res(ZebraHandle zh);
53 static void zebra_close_res(ZebraHandle zh);
55 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
62 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
64 static void zebra_chdir(ZebraService zs)
68 yaz_log(log_level, "zebra_chdir");
69 dir = res_get(zs->global_res, "chdir");
72 yaz_log(YLOG_DEBUG, "chdir %s", dir);
80 static ZEBRA_RES zebra_flush_reg(ZebraHandle zh)
82 ZEBRA_CHECK_HANDLE(zh);
83 yaz_log(log_level, "zebra_flush_reg");
84 zebraExplain_flush(zh->reg->zei, zh);
86 key_block_flush(zh->reg->key_block, 1);
88 zebra_index_merge(zh);
92 static struct zebra_register *zebra_register_open(ZebraService zs,
94 int rw, int useshadow,
96 const char *reg_path);
97 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
99 const char *zebra_get_encoding(ZebraHandle zh)
101 assert(zh && zh->session_res);
102 return res_get_def(zh->session_res, "encoding", "ISO-8859-1");
105 ZebraHandle zebra_open(ZebraService zs, Res res)
108 const char *default_encoding;
109 if (!log_level_initialized)
111 log_level = yaz_log_module_level("zebraapi");
112 log_level_initialized = 1;
115 yaz_log(log_level, "zebra_open");
120 zh = (ZebraHandle) xmalloc(sizeof(*zh));
121 yaz_log(YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
124 zh->reg = 0; /* no register attached yet */
130 zh->session_res = res_open(zs->global_res, res);
132 zh->dbaccesslist = 0;
134 zh->reg_name = xstrdup("");
136 zh->num_basenames = 0;
139 zh->approx_limit = DEFAULT_APPROX_LIMIT;
146 zh->shadow_enable = 1;
147 zh->m_staticrank = 0;
148 zh->m_segment_indexing = 0;
150 zh->break_handler_func = 0;
151 zh->break_handler_data = 0;
153 default_encoding = zebra_get_encoding(zh);
156 yaz_iconv_open("UTF-8", default_encoding);
157 if (zh->iconv_to_utf8 == 0)
158 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported",
160 zh->iconv_from_utf8 =
161 yaz_iconv_open(default_encoding, "UTF-8");
162 if (zh->iconv_to_utf8 == 0)
163 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported",
166 zh->record_encoding = 0;
168 zebra_mutex_cond_lock(&zs->session_lock);
170 zh->next = zs->sessions;
173 zebra_mutex_cond_unlock(&zs->session_lock);
175 zh->store_data_buf = 0;
177 zh->m_limit = zebra_limit_create(1, 0);
179 zh->nmem_error = nmem_create();
184 ZebraService zebra_start(const char *configName)
186 return zebra_start_res(configName, 0, 0);
189 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
192 char version_str[16];
197 if (!log_level_initialized)
199 log_level = yaz_log_module_level("zebraapi");
200 log_level_initialized = 1;
203 zebra_get_version(version_str, system_str);
205 yaz_log(YLOG_LOG, "zebra_start %s %s", version_str,
206 configName ? configName : "");
208 if ((res = res_open(def_res, over_res)))
210 const char *passwd_plain = 0;
211 const char *passwd_encrypt = 0;
212 const char *dbaccess = 0;
217 ZEBRA_RES ret = res_read_file(res, configName);
223 if (zebra_check_res(res))
225 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
232 zebra_check_res(res);
235 zh = xmalloc(sizeof(*zh));
236 zh->global_res = res;
241 zebra_mutex_cond_init(&zh->session_lock);
242 passwd_plain = res_get(zh->global_res, "passwd");
243 passwd_encrypt = res_get(zh->global_res, "passwd.c");
244 dbaccess = res_get(zh->global_res, "dbaccess");
246 if (!passwd_plain && !passwd_encrypt)
247 zh->passwd_db = NULL;
250 zh->passwd_db = passwd_db_open();
252 yaz_log(YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
256 passwd_db_file_plain(zh->passwd_db, passwd_plain);
258 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
265 zh->dbaccess = res_open(NULL, NULL);
266 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
267 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
272 zh->timing = yaz_timing_create();
273 zh->path_root = res_get(zh->global_res, "root");
274 zh->nmem = nmem_create();
275 zh->record_classes = recTypeClass_create(zh->global_res, zh->nmem);
279 const char *module_path = res_get(res, "modulePath");
281 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
289 void zebra_filter_info(ZebraService zs, void *cd,
290 void(*cb)(void *cd, const char *name))
294 recTypeClass_info(zs->record_classes, cd, cb);
297 void zebra_pidfname(ZebraService zs, char *path)
300 zebra_lock_prefix(zs->global_res, path);
301 strcat(path, "zebrasrv.pid");
304 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
305 int compact_flag, Res res)
307 int page_size = 4096;
308 char resource_str[200];
309 sprintf(resource_str, "dict.%.100s.pagesize", name);
313 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
314 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
316 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
320 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
321 int rw, int useshadow, Res res,
322 const char *reg_path)
324 struct zebra_register *reg;
325 int record_compression = REC_COMPRESS_NONE;
326 const char *recordCompression = 0;
327 const char *profilePath;
329 int sort_type = ZEBRA_SORT_TYPE_FLAT;
330 ZEBRA_RES ret = ZEBRA_OK;
334 reg = xmalloc(sizeof(*reg));
337 reg->name = xstrdup(name);
344 yaz_log(YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
345 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
347 reg->dh = data1_create();
354 reg->bfs = bfs_create(res_get(res, "register"), reg_path);
357 data1_destroy(reg->dh);
364 if (bf_cache(reg->bfs, res_get(res, "shadow")) == ZEBRA_FAIL)
366 bfs_destroy(reg->bfs);
367 data1_destroy(reg->dh);
374 getcwd(cwd, sizeof(cwd)-1);
375 profilePath = res_get_def(res, "profilePath", 0);
377 data1_set_tabpath(reg->dh, profilePath);
378 data1_set_tabroot(reg->dh, reg_path);
379 reg->recTypes = recTypes_init(zs->record_classes, reg->dh);
382 zebra_maps_open(res, reg_path, profilePath);
383 if (!reg->zebra_maps)
385 recTypes_destroy(reg->recTypes);
386 bfs_destroy(reg->bfs);
387 data1_destroy(reg->dh);
392 reg->rank_classes = NULL;
395 reg->keys = zebra_rec_keys_open();
397 reg->sortKeys = zebra_rec_keys_open();
408 /* installing rank classes */
409 zebraRankInstall(reg, rank_1_class);
410 zebraRankInstall(reg, rank_similarity_class);
411 zebraRankInstall(reg, rank_static_class);
413 recordCompression = res_get_def(res, "recordCompression", "none");
414 if (!strcmp(recordCompression, "none"))
415 record_compression = REC_COMPRESS_NONE;
416 if (!strcmp(recordCompression, "bzip2"))
417 record_compression = REC_COMPRESS_BZIP2;
420 const char *index_fname = res_get_def(res, "index", "default.idx");
421 if (index_fname && *index_fname && strcmp(index_fname, "none"))
423 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
428 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
430 yaz_log(YLOG_WARN, "rec_open failed");
435 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
437 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
439 yaz_log(YLOG_WARN, "dict_open failed");
444 if (res_get_match(res, "sortindex", "f", "f"))
445 sort_type = ZEBRA_SORT_TYPE_FLAT;
446 else if (res_get_match(res, "sortindex", "i", "f"))
447 sort_type = ZEBRA_SORT_TYPE_ISAMB;
450 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
455 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
457 yaz_log(YLOG_WARN, "zebra_sort_open failed");
460 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
462 struct ISAMS_M_s isams_m;
463 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
464 key_isams_m(res, &isams_m))))
466 yaz_log(YLOG_WARN, "isams_open failed");
470 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
472 struct ISAMC_M_s isamc_m;
473 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
474 rw, key_isamc_m(res, &isamc_m))))
476 yaz_log(YLOG_WARN, "isamc_open failed");
480 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
482 struct ISAMC_M_s isamc_m;
484 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
485 rw, key_isamc_m(res, &isamc_m), 0)))
487 yaz_log(YLOG_WARN, "isamb_open failed");
491 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
493 struct ISAMC_M_s isamc_m;
495 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
496 rw, key_isamc_m(res, &isamc_m), 1)))
498 yaz_log(YLOG_WARN, "isamb_open failed");
502 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
504 struct ISAMC_M_s isamc_m;
506 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
507 rw, key_isamc_m(res, &isamc_m), -1)))
509 yaz_log(YLOG_WARN, "isamb_open failed");
515 reg->zei = zebraExplain_open(reg->records, reg->dh,
517 zebra_extract_explain);
520 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
527 zebra_register_close(zs, reg);
530 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
534 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
536 ZEBRA_CHECK_HANDLE(zh);
537 yaz_log(log_level, "zebra_admin_shutdown");
539 zebra_mutex_cond_lock(&zh->service->session_lock);
540 zh->service->stop_flag = 1;
541 zebra_mutex_cond_unlock(&zh->service->session_lock);
545 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
548 ZEBRA_CHECK_HANDLE(zh);
549 yaz_log(log_level, "zebra_admin_start");
551 zebra_mutex_cond_lock(&zs->session_lock);
552 zebra_mutex_cond_unlock(&zs->session_lock);
556 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
560 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
564 zebraExplain_close(reg->zei);
565 dict_close(reg->dict);
567 dict_close(reg->matchDict);
568 zebra_sort_close(reg->sort_index);
570 isams_close(reg->isams);
572 isamc_close(reg->isamc);
574 isamb_close(reg->isamb);
575 rec_close(®->records);
577 recTypes_destroy(reg->recTypes);
578 zebra_maps_close(reg->zebra_maps);
579 zebraRankDestroy(reg);
580 bfs_destroy(reg->bfs);
581 data1_destroy(reg->dh);
583 zebra_rec_keys_close(reg->keys);
584 zebra_rec_keys_close(reg->sortKeys);
586 key_block_destroy(®->key_block);
591 ZEBRA_RES zebra_stop(ZebraService zs)
597 zebra_close(zs->sessions);
600 zebra_mutex_cond_destroy(&zs->session_lock);
603 passwd_db_close(zs->passwd_db);
605 recTypeClass_destroy(zs->record_classes);
606 nmem_destroy(zs->nmem);
607 res_close(zs->global_res);
609 yaz_timing_stop(zs->timing);
610 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
611 yaz_timing_get_real(zs->timing),
612 yaz_timing_get_user(zs->timing),
613 yaz_timing_get_sys(zs->timing));
616 yaz_timing_destroy(&zs->timing);
621 ZEBRA_RES zebra_close(ZebraHandle zh)
624 struct zebra_session **sp;
627 yaz_log(log_level, "zebra_close");
628 ZEBRA_CHECK_HANDLE(zh);
633 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
634 resultSetDestroy(zh, -1, 0, 0);
637 zebra_register_close(zh->service, zh->reg);
639 res_close(zh->session_res);
641 xfree(zh->record_encoding);
643 xfree(zh->dbaccesslist);
645 for (i = 0; i < zh->num_basenames; i++)
646 xfree(zh->basenames[i]);
647 xfree(zh->basenames);
649 if (zh->iconv_to_utf8 != 0)
650 yaz_iconv_close(zh->iconv_to_utf8);
651 if (zh->iconv_from_utf8 != 0)
652 yaz_iconv_close(zh->iconv_from_utf8);
654 zebra_mutex_cond_lock(&zs->session_lock);
655 zebra_lock_destroy(zh->lock_normal);
656 zebra_lock_destroy(zh->lock_shadow);
668 zebra_mutex_cond_unlock(&zs->session_lock);
670 xfree(zh->user_perm);
671 zh->service = 0; /* more likely to trigger an assert */
673 zebra_limit_destroy(zh->m_limit);
675 nmem_destroy(zh->nmem_error);
682 struct map_baseinfo {
688 char **new_basenames;
692 static void zebra_open_res(ZebraHandle zh)
700 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
701 zh->res = res_open(zh->session_res, 0);
702 res_read_file(zh->res, fname);
704 else if (*zh->reg_name == 0)
706 zh->res = res_open(zh->session_res, 0);
710 yaz_log(YLOG_WARN, "no register root specified");
711 zh->res = 0; /* no path for register - fail! */
715 static void zebra_close_res(ZebraHandle zh)
723 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
727 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
731 assert(zh->reg == 0);
732 assert(*zh->reg_name == 0);
738 resultSetInvalidate(zh);
739 zebra_register_close(zh->service, zh->reg);
745 zh->reg_name = xstrdup(new_reg);
749 if (zh->service->path_root)
751 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
752 strlen(zh->reg_name) + 3);
753 strcpy(zh->path_reg, zh->service->path_root);
756 strcat(zh->path_reg, "/");
757 strcat(zh->path_reg, zh->reg_name);
763 zebra_lock_destroy(zh->lock_normal);
767 zebra_lock_destroy(zh->lock_shadow);
773 const char *lock_area = res_get(zh->res, "lockDir");
775 if (!lock_area && zh->path_reg)
776 res_set(zh->res, "lockDir", zh->path_reg);
777 sprintf(fname, "norm.%s.LCK", zh->reg_name);
779 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
781 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
783 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
785 if (!zh->lock_normal || !zh->lock_shadow)
789 zebra_lock_destroy(zh->lock_normal);
794 zebra_lock_destroy(zh->lock_shadow);
803 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
804 zebra_set_approx_limit(zh, approx);
808 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
809 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
813 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
816 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
817 zh->m_segment_indexing);
822 void map_basenames_func(void *vp, const char *name, const char *value)
824 struct map_baseinfo *p = (struct map_baseinfo *) vp;
826 char fromdb[128], todb[8][128];
833 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
834 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
835 todb[5], todb[6], todb[7]);
839 for (i = 0; i<p->num_bases; i++)
840 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
843 for (i = 0; i < no; i++)
845 if (p->new_num_bases == p->new_num_max)
847 p->new_basenames[(p->new_num_bases)++] =
848 nmem_strdup(p->mem, todb[i]);
854 int zebra_select_default_database(ZebraHandle zh)
858 /* no database has been selected - so we select based on
859 resource setting (including group)
861 const char *group = res_get(zh->session_res, "group");
862 const char *v = res_get_prefix(zh->session_res,
863 "database", group, "Default");
864 return zebra_select_database(zh, v);
869 void map_basenames(ZebraHandle zh, ODR stream,
870 int *num_bases, char ***basenames)
872 struct map_baseinfo info;
873 struct map_baseinfo *p = &info;
876 yaz_log(log_level, "map_basenames ");
881 info.num_bases = *num_bases;
882 info.basenames = *basenames;
883 info.new_num_max = 128;
884 info.new_num_bases = 0;
885 info.new_basenames = (char **)
886 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
887 info.mem = stream->mem;
889 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
891 for (i = 0; i<p->num_bases; i++)
892 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
894 p->new_basenames[(p->new_num_bases)++] =
895 nmem_strdup(p->mem, p->basenames[i]);
897 *num_bases = info.new_num_bases;
898 *basenames = info.new_basenames;
899 for (i = 0; i<*num_bases; i++)
900 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
903 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
905 ZEBRA_CHECK_HANDLE(zh);
907 yaz_log(log_level, "zebra_select_database %s",basename);
909 return zebra_select_databases(zh, 1, &basename);
912 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
913 const char **basenames)
920 ZEBRA_CHECK_HANDLE(zh);
923 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
924 num_bases,basenames[0]);
929 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
933 /* Check if the user has access to all databases (Seb) */
934 /* You could argue that this should happen later, after we have
935 * determined that the database(s) exist. */
936 if (zh->dbaccesslist) {
937 for (i = 0; i < num_bases; i++) {
938 const char *db = basenames[i];
940 for (p = zh->dbaccesslist; p && *p; p = pp) {
942 if ((pp = strchr(p, '+'))) {
948 if (len == strlen(db) && !strncmp(db, p, len))
952 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
958 for (i = 0; i < zh->num_basenames; i++)
959 xfree(zh->basenames[i]);
960 xfree(zh->basenames);
962 zh->num_basenames = num_bases;
963 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
964 for (i = 0; i < zh->num_basenames; i++)
965 zh->basenames[i] = xstrdup(basenames[i]);
967 cp = strrchr(basenames[0], '/');
970 len = cp - basenames[0];
971 new_reg = xmalloc(len + 1);
972 memcpy(new_reg, basenames[0], len);
976 new_reg = xstrdup("");
977 for (i = 1; i<num_bases; i++)
981 cp1 = strrchr(basenames[i], '/');
986 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
989 if (len != cp1 - basenames[i] ||
990 memcmp(basenames[i], new_reg, len))
992 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1000 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1005 zebra_select_register(zh, new_reg);
1009 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1012 if (!zh->lock_normal || !zh->lock_shadow)
1014 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1020 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1022 if (approx_limit == 0)
1023 approx_limit = DEFAULT_APPROX_LIMIT;
1024 zh->approx_limit = approx_limit;
1028 void zebra_set_partial_result(ZebraHandle zh)
1030 zh->partial_result = 1;
1034 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1035 int (*f)(void *client_data),
1038 zh->break_handler_func = f;
1039 zh->break_handler_data = client_data;
1043 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1044 const char *setname, zint *hits,
1045 int *estimated_hit_count,
1046 int *partial_resultset)
1050 ZEBRA_CHECK_HANDLE(zh);
1056 yaz_log(log_level, "zebra_search_rpn");
1058 zh->partial_result = 0;
1060 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1063 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1064 zh->num_basenames, zh->basenames, setname,
1065 hits, estimated_hit_count);
1067 *partial_resultset = zh->partial_result;
1072 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1073 const char *setname, zint *hits)
1075 int estimated_hit_count;
1076 int partial_resultset;
1077 return zebra_search_RPN_x(zh, o, query, setname, hits,
1078 &estimated_hit_count,
1079 &partial_resultset);
1082 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1083 const char *setname,
1084 Z_RecordComposition *comp,
1085 const Odr_oid *input_format, int num_recs,
1086 ZebraRetrievalRecord *recs)
1088 ZebraMetaRecord *poset;
1090 ZEBRA_RES ret = ZEBRA_OK;
1093 ZEBRA_CHECK_HANDLE(zh);
1099 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1103 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1108 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1111 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1112 for (i = 0; i<num_recs; i++)
1113 pos_array[i] = recs[i].position;
1114 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1117 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1118 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1124 for (i = 0; i<num_recs; i++)
1128 recs[i].errCode = 0;
1129 recs[i].format = yaz_oid_recsyn_sutrs;
1130 recs[i].len = strlen(poset[i].term);
1131 recs[i].buf = poset[i].term;
1132 recs[i].base = poset[i].db;
1134 else if (poset[i].sysno)
1138 zebra_snippets *hit_snippet = zebra_snippets_create();
1140 /* we disable hit snippets for now. It does not work well
1141 and it slows retrieval down a lot */
1143 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1147 zebra_record_fetch(zh, setname,
1148 poset[i].sysno, poset[i].score,
1149 stream, input_format, comp,
1150 &recs[i].format, &buf, &len,
1151 &recs[i].base, &recs[i].errString);
1156 recs[i].buf = (char*) odr_malloc(stream, len);
1157 memcpy(recs[i].buf, buf, len);
1161 recs[i].score = poset[i].score;
1162 recs[i].sysno = poset[i].sysno;
1163 zebra_snippets_destroy(hit_snippet);
1167 /* only need to set it once */
1168 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1170 zebra_setError_zint(zh,
1171 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1176 recs[i].buf = 0; /* no record and no error issued */
1178 recs[i].errCode = 0;
1183 zebra_meta_records_destroy(zh, poset, num_recs);
1190 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1192 int *num_entries, ZebraScanEntry **entries,
1194 const char *setname)
1196 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1197 Z_AttributesPlusTerm *zapt;
1198 Odr_oid *attributeSet;
1201 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1204 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1208 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1209 position, num_entries, entries, is_partial,
1212 yaz_pqf_destroy(pqf_parser);
1216 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1217 const Odr_oid *attributeset,
1219 int *num_entries, ZebraScanEntry **entries,
1221 const char *setname)
1225 ZEBRA_CHECK_HANDLE(zh);
1230 assert(num_entries);
1233 yaz_log(log_level, "zebra_scan");
1235 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1242 res = rpn_scan(zh, stream, zapt, attributeset,
1243 zh->num_basenames, zh->basenames, position,
1244 num_entries, entries, is_partial, setname);
1249 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1250 int num_input_setnames, const char **input_setnames,
1251 const char *output_setname,
1252 Z_SortKeySpecList *sort_sequence,
1256 ZEBRA_CHECK_HANDLE(zh);
1258 assert(num_input_setnames>0);
1259 assert(input_setnames);
1260 assert(sort_sequence);
1261 assert(sort_status);
1262 yaz_log(log_level, "zebra_sort");
1264 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1266 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1267 output_setname, sort_sequence, sort_status);
1272 int zebra_deleteResultSet(ZebraHandle zh, int function,
1273 int num_setnames, char **setnames,
1278 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1280 if (zebra_begin_read(zh))
1281 return Z_DeleteStatus_systemProblemAtTarget;
1284 case Z_DeleteResultSetRequest_list:
1285 assert(num_setnames>0);
1287 resultSetDestroy(zh, num_setnames, setnames, statuses);
1289 case Z_DeleteResultSetRequest_all:
1290 resultSetDestroy(zh, -1, 0, statuses);
1294 status = Z_DeleteStatus_success;
1295 for (i = 0; i<num_setnames; i++)
1296 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1297 status = statuses[i];
1301 int zebra_errCode(ZebraHandle zh)
1305 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1308 yaz_log(log_level, "zebra_errCode: o");
1312 const char *zebra_errString(ZebraHandle zh)
1316 e= diagbib1_str(zh->errCode);
1317 yaz_log(log_level, "zebra_errString: %s",e);
1321 char *zebra_errAdd(ZebraHandle zh)
1326 yaz_log(log_level, "zebra_errAdd: %s",a);
1330 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1333 const char *astring;
1337 ZEBRA_CHECK_HANDLE(zh);
1341 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1342 p = res_get(zs->global_res, u);
1343 xfree(zh->user_perm);
1344 zh->user_perm = xstrdup(p ? p : "r");
1346 /* Determine database access list */
1347 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1349 zh->dbaccesslist = xstrdup(astring);
1351 zh->dbaccesslist = 0;
1353 /* users that don't require a password .. */
1354 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1357 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1362 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1363 const char *record_type)
1365 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1366 database, record_type);
1367 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1369 return zebra_begin_trans(zh, 1);
1372 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1374 ZEBRA_CHECK_HANDLE(zh);
1375 yaz_log(log_level, "zebra_admin_import_end");
1376 return zebra_end_trans(zh);
1379 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1381 ZEBRA_RES res = ZEBRA_OK;
1384 ZEBRA_CHECK_HANDLE(zh);
1385 yaz_log(log_level, "zebra_admin_import_segment");
1387 for (i = 0; i<segment->num_segmentRecords; i++)
1389 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1391 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1393 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1394 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1396 Odr_oct *oct = fragment->u.notExternallyTagged;
1399 if(zebra_update_record(
1402 0, /* record Type */
1406 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1414 int delete_w_handle(const char *info, void *handle)
1416 ZebraHandle zh = (ZebraHandle) handle;
1420 if (*info == sizeof(pos))
1422 memcpy(&pos, info+1, sizeof(pos));
1423 isamb_unlink(zh->reg->isamb, pos);
1428 static int delete_SU_handle(void *handle, int ord)
1430 ZebraHandle zh = (ZebraHandle) handle;
1434 ord_len = key_SU_encode(ord, ord_buf);
1435 ord_buf[ord_len] = '\0';
1437 assert(zh->reg->isamb);
1438 dict_delete_subtree(zh->reg->dict, ord_buf,
1439 zh, delete_w_handle);
1443 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1445 ZEBRA_RES ret = ZEBRA_OK;
1447 yaz_log(log_level, "zebra_drop_database %s", db);
1448 ZEBRA_CHECK_HANDLE(zh);
1450 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1452 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1457 if (zebraExplain_curDatabase(zh->reg->zei, db))
1459 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1464 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1465 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1466 0 /* handle */, 0 /* func */);
1467 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1468 zebraExplain_removeDatabase(zh->reg->zei, zh);
1469 zebra_remove_file_match(zh);
1474 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1475 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1476 "drop database only supported for isam:b");
1479 if (zebra_end_trans(zh) != ZEBRA_OK)
1481 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1487 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1489 yaz_log(log_level, "zebra_create_database %s", db);
1490 ZEBRA_CHECK_HANDLE(zh);
1493 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1495 if (zebra_begin_trans(zh, 1))
1498 /* announce database */
1499 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1500 /* explainDatabase */))
1502 if (zebra_end_trans(zh) != ZEBRA_OK)
1504 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1506 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1509 return zebra_end_trans(zh);
1512 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1513 const char *input_str, int input_len,
1514 char *output_str, int output_len)
1517 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1521 yaz_log(log_level, "zebra_string_norm ");
1523 if (!zh->reg->zebra_maps)
1525 wrbuf = zebra_replace(zm, "", input_str, input_len);
1528 if (wrbuf_len(wrbuf) >= output_len)
1530 if (wrbuf_len(wrbuf))
1531 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1532 output_str[wrbuf_len(wrbuf)] = '\0';
1533 return wrbuf_len(wrbuf);
1536 /** \brief set register state (state*.LCK)
1537 \param zh Zebra handle
1539 \param seqno sequence number
1542 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1544 c=commit (writing to register, reading from shadow, shadow mode only)
1546 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1548 char state_fname[256];
1553 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1555 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1556 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1557 f = fopen(fname, "w");
1559 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1560 fprintf(f, "%c %d %ld\n", val, seqno, p);
1565 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1567 char state_fname[256];
1572 yaz_log(log_level, "zebra_get_state ");
1574 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1575 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1576 f = fopen(fname, "r");
1582 fscanf(f, "%c %d", val, seqno);
1588 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1590 return zebra_begin_trans(zh, 0);
1593 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1595 return zebra_end_trans(zh);
1598 static void read_res_for_transaction(ZebraHandle zh)
1600 const char *group = res_get(zh->res, "group");
1602 /* FIXME - do we still use groups ?? */
1604 zh->m_group = group;
1605 v = res_get_prefix(zh->res, "followLinks", group, "1");
1606 zh->m_follow_links = atoi(v);
1608 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1609 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1611 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1612 zh->m_store_keys = atoi(v);
1614 v = res_get_prefix(zh->res, "storeData", group, "1");
1615 zh->m_store_data = atoi(v);
1617 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1618 zh->m_explain_database = atoi(v);
1620 v = res_get_prefix(zh->res, "openRW", group, "1");
1621 zh->m_flag_rw = atoi(v);
1623 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1624 zh->m_file_verbose_limit = atoi(v);
1627 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1629 ZEBRA_CHECK_HANDLE(zh);
1630 zebra_select_default_database(zh);
1633 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1634 "zebra_begin_trans: no database selected");
1638 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1642 if (rw && !strchr(zh->user_perm, 'w'))
1646 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1657 const char *rval = 0;
1662 read_res_for_transaction(zh);
1665 if (zh->trans_no != 1)
1667 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1668 "zebra_begin_trans: no write trans within read");
1673 resultSetInvalidate(zh);
1674 zebra_register_close(zh->service, zh->reg);
1676 zh->trans_w_no = zh->trans_no;
1678 zh->records_inserted = 0;
1679 zh->records_updated = 0;
1680 zh->records_deleted = 0;
1681 zh->records_processed = 0;
1682 zh->records_skipped = 0;
1684 #if HAVE_SYS_TIMES_H
1688 if (zh->shadow_enable)
1689 rval = res_get(zh->res, "shadow");
1693 zebra_lock_r(zh->lock_normal);
1694 zebra_lock_w(zh->lock_shadow);
1698 zebra_lock_w(zh->lock_normal);
1699 zebra_lock_w(zh->lock_shadow);
1701 zebra_get_state(zh, &val, &seqno);
1704 /* either we didn't finish commit or shadow is dirty */
1707 yaz_log(YLOG_WARN, "previous transaction did not finish "
1708 "(shadow disabled)");
1710 zebra_unlock(zh->lock_shadow);
1711 zebra_unlock(zh->lock_normal);
1712 if (zebra_commit(zh))
1720 zebra_lock_r(zh->lock_normal);
1721 zebra_lock_w(zh->lock_shadow);
1725 zebra_lock_w(zh->lock_normal);
1726 zebra_lock_w(zh->lock_shadow);
1730 zebra_set_state(zh, 'd', seqno);
1732 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1733 1, rval ? 1 : 0, zh->res,
1736 zh->reg->seqno = seqno;
1739 zebra_set_state(zh, 'o', seqno);
1741 zebra_unlock(zh->lock_shadow);
1742 zebra_unlock(zh->lock_normal);
1747 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1748 "zebra_begin_trans: cannot open register");
1749 yaz_log(YLOG_FATAL, "%s", zh->errString);
1752 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1762 if (zh->trans_no != 1)
1764 return zebra_flush_reg(zh);
1766 #if HAVE_SYS_TIMES_H
1772 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1775 if (!zh->lock_normal || !zh->lock_shadow)
1778 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1781 zebra_get_state(zh, &val, &seqno);
1787 else if (seqno != zh->reg->seqno)
1789 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1790 seqno, zh->reg->seqno);
1793 else if (zh->reg->last_val != val)
1795 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1796 val, zh->reg->last_val);
1803 zebra_lock_r(zh->lock_shadow);
1805 zebra_lock_r(zh->lock_normal);
1809 resultSetInvalidate(zh);
1810 zebra_register_close(zh->service, zh->reg);
1812 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1813 0, val == 'c' ? 1 : 0,
1814 zh->res, zh->path_reg);
1817 zebra_unlock(zh->lock_normal);
1818 zebra_unlock(zh->lock_shadow);
1820 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1823 zh->reg->last_val = val;
1824 zh->reg->seqno = seqno;
1826 read_res_for_transaction(zh);
1830 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1832 ZebraTransactionStatus dummy;
1834 yaz_log(log_level, "zebra_end_trans");
1835 ZEBRA_CHECK_HANDLE(zh);
1836 return zebra_end_transaction(zh, &dummy);
1839 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1845 ZEBRA_CHECK_HANDLE(zh);
1848 yaz_log(log_level, "zebra_end_transaction");
1850 status->processed = 0;
1851 status->inserted = 0;
1852 status->updated = 0;
1853 status->deleted = 0;
1857 if (!zh->res || !zh->reg)
1859 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1860 "zebra_end_trans: no open transaction");
1863 if (zh->trans_no != zh->trans_w_no)
1866 if (zh->trans_no != 0)
1869 /* release read lock */
1871 zebra_unlock(zh->lock_normal);
1872 zebra_unlock(zh->lock_shadow);
1875 { /* release write lock */
1879 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1880 rval = res_get(zh->res, "shadow");
1882 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1884 zebra_flush_reg(zh);
1886 resultSetInvalidate(zh);
1888 zebra_register_close(zh->service, zh->reg);
1891 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1892 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1893 zh->records_processed, zh->records_inserted,
1894 zh->records_updated, zh->records_deleted);
1896 status->processed = zh->records_processed;
1897 status->inserted = zh->records_inserted;
1898 status->updated = zh->records_updated;
1899 status->deleted = zh->records_deleted;
1901 zebra_get_state(zh, &val, &seqno);
1904 BFiles bfs = bfs_create(rval, zh->path_reg);
1905 bf_commitClean(bfs, rval);
1910 zebra_set_state(zh, 'o', seqno);
1911 zebra_unlock(zh->lock_shadow);
1912 zebra_unlock(zh->lock_normal);
1915 #if HAVE_SYS_TIMES_H
1917 yaz_log(log_level, "user/system: %ld/%ld",
1918 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1919 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1921 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1922 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1927 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1929 return zebra_repository_index(zh, path, action_update);
1932 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1934 return zebra_repository_index(zh, path, action_delete);
1937 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
1938 enum zebra_recctrl_action_t action)
1943 if (action == action_update)
1944 yaz_log(log_level, "updating %s", path);
1945 else if (action == action_delete)
1946 yaz_log(log_level, "deleting %s", path);
1947 else if (action == action_a_delete)
1948 yaz_log(log_level, "attempt deleting %s", path);
1950 yaz_log(log_level, "update action=%d", (int) action);
1952 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
1953 return zebra_update_file_match(zh, path);
1955 return zebra_update_from_path(zh, path, action);
1958 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1962 yaz_log(log_level, "zebra_repository_show");
1963 repositoryShow(zh, path);
1967 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1973 ZEBRA_RES res = ZEBRA_OK;
1977 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
1978 zebra_select_default_database(zh);
1981 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1984 rval = res_get(zh->res, "shadow");
1987 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
1991 zebra_lock_w(zh->lock_normal);
1992 zebra_lock_r(zh->lock_shadow);
1994 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
1997 zebra_unlock(zh->lock_shadow);
1998 zebra_unlock(zh->lock_normal);
2001 zebra_get_state(zh, &val, &seqno);
2005 /* shadow area is dirty and so we must throw it away */
2006 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2009 else if (val == 'c')
2011 /* commit has started. We can not remove it anymore */
2016 bf_cache(bfs, rval);
2017 if (bf_commitExists(bfs))
2020 zebra_set_state(zh, 'd', seqno);
2023 zebra_set_state(zh, 'c', seqno);
2025 yaz_log(log_level, "commit start");
2026 if (bf_commitExec(bfs))
2029 if (res == ZEBRA_OK)
2032 zebra_set_state(zh, 'o', seqno);
2034 zebra_unlock(zh->lock_shadow);
2035 zebra_unlock(zh->lock_normal);
2037 zebra_lock_w(zh->lock_shadow);
2038 bf_commitClean(bfs, rval);
2039 zebra_unlock(zh->lock_shadow);
2043 zebra_unlock(zh->lock_shadow);
2044 zebra_unlock(zh->lock_normal);
2045 yaz_log(YLOG_WARN, "zebra_commit: failed");
2050 zebra_unlock(zh->lock_shadow);
2051 zebra_unlock(zh->lock_normal);
2052 yaz_log(log_level, "nothing to commit");
2059 ZEBRA_RES zebra_clean(ZebraHandle zh)
2061 yaz_log(log_level, "zebra_clean");
2062 ZEBRA_CHECK_HANDLE(zh);
2063 return zebra_commit_ex(zh, 1);
2066 ZEBRA_RES zebra_commit(ZebraHandle zh)
2068 yaz_log(log_level, "zebra_commit");
2069 ZEBRA_CHECK_HANDLE(zh);
2070 return zebra_commit_ex(zh, 0);
2074 ZEBRA_RES zebra_init(ZebraHandle zh)
2079 yaz_log(log_level, "zebra_init");
2081 ZEBRA_CHECK_HANDLE(zh);
2083 zebra_select_default_database(zh);
2086 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2087 "cannot select default database");
2090 rval = res_get(zh->res, "shadow");
2092 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2095 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2099 bf_cache(bfs, rval);
2103 zebra_set_state(zh, 'o', 0);
2107 ZEBRA_RES zebra_compact(ZebraHandle zh)
2111 yaz_log(log_level, "zebra_compact");
2112 ZEBRA_CHECK_HANDLE(zh);
2115 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2118 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2124 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2126 yaz_log(log_level, "zebra_result");
2129 *code = zh->errCode;
2130 *addinfo = zh->errString;
2134 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2135 *addinfo ="ZebraHandle is NULL";
2139 void zebra_shadow_enable(ZebraHandle zh, int value)
2142 yaz_log(log_level, "zebra_shadow_enable");
2143 zh->shadow_enable = value;
2146 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2148 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2149 ZEBRA_CHECK_HANDLE(zh);
2152 if (zh->iconv_to_utf8 != 0)
2153 yaz_iconv_close(zh->iconv_to_utf8);
2154 if (zh->iconv_from_utf8 != 0)
2155 yaz_iconv_close(zh->iconv_from_utf8);
2158 yaz_iconv_open("UTF-8", encoding);
2159 if (zh->iconv_to_utf8 == 0)
2160 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2161 zh->iconv_from_utf8 =
2162 yaz_iconv_open(encoding, "UTF-8");
2163 if (zh->iconv_to_utf8 == 0)
2164 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2169 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2171 yaz_log(log_level, "zebra_record_encoding");
2172 ZEBRA_CHECK_HANDLE(zh);
2173 xfree(zh->record_encoding);
2174 zh->record_encoding = 0;
2176 zh->record_encoding = xstrdup(encoding);
2180 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2184 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2186 res_set(zh->res, name, value);
2189 const char *zebra_get_resource(ZebraHandle zh,
2190 const char *name, const char *defaultvalue)
2195 v = res_get_def(zh->res, name,(char *)defaultvalue);
2196 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2200 /* moved from zebra_api_ext.c by pop */
2201 /* FIXME: Should this really be public??? -Heikki */
2203 int zebra_trans_no(ZebraHandle zh)
2205 yaz_log(log_level, "zebra_trans_no");
2207 return zh->trans_no;
2210 int zebra_get_shadow_enable(ZebraHandle zh)
2212 yaz_log(log_level, "zebra_get_shadow_enable");
2214 return zh->shadow_enable;
2217 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2219 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2221 zh->shadow_enable = value;
2224 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2225 const char *buf, int buf_size)
2227 return zebra_update_record(zh, action_update,
2228 0 /* record type */,
2235 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2236 enum zebra_recctrl_action_t action,
2237 const char *recordType,
2238 zint *sysno, const char *match,
2240 const char *buf, int buf_size)
2244 ZEBRA_CHECK_HANDLE(zh);
2248 yaz_log(log_level, "zebra_update_record");
2250 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2253 buf_size = strlen(buf);
2255 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2257 res = zebra_buffer_extract_record(zh, buf, buf_size,
2264 if (zebra_end_trans(zh) != ZEBRA_OK)
2266 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2272 /* ---------------------------------------------------------------------------
2276 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2277 const char *setname, zint *hits)
2280 ZEBRA_RES res = ZEBRA_OK;
2285 ZEBRA_CHECK_HANDLE(zh);
2287 odr = odr_createmem(ODR_ENCODE);
2292 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2294 query = p_query_rpn(odr, pqf_query);
2298 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2299 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2303 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2307 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2315 /* ---------------------------------------------------------------------------
2316 Sort - a simplified interface, with optional read locks.
2318 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2319 const char *sort_spec,
2320 const char *output_setname,
2321 const char **input_setnames)
2323 int num_input_setnames = 0;
2324 int sort_status = 0;
2325 Z_SortKeySpecList *sort_sequence;
2327 ZEBRA_CHECK_HANDLE(zh);
2330 assert(output_setname);
2331 assert(input_setnames);
2332 sort_sequence = yaz_sort_spec(stream, sort_spec);
2333 yaz_log(log_level, "sort (FIXME) ");
2336 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2337 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2341 /* we can do this, since the perl typemap code for char** will
2342 put a NULL at the end of list */
2343 while (input_setnames[num_input_setnames]) num_input_setnames++;
2345 if (zebra_begin_read(zh))
2348 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2349 output_setname, sort_sequence, &sort_status);
2355 /* ---------------------------------------------------------------------------
2356 Get BFS for Zebra system (to make alternative storage methods)
2358 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2361 return zh->reg->bfs;
2366 /* ---------------------------------------------------------------------------
2367 Set limit for search/scan
2369 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2371 ZEBRA_CHECK_HANDLE(zh);
2372 zebra_limit_destroy(zh->m_limit);
2373 zh->m_limit = zebra_limit_create(complement_flag, ids);
2378 Set Error code + addinfo
2380 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2385 nmem_reset(zh->nmem_error);
2386 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2389 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2392 sprintf(vstr, ZINT_FORMAT, i);
2395 nmem_reset(zh->nmem_error);
2396 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2399 void zebra_lock_prefix(Res res, char *path)
2401 const char *lock_dir = res_get_def(res, "lockDir", "");
2403 strcpy(path, lock_dir);
2404 if (*path && path[strlen(path)-1] != '/')
2411 * indent-tabs-mode: nil
2413 * vim: shiftwidth=4 tabstop=8 expandtab