1 /* $Id: zebraapi.c,v 1.256 2007-05-21 11:54:59 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <yaz/diagbib1.h>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
42 #include <idzebra/api.h>
43 #include <yaz/oid_db.h>
45 #define DEFAULT_APPROX_LIMIT 2000000000
47 /* simple asserts to validate the most essential input args */
48 #define ASSERTZH assert(zh && zh->service)
49 #define ASSERTZHRES assert(zh && zh->service && zh->res)
50 #define ASSERTZS assert(zs)
52 static int log_level = 0;
53 static int log_level_initialized = 0;
55 static void zebra_open_res(ZebraHandle zh);
56 static void zebra_close_res(ZebraHandle zh);
58 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
65 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
67 static void zebra_chdir (ZebraService zs)
71 yaz_log(log_level, "zebra_chdir");
72 dir = res_get (zs->global_res, "chdir");
75 yaz_log (YLOG_DEBUG, "chdir %s", dir);
83 static ZEBRA_RES zebra_flush_reg (ZebraHandle zh)
85 ZEBRA_CHECK_HANDLE(zh);
86 yaz_log(log_level, "zebra_flush_reg");
87 zebraExplain_flush (zh->reg->zei, zh);
89 key_block_flush(zh->reg->key_block, 1);
91 zebra_index_merge(zh);
95 static struct zebra_register *zebra_register_open(ZebraService zs,
97 int rw, int useshadow,
99 const char *reg_path);
100 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
102 const char *zebra_get_encoding(ZebraHandle zh)
104 assert(zh && zh->session_res);
105 return res_get_def(zh->session_res, "encoding", "ISO-8859-1");
108 ZebraHandle zebra_open(ZebraService zs, Res res)
111 const char *default_encoding;
112 if (!log_level_initialized)
114 log_level = yaz_log_module_level("zebraapi");
115 log_level_initialized = 1;
118 yaz_log(log_level, "zebra_open");
123 zh = (ZebraHandle) xmalloc(sizeof(*zh));
124 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
127 zh->reg = 0; /* no register attached yet */
133 zh->session_res = res_open(zs->global_res, res);
135 zh->dbaccesslist = 0;
137 zh->reg_name = xstrdup ("");
139 zh->num_basenames = 0;
142 zh->approx_limit = DEFAULT_APPROX_LIMIT;
149 zh->shadow_enable = 1;
150 zh->m_staticrank = 0;
151 zh->m_segment_indexing = 0;
153 zh->break_handler_func = 0;
154 zh->break_handler_data = 0;
156 default_encoding = zebra_get_encoding(zh);
159 yaz_iconv_open ("UTF-8", default_encoding);
160 if (zh->iconv_to_utf8 == 0)
161 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
163 zh->iconv_from_utf8 =
164 yaz_iconv_open (default_encoding, "UTF-8");
165 if (zh->iconv_to_utf8 == 0)
166 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
169 zh->record_encoding = 0;
171 zebra_mutex_cond_lock (&zs->session_lock);
173 zh->next = zs->sessions;
176 zebra_mutex_cond_unlock (&zs->session_lock);
178 zh->store_data_buf = 0;
180 zh->m_limit = zebra_limit_create(1, 0);
182 zh->nmem_error = nmem_create();
187 ZebraService zebra_start(const char *configName)
189 return zebra_start_res(configName, 0, 0);
192 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
198 if (!log_level_initialized)
200 log_level = yaz_log_module_level("zebraapi");
201 log_level_initialized = 1;
204 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
205 configName ? configName : "");
207 if ((res = res_open(def_res, over_res)))
209 const char *passwd_plain = 0;
210 const char *passwd_encrypt = 0;
211 const char *dbaccess = 0;
216 ZEBRA_RES ret = res_read_file(res, configName);
222 if (zebra_check_res(res))
224 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
231 zebra_check_res(res);
234 zh = xmalloc(sizeof(*zh));
235 zh->global_res = res;
240 zebra_mutex_cond_init (&zh->session_lock);
241 passwd_plain = res_get (zh->global_res, "passwd");
242 passwd_encrypt = res_get (zh->global_res, "passwd.c");
243 dbaccess = res_get (zh->global_res, "dbaccess");
245 if (!passwd_plain && !passwd_encrypt)
246 zh->passwd_db = NULL;
249 zh->passwd_db = passwd_db_open();
251 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
255 passwd_db_file_plain(zh->passwd_db, passwd_plain);
257 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
264 zh->dbaccess = res_open(NULL, NULL);
265 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
266 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
271 zh->timing = yaz_timing_create();
272 zh->path_root = res_get (zh->global_res, "root");
273 zh->nmem = nmem_create();
274 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
278 const char *module_path = res_get(res, "modulePath");
280 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
288 void zebra_filter_info(ZebraService zs, void *cd,
289 void (*cb)(void *cd, const char *name))
293 recTypeClass_info(zs->record_classes, cd, cb);
296 void zebra_pidfname(ZebraService zs, char *path)
299 zebra_lock_prefix (zs->global_res, path);
300 strcat(path, "zebrasrv.pid");
303 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
304 int compact_flag, Res res)
306 int page_size = 4096;
307 char resource_str[200];
308 sprintf (resource_str, "dict.%.100s.pagesize", name);
312 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
313 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
315 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
319 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
320 int rw, int useshadow, Res res,
321 const char *reg_path)
323 struct zebra_register *reg;
324 int record_compression = REC_COMPRESS_NONE;
325 const char *recordCompression = 0;
326 const char *profilePath;
328 int sort_type = ZEBRA_SORT_TYPE_FLAT;
329 ZEBRA_RES ret = ZEBRA_OK;
333 reg = xmalloc(sizeof(*reg));
336 reg->name = xstrdup (name);
343 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
344 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
346 reg->dh = data1_create();
353 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
356 data1_destroy(reg->dh);
363 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
365 bfs_destroy(reg->bfs);
366 data1_destroy(reg->dh);
373 getcwd(cwd, sizeof(cwd)-1);
374 profilePath = res_get_def(res, "profilePath", 0);
376 data1_set_tabpath (reg->dh, profilePath);
377 data1_set_tabroot (reg->dh, reg_path);
378 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
381 zebra_maps_open(res, reg_path, profilePath);
382 if (!reg->zebra_maps)
384 recTypes_destroy(reg->recTypes);
385 bfs_destroy(reg->bfs);
386 data1_destroy(reg->dh);
391 reg->rank_classes = NULL;
394 reg->keys = zebra_rec_keys_open();
396 reg->sortKeys = zebra_rec_keys_open();
407 /* installing rank classes */
408 zebraRankInstall (reg, rank_1_class);
409 zebraRankInstall (reg, rank_similarity_class);
410 zebraRankInstall (reg, rank_static_class);
412 recordCompression = res_get_def (res, "recordCompression", "none");
413 if (!strcmp (recordCompression, "none"))
414 record_compression = REC_COMPRESS_NONE;
415 if (!strcmp (recordCompression, "bzip2"))
416 record_compression = REC_COMPRESS_BZIP2;
420 const char *index_fname = res_get_def(res, "index", "default.idx");
421 if (index_fname && *index_fname)
423 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
428 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
430 yaz_log (YLOG_WARN, "rec_open failed");
435 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
437 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
439 yaz_log (YLOG_WARN, "dict_open failed");
444 if (res_get_match (res, "sortindex", "f", "f"))
445 sort_type = ZEBRA_SORT_TYPE_FLAT;
446 else if (res_get_match (res, "sortindex", "i", "f"))
447 sort_type = ZEBRA_SORT_TYPE_ISAMB;
450 yaz_log (YLOG_WARN, "bad_value for 'sortindex'");
455 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
457 yaz_log (YLOG_WARN, "zebra_sort_open failed");
460 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
462 struct ISAMS_M_s isams_m;
463 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
464 key_isams_m(res, &isams_m))))
466 yaz_log (YLOG_WARN, "isams_open failed");
470 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
472 struct ISAMC_M_s isamc_m;
473 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
474 rw, key_isamc_m(res, &isamc_m))))
476 yaz_log (YLOG_WARN, "isamc_open failed");
480 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
482 struct ISAMC_M_s isamc_m;
484 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
485 rw, key_isamc_m(res, &isamc_m), 0)))
487 yaz_log (YLOG_WARN, "isamb_open failed");
491 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
493 struct ISAMC_M_s isamc_m;
495 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
496 rw, key_isamc_m(res, &isamc_m), 1)))
498 yaz_log (YLOG_WARN, "isamb_open failed");
502 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
504 struct ISAMC_M_s isamc_m;
506 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
507 rw, key_isamc_m(res, &isamc_m), -1)))
509 yaz_log (YLOG_WARN, "isamb_open failed");
515 reg->zei = zebraExplain_open(reg->records, reg->dh,
517 zebra_extract_explain);
520 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
527 zebra_register_close(zs, reg);
530 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
534 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
536 ZEBRA_CHECK_HANDLE(zh);
537 yaz_log(log_level, "zebra_admin_shutdown");
539 zebra_mutex_cond_lock (&zh->service->session_lock);
540 zh->service->stop_flag = 1;
541 zebra_mutex_cond_unlock (&zh->service->session_lock);
545 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
548 ZEBRA_CHECK_HANDLE(zh);
549 yaz_log(log_level, "zebra_admin_start");
551 zebra_mutex_cond_lock (&zs->session_lock);
552 zebra_mutex_cond_unlock (&zs->session_lock);
556 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
560 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
564 zebraExplain_close (reg->zei);
565 dict_close (reg->dict);
567 dict_close (reg->matchDict);
568 zebra_sort_close(reg->sort_index);
570 isams_close (reg->isams);
572 isamc_close (reg->isamc);
574 isamb_close (reg->isamb);
575 rec_close (®->records);
577 recTypes_destroy (reg->recTypes);
578 zebra_maps_close (reg->zebra_maps);
579 zebraRankDestroy (reg);
580 bfs_destroy (reg->bfs);
581 data1_destroy (reg->dh);
583 zebra_rec_keys_close(reg->keys);
584 zebra_rec_keys_close(reg->sortKeys);
586 key_block_destroy(®->key_block);
591 ZEBRA_RES zebra_stop(ZebraService zs)
597 zebra_close (zs->sessions);
600 zebra_mutex_cond_destroy (&zs->session_lock);
603 passwd_db_close (zs->passwd_db);
605 recTypeClass_destroy(zs->record_classes);
606 nmem_destroy(zs->nmem);
607 res_close (zs->global_res);
609 yaz_timing_stop(zs->timing);
610 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
611 yaz_timing_get_real(zs->timing),
612 yaz_timing_get_user(zs->timing),
613 yaz_timing_get_sys(zs->timing));
616 yaz_timing_destroy(&zs->timing);
621 ZEBRA_RES zebra_close(ZebraHandle zh)
624 struct zebra_session **sp;
627 yaz_log(log_level, "zebra_close");
628 ZEBRA_CHECK_HANDLE(zh);
633 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
634 resultSetDestroy (zh, -1, 0, 0);
637 zebra_register_close(zh->service, zh->reg);
638 zebra_close_res (zh);
639 res_close(zh->session_res);
641 xfree(zh->record_encoding);
643 xfree(zh->dbaccesslist);
645 for (i = 0; i < zh->num_basenames; i++)
646 xfree(zh->basenames[i]);
647 xfree(zh->basenames);
649 if (zh->iconv_to_utf8 != 0)
650 yaz_iconv_close (zh->iconv_to_utf8);
651 if (zh->iconv_from_utf8 != 0)
652 yaz_iconv_close (zh->iconv_from_utf8);
654 zebra_mutex_cond_lock (&zs->session_lock);
655 zebra_lock_destroy (zh->lock_normal);
656 zebra_lock_destroy (zh->lock_shadow);
668 zebra_mutex_cond_unlock (&zs->session_lock);
670 xfree(zh->user_perm);
671 zh->service = 0; /* more likely to trigger an assert */
673 zebra_limit_destroy(zh->m_limit);
675 nmem_destroy(zh->nmem_error);
682 struct map_baseinfo {
688 char **new_basenames;
692 static void zebra_open_res(ZebraHandle zh)
700 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
701 zh->res = res_open(zh->session_res, 0);
702 res_read_file(zh->res, fname);
704 else if (*zh->reg_name == 0)
706 zh->res = res_open(zh->session_res, 0);
710 yaz_log (YLOG_WARN, "no register root specified");
711 zh->res = 0; /* no path for register - fail! */
715 static void zebra_close_res (ZebraHandle zh)
723 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
727 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
731 assert (zh->reg == 0);
732 assert (*zh->reg_name == 0);
738 resultSetInvalidate (zh);
739 zebra_register_close(zh->service, zh->reg);
745 zh->reg_name = xstrdup (new_reg);
749 if (zh->service->path_root)
751 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
752 strlen(zh->reg_name) + 3);
753 strcpy (zh->path_reg, zh->service->path_root);
756 strcat (zh->path_reg, "/");
757 strcat (zh->path_reg, zh->reg_name);
763 zebra_lock_destroy (zh->lock_normal);
767 zebra_lock_destroy (zh->lock_shadow);
773 const char *lock_area = res_get (zh->res, "lockDir");
775 if (!lock_area && zh->path_reg)
776 res_set (zh->res, "lockDir", zh->path_reg);
777 sprintf (fname, "norm.%s.LCK", zh->reg_name);
779 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
781 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
783 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
785 if (!zh->lock_normal || !zh->lock_shadow)
789 zebra_lock_destroy(zh->lock_normal);
794 zebra_lock_destroy(zh->lock_shadow);
803 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
804 zebra_set_approx_limit(zh, approx);
808 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
809 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
813 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
816 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
817 zh->m_segment_indexing);
822 void map_basenames_func (void *vp, const char *name, const char *value)
824 struct map_baseinfo *p = (struct map_baseinfo *) vp;
826 char fromdb[128], todb[8][128];
833 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
834 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
835 todb[5], todb[6], todb[7]);
839 for (i = 0; i<p->num_bases; i++)
840 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
843 for (i = 0; i < no; i++)
845 if (p->new_num_bases == p->new_num_max)
847 p->new_basenames[(p->new_num_bases)++] =
848 nmem_strdup (p->mem, todb[i]);
854 int zebra_select_default_database(ZebraHandle zh)
858 /* no database has been selected - so we select based on
859 resource setting (including group)
861 const char *group = res_get(zh->session_res, "group");
862 const char *v = res_get_prefix(zh->session_res,
863 "database", group, "Default");
864 return zebra_select_database(zh, v);
869 void map_basenames (ZebraHandle zh, ODR stream,
870 int *num_bases, char ***basenames)
872 struct map_baseinfo info;
873 struct map_baseinfo *p = &info;
876 yaz_log(log_level, "map_basenames ");
881 info.num_bases = *num_bases;
882 info.basenames = *basenames;
883 info.new_num_max = 128;
884 info.new_num_bases = 0;
885 info.new_basenames = (char **)
886 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
887 info.mem = stream->mem;
889 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
891 for (i = 0; i<p->num_bases; i++)
892 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
894 p->new_basenames[(p->new_num_bases)++] =
895 nmem_strdup (p->mem, p->basenames[i]);
897 *num_bases = info.new_num_bases;
898 *basenames = info.new_basenames;
899 for (i = 0; i<*num_bases; i++)
900 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
903 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
905 ZEBRA_CHECK_HANDLE(zh);
907 yaz_log(log_level, "zebra_select_database %s",basename);
909 return zebra_select_databases (zh, 1, &basename);
912 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
913 const char **basenames)
920 ZEBRA_CHECK_HANDLE(zh);
923 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
924 num_bases,basenames[0]);
929 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
933 /* Check if the user has access to all databases (Seb) */
934 /* You could argue that this should happen later, after we have
935 * determined that the database(s) exist. */
936 if (zh->dbaccesslist) {
937 for (i = 0; i < num_bases; i++) {
938 const char *db = basenames[i];
940 for (p = zh->dbaccesslist; p && *p; p = pp) {
942 if ((pp = strchr(p, '+'))) {
948 if (len == strlen(db) && !strncmp(db, p, len))
952 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
958 for (i = 0; i < zh->num_basenames; i++)
959 xfree(zh->basenames[i]);
960 xfree(zh->basenames);
962 zh->num_basenames = num_bases;
963 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
964 for (i = 0; i < zh->num_basenames; i++)
965 zh->basenames[i] = xstrdup (basenames[i]);
967 cp = strrchr(basenames[0], '/');
970 len = cp - basenames[0];
971 new_reg = xmalloc(len + 1);
972 memcpy (new_reg, basenames[0], len);
976 new_reg = xstrdup ("");
977 for (i = 1; i<num_bases; i++)
981 cp1 = strrchr (basenames[i], '/');
986 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
989 if (len != cp1 - basenames[i] ||
990 memcmp (basenames[i], new_reg, len))
992 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1000 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1005 zebra_select_register (zh, new_reg);
1009 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1012 if (!zh->lock_normal || !zh->lock_shadow)
1014 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1020 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1022 if (approx_limit == 0)
1023 approx_limit = DEFAULT_APPROX_LIMIT;
1024 zh->approx_limit = approx_limit;
1028 void zebra_set_partial_result(ZebraHandle zh)
1030 zh->partial_result = 1;
1034 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1035 int (*f)(void *client_data),
1038 zh->break_handler_func = f;
1039 zh->break_handler_data = client_data;
1043 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1044 const char *setname, zint *hits,
1045 int *estimated_hit_count,
1046 int *partial_resultset)
1050 ZEBRA_CHECK_HANDLE(zh);
1056 yaz_log(log_level, "zebra_search_rpn");
1058 zh->partial_result = 0;
1060 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1063 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1064 zh->num_basenames, zh->basenames, setname,
1065 hits, estimated_hit_count);
1067 *partial_resultset = zh->partial_result;
1072 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1073 const char *setname, zint *hits)
1075 int estimated_hit_count;
1076 int partial_resultset;
1077 return zebra_search_RPN_x(zh, o, query, setname, hits,
1078 &estimated_hit_count,
1079 &partial_resultset);
1082 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1083 const char *setname,
1084 Z_RecordComposition *comp,
1085 const Odr_oid *input_format, int num_recs,
1086 ZebraRetrievalRecord *recs)
1088 ZebraMetaRecord *poset;
1090 ZEBRA_RES ret = ZEBRA_OK;
1093 ZEBRA_CHECK_HANDLE(zh);
1099 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1103 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1108 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1111 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1112 for (i = 0; i<num_recs; i++)
1113 pos_array[i] = recs[i].position;
1114 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1117 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1118 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1124 for (i = 0; i<num_recs; i++)
1128 recs[i].errCode = 0;
1129 recs[i].format = yaz_oid_recsyn_sutrs;
1130 recs[i].len = strlen(poset[i].term);
1131 recs[i].buf = poset[i].term;
1132 recs[i].base = poset[i].db;
1134 else if (poset[i].sysno)
1138 zebra_snippets *hit_snippet = zebra_snippets_create();
1140 /* we disable hit snippets for now. It does not work well
1141 and it slows retrieval down a lot */
1143 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1147 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1149 stream, input_format, comp,
1150 &recs[i].format, &buf, &len,
1151 &recs[i].base, &recs[i].errString);
1156 recs[i].buf = (char*) odr_malloc(stream, len);
1157 memcpy(recs[i].buf, buf, len);
1161 recs[i].score = poset[i].score;
1162 recs[i].sysno = poset[i].sysno;
1163 zebra_snippets_destroy(hit_snippet);
1167 /* only need to set it once */
1168 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1170 zebra_setError_zint(zh,
1171 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1176 recs[i].buf = 0; /* no record and no error issued */
1178 recs[i].errCode = 0;
1183 zebra_meta_records_destroy(zh, poset, num_recs);
1185 zebra_end_read (zh);
1190 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1192 int *num_entries, ZebraScanEntry **entries,
1194 const char *setname)
1196 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1197 Z_AttributesPlusTerm *zapt;
1198 Odr_oid *attributeSet;
1201 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1204 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1208 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1209 position, num_entries, entries, is_partial,
1212 yaz_pqf_destroy (pqf_parser);
1216 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1217 const Odr_oid *attributeset,
1219 int *num_entries, ZebraScanEntry **entries,
1221 const char *setname)
1224 RSET limit_rset = 0;
1226 ZEBRA_CHECK_HANDLE(zh);
1231 assert(num_entries);
1234 yaz_log(log_level, "zebra_scan");
1236 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1244 limit_rset = resultSetRef(zh, setname);
1248 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1250 zebra_end_read (zh);
1254 res = rpn_scan(zh, stream, zapt, attributeset,
1255 zh->num_basenames, zh->basenames, position,
1256 num_entries, entries, is_partial, limit_rset);
1261 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1262 int num_input_setnames, const char **input_setnames,
1263 const char *output_setname,
1264 Z_SortKeySpecList *sort_sequence,
1268 ZEBRA_CHECK_HANDLE(zh);
1270 assert(num_input_setnames>0);
1271 assert(input_setnames);
1272 assert(sort_sequence);
1273 assert(sort_status);
1274 yaz_log(log_level, "zebra_sort");
1276 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1278 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1279 output_setname, sort_sequence, sort_status);
1284 int zebra_deleteResultSet(ZebraHandle zh, int function,
1285 int num_setnames, char **setnames,
1291 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1293 if (zebra_begin_read(zh))
1294 return Z_DeleteStatus_systemProblemAtTarget;
1297 case Z_DeleteResultSetRequest_list:
1298 assert(num_setnames>0);
1300 resultSetDestroy (zh, num_setnames, setnames, statuses);
1302 case Z_DeleteResultSetRequest_all:
1303 resultSetDestroy (zh, -1, 0, statuses);
1306 zebra_end_read (zh);
1307 status = Z_DeleteStatus_success;
1308 for (i = 0; i<num_setnames; i++)
1309 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1310 status = statuses[i];
1314 int zebra_errCode (ZebraHandle zh)
1318 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1321 yaz_log(log_level, "zebra_errCode: o");
1325 const char *zebra_errString (ZebraHandle zh)
1329 e= diagbib1_str (zh->errCode);
1330 yaz_log(log_level, "zebra_errString: %s",e);
1334 char *zebra_errAdd (ZebraHandle zh)
1339 yaz_log(log_level, "zebra_errAdd: %s",a);
1343 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1346 const char *astring;
1350 ZEBRA_CHECK_HANDLE(zh);
1354 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1355 p = res_get(zs->global_res, u);
1356 xfree(zh->user_perm);
1357 zh->user_perm = xstrdup(p ? p : "r");
1359 /* Determine database access list */
1360 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1362 zh->dbaccesslist = xstrdup(astring);
1364 zh->dbaccesslist = 0;
1366 /* users that don't require a password .. */
1367 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1370 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1375 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1376 const char *record_type)
1378 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1379 database, record_type);
1380 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1382 return zebra_begin_trans(zh, 1);
1385 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1387 ZEBRA_CHECK_HANDLE(zh);
1388 yaz_log(log_level, "zebra_admin_import_end");
1389 return zebra_end_trans(zh);
1392 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1394 ZEBRA_RES res = ZEBRA_OK;
1397 ZEBRA_CHECK_HANDLE(zh);
1398 yaz_log(log_level, "zebra_admin_import_segment");
1400 for (i = 0; i<segment->num_segmentRecords; i++)
1402 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1404 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1406 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1407 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1409 Odr_oct *oct = fragment->u.notExternallyTagged;
1412 if (zebra_update_record(
1415 0, /* record Type */
1419 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1427 int delete_w_handle(const char *info, void *handle)
1429 ZebraHandle zh = (ZebraHandle) handle;
1433 if (*info == sizeof(pos))
1435 memcpy (&pos, info+1, sizeof(pos));
1436 isamb_unlink(zh->reg->isamb, pos);
1441 static int delete_SU_handle(void *handle, int ord)
1443 ZebraHandle zh = (ZebraHandle) handle;
1447 ord_len = key_SU_encode (ord, ord_buf);
1448 ord_buf[ord_len] = '\0';
1450 assert (zh->reg->isamb);
1451 dict_delete_subtree(zh->reg->dict, ord_buf,
1452 zh, delete_w_handle);
1456 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1458 ZEBRA_RES ret = ZEBRA_OK;
1460 yaz_log(log_level, "zebra_drop_database %s", db);
1461 ZEBRA_CHECK_HANDLE(zh);
1463 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1465 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1470 if (zebraExplain_curDatabase (zh->reg->zei, db))
1472 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1477 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1478 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1479 0 /* handle */, 0 /* func */);
1480 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1481 zebraExplain_removeDatabase(zh->reg->zei, zh);
1482 zebra_remove_file_match(zh);
1487 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1488 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1489 "drop database only supported for isam:b");
1492 if (zebra_end_trans (zh) != ZEBRA_OK)
1494 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1500 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1502 yaz_log(log_level, "zebra_create_database %s", db);
1503 ZEBRA_CHECK_HANDLE(zh);
1506 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1508 if (zebra_begin_trans (zh, 1))
1511 /* announce database */
1512 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1513 /* explainDatabase */))
1515 if (zebra_end_trans (zh) != ZEBRA_OK)
1517 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1519 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1522 return zebra_end_trans (zh);
1525 int zebra_string_norm(ZebraHandle zh, unsigned reg_id,
1526 const char *input_str, int input_len,
1527 char *output_str, int output_len)
1533 yaz_log(log_level, "zebra_string_norm ");
1535 if (!zh->reg->zebra_maps)
1537 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1538 input_str, input_len);
1541 if (wrbuf_len(wrbuf) >= output_len)
1543 if (wrbuf_len(wrbuf))
1544 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1545 output_str[wrbuf_len(wrbuf)] = '\0';
1546 return wrbuf_len(wrbuf);
1549 /** \brief set register state (state*.LCK)
1550 \param zh Zebra handle
1552 \param seqno sequence number
1555 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1557 c=commit (writing to register, reading from shadow, shadow mode only)
1559 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1561 char state_fname[256];
1566 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1568 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1569 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1570 f = fopen (fname, "w");
1572 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1573 fprintf (f, "%c %d %ld\n", val, seqno, p);
1578 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1580 char state_fname[256];
1585 yaz_log(log_level, "zebra_get_state ");
1587 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1588 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1589 f = fopen (fname, "r");
1595 fscanf (f, "%c %d", val, seqno);
1601 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1603 return zebra_begin_trans(zh, 0);
1606 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1608 return zebra_end_trans(zh);
1611 static void read_res_for_transaction(ZebraHandle zh)
1613 const char *group = res_get(zh->res, "group");
1615 /* FIXME - do we still use groups ?? */
1617 zh->m_group = group;
1618 v = res_get_prefix(zh->res, "followLinks", group, "1");
1619 zh->m_follow_links = atoi(v);
1621 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1622 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1624 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1625 zh->m_store_keys = atoi(v);
1627 v = res_get_prefix(zh->res, "storeData", group, "1");
1628 zh->m_store_data = atoi(v);
1630 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1631 zh->m_explain_database = atoi(v);
1633 v = res_get_prefix(zh->res, "openRW", group, "1");
1634 zh->m_flag_rw = atoi(v);
1636 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1637 zh->m_file_verbose_limit = atoi(v);
1640 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1642 ZEBRA_CHECK_HANDLE(zh);
1643 zebra_select_default_database(zh);
1646 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1647 "zebra_begin_trans: no database selected");
1651 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1655 if (rw && !strchr(zh->user_perm, 'w'))
1659 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1670 const char *rval = 0;
1675 read_res_for_transaction(zh);
1678 if (zh->trans_no != 1)
1680 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1681 "zebra_begin_trans: no write trans within read");
1686 resultSetInvalidate (zh);
1687 zebra_register_close(zh->service, zh->reg);
1689 zh->trans_w_no = zh->trans_no;
1691 zh->records_inserted = 0;
1692 zh->records_updated = 0;
1693 zh->records_deleted = 0;
1694 zh->records_processed = 0;
1695 zh->records_skipped = 0;
1697 #if HAVE_SYS_TIMES_H
1701 if (zh->shadow_enable)
1702 rval = res_get (zh->res, "shadow");
1706 zebra_lock_r(zh->lock_normal);
1707 zebra_lock_w(zh->lock_shadow);
1711 zebra_lock_w(zh->lock_normal);
1712 zebra_lock_w(zh->lock_shadow);
1714 zebra_get_state (zh, &val, &seqno);
1717 /* either we didn't finish commit or shadow is dirty */
1720 yaz_log(YLOG_WARN, "previous transaction did not finish "
1721 "(shadow disabled)");
1723 zebra_unlock (zh->lock_shadow);
1724 zebra_unlock (zh->lock_normal);
1725 if (zebra_commit (zh))
1733 zebra_lock_r(zh->lock_normal);
1734 zebra_lock_w(zh->lock_shadow);
1738 zebra_lock_w(zh->lock_normal);
1739 zebra_lock_w(zh->lock_shadow);
1743 zebra_set_state (zh, 'd', seqno);
1745 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1746 1, rval ? 1 : 0, zh->res,
1749 zh->reg->seqno = seqno;
1752 zebra_set_state (zh, 'o', seqno);
1754 zebra_unlock (zh->lock_shadow);
1755 zebra_unlock (zh->lock_normal);
1760 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1761 "zebra_begin_trans: cannot open register");
1762 yaz_log(YLOG_FATAL, "%s", zh->errString);
1765 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1775 if (zh->trans_no != 1)
1777 return zebra_flush_reg (zh);
1779 #if HAVE_SYS_TIMES_H
1785 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1788 if (!zh->lock_normal || !zh->lock_shadow)
1791 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1794 zebra_get_state (zh, &val, &seqno);
1800 else if (seqno != zh->reg->seqno)
1802 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1803 seqno, zh->reg->seqno);
1806 else if (zh->reg->last_val != val)
1808 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1809 val, zh->reg->last_val);
1816 zebra_lock_r (zh->lock_shadow);
1818 zebra_lock_r (zh->lock_normal);
1822 resultSetInvalidate (zh);
1823 zebra_register_close(zh->service, zh->reg);
1825 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1826 0, val == 'c' ? 1 : 0,
1827 zh->res, zh->path_reg);
1830 zebra_unlock (zh->lock_normal);
1831 zebra_unlock (zh->lock_shadow);
1833 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1836 zh->reg->last_val = val;
1837 zh->reg->seqno = seqno;
1839 read_res_for_transaction(zh);
1843 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1845 ZebraTransactionStatus dummy;
1847 yaz_log(log_level, "zebra_end_trans");
1848 ZEBRA_CHECK_HANDLE(zh);
1849 return zebra_end_transaction(zh, &dummy);
1852 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1858 ZEBRA_CHECK_HANDLE(zh);
1861 yaz_log(log_level, "zebra_end_transaction");
1863 status->processed = 0;
1864 status->inserted = 0;
1865 status->updated = 0;
1866 status->deleted = 0;
1870 if (!zh->res || !zh->reg)
1872 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1873 "zebra_end_trans: no open transaction");
1876 if (zh->trans_no != zh->trans_w_no)
1879 if (zh->trans_no != 0)
1882 /* release read lock */
1884 zebra_unlock (zh->lock_normal);
1885 zebra_unlock (zh->lock_shadow);
1888 { /* release write lock */
1892 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1893 rval = res_get (zh->res, "shadow");
1895 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1897 zebra_flush_reg (zh);
1899 resultSetInvalidate (zh);
1901 zebra_register_close(zh->service, zh->reg);
1904 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1905 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1906 zh->records_processed, zh->records_inserted,
1907 zh->records_updated, zh->records_deleted);
1909 status->processed = zh->records_processed;
1910 status->inserted = zh->records_inserted;
1911 status->updated = zh->records_updated;
1912 status->deleted = zh->records_deleted;
1914 zebra_get_state (zh, &val, &seqno);
1917 BFiles bfs = bfs_create (rval, zh->path_reg);
1918 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1919 bf_commitClean (bfs, rval);
1924 zebra_set_state (zh, 'o', seqno);
1926 zebra_unlock (zh->lock_shadow);
1927 zebra_unlock (zh->lock_normal);
1930 #if HAVE_SYS_TIMES_H
1932 yaz_log (log_level, "user/system: %ld/%ld",
1933 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1934 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1936 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1937 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1942 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1946 yaz_log (log_level, "updating %s", path);
1948 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1949 return zebra_update_file_match(zh, path);
1951 return zebra_update_from_path(zh, path);
1954 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1958 yaz_log (log_level, "deleting %s", path);
1959 return zebra_delete_from_path(zh, path);
1962 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1966 yaz_log(log_level, "zebra_repository_show");
1967 repositoryShow (zh, path);
1971 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1977 ZEBRA_RES res = ZEBRA_OK;
1981 zebra_select_default_database(zh);
1984 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1987 rval = res_get(zh->res, "shadow");
1990 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1994 zebra_lock_w(zh->lock_normal);
1995 zebra_lock_r(zh->lock_shadow);
1997 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2000 zebra_unlock(zh->lock_shadow);
2001 zebra_unlock(zh->lock_normal);
2004 zebra_get_state(zh, &val, &seqno);
2008 /* shadow area is dirty and so we must throw it away */
2009 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2014 bf_cache (bfs, rval);
2015 if (bf_commitExists (bfs))
2018 zebra_set_state(zh, 'd', seqno);
2021 zebra_set_state(zh, 'c', seqno);
2023 yaz_log(YLOG_DEBUG, "commit start");
2024 if (bf_commitExec (bfs))
2027 if (res == ZEBRA_OK)
2030 zebra_set_state(zh, 'o', seqno);
2032 zebra_unlock(zh->lock_shadow);
2033 zebra_unlock(zh->lock_normal);
2035 zebra_lock_w(zh->lock_shadow);
2036 bf_commitClean(bfs, rval);
2037 zebra_unlock(zh->lock_shadow);
2041 zebra_unlock(zh->lock_shadow);
2042 zebra_unlock(zh->lock_normal);
2043 yaz_log(YLOG_WARN, "zebra_commit: failed");
2048 zebra_unlock(zh->lock_shadow);
2049 zebra_unlock(zh->lock_normal);
2050 yaz_log(log_level, "nothing to commit");
2057 ZEBRA_RES zebra_clean(ZebraHandle zh)
2059 yaz_log(log_level, "zebra_clean");
2060 ZEBRA_CHECK_HANDLE(zh);
2061 return zebra_commit_ex(zh, 1);
2064 ZEBRA_RES zebra_commit(ZebraHandle zh)
2066 yaz_log(log_level, "zebra_commit");
2067 ZEBRA_CHECK_HANDLE(zh);
2068 return zebra_commit_ex(zh, 0);
2072 ZEBRA_RES zebra_init(ZebraHandle zh)
2077 yaz_log(log_level, "zebra_init");
2079 ZEBRA_CHECK_HANDLE(zh);
2081 zebra_select_default_database(zh);
2084 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2085 "cannot select default database");
2088 rval = res_get (zh->res, "shadow");
2090 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2093 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2097 bf_cache (bfs, rval);
2101 zebra_set_state (zh, 'o', 0);
2105 ZEBRA_RES zebra_compact(ZebraHandle zh)
2109 yaz_log(log_level, "zebra_compact");
2110 ZEBRA_CHECK_HANDLE(zh);
2113 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2116 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2122 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2124 yaz_log(log_level, "zebra_result");
2127 *code = zh->errCode;
2128 *addinfo = zh->errString;
2132 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2133 *addinfo ="ZebraHandle is NULL";
2137 void zebra_shadow_enable(ZebraHandle zh, int value)
2140 yaz_log(log_level, "zebra_shadow_enable");
2141 zh->shadow_enable = value;
2144 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2146 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2147 ZEBRA_CHECK_HANDLE(zh);
2150 if (zh->iconv_to_utf8 != 0)
2151 yaz_iconv_close(zh->iconv_to_utf8);
2152 if (zh->iconv_from_utf8 != 0)
2153 yaz_iconv_close(zh->iconv_from_utf8);
2156 yaz_iconv_open ("UTF-8", encoding);
2157 if (zh->iconv_to_utf8 == 0)
2158 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2159 zh->iconv_from_utf8 =
2160 yaz_iconv_open (encoding, "UTF-8");
2161 if (zh->iconv_to_utf8 == 0)
2162 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2167 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2169 yaz_log(log_level, "zebra_record_encoding");
2170 ZEBRA_CHECK_HANDLE(zh);
2171 xfree(zh->record_encoding);
2172 zh->record_encoding = 0;
2174 zh->record_encoding = xstrdup (encoding);
2178 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2182 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2184 res_set(zh->res, name, value);
2187 const char *zebra_get_resource(ZebraHandle zh,
2188 const char *name, const char *defaultvalue)
2193 v = res_get_def (zh->res, name, (char *)defaultvalue);
2194 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2198 /* moved from zebra_api_ext.c by pop */
2199 /* FIXME: Should this really be public??? -Heikki */
2201 int zebra_trans_no (ZebraHandle zh)
2203 yaz_log(log_level, "zebra_trans_no");
2205 return zh->trans_no;
2208 int zebra_get_shadow_enable (ZebraHandle zh)
2210 yaz_log(log_level, "zebra_get_shadow_enable");
2212 return zh->shadow_enable;
2215 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2217 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2219 zh->shadow_enable = value;
2222 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2223 const char *buf, int buf_size)
2225 return zebra_update_record(zh, action_update,
2226 0 /* record type */,
2233 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2234 enum zebra_recctrl_action_t action,
2235 const char *recordType,
2236 zint *sysno, const char *match,
2238 const char *buf, int buf_size)
2242 ZEBRA_CHECK_HANDLE(zh);
2246 yaz_log(log_level, "zebra_update_record");
2248 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2251 buf_size = strlen(buf);
2253 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2255 res = zebra_buffer_extract_record(zh, buf, buf_size,
2262 if (zebra_end_trans(zh) != ZEBRA_OK)
2264 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2270 /* ---------------------------------------------------------------------------
2274 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2275 const char *setname, zint *hits)
2278 ZEBRA_RES res = ZEBRA_OK;
2283 ZEBRA_CHECK_HANDLE(zh);
2285 odr = odr_createmem(ODR_ENCODE);
2290 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2292 query = p_query_rpn(odr, pqf_query);
2296 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2297 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2301 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2305 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2313 /* ---------------------------------------------------------------------------
2314 Sort - a simplified interface, with optional read locks.
2316 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2317 const char *sort_spec,
2318 const char *output_setname,
2319 const char **input_setnames)
2321 int num_input_setnames = 0;
2322 int sort_status = 0;
2323 Z_SortKeySpecList *sort_sequence;
2325 ZEBRA_CHECK_HANDLE(zh);
2328 assert(output_setname);
2329 assert(input_setnames);
2330 sort_sequence = yaz_sort_spec (stream, sort_spec);
2331 yaz_log(log_level, "sort (FIXME) ");
2334 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2335 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2339 /* we can do this, since the perl typemap code for char** will
2340 put a NULL at the end of list */
2341 while (input_setnames[num_input_setnames]) num_input_setnames++;
2343 if (zebra_begin_read (zh))
2346 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2347 output_setname, sort_sequence, &sort_status);
2353 /* ---------------------------------------------------------------------------
2354 Get BFS for Zebra system (to make alternative storage methods)
2356 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2359 return zh->reg->bfs;
2364 /* ---------------------------------------------------------------------------
2365 Set limit for search/scan
2367 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2369 ZEBRA_CHECK_HANDLE(zh);
2370 zebra_limit_destroy(zh->m_limit);
2371 zh->m_limit = zebra_limit_create(complement_flag, ids);
2376 Set Error code + addinfo
2378 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2383 nmem_reset(zh->nmem_error);
2384 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2387 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2390 sprintf(vstr, ZINT_FORMAT, i);
2393 nmem_reset(zh->nmem_error);
2394 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2397 void zebra_lock_prefix(Res res, char *path)
2399 const char *lock_dir = res_get_def (res, "lockDir", "");
2401 strcpy(path, lock_dir);
2402 if (*path && path[strlen(path)-1] != '/')
2409 * indent-tabs-mode: nil
2411 * vim: shiftwidth=4 tabstop=8 expandtab