1 /* $Id: zebraapi.c,v 1.265 2007-11-30 12:19:08 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 const char *zebra_get_encoding(ZebraHandle zh)
104 assert(zh && zh->session_res);
105 return res_get_def(zh->session_res, "encoding", "ISO-8859-1");
108 ZebraHandle zebra_open(ZebraService zs, Res res)
111 const char *default_encoding;
112 if (!log_level_initialized)
114 log_level = yaz_log_module_level("zebraapi");
115 log_level_initialized = 1;
118 yaz_log(log_level, "zebra_open");
123 zh = (ZebraHandle) xmalloc(sizeof(*zh));
124 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
127 zh->reg = 0; /* no register attached yet */
133 zh->session_res = res_open(zs->global_res, res);
135 zh->dbaccesslist = 0;
137 zh->reg_name = xstrdup ("");
139 zh->num_basenames = 0;
142 zh->approx_limit = DEFAULT_APPROX_LIMIT;
149 zh->shadow_enable = 1;
150 zh->m_staticrank = 0;
151 zh->m_segment_indexing = 0;
153 zh->break_handler_func = 0;
154 zh->break_handler_data = 0;
156 default_encoding = zebra_get_encoding(zh);
159 yaz_iconv_open ("UTF-8", default_encoding);
160 if (zh->iconv_to_utf8 == 0)
161 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
163 zh->iconv_from_utf8 =
164 yaz_iconv_open (default_encoding, "UTF-8");
165 if (zh->iconv_to_utf8 == 0)
166 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
169 zh->record_encoding = 0;
171 zebra_mutex_cond_lock (&zs->session_lock);
173 zh->next = zs->sessions;
176 zebra_mutex_cond_unlock (&zs->session_lock);
178 zh->store_data_buf = 0;
180 zh->m_limit = zebra_limit_create(1, 0);
182 zh->nmem_error = nmem_create();
187 ZebraService zebra_start(const char *configName)
189 return zebra_start_res(configName, 0, 0);
192 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
195 char version_str[16];
200 if (!log_level_initialized)
202 log_level = yaz_log_module_level("zebraapi");
203 log_level_initialized = 1;
206 zebra_get_version(version_str, system_str);
208 yaz_log(YLOG_LOG, "zebra_start %s %s", version_str,
209 configName ? configName : "");
211 if ((res = res_open(def_res, over_res)))
213 const char *passwd_plain = 0;
214 const char *passwd_encrypt = 0;
215 const char *dbaccess = 0;
220 ZEBRA_RES ret = res_read_file(res, configName);
226 if (zebra_check_res(res))
228 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
235 zebra_check_res(res);
238 zh = xmalloc(sizeof(*zh));
239 zh->global_res = res;
244 zebra_mutex_cond_init (&zh->session_lock);
245 passwd_plain = res_get (zh->global_res, "passwd");
246 passwd_encrypt = res_get (zh->global_res, "passwd.c");
247 dbaccess = res_get (zh->global_res, "dbaccess");
249 if (!passwd_plain && !passwd_encrypt)
250 zh->passwd_db = NULL;
253 zh->passwd_db = passwd_db_open();
255 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
259 passwd_db_file_plain(zh->passwd_db, passwd_plain);
261 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
268 zh->dbaccess = res_open(NULL, NULL);
269 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
270 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
275 zh->timing = yaz_timing_create();
276 zh->path_root = res_get (zh->global_res, "root");
277 zh->nmem = nmem_create();
278 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
282 const char *module_path = res_get(res, "modulePath");
284 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
292 void zebra_filter_info(ZebraService zs, void *cd,
293 void (*cb)(void *cd, const char *name))
297 recTypeClass_info(zs->record_classes, cd, cb);
300 void zebra_pidfname(ZebraService zs, char *path)
303 zebra_lock_prefix (zs->global_res, path);
304 strcat(path, "zebrasrv.pid");
307 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
308 int compact_flag, Res res)
310 int page_size = 4096;
311 char resource_str[200];
312 sprintf (resource_str, "dict.%.100s.pagesize", name);
316 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
317 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
319 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
323 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
324 int rw, int useshadow, Res res,
325 const char *reg_path)
327 struct zebra_register *reg;
328 int record_compression = REC_COMPRESS_NONE;
329 const char *recordCompression = 0;
330 const char *profilePath;
332 int sort_type = ZEBRA_SORT_TYPE_FLAT;
333 ZEBRA_RES ret = ZEBRA_OK;
337 reg = xmalloc(sizeof(*reg));
340 reg->name = xstrdup (name);
347 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
348 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
350 reg->dh = data1_create();
357 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
360 data1_destroy(reg->dh);
367 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
369 bfs_destroy(reg->bfs);
370 data1_destroy(reg->dh);
377 getcwd(cwd, sizeof(cwd)-1);
378 profilePath = res_get_def(res, "profilePath", 0);
380 data1_set_tabpath (reg->dh, profilePath);
381 data1_set_tabroot (reg->dh, reg_path);
382 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
385 zebra_maps_open(res, reg_path, profilePath);
386 if (!reg->zebra_maps)
388 recTypes_destroy(reg->recTypes);
389 bfs_destroy(reg->bfs);
390 data1_destroy(reg->dh);
395 reg->rank_classes = NULL;
398 reg->keys = zebra_rec_keys_open();
400 reg->sortKeys = zebra_rec_keys_open();
411 /* installing rank classes */
412 zebraRankInstall (reg, rank_1_class);
413 zebraRankInstall (reg, rank_similarity_class);
414 zebraRankInstall (reg, rank_static_class);
416 recordCompression = res_get_def (res, "recordCompression", "none");
417 if (!strcmp (recordCompression, "none"))
418 record_compression = REC_COMPRESS_NONE;
419 if (!strcmp (recordCompression, "bzip2"))
420 record_compression = REC_COMPRESS_BZIP2;
423 const char *index_fname = res_get_def(res, "index", "default.idx");
424 if (index_fname && *index_fname)
426 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
431 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
433 yaz_log (YLOG_WARN, "rec_open failed");
438 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
440 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
442 yaz_log (YLOG_WARN, "dict_open failed");
447 if (res_get_match (res, "sortindex", "f", "f"))
448 sort_type = ZEBRA_SORT_TYPE_FLAT;
449 else if (res_get_match (res, "sortindex", "i", "f"))
450 sort_type = ZEBRA_SORT_TYPE_ISAMB;
453 yaz_log (YLOG_WARN, "bad_value for 'sortindex'");
458 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
460 yaz_log (YLOG_WARN, "zebra_sort_open failed");
463 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
465 struct ISAMS_M_s isams_m;
466 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
467 key_isams_m(res, &isams_m))))
469 yaz_log (YLOG_WARN, "isams_open failed");
473 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
475 struct ISAMC_M_s isamc_m;
476 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
477 rw, key_isamc_m(res, &isamc_m))))
479 yaz_log (YLOG_WARN, "isamc_open failed");
483 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
485 struct ISAMC_M_s isamc_m;
487 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
488 rw, key_isamc_m(res, &isamc_m), 0)))
490 yaz_log (YLOG_WARN, "isamb_open failed");
494 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
496 struct ISAMC_M_s isamc_m;
498 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
499 rw, key_isamc_m(res, &isamc_m), 1)))
501 yaz_log (YLOG_WARN, "isamb_open failed");
505 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
507 struct ISAMC_M_s isamc_m;
509 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
510 rw, key_isamc_m(res, &isamc_m), -1)))
512 yaz_log (YLOG_WARN, "isamb_open failed");
518 reg->zei = zebraExplain_open(reg->records, reg->dh,
520 zebra_extract_explain);
523 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
530 zebra_register_close(zs, reg);
533 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
537 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
539 ZEBRA_CHECK_HANDLE(zh);
540 yaz_log(log_level, "zebra_admin_shutdown");
542 zebra_mutex_cond_lock (&zh->service->session_lock);
543 zh->service->stop_flag = 1;
544 zebra_mutex_cond_unlock (&zh->service->session_lock);
548 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
551 ZEBRA_CHECK_HANDLE(zh);
552 yaz_log(log_level, "zebra_admin_start");
554 zebra_mutex_cond_lock (&zs->session_lock);
555 zebra_mutex_cond_unlock (&zs->session_lock);
559 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
563 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
567 zebraExplain_close (reg->zei);
568 dict_close (reg->dict);
570 dict_close (reg->matchDict);
571 zebra_sort_close(reg->sort_index);
573 isams_close (reg->isams);
575 isamc_close (reg->isamc);
577 isamb_close (reg->isamb);
578 rec_close (®->records);
580 recTypes_destroy (reg->recTypes);
581 zebra_maps_close (reg->zebra_maps);
582 zebraRankDestroy (reg);
583 bfs_destroy (reg->bfs);
584 data1_destroy (reg->dh);
586 zebra_rec_keys_close(reg->keys);
587 zebra_rec_keys_close(reg->sortKeys);
589 key_block_destroy(®->key_block);
594 ZEBRA_RES zebra_stop(ZebraService zs)
600 zebra_close (zs->sessions);
603 zebra_mutex_cond_destroy (&zs->session_lock);
606 passwd_db_close (zs->passwd_db);
608 recTypeClass_destroy(zs->record_classes);
609 nmem_destroy(zs->nmem);
610 res_close (zs->global_res);
612 yaz_timing_stop(zs->timing);
613 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
614 yaz_timing_get_real(zs->timing),
615 yaz_timing_get_user(zs->timing),
616 yaz_timing_get_sys(zs->timing));
619 yaz_timing_destroy(&zs->timing);
624 ZEBRA_RES zebra_close(ZebraHandle zh)
627 struct zebra_session **sp;
630 yaz_log(log_level, "zebra_close");
631 ZEBRA_CHECK_HANDLE(zh);
636 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
637 resultSetDestroy (zh, -1, 0, 0);
640 zebra_register_close(zh->service, zh->reg);
641 zebra_close_res (zh);
642 res_close(zh->session_res);
644 xfree(zh->record_encoding);
646 xfree(zh->dbaccesslist);
648 for (i = 0; i < zh->num_basenames; i++)
649 xfree(zh->basenames[i]);
650 xfree(zh->basenames);
652 if (zh->iconv_to_utf8 != 0)
653 yaz_iconv_close (zh->iconv_to_utf8);
654 if (zh->iconv_from_utf8 != 0)
655 yaz_iconv_close (zh->iconv_from_utf8);
657 zebra_mutex_cond_lock (&zs->session_lock);
658 zebra_lock_destroy (zh->lock_normal);
659 zebra_lock_destroy (zh->lock_shadow);
671 zebra_mutex_cond_unlock (&zs->session_lock);
673 xfree(zh->user_perm);
674 zh->service = 0; /* more likely to trigger an assert */
676 zebra_limit_destroy(zh->m_limit);
678 nmem_destroy(zh->nmem_error);
685 struct map_baseinfo {
691 char **new_basenames;
695 static void zebra_open_res(ZebraHandle zh)
703 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
704 zh->res = res_open(zh->session_res, 0);
705 res_read_file(zh->res, fname);
707 else if (*zh->reg_name == 0)
709 zh->res = res_open(zh->session_res, 0);
713 yaz_log (YLOG_WARN, "no register root specified");
714 zh->res = 0; /* no path for register - fail! */
718 static void zebra_close_res (ZebraHandle zh)
726 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
730 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
734 assert (zh->reg == 0);
735 assert (*zh->reg_name == 0);
741 resultSetInvalidate (zh);
742 zebra_register_close(zh->service, zh->reg);
748 zh->reg_name = xstrdup (new_reg);
752 if (zh->service->path_root)
754 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
755 strlen(zh->reg_name) + 3);
756 strcpy (zh->path_reg, zh->service->path_root);
759 strcat (zh->path_reg, "/");
760 strcat (zh->path_reg, zh->reg_name);
766 zebra_lock_destroy (zh->lock_normal);
770 zebra_lock_destroy (zh->lock_shadow);
776 const char *lock_area = res_get (zh->res, "lockDir");
778 if (!lock_area && zh->path_reg)
779 res_set (zh->res, "lockDir", zh->path_reg);
780 sprintf (fname, "norm.%s.LCK", zh->reg_name);
782 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
784 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
786 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
788 if (!zh->lock_normal || !zh->lock_shadow)
792 zebra_lock_destroy(zh->lock_normal);
797 zebra_lock_destroy(zh->lock_shadow);
806 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
807 zebra_set_approx_limit(zh, approx);
811 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
812 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
816 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
819 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
820 zh->m_segment_indexing);
825 void map_basenames_func (void *vp, const char *name, const char *value)
827 struct map_baseinfo *p = (struct map_baseinfo *) vp;
829 char fromdb[128], todb[8][128];
836 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
837 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
838 todb[5], todb[6], todb[7]);
842 for (i = 0; i<p->num_bases; i++)
843 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
846 for (i = 0; i < no; i++)
848 if (p->new_num_bases == p->new_num_max)
850 p->new_basenames[(p->new_num_bases)++] =
851 nmem_strdup (p->mem, todb[i]);
857 int zebra_select_default_database(ZebraHandle zh)
861 /* no database has been selected - so we select based on
862 resource setting (including group)
864 const char *group = res_get(zh->session_res, "group");
865 const char *v = res_get_prefix(zh->session_res,
866 "database", group, "Default");
867 return zebra_select_database(zh, v);
872 void map_basenames (ZebraHandle zh, ODR stream,
873 int *num_bases, char ***basenames)
875 struct map_baseinfo info;
876 struct map_baseinfo *p = &info;
879 yaz_log(log_level, "map_basenames ");
884 info.num_bases = *num_bases;
885 info.basenames = *basenames;
886 info.new_num_max = 128;
887 info.new_num_bases = 0;
888 info.new_basenames = (char **)
889 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
890 info.mem = stream->mem;
892 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
894 for (i = 0; i<p->num_bases; i++)
895 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
897 p->new_basenames[(p->new_num_bases)++] =
898 nmem_strdup (p->mem, p->basenames[i]);
900 *num_bases = info.new_num_bases;
901 *basenames = info.new_basenames;
902 for (i = 0; i<*num_bases; i++)
903 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
906 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
908 ZEBRA_CHECK_HANDLE(zh);
910 yaz_log(log_level, "zebra_select_database %s",basename);
912 return zebra_select_databases (zh, 1, &basename);
915 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
916 const char **basenames)
923 ZEBRA_CHECK_HANDLE(zh);
926 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
927 num_bases,basenames[0]);
932 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
936 /* Check if the user has access to all databases (Seb) */
937 /* You could argue that this should happen later, after we have
938 * determined that the database(s) exist. */
939 if (zh->dbaccesslist) {
940 for (i = 0; i < num_bases; i++) {
941 const char *db = basenames[i];
943 for (p = zh->dbaccesslist; p && *p; p = pp) {
945 if ((pp = strchr(p, '+'))) {
951 if (len == strlen(db) && !strncmp(db, p, len))
955 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
961 for (i = 0; i < zh->num_basenames; i++)
962 xfree(zh->basenames[i]);
963 xfree(zh->basenames);
965 zh->num_basenames = num_bases;
966 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
967 for (i = 0; i < zh->num_basenames; i++)
968 zh->basenames[i] = xstrdup (basenames[i]);
970 cp = strrchr(basenames[0], '/');
973 len = cp - basenames[0];
974 new_reg = xmalloc(len + 1);
975 memcpy (new_reg, basenames[0], len);
979 new_reg = xstrdup ("");
980 for (i = 1; i<num_bases; i++)
984 cp1 = strrchr (basenames[i], '/');
989 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
992 if (len != cp1 - basenames[i] ||
993 memcmp (basenames[i], new_reg, len))
995 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1003 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1008 zebra_select_register (zh, new_reg);
1012 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1015 if (!zh->lock_normal || !zh->lock_shadow)
1017 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1023 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1025 if (approx_limit == 0)
1026 approx_limit = DEFAULT_APPROX_LIMIT;
1027 zh->approx_limit = approx_limit;
1031 void zebra_set_partial_result(ZebraHandle zh)
1033 zh->partial_result = 1;
1037 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1038 int (*f)(void *client_data),
1041 zh->break_handler_func = f;
1042 zh->break_handler_data = client_data;
1046 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1047 const char *setname, zint *hits,
1048 int *estimated_hit_count,
1049 int *partial_resultset)
1053 ZEBRA_CHECK_HANDLE(zh);
1059 yaz_log(log_level, "zebra_search_rpn");
1061 zh->partial_result = 0;
1063 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1066 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1067 zh->num_basenames, zh->basenames, setname,
1068 hits, estimated_hit_count);
1070 *partial_resultset = zh->partial_result;
1075 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1076 const char *setname, zint *hits)
1078 int estimated_hit_count;
1079 int partial_resultset;
1080 return zebra_search_RPN_x(zh, o, query, setname, hits,
1081 &estimated_hit_count,
1082 &partial_resultset);
1085 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1086 const char *setname,
1087 Z_RecordComposition *comp,
1088 const Odr_oid *input_format, int num_recs,
1089 ZebraRetrievalRecord *recs)
1091 ZebraMetaRecord *poset;
1093 ZEBRA_RES ret = ZEBRA_OK;
1096 ZEBRA_CHECK_HANDLE(zh);
1102 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1106 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1111 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1114 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1115 for (i = 0; i<num_recs; i++)
1116 pos_array[i] = recs[i].position;
1117 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1120 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1121 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1127 for (i = 0; i<num_recs; i++)
1131 recs[i].errCode = 0;
1132 recs[i].format = yaz_oid_recsyn_sutrs;
1133 recs[i].len = strlen(poset[i].term);
1134 recs[i].buf = poset[i].term;
1135 recs[i].base = poset[i].db;
1137 else if (poset[i].sysno)
1141 zebra_snippets *hit_snippet = zebra_snippets_create();
1143 /* we disable hit snippets for now. It does not work well
1144 and it slows retrieval down a lot */
1146 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1150 zebra_record_fetch(zh, setname,
1151 poset[i].sysno, poset[i].score,
1152 stream, input_format, comp,
1153 &recs[i].format, &buf, &len,
1154 &recs[i].base, &recs[i].errString);
1159 recs[i].buf = (char*) odr_malloc(stream, len);
1160 memcpy(recs[i].buf, buf, len);
1164 recs[i].score = poset[i].score;
1165 recs[i].sysno = poset[i].sysno;
1166 zebra_snippets_destroy(hit_snippet);
1170 /* only need to set it once */
1171 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1173 zebra_setError_zint(zh,
1174 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1179 recs[i].buf = 0; /* no record and no error issued */
1181 recs[i].errCode = 0;
1186 zebra_meta_records_destroy(zh, poset, num_recs);
1188 zebra_end_read (zh);
1193 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1195 int *num_entries, ZebraScanEntry **entries,
1197 const char *setname)
1199 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1200 Z_AttributesPlusTerm *zapt;
1201 Odr_oid *attributeSet;
1204 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1207 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1211 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1212 position, num_entries, entries, is_partial,
1215 yaz_pqf_destroy (pqf_parser);
1219 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1220 const Odr_oid *attributeset,
1222 int *num_entries, ZebraScanEntry **entries,
1224 const char *setname)
1228 ZEBRA_CHECK_HANDLE(zh);
1233 assert(num_entries);
1236 yaz_log(log_level, "zebra_scan");
1238 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1245 res = rpn_scan(zh, stream, zapt, attributeset,
1246 zh->num_basenames, zh->basenames, position,
1247 num_entries, entries, is_partial, setname);
1252 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1253 int num_input_setnames, const char **input_setnames,
1254 const char *output_setname,
1255 Z_SortKeySpecList *sort_sequence,
1259 ZEBRA_CHECK_HANDLE(zh);
1261 assert(num_input_setnames>0);
1262 assert(input_setnames);
1263 assert(sort_sequence);
1264 assert(sort_status);
1265 yaz_log(log_level, "zebra_sort");
1267 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1269 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1270 output_setname, sort_sequence, sort_status);
1275 int zebra_deleteResultSet(ZebraHandle zh, int function,
1276 int num_setnames, char **setnames,
1281 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1283 if (zebra_begin_read(zh))
1284 return Z_DeleteStatus_systemProblemAtTarget;
1287 case Z_DeleteResultSetRequest_list:
1288 assert(num_setnames>0);
1290 resultSetDestroy (zh, num_setnames, setnames, statuses);
1292 case Z_DeleteResultSetRequest_all:
1293 resultSetDestroy (zh, -1, 0, statuses);
1296 zebra_end_read (zh);
1297 status = Z_DeleteStatus_success;
1298 for (i = 0; i<num_setnames; i++)
1299 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1300 status = statuses[i];
1304 int zebra_errCode (ZebraHandle zh)
1308 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1311 yaz_log(log_level, "zebra_errCode: o");
1315 const char *zebra_errString (ZebraHandle zh)
1319 e= diagbib1_str (zh->errCode);
1320 yaz_log(log_level, "zebra_errString: %s",e);
1324 char *zebra_errAdd (ZebraHandle zh)
1329 yaz_log(log_level, "zebra_errAdd: %s",a);
1333 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1336 const char *astring;
1340 ZEBRA_CHECK_HANDLE(zh);
1344 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1345 p = res_get(zs->global_res, u);
1346 xfree(zh->user_perm);
1347 zh->user_perm = xstrdup(p ? p : "r");
1349 /* Determine database access list */
1350 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1352 zh->dbaccesslist = xstrdup(astring);
1354 zh->dbaccesslist = 0;
1356 /* users that don't require a password .. */
1357 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1360 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1365 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1366 const char *record_type)
1368 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1369 database, record_type);
1370 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1372 return zebra_begin_trans(zh, 1);
1375 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1377 ZEBRA_CHECK_HANDLE(zh);
1378 yaz_log(log_level, "zebra_admin_import_end");
1379 return zebra_end_trans(zh);
1382 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1384 ZEBRA_RES res = ZEBRA_OK;
1387 ZEBRA_CHECK_HANDLE(zh);
1388 yaz_log(log_level, "zebra_admin_import_segment");
1390 for (i = 0; i<segment->num_segmentRecords; i++)
1392 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1394 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1396 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1397 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1399 Odr_oct *oct = fragment->u.notExternallyTagged;
1402 if (zebra_update_record(
1405 0, /* record Type */
1409 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1417 int delete_w_handle(const char *info, void *handle)
1419 ZebraHandle zh = (ZebraHandle) handle;
1423 if (*info == sizeof(pos))
1425 memcpy (&pos, info+1, sizeof(pos));
1426 isamb_unlink(zh->reg->isamb, pos);
1431 static int delete_SU_handle(void *handle, int ord)
1433 ZebraHandle zh = (ZebraHandle) handle;
1437 ord_len = key_SU_encode (ord, ord_buf);
1438 ord_buf[ord_len] = '\0';
1440 assert (zh->reg->isamb);
1441 dict_delete_subtree(zh->reg->dict, ord_buf,
1442 zh, delete_w_handle);
1446 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1448 ZEBRA_RES ret = ZEBRA_OK;
1450 yaz_log(log_level, "zebra_drop_database %s", db);
1451 ZEBRA_CHECK_HANDLE(zh);
1453 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1455 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1460 if (zebraExplain_curDatabase (zh->reg->zei, db))
1462 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1467 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1468 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1469 0 /* handle */, 0 /* func */);
1470 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1471 zebraExplain_removeDatabase(zh->reg->zei, zh);
1472 zebra_remove_file_match(zh);
1477 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1478 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1479 "drop database only supported for isam:b");
1482 if (zebra_end_trans (zh) != ZEBRA_OK)
1484 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1490 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1492 yaz_log(log_level, "zebra_create_database %s", db);
1493 ZEBRA_CHECK_HANDLE(zh);
1496 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1498 if (zebra_begin_trans (zh, 1))
1501 /* announce database */
1502 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1503 /* explainDatabase */))
1505 if (zebra_end_trans (zh) != ZEBRA_OK)
1507 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1509 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1512 return zebra_end_trans (zh);
1515 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1516 const char *input_str, int input_len,
1517 char *output_str, int output_len)
1520 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1524 yaz_log(log_level, "zebra_string_norm ");
1526 if (!zh->reg->zebra_maps)
1528 wrbuf = zebra_replace(zm, "", input_str, input_len);
1531 if (wrbuf_len(wrbuf) >= output_len)
1533 if (wrbuf_len(wrbuf))
1534 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1535 output_str[wrbuf_len(wrbuf)] = '\0';
1536 return wrbuf_len(wrbuf);
1539 /** \brief set register state (state*.LCK)
1540 \param zh Zebra handle
1542 \param seqno sequence number
1545 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1547 c=commit (writing to register, reading from shadow, shadow mode only)
1549 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1551 char state_fname[256];
1556 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1558 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1559 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1560 f = fopen (fname, "w");
1562 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1563 fprintf (f, "%c %d %ld\n", val, seqno, p);
1568 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1570 char state_fname[256];
1575 yaz_log(log_level, "zebra_get_state ");
1577 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1578 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1579 f = fopen (fname, "r");
1585 fscanf (f, "%c %d", val, seqno);
1591 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1593 return zebra_begin_trans(zh, 0);
1596 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1598 return zebra_end_trans(zh);
1601 static void read_res_for_transaction(ZebraHandle zh)
1603 const char *group = res_get(zh->res, "group");
1605 /* FIXME - do we still use groups ?? */
1607 zh->m_group = group;
1608 v = res_get_prefix(zh->res, "followLinks", group, "1");
1609 zh->m_follow_links = atoi(v);
1611 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1612 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1614 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1615 zh->m_store_keys = atoi(v);
1617 v = res_get_prefix(zh->res, "storeData", group, "1");
1618 zh->m_store_data = atoi(v);
1620 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1621 zh->m_explain_database = atoi(v);
1623 v = res_get_prefix(zh->res, "openRW", group, "1");
1624 zh->m_flag_rw = atoi(v);
1626 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1627 zh->m_file_verbose_limit = atoi(v);
1630 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1632 ZEBRA_CHECK_HANDLE(zh);
1633 zebra_select_default_database(zh);
1636 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1637 "zebra_begin_trans: no database selected");
1641 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1645 if (rw && !strchr(zh->user_perm, 'w'))
1649 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1660 const char *rval = 0;
1665 read_res_for_transaction(zh);
1668 if (zh->trans_no != 1)
1670 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1671 "zebra_begin_trans: no write trans within read");
1676 resultSetInvalidate (zh);
1677 zebra_register_close(zh->service, zh->reg);
1679 zh->trans_w_no = zh->trans_no;
1681 zh->records_inserted = 0;
1682 zh->records_updated = 0;
1683 zh->records_deleted = 0;
1684 zh->records_processed = 0;
1685 zh->records_skipped = 0;
1687 #if HAVE_SYS_TIMES_H
1691 if (zh->shadow_enable)
1692 rval = res_get (zh->res, "shadow");
1696 zebra_lock_r(zh->lock_normal);
1697 zebra_lock_w(zh->lock_shadow);
1701 zebra_lock_w(zh->lock_normal);
1702 zebra_lock_w(zh->lock_shadow);
1704 zebra_get_state (zh, &val, &seqno);
1707 /* either we didn't finish commit or shadow is dirty */
1710 yaz_log(YLOG_WARN, "previous transaction did not finish "
1711 "(shadow disabled)");
1713 zebra_unlock (zh->lock_shadow);
1714 zebra_unlock (zh->lock_normal);
1715 if (zebra_commit (zh))
1723 zebra_lock_r(zh->lock_normal);
1724 zebra_lock_w(zh->lock_shadow);
1728 zebra_lock_w(zh->lock_normal);
1729 zebra_lock_w(zh->lock_shadow);
1733 zebra_set_state (zh, 'd', seqno);
1735 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1736 1, rval ? 1 : 0, zh->res,
1739 zh->reg->seqno = seqno;
1742 zebra_set_state (zh, 'o', seqno);
1744 zebra_unlock (zh->lock_shadow);
1745 zebra_unlock (zh->lock_normal);
1750 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1751 "zebra_begin_trans: cannot open register");
1752 yaz_log(YLOG_FATAL, "%s", zh->errString);
1755 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1765 if (zh->trans_no != 1)
1767 return zebra_flush_reg (zh);
1769 #if HAVE_SYS_TIMES_H
1775 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1778 if (!zh->lock_normal || !zh->lock_shadow)
1781 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1784 zebra_get_state (zh, &val, &seqno);
1790 else if (seqno != zh->reg->seqno)
1792 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1793 seqno, zh->reg->seqno);
1796 else if (zh->reg->last_val != val)
1798 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1799 val, zh->reg->last_val);
1806 zebra_lock_r (zh->lock_shadow);
1808 zebra_lock_r (zh->lock_normal);
1812 resultSetInvalidate (zh);
1813 zebra_register_close(zh->service, zh->reg);
1815 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1816 0, val == 'c' ? 1 : 0,
1817 zh->res, zh->path_reg);
1820 zebra_unlock (zh->lock_normal);
1821 zebra_unlock (zh->lock_shadow);
1823 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1826 zh->reg->last_val = val;
1827 zh->reg->seqno = seqno;
1829 read_res_for_transaction(zh);
1833 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1835 ZebraTransactionStatus dummy;
1837 yaz_log(log_level, "zebra_end_trans");
1838 ZEBRA_CHECK_HANDLE(zh);
1839 return zebra_end_transaction(zh, &dummy);
1842 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1848 ZEBRA_CHECK_HANDLE(zh);
1851 yaz_log(log_level, "zebra_end_transaction");
1853 status->processed = 0;
1854 status->inserted = 0;
1855 status->updated = 0;
1856 status->deleted = 0;
1860 if (!zh->res || !zh->reg)
1862 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1863 "zebra_end_trans: no open transaction");
1866 if (zh->trans_no != zh->trans_w_no)
1869 if (zh->trans_no != 0)
1872 /* release read lock */
1874 zebra_unlock (zh->lock_normal);
1875 zebra_unlock (zh->lock_shadow);
1878 { /* release write lock */
1882 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1883 rval = res_get (zh->res, "shadow");
1885 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1887 zebra_flush_reg (zh);
1889 resultSetInvalidate (zh);
1891 zebra_register_close(zh->service, zh->reg);
1894 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1895 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1896 zh->records_processed, zh->records_inserted,
1897 zh->records_updated, zh->records_deleted);
1899 status->processed = zh->records_processed;
1900 status->inserted = zh->records_inserted;
1901 status->updated = zh->records_updated;
1902 status->deleted = zh->records_deleted;
1904 zebra_get_state (zh, &val, &seqno);
1907 BFiles bfs = bfs_create (rval, zh->path_reg);
1908 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1909 bf_commitClean (bfs, rval);
1914 zebra_set_state (zh, 'o', seqno);
1916 zebra_unlock (zh->lock_shadow);
1917 zebra_unlock (zh->lock_normal);
1920 #if HAVE_SYS_TIMES_H
1922 yaz_log (log_level, "user/system: %ld/%ld",
1923 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1924 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1926 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1927 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1932 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1936 yaz_log (log_level, "updating %s", path);
1938 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1939 return zebra_update_file_match(zh, path);
1941 return zebra_update_from_path(zh, path);
1944 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1948 yaz_log (log_level, "deleting %s", path);
1949 return zebra_delete_from_path(zh, path);
1952 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1956 yaz_log(log_level, "zebra_repository_show");
1957 repositoryShow (zh, path);
1961 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1967 ZEBRA_RES res = ZEBRA_OK;
1971 zebra_select_default_database(zh);
1974 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1977 rval = res_get(zh->res, "shadow");
1980 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1984 zebra_lock_w(zh->lock_normal);
1985 zebra_lock_r(zh->lock_shadow);
1987 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
1990 zebra_unlock(zh->lock_shadow);
1991 zebra_unlock(zh->lock_normal);
1994 zebra_get_state(zh, &val, &seqno);
1998 /* shadow area is dirty and so we must throw it away */
1999 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2004 bf_cache (bfs, rval);
2005 if (bf_commitExists (bfs))
2008 zebra_set_state(zh, 'd', seqno);
2011 zebra_set_state(zh, 'c', seqno);
2013 yaz_log(YLOG_DEBUG, "commit start");
2014 if (bf_commitExec (bfs))
2017 if (res == ZEBRA_OK)
2020 zebra_set_state(zh, 'o', seqno);
2022 zebra_unlock(zh->lock_shadow);
2023 zebra_unlock(zh->lock_normal);
2025 zebra_lock_w(zh->lock_shadow);
2026 bf_commitClean(bfs, rval);
2027 zebra_unlock(zh->lock_shadow);
2031 zebra_unlock(zh->lock_shadow);
2032 zebra_unlock(zh->lock_normal);
2033 yaz_log(YLOG_WARN, "zebra_commit: failed");
2038 zebra_unlock(zh->lock_shadow);
2039 zebra_unlock(zh->lock_normal);
2040 yaz_log(log_level, "nothing to commit");
2047 ZEBRA_RES zebra_clean(ZebraHandle zh)
2049 yaz_log(log_level, "zebra_clean");
2050 ZEBRA_CHECK_HANDLE(zh);
2051 return zebra_commit_ex(zh, 1);
2054 ZEBRA_RES zebra_commit(ZebraHandle zh)
2056 yaz_log(log_level, "zebra_commit");
2057 ZEBRA_CHECK_HANDLE(zh);
2058 return zebra_commit_ex(zh, 0);
2062 ZEBRA_RES zebra_init(ZebraHandle zh)
2067 yaz_log(log_level, "zebra_init");
2069 ZEBRA_CHECK_HANDLE(zh);
2071 zebra_select_default_database(zh);
2074 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2075 "cannot select default database");
2078 rval = res_get (zh->res, "shadow");
2080 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2083 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2087 bf_cache (bfs, rval);
2091 zebra_set_state (zh, 'o', 0);
2095 ZEBRA_RES zebra_compact(ZebraHandle zh)
2099 yaz_log(log_level, "zebra_compact");
2100 ZEBRA_CHECK_HANDLE(zh);
2103 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2106 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2112 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2114 yaz_log(log_level, "zebra_result");
2117 *code = zh->errCode;
2118 *addinfo = zh->errString;
2122 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2123 *addinfo ="ZebraHandle is NULL";
2127 void zebra_shadow_enable(ZebraHandle zh, int value)
2130 yaz_log(log_level, "zebra_shadow_enable");
2131 zh->shadow_enable = value;
2134 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2136 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2137 ZEBRA_CHECK_HANDLE(zh);
2140 if (zh->iconv_to_utf8 != 0)
2141 yaz_iconv_close(zh->iconv_to_utf8);
2142 if (zh->iconv_from_utf8 != 0)
2143 yaz_iconv_close(zh->iconv_from_utf8);
2146 yaz_iconv_open ("UTF-8", encoding);
2147 if (zh->iconv_to_utf8 == 0)
2148 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2149 zh->iconv_from_utf8 =
2150 yaz_iconv_open (encoding, "UTF-8");
2151 if (zh->iconv_to_utf8 == 0)
2152 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2157 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2159 yaz_log(log_level, "zebra_record_encoding");
2160 ZEBRA_CHECK_HANDLE(zh);
2161 xfree(zh->record_encoding);
2162 zh->record_encoding = 0;
2164 zh->record_encoding = xstrdup (encoding);
2168 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2172 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2174 res_set(zh->res, name, value);
2177 const char *zebra_get_resource(ZebraHandle zh,
2178 const char *name, const char *defaultvalue)
2183 v = res_get_def (zh->res, name, (char *)defaultvalue);
2184 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2188 /* moved from zebra_api_ext.c by pop */
2189 /* FIXME: Should this really be public??? -Heikki */
2191 int zebra_trans_no (ZebraHandle zh)
2193 yaz_log(log_level, "zebra_trans_no");
2195 return zh->trans_no;
2198 int zebra_get_shadow_enable (ZebraHandle zh)
2200 yaz_log(log_level, "zebra_get_shadow_enable");
2202 return zh->shadow_enable;
2205 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2207 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2209 zh->shadow_enable = value;
2212 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2213 const char *buf, int buf_size)
2215 return zebra_update_record(zh, action_update,
2216 0 /* record type */,
2223 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2224 enum zebra_recctrl_action_t action,
2225 const char *recordType,
2226 zint *sysno, const char *match,
2228 const char *buf, int buf_size)
2232 ZEBRA_CHECK_HANDLE(zh);
2236 yaz_log(log_level, "zebra_update_record");
2238 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2241 buf_size = strlen(buf);
2243 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2245 res = zebra_buffer_extract_record(zh, buf, buf_size,
2252 if (zebra_end_trans(zh) != ZEBRA_OK)
2254 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2260 /* ---------------------------------------------------------------------------
2264 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2265 const char *setname, zint *hits)
2268 ZEBRA_RES res = ZEBRA_OK;
2273 ZEBRA_CHECK_HANDLE(zh);
2275 odr = odr_createmem(ODR_ENCODE);
2280 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2282 query = p_query_rpn(odr, pqf_query);
2286 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2287 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2291 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2295 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2303 /* ---------------------------------------------------------------------------
2304 Sort - a simplified interface, with optional read locks.
2306 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2307 const char *sort_spec,
2308 const char *output_setname,
2309 const char **input_setnames)
2311 int num_input_setnames = 0;
2312 int sort_status = 0;
2313 Z_SortKeySpecList *sort_sequence;
2315 ZEBRA_CHECK_HANDLE(zh);
2318 assert(output_setname);
2319 assert(input_setnames);
2320 sort_sequence = yaz_sort_spec (stream, sort_spec);
2321 yaz_log(log_level, "sort (FIXME) ");
2324 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2325 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2329 /* we can do this, since the perl typemap code for char** will
2330 put a NULL at the end of list */
2331 while (input_setnames[num_input_setnames]) num_input_setnames++;
2333 if (zebra_begin_read (zh))
2336 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2337 output_setname, sort_sequence, &sort_status);
2343 /* ---------------------------------------------------------------------------
2344 Get BFS for Zebra system (to make alternative storage methods)
2346 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2349 return zh->reg->bfs;
2354 /* ---------------------------------------------------------------------------
2355 Set limit for search/scan
2357 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2359 ZEBRA_CHECK_HANDLE(zh);
2360 zebra_limit_destroy(zh->m_limit);
2361 zh->m_limit = zebra_limit_create(complement_flag, ids);
2366 Set Error code + addinfo
2368 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2373 nmem_reset(zh->nmem_error);
2374 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2377 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2380 sprintf(vstr, ZINT_FORMAT, i);
2383 nmem_reset(zh->nmem_error);
2384 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2387 void zebra_lock_prefix(Res res, char *path)
2389 const char *lock_dir = res_get_def (res, "lockDir", "");
2391 strcpy(path, lock_dir);
2392 if (*path && path[strlen(path)-1] != '/')
2399 * indent-tabs-mode: nil
2401 * vim: shiftwidth=4 tabstop=8 expandtab