1 /* $Id: zebraapi.c,v 1.244 2007-01-16 15:31:23 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 void zebra_set_partial_result(ZebraHandle zh)
1020 zh->partial_result = 1;
1023 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1024 const char *setname, zint *hits,
1025 int *estimated_hit_count,
1026 int *partial_resultset)
1030 ZEBRA_CHECK_HANDLE(zh);
1036 yaz_log(log_level, "zebra_search_rpn");
1038 zh->partial_result = 0;
1040 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1043 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1044 zh->num_basenames, zh->basenames, setname,
1045 hits, estimated_hit_count);
1047 *partial_resultset = zh->partial_result;
1052 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1053 const char *setname, zint *hits)
1055 int estimated_hit_count;
1056 int partial_resultset;
1057 return zebra_search_RPN_x(zh, o, query, setname, hits,
1058 &estimated_hit_count,
1059 &partial_resultset);
1062 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1063 const char *setname,
1064 Z_RecordComposition *comp,
1065 oid_value input_format, int num_recs,
1066 ZebraRetrievalRecord *recs)
1068 ZebraMetaRecord *poset;
1070 ZEBRA_RES ret = ZEBRA_OK;
1073 ZEBRA_CHECK_HANDLE(zh);
1079 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1083 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1088 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1091 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1092 for (i = 0; i<num_recs; i++)
1093 pos_array[i] = recs[i].position;
1094 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1097 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1098 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1104 for (i = 0; i<num_recs; i++)
1108 recs[i].errCode = 0;
1109 recs[i].format = VAL_SUTRS;
1110 recs[i].len = strlen(poset[i].term);
1111 recs[i].buf = poset[i].term;
1112 recs[i].base = poset[i].db;
1114 else if (poset[i].sysno)
1118 zebra_snippets *hit_snippet = zebra_snippets_create();
1120 /* we disable hit snippets for now. It does not work well
1121 and it slows retrieval down a lot */
1123 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1127 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1129 stream, input_format, comp,
1130 &recs[i].format, &buf, &len,
1131 &recs[i].base, &recs[i].errString);
1136 recs[i].buf = (char*) odr_malloc(stream, len);
1137 memcpy(recs[i].buf, buf, len);
1141 recs[i].score = poset[i].score;
1142 recs[i].sysno = poset[i].sysno;
1143 zebra_snippets_destroy(hit_snippet);
1147 /* only need to set it once */
1148 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1150 zebra_setError_zint(zh,
1151 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1156 recs[i].buf = 0; /* no record and no error issued */
1158 recs[i].errCode = 0;
1159 recs[i].format = VAL_NONE;
1163 zebra_meta_records_destroy(zh, poset, num_recs);
1165 zebra_end_read (zh);
1170 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1172 int *num_entries, ZebraScanEntry **entries,
1174 const char *setname)
1176 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1177 Z_AttributesPlusTerm *zapt;
1181 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1184 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1187 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1188 position, num_entries, entries, is_partial,
1190 yaz_pqf_destroy (pqf_parser);
1194 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1195 oid_value attributeset,
1197 int *num_entries, ZebraScanEntry **entries,
1199 const char *setname)
1202 RSET limit_rset = 0;
1204 ZEBRA_CHECK_HANDLE(zh);
1209 assert(num_entries);
1212 yaz_log(log_level, "zebra_scan");
1214 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1222 limit_rset = resultSetRef(zh, setname);
1226 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1228 zebra_end_read (zh);
1232 res = rpn_scan(zh, stream, zapt, attributeset,
1233 zh->num_basenames, zh->basenames, position,
1234 num_entries, entries, is_partial, limit_rset);
1239 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1240 int num_input_setnames, const char **input_setnames,
1241 const char *output_setname,
1242 Z_SortKeySpecList *sort_sequence,
1246 ZEBRA_CHECK_HANDLE(zh);
1248 assert(num_input_setnames>0);
1249 assert(input_setnames);
1250 assert(sort_sequence);
1251 assert(sort_status);
1252 yaz_log(log_level, "zebra_sort");
1254 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1256 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1257 output_setname, sort_sequence, sort_status);
1262 int zebra_deleteResultSet(ZebraHandle zh, int function,
1263 int num_setnames, char **setnames,
1269 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1271 if (zebra_begin_read(zh))
1272 return Z_DeleteStatus_systemProblemAtTarget;
1275 case Z_DeleteResultSetRequest_list:
1276 assert(num_setnames>0);
1278 resultSetDestroy (zh, num_setnames, setnames, statuses);
1280 case Z_DeleteResultSetRequest_all:
1281 resultSetDestroy (zh, -1, 0, statuses);
1284 zebra_end_read (zh);
1285 status = Z_DeleteStatus_success;
1286 for (i = 0; i<num_setnames; i++)
1287 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1288 status = statuses[i];
1292 int zebra_errCode (ZebraHandle zh)
1296 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1299 yaz_log(log_level, "zebra_errCode: o");
1303 const char *zebra_errString (ZebraHandle zh)
1307 e= diagbib1_str (zh->errCode);
1308 yaz_log(log_level, "zebra_errString: %s",e);
1312 char *zebra_errAdd (ZebraHandle zh)
1317 yaz_log(log_level, "zebra_errAdd: %s",a);
1321 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1324 const char *astring;
1328 ZEBRA_CHECK_HANDLE(zh);
1332 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1333 p = res_get(zs->global_res, u);
1334 xfree(zh->user_perm);
1335 zh->user_perm = xstrdup(p ? p : "r");
1337 /* Determine database access list */
1338 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1340 zh->dbaccesslist = xstrdup(astring);
1342 zh->dbaccesslist = 0;
1344 /* users that don't require a password .. */
1345 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1348 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1353 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1354 const char *record_type)
1356 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1357 database, record_type);
1358 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1360 return zebra_begin_trans(zh, 1);
1363 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1365 ZEBRA_CHECK_HANDLE(zh);
1366 yaz_log(log_level, "zebra_admin_import_end");
1367 return zebra_end_trans(zh);
1370 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1372 ZEBRA_RES res = ZEBRA_OK;
1375 ZEBRA_CHECK_HANDLE(zh);
1376 yaz_log(log_level, "zebra_admin_import_segment");
1378 for (i = 0; i<segment->num_segmentRecords; i++)
1380 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1382 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1384 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1385 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1387 Odr_oct *oct = fragment->u.notExternallyTagged;
1390 if (zebra_update_record(zh,
1391 0, /* record Type */
1395 (const char *) oct->buf, oct->len,
1404 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1405 const char *rec_buf,
1407 const char *recid_buf, size_t recid_len,
1409 /* 1 = insert. Fail it already exists */
1410 /* 2 = replace. Fail it does not exist */
1411 /* 3 = delete. Fail if does not exist */
1412 /* 4 = update. Insert/replace */
1419 ZEBRA_CHECK_HANDLE(zh);
1420 assert(action>0 && action <=4);
1423 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1425 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1427 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1428 "no record ID or empty record ID");
1432 memcpy (recid_z, recid_buf, recid_len);
1433 recid_z[recid_len] = 0;
1435 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1438 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1439 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1442 if (action == 1) /* fail if insert */
1444 if (zebra_end_trans(zh) != ZEBRA_OK)
1445 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1446 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1447 "Cannot insert record: already exist");
1451 memcpy (&sysno, rinfo+1, sizeof(sysno));
1455 if (action == 2 || action == 3) /* fail if delete or update */
1457 if (zebra_end_trans(zh) != ZEBRA_OK)
1458 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1459 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1460 "Cannot delete/update record: does not exist");
1463 action = 1; /* make it an insert (if it's an update).. */
1465 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1466 action == 3 ? 1 : 0 /* delete flag */,
1472 0, /* force update */
1473 1 /* allow update */
1475 if (res == ZEBRA_FAIL)
1477 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1478 "Unable to parse record");
1482 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1483 sizeof(sysno), &sysno);
1485 else if (action == 3)
1487 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1489 if (zebra_end_trans(zh) != ZEBRA_OK)
1491 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1497 int delete_w_handle(const char *info, void *handle)
1499 ZebraHandle zh = (ZebraHandle) handle;
1503 if (*info == sizeof(pos))
1505 memcpy (&pos, info+1, sizeof(pos));
1506 isamb_unlink(zh->reg->isamb, pos);
1511 static int delete_SU_handle(void *handle, int ord)
1513 ZebraHandle zh = (ZebraHandle) handle;
1517 ord_len = key_SU_encode (ord, ord_buf);
1518 ord_buf[ord_len] = '\0';
1520 assert (zh->reg->isamb);
1521 dict_delete_subtree(zh->reg->dict, ord_buf,
1522 zh, delete_w_handle);
1526 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1528 ZEBRA_RES ret = ZEBRA_OK;
1530 yaz_log(log_level, "zebra_drop_database %s", db);
1531 ZEBRA_CHECK_HANDLE(zh);
1533 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1535 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1540 if (zebraExplain_curDatabase (zh->reg->zei, db))
1542 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1547 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1548 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1549 0 /* handle */, 0 /* func */);
1550 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1551 zebraExplain_removeDatabase(zh->reg->zei, zh);
1552 zebra_remove_file_match(zh);
1557 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1558 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1559 "drop database only supported for isam:b");
1562 if (zebra_end_trans (zh) != ZEBRA_OK)
1564 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1570 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1572 yaz_log(log_level, "zebra_create_database %s", db);
1573 ZEBRA_CHECK_HANDLE(zh);
1576 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1578 if (zebra_begin_trans (zh, 1))
1581 /* announce database */
1582 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1583 /* explainDatabase */))
1585 if (zebra_end_trans (zh) != ZEBRA_OK)
1587 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1589 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1592 return zebra_end_trans (zh);
1595 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1596 const char *input_str, int input_len,
1597 char *output_str, int output_len)
1603 yaz_log(log_level, "zebra_string_norm ");
1605 if (!zh->reg->zebra_maps)
1607 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1608 input_str, input_len);
1611 if (wrbuf_len(wrbuf) >= output_len)
1613 if (wrbuf_len(wrbuf))
1614 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1615 output_str[wrbuf_len(wrbuf)] = '\0';
1616 return wrbuf_len(wrbuf);
1619 /** \brief set register state (state*.LCK)
1620 \param zh Zebra handle
1622 \param seqno sequence number
1625 d=writing to shadow(dirty)
1629 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1631 char state_fname[256];
1636 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
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, "w");
1642 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1643 fprintf (f, "%c %d %ld\n", val, seqno, p);
1648 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1650 char state_fname[256];
1655 yaz_log(log_level, "zebra_get_state ");
1657 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1658 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1659 f = fopen (fname, "r");
1665 fscanf (f, "%c %d", val, seqno);
1671 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1673 return zebra_begin_trans(zh, 0);
1676 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1678 return zebra_end_trans(zh);
1681 static void read_res_for_transaction(ZebraHandle zh)
1683 const char *group = res_get(zh->res, "group");
1685 /* FIXME - do we still use groups ?? */
1687 zh->m_group = group;
1688 v = res_get_prefix(zh->res, "followLinks", group, "1");
1689 zh->m_follow_links = atoi(v);
1691 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1692 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1694 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1695 zh->m_store_keys = atoi(v);
1697 v = res_get_prefix(zh->res, "storeData", group, "1");
1698 zh->m_store_data = atoi(v);
1700 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1701 zh->m_explain_database = atoi(v);
1703 v = res_get_prefix(zh->res, "openRW", group, "1");
1704 zh->m_flag_rw = atoi(v);
1706 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1707 zh->m_file_verbose_limit = atoi(v);
1710 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1712 ZEBRA_CHECK_HANDLE(zh);
1713 zebra_select_default_database(zh);
1716 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1717 "zebra_begin_trans: no database selected");
1721 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1725 if (rw && !strchr(zh->user_perm, 'w'))
1729 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1740 const char *rval = 0;
1745 read_res_for_transaction(zh);
1748 if (zh->trans_no != 1)
1750 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1751 "zebra_begin_trans: no write trans within read");
1756 resultSetInvalidate (zh);
1757 zebra_register_close(zh->service, zh->reg);
1759 zh->trans_w_no = zh->trans_no;
1761 zh->records_inserted = 0;
1762 zh->records_updated = 0;
1763 zh->records_deleted = 0;
1764 zh->records_processed = 0;
1766 #if HAVE_SYS_TIMES_H
1770 if (zh->shadow_enable)
1771 rval = res_get (zh->res, "shadow");
1775 zebra_lock_r(zh->lock_normal);
1776 zebra_lock_w(zh->lock_shadow);
1780 zebra_lock_w(zh->lock_normal);
1781 zebra_lock_w(zh->lock_shadow);
1783 zebra_get_state (zh, &val, &seqno);
1786 /* either we didn't finish commit or shadow is dirty */
1787 zebra_unlock (zh->lock_shadow);
1788 zebra_unlock (zh->lock_normal);
1789 if (zebra_commit (zh))
1797 zebra_lock_r(zh->lock_normal);
1798 zebra_lock_w(zh->lock_shadow);
1802 zebra_lock_w(zh->lock_normal);
1803 zebra_lock_w(zh->lock_shadow);
1807 zebra_set_state (zh, 'd', seqno);
1809 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1810 1, rval ? 1 : 0, zh->res,
1813 zh->reg->seqno = seqno;
1816 zebra_set_state (zh, 'o', seqno);
1818 zebra_unlock (zh->lock_shadow);
1819 zebra_unlock (zh->lock_normal);
1824 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1825 "zebra_begin_trans: cannot open register");
1826 yaz_log(YLOG_FATAL, "%s", zh->errString);
1829 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1839 if (zh->trans_no != 1)
1841 return zebra_flush_reg (zh);
1843 #if HAVE_SYS_TIMES_H
1849 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1852 if (!zh->lock_normal || !zh->lock_shadow)
1855 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1858 zebra_get_state (zh, &val, &seqno);
1864 else if (seqno != zh->reg->seqno)
1866 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1867 seqno, zh->reg->seqno);
1870 else if (zh->reg->last_val != val)
1872 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1873 val, zh->reg->last_val);
1880 zebra_lock_r (zh->lock_shadow);
1882 zebra_lock_r (zh->lock_normal);
1886 resultSetInvalidate (zh);
1887 zebra_register_close(zh->service, zh->reg);
1889 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1890 0, val == 'c' ? 1 : 0,
1891 zh->res, zh->path_reg);
1894 zebra_unlock (zh->lock_normal);
1895 zebra_unlock (zh->lock_shadow);
1897 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1900 zh->reg->last_val = val;
1901 zh->reg->seqno = seqno;
1903 read_res_for_transaction(zh);
1907 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1909 ZebraTransactionStatus dummy;
1911 yaz_log(log_level, "zebra_end_trans");
1912 ZEBRA_CHECK_HANDLE(zh);
1913 return zebra_end_transaction(zh, &dummy);
1916 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1922 ZEBRA_CHECK_HANDLE(zh);
1925 yaz_log(log_level, "zebra_end_transaction");
1927 status->processed = 0;
1928 status->inserted = 0;
1929 status->updated = 0;
1930 status->deleted = 0;
1934 if (!zh->res || !zh->reg)
1936 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1937 "zebra_end_trans: no open transaction");
1940 if (zh->trans_no != zh->trans_w_no)
1943 if (zh->trans_no != 0)
1946 /* release read lock */
1948 zebra_unlock (zh->lock_normal);
1949 zebra_unlock (zh->lock_shadow);
1952 { /* release write lock */
1956 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1957 rval = res_get (zh->res, "shadow");
1959 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1961 zebra_flush_reg (zh);
1963 resultSetInvalidate (zh);
1965 zebra_register_close(zh->service, zh->reg);
1968 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1969 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1970 zh->records_processed, zh->records_inserted,
1971 zh->records_updated, zh->records_deleted);
1973 status->processed = zh->records_processed;
1974 status->inserted = zh->records_inserted;
1975 status->updated = zh->records_updated;
1976 status->deleted = zh->records_deleted;
1978 zebra_get_state (zh, &val, &seqno);
1981 BFiles bfs = bfs_create (rval, zh->path_reg);
1982 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1983 bf_commitClean (bfs, rval);
1988 zebra_set_state (zh, 'o', seqno);
1990 zebra_unlock (zh->lock_shadow);
1991 zebra_unlock (zh->lock_normal);
1994 #if HAVE_SYS_TIMES_H
1996 yaz_log (log_level, "user/system: %ld/%ld",
1997 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1998 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
2000 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
2001 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
2006 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
2010 yaz_log (log_level, "updating %s", path);
2012 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
2013 return zebra_update_file_match(zh, path);
2015 return zebra_update_from_path(zh, path);
2018 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2022 yaz_log (log_level, "deleting %s", path);
2023 return zebra_delete_from_path(zh, path);
2026 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2030 yaz_log(log_level, "zebra_repository_show");
2031 repositoryShow (zh, path);
2035 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2041 ZEBRA_RES res = ZEBRA_OK;
2045 zebra_select_default_database(zh);
2048 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2051 rval = res_get(zh->res, "shadow");
2054 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2058 zebra_lock_w(zh->lock_normal);
2059 zebra_lock_r(zh->lock_shadow);
2061 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2064 zebra_unlock(zh->lock_shadow);
2065 zebra_unlock(zh->lock_normal);
2068 zebra_get_state(zh, &val, &seqno);
2072 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2077 bf_cache (bfs, rval);
2078 if (bf_commitExists (bfs))
2081 zebra_set_state(zh, 'd', seqno);
2084 zebra_set_state(zh, 'c', seqno);
2086 yaz_log(YLOG_DEBUG, "commit start");
2087 if (bf_commitExec (bfs))
2090 if (res == ZEBRA_OK)
2093 zebra_set_state(zh, 'o', seqno);
2095 zebra_unlock(zh->lock_shadow);
2096 zebra_unlock(zh->lock_normal);
2098 zebra_lock_w(zh->lock_shadow);
2099 bf_commitClean(bfs, rval);
2100 zebra_unlock(zh->lock_shadow);
2104 zebra_unlock(zh->lock_shadow);
2105 zebra_unlock(zh->lock_normal);
2106 yaz_log(YLOG_WARN, "zebra_commit: failed");
2111 zebra_unlock(zh->lock_shadow);
2112 zebra_unlock(zh->lock_normal);
2113 yaz_log(log_level, "nothing to commit");
2120 ZEBRA_RES zebra_clean(ZebraHandle zh)
2122 yaz_log(log_level, "zebra_clean");
2123 ZEBRA_CHECK_HANDLE(zh);
2124 return zebra_commit_ex(zh, 1);
2127 ZEBRA_RES zebra_commit(ZebraHandle zh)
2129 yaz_log(log_level, "zebra_commit");
2130 ZEBRA_CHECK_HANDLE(zh);
2131 return zebra_commit_ex(zh, 0);
2135 ZEBRA_RES zebra_init(ZebraHandle zh)
2140 yaz_log(log_level, "zebra_init");
2142 ZEBRA_CHECK_HANDLE(zh);
2144 zebra_select_default_database(zh);
2147 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2148 "cannot select default database");
2151 rval = res_get (zh->res, "shadow");
2153 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2156 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2160 bf_cache (bfs, rval);
2164 zebra_set_state (zh, 'o', 0);
2168 ZEBRA_RES zebra_compact(ZebraHandle zh)
2172 yaz_log(log_level, "zebra_compact");
2173 ZEBRA_CHECK_HANDLE(zh);
2176 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2179 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2185 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2187 yaz_log(log_level, "zebra_result");
2190 *code = zh->errCode;
2191 *addinfo = zh->errString;
2195 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2196 *addinfo ="ZebraHandle is NULL";
2200 void zebra_shadow_enable(ZebraHandle zh, int value)
2203 yaz_log(log_level, "zebra_shadow_enable");
2204 zh->shadow_enable = value;
2207 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2209 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2210 ZEBRA_CHECK_HANDLE(zh);
2213 if (zh->iconv_to_utf8 != 0)
2214 yaz_iconv_close(zh->iconv_to_utf8);
2215 if (zh->iconv_from_utf8 != 0)
2216 yaz_iconv_close(zh->iconv_from_utf8);
2219 yaz_iconv_open ("UTF-8", encoding);
2220 if (zh->iconv_to_utf8 == 0)
2221 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2222 zh->iconv_from_utf8 =
2223 yaz_iconv_open (encoding, "UTF-8");
2224 if (zh->iconv_to_utf8 == 0)
2225 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2230 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2232 yaz_log(log_level, "zebra_record_encoding");
2233 ZEBRA_CHECK_HANDLE(zh);
2234 xfree(zh->record_encoding);
2235 zh->record_encoding = 0;
2237 zh->record_encoding = xstrdup (encoding);
2241 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2245 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2247 res_set(zh->res, name, value);
2250 const char *zebra_get_resource(ZebraHandle zh,
2251 const char *name, const char *defaultvalue)
2256 v = res_get_def (zh->res, name, (char *)defaultvalue);
2257 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2261 /* moved from zebra_api_ext.c by pop */
2262 /* FIXME: Should this really be public??? -Heikki */
2264 int zebra_trans_no (ZebraHandle zh)
2266 yaz_log(log_level, "zebra_trans_no");
2268 return zh->trans_no;
2271 int zebra_get_shadow_enable (ZebraHandle zh)
2273 yaz_log(log_level, "zebra_get_shadow_enable");
2275 return zh->shadow_enable;
2278 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2280 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2282 zh->shadow_enable = value;
2285 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2286 const char *buf, int buf_size)
2288 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2291 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2292 const char *recordType,
2293 zint *sysno, const char *match,
2295 const char *buf, int buf_size, int force_update)
2301 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2304 buf_size = strlen(buf);
2306 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2308 res = zebra_buffer_extract_record(zh, buf, buf_size,
2309 0, /* delete_flag */
2315 0); /* allow_update */
2316 if (zebra_end_trans(zh) != ZEBRA_OK)
2318 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2324 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2325 const char *recordType,
2326 zint *sysno, const char *match,
2328 const char *buf, int buf_size,
2333 ZEBRA_CHECK_HANDLE(zh);
2337 yaz_log(log_level, "zebra_update_record");
2339 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2341 if (buf_size < 1) buf_size = strlen(buf);
2343 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2345 res = zebra_buffer_extract_record(zh, buf, buf_size,
2346 0, /* delete_flag */
2352 1); /* allow_update */
2353 if (zebra_end_trans(zh) != ZEBRA_OK)
2355 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2361 ZEBRA_RES zebra_delete_record(ZebraHandle zh,
2362 const char *recordType,
2363 zint *sysno, const char *match,
2365 const char *buf, int buf_size,
2370 ZEBRA_CHECK_HANDLE(zh);
2373 yaz_log(log_level, "zebra_delete_record");
2375 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2377 if (buf_size < 1) buf_size = strlen(buf);
2379 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2381 res = zebra_buffer_extract_record(zh, buf, buf_size,
2382 1, /* delete_flag */
2388 1); /* allow_update */
2389 if (zebra_end_trans(zh) != ZEBRA_OK)
2391 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2397 /* ---------------------------------------------------------------------------
2401 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2402 const char *setname, zint *hits)
2405 ZEBRA_RES res = ZEBRA_OK;
2410 ZEBRA_CHECK_HANDLE(zh);
2412 odr = odr_createmem(ODR_ENCODE);
2417 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2419 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2423 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2424 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2428 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2432 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2440 /* ---------------------------------------------------------------------------
2441 Sort - a simplified interface, with optional read locks.
2443 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2444 const char *sort_spec,
2445 const char *output_setname,
2446 const char **input_setnames)
2448 int num_input_setnames = 0;
2449 int sort_status = 0;
2450 Z_SortKeySpecList *sort_sequence;
2452 ZEBRA_CHECK_HANDLE(zh);
2455 assert(output_setname);
2456 assert(input_setnames);
2457 sort_sequence = yaz_sort_spec (stream, sort_spec);
2458 yaz_log(log_level, "sort (FIXME) ");
2461 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2462 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2466 /* we can do this, since the perl typemap code for char** will
2467 put a NULL at the end of list */
2468 while (input_setnames[num_input_setnames]) num_input_setnames++;
2470 if (zebra_begin_read (zh))
2473 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2474 output_setname, sort_sequence, &sort_status);
2480 /* ---------------------------------------------------------------------------
2481 Get BFS for Zebra system (to make alternative storage methods)
2483 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2486 return zh->reg->bfs;
2491 /* ---------------------------------------------------------------------------
2492 Set limit for search/scan
2494 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2496 ZEBRA_CHECK_HANDLE(zh);
2497 zebra_limit_destroy(zh->m_limit);
2498 zh->m_limit = zebra_limit_create(complement_flag, ids);
2503 Set Error code + addinfo
2505 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2510 nmem_reset(zh->nmem_error);
2511 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2514 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2517 sprintf(vstr, ZINT_FORMAT, i);
2520 nmem_reset(zh->nmem_error);
2521 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2524 void zebra_lock_prefix(Res res, char *path)
2526 const char *lock_dir = res_get_def (res, "lockDir", "");
2528 strcpy(path, lock_dir);
2529 if (*path && path[strlen(path)-1] != '/')
2536 * indent-tabs-mode: nil
2538 * vim: shiftwidth=4 tabstop=8 expandtab