1 /* $Id: zebraapi.c,v 1.245 2007-01-17 13:22:53 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>
44 #define DEFAULT_APPROX_LIMIT 2000000000
46 /* simple asserts to validate the most essential input args */
47 #define ASSERTZH assert(zh && zh->service)
48 #define ASSERTZHRES assert(zh && zh->service && zh->res)
49 #define ASSERTZS assert(zs)
51 static int log_level = 0;
52 static int log_level_initialized = 0;
54 static void zebra_open_res(ZebraHandle zh);
55 static void zebra_close_res(ZebraHandle zh);
57 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
64 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
66 static void zebra_chdir (ZebraService zs)
70 yaz_log(log_level, "zebra_chdir");
71 dir = res_get (zs->global_res, "chdir");
74 yaz_log (YLOG_DEBUG, "chdir %s", dir);
82 static ZEBRA_RES zebra_flush_reg (ZebraHandle zh)
84 ZEBRA_CHECK_HANDLE(zh);
85 yaz_log(log_level, "zebra_flush_reg");
86 zebraExplain_flush (zh->reg->zei, zh);
88 key_block_flush(zh->reg->key_block, 1);
90 zebra_index_merge(zh);
94 static struct zebra_register *zebra_register_open(ZebraService zs,
96 int rw, int useshadow,
98 const char *reg_path);
99 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
101 ZebraHandle zebra_open(ZebraService zs, Res res)
104 const char *default_encoding;
105 if (!log_level_initialized)
107 log_level = yaz_log_module_level("zebraapi");
108 log_level_initialized = 1;
111 yaz_log(log_level, "zebra_open");
116 zh = (ZebraHandle) xmalloc(sizeof(*zh));
117 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
120 zh->reg = 0; /* no register attached yet */
126 zh->session_res = res_open(zs->global_res, res);
128 zh->dbaccesslist = 0;
130 zh->reg_name = xstrdup ("");
132 zh->num_basenames = 0;
135 zh->approx_limit = DEFAULT_APPROX_LIMIT;
142 zh->shadow_enable = 1;
143 zh->m_staticrank = 0;
144 zh->m_segment_indexing = 0;
146 zh->busy_handler_func = 0;
147 zh->busy_handler_data = 0;
149 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
152 yaz_iconv_open ("UTF-8", default_encoding);
153 if (zh->iconv_to_utf8 == 0)
154 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
156 zh->iconv_from_utf8 =
157 yaz_iconv_open (default_encoding, "UTF-8");
158 if (zh->iconv_to_utf8 == 0)
159 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
162 zh->record_encoding = 0;
164 zebra_mutex_cond_lock (&zs->session_lock);
166 zh->next = zs->sessions;
169 zebra_mutex_cond_unlock (&zs->session_lock);
171 zh->store_data_buf = 0;
173 zh->m_limit = zebra_limit_create(1, 0);
175 zh->nmem_error = nmem_create();
180 ZebraService zebra_start(const char *configName)
182 return zebra_start_res(configName, 0, 0);
185 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
191 if (!log_level_initialized)
193 log_level = yaz_log_module_level("zebraapi");
194 log_level_initialized = 1;
197 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
198 configName ? configName : "");
200 if ((res = res_open(def_res, over_res)))
202 const char *passwd_plain = 0;
203 const char *passwd_encrypt = 0;
204 const char *dbaccess = 0;
209 ZEBRA_RES ret = res_read_file(res, configName);
215 if (zebra_check_res(res))
217 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
224 zebra_check_res(res);
227 zh = xmalloc(sizeof(*zh));
228 zh->global_res = res;
233 zebra_mutex_cond_init (&zh->session_lock);
234 passwd_plain = res_get (zh->global_res, "passwd");
235 passwd_encrypt = res_get (zh->global_res, "passwd.c");
236 dbaccess = res_get (zh->global_res, "dbaccess");
238 if (!passwd_plain && !passwd_encrypt)
239 zh->passwd_db = NULL;
242 zh->passwd_db = passwd_db_open();
244 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
248 passwd_db_file_plain(zh->passwd_db, passwd_plain);
250 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
257 zh->dbaccess = res_open(NULL, NULL);
258 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
259 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
264 zh->timing = yaz_timing_create();
265 zh->path_root = res_get (zh->global_res, "root");
266 zh->nmem = nmem_create();
267 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
271 const char *module_path = res_get(res, "modulePath");
273 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
281 void zebra_filter_info(ZebraService zs, void *cd,
282 void (*cb)(void *cd, const char *name))
286 recTypeClass_info(zs->record_classes, cd, cb);
289 void zebra_pidfname(ZebraService zs, char *path)
292 zebra_lock_prefix (zs->global_res, path);
293 strcat(path, "zebrasrv.pid");
296 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
297 int compact_flag, Res res)
299 int page_size = 4096;
300 char resource_str[200];
301 sprintf (resource_str, "dict.%.100s.pagesize", name);
305 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
306 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
308 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
312 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
313 int rw, int useshadow, Res res,
314 const char *reg_path)
316 struct zebra_register *reg;
317 int record_compression = REC_COMPRESS_NONE;
318 const char *recordCompression = 0;
319 const char *profilePath;
321 int sort_type = ZEBRA_SORT_TYPE_FLAT;
322 ZEBRA_RES ret = ZEBRA_OK;
326 reg = xmalloc(sizeof(*reg));
329 reg->name = xstrdup (name);
336 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
337 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
339 reg->dh = data1_create();
346 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
349 data1_destroy(reg->dh);
356 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
358 bfs_destroy(reg->bfs);
359 data1_destroy(reg->dh);
366 getcwd(cwd, sizeof(cwd)-1);
367 profilePath = res_get_def(res, "profilePath", 0);
369 data1_set_tabpath (reg->dh, profilePath);
370 data1_set_tabroot (reg->dh, reg_path);
371 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
374 zebra_maps_open(res, reg_path, profilePath);
375 if (!reg->zebra_maps)
377 recTypes_destroy(reg->recTypes);
378 bfs_destroy(reg->bfs);
379 data1_destroy(reg->dh);
384 reg->rank_classes = NULL;
387 reg->keys = zebra_rec_keys_open();
389 reg->sortKeys = zebra_rec_keys_open();
400 /* installing rank classes */
401 zebraRankInstall (reg, rank_1_class);
402 zebraRankInstall (reg, rank_similarity_class);
403 zebraRankInstall (reg, rank_static_class);
405 recordCompression = res_get_def (res, "recordCompression", "none");
406 if (!strcmp (recordCompression, "none"))
407 record_compression = REC_COMPRESS_NONE;
408 if (!strcmp (recordCompression, "bzip2"))
409 record_compression = REC_COMPRESS_BZIP2;
413 const char *index_fname = res_get_def(res, "index", "default.idx");
414 if (index_fname && *index_fname)
416 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
421 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
423 yaz_log (YLOG_WARN, "rec_open failed");
428 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
430 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
432 yaz_log (YLOG_WARN, "dict_open failed");
437 if (res_get_match (res, "sortindex", "f", "f"))
438 sort_type = ZEBRA_SORT_TYPE_FLAT;
439 else if (res_get_match (res, "sortindex", "i", "f"))
440 sort_type = ZEBRA_SORT_TYPE_ISAMB;
443 yaz_log (YLOG_WARN, "bad_value for 'sortindex'");
448 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
450 yaz_log (YLOG_WARN, "zebra_sort_open failed");
453 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
455 struct ISAMS_M_s isams_m;
456 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
457 key_isams_m(res, &isams_m))))
459 yaz_log (YLOG_WARN, "isams_open failed");
463 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
465 struct ISAMC_M_s isamc_m;
466 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
467 rw, key_isamc_m(res, &isamc_m))))
469 yaz_log (YLOG_WARN, "isamc_open failed");
473 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
475 struct ISAMC_M_s isamc_m;
477 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
478 rw, key_isamc_m(res, &isamc_m), 0)))
480 yaz_log (YLOG_WARN, "isamb_open failed");
484 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
486 struct ISAMC_M_s isamc_m;
488 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
489 rw, key_isamc_m(res, &isamc_m), 1)))
491 yaz_log (YLOG_WARN, "isamb_open failed");
495 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
497 struct ISAMC_M_s isamc_m;
499 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
500 rw, key_isamc_m(res, &isamc_m), -1)))
502 yaz_log (YLOG_WARN, "isamb_open failed");
508 reg->zei = zebraExplain_open(reg->records, reg->dh,
510 zebra_extract_explain);
513 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
520 zebra_register_close(zs, reg);
523 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
527 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
529 ZEBRA_CHECK_HANDLE(zh);
530 yaz_log(log_level, "zebra_admin_shutdown");
532 zebra_mutex_cond_lock (&zh->service->session_lock);
533 zh->service->stop_flag = 1;
534 zebra_mutex_cond_unlock (&zh->service->session_lock);
538 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
541 ZEBRA_CHECK_HANDLE(zh);
542 yaz_log(log_level, "zebra_admin_start");
544 zebra_mutex_cond_lock (&zs->session_lock);
545 zebra_mutex_cond_unlock (&zs->session_lock);
549 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
553 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
557 zebraExplain_close (reg->zei);
558 dict_close (reg->dict);
560 dict_close (reg->matchDict);
561 zebra_sort_close(reg->sort_index);
563 isams_close (reg->isams);
565 isamc_close (reg->isamc);
567 isamb_close (reg->isamb);
568 rec_close (®->records);
570 recTypes_destroy (reg->recTypes);
571 zebra_maps_close (reg->zebra_maps);
572 zebraRankDestroy (reg);
573 bfs_destroy (reg->bfs);
574 data1_destroy (reg->dh);
576 zebra_rec_keys_close(reg->keys);
577 zebra_rec_keys_close(reg->sortKeys);
579 key_block_destroy(®->key_block);
584 ZEBRA_RES zebra_stop(ZebraService zs)
590 zebra_close (zs->sessions);
593 zebra_mutex_cond_destroy (&zs->session_lock);
596 passwd_db_close (zs->passwd_db);
598 recTypeClass_destroy(zs->record_classes);
599 nmem_destroy(zs->nmem);
600 res_close (zs->global_res);
602 yaz_timing_stop(zs->timing);
603 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
604 yaz_timing_get_real(zs->timing),
605 yaz_timing_get_user(zs->timing),
606 yaz_timing_get_sys(zs->timing));
609 yaz_timing_destroy(&zs->timing);
614 ZEBRA_RES zebra_close(ZebraHandle zh)
617 struct zebra_session **sp;
620 yaz_log(log_level, "zebra_close");
621 ZEBRA_CHECK_HANDLE(zh);
626 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
627 resultSetDestroy (zh, -1, 0, 0);
630 zebra_register_close(zh->service, zh->reg);
631 zebra_close_res (zh);
632 res_close(zh->session_res);
634 xfree(zh->record_encoding);
636 xfree(zh->dbaccesslist);
638 for (i = 0; i < zh->num_basenames; i++)
639 xfree(zh->basenames[i]);
640 xfree(zh->basenames);
642 if (zh->iconv_to_utf8 != 0)
643 yaz_iconv_close (zh->iconv_to_utf8);
644 if (zh->iconv_from_utf8 != 0)
645 yaz_iconv_close (zh->iconv_from_utf8);
647 zebra_mutex_cond_lock (&zs->session_lock);
648 zebra_lock_destroy (zh->lock_normal);
649 zebra_lock_destroy (zh->lock_shadow);
661 zebra_mutex_cond_unlock (&zs->session_lock);
663 xfree(zh->user_perm);
664 zh->service = 0; /* more likely to trigger an assert */
666 zebra_limit_destroy(zh->m_limit);
668 nmem_destroy(zh->nmem_error);
675 struct map_baseinfo {
681 char **new_basenames;
685 static void zebra_open_res(ZebraHandle zh)
693 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
694 zh->res = res_open(zh->session_res, 0);
695 res_read_file(zh->res, fname);
697 else if (*zh->reg_name == 0)
699 zh->res = res_open(zh->session_res, 0);
703 yaz_log (YLOG_WARN, "no register root specified");
704 zh->res = 0; /* no path for register - fail! */
708 static void zebra_close_res (ZebraHandle zh)
716 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
720 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
724 assert (zh->reg == 0);
725 assert (*zh->reg_name == 0);
731 resultSetInvalidate (zh);
732 zebra_register_close(zh->service, zh->reg);
738 zh->reg_name = xstrdup (new_reg);
742 if (zh->service->path_root)
744 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
745 strlen(zh->reg_name) + 3);
746 strcpy (zh->path_reg, zh->service->path_root);
749 strcat (zh->path_reg, "/");
750 strcat (zh->path_reg, zh->reg_name);
756 zebra_lock_destroy (zh->lock_normal);
760 zebra_lock_destroy (zh->lock_shadow);
766 const char *lock_area = res_get (zh->res, "lockDir");
768 if (!lock_area && zh->path_reg)
769 res_set (zh->res, "lockDir", zh->path_reg);
770 sprintf (fname, "norm.%s.LCK", zh->reg_name);
772 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
774 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
776 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
778 if (!zh->lock_normal || !zh->lock_shadow)
782 zebra_lock_destroy(zh->lock_normal);
787 zebra_lock_destroy(zh->lock_shadow);
796 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
797 zebra_set_approx_limit(zh, approx);
801 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
802 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
806 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
809 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
810 zh->m_segment_indexing);
815 void map_basenames_func (void *vp, const char *name, const char *value)
817 struct map_baseinfo *p = (struct map_baseinfo *) vp;
819 char fromdb[128], todb[8][128];
826 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
827 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
828 todb[5], todb[6], todb[7]);
832 for (i = 0; i<p->num_bases; i++)
833 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
836 for (i = 0; i < no; i++)
838 if (p->new_num_bases == p->new_num_max)
840 p->new_basenames[(p->new_num_bases)++] =
841 nmem_strdup (p->mem, todb[i]);
847 int zebra_select_default_database(ZebraHandle zh)
851 /* no database has been selected - so we select based on
852 resource setting (including group)
854 const char *group = res_get(zh->session_res, "group");
855 const char *v = res_get_prefix(zh->session_res,
856 "database", group, "Default");
857 return zebra_select_database(zh, v);
862 void map_basenames (ZebraHandle zh, ODR stream,
863 int *num_bases, char ***basenames)
865 struct map_baseinfo info;
866 struct map_baseinfo *p = &info;
869 yaz_log(log_level, "map_basenames ");
874 info.num_bases = *num_bases;
875 info.basenames = *basenames;
876 info.new_num_max = 128;
877 info.new_num_bases = 0;
878 info.new_basenames = (char **)
879 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
880 info.mem = stream->mem;
882 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
884 for (i = 0; i<p->num_bases; i++)
885 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
887 p->new_basenames[(p->new_num_bases)++] =
888 nmem_strdup (p->mem, p->basenames[i]);
890 *num_bases = info.new_num_bases;
891 *basenames = info.new_basenames;
892 for (i = 0; i<*num_bases; i++)
893 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
896 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
898 ZEBRA_CHECK_HANDLE(zh);
900 yaz_log(log_level, "zebra_select_database %s",basename);
902 return zebra_select_databases (zh, 1, &basename);
905 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
906 const char **basenames)
913 ZEBRA_CHECK_HANDLE(zh);
916 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
917 num_bases,basenames[0]);
922 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
926 /* Check if the user has access to all databases (Seb) */
927 /* You could argue that this should happen later, after we have
928 * determined that the database(s) exist. */
929 if (zh->dbaccesslist) {
930 for (i = 0; i < num_bases; i++) {
931 const char *db = basenames[i];
933 for (p = zh->dbaccesslist; p && *p; p = pp) {
935 if ((pp = strchr(p, '+'))) {
941 if (len == strlen(db) && !strncmp(db, p, len))
945 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
951 for (i = 0; i < zh->num_basenames; i++)
952 xfree(zh->basenames[i]);
953 xfree(zh->basenames);
955 zh->num_basenames = num_bases;
956 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
957 for (i = 0; i < zh->num_basenames; i++)
958 zh->basenames[i] = xstrdup (basenames[i]);
960 cp = strrchr(basenames[0], '/');
963 len = cp - basenames[0];
964 new_reg = xmalloc(len + 1);
965 memcpy (new_reg, basenames[0], len);
969 new_reg = xstrdup ("");
970 for (i = 1; i<num_bases; i++)
974 cp1 = strrchr (basenames[i], '/');
979 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
982 if (len != cp1 - basenames[i] ||
983 memcmp (basenames[i], new_reg, len))
985 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
993 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
998 zebra_select_register (zh, new_reg);
1002 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1005 if (!zh->lock_normal || !zh->lock_shadow)
1007 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1013 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1015 if (approx_limit == 0)
1016 approx_limit = DEFAULT_APPROX_LIMIT;
1017 zh->approx_limit = approx_limit;
1021 void zebra_set_partial_result(ZebraHandle zh)
1023 zh->partial_result = 1;
1027 ZEBRA_RES zebra_set_busy_handler(ZebraHandle zh,
1028 int (*f)(void *client_data, ZebraHandle zh),
1031 zh->busy_handler_func = f;
1032 zh->busy_handler_data = client_data;
1036 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1037 const char *setname, zint *hits,
1038 int *estimated_hit_count,
1039 int *partial_resultset)
1043 ZEBRA_CHECK_HANDLE(zh);
1049 yaz_log(log_level, "zebra_search_rpn");
1051 zh->partial_result = 0;
1053 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1056 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1057 zh->num_basenames, zh->basenames, setname,
1058 hits, estimated_hit_count);
1060 *partial_resultset = zh->partial_result;
1065 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1066 const char *setname, zint *hits)
1068 int estimated_hit_count;
1069 int partial_resultset;
1070 return zebra_search_RPN_x(zh, o, query, setname, hits,
1071 &estimated_hit_count,
1072 &partial_resultset);
1075 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1076 const char *setname,
1077 Z_RecordComposition *comp,
1078 oid_value input_format, int num_recs,
1079 ZebraRetrievalRecord *recs)
1081 ZebraMetaRecord *poset;
1083 ZEBRA_RES ret = ZEBRA_OK;
1086 ZEBRA_CHECK_HANDLE(zh);
1092 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1096 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1101 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1104 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1105 for (i = 0; i<num_recs; i++)
1106 pos_array[i] = recs[i].position;
1107 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1110 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1111 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1117 for (i = 0; i<num_recs; i++)
1121 recs[i].errCode = 0;
1122 recs[i].format = VAL_SUTRS;
1123 recs[i].len = strlen(poset[i].term);
1124 recs[i].buf = poset[i].term;
1125 recs[i].base = poset[i].db;
1127 else if (poset[i].sysno)
1131 zebra_snippets *hit_snippet = zebra_snippets_create();
1133 /* we disable hit snippets for now. It does not work well
1134 and it slows retrieval down a lot */
1136 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1140 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1142 stream, input_format, comp,
1143 &recs[i].format, &buf, &len,
1144 &recs[i].base, &recs[i].errString);
1149 recs[i].buf = (char*) odr_malloc(stream, len);
1150 memcpy(recs[i].buf, buf, len);
1154 recs[i].score = poset[i].score;
1155 recs[i].sysno = poset[i].sysno;
1156 zebra_snippets_destroy(hit_snippet);
1160 /* only need to set it once */
1161 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1163 zebra_setError_zint(zh,
1164 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1169 recs[i].buf = 0; /* no record and no error issued */
1171 recs[i].errCode = 0;
1172 recs[i].format = VAL_NONE;
1176 zebra_meta_records_destroy(zh, poset, num_recs);
1178 zebra_end_read (zh);
1183 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1185 int *num_entries, ZebraScanEntry **entries,
1187 const char *setname)
1189 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1190 Z_AttributesPlusTerm *zapt;
1194 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1197 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1200 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1201 position, num_entries, entries, is_partial,
1203 yaz_pqf_destroy (pqf_parser);
1207 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1208 oid_value attributeset,
1210 int *num_entries, ZebraScanEntry **entries,
1212 const char *setname)
1215 RSET limit_rset = 0;
1217 ZEBRA_CHECK_HANDLE(zh);
1222 assert(num_entries);
1225 yaz_log(log_level, "zebra_scan");
1227 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1235 limit_rset = resultSetRef(zh, setname);
1239 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1241 zebra_end_read (zh);
1245 res = rpn_scan(zh, stream, zapt, attributeset,
1246 zh->num_basenames, zh->basenames, position,
1247 num_entries, entries, is_partial, limit_rset);
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,
1282 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1284 if (zebra_begin_read(zh))
1285 return Z_DeleteStatus_systemProblemAtTarget;
1288 case Z_DeleteResultSetRequest_list:
1289 assert(num_setnames>0);
1291 resultSetDestroy (zh, num_setnames, setnames, statuses);
1293 case Z_DeleteResultSetRequest_all:
1294 resultSetDestroy (zh, -1, 0, statuses);
1297 zebra_end_read (zh);
1298 status = Z_DeleteStatus_success;
1299 for (i = 0; i<num_setnames; i++)
1300 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1301 status = statuses[i];
1305 int zebra_errCode (ZebraHandle zh)
1309 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1312 yaz_log(log_level, "zebra_errCode: o");
1316 const char *zebra_errString (ZebraHandle zh)
1320 e= diagbib1_str (zh->errCode);
1321 yaz_log(log_level, "zebra_errString: %s",e);
1325 char *zebra_errAdd (ZebraHandle zh)
1330 yaz_log(log_level, "zebra_errAdd: %s",a);
1334 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1337 const char *astring;
1341 ZEBRA_CHECK_HANDLE(zh);
1345 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1346 p = res_get(zs->global_res, u);
1347 xfree(zh->user_perm);
1348 zh->user_perm = xstrdup(p ? p : "r");
1350 /* Determine database access list */
1351 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1353 zh->dbaccesslist = xstrdup(astring);
1355 zh->dbaccesslist = 0;
1357 /* users that don't require a password .. */
1358 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1361 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1366 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1367 const char *record_type)
1369 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1370 database, record_type);
1371 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1373 return zebra_begin_trans(zh, 1);
1376 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1378 ZEBRA_CHECK_HANDLE(zh);
1379 yaz_log(log_level, "zebra_admin_import_end");
1380 return zebra_end_trans(zh);
1383 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1385 ZEBRA_RES res = ZEBRA_OK;
1388 ZEBRA_CHECK_HANDLE(zh);
1389 yaz_log(log_level, "zebra_admin_import_segment");
1391 for (i = 0; i<segment->num_segmentRecords; i++)
1393 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1395 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1397 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1398 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1400 Odr_oct *oct = fragment->u.notExternallyTagged;
1403 if (zebra_update_record(zh,
1404 0, /* record Type */
1408 (const char *) oct->buf, oct->len,
1417 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1418 const char *rec_buf,
1420 const char *recid_buf, size_t recid_len,
1422 /* 1 = insert. Fail it already exists */
1423 /* 2 = replace. Fail it does not exist */
1424 /* 3 = delete. Fail if does not exist */
1425 /* 4 = update. Insert/replace */
1432 ZEBRA_CHECK_HANDLE(zh);
1433 assert(action>0 && action <=4);
1436 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1438 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1440 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1441 "no record ID or empty record ID");
1445 memcpy (recid_z, recid_buf, recid_len);
1446 recid_z[recid_len] = 0;
1448 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1451 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1452 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1455 if (action == 1) /* fail if insert */
1457 if (zebra_end_trans(zh) != ZEBRA_OK)
1458 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1459 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1460 "Cannot insert record: already exist");
1464 memcpy (&sysno, rinfo+1, sizeof(sysno));
1468 if (action == 2 || action == 3) /* fail if delete or update */
1470 if (zebra_end_trans(zh) != ZEBRA_OK)
1471 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1472 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1473 "Cannot delete/update record: does not exist");
1476 action = 1; /* make it an insert (if it's an update).. */
1478 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1479 action == 3 ? 1 : 0 /* delete flag */,
1485 0, /* force update */
1486 1 /* allow update */
1488 if (res == ZEBRA_FAIL)
1490 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1491 "Unable to parse record");
1495 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1496 sizeof(sysno), &sysno);
1498 else if (action == 3)
1500 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1502 if (zebra_end_trans(zh) != ZEBRA_OK)
1504 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1510 int delete_w_handle(const char *info, void *handle)
1512 ZebraHandle zh = (ZebraHandle) handle;
1516 if (*info == sizeof(pos))
1518 memcpy (&pos, info+1, sizeof(pos));
1519 isamb_unlink(zh->reg->isamb, pos);
1524 static int delete_SU_handle(void *handle, int ord)
1526 ZebraHandle zh = (ZebraHandle) handle;
1530 ord_len = key_SU_encode (ord, ord_buf);
1531 ord_buf[ord_len] = '\0';
1533 assert (zh->reg->isamb);
1534 dict_delete_subtree(zh->reg->dict, ord_buf,
1535 zh, delete_w_handle);
1539 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1541 ZEBRA_RES ret = ZEBRA_OK;
1543 yaz_log(log_level, "zebra_drop_database %s", db);
1544 ZEBRA_CHECK_HANDLE(zh);
1546 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1548 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1553 if (zebraExplain_curDatabase (zh->reg->zei, db))
1555 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1560 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1561 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1562 0 /* handle */, 0 /* func */);
1563 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1564 zebraExplain_removeDatabase(zh->reg->zei, zh);
1565 zebra_remove_file_match(zh);
1570 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1571 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1572 "drop database only supported for isam:b");
1575 if (zebra_end_trans (zh) != ZEBRA_OK)
1577 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1583 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1585 yaz_log(log_level, "zebra_create_database %s", db);
1586 ZEBRA_CHECK_HANDLE(zh);
1589 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1591 if (zebra_begin_trans (zh, 1))
1594 /* announce database */
1595 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1596 /* explainDatabase */))
1598 if (zebra_end_trans (zh) != ZEBRA_OK)
1600 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1602 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1605 return zebra_end_trans (zh);
1608 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1609 const char *input_str, int input_len,
1610 char *output_str, int output_len)
1616 yaz_log(log_level, "zebra_string_norm ");
1618 if (!zh->reg->zebra_maps)
1620 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1621 input_str, input_len);
1624 if (wrbuf_len(wrbuf) >= output_len)
1626 if (wrbuf_len(wrbuf))
1627 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1628 output_str[wrbuf_len(wrbuf)] = '\0';
1629 return wrbuf_len(wrbuf);
1632 /** \brief set register state (state*.LCK)
1633 \param zh Zebra handle
1635 \param seqno sequence number
1638 d=writing to shadow(dirty)
1642 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1644 char state_fname[256];
1649 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1651 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1652 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1653 f = fopen (fname, "w");
1655 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1656 fprintf (f, "%c %d %ld\n", val, seqno, p);
1661 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1663 char state_fname[256];
1668 yaz_log(log_level, "zebra_get_state ");
1670 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1671 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1672 f = fopen (fname, "r");
1678 fscanf (f, "%c %d", val, seqno);
1684 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1686 return zebra_begin_trans(zh, 0);
1689 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1691 return zebra_end_trans(zh);
1694 static void read_res_for_transaction(ZebraHandle zh)
1696 const char *group = res_get(zh->res, "group");
1698 /* FIXME - do we still use groups ?? */
1700 zh->m_group = group;
1701 v = res_get_prefix(zh->res, "followLinks", group, "1");
1702 zh->m_follow_links = atoi(v);
1704 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1705 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1707 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1708 zh->m_store_keys = atoi(v);
1710 v = res_get_prefix(zh->res, "storeData", group, "1");
1711 zh->m_store_data = atoi(v);
1713 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1714 zh->m_explain_database = atoi(v);
1716 v = res_get_prefix(zh->res, "openRW", group, "1");
1717 zh->m_flag_rw = atoi(v);
1719 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1720 zh->m_file_verbose_limit = atoi(v);
1723 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1725 ZEBRA_CHECK_HANDLE(zh);
1726 zebra_select_default_database(zh);
1729 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1730 "zebra_begin_trans: no database selected");
1734 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1738 if (rw && !strchr(zh->user_perm, 'w'))
1742 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1753 const char *rval = 0;
1758 read_res_for_transaction(zh);
1761 if (zh->trans_no != 1)
1763 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1764 "zebra_begin_trans: no write trans within read");
1769 resultSetInvalidate (zh);
1770 zebra_register_close(zh->service, zh->reg);
1772 zh->trans_w_no = zh->trans_no;
1774 zh->records_inserted = 0;
1775 zh->records_updated = 0;
1776 zh->records_deleted = 0;
1777 zh->records_processed = 0;
1779 #if HAVE_SYS_TIMES_H
1783 if (zh->shadow_enable)
1784 rval = res_get (zh->res, "shadow");
1788 zebra_lock_r(zh->lock_normal);
1789 zebra_lock_w(zh->lock_shadow);
1793 zebra_lock_w(zh->lock_normal);
1794 zebra_lock_w(zh->lock_shadow);
1796 zebra_get_state (zh, &val, &seqno);
1799 /* either we didn't finish commit or shadow is dirty */
1800 zebra_unlock (zh->lock_shadow);
1801 zebra_unlock (zh->lock_normal);
1802 if (zebra_commit (zh))
1810 zebra_lock_r(zh->lock_normal);
1811 zebra_lock_w(zh->lock_shadow);
1815 zebra_lock_w(zh->lock_normal);
1816 zebra_lock_w(zh->lock_shadow);
1820 zebra_set_state (zh, 'd', seqno);
1822 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1823 1, rval ? 1 : 0, zh->res,
1826 zh->reg->seqno = seqno;
1829 zebra_set_state (zh, 'o', seqno);
1831 zebra_unlock (zh->lock_shadow);
1832 zebra_unlock (zh->lock_normal);
1837 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1838 "zebra_begin_trans: cannot open register");
1839 yaz_log(YLOG_FATAL, "%s", zh->errString);
1842 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1852 if (zh->trans_no != 1)
1854 return zebra_flush_reg (zh);
1856 #if HAVE_SYS_TIMES_H
1862 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1865 if (!zh->lock_normal || !zh->lock_shadow)
1868 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1871 zebra_get_state (zh, &val, &seqno);
1877 else if (seqno != zh->reg->seqno)
1879 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1880 seqno, zh->reg->seqno);
1883 else if (zh->reg->last_val != val)
1885 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1886 val, zh->reg->last_val);
1893 zebra_lock_r (zh->lock_shadow);
1895 zebra_lock_r (zh->lock_normal);
1899 resultSetInvalidate (zh);
1900 zebra_register_close(zh->service, zh->reg);
1902 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1903 0, val == 'c' ? 1 : 0,
1904 zh->res, zh->path_reg);
1907 zebra_unlock (zh->lock_normal);
1908 zebra_unlock (zh->lock_shadow);
1910 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1913 zh->reg->last_val = val;
1914 zh->reg->seqno = seqno;
1916 read_res_for_transaction(zh);
1920 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1922 ZebraTransactionStatus dummy;
1924 yaz_log(log_level, "zebra_end_trans");
1925 ZEBRA_CHECK_HANDLE(zh);
1926 return zebra_end_transaction(zh, &dummy);
1929 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1935 ZEBRA_CHECK_HANDLE(zh);
1938 yaz_log(log_level, "zebra_end_transaction");
1940 status->processed = 0;
1941 status->inserted = 0;
1942 status->updated = 0;
1943 status->deleted = 0;
1947 if (!zh->res || !zh->reg)
1949 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1950 "zebra_end_trans: no open transaction");
1953 if (zh->trans_no != zh->trans_w_no)
1956 if (zh->trans_no != 0)
1959 /* release read lock */
1961 zebra_unlock (zh->lock_normal);
1962 zebra_unlock (zh->lock_shadow);
1965 { /* release write lock */
1969 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1970 rval = res_get (zh->res, "shadow");
1972 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1974 zebra_flush_reg (zh);
1976 resultSetInvalidate (zh);
1978 zebra_register_close(zh->service, zh->reg);
1981 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1982 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1983 zh->records_processed, zh->records_inserted,
1984 zh->records_updated, zh->records_deleted);
1986 status->processed = zh->records_processed;
1987 status->inserted = zh->records_inserted;
1988 status->updated = zh->records_updated;
1989 status->deleted = zh->records_deleted;
1991 zebra_get_state (zh, &val, &seqno);
1994 BFiles bfs = bfs_create (rval, zh->path_reg);
1995 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1996 bf_commitClean (bfs, rval);
2001 zebra_set_state (zh, 'o', seqno);
2003 zebra_unlock (zh->lock_shadow);
2004 zebra_unlock (zh->lock_normal);
2007 #if HAVE_SYS_TIMES_H
2009 yaz_log (log_level, "user/system: %ld/%ld",
2010 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
2011 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
2013 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
2014 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
2019 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
2023 yaz_log (log_level, "updating %s", path);
2025 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
2026 return zebra_update_file_match(zh, path);
2028 return zebra_update_from_path(zh, path);
2031 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2035 yaz_log (log_level, "deleting %s", path);
2036 return zebra_delete_from_path(zh, path);
2039 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2043 yaz_log(log_level, "zebra_repository_show");
2044 repositoryShow (zh, path);
2048 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2054 ZEBRA_RES res = ZEBRA_OK;
2058 zebra_select_default_database(zh);
2061 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2064 rval = res_get(zh->res, "shadow");
2067 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2071 zebra_lock_w(zh->lock_normal);
2072 zebra_lock_r(zh->lock_shadow);
2074 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2077 zebra_unlock(zh->lock_shadow);
2078 zebra_unlock(zh->lock_normal);
2081 zebra_get_state(zh, &val, &seqno);
2085 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2090 bf_cache (bfs, rval);
2091 if (bf_commitExists (bfs))
2094 zebra_set_state(zh, 'd', seqno);
2097 zebra_set_state(zh, 'c', seqno);
2099 yaz_log(YLOG_DEBUG, "commit start");
2100 if (bf_commitExec (bfs))
2103 if (res == ZEBRA_OK)
2106 zebra_set_state(zh, 'o', seqno);
2108 zebra_unlock(zh->lock_shadow);
2109 zebra_unlock(zh->lock_normal);
2111 zebra_lock_w(zh->lock_shadow);
2112 bf_commitClean(bfs, rval);
2113 zebra_unlock(zh->lock_shadow);
2117 zebra_unlock(zh->lock_shadow);
2118 zebra_unlock(zh->lock_normal);
2119 yaz_log(YLOG_WARN, "zebra_commit: failed");
2124 zebra_unlock(zh->lock_shadow);
2125 zebra_unlock(zh->lock_normal);
2126 yaz_log(log_level, "nothing to commit");
2133 ZEBRA_RES zebra_clean(ZebraHandle zh)
2135 yaz_log(log_level, "zebra_clean");
2136 ZEBRA_CHECK_HANDLE(zh);
2137 return zebra_commit_ex(zh, 1);
2140 ZEBRA_RES zebra_commit(ZebraHandle zh)
2142 yaz_log(log_level, "zebra_commit");
2143 ZEBRA_CHECK_HANDLE(zh);
2144 return zebra_commit_ex(zh, 0);
2148 ZEBRA_RES zebra_init(ZebraHandle zh)
2153 yaz_log(log_level, "zebra_init");
2155 ZEBRA_CHECK_HANDLE(zh);
2157 zebra_select_default_database(zh);
2160 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2161 "cannot select default database");
2164 rval = res_get (zh->res, "shadow");
2166 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2169 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2173 bf_cache (bfs, rval);
2177 zebra_set_state (zh, 'o', 0);
2181 ZEBRA_RES zebra_compact(ZebraHandle zh)
2185 yaz_log(log_level, "zebra_compact");
2186 ZEBRA_CHECK_HANDLE(zh);
2189 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2192 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2198 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2200 yaz_log(log_level, "zebra_result");
2203 *code = zh->errCode;
2204 *addinfo = zh->errString;
2208 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2209 *addinfo ="ZebraHandle is NULL";
2213 void zebra_shadow_enable(ZebraHandle zh, int value)
2216 yaz_log(log_level, "zebra_shadow_enable");
2217 zh->shadow_enable = value;
2220 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2222 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2223 ZEBRA_CHECK_HANDLE(zh);
2226 if (zh->iconv_to_utf8 != 0)
2227 yaz_iconv_close(zh->iconv_to_utf8);
2228 if (zh->iconv_from_utf8 != 0)
2229 yaz_iconv_close(zh->iconv_from_utf8);
2232 yaz_iconv_open ("UTF-8", encoding);
2233 if (zh->iconv_to_utf8 == 0)
2234 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2235 zh->iconv_from_utf8 =
2236 yaz_iconv_open (encoding, "UTF-8");
2237 if (zh->iconv_to_utf8 == 0)
2238 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2243 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2245 yaz_log(log_level, "zebra_record_encoding");
2246 ZEBRA_CHECK_HANDLE(zh);
2247 xfree(zh->record_encoding);
2248 zh->record_encoding = 0;
2250 zh->record_encoding = xstrdup (encoding);
2254 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2258 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2260 res_set(zh->res, name, value);
2263 const char *zebra_get_resource(ZebraHandle zh,
2264 const char *name, const char *defaultvalue)
2269 v = res_get_def (zh->res, name, (char *)defaultvalue);
2270 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2274 /* moved from zebra_api_ext.c by pop */
2275 /* FIXME: Should this really be public??? -Heikki */
2277 int zebra_trans_no (ZebraHandle zh)
2279 yaz_log(log_level, "zebra_trans_no");
2281 return zh->trans_no;
2284 int zebra_get_shadow_enable (ZebraHandle zh)
2286 yaz_log(log_level, "zebra_get_shadow_enable");
2288 return zh->shadow_enable;
2291 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2293 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2295 zh->shadow_enable = value;
2298 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2299 const char *buf, int buf_size)
2301 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2304 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2305 const char *recordType,
2306 zint *sysno, const char *match,
2308 const char *buf, int buf_size, int force_update)
2314 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2317 buf_size = strlen(buf);
2319 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2321 res = zebra_buffer_extract_record(zh, buf, buf_size,
2322 0, /* delete_flag */
2328 0); /* allow_update */
2329 if (zebra_end_trans(zh) != ZEBRA_OK)
2331 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2337 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2338 const char *recordType,
2339 zint *sysno, const char *match,
2341 const char *buf, int buf_size,
2346 ZEBRA_CHECK_HANDLE(zh);
2350 yaz_log(log_level, "zebra_update_record");
2352 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2354 if (buf_size < 1) buf_size = strlen(buf);
2356 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2358 res = zebra_buffer_extract_record(zh, buf, buf_size,
2359 0, /* delete_flag */
2365 1); /* allow_update */
2366 if (zebra_end_trans(zh) != ZEBRA_OK)
2368 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2374 ZEBRA_RES zebra_delete_record(ZebraHandle zh,
2375 const char *recordType,
2376 zint *sysno, const char *match,
2378 const char *buf, int buf_size,
2383 ZEBRA_CHECK_HANDLE(zh);
2386 yaz_log(log_level, "zebra_delete_record");
2388 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2390 if (buf_size < 1) buf_size = strlen(buf);
2392 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2394 res = zebra_buffer_extract_record(zh, buf, buf_size,
2395 1, /* delete_flag */
2401 1); /* allow_update */
2402 if (zebra_end_trans(zh) != ZEBRA_OK)
2404 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2410 /* ---------------------------------------------------------------------------
2414 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2415 const char *setname, zint *hits)
2418 ZEBRA_RES res = ZEBRA_OK;
2423 ZEBRA_CHECK_HANDLE(zh);
2425 odr = odr_createmem(ODR_ENCODE);
2430 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2432 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2436 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2437 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2441 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2445 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2453 /* ---------------------------------------------------------------------------
2454 Sort - a simplified interface, with optional read locks.
2456 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2457 const char *sort_spec,
2458 const char *output_setname,
2459 const char **input_setnames)
2461 int num_input_setnames = 0;
2462 int sort_status = 0;
2463 Z_SortKeySpecList *sort_sequence;
2465 ZEBRA_CHECK_HANDLE(zh);
2468 assert(output_setname);
2469 assert(input_setnames);
2470 sort_sequence = yaz_sort_spec (stream, sort_spec);
2471 yaz_log(log_level, "sort (FIXME) ");
2474 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2475 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2479 /* we can do this, since the perl typemap code for char** will
2480 put a NULL at the end of list */
2481 while (input_setnames[num_input_setnames]) num_input_setnames++;
2483 if (zebra_begin_read (zh))
2486 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2487 output_setname, sort_sequence, &sort_status);
2493 /* ---------------------------------------------------------------------------
2494 Get BFS for Zebra system (to make alternative storage methods)
2496 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2499 return zh->reg->bfs;
2504 /* ---------------------------------------------------------------------------
2505 Set limit for search/scan
2507 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2509 ZEBRA_CHECK_HANDLE(zh);
2510 zebra_limit_destroy(zh->m_limit);
2511 zh->m_limit = zebra_limit_create(complement_flag, ids);
2516 Set Error code + addinfo
2518 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2523 nmem_reset(zh->nmem_error);
2524 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2527 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2530 sprintf(vstr, ZINT_FORMAT, i);
2533 nmem_reset(zh->nmem_error);
2534 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2537 void zebra_lock_prefix(Res res, char *path)
2539 const char *lock_dir = res_get_def (res, "lockDir", "");
2541 strcpy(path, lock_dir);
2542 if (*path && path[strlen(path)-1] != '/')
2549 * indent-tabs-mode: nil
2551 * vim: shiftwidth=4 tabstop=8 expandtab