1 /* $Id: zebraapi.c,v 1.262 2007-10-31 16:56:14 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);
384 reg->index_types = 0;
386 zebra_maps_open(res, reg_path, profilePath);
387 if (!reg->zebra_maps)
389 recTypes_destroy(reg->recTypes);
390 bfs_destroy(reg->bfs);
391 data1_destroy(reg->dh);
396 reg->rank_classes = NULL;
399 reg->keys = zebra_rec_keys_open();
401 reg->sortKeys = zebra_rec_keys_open();
412 /* installing rank classes */
413 zebraRankInstall (reg, rank_1_class);
414 zebraRankInstall (reg, rank_similarity_class);
415 zebraRankInstall (reg, rank_static_class);
417 recordCompression = res_get_def (res, "recordCompression", "none");
418 if (!strcmp (recordCompression, "none"))
419 record_compression = REC_COMPRESS_NONE;
420 if (!strcmp (recordCompression, "bzip2"))
421 record_compression = REC_COMPRESS_BZIP2;
424 const char *index_types_fname = res_get(res, "indextypes");
425 if (index_types_fname)
427 char tmp_full_name[1024];
429 if (!yaz_filepath_resolve(index_types_fname,
434 yaz_log(YLOG_WARN, "Could not find %s", index_types_fname);
439 reg->index_types = zebra_index_types_create(
441 yaz_log(YLOG_LOG, "zebra_index_types_create returned %p",
448 const char *index_fname = res_get_def(res, "index", "default.idx");
449 if (index_fname && *index_fname)
451 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
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;
478 yaz_log (YLOG_WARN, "bad_value for 'sortindex'");
483 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
485 yaz_log (YLOG_WARN, "zebra_sort_open failed");
488 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
490 struct ISAMS_M_s isams_m;
491 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
492 key_isams_m(res, &isams_m))))
494 yaz_log (YLOG_WARN, "isams_open failed");
498 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
500 struct ISAMC_M_s isamc_m;
501 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
502 rw, key_isamc_m(res, &isamc_m))))
504 yaz_log (YLOG_WARN, "isamc_open failed");
508 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
510 struct ISAMC_M_s isamc_m;
512 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
513 rw, key_isamc_m(res, &isamc_m), 0)))
515 yaz_log (YLOG_WARN, "isamb_open failed");
519 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
521 struct ISAMC_M_s isamc_m;
523 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
524 rw, key_isamc_m(res, &isamc_m), 1)))
526 yaz_log (YLOG_WARN, "isamb_open failed");
530 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
532 struct ISAMC_M_s isamc_m;
534 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
535 rw, key_isamc_m(res, &isamc_m), -1)))
537 yaz_log (YLOG_WARN, "isamb_open failed");
543 reg->zei = zebraExplain_open(reg->records, reg->dh,
545 zebra_extract_explain);
548 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
555 zebra_register_close(zs, reg);
558 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
562 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
564 ZEBRA_CHECK_HANDLE(zh);
565 yaz_log(log_level, "zebra_admin_shutdown");
567 zebra_mutex_cond_lock (&zh->service->session_lock);
568 zh->service->stop_flag = 1;
569 zebra_mutex_cond_unlock (&zh->service->session_lock);
573 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
576 ZEBRA_CHECK_HANDLE(zh);
577 yaz_log(log_level, "zebra_admin_start");
579 zebra_mutex_cond_lock (&zs->session_lock);
580 zebra_mutex_cond_unlock (&zs->session_lock);
584 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
588 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
592 zebraExplain_close (reg->zei);
593 dict_close (reg->dict);
595 dict_close (reg->matchDict);
596 zebra_sort_close(reg->sort_index);
598 isams_close (reg->isams);
600 isamc_close (reg->isamc);
602 isamb_close (reg->isamb);
603 rec_close (®->records);
605 recTypes_destroy (reg->recTypes);
606 zebra_maps_close (reg->zebra_maps);
607 zebra_index_types_destroy(reg->index_types);
608 zebraRankDestroy (reg);
609 bfs_destroy (reg->bfs);
610 data1_destroy (reg->dh);
612 zebra_rec_keys_close(reg->keys);
613 zebra_rec_keys_close(reg->sortKeys);
615 key_block_destroy(®->key_block);
620 ZEBRA_RES zebra_stop(ZebraService zs)
626 zebra_close (zs->sessions);
629 zebra_mutex_cond_destroy (&zs->session_lock);
632 passwd_db_close (zs->passwd_db);
634 recTypeClass_destroy(zs->record_classes);
635 nmem_destroy(zs->nmem);
636 res_close (zs->global_res);
638 yaz_timing_stop(zs->timing);
639 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
640 yaz_timing_get_real(zs->timing),
641 yaz_timing_get_user(zs->timing),
642 yaz_timing_get_sys(zs->timing));
645 yaz_timing_destroy(&zs->timing);
650 ZEBRA_RES zebra_close(ZebraHandle zh)
653 struct zebra_session **sp;
656 yaz_log(log_level, "zebra_close");
657 ZEBRA_CHECK_HANDLE(zh);
662 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
663 resultSetDestroy (zh, -1, 0, 0);
666 zebra_register_close(zh->service, zh->reg);
667 zebra_close_res (zh);
668 res_close(zh->session_res);
670 xfree(zh->record_encoding);
672 xfree(zh->dbaccesslist);
674 for (i = 0; i < zh->num_basenames; i++)
675 xfree(zh->basenames[i]);
676 xfree(zh->basenames);
678 if (zh->iconv_to_utf8 != 0)
679 yaz_iconv_close (zh->iconv_to_utf8);
680 if (zh->iconv_from_utf8 != 0)
681 yaz_iconv_close (zh->iconv_from_utf8);
683 zebra_mutex_cond_lock (&zs->session_lock);
684 zebra_lock_destroy (zh->lock_normal);
685 zebra_lock_destroy (zh->lock_shadow);
697 zebra_mutex_cond_unlock (&zs->session_lock);
699 xfree(zh->user_perm);
700 zh->service = 0; /* more likely to trigger an assert */
702 zebra_limit_destroy(zh->m_limit);
704 nmem_destroy(zh->nmem_error);
711 struct map_baseinfo {
717 char **new_basenames;
721 static void zebra_open_res(ZebraHandle zh)
729 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
730 zh->res = res_open(zh->session_res, 0);
731 res_read_file(zh->res, fname);
733 else if (*zh->reg_name == 0)
735 zh->res = res_open(zh->session_res, 0);
739 yaz_log (YLOG_WARN, "no register root specified");
740 zh->res = 0; /* no path for register - fail! */
744 static void zebra_close_res (ZebraHandle zh)
752 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
756 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
760 assert (zh->reg == 0);
761 assert (*zh->reg_name == 0);
767 resultSetInvalidate (zh);
768 zebra_register_close(zh->service, zh->reg);
774 zh->reg_name = xstrdup (new_reg);
778 if (zh->service->path_root)
780 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
781 strlen(zh->reg_name) + 3);
782 strcpy (zh->path_reg, zh->service->path_root);
785 strcat (zh->path_reg, "/");
786 strcat (zh->path_reg, zh->reg_name);
792 zebra_lock_destroy (zh->lock_normal);
796 zebra_lock_destroy (zh->lock_shadow);
802 const char *lock_area = res_get (zh->res, "lockDir");
804 if (!lock_area && zh->path_reg)
805 res_set (zh->res, "lockDir", zh->path_reg);
806 sprintf (fname, "norm.%s.LCK", zh->reg_name);
808 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
810 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
812 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
814 if (!zh->lock_normal || !zh->lock_shadow)
818 zebra_lock_destroy(zh->lock_normal);
823 zebra_lock_destroy(zh->lock_shadow);
832 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
833 zebra_set_approx_limit(zh, approx);
837 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
838 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
842 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
845 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
846 zh->m_segment_indexing);
851 void map_basenames_func (void *vp, const char *name, const char *value)
853 struct map_baseinfo *p = (struct map_baseinfo *) vp;
855 char fromdb[128], todb[8][128];
862 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
863 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
864 todb[5], todb[6], todb[7]);
868 for (i = 0; i<p->num_bases; i++)
869 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
872 for (i = 0; i < no; i++)
874 if (p->new_num_bases == p->new_num_max)
876 p->new_basenames[(p->new_num_bases)++] =
877 nmem_strdup (p->mem, todb[i]);
883 int zebra_select_default_database(ZebraHandle zh)
887 /* no database has been selected - so we select based on
888 resource setting (including group)
890 const char *group = res_get(zh->session_res, "group");
891 const char *v = res_get_prefix(zh->session_res,
892 "database", group, "Default");
893 return zebra_select_database(zh, v);
898 void map_basenames (ZebraHandle zh, ODR stream,
899 int *num_bases, char ***basenames)
901 struct map_baseinfo info;
902 struct map_baseinfo *p = &info;
905 yaz_log(log_level, "map_basenames ");
910 info.num_bases = *num_bases;
911 info.basenames = *basenames;
912 info.new_num_max = 128;
913 info.new_num_bases = 0;
914 info.new_basenames = (char **)
915 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
916 info.mem = stream->mem;
918 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
920 for (i = 0; i<p->num_bases; i++)
921 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
923 p->new_basenames[(p->new_num_bases)++] =
924 nmem_strdup (p->mem, p->basenames[i]);
926 *num_bases = info.new_num_bases;
927 *basenames = info.new_basenames;
928 for (i = 0; i<*num_bases; i++)
929 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
932 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
934 ZEBRA_CHECK_HANDLE(zh);
936 yaz_log(log_level, "zebra_select_database %s",basename);
938 return zebra_select_databases (zh, 1, &basename);
941 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
942 const char **basenames)
949 ZEBRA_CHECK_HANDLE(zh);
952 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
953 num_bases,basenames[0]);
958 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
962 /* Check if the user has access to all databases (Seb) */
963 /* You could argue that this should happen later, after we have
964 * determined that the database(s) exist. */
965 if (zh->dbaccesslist) {
966 for (i = 0; i < num_bases; i++) {
967 const char *db = basenames[i];
969 for (p = zh->dbaccesslist; p && *p; p = pp) {
971 if ((pp = strchr(p, '+'))) {
977 if (len == strlen(db) && !strncmp(db, p, len))
981 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
987 for (i = 0; i < zh->num_basenames; i++)
988 xfree(zh->basenames[i]);
989 xfree(zh->basenames);
991 zh->num_basenames = num_bases;
992 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
993 for (i = 0; i < zh->num_basenames; i++)
994 zh->basenames[i] = xstrdup (basenames[i]);
996 cp = strrchr(basenames[0], '/');
999 len = cp - basenames[0];
1000 new_reg = xmalloc(len + 1);
1001 memcpy (new_reg, basenames[0], len);
1002 new_reg[len] = '\0';
1005 new_reg = xstrdup ("");
1006 for (i = 1; i<num_bases; i++)
1010 cp1 = strrchr (basenames[i], '/');
1015 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1018 if (len != cp1 - basenames[i] ||
1019 memcmp (basenames[i], new_reg, len))
1021 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1029 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1034 zebra_select_register (zh, new_reg);
1038 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1041 if (!zh->lock_normal || !zh->lock_shadow)
1043 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1049 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1051 if (approx_limit == 0)
1052 approx_limit = DEFAULT_APPROX_LIMIT;
1053 zh->approx_limit = approx_limit;
1057 void zebra_set_partial_result(ZebraHandle zh)
1059 zh->partial_result = 1;
1063 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1064 int (*f)(void *client_data),
1067 zh->break_handler_func = f;
1068 zh->break_handler_data = client_data;
1072 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1073 const char *setname, zint *hits,
1074 int *estimated_hit_count,
1075 int *partial_resultset)
1079 ZEBRA_CHECK_HANDLE(zh);
1085 yaz_log(log_level, "zebra_search_rpn");
1087 zh->partial_result = 0;
1089 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1092 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1093 zh->num_basenames, zh->basenames, setname,
1094 hits, estimated_hit_count);
1096 *partial_resultset = zh->partial_result;
1101 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1102 const char *setname, zint *hits)
1104 int estimated_hit_count;
1105 int partial_resultset;
1106 return zebra_search_RPN_x(zh, o, query, setname, hits,
1107 &estimated_hit_count,
1108 &partial_resultset);
1111 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1112 const char *setname,
1113 Z_RecordComposition *comp,
1114 const Odr_oid *input_format, int num_recs,
1115 ZebraRetrievalRecord *recs)
1117 ZebraMetaRecord *poset;
1119 ZEBRA_RES ret = ZEBRA_OK;
1122 ZEBRA_CHECK_HANDLE(zh);
1128 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1132 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1137 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1140 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1141 for (i = 0; i<num_recs; i++)
1142 pos_array[i] = recs[i].position;
1143 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1146 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1147 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1153 for (i = 0; i<num_recs; i++)
1157 recs[i].errCode = 0;
1158 recs[i].format = yaz_oid_recsyn_sutrs;
1159 recs[i].len = strlen(poset[i].term);
1160 recs[i].buf = poset[i].term;
1161 recs[i].base = poset[i].db;
1163 else if (poset[i].sysno)
1167 zebra_snippets *hit_snippet = zebra_snippets_create();
1169 /* we disable hit snippets for now. It does not work well
1170 and it slows retrieval down a lot */
1172 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1176 zebra_record_fetch(zh, setname,
1177 poset[i].sysno, poset[i].score,
1179 stream, input_format, comp,
1180 &recs[i].format, &buf, &len,
1181 &recs[i].base, &recs[i].errString);
1186 recs[i].buf = (char*) odr_malloc(stream, len);
1187 memcpy(recs[i].buf, buf, len);
1191 recs[i].score = poset[i].score;
1192 recs[i].sysno = poset[i].sysno;
1193 zebra_snippets_destroy(hit_snippet);
1197 /* only need to set it once */
1198 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1200 zebra_setError_zint(zh,
1201 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1206 recs[i].buf = 0; /* no record and no error issued */
1208 recs[i].errCode = 0;
1213 zebra_meta_records_destroy(zh, poset, num_recs);
1215 zebra_end_read (zh);
1220 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1222 int *num_entries, ZebraScanEntry **entries,
1224 const char *setname)
1226 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1227 Z_AttributesPlusTerm *zapt;
1228 Odr_oid *attributeSet;
1231 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1234 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1238 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1239 position, num_entries, entries, is_partial,
1242 yaz_pqf_destroy (pqf_parser);
1246 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1247 const Odr_oid *attributeset,
1249 int *num_entries, ZebraScanEntry **entries,
1251 const char *setname)
1254 RSET limit_rset = 0;
1256 ZEBRA_CHECK_HANDLE(zh);
1261 assert(num_entries);
1264 yaz_log(log_level, "zebra_scan");
1266 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1274 limit_rset = resultSetRef(zh, setname);
1278 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1280 zebra_end_read (zh);
1284 res = rpn_scan(zh, stream, zapt, attributeset,
1285 zh->num_basenames, zh->basenames, position,
1286 num_entries, entries, is_partial, limit_rset);
1291 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1292 int num_input_setnames, const char **input_setnames,
1293 const char *output_setname,
1294 Z_SortKeySpecList *sort_sequence,
1298 ZEBRA_CHECK_HANDLE(zh);
1300 assert(num_input_setnames>0);
1301 assert(input_setnames);
1302 assert(sort_sequence);
1303 assert(sort_status);
1304 yaz_log(log_level, "zebra_sort");
1306 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1308 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1309 output_setname, sort_sequence, sort_status);
1314 int zebra_deleteResultSet(ZebraHandle zh, int function,
1315 int num_setnames, char **setnames,
1320 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1322 if (zebra_begin_read(zh))
1323 return Z_DeleteStatus_systemProblemAtTarget;
1326 case Z_DeleteResultSetRequest_list:
1327 assert(num_setnames>0);
1329 resultSetDestroy (zh, num_setnames, setnames, statuses);
1331 case Z_DeleteResultSetRequest_all:
1332 resultSetDestroy (zh, -1, 0, statuses);
1335 zebra_end_read (zh);
1336 status = Z_DeleteStatus_success;
1337 for (i = 0; i<num_setnames; i++)
1338 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1339 status = statuses[i];
1343 int zebra_errCode (ZebraHandle zh)
1347 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1350 yaz_log(log_level, "zebra_errCode: o");
1354 const char *zebra_errString (ZebraHandle zh)
1358 e= diagbib1_str (zh->errCode);
1359 yaz_log(log_level, "zebra_errString: %s",e);
1363 char *zebra_errAdd (ZebraHandle zh)
1368 yaz_log(log_level, "zebra_errAdd: %s",a);
1372 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1375 const char *astring;
1379 ZEBRA_CHECK_HANDLE(zh);
1383 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1384 p = res_get(zs->global_res, u);
1385 xfree(zh->user_perm);
1386 zh->user_perm = xstrdup(p ? p : "r");
1388 /* Determine database access list */
1389 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1391 zh->dbaccesslist = xstrdup(astring);
1393 zh->dbaccesslist = 0;
1395 /* users that don't require a password .. */
1396 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1399 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1404 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1405 const char *record_type)
1407 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1408 database, record_type);
1409 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1411 return zebra_begin_trans(zh, 1);
1414 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1416 ZEBRA_CHECK_HANDLE(zh);
1417 yaz_log(log_level, "zebra_admin_import_end");
1418 return zebra_end_trans(zh);
1421 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1423 ZEBRA_RES res = ZEBRA_OK;
1426 ZEBRA_CHECK_HANDLE(zh);
1427 yaz_log(log_level, "zebra_admin_import_segment");
1429 for (i = 0; i<segment->num_segmentRecords; i++)
1431 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1433 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1435 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1436 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1438 Odr_oct *oct = fragment->u.notExternallyTagged;
1441 if (zebra_update_record(
1444 0, /* record Type */
1448 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1456 int delete_w_handle(const char *info, void *handle)
1458 ZebraHandle zh = (ZebraHandle) handle;
1462 if (*info == sizeof(pos))
1464 memcpy (&pos, info+1, sizeof(pos));
1465 isamb_unlink(zh->reg->isamb, pos);
1470 static int delete_SU_handle(void *handle, int ord)
1472 ZebraHandle zh = (ZebraHandle) handle;
1476 ord_len = key_SU_encode (ord, ord_buf);
1477 ord_buf[ord_len] = '\0';
1479 assert (zh->reg->isamb);
1480 dict_delete_subtree(zh->reg->dict, ord_buf,
1481 zh, delete_w_handle);
1485 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1487 ZEBRA_RES ret = ZEBRA_OK;
1489 yaz_log(log_level, "zebra_drop_database %s", db);
1490 ZEBRA_CHECK_HANDLE(zh);
1492 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1494 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1499 if (zebraExplain_curDatabase (zh->reg->zei, db))
1501 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1506 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1507 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1508 0 /* handle */, 0 /* func */);
1509 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1510 zebraExplain_removeDatabase(zh->reg->zei, zh);
1511 zebra_remove_file_match(zh);
1516 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1517 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1518 "drop database only supported for isam:b");
1521 if (zebra_end_trans (zh) != ZEBRA_OK)
1523 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1529 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1531 yaz_log(log_level, "zebra_create_database %s", db);
1532 ZEBRA_CHECK_HANDLE(zh);
1535 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1537 if (zebra_begin_trans (zh, 1))
1540 /* announce database */
1541 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1542 /* explainDatabase */))
1544 if (zebra_end_trans (zh) != ZEBRA_OK)
1546 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1548 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1551 return zebra_end_trans (zh);
1554 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1555 const char *input_str, int input_len,
1556 char *output_str, int output_len)
1559 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1563 yaz_log(log_level, "zebra_string_norm ");
1565 if (!zh->reg->zebra_maps)
1567 wrbuf = zebra_replace(zm, "", input_str, input_len);
1570 if (wrbuf_len(wrbuf) >= output_len)
1572 if (wrbuf_len(wrbuf))
1573 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1574 output_str[wrbuf_len(wrbuf)] = '\0';
1575 return wrbuf_len(wrbuf);
1578 /** \brief set register state (state*.LCK)
1579 \param zh Zebra handle
1581 \param seqno sequence number
1584 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1586 c=commit (writing to register, reading from shadow, shadow mode only)
1588 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1590 char state_fname[256];
1595 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1597 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1598 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1599 f = fopen (fname, "w");
1601 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1602 fprintf (f, "%c %d %ld\n", val, seqno, p);
1607 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1609 char state_fname[256];
1614 yaz_log(log_level, "zebra_get_state ");
1616 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1617 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1618 f = fopen (fname, "r");
1624 fscanf (f, "%c %d", val, seqno);
1630 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1632 return zebra_begin_trans(zh, 0);
1635 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1637 return zebra_end_trans(zh);
1640 static void read_res_for_transaction(ZebraHandle zh)
1642 const char *group = res_get(zh->res, "group");
1644 /* FIXME - do we still use groups ?? */
1646 zh->m_group = group;
1647 v = res_get_prefix(zh->res, "followLinks", group, "1");
1648 zh->m_follow_links = atoi(v);
1650 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1651 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1653 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1654 zh->m_store_keys = atoi(v);
1656 v = res_get_prefix(zh->res, "storeData", group, "1");
1657 zh->m_store_data = atoi(v);
1659 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1660 zh->m_explain_database = atoi(v);
1662 v = res_get_prefix(zh->res, "openRW", group, "1");
1663 zh->m_flag_rw = atoi(v);
1665 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1666 zh->m_file_verbose_limit = atoi(v);
1669 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1671 ZEBRA_CHECK_HANDLE(zh);
1672 zebra_select_default_database(zh);
1675 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1676 "zebra_begin_trans: no database selected");
1680 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1684 if (rw && !strchr(zh->user_perm, 'w'))
1688 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1699 const char *rval = 0;
1704 read_res_for_transaction(zh);
1707 if (zh->trans_no != 1)
1709 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1710 "zebra_begin_trans: no write trans within read");
1715 resultSetInvalidate (zh);
1716 zebra_register_close(zh->service, zh->reg);
1718 zh->trans_w_no = zh->trans_no;
1720 zh->records_inserted = 0;
1721 zh->records_updated = 0;
1722 zh->records_deleted = 0;
1723 zh->records_processed = 0;
1724 zh->records_skipped = 0;
1726 #if HAVE_SYS_TIMES_H
1730 if (zh->shadow_enable)
1731 rval = res_get (zh->res, "shadow");
1735 zebra_lock_r(zh->lock_normal);
1736 zebra_lock_w(zh->lock_shadow);
1740 zebra_lock_w(zh->lock_normal);
1741 zebra_lock_w(zh->lock_shadow);
1743 zebra_get_state (zh, &val, &seqno);
1746 /* either we didn't finish commit or shadow is dirty */
1749 yaz_log(YLOG_WARN, "previous transaction did not finish "
1750 "(shadow disabled)");
1752 zebra_unlock (zh->lock_shadow);
1753 zebra_unlock (zh->lock_normal);
1754 if (zebra_commit (zh))
1762 zebra_lock_r(zh->lock_normal);
1763 zebra_lock_w(zh->lock_shadow);
1767 zebra_lock_w(zh->lock_normal);
1768 zebra_lock_w(zh->lock_shadow);
1772 zebra_set_state (zh, 'd', seqno);
1774 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1775 1, rval ? 1 : 0, zh->res,
1778 zh->reg->seqno = seqno;
1781 zebra_set_state (zh, 'o', seqno);
1783 zebra_unlock (zh->lock_shadow);
1784 zebra_unlock (zh->lock_normal);
1789 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1790 "zebra_begin_trans: cannot open register");
1791 yaz_log(YLOG_FATAL, "%s", zh->errString);
1794 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1804 if (zh->trans_no != 1)
1806 return zebra_flush_reg (zh);
1808 #if HAVE_SYS_TIMES_H
1814 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1817 if (!zh->lock_normal || !zh->lock_shadow)
1820 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1823 zebra_get_state (zh, &val, &seqno);
1829 else if (seqno != zh->reg->seqno)
1831 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1832 seqno, zh->reg->seqno);
1835 else if (zh->reg->last_val != val)
1837 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1838 val, zh->reg->last_val);
1845 zebra_lock_r (zh->lock_shadow);
1847 zebra_lock_r (zh->lock_normal);
1851 resultSetInvalidate (zh);
1852 zebra_register_close(zh->service, zh->reg);
1854 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1855 0, val == 'c' ? 1 : 0,
1856 zh->res, zh->path_reg);
1859 zebra_unlock (zh->lock_normal);
1860 zebra_unlock (zh->lock_shadow);
1862 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1865 zh->reg->last_val = val;
1866 zh->reg->seqno = seqno;
1868 read_res_for_transaction(zh);
1872 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1874 ZebraTransactionStatus dummy;
1876 yaz_log(log_level, "zebra_end_trans");
1877 ZEBRA_CHECK_HANDLE(zh);
1878 return zebra_end_transaction(zh, &dummy);
1881 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1887 ZEBRA_CHECK_HANDLE(zh);
1890 yaz_log(log_level, "zebra_end_transaction");
1892 status->processed = 0;
1893 status->inserted = 0;
1894 status->updated = 0;
1895 status->deleted = 0;
1899 if (!zh->res || !zh->reg)
1901 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1902 "zebra_end_trans: no open transaction");
1905 if (zh->trans_no != zh->trans_w_no)
1908 if (zh->trans_no != 0)
1911 /* release read lock */
1913 zebra_unlock (zh->lock_normal);
1914 zebra_unlock (zh->lock_shadow);
1917 { /* release write lock */
1921 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1922 rval = res_get (zh->res, "shadow");
1924 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1926 zebra_flush_reg (zh);
1928 resultSetInvalidate (zh);
1930 zebra_register_close(zh->service, zh->reg);
1933 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1934 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1935 zh->records_processed, zh->records_inserted,
1936 zh->records_updated, zh->records_deleted);
1938 status->processed = zh->records_processed;
1939 status->inserted = zh->records_inserted;
1940 status->updated = zh->records_updated;
1941 status->deleted = zh->records_deleted;
1943 zebra_get_state (zh, &val, &seqno);
1946 BFiles bfs = bfs_create (rval, zh->path_reg);
1947 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1948 bf_commitClean (bfs, rval);
1953 zebra_set_state (zh, 'o', seqno);
1955 zebra_unlock (zh->lock_shadow);
1956 zebra_unlock (zh->lock_normal);
1959 #if HAVE_SYS_TIMES_H
1961 yaz_log (log_level, "user/system: %ld/%ld",
1962 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1963 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1965 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1966 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1971 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1975 yaz_log (log_level, "updating %s", path);
1977 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1978 return zebra_update_file_match(zh, path);
1980 return zebra_update_from_path(zh, path);
1983 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1987 yaz_log (log_level, "deleting %s", path);
1988 return zebra_delete_from_path(zh, path);
1991 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1995 yaz_log(log_level, "zebra_repository_show");
1996 repositoryShow (zh, path);
2000 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2006 ZEBRA_RES res = ZEBRA_OK;
2010 zebra_select_default_database(zh);
2013 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2016 rval = res_get(zh->res, "shadow");
2019 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2023 zebra_lock_w(zh->lock_normal);
2024 zebra_lock_r(zh->lock_shadow);
2026 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2029 zebra_unlock(zh->lock_shadow);
2030 zebra_unlock(zh->lock_normal);
2033 zebra_get_state(zh, &val, &seqno);
2037 /* shadow area is dirty and so we must throw it away */
2038 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2043 bf_cache (bfs, rval);
2044 if (bf_commitExists (bfs))
2047 zebra_set_state(zh, 'd', seqno);
2050 zebra_set_state(zh, 'c', seqno);
2052 yaz_log(YLOG_DEBUG, "commit start");
2053 if (bf_commitExec (bfs))
2056 if (res == ZEBRA_OK)
2059 zebra_set_state(zh, 'o', seqno);
2061 zebra_unlock(zh->lock_shadow);
2062 zebra_unlock(zh->lock_normal);
2064 zebra_lock_w(zh->lock_shadow);
2065 bf_commitClean(bfs, rval);
2066 zebra_unlock(zh->lock_shadow);
2070 zebra_unlock(zh->lock_shadow);
2071 zebra_unlock(zh->lock_normal);
2072 yaz_log(YLOG_WARN, "zebra_commit: failed");
2077 zebra_unlock(zh->lock_shadow);
2078 zebra_unlock(zh->lock_normal);
2079 yaz_log(log_level, "nothing to commit");
2086 ZEBRA_RES zebra_clean(ZebraHandle zh)
2088 yaz_log(log_level, "zebra_clean");
2089 ZEBRA_CHECK_HANDLE(zh);
2090 return zebra_commit_ex(zh, 1);
2093 ZEBRA_RES zebra_commit(ZebraHandle zh)
2095 yaz_log(log_level, "zebra_commit");
2096 ZEBRA_CHECK_HANDLE(zh);
2097 return zebra_commit_ex(zh, 0);
2101 ZEBRA_RES zebra_init(ZebraHandle zh)
2106 yaz_log(log_level, "zebra_init");
2108 ZEBRA_CHECK_HANDLE(zh);
2110 zebra_select_default_database(zh);
2113 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2114 "cannot select default database");
2117 rval = res_get (zh->res, "shadow");
2119 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2122 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2126 bf_cache (bfs, rval);
2130 zebra_set_state (zh, 'o', 0);
2134 ZEBRA_RES zebra_compact(ZebraHandle zh)
2138 yaz_log(log_level, "zebra_compact");
2139 ZEBRA_CHECK_HANDLE(zh);
2142 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2145 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2151 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2153 yaz_log(log_level, "zebra_result");
2156 *code = zh->errCode;
2157 *addinfo = zh->errString;
2161 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2162 *addinfo ="ZebraHandle is NULL";
2166 void zebra_shadow_enable(ZebraHandle zh, int value)
2169 yaz_log(log_level, "zebra_shadow_enable");
2170 zh->shadow_enable = value;
2173 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2175 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2176 ZEBRA_CHECK_HANDLE(zh);
2179 if (zh->iconv_to_utf8 != 0)
2180 yaz_iconv_close(zh->iconv_to_utf8);
2181 if (zh->iconv_from_utf8 != 0)
2182 yaz_iconv_close(zh->iconv_from_utf8);
2185 yaz_iconv_open ("UTF-8", encoding);
2186 if (zh->iconv_to_utf8 == 0)
2187 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2188 zh->iconv_from_utf8 =
2189 yaz_iconv_open (encoding, "UTF-8");
2190 if (zh->iconv_to_utf8 == 0)
2191 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2196 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2198 yaz_log(log_level, "zebra_record_encoding");
2199 ZEBRA_CHECK_HANDLE(zh);
2200 xfree(zh->record_encoding);
2201 zh->record_encoding = 0;
2203 zh->record_encoding = xstrdup (encoding);
2207 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2211 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2213 res_set(zh->res, name, value);
2216 const char *zebra_get_resource(ZebraHandle zh,
2217 const char *name, const char *defaultvalue)
2222 v = res_get_def (zh->res, name, (char *)defaultvalue);
2223 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2227 /* moved from zebra_api_ext.c by pop */
2228 /* FIXME: Should this really be public??? -Heikki */
2230 int zebra_trans_no (ZebraHandle zh)
2232 yaz_log(log_level, "zebra_trans_no");
2234 return zh->trans_no;
2237 int zebra_get_shadow_enable (ZebraHandle zh)
2239 yaz_log(log_level, "zebra_get_shadow_enable");
2241 return zh->shadow_enable;
2244 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2246 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2248 zh->shadow_enable = value;
2251 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2252 const char *buf, int buf_size)
2254 return zebra_update_record(zh, action_update,
2255 0 /* record type */,
2262 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2263 enum zebra_recctrl_action_t action,
2264 const char *recordType,
2265 zint *sysno, const char *match,
2267 const char *buf, int buf_size)
2271 ZEBRA_CHECK_HANDLE(zh);
2275 yaz_log(log_level, "zebra_update_record");
2277 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2280 buf_size = strlen(buf);
2282 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2284 res = zebra_buffer_extract_record(zh, buf, buf_size,
2291 if (zebra_end_trans(zh) != ZEBRA_OK)
2293 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2299 /* ---------------------------------------------------------------------------
2303 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2304 const char *setname, zint *hits)
2307 ZEBRA_RES res = ZEBRA_OK;
2312 ZEBRA_CHECK_HANDLE(zh);
2314 odr = odr_createmem(ODR_ENCODE);
2319 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2321 query = p_query_rpn(odr, pqf_query);
2325 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2326 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2330 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2334 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2342 /* ---------------------------------------------------------------------------
2343 Sort - a simplified interface, with optional read locks.
2345 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2346 const char *sort_spec,
2347 const char *output_setname,
2348 const char **input_setnames)
2350 int num_input_setnames = 0;
2351 int sort_status = 0;
2352 Z_SortKeySpecList *sort_sequence;
2354 ZEBRA_CHECK_HANDLE(zh);
2357 assert(output_setname);
2358 assert(input_setnames);
2359 sort_sequence = yaz_sort_spec (stream, sort_spec);
2360 yaz_log(log_level, "sort (FIXME) ");
2363 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2364 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2368 /* we can do this, since the perl typemap code for char** will
2369 put a NULL at the end of list */
2370 while (input_setnames[num_input_setnames]) num_input_setnames++;
2372 if (zebra_begin_read (zh))
2375 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2376 output_setname, sort_sequence, &sort_status);
2382 /* ---------------------------------------------------------------------------
2383 Get BFS for Zebra system (to make alternative storage methods)
2385 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2388 return zh->reg->bfs;
2393 /* ---------------------------------------------------------------------------
2394 Set limit for search/scan
2396 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2398 ZEBRA_CHECK_HANDLE(zh);
2399 zebra_limit_destroy(zh->m_limit);
2400 zh->m_limit = zebra_limit_create(complement_flag, ids);
2405 Set Error code + addinfo
2407 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2412 nmem_reset(zh->nmem_error);
2413 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2416 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2419 sprintf(vstr, ZINT_FORMAT, i);
2422 nmem_reset(zh->nmem_error);
2423 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2426 void zebra_lock_prefix(Res res, char *path)
2428 const char *lock_dir = res_get_def (res, "lockDir", "");
2430 strcpy(path, lock_dir);
2431 if (*path && path[strlen(path)-1] != '/')
2438 * indent-tabs-mode: nil
2440 * vim: shiftwidth=4 tabstop=8 expandtab