1 /* $Id: zebraapi.c,v 1.253 2007-04-16 08:44:32 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 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 void zebra_chdir (ZebraService zs)
71 yaz_log(log_level, "zebra_chdir");
72 dir = res_get (zs->global_res, "chdir");
75 yaz_log (YLOG_DEBUG, "chdir %s", dir);
83 static ZEBRA_RES zebra_flush_reg (ZebraHandle zh)
85 ZEBRA_CHECK_HANDLE(zh);
86 yaz_log(log_level, "zebra_flush_reg");
87 zebraExplain_flush (zh->reg->zei, zh);
89 key_block_flush(zh->reg->key_block, 1);
91 zebra_index_merge(zh);
95 static struct zebra_register *zebra_register_open(ZebraService zs,
97 int rw, int useshadow,
99 const char *reg_path);
100 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
102 ZebraHandle zebra_open(ZebraService zs, Res res)
105 const char *default_encoding;
106 if (!log_level_initialized)
108 log_level = yaz_log_module_level("zebraapi");
109 log_level_initialized = 1;
112 yaz_log(log_level, "zebra_open");
117 zh = (ZebraHandle) xmalloc(sizeof(*zh));
118 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
121 zh->reg = 0; /* no register attached yet */
127 zh->session_res = res_open(zs->global_res, res);
129 zh->dbaccesslist = 0;
131 zh->reg_name = xstrdup ("");
133 zh->num_basenames = 0;
136 zh->approx_limit = DEFAULT_APPROX_LIMIT;
143 zh->shadow_enable = 1;
144 zh->m_staticrank = 0;
145 zh->m_segment_indexing = 0;
147 zh->break_handler_func = 0;
148 zh->break_handler_data = 0;
150 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
153 yaz_iconv_open ("UTF-8", default_encoding);
154 if (zh->iconv_to_utf8 == 0)
155 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
157 zh->iconv_from_utf8 =
158 yaz_iconv_open (default_encoding, "UTF-8");
159 if (zh->iconv_to_utf8 == 0)
160 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
163 zh->record_encoding = 0;
165 zebra_mutex_cond_lock (&zs->session_lock);
167 zh->next = zs->sessions;
170 zebra_mutex_cond_unlock (&zs->session_lock);
172 zh->store_data_buf = 0;
174 zh->m_limit = zebra_limit_create(1, 0);
176 zh->nmem_error = nmem_create();
181 ZebraService zebra_start(const char *configName)
183 return zebra_start_res(configName, 0, 0);
186 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
192 if (!log_level_initialized)
194 log_level = yaz_log_module_level("zebraapi");
195 log_level_initialized = 1;
198 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
199 configName ? configName : "");
201 if ((res = res_open(def_res, over_res)))
203 const char *passwd_plain = 0;
204 const char *passwd_encrypt = 0;
205 const char *dbaccess = 0;
210 ZEBRA_RES ret = res_read_file(res, configName);
216 if (zebra_check_res(res))
218 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
225 zebra_check_res(res);
228 zh = xmalloc(sizeof(*zh));
229 zh->global_res = res;
234 zebra_mutex_cond_init (&zh->session_lock);
235 passwd_plain = res_get (zh->global_res, "passwd");
236 passwd_encrypt = res_get (zh->global_res, "passwd.c");
237 dbaccess = res_get (zh->global_res, "dbaccess");
239 if (!passwd_plain && !passwd_encrypt)
240 zh->passwd_db = NULL;
243 zh->passwd_db = passwd_db_open();
245 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
249 passwd_db_file_plain(zh->passwd_db, passwd_plain);
251 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
258 zh->dbaccess = res_open(NULL, NULL);
259 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
260 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
265 zh->timing = yaz_timing_create();
266 zh->path_root = res_get (zh->global_res, "root");
267 zh->nmem = nmem_create();
268 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
272 const char *module_path = res_get(res, "modulePath");
274 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
282 void zebra_filter_info(ZebraService zs, void *cd,
283 void (*cb)(void *cd, const char *name))
287 recTypeClass_info(zs->record_classes, cd, cb);
290 void zebra_pidfname(ZebraService zs, char *path)
293 zebra_lock_prefix (zs->global_res, path);
294 strcat(path, "zebrasrv.pid");
297 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
298 int compact_flag, Res res)
300 int page_size = 4096;
301 char resource_str[200];
302 sprintf (resource_str, "dict.%.100s.pagesize", name);
306 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
307 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
309 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
313 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
314 int rw, int useshadow, Res res,
315 const char *reg_path)
317 struct zebra_register *reg;
318 int record_compression = REC_COMPRESS_NONE;
319 const char *recordCompression = 0;
320 const char *profilePath;
322 int sort_type = ZEBRA_SORT_TYPE_FLAT;
323 ZEBRA_RES ret = ZEBRA_OK;
327 reg = xmalloc(sizeof(*reg));
330 reg->name = xstrdup (name);
337 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
338 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
340 reg->dh = data1_create();
347 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
350 data1_destroy(reg->dh);
357 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
359 bfs_destroy(reg->bfs);
360 data1_destroy(reg->dh);
367 getcwd(cwd, sizeof(cwd)-1);
368 profilePath = res_get_def(res, "profilePath", 0);
370 data1_set_tabpath (reg->dh, profilePath);
371 data1_set_tabroot (reg->dh, reg_path);
372 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
375 zebra_maps_open(res, reg_path, profilePath);
376 if (!reg->zebra_maps)
378 recTypes_destroy(reg->recTypes);
379 bfs_destroy(reg->bfs);
380 data1_destroy(reg->dh);
385 reg->rank_classes = NULL;
388 reg->keys = zebra_rec_keys_open();
390 reg->sortKeys = zebra_rec_keys_open();
401 /* installing rank classes */
402 zebraRankInstall (reg, rank_1_class);
403 zebraRankInstall (reg, rank_similarity_class);
404 zebraRankInstall (reg, rank_static_class);
406 recordCompression = res_get_def (res, "recordCompression", "none");
407 if (!strcmp (recordCompression, "none"))
408 record_compression = REC_COMPRESS_NONE;
409 if (!strcmp (recordCompression, "bzip2"))
410 record_compression = REC_COMPRESS_BZIP2;
414 const char *index_fname = res_get_def(res, "index", "default.idx");
415 if (index_fname && *index_fname)
417 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
422 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
424 yaz_log (YLOG_WARN, "rec_open failed");
429 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
431 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
433 yaz_log (YLOG_WARN, "dict_open failed");
438 if (res_get_match (res, "sortindex", "f", "f"))
439 sort_type = ZEBRA_SORT_TYPE_FLAT;
440 else if (res_get_match (res, "sortindex", "i", "f"))
441 sort_type = ZEBRA_SORT_TYPE_ISAMB;
444 yaz_log (YLOG_WARN, "bad_value for 'sortindex'");
449 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
451 yaz_log (YLOG_WARN, "zebra_sort_open failed");
454 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
456 struct ISAMS_M_s isams_m;
457 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
458 key_isams_m(res, &isams_m))))
460 yaz_log (YLOG_WARN, "isams_open failed");
464 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
466 struct ISAMC_M_s isamc_m;
467 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
468 rw, key_isamc_m(res, &isamc_m))))
470 yaz_log (YLOG_WARN, "isamc_open failed");
474 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
476 struct ISAMC_M_s isamc_m;
478 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
479 rw, key_isamc_m(res, &isamc_m), 0)))
481 yaz_log (YLOG_WARN, "isamb_open failed");
485 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
487 struct ISAMC_M_s isamc_m;
489 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
490 rw, key_isamc_m(res, &isamc_m), 1)))
492 yaz_log (YLOG_WARN, "isamb_open failed");
496 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
498 struct ISAMC_M_s isamc_m;
500 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
501 rw, key_isamc_m(res, &isamc_m), -1)))
503 yaz_log (YLOG_WARN, "isamb_open failed");
509 reg->zei = zebraExplain_open(reg->records, reg->dh,
511 zebra_extract_explain);
514 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
521 zebra_register_close(zs, reg);
524 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
528 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
530 ZEBRA_CHECK_HANDLE(zh);
531 yaz_log(log_level, "zebra_admin_shutdown");
533 zebra_mutex_cond_lock (&zh->service->session_lock);
534 zh->service->stop_flag = 1;
535 zebra_mutex_cond_unlock (&zh->service->session_lock);
539 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
542 ZEBRA_CHECK_HANDLE(zh);
543 yaz_log(log_level, "zebra_admin_start");
545 zebra_mutex_cond_lock (&zs->session_lock);
546 zebra_mutex_cond_unlock (&zs->session_lock);
550 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
554 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
558 zebraExplain_close (reg->zei);
559 dict_close (reg->dict);
561 dict_close (reg->matchDict);
562 zebra_sort_close(reg->sort_index);
564 isams_close (reg->isams);
566 isamc_close (reg->isamc);
568 isamb_close (reg->isamb);
569 rec_close (®->records);
571 recTypes_destroy (reg->recTypes);
572 zebra_maps_close (reg->zebra_maps);
573 zebraRankDestroy (reg);
574 bfs_destroy (reg->bfs);
575 data1_destroy (reg->dh);
577 zebra_rec_keys_close(reg->keys);
578 zebra_rec_keys_close(reg->sortKeys);
580 key_block_destroy(®->key_block);
585 ZEBRA_RES zebra_stop(ZebraService zs)
591 zebra_close (zs->sessions);
594 zebra_mutex_cond_destroy (&zs->session_lock);
597 passwd_db_close (zs->passwd_db);
599 recTypeClass_destroy(zs->record_classes);
600 nmem_destroy(zs->nmem);
601 res_close (zs->global_res);
603 yaz_timing_stop(zs->timing);
604 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
605 yaz_timing_get_real(zs->timing),
606 yaz_timing_get_user(zs->timing),
607 yaz_timing_get_sys(zs->timing));
610 yaz_timing_destroy(&zs->timing);
615 ZEBRA_RES zebra_close(ZebraHandle zh)
618 struct zebra_session **sp;
621 yaz_log(log_level, "zebra_close");
622 ZEBRA_CHECK_HANDLE(zh);
627 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
628 resultSetDestroy (zh, -1, 0, 0);
631 zebra_register_close(zh->service, zh->reg);
632 zebra_close_res (zh);
633 res_close(zh->session_res);
635 xfree(zh->record_encoding);
637 xfree(zh->dbaccesslist);
639 for (i = 0; i < zh->num_basenames; i++)
640 xfree(zh->basenames[i]);
641 xfree(zh->basenames);
643 if (zh->iconv_to_utf8 != 0)
644 yaz_iconv_close (zh->iconv_to_utf8);
645 if (zh->iconv_from_utf8 != 0)
646 yaz_iconv_close (zh->iconv_from_utf8);
648 zebra_mutex_cond_lock (&zs->session_lock);
649 zebra_lock_destroy (zh->lock_normal);
650 zebra_lock_destroy (zh->lock_shadow);
662 zebra_mutex_cond_unlock (&zs->session_lock);
664 xfree(zh->user_perm);
665 zh->service = 0; /* more likely to trigger an assert */
667 zebra_limit_destroy(zh->m_limit);
669 nmem_destroy(zh->nmem_error);
676 struct map_baseinfo {
682 char **new_basenames;
686 static void zebra_open_res(ZebraHandle zh)
694 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
695 zh->res = res_open(zh->session_res, 0);
696 res_read_file(zh->res, fname);
698 else if (*zh->reg_name == 0)
700 zh->res = res_open(zh->session_res, 0);
704 yaz_log (YLOG_WARN, "no register root specified");
705 zh->res = 0; /* no path for register - fail! */
709 static void zebra_close_res (ZebraHandle zh)
717 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
721 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
725 assert (zh->reg == 0);
726 assert (*zh->reg_name == 0);
732 resultSetInvalidate (zh);
733 zebra_register_close(zh->service, zh->reg);
739 zh->reg_name = xstrdup (new_reg);
743 if (zh->service->path_root)
745 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
746 strlen(zh->reg_name) + 3);
747 strcpy (zh->path_reg, zh->service->path_root);
750 strcat (zh->path_reg, "/");
751 strcat (zh->path_reg, zh->reg_name);
757 zebra_lock_destroy (zh->lock_normal);
761 zebra_lock_destroy (zh->lock_shadow);
767 const char *lock_area = res_get (zh->res, "lockDir");
769 if (!lock_area && zh->path_reg)
770 res_set (zh->res, "lockDir", zh->path_reg);
771 sprintf (fname, "norm.%s.LCK", zh->reg_name);
773 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
775 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
777 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
779 if (!zh->lock_normal || !zh->lock_shadow)
783 zebra_lock_destroy(zh->lock_normal);
788 zebra_lock_destroy(zh->lock_shadow);
797 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
798 zebra_set_approx_limit(zh, approx);
802 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
803 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
807 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
810 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
811 zh->m_segment_indexing);
816 void map_basenames_func (void *vp, const char *name, const char *value)
818 struct map_baseinfo *p = (struct map_baseinfo *) vp;
820 char fromdb[128], todb[8][128];
827 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
828 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
829 todb[5], todb[6], todb[7]);
833 for (i = 0; i<p->num_bases; i++)
834 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
837 for (i = 0; i < no; i++)
839 if (p->new_num_bases == p->new_num_max)
841 p->new_basenames[(p->new_num_bases)++] =
842 nmem_strdup (p->mem, todb[i]);
848 int zebra_select_default_database(ZebraHandle zh)
852 /* no database has been selected - so we select based on
853 resource setting (including group)
855 const char *group = res_get(zh->session_res, "group");
856 const char *v = res_get_prefix(zh->session_res,
857 "database", group, "Default");
858 return zebra_select_database(zh, v);
863 void map_basenames (ZebraHandle zh, ODR stream,
864 int *num_bases, char ***basenames)
866 struct map_baseinfo info;
867 struct map_baseinfo *p = &info;
870 yaz_log(log_level, "map_basenames ");
875 info.num_bases = *num_bases;
876 info.basenames = *basenames;
877 info.new_num_max = 128;
878 info.new_num_bases = 0;
879 info.new_basenames = (char **)
880 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
881 info.mem = stream->mem;
883 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
885 for (i = 0; i<p->num_bases; i++)
886 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
888 p->new_basenames[(p->new_num_bases)++] =
889 nmem_strdup (p->mem, p->basenames[i]);
891 *num_bases = info.new_num_bases;
892 *basenames = info.new_basenames;
893 for (i = 0; i<*num_bases; i++)
894 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
897 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
899 ZEBRA_CHECK_HANDLE(zh);
901 yaz_log(log_level, "zebra_select_database %s",basename);
903 return zebra_select_databases (zh, 1, &basename);
906 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
907 const char **basenames)
914 ZEBRA_CHECK_HANDLE(zh);
917 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
918 num_bases,basenames[0]);
923 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
927 /* Check if the user has access to all databases (Seb) */
928 /* You could argue that this should happen later, after we have
929 * determined that the database(s) exist. */
930 if (zh->dbaccesslist) {
931 for (i = 0; i < num_bases; i++) {
932 const char *db = basenames[i];
934 for (p = zh->dbaccesslist; p && *p; p = pp) {
936 if ((pp = strchr(p, '+'))) {
942 if (len == strlen(db) && !strncmp(db, p, len))
946 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
952 for (i = 0; i < zh->num_basenames; i++)
953 xfree(zh->basenames[i]);
954 xfree(zh->basenames);
956 zh->num_basenames = num_bases;
957 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
958 for (i = 0; i < zh->num_basenames; i++)
959 zh->basenames[i] = xstrdup (basenames[i]);
961 cp = strrchr(basenames[0], '/');
964 len = cp - basenames[0];
965 new_reg = xmalloc(len + 1);
966 memcpy (new_reg, basenames[0], len);
970 new_reg = xstrdup ("");
971 for (i = 1; i<num_bases; i++)
975 cp1 = strrchr (basenames[i], '/');
980 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
983 if (len != cp1 - basenames[i] ||
984 memcmp (basenames[i], new_reg, len))
986 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
994 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
999 zebra_select_register (zh, new_reg);
1003 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1006 if (!zh->lock_normal || !zh->lock_shadow)
1008 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1014 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1016 if (approx_limit == 0)
1017 approx_limit = DEFAULT_APPROX_LIMIT;
1018 zh->approx_limit = approx_limit;
1022 void zebra_set_partial_result(ZebraHandle zh)
1024 zh->partial_result = 1;
1028 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1029 int (*f)(void *client_data),
1032 zh->break_handler_func = f;
1033 zh->break_handler_data = client_data;
1037 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1038 const char *setname, zint *hits,
1039 int *estimated_hit_count,
1040 int *partial_resultset)
1044 ZEBRA_CHECK_HANDLE(zh);
1050 yaz_log(log_level, "zebra_search_rpn");
1052 zh->partial_result = 0;
1054 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1057 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1058 zh->num_basenames, zh->basenames, setname,
1059 hits, estimated_hit_count);
1061 *partial_resultset = zh->partial_result;
1066 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1067 const char *setname, zint *hits)
1069 int estimated_hit_count;
1070 int partial_resultset;
1071 return zebra_search_RPN_x(zh, o, query, setname, hits,
1072 &estimated_hit_count,
1073 &partial_resultset);
1076 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1077 const char *setname,
1078 Z_RecordComposition *comp,
1079 const int *input_format, int num_recs,
1080 ZebraRetrievalRecord *recs)
1082 ZebraMetaRecord *poset;
1084 ZEBRA_RES ret = ZEBRA_OK;
1087 ZEBRA_CHECK_HANDLE(zh);
1093 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1097 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1102 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1105 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1106 for (i = 0; i<num_recs; i++)
1107 pos_array[i] = recs[i].position;
1108 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1111 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1112 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1118 for (i = 0; i<num_recs; i++)
1122 recs[i].errCode = 0;
1123 recs[i].format = yaz_oid_sutrs();
1124 recs[i].len = strlen(poset[i].term);
1125 recs[i].buf = poset[i].term;
1126 recs[i].base = poset[i].db;
1128 else if (poset[i].sysno)
1132 zebra_snippets *hit_snippet = zebra_snippets_create();
1134 /* we disable hit snippets for now. It does not work well
1135 and it slows retrieval down a lot */
1137 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1141 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1143 stream, input_format, comp,
1144 &recs[i].format, &buf, &len,
1145 &recs[i].base, &recs[i].errString);
1150 recs[i].buf = (char*) odr_malloc(stream, len);
1151 memcpy(recs[i].buf, buf, len);
1155 recs[i].score = poset[i].score;
1156 recs[i].sysno = poset[i].sysno;
1157 zebra_snippets_destroy(hit_snippet);
1161 /* only need to set it once */
1162 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1164 zebra_setError_zint(zh,
1165 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1170 recs[i].buf = 0; /* no record and no error issued */
1172 recs[i].errCode = 0;
1177 zebra_meta_records_destroy(zh, poset, num_recs);
1179 zebra_end_read (zh);
1184 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1186 int *num_entries, ZebraScanEntry **entries,
1188 const char *setname)
1190 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1191 Z_AttributesPlusTerm *zapt;
1195 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1198 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1202 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib1(),
1203 position, num_entries, entries, is_partial,
1206 yaz_pqf_destroy (pqf_parser);
1210 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1211 const int *attributeset,
1213 int *num_entries, ZebraScanEntry **entries,
1215 const char *setname)
1218 RSET limit_rset = 0;
1220 ZEBRA_CHECK_HANDLE(zh);
1225 assert(num_entries);
1228 yaz_log(log_level, "zebra_scan");
1230 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1238 limit_rset = resultSetRef(zh, setname);
1242 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1244 zebra_end_read (zh);
1248 res = rpn_scan(zh, stream, zapt, attributeset,
1249 zh->num_basenames, zh->basenames, position,
1250 num_entries, entries, is_partial, limit_rset);
1255 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1256 int num_input_setnames, const char **input_setnames,
1257 const char *output_setname,
1258 Z_SortKeySpecList *sort_sequence,
1262 ZEBRA_CHECK_HANDLE(zh);
1264 assert(num_input_setnames>0);
1265 assert(input_setnames);
1266 assert(sort_sequence);
1267 assert(sort_status);
1268 yaz_log(log_level, "zebra_sort");
1270 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1272 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1273 output_setname, sort_sequence, sort_status);
1278 int zebra_deleteResultSet(ZebraHandle zh, int function,
1279 int num_setnames, char **setnames,
1285 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1287 if (zebra_begin_read(zh))
1288 return Z_DeleteStatus_systemProblemAtTarget;
1291 case Z_DeleteResultSetRequest_list:
1292 assert(num_setnames>0);
1294 resultSetDestroy (zh, num_setnames, setnames, statuses);
1296 case Z_DeleteResultSetRequest_all:
1297 resultSetDestroy (zh, -1, 0, statuses);
1300 zebra_end_read (zh);
1301 status = Z_DeleteStatus_success;
1302 for (i = 0; i<num_setnames; i++)
1303 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1304 status = statuses[i];
1308 int zebra_errCode (ZebraHandle zh)
1312 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1315 yaz_log(log_level, "zebra_errCode: o");
1319 const char *zebra_errString (ZebraHandle zh)
1323 e= diagbib1_str (zh->errCode);
1324 yaz_log(log_level, "zebra_errString: %s",e);
1328 char *zebra_errAdd (ZebraHandle zh)
1333 yaz_log(log_level, "zebra_errAdd: %s",a);
1337 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1340 const char *astring;
1344 ZEBRA_CHECK_HANDLE(zh);
1348 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1349 p = res_get(zs->global_res, u);
1350 xfree(zh->user_perm);
1351 zh->user_perm = xstrdup(p ? p : "r");
1353 /* Determine database access list */
1354 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1356 zh->dbaccesslist = xstrdup(astring);
1358 zh->dbaccesslist = 0;
1360 /* users that don't require a password .. */
1361 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1364 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1369 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1370 const char *record_type)
1372 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1373 database, record_type);
1374 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1376 return zebra_begin_trans(zh, 1);
1379 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1381 ZEBRA_CHECK_HANDLE(zh);
1382 yaz_log(log_level, "zebra_admin_import_end");
1383 return zebra_end_trans(zh);
1386 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1388 ZEBRA_RES res = ZEBRA_OK;
1391 ZEBRA_CHECK_HANDLE(zh);
1392 yaz_log(log_level, "zebra_admin_import_segment");
1394 for (i = 0; i<segment->num_segmentRecords; i++)
1396 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1398 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1400 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1401 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1403 Odr_oct *oct = fragment->u.notExternallyTagged;
1406 if (zebra_update_record(
1409 0, /* record Type */
1413 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1421 int delete_w_handle(const char *info, void *handle)
1423 ZebraHandle zh = (ZebraHandle) handle;
1427 if (*info == sizeof(pos))
1429 memcpy (&pos, info+1, sizeof(pos));
1430 isamb_unlink(zh->reg->isamb, pos);
1435 static int delete_SU_handle(void *handle, int ord)
1437 ZebraHandle zh = (ZebraHandle) handle;
1441 ord_len = key_SU_encode (ord, ord_buf);
1442 ord_buf[ord_len] = '\0';
1444 assert (zh->reg->isamb);
1445 dict_delete_subtree(zh->reg->dict, ord_buf,
1446 zh, delete_w_handle);
1450 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1452 ZEBRA_RES ret = ZEBRA_OK;
1454 yaz_log(log_level, "zebra_drop_database %s", db);
1455 ZEBRA_CHECK_HANDLE(zh);
1457 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1459 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1464 if (zebraExplain_curDatabase (zh->reg->zei, db))
1466 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1471 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1472 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1473 0 /* handle */, 0 /* func */);
1474 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1475 zebraExplain_removeDatabase(zh->reg->zei, zh);
1476 zebra_remove_file_match(zh);
1481 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1482 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1483 "drop database only supported for isam:b");
1486 if (zebra_end_trans (zh) != ZEBRA_OK)
1488 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1494 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1496 yaz_log(log_level, "zebra_create_database %s", db);
1497 ZEBRA_CHECK_HANDLE(zh);
1500 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1502 if (zebra_begin_trans (zh, 1))
1505 /* announce database */
1506 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1507 /* explainDatabase */))
1509 if (zebra_end_trans (zh) != ZEBRA_OK)
1511 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1513 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1516 return zebra_end_trans (zh);
1519 int zebra_string_norm(ZebraHandle zh, unsigned reg_id,
1520 const char *input_str, int input_len,
1521 char *output_str, int output_len)
1527 yaz_log(log_level, "zebra_string_norm ");
1529 if (!zh->reg->zebra_maps)
1531 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1532 input_str, input_len);
1535 if (wrbuf_len(wrbuf) >= output_len)
1537 if (wrbuf_len(wrbuf))
1538 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1539 output_str[wrbuf_len(wrbuf)] = '\0';
1540 return wrbuf_len(wrbuf);
1543 /** \brief set register state (state*.LCK)
1544 \param zh Zebra handle
1546 \param seqno sequence number
1549 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1551 c=commit (writing to register, reading from shadow, shadow mode only)
1553 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1555 char state_fname[256];
1560 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1562 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1563 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1564 f = fopen (fname, "w");
1566 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1567 fprintf (f, "%c %d %ld\n", val, seqno, p);
1572 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1574 char state_fname[256];
1579 yaz_log(log_level, "zebra_get_state ");
1581 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1582 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1583 f = fopen (fname, "r");
1589 fscanf (f, "%c %d", val, seqno);
1595 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1597 return zebra_begin_trans(zh, 0);
1600 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1602 return zebra_end_trans(zh);
1605 static void read_res_for_transaction(ZebraHandle zh)
1607 const char *group = res_get(zh->res, "group");
1609 /* FIXME - do we still use groups ?? */
1611 zh->m_group = group;
1612 v = res_get_prefix(zh->res, "followLinks", group, "1");
1613 zh->m_follow_links = atoi(v);
1615 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1616 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1618 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1619 zh->m_store_keys = atoi(v);
1621 v = res_get_prefix(zh->res, "storeData", group, "1");
1622 zh->m_store_data = atoi(v);
1624 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1625 zh->m_explain_database = atoi(v);
1627 v = res_get_prefix(zh->res, "openRW", group, "1");
1628 zh->m_flag_rw = atoi(v);
1630 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1631 zh->m_file_verbose_limit = atoi(v);
1634 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1636 ZEBRA_CHECK_HANDLE(zh);
1637 zebra_select_default_database(zh);
1640 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1641 "zebra_begin_trans: no database selected");
1645 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1649 if (rw && !strchr(zh->user_perm, 'w'))
1653 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1664 const char *rval = 0;
1669 read_res_for_transaction(zh);
1672 if (zh->trans_no != 1)
1674 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1675 "zebra_begin_trans: no write trans within read");
1680 resultSetInvalidate (zh);
1681 zebra_register_close(zh->service, zh->reg);
1683 zh->trans_w_no = zh->trans_no;
1685 zh->records_inserted = 0;
1686 zh->records_updated = 0;
1687 zh->records_deleted = 0;
1688 zh->records_processed = 0;
1689 zh->records_skipped = 0;
1691 #if HAVE_SYS_TIMES_H
1695 if (zh->shadow_enable)
1696 rval = res_get (zh->res, "shadow");
1700 zebra_lock_r(zh->lock_normal);
1701 zebra_lock_w(zh->lock_shadow);
1705 zebra_lock_w(zh->lock_normal);
1706 zebra_lock_w(zh->lock_shadow);
1708 zebra_get_state (zh, &val, &seqno);
1711 /* either we didn't finish commit or shadow is dirty */
1714 yaz_log(YLOG_WARN, "previous transaction did not finish "
1715 "(shadow disabled)");
1717 zebra_unlock (zh->lock_shadow);
1718 zebra_unlock (zh->lock_normal);
1719 if (zebra_commit (zh))
1727 zebra_lock_r(zh->lock_normal);
1728 zebra_lock_w(zh->lock_shadow);
1732 zebra_lock_w(zh->lock_normal);
1733 zebra_lock_w(zh->lock_shadow);
1737 zebra_set_state (zh, 'd', seqno);
1739 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1740 1, rval ? 1 : 0, zh->res,
1743 zh->reg->seqno = seqno;
1746 zebra_set_state (zh, 'o', seqno);
1748 zebra_unlock (zh->lock_shadow);
1749 zebra_unlock (zh->lock_normal);
1754 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1755 "zebra_begin_trans: cannot open register");
1756 yaz_log(YLOG_FATAL, "%s", zh->errString);
1759 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1769 if (zh->trans_no != 1)
1771 return zebra_flush_reg (zh);
1773 #if HAVE_SYS_TIMES_H
1779 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1782 if (!zh->lock_normal || !zh->lock_shadow)
1785 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1788 zebra_get_state (zh, &val, &seqno);
1794 else if (seqno != zh->reg->seqno)
1796 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1797 seqno, zh->reg->seqno);
1800 else if (zh->reg->last_val != val)
1802 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1803 val, zh->reg->last_val);
1810 zebra_lock_r (zh->lock_shadow);
1812 zebra_lock_r (zh->lock_normal);
1816 resultSetInvalidate (zh);
1817 zebra_register_close(zh->service, zh->reg);
1819 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1820 0, val == 'c' ? 1 : 0,
1821 zh->res, zh->path_reg);
1824 zebra_unlock (zh->lock_normal);
1825 zebra_unlock (zh->lock_shadow);
1827 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1830 zh->reg->last_val = val;
1831 zh->reg->seqno = seqno;
1833 read_res_for_transaction(zh);
1837 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1839 ZebraTransactionStatus dummy;
1841 yaz_log(log_level, "zebra_end_trans");
1842 ZEBRA_CHECK_HANDLE(zh);
1843 return zebra_end_transaction(zh, &dummy);
1846 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1852 ZEBRA_CHECK_HANDLE(zh);
1855 yaz_log(log_level, "zebra_end_transaction");
1857 status->processed = 0;
1858 status->inserted = 0;
1859 status->updated = 0;
1860 status->deleted = 0;
1864 if (!zh->res || !zh->reg)
1866 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1867 "zebra_end_trans: no open transaction");
1870 if (zh->trans_no != zh->trans_w_no)
1873 if (zh->trans_no != 0)
1876 /* release read lock */
1878 zebra_unlock (zh->lock_normal);
1879 zebra_unlock (zh->lock_shadow);
1882 { /* release write lock */
1886 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1887 rval = res_get (zh->res, "shadow");
1889 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1891 zebra_flush_reg (zh);
1893 resultSetInvalidate (zh);
1895 zebra_register_close(zh->service, zh->reg);
1898 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1899 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1900 zh->records_processed, zh->records_inserted,
1901 zh->records_updated, zh->records_deleted);
1903 status->processed = zh->records_processed;
1904 status->inserted = zh->records_inserted;
1905 status->updated = zh->records_updated;
1906 status->deleted = zh->records_deleted;
1908 zebra_get_state (zh, &val, &seqno);
1911 BFiles bfs = bfs_create (rval, zh->path_reg);
1912 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1913 bf_commitClean (bfs, rval);
1918 zebra_set_state (zh, 'o', seqno);
1920 zebra_unlock (zh->lock_shadow);
1921 zebra_unlock (zh->lock_normal);
1924 #if HAVE_SYS_TIMES_H
1926 yaz_log (log_level, "user/system: %ld/%ld",
1927 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1928 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1930 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1931 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1936 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1940 yaz_log (log_level, "updating %s", path);
1942 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1943 return zebra_update_file_match(zh, path);
1945 return zebra_update_from_path(zh, path);
1948 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1952 yaz_log (log_level, "deleting %s", path);
1953 return zebra_delete_from_path(zh, path);
1956 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1960 yaz_log(log_level, "zebra_repository_show");
1961 repositoryShow (zh, path);
1965 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1971 ZEBRA_RES res = ZEBRA_OK;
1975 zebra_select_default_database(zh);
1978 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1981 rval = res_get(zh->res, "shadow");
1984 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1988 zebra_lock_w(zh->lock_normal);
1989 zebra_lock_r(zh->lock_shadow);
1991 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
1994 zebra_unlock(zh->lock_shadow);
1995 zebra_unlock(zh->lock_normal);
1998 zebra_get_state(zh, &val, &seqno);
2002 /* shadow area is dirty and so we must throw it away */
2003 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2008 bf_cache (bfs, rval);
2009 if (bf_commitExists (bfs))
2012 zebra_set_state(zh, 'd', seqno);
2015 zebra_set_state(zh, 'c', seqno);
2017 yaz_log(YLOG_DEBUG, "commit start");
2018 if (bf_commitExec (bfs))
2021 if (res == ZEBRA_OK)
2024 zebra_set_state(zh, 'o', seqno);
2026 zebra_unlock(zh->lock_shadow);
2027 zebra_unlock(zh->lock_normal);
2029 zebra_lock_w(zh->lock_shadow);
2030 bf_commitClean(bfs, rval);
2031 zebra_unlock(zh->lock_shadow);
2035 zebra_unlock(zh->lock_shadow);
2036 zebra_unlock(zh->lock_normal);
2037 yaz_log(YLOG_WARN, "zebra_commit: failed");
2042 zebra_unlock(zh->lock_shadow);
2043 zebra_unlock(zh->lock_normal);
2044 yaz_log(log_level, "nothing to commit");
2051 ZEBRA_RES zebra_clean(ZebraHandle zh)
2053 yaz_log(log_level, "zebra_clean");
2054 ZEBRA_CHECK_HANDLE(zh);
2055 return zebra_commit_ex(zh, 1);
2058 ZEBRA_RES zebra_commit(ZebraHandle zh)
2060 yaz_log(log_level, "zebra_commit");
2061 ZEBRA_CHECK_HANDLE(zh);
2062 return zebra_commit_ex(zh, 0);
2066 ZEBRA_RES zebra_init(ZebraHandle zh)
2071 yaz_log(log_level, "zebra_init");
2073 ZEBRA_CHECK_HANDLE(zh);
2075 zebra_select_default_database(zh);
2078 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2079 "cannot select default database");
2082 rval = res_get (zh->res, "shadow");
2084 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2087 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2091 bf_cache (bfs, rval);
2095 zebra_set_state (zh, 'o', 0);
2099 ZEBRA_RES zebra_compact(ZebraHandle zh)
2103 yaz_log(log_level, "zebra_compact");
2104 ZEBRA_CHECK_HANDLE(zh);
2107 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2110 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2116 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2118 yaz_log(log_level, "zebra_result");
2121 *code = zh->errCode;
2122 *addinfo = zh->errString;
2126 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2127 *addinfo ="ZebraHandle is NULL";
2131 void zebra_shadow_enable(ZebraHandle zh, int value)
2134 yaz_log(log_level, "zebra_shadow_enable");
2135 zh->shadow_enable = value;
2138 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2140 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2141 ZEBRA_CHECK_HANDLE(zh);
2144 if (zh->iconv_to_utf8 != 0)
2145 yaz_iconv_close(zh->iconv_to_utf8);
2146 if (zh->iconv_from_utf8 != 0)
2147 yaz_iconv_close(zh->iconv_from_utf8);
2150 yaz_iconv_open ("UTF-8", encoding);
2151 if (zh->iconv_to_utf8 == 0)
2152 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2153 zh->iconv_from_utf8 =
2154 yaz_iconv_open (encoding, "UTF-8");
2155 if (zh->iconv_to_utf8 == 0)
2156 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2161 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2163 yaz_log(log_level, "zebra_record_encoding");
2164 ZEBRA_CHECK_HANDLE(zh);
2165 xfree(zh->record_encoding);
2166 zh->record_encoding = 0;
2168 zh->record_encoding = xstrdup (encoding);
2172 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2176 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2178 res_set(zh->res, name, value);
2181 const char *zebra_get_resource(ZebraHandle zh,
2182 const char *name, const char *defaultvalue)
2187 v = res_get_def (zh->res, name, (char *)defaultvalue);
2188 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2192 /* moved from zebra_api_ext.c by pop */
2193 /* FIXME: Should this really be public??? -Heikki */
2195 int zebra_trans_no (ZebraHandle zh)
2197 yaz_log(log_level, "zebra_trans_no");
2199 return zh->trans_no;
2202 int zebra_get_shadow_enable (ZebraHandle zh)
2204 yaz_log(log_level, "zebra_get_shadow_enable");
2206 return zh->shadow_enable;
2209 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2211 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2213 zh->shadow_enable = value;
2216 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2217 const char *buf, int buf_size)
2219 return zebra_update_record(zh, action_update,
2220 0 /* record type */,
2227 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2228 enum zebra_recctrl_action_t action,
2229 const char *recordType,
2230 zint *sysno, const char *match,
2232 const char *buf, int buf_size)
2236 ZEBRA_CHECK_HANDLE(zh);
2240 yaz_log(log_level, "zebra_update_record");
2242 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2245 buf_size = strlen(buf);
2247 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2249 res = zebra_buffer_extract_record(zh, buf, buf_size,
2256 if (zebra_end_trans(zh) != ZEBRA_OK)
2258 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2264 /* ---------------------------------------------------------------------------
2268 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2269 const char *setname, zint *hits)
2272 ZEBRA_RES res = ZEBRA_OK;
2277 ZEBRA_CHECK_HANDLE(zh);
2279 odr = odr_createmem(ODR_ENCODE);
2284 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2286 query = p_query_rpn(odr, pqf_query);
2290 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2291 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2295 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2299 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2307 /* ---------------------------------------------------------------------------
2308 Sort - a simplified interface, with optional read locks.
2310 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2311 const char *sort_spec,
2312 const char *output_setname,
2313 const char **input_setnames)
2315 int num_input_setnames = 0;
2316 int sort_status = 0;
2317 Z_SortKeySpecList *sort_sequence;
2319 ZEBRA_CHECK_HANDLE(zh);
2322 assert(output_setname);
2323 assert(input_setnames);
2324 sort_sequence = yaz_sort_spec (stream, sort_spec);
2325 yaz_log(log_level, "sort (FIXME) ");
2328 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2329 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2333 /* we can do this, since the perl typemap code for char** will
2334 put a NULL at the end of list */
2335 while (input_setnames[num_input_setnames]) num_input_setnames++;
2337 if (zebra_begin_read (zh))
2340 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2341 output_setname, sort_sequence, &sort_status);
2347 /* ---------------------------------------------------------------------------
2348 Get BFS for Zebra system (to make alternative storage methods)
2350 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2353 return zh->reg->bfs;
2358 /* ---------------------------------------------------------------------------
2359 Set limit for search/scan
2361 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2363 ZEBRA_CHECK_HANDLE(zh);
2364 zebra_limit_destroy(zh->m_limit);
2365 zh->m_limit = zebra_limit_create(complement_flag, ids);
2370 Set Error code + addinfo
2372 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2377 nmem_reset(zh->nmem_error);
2378 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2381 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2384 sprintf(vstr, ZINT_FORMAT, i);
2387 nmem_reset(zh->nmem_error);
2388 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2391 void zebra_lock_prefix(Res res, char *path)
2393 const char *lock_dir = res_get_def (res, "lockDir", "");
2395 strcpy(path, lock_dir);
2396 if (*path && path[strlen(path)-1] != '/')
2403 * indent-tabs-mode: nil
2405 * vim: shiftwidth=4 tabstop=8 expandtab