1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 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
35 #include <yaz/diagbib1.h>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
42 #include <idzebra/api.h>
43 #include <yaz/oid_db.h>
45 #define DEFAULT_APPROX_LIMIT 2000000000
47 /* simple asserts to validate the most essential input args */
48 #define ASSERTZH assert(zh && zh->service)
49 #define ASSERTZHRES assert(zh && zh->service && zh->res)
50 #define ASSERTZS assert(zs)
52 static int log_level = 0;
53 static int log_level_initialized = 0;
55 static void zebra_open_res(ZebraHandle zh);
56 static void zebra_close_res(ZebraHandle zh);
58 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
65 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
67 static int zebra_chdir(ZebraService zs)
72 yaz_log(log_level, "zebra_chdir");
73 dir = res_get(zs->global_res, "chdir");
76 yaz_log(YLOG_DEBUG, "chdir %s", dir);
83 yaz_log(YLOG_FATAL|YLOG_ERRNO, "chdir %s", dir);
87 static ZEBRA_RES zebra_flush_reg(ZebraHandle zh)
89 ZEBRA_CHECK_HANDLE(zh);
90 yaz_log(log_level, "zebra_flush_reg");
91 zebraExplain_flush(zh->reg->zei, zh);
93 key_block_flush(zh->reg->key_block, 1);
95 zebra_index_merge(zh);
99 static struct zebra_register *zebra_register_open(ZebraService zs,
101 int rw, int useshadow,
103 const char *reg_path);
104 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
106 const char *zebra_get_encoding(ZebraHandle zh)
108 assert(zh && zh->session_res);
109 return res_get_def(zh->session_res, "encoding", "ISO-8859-1");
112 ZebraHandle zebra_open(ZebraService zs, Res res)
115 const char *default_encoding;
116 if (!log_level_initialized)
118 log_level = yaz_log_module_level("zebraapi");
119 log_level_initialized = 1;
122 yaz_log(log_level, "zebra_open");
127 zh = (ZebraHandle) xmalloc(sizeof(*zh));
128 yaz_log(YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
131 zh->reg = 0; /* no register attached yet */
137 zh->session_res = res_open(zs->global_res, res);
139 zh->dbaccesslist = 0;
141 zh->reg_name = xstrdup("");
143 zh->num_basenames = 0;
146 zh->approx_limit = DEFAULT_APPROX_LIMIT;
153 zh->shadow_enable = 1;
154 zh->m_staticrank = 0;
155 zh->m_segment_indexing = 0;
157 zh->break_handler_func = 0;
158 zh->break_handler_data = 0;
160 default_encoding = zebra_get_encoding(zh);
163 yaz_iconv_open("UTF-8", default_encoding);
164 if (zh->iconv_to_utf8 == 0)
165 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported",
167 zh->iconv_from_utf8 =
168 yaz_iconv_open(default_encoding, "UTF-8");
169 if (zh->iconv_to_utf8 == 0)
170 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported",
173 zh->record_encoding = 0;
175 zebra_mutex_cond_lock(&zs->session_lock);
177 zh->next = zs->sessions;
180 zebra_mutex_cond_unlock(&zs->session_lock);
182 zh->store_data_buf = 0;
184 zh->m_limit = zebra_limit_create(1, 0);
186 zh->nmem_error = nmem_create();
191 ZebraService zebra_start(const char *configName)
193 return zebra_start_res(configName, 0, 0);
196 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
199 char version_str[16];
204 if (!log_level_initialized)
206 log_level = yaz_log_module_level("zebraapi");
207 log_level_initialized = 1;
212 zebra_get_version(version_str, system_str);
214 yaz_log(YLOG_LOG, "zebra_start %s %s", version_str, system_str);
216 yaz_log(YLOG_LOG, "config %s", configName);
218 if ((res = res_open(def_res, over_res)))
220 const char *passwd_plain = 0;
221 const char *passwd_encrypt = 0;
222 const char *dbaccess = 0;
227 ZEBRA_RES ret = res_read_file(res, configName);
233 if (zebra_check_res(res))
235 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
242 zebra_check_res(res);
245 zh = xmalloc(sizeof(*zh));
246 zh->global_res = res;
255 zebra_mutex_cond_init(&zh->session_lock);
256 passwd_plain = res_get(zh->global_res, "passwd");
257 passwd_encrypt = res_get(zh->global_res, "passwd.c");
258 dbaccess = res_get(zh->global_res, "dbaccess");
260 if (!passwd_plain && !passwd_encrypt)
261 zh->passwd_db = NULL;
264 zh->passwd_db = passwd_db_open();
266 yaz_log(YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
270 passwd_db_file_plain(zh->passwd_db, passwd_plain);
272 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
279 zh->dbaccess = res_open(NULL, NULL);
280 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
281 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
286 zh->timing = yaz_timing_create();
287 zh->path_root = res_get(zh->global_res, "root");
288 zh->nmem = nmem_create();
289 zh->record_classes = recTypeClass_create(zh->global_res, zh->nmem);
293 const char *module_path = res_get(res, "modulePath");
295 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
303 void zebra_filter_info(ZebraService zs, void *cd,
304 void(*cb)(void *cd, const char *name))
308 recTypeClass_info(zs->record_classes, cd, cb);
311 void zebra_pidfname(ZebraService zs, char *path)
314 zebra_lock_prefix(zs->global_res, path);
315 strcat(path, "zebrasrv.pid");
318 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
319 int compact_flag, Res res)
321 int page_size = 4096;
322 char resource_str[200];
323 sprintf(resource_str, "dict.%.100s.pagesize", name);
327 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
328 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
330 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
334 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
335 int rw, int useshadow, Res res,
336 const char *reg_path)
338 struct zebra_register *reg;
339 int record_compression = REC_COMPRESS_NONE;
340 const char *compression_str = 0;
341 const char *profilePath;
342 int sort_type = ZEBRA_SORT_TYPE_FLAT;
343 ZEBRA_RES ret = ZEBRA_OK;
347 reg = xmalloc(sizeof(*reg));
350 reg->name = xstrdup(name);
357 yaz_log(YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
358 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
360 reg->dh = data1_create();
367 reg->bfs = bfs_create(res_get(res, "register"), reg_path);
370 data1_destroy(reg->dh);
377 if (bf_cache(reg->bfs, res_get(res, "shadow")) == ZEBRA_FAIL)
379 bfs_destroy(reg->bfs);
380 data1_destroy(reg->dh);
387 profilePath = res_get_def(res, "profilePath", 0);
389 data1_set_tabpath(reg->dh, profilePath);
390 data1_set_tabroot(reg->dh, reg_path);
391 reg->recTypes = recTypes_init(zs->record_classes, reg->dh);
394 zebra_maps_open(res, reg_path, profilePath);
395 if (!reg->zebra_maps)
397 recTypes_destroy(reg->recTypes);
398 bfs_destroy(reg->bfs);
399 data1_destroy(reg->dh);
404 reg->rank_classes = NULL;
407 reg->keys = zebra_rec_keys_open();
409 reg->sortKeys = zebra_rec_keys_open();
420 /* installing rank classes */
421 zebraRankInstall(reg, rank_1_class);
422 zebraRankInstall(reg, rank_2_class);
423 zebraRankInstall(reg, rank_similarity_class);
424 zebraRankInstall(reg, rank_static_class);
426 compression_str = res_get_def(res, "recordCompression", "none");
427 if (!strcmp(compression_str, "none"))
428 record_compression = REC_COMPRESS_NONE;
429 else if (!strcmp(compression_str, "bzip2"))
430 record_compression = REC_COMPRESS_BZIP2;
431 else if (!strcmp(compression_str, "zlib"))
432 record_compression = REC_COMPRESS_ZLIB;
435 yaz_log(YLOG_FATAL, "invalid recordCompression: %s", compression_str);
439 if (!rec_check_compression_method(record_compression))
441 yaz_log(YLOG_FATAL, "unsupported recordCompression: %s",
447 const char *index_fname = res_get_def(res, "index", "default.idx");
448 if (index_fname && *index_fname && strcmp(index_fname, "none"))
450 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
455 zebra_maps_define_default_sort(reg->zebra_maps);
459 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
461 yaz_log(YLOG_WARN, "rec_open failed");
466 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
468 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
470 yaz_log(YLOG_WARN, "dict_open failed");
475 if (res_get_match(res, "sortindex", "f", "f"))
476 sort_type = ZEBRA_SORT_TYPE_FLAT;
477 else if (res_get_match(res, "sortindex", "i", "f"))
478 sort_type = ZEBRA_SORT_TYPE_ISAMB;
479 else if (res_get_match(res, "sortindex", "m", "f"))
480 sort_type = ZEBRA_SORT_TYPE_MULTI;
483 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
488 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
490 yaz_log(YLOG_WARN, "zebra_sort_open failed");
493 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
495 struct ISAMS_M_s isams_m;
496 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
497 key_isams_m(res, &isams_m))))
499 yaz_log(YLOG_WARN, "isams_open failed");
503 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
505 struct ISAMC_M_s isamc_m;
506 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
507 rw, key_isamc_m(res, &isamc_m))))
509 yaz_log(YLOG_WARN, "isamc_open failed");
513 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
515 struct ISAMC_M_s isamc_m;
517 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
518 rw, key_isamc_m(res, &isamc_m), 0)))
520 yaz_log(YLOG_WARN, "isamb_open failed");
524 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
526 struct ISAMC_M_s isamc_m;
528 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
529 rw, key_isamc_m(res, &isamc_m), 1)))
531 yaz_log(YLOG_WARN, "isamb_open failed");
535 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
537 struct ISAMC_M_s isamc_m;
539 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
540 rw, key_isamc_m(res, &isamc_m), -1)))
542 yaz_log(YLOG_WARN, "isamb_open failed");
548 reg->zei = zebraExplain_open(reg->records, reg->dh,
550 zebra_extract_explain);
553 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
560 zebra_register_close(zs, reg);
563 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
567 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
569 ZEBRA_CHECK_HANDLE(zh);
570 yaz_log(log_level, "zebra_admin_shutdown");
572 zebra_mutex_cond_lock(&zh->service->session_lock);
573 zh->service->stop_flag = 1;
574 zebra_mutex_cond_unlock(&zh->service->session_lock);
578 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
581 ZEBRA_CHECK_HANDLE(zh);
582 yaz_log(log_level, "zebra_admin_start");
584 zebra_mutex_cond_lock(&zs->session_lock);
585 zebra_mutex_cond_unlock(&zs->session_lock);
589 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
593 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
597 zebraExplain_close(reg->zei);
598 dict_close(reg->dict);
600 dict_close(reg->matchDict);
601 zebra_sort_close(reg->sort_index);
603 isams_close(reg->isams);
605 isamc_close(reg->isamc);
607 isamb_close(reg->isamb);
608 rec_close(®->records);
610 recTypes_destroy(reg->recTypes);
611 zebra_maps_close(reg->zebra_maps);
612 zebraRankDestroy(reg);
613 bfs_destroy(reg->bfs);
614 data1_destroy(reg->dh);
616 zebra_rec_keys_close(reg->keys);
617 zebra_rec_keys_close(reg->sortKeys);
619 key_block_destroy(®->key_block);
624 ZEBRA_RES zebra_stop(ZebraService zs)
630 zebra_close(zs->sessions);
633 zebra_mutex_cond_destroy(&zs->session_lock);
636 passwd_db_close(zs->passwd_db);
638 recTypeClass_destroy(zs->record_classes);
639 nmem_destroy(zs->nmem);
640 res_close(zs->global_res);
642 yaz_timing_stop(zs->timing);
643 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
644 yaz_timing_get_real(zs->timing),
645 yaz_timing_get_user(zs->timing),
646 yaz_timing_get_sys(zs->timing));
649 yaz_timing_destroy(&zs->timing);
654 ZEBRA_RES zebra_close(ZebraHandle zh)
657 struct zebra_session **sp;
660 yaz_log(log_level, "zebra_close");
661 ZEBRA_CHECK_HANDLE(zh);
666 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
667 resultSetDestroy(zh, -1, 0, 0);
670 zebra_register_close(zh->service, zh->reg);
672 res_close(zh->session_res);
674 xfree(zh->record_encoding);
676 xfree(zh->dbaccesslist);
678 for (i = 0; i < zh->num_basenames; i++)
679 xfree(zh->basenames[i]);
680 xfree(zh->basenames);
682 if (zh->iconv_to_utf8 != 0)
683 yaz_iconv_close(zh->iconv_to_utf8);
684 if (zh->iconv_from_utf8 != 0)
685 yaz_iconv_close(zh->iconv_from_utf8);
687 zebra_mutex_cond_lock(&zs->session_lock);
688 zebra_lock_destroy(zh->lock_normal);
689 zebra_lock_destroy(zh->lock_shadow);
701 zebra_mutex_cond_unlock(&zs->session_lock);
703 xfree(zh->user_perm);
704 zh->service = 0; /* more likely to trigger an assert */
706 zebra_limit_destroy(zh->m_limit);
708 nmem_destroy(zh->nmem_error);
715 struct map_baseinfo {
721 char **new_basenames;
725 static void zebra_open_res(ZebraHandle zh)
733 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
734 zh->res = res_open(zh->session_res, 0);
735 res_read_file(zh->res, fname);
737 else if (*zh->reg_name == 0)
739 zh->res = res_open(zh->session_res, 0);
743 yaz_log(YLOG_WARN, "no register root specified");
744 zh->res = 0; /* no path for register - fail! */
748 static void zebra_close_res(ZebraHandle zh)
756 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
760 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
764 assert(zh->reg == 0);
765 assert(*zh->reg_name == 0);
771 resultSetInvalidate(zh);
772 zebra_register_close(zh->service, zh->reg);
778 zh->reg_name = xstrdup(new_reg);
782 if (zh->service->path_root)
784 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
785 strlen(zh->reg_name) + 3);
786 strcpy(zh->path_reg, zh->service->path_root);
789 strcat(zh->path_reg, "/");
790 strcat(zh->path_reg, zh->reg_name);
796 zebra_lock_destroy(zh->lock_normal);
800 zebra_lock_destroy(zh->lock_shadow);
806 const char *lock_area = res_get(zh->res, "lockDir");
808 if (!lock_area && zh->path_reg)
809 res_set(zh->res, "lockDir", zh->path_reg);
810 sprintf(fname, "norm.%s.LCK", zh->reg_name);
812 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
814 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
816 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
818 if (!zh->lock_normal || !zh->lock_shadow)
822 zebra_lock_destroy(zh->lock_normal);
827 zebra_lock_destroy(zh->lock_shadow);
836 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
837 zebra_set_approx_limit(zh, approx);
841 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
842 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
846 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
849 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
850 zh->m_segment_indexing);
855 void map_basenames_func(void *vp, const char *name, const char *value)
857 struct map_baseinfo *p = (struct map_baseinfo *) vp;
859 char fromdb[128], todb[8][128];
866 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
867 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
868 todb[5], todb[6], todb[7]);
872 for (i = 0; i<p->num_bases; i++)
873 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
876 for (i = 0; i < no; i++)
878 if (p->new_num_bases == p->new_num_max)
880 p->new_basenames[(p->new_num_bases)++] =
881 nmem_strdup(p->mem, todb[i]);
887 int zebra_select_default_database(ZebraHandle zh)
891 /* no database has been selected - so we select based on
892 resource setting (including group)
894 const char *group = res_get(zh->session_res, "group");
895 const char *v = res_get_prefix(zh->session_res,
896 "database", group, "Default");
897 return zebra_select_database(zh, v);
902 void map_basenames(ZebraHandle zh, ODR stream,
903 int *num_bases, char ***basenames)
905 struct map_baseinfo info;
906 struct map_baseinfo *p = &info;
909 yaz_log(log_level, "map_basenames ");
914 info.num_bases = *num_bases;
915 info.basenames = *basenames;
916 info.new_num_max = 128;
917 info.new_num_bases = 0;
918 info.new_basenames = (char **)
919 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
920 info.mem = stream->mem;
922 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
924 for (i = 0; i<p->num_bases; i++)
925 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
927 p->new_basenames[(p->new_num_bases)++] =
928 nmem_strdup(p->mem, p->basenames[i]);
930 *num_bases = info.new_num_bases;
931 *basenames = info.new_basenames;
932 for (i = 0; i<*num_bases; i++)
933 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
936 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
938 ZEBRA_CHECK_HANDLE(zh);
940 yaz_log(log_level, "zebra_select_database %s",basename);
942 return zebra_select_databases(zh, 1, &basename);
945 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
946 const char **basenames)
953 ZEBRA_CHECK_HANDLE(zh);
956 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
957 num_bases,basenames[0]);
962 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
966 /* Check if the user has access to all databases (Seb) */
967 /* You could argue that this should happen later, after we have
968 * determined that the database(s) exist. */
969 if (zh->dbaccesslist) {
970 for (i = 0; i < num_bases; i++) {
971 const char *db = basenames[i];
973 for (p = zh->dbaccesslist; p && *p; p = pp) {
975 if ((pp = strchr(p, '+'))) {
981 if (len == strlen(db) && !strncmp(db, p, len))
985 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
991 for (i = 0; i < zh->num_basenames; i++)
992 xfree(zh->basenames[i]);
993 xfree(zh->basenames);
995 zh->num_basenames = num_bases;
996 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
997 for (i = 0; i < zh->num_basenames; i++)
998 zh->basenames[i] = xstrdup(basenames[i]);
1000 cp = strrchr(basenames[0], '/');
1003 len = cp - basenames[0];
1004 new_reg = xmalloc(len + 1);
1005 memcpy(new_reg, basenames[0], len);
1006 new_reg[len] = '\0';
1009 new_reg = xstrdup("");
1010 for (i = 1; i<num_bases; i++)
1014 cp1 = strrchr(basenames[i], '/');
1019 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1022 if (len != cp1 - basenames[i] ||
1023 memcmp(basenames[i], new_reg, len))
1025 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1033 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1038 zebra_select_register(zh, new_reg);
1042 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1045 if (!zh->lock_normal || !zh->lock_shadow)
1047 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1053 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1055 if (approx_limit == 0)
1056 approx_limit = DEFAULT_APPROX_LIMIT;
1057 zh->approx_limit = approx_limit;
1061 void zebra_set_partial_result(ZebraHandle zh)
1063 zh->partial_result = 1;
1067 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1068 int (*f)(void *client_data),
1071 zh->break_handler_func = f;
1072 zh->break_handler_data = client_data;
1076 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1077 const char *setname, zint *hits,
1078 int *estimated_hit_count,
1079 int *partial_resultset)
1083 ZEBRA_CHECK_HANDLE(zh);
1089 yaz_log(log_level, "zebra_search_rpn");
1091 zh->partial_result = 0;
1093 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1096 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1097 zh->num_basenames, zh->basenames, setname,
1098 hits, estimated_hit_count);
1100 *partial_resultset = zh->partial_result;
1105 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1106 const char *setname, zint *hits)
1108 int estimated_hit_count;
1109 int partial_resultset;
1110 return zebra_search_RPN_x(zh, o, query, setname, hits,
1111 &estimated_hit_count,
1112 &partial_resultset);
1115 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1116 const char *setname,
1117 Z_RecordComposition *comp,
1118 const Odr_oid *input_format, int num_recs,
1119 ZebraRetrievalRecord *recs)
1121 ZebraMetaRecord *poset;
1123 ZEBRA_RES ret = ZEBRA_OK;
1126 ZEBRA_CHECK_HANDLE(zh);
1132 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1136 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1141 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1144 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1145 for (i = 0; i<num_recs; i++)
1146 pos_array[i] = recs[i].position;
1147 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1150 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1151 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1157 WRBUF addinfo_w = wrbuf_alloc();
1158 for (i = 0; i < num_recs; i++)
1160 recs[i].errCode = 0;
1161 recs[i].errString = 0;
1166 recs[i].sysno = poset[i].sysno;
1169 recs[i].format = yaz_oid_recsyn_sutrs;
1170 recs[i].len = strlen(poset[i].term);
1171 recs[i].buf = poset[i].term;
1172 recs[i].base = poset[i].db;
1174 else if (poset[i].sysno)
1178 zebra_snippets *hit_snippet = zebra_snippets_create();
1180 /* we disable hit snippets for now. It does not work well
1181 and it slows retrieval down a lot */
1183 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1186 wrbuf_rewind(addinfo_w);
1188 zebra_record_fetch(zh, setname,
1189 poset[i].sysno, poset[i].score,
1190 stream, input_format, comp,
1191 &recs[i].format, &buf, &len,
1192 &recs[i].base, addinfo_w);
1194 if (wrbuf_len(addinfo_w))
1196 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1200 recs[i].buf = (char*) odr_malloc(stream, len);
1201 memcpy(recs[i].buf, buf, len);
1205 recs[i].score = poset[i].score;
1206 zebra_snippets_destroy(hit_snippet);
1210 /* only need to set it once */
1211 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1213 zebra_setError_zint(zh,
1214 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1221 zebra_meta_records_destroy(zh, poset, num_recs);
1222 wrbuf_destroy(addinfo_w);
1229 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1231 int *num_entries, ZebraScanEntry **entries,
1233 const char *setname)
1235 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1236 Z_AttributesPlusTerm *zapt;
1237 Odr_oid *attributeSet;
1240 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1243 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1247 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1248 position, num_entries, entries, is_partial,
1251 yaz_pqf_destroy(pqf_parser);
1255 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1256 const Odr_oid *attributeset,
1258 int *num_entries, ZebraScanEntry **entries,
1260 const char *setname)
1264 ZEBRA_CHECK_HANDLE(zh);
1269 assert(num_entries);
1272 yaz_log(log_level, "zebra_scan");
1274 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1281 res = rpn_scan(zh, stream, zapt, attributeset,
1282 zh->num_basenames, zh->basenames, position,
1283 num_entries, entries, is_partial, setname);
1288 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1289 int num_input_setnames, const char **input_setnames,
1290 const char *output_setname,
1291 Z_SortKeySpecList *sort_sequence,
1295 ZEBRA_CHECK_HANDLE(zh);
1297 assert(num_input_setnames>0);
1298 assert(input_setnames);
1299 assert(sort_sequence);
1300 assert(sort_status);
1301 yaz_log(log_level, "zebra_sort");
1303 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1305 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1306 output_setname, sort_sequence, sort_status);
1311 int zebra_deleteResultSet(ZebraHandle zh, int function,
1312 int num_setnames, char **setnames,
1317 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1319 if (zebra_begin_read(zh))
1320 return Z_DeleteStatus_systemProblemAtTarget;
1323 case Z_DeleteResultSetRequest_list:
1324 assert(num_setnames>0);
1326 resultSetDestroy(zh, num_setnames, setnames, statuses);
1328 case Z_DeleteResultSetRequest_all:
1329 resultSetDestroy(zh, -1, 0, statuses);
1333 status = Z_DeleteStatus_success;
1334 for (i = 0; i<num_setnames; i++)
1335 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1336 status = statuses[i];
1340 int zebra_errCode(ZebraHandle zh)
1344 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1347 yaz_log(log_level, "zebra_errCode: o");
1351 const char *zebra_errString(ZebraHandle zh)
1355 e= diagbib1_str(zh->errCode);
1356 yaz_log(log_level, "zebra_errString: %s",e);
1360 char *zebra_errAdd(ZebraHandle zh)
1365 yaz_log(log_level, "zebra_errAdd: %s",a);
1369 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1372 const char *astring;
1376 ZEBRA_CHECK_HANDLE(zh);
1380 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1381 p = res_get(zs->global_res, u);
1382 xfree(zh->user_perm);
1383 zh->user_perm = xstrdup(p ? p : "r");
1385 /* Determine database access list */
1386 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1388 zh->dbaccesslist = xstrdup(astring);
1390 zh->dbaccesslist = 0;
1392 /* users that don't require a password .. */
1393 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1396 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1401 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1402 const char *record_type)
1404 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1405 database, record_type);
1406 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1408 return zebra_begin_trans(zh, 1);
1411 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1413 ZEBRA_CHECK_HANDLE(zh);
1414 yaz_log(log_level, "zebra_admin_import_end");
1415 return zebra_end_trans(zh);
1418 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1420 ZEBRA_RES res = ZEBRA_OK;
1423 ZEBRA_CHECK_HANDLE(zh);
1424 yaz_log(log_level, "zebra_admin_import_segment");
1426 for (i = 0; i<segment->num_segmentRecords; i++)
1428 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1430 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1432 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1433 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1435 Odr_oct *oct = fragment->u.notExternallyTagged;
1438 if(zebra_update_record(
1441 0, /* record Type */
1445 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1453 int delete_w_handle(const char *info, void *handle)
1455 ZebraHandle zh = (ZebraHandle) handle;
1458 if (*info == sizeof(pos))
1460 memcpy(&pos, info+1, sizeof(pos));
1461 isamb_unlink(zh->reg->isamb, pos);
1466 int delete_w_all_handle(const char *info, void *handle)
1468 ZebraHandle zh = (ZebraHandle) handle;
1471 if (*info == sizeof(pos))
1474 memcpy(&pos, info+1, sizeof(pos));
1475 pt = isamb_pp_open(zh->reg->isamb, pos, 2);
1480 while (isamb_pp_read(pt, &key))
1483 rec = rec_get(zh->reg->records, key.mem[0]);
1484 rec_del(zh->reg->records, &rec);
1489 return delete_w_handle(info, handle);
1492 static int delete_SU_handle(void *handle, int ord,
1493 const char *index_type, const char *string_index,
1494 zinfo_index_category_t cat)
1496 ZebraHandle zh = (ZebraHandle) handle;
1500 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s cat=%d", ord,
1501 index_type, string_index, (int) cat);
1503 ord_len = key_SU_encode(ord, ord_buf);
1504 ord_buf[ord_len] = '\0';
1506 assert(zh->reg->isamb);
1507 assert(zh->reg->records);
1508 dict_delete_subtree(zh->reg->dict, ord_buf,
1510 !strcmp(string_index, "_ALLRECORDS") ?
1511 delete_w_all_handle : delete_w_handle);
1515 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1517 ZEBRA_RES ret = ZEBRA_OK;
1519 yaz_log(log_level, "zebra_drop_database %s", db);
1520 ZEBRA_CHECK_HANDLE(zh);
1522 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1524 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1529 if (zebraExplain_curDatabase(zh->reg->zei, db))
1531 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1536 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1537 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1538 0 /* handle */, 0 /* func */);
1539 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1540 zebraExplain_removeDatabase(zh->reg->zei, zh);
1541 zebra_remove_file_match(zh);
1546 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1547 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1548 "drop database only supported for isam:b");
1551 if (zebra_end_trans(zh) != ZEBRA_OK)
1553 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1559 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1561 yaz_log(log_level, "zebra_create_database %s", db);
1562 ZEBRA_CHECK_HANDLE(zh);
1565 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1567 if (zebra_begin_trans(zh, 1))
1570 /* announce database */
1571 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1572 /* explainDatabase */))
1574 if (zebra_end_trans(zh) != ZEBRA_OK)
1576 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1578 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1581 return zebra_end_trans(zh);
1584 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1585 const char *input_str, int input_len,
1586 char *output_str, int output_len)
1589 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1593 yaz_log(log_level, "zebra_string_norm ");
1595 if (!zh->reg->zebra_maps)
1597 wrbuf = zebra_replace(zm, "", input_str, input_len);
1600 if (wrbuf_len(wrbuf) >= output_len)
1602 if (wrbuf_len(wrbuf))
1603 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1604 output_str[wrbuf_len(wrbuf)] = '\0';
1605 return wrbuf_len(wrbuf);
1608 /** \brief set register state (state*.LCK)
1609 \param zh Zebra handle
1611 \param seqno sequence number
1614 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1616 c=commit (writing to register, reading from shadow, shadow mode only)
1618 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1620 char state_fname[256];
1625 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1627 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1628 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1629 f = fopen(fname, "w");
1632 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s w", state_fname);
1635 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1636 fprintf(f, "%c %d %ld\n", val, seqno, p);
1641 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1643 char state_fname[256];
1648 yaz_log(log_level, "zebra_get_state ");
1650 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1651 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1652 f = fopen(fname, "r");
1658 if (fscanf(f, "%c %d", val, seqno) != 2)
1660 yaz_log(YLOG_ERRNO|YLOG_WARN, "fscan fail %s",
1668 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1670 return zebra_begin_trans(zh, 0);
1673 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1675 return zebra_end_trans(zh);
1678 static void read_res_for_transaction(ZebraHandle zh)
1680 const char *group = res_get(zh->res, "group");
1682 /* FIXME - do we still use groups ?? */
1684 zh->m_group = group;
1685 v = res_get_prefix(zh->res, "followLinks", group, "1");
1686 zh->m_follow_links = atoi(v);
1688 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1689 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1691 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1692 zh->m_store_keys = atoi(v);
1694 v = res_get_prefix(zh->res, "storeData", group, "1");
1695 zh->m_store_data = atoi(v);
1697 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1698 zh->m_explain_database = atoi(v);
1700 v = res_get_prefix(zh->res, "openRW", group, "1");
1701 zh->m_flag_rw = atoi(v);
1703 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1704 zh->m_file_verbose_limit = atoi(v);
1707 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1709 ZEBRA_CHECK_HANDLE(zh);
1710 zebra_select_default_database(zh);
1713 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1714 "zebra_begin_trans: no database selected");
1718 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1722 if (rw && !strchr(zh->user_perm, 'w'))
1726 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1737 const char *rval = 0;
1742 read_res_for_transaction(zh);
1745 if (zh->trans_no != 1)
1747 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1748 "zebra_begin_trans: no write trans within read");
1753 resultSetInvalidate(zh);
1754 zebra_register_close(zh->service, zh->reg);
1756 zh->trans_w_no = zh->trans_no;
1758 zh->records_inserted = 0;
1759 zh->records_updated = 0;
1760 zh->records_deleted = 0;
1761 zh->records_processed = 0;
1762 zh->records_skipped = 0;
1764 #if HAVE_SYS_TIMES_H
1768 if (zh->shadow_enable)
1769 rval = res_get(zh->res, "shadow");
1773 zebra_lock_r(zh->lock_normal);
1774 zebra_lock_w(zh->lock_shadow);
1778 zebra_lock_w(zh->lock_normal);
1779 zebra_lock_w(zh->lock_shadow);
1781 zebra_get_state(zh, &val, &seqno);
1784 /* either we didn't finish commit or shadow is dirty */
1787 yaz_log(YLOG_WARN, "previous transaction did not finish "
1788 "(shadow disabled)");
1790 zebra_unlock(zh->lock_shadow);
1791 zebra_unlock(zh->lock_normal);
1792 if (zebra_commit(zh))
1800 zebra_lock_r(zh->lock_normal);
1801 zebra_lock_w(zh->lock_shadow);
1805 zebra_lock_w(zh->lock_normal);
1806 zebra_lock_w(zh->lock_shadow);
1810 zebra_set_state(zh, 'd', seqno);
1812 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1813 1, rval ? 1 : 0, zh->res,
1817 zebra_unlock(zh->lock_shadow);
1818 zebra_unlock(zh->lock_normal);
1823 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1824 "zebra_begin_trans: cannot open register");
1825 yaz_log(YLOG_FATAL, "%s", zh->errString);
1828 zh->reg->seqno = seqno;
1829 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1839 if (zh->trans_no != 1)
1841 return zebra_flush_reg(zh);
1843 #if HAVE_SYS_TIMES_H
1849 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1852 if (!zh->lock_normal || !zh->lock_shadow)
1855 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1858 zebra_get_state(zh, &val, &seqno);
1864 else if (seqno != zh->reg->seqno)
1866 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1867 seqno, zh->reg->seqno);
1870 else if (zh->reg->last_val != val)
1872 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1873 val, zh->reg->last_val);
1880 zebra_lock_r(zh->lock_shadow);
1882 zebra_lock_r(zh->lock_normal);
1886 resultSetInvalidate(zh);
1887 zebra_register_close(zh->service, zh->reg);
1889 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1890 0, val == 'c' ? 1 : 0,
1891 zh->res, zh->path_reg);
1894 zebra_unlock(zh->lock_normal);
1895 zebra_unlock(zh->lock_shadow);
1897 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1900 zh->reg->last_val = val;
1901 zh->reg->seqno = seqno;
1903 read_res_for_transaction(zh);
1907 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1909 ZebraTransactionStatus dummy;
1911 yaz_log(log_level, "zebra_end_trans");
1912 ZEBRA_CHECK_HANDLE(zh);
1913 return zebra_end_transaction(zh, &dummy);
1916 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1922 ZEBRA_CHECK_HANDLE(zh);
1925 yaz_log(log_level, "zebra_end_transaction");
1927 status->processed = 0;
1928 status->inserted = 0;
1929 status->updated = 0;
1930 status->deleted = 0;
1934 if (!zh->res || !zh->reg)
1936 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1937 "zebra_end_trans: no open transaction");
1940 if (zh->trans_no != zh->trans_w_no)
1943 if (zh->trans_no != 0)
1946 /* release read lock */
1948 zebra_unlock(zh->lock_normal);
1949 zebra_unlock(zh->lock_shadow);
1952 { /* release write lock */
1956 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1957 rval = res_get(zh->res, "shadow");
1959 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1961 zebra_flush_reg(zh);
1963 resultSetInvalidate(zh);
1965 zebra_register_close(zh->service, zh->reg);
1968 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1969 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1970 zh->records_processed, zh->records_inserted,
1971 zh->records_updated, zh->records_deleted);
1973 status->processed = zh->records_processed;
1974 status->inserted = zh->records_inserted;
1975 status->updated = zh->records_updated;
1976 status->deleted = zh->records_deleted;
1978 zebra_get_state(zh, &val, &seqno);
1981 BFiles bfs = bfs_create(rval, zh->path_reg);
1982 bf_commitClean(bfs, rval);
1987 zebra_set_state(zh, 'o', seqno);
1988 zebra_unlock(zh->lock_shadow);
1989 zebra_unlock(zh->lock_normal);
1992 #if HAVE_SYS_TIMES_H
1994 yaz_log(log_level, "user/system: %ld/%ld",
1995 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1996 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1998 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1999 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
2004 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
2006 return zebra_repository_index(zh, path, action_update);
2009 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2011 return zebra_repository_index(zh, path, action_delete);
2014 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
2015 enum zebra_recctrl_action_t action)
2020 if (action == action_update)
2021 yaz_log(log_level, "updating %s", path);
2022 else if (action == action_delete)
2023 yaz_log(log_level, "deleting %s", path);
2024 else if (action == action_a_delete)
2025 yaz_log(log_level, "attempt deleting %s", path);
2027 yaz_log(log_level, "update action=%d", (int) action);
2029 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
2030 return zebra_update_file_match(zh, path);
2032 return zebra_update_from_path(zh, path, action);
2035 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2039 yaz_log(log_level, "zebra_repository_show");
2040 repositoryShow(zh, path);
2044 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2050 ZEBRA_RES res = ZEBRA_OK;
2054 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
2055 zebra_select_default_database(zh);
2058 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2061 rval = res_get(zh->res, "shadow");
2064 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2068 zebra_lock_w(zh->lock_normal);
2069 zebra_lock_r(zh->lock_shadow);
2071 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2074 zebra_unlock(zh->lock_shadow);
2075 zebra_unlock(zh->lock_normal);
2078 zebra_get_state(zh, &val, &seqno);
2082 /* shadow area is dirty and so we must throw it away */
2083 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2086 else if (val == 'c')
2088 /* commit has started. We can not remove it anymore */
2093 bf_cache(bfs, rval);
2094 if (bf_commitExists(bfs))
2097 zebra_set_state(zh, 'd', seqno);
2100 zebra_set_state(zh, 'c', seqno);
2102 yaz_log(log_level, "commit start");
2103 if (bf_commitExec(bfs))
2106 if (res == ZEBRA_OK)
2109 zebra_set_state(zh, 'o', seqno);
2111 zebra_unlock(zh->lock_shadow);
2112 zebra_unlock(zh->lock_normal);
2114 zebra_lock_w(zh->lock_shadow);
2115 bf_commitClean(bfs, rval);
2116 zebra_unlock(zh->lock_shadow);
2120 zebra_unlock(zh->lock_shadow);
2121 zebra_unlock(zh->lock_normal);
2122 yaz_log(YLOG_WARN, "zebra_commit: failed");
2127 zebra_unlock(zh->lock_shadow);
2128 zebra_unlock(zh->lock_normal);
2129 yaz_log(log_level, "nothing to commit");
2136 ZEBRA_RES zebra_clean(ZebraHandle zh)
2138 yaz_log(log_level, "zebra_clean");
2139 ZEBRA_CHECK_HANDLE(zh);
2140 return zebra_commit_ex(zh, 1);
2143 ZEBRA_RES zebra_commit(ZebraHandle zh)
2145 yaz_log(log_level, "zebra_commit");
2146 ZEBRA_CHECK_HANDLE(zh);
2147 return zebra_commit_ex(zh, 0);
2151 ZEBRA_RES zebra_init(ZebraHandle zh)
2156 yaz_log(log_level, "zebra_init");
2158 ZEBRA_CHECK_HANDLE(zh);
2160 zebra_select_default_database(zh);
2163 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2164 "cannot select default database");
2167 rval = res_get(zh->res, "shadow");
2169 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2172 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2176 bf_cache(bfs, rval);
2180 zebra_set_state(zh, 'o', 0);
2184 ZEBRA_RES zebra_compact(ZebraHandle zh)
2188 yaz_log(log_level, "zebra_compact");
2189 ZEBRA_CHECK_HANDLE(zh);
2192 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2195 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2201 #define ZEBRA_CHECK_DICT 1
2202 #define ZEBRA_CHECK_ISAM 2
2204 static ZEBRA_RES zebra_record_check(ZebraHandle zh, Record rec,
2205 zint *no_keys, int message_limit,
2207 zint *no_long_dict_entries,
2208 zint *no_failed_dict_lookups,
2209 zint *no_invalid_keys,
2210 zint *no_invalid_dict_infos,
2211 zint *no_invalid_isam_entries)
2213 ZEBRA_RES res = ZEBRA_OK;
2214 zebra_rec_keys_t keys = zebra_rec_keys_open();
2215 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
2216 rec->size[recInfo_delKeys], 0);
2219 if (!zebra_rec_keys_rewind(keys))
2227 struct it_key key_in;
2228 NMEM nmem = nmem_create();
2230 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
2233 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
2234 char ord_buf[IT_MAX_WORD+20];
2235 int ord_len = key_SU_encode(ord, ord_buf);
2240 if (key_in.len < 2 || key_in.len > IT_KEY_LEVEL_MAX)
2243 (*no_invalid_keys)++;
2244 if (*no_invalid_keys <= message_limit)
2247 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2248 ": unexpected key length %d",
2249 rec->sysno, key_in.len);
2252 if (ord_len + slen >= sizeof(ord_buf)-1)
2255 (*no_long_dict_entries)++;
2256 if (*no_long_dict_entries <= message_limit)
2259 /* so bad it can not fit into our ord_buf */
2260 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2261 ": long dictionary entry %d + %d",
2262 rec->sysno, ord_len, (int) slen);
2266 memcpy(ord_buf + ord_len, str, slen);
2267 ord_buf[ord_len + slen] = '\0';
2268 if (ord_len + slen >= IT_MAX_WORD)
2271 (*no_long_dict_entries)++;
2272 if (*no_long_dict_entries <= message_limit)
2275 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2276 ": long dictionary entry %d + %d",
2277 rec->sysno, (int) ord_len, (int) slen);
2280 if ((flags & ZEBRA_CHECK_DICT) == 0)
2282 info = dict_lookup(zh->reg->dict, ord_buf);
2286 (*no_failed_dict_lookups)++;
2287 if (*no_failed_dict_lookups <= message_limit)
2290 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2291 ": term do not exist in dictionary", rec->sysno);
2294 else if (flags & ZEBRA_CHECK_ISAM)
2298 if (*info != sizeof(pos))
2301 (*no_invalid_dict_infos)++;
2302 if (*no_invalid_dict_infos <= message_limit)
2305 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2306 ": long dictionary entry %d + %d",
2307 rec->sysno, (int) ord_len, (int) slen);
2313 memcpy(&pos, info+1, sizeof(pos));
2316 ISAMB_PP ispt = isamb_pp_open(zh->reg->isamb, pos,
2321 (*no_invalid_isam_entries)++;
2322 if (*no_invalid_isam_entries <= message_limit)
2325 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2326 ": isamb_pp_open entry " ZINT_FORMAT
2331 else if (zh->m_staticrank)
2333 isamb_pp_close(ispt);
2337 struct it_key until_key;
2338 struct it_key isam_key;
2342 until_key.len = key_in.len - 1;
2343 for (i = 0; i < until_key.len; i++)
2344 until_key.mem[i] = key_in.mem[i+1];
2346 if (until_key.mem[0] == 0)
2347 until_key.mem[0] = rec->sysno;
2348 r = isamb_pp_forward(ispt, &isam_key, &until_key);
2352 (*no_invalid_isam_entries)++;
2353 if (*no_invalid_isam_entries <= message_limit)
2356 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2357 ": isamb_pp_forward " ZINT_FORMAT
2358 " returned no entry",
2364 int cmp = key_compare(&until_key, &isam_key);
2368 (*no_invalid_isam_entries)++;
2369 if (*no_invalid_isam_entries
2373 yaz_log(YLOG_WARN, "Record "
2375 ": isamb_pp_forward "
2377 " returned different entry",
2380 key_logdump_txt(YLOG_LOG,
2384 key_logdump_txt(YLOG_LOG,
2391 isamb_pp_close(ispt);
2399 zebra_it_key_str_dump(zh, &key_in, str,
2400 slen, nmem, YLOG_LOG);
2406 zebra_rec_keys_close(keys);
2410 ZEBRA_RES zebra_register_check(ZebraHandle zh, const char *spec)
2412 ZEBRA_RES res = ZEBRA_FAIL;
2414 int message_limit = 10;
2416 if (!spec || *spec == '\0'
2417 || !strcmp(spec, "dict") || !strcmp(spec, "default"))
2418 flags = ZEBRA_CHECK_DICT;
2419 else if (!strcmp(spec, "isam") || !strcmp(spec, "full"))
2420 flags = ZEBRA_CHECK_DICT|ZEBRA_CHECK_ISAM;
2421 else if (!strcmp(spec, "quick"))
2426 yaz_log(YLOG_LOG, "zebra_register_check begin flags=%u message_limit=%d",
2427 flags, message_limit);
2428 if (zebra_begin_read(zh) == ZEBRA_OK)
2430 zint no_records_total = 0;
2431 zint no_records_fail = 0;
2432 zint total_keys = 0;
2436 Record rec = rec_get_root(zh->reg->records);
2438 zint no_long_dict_entries = 0;
2439 zint no_failed_dict_lookups = 0;
2440 zint no_invalid_keys = 0;
2441 zint no_invalid_dict_infos = 0;
2442 zint no_invalid_isam_entries = 0;
2450 if (zebra_record_check(zh, rec, &no_keys, message_limit,
2452 &no_long_dict_entries,
2453 &no_failed_dict_lookups,
2455 &no_invalid_dict_infos,
2456 &no_invalid_isam_entries
2464 r1 = rec_get_next(zh->reg->records, rec);
2468 total_keys += no_keys;
2470 yaz_log(YLOG_LOG, "records total: " ZINT_FORMAT,
2472 yaz_log(YLOG_LOG, "records fail: " ZINT_FORMAT,
2474 yaz_log(YLOG_LOG, "total keys: " ZINT_FORMAT,
2476 yaz_log(YLOG_LOG, "long dict entries: " ZINT_FORMAT,
2477 no_long_dict_entries);
2478 if (flags & ZEBRA_CHECK_DICT)
2480 yaz_log(YLOG_LOG, "failed dict lookups: " ZINT_FORMAT,
2481 no_failed_dict_lookups);
2482 yaz_log(YLOG_LOG, "invalid dict infos: " ZINT_FORMAT,
2483 no_invalid_dict_infos);
2485 if (flags & ZEBRA_CHECK_ISAM)
2486 yaz_log(YLOG_LOG, "invalid isam entries: " ZINT_FORMAT,
2487 no_invalid_isam_entries);
2491 yaz_log(YLOG_LOG, "zebra_register_check end ret=%d", res);
2495 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2497 yaz_log(log_level, "zebra_result");
2500 *code = zh->errCode;
2501 *addinfo = zh->errString;
2505 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2506 *addinfo ="ZebraHandle is NULL";
2510 void zebra_shadow_enable(ZebraHandle zh, int value)
2513 yaz_log(log_level, "zebra_shadow_enable");
2514 zh->shadow_enable = value;
2517 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2519 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2520 ZEBRA_CHECK_HANDLE(zh);
2523 if (zh->iconv_to_utf8 != 0)
2524 yaz_iconv_close(zh->iconv_to_utf8);
2525 if (zh->iconv_from_utf8 != 0)
2526 yaz_iconv_close(zh->iconv_from_utf8);
2529 yaz_iconv_open("UTF-8", encoding);
2530 if (zh->iconv_to_utf8 == 0)
2531 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2532 zh->iconv_from_utf8 =
2533 yaz_iconv_open(encoding, "UTF-8");
2534 if (zh->iconv_to_utf8 == 0)
2535 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2540 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2542 yaz_log(log_level, "zebra_record_encoding");
2543 ZEBRA_CHECK_HANDLE(zh);
2544 xfree(zh->record_encoding);
2545 zh->record_encoding = 0;
2547 zh->record_encoding = xstrdup(encoding);
2551 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2555 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2557 res_set(zh->res, name, value);
2560 const char *zebra_get_resource(ZebraHandle zh,
2561 const char *name, const char *defaultvalue)
2566 v = res_get_def(zh->res, name,(char *)defaultvalue);
2567 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2571 /* moved from zebra_api_ext.c by pop */
2572 /* FIXME: Should this really be public??? -Heikki */
2574 int zebra_trans_no(ZebraHandle zh)
2576 yaz_log(log_level, "zebra_trans_no");
2578 return zh->trans_no;
2581 int zebra_get_shadow_enable(ZebraHandle zh)
2583 yaz_log(log_level, "zebra_get_shadow_enable");
2585 return zh->shadow_enable;
2588 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2590 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2592 zh->shadow_enable = value;
2595 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2596 const char *buf, int buf_size)
2598 return zebra_update_record(zh, action_update,
2599 0 /* record type */,
2606 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2607 enum zebra_recctrl_action_t action,
2608 const char *recordType,
2609 zint *sysno, const char *match,
2611 const char *buf, int buf_size)
2615 ZEBRA_CHECK_HANDLE(zh);
2619 yaz_log(log_level, "zebra_update_record");
2621 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2624 buf_size = strlen(buf);
2626 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2628 res = zebra_buffer_extract_record(zh, buf, buf_size,
2634 if (zebra_end_trans(zh) != ZEBRA_OK)
2636 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2642 /* ---------------------------------------------------------------------------
2646 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2647 const char *setname, zint *hits)
2650 ZEBRA_RES res = ZEBRA_OK;
2655 ZEBRA_CHECK_HANDLE(zh);
2657 odr = odr_createmem(ODR_ENCODE);
2662 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2664 query = p_query_rpn(odr, pqf_query);
2668 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2669 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2673 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2677 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2685 /* ---------------------------------------------------------------------------
2686 Sort - a simplified interface, with optional read locks.
2688 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2689 const char *sort_spec,
2690 const char *output_setname,
2691 const char **input_setnames)
2693 int num_input_setnames = 0;
2694 int sort_status = 0;
2695 Z_SortKeySpecList *sort_sequence;
2697 ZEBRA_CHECK_HANDLE(zh);
2700 assert(output_setname);
2701 assert(input_setnames);
2702 sort_sequence = yaz_sort_spec(stream, sort_spec);
2703 yaz_log(log_level, "sort (FIXME) ");
2706 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2707 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2711 /* we can do this, since the perl typemap code for char** will
2712 put a NULL at the end of list */
2713 while (input_setnames[num_input_setnames]) num_input_setnames++;
2715 if (zebra_begin_read(zh))
2718 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2719 output_setname, sort_sequence, &sort_status);
2725 /* ---------------------------------------------------------------------------
2726 Get BFS for Zebra system (to make alternative storage methods)
2728 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2731 return zh->reg->bfs;
2736 /* ---------------------------------------------------------------------------
2737 Set limit for search/scan
2739 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2741 ZEBRA_CHECK_HANDLE(zh);
2742 zebra_limit_destroy(zh->m_limit);
2743 zh->m_limit = zebra_limit_create(complement_flag, ids);
2748 Set Error code + addinfo
2750 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2755 nmem_reset(zh->nmem_error);
2756 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2759 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2762 sprintf(vstr, ZINT_FORMAT, i);
2765 nmem_reset(zh->nmem_error);
2766 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2769 void zebra_lock_prefix(Res res, char *path)
2771 const char *lock_dir = res_get_def(res, "lockDir", "");
2773 strcpy(path, lock_dir);
2774 if (*path && path[strlen(path)-1] != '/')
2781 * c-file-style: "Stroustrup"
2782 * indent-tabs-mode: nil
2784 * vim: shiftwidth=4 tabstop=8 expandtab