1 /* $Id: zebraapi.c,v 1.240 2006-12-22 12:14:25 adam Exp $
2 Copyright (C) 1995-2006
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 = zebra_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 zebra_timing_stop(zs->timing);
600 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
601 zebra_timing_get_real(zs->timing),
602 zebra_timing_get_user(zs->timing),
603 zebra_timing_get_sys(zs->timing));
606 zebra_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(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1019 const char *setname, zint *hits)
1023 ZEBRA_CHECK_HANDLE(zh);
1029 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);
1043 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1044 const char *setname,
1045 Z_RecordComposition *comp,
1046 oid_value input_format, int num_recs,
1047 ZebraRetrievalRecord *recs)
1049 ZebraMetaRecord *poset;
1051 ZEBRA_RES ret = ZEBRA_OK;
1054 ZEBRA_CHECK_HANDLE(zh);
1060 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1064 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1069 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1072 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1073 for (i = 0; i<num_recs; i++)
1074 pos_array[i] = recs[i].position;
1075 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1078 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1079 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1085 for (i = 0; i<num_recs; i++)
1089 recs[i].errCode = 0;
1090 recs[i].format = VAL_SUTRS;
1091 recs[i].len = strlen(poset[i].term);
1092 recs[i].buf = poset[i].term;
1093 recs[i].base = poset[i].db;
1095 else if (poset[i].sysno)
1099 zebra_snippets *hit_snippet = zebra_snippets_create();
1101 /* we disable hit snippets for now. It does not work well
1102 and it slows retrieval down a lot */
1104 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1108 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1110 stream, input_format, comp,
1111 &recs[i].format, &buf, &len,
1112 &recs[i].base, &recs[i].errString);
1117 recs[i].buf = (char*) odr_malloc(stream, len);
1118 memcpy(recs[i].buf, buf, len);
1122 recs[i].score = poset[i].score;
1123 recs[i].sysno = poset[i].sysno;
1124 zebra_snippets_destroy(hit_snippet);
1128 /* only need to set it once */
1129 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1131 zebra_setError_zint(zh,
1132 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1137 recs[i].buf = 0; /* no record and no error issued */
1139 recs[i].errCode = 0;
1140 recs[i].format = VAL_NONE;
1144 zebra_meta_records_destroy(zh, poset, num_recs);
1146 zebra_end_read (zh);
1151 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1153 int *num_entries, ZebraScanEntry **entries,
1155 const char *setname)
1157 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1158 Z_AttributesPlusTerm *zapt;
1162 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1165 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1168 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1169 position, num_entries, entries, is_partial,
1171 yaz_pqf_destroy (pqf_parser);
1175 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1176 oid_value attributeset,
1178 int *num_entries, ZebraScanEntry **entries,
1180 const char *setname)
1183 RSET limit_rset = 0;
1185 ZEBRA_CHECK_HANDLE(zh);
1190 assert(num_entries);
1193 yaz_log(log_level, "zebra_scan");
1195 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1203 limit_rset = resultSetRef(zh, setname);
1207 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1209 zebra_end_read (zh);
1213 res = rpn_scan(zh, stream, zapt, attributeset,
1214 zh->num_basenames, zh->basenames, position,
1215 num_entries, entries, is_partial, limit_rset);
1220 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1221 int num_input_setnames, const char **input_setnames,
1222 const char *output_setname,
1223 Z_SortKeySpecList *sort_sequence,
1227 ZEBRA_CHECK_HANDLE(zh);
1229 assert(num_input_setnames>0);
1230 assert(input_setnames);
1231 assert(sort_sequence);
1232 assert(sort_status);
1233 yaz_log(log_level, "zebra_sort");
1235 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1237 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1238 output_setname, sort_sequence, sort_status);
1243 int zebra_deleteResultSet(ZebraHandle zh, int function,
1244 int num_setnames, char **setnames,
1250 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1252 if (zebra_begin_read(zh))
1253 return Z_DeleteStatus_systemProblemAtTarget;
1256 case Z_DeleteResultSetRequest_list:
1257 assert(num_setnames>0);
1259 resultSetDestroy (zh, num_setnames, setnames, statuses);
1261 case Z_DeleteResultSetRequest_all:
1262 resultSetDestroy (zh, -1, 0, statuses);
1265 zebra_end_read (zh);
1266 status = Z_DeleteStatus_success;
1267 for (i = 0; i<num_setnames; i++)
1268 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1269 status = statuses[i];
1273 int zebra_errCode (ZebraHandle zh)
1277 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1280 yaz_log(log_level, "zebra_errCode: o");
1284 const char *zebra_errString (ZebraHandle zh)
1288 e= diagbib1_str (zh->errCode);
1289 yaz_log(log_level, "zebra_errString: %s",e);
1293 char *zebra_errAdd (ZebraHandle zh)
1298 yaz_log(log_level, "zebra_errAdd: %s",a);
1302 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1305 const char *astring;
1309 ZEBRA_CHECK_HANDLE(zh);
1313 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1314 p = res_get(zs->global_res, u);
1315 xfree(zh->user_perm);
1316 zh->user_perm = xstrdup(p ? p : "r");
1318 /* Determine database access list */
1319 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1321 zh->dbaccesslist = xstrdup(astring);
1323 zh->dbaccesslist = 0;
1325 /* users that don't require a password .. */
1326 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1329 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1334 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1335 const char *record_type)
1337 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1338 database, record_type);
1339 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1341 return zebra_begin_trans(zh, 1);
1344 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1346 ZEBRA_CHECK_HANDLE(zh);
1347 yaz_log(log_level, "zebra_admin_import_end");
1348 return zebra_end_trans(zh);
1351 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1353 ZEBRA_RES res = ZEBRA_OK;
1356 ZEBRA_CHECK_HANDLE(zh);
1357 yaz_log(log_level, "zebra_admin_import_segment");
1359 for (i = 0; i<segment->num_segmentRecords; i++)
1361 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1363 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1365 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1366 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1368 Odr_oct *oct = fragment->u.notExternallyTagged;
1371 if (zebra_update_record(zh,
1372 0, /* record Type */
1376 (const char *) oct->buf, oct->len,
1385 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1386 const char *rec_buf,
1388 const char *recid_buf, size_t recid_len,
1390 /* 1 = insert. Fail it already exists */
1391 /* 2 = replace. Fail it does not exist */
1392 /* 3 = delete. Fail if does not exist */
1393 /* 4 = update. Insert/replace */
1400 ZEBRA_CHECK_HANDLE(zh);
1401 assert(action>0 && action <=4);
1404 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1406 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1408 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1409 "no record ID or empty record ID");
1413 memcpy (recid_z, recid_buf, recid_len);
1414 recid_z[recid_len] = 0;
1416 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1419 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1420 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1423 if (action == 1) /* fail if insert */
1425 if (zebra_end_trans(zh) != ZEBRA_OK)
1426 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1427 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1428 "Cannot insert record: already exist");
1432 memcpy (&sysno, rinfo+1, sizeof(sysno));
1436 if (action == 2 || action == 3) /* fail if delete or update */
1438 if (zebra_end_trans(zh) != ZEBRA_OK)
1439 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1440 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1441 "Cannot delete/update record: does not exist");
1444 action = 1; /* make it an insert (if it's an update).. */
1446 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1447 action == 3 ? 1 : 0 /* delete flag */,
1453 0, /* force update */
1454 1 /* allow update */
1456 if (res == ZEBRA_FAIL)
1458 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1459 "Unable to parse record");
1463 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1464 sizeof(sysno), &sysno);
1466 else if (action == 3)
1468 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1470 if (zebra_end_trans(zh) != ZEBRA_OK)
1472 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1478 int delete_w_handle(const char *info, void *handle)
1480 ZebraHandle zh = (ZebraHandle) handle;
1484 if (*info == sizeof(pos))
1486 memcpy (&pos, info+1, sizeof(pos));
1487 isamb_unlink(zh->reg->isamb, pos);
1492 static int delete_SU_handle(void *handle, int ord)
1494 ZebraHandle zh = (ZebraHandle) handle;
1498 ord_len = key_SU_encode (ord, ord_buf);
1499 ord_buf[ord_len] = '\0';
1501 assert (zh->reg->isamb);
1502 dict_delete_subtree(zh->reg->dict, ord_buf,
1503 zh, delete_w_handle);
1507 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1509 ZEBRA_RES ret = ZEBRA_OK;
1511 yaz_log(log_level, "zebra_drop_database %s", db);
1512 ZEBRA_CHECK_HANDLE(zh);
1514 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1516 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1521 if (zebraExplain_curDatabase (zh->reg->zei, db))
1523 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1528 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1529 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1530 0 /* handle */, 0 /* func */);
1531 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1532 zebraExplain_removeDatabase(zh->reg->zei, zh);
1533 zebra_remove_file_match(zh);
1538 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1539 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1540 "drop database only supported for isam:b");
1543 if (zebra_end_trans (zh) != ZEBRA_OK)
1545 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1551 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1553 yaz_log(log_level, "zebra_create_database %s", db);
1554 ZEBRA_CHECK_HANDLE(zh);
1557 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1559 if (zebra_begin_trans (zh, 1))
1562 /* announce database */
1563 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1564 /* explainDatabase */))
1566 if (zebra_end_trans (zh) != ZEBRA_OK)
1568 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1570 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1573 return zebra_end_trans (zh);
1576 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1577 const char *input_str, int input_len,
1578 char *output_str, int output_len)
1584 yaz_log(log_level, "zebra_string_norm ");
1586 if (!zh->reg->zebra_maps)
1588 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1589 input_str, input_len);
1592 if (wrbuf_len(wrbuf) >= output_len)
1594 if (wrbuf_len(wrbuf))
1595 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1596 output_str[wrbuf_len(wrbuf)] = '\0';
1597 return wrbuf_len(wrbuf);
1600 /** \brief set register state (state*.LCK)
1601 \param zh Zebra handle
1603 \param seqno sequence number
1606 d=writing to shadow(dirty)
1610 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1612 char state_fname[256];
1617 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1619 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1620 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1621 f = fopen (fname, "w");
1623 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1624 fprintf (f, "%c %d %ld\n", val, seqno, p);
1629 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1631 char state_fname[256];
1636 yaz_log(log_level, "zebra_get_state ");
1638 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1639 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1640 f = fopen (fname, "r");
1646 fscanf (f, "%c %d", val, seqno);
1652 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1654 return zebra_begin_trans(zh, 0);
1657 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1659 return zebra_end_trans(zh);
1662 static void read_res_for_transaction(ZebraHandle zh)
1664 const char *group = res_get(zh->res, "group");
1666 /* FIXME - do we still use groups ?? */
1668 zh->m_group = group;
1669 v = res_get_prefix(zh->res, "followLinks", group, "1");
1670 zh->m_follow_links = atoi(v);
1672 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1673 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1675 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1676 zh->m_store_keys = atoi(v);
1678 v = res_get_prefix(zh->res, "storeData", group, "1");
1679 zh->m_store_data = atoi(v);
1681 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1682 zh->m_explain_database = atoi(v);
1684 v = res_get_prefix(zh->res, "openRW", group, "1");
1685 zh->m_flag_rw = atoi(v);
1687 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1688 zh->m_file_verbose_limit = atoi(v);
1691 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1693 ZEBRA_CHECK_HANDLE(zh);
1694 zebra_select_default_database(zh);
1697 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1698 "zebra_begin_trans: no database selected");
1702 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1706 if (rw && !strchr(zh->user_perm, 'w'))
1710 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1721 const char *rval = 0;
1726 read_res_for_transaction(zh);
1729 if (zh->trans_no != 1)
1731 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1732 "zebra_begin_trans: no write trans within read");
1737 resultSetInvalidate (zh);
1738 zebra_register_close(zh->service, zh->reg);
1740 zh->trans_w_no = zh->trans_no;
1742 zh->records_inserted = 0;
1743 zh->records_updated = 0;
1744 zh->records_deleted = 0;
1745 zh->records_processed = 0;
1747 #if HAVE_SYS_TIMES_H
1751 if (zh->shadow_enable)
1752 rval = res_get (zh->res, "shadow");
1756 zebra_lock_r(zh->lock_normal);
1757 zebra_lock_w(zh->lock_shadow);
1761 zebra_lock_w(zh->lock_normal);
1762 zebra_lock_w(zh->lock_shadow);
1764 zebra_get_state (zh, &val, &seqno);
1767 /* either we didn't finish commit or shadow is dirty */
1768 zebra_unlock (zh->lock_shadow);
1769 zebra_unlock (zh->lock_normal);
1770 if (zebra_commit (zh))
1778 zebra_lock_r(zh->lock_normal);
1779 zebra_lock_w(zh->lock_shadow);
1783 zebra_lock_w(zh->lock_normal);
1784 zebra_lock_w(zh->lock_shadow);
1788 zebra_set_state (zh, 'd', seqno);
1790 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1791 1, rval ? 1 : 0, zh->res,
1794 zh->reg->seqno = seqno;
1797 zebra_set_state (zh, 'o', seqno);
1799 zebra_unlock (zh->lock_shadow);
1800 zebra_unlock (zh->lock_normal);
1805 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1806 "zebra_begin_trans: cannot open register");
1807 yaz_log(YLOG_FATAL, "%s", zh->errString);
1810 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1820 if (zh->trans_no != 1)
1822 return zebra_flush_reg (zh);
1824 #if HAVE_SYS_TIMES_H
1830 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1833 if (!zh->lock_normal || !zh->lock_shadow)
1836 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1839 zebra_get_state (zh, &val, &seqno);
1845 else if (seqno != zh->reg->seqno)
1847 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1848 seqno, zh->reg->seqno);
1851 else if (zh->reg->last_val != val)
1853 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1854 val, zh->reg->last_val);
1861 zebra_lock_r (zh->lock_shadow);
1863 zebra_lock_r (zh->lock_normal);
1867 resultSetInvalidate (zh);
1868 zebra_register_close(zh->service, zh->reg);
1870 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1871 0, val == 'c' ? 1 : 0,
1872 zh->res, zh->path_reg);
1875 zebra_unlock (zh->lock_normal);
1876 zebra_unlock (zh->lock_shadow);
1878 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1881 zh->reg->last_val = val;
1882 zh->reg->seqno = seqno;
1884 read_res_for_transaction(zh);
1888 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1890 ZebraTransactionStatus dummy;
1892 yaz_log(log_level, "zebra_end_trans");
1893 ZEBRA_CHECK_HANDLE(zh);
1894 return zebra_end_transaction(zh, &dummy);
1897 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1903 ZEBRA_CHECK_HANDLE(zh);
1906 yaz_log(log_level, "zebra_end_transaction");
1908 status->processed = 0;
1909 status->inserted = 0;
1910 status->updated = 0;
1911 status->deleted = 0;
1915 if (!zh->res || !zh->reg)
1917 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1918 "zebra_end_trans: no open transaction");
1921 if (zh->trans_no != zh->trans_w_no)
1924 if (zh->trans_no != 0)
1927 /* release read lock */
1929 zebra_unlock (zh->lock_normal);
1930 zebra_unlock (zh->lock_shadow);
1933 { /* release write lock */
1937 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1938 rval = res_get (zh->res, "shadow");
1940 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1942 zebra_flush_reg (zh);
1944 resultSetInvalidate (zh);
1946 zebra_register_close(zh->service, zh->reg);
1949 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1950 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1951 zh->records_processed, zh->records_inserted,
1952 zh->records_updated, zh->records_deleted);
1954 status->processed = zh->records_processed;
1955 status->inserted = zh->records_inserted;
1956 status->updated = zh->records_updated;
1957 status->deleted = zh->records_deleted;
1959 zebra_get_state (zh, &val, &seqno);
1962 BFiles bfs = bfs_create (rval, zh->path_reg);
1963 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1964 bf_commitClean (bfs, rval);
1969 zebra_set_state (zh, 'o', seqno);
1971 zebra_unlock (zh->lock_shadow);
1972 zebra_unlock (zh->lock_normal);
1975 #if HAVE_SYS_TIMES_H
1977 yaz_log (log_level, "user/system: %ld/%ld",
1978 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1979 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1981 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1982 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1987 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1991 yaz_log (log_level, "updating %s", path);
1993 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1994 return zebra_update_file_match(zh, path);
1996 return zebra_update_from_path(zh, path);
1999 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2003 yaz_log (log_level, "deleting %s", path);
2004 return zebra_delete_from_path(zh, path);
2007 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2011 yaz_log(log_level, "zebra_repository_show");
2012 repositoryShow (zh, path);
2016 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2022 ZEBRA_RES res = ZEBRA_OK;
2026 zebra_select_default_database(zh);
2029 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2032 rval = res_get(zh->res, "shadow");
2035 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2039 zebra_lock_w(zh->lock_normal);
2040 zebra_lock_r(zh->lock_shadow);
2042 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2045 zebra_unlock(zh->lock_shadow);
2046 zebra_unlock(zh->lock_normal);
2049 zebra_get_state(zh, &val, &seqno);
2053 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2058 bf_cache (bfs, rval);
2059 if (bf_commitExists (bfs))
2062 zebra_set_state(zh, 'd', seqno);
2065 zebra_set_state(zh, 'c', seqno);
2067 yaz_log(YLOG_DEBUG, "commit start");
2068 if (bf_commitExec (bfs))
2071 if (res == ZEBRA_OK)
2074 zebra_set_state(zh, 'o', seqno);
2076 zebra_unlock(zh->lock_shadow);
2077 zebra_unlock(zh->lock_normal);
2079 zebra_lock_w(zh->lock_shadow);
2080 bf_commitClean(bfs, rval);
2081 zebra_unlock(zh->lock_shadow);
2085 zebra_unlock(zh->lock_shadow);
2086 zebra_unlock(zh->lock_normal);
2087 yaz_log(YLOG_WARN, "zebra_commit: failed");
2092 zebra_unlock(zh->lock_shadow);
2093 zebra_unlock(zh->lock_normal);
2094 yaz_log(log_level, "nothing to commit");
2101 ZEBRA_RES zebra_clean(ZebraHandle zh)
2103 yaz_log(log_level, "zebra_clean");
2104 ZEBRA_CHECK_HANDLE(zh);
2105 return zebra_commit_ex(zh, 1);
2108 ZEBRA_RES zebra_commit(ZebraHandle zh)
2110 yaz_log(log_level, "zebra_commit");
2111 ZEBRA_CHECK_HANDLE(zh);
2112 return zebra_commit_ex(zh, 0);
2116 ZEBRA_RES zebra_init(ZebraHandle zh)
2121 yaz_log(log_level, "zebra_init");
2123 ZEBRA_CHECK_HANDLE(zh);
2125 zebra_select_default_database(zh);
2128 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2129 "cannot select default database");
2132 rval = res_get (zh->res, "shadow");
2134 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2137 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2141 bf_cache (bfs, rval);
2145 zebra_set_state (zh, 'o', 0);
2149 ZEBRA_RES zebra_compact(ZebraHandle zh)
2153 yaz_log(log_level, "zebra_compact");
2154 ZEBRA_CHECK_HANDLE(zh);
2157 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2160 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2166 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2168 yaz_log(log_level, "zebra_result");
2171 *code = zh->errCode;
2172 *addinfo = zh->errString;
2176 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2177 *addinfo ="ZebraHandle is NULL";
2181 void zebra_shadow_enable(ZebraHandle zh, int value)
2184 yaz_log(log_level, "zebra_shadow_enable");
2185 zh->shadow_enable = value;
2188 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2190 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2191 ZEBRA_CHECK_HANDLE(zh);
2194 if (zh->iconv_to_utf8 != 0)
2195 yaz_iconv_close(zh->iconv_to_utf8);
2196 if (zh->iconv_from_utf8 != 0)
2197 yaz_iconv_close(zh->iconv_from_utf8);
2200 yaz_iconv_open ("UTF-8", encoding);
2201 if (zh->iconv_to_utf8 == 0)
2202 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2203 zh->iconv_from_utf8 =
2204 yaz_iconv_open (encoding, "UTF-8");
2205 if (zh->iconv_to_utf8 == 0)
2206 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2211 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2213 yaz_log(log_level, "zebra_record_encoding");
2214 ZEBRA_CHECK_HANDLE(zh);
2215 xfree(zh->record_encoding);
2216 zh->record_encoding = 0;
2218 zh->record_encoding = xstrdup (encoding);
2222 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2226 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2228 res_set(zh->res, name, value);
2231 const char *zebra_get_resource(ZebraHandle zh,
2232 const char *name, const char *defaultvalue)
2237 v = res_get_def (zh->res, name, (char *)defaultvalue);
2238 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2242 /* moved from zebra_api_ext.c by pop */
2243 /* FIXME: Should this really be public??? -Heikki */
2245 int zebra_trans_no (ZebraHandle zh)
2247 yaz_log(log_level, "zebra_trans_no");
2249 return zh->trans_no;
2252 int zebra_get_shadow_enable (ZebraHandle zh)
2254 yaz_log(log_level, "zebra_get_shadow_enable");
2256 return zh->shadow_enable;
2259 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2261 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2263 zh->shadow_enable = value;
2266 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2267 const char *buf, int buf_size)
2269 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2272 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2273 const char *recordType,
2274 zint *sysno, const char *match,
2276 const char *buf, int buf_size, int force_update)
2282 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2285 buf_size = strlen(buf);
2287 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2289 res = zebra_buffer_extract_record(zh, buf, buf_size,
2290 0, /* delete_flag */
2296 0); /* allow_update */
2297 if (zebra_end_trans(zh) != ZEBRA_OK)
2299 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2305 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2306 const char *recordType,
2307 zint *sysno, const char *match,
2309 const char *buf, int buf_size,
2314 ZEBRA_CHECK_HANDLE(zh);
2318 yaz_log(log_level, "zebra_update_record");
2320 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2322 if (buf_size < 1) buf_size = strlen(buf);
2324 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2326 res = zebra_buffer_extract_record(zh, buf, buf_size,
2327 0, /* delete_flag */
2333 1); /* allow_update */
2334 if (zebra_end_trans(zh) != ZEBRA_OK)
2336 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2342 ZEBRA_RES zebra_delete_record(ZebraHandle zh,
2343 const char *recordType,
2344 zint *sysno, const char *match,
2346 const char *buf, int buf_size,
2351 ZEBRA_CHECK_HANDLE(zh);
2354 yaz_log(log_level, "zebra_delete_record");
2356 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2358 if (buf_size < 1) buf_size = strlen(buf);
2360 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2362 res = zebra_buffer_extract_record(zh, buf, buf_size,
2363 1, /* delete_flag */
2369 1); /* allow_update */
2370 if (zebra_end_trans(zh) != ZEBRA_OK)
2372 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2378 /* ---------------------------------------------------------------------------
2382 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2383 const char *setname, zint *hits)
2386 ZEBRA_RES res = ZEBRA_OK;
2390 ZEBRA_CHECK_HANDLE(zh);
2392 odr = odr_createmem(ODR_ENCODE);
2397 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2399 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2403 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2404 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2408 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2412 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2420 /* ---------------------------------------------------------------------------
2421 Sort - a simplified interface, with optional read locks.
2423 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2424 const char *sort_spec,
2425 const char *output_setname,
2426 const char **input_setnames)
2428 int num_input_setnames = 0;
2429 int sort_status = 0;
2430 Z_SortKeySpecList *sort_sequence;
2432 ZEBRA_CHECK_HANDLE(zh);
2435 assert(output_setname);
2436 assert(input_setnames);
2437 sort_sequence = yaz_sort_spec (stream, sort_spec);
2438 yaz_log(log_level, "sort (FIXME) ");
2441 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2442 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2446 /* we can do this, since the perl typemap code for char** will
2447 put a NULL at the end of list */
2448 while (input_setnames[num_input_setnames]) num_input_setnames++;
2450 if (zebra_begin_read (zh))
2453 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2454 output_setname, sort_sequence, &sort_status);
2460 /* ---------------------------------------------------------------------------
2461 Get BFS for Zebra system (to make alternative storage methods)
2463 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2466 return zh->reg->bfs;
2471 /* ---------------------------------------------------------------------------
2472 Set limit for search/scan
2474 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2476 ZEBRA_CHECK_HANDLE(zh);
2477 zebra_limit_destroy(zh->m_limit);
2478 zh->m_limit = zebra_limit_create(complement_flag, ids);
2483 Set Error code + addinfo
2485 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2490 nmem_reset(zh->nmem_error);
2491 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2494 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2497 sprintf(vstr, ZINT_FORMAT, i);
2500 nmem_reset(zh->nmem_error);
2501 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2504 void zebra_lock_prefix(Res res, char *path)
2506 const char *lock_dir = res_get_def (res, "lockDir", "");
2508 strcpy(path, lock_dir);
2509 if (*path && path[strlen(path)-1] != '/')
2516 * indent-tabs-mode: nil
2518 * vim: shiftwidth=4 tabstop=8 expandtab