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;
448 else if (res_get_match(res, "sortindex", "m", "f"))
449 sort_type = ZEBRA_SORT_TYPE_MULTI;
452 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
457 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
459 yaz_log(YLOG_WARN, "zebra_sort_open failed");
462 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
464 struct ISAMS_M_s isams_m;
465 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
466 key_isams_m(res, &isams_m))))
468 yaz_log(YLOG_WARN, "isams_open failed");
472 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
474 struct ISAMC_M_s isamc_m;
475 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
476 rw, key_isamc_m(res, &isamc_m))))
478 yaz_log(YLOG_WARN, "isamc_open failed");
482 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
484 struct ISAMC_M_s isamc_m;
486 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
487 rw, key_isamc_m(res, &isamc_m), 0)))
489 yaz_log(YLOG_WARN, "isamb_open failed");
493 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
495 struct ISAMC_M_s isamc_m;
497 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
498 rw, key_isamc_m(res, &isamc_m), 1)))
500 yaz_log(YLOG_WARN, "isamb_open failed");
504 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
506 struct ISAMC_M_s isamc_m;
508 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
509 rw, key_isamc_m(res, &isamc_m), -1)))
511 yaz_log(YLOG_WARN, "isamb_open failed");
517 reg->zei = zebraExplain_open(reg->records, reg->dh,
519 zebra_extract_explain);
522 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
529 zebra_register_close(zs, reg);
532 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
536 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
538 ZEBRA_CHECK_HANDLE(zh);
539 yaz_log(log_level, "zebra_admin_shutdown");
541 zebra_mutex_cond_lock(&zh->service->session_lock);
542 zh->service->stop_flag = 1;
543 zebra_mutex_cond_unlock(&zh->service->session_lock);
547 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
550 ZEBRA_CHECK_HANDLE(zh);
551 yaz_log(log_level, "zebra_admin_start");
553 zebra_mutex_cond_lock(&zs->session_lock);
554 zebra_mutex_cond_unlock(&zs->session_lock);
558 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
562 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
566 zebraExplain_close(reg->zei);
567 dict_close(reg->dict);
569 dict_close(reg->matchDict);
570 zebra_sort_close(reg->sort_index);
572 isams_close(reg->isams);
574 isamc_close(reg->isamc);
576 isamb_close(reg->isamb);
577 rec_close(®->records);
579 recTypes_destroy(reg->recTypes);
580 zebra_maps_close(reg->zebra_maps);
581 zebraRankDestroy(reg);
582 bfs_destroy(reg->bfs);
583 data1_destroy(reg->dh);
585 zebra_rec_keys_close(reg->keys);
586 zebra_rec_keys_close(reg->sortKeys);
588 key_block_destroy(®->key_block);
593 ZEBRA_RES zebra_stop(ZebraService zs)
599 zebra_close(zs->sessions);
602 zebra_mutex_cond_destroy(&zs->session_lock);
605 passwd_db_close(zs->passwd_db);
607 recTypeClass_destroy(zs->record_classes);
608 nmem_destroy(zs->nmem);
609 res_close(zs->global_res);
611 yaz_timing_stop(zs->timing);
612 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
613 yaz_timing_get_real(zs->timing),
614 yaz_timing_get_user(zs->timing),
615 yaz_timing_get_sys(zs->timing));
618 yaz_timing_destroy(&zs->timing);
623 ZEBRA_RES zebra_close(ZebraHandle zh)
626 struct zebra_session **sp;
629 yaz_log(log_level, "zebra_close");
630 ZEBRA_CHECK_HANDLE(zh);
635 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
636 resultSetDestroy(zh, -1, 0, 0);
639 zebra_register_close(zh->service, zh->reg);
641 res_close(zh->session_res);
643 xfree(zh->record_encoding);
645 xfree(zh->dbaccesslist);
647 for (i = 0; i < zh->num_basenames; i++)
648 xfree(zh->basenames[i]);
649 xfree(zh->basenames);
651 if (zh->iconv_to_utf8 != 0)
652 yaz_iconv_close(zh->iconv_to_utf8);
653 if (zh->iconv_from_utf8 != 0)
654 yaz_iconv_close(zh->iconv_from_utf8);
656 zebra_mutex_cond_lock(&zs->session_lock);
657 zebra_lock_destroy(zh->lock_normal);
658 zebra_lock_destroy(zh->lock_shadow);
670 zebra_mutex_cond_unlock(&zs->session_lock);
672 xfree(zh->user_perm);
673 zh->service = 0; /* more likely to trigger an assert */
675 zebra_limit_destroy(zh->m_limit);
677 nmem_destroy(zh->nmem_error);
684 struct map_baseinfo {
690 char **new_basenames;
694 static void zebra_open_res(ZebraHandle zh)
702 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
703 zh->res = res_open(zh->session_res, 0);
704 res_read_file(zh->res, fname);
706 else if (*zh->reg_name == 0)
708 zh->res = res_open(zh->session_res, 0);
712 yaz_log(YLOG_WARN, "no register root specified");
713 zh->res = 0; /* no path for register - fail! */
717 static void zebra_close_res(ZebraHandle zh)
725 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
729 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
733 assert(zh->reg == 0);
734 assert(*zh->reg_name == 0);
740 resultSetInvalidate(zh);
741 zebra_register_close(zh->service, zh->reg);
747 zh->reg_name = xstrdup(new_reg);
751 if (zh->service->path_root)
753 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
754 strlen(zh->reg_name) + 3);
755 strcpy(zh->path_reg, zh->service->path_root);
758 strcat(zh->path_reg, "/");
759 strcat(zh->path_reg, zh->reg_name);
765 zebra_lock_destroy(zh->lock_normal);
769 zebra_lock_destroy(zh->lock_shadow);
775 const char *lock_area = res_get(zh->res, "lockDir");
777 if (!lock_area && zh->path_reg)
778 res_set(zh->res, "lockDir", zh->path_reg);
779 sprintf(fname, "norm.%s.LCK", zh->reg_name);
781 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
783 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
785 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
787 if (!zh->lock_normal || !zh->lock_shadow)
791 zebra_lock_destroy(zh->lock_normal);
796 zebra_lock_destroy(zh->lock_shadow);
805 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
806 zebra_set_approx_limit(zh, approx);
810 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
811 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
815 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
818 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
819 zh->m_segment_indexing);
824 void map_basenames_func(void *vp, const char *name, const char *value)
826 struct map_baseinfo *p = (struct map_baseinfo *) vp;
828 char fromdb[128], todb[8][128];
835 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
836 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
837 todb[5], todb[6], todb[7]);
841 for (i = 0; i<p->num_bases; i++)
842 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
845 for (i = 0; i < no; i++)
847 if (p->new_num_bases == p->new_num_max)
849 p->new_basenames[(p->new_num_bases)++] =
850 nmem_strdup(p->mem, todb[i]);
856 int zebra_select_default_database(ZebraHandle zh)
860 /* no database has been selected - so we select based on
861 resource setting (including group)
863 const char *group = res_get(zh->session_res, "group");
864 const char *v = res_get_prefix(zh->session_res,
865 "database", group, "Default");
866 return zebra_select_database(zh, v);
871 void map_basenames(ZebraHandle zh, ODR stream,
872 int *num_bases, char ***basenames)
874 struct map_baseinfo info;
875 struct map_baseinfo *p = &info;
878 yaz_log(log_level, "map_basenames ");
883 info.num_bases = *num_bases;
884 info.basenames = *basenames;
885 info.new_num_max = 128;
886 info.new_num_bases = 0;
887 info.new_basenames = (char **)
888 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
889 info.mem = stream->mem;
891 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
893 for (i = 0; i<p->num_bases; i++)
894 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
896 p->new_basenames[(p->new_num_bases)++] =
897 nmem_strdup(p->mem, p->basenames[i]);
899 *num_bases = info.new_num_bases;
900 *basenames = info.new_basenames;
901 for (i = 0; i<*num_bases; i++)
902 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
905 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
907 ZEBRA_CHECK_HANDLE(zh);
909 yaz_log(log_level, "zebra_select_database %s",basename);
911 return zebra_select_databases(zh, 1, &basename);
914 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
915 const char **basenames)
922 ZEBRA_CHECK_HANDLE(zh);
925 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
926 num_bases,basenames[0]);
931 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
935 /* Check if the user has access to all databases (Seb) */
936 /* You could argue that this should happen later, after we have
937 * determined that the database(s) exist. */
938 if (zh->dbaccesslist) {
939 for (i = 0; i < num_bases; i++) {
940 const char *db = basenames[i];
942 for (p = zh->dbaccesslist; p && *p; p = pp) {
944 if ((pp = strchr(p, '+'))) {
950 if (len == strlen(db) && !strncmp(db, p, len))
954 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
960 for (i = 0; i < zh->num_basenames; i++)
961 xfree(zh->basenames[i]);
962 xfree(zh->basenames);
964 zh->num_basenames = num_bases;
965 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
966 for (i = 0; i < zh->num_basenames; i++)
967 zh->basenames[i] = xstrdup(basenames[i]);
969 cp = strrchr(basenames[0], '/');
972 len = cp - basenames[0];
973 new_reg = xmalloc(len + 1);
974 memcpy(new_reg, basenames[0], len);
978 new_reg = xstrdup("");
979 for (i = 1; i<num_bases; i++)
983 cp1 = strrchr(basenames[i], '/');
988 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
991 if (len != cp1 - basenames[i] ||
992 memcmp(basenames[i], new_reg, len))
994 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1002 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1007 zebra_select_register(zh, new_reg);
1011 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1014 if (!zh->lock_normal || !zh->lock_shadow)
1016 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1022 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1024 if (approx_limit == 0)
1025 approx_limit = DEFAULT_APPROX_LIMIT;
1026 zh->approx_limit = approx_limit;
1030 void zebra_set_partial_result(ZebraHandle zh)
1032 zh->partial_result = 1;
1036 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1037 int (*f)(void *client_data),
1040 zh->break_handler_func = f;
1041 zh->break_handler_data = client_data;
1045 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1046 const char *setname, zint *hits,
1047 int *estimated_hit_count,
1048 int *partial_resultset)
1052 ZEBRA_CHECK_HANDLE(zh);
1058 yaz_log(log_level, "zebra_search_rpn");
1060 zh->partial_result = 0;
1062 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1065 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1066 zh->num_basenames, zh->basenames, setname,
1067 hits, estimated_hit_count);
1069 *partial_resultset = zh->partial_result;
1074 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1075 const char *setname, zint *hits)
1077 int estimated_hit_count;
1078 int partial_resultset;
1079 return zebra_search_RPN_x(zh, o, query, setname, hits,
1080 &estimated_hit_count,
1081 &partial_resultset);
1084 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1085 const char *setname,
1086 Z_RecordComposition *comp,
1087 const Odr_oid *input_format, int num_recs,
1088 ZebraRetrievalRecord *recs)
1090 ZebraMetaRecord *poset;
1092 ZEBRA_RES ret = ZEBRA_OK;
1095 ZEBRA_CHECK_HANDLE(zh);
1101 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1105 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1110 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1113 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1114 for (i = 0; i<num_recs; i++)
1115 pos_array[i] = recs[i].position;
1116 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1119 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1120 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1126 for (i = 0; i<num_recs; i++)
1130 recs[i].errCode = 0;
1131 recs[i].format = yaz_oid_recsyn_sutrs;
1132 recs[i].len = strlen(poset[i].term);
1133 recs[i].buf = poset[i].term;
1134 recs[i].base = poset[i].db;
1136 else if (poset[i].sysno)
1140 zebra_snippets *hit_snippet = zebra_snippets_create();
1142 /* we disable hit snippets for now. It does not work well
1143 and it slows retrieval down a lot */
1145 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1149 zebra_record_fetch(zh, setname,
1150 poset[i].sysno, poset[i].score,
1151 stream, input_format, comp,
1152 &recs[i].format, &buf, &len,
1153 &recs[i].base, &recs[i].errString);
1158 recs[i].buf = (char*) odr_malloc(stream, len);
1159 memcpy(recs[i].buf, buf, len);
1163 recs[i].score = poset[i].score;
1164 recs[i].sysno = poset[i].sysno;
1165 zebra_snippets_destroy(hit_snippet);
1169 /* only need to set it once */
1170 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1172 zebra_setError_zint(zh,
1173 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1178 recs[i].buf = 0; /* no record and no error issued */
1180 recs[i].errCode = 0;
1185 zebra_meta_records_destroy(zh, poset, num_recs);
1192 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1194 int *num_entries, ZebraScanEntry **entries,
1196 const char *setname)
1198 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1199 Z_AttributesPlusTerm *zapt;
1200 Odr_oid *attributeSet;
1203 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1206 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1210 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1211 position, num_entries, entries, is_partial,
1214 yaz_pqf_destroy(pqf_parser);
1218 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1219 const Odr_oid *attributeset,
1221 int *num_entries, ZebraScanEntry **entries,
1223 const char *setname)
1227 ZEBRA_CHECK_HANDLE(zh);
1232 assert(num_entries);
1235 yaz_log(log_level, "zebra_scan");
1237 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1244 res = rpn_scan(zh, stream, zapt, attributeset,
1245 zh->num_basenames, zh->basenames, position,
1246 num_entries, entries, is_partial, setname);
1251 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1252 int num_input_setnames, const char **input_setnames,
1253 const char *output_setname,
1254 Z_SortKeySpecList *sort_sequence,
1258 ZEBRA_CHECK_HANDLE(zh);
1260 assert(num_input_setnames>0);
1261 assert(input_setnames);
1262 assert(sort_sequence);
1263 assert(sort_status);
1264 yaz_log(log_level, "zebra_sort");
1266 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1268 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1269 output_setname, sort_sequence, sort_status);
1274 int zebra_deleteResultSet(ZebraHandle zh, int function,
1275 int num_setnames, char **setnames,
1280 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1282 if (zebra_begin_read(zh))
1283 return Z_DeleteStatus_systemProblemAtTarget;
1286 case Z_DeleteResultSetRequest_list:
1287 assert(num_setnames>0);
1289 resultSetDestroy(zh, num_setnames, setnames, statuses);
1291 case Z_DeleteResultSetRequest_all:
1292 resultSetDestroy(zh, -1, 0, statuses);
1296 status = Z_DeleteStatus_success;
1297 for (i = 0; i<num_setnames; i++)
1298 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1299 status = statuses[i];
1303 int zebra_errCode(ZebraHandle zh)
1307 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1310 yaz_log(log_level, "zebra_errCode: o");
1314 const char *zebra_errString(ZebraHandle zh)
1318 e= diagbib1_str(zh->errCode);
1319 yaz_log(log_level, "zebra_errString: %s",e);
1323 char *zebra_errAdd(ZebraHandle zh)
1328 yaz_log(log_level, "zebra_errAdd: %s",a);
1332 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1335 const char *astring;
1339 ZEBRA_CHECK_HANDLE(zh);
1343 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1344 p = res_get(zs->global_res, u);
1345 xfree(zh->user_perm);
1346 zh->user_perm = xstrdup(p ? p : "r");
1348 /* Determine database access list */
1349 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1351 zh->dbaccesslist = xstrdup(astring);
1353 zh->dbaccesslist = 0;
1355 /* users that don't require a password .. */
1356 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1359 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1364 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1365 const char *record_type)
1367 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1368 database, record_type);
1369 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1371 return zebra_begin_trans(zh, 1);
1374 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1376 ZEBRA_CHECK_HANDLE(zh);
1377 yaz_log(log_level, "zebra_admin_import_end");
1378 return zebra_end_trans(zh);
1381 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1383 ZEBRA_RES res = ZEBRA_OK;
1386 ZEBRA_CHECK_HANDLE(zh);
1387 yaz_log(log_level, "zebra_admin_import_segment");
1389 for (i = 0; i<segment->num_segmentRecords; i++)
1391 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1393 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1395 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1396 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1398 Odr_oct *oct = fragment->u.notExternallyTagged;
1401 if(zebra_update_record(
1404 0, /* record Type */
1408 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1416 int delete_w_handle(const char *info, void *handle)
1418 ZebraHandle zh = (ZebraHandle) handle;
1422 if (*info == sizeof(pos))
1424 memcpy(&pos, info+1, sizeof(pos));
1425 isamb_unlink(zh->reg->isamb, pos);
1430 static int delete_SU_handle(void *handle, int ord)
1432 ZebraHandle zh = (ZebraHandle) handle;
1436 ord_len = key_SU_encode(ord, ord_buf);
1437 ord_buf[ord_len] = '\0';
1439 assert(zh->reg->isamb);
1440 dict_delete_subtree(zh->reg->dict, ord_buf,
1441 zh, delete_w_handle);
1445 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1447 ZEBRA_RES ret = ZEBRA_OK;
1449 yaz_log(log_level, "zebra_drop_database %s", db);
1450 ZEBRA_CHECK_HANDLE(zh);
1452 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1454 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1459 if (zebraExplain_curDatabase(zh->reg->zei, db))
1461 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1466 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1467 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1468 0 /* handle */, 0 /* func */);
1469 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1470 zebraExplain_removeDatabase(zh->reg->zei, zh);
1471 zebra_remove_file_match(zh);
1476 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1477 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1478 "drop database only supported for isam:b");
1481 if (zebra_end_trans(zh) != ZEBRA_OK)
1483 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1489 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1491 yaz_log(log_level, "zebra_create_database %s", db);
1492 ZEBRA_CHECK_HANDLE(zh);
1495 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1497 if (zebra_begin_trans(zh, 1))
1500 /* announce database */
1501 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1502 /* explainDatabase */))
1504 if (zebra_end_trans(zh) != ZEBRA_OK)
1506 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1508 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1511 return zebra_end_trans(zh);
1514 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1515 const char *input_str, int input_len,
1516 char *output_str, int output_len)
1519 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1523 yaz_log(log_level, "zebra_string_norm ");
1525 if (!zh->reg->zebra_maps)
1527 wrbuf = zebra_replace(zm, "", input_str, input_len);
1530 if (wrbuf_len(wrbuf) >= output_len)
1532 if (wrbuf_len(wrbuf))
1533 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1534 output_str[wrbuf_len(wrbuf)] = '\0';
1535 return wrbuf_len(wrbuf);
1538 /** \brief set register state (state*.LCK)
1539 \param zh Zebra handle
1541 \param seqno sequence number
1544 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1546 c=commit (writing to register, reading from shadow, shadow mode only)
1548 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1550 char state_fname[256];
1555 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1557 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1558 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1559 f = fopen(fname, "w");
1561 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1562 fprintf(f, "%c %d %ld\n", val, seqno, p);
1567 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1569 char state_fname[256];
1574 yaz_log(log_level, "zebra_get_state ");
1576 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1577 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1578 f = fopen(fname, "r");
1584 fscanf(f, "%c %d", val, seqno);
1590 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1592 return zebra_begin_trans(zh, 0);
1595 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1597 return zebra_end_trans(zh);
1600 static void read_res_for_transaction(ZebraHandle zh)
1602 const char *group = res_get(zh->res, "group");
1604 /* FIXME - do we still use groups ?? */
1606 zh->m_group = group;
1607 v = res_get_prefix(zh->res, "followLinks", group, "1");
1608 zh->m_follow_links = atoi(v);
1610 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1611 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1613 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1614 zh->m_store_keys = atoi(v);
1616 v = res_get_prefix(zh->res, "storeData", group, "1");
1617 zh->m_store_data = atoi(v);
1619 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1620 zh->m_explain_database = atoi(v);
1622 v = res_get_prefix(zh->res, "openRW", group, "1");
1623 zh->m_flag_rw = atoi(v);
1625 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1626 zh->m_file_verbose_limit = atoi(v);
1629 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1631 ZEBRA_CHECK_HANDLE(zh);
1632 zebra_select_default_database(zh);
1635 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1636 "zebra_begin_trans: no database selected");
1640 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1644 if (rw && !strchr(zh->user_perm, 'w'))
1648 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1659 const char *rval = 0;
1664 read_res_for_transaction(zh);
1667 if (zh->trans_no != 1)
1669 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1670 "zebra_begin_trans: no write trans within read");
1675 resultSetInvalidate(zh);
1676 zebra_register_close(zh->service, zh->reg);
1678 zh->trans_w_no = zh->trans_no;
1680 zh->records_inserted = 0;
1681 zh->records_updated = 0;
1682 zh->records_deleted = 0;
1683 zh->records_processed = 0;
1684 zh->records_skipped = 0;
1686 #if HAVE_SYS_TIMES_H
1690 if (zh->shadow_enable)
1691 rval = res_get(zh->res, "shadow");
1695 zebra_lock_r(zh->lock_normal);
1696 zebra_lock_w(zh->lock_shadow);
1700 zebra_lock_w(zh->lock_normal);
1701 zebra_lock_w(zh->lock_shadow);
1703 zebra_get_state(zh, &val, &seqno);
1706 /* either we didn't finish commit or shadow is dirty */
1709 yaz_log(YLOG_WARN, "previous transaction did not finish "
1710 "(shadow disabled)");
1712 zebra_unlock(zh->lock_shadow);
1713 zebra_unlock(zh->lock_normal);
1714 if (zebra_commit(zh))
1722 zebra_lock_r(zh->lock_normal);
1723 zebra_lock_w(zh->lock_shadow);
1727 zebra_lock_w(zh->lock_normal);
1728 zebra_lock_w(zh->lock_shadow);
1732 zebra_set_state(zh, 'd', seqno);
1734 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1735 1, rval ? 1 : 0, zh->res,
1738 zh->reg->seqno = seqno;
1741 zebra_set_state(zh, 'o', seqno);
1743 zebra_unlock(zh->lock_shadow);
1744 zebra_unlock(zh->lock_normal);
1749 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1750 "zebra_begin_trans: cannot open register");
1751 yaz_log(YLOG_FATAL, "%s", zh->errString);
1754 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1764 if (zh->trans_no != 1)
1766 return zebra_flush_reg(zh);
1768 #if HAVE_SYS_TIMES_H
1774 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1777 if (!zh->lock_normal || !zh->lock_shadow)
1780 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1783 zebra_get_state(zh, &val, &seqno);
1789 else if (seqno != zh->reg->seqno)
1791 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1792 seqno, zh->reg->seqno);
1795 else if (zh->reg->last_val != val)
1797 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1798 val, zh->reg->last_val);
1805 zebra_lock_r(zh->lock_shadow);
1807 zebra_lock_r(zh->lock_normal);
1811 resultSetInvalidate(zh);
1812 zebra_register_close(zh->service, zh->reg);
1814 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1815 0, val == 'c' ? 1 : 0,
1816 zh->res, zh->path_reg);
1819 zebra_unlock(zh->lock_normal);
1820 zebra_unlock(zh->lock_shadow);
1822 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1825 zh->reg->last_val = val;
1826 zh->reg->seqno = seqno;
1828 read_res_for_transaction(zh);
1832 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1834 ZebraTransactionStatus dummy;
1836 yaz_log(log_level, "zebra_end_trans");
1837 ZEBRA_CHECK_HANDLE(zh);
1838 return zebra_end_transaction(zh, &dummy);
1841 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1847 ZEBRA_CHECK_HANDLE(zh);
1850 yaz_log(log_level, "zebra_end_transaction");
1852 status->processed = 0;
1853 status->inserted = 0;
1854 status->updated = 0;
1855 status->deleted = 0;
1859 if (!zh->res || !zh->reg)
1861 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1862 "zebra_end_trans: no open transaction");
1865 if (zh->trans_no != zh->trans_w_no)
1868 if (zh->trans_no != 0)
1871 /* release read lock */
1873 zebra_unlock(zh->lock_normal);
1874 zebra_unlock(zh->lock_shadow);
1877 { /* release write lock */
1881 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1882 rval = res_get(zh->res, "shadow");
1884 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1886 zebra_flush_reg(zh);
1888 resultSetInvalidate(zh);
1890 zebra_register_close(zh->service, zh->reg);
1893 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1894 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1895 zh->records_processed, zh->records_inserted,
1896 zh->records_updated, zh->records_deleted);
1898 status->processed = zh->records_processed;
1899 status->inserted = zh->records_inserted;
1900 status->updated = zh->records_updated;
1901 status->deleted = zh->records_deleted;
1903 zebra_get_state(zh, &val, &seqno);
1906 BFiles bfs = bfs_create(rval, zh->path_reg);
1907 bf_commitClean(bfs, rval);
1912 zebra_set_state(zh, 'o', seqno);
1913 zebra_unlock(zh->lock_shadow);
1914 zebra_unlock(zh->lock_normal);
1917 #if HAVE_SYS_TIMES_H
1919 yaz_log(log_level, "user/system: %ld/%ld",
1920 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1921 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1923 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1924 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1929 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1931 return zebra_repository_index(zh, path, action_update);
1934 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1936 return zebra_repository_index(zh, path, action_delete);
1939 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
1940 enum zebra_recctrl_action_t action)
1945 if (action == action_update)
1946 yaz_log(log_level, "updating %s", path);
1947 else if (action == action_delete)
1948 yaz_log(log_level, "deleting %s", path);
1949 else if (action == action_a_delete)
1950 yaz_log(log_level, "attempt deleting %s", path);
1952 yaz_log(log_level, "update action=%d", (int) action);
1954 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
1955 return zebra_update_file_match(zh, path);
1957 return zebra_update_from_path(zh, path, action);
1960 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1964 yaz_log(log_level, "zebra_repository_show");
1965 repositoryShow(zh, path);
1969 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1975 ZEBRA_RES res = ZEBRA_OK;
1979 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
1980 zebra_select_default_database(zh);
1983 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1986 rval = res_get(zh->res, "shadow");
1989 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
1993 zebra_lock_w(zh->lock_normal);
1994 zebra_lock_r(zh->lock_shadow);
1996 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
1999 zebra_unlock(zh->lock_shadow);
2000 zebra_unlock(zh->lock_normal);
2003 zebra_get_state(zh, &val, &seqno);
2007 /* shadow area is dirty and so we must throw it away */
2008 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2011 else if (val == 'c')
2013 /* commit has started. We can not remove it anymore */
2018 bf_cache(bfs, rval);
2019 if (bf_commitExists(bfs))
2022 zebra_set_state(zh, 'd', seqno);
2025 zebra_set_state(zh, 'c', seqno);
2027 yaz_log(log_level, "commit start");
2028 if (bf_commitExec(bfs))
2031 if (res == ZEBRA_OK)
2034 zebra_set_state(zh, 'o', seqno);
2036 zebra_unlock(zh->lock_shadow);
2037 zebra_unlock(zh->lock_normal);
2039 zebra_lock_w(zh->lock_shadow);
2040 bf_commitClean(bfs, rval);
2041 zebra_unlock(zh->lock_shadow);
2045 zebra_unlock(zh->lock_shadow);
2046 zebra_unlock(zh->lock_normal);
2047 yaz_log(YLOG_WARN, "zebra_commit: failed");
2052 zebra_unlock(zh->lock_shadow);
2053 zebra_unlock(zh->lock_normal);
2054 yaz_log(log_level, "nothing to commit");
2061 ZEBRA_RES zebra_clean(ZebraHandle zh)
2063 yaz_log(log_level, "zebra_clean");
2064 ZEBRA_CHECK_HANDLE(zh);
2065 return zebra_commit_ex(zh, 1);
2068 ZEBRA_RES zebra_commit(ZebraHandle zh)
2070 yaz_log(log_level, "zebra_commit");
2071 ZEBRA_CHECK_HANDLE(zh);
2072 return zebra_commit_ex(zh, 0);
2076 ZEBRA_RES zebra_init(ZebraHandle zh)
2081 yaz_log(log_level, "zebra_init");
2083 ZEBRA_CHECK_HANDLE(zh);
2085 zebra_select_default_database(zh);
2088 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2089 "cannot select default database");
2092 rval = res_get(zh->res, "shadow");
2094 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2097 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2101 bf_cache(bfs, rval);
2105 zebra_set_state(zh, 'o', 0);
2109 ZEBRA_RES zebra_compact(ZebraHandle zh)
2113 yaz_log(log_level, "zebra_compact");
2114 ZEBRA_CHECK_HANDLE(zh);
2117 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2120 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2126 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2128 yaz_log(log_level, "zebra_result");
2131 *code = zh->errCode;
2132 *addinfo = zh->errString;
2136 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2137 *addinfo ="ZebraHandle is NULL";
2141 void zebra_shadow_enable(ZebraHandle zh, int value)
2144 yaz_log(log_level, "zebra_shadow_enable");
2145 zh->shadow_enable = value;
2148 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2150 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2151 ZEBRA_CHECK_HANDLE(zh);
2154 if (zh->iconv_to_utf8 != 0)
2155 yaz_iconv_close(zh->iconv_to_utf8);
2156 if (zh->iconv_from_utf8 != 0)
2157 yaz_iconv_close(zh->iconv_from_utf8);
2160 yaz_iconv_open("UTF-8", encoding);
2161 if (zh->iconv_to_utf8 == 0)
2162 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2163 zh->iconv_from_utf8 =
2164 yaz_iconv_open(encoding, "UTF-8");
2165 if (zh->iconv_to_utf8 == 0)
2166 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2171 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2173 yaz_log(log_level, "zebra_record_encoding");
2174 ZEBRA_CHECK_HANDLE(zh);
2175 xfree(zh->record_encoding);
2176 zh->record_encoding = 0;
2178 zh->record_encoding = xstrdup(encoding);
2182 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2186 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2188 res_set(zh->res, name, value);
2191 const char *zebra_get_resource(ZebraHandle zh,
2192 const char *name, const char *defaultvalue)
2197 v = res_get_def(zh->res, name,(char *)defaultvalue);
2198 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2202 /* moved from zebra_api_ext.c by pop */
2203 /* FIXME: Should this really be public??? -Heikki */
2205 int zebra_trans_no(ZebraHandle zh)
2207 yaz_log(log_level, "zebra_trans_no");
2209 return zh->trans_no;
2212 int zebra_get_shadow_enable(ZebraHandle zh)
2214 yaz_log(log_level, "zebra_get_shadow_enable");
2216 return zh->shadow_enable;
2219 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2221 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2223 zh->shadow_enable = value;
2226 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2227 const char *buf, int buf_size)
2229 return zebra_update_record(zh, action_update,
2230 0 /* record type */,
2237 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2238 enum zebra_recctrl_action_t action,
2239 const char *recordType,
2240 zint *sysno, const char *match,
2242 const char *buf, int buf_size)
2246 ZEBRA_CHECK_HANDLE(zh);
2250 yaz_log(log_level, "zebra_update_record");
2252 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2255 buf_size = strlen(buf);
2257 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2259 res = zebra_buffer_extract_record(zh, buf, buf_size,
2266 if (zebra_end_trans(zh) != ZEBRA_OK)
2268 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2274 /* ---------------------------------------------------------------------------
2278 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2279 const char *setname, zint *hits)
2282 ZEBRA_RES res = ZEBRA_OK;
2287 ZEBRA_CHECK_HANDLE(zh);
2289 odr = odr_createmem(ODR_ENCODE);
2294 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2296 query = p_query_rpn(odr, pqf_query);
2300 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2301 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2305 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2309 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2317 /* ---------------------------------------------------------------------------
2318 Sort - a simplified interface, with optional read locks.
2320 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2321 const char *sort_spec,
2322 const char *output_setname,
2323 const char **input_setnames)
2325 int num_input_setnames = 0;
2326 int sort_status = 0;
2327 Z_SortKeySpecList *sort_sequence;
2329 ZEBRA_CHECK_HANDLE(zh);
2332 assert(output_setname);
2333 assert(input_setnames);
2334 sort_sequence = yaz_sort_spec(stream, sort_spec);
2335 yaz_log(log_level, "sort (FIXME) ");
2338 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2339 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2343 /* we can do this, since the perl typemap code for char** will
2344 put a NULL at the end of list */
2345 while (input_setnames[num_input_setnames]) num_input_setnames++;
2347 if (zebra_begin_read(zh))
2350 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2351 output_setname, sort_sequence, &sort_status);
2357 /* ---------------------------------------------------------------------------
2358 Get BFS for Zebra system (to make alternative storage methods)
2360 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2363 return zh->reg->bfs;
2368 /* ---------------------------------------------------------------------------
2369 Set limit for search/scan
2371 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2373 ZEBRA_CHECK_HANDLE(zh);
2374 zebra_limit_destroy(zh->m_limit);
2375 zh->m_limit = zebra_limit_create(complement_flag, ids);
2380 Set Error code + addinfo
2382 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2387 nmem_reset(zh->nmem_error);
2388 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2391 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2394 sprintf(vstr, ZINT_FORMAT, i);
2397 nmem_reset(zh->nmem_error);
2398 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2401 void zebra_lock_prefix(Res res, char *path)
2403 const char *lock_dir = res_get_def(res, "lockDir", "");
2405 strcpy(path, lock_dir);
2406 if (*path && path[strlen(path)-1] != '/')
2413 * indent-tabs-mode: nil
2415 * vim: shiftwidth=4 tabstop=8 expandtab