1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 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);
436 if (!rec_check_compression_method(record_compression))
438 yaz_log(YLOG_FATAL, "unsupported recordCompression: %s",
444 const char *index_fname = res_get_def(res, "index", "default.idx");
445 if (index_fname && *index_fname && strcmp(index_fname, "none"))
447 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
452 zebra_maps_define_default_sort(reg->zebra_maps);
456 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
458 yaz_log(YLOG_WARN, "rec_open failed");
463 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
465 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
467 yaz_log(YLOG_WARN, "dict_open failed");
472 if (res_get_match(res, "sortindex", "f", "f"))
473 sort_type = ZEBRA_SORT_TYPE_FLAT;
474 else if (res_get_match(res, "sortindex", "i", "f"))
475 sort_type = ZEBRA_SORT_TYPE_ISAMB;
476 else if (res_get_match(res, "sortindex", "m", "f"))
477 sort_type = ZEBRA_SORT_TYPE_MULTI;
480 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
485 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
487 yaz_log(YLOG_WARN, "zebra_sort_open failed");
490 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
492 struct ISAMS_M_s isams_m;
493 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
494 key_isams_m(res, &isams_m))))
496 yaz_log(YLOG_WARN, "isams_open failed");
500 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
502 struct ISAMC_M_s isamc_m;
503 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
504 rw, key_isamc_m(res, &isamc_m))))
506 yaz_log(YLOG_WARN, "isamc_open failed");
510 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
512 struct ISAMC_M_s isamc_m;
514 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
515 rw, key_isamc_m(res, &isamc_m), 0)))
517 yaz_log(YLOG_WARN, "isamb_open failed");
521 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
523 struct ISAMC_M_s isamc_m;
525 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
526 rw, key_isamc_m(res, &isamc_m), 1)))
528 yaz_log(YLOG_WARN, "isamb_open failed");
532 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
534 struct ISAMC_M_s isamc_m;
536 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
537 rw, key_isamc_m(res, &isamc_m), -1)))
539 yaz_log(YLOG_WARN, "isamb_open failed");
545 reg->zei = zebraExplain_open(reg->records, reg->dh,
547 zebra_extract_explain);
550 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
557 zebra_register_close(zs, reg);
560 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
564 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
566 ZEBRA_CHECK_HANDLE(zh);
567 yaz_log(log_level, "zebra_admin_shutdown");
569 zebra_mutex_cond_lock(&zh->service->session_lock);
570 zh->service->stop_flag = 1;
571 zebra_mutex_cond_unlock(&zh->service->session_lock);
575 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
578 ZEBRA_CHECK_HANDLE(zh);
579 yaz_log(log_level, "zebra_admin_start");
581 zebra_mutex_cond_lock(&zs->session_lock);
582 zebra_mutex_cond_unlock(&zs->session_lock);
586 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
590 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
594 zebraExplain_close(reg->zei);
595 dict_close(reg->dict);
597 dict_close(reg->matchDict);
598 zebra_sort_close(reg->sort_index);
600 isams_close(reg->isams);
602 isamc_close(reg->isamc);
604 isamb_close(reg->isamb);
605 rec_close(®->records);
607 recTypes_destroy(reg->recTypes);
608 zebra_maps_close(reg->zebra_maps);
609 zebraRankDestroy(reg);
610 bfs_destroy(reg->bfs);
611 data1_destroy(reg->dh);
613 zebra_rec_keys_close(reg->keys);
614 zebra_rec_keys_close(reg->sortKeys);
616 key_block_destroy(®->key_block);
621 ZEBRA_RES zebra_stop(ZebraService zs)
627 zebra_close(zs->sessions);
630 zebra_mutex_cond_destroy(&zs->session_lock);
633 passwd_db_close(zs->passwd_db);
635 recTypeClass_destroy(zs->record_classes);
636 nmem_destroy(zs->nmem);
637 res_close(zs->global_res);
639 yaz_timing_stop(zs->timing);
640 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
641 yaz_timing_get_real(zs->timing),
642 yaz_timing_get_user(zs->timing),
643 yaz_timing_get_sys(zs->timing));
646 yaz_timing_destroy(&zs->timing);
651 ZEBRA_RES zebra_close(ZebraHandle zh)
654 struct zebra_session **sp;
657 yaz_log(log_level, "zebra_close");
658 ZEBRA_CHECK_HANDLE(zh);
663 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
664 resultSetDestroy(zh, -1, 0, 0);
667 zebra_register_close(zh->service, zh->reg);
669 res_close(zh->session_res);
671 xfree(zh->record_encoding);
673 xfree(zh->dbaccesslist);
675 for (i = 0; i < zh->num_basenames; i++)
676 xfree(zh->basenames[i]);
677 xfree(zh->basenames);
679 if (zh->iconv_to_utf8 != 0)
680 yaz_iconv_close(zh->iconv_to_utf8);
681 if (zh->iconv_from_utf8 != 0)
682 yaz_iconv_close(zh->iconv_from_utf8);
684 zebra_mutex_cond_lock(&zs->session_lock);
685 zebra_lock_destroy(zh->lock_normal);
686 zebra_lock_destroy(zh->lock_shadow);
698 zebra_mutex_cond_unlock(&zs->session_lock);
700 xfree(zh->user_perm);
701 zh->service = 0; /* more likely to trigger an assert */
703 zebra_limit_destroy(zh->m_limit);
705 nmem_destroy(zh->nmem_error);
712 struct map_baseinfo {
718 char **new_basenames;
722 static void zebra_open_res(ZebraHandle zh)
730 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
731 zh->res = res_open(zh->session_res, 0);
732 res_read_file(zh->res, fname);
734 else if (*zh->reg_name == 0)
736 zh->res = res_open(zh->session_res, 0);
740 yaz_log(YLOG_WARN, "no register root specified");
741 zh->res = 0; /* no path for register - fail! */
745 static void zebra_close_res(ZebraHandle zh)
753 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
757 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
761 assert(zh->reg == 0);
762 assert(*zh->reg_name == 0);
768 resultSetInvalidate(zh);
769 zebra_register_close(zh->service, zh->reg);
775 zh->reg_name = xstrdup(new_reg);
779 if (zh->service->path_root)
781 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
782 strlen(zh->reg_name) + 3);
783 strcpy(zh->path_reg, zh->service->path_root);
786 strcat(zh->path_reg, "/");
787 strcat(zh->path_reg, zh->reg_name);
793 zebra_lock_destroy(zh->lock_normal);
797 zebra_lock_destroy(zh->lock_shadow);
803 const char *lock_area = res_get(zh->res, "lockDir");
805 if (!lock_area && zh->path_reg)
806 res_set(zh->res, "lockDir", zh->path_reg);
807 sprintf(fname, "norm.%s.LCK", zh->reg_name);
809 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
811 sprintf(fname, "shadow.%s.LCK", zh->reg_name);
813 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
815 if (!zh->lock_normal || !zh->lock_shadow)
819 zebra_lock_destroy(zh->lock_normal);
824 zebra_lock_destroy(zh->lock_shadow);
833 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
834 zebra_set_approx_limit(zh, approx);
838 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
839 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
843 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
846 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
847 zh->m_segment_indexing);
852 void map_basenames_func(void *vp, const char *name, const char *value)
854 struct map_baseinfo *p = (struct map_baseinfo *) vp;
856 char fromdb[128], todb[8][128];
863 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
864 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
865 todb[5], todb[6], todb[7]);
869 for (i = 0; i<p->num_bases; i++)
870 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
873 for (i = 0; i < no; i++)
875 if (p->new_num_bases == p->new_num_max)
877 p->new_basenames[(p->new_num_bases)++] =
878 nmem_strdup(p->mem, todb[i]);
884 int zebra_select_default_database(ZebraHandle zh)
888 /* no database has been selected - so we select based on
889 resource setting (including group)
891 const char *group = res_get(zh->session_res, "group");
892 const char *v = res_get_prefix(zh->session_res,
893 "database", group, "Default");
894 return zebra_select_database(zh, v);
899 void map_basenames(ZebraHandle zh, ODR stream,
900 int *num_bases, char ***basenames)
902 struct map_baseinfo info;
903 struct map_baseinfo *p = &info;
906 yaz_log(log_level, "map_basenames ");
911 info.num_bases = *num_bases;
912 info.basenames = *basenames;
913 info.new_num_max = 128;
914 info.new_num_bases = 0;
915 info.new_basenames = (char **)
916 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
917 info.mem = stream->mem;
919 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
921 for (i = 0; i<p->num_bases; i++)
922 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
924 p->new_basenames[(p->new_num_bases)++] =
925 nmem_strdup(p->mem, p->basenames[i]);
927 *num_bases = info.new_num_bases;
928 *basenames = info.new_basenames;
929 for (i = 0; i<*num_bases; i++)
930 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
933 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
935 ZEBRA_CHECK_HANDLE(zh);
937 yaz_log(log_level, "zebra_select_database %s",basename);
939 return zebra_select_databases(zh, 1, &basename);
942 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
943 const char **basenames)
950 ZEBRA_CHECK_HANDLE(zh);
953 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
954 num_bases,basenames[0]);
959 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
963 /* Check if the user has access to all databases (Seb) */
964 /* You could argue that this should happen later, after we have
965 * determined that the database(s) exist. */
966 if (zh->dbaccesslist) {
967 for (i = 0; i < num_bases; i++) {
968 const char *db = basenames[i];
970 for (p = zh->dbaccesslist; p && *p; p = pp) {
972 if ((pp = strchr(p, '+'))) {
978 if (len == strlen(db) && !strncmp(db, p, len))
982 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
988 for (i = 0; i < zh->num_basenames; i++)
989 xfree(zh->basenames[i]);
990 xfree(zh->basenames);
992 zh->num_basenames = num_bases;
993 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
994 for (i = 0; i < zh->num_basenames; i++)
995 zh->basenames[i] = xstrdup(basenames[i]);
997 cp = strrchr(basenames[0], '/');
1000 len = cp - basenames[0];
1001 new_reg = xmalloc(len + 1);
1002 memcpy(new_reg, basenames[0], len);
1003 new_reg[len] = '\0';
1006 new_reg = xstrdup("");
1007 for (i = 1; i<num_bases; i++)
1011 cp1 = strrchr(basenames[i], '/');
1016 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1019 if (len != cp1 - basenames[i] ||
1020 memcmp(basenames[i], new_reg, len))
1022 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1030 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1035 zebra_select_register(zh, new_reg);
1039 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1042 if (!zh->lock_normal || !zh->lock_shadow)
1044 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1050 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1052 if (approx_limit == 0)
1053 approx_limit = DEFAULT_APPROX_LIMIT;
1054 zh->approx_limit = approx_limit;
1058 void zebra_set_partial_result(ZebraHandle zh)
1060 zh->partial_result = 1;
1064 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1065 int (*f)(void *client_data),
1068 zh->break_handler_func = f;
1069 zh->break_handler_data = client_data;
1073 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1074 const char *setname, zint *hits,
1075 int *estimated_hit_count,
1076 int *partial_resultset)
1080 ZEBRA_CHECK_HANDLE(zh);
1086 yaz_log(log_level, "zebra_search_rpn");
1088 zh->partial_result = 0;
1090 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1093 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1094 zh->num_basenames, zh->basenames, setname,
1095 hits, estimated_hit_count);
1097 *partial_resultset = zh->partial_result;
1102 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1103 const char *setname, zint *hits)
1105 int estimated_hit_count;
1106 int partial_resultset;
1107 return zebra_search_RPN_x(zh, o, query, setname, hits,
1108 &estimated_hit_count,
1109 &partial_resultset);
1112 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1113 const char *setname,
1114 Z_RecordComposition *comp,
1115 const Odr_oid *input_format, int num_recs,
1116 ZebraRetrievalRecord *recs)
1118 ZebraMetaRecord *poset;
1120 ZEBRA_RES ret = ZEBRA_OK;
1123 ZEBRA_CHECK_HANDLE(zh);
1129 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1133 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1138 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1141 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1142 for (i = 0; i<num_recs; i++)
1143 pos_array[i] = recs[i].position;
1144 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1147 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1148 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1154 WRBUF addinfo_w = wrbuf_alloc();
1155 for (i = 0; i < num_recs; i++)
1157 recs[i].errCode = 0;
1158 recs[i].errString = 0;
1163 recs[i].sysno = poset[i].sysno;
1166 recs[i].format = yaz_oid_recsyn_sutrs;
1167 recs[i].len = strlen(poset[i].term);
1168 recs[i].buf = poset[i].term;
1169 recs[i].base = poset[i].db;
1171 else if (poset[i].sysno)
1175 zebra_snippets *hit_snippet = zebra_snippets_create();
1177 /* we disable hit snippets for now. It does not work well
1178 and it slows retrieval down a lot */
1180 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1183 wrbuf_rewind(addinfo_w);
1185 zebra_record_fetch(zh, setname,
1186 poset[i].sysno, poset[i].score,
1187 stream, input_format, comp,
1188 &recs[i].format, &buf, &len,
1189 &recs[i].base, addinfo_w);
1191 if (wrbuf_len(addinfo_w))
1193 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1197 recs[i].buf = (char*) odr_malloc(stream, len);
1198 memcpy(recs[i].buf, buf, len);
1202 recs[i].score = poset[i].score;
1203 zebra_snippets_destroy(hit_snippet);
1207 /* only need to set it once */
1208 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1210 zebra_setError_zint(zh,
1211 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1218 zebra_meta_records_destroy(zh, poset, num_recs);
1219 wrbuf_destroy(addinfo_w);
1226 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1228 int *num_entries, ZebraScanEntry **entries,
1230 const char *setname)
1232 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1233 Z_AttributesPlusTerm *zapt;
1234 Odr_oid *attributeSet;
1237 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1240 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1244 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1245 position, num_entries, entries, is_partial,
1248 yaz_pqf_destroy(pqf_parser);
1252 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1253 const Odr_oid *attributeset,
1255 int *num_entries, ZebraScanEntry **entries,
1257 const char *setname)
1261 ZEBRA_CHECK_HANDLE(zh);
1266 assert(num_entries);
1269 yaz_log(log_level, "zebra_scan");
1271 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1278 res = rpn_scan(zh, stream, zapt, attributeset,
1279 zh->num_basenames, zh->basenames, position,
1280 num_entries, entries, is_partial, setname);
1285 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1286 int num_input_setnames, const char **input_setnames,
1287 const char *output_setname,
1288 Z_SortKeySpecList *sort_sequence,
1292 ZEBRA_CHECK_HANDLE(zh);
1294 assert(num_input_setnames>0);
1295 assert(input_setnames);
1296 assert(sort_sequence);
1297 assert(sort_status);
1298 yaz_log(log_level, "zebra_sort");
1300 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1302 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1303 output_setname, sort_sequence, sort_status);
1308 int zebra_deleteResultSet(ZebraHandle zh, int function,
1309 int num_setnames, char **setnames,
1314 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1316 if (zebra_begin_read(zh))
1317 return Z_DeleteStatus_systemProblemAtTarget;
1320 case Z_DeleteResultSetRequest_list:
1321 assert(num_setnames>0);
1323 resultSetDestroy(zh, num_setnames, setnames, statuses);
1325 case Z_DeleteResultSetRequest_all:
1326 resultSetDestroy(zh, -1, 0, statuses);
1330 status = Z_DeleteStatus_success;
1331 for (i = 0; i<num_setnames; i++)
1332 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1333 status = statuses[i];
1337 int zebra_errCode(ZebraHandle zh)
1341 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1344 yaz_log(log_level, "zebra_errCode: o");
1348 const char *zebra_errString(ZebraHandle zh)
1352 e= diagbib1_str(zh->errCode);
1353 yaz_log(log_level, "zebra_errString: %s",e);
1357 char *zebra_errAdd(ZebraHandle zh)
1362 yaz_log(log_level, "zebra_errAdd: %s",a);
1366 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1369 const char *astring;
1373 ZEBRA_CHECK_HANDLE(zh);
1377 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1378 p = res_get(zs->global_res, u);
1379 xfree(zh->user_perm);
1380 zh->user_perm = xstrdup(p ? p : "r");
1382 /* Determine database access list */
1383 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1385 zh->dbaccesslist = xstrdup(astring);
1387 zh->dbaccesslist = 0;
1389 /* users that don't require a password .. */
1390 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1393 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1398 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1399 const char *record_type)
1401 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1402 database, record_type);
1403 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1405 return zebra_begin_trans(zh, 1);
1408 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1410 ZEBRA_CHECK_HANDLE(zh);
1411 yaz_log(log_level, "zebra_admin_import_end");
1412 return zebra_end_trans(zh);
1415 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1417 ZEBRA_RES res = ZEBRA_OK;
1420 ZEBRA_CHECK_HANDLE(zh);
1421 yaz_log(log_level, "zebra_admin_import_segment");
1423 for (i = 0; i<segment->num_segmentRecords; i++)
1425 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1427 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1429 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1430 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1432 Odr_oct *oct = fragment->u.notExternallyTagged;
1435 if(zebra_update_record(
1438 0, /* record Type */
1442 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1450 int delete_w_handle(const char *info, void *handle)
1452 ZebraHandle zh = (ZebraHandle) handle;
1455 if (*info == sizeof(pos))
1457 memcpy(&pos, info+1, sizeof(pos));
1458 isamb_unlink(zh->reg->isamb, pos);
1463 int delete_w_all_handle(const char *info, void *handle)
1465 ZebraHandle zh = (ZebraHandle) handle;
1468 if (*info == sizeof(pos))
1471 memcpy(&pos, info+1, sizeof(pos));
1472 pt = isamb_pp_open(zh->reg->isamb, pos, 2);
1477 while (isamb_pp_read(pt, &key))
1480 rec = rec_get(zh->reg->records, key.mem[0]);
1481 rec_del(zh->reg->records, &rec);
1486 return delete_w_handle(info, handle);
1489 static int delete_SU_handle(void *handle, int ord,
1490 const char *index_type, const char *string_index,
1491 zinfo_index_category_t cat)
1493 ZebraHandle zh = (ZebraHandle) handle;
1497 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s cat=%d", ord,
1498 index_type, string_index, (int) cat);
1500 ord_len = key_SU_encode(ord, ord_buf);
1501 ord_buf[ord_len] = '\0';
1503 assert(zh->reg->isamb);
1504 assert(zh->reg->records);
1505 dict_delete_subtree(zh->reg->dict, ord_buf,
1507 !strcmp(string_index, "_ALLRECORDS") ?
1508 delete_w_all_handle : delete_w_handle);
1512 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1514 ZEBRA_RES ret = ZEBRA_OK;
1516 yaz_log(log_level, "zebra_drop_database %s", db);
1517 ZEBRA_CHECK_HANDLE(zh);
1519 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1521 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1526 if (zebraExplain_curDatabase(zh->reg->zei, db))
1528 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1533 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1534 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1535 0 /* handle */, 0 /* func */);
1536 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1537 zebraExplain_removeDatabase(zh->reg->zei, zh);
1538 zebra_remove_file_match(zh);
1543 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1544 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1545 "drop database only supported for isam:b");
1548 if (zebra_end_trans(zh) != ZEBRA_OK)
1550 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1556 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1558 yaz_log(log_level, "zebra_create_database %s", db);
1559 ZEBRA_CHECK_HANDLE(zh);
1562 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1564 if (zebra_begin_trans(zh, 1))
1567 /* announce database */
1568 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1569 /* explainDatabase */))
1571 if (zebra_end_trans(zh) != ZEBRA_OK)
1573 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1575 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1578 return zebra_end_trans(zh);
1581 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1582 const char *input_str, int input_len,
1583 char *output_str, int output_len)
1586 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1590 yaz_log(log_level, "zebra_string_norm ");
1592 if (!zh->reg->zebra_maps)
1594 wrbuf = zebra_replace(zm, "", input_str, input_len);
1597 if (wrbuf_len(wrbuf) >= output_len)
1599 if (wrbuf_len(wrbuf))
1600 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1601 output_str[wrbuf_len(wrbuf)] = '\0';
1602 return wrbuf_len(wrbuf);
1605 /** \brief set register state (state*.LCK)
1606 \param zh Zebra handle
1608 \param seqno sequence number
1611 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1613 c=commit (writing to register, reading from shadow, shadow mode only)
1615 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1617 char state_fname[256];
1622 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1624 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1625 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1626 f = fopen(fname, "w");
1628 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1629 fprintf(f, "%c %d %ld\n", val, seqno, p);
1634 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1636 char state_fname[256];
1641 yaz_log(log_level, "zebra_get_state ");
1643 sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1644 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1645 f = fopen(fname, "r");
1651 if (fscanf(f, "%c %d", val, seqno) != 2)
1653 yaz_log(YLOG_ERRNO|YLOG_WARN, "fscan fail %s",
1661 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1663 return zebra_begin_trans(zh, 0);
1666 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1668 return zebra_end_trans(zh);
1671 static void read_res_for_transaction(ZebraHandle zh)
1673 const char *group = res_get(zh->res, "group");
1675 /* FIXME - do we still use groups ?? */
1677 zh->m_group = group;
1678 v = res_get_prefix(zh->res, "followLinks", group, "1");
1679 zh->m_follow_links = atoi(v);
1681 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1682 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1684 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1685 zh->m_store_keys = atoi(v);
1687 v = res_get_prefix(zh->res, "storeData", group, "1");
1688 zh->m_store_data = atoi(v);
1690 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1691 zh->m_explain_database = atoi(v);
1693 v = res_get_prefix(zh->res, "openRW", group, "1");
1694 zh->m_flag_rw = atoi(v);
1696 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1697 zh->m_file_verbose_limit = atoi(v);
1700 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1702 ZEBRA_CHECK_HANDLE(zh);
1703 zebra_select_default_database(zh);
1706 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1707 "zebra_begin_trans: no database selected");
1711 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1715 if (rw && !strchr(zh->user_perm, 'w'))
1719 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1730 const char *rval = 0;
1735 read_res_for_transaction(zh);
1738 if (zh->trans_no != 1)
1740 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1741 "zebra_begin_trans: no write trans within read");
1746 resultSetInvalidate(zh);
1747 zebra_register_close(zh->service, zh->reg);
1749 zh->trans_w_no = zh->trans_no;
1751 zh->records_inserted = 0;
1752 zh->records_updated = 0;
1753 zh->records_deleted = 0;
1754 zh->records_processed = 0;
1755 zh->records_skipped = 0;
1757 #if HAVE_SYS_TIMES_H
1761 if (zh->shadow_enable)
1762 rval = res_get(zh->res, "shadow");
1766 zebra_lock_r(zh->lock_normal);
1767 zebra_lock_w(zh->lock_shadow);
1771 zebra_lock_w(zh->lock_normal);
1772 zebra_lock_w(zh->lock_shadow);
1774 zebra_get_state(zh, &val, &seqno);
1777 /* either we didn't finish commit or shadow is dirty */
1780 yaz_log(YLOG_WARN, "previous transaction did not finish "
1781 "(shadow disabled)");
1783 zebra_unlock(zh->lock_shadow);
1784 zebra_unlock(zh->lock_normal);
1785 if (zebra_commit(zh))
1793 zebra_lock_r(zh->lock_normal);
1794 zebra_lock_w(zh->lock_shadow);
1798 zebra_lock_w(zh->lock_normal);
1799 zebra_lock_w(zh->lock_shadow);
1803 zebra_set_state(zh, 'd', seqno);
1805 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1806 1, rval ? 1 : 0, zh->res,
1809 zh->reg->seqno = seqno;
1812 zebra_set_state(zh, 'o', seqno);
1814 zebra_unlock(zh->lock_shadow);
1815 zebra_unlock(zh->lock_normal);
1820 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1821 "zebra_begin_trans: cannot open register");
1822 yaz_log(YLOG_FATAL, "%s", zh->errString);
1825 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1835 if (zh->trans_no != 1)
1837 return zebra_flush_reg(zh);
1839 #if HAVE_SYS_TIMES_H
1845 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1848 if (!zh->lock_normal || !zh->lock_shadow)
1851 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1854 zebra_get_state(zh, &val, &seqno);
1860 else if (seqno != zh->reg->seqno)
1862 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1863 seqno, zh->reg->seqno);
1866 else if (zh->reg->last_val != val)
1868 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1869 val, zh->reg->last_val);
1876 zebra_lock_r(zh->lock_shadow);
1878 zebra_lock_r(zh->lock_normal);
1882 resultSetInvalidate(zh);
1883 zebra_register_close(zh->service, zh->reg);
1885 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1886 0, val == 'c' ? 1 : 0,
1887 zh->res, zh->path_reg);
1890 zebra_unlock(zh->lock_normal);
1891 zebra_unlock(zh->lock_shadow);
1893 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1896 zh->reg->last_val = val;
1897 zh->reg->seqno = seqno;
1899 read_res_for_transaction(zh);
1903 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1905 ZebraTransactionStatus dummy;
1907 yaz_log(log_level, "zebra_end_trans");
1908 ZEBRA_CHECK_HANDLE(zh);
1909 return zebra_end_transaction(zh, &dummy);
1912 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1918 ZEBRA_CHECK_HANDLE(zh);
1921 yaz_log(log_level, "zebra_end_transaction");
1923 status->processed = 0;
1924 status->inserted = 0;
1925 status->updated = 0;
1926 status->deleted = 0;
1930 if (!zh->res || !zh->reg)
1932 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1933 "zebra_end_trans: no open transaction");
1936 if (zh->trans_no != zh->trans_w_no)
1939 if (zh->trans_no != 0)
1942 /* release read lock */
1944 zebra_unlock(zh->lock_normal);
1945 zebra_unlock(zh->lock_shadow);
1948 { /* release write lock */
1952 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1953 rval = res_get(zh->res, "shadow");
1955 zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1957 zebra_flush_reg(zh);
1959 resultSetInvalidate(zh);
1961 zebra_register_close(zh->service, zh->reg);
1964 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1965 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1966 zh->records_processed, zh->records_inserted,
1967 zh->records_updated, zh->records_deleted);
1969 status->processed = zh->records_processed;
1970 status->inserted = zh->records_inserted;
1971 status->updated = zh->records_updated;
1972 status->deleted = zh->records_deleted;
1974 zebra_get_state(zh, &val, &seqno);
1977 BFiles bfs = bfs_create(rval, zh->path_reg);
1978 bf_commitClean(bfs, rval);
1983 zebra_set_state(zh, 'o', seqno);
1984 zebra_unlock(zh->lock_shadow);
1985 zebra_unlock(zh->lock_normal);
1988 #if HAVE_SYS_TIMES_H
1990 yaz_log(log_level, "user/system: %ld/%ld",
1991 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1992 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1994 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1995 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
2000 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
2002 return zebra_repository_index(zh, path, action_update);
2005 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2007 return zebra_repository_index(zh, path, action_delete);
2010 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
2011 enum zebra_recctrl_action_t action)
2016 if (action == action_update)
2017 yaz_log(log_level, "updating %s", path);
2018 else if (action == action_delete)
2019 yaz_log(log_level, "deleting %s", path);
2020 else if (action == action_a_delete)
2021 yaz_log(log_level, "attempt deleting %s", path);
2023 yaz_log(log_level, "update action=%d", (int) action);
2025 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
2026 return zebra_update_file_match(zh, path);
2028 return zebra_update_from_path(zh, path, action);
2031 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2035 yaz_log(log_level, "zebra_repository_show");
2036 repositoryShow(zh, path);
2040 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2046 ZEBRA_RES res = ZEBRA_OK;
2050 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
2051 zebra_select_default_database(zh);
2054 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2057 rval = res_get(zh->res, "shadow");
2060 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2064 zebra_lock_w(zh->lock_normal);
2065 zebra_lock_r(zh->lock_shadow);
2067 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2070 zebra_unlock(zh->lock_shadow);
2071 zebra_unlock(zh->lock_normal);
2074 zebra_get_state(zh, &val, &seqno);
2078 /* shadow area is dirty and so we must throw it away */
2079 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2082 else if (val == 'c')
2084 /* commit has started. We can not remove it anymore */
2089 bf_cache(bfs, rval);
2090 if (bf_commitExists(bfs))
2093 zebra_set_state(zh, 'd', seqno);
2096 zebra_set_state(zh, 'c', seqno);
2098 yaz_log(log_level, "commit start");
2099 if (bf_commitExec(bfs))
2102 if (res == ZEBRA_OK)
2105 zebra_set_state(zh, 'o', seqno);
2107 zebra_unlock(zh->lock_shadow);
2108 zebra_unlock(zh->lock_normal);
2110 zebra_lock_w(zh->lock_shadow);
2111 bf_commitClean(bfs, rval);
2112 zebra_unlock(zh->lock_shadow);
2116 zebra_unlock(zh->lock_shadow);
2117 zebra_unlock(zh->lock_normal);
2118 yaz_log(YLOG_WARN, "zebra_commit: failed");
2123 zebra_unlock(zh->lock_shadow);
2124 zebra_unlock(zh->lock_normal);
2125 yaz_log(log_level, "nothing to commit");
2132 ZEBRA_RES zebra_clean(ZebraHandle zh)
2134 yaz_log(log_level, "zebra_clean");
2135 ZEBRA_CHECK_HANDLE(zh);
2136 return zebra_commit_ex(zh, 1);
2139 ZEBRA_RES zebra_commit(ZebraHandle zh)
2141 yaz_log(log_level, "zebra_commit");
2142 ZEBRA_CHECK_HANDLE(zh);
2143 return zebra_commit_ex(zh, 0);
2147 ZEBRA_RES zebra_init(ZebraHandle zh)
2152 yaz_log(log_level, "zebra_init");
2154 ZEBRA_CHECK_HANDLE(zh);
2156 zebra_select_default_database(zh);
2159 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2160 "cannot select default database");
2163 rval = res_get(zh->res, "shadow");
2165 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2168 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2172 bf_cache(bfs, rval);
2176 zebra_set_state(zh, 'o', 0);
2180 ZEBRA_RES zebra_compact(ZebraHandle zh)
2184 yaz_log(log_level, "zebra_compact");
2185 ZEBRA_CHECK_HANDLE(zh);
2188 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2191 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2197 static ZEBRA_RES zebra_record_check(ZebraHandle zh, Record rec,
2200 ZEBRA_RES res = ZEBRA_FAIL;
2201 zebra_rec_keys_t keys = zebra_rec_keys_open();
2202 zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
2203 rec->size[recInfo_delKeys], 0);
2206 if (!zebra_rec_keys_rewind(keys))
2214 struct it_key key_in;
2215 int no_long_dict_entries = 0;
2216 int no_failed_dict_lookup = 0;
2218 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
2220 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
2221 char ord_buf[IT_MAX_WORD];
2222 int ord_len = key_SU_encode(ord, ord_buf);
2224 memcpy(ord_buf + ord_len, str, slen);
2225 ord_buf[ord_len + slen] = '\0';
2226 if (ord_len + slen >= IT_MAX_WORD)
2227 ++no_long_dict_entries;
2230 char *info = dict_lookup(zh->reg->dict, ord_buf);
2232 no_failed_dict_lookup++;
2240 if (no_long_dict_entries)
2242 yaz_log(YLOG_WARN, "Record id " ZINT_FORMAT
2243 " has %d dictionary entries that are too long",
2244 rec->sysno, no_long_dict_entries);
2246 if (no_failed_dict_lookup)
2248 yaz_log(YLOG_WARN, "Record id " ZINT_FORMAT
2249 " has %d terms that do not exist in dictionary",
2250 rec->sysno, no_failed_dict_lookup);
2254 zebra_rec_keys_close(keys);
2258 ZEBRA_RES zebra_register_check(ZebraHandle zh)
2260 ZEBRA_RES res = ZEBRA_FAIL;
2261 if (zebra_begin_read(zh) == ZEBRA_OK)
2263 zint no_records = 0;
2264 zint total_keys = 0;
2267 Record rec = rec_get_root(zh->reg->records);
2274 zebra_record_check(zh, rec, &no_keys);
2275 r1 = rec_get_next(zh->reg->records, rec);
2279 total_keys += no_keys;
2282 yaz_log(YLOG_LOG, "records: " ZINT_FORMAT, no_records);
2283 yaz_log(YLOG_LOG, "keys: " ZINT_FORMAT, total_keys);
2290 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2292 yaz_log(log_level, "zebra_result");
2295 *code = zh->errCode;
2296 *addinfo = zh->errString;
2300 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2301 *addinfo ="ZebraHandle is NULL";
2305 void zebra_shadow_enable(ZebraHandle zh, int value)
2308 yaz_log(log_level, "zebra_shadow_enable");
2309 zh->shadow_enable = value;
2312 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2314 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2315 ZEBRA_CHECK_HANDLE(zh);
2318 if (zh->iconv_to_utf8 != 0)
2319 yaz_iconv_close(zh->iconv_to_utf8);
2320 if (zh->iconv_from_utf8 != 0)
2321 yaz_iconv_close(zh->iconv_from_utf8);
2324 yaz_iconv_open("UTF-8", encoding);
2325 if (zh->iconv_to_utf8 == 0)
2326 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2327 zh->iconv_from_utf8 =
2328 yaz_iconv_open(encoding, "UTF-8");
2329 if (zh->iconv_to_utf8 == 0)
2330 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2335 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2337 yaz_log(log_level, "zebra_record_encoding");
2338 ZEBRA_CHECK_HANDLE(zh);
2339 xfree(zh->record_encoding);
2340 zh->record_encoding = 0;
2342 zh->record_encoding = xstrdup(encoding);
2346 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2350 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2352 res_set(zh->res, name, value);
2355 const char *zebra_get_resource(ZebraHandle zh,
2356 const char *name, const char *defaultvalue)
2361 v = res_get_def(zh->res, name,(char *)defaultvalue);
2362 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2366 /* moved from zebra_api_ext.c by pop */
2367 /* FIXME: Should this really be public??? -Heikki */
2369 int zebra_trans_no(ZebraHandle zh)
2371 yaz_log(log_level, "zebra_trans_no");
2373 return zh->trans_no;
2376 int zebra_get_shadow_enable(ZebraHandle zh)
2378 yaz_log(log_level, "zebra_get_shadow_enable");
2380 return zh->shadow_enable;
2383 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2385 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2387 zh->shadow_enable = value;
2390 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2391 const char *buf, int buf_size)
2393 return zebra_update_record(zh, action_update,
2394 0 /* record type */,
2401 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2402 enum zebra_recctrl_action_t action,
2403 const char *recordType,
2404 zint *sysno, const char *match,
2406 const char *buf, int buf_size)
2410 ZEBRA_CHECK_HANDLE(zh);
2414 yaz_log(log_level, "zebra_update_record");
2416 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2419 buf_size = strlen(buf);
2421 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2423 res = zebra_buffer_extract_record(zh, buf, buf_size,
2429 if (zebra_end_trans(zh) != ZEBRA_OK)
2431 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2437 /* ---------------------------------------------------------------------------
2441 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2442 const char *setname, zint *hits)
2445 ZEBRA_RES res = ZEBRA_OK;
2450 ZEBRA_CHECK_HANDLE(zh);
2452 odr = odr_createmem(ODR_ENCODE);
2457 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2459 query = p_query_rpn(odr, pqf_query);
2463 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2464 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2468 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2472 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2480 /* ---------------------------------------------------------------------------
2481 Sort - a simplified interface, with optional read locks.
2483 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2484 const char *sort_spec,
2485 const char *output_setname,
2486 const char **input_setnames)
2488 int num_input_setnames = 0;
2489 int sort_status = 0;
2490 Z_SortKeySpecList *sort_sequence;
2492 ZEBRA_CHECK_HANDLE(zh);
2495 assert(output_setname);
2496 assert(input_setnames);
2497 sort_sequence = yaz_sort_spec(stream, sort_spec);
2498 yaz_log(log_level, "sort (FIXME) ");
2501 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2502 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2506 /* we can do this, since the perl typemap code for char** will
2507 put a NULL at the end of list */
2508 while (input_setnames[num_input_setnames]) num_input_setnames++;
2510 if (zebra_begin_read(zh))
2513 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2514 output_setname, sort_sequence, &sort_status);
2520 /* ---------------------------------------------------------------------------
2521 Get BFS for Zebra system (to make alternative storage methods)
2523 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2526 return zh->reg->bfs;
2531 /* ---------------------------------------------------------------------------
2532 Set limit for search/scan
2534 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2536 ZEBRA_CHECK_HANDLE(zh);
2537 zebra_limit_destroy(zh->m_limit);
2538 zh->m_limit = zebra_limit_create(complement_flag, ids);
2543 Set Error code + addinfo
2545 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2550 nmem_reset(zh->nmem_error);
2551 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2554 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2557 sprintf(vstr, ZINT_FORMAT, i);
2560 nmem_reset(zh->nmem_error);
2561 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2564 void zebra_lock_prefix(Res res, char *path)
2566 const char *lock_dir = res_get_def(res, "lockDir", "");
2568 strcpy(path, lock_dir);
2569 if (*path && path[strlen(path)-1] != '/')
2576 * c-file-style: "Stroustrup"
2577 * indent-tabs-mode: nil
2579 * vim: shiftwidth=4 tabstop=8 expandtab