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 zebra_maps_define_default_sort(reg->zebra_maps);
432 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
434 yaz_log(YLOG_WARN, "rec_open failed");
439 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
441 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
443 yaz_log(YLOG_WARN, "dict_open failed");
448 if (res_get_match(res, "sortindex", "f", "f"))
449 sort_type = ZEBRA_SORT_TYPE_FLAT;
450 else if (res_get_match(res, "sortindex", "i", "f"))
451 sort_type = ZEBRA_SORT_TYPE_ISAMB;
452 else if (res_get_match(res, "sortindex", "m", "f"))
453 sort_type = ZEBRA_SORT_TYPE_MULTI;
456 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
461 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
463 yaz_log(YLOG_WARN, "zebra_sort_open failed");
466 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
468 struct ISAMS_M_s isams_m;
469 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
470 key_isams_m(res, &isams_m))))
472 yaz_log(YLOG_WARN, "isams_open failed");
476 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
478 struct ISAMC_M_s isamc_m;
479 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
480 rw, key_isamc_m(res, &isamc_m))))
482 yaz_log(YLOG_WARN, "isamc_open failed");
486 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
488 struct ISAMC_M_s isamc_m;
490 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
491 rw, key_isamc_m(res, &isamc_m), 0)))
493 yaz_log(YLOG_WARN, "isamb_open failed");
497 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
499 struct ISAMC_M_s isamc_m;
501 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
502 rw, key_isamc_m(res, &isamc_m), 1)))
504 yaz_log(YLOG_WARN, "isamb_open failed");
508 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
510 struct ISAMC_M_s isamc_m;
512 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
513 rw, key_isamc_m(res, &isamc_m), -1)))
515 yaz_log(YLOG_WARN, "isamb_open failed");
521 reg->zei = zebraExplain_open(reg->records, reg->dh,
523 zebra_extract_explain);
526 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
533 zebra_register_close(zs, reg);
536 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
540 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
542 ZEBRA_CHECK_HANDLE(zh);
543 yaz_log(log_level, "zebra_admin_shutdown");
545 zebra_mutex_cond_lock(&zh->service->session_lock);
546 zh->service->stop_flag = 1;
547 zebra_mutex_cond_unlock(&zh->service->session_lock);
551 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
554 ZEBRA_CHECK_HANDLE(zh);
555 yaz_log(log_level, "zebra_admin_start");
557 zebra_mutex_cond_lock(&zs->session_lock);
558 zebra_mutex_cond_unlock(&zs->session_lock);
562 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
566 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
570 zebraExplain_close(reg->zei);
571 dict_close(reg->dict);
573 dict_close(reg->matchDict);
574 zebra_sort_close(reg->sort_index);
576 isams_close(reg->isams);
578 isamc_close(reg->isamc);
580 isamb_close(reg->isamb);
581 rec_close(®->records);
583 recTypes_destroy(reg->recTypes);
584 zebra_maps_close(reg->zebra_maps);
585 zebraRankDestroy(reg);
586 bfs_destroy(reg->bfs);
587 data1_destroy(reg->dh);
589 zebra_rec_keys_close(reg->keys);
590 zebra_rec_keys_close(reg->sortKeys);
592 key_block_destroy(®->key_block);
597 ZEBRA_RES zebra_stop(ZebraService zs)
603 zebra_close(zs->sessions);
606 zebra_mutex_cond_destroy(&zs->session_lock);
609 passwd_db_close(zs->passwd_db);
611 recTypeClass_destroy(zs->record_classes);
612 nmem_destroy(zs->nmem);
613 res_close(zs->global_res);
615 yaz_timing_stop(zs->timing);
616 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
617 yaz_timing_get_real(zs->timing),
618 yaz_timing_get_user(zs->timing),
619 yaz_timing_get_sys(zs->timing));
622 yaz_timing_destroy(&zs->timing);
627 ZEBRA_RES zebra_close(ZebraHandle zh)
630 struct zebra_session **sp;
633 yaz_log(log_level, "zebra_close");
634 ZEBRA_CHECK_HANDLE(zh);
639 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
640 resultSetDestroy(zh, -1, 0, 0);
643 zebra_register_close(zh->service, zh->reg);
645 res_close(zh->session_res);
647 xfree(zh->record_encoding);
649 xfree(zh->dbaccesslist);
651 for (i = 0; i < zh->num_basenames; i++)
652 xfree(zh->basenames[i]);
653 xfree(zh->basenames);
655 if (zh->iconv_to_utf8 != 0)
656 yaz_iconv_close(zh->iconv_to_utf8);
657 if (zh->iconv_from_utf8 != 0)
658 yaz_iconv_close(zh->iconv_from_utf8);
660 zebra_mutex_cond_lock(&zs->session_lock);
661 zebra_lock_destroy(zh->lock_normal);
662 zebra_lock_destroy(zh->lock_shadow);
674 zebra_mutex_cond_unlock(&zs->session_lock);
676 xfree(zh->user_perm);
677 zh->service = 0; /* more likely to trigger an assert */
679 zebra_limit_destroy(zh->m_limit);
681 nmem_destroy(zh->nmem_error);
688 struct map_baseinfo {
694 char **new_basenames;
698 static void zebra_open_res(ZebraHandle zh)
706 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
707 zh->res = res_open(zh->session_res, 0);
708 res_read_file(zh->res, fname);
710 else if (*zh->reg_name == 0)
712 zh->res = res_open(zh->session_res, 0);
716 yaz_log(YLOG_WARN, "no register root specified");
717 zh->res = 0; /* no path for register - fail! */
721 static void zebra_close_res(ZebraHandle zh)
729 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
733 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
737 assert(zh->reg == 0);
738 assert(*zh->reg_name == 0);
744 resultSetInvalidate(zh);
745 zebra_register_close(zh->service, zh->reg);
751 zh->reg_name = xstrdup(new_reg);
755 if (zh->service->path_root)
757 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
758 strlen(zh->reg_name) + 3);
759 strcpy(zh->path_reg, zh->service->path_root);
762 strcat(zh->path_reg, "/");
763 strcat(zh->path_reg, zh->reg_name);
769 zebra_lock_destroy(zh->lock_normal);
773 zebra_lock_destroy(zh->lock_shadow);
779 const char *lock_area = res_get(zh->res, "lockDir");
781 if (!lock_area && zh->path_reg)
782 res_set(zh->res, "lockDir", zh->path_reg);
783 sprintf(fname, "norm.%s.LCK", zh->reg_name);
785 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
787 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
789 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
791 if (!zh->lock_normal || !zh->lock_shadow)
795 zebra_lock_destroy(zh->lock_normal);
800 zebra_lock_destroy(zh->lock_shadow);
809 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
810 zebra_set_approx_limit(zh, approx);
814 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
815 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
819 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
822 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
823 zh->m_segment_indexing);
828 void map_basenames_func(void *vp, const char *name, const char *value)
830 struct map_baseinfo *p = (struct map_baseinfo *) vp;
832 char fromdb[128], todb[8][128];
839 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
840 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
841 todb[5], todb[6], todb[7]);
845 for (i = 0; i<p->num_bases; i++)
846 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
849 for (i = 0; i < no; i++)
851 if (p->new_num_bases == p->new_num_max)
853 p->new_basenames[(p->new_num_bases)++] =
854 nmem_strdup(p->mem, todb[i]);
860 int zebra_select_default_database(ZebraHandle zh)
864 /* no database has been selected - so we select based on
865 resource setting (including group)
867 const char *group = res_get(zh->session_res, "group");
868 const char *v = res_get_prefix(zh->session_res,
869 "database", group, "Default");
870 return zebra_select_database(zh, v);
875 void map_basenames(ZebraHandle zh, ODR stream,
876 int *num_bases, char ***basenames)
878 struct map_baseinfo info;
879 struct map_baseinfo *p = &info;
882 yaz_log(log_level, "map_basenames ");
887 info.num_bases = *num_bases;
888 info.basenames = *basenames;
889 info.new_num_max = 128;
890 info.new_num_bases = 0;
891 info.new_basenames = (char **)
892 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
893 info.mem = stream->mem;
895 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
897 for (i = 0; i<p->num_bases; i++)
898 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
900 p->new_basenames[(p->new_num_bases)++] =
901 nmem_strdup(p->mem, p->basenames[i]);
903 *num_bases = info.new_num_bases;
904 *basenames = info.new_basenames;
905 for (i = 0; i<*num_bases; i++)
906 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
909 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
911 ZEBRA_CHECK_HANDLE(zh);
913 yaz_log(log_level, "zebra_select_database %s",basename);
915 return zebra_select_databases(zh, 1, &basename);
918 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
919 const char **basenames)
926 ZEBRA_CHECK_HANDLE(zh);
929 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
930 num_bases,basenames[0]);
935 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
939 /* Check if the user has access to all databases (Seb) */
940 /* You could argue that this should happen later, after we have
941 * determined that the database(s) exist. */
942 if (zh->dbaccesslist) {
943 for (i = 0; i < num_bases; i++) {
944 const char *db = basenames[i];
946 for (p = zh->dbaccesslist; p && *p; p = pp) {
948 if ((pp = strchr(p, '+'))) {
954 if (len == strlen(db) && !strncmp(db, p, len))
958 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
964 for (i = 0; i < zh->num_basenames; i++)
965 xfree(zh->basenames[i]);
966 xfree(zh->basenames);
968 zh->num_basenames = num_bases;
969 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
970 for (i = 0; i < zh->num_basenames; i++)
971 zh->basenames[i] = xstrdup(basenames[i]);
973 cp = strrchr(basenames[0], '/');
976 len = cp - basenames[0];
977 new_reg = xmalloc(len + 1);
978 memcpy(new_reg, basenames[0], len);
982 new_reg = xstrdup("");
983 for (i = 1; i<num_bases; i++)
987 cp1 = strrchr(basenames[i], '/');
992 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
995 if (len != cp1 - basenames[i] ||
996 memcmp(basenames[i], new_reg, len))
998 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1006 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1011 zebra_select_register(zh, new_reg);
1015 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1018 if (!zh->lock_normal || !zh->lock_shadow)
1020 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1026 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1028 if (approx_limit == 0)
1029 approx_limit = DEFAULT_APPROX_LIMIT;
1030 zh->approx_limit = approx_limit;
1034 void zebra_set_partial_result(ZebraHandle zh)
1036 zh->partial_result = 1;
1040 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1041 int (*f)(void *client_data),
1044 zh->break_handler_func = f;
1045 zh->break_handler_data = client_data;
1049 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1050 const char *setname, zint *hits,
1051 int *estimated_hit_count,
1052 int *partial_resultset)
1056 ZEBRA_CHECK_HANDLE(zh);
1062 yaz_log(log_level, "zebra_search_rpn");
1064 zh->partial_result = 0;
1066 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1069 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1070 zh->num_basenames, zh->basenames, setname,
1071 hits, estimated_hit_count);
1073 *partial_resultset = zh->partial_result;
1078 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1079 const char *setname, zint *hits)
1081 int estimated_hit_count;
1082 int partial_resultset;
1083 return zebra_search_RPN_x(zh, o, query, setname, hits,
1084 &estimated_hit_count,
1085 &partial_resultset);
1088 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1089 const char *setname,
1090 Z_RecordComposition *comp,
1091 const Odr_oid *input_format, int num_recs,
1092 ZebraRetrievalRecord *recs)
1094 ZebraMetaRecord *poset;
1096 ZEBRA_RES ret = ZEBRA_OK;
1099 ZEBRA_CHECK_HANDLE(zh);
1105 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1109 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1114 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1117 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1118 for (i = 0; i<num_recs; i++)
1119 pos_array[i] = recs[i].position;
1120 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1123 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1124 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1130 WRBUF addinfo_w = wrbuf_alloc();
1131 for (i = 0; i < num_recs; i++)
1133 recs[i].errCode = 0;
1134 recs[i].errString = 0;
1139 recs[i].sysno = poset[i].sysno;
1142 recs[i].format = yaz_oid_recsyn_sutrs;
1143 recs[i].len = strlen(poset[i].term);
1144 recs[i].buf = poset[i].term;
1145 recs[i].base = poset[i].db;
1147 else if (poset[i].sysno)
1151 zebra_snippets *hit_snippet = zebra_snippets_create();
1153 /* we disable hit snippets for now. It does not work well
1154 and it slows retrieval down a lot */
1156 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1159 wrbuf_rewind(addinfo_w);
1161 zebra_record_fetch(zh, setname,
1162 poset[i].sysno, poset[i].score,
1163 stream, input_format, comp,
1164 &recs[i].format, &buf, &len,
1165 &recs[i].base, addinfo_w);
1167 if (wrbuf_len(addinfo_w))
1169 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1173 recs[i].buf = (char*) odr_malloc(stream, len);
1174 memcpy(recs[i].buf, buf, len);
1178 recs[i].score = poset[i].score;
1179 zebra_snippets_destroy(hit_snippet);
1183 /* only need to set it once */
1184 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1186 zebra_setError_zint(zh,
1187 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1194 zebra_meta_records_destroy(zh, poset, num_recs);
1195 wrbuf_destroy(addinfo_w);
1202 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1204 int *num_entries, ZebraScanEntry **entries,
1206 const char *setname)
1208 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1209 Z_AttributesPlusTerm *zapt;
1210 Odr_oid *attributeSet;
1213 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1216 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1220 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1221 position, num_entries, entries, is_partial,
1224 yaz_pqf_destroy(pqf_parser);
1228 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1229 const Odr_oid *attributeset,
1231 int *num_entries, ZebraScanEntry **entries,
1233 const char *setname)
1237 ZEBRA_CHECK_HANDLE(zh);
1242 assert(num_entries);
1245 yaz_log(log_level, "zebra_scan");
1247 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1254 res = rpn_scan(zh, stream, zapt, attributeset,
1255 zh->num_basenames, zh->basenames, position,
1256 num_entries, entries, is_partial, setname);
1261 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1262 int num_input_setnames, const char **input_setnames,
1263 const char *output_setname,
1264 Z_SortKeySpecList *sort_sequence,
1268 ZEBRA_CHECK_HANDLE(zh);
1270 assert(num_input_setnames>0);
1271 assert(input_setnames);
1272 assert(sort_sequence);
1273 assert(sort_status);
1274 yaz_log(log_level, "zebra_sort");
1276 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1278 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1279 output_setname, sort_sequence, sort_status);
1284 int zebra_deleteResultSet(ZebraHandle zh, int function,
1285 int num_setnames, char **setnames,
1290 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1292 if (zebra_begin_read(zh))
1293 return Z_DeleteStatus_systemProblemAtTarget;
1296 case Z_DeleteResultSetRequest_list:
1297 assert(num_setnames>0);
1299 resultSetDestroy(zh, num_setnames, setnames, statuses);
1301 case Z_DeleteResultSetRequest_all:
1302 resultSetDestroy(zh, -1, 0, statuses);
1306 status = Z_DeleteStatus_success;
1307 for (i = 0; i<num_setnames; i++)
1308 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1309 status = statuses[i];
1313 int zebra_errCode(ZebraHandle zh)
1317 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1320 yaz_log(log_level, "zebra_errCode: o");
1324 const char *zebra_errString(ZebraHandle zh)
1328 e= diagbib1_str(zh->errCode);
1329 yaz_log(log_level, "zebra_errString: %s",e);
1333 char *zebra_errAdd(ZebraHandle zh)
1338 yaz_log(log_level, "zebra_errAdd: %s",a);
1342 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1345 const char *astring;
1349 ZEBRA_CHECK_HANDLE(zh);
1353 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1354 p = res_get(zs->global_res, u);
1355 xfree(zh->user_perm);
1356 zh->user_perm = xstrdup(p ? p : "r");
1358 /* Determine database access list */
1359 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1361 zh->dbaccesslist = xstrdup(astring);
1363 zh->dbaccesslist = 0;
1365 /* users that don't require a password .. */
1366 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1369 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1374 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1375 const char *record_type)
1377 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1378 database, record_type);
1379 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1381 return zebra_begin_trans(zh, 1);
1384 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1386 ZEBRA_CHECK_HANDLE(zh);
1387 yaz_log(log_level, "zebra_admin_import_end");
1388 return zebra_end_trans(zh);
1391 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1393 ZEBRA_RES res = ZEBRA_OK;
1396 ZEBRA_CHECK_HANDLE(zh);
1397 yaz_log(log_level, "zebra_admin_import_segment");
1399 for (i = 0; i<segment->num_segmentRecords; i++)
1401 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1403 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1405 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1406 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1408 Odr_oct *oct = fragment->u.notExternallyTagged;
1411 if(zebra_update_record(
1414 0, /* record Type */
1418 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1426 int delete_w_handle(const char *info, void *handle)
1428 ZebraHandle zh = (ZebraHandle) handle;
1432 if (*info == sizeof(pos))
1434 memcpy(&pos, info+1, sizeof(pos));
1435 isamb_unlink(zh->reg->isamb, pos);
1440 static int delete_SU_handle(void *handle, int ord)
1442 ZebraHandle zh = (ZebraHandle) handle;
1446 ord_len = key_SU_encode(ord, ord_buf);
1447 ord_buf[ord_len] = '\0';
1449 assert(zh->reg->isamb);
1450 dict_delete_subtree(zh->reg->dict, ord_buf,
1451 zh, delete_w_handle);
1455 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1457 ZEBRA_RES ret = ZEBRA_OK;
1459 yaz_log(log_level, "zebra_drop_database %s", db);
1460 ZEBRA_CHECK_HANDLE(zh);
1462 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1464 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1469 if (zebraExplain_curDatabase(zh->reg->zei, db))
1471 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1476 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1477 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1478 0 /* handle */, 0 /* func */);
1479 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1480 zebraExplain_removeDatabase(zh->reg->zei, zh);
1481 zebra_remove_file_match(zh);
1486 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1487 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1488 "drop database only supported for isam:b");
1491 if (zebra_end_trans(zh) != ZEBRA_OK)
1493 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1499 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1501 yaz_log(log_level, "zebra_create_database %s", db);
1502 ZEBRA_CHECK_HANDLE(zh);
1505 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1507 if (zebra_begin_trans(zh, 1))
1510 /* announce database */
1511 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1512 /* explainDatabase */))
1514 if (zebra_end_trans(zh) != ZEBRA_OK)
1516 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1518 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1521 return zebra_end_trans(zh);
1524 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1525 const char *input_str, int input_len,
1526 char *output_str, int output_len)
1529 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1533 yaz_log(log_level, "zebra_string_norm ");
1535 if (!zh->reg->zebra_maps)
1537 wrbuf = zebra_replace(zm, "", input_str, input_len);
1540 if (wrbuf_len(wrbuf) >= output_len)
1542 if (wrbuf_len(wrbuf))
1543 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1544 output_str[wrbuf_len(wrbuf)] = '\0';
1545 return wrbuf_len(wrbuf);
1548 /** \brief set register state (state*.LCK)
1549 \param zh Zebra handle
1551 \param seqno sequence number
1554 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1556 c=commit (writing to register, reading from shadow, shadow mode only)
1558 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1560 char state_fname[256];
1565 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1567 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1568 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1569 f = fopen(fname, "w");
1571 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1572 fprintf(f, "%c %d %ld\n", val, seqno, p);
1577 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1579 char state_fname[256];
1584 yaz_log(log_level, "zebra_get_state ");
1586 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1587 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1588 f = fopen(fname, "r");
1594 fscanf(f, "%c %d", val, seqno);
1600 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1602 return zebra_begin_trans(zh, 0);
1605 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1607 return zebra_end_trans(zh);
1610 static void read_res_for_transaction(ZebraHandle zh)
1612 const char *group = res_get(zh->res, "group");
1614 /* FIXME - do we still use groups ?? */
1616 zh->m_group = group;
1617 v = res_get_prefix(zh->res, "followLinks", group, "1");
1618 zh->m_follow_links = atoi(v);
1620 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1621 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1623 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1624 zh->m_store_keys = atoi(v);
1626 v = res_get_prefix(zh->res, "storeData", group, "1");
1627 zh->m_store_data = atoi(v);
1629 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1630 zh->m_explain_database = atoi(v);
1632 v = res_get_prefix(zh->res, "openRW", group, "1");
1633 zh->m_flag_rw = atoi(v);
1635 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1636 zh->m_file_verbose_limit = atoi(v);
1639 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1641 ZEBRA_CHECK_HANDLE(zh);
1642 zebra_select_default_database(zh);
1645 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1646 "zebra_begin_trans: no database selected");
1650 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1654 if (rw && !strchr(zh->user_perm, 'w'))
1658 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1669 const char *rval = 0;
1674 read_res_for_transaction(zh);
1677 if (zh->trans_no != 1)
1679 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1680 "zebra_begin_trans: no write trans within read");
1685 resultSetInvalidate(zh);
1686 zebra_register_close(zh->service, zh->reg);
1688 zh->trans_w_no = zh->trans_no;
1690 zh->records_inserted = 0;
1691 zh->records_updated = 0;
1692 zh->records_deleted = 0;
1693 zh->records_processed = 0;
1694 zh->records_skipped = 0;
1696 #if HAVE_SYS_TIMES_H
1700 if (zh->shadow_enable)
1701 rval = res_get(zh->res, "shadow");
1705 zebra_lock_r(zh->lock_normal);
1706 zebra_lock_w(zh->lock_shadow);
1710 zebra_lock_w(zh->lock_normal);
1711 zebra_lock_w(zh->lock_shadow);
1713 zebra_get_state(zh, &val, &seqno);
1716 /* either we didn't finish commit or shadow is dirty */
1719 yaz_log(YLOG_WARN, "previous transaction did not finish "
1720 "(shadow disabled)");
1722 zebra_unlock(zh->lock_shadow);
1723 zebra_unlock(zh->lock_normal);
1724 if (zebra_commit(zh))
1732 zebra_lock_r(zh->lock_normal);
1733 zebra_lock_w(zh->lock_shadow);
1737 zebra_lock_w(zh->lock_normal);
1738 zebra_lock_w(zh->lock_shadow);
1742 zebra_set_state(zh, 'd', seqno);
1744 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1745 1, rval ? 1 : 0, zh->res,
1748 zh->reg->seqno = seqno;
1751 zebra_set_state(zh, 'o', seqno);
1753 zebra_unlock(zh->lock_shadow);
1754 zebra_unlock(zh->lock_normal);
1759 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1760 "zebra_begin_trans: cannot open register");
1761 yaz_log(YLOG_FATAL, "%s", zh->errString);
1764 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1774 if (zh->trans_no != 1)
1776 return zebra_flush_reg(zh);
1778 #if HAVE_SYS_TIMES_H
1784 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1787 if (!zh->lock_normal || !zh->lock_shadow)
1790 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1793 zebra_get_state(zh, &val, &seqno);
1799 else if (seqno != zh->reg->seqno)
1801 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1802 seqno, zh->reg->seqno);
1805 else if (zh->reg->last_val != val)
1807 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1808 val, zh->reg->last_val);
1815 zebra_lock_r(zh->lock_shadow);
1817 zebra_lock_r(zh->lock_normal);
1821 resultSetInvalidate(zh);
1822 zebra_register_close(zh->service, zh->reg);
1824 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1825 0, val == 'c' ? 1 : 0,
1826 zh->res, zh->path_reg);
1829 zebra_unlock(zh->lock_normal);
1830 zebra_unlock(zh->lock_shadow);
1832 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1835 zh->reg->last_val = val;
1836 zh->reg->seqno = seqno;
1838 read_res_for_transaction(zh);
1842 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1844 ZebraTransactionStatus dummy;
1846 yaz_log(log_level, "zebra_end_trans");
1847 ZEBRA_CHECK_HANDLE(zh);
1848 return zebra_end_transaction(zh, &dummy);
1851 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1857 ZEBRA_CHECK_HANDLE(zh);
1860 yaz_log(log_level, "zebra_end_transaction");
1862 status->processed = 0;
1863 status->inserted = 0;
1864 status->updated = 0;
1865 status->deleted = 0;
1869 if (!zh->res || !zh->reg)
1871 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1872 "zebra_end_trans: no open transaction");
1875 if (zh->trans_no != zh->trans_w_no)
1878 if (zh->trans_no != 0)
1881 /* release read lock */
1883 zebra_unlock(zh->lock_normal);
1884 zebra_unlock(zh->lock_shadow);
1887 { /* release write lock */
1891 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1892 rval = res_get(zh->res, "shadow");
1894 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1896 zebra_flush_reg(zh);
1898 resultSetInvalidate(zh);
1900 zebra_register_close(zh->service, zh->reg);
1903 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1904 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1905 zh->records_processed, zh->records_inserted,
1906 zh->records_updated, zh->records_deleted);
1908 status->processed = zh->records_processed;
1909 status->inserted = zh->records_inserted;
1910 status->updated = zh->records_updated;
1911 status->deleted = zh->records_deleted;
1913 zebra_get_state(zh, &val, &seqno);
1916 BFiles bfs = bfs_create(rval, zh->path_reg);
1917 bf_commitClean(bfs, rval);
1922 zebra_set_state(zh, 'o', seqno);
1923 zebra_unlock(zh->lock_shadow);
1924 zebra_unlock(zh->lock_normal);
1927 #if HAVE_SYS_TIMES_H
1929 yaz_log(log_level, "user/system: %ld/%ld",
1930 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1931 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1933 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1934 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1939 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1941 return zebra_repository_index(zh, path, action_update);
1944 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1946 return zebra_repository_index(zh, path, action_delete);
1949 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
1950 enum zebra_recctrl_action_t action)
1955 if (action == action_update)
1956 yaz_log(log_level, "updating %s", path);
1957 else if (action == action_delete)
1958 yaz_log(log_level, "deleting %s", path);
1959 else if (action == action_a_delete)
1960 yaz_log(log_level, "attempt deleting %s", path);
1962 yaz_log(log_level, "update action=%d", (int) action);
1964 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
1965 return zebra_update_file_match(zh, path);
1967 return zebra_update_from_path(zh, path, action);
1970 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1974 yaz_log(log_level, "zebra_repository_show");
1975 repositoryShow(zh, path);
1979 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1985 ZEBRA_RES res = ZEBRA_OK;
1989 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
1990 zebra_select_default_database(zh);
1993 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1996 rval = res_get(zh->res, "shadow");
1999 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2003 zebra_lock_w(zh->lock_normal);
2004 zebra_lock_r(zh->lock_shadow);
2006 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2009 zebra_unlock(zh->lock_shadow);
2010 zebra_unlock(zh->lock_normal);
2013 zebra_get_state(zh, &val, &seqno);
2017 /* shadow area is dirty and so we must throw it away */
2018 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2021 else if (val == 'c')
2023 /* commit has started. We can not remove it anymore */
2028 bf_cache(bfs, rval);
2029 if (bf_commitExists(bfs))
2032 zebra_set_state(zh, 'd', seqno);
2035 zebra_set_state(zh, 'c', seqno);
2037 yaz_log(log_level, "commit start");
2038 if (bf_commitExec(bfs))
2041 if (res == ZEBRA_OK)
2044 zebra_set_state(zh, 'o', seqno);
2046 zebra_unlock(zh->lock_shadow);
2047 zebra_unlock(zh->lock_normal);
2049 zebra_lock_w(zh->lock_shadow);
2050 bf_commitClean(bfs, rval);
2051 zebra_unlock(zh->lock_shadow);
2055 zebra_unlock(zh->lock_shadow);
2056 zebra_unlock(zh->lock_normal);
2057 yaz_log(YLOG_WARN, "zebra_commit: failed");
2062 zebra_unlock(zh->lock_shadow);
2063 zebra_unlock(zh->lock_normal);
2064 yaz_log(log_level, "nothing to commit");
2071 ZEBRA_RES zebra_clean(ZebraHandle zh)
2073 yaz_log(log_level, "zebra_clean");
2074 ZEBRA_CHECK_HANDLE(zh);
2075 return zebra_commit_ex(zh, 1);
2078 ZEBRA_RES zebra_commit(ZebraHandle zh)
2080 yaz_log(log_level, "zebra_commit");
2081 ZEBRA_CHECK_HANDLE(zh);
2082 return zebra_commit_ex(zh, 0);
2086 ZEBRA_RES zebra_init(ZebraHandle zh)
2091 yaz_log(log_level, "zebra_init");
2093 ZEBRA_CHECK_HANDLE(zh);
2095 zebra_select_default_database(zh);
2098 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2099 "cannot select default database");
2102 rval = res_get(zh->res, "shadow");
2104 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2107 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2111 bf_cache(bfs, rval);
2115 zebra_set_state(zh, 'o', 0);
2119 ZEBRA_RES zebra_compact(ZebraHandle zh)
2123 yaz_log(log_level, "zebra_compact");
2124 ZEBRA_CHECK_HANDLE(zh);
2127 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2130 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2136 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2138 yaz_log(log_level, "zebra_result");
2141 *code = zh->errCode;
2142 *addinfo = zh->errString;
2146 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2147 *addinfo ="ZebraHandle is NULL";
2151 void zebra_shadow_enable(ZebraHandle zh, int value)
2154 yaz_log(log_level, "zebra_shadow_enable");
2155 zh->shadow_enable = value;
2158 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2160 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2161 ZEBRA_CHECK_HANDLE(zh);
2164 if (zh->iconv_to_utf8 != 0)
2165 yaz_iconv_close(zh->iconv_to_utf8);
2166 if (zh->iconv_from_utf8 != 0)
2167 yaz_iconv_close(zh->iconv_from_utf8);
2170 yaz_iconv_open("UTF-8", encoding);
2171 if (zh->iconv_to_utf8 == 0)
2172 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2173 zh->iconv_from_utf8 =
2174 yaz_iconv_open(encoding, "UTF-8");
2175 if (zh->iconv_to_utf8 == 0)
2176 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2181 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2183 yaz_log(log_level, "zebra_record_encoding");
2184 ZEBRA_CHECK_HANDLE(zh);
2185 xfree(zh->record_encoding);
2186 zh->record_encoding = 0;
2188 zh->record_encoding = xstrdup(encoding);
2192 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2196 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2198 res_set(zh->res, name, value);
2201 const char *zebra_get_resource(ZebraHandle zh,
2202 const char *name, const char *defaultvalue)
2207 v = res_get_def(zh->res, name,(char *)defaultvalue);
2208 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2212 /* moved from zebra_api_ext.c by pop */
2213 /* FIXME: Should this really be public??? -Heikki */
2215 int zebra_trans_no(ZebraHandle zh)
2217 yaz_log(log_level, "zebra_trans_no");
2219 return zh->trans_no;
2222 int zebra_get_shadow_enable(ZebraHandle zh)
2224 yaz_log(log_level, "zebra_get_shadow_enable");
2226 return zh->shadow_enable;
2229 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2231 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2233 zh->shadow_enable = value;
2236 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2237 const char *buf, int buf_size)
2239 return zebra_update_record(zh, action_update,
2240 0 /* record type */,
2247 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2248 enum zebra_recctrl_action_t action,
2249 const char *recordType,
2250 zint *sysno, const char *match,
2252 const char *buf, int buf_size)
2256 ZEBRA_CHECK_HANDLE(zh);
2260 yaz_log(log_level, "zebra_update_record");
2262 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2265 buf_size = strlen(buf);
2267 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2269 res = zebra_buffer_extract_record(zh, buf, buf_size,
2276 if (zebra_end_trans(zh) != ZEBRA_OK)
2278 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2284 /* ---------------------------------------------------------------------------
2288 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2289 const char *setname, zint *hits)
2292 ZEBRA_RES res = ZEBRA_OK;
2297 ZEBRA_CHECK_HANDLE(zh);
2299 odr = odr_createmem(ODR_ENCODE);
2304 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2306 query = p_query_rpn(odr, pqf_query);
2310 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2311 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2315 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2319 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2327 /* ---------------------------------------------------------------------------
2328 Sort - a simplified interface, with optional read locks.
2330 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2331 const char *sort_spec,
2332 const char *output_setname,
2333 const char **input_setnames)
2335 int num_input_setnames = 0;
2336 int sort_status = 0;
2337 Z_SortKeySpecList *sort_sequence;
2339 ZEBRA_CHECK_HANDLE(zh);
2342 assert(output_setname);
2343 assert(input_setnames);
2344 sort_sequence = yaz_sort_spec(stream, sort_spec);
2345 yaz_log(log_level, "sort (FIXME) ");
2348 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2349 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2353 /* we can do this, since the perl typemap code for char** will
2354 put a NULL at the end of list */
2355 while (input_setnames[num_input_setnames]) num_input_setnames++;
2357 if (zebra_begin_read(zh))
2360 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2361 output_setname, sort_sequence, &sort_status);
2367 /* ---------------------------------------------------------------------------
2368 Get BFS for Zebra system (to make alternative storage methods)
2370 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2373 return zh->reg->bfs;
2378 /* ---------------------------------------------------------------------------
2379 Set limit for search/scan
2381 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2383 ZEBRA_CHECK_HANDLE(zh);
2384 zebra_limit_destroy(zh->m_limit);
2385 zh->m_limit = zebra_limit_create(complement_flag, ids);
2390 Set Error code + addinfo
2392 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2397 nmem_reset(zh->nmem_error);
2398 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2401 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2404 sprintf(vstr, ZINT_FORMAT, i);
2407 nmem_reset(zh->nmem_error);
2408 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2411 void zebra_lock_prefix(Res res, char *path)
2413 const char *lock_dir = res_get_def(res, "lockDir", "");
2415 strcpy(path, lock_dir);
2416 if (*path && path[strlen(path)-1] != '/')
2423 * indent-tabs-mode: nil
2425 * vim: shiftwidth=4 tabstop=8 expandtab