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 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;
205 zebra_get_version(version_str, system_str);
207 yaz_log(YLOG_LOG, "zebra_start %s %s", version_str, system_str);
209 yaz_log(YLOG_LOG, "config %s", configName);
211 if ((res = res_open(def_res, over_res)))
213 const char *passwd_plain = 0;
214 const char *passwd_encrypt = 0;
215 const char *dbaccess = 0;
220 ZEBRA_RES ret = res_read_file(res, configName);
226 if (zebra_check_res(res))
228 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
235 zebra_check_res(res);
238 zh = xmalloc(sizeof(*zh));
239 zh->global_res = res;
244 zebra_mutex_cond_init(&zh->session_lock);
245 passwd_plain = res_get(zh->global_res, "passwd");
246 passwd_encrypt = res_get(zh->global_res, "passwd.c");
247 dbaccess = res_get(zh->global_res, "dbaccess");
249 if (!passwd_plain && !passwd_encrypt)
250 zh->passwd_db = NULL;
253 zh->passwd_db = passwd_db_open();
255 yaz_log(YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
259 passwd_db_file_plain(zh->passwd_db, passwd_plain);
261 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
268 zh->dbaccess = res_open(NULL, NULL);
269 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
270 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
275 zh->timing = yaz_timing_create();
276 zh->path_root = res_get(zh->global_res, "root");
277 zh->nmem = nmem_create();
278 zh->record_classes = recTypeClass_create(zh->global_res, zh->nmem);
282 const char *module_path = res_get(res, "modulePath");
284 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
292 void zebra_filter_info(ZebraService zs, void *cd,
293 void(*cb)(void *cd, const char *name))
297 recTypeClass_info(zs->record_classes, cd, cb);
300 void zebra_pidfname(ZebraService zs, char *path)
303 zebra_lock_prefix(zs->global_res, path);
304 strcat(path, "zebrasrv.pid");
307 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
308 int compact_flag, Res res)
310 int page_size = 4096;
311 char resource_str[200];
312 sprintf(resource_str, "dict.%.100s.pagesize", name);
316 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
317 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
319 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
323 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
324 int rw, int useshadow, Res res,
325 const char *reg_path)
327 struct zebra_register *reg;
328 int record_compression = REC_COMPRESS_NONE;
329 const char *recordCompression = 0;
330 const char *profilePath;
332 int sort_type = ZEBRA_SORT_TYPE_FLAT;
333 ZEBRA_RES ret = ZEBRA_OK;
337 reg = xmalloc(sizeof(*reg));
340 reg->name = xstrdup(name);
347 yaz_log(YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
348 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
350 reg->dh = data1_create();
357 reg->bfs = bfs_create(res_get(res, "register"), reg_path);
360 data1_destroy(reg->dh);
367 if (bf_cache(reg->bfs, res_get(res, "shadow")) == ZEBRA_FAIL)
369 bfs_destroy(reg->bfs);
370 data1_destroy(reg->dh);
377 getcwd(cwd, sizeof(cwd)-1);
378 profilePath = res_get_def(res, "profilePath", 0);
380 data1_set_tabpath(reg->dh, profilePath);
381 data1_set_tabroot(reg->dh, reg_path);
382 reg->recTypes = recTypes_init(zs->record_classes, reg->dh);
385 zebra_maps_open(res, reg_path, profilePath);
386 if (!reg->zebra_maps)
388 recTypes_destroy(reg->recTypes);
389 bfs_destroy(reg->bfs);
390 data1_destroy(reg->dh);
395 reg->rank_classes = NULL;
398 reg->keys = zebra_rec_keys_open();
400 reg->sortKeys = zebra_rec_keys_open();
411 /* installing rank classes */
412 zebraRankInstall(reg, rank_1_class);
413 zebraRankInstall(reg, rank_2_class);
414 zebraRankInstall(reg, rank_similarity_class);
415 zebraRankInstall(reg, rank_static_class);
417 recordCompression = res_get_def(res, "recordCompression", "none");
418 if (!strcmp(recordCompression, "none"))
419 record_compression = REC_COMPRESS_NONE;
420 if (!strcmp(recordCompression, "bzip2"))
421 record_compression = REC_COMPRESS_BZIP2;
424 const char *index_fname = res_get_def(res, "index", "default.idx");
425 if (index_fname && *index_fname && strcmp(index_fname, "none"))
427 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
432 zebra_maps_define_default_sort(reg->zebra_maps);
436 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
438 yaz_log(YLOG_WARN, "rec_open failed");
443 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
445 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
447 yaz_log(YLOG_WARN, "dict_open failed");
452 if (res_get_match(res, "sortindex", "f", "f"))
453 sort_type = ZEBRA_SORT_TYPE_FLAT;
454 else if (res_get_match(res, "sortindex", "i", "f"))
455 sort_type = ZEBRA_SORT_TYPE_ISAMB;
456 else if (res_get_match(res, "sortindex", "m", "f"))
457 sort_type = ZEBRA_SORT_TYPE_MULTI;
460 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
465 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
467 yaz_log(YLOG_WARN, "zebra_sort_open failed");
470 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
472 struct ISAMS_M_s isams_m;
473 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
474 key_isams_m(res, &isams_m))))
476 yaz_log(YLOG_WARN, "isams_open failed");
480 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
482 struct ISAMC_M_s isamc_m;
483 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
484 rw, key_isamc_m(res, &isamc_m))))
486 yaz_log(YLOG_WARN, "isamc_open failed");
490 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
492 struct ISAMC_M_s isamc_m;
494 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
495 rw, key_isamc_m(res, &isamc_m), 0)))
497 yaz_log(YLOG_WARN, "isamb_open failed");
501 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
503 struct ISAMC_M_s isamc_m;
505 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
506 rw, key_isamc_m(res, &isamc_m), 1)))
508 yaz_log(YLOG_WARN, "isamb_open failed");
512 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
514 struct ISAMC_M_s isamc_m;
516 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
517 rw, key_isamc_m(res, &isamc_m), -1)))
519 yaz_log(YLOG_WARN, "isamb_open failed");
525 reg->zei = zebraExplain_open(reg->records, reg->dh,
527 zebra_extract_explain);
530 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
537 zebra_register_close(zs, reg);
540 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
544 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
546 ZEBRA_CHECK_HANDLE(zh);
547 yaz_log(log_level, "zebra_admin_shutdown");
549 zebra_mutex_cond_lock(&zh->service->session_lock);
550 zh->service->stop_flag = 1;
551 zebra_mutex_cond_unlock(&zh->service->session_lock);
555 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
558 ZEBRA_CHECK_HANDLE(zh);
559 yaz_log(log_level, "zebra_admin_start");
561 zebra_mutex_cond_lock(&zs->session_lock);
562 zebra_mutex_cond_unlock(&zs->session_lock);
566 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
570 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
574 zebraExplain_close(reg->zei);
575 dict_close(reg->dict);
577 dict_close(reg->matchDict);
578 zebra_sort_close(reg->sort_index);
580 isams_close(reg->isams);
582 isamc_close(reg->isamc);
584 isamb_close(reg->isamb);
585 rec_close(®->records);
587 recTypes_destroy(reg->recTypes);
588 zebra_maps_close(reg->zebra_maps);
589 zebraRankDestroy(reg);
590 bfs_destroy(reg->bfs);
591 data1_destroy(reg->dh);
593 zebra_rec_keys_close(reg->keys);
594 zebra_rec_keys_close(reg->sortKeys);
596 key_block_destroy(®->key_block);
601 ZEBRA_RES zebra_stop(ZebraService zs)
607 zebra_close(zs->sessions);
610 zebra_mutex_cond_destroy(&zs->session_lock);
613 passwd_db_close(zs->passwd_db);
615 recTypeClass_destroy(zs->record_classes);
616 nmem_destroy(zs->nmem);
617 res_close(zs->global_res);
619 yaz_timing_stop(zs->timing);
620 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
621 yaz_timing_get_real(zs->timing),
622 yaz_timing_get_user(zs->timing),
623 yaz_timing_get_sys(zs->timing));
626 yaz_timing_destroy(&zs->timing);
631 ZEBRA_RES zebra_close(ZebraHandle zh)
634 struct zebra_session **sp;
637 yaz_log(log_level, "zebra_close");
638 ZEBRA_CHECK_HANDLE(zh);
643 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
644 resultSetDestroy(zh, -1, 0, 0);
647 zebra_register_close(zh->service, zh->reg);
649 res_close(zh->session_res);
651 xfree(zh->record_encoding);
653 xfree(zh->dbaccesslist);
655 for (i = 0; i < zh->num_basenames; i++)
656 xfree(zh->basenames[i]);
657 xfree(zh->basenames);
659 if (zh->iconv_to_utf8 != 0)
660 yaz_iconv_close(zh->iconv_to_utf8);
661 if (zh->iconv_from_utf8 != 0)
662 yaz_iconv_close(zh->iconv_from_utf8);
664 zebra_mutex_cond_lock(&zs->session_lock);
665 zebra_lock_destroy(zh->lock_normal);
666 zebra_lock_destroy(zh->lock_shadow);
678 zebra_mutex_cond_unlock(&zs->session_lock);
680 xfree(zh->user_perm);
681 zh->service = 0; /* more likely to trigger an assert */
683 zebra_limit_destroy(zh->m_limit);
685 nmem_destroy(zh->nmem_error);
692 struct map_baseinfo {
698 char **new_basenames;
702 static void zebra_open_res(ZebraHandle zh)
710 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
711 zh->res = res_open(zh->session_res, 0);
712 res_read_file(zh->res, fname);
714 else if (*zh->reg_name == 0)
716 zh->res = res_open(zh->session_res, 0);
720 yaz_log(YLOG_WARN, "no register root specified");
721 zh->res = 0; /* no path for register - fail! */
725 static void zebra_close_res(ZebraHandle zh)
733 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
737 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
741 assert(zh->reg == 0);
742 assert(*zh->reg_name == 0);
748 resultSetInvalidate(zh);
749 zebra_register_close(zh->service, zh->reg);
755 zh->reg_name = xstrdup(new_reg);
759 if (zh->service->path_root)
761 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
762 strlen(zh->reg_name) + 3);
763 strcpy(zh->path_reg, zh->service->path_root);
766 strcat(zh->path_reg, "/");
767 strcat(zh->path_reg, zh->reg_name);
773 zebra_lock_destroy(zh->lock_normal);
777 zebra_lock_destroy(zh->lock_shadow);
783 const char *lock_area = res_get(zh->res, "lockDir");
785 if (!lock_area && zh->path_reg)
786 res_set(zh->res, "lockDir", zh->path_reg);
787 sprintf(fname, "norm.%s.LCK", zh->reg_name);
789 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
791 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
793 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
795 if (!zh->lock_normal || !zh->lock_shadow)
799 zebra_lock_destroy(zh->lock_normal);
804 zebra_lock_destroy(zh->lock_shadow);
813 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
814 zebra_set_approx_limit(zh, approx);
818 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
819 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
823 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
826 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
827 zh->m_segment_indexing);
832 void map_basenames_func(void *vp, const char *name, const char *value)
834 struct map_baseinfo *p = (struct map_baseinfo *) vp;
836 char fromdb[128], todb[8][128];
843 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
844 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
845 todb[5], todb[6], todb[7]);
849 for (i = 0; i<p->num_bases; i++)
850 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
853 for (i = 0; i < no; i++)
855 if (p->new_num_bases == p->new_num_max)
857 p->new_basenames[(p->new_num_bases)++] =
858 nmem_strdup(p->mem, todb[i]);
864 int zebra_select_default_database(ZebraHandle zh)
868 /* no database has been selected - so we select based on
869 resource setting (including group)
871 const char *group = res_get(zh->session_res, "group");
872 const char *v = res_get_prefix(zh->session_res,
873 "database", group, "Default");
874 return zebra_select_database(zh, v);
879 void map_basenames(ZebraHandle zh, ODR stream,
880 int *num_bases, char ***basenames)
882 struct map_baseinfo info;
883 struct map_baseinfo *p = &info;
886 yaz_log(log_level, "map_basenames ");
891 info.num_bases = *num_bases;
892 info.basenames = *basenames;
893 info.new_num_max = 128;
894 info.new_num_bases = 0;
895 info.new_basenames = (char **)
896 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
897 info.mem = stream->mem;
899 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
901 for (i = 0; i<p->num_bases; i++)
902 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
904 p->new_basenames[(p->new_num_bases)++] =
905 nmem_strdup(p->mem, p->basenames[i]);
907 *num_bases = info.new_num_bases;
908 *basenames = info.new_basenames;
909 for (i = 0; i<*num_bases; i++)
910 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
913 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
915 ZEBRA_CHECK_HANDLE(zh);
917 yaz_log(log_level, "zebra_select_database %s",basename);
919 return zebra_select_databases(zh, 1, &basename);
922 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
923 const char **basenames)
930 ZEBRA_CHECK_HANDLE(zh);
933 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
934 num_bases,basenames[0]);
939 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
943 /* Check if the user has access to all databases (Seb) */
944 /* You could argue that this should happen later, after we have
945 * determined that the database(s) exist. */
946 if (zh->dbaccesslist) {
947 for (i = 0; i < num_bases; i++) {
948 const char *db = basenames[i];
950 for (p = zh->dbaccesslist; p && *p; p = pp) {
952 if ((pp = strchr(p, '+'))) {
958 if (len == strlen(db) && !strncmp(db, p, len))
962 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
968 for (i = 0; i < zh->num_basenames; i++)
969 xfree(zh->basenames[i]);
970 xfree(zh->basenames);
972 zh->num_basenames = num_bases;
973 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
974 for (i = 0; i < zh->num_basenames; i++)
975 zh->basenames[i] = xstrdup(basenames[i]);
977 cp = strrchr(basenames[0], '/');
980 len = cp - basenames[0];
981 new_reg = xmalloc(len + 1);
982 memcpy(new_reg, basenames[0], len);
986 new_reg = xstrdup("");
987 for (i = 1; i<num_bases; i++)
991 cp1 = strrchr(basenames[i], '/');
996 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
999 if (len != cp1 - basenames[i] ||
1000 memcmp(basenames[i], new_reg, len))
1002 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1010 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1015 zebra_select_register(zh, new_reg);
1019 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1022 if (!zh->lock_normal || !zh->lock_shadow)
1024 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1030 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1032 if (approx_limit == 0)
1033 approx_limit = DEFAULT_APPROX_LIMIT;
1034 zh->approx_limit = approx_limit;
1038 void zebra_set_partial_result(ZebraHandle zh)
1040 zh->partial_result = 1;
1044 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1045 int (*f)(void *client_data),
1048 zh->break_handler_func = f;
1049 zh->break_handler_data = client_data;
1053 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1054 const char *setname, zint *hits,
1055 int *estimated_hit_count,
1056 int *partial_resultset)
1060 ZEBRA_CHECK_HANDLE(zh);
1066 yaz_log(log_level, "zebra_search_rpn");
1068 zh->partial_result = 0;
1070 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1073 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1074 zh->num_basenames, zh->basenames, setname,
1075 hits, estimated_hit_count);
1077 *partial_resultset = zh->partial_result;
1082 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1083 const char *setname, zint *hits)
1085 int estimated_hit_count;
1086 int partial_resultset;
1087 return zebra_search_RPN_x(zh, o, query, setname, hits,
1088 &estimated_hit_count,
1089 &partial_resultset);
1092 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1093 const char *setname,
1094 Z_RecordComposition *comp,
1095 const Odr_oid *input_format, int num_recs,
1096 ZebraRetrievalRecord *recs)
1098 ZebraMetaRecord *poset;
1100 ZEBRA_RES ret = ZEBRA_OK;
1103 ZEBRA_CHECK_HANDLE(zh);
1109 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1113 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1118 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1121 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1122 for (i = 0; i<num_recs; i++)
1123 pos_array[i] = recs[i].position;
1124 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1127 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1128 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1134 WRBUF addinfo_w = wrbuf_alloc();
1135 for (i = 0; i < num_recs; i++)
1137 recs[i].errCode = 0;
1138 recs[i].errString = 0;
1143 recs[i].sysno = poset[i].sysno;
1146 recs[i].format = yaz_oid_recsyn_sutrs;
1147 recs[i].len = strlen(poset[i].term);
1148 recs[i].buf = poset[i].term;
1149 recs[i].base = poset[i].db;
1151 else if (poset[i].sysno)
1155 zebra_snippets *hit_snippet = zebra_snippets_create();
1157 /* we disable hit snippets for now. It does not work well
1158 and it slows retrieval down a lot */
1160 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1163 wrbuf_rewind(addinfo_w);
1165 zebra_record_fetch(zh, setname,
1166 poset[i].sysno, poset[i].score,
1167 stream, input_format, comp,
1168 &recs[i].format, &buf, &len,
1169 &recs[i].base, addinfo_w);
1171 if (wrbuf_len(addinfo_w))
1173 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1177 recs[i].buf = (char*) odr_malloc(stream, len);
1178 memcpy(recs[i].buf, buf, len);
1182 recs[i].score = poset[i].score;
1183 zebra_snippets_destroy(hit_snippet);
1187 /* only need to set it once */
1188 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1190 zebra_setError_zint(zh,
1191 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1198 zebra_meta_records_destroy(zh, poset, num_recs);
1199 wrbuf_destroy(addinfo_w);
1206 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1208 int *num_entries, ZebraScanEntry **entries,
1210 const char *setname)
1212 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1213 Z_AttributesPlusTerm *zapt;
1214 Odr_oid *attributeSet;
1217 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1220 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1224 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1225 position, num_entries, entries, is_partial,
1228 yaz_pqf_destroy(pqf_parser);
1232 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1233 const Odr_oid *attributeset,
1235 int *num_entries, ZebraScanEntry **entries,
1237 const char *setname)
1241 ZEBRA_CHECK_HANDLE(zh);
1246 assert(num_entries);
1249 yaz_log(log_level, "zebra_scan");
1251 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1258 res = rpn_scan(zh, stream, zapt, attributeset,
1259 zh->num_basenames, zh->basenames, position,
1260 num_entries, entries, is_partial, setname);
1265 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1266 int num_input_setnames, const char **input_setnames,
1267 const char *output_setname,
1268 Z_SortKeySpecList *sort_sequence,
1272 ZEBRA_CHECK_HANDLE(zh);
1274 assert(num_input_setnames>0);
1275 assert(input_setnames);
1276 assert(sort_sequence);
1277 assert(sort_status);
1278 yaz_log(log_level, "zebra_sort");
1280 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1282 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1283 output_setname, sort_sequence, sort_status);
1288 int zebra_deleteResultSet(ZebraHandle zh, int function,
1289 int num_setnames, char **setnames,
1294 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1296 if (zebra_begin_read(zh))
1297 return Z_DeleteStatus_systemProblemAtTarget;
1300 case Z_DeleteResultSetRequest_list:
1301 assert(num_setnames>0);
1303 resultSetDestroy(zh, num_setnames, setnames, statuses);
1305 case Z_DeleteResultSetRequest_all:
1306 resultSetDestroy(zh, -1, 0, statuses);
1310 status = Z_DeleteStatus_success;
1311 for (i = 0; i<num_setnames; i++)
1312 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1313 status = statuses[i];
1317 int zebra_errCode(ZebraHandle zh)
1321 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1324 yaz_log(log_level, "zebra_errCode: o");
1328 const char *zebra_errString(ZebraHandle zh)
1332 e= diagbib1_str(zh->errCode);
1333 yaz_log(log_level, "zebra_errString: %s",e);
1337 char *zebra_errAdd(ZebraHandle zh)
1342 yaz_log(log_level, "zebra_errAdd: %s",a);
1346 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1349 const char *astring;
1353 ZEBRA_CHECK_HANDLE(zh);
1357 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1358 p = res_get(zs->global_res, u);
1359 xfree(zh->user_perm);
1360 zh->user_perm = xstrdup(p ? p : "r");
1362 /* Determine database access list */
1363 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1365 zh->dbaccesslist = xstrdup(astring);
1367 zh->dbaccesslist = 0;
1369 /* users that don't require a password .. */
1370 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1373 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1378 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1379 const char *record_type)
1381 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1382 database, record_type);
1383 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1385 return zebra_begin_trans(zh, 1);
1388 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1390 ZEBRA_CHECK_HANDLE(zh);
1391 yaz_log(log_level, "zebra_admin_import_end");
1392 return zebra_end_trans(zh);
1395 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1397 ZEBRA_RES res = ZEBRA_OK;
1400 ZEBRA_CHECK_HANDLE(zh);
1401 yaz_log(log_level, "zebra_admin_import_segment");
1403 for (i = 0; i<segment->num_segmentRecords; i++)
1405 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1407 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1409 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1410 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1412 Odr_oct *oct = fragment->u.notExternallyTagged;
1415 if(zebra_update_record(
1418 0, /* record Type */
1422 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1430 int delete_w_handle(const char *info, void *handle)
1432 ZebraHandle zh = (ZebraHandle) handle;
1435 if (*info == sizeof(pos))
1437 memcpy(&pos, info+1, sizeof(pos));
1438 isamb_unlink(zh->reg->isamb, pos);
1443 int delete_w_all_handle(const char *info, void *handle)
1445 ZebraHandle zh = (ZebraHandle) handle;
1448 if (*info == sizeof(pos))
1451 memcpy(&pos, info+1, sizeof(pos));
1452 pt = isamb_pp_open(zh->reg->isamb, pos, 2);
1457 while (isamb_pp_read(pt, &key))
1460 rec = rec_get(zh->reg->records, key.mem[0]);
1461 rec_del(zh->reg->records, &rec);
1466 return delete_w_handle(info, handle);
1469 static int delete_SU_handle(void *handle, int ord,
1470 const char *index_type, const char *string_index,
1471 zinfo_index_category_t cat)
1473 ZebraHandle zh = (ZebraHandle) handle;
1477 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s cat=%d", ord,
1478 index_type, string_index, (int) cat);
1480 ord_len = key_SU_encode(ord, ord_buf);
1481 ord_buf[ord_len] = '\0';
1483 assert(zh->reg->isamb);
1484 assert(zh->reg->records);
1485 dict_delete_subtree(zh->reg->dict, ord_buf,
1487 !strcmp(string_index, "_ALLRECORDS") ?
1488 delete_w_all_handle : delete_w_handle);
1492 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1494 ZEBRA_RES ret = ZEBRA_OK;
1496 yaz_log(log_level, "zebra_drop_database %s", db);
1497 ZEBRA_CHECK_HANDLE(zh);
1499 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1501 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1506 if (zebraExplain_curDatabase(zh->reg->zei, db))
1508 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1513 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1514 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1515 0 /* handle */, 0 /* func */);
1516 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1517 zebraExplain_removeDatabase(zh->reg->zei, zh);
1518 zebra_remove_file_match(zh);
1523 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1524 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1525 "drop database only supported for isam:b");
1528 if (zebra_end_trans(zh) != ZEBRA_OK)
1530 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1536 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1538 yaz_log(log_level, "zebra_create_database %s", db);
1539 ZEBRA_CHECK_HANDLE(zh);
1542 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1544 if (zebra_begin_trans(zh, 1))
1547 /* announce database */
1548 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1549 /* explainDatabase */))
1551 if (zebra_end_trans(zh) != ZEBRA_OK)
1553 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1555 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1558 return zebra_end_trans(zh);
1561 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1562 const char *input_str, int input_len,
1563 char *output_str, int output_len)
1566 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1570 yaz_log(log_level, "zebra_string_norm ");
1572 if (!zh->reg->zebra_maps)
1574 wrbuf = zebra_replace(zm, "", input_str, input_len);
1577 if (wrbuf_len(wrbuf) >= output_len)
1579 if (wrbuf_len(wrbuf))
1580 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1581 output_str[wrbuf_len(wrbuf)] = '\0';
1582 return wrbuf_len(wrbuf);
1585 /** \brief set register state (state*.LCK)
1586 \param zh Zebra handle
1588 \param seqno sequence number
1591 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1593 c=commit (writing to register, reading from shadow, shadow mode only)
1595 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1597 char state_fname[256];
1602 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1604 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1605 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1606 f = fopen(fname, "w");
1608 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1609 fprintf(f, "%c %d %ld\n", val, seqno, p);
1614 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1616 char state_fname[256];
1621 yaz_log(log_level, "zebra_get_state ");
1623 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1624 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1625 f = fopen(fname, "r");
1631 fscanf(f, "%c %d", val, seqno);
1637 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1639 return zebra_begin_trans(zh, 0);
1642 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1644 return zebra_end_trans(zh);
1647 static void read_res_for_transaction(ZebraHandle zh)
1649 const char *group = res_get(zh->res, "group");
1651 /* FIXME - do we still use groups ?? */
1653 zh->m_group = group;
1654 v = res_get_prefix(zh->res, "followLinks", group, "1");
1655 zh->m_follow_links = atoi(v);
1657 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1658 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1660 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1661 zh->m_store_keys = atoi(v);
1663 v = res_get_prefix(zh->res, "storeData", group, "1");
1664 zh->m_store_data = atoi(v);
1666 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1667 zh->m_explain_database = atoi(v);
1669 v = res_get_prefix(zh->res, "openRW", group, "1");
1670 zh->m_flag_rw = atoi(v);
1672 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1673 zh->m_file_verbose_limit = atoi(v);
1676 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1678 ZEBRA_CHECK_HANDLE(zh);
1679 zebra_select_default_database(zh);
1682 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1683 "zebra_begin_trans: no database selected");
1687 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1691 if (rw && !strchr(zh->user_perm, 'w'))
1695 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1706 const char *rval = 0;
1711 read_res_for_transaction(zh);
1714 if (zh->trans_no != 1)
1716 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1717 "zebra_begin_trans: no write trans within read");
1722 resultSetInvalidate(zh);
1723 zebra_register_close(zh->service, zh->reg);
1725 zh->trans_w_no = zh->trans_no;
1727 zh->records_inserted = 0;
1728 zh->records_updated = 0;
1729 zh->records_deleted = 0;
1730 zh->records_processed = 0;
1731 zh->records_skipped = 0;
1733 #if HAVE_SYS_TIMES_H
1737 if (zh->shadow_enable)
1738 rval = res_get(zh->res, "shadow");
1742 zebra_lock_r(zh->lock_normal);
1743 zebra_lock_w(zh->lock_shadow);
1747 zebra_lock_w(zh->lock_normal);
1748 zebra_lock_w(zh->lock_shadow);
1750 zebra_get_state(zh, &val, &seqno);
1753 /* either we didn't finish commit or shadow is dirty */
1756 yaz_log(YLOG_WARN, "previous transaction did not finish "
1757 "(shadow disabled)");
1759 zebra_unlock(zh->lock_shadow);
1760 zebra_unlock(zh->lock_normal);
1761 if (zebra_commit(zh))
1769 zebra_lock_r(zh->lock_normal);
1770 zebra_lock_w(zh->lock_shadow);
1774 zebra_lock_w(zh->lock_normal);
1775 zebra_lock_w(zh->lock_shadow);
1779 zebra_set_state(zh, 'd', seqno);
1781 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1782 1, rval ? 1 : 0, zh->res,
1785 zh->reg->seqno = seqno;
1788 zebra_set_state(zh, 'o', seqno);
1790 zebra_unlock(zh->lock_shadow);
1791 zebra_unlock(zh->lock_normal);
1796 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1797 "zebra_begin_trans: cannot open register");
1798 yaz_log(YLOG_FATAL, "%s", zh->errString);
1801 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1811 if (zh->trans_no != 1)
1813 return zebra_flush_reg(zh);
1815 #if HAVE_SYS_TIMES_H
1821 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1824 if (!zh->lock_normal || !zh->lock_shadow)
1827 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1830 zebra_get_state(zh, &val, &seqno);
1836 else if (seqno != zh->reg->seqno)
1838 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1839 seqno, zh->reg->seqno);
1842 else if (zh->reg->last_val != val)
1844 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1845 val, zh->reg->last_val);
1852 zebra_lock_r(zh->lock_shadow);
1854 zebra_lock_r(zh->lock_normal);
1858 resultSetInvalidate(zh);
1859 zebra_register_close(zh->service, zh->reg);
1861 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1862 0, val == 'c' ? 1 : 0,
1863 zh->res, zh->path_reg);
1866 zebra_unlock(zh->lock_normal);
1867 zebra_unlock(zh->lock_shadow);
1869 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1872 zh->reg->last_val = val;
1873 zh->reg->seqno = seqno;
1875 read_res_for_transaction(zh);
1879 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1881 ZebraTransactionStatus dummy;
1883 yaz_log(log_level, "zebra_end_trans");
1884 ZEBRA_CHECK_HANDLE(zh);
1885 return zebra_end_transaction(zh, &dummy);
1888 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1894 ZEBRA_CHECK_HANDLE(zh);
1897 yaz_log(log_level, "zebra_end_transaction");
1899 status->processed = 0;
1900 status->inserted = 0;
1901 status->updated = 0;
1902 status->deleted = 0;
1906 if (!zh->res || !zh->reg)
1908 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1909 "zebra_end_trans: no open transaction");
1912 if (zh->trans_no != zh->trans_w_no)
1915 if (zh->trans_no != 0)
1918 /* release read lock */
1920 zebra_unlock(zh->lock_normal);
1921 zebra_unlock(zh->lock_shadow);
1924 { /* release write lock */
1928 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1929 rval = res_get(zh->res, "shadow");
1931 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1933 zebra_flush_reg(zh);
1935 resultSetInvalidate(zh);
1937 zebra_register_close(zh->service, zh->reg);
1940 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1941 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1942 zh->records_processed, zh->records_inserted,
1943 zh->records_updated, zh->records_deleted);
1945 status->processed = zh->records_processed;
1946 status->inserted = zh->records_inserted;
1947 status->updated = zh->records_updated;
1948 status->deleted = zh->records_deleted;
1950 zebra_get_state(zh, &val, &seqno);
1953 BFiles bfs = bfs_create(rval, zh->path_reg);
1954 bf_commitClean(bfs, rval);
1959 zebra_set_state(zh, 'o', seqno);
1960 zebra_unlock(zh->lock_shadow);
1961 zebra_unlock(zh->lock_normal);
1964 #if HAVE_SYS_TIMES_H
1966 yaz_log(log_level, "user/system: %ld/%ld",
1967 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1968 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1970 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1971 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1976 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1978 return zebra_repository_index(zh, path, action_update);
1981 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1983 return zebra_repository_index(zh, path, action_delete);
1986 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
1987 enum zebra_recctrl_action_t action)
1992 if (action == action_update)
1993 yaz_log(log_level, "updating %s", path);
1994 else if (action == action_delete)
1995 yaz_log(log_level, "deleting %s", path);
1996 else if (action == action_a_delete)
1997 yaz_log(log_level, "attempt deleting %s", path);
1999 yaz_log(log_level, "update action=%d", (int) action);
2001 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
2002 return zebra_update_file_match(zh, path);
2004 return zebra_update_from_path(zh, path, action);
2007 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2011 yaz_log(log_level, "zebra_repository_show");
2012 repositoryShow(zh, path);
2016 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2022 ZEBRA_RES res = ZEBRA_OK;
2026 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
2027 zebra_select_default_database(zh);
2030 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2033 rval = res_get(zh->res, "shadow");
2036 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2040 zebra_lock_w(zh->lock_normal);
2041 zebra_lock_r(zh->lock_shadow);
2043 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2046 zebra_unlock(zh->lock_shadow);
2047 zebra_unlock(zh->lock_normal);
2050 zebra_get_state(zh, &val, &seqno);
2054 /* shadow area is dirty and so we must throw it away */
2055 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2058 else if (val == 'c')
2060 /* commit has started. We can not remove it anymore */
2065 bf_cache(bfs, rval);
2066 if (bf_commitExists(bfs))
2069 zebra_set_state(zh, 'd', seqno);
2072 zebra_set_state(zh, 'c', seqno);
2074 yaz_log(log_level, "commit start");
2075 if (bf_commitExec(bfs))
2078 if (res == ZEBRA_OK)
2081 zebra_set_state(zh, 'o', seqno);
2083 zebra_unlock(zh->lock_shadow);
2084 zebra_unlock(zh->lock_normal);
2086 zebra_lock_w(zh->lock_shadow);
2087 bf_commitClean(bfs, rval);
2088 zebra_unlock(zh->lock_shadow);
2092 zebra_unlock(zh->lock_shadow);
2093 zebra_unlock(zh->lock_normal);
2094 yaz_log(YLOG_WARN, "zebra_commit: failed");
2099 zebra_unlock(zh->lock_shadow);
2100 zebra_unlock(zh->lock_normal);
2101 yaz_log(log_level, "nothing to commit");
2108 ZEBRA_RES zebra_clean(ZebraHandle zh)
2110 yaz_log(log_level, "zebra_clean");
2111 ZEBRA_CHECK_HANDLE(zh);
2112 return zebra_commit_ex(zh, 1);
2115 ZEBRA_RES zebra_commit(ZebraHandle zh)
2117 yaz_log(log_level, "zebra_commit");
2118 ZEBRA_CHECK_HANDLE(zh);
2119 return zebra_commit_ex(zh, 0);
2123 ZEBRA_RES zebra_init(ZebraHandle zh)
2128 yaz_log(log_level, "zebra_init");
2130 ZEBRA_CHECK_HANDLE(zh);
2132 zebra_select_default_database(zh);
2135 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2136 "cannot select default database");
2139 rval = res_get(zh->res, "shadow");
2141 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2144 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2148 bf_cache(bfs, rval);
2152 zebra_set_state(zh, 'o', 0);
2156 ZEBRA_RES zebra_compact(ZebraHandle zh)
2160 yaz_log(log_level, "zebra_compact");
2161 ZEBRA_CHECK_HANDLE(zh);
2164 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2167 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2173 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2175 yaz_log(log_level, "zebra_result");
2178 *code = zh->errCode;
2179 *addinfo = zh->errString;
2183 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2184 *addinfo ="ZebraHandle is NULL";
2188 void zebra_shadow_enable(ZebraHandle zh, int value)
2191 yaz_log(log_level, "zebra_shadow_enable");
2192 zh->shadow_enable = value;
2195 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2197 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2198 ZEBRA_CHECK_HANDLE(zh);
2201 if (zh->iconv_to_utf8 != 0)
2202 yaz_iconv_close(zh->iconv_to_utf8);
2203 if (zh->iconv_from_utf8 != 0)
2204 yaz_iconv_close(zh->iconv_from_utf8);
2207 yaz_iconv_open("UTF-8", encoding);
2208 if (zh->iconv_to_utf8 == 0)
2209 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2210 zh->iconv_from_utf8 =
2211 yaz_iconv_open(encoding, "UTF-8");
2212 if (zh->iconv_to_utf8 == 0)
2213 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2218 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2220 yaz_log(log_level, "zebra_record_encoding");
2221 ZEBRA_CHECK_HANDLE(zh);
2222 xfree(zh->record_encoding);
2223 zh->record_encoding = 0;
2225 zh->record_encoding = xstrdup(encoding);
2229 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2233 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2235 res_set(zh->res, name, value);
2238 const char *zebra_get_resource(ZebraHandle zh,
2239 const char *name, const char *defaultvalue)
2244 v = res_get_def(zh->res, name,(char *)defaultvalue);
2245 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2249 /* moved from zebra_api_ext.c by pop */
2250 /* FIXME: Should this really be public??? -Heikki */
2252 int zebra_trans_no(ZebraHandle zh)
2254 yaz_log(log_level, "zebra_trans_no");
2256 return zh->trans_no;
2259 int zebra_get_shadow_enable(ZebraHandle zh)
2261 yaz_log(log_level, "zebra_get_shadow_enable");
2263 return zh->shadow_enable;
2266 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2268 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2270 zh->shadow_enable = value;
2273 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2274 const char *buf, int buf_size)
2276 return zebra_update_record(zh, action_update,
2277 0 /* record type */,
2284 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2285 enum zebra_recctrl_action_t action,
2286 const char *recordType,
2287 zint *sysno, const char *match,
2289 const char *buf, int buf_size)
2293 ZEBRA_CHECK_HANDLE(zh);
2297 yaz_log(log_level, "zebra_update_record");
2299 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2302 buf_size = strlen(buf);
2304 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2306 res = zebra_buffer_extract_record(zh, buf, buf_size,
2312 if (zebra_end_trans(zh) != ZEBRA_OK)
2314 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2320 /* ---------------------------------------------------------------------------
2324 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2325 const char *setname, zint *hits)
2328 ZEBRA_RES res = ZEBRA_OK;
2333 ZEBRA_CHECK_HANDLE(zh);
2335 odr = odr_createmem(ODR_ENCODE);
2340 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2342 query = p_query_rpn(odr, pqf_query);
2346 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2347 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2351 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2355 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2363 /* ---------------------------------------------------------------------------
2364 Sort - a simplified interface, with optional read locks.
2366 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2367 const char *sort_spec,
2368 const char *output_setname,
2369 const char **input_setnames)
2371 int num_input_setnames = 0;
2372 int sort_status = 0;
2373 Z_SortKeySpecList *sort_sequence;
2375 ZEBRA_CHECK_HANDLE(zh);
2378 assert(output_setname);
2379 assert(input_setnames);
2380 sort_sequence = yaz_sort_spec(stream, sort_spec);
2381 yaz_log(log_level, "sort (FIXME) ");
2384 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2385 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2389 /* we can do this, since the perl typemap code for char** will
2390 put a NULL at the end of list */
2391 while (input_setnames[num_input_setnames]) num_input_setnames++;
2393 if (zebra_begin_read(zh))
2396 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2397 output_setname, sort_sequence, &sort_status);
2403 /* ---------------------------------------------------------------------------
2404 Get BFS for Zebra system (to make alternative storage methods)
2406 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2409 return zh->reg->bfs;
2414 /* ---------------------------------------------------------------------------
2415 Set limit for search/scan
2417 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2419 ZEBRA_CHECK_HANDLE(zh);
2420 zebra_limit_destroy(zh->m_limit);
2421 zh->m_limit = zebra_limit_create(complement_flag, ids);
2426 Set Error code + addinfo
2428 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2433 nmem_reset(zh->nmem_error);
2434 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2437 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2440 sprintf(vstr, ZINT_FORMAT, i);
2443 nmem_reset(zh->nmem_error);
2444 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2447 void zebra_lock_prefix(Res res, char *path)
2449 const char *lock_dir = res_get_def(res, "lockDir", "");
2451 strcpy(path, lock_dir);
2452 if (*path && path[strlen(path)-1] != '/')
2459 * c-file-style: "Stroustrup"
2460 * indent-tabs-mode: nil
2462 * vim: shiftwidth=4 tabstop=8 expandtab