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 *compression_str = 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 compression_str = res_get_def(res, "recordCompression", "none");
424 if (!strcmp(compression_str, "none"))
425 record_compression = REC_COMPRESS_NONE;
426 else if (!strcmp(compression_str, "bzip2"))
427 record_compression = REC_COMPRESS_BZIP2;
428 else if (!strcmp(compression_str, "zlib"))
429 record_compression = REC_COMPRESS_ZLIB;
432 yaz_log(YLOG_FATAL, "invalid recordCompression: %s", compression_str);
437 const char *index_fname = res_get_def(res, "index", "default.idx");
438 if (index_fname && *index_fname && strcmp(index_fname, "none"))
440 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
445 zebra_maps_define_default_sort(reg->zebra_maps);
449 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
451 yaz_log(YLOG_WARN, "rec_open failed");
456 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
458 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
460 yaz_log(YLOG_WARN, "dict_open failed");
465 if (res_get_match(res, "sortindex", "f", "f"))
466 sort_type = ZEBRA_SORT_TYPE_FLAT;
467 else if (res_get_match(res, "sortindex", "i", "f"))
468 sort_type = ZEBRA_SORT_TYPE_ISAMB;
469 else if (res_get_match(res, "sortindex", "m", "f"))
470 sort_type = ZEBRA_SORT_TYPE_MULTI;
473 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
478 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
480 yaz_log(YLOG_WARN, "zebra_sort_open failed");
483 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
485 struct ISAMS_M_s isams_m;
486 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
487 key_isams_m(res, &isams_m))))
489 yaz_log(YLOG_WARN, "isams_open failed");
493 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
495 struct ISAMC_M_s isamc_m;
496 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
497 rw, key_isamc_m(res, &isamc_m))))
499 yaz_log(YLOG_WARN, "isamc_open failed");
503 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
505 struct ISAMC_M_s isamc_m;
507 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
508 rw, key_isamc_m(res, &isamc_m), 0)))
510 yaz_log(YLOG_WARN, "isamb_open failed");
514 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
516 struct ISAMC_M_s isamc_m;
518 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
519 rw, key_isamc_m(res, &isamc_m), 1)))
521 yaz_log(YLOG_WARN, "isamb_open failed");
525 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
527 struct ISAMC_M_s isamc_m;
529 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
530 rw, key_isamc_m(res, &isamc_m), -1)))
532 yaz_log(YLOG_WARN, "isamb_open failed");
538 reg->zei = zebraExplain_open(reg->records, reg->dh,
540 zebra_extract_explain);
543 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
550 zebra_register_close(zs, reg);
553 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
557 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
559 ZEBRA_CHECK_HANDLE(zh);
560 yaz_log(log_level, "zebra_admin_shutdown");
562 zebra_mutex_cond_lock(&zh->service->session_lock);
563 zh->service->stop_flag = 1;
564 zebra_mutex_cond_unlock(&zh->service->session_lock);
568 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
571 ZEBRA_CHECK_HANDLE(zh);
572 yaz_log(log_level, "zebra_admin_start");
574 zebra_mutex_cond_lock(&zs->session_lock);
575 zebra_mutex_cond_unlock(&zs->session_lock);
579 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
583 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
587 zebraExplain_close(reg->zei);
588 dict_close(reg->dict);
590 dict_close(reg->matchDict);
591 zebra_sort_close(reg->sort_index);
593 isams_close(reg->isams);
595 isamc_close(reg->isamc);
597 isamb_close(reg->isamb);
598 rec_close(®->records);
600 recTypes_destroy(reg->recTypes);
601 zebra_maps_close(reg->zebra_maps);
602 zebraRankDestroy(reg);
603 bfs_destroy(reg->bfs);
604 data1_destroy(reg->dh);
606 zebra_rec_keys_close(reg->keys);
607 zebra_rec_keys_close(reg->sortKeys);
609 key_block_destroy(®->key_block);
614 ZEBRA_RES zebra_stop(ZebraService zs)
620 zebra_close(zs->sessions);
623 zebra_mutex_cond_destroy(&zs->session_lock);
626 passwd_db_close(zs->passwd_db);
628 recTypeClass_destroy(zs->record_classes);
629 nmem_destroy(zs->nmem);
630 res_close(zs->global_res);
632 yaz_timing_stop(zs->timing);
633 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
634 yaz_timing_get_real(zs->timing),
635 yaz_timing_get_user(zs->timing),
636 yaz_timing_get_sys(zs->timing));
639 yaz_timing_destroy(&zs->timing);
644 ZEBRA_RES zebra_close(ZebraHandle zh)
647 struct zebra_session **sp;
650 yaz_log(log_level, "zebra_close");
651 ZEBRA_CHECK_HANDLE(zh);
656 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
657 resultSetDestroy(zh, -1, 0, 0);
660 zebra_register_close(zh->service, zh->reg);
662 res_close(zh->session_res);
664 xfree(zh->record_encoding);
666 xfree(zh->dbaccesslist);
668 for (i = 0; i < zh->num_basenames; i++)
669 xfree(zh->basenames[i]);
670 xfree(zh->basenames);
672 if (zh->iconv_to_utf8 != 0)
673 yaz_iconv_close(zh->iconv_to_utf8);
674 if (zh->iconv_from_utf8 != 0)
675 yaz_iconv_close(zh->iconv_from_utf8);
677 zebra_mutex_cond_lock(&zs->session_lock);
678 zebra_lock_destroy(zh->lock_normal);
679 zebra_lock_destroy(zh->lock_shadow);
691 zebra_mutex_cond_unlock(&zs->session_lock);
693 xfree(zh->user_perm);
694 zh->service = 0; /* more likely to trigger an assert */
696 zebra_limit_destroy(zh->m_limit);
698 nmem_destroy(zh->nmem_error);
705 struct map_baseinfo {
711 char **new_basenames;
715 static void zebra_open_res(ZebraHandle zh)
723 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
724 zh->res = res_open(zh->session_res, 0);
725 res_read_file(zh->res, fname);
727 else if (*zh->reg_name == 0)
729 zh->res = res_open(zh->session_res, 0);
733 yaz_log(YLOG_WARN, "no register root specified");
734 zh->res = 0; /* no path for register - fail! */
738 static void zebra_close_res(ZebraHandle zh)
746 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
750 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
754 assert(zh->reg == 0);
755 assert(*zh->reg_name == 0);
761 resultSetInvalidate(zh);
762 zebra_register_close(zh->service, zh->reg);
768 zh->reg_name = xstrdup(new_reg);
772 if (zh->service->path_root)
774 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
775 strlen(zh->reg_name) + 3);
776 strcpy(zh->path_reg, zh->service->path_root);
779 strcat(zh->path_reg, "/");
780 strcat(zh->path_reg, zh->reg_name);
786 zebra_lock_destroy(zh->lock_normal);
790 zebra_lock_destroy(zh->lock_shadow);
796 const char *lock_area = res_get(zh->res, "lockDir");
798 if (!lock_area && zh->path_reg)
799 res_set(zh->res, "lockDir", zh->path_reg);
800 sprintf(fname, "norm.%s.LCK", zh->reg_name);
802 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
804 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
806 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
808 if (!zh->lock_normal || !zh->lock_shadow)
812 zebra_lock_destroy(zh->lock_normal);
817 zebra_lock_destroy(zh->lock_shadow);
826 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
827 zebra_set_approx_limit(zh, approx);
831 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
832 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
836 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
839 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
840 zh->m_segment_indexing);
845 void map_basenames_func(void *vp, const char *name, const char *value)
847 struct map_baseinfo *p = (struct map_baseinfo *) vp;
849 char fromdb[128], todb[8][128];
856 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
857 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
858 todb[5], todb[6], todb[7]);
862 for (i = 0; i<p->num_bases; i++)
863 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
866 for (i = 0; i < no; i++)
868 if (p->new_num_bases == p->new_num_max)
870 p->new_basenames[(p->new_num_bases)++] =
871 nmem_strdup(p->mem, todb[i]);
877 int zebra_select_default_database(ZebraHandle zh)
881 /* no database has been selected - so we select based on
882 resource setting (including group)
884 const char *group = res_get(zh->session_res, "group");
885 const char *v = res_get_prefix(zh->session_res,
886 "database", group, "Default");
887 return zebra_select_database(zh, v);
892 void map_basenames(ZebraHandle zh, ODR stream,
893 int *num_bases, char ***basenames)
895 struct map_baseinfo info;
896 struct map_baseinfo *p = &info;
899 yaz_log(log_level, "map_basenames ");
904 info.num_bases = *num_bases;
905 info.basenames = *basenames;
906 info.new_num_max = 128;
907 info.new_num_bases = 0;
908 info.new_basenames = (char **)
909 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
910 info.mem = stream->mem;
912 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
914 for (i = 0; i<p->num_bases; i++)
915 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
917 p->new_basenames[(p->new_num_bases)++] =
918 nmem_strdup(p->mem, p->basenames[i]);
920 *num_bases = info.new_num_bases;
921 *basenames = info.new_basenames;
922 for (i = 0; i<*num_bases; i++)
923 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
926 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
928 ZEBRA_CHECK_HANDLE(zh);
930 yaz_log(log_level, "zebra_select_database %s",basename);
932 return zebra_select_databases(zh, 1, &basename);
935 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
936 const char **basenames)
943 ZEBRA_CHECK_HANDLE(zh);
946 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
947 num_bases,basenames[0]);
952 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
956 /* Check if the user has access to all databases (Seb) */
957 /* You could argue that this should happen later, after we have
958 * determined that the database(s) exist. */
959 if (zh->dbaccesslist) {
960 for (i = 0; i < num_bases; i++) {
961 const char *db = basenames[i];
963 for (p = zh->dbaccesslist; p && *p; p = pp) {
965 if ((pp = strchr(p, '+'))) {
971 if (len == strlen(db) && !strncmp(db, p, len))
975 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
981 for (i = 0; i < zh->num_basenames; i++)
982 xfree(zh->basenames[i]);
983 xfree(zh->basenames);
985 zh->num_basenames = num_bases;
986 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
987 for (i = 0; i < zh->num_basenames; i++)
988 zh->basenames[i] = xstrdup(basenames[i]);
990 cp = strrchr(basenames[0], '/');
993 len = cp - basenames[0];
994 new_reg = xmalloc(len + 1);
995 memcpy(new_reg, basenames[0], len);
999 new_reg = xstrdup("");
1000 for (i = 1; i<num_bases; i++)
1004 cp1 = strrchr(basenames[i], '/');
1009 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1012 if (len != cp1 - basenames[i] ||
1013 memcmp(basenames[i], new_reg, len))
1015 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1023 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1028 zebra_select_register(zh, new_reg);
1032 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1035 if (!zh->lock_normal || !zh->lock_shadow)
1037 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1043 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1045 if (approx_limit == 0)
1046 approx_limit = DEFAULT_APPROX_LIMIT;
1047 zh->approx_limit = approx_limit;
1051 void zebra_set_partial_result(ZebraHandle zh)
1053 zh->partial_result = 1;
1057 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1058 int (*f)(void *client_data),
1061 zh->break_handler_func = f;
1062 zh->break_handler_data = client_data;
1066 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1067 const char *setname, zint *hits,
1068 int *estimated_hit_count,
1069 int *partial_resultset)
1073 ZEBRA_CHECK_HANDLE(zh);
1079 yaz_log(log_level, "zebra_search_rpn");
1081 zh->partial_result = 0;
1083 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1086 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1087 zh->num_basenames, zh->basenames, setname,
1088 hits, estimated_hit_count);
1090 *partial_resultset = zh->partial_result;
1095 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1096 const char *setname, zint *hits)
1098 int estimated_hit_count;
1099 int partial_resultset;
1100 return zebra_search_RPN_x(zh, o, query, setname, hits,
1101 &estimated_hit_count,
1102 &partial_resultset);
1105 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1106 const char *setname,
1107 Z_RecordComposition *comp,
1108 const Odr_oid *input_format, int num_recs,
1109 ZebraRetrievalRecord *recs)
1111 ZebraMetaRecord *poset;
1113 ZEBRA_RES ret = ZEBRA_OK;
1116 ZEBRA_CHECK_HANDLE(zh);
1122 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1126 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1131 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1134 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1135 for (i = 0; i<num_recs; i++)
1136 pos_array[i] = recs[i].position;
1137 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1140 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1141 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1147 WRBUF addinfo_w = wrbuf_alloc();
1148 for (i = 0; i < num_recs; i++)
1150 recs[i].errCode = 0;
1151 recs[i].errString = 0;
1156 recs[i].sysno = poset[i].sysno;
1159 recs[i].format = yaz_oid_recsyn_sutrs;
1160 recs[i].len = strlen(poset[i].term);
1161 recs[i].buf = poset[i].term;
1162 recs[i].base = poset[i].db;
1164 else if (poset[i].sysno)
1168 zebra_snippets *hit_snippet = zebra_snippets_create();
1170 /* we disable hit snippets for now. It does not work well
1171 and it slows retrieval down a lot */
1173 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1176 wrbuf_rewind(addinfo_w);
1178 zebra_record_fetch(zh, setname,
1179 poset[i].sysno, poset[i].score,
1180 stream, input_format, comp,
1181 &recs[i].format, &buf, &len,
1182 &recs[i].base, addinfo_w);
1184 if (wrbuf_len(addinfo_w))
1186 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1190 recs[i].buf = (char*) odr_malloc(stream, len);
1191 memcpy(recs[i].buf, buf, len);
1195 recs[i].score = poset[i].score;
1196 zebra_snippets_destroy(hit_snippet);
1200 /* only need to set it once */
1201 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1203 zebra_setError_zint(zh,
1204 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1211 zebra_meta_records_destroy(zh, poset, num_recs);
1212 wrbuf_destroy(addinfo_w);
1219 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1221 int *num_entries, ZebraScanEntry **entries,
1223 const char *setname)
1225 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1226 Z_AttributesPlusTerm *zapt;
1227 Odr_oid *attributeSet;
1230 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1233 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1237 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1238 position, num_entries, entries, is_partial,
1241 yaz_pqf_destroy(pqf_parser);
1245 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1246 const Odr_oid *attributeset,
1248 int *num_entries, ZebraScanEntry **entries,
1250 const char *setname)
1254 ZEBRA_CHECK_HANDLE(zh);
1259 assert(num_entries);
1262 yaz_log(log_level, "zebra_scan");
1264 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1271 res = rpn_scan(zh, stream, zapt, attributeset,
1272 zh->num_basenames, zh->basenames, position,
1273 num_entries, entries, is_partial, setname);
1278 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1279 int num_input_setnames, const char **input_setnames,
1280 const char *output_setname,
1281 Z_SortKeySpecList *sort_sequence,
1285 ZEBRA_CHECK_HANDLE(zh);
1287 assert(num_input_setnames>0);
1288 assert(input_setnames);
1289 assert(sort_sequence);
1290 assert(sort_status);
1291 yaz_log(log_level, "zebra_sort");
1293 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1295 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1296 output_setname, sort_sequence, sort_status);
1301 int zebra_deleteResultSet(ZebraHandle zh, int function,
1302 int num_setnames, char **setnames,
1307 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1309 if (zebra_begin_read(zh))
1310 return Z_DeleteStatus_systemProblemAtTarget;
1313 case Z_DeleteResultSetRequest_list:
1314 assert(num_setnames>0);
1316 resultSetDestroy(zh, num_setnames, setnames, statuses);
1318 case Z_DeleteResultSetRequest_all:
1319 resultSetDestroy(zh, -1, 0, statuses);
1323 status = Z_DeleteStatus_success;
1324 for (i = 0; i<num_setnames; i++)
1325 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1326 status = statuses[i];
1330 int zebra_errCode(ZebraHandle zh)
1334 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1337 yaz_log(log_level, "zebra_errCode: o");
1341 const char *zebra_errString(ZebraHandle zh)
1345 e= diagbib1_str(zh->errCode);
1346 yaz_log(log_level, "zebra_errString: %s",e);
1350 char *zebra_errAdd(ZebraHandle zh)
1355 yaz_log(log_level, "zebra_errAdd: %s",a);
1359 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1362 const char *astring;
1366 ZEBRA_CHECK_HANDLE(zh);
1370 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1371 p = res_get(zs->global_res, u);
1372 xfree(zh->user_perm);
1373 zh->user_perm = xstrdup(p ? p : "r");
1375 /* Determine database access list */
1376 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1378 zh->dbaccesslist = xstrdup(astring);
1380 zh->dbaccesslist = 0;
1382 /* users that don't require a password .. */
1383 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1386 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1391 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1392 const char *record_type)
1394 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1395 database, record_type);
1396 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1398 return zebra_begin_trans(zh, 1);
1401 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1403 ZEBRA_CHECK_HANDLE(zh);
1404 yaz_log(log_level, "zebra_admin_import_end");
1405 return zebra_end_trans(zh);
1408 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1410 ZEBRA_RES res = ZEBRA_OK;
1413 ZEBRA_CHECK_HANDLE(zh);
1414 yaz_log(log_level, "zebra_admin_import_segment");
1416 for (i = 0; i<segment->num_segmentRecords; i++)
1418 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1420 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1422 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1423 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1425 Odr_oct *oct = fragment->u.notExternallyTagged;
1428 if(zebra_update_record(
1431 0, /* record Type */
1435 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1443 int delete_w_handle(const char *info, void *handle)
1445 ZebraHandle zh = (ZebraHandle) handle;
1448 if (*info == sizeof(pos))
1450 memcpy(&pos, info+1, sizeof(pos));
1451 isamb_unlink(zh->reg->isamb, pos);
1456 int delete_w_all_handle(const char *info, void *handle)
1458 ZebraHandle zh = (ZebraHandle) handle;
1461 if (*info == sizeof(pos))
1464 memcpy(&pos, info+1, sizeof(pos));
1465 pt = isamb_pp_open(zh->reg->isamb, pos, 2);
1470 while (isamb_pp_read(pt, &key))
1473 rec = rec_get(zh->reg->records, key.mem[0]);
1474 rec_del(zh->reg->records, &rec);
1479 return delete_w_handle(info, handle);
1482 static int delete_SU_handle(void *handle, int ord,
1483 const char *index_type, const char *string_index,
1484 zinfo_index_category_t cat)
1486 ZebraHandle zh = (ZebraHandle) handle;
1490 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s cat=%d", ord,
1491 index_type, string_index, (int) cat);
1493 ord_len = key_SU_encode(ord, ord_buf);
1494 ord_buf[ord_len] = '\0';
1496 assert(zh->reg->isamb);
1497 assert(zh->reg->records);
1498 dict_delete_subtree(zh->reg->dict, ord_buf,
1500 !strcmp(string_index, "_ALLRECORDS") ?
1501 delete_w_all_handle : delete_w_handle);
1505 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1507 ZEBRA_RES ret = ZEBRA_OK;
1509 yaz_log(log_level, "zebra_drop_database %s", db);
1510 ZEBRA_CHECK_HANDLE(zh);
1512 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1514 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1519 if (zebraExplain_curDatabase(zh->reg->zei, db))
1521 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1526 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1527 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1528 0 /* handle */, 0 /* func */);
1529 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1530 zebraExplain_removeDatabase(zh->reg->zei, zh);
1531 zebra_remove_file_match(zh);
1536 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1537 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1538 "drop database only supported for isam:b");
1541 if (zebra_end_trans(zh) != ZEBRA_OK)
1543 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1549 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1551 yaz_log(log_level, "zebra_create_database %s", db);
1552 ZEBRA_CHECK_HANDLE(zh);
1555 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1557 if (zebra_begin_trans(zh, 1))
1560 /* announce database */
1561 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1562 /* explainDatabase */))
1564 if (zebra_end_trans(zh) != ZEBRA_OK)
1566 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1568 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1571 return zebra_end_trans(zh);
1574 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1575 const char *input_str, int input_len,
1576 char *output_str, int output_len)
1579 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1583 yaz_log(log_level, "zebra_string_norm ");
1585 if (!zh->reg->zebra_maps)
1587 wrbuf = zebra_replace(zm, "", input_str, input_len);
1590 if (wrbuf_len(wrbuf) >= output_len)
1592 if (wrbuf_len(wrbuf))
1593 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1594 output_str[wrbuf_len(wrbuf)] = '\0';
1595 return wrbuf_len(wrbuf);
1598 /** \brief set register state (state*.LCK)
1599 \param zh Zebra handle
1601 \param seqno sequence number
1604 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1606 c=commit (writing to register, reading from shadow, shadow mode only)
1608 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1610 char state_fname[256];
1615 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1617 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1618 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1619 f = fopen(fname, "w");
1621 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1622 fprintf(f, "%c %d %ld\n", val, seqno, p);
1627 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1629 char state_fname[256];
1634 yaz_log(log_level, "zebra_get_state ");
1636 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1637 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1638 f = fopen(fname, "r");
1644 if (fscanf(f, "%c %d", val, seqno) != 2)
1646 yaz_log(YLOG_ERRNO|YLOG_WARN, "fscan fail %s",
1654 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1656 return zebra_begin_trans(zh, 0);
1659 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1661 return zebra_end_trans(zh);
1664 static void read_res_for_transaction(ZebraHandle zh)
1666 const char *group = res_get(zh->res, "group");
1668 /* FIXME - do we still use groups ?? */
1670 zh->m_group = group;
1671 v = res_get_prefix(zh->res, "followLinks", group, "1");
1672 zh->m_follow_links = atoi(v);
1674 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1675 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1677 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1678 zh->m_store_keys = atoi(v);
1680 v = res_get_prefix(zh->res, "storeData", group, "1");
1681 zh->m_store_data = atoi(v);
1683 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1684 zh->m_explain_database = atoi(v);
1686 v = res_get_prefix(zh->res, "openRW", group, "1");
1687 zh->m_flag_rw = atoi(v);
1689 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1690 zh->m_file_verbose_limit = atoi(v);
1693 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1695 ZEBRA_CHECK_HANDLE(zh);
1696 zebra_select_default_database(zh);
1699 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1700 "zebra_begin_trans: no database selected");
1704 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1708 if (rw && !strchr(zh->user_perm, 'w'))
1712 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1723 const char *rval = 0;
1728 read_res_for_transaction(zh);
1731 if (zh->trans_no != 1)
1733 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1734 "zebra_begin_trans: no write trans within read");
1739 resultSetInvalidate(zh);
1740 zebra_register_close(zh->service, zh->reg);
1742 zh->trans_w_no = zh->trans_no;
1744 zh->records_inserted = 0;
1745 zh->records_updated = 0;
1746 zh->records_deleted = 0;
1747 zh->records_processed = 0;
1748 zh->records_skipped = 0;
1750 #if HAVE_SYS_TIMES_H
1754 if (zh->shadow_enable)
1755 rval = res_get(zh->res, "shadow");
1759 zebra_lock_r(zh->lock_normal);
1760 zebra_lock_w(zh->lock_shadow);
1764 zebra_lock_w(zh->lock_normal);
1765 zebra_lock_w(zh->lock_shadow);
1767 zebra_get_state(zh, &val, &seqno);
1770 /* either we didn't finish commit or shadow is dirty */
1773 yaz_log(YLOG_WARN, "previous transaction did not finish "
1774 "(shadow disabled)");
1776 zebra_unlock(zh->lock_shadow);
1777 zebra_unlock(zh->lock_normal);
1778 if (zebra_commit(zh))
1786 zebra_lock_r(zh->lock_normal);
1787 zebra_lock_w(zh->lock_shadow);
1791 zebra_lock_w(zh->lock_normal);
1792 zebra_lock_w(zh->lock_shadow);
1796 zebra_set_state(zh, 'd', seqno);
1798 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1799 1, rval ? 1 : 0, zh->res,
1802 zh->reg->seqno = seqno;
1805 zebra_set_state(zh, 'o', seqno);
1807 zebra_unlock(zh->lock_shadow);
1808 zebra_unlock(zh->lock_normal);
1813 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1814 "zebra_begin_trans: cannot open register");
1815 yaz_log(YLOG_FATAL, "%s", zh->errString);
1818 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1828 if (zh->trans_no != 1)
1830 return zebra_flush_reg(zh);
1832 #if HAVE_SYS_TIMES_H
1838 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1841 if (!zh->lock_normal || !zh->lock_shadow)
1844 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1847 zebra_get_state(zh, &val, &seqno);
1853 else if (seqno != zh->reg->seqno)
1855 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1856 seqno, zh->reg->seqno);
1859 else if (zh->reg->last_val != val)
1861 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1862 val, zh->reg->last_val);
1869 zebra_lock_r(zh->lock_shadow);
1871 zebra_lock_r(zh->lock_normal);
1875 resultSetInvalidate(zh);
1876 zebra_register_close(zh->service, zh->reg);
1878 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1879 0, val == 'c' ? 1 : 0,
1880 zh->res, zh->path_reg);
1883 zebra_unlock(zh->lock_normal);
1884 zebra_unlock(zh->lock_shadow);
1886 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1889 zh->reg->last_val = val;
1890 zh->reg->seqno = seqno;
1892 read_res_for_transaction(zh);
1896 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1898 ZebraTransactionStatus dummy;
1900 yaz_log(log_level, "zebra_end_trans");
1901 ZEBRA_CHECK_HANDLE(zh);
1902 return zebra_end_transaction(zh, &dummy);
1905 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1911 ZEBRA_CHECK_HANDLE(zh);
1914 yaz_log(log_level, "zebra_end_transaction");
1916 status->processed = 0;
1917 status->inserted = 0;
1918 status->updated = 0;
1919 status->deleted = 0;
1923 if (!zh->res || !zh->reg)
1925 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1926 "zebra_end_trans: no open transaction");
1929 if (zh->trans_no != zh->trans_w_no)
1932 if (zh->trans_no != 0)
1935 /* release read lock */
1937 zebra_unlock(zh->lock_normal);
1938 zebra_unlock(zh->lock_shadow);
1941 { /* release write lock */
1945 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1946 rval = res_get(zh->res, "shadow");
1948 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1950 zebra_flush_reg(zh);
1952 resultSetInvalidate(zh);
1954 zebra_register_close(zh->service, zh->reg);
1957 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1958 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1959 zh->records_processed, zh->records_inserted,
1960 zh->records_updated, zh->records_deleted);
1962 status->processed = zh->records_processed;
1963 status->inserted = zh->records_inserted;
1964 status->updated = zh->records_updated;
1965 status->deleted = zh->records_deleted;
1967 zebra_get_state(zh, &val, &seqno);
1970 BFiles bfs = bfs_create(rval, zh->path_reg);
1971 bf_commitClean(bfs, rval);
1976 zebra_set_state(zh, 'o', seqno);
1977 zebra_unlock(zh->lock_shadow);
1978 zebra_unlock(zh->lock_normal);
1981 #if HAVE_SYS_TIMES_H
1983 yaz_log(log_level, "user/system: %ld/%ld",
1984 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1985 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1987 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1988 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1993 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1995 return zebra_repository_index(zh, path, action_update);
1998 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2000 return zebra_repository_index(zh, path, action_delete);
2003 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
2004 enum zebra_recctrl_action_t action)
2009 if (action == action_update)
2010 yaz_log(log_level, "updating %s", path);
2011 else if (action == action_delete)
2012 yaz_log(log_level, "deleting %s", path);
2013 else if (action == action_a_delete)
2014 yaz_log(log_level, "attempt deleting %s", path);
2016 yaz_log(log_level, "update action=%d", (int) action);
2018 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
2019 return zebra_update_file_match(zh, path);
2021 return zebra_update_from_path(zh, path, action);
2024 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2028 yaz_log(log_level, "zebra_repository_show");
2029 repositoryShow(zh, path);
2033 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2039 ZEBRA_RES res = ZEBRA_OK;
2043 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
2044 zebra_select_default_database(zh);
2047 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2050 rval = res_get(zh->res, "shadow");
2053 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2057 zebra_lock_w(zh->lock_normal);
2058 zebra_lock_r(zh->lock_shadow);
2060 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2063 zebra_unlock(zh->lock_shadow);
2064 zebra_unlock(zh->lock_normal);
2067 zebra_get_state(zh, &val, &seqno);
2071 /* shadow area is dirty and so we must throw it away */
2072 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2075 else if (val == 'c')
2077 /* commit has started. We can not remove it anymore */
2082 bf_cache(bfs, rval);
2083 if (bf_commitExists(bfs))
2086 zebra_set_state(zh, 'd', seqno);
2089 zebra_set_state(zh, 'c', seqno);
2091 yaz_log(log_level, "commit start");
2092 if (bf_commitExec(bfs))
2095 if (res == ZEBRA_OK)
2098 zebra_set_state(zh, 'o', seqno);
2100 zebra_unlock(zh->lock_shadow);
2101 zebra_unlock(zh->lock_normal);
2103 zebra_lock_w(zh->lock_shadow);
2104 bf_commitClean(bfs, rval);
2105 zebra_unlock(zh->lock_shadow);
2109 zebra_unlock(zh->lock_shadow);
2110 zebra_unlock(zh->lock_normal);
2111 yaz_log(YLOG_WARN, "zebra_commit: failed");
2116 zebra_unlock(zh->lock_shadow);
2117 zebra_unlock(zh->lock_normal);
2118 yaz_log(log_level, "nothing to commit");
2125 ZEBRA_RES zebra_clean(ZebraHandle zh)
2127 yaz_log(log_level, "zebra_clean");
2128 ZEBRA_CHECK_HANDLE(zh);
2129 return zebra_commit_ex(zh, 1);
2132 ZEBRA_RES zebra_commit(ZebraHandle zh)
2134 yaz_log(log_level, "zebra_commit");
2135 ZEBRA_CHECK_HANDLE(zh);
2136 return zebra_commit_ex(zh, 0);
2140 ZEBRA_RES zebra_init(ZebraHandle zh)
2145 yaz_log(log_level, "zebra_init");
2147 ZEBRA_CHECK_HANDLE(zh);
2149 zebra_select_default_database(zh);
2152 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2153 "cannot select default database");
2156 rval = res_get(zh->res, "shadow");
2158 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2161 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2165 bf_cache(bfs, rval);
2169 zebra_set_state(zh, 'o', 0);
2173 ZEBRA_RES zebra_compact(ZebraHandle zh)
2177 yaz_log(log_level, "zebra_compact");
2178 ZEBRA_CHECK_HANDLE(zh);
2181 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2184 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2190 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2192 yaz_log(log_level, "zebra_result");
2195 *code = zh->errCode;
2196 *addinfo = zh->errString;
2200 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2201 *addinfo ="ZebraHandle is NULL";
2205 void zebra_shadow_enable(ZebraHandle zh, int value)
2208 yaz_log(log_level, "zebra_shadow_enable");
2209 zh->shadow_enable = value;
2212 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2214 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2215 ZEBRA_CHECK_HANDLE(zh);
2218 if (zh->iconv_to_utf8 != 0)
2219 yaz_iconv_close(zh->iconv_to_utf8);
2220 if (zh->iconv_from_utf8 != 0)
2221 yaz_iconv_close(zh->iconv_from_utf8);
2224 yaz_iconv_open("UTF-8", encoding);
2225 if (zh->iconv_to_utf8 == 0)
2226 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2227 zh->iconv_from_utf8 =
2228 yaz_iconv_open(encoding, "UTF-8");
2229 if (zh->iconv_to_utf8 == 0)
2230 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2235 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2237 yaz_log(log_level, "zebra_record_encoding");
2238 ZEBRA_CHECK_HANDLE(zh);
2239 xfree(zh->record_encoding);
2240 zh->record_encoding = 0;
2242 zh->record_encoding = xstrdup(encoding);
2246 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2250 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2252 res_set(zh->res, name, value);
2255 const char *zebra_get_resource(ZebraHandle zh,
2256 const char *name, const char *defaultvalue)
2261 v = res_get_def(zh->res, name,(char *)defaultvalue);
2262 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2266 /* moved from zebra_api_ext.c by pop */
2267 /* FIXME: Should this really be public??? -Heikki */
2269 int zebra_trans_no(ZebraHandle zh)
2271 yaz_log(log_level, "zebra_trans_no");
2273 return zh->trans_no;
2276 int zebra_get_shadow_enable(ZebraHandle zh)
2278 yaz_log(log_level, "zebra_get_shadow_enable");
2280 return zh->shadow_enable;
2283 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2285 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2287 zh->shadow_enable = value;
2290 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2291 const char *buf, int buf_size)
2293 return zebra_update_record(zh, action_update,
2294 0 /* record type */,
2301 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2302 enum zebra_recctrl_action_t action,
2303 const char *recordType,
2304 zint *sysno, const char *match,
2306 const char *buf, int buf_size)
2310 ZEBRA_CHECK_HANDLE(zh);
2314 yaz_log(log_level, "zebra_update_record");
2316 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2319 buf_size = strlen(buf);
2321 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2323 res = zebra_buffer_extract_record(zh, buf, buf_size,
2329 if (zebra_end_trans(zh) != ZEBRA_OK)
2331 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2337 /* ---------------------------------------------------------------------------
2341 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2342 const char *setname, zint *hits)
2345 ZEBRA_RES res = ZEBRA_OK;
2350 ZEBRA_CHECK_HANDLE(zh);
2352 odr = odr_createmem(ODR_ENCODE);
2357 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2359 query = p_query_rpn(odr, pqf_query);
2363 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2364 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2368 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2372 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2380 /* ---------------------------------------------------------------------------
2381 Sort - a simplified interface, with optional read locks.
2383 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2384 const char *sort_spec,
2385 const char *output_setname,
2386 const char **input_setnames)
2388 int num_input_setnames = 0;
2389 int sort_status = 0;
2390 Z_SortKeySpecList *sort_sequence;
2392 ZEBRA_CHECK_HANDLE(zh);
2395 assert(output_setname);
2396 assert(input_setnames);
2397 sort_sequence = yaz_sort_spec(stream, sort_spec);
2398 yaz_log(log_level, "sort (FIXME) ");
2401 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2402 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2406 /* we can do this, since the perl typemap code for char** will
2407 put a NULL at the end of list */
2408 while (input_setnames[num_input_setnames]) num_input_setnames++;
2410 if (zebra_begin_read(zh))
2413 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2414 output_setname, sort_sequence, &sort_status);
2420 /* ---------------------------------------------------------------------------
2421 Get BFS for Zebra system (to make alternative storage methods)
2423 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2426 return zh->reg->bfs;
2431 /* ---------------------------------------------------------------------------
2432 Set limit for search/scan
2434 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2436 ZEBRA_CHECK_HANDLE(zh);
2437 zebra_limit_destroy(zh->m_limit);
2438 zh->m_limit = zebra_limit_create(complement_flag, ids);
2443 Set Error code + addinfo
2445 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2450 nmem_reset(zh->nmem_error);
2451 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2454 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2457 sprintf(vstr, ZINT_FORMAT, i);
2460 nmem_reset(zh->nmem_error);
2461 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2464 void zebra_lock_prefix(Res res, char *path)
2466 const char *lock_dir = res_get_def(res, "lockDir", "");
2468 strcpy(path, lock_dir);
2469 if (*path && path[strlen(path)-1] != '/')
2476 * c-file-style: "Stroustrup"
2477 * indent-tabs-mode: nil
2479 * vim: shiftwidth=4 tabstop=8 expandtab