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 WRBUF addinfo_w = wrbuf_alloc();
1127 for (i = 0; i < num_recs; i++)
1129 recs[i].errCode = 0;
1130 recs[i].errString = 0;
1135 recs[i].sysno = poset[i].term;
1138 recs[i].format = yaz_oid_recsyn_sutrs;
1139 recs[i].len = strlen(poset[i].term);
1140 recs[i].buf = poset[i].term;
1141 recs[i].base = poset[i].db;
1143 else if (poset[i].sysno)
1147 zebra_snippets *hit_snippet = zebra_snippets_create();
1149 /* we disable hit snippets for now. It does not work well
1150 and it slows retrieval down a lot */
1152 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1155 wrbuf_rewind(addinfo_w);
1157 zebra_record_fetch(zh, setname,
1158 poset[i].sysno, poset[i].score,
1159 stream, input_format, comp,
1160 &recs[i].format, &buf, &len,
1161 &recs[i].base, addinfo_w);
1163 if (wrbuf_len(addinfo_w))
1165 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1169 recs[i].buf = (char*) odr_malloc(stream, len);
1170 memcpy(recs[i].buf, buf, len);
1174 recs[i].score = poset[i].score;
1175 zebra_snippets_destroy(hit_snippet);
1179 /* only need to set it once */
1180 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1182 zebra_setError_zint(zh,
1183 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1190 zebra_meta_records_destroy(zh, poset, num_recs);
1191 wrbuf_destroy(addinfo_w);
1198 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1200 int *num_entries, ZebraScanEntry **entries,
1202 const char *setname)
1204 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1205 Z_AttributesPlusTerm *zapt;
1206 Odr_oid *attributeSet;
1209 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1212 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1216 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1217 position, num_entries, entries, is_partial,
1220 yaz_pqf_destroy(pqf_parser);
1224 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1225 const Odr_oid *attributeset,
1227 int *num_entries, ZebraScanEntry **entries,
1229 const char *setname)
1233 ZEBRA_CHECK_HANDLE(zh);
1238 assert(num_entries);
1241 yaz_log(log_level, "zebra_scan");
1243 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1250 res = rpn_scan(zh, stream, zapt, attributeset,
1251 zh->num_basenames, zh->basenames, position,
1252 num_entries, entries, is_partial, setname);
1257 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1258 int num_input_setnames, const char **input_setnames,
1259 const char *output_setname,
1260 Z_SortKeySpecList *sort_sequence,
1264 ZEBRA_CHECK_HANDLE(zh);
1266 assert(num_input_setnames>0);
1267 assert(input_setnames);
1268 assert(sort_sequence);
1269 assert(sort_status);
1270 yaz_log(log_level, "zebra_sort");
1272 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1274 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1275 output_setname, sort_sequence, sort_status);
1280 int zebra_deleteResultSet(ZebraHandle zh, int function,
1281 int num_setnames, char **setnames,
1286 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1288 if (zebra_begin_read(zh))
1289 return Z_DeleteStatus_systemProblemAtTarget;
1292 case Z_DeleteResultSetRequest_list:
1293 assert(num_setnames>0);
1295 resultSetDestroy(zh, num_setnames, setnames, statuses);
1297 case Z_DeleteResultSetRequest_all:
1298 resultSetDestroy(zh, -1, 0, statuses);
1302 status = Z_DeleteStatus_success;
1303 for (i = 0; i<num_setnames; i++)
1304 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1305 status = statuses[i];
1309 int zebra_errCode(ZebraHandle zh)
1313 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1316 yaz_log(log_level, "zebra_errCode: o");
1320 const char *zebra_errString(ZebraHandle zh)
1324 e= diagbib1_str(zh->errCode);
1325 yaz_log(log_level, "zebra_errString: %s",e);
1329 char *zebra_errAdd(ZebraHandle zh)
1334 yaz_log(log_level, "zebra_errAdd: %s",a);
1338 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1341 const char *astring;
1345 ZEBRA_CHECK_HANDLE(zh);
1349 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1350 p = res_get(zs->global_res, u);
1351 xfree(zh->user_perm);
1352 zh->user_perm = xstrdup(p ? p : "r");
1354 /* Determine database access list */
1355 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1357 zh->dbaccesslist = xstrdup(astring);
1359 zh->dbaccesslist = 0;
1361 /* users that don't require a password .. */
1362 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1365 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1370 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1371 const char *record_type)
1373 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1374 database, record_type);
1375 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1377 return zebra_begin_trans(zh, 1);
1380 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1382 ZEBRA_CHECK_HANDLE(zh);
1383 yaz_log(log_level, "zebra_admin_import_end");
1384 return zebra_end_trans(zh);
1387 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1389 ZEBRA_RES res = ZEBRA_OK;
1392 ZEBRA_CHECK_HANDLE(zh);
1393 yaz_log(log_level, "zebra_admin_import_segment");
1395 for (i = 0; i<segment->num_segmentRecords; i++)
1397 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1399 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1401 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1402 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1404 Odr_oct *oct = fragment->u.notExternallyTagged;
1407 if(zebra_update_record(
1410 0, /* record Type */
1414 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1422 int delete_w_handle(const char *info, void *handle)
1424 ZebraHandle zh = (ZebraHandle) handle;
1428 if (*info == sizeof(pos))
1430 memcpy(&pos, info+1, sizeof(pos));
1431 isamb_unlink(zh->reg->isamb, pos);
1436 static int delete_SU_handle(void *handle, int ord)
1438 ZebraHandle zh = (ZebraHandle) handle;
1442 ord_len = key_SU_encode(ord, ord_buf);
1443 ord_buf[ord_len] = '\0';
1445 assert(zh->reg->isamb);
1446 dict_delete_subtree(zh->reg->dict, ord_buf,
1447 zh, delete_w_handle);
1451 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1453 ZEBRA_RES ret = ZEBRA_OK;
1455 yaz_log(log_level, "zebra_drop_database %s", db);
1456 ZEBRA_CHECK_HANDLE(zh);
1458 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1460 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1465 if (zebraExplain_curDatabase(zh->reg->zei, db))
1467 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1472 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1473 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1474 0 /* handle */, 0 /* func */);
1475 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1476 zebraExplain_removeDatabase(zh->reg->zei, zh);
1477 zebra_remove_file_match(zh);
1482 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1483 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1484 "drop database only supported for isam:b");
1487 if (zebra_end_trans(zh) != ZEBRA_OK)
1489 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1495 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1497 yaz_log(log_level, "zebra_create_database %s", db);
1498 ZEBRA_CHECK_HANDLE(zh);
1501 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1503 if (zebra_begin_trans(zh, 1))
1506 /* announce database */
1507 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1508 /* explainDatabase */))
1510 if (zebra_end_trans(zh) != ZEBRA_OK)
1512 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1514 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1517 return zebra_end_trans(zh);
1520 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1521 const char *input_str, int input_len,
1522 char *output_str, int output_len)
1525 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1529 yaz_log(log_level, "zebra_string_norm ");
1531 if (!zh->reg->zebra_maps)
1533 wrbuf = zebra_replace(zm, "", input_str, input_len);
1536 if (wrbuf_len(wrbuf) >= output_len)
1538 if (wrbuf_len(wrbuf))
1539 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1540 output_str[wrbuf_len(wrbuf)] = '\0';
1541 return wrbuf_len(wrbuf);
1544 /** \brief set register state (state*.LCK)
1545 \param zh Zebra handle
1547 \param seqno sequence number
1550 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1552 c=commit (writing to register, reading from shadow, shadow mode only)
1554 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1556 char state_fname[256];
1561 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1563 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1564 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1565 f = fopen(fname, "w");
1567 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1568 fprintf(f, "%c %d %ld\n", val, seqno, p);
1573 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1575 char state_fname[256];
1580 yaz_log(log_level, "zebra_get_state ");
1582 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1583 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1584 f = fopen(fname, "r");
1590 fscanf(f, "%c %d", val, seqno);
1596 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1598 return zebra_begin_trans(zh, 0);
1601 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1603 return zebra_end_trans(zh);
1606 static void read_res_for_transaction(ZebraHandle zh)
1608 const char *group = res_get(zh->res, "group");
1610 /* FIXME - do we still use groups ?? */
1612 zh->m_group = group;
1613 v = res_get_prefix(zh->res, "followLinks", group, "1");
1614 zh->m_follow_links = atoi(v);
1616 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1617 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1619 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1620 zh->m_store_keys = atoi(v);
1622 v = res_get_prefix(zh->res, "storeData", group, "1");
1623 zh->m_store_data = atoi(v);
1625 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1626 zh->m_explain_database = atoi(v);
1628 v = res_get_prefix(zh->res, "openRW", group, "1");
1629 zh->m_flag_rw = atoi(v);
1631 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1632 zh->m_file_verbose_limit = atoi(v);
1635 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1637 ZEBRA_CHECK_HANDLE(zh);
1638 zebra_select_default_database(zh);
1641 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1642 "zebra_begin_trans: no database selected");
1646 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1650 if (rw && !strchr(zh->user_perm, 'w'))
1654 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1665 const char *rval = 0;
1670 read_res_for_transaction(zh);
1673 if (zh->trans_no != 1)
1675 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1676 "zebra_begin_trans: no write trans within read");
1681 resultSetInvalidate(zh);
1682 zebra_register_close(zh->service, zh->reg);
1684 zh->trans_w_no = zh->trans_no;
1686 zh->records_inserted = 0;
1687 zh->records_updated = 0;
1688 zh->records_deleted = 0;
1689 zh->records_processed = 0;
1690 zh->records_skipped = 0;
1692 #if HAVE_SYS_TIMES_H
1696 if (zh->shadow_enable)
1697 rval = res_get(zh->res, "shadow");
1701 zebra_lock_r(zh->lock_normal);
1702 zebra_lock_w(zh->lock_shadow);
1706 zebra_lock_w(zh->lock_normal);
1707 zebra_lock_w(zh->lock_shadow);
1709 zebra_get_state(zh, &val, &seqno);
1712 /* either we didn't finish commit or shadow is dirty */
1715 yaz_log(YLOG_WARN, "previous transaction did not finish "
1716 "(shadow disabled)");
1718 zebra_unlock(zh->lock_shadow);
1719 zebra_unlock(zh->lock_normal);
1720 if (zebra_commit(zh))
1728 zebra_lock_r(zh->lock_normal);
1729 zebra_lock_w(zh->lock_shadow);
1733 zebra_lock_w(zh->lock_normal);
1734 zebra_lock_w(zh->lock_shadow);
1738 zebra_set_state(zh, 'd', seqno);
1740 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1741 1, rval ? 1 : 0, zh->res,
1744 zh->reg->seqno = seqno;
1747 zebra_set_state(zh, 'o', seqno);
1749 zebra_unlock(zh->lock_shadow);
1750 zebra_unlock(zh->lock_normal);
1755 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1756 "zebra_begin_trans: cannot open register");
1757 yaz_log(YLOG_FATAL, "%s", zh->errString);
1760 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1770 if (zh->trans_no != 1)
1772 return zebra_flush_reg(zh);
1774 #if HAVE_SYS_TIMES_H
1780 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1783 if (!zh->lock_normal || !zh->lock_shadow)
1786 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1789 zebra_get_state(zh, &val, &seqno);
1795 else if (seqno != zh->reg->seqno)
1797 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1798 seqno, zh->reg->seqno);
1801 else if (zh->reg->last_val != val)
1803 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1804 val, zh->reg->last_val);
1811 zebra_lock_r(zh->lock_shadow);
1813 zebra_lock_r(zh->lock_normal);
1817 resultSetInvalidate(zh);
1818 zebra_register_close(zh->service, zh->reg);
1820 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1821 0, val == 'c' ? 1 : 0,
1822 zh->res, zh->path_reg);
1825 zebra_unlock(zh->lock_normal);
1826 zebra_unlock(zh->lock_shadow);
1828 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1831 zh->reg->last_val = val;
1832 zh->reg->seqno = seqno;
1834 read_res_for_transaction(zh);
1838 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1840 ZebraTransactionStatus dummy;
1842 yaz_log(log_level, "zebra_end_trans");
1843 ZEBRA_CHECK_HANDLE(zh);
1844 return zebra_end_transaction(zh, &dummy);
1847 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1853 ZEBRA_CHECK_HANDLE(zh);
1856 yaz_log(log_level, "zebra_end_transaction");
1858 status->processed = 0;
1859 status->inserted = 0;
1860 status->updated = 0;
1861 status->deleted = 0;
1865 if (!zh->res || !zh->reg)
1867 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1868 "zebra_end_trans: no open transaction");
1871 if (zh->trans_no != zh->trans_w_no)
1874 if (zh->trans_no != 0)
1877 /* release read lock */
1879 zebra_unlock(zh->lock_normal);
1880 zebra_unlock(zh->lock_shadow);
1883 { /* release write lock */
1887 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1888 rval = res_get(zh->res, "shadow");
1890 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1892 zebra_flush_reg(zh);
1894 resultSetInvalidate(zh);
1896 zebra_register_close(zh->service, zh->reg);
1899 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1900 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1901 zh->records_processed, zh->records_inserted,
1902 zh->records_updated, zh->records_deleted);
1904 status->processed = zh->records_processed;
1905 status->inserted = zh->records_inserted;
1906 status->updated = zh->records_updated;
1907 status->deleted = zh->records_deleted;
1909 zebra_get_state(zh, &val, &seqno);
1912 BFiles bfs = bfs_create(rval, zh->path_reg);
1913 bf_commitClean(bfs, rval);
1918 zebra_set_state(zh, 'o', seqno);
1919 zebra_unlock(zh->lock_shadow);
1920 zebra_unlock(zh->lock_normal);
1923 #if HAVE_SYS_TIMES_H
1925 yaz_log(log_level, "user/system: %ld/%ld",
1926 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1927 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1929 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1930 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1935 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1937 return zebra_repository_index(zh, path, action_update);
1940 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1942 return zebra_repository_index(zh, path, action_delete);
1945 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
1946 enum zebra_recctrl_action_t action)
1951 if (action == action_update)
1952 yaz_log(log_level, "updating %s", path);
1953 else if (action == action_delete)
1954 yaz_log(log_level, "deleting %s", path);
1955 else if (action == action_a_delete)
1956 yaz_log(log_level, "attempt deleting %s", path);
1958 yaz_log(log_level, "update action=%d", (int) action);
1960 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
1961 return zebra_update_file_match(zh, path);
1963 return zebra_update_from_path(zh, path, action);
1966 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1970 yaz_log(log_level, "zebra_repository_show");
1971 repositoryShow(zh, path);
1975 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1981 ZEBRA_RES res = ZEBRA_OK;
1985 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
1986 zebra_select_default_database(zh);
1989 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1992 rval = res_get(zh->res, "shadow");
1995 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
1999 zebra_lock_w(zh->lock_normal);
2000 zebra_lock_r(zh->lock_shadow);
2002 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2005 zebra_unlock(zh->lock_shadow);
2006 zebra_unlock(zh->lock_normal);
2009 zebra_get_state(zh, &val, &seqno);
2013 /* shadow area is dirty and so we must throw it away */
2014 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2017 else if (val == 'c')
2019 /* commit has started. We can not remove it anymore */
2024 bf_cache(bfs, rval);
2025 if (bf_commitExists(bfs))
2028 zebra_set_state(zh, 'd', seqno);
2031 zebra_set_state(zh, 'c', seqno);
2033 yaz_log(log_level, "commit start");
2034 if (bf_commitExec(bfs))
2037 if (res == ZEBRA_OK)
2040 zebra_set_state(zh, 'o', seqno);
2042 zebra_unlock(zh->lock_shadow);
2043 zebra_unlock(zh->lock_normal);
2045 zebra_lock_w(zh->lock_shadow);
2046 bf_commitClean(bfs, rval);
2047 zebra_unlock(zh->lock_shadow);
2051 zebra_unlock(zh->lock_shadow);
2052 zebra_unlock(zh->lock_normal);
2053 yaz_log(YLOG_WARN, "zebra_commit: failed");
2058 zebra_unlock(zh->lock_shadow);
2059 zebra_unlock(zh->lock_normal);
2060 yaz_log(log_level, "nothing to commit");
2067 ZEBRA_RES zebra_clean(ZebraHandle zh)
2069 yaz_log(log_level, "zebra_clean");
2070 ZEBRA_CHECK_HANDLE(zh);
2071 return zebra_commit_ex(zh, 1);
2074 ZEBRA_RES zebra_commit(ZebraHandle zh)
2076 yaz_log(log_level, "zebra_commit");
2077 ZEBRA_CHECK_HANDLE(zh);
2078 return zebra_commit_ex(zh, 0);
2082 ZEBRA_RES zebra_init(ZebraHandle zh)
2087 yaz_log(log_level, "zebra_init");
2089 ZEBRA_CHECK_HANDLE(zh);
2091 zebra_select_default_database(zh);
2094 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2095 "cannot select default database");
2098 rval = res_get(zh->res, "shadow");
2100 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2103 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2107 bf_cache(bfs, rval);
2111 zebra_set_state(zh, 'o', 0);
2115 ZEBRA_RES zebra_compact(ZebraHandle zh)
2119 yaz_log(log_level, "zebra_compact");
2120 ZEBRA_CHECK_HANDLE(zh);
2123 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2126 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2132 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2134 yaz_log(log_level, "zebra_result");
2137 *code = zh->errCode;
2138 *addinfo = zh->errString;
2142 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2143 *addinfo ="ZebraHandle is NULL";
2147 void zebra_shadow_enable(ZebraHandle zh, int value)
2150 yaz_log(log_level, "zebra_shadow_enable");
2151 zh->shadow_enable = value;
2154 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2156 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2157 ZEBRA_CHECK_HANDLE(zh);
2160 if (zh->iconv_to_utf8 != 0)
2161 yaz_iconv_close(zh->iconv_to_utf8);
2162 if (zh->iconv_from_utf8 != 0)
2163 yaz_iconv_close(zh->iconv_from_utf8);
2166 yaz_iconv_open("UTF-8", encoding);
2167 if (zh->iconv_to_utf8 == 0)
2168 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2169 zh->iconv_from_utf8 =
2170 yaz_iconv_open(encoding, "UTF-8");
2171 if (zh->iconv_to_utf8 == 0)
2172 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2177 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2179 yaz_log(log_level, "zebra_record_encoding");
2180 ZEBRA_CHECK_HANDLE(zh);
2181 xfree(zh->record_encoding);
2182 zh->record_encoding = 0;
2184 zh->record_encoding = xstrdup(encoding);
2188 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2192 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2194 res_set(zh->res, name, value);
2197 const char *zebra_get_resource(ZebraHandle zh,
2198 const char *name, const char *defaultvalue)
2203 v = res_get_def(zh->res, name,(char *)defaultvalue);
2204 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2208 /* moved from zebra_api_ext.c by pop */
2209 /* FIXME: Should this really be public??? -Heikki */
2211 int zebra_trans_no(ZebraHandle zh)
2213 yaz_log(log_level, "zebra_trans_no");
2215 return zh->trans_no;
2218 int zebra_get_shadow_enable(ZebraHandle zh)
2220 yaz_log(log_level, "zebra_get_shadow_enable");
2222 return zh->shadow_enable;
2225 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2227 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2229 zh->shadow_enable = value;
2232 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2233 const char *buf, int buf_size)
2235 return zebra_update_record(zh, action_update,
2236 0 /* record type */,
2243 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2244 enum zebra_recctrl_action_t action,
2245 const char *recordType,
2246 zint *sysno, const char *match,
2248 const char *buf, int buf_size)
2252 ZEBRA_CHECK_HANDLE(zh);
2256 yaz_log(log_level, "zebra_update_record");
2258 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2261 buf_size = strlen(buf);
2263 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2265 res = zebra_buffer_extract_record(zh, buf, buf_size,
2272 if (zebra_end_trans(zh) != ZEBRA_OK)
2274 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2280 /* ---------------------------------------------------------------------------
2284 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2285 const char *setname, zint *hits)
2288 ZEBRA_RES res = ZEBRA_OK;
2293 ZEBRA_CHECK_HANDLE(zh);
2295 odr = odr_createmem(ODR_ENCODE);
2300 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2302 query = p_query_rpn(odr, pqf_query);
2306 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2307 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2311 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2315 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2323 /* ---------------------------------------------------------------------------
2324 Sort - a simplified interface, with optional read locks.
2326 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2327 const char *sort_spec,
2328 const char *output_setname,
2329 const char **input_setnames)
2331 int num_input_setnames = 0;
2332 int sort_status = 0;
2333 Z_SortKeySpecList *sort_sequence;
2335 ZEBRA_CHECK_HANDLE(zh);
2338 assert(output_setname);
2339 assert(input_setnames);
2340 sort_sequence = yaz_sort_spec(stream, sort_spec);
2341 yaz_log(log_level, "sort (FIXME) ");
2344 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2345 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2349 /* we can do this, since the perl typemap code for char** will
2350 put a NULL at the end of list */
2351 while (input_setnames[num_input_setnames]) num_input_setnames++;
2353 if (zebra_begin_read(zh))
2356 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2357 output_setname, sort_sequence, &sort_status);
2363 /* ---------------------------------------------------------------------------
2364 Get BFS for Zebra system (to make alternative storage methods)
2366 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2369 return zh->reg->bfs;
2374 /* ---------------------------------------------------------------------------
2375 Set limit for search/scan
2377 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2379 ZEBRA_CHECK_HANDLE(zh);
2380 zebra_limit_destroy(zh->m_limit);
2381 zh->m_limit = zebra_limit_create(complement_flag, ids);
2386 Set Error code + addinfo
2388 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2393 nmem_reset(zh->nmem_error);
2394 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2397 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2400 sprintf(vstr, ZINT_FORMAT, i);
2403 nmem_reset(zh->nmem_error);
2404 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2407 void zebra_lock_prefix(Res res, char *path)
2409 const char *lock_dir = res_get_def(res, "lockDir", "");
2411 strcpy(path, lock_dir);
2412 if (*path && path[strlen(path)-1] != '/')
2419 * indent-tabs-mode: nil
2421 * vim: shiftwidth=4 tabstop=8 expandtab