1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 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 int zebra_chdir(ZebraService zs)
69 yaz_log(log_level, "zebra_chdir");
70 dir = res_get(zs->global_res, "chdir");
73 yaz_log(YLOG_DEBUG, "chdir %s", dir);
80 yaz_log(YLOG_FATAL|YLOG_ERRNO, "chdir %s", dir);
84 static ZEBRA_RES zebra_flush_reg(ZebraHandle zh)
86 ZEBRA_CHECK_HANDLE(zh);
87 yaz_log(log_level, "zebra_flush_reg");
88 zebraExplain_flush(zh->reg->zei, zh);
90 key_block_flush(zh->reg->key_block, 1);
92 zebra_index_merge(zh);
96 static struct zebra_register *zebra_register_open(ZebraService zs,
98 int rw, int useshadow,
100 const char *reg_path);
101 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
103 const char *zebra_get_encoding(ZebraHandle zh)
105 assert(zh && zh->session_res);
106 return res_get_def(zh->session_res, "encoding", "ISO-8859-1");
109 ZebraHandle zebra_open(ZebraService zs, Res res)
112 const char *default_encoding;
113 if (!log_level_initialized)
115 log_level = yaz_log_module_level("zebraapi");
116 log_level_initialized = 1;
119 yaz_log(log_level, "zebra_open");
124 zh = (ZebraHandle) xmalloc(sizeof(*zh));
125 yaz_log(YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
128 zh->reg = 0; /* no register attached yet */
134 zh->session_res = res_open(zs->global_res, res);
136 zh->dbaccesslist = 0;
138 zh->reg_name = xstrdup("");
140 zh->num_basenames = 0;
143 zh->approx_limit = DEFAULT_APPROX_LIMIT;
150 zh->shadow_enable = 1;
151 zh->m_staticrank = 0;
152 zh->m_segment_indexing = 0;
154 zh->break_handler_func = 0;
155 zh->break_handler_data = 0;
157 default_encoding = zebra_get_encoding(zh);
160 yaz_iconv_open("UTF-8", default_encoding);
161 if (zh->iconv_to_utf8 == 0)
162 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported",
164 zh->iconv_from_utf8 =
165 yaz_iconv_open(default_encoding, "UTF-8");
166 if (zh->iconv_to_utf8 == 0)
167 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported",
170 zh->record_encoding = 0;
172 zebra_mutex_cond_lock(&zs->session_lock);
174 zh->next = zs->sessions;
177 zebra_mutex_cond_unlock(&zs->session_lock);
179 zh->store_data_buf = 0;
181 zh->m_limit = zebra_limit_create(1, 0);
183 zh->nmem_error = nmem_create();
188 ZebraService zebra_start(const char *configName)
190 return zebra_start_res(configName, 0, 0);
193 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
196 char version_str[16];
201 if (!log_level_initialized)
203 log_level = yaz_log_module_level("zebraapi");
204 log_level_initialized = 1;
209 zebra_get_version(version_str, system_str);
211 yaz_log(YLOG_LOG, "zebra_start %s %s", version_str, system_str);
213 yaz_log(YLOG_LOG, "config %s", configName);
215 if ((res = res_open(def_res, over_res)))
217 const char *passwd_plain = 0;
218 const char *passwd_encrypt = 0;
219 const char *dbaccess = 0;
224 ZEBRA_RES ret = res_read_file(res, configName);
230 if (zebra_check_res(res))
232 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
239 zebra_check_res(res);
242 zh = xmalloc(sizeof(*zh));
243 zh->global_res = res;
252 zebra_mutex_cond_init(&zh->session_lock);
253 passwd_plain = res_get(zh->global_res, "passwd");
254 passwd_encrypt = res_get(zh->global_res, "passwd.c");
255 dbaccess = res_get(zh->global_res, "dbaccess");
257 if (!passwd_plain && !passwd_encrypt)
258 zh->passwd_db = NULL;
261 zh->passwd_db = passwd_db_open();
263 yaz_log(YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
267 passwd_db_file_plain(zh->passwd_db, passwd_plain);
269 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
276 zh->dbaccess = res_open(NULL, NULL);
277 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
278 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
283 zh->timing = yaz_timing_create();
284 zh->path_root = res_get(zh->global_res, "root");
285 zh->nmem = nmem_create();
286 zh->record_classes = recTypeClass_create(zh->global_res, zh->nmem);
290 const char *module_path = res_get(res, "modulePath");
292 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
300 void zebra_filter_info(ZebraService zs, void *cd,
301 void(*cb)(void *cd, const char *name))
305 recTypeClass_info(zs->record_classes, cd, cb);
308 void zebra_pidfname(ZebraService zs, char *path)
311 zebra_lock_prefix(zs->global_res, path);
312 strcat(path, "zebrasrv.pid");
315 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
316 int compact_flag, Res res)
318 int page_size = 4096;
319 char resource_str[200];
320 sprintf(resource_str, "dict.%.100s.pagesize", name);
324 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
325 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
327 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
331 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
332 int rw, int useshadow, Res res,
333 const char *reg_path)
335 struct zebra_register *reg;
336 int record_compression = REC_COMPRESS_NONE;
337 const char *recordCompression = 0;
338 const char *profilePath;
339 int sort_type = ZEBRA_SORT_TYPE_FLAT;
340 ZEBRA_RES ret = ZEBRA_OK;
344 reg = xmalloc(sizeof(*reg));
347 reg->name = xstrdup(name);
354 yaz_log(YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
355 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
357 reg->dh = data1_create();
364 reg->bfs = bfs_create(res_get(res, "register"), reg_path);
367 data1_destroy(reg->dh);
374 if (bf_cache(reg->bfs, res_get(res, "shadow")) == ZEBRA_FAIL)
376 bfs_destroy(reg->bfs);
377 data1_destroy(reg->dh);
384 profilePath = res_get_def(res, "profilePath", 0);
386 data1_set_tabpath(reg->dh, profilePath);
387 data1_set_tabroot(reg->dh, reg_path);
388 reg->recTypes = recTypes_init(zs->record_classes, reg->dh);
391 zebra_maps_open(res, reg_path, profilePath);
392 if (!reg->zebra_maps)
394 recTypes_destroy(reg->recTypes);
395 bfs_destroy(reg->bfs);
396 data1_destroy(reg->dh);
401 reg->rank_classes = NULL;
404 reg->keys = zebra_rec_keys_open();
406 reg->sortKeys = zebra_rec_keys_open();
417 /* installing rank classes */
418 zebraRankInstall(reg, rank_1_class);
419 zebraRankInstall(reg, rank_2_class);
420 zebraRankInstall(reg, rank_similarity_class);
421 zebraRankInstall(reg, rank_static_class);
423 recordCompression = res_get_def(res, "recordCompression", "none");
424 if (!strcmp(recordCompression, "none"))
425 record_compression = REC_COMPRESS_NONE;
426 if (!strcmp(recordCompression, "bzip2"))
427 record_compression = REC_COMPRESS_BZIP2;
430 const char *index_fname = res_get_def(res, "index", "default.idx");
431 if (index_fname && *index_fname && strcmp(index_fname, "none"))
433 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
438 zebra_maps_define_default_sort(reg->zebra_maps);
442 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
444 yaz_log(YLOG_WARN, "rec_open failed");
449 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
451 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
453 yaz_log(YLOG_WARN, "dict_open failed");
458 if (res_get_match(res, "sortindex", "f", "f"))
459 sort_type = ZEBRA_SORT_TYPE_FLAT;
460 else if (res_get_match(res, "sortindex", "i", "f"))
461 sort_type = ZEBRA_SORT_TYPE_ISAMB;
462 else if (res_get_match(res, "sortindex", "m", "f"))
463 sort_type = ZEBRA_SORT_TYPE_MULTI;
466 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
471 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
473 yaz_log(YLOG_WARN, "zebra_sort_open failed");
476 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
478 struct ISAMS_M_s isams_m;
479 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
480 key_isams_m(res, &isams_m))))
482 yaz_log(YLOG_WARN, "isams_open failed");
486 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
488 struct ISAMC_M_s isamc_m;
489 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
490 rw, key_isamc_m(res, &isamc_m))))
492 yaz_log(YLOG_WARN, "isamc_open failed");
496 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
498 struct ISAMC_M_s isamc_m;
500 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
501 rw, key_isamc_m(res, &isamc_m), 0)))
503 yaz_log(YLOG_WARN, "isamb_open failed");
507 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
509 struct ISAMC_M_s isamc_m;
511 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
512 rw, key_isamc_m(res, &isamc_m), 1)))
514 yaz_log(YLOG_WARN, "isamb_open failed");
518 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
520 struct ISAMC_M_s isamc_m;
522 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
523 rw, key_isamc_m(res, &isamc_m), -1)))
525 yaz_log(YLOG_WARN, "isamb_open failed");
531 reg->zei = zebraExplain_open(reg->records, reg->dh,
533 zebra_extract_explain);
536 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
543 zebra_register_close(zs, reg);
546 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
550 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
552 ZEBRA_CHECK_HANDLE(zh);
553 yaz_log(log_level, "zebra_admin_shutdown");
555 zebra_mutex_cond_lock(&zh->service->session_lock);
556 zh->service->stop_flag = 1;
557 zebra_mutex_cond_unlock(&zh->service->session_lock);
561 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
564 ZEBRA_CHECK_HANDLE(zh);
565 yaz_log(log_level, "zebra_admin_start");
567 zebra_mutex_cond_lock(&zs->session_lock);
568 zebra_mutex_cond_unlock(&zs->session_lock);
572 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
576 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
580 zebraExplain_close(reg->zei);
581 dict_close(reg->dict);
583 dict_close(reg->matchDict);
584 zebra_sort_close(reg->sort_index);
586 isams_close(reg->isams);
588 isamc_close(reg->isamc);
590 isamb_close(reg->isamb);
591 rec_close(®->records);
593 recTypes_destroy(reg->recTypes);
594 zebra_maps_close(reg->zebra_maps);
595 zebraRankDestroy(reg);
596 bfs_destroy(reg->bfs);
597 data1_destroy(reg->dh);
599 zebra_rec_keys_close(reg->keys);
600 zebra_rec_keys_close(reg->sortKeys);
602 key_block_destroy(®->key_block);
607 ZEBRA_RES zebra_stop(ZebraService zs)
613 zebra_close(zs->sessions);
616 zebra_mutex_cond_destroy(&zs->session_lock);
619 passwd_db_close(zs->passwd_db);
621 recTypeClass_destroy(zs->record_classes);
622 nmem_destroy(zs->nmem);
623 res_close(zs->global_res);
625 yaz_timing_stop(zs->timing);
626 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
627 yaz_timing_get_real(zs->timing),
628 yaz_timing_get_user(zs->timing),
629 yaz_timing_get_sys(zs->timing));
632 yaz_timing_destroy(&zs->timing);
637 ZEBRA_RES zebra_close(ZebraHandle zh)
640 struct zebra_session **sp;
643 yaz_log(log_level, "zebra_close");
644 ZEBRA_CHECK_HANDLE(zh);
649 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
650 resultSetDestroy(zh, -1, 0, 0);
653 zebra_register_close(zh->service, zh->reg);
655 res_close(zh->session_res);
657 xfree(zh->record_encoding);
659 xfree(zh->dbaccesslist);
661 for (i = 0; i < zh->num_basenames; i++)
662 xfree(zh->basenames[i]);
663 xfree(zh->basenames);
665 if (zh->iconv_to_utf8 != 0)
666 yaz_iconv_close(zh->iconv_to_utf8);
667 if (zh->iconv_from_utf8 != 0)
668 yaz_iconv_close(zh->iconv_from_utf8);
670 zebra_mutex_cond_lock(&zs->session_lock);
671 zebra_lock_destroy(zh->lock_normal);
672 zebra_lock_destroy(zh->lock_shadow);
684 zebra_mutex_cond_unlock(&zs->session_lock);
686 xfree(zh->user_perm);
687 zh->service = 0; /* more likely to trigger an assert */
689 zebra_limit_destroy(zh->m_limit);
691 nmem_destroy(zh->nmem_error);
698 struct map_baseinfo {
704 char **new_basenames;
708 static void zebra_open_res(ZebraHandle zh)
716 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
717 zh->res = res_open(zh->session_res, 0);
718 res_read_file(zh->res, fname);
720 else if (*zh->reg_name == 0)
722 zh->res = res_open(zh->session_res, 0);
726 yaz_log(YLOG_WARN, "no register root specified");
727 zh->res = 0; /* no path for register - fail! */
731 static void zebra_close_res(ZebraHandle zh)
739 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
743 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
747 assert(zh->reg == 0);
748 assert(*zh->reg_name == 0);
754 resultSetInvalidate(zh);
755 zebra_register_close(zh->service, zh->reg);
761 zh->reg_name = xstrdup(new_reg);
765 if (zh->service->path_root)
767 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
768 strlen(zh->reg_name) + 3);
769 strcpy(zh->path_reg, zh->service->path_root);
772 strcat(zh->path_reg, "/");
773 strcat(zh->path_reg, zh->reg_name);
779 zebra_lock_destroy(zh->lock_normal);
783 zebra_lock_destroy(zh->lock_shadow);
789 const char *lock_area = res_get(zh->res, "lockDir");
791 if (!lock_area && zh->path_reg)
792 res_set(zh->res, "lockDir", zh->path_reg);
793 sprintf(fname, "norm.%s.LCK", zh->reg_name);
795 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
797 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
799 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
801 if (!zh->lock_normal || !zh->lock_shadow)
805 zebra_lock_destroy(zh->lock_normal);
810 zebra_lock_destroy(zh->lock_shadow);
819 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
820 zebra_set_approx_limit(zh, approx);
824 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
825 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
829 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
832 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
833 zh->m_segment_indexing);
838 void map_basenames_func(void *vp, const char *name, const char *value)
840 struct map_baseinfo *p = (struct map_baseinfo *) vp;
842 char fromdb[128], todb[8][128];
849 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
850 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
851 todb[5], todb[6], todb[7]);
855 for (i = 0; i<p->num_bases; i++)
856 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
859 for (i = 0; i < no; i++)
861 if (p->new_num_bases == p->new_num_max)
863 p->new_basenames[(p->new_num_bases)++] =
864 nmem_strdup(p->mem, todb[i]);
870 int zebra_select_default_database(ZebraHandle zh)
874 /* no database has been selected - so we select based on
875 resource setting (including group)
877 const char *group = res_get(zh->session_res, "group");
878 const char *v = res_get_prefix(zh->session_res,
879 "database", group, "Default");
880 return zebra_select_database(zh, v);
885 void map_basenames(ZebraHandle zh, ODR stream,
886 int *num_bases, char ***basenames)
888 struct map_baseinfo info;
889 struct map_baseinfo *p = &info;
892 yaz_log(log_level, "map_basenames ");
897 info.num_bases = *num_bases;
898 info.basenames = *basenames;
899 info.new_num_max = 128;
900 info.new_num_bases = 0;
901 info.new_basenames = (char **)
902 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
903 info.mem = stream->mem;
905 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
907 for (i = 0; i<p->num_bases; i++)
908 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
910 p->new_basenames[(p->new_num_bases)++] =
911 nmem_strdup(p->mem, p->basenames[i]);
913 *num_bases = info.new_num_bases;
914 *basenames = info.new_basenames;
915 for (i = 0; i<*num_bases; i++)
916 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
919 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
921 ZEBRA_CHECK_HANDLE(zh);
923 yaz_log(log_level, "zebra_select_database %s",basename);
925 return zebra_select_databases(zh, 1, &basename);
928 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
929 const char **basenames)
936 ZEBRA_CHECK_HANDLE(zh);
939 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
940 num_bases,basenames[0]);
945 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
949 /* Check if the user has access to all databases (Seb) */
950 /* You could argue that this should happen later, after we have
951 * determined that the database(s) exist. */
952 if (zh->dbaccesslist) {
953 for (i = 0; i < num_bases; i++) {
954 const char *db = basenames[i];
956 for (p = zh->dbaccesslist; p && *p; p = pp) {
958 if ((pp = strchr(p, '+'))) {
964 if (len == strlen(db) && !strncmp(db, p, len))
968 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
974 for (i = 0; i < zh->num_basenames; i++)
975 xfree(zh->basenames[i]);
976 xfree(zh->basenames);
978 zh->num_basenames = num_bases;
979 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
980 for (i = 0; i < zh->num_basenames; i++)
981 zh->basenames[i] = xstrdup(basenames[i]);
983 cp = strrchr(basenames[0], '/');
986 len = cp - basenames[0];
987 new_reg = xmalloc(len + 1);
988 memcpy(new_reg, basenames[0], len);
992 new_reg = xstrdup("");
993 for (i = 1; i<num_bases; i++)
997 cp1 = strrchr(basenames[i], '/');
1002 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1005 if (len != cp1 - basenames[i] ||
1006 memcmp(basenames[i], new_reg, len))
1008 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1016 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1021 zebra_select_register(zh, new_reg);
1025 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1028 if (!zh->lock_normal || !zh->lock_shadow)
1030 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1036 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1038 if (approx_limit == 0)
1039 approx_limit = DEFAULT_APPROX_LIMIT;
1040 zh->approx_limit = approx_limit;
1044 void zebra_set_partial_result(ZebraHandle zh)
1046 zh->partial_result = 1;
1050 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1051 int (*f)(void *client_data),
1054 zh->break_handler_func = f;
1055 zh->break_handler_data = client_data;
1059 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1060 const char *setname, zint *hits,
1061 int *estimated_hit_count,
1062 int *partial_resultset)
1066 ZEBRA_CHECK_HANDLE(zh);
1072 yaz_log(log_level, "zebra_search_rpn");
1074 zh->partial_result = 0;
1076 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1079 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1080 zh->num_basenames, zh->basenames, setname,
1081 hits, estimated_hit_count);
1083 *partial_resultset = zh->partial_result;
1088 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1089 const char *setname, zint *hits)
1091 int estimated_hit_count;
1092 int partial_resultset;
1093 return zebra_search_RPN_x(zh, o, query, setname, hits,
1094 &estimated_hit_count,
1095 &partial_resultset);
1098 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1099 const char *setname,
1100 Z_RecordComposition *comp,
1101 const Odr_oid *input_format, int num_recs,
1102 ZebraRetrievalRecord *recs)
1104 ZebraMetaRecord *poset;
1106 ZEBRA_RES ret = ZEBRA_OK;
1109 ZEBRA_CHECK_HANDLE(zh);
1115 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1119 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1124 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1127 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1128 for (i = 0; i<num_recs; i++)
1129 pos_array[i] = recs[i].position;
1130 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1133 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1134 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1140 WRBUF addinfo_w = wrbuf_alloc();
1141 for (i = 0; i < num_recs; i++)
1143 recs[i].errCode = 0;
1144 recs[i].errString = 0;
1149 recs[i].sysno = poset[i].sysno;
1152 recs[i].format = yaz_oid_recsyn_sutrs;
1153 recs[i].len = strlen(poset[i].term);
1154 recs[i].buf = poset[i].term;
1155 recs[i].base = poset[i].db;
1157 else if (poset[i].sysno)
1161 zebra_snippets *hit_snippet = zebra_snippets_create();
1163 /* we disable hit snippets for now. It does not work well
1164 and it slows retrieval down a lot */
1166 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1169 wrbuf_rewind(addinfo_w);
1171 zebra_record_fetch(zh, setname,
1172 poset[i].sysno, poset[i].score,
1173 stream, input_format, comp,
1174 &recs[i].format, &buf, &len,
1175 &recs[i].base, addinfo_w);
1177 if (wrbuf_len(addinfo_w))
1179 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1183 recs[i].buf = (char*) odr_malloc(stream, len);
1184 memcpy(recs[i].buf, buf, len);
1188 recs[i].score = poset[i].score;
1189 zebra_snippets_destroy(hit_snippet);
1193 /* only need to set it once */
1194 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1196 zebra_setError_zint(zh,
1197 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1204 zebra_meta_records_destroy(zh, poset, num_recs);
1205 wrbuf_destroy(addinfo_w);
1212 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1214 int *num_entries, ZebraScanEntry **entries,
1216 const char *setname)
1218 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1219 Z_AttributesPlusTerm *zapt;
1220 Odr_oid *attributeSet;
1223 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1226 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1230 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1231 position, num_entries, entries, is_partial,
1234 yaz_pqf_destroy(pqf_parser);
1238 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1239 const Odr_oid *attributeset,
1241 int *num_entries, ZebraScanEntry **entries,
1243 const char *setname)
1247 ZEBRA_CHECK_HANDLE(zh);
1252 assert(num_entries);
1255 yaz_log(log_level, "zebra_scan");
1257 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1264 res = rpn_scan(zh, stream, zapt, attributeset,
1265 zh->num_basenames, zh->basenames, position,
1266 num_entries, entries, is_partial, setname);
1271 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1272 int num_input_setnames, const char **input_setnames,
1273 const char *output_setname,
1274 Z_SortKeySpecList *sort_sequence,
1278 ZEBRA_CHECK_HANDLE(zh);
1280 assert(num_input_setnames>0);
1281 assert(input_setnames);
1282 assert(sort_sequence);
1283 assert(sort_status);
1284 yaz_log(log_level, "zebra_sort");
1286 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1288 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1289 output_setname, sort_sequence, sort_status);
1294 int zebra_deleteResultSet(ZebraHandle zh, int function,
1295 int num_setnames, char **setnames,
1300 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1302 if (zebra_begin_read(zh))
1303 return Z_DeleteStatus_systemProblemAtTarget;
1306 case Z_DeleteResultSetRequest_list:
1307 assert(num_setnames>0);
1309 resultSetDestroy(zh, num_setnames, setnames, statuses);
1311 case Z_DeleteResultSetRequest_all:
1312 resultSetDestroy(zh, -1, 0, statuses);
1316 status = Z_DeleteStatus_success;
1317 for (i = 0; i<num_setnames; i++)
1318 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1319 status = statuses[i];
1323 int zebra_errCode(ZebraHandle zh)
1327 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1330 yaz_log(log_level, "zebra_errCode: o");
1334 const char *zebra_errString(ZebraHandle zh)
1338 e= diagbib1_str(zh->errCode);
1339 yaz_log(log_level, "zebra_errString: %s",e);
1343 char *zebra_errAdd(ZebraHandle zh)
1348 yaz_log(log_level, "zebra_errAdd: %s",a);
1352 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1355 const char *astring;
1359 ZEBRA_CHECK_HANDLE(zh);
1363 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1364 p = res_get(zs->global_res, u);
1365 xfree(zh->user_perm);
1366 zh->user_perm = xstrdup(p ? p : "r");
1368 /* Determine database access list */
1369 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1371 zh->dbaccesslist = xstrdup(astring);
1373 zh->dbaccesslist = 0;
1375 /* users that don't require a password .. */
1376 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1379 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1384 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1385 const char *record_type)
1387 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1388 database, record_type);
1389 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1391 return zebra_begin_trans(zh, 1);
1394 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1396 ZEBRA_CHECK_HANDLE(zh);
1397 yaz_log(log_level, "zebra_admin_import_end");
1398 return zebra_end_trans(zh);
1401 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1403 ZEBRA_RES res = ZEBRA_OK;
1406 ZEBRA_CHECK_HANDLE(zh);
1407 yaz_log(log_level, "zebra_admin_import_segment");
1409 for (i = 0; i<segment->num_segmentRecords; i++)
1411 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1413 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1415 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1416 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1418 Odr_oct *oct = fragment->u.notExternallyTagged;
1421 if(zebra_update_record(
1424 0, /* record Type */
1428 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1436 int delete_w_handle(const char *info, void *handle)
1438 ZebraHandle zh = (ZebraHandle) handle;
1441 if (*info == sizeof(pos))
1443 memcpy(&pos, info+1, sizeof(pos));
1444 isamb_unlink(zh->reg->isamb, pos);
1449 int delete_w_all_handle(const char *info, void *handle)
1451 ZebraHandle zh = (ZebraHandle) handle;
1454 if (*info == sizeof(pos))
1457 memcpy(&pos, info+1, sizeof(pos));
1458 pt = isamb_pp_open(zh->reg->isamb, pos, 2);
1463 while (isamb_pp_read(pt, &key))
1466 rec = rec_get(zh->reg->records, key.mem[0]);
1467 rec_del(zh->reg->records, &rec);
1472 return delete_w_handle(info, handle);
1475 static int delete_SU_handle(void *handle, int ord,
1476 const char *index_type, const char *string_index,
1477 zinfo_index_category_t cat)
1479 ZebraHandle zh = (ZebraHandle) handle;
1483 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s cat=%d", ord,
1484 index_type, string_index, (int) cat);
1486 ord_len = key_SU_encode(ord, ord_buf);
1487 ord_buf[ord_len] = '\0';
1489 assert(zh->reg->isamb);
1490 assert(zh->reg->records);
1491 dict_delete_subtree(zh->reg->dict, ord_buf,
1493 !strcmp(string_index, "_ALLRECORDS") ?
1494 delete_w_all_handle : delete_w_handle);
1498 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1500 ZEBRA_RES ret = ZEBRA_OK;
1502 yaz_log(log_level, "zebra_drop_database %s", db);
1503 ZEBRA_CHECK_HANDLE(zh);
1505 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1507 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1512 if (zebraExplain_curDatabase(zh->reg->zei, db))
1514 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1519 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1520 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1521 0 /* handle */, 0 /* func */);
1522 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1523 zebraExplain_removeDatabase(zh->reg->zei, zh);
1524 zebra_remove_file_match(zh);
1529 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1530 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1531 "drop database only supported for isam:b");
1534 if (zebra_end_trans(zh) != ZEBRA_OK)
1536 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1542 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1544 yaz_log(log_level, "zebra_create_database %s", db);
1545 ZEBRA_CHECK_HANDLE(zh);
1548 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1550 if (zebra_begin_trans(zh, 1))
1553 /* announce database */
1554 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1555 /* explainDatabase */))
1557 if (zebra_end_trans(zh) != ZEBRA_OK)
1559 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1561 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1564 return zebra_end_trans(zh);
1567 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1568 const char *input_str, int input_len,
1569 char *output_str, int output_len)
1572 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1576 yaz_log(log_level, "zebra_string_norm ");
1578 if (!zh->reg->zebra_maps)
1580 wrbuf = zebra_replace(zm, "", input_str, input_len);
1583 if (wrbuf_len(wrbuf) >= output_len)
1585 if (wrbuf_len(wrbuf))
1586 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1587 output_str[wrbuf_len(wrbuf)] = '\0';
1588 return wrbuf_len(wrbuf);
1591 /** \brief set register state (state*.LCK)
1592 \param zh Zebra handle
1594 \param seqno sequence number
1597 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1599 c=commit (writing to register, reading from shadow, shadow mode only)
1601 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1603 char state_fname[256];
1608 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1610 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1611 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1612 f = fopen(fname, "w");
1614 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1615 fprintf(f, "%c %d %ld\n", val, seqno, p);
1620 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1622 char state_fname[256];
1627 yaz_log(log_level, "zebra_get_state ");
1629 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1630 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1631 f = fopen(fname, "r");
1637 if (fscanf(f, "%c %d", val, seqno))
1639 yaz_log(YLOG_ERRNO|YLOG_WARN, "fscan fail %s",
1647 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1649 return zebra_begin_trans(zh, 0);
1652 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1654 return zebra_end_trans(zh);
1657 static void read_res_for_transaction(ZebraHandle zh)
1659 const char *group = res_get(zh->res, "group");
1661 /* FIXME - do we still use groups ?? */
1663 zh->m_group = group;
1664 v = res_get_prefix(zh->res, "followLinks", group, "1");
1665 zh->m_follow_links = atoi(v);
1667 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1668 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1670 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1671 zh->m_store_keys = atoi(v);
1673 v = res_get_prefix(zh->res, "storeData", group, "1");
1674 zh->m_store_data = atoi(v);
1676 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1677 zh->m_explain_database = atoi(v);
1679 v = res_get_prefix(zh->res, "openRW", group, "1");
1680 zh->m_flag_rw = atoi(v);
1682 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1683 zh->m_file_verbose_limit = atoi(v);
1686 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1688 ZEBRA_CHECK_HANDLE(zh);
1689 zebra_select_default_database(zh);
1692 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1693 "zebra_begin_trans: no database selected");
1697 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1701 if (rw && !strchr(zh->user_perm, 'w'))
1705 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1716 const char *rval = 0;
1721 read_res_for_transaction(zh);
1724 if (zh->trans_no != 1)
1726 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1727 "zebra_begin_trans: no write trans within read");
1732 resultSetInvalidate(zh);
1733 zebra_register_close(zh->service, zh->reg);
1735 zh->trans_w_no = zh->trans_no;
1737 zh->records_inserted = 0;
1738 zh->records_updated = 0;
1739 zh->records_deleted = 0;
1740 zh->records_processed = 0;
1741 zh->records_skipped = 0;
1743 #if HAVE_SYS_TIMES_H
1747 if (zh->shadow_enable)
1748 rval = res_get(zh->res, "shadow");
1752 zebra_lock_r(zh->lock_normal);
1753 zebra_lock_w(zh->lock_shadow);
1757 zebra_lock_w(zh->lock_normal);
1758 zebra_lock_w(zh->lock_shadow);
1760 zebra_get_state(zh, &val, &seqno);
1763 /* either we didn't finish commit or shadow is dirty */
1766 yaz_log(YLOG_WARN, "previous transaction did not finish "
1767 "(shadow disabled)");
1769 zebra_unlock(zh->lock_shadow);
1770 zebra_unlock(zh->lock_normal);
1771 if (zebra_commit(zh))
1779 zebra_lock_r(zh->lock_normal);
1780 zebra_lock_w(zh->lock_shadow);
1784 zebra_lock_w(zh->lock_normal);
1785 zebra_lock_w(zh->lock_shadow);
1789 zebra_set_state(zh, 'd', seqno);
1791 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1792 1, rval ? 1 : 0, zh->res,
1795 zh->reg->seqno = seqno;
1798 zebra_set_state(zh, 'o', seqno);
1800 zebra_unlock(zh->lock_shadow);
1801 zebra_unlock(zh->lock_normal);
1806 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1807 "zebra_begin_trans: cannot open register");
1808 yaz_log(YLOG_FATAL, "%s", zh->errString);
1811 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1821 if (zh->trans_no != 1)
1823 return zebra_flush_reg(zh);
1825 #if HAVE_SYS_TIMES_H
1831 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1834 if (!zh->lock_normal || !zh->lock_shadow)
1837 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1840 zebra_get_state(zh, &val, &seqno);
1846 else if (seqno != zh->reg->seqno)
1848 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1849 seqno, zh->reg->seqno);
1852 else if (zh->reg->last_val != val)
1854 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1855 val, zh->reg->last_val);
1862 zebra_lock_r(zh->lock_shadow);
1864 zebra_lock_r(zh->lock_normal);
1868 resultSetInvalidate(zh);
1869 zebra_register_close(zh->service, zh->reg);
1871 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1872 0, val == 'c' ? 1 : 0,
1873 zh->res, zh->path_reg);
1876 zebra_unlock(zh->lock_normal);
1877 zebra_unlock(zh->lock_shadow);
1879 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1882 zh->reg->last_val = val;
1883 zh->reg->seqno = seqno;
1885 read_res_for_transaction(zh);
1889 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1891 ZebraTransactionStatus dummy;
1893 yaz_log(log_level, "zebra_end_trans");
1894 ZEBRA_CHECK_HANDLE(zh);
1895 return zebra_end_transaction(zh, &dummy);
1898 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1904 ZEBRA_CHECK_HANDLE(zh);
1907 yaz_log(log_level, "zebra_end_transaction");
1909 status->processed = 0;
1910 status->inserted = 0;
1911 status->updated = 0;
1912 status->deleted = 0;
1916 if (!zh->res || !zh->reg)
1918 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1919 "zebra_end_trans: no open transaction");
1922 if (zh->trans_no != zh->trans_w_no)
1925 if (zh->trans_no != 0)
1928 /* release read lock */
1930 zebra_unlock(zh->lock_normal);
1931 zebra_unlock(zh->lock_shadow);
1934 { /* release write lock */
1938 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1939 rval = res_get(zh->res, "shadow");
1941 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1943 zebra_flush_reg(zh);
1945 resultSetInvalidate(zh);
1947 zebra_register_close(zh->service, zh->reg);
1950 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1951 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1952 zh->records_processed, zh->records_inserted,
1953 zh->records_updated, zh->records_deleted);
1955 status->processed = zh->records_processed;
1956 status->inserted = zh->records_inserted;
1957 status->updated = zh->records_updated;
1958 status->deleted = zh->records_deleted;
1960 zebra_get_state(zh, &val, &seqno);
1963 BFiles bfs = bfs_create(rval, zh->path_reg);
1964 bf_commitClean(bfs, rval);
1969 zebra_set_state(zh, 'o', seqno);
1970 zebra_unlock(zh->lock_shadow);
1971 zebra_unlock(zh->lock_normal);
1974 #if HAVE_SYS_TIMES_H
1976 yaz_log(log_level, "user/system: %ld/%ld",
1977 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1978 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1980 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1981 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1986 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1988 return zebra_repository_index(zh, path, action_update);
1991 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1993 return zebra_repository_index(zh, path, action_delete);
1996 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
1997 enum zebra_recctrl_action_t action)
2002 if (action == action_update)
2003 yaz_log(log_level, "updating %s", path);
2004 else if (action == action_delete)
2005 yaz_log(log_level, "deleting %s", path);
2006 else if (action == action_a_delete)
2007 yaz_log(log_level, "attempt deleting %s", path);
2009 yaz_log(log_level, "update action=%d", (int) action);
2011 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
2012 return zebra_update_file_match(zh, path);
2014 return zebra_update_from_path(zh, path, action);
2017 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2021 yaz_log(log_level, "zebra_repository_show");
2022 repositoryShow(zh, path);
2026 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2032 ZEBRA_RES res = ZEBRA_OK;
2036 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
2037 zebra_select_default_database(zh);
2040 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2043 rval = res_get(zh->res, "shadow");
2046 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2050 zebra_lock_w(zh->lock_normal);
2051 zebra_lock_r(zh->lock_shadow);
2053 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2056 zebra_unlock(zh->lock_shadow);
2057 zebra_unlock(zh->lock_normal);
2060 zebra_get_state(zh, &val, &seqno);
2064 /* shadow area is dirty and so we must throw it away */
2065 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2068 else if (val == 'c')
2070 /* commit has started. We can not remove it anymore */
2075 bf_cache(bfs, rval);
2076 if (bf_commitExists(bfs))
2079 zebra_set_state(zh, 'd', seqno);
2082 zebra_set_state(zh, 'c', seqno);
2084 yaz_log(log_level, "commit start");
2085 if (bf_commitExec(bfs))
2088 if (res == ZEBRA_OK)
2091 zebra_set_state(zh, 'o', seqno);
2093 zebra_unlock(zh->lock_shadow);
2094 zebra_unlock(zh->lock_normal);
2096 zebra_lock_w(zh->lock_shadow);
2097 bf_commitClean(bfs, rval);
2098 zebra_unlock(zh->lock_shadow);
2102 zebra_unlock(zh->lock_shadow);
2103 zebra_unlock(zh->lock_normal);
2104 yaz_log(YLOG_WARN, "zebra_commit: failed");
2109 zebra_unlock(zh->lock_shadow);
2110 zebra_unlock(zh->lock_normal);
2111 yaz_log(log_level, "nothing to commit");
2118 ZEBRA_RES zebra_clean(ZebraHandle zh)
2120 yaz_log(log_level, "zebra_clean");
2121 ZEBRA_CHECK_HANDLE(zh);
2122 return zebra_commit_ex(zh, 1);
2125 ZEBRA_RES zebra_commit(ZebraHandle zh)
2127 yaz_log(log_level, "zebra_commit");
2128 ZEBRA_CHECK_HANDLE(zh);
2129 return zebra_commit_ex(zh, 0);
2133 ZEBRA_RES zebra_init(ZebraHandle zh)
2138 yaz_log(log_level, "zebra_init");
2140 ZEBRA_CHECK_HANDLE(zh);
2142 zebra_select_default_database(zh);
2145 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2146 "cannot select default database");
2149 rval = res_get(zh->res, "shadow");
2151 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2154 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2158 bf_cache(bfs, rval);
2162 zebra_set_state(zh, 'o', 0);
2166 ZEBRA_RES zebra_compact(ZebraHandle zh)
2170 yaz_log(log_level, "zebra_compact");
2171 ZEBRA_CHECK_HANDLE(zh);
2174 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2177 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2183 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2185 yaz_log(log_level, "zebra_result");
2188 *code = zh->errCode;
2189 *addinfo = zh->errString;
2193 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2194 *addinfo ="ZebraHandle is NULL";
2198 void zebra_shadow_enable(ZebraHandle zh, int value)
2201 yaz_log(log_level, "zebra_shadow_enable");
2202 zh->shadow_enable = value;
2205 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2207 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2208 ZEBRA_CHECK_HANDLE(zh);
2211 if (zh->iconv_to_utf8 != 0)
2212 yaz_iconv_close(zh->iconv_to_utf8);
2213 if (zh->iconv_from_utf8 != 0)
2214 yaz_iconv_close(zh->iconv_from_utf8);
2217 yaz_iconv_open("UTF-8", encoding);
2218 if (zh->iconv_to_utf8 == 0)
2219 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2220 zh->iconv_from_utf8 =
2221 yaz_iconv_open(encoding, "UTF-8");
2222 if (zh->iconv_to_utf8 == 0)
2223 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2228 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2230 yaz_log(log_level, "zebra_record_encoding");
2231 ZEBRA_CHECK_HANDLE(zh);
2232 xfree(zh->record_encoding);
2233 zh->record_encoding = 0;
2235 zh->record_encoding = xstrdup(encoding);
2239 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2243 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2245 res_set(zh->res, name, value);
2248 const char *zebra_get_resource(ZebraHandle zh,
2249 const char *name, const char *defaultvalue)
2254 v = res_get_def(zh->res, name,(char *)defaultvalue);
2255 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2259 /* moved from zebra_api_ext.c by pop */
2260 /* FIXME: Should this really be public??? -Heikki */
2262 int zebra_trans_no(ZebraHandle zh)
2264 yaz_log(log_level, "zebra_trans_no");
2266 return zh->trans_no;
2269 int zebra_get_shadow_enable(ZebraHandle zh)
2271 yaz_log(log_level, "zebra_get_shadow_enable");
2273 return zh->shadow_enable;
2276 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2278 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2280 zh->shadow_enable = value;
2283 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2284 const char *buf, int buf_size)
2286 return zebra_update_record(zh, action_update,
2287 0 /* record type */,
2294 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2295 enum zebra_recctrl_action_t action,
2296 const char *recordType,
2297 zint *sysno, const char *match,
2299 const char *buf, int buf_size)
2303 ZEBRA_CHECK_HANDLE(zh);
2307 yaz_log(log_level, "zebra_update_record");
2309 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2312 buf_size = strlen(buf);
2314 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2316 res = zebra_buffer_extract_record(zh, buf, buf_size,
2322 if (zebra_end_trans(zh) != ZEBRA_OK)
2324 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2330 /* ---------------------------------------------------------------------------
2334 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2335 const char *setname, zint *hits)
2338 ZEBRA_RES res = ZEBRA_OK;
2343 ZEBRA_CHECK_HANDLE(zh);
2345 odr = odr_createmem(ODR_ENCODE);
2350 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2352 query = p_query_rpn(odr, pqf_query);
2356 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2357 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2361 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2365 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2373 /* ---------------------------------------------------------------------------
2374 Sort - a simplified interface, with optional read locks.
2376 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2377 const char *sort_spec,
2378 const char *output_setname,
2379 const char **input_setnames)
2381 int num_input_setnames = 0;
2382 int sort_status = 0;
2383 Z_SortKeySpecList *sort_sequence;
2385 ZEBRA_CHECK_HANDLE(zh);
2388 assert(output_setname);
2389 assert(input_setnames);
2390 sort_sequence = yaz_sort_spec(stream, sort_spec);
2391 yaz_log(log_level, "sort (FIXME) ");
2394 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2395 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2399 /* we can do this, since the perl typemap code for char** will
2400 put a NULL at the end of list */
2401 while (input_setnames[num_input_setnames]) num_input_setnames++;
2403 if (zebra_begin_read(zh))
2406 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2407 output_setname, sort_sequence, &sort_status);
2413 /* ---------------------------------------------------------------------------
2414 Get BFS for Zebra system (to make alternative storage methods)
2416 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2419 return zh->reg->bfs;
2424 /* ---------------------------------------------------------------------------
2425 Set limit for search/scan
2427 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2429 ZEBRA_CHECK_HANDLE(zh);
2430 zebra_limit_destroy(zh->m_limit);
2431 zh->m_limit = zebra_limit_create(complement_flag, ids);
2436 Set Error code + addinfo
2438 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2443 nmem_reset(zh->nmem_error);
2444 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2447 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2450 sprintf(vstr, ZINT_FORMAT, i);
2453 nmem_reset(zh->nmem_error);
2454 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2457 void zebra_lock_prefix(Res res, char *path)
2459 const char *lock_dir = res_get_def(res, "lockDir", "");
2461 strcpy(path, lock_dir);
2462 if (*path && path[strlen(path)-1] != '/')
2469 * c-file-style: "Stroustrup"
2470 * indent-tabs-mode: nil
2472 * vim: shiftwidth=4 tabstop=8 expandtab