1 /* $Id: zebraapi.c,v 1.243 2007-01-16 15:01:15 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 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
149 yaz_iconv_open ("UTF-8", default_encoding);
150 if (zh->iconv_to_utf8 == 0)
151 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
153 zh->iconv_from_utf8 =
154 yaz_iconv_open (default_encoding, "UTF-8");
155 if (zh->iconv_to_utf8 == 0)
156 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
159 zh->record_encoding = 0;
161 zebra_mutex_cond_lock (&zs->session_lock);
163 zh->next = zs->sessions;
166 zebra_mutex_cond_unlock (&zs->session_lock);
168 zh->store_data_buf = 0;
170 zh->m_limit = zebra_limit_create(1, 0);
172 zh->nmem_error = nmem_create();
177 ZebraService zebra_start(const char *configName)
179 return zebra_start_res(configName, 0, 0);
182 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
188 if (!log_level_initialized)
190 log_level = yaz_log_module_level("zebraapi");
191 log_level_initialized = 1;
194 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
195 configName ? configName : "");
197 if ((res = res_open(def_res, over_res)))
199 const char *passwd_plain = 0;
200 const char *passwd_encrypt = 0;
201 const char *dbaccess = 0;
206 ZEBRA_RES ret = res_read_file(res, configName);
212 if (zebra_check_res(res))
214 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
221 zebra_check_res(res);
224 zh = xmalloc(sizeof(*zh));
225 zh->global_res = res;
230 zebra_mutex_cond_init (&zh->session_lock);
231 passwd_plain = res_get (zh->global_res, "passwd");
232 passwd_encrypt = res_get (zh->global_res, "passwd.c");
233 dbaccess = res_get (zh->global_res, "dbaccess");
235 if (!passwd_plain && !passwd_encrypt)
236 zh->passwd_db = NULL;
239 zh->passwd_db = passwd_db_open();
241 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
245 passwd_db_file_plain(zh->passwd_db, passwd_plain);
247 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
254 zh->dbaccess = res_open(NULL, NULL);
255 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
256 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
261 zh->timing = yaz_timing_create();
262 zh->path_root = res_get (zh->global_res, "root");
263 zh->nmem = nmem_create();
264 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
268 const char *module_path = res_get(res, "modulePath");
270 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
278 void zebra_filter_info(ZebraService zs, void *cd,
279 void (*cb)(void *cd, const char *name))
283 recTypeClass_info(zs->record_classes, cd, cb);
286 void zebra_pidfname(ZebraService zs, char *path)
289 zebra_lock_prefix (zs->global_res, path);
290 strcat(path, "zebrasrv.pid");
293 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
294 int compact_flag, Res res)
296 int page_size = 4096;
297 char resource_str[200];
298 sprintf (resource_str, "dict.%.100s.pagesize", name);
302 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
303 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
305 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
309 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
310 int rw, int useshadow, Res res,
311 const char *reg_path)
313 struct zebra_register *reg;
314 int record_compression = REC_COMPRESS_NONE;
315 const char *recordCompression = 0;
316 const char *profilePath;
318 int sort_type = ZEBRA_SORT_TYPE_FLAT;
319 ZEBRA_RES ret = ZEBRA_OK;
323 reg = xmalloc(sizeof(*reg));
326 reg->name = xstrdup (name);
333 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
334 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
336 reg->dh = data1_create();
343 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
346 data1_destroy(reg->dh);
353 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
355 bfs_destroy(reg->bfs);
356 data1_destroy(reg->dh);
363 getcwd(cwd, sizeof(cwd)-1);
364 profilePath = res_get_def(res, "profilePath", 0);
366 data1_set_tabpath (reg->dh, profilePath);
367 data1_set_tabroot (reg->dh, reg_path);
368 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
371 zebra_maps_open(res, reg_path, profilePath);
372 if (!reg->zebra_maps)
374 recTypes_destroy(reg->recTypes);
375 bfs_destroy(reg->bfs);
376 data1_destroy(reg->dh);
381 reg->rank_classes = NULL;
384 reg->keys = zebra_rec_keys_open();
386 reg->sortKeys = zebra_rec_keys_open();
397 /* installing rank classes */
398 zebraRankInstall (reg, rank_1_class);
399 zebraRankInstall (reg, rank_similarity_class);
400 zebraRankInstall (reg, rank_static_class);
402 recordCompression = res_get_def (res, "recordCompression", "none");
403 if (!strcmp (recordCompression, "none"))
404 record_compression = REC_COMPRESS_NONE;
405 if (!strcmp (recordCompression, "bzip2"))
406 record_compression = REC_COMPRESS_BZIP2;
410 const char *index_fname = res_get_def(res, "index", "default.idx");
411 if (index_fname && *index_fname)
413 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
418 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
420 yaz_log (YLOG_WARN, "rec_open failed");
425 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
427 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
429 yaz_log (YLOG_WARN, "dict_open failed");
434 if (res_get_match (res, "sortindex", "f", "f"))
435 sort_type = ZEBRA_SORT_TYPE_FLAT;
436 else if (res_get_match (res, "sortindex", "i", "f"))
437 sort_type = ZEBRA_SORT_TYPE_ISAMB;
440 yaz_log (YLOG_WARN, "bad_value for 'sortindex'");
445 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
447 yaz_log (YLOG_WARN, "zebra_sort_open failed");
450 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
452 struct ISAMS_M_s isams_m;
453 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
454 key_isams_m(res, &isams_m))))
456 yaz_log (YLOG_WARN, "isams_open failed");
460 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
462 struct ISAMC_M_s isamc_m;
463 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
464 rw, key_isamc_m(res, &isamc_m))))
466 yaz_log (YLOG_WARN, "isamc_open failed");
470 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
472 struct ISAMC_M_s isamc_m;
474 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
475 rw, key_isamc_m(res, &isamc_m), 0)))
477 yaz_log (YLOG_WARN, "isamb_open failed");
481 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
483 struct ISAMC_M_s isamc_m;
485 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
486 rw, key_isamc_m(res, &isamc_m), 1)))
488 yaz_log (YLOG_WARN, "isamb_open failed");
492 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
494 struct ISAMC_M_s isamc_m;
496 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
497 rw, key_isamc_m(res, &isamc_m), -1)))
499 yaz_log (YLOG_WARN, "isamb_open failed");
505 reg->zei = zebraExplain_open(reg->records, reg->dh,
507 zebra_extract_explain);
510 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
517 zebra_register_close(zs, reg);
520 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
524 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
526 ZEBRA_CHECK_HANDLE(zh);
527 yaz_log(log_level, "zebra_admin_shutdown");
529 zebra_mutex_cond_lock (&zh->service->session_lock);
530 zh->service->stop_flag = 1;
531 zebra_mutex_cond_unlock (&zh->service->session_lock);
535 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
538 ZEBRA_CHECK_HANDLE(zh);
539 yaz_log(log_level, "zebra_admin_start");
541 zebra_mutex_cond_lock (&zs->session_lock);
542 zebra_mutex_cond_unlock (&zs->session_lock);
546 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
550 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
554 zebraExplain_close (reg->zei);
555 dict_close (reg->dict);
557 dict_close (reg->matchDict);
558 zebra_sort_close(reg->sort_index);
560 isams_close (reg->isams);
562 isamc_close (reg->isamc);
564 isamb_close (reg->isamb);
565 rec_close (®->records);
567 recTypes_destroy (reg->recTypes);
568 zebra_maps_close (reg->zebra_maps);
569 zebraRankDestroy (reg);
570 bfs_destroy (reg->bfs);
571 data1_destroy (reg->dh);
573 zebra_rec_keys_close(reg->keys);
574 zebra_rec_keys_close(reg->sortKeys);
576 key_block_destroy(®->key_block);
581 ZEBRA_RES zebra_stop(ZebraService zs)
587 zebra_close (zs->sessions);
590 zebra_mutex_cond_destroy (&zs->session_lock);
593 passwd_db_close (zs->passwd_db);
595 recTypeClass_destroy(zs->record_classes);
596 nmem_destroy(zs->nmem);
597 res_close (zs->global_res);
599 yaz_timing_stop(zs->timing);
600 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
601 yaz_timing_get_real(zs->timing),
602 yaz_timing_get_user(zs->timing),
603 yaz_timing_get_sys(zs->timing));
606 yaz_timing_destroy(&zs->timing);
611 ZEBRA_RES zebra_close(ZebraHandle zh)
614 struct zebra_session **sp;
617 yaz_log(log_level, "zebra_close");
618 ZEBRA_CHECK_HANDLE(zh);
623 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
624 resultSetDestroy (zh, -1, 0, 0);
627 zebra_register_close(zh->service, zh->reg);
628 zebra_close_res (zh);
629 res_close(zh->session_res);
631 xfree(zh->record_encoding);
633 xfree(zh->dbaccesslist);
635 for (i = 0; i < zh->num_basenames; i++)
636 xfree(zh->basenames[i]);
637 xfree(zh->basenames);
639 if (zh->iconv_to_utf8 != 0)
640 yaz_iconv_close (zh->iconv_to_utf8);
641 if (zh->iconv_from_utf8 != 0)
642 yaz_iconv_close (zh->iconv_from_utf8);
644 zebra_mutex_cond_lock (&zs->session_lock);
645 zebra_lock_destroy (zh->lock_normal);
646 zebra_lock_destroy (zh->lock_shadow);
658 zebra_mutex_cond_unlock (&zs->session_lock);
660 xfree(zh->user_perm);
661 zh->service = 0; /* more likely to trigger an assert */
663 zebra_limit_destroy(zh->m_limit);
665 nmem_destroy(zh->nmem_error);
672 struct map_baseinfo {
678 char **new_basenames;
682 static void zebra_open_res(ZebraHandle zh)
690 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
691 zh->res = res_open(zh->session_res, 0);
692 res_read_file(zh->res, fname);
694 else if (*zh->reg_name == 0)
696 zh->res = res_open(zh->session_res, 0);
700 yaz_log (YLOG_WARN, "no register root specified");
701 zh->res = 0; /* no path for register - fail! */
705 static void zebra_close_res (ZebraHandle zh)
713 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
717 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
721 assert (zh->reg == 0);
722 assert (*zh->reg_name == 0);
728 resultSetInvalidate (zh);
729 zebra_register_close(zh->service, zh->reg);
735 zh->reg_name = xstrdup (new_reg);
739 if (zh->service->path_root)
741 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
742 strlen(zh->reg_name) + 3);
743 strcpy (zh->path_reg, zh->service->path_root);
746 strcat (zh->path_reg, "/");
747 strcat (zh->path_reg, zh->reg_name);
753 zebra_lock_destroy (zh->lock_normal);
757 zebra_lock_destroy (zh->lock_shadow);
763 const char *lock_area = res_get (zh->res, "lockDir");
765 if (!lock_area && zh->path_reg)
766 res_set (zh->res, "lockDir", zh->path_reg);
767 sprintf (fname, "norm.%s.LCK", zh->reg_name);
769 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
771 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
773 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
775 if (!zh->lock_normal || !zh->lock_shadow)
779 zebra_lock_destroy(zh->lock_normal);
784 zebra_lock_destroy(zh->lock_shadow);
793 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
794 zebra_set_approx_limit(zh, approx);
798 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
799 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
803 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
806 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
807 zh->m_segment_indexing);
812 void map_basenames_func (void *vp, const char *name, const char *value)
814 struct map_baseinfo *p = (struct map_baseinfo *) vp;
816 char fromdb[128], todb[8][128];
823 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
824 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
825 todb[5], todb[6], todb[7]);
829 for (i = 0; i<p->num_bases; i++)
830 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
833 for (i = 0; i < no; i++)
835 if (p->new_num_bases == p->new_num_max)
837 p->new_basenames[(p->new_num_bases)++] =
838 nmem_strdup (p->mem, todb[i]);
844 int zebra_select_default_database(ZebraHandle zh)
848 /* no database has been selected - so we select based on
849 resource setting (including group)
851 const char *group = res_get(zh->session_res, "group");
852 const char *v = res_get_prefix(zh->session_res,
853 "database", group, "Default");
854 return zebra_select_database(zh, v);
859 void map_basenames (ZebraHandle zh, ODR stream,
860 int *num_bases, char ***basenames)
862 struct map_baseinfo info;
863 struct map_baseinfo *p = &info;
866 yaz_log(log_level, "map_basenames ");
871 info.num_bases = *num_bases;
872 info.basenames = *basenames;
873 info.new_num_max = 128;
874 info.new_num_bases = 0;
875 info.new_basenames = (char **)
876 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
877 info.mem = stream->mem;
879 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
881 for (i = 0; i<p->num_bases; i++)
882 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
884 p->new_basenames[(p->new_num_bases)++] =
885 nmem_strdup (p->mem, p->basenames[i]);
887 *num_bases = info.new_num_bases;
888 *basenames = info.new_basenames;
889 for (i = 0; i<*num_bases; i++)
890 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
893 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
895 ZEBRA_CHECK_HANDLE(zh);
897 yaz_log(log_level, "zebra_select_database %s",basename);
899 return zebra_select_databases (zh, 1, &basename);
902 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
903 const char **basenames)
910 ZEBRA_CHECK_HANDLE(zh);
913 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
914 num_bases,basenames[0]);
919 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
923 /* Check if the user has access to all databases (Seb) */
924 /* You could argue that this should happen later, after we have
925 * determined that the database(s) exist. */
926 if (zh->dbaccesslist) {
927 for (i = 0; i < num_bases; i++) {
928 const char *db = basenames[i];
930 for (p = zh->dbaccesslist; p && *p; p = pp) {
932 if ((pp = strchr(p, '+'))) {
938 if (len == strlen(db) && !strncmp(db, p, len))
942 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
948 for (i = 0; i < zh->num_basenames; i++)
949 xfree(zh->basenames[i]);
950 xfree(zh->basenames);
952 zh->num_basenames = num_bases;
953 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
954 for (i = 0; i < zh->num_basenames; i++)
955 zh->basenames[i] = xstrdup (basenames[i]);
957 cp = strrchr(basenames[0], '/');
960 len = cp - basenames[0];
961 new_reg = xmalloc(len + 1);
962 memcpy (new_reg, basenames[0], len);
966 new_reg = xstrdup ("");
967 for (i = 1; i<num_bases; i++)
971 cp1 = strrchr (basenames[i], '/');
976 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
979 if (len != cp1 - basenames[i] ||
980 memcmp (basenames[i], new_reg, len))
982 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
990 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
995 zebra_select_register (zh, new_reg);
999 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1002 if (!zh->lock_normal || !zh->lock_shadow)
1004 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1010 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1012 if (approx_limit == 0)
1013 approx_limit = DEFAULT_APPROX_LIMIT;
1014 zh->approx_limit = approx_limit;
1018 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1019 const char *setname, zint *hits,
1020 int *estimated_hit_count,
1021 int *partial_resultset)
1025 ZEBRA_CHECK_HANDLE(zh);
1031 yaz_log(log_level, "zebra_search_rpn");
1033 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1036 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1037 zh->num_basenames, zh->basenames, setname,
1038 hits, estimated_hit_count, partial_resultset);
1043 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1044 const char *setname, zint *hits)
1046 int estimated_hit_count;
1047 int partial_resultset;
1048 return zebra_search_RPN_x(zh, o, query, setname, hits,
1049 &estimated_hit_count,
1050 &partial_resultset);
1053 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1054 const char *setname,
1055 Z_RecordComposition *comp,
1056 oid_value input_format, int num_recs,
1057 ZebraRetrievalRecord *recs)
1059 ZebraMetaRecord *poset;
1061 ZEBRA_RES ret = ZEBRA_OK;
1064 ZEBRA_CHECK_HANDLE(zh);
1070 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1074 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1079 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1082 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1083 for (i = 0; i<num_recs; i++)
1084 pos_array[i] = recs[i].position;
1085 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1088 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1089 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1095 for (i = 0; i<num_recs; i++)
1099 recs[i].errCode = 0;
1100 recs[i].format = VAL_SUTRS;
1101 recs[i].len = strlen(poset[i].term);
1102 recs[i].buf = poset[i].term;
1103 recs[i].base = poset[i].db;
1105 else if (poset[i].sysno)
1109 zebra_snippets *hit_snippet = zebra_snippets_create();
1111 /* we disable hit snippets for now. It does not work well
1112 and it slows retrieval down a lot */
1114 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1118 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1120 stream, input_format, comp,
1121 &recs[i].format, &buf, &len,
1122 &recs[i].base, &recs[i].errString);
1127 recs[i].buf = (char*) odr_malloc(stream, len);
1128 memcpy(recs[i].buf, buf, len);
1132 recs[i].score = poset[i].score;
1133 recs[i].sysno = poset[i].sysno;
1134 zebra_snippets_destroy(hit_snippet);
1138 /* only need to set it once */
1139 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1141 zebra_setError_zint(zh,
1142 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1147 recs[i].buf = 0; /* no record and no error issued */
1149 recs[i].errCode = 0;
1150 recs[i].format = VAL_NONE;
1154 zebra_meta_records_destroy(zh, poset, num_recs);
1156 zebra_end_read (zh);
1161 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1163 int *num_entries, ZebraScanEntry **entries,
1165 const char *setname)
1167 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1168 Z_AttributesPlusTerm *zapt;
1172 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1175 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1178 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1179 position, num_entries, entries, is_partial,
1181 yaz_pqf_destroy (pqf_parser);
1185 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1186 oid_value attributeset,
1188 int *num_entries, ZebraScanEntry **entries,
1190 const char *setname)
1193 RSET limit_rset = 0;
1195 ZEBRA_CHECK_HANDLE(zh);
1200 assert(num_entries);
1203 yaz_log(log_level, "zebra_scan");
1205 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1213 limit_rset = resultSetRef(zh, setname);
1217 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1219 zebra_end_read (zh);
1223 res = rpn_scan(zh, stream, zapt, attributeset,
1224 zh->num_basenames, zh->basenames, position,
1225 num_entries, entries, is_partial, limit_rset);
1230 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1231 int num_input_setnames, const char **input_setnames,
1232 const char *output_setname,
1233 Z_SortKeySpecList *sort_sequence,
1237 ZEBRA_CHECK_HANDLE(zh);
1239 assert(num_input_setnames>0);
1240 assert(input_setnames);
1241 assert(sort_sequence);
1242 assert(sort_status);
1243 yaz_log(log_level, "zebra_sort");
1245 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1247 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1248 output_setname, sort_sequence, sort_status);
1253 int zebra_deleteResultSet(ZebraHandle zh, int function,
1254 int num_setnames, char **setnames,
1260 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1262 if (zebra_begin_read(zh))
1263 return Z_DeleteStatus_systemProblemAtTarget;
1266 case Z_DeleteResultSetRequest_list:
1267 assert(num_setnames>0);
1269 resultSetDestroy (zh, num_setnames, setnames, statuses);
1271 case Z_DeleteResultSetRequest_all:
1272 resultSetDestroy (zh, -1, 0, statuses);
1275 zebra_end_read (zh);
1276 status = Z_DeleteStatus_success;
1277 for (i = 0; i<num_setnames; i++)
1278 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1279 status = statuses[i];
1283 int zebra_errCode (ZebraHandle zh)
1287 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1290 yaz_log(log_level, "zebra_errCode: o");
1294 const char *zebra_errString (ZebraHandle zh)
1298 e= diagbib1_str (zh->errCode);
1299 yaz_log(log_level, "zebra_errString: %s",e);
1303 char *zebra_errAdd (ZebraHandle zh)
1308 yaz_log(log_level, "zebra_errAdd: %s",a);
1312 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1315 const char *astring;
1319 ZEBRA_CHECK_HANDLE(zh);
1323 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1324 p = res_get(zs->global_res, u);
1325 xfree(zh->user_perm);
1326 zh->user_perm = xstrdup(p ? p : "r");
1328 /* Determine database access list */
1329 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1331 zh->dbaccesslist = xstrdup(astring);
1333 zh->dbaccesslist = 0;
1335 /* users that don't require a password .. */
1336 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1339 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1344 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1345 const char *record_type)
1347 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1348 database, record_type);
1349 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1351 return zebra_begin_trans(zh, 1);
1354 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1356 ZEBRA_CHECK_HANDLE(zh);
1357 yaz_log(log_level, "zebra_admin_import_end");
1358 return zebra_end_trans(zh);
1361 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1363 ZEBRA_RES res = ZEBRA_OK;
1366 ZEBRA_CHECK_HANDLE(zh);
1367 yaz_log(log_level, "zebra_admin_import_segment");
1369 for (i = 0; i<segment->num_segmentRecords; i++)
1371 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1373 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1375 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1376 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1378 Odr_oct *oct = fragment->u.notExternallyTagged;
1381 if (zebra_update_record(zh,
1382 0, /* record Type */
1386 (const char *) oct->buf, oct->len,
1395 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1396 const char *rec_buf,
1398 const char *recid_buf, size_t recid_len,
1400 /* 1 = insert. Fail it already exists */
1401 /* 2 = replace. Fail it does not exist */
1402 /* 3 = delete. Fail if does not exist */
1403 /* 4 = update. Insert/replace */
1410 ZEBRA_CHECK_HANDLE(zh);
1411 assert(action>0 && action <=4);
1414 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1416 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1418 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1419 "no record ID or empty record ID");
1423 memcpy (recid_z, recid_buf, recid_len);
1424 recid_z[recid_len] = 0;
1426 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1429 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1430 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1433 if (action == 1) /* fail if insert */
1435 if (zebra_end_trans(zh) != ZEBRA_OK)
1436 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1437 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1438 "Cannot insert record: already exist");
1442 memcpy (&sysno, rinfo+1, sizeof(sysno));
1446 if (action == 2 || action == 3) /* fail if delete or update */
1448 if (zebra_end_trans(zh) != ZEBRA_OK)
1449 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1450 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1451 "Cannot delete/update record: does not exist");
1454 action = 1; /* make it an insert (if it's an update).. */
1456 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1457 action == 3 ? 1 : 0 /* delete flag */,
1463 0, /* force update */
1464 1 /* allow update */
1466 if (res == ZEBRA_FAIL)
1468 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1469 "Unable to parse record");
1473 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1474 sizeof(sysno), &sysno);
1476 else if (action == 3)
1478 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1480 if (zebra_end_trans(zh) != ZEBRA_OK)
1482 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1488 int delete_w_handle(const char *info, void *handle)
1490 ZebraHandle zh = (ZebraHandle) handle;
1494 if (*info == sizeof(pos))
1496 memcpy (&pos, info+1, sizeof(pos));
1497 isamb_unlink(zh->reg->isamb, pos);
1502 static int delete_SU_handle(void *handle, int ord)
1504 ZebraHandle zh = (ZebraHandle) handle;
1508 ord_len = key_SU_encode (ord, ord_buf);
1509 ord_buf[ord_len] = '\0';
1511 assert (zh->reg->isamb);
1512 dict_delete_subtree(zh->reg->dict, ord_buf,
1513 zh, delete_w_handle);
1517 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1519 ZEBRA_RES ret = ZEBRA_OK;
1521 yaz_log(log_level, "zebra_drop_database %s", db);
1522 ZEBRA_CHECK_HANDLE(zh);
1524 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1526 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1531 if (zebraExplain_curDatabase (zh->reg->zei, db))
1533 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1538 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1539 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1540 0 /* handle */, 0 /* func */);
1541 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1542 zebraExplain_removeDatabase(zh->reg->zei, zh);
1543 zebra_remove_file_match(zh);
1548 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1549 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1550 "drop database only supported for isam:b");
1553 if (zebra_end_trans (zh) != ZEBRA_OK)
1555 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1561 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1563 yaz_log(log_level, "zebra_create_database %s", db);
1564 ZEBRA_CHECK_HANDLE(zh);
1567 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1569 if (zebra_begin_trans (zh, 1))
1572 /* announce database */
1573 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1574 /* explainDatabase */))
1576 if (zebra_end_trans (zh) != ZEBRA_OK)
1578 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1580 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1583 return zebra_end_trans (zh);
1586 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1587 const char *input_str, int input_len,
1588 char *output_str, int output_len)
1594 yaz_log(log_level, "zebra_string_norm ");
1596 if (!zh->reg->zebra_maps)
1598 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1599 input_str, input_len);
1602 if (wrbuf_len(wrbuf) >= output_len)
1604 if (wrbuf_len(wrbuf))
1605 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1606 output_str[wrbuf_len(wrbuf)] = '\0';
1607 return wrbuf_len(wrbuf);
1610 /** \brief set register state (state*.LCK)
1611 \param zh Zebra handle
1613 \param seqno sequence number
1616 d=writing to shadow(dirty)
1620 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1622 char state_fname[256];
1627 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1629 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1630 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1631 f = fopen (fname, "w");
1633 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1634 fprintf (f, "%c %d %ld\n", val, seqno, p);
1639 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1641 char state_fname[256];
1646 yaz_log(log_level, "zebra_get_state ");
1648 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1649 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1650 f = fopen (fname, "r");
1656 fscanf (f, "%c %d", val, seqno);
1662 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1664 return zebra_begin_trans(zh, 0);
1667 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1669 return zebra_end_trans(zh);
1672 static void read_res_for_transaction(ZebraHandle zh)
1674 const char *group = res_get(zh->res, "group");
1676 /* FIXME - do we still use groups ?? */
1678 zh->m_group = group;
1679 v = res_get_prefix(zh->res, "followLinks", group, "1");
1680 zh->m_follow_links = atoi(v);
1682 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1683 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1685 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1686 zh->m_store_keys = atoi(v);
1688 v = res_get_prefix(zh->res, "storeData", group, "1");
1689 zh->m_store_data = atoi(v);
1691 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1692 zh->m_explain_database = atoi(v);
1694 v = res_get_prefix(zh->res, "openRW", group, "1");
1695 zh->m_flag_rw = atoi(v);
1697 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1698 zh->m_file_verbose_limit = atoi(v);
1701 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1703 ZEBRA_CHECK_HANDLE(zh);
1704 zebra_select_default_database(zh);
1707 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1708 "zebra_begin_trans: no database selected");
1712 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1716 if (rw && !strchr(zh->user_perm, 'w'))
1720 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1731 const char *rval = 0;
1736 read_res_for_transaction(zh);
1739 if (zh->trans_no != 1)
1741 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1742 "zebra_begin_trans: no write trans within read");
1747 resultSetInvalidate (zh);
1748 zebra_register_close(zh->service, zh->reg);
1750 zh->trans_w_no = zh->trans_no;
1752 zh->records_inserted = 0;
1753 zh->records_updated = 0;
1754 zh->records_deleted = 0;
1755 zh->records_processed = 0;
1757 #if HAVE_SYS_TIMES_H
1761 if (zh->shadow_enable)
1762 rval = res_get (zh->res, "shadow");
1766 zebra_lock_r(zh->lock_normal);
1767 zebra_lock_w(zh->lock_shadow);
1771 zebra_lock_w(zh->lock_normal);
1772 zebra_lock_w(zh->lock_shadow);
1774 zebra_get_state (zh, &val, &seqno);
1777 /* either we didn't finish commit or shadow is dirty */
1778 zebra_unlock (zh->lock_shadow);
1779 zebra_unlock (zh->lock_normal);
1780 if (zebra_commit (zh))
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);
1798 zebra_set_state (zh, 'd', seqno);
1800 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1801 1, rval ? 1 : 0, zh->res,
1804 zh->reg->seqno = seqno;
1807 zebra_set_state (zh, 'o', seqno);
1809 zebra_unlock (zh->lock_shadow);
1810 zebra_unlock (zh->lock_normal);
1815 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1816 "zebra_begin_trans: cannot open register");
1817 yaz_log(YLOG_FATAL, "%s", zh->errString);
1820 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1830 if (zh->trans_no != 1)
1832 return zebra_flush_reg (zh);
1834 #if HAVE_SYS_TIMES_H
1840 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1843 if (!zh->lock_normal || !zh->lock_shadow)
1846 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1849 zebra_get_state (zh, &val, &seqno);
1855 else if (seqno != zh->reg->seqno)
1857 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1858 seqno, zh->reg->seqno);
1861 else if (zh->reg->last_val != val)
1863 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1864 val, zh->reg->last_val);
1871 zebra_lock_r (zh->lock_shadow);
1873 zebra_lock_r (zh->lock_normal);
1877 resultSetInvalidate (zh);
1878 zebra_register_close(zh->service, zh->reg);
1880 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1881 0, val == 'c' ? 1 : 0,
1882 zh->res, zh->path_reg);
1885 zebra_unlock (zh->lock_normal);
1886 zebra_unlock (zh->lock_shadow);
1888 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1891 zh->reg->last_val = val;
1892 zh->reg->seqno = seqno;
1894 read_res_for_transaction(zh);
1898 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1900 ZebraTransactionStatus dummy;
1902 yaz_log(log_level, "zebra_end_trans");
1903 ZEBRA_CHECK_HANDLE(zh);
1904 return zebra_end_transaction(zh, &dummy);
1907 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1913 ZEBRA_CHECK_HANDLE(zh);
1916 yaz_log(log_level, "zebra_end_transaction");
1918 status->processed = 0;
1919 status->inserted = 0;
1920 status->updated = 0;
1921 status->deleted = 0;
1925 if (!zh->res || !zh->reg)
1927 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1928 "zebra_end_trans: no open transaction");
1931 if (zh->trans_no != zh->trans_w_no)
1934 if (zh->trans_no != 0)
1937 /* release read lock */
1939 zebra_unlock (zh->lock_normal);
1940 zebra_unlock (zh->lock_shadow);
1943 { /* release write lock */
1947 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1948 rval = res_get (zh->res, "shadow");
1950 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1952 zebra_flush_reg (zh);
1954 resultSetInvalidate (zh);
1956 zebra_register_close(zh->service, zh->reg);
1959 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1960 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1961 zh->records_processed, zh->records_inserted,
1962 zh->records_updated, zh->records_deleted);
1964 status->processed = zh->records_processed;
1965 status->inserted = zh->records_inserted;
1966 status->updated = zh->records_updated;
1967 status->deleted = zh->records_deleted;
1969 zebra_get_state (zh, &val, &seqno);
1972 BFiles bfs = bfs_create (rval, zh->path_reg);
1973 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1974 bf_commitClean (bfs, rval);
1979 zebra_set_state (zh, 'o', seqno);
1981 zebra_unlock (zh->lock_shadow);
1982 zebra_unlock (zh->lock_normal);
1985 #if HAVE_SYS_TIMES_H
1987 yaz_log (log_level, "user/system: %ld/%ld",
1988 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1989 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1991 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1992 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1997 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
2001 yaz_log (log_level, "updating %s", path);
2003 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
2004 return zebra_update_file_match(zh, path);
2006 return zebra_update_from_path(zh, path);
2009 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2013 yaz_log (log_level, "deleting %s", path);
2014 return zebra_delete_from_path(zh, path);
2017 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2021 yaz_log(log_level, "zebra_repository_show");
2022 repositoryShow (zh, path);
2026 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2032 ZEBRA_RES res = ZEBRA_OK;
2036 zebra_select_default_database(zh);
2039 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2042 rval = res_get(zh->res, "shadow");
2045 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2049 zebra_lock_w(zh->lock_normal);
2050 zebra_lock_r(zh->lock_shadow);
2052 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2055 zebra_unlock(zh->lock_shadow);
2056 zebra_unlock(zh->lock_normal);
2059 zebra_get_state(zh, &val, &seqno);
2063 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2068 bf_cache (bfs, rval);
2069 if (bf_commitExists (bfs))
2072 zebra_set_state(zh, 'd', seqno);
2075 zebra_set_state(zh, 'c', seqno);
2077 yaz_log(YLOG_DEBUG, "commit start");
2078 if (bf_commitExec (bfs))
2081 if (res == ZEBRA_OK)
2084 zebra_set_state(zh, 'o', seqno);
2086 zebra_unlock(zh->lock_shadow);
2087 zebra_unlock(zh->lock_normal);
2089 zebra_lock_w(zh->lock_shadow);
2090 bf_commitClean(bfs, rval);
2091 zebra_unlock(zh->lock_shadow);
2095 zebra_unlock(zh->lock_shadow);
2096 zebra_unlock(zh->lock_normal);
2097 yaz_log(YLOG_WARN, "zebra_commit: failed");
2102 zebra_unlock(zh->lock_shadow);
2103 zebra_unlock(zh->lock_normal);
2104 yaz_log(log_level, "nothing to commit");
2111 ZEBRA_RES zebra_clean(ZebraHandle zh)
2113 yaz_log(log_level, "zebra_clean");
2114 ZEBRA_CHECK_HANDLE(zh);
2115 return zebra_commit_ex(zh, 1);
2118 ZEBRA_RES zebra_commit(ZebraHandle zh)
2120 yaz_log(log_level, "zebra_commit");
2121 ZEBRA_CHECK_HANDLE(zh);
2122 return zebra_commit_ex(zh, 0);
2126 ZEBRA_RES zebra_init(ZebraHandle zh)
2131 yaz_log(log_level, "zebra_init");
2133 ZEBRA_CHECK_HANDLE(zh);
2135 zebra_select_default_database(zh);
2138 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2139 "cannot select default database");
2142 rval = res_get (zh->res, "shadow");
2144 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2147 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2151 bf_cache (bfs, rval);
2155 zebra_set_state (zh, 'o', 0);
2159 ZEBRA_RES zebra_compact(ZebraHandle zh)
2163 yaz_log(log_level, "zebra_compact");
2164 ZEBRA_CHECK_HANDLE(zh);
2167 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2170 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2176 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2178 yaz_log(log_level, "zebra_result");
2181 *code = zh->errCode;
2182 *addinfo = zh->errString;
2186 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2187 *addinfo ="ZebraHandle is NULL";
2191 void zebra_shadow_enable(ZebraHandle zh, int value)
2194 yaz_log(log_level, "zebra_shadow_enable");
2195 zh->shadow_enable = value;
2198 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2200 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2201 ZEBRA_CHECK_HANDLE(zh);
2204 if (zh->iconv_to_utf8 != 0)
2205 yaz_iconv_close(zh->iconv_to_utf8);
2206 if (zh->iconv_from_utf8 != 0)
2207 yaz_iconv_close(zh->iconv_from_utf8);
2210 yaz_iconv_open ("UTF-8", encoding);
2211 if (zh->iconv_to_utf8 == 0)
2212 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2213 zh->iconv_from_utf8 =
2214 yaz_iconv_open (encoding, "UTF-8");
2215 if (zh->iconv_to_utf8 == 0)
2216 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2221 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2223 yaz_log(log_level, "zebra_record_encoding");
2224 ZEBRA_CHECK_HANDLE(zh);
2225 xfree(zh->record_encoding);
2226 zh->record_encoding = 0;
2228 zh->record_encoding = xstrdup (encoding);
2232 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2236 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2238 res_set(zh->res, name, value);
2241 const char *zebra_get_resource(ZebraHandle zh,
2242 const char *name, const char *defaultvalue)
2247 v = res_get_def (zh->res, name, (char *)defaultvalue);
2248 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2252 /* moved from zebra_api_ext.c by pop */
2253 /* FIXME: Should this really be public??? -Heikki */
2255 int zebra_trans_no (ZebraHandle zh)
2257 yaz_log(log_level, "zebra_trans_no");
2259 return zh->trans_no;
2262 int zebra_get_shadow_enable (ZebraHandle zh)
2264 yaz_log(log_level, "zebra_get_shadow_enable");
2266 return zh->shadow_enable;
2269 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2271 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2273 zh->shadow_enable = value;
2276 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2277 const char *buf, int buf_size)
2279 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2282 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2283 const char *recordType,
2284 zint *sysno, const char *match,
2286 const char *buf, int buf_size, int force_update)
2292 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2295 buf_size = strlen(buf);
2297 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2299 res = zebra_buffer_extract_record(zh, buf, buf_size,
2300 0, /* delete_flag */
2306 0); /* allow_update */
2307 if (zebra_end_trans(zh) != ZEBRA_OK)
2309 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2315 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2316 const char *recordType,
2317 zint *sysno, const char *match,
2319 const char *buf, int buf_size,
2324 ZEBRA_CHECK_HANDLE(zh);
2328 yaz_log(log_level, "zebra_update_record");
2330 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2332 if (buf_size < 1) buf_size = strlen(buf);
2334 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2336 res = zebra_buffer_extract_record(zh, buf, buf_size,
2337 0, /* delete_flag */
2343 1); /* allow_update */
2344 if (zebra_end_trans(zh) != ZEBRA_OK)
2346 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2352 ZEBRA_RES zebra_delete_record(ZebraHandle zh,
2353 const char *recordType,
2354 zint *sysno, const char *match,
2356 const char *buf, int buf_size,
2361 ZEBRA_CHECK_HANDLE(zh);
2364 yaz_log(log_level, "zebra_delete_record");
2366 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2368 if (buf_size < 1) buf_size = strlen(buf);
2370 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2372 res = zebra_buffer_extract_record(zh, buf, buf_size,
2373 1, /* delete_flag */
2379 1); /* allow_update */
2380 if (zebra_end_trans(zh) != ZEBRA_OK)
2382 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2388 /* ---------------------------------------------------------------------------
2392 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2393 const char *setname, zint *hits)
2396 ZEBRA_RES res = ZEBRA_OK;
2401 ZEBRA_CHECK_HANDLE(zh);
2403 odr = odr_createmem(ODR_ENCODE);
2408 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2410 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2414 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2415 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2419 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2423 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2431 /* ---------------------------------------------------------------------------
2432 Sort - a simplified interface, with optional read locks.
2434 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2435 const char *sort_spec,
2436 const char *output_setname,
2437 const char **input_setnames)
2439 int num_input_setnames = 0;
2440 int sort_status = 0;
2441 Z_SortKeySpecList *sort_sequence;
2443 ZEBRA_CHECK_HANDLE(zh);
2446 assert(output_setname);
2447 assert(input_setnames);
2448 sort_sequence = yaz_sort_spec (stream, sort_spec);
2449 yaz_log(log_level, "sort (FIXME) ");
2452 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2453 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2457 /* we can do this, since the perl typemap code for char** will
2458 put a NULL at the end of list */
2459 while (input_setnames[num_input_setnames]) num_input_setnames++;
2461 if (zebra_begin_read (zh))
2464 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2465 output_setname, sort_sequence, &sort_status);
2471 /* ---------------------------------------------------------------------------
2472 Get BFS for Zebra system (to make alternative storage methods)
2474 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2477 return zh->reg->bfs;
2482 /* ---------------------------------------------------------------------------
2483 Set limit for search/scan
2485 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2487 ZEBRA_CHECK_HANDLE(zh);
2488 zebra_limit_destroy(zh->m_limit);
2489 zh->m_limit = zebra_limit_create(complement_flag, ids);
2494 Set Error code + addinfo
2496 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2501 nmem_reset(zh->nmem_error);
2502 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2505 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2508 sprintf(vstr, ZINT_FORMAT, i);
2511 nmem_reset(zh->nmem_error);
2512 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2515 void zebra_lock_prefix(Res res, char *path)
2517 const char *lock_dir = res_get_def (res, "lockDir", "");
2519 strcpy(path, lock_dir);
2520 if (*path && path[strlen(path)-1] != '/')
2527 * indent-tabs-mode: nil
2529 * vim: shiftwidth=4 tabstop=8 expandtab