1 /* $Id: zebraapi.c,v 1.258 2007-08-27 17:22:22 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)
195 char version_str[16];
200 if (!log_level_initialized)
202 log_level = yaz_log_module_level("zebraapi");
203 log_level_initialized = 1;
206 zebra_get_version(version_str, system_str);
208 yaz_log(YLOG_LOG, "zebra_start %s %s", version_str,
209 configName ? configName : "");
211 if ((res = res_open(def_res, over_res)))
213 const char *passwd_plain = 0;
214 const char *passwd_encrypt = 0;
215 const char *dbaccess = 0;
220 ZEBRA_RES ret = res_read_file(res, configName);
226 if (zebra_check_res(res))
228 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
235 zebra_check_res(res);
238 zh = xmalloc(sizeof(*zh));
239 zh->global_res = res;
244 zebra_mutex_cond_init (&zh->session_lock);
245 passwd_plain = res_get (zh->global_res, "passwd");
246 passwd_encrypt = res_get (zh->global_res, "passwd.c");
247 dbaccess = res_get (zh->global_res, "dbaccess");
249 if (!passwd_plain && !passwd_encrypt)
250 zh->passwd_db = NULL;
253 zh->passwd_db = passwd_db_open();
255 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
259 passwd_db_file_plain(zh->passwd_db, passwd_plain);
261 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
268 zh->dbaccess = res_open(NULL, NULL);
269 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
270 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
275 zh->timing = yaz_timing_create();
276 zh->path_root = res_get (zh->global_res, "root");
277 zh->nmem = nmem_create();
278 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
282 const char *module_path = res_get(res, "modulePath");
284 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
292 void zebra_filter_info(ZebraService zs, void *cd,
293 void (*cb)(void *cd, const char *name))
297 recTypeClass_info(zs->record_classes, cd, cb);
300 void zebra_pidfname(ZebraService zs, char *path)
303 zebra_lock_prefix (zs->global_res, path);
304 strcat(path, "zebrasrv.pid");
307 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
308 int compact_flag, Res res)
310 int page_size = 4096;
311 char resource_str[200];
312 sprintf (resource_str, "dict.%.100s.pagesize", name);
316 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
317 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
319 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
323 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
324 int rw, int useshadow, Res res,
325 const char *reg_path)
327 struct zebra_register *reg;
328 int record_compression = REC_COMPRESS_NONE;
329 const char *recordCompression = 0;
330 const char *profilePath;
332 int sort_type = ZEBRA_SORT_TYPE_FLAT;
333 ZEBRA_RES ret = ZEBRA_OK;
337 reg = xmalloc(sizeof(*reg));
340 reg->name = xstrdup (name);
347 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
348 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
350 reg->dh = data1_create();
357 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
360 data1_destroy(reg->dh);
367 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
369 bfs_destroy(reg->bfs);
370 data1_destroy(reg->dh);
377 getcwd(cwd, sizeof(cwd)-1);
378 profilePath = res_get_def(res, "profilePath", 0);
380 data1_set_tabpath (reg->dh, profilePath);
381 data1_set_tabroot (reg->dh, reg_path);
382 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
385 zebra_maps_open(res, reg_path, profilePath);
386 if (!reg->zebra_maps)
388 recTypes_destroy(reg->recTypes);
389 bfs_destroy(reg->bfs);
390 data1_destroy(reg->dh);
395 reg->rank_classes = NULL;
398 reg->keys = zebra_rec_keys_open();
400 reg->sortKeys = zebra_rec_keys_open();
411 /* installing rank classes */
412 zebraRankInstall (reg, rank_1_class);
413 zebraRankInstall (reg, rank_similarity_class);
414 zebraRankInstall (reg, rank_static_class);
416 recordCompression = res_get_def (res, "recordCompression", "none");
417 if (!strcmp (recordCompression, "none"))
418 record_compression = REC_COMPRESS_NONE;
419 if (!strcmp (recordCompression, "bzip2"))
420 record_compression = REC_COMPRESS_BZIP2;
424 const char *index_fname = res_get_def(res, "index", "default.idx");
425 if (index_fname && *index_fname)
427 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
432 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
434 yaz_log (YLOG_WARN, "rec_open failed");
439 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
441 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
443 yaz_log (YLOG_WARN, "dict_open failed");
448 if (res_get_match (res, "sortindex", "f", "f"))
449 sort_type = ZEBRA_SORT_TYPE_FLAT;
450 else if (res_get_match (res, "sortindex", "i", "f"))
451 sort_type = ZEBRA_SORT_TYPE_ISAMB;
454 yaz_log (YLOG_WARN, "bad_value for 'sortindex'");
459 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
461 yaz_log (YLOG_WARN, "zebra_sort_open failed");
464 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
466 struct ISAMS_M_s isams_m;
467 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
468 key_isams_m(res, &isams_m))))
470 yaz_log (YLOG_WARN, "isams_open failed");
474 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
476 struct ISAMC_M_s isamc_m;
477 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
478 rw, key_isamc_m(res, &isamc_m))))
480 yaz_log (YLOG_WARN, "isamc_open failed");
484 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
486 struct ISAMC_M_s isamc_m;
488 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
489 rw, key_isamc_m(res, &isamc_m), 0)))
491 yaz_log (YLOG_WARN, "isamb_open failed");
495 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
497 struct ISAMC_M_s isamc_m;
499 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
500 rw, key_isamc_m(res, &isamc_m), 1)))
502 yaz_log (YLOG_WARN, "isamb_open failed");
506 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
508 struct ISAMC_M_s isamc_m;
510 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
511 rw, key_isamc_m(res, &isamc_m), -1)))
513 yaz_log (YLOG_WARN, "isamb_open failed");
519 reg->zei = zebraExplain_open(reg->records, reg->dh,
521 zebra_extract_explain);
524 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
531 zebra_register_close(zs, reg);
534 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
538 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
540 ZEBRA_CHECK_HANDLE(zh);
541 yaz_log(log_level, "zebra_admin_shutdown");
543 zebra_mutex_cond_lock (&zh->service->session_lock);
544 zh->service->stop_flag = 1;
545 zebra_mutex_cond_unlock (&zh->service->session_lock);
549 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
552 ZEBRA_CHECK_HANDLE(zh);
553 yaz_log(log_level, "zebra_admin_start");
555 zebra_mutex_cond_lock (&zs->session_lock);
556 zebra_mutex_cond_unlock (&zs->session_lock);
560 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
564 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
568 zebraExplain_close (reg->zei);
569 dict_close (reg->dict);
571 dict_close (reg->matchDict);
572 zebra_sort_close(reg->sort_index);
574 isams_close (reg->isams);
576 isamc_close (reg->isamc);
578 isamb_close (reg->isamb);
579 rec_close (®->records);
581 recTypes_destroy (reg->recTypes);
582 zebra_maps_close (reg->zebra_maps);
583 zebraRankDestroy (reg);
584 bfs_destroy (reg->bfs);
585 data1_destroy (reg->dh);
587 zebra_rec_keys_close(reg->keys);
588 zebra_rec_keys_close(reg->sortKeys);
590 key_block_destroy(®->key_block);
595 ZEBRA_RES zebra_stop(ZebraService zs)
601 zebra_close (zs->sessions);
604 zebra_mutex_cond_destroy (&zs->session_lock);
607 passwd_db_close (zs->passwd_db);
609 recTypeClass_destroy(zs->record_classes);
610 nmem_destroy(zs->nmem);
611 res_close (zs->global_res);
613 yaz_timing_stop(zs->timing);
614 yaz_log (YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
615 yaz_timing_get_real(zs->timing),
616 yaz_timing_get_user(zs->timing),
617 yaz_timing_get_sys(zs->timing));
620 yaz_timing_destroy(&zs->timing);
625 ZEBRA_RES zebra_close(ZebraHandle zh)
628 struct zebra_session **sp;
631 yaz_log(log_level, "zebra_close");
632 ZEBRA_CHECK_HANDLE(zh);
637 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
638 resultSetDestroy (zh, -1, 0, 0);
641 zebra_register_close(zh->service, zh->reg);
642 zebra_close_res (zh);
643 res_close(zh->session_res);
645 xfree(zh->record_encoding);
647 xfree(zh->dbaccesslist);
649 for (i = 0; i < zh->num_basenames; i++)
650 xfree(zh->basenames[i]);
651 xfree(zh->basenames);
653 if (zh->iconv_to_utf8 != 0)
654 yaz_iconv_close (zh->iconv_to_utf8);
655 if (zh->iconv_from_utf8 != 0)
656 yaz_iconv_close (zh->iconv_from_utf8);
658 zebra_mutex_cond_lock (&zs->session_lock);
659 zebra_lock_destroy (zh->lock_normal);
660 zebra_lock_destroy (zh->lock_shadow);
672 zebra_mutex_cond_unlock (&zs->session_lock);
674 xfree(zh->user_perm);
675 zh->service = 0; /* more likely to trigger an assert */
677 zebra_limit_destroy(zh->m_limit);
679 nmem_destroy(zh->nmem_error);
686 struct map_baseinfo {
692 char **new_basenames;
696 static void zebra_open_res(ZebraHandle zh)
704 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
705 zh->res = res_open(zh->session_res, 0);
706 res_read_file(zh->res, fname);
708 else if (*zh->reg_name == 0)
710 zh->res = res_open(zh->session_res, 0);
714 yaz_log (YLOG_WARN, "no register root specified");
715 zh->res = 0; /* no path for register - fail! */
719 static void zebra_close_res (ZebraHandle zh)
727 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
731 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
735 assert (zh->reg == 0);
736 assert (*zh->reg_name == 0);
742 resultSetInvalidate (zh);
743 zebra_register_close(zh->service, zh->reg);
749 zh->reg_name = xstrdup (new_reg);
753 if (zh->service->path_root)
755 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
756 strlen(zh->reg_name) + 3);
757 strcpy (zh->path_reg, zh->service->path_root);
760 strcat (zh->path_reg, "/");
761 strcat (zh->path_reg, zh->reg_name);
767 zebra_lock_destroy (zh->lock_normal);
771 zebra_lock_destroy (zh->lock_shadow);
777 const char *lock_area = res_get (zh->res, "lockDir");
779 if (!lock_area && zh->path_reg)
780 res_set (zh->res, "lockDir", zh->path_reg);
781 sprintf (fname, "norm.%s.LCK", zh->reg_name);
783 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
785 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
787 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
789 if (!zh->lock_normal || !zh->lock_shadow)
793 zebra_lock_destroy(zh->lock_normal);
798 zebra_lock_destroy(zh->lock_shadow);
807 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
808 zebra_set_approx_limit(zh, approx);
812 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
813 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
817 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
820 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
821 zh->m_segment_indexing);
826 void map_basenames_func (void *vp, const char *name, const char *value)
828 struct map_baseinfo *p = (struct map_baseinfo *) vp;
830 char fromdb[128], todb[8][128];
837 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
838 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
839 todb[5], todb[6], todb[7]);
843 for (i = 0; i<p->num_bases; i++)
844 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
847 for (i = 0; i < no; i++)
849 if (p->new_num_bases == p->new_num_max)
851 p->new_basenames[(p->new_num_bases)++] =
852 nmem_strdup (p->mem, todb[i]);
858 int zebra_select_default_database(ZebraHandle zh)
862 /* no database has been selected - so we select based on
863 resource setting (including group)
865 const char *group = res_get(zh->session_res, "group");
866 const char *v = res_get_prefix(zh->session_res,
867 "database", group, "Default");
868 return zebra_select_database(zh, v);
873 void map_basenames (ZebraHandle zh, ODR stream,
874 int *num_bases, char ***basenames)
876 struct map_baseinfo info;
877 struct map_baseinfo *p = &info;
880 yaz_log(log_level, "map_basenames ");
885 info.num_bases = *num_bases;
886 info.basenames = *basenames;
887 info.new_num_max = 128;
888 info.new_num_bases = 0;
889 info.new_basenames = (char **)
890 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
891 info.mem = stream->mem;
893 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
895 for (i = 0; i<p->num_bases; i++)
896 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
898 p->new_basenames[(p->new_num_bases)++] =
899 nmem_strdup (p->mem, p->basenames[i]);
901 *num_bases = info.new_num_bases;
902 *basenames = info.new_basenames;
903 for (i = 0; i<*num_bases; i++)
904 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
907 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
909 ZEBRA_CHECK_HANDLE(zh);
911 yaz_log(log_level, "zebra_select_database %s",basename);
913 return zebra_select_databases (zh, 1, &basename);
916 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
917 const char **basenames)
924 ZEBRA_CHECK_HANDLE(zh);
927 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
928 num_bases,basenames[0]);
933 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
937 /* Check if the user has access to all databases (Seb) */
938 /* You could argue that this should happen later, after we have
939 * determined that the database(s) exist. */
940 if (zh->dbaccesslist) {
941 for (i = 0; i < num_bases; i++) {
942 const char *db = basenames[i];
944 for (p = zh->dbaccesslist; p && *p; p = pp) {
946 if ((pp = strchr(p, '+'))) {
952 if (len == strlen(db) && !strncmp(db, p, len))
956 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
962 for (i = 0; i < zh->num_basenames; i++)
963 xfree(zh->basenames[i]);
964 xfree(zh->basenames);
966 zh->num_basenames = num_bases;
967 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
968 for (i = 0; i < zh->num_basenames; i++)
969 zh->basenames[i] = xstrdup (basenames[i]);
971 cp = strrchr(basenames[0], '/');
974 len = cp - basenames[0];
975 new_reg = xmalloc(len + 1);
976 memcpy (new_reg, basenames[0], len);
980 new_reg = xstrdup ("");
981 for (i = 1; i<num_bases; i++)
985 cp1 = strrchr (basenames[i], '/');
990 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
993 if (len != cp1 - basenames[i] ||
994 memcmp (basenames[i], new_reg, len))
996 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1004 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1009 zebra_select_register (zh, new_reg);
1013 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1016 if (!zh->lock_normal || !zh->lock_shadow)
1018 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1024 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1026 if (approx_limit == 0)
1027 approx_limit = DEFAULT_APPROX_LIMIT;
1028 zh->approx_limit = approx_limit;
1032 void zebra_set_partial_result(ZebraHandle zh)
1034 zh->partial_result = 1;
1038 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1039 int (*f)(void *client_data),
1042 zh->break_handler_func = f;
1043 zh->break_handler_data = client_data;
1047 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1048 const char *setname, zint *hits,
1049 int *estimated_hit_count,
1050 int *partial_resultset)
1054 ZEBRA_CHECK_HANDLE(zh);
1060 yaz_log(log_level, "zebra_search_rpn");
1062 zh->partial_result = 0;
1064 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1067 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1068 zh->num_basenames, zh->basenames, setname,
1069 hits, estimated_hit_count);
1071 *partial_resultset = zh->partial_result;
1076 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1077 const char *setname, zint *hits)
1079 int estimated_hit_count;
1080 int partial_resultset;
1081 return zebra_search_RPN_x(zh, o, query, setname, hits,
1082 &estimated_hit_count,
1083 &partial_resultset);
1086 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1087 const char *setname,
1088 Z_RecordComposition *comp,
1089 const Odr_oid *input_format, int num_recs,
1090 ZebraRetrievalRecord *recs)
1092 ZebraMetaRecord *poset;
1094 ZEBRA_RES ret = ZEBRA_OK;
1097 ZEBRA_CHECK_HANDLE(zh);
1103 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1107 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1112 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1115 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1116 for (i = 0; i<num_recs; i++)
1117 pos_array[i] = recs[i].position;
1118 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1121 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1122 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1128 for (i = 0; i<num_recs; i++)
1132 recs[i].errCode = 0;
1133 recs[i].format = yaz_oid_recsyn_sutrs;
1134 recs[i].len = strlen(poset[i].term);
1135 recs[i].buf = poset[i].term;
1136 recs[i].base = poset[i].db;
1138 else if (poset[i].sysno)
1142 zebra_snippets *hit_snippet = zebra_snippets_create();
1144 /* we disable hit snippets for now. It does not work well
1145 and it slows retrieval down a lot */
1147 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1151 zebra_record_fetch(zh, setname,
1152 poset[i].sysno, poset[i].score,
1154 stream, input_format, comp,
1155 &recs[i].format, &buf, &len,
1156 &recs[i].base, &recs[i].errString);
1161 recs[i].buf = (char*) odr_malloc(stream, len);
1162 memcpy(recs[i].buf, buf, len);
1166 recs[i].score = poset[i].score;
1167 recs[i].sysno = poset[i].sysno;
1168 zebra_snippets_destroy(hit_snippet);
1172 /* only need to set it once */
1173 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1175 zebra_setError_zint(zh,
1176 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1181 recs[i].buf = 0; /* no record and no error issued */
1183 recs[i].errCode = 0;
1188 zebra_meta_records_destroy(zh, poset, num_recs);
1190 zebra_end_read (zh);
1195 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1197 int *num_entries, ZebraScanEntry **entries,
1199 const char *setname)
1201 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1202 Z_AttributesPlusTerm *zapt;
1203 Odr_oid *attributeSet;
1206 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1209 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1213 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1214 position, num_entries, entries, is_partial,
1217 yaz_pqf_destroy (pqf_parser);
1221 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1222 const Odr_oid *attributeset,
1224 int *num_entries, ZebraScanEntry **entries,
1226 const char *setname)
1229 RSET limit_rset = 0;
1231 ZEBRA_CHECK_HANDLE(zh);
1236 assert(num_entries);
1239 yaz_log(log_level, "zebra_scan");
1241 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1249 limit_rset = resultSetRef(zh, setname);
1253 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1255 zebra_end_read (zh);
1259 res = rpn_scan(zh, stream, zapt, attributeset,
1260 zh->num_basenames, zh->basenames, position,
1261 num_entries, entries, is_partial, limit_rset);
1266 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1267 int num_input_setnames, const char **input_setnames,
1268 const char *output_setname,
1269 Z_SortKeySpecList *sort_sequence,
1273 ZEBRA_CHECK_HANDLE(zh);
1275 assert(num_input_setnames>0);
1276 assert(input_setnames);
1277 assert(sort_sequence);
1278 assert(sort_status);
1279 yaz_log(log_level, "zebra_sort");
1281 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1283 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1284 output_setname, sort_sequence, sort_status);
1289 int zebra_deleteResultSet(ZebraHandle zh, int function,
1290 int num_setnames, char **setnames,
1296 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1298 if (zebra_begin_read(zh))
1299 return Z_DeleteStatus_systemProblemAtTarget;
1302 case Z_DeleteResultSetRequest_list:
1303 assert(num_setnames>0);
1305 resultSetDestroy (zh, num_setnames, setnames, statuses);
1307 case Z_DeleteResultSetRequest_all:
1308 resultSetDestroy (zh, -1, 0, statuses);
1311 zebra_end_read (zh);
1312 status = Z_DeleteStatus_success;
1313 for (i = 0; i<num_setnames; i++)
1314 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1315 status = statuses[i];
1319 int zebra_errCode (ZebraHandle zh)
1323 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1326 yaz_log(log_level, "zebra_errCode: o");
1330 const char *zebra_errString (ZebraHandle zh)
1334 e= diagbib1_str (zh->errCode);
1335 yaz_log(log_level, "zebra_errString: %s",e);
1339 char *zebra_errAdd (ZebraHandle zh)
1344 yaz_log(log_level, "zebra_errAdd: %s",a);
1348 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1351 const char *astring;
1355 ZEBRA_CHECK_HANDLE(zh);
1359 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1360 p = res_get(zs->global_res, u);
1361 xfree(zh->user_perm);
1362 zh->user_perm = xstrdup(p ? p : "r");
1364 /* Determine database access list */
1365 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1367 zh->dbaccesslist = xstrdup(astring);
1369 zh->dbaccesslist = 0;
1371 /* users that don't require a password .. */
1372 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1375 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1380 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1381 const char *record_type)
1383 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1384 database, record_type);
1385 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1387 return zebra_begin_trans(zh, 1);
1390 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1392 ZEBRA_CHECK_HANDLE(zh);
1393 yaz_log(log_level, "zebra_admin_import_end");
1394 return zebra_end_trans(zh);
1397 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1399 ZEBRA_RES res = ZEBRA_OK;
1402 ZEBRA_CHECK_HANDLE(zh);
1403 yaz_log(log_level, "zebra_admin_import_segment");
1405 for (i = 0; i<segment->num_segmentRecords; i++)
1407 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1409 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1411 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1412 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1414 Odr_oct *oct = fragment->u.notExternallyTagged;
1417 if (zebra_update_record(
1420 0, /* record Type */
1424 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1432 int delete_w_handle(const char *info, void *handle)
1434 ZebraHandle zh = (ZebraHandle) handle;
1438 if (*info == sizeof(pos))
1440 memcpy (&pos, info+1, sizeof(pos));
1441 isamb_unlink(zh->reg->isamb, pos);
1446 static int delete_SU_handle(void *handle, int ord)
1448 ZebraHandle zh = (ZebraHandle) handle;
1452 ord_len = key_SU_encode (ord, ord_buf);
1453 ord_buf[ord_len] = '\0';
1455 assert (zh->reg->isamb);
1456 dict_delete_subtree(zh->reg->dict, ord_buf,
1457 zh, delete_w_handle);
1461 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1463 ZEBRA_RES ret = ZEBRA_OK;
1465 yaz_log(log_level, "zebra_drop_database %s", db);
1466 ZEBRA_CHECK_HANDLE(zh);
1468 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1470 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1475 if (zebraExplain_curDatabase (zh->reg->zei, db))
1477 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1482 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1483 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1484 0 /* handle */, 0 /* func */);
1485 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1486 zebraExplain_removeDatabase(zh->reg->zei, zh);
1487 zebra_remove_file_match(zh);
1492 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1493 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1494 "drop database only supported for isam:b");
1497 if (zebra_end_trans (zh) != ZEBRA_OK)
1499 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1505 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1507 yaz_log(log_level, "zebra_create_database %s", db);
1508 ZEBRA_CHECK_HANDLE(zh);
1511 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1513 if (zebra_begin_trans (zh, 1))
1516 /* announce database */
1517 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1518 /* explainDatabase */))
1520 if (zebra_end_trans (zh) != ZEBRA_OK)
1522 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1524 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1527 return zebra_end_trans (zh);
1530 int zebra_string_norm(ZebraHandle zh, unsigned reg_id,
1531 const char *input_str, int input_len,
1532 char *output_str, int output_len)
1538 yaz_log(log_level, "zebra_string_norm ");
1540 if (!zh->reg->zebra_maps)
1542 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1543 input_str, input_len);
1546 if (wrbuf_len(wrbuf) >= output_len)
1548 if (wrbuf_len(wrbuf))
1549 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1550 output_str[wrbuf_len(wrbuf)] = '\0';
1551 return wrbuf_len(wrbuf);
1554 /** \brief set register state (state*.LCK)
1555 \param zh Zebra handle
1557 \param seqno sequence number
1560 d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1562 c=commit (writing to register, reading from shadow, shadow mode only)
1564 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1566 char state_fname[256];
1571 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1573 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1574 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1575 f = fopen (fname, "w");
1577 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1578 fprintf (f, "%c %d %ld\n", val, seqno, p);
1583 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1585 char state_fname[256];
1590 yaz_log(log_level, "zebra_get_state ");
1592 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1593 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1594 f = fopen (fname, "r");
1600 fscanf (f, "%c %d", val, seqno);
1606 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1608 return zebra_begin_trans(zh, 0);
1611 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1613 return zebra_end_trans(zh);
1616 static void read_res_for_transaction(ZebraHandle zh)
1618 const char *group = res_get(zh->res, "group");
1620 /* FIXME - do we still use groups ?? */
1622 zh->m_group = group;
1623 v = res_get_prefix(zh->res, "followLinks", group, "1");
1624 zh->m_follow_links = atoi(v);
1626 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1627 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1629 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1630 zh->m_store_keys = atoi(v);
1632 v = res_get_prefix(zh->res, "storeData", group, "1");
1633 zh->m_store_data = atoi(v);
1635 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1636 zh->m_explain_database = atoi(v);
1638 v = res_get_prefix(zh->res, "openRW", group, "1");
1639 zh->m_flag_rw = atoi(v);
1641 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1642 zh->m_file_verbose_limit = atoi(v);
1645 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1647 ZEBRA_CHECK_HANDLE(zh);
1648 zebra_select_default_database(zh);
1651 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1652 "zebra_begin_trans: no database selected");
1656 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1660 if (rw && !strchr(zh->user_perm, 'w'))
1664 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1675 const char *rval = 0;
1680 read_res_for_transaction(zh);
1683 if (zh->trans_no != 1)
1685 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1686 "zebra_begin_trans: no write trans within read");
1691 resultSetInvalidate (zh);
1692 zebra_register_close(zh->service, zh->reg);
1694 zh->trans_w_no = zh->trans_no;
1696 zh->records_inserted = 0;
1697 zh->records_updated = 0;
1698 zh->records_deleted = 0;
1699 zh->records_processed = 0;
1700 zh->records_skipped = 0;
1702 #if HAVE_SYS_TIMES_H
1706 if (zh->shadow_enable)
1707 rval = res_get (zh->res, "shadow");
1711 zebra_lock_r(zh->lock_normal);
1712 zebra_lock_w(zh->lock_shadow);
1716 zebra_lock_w(zh->lock_normal);
1717 zebra_lock_w(zh->lock_shadow);
1719 zebra_get_state (zh, &val, &seqno);
1722 /* either we didn't finish commit or shadow is dirty */
1725 yaz_log(YLOG_WARN, "previous transaction did not finish "
1726 "(shadow disabled)");
1728 zebra_unlock (zh->lock_shadow);
1729 zebra_unlock (zh->lock_normal);
1730 if (zebra_commit (zh))
1738 zebra_lock_r(zh->lock_normal);
1739 zebra_lock_w(zh->lock_shadow);
1743 zebra_lock_w(zh->lock_normal);
1744 zebra_lock_w(zh->lock_shadow);
1748 zebra_set_state (zh, 'd', seqno);
1750 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1751 1, rval ? 1 : 0, zh->res,
1754 zh->reg->seqno = seqno;
1757 zebra_set_state (zh, 'o', seqno);
1759 zebra_unlock (zh->lock_shadow);
1760 zebra_unlock (zh->lock_normal);
1765 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1766 "zebra_begin_trans: cannot open register");
1767 yaz_log(YLOG_FATAL, "%s", zh->errString);
1770 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1780 if (zh->trans_no != 1)
1782 return zebra_flush_reg (zh);
1784 #if HAVE_SYS_TIMES_H
1790 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1793 if (!zh->lock_normal || !zh->lock_shadow)
1796 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1799 zebra_get_state (zh, &val, &seqno);
1805 else if (seqno != zh->reg->seqno)
1807 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1808 seqno, zh->reg->seqno);
1811 else if (zh->reg->last_val != val)
1813 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1814 val, zh->reg->last_val);
1821 zebra_lock_r (zh->lock_shadow);
1823 zebra_lock_r (zh->lock_normal);
1827 resultSetInvalidate (zh);
1828 zebra_register_close(zh->service, zh->reg);
1830 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1831 0, val == 'c' ? 1 : 0,
1832 zh->res, zh->path_reg);
1835 zebra_unlock (zh->lock_normal);
1836 zebra_unlock (zh->lock_shadow);
1838 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1841 zh->reg->last_val = val;
1842 zh->reg->seqno = seqno;
1844 read_res_for_transaction(zh);
1848 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1850 ZebraTransactionStatus dummy;
1852 yaz_log(log_level, "zebra_end_trans");
1853 ZEBRA_CHECK_HANDLE(zh);
1854 return zebra_end_transaction(zh, &dummy);
1857 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1863 ZEBRA_CHECK_HANDLE(zh);
1866 yaz_log(log_level, "zebra_end_transaction");
1868 status->processed = 0;
1869 status->inserted = 0;
1870 status->updated = 0;
1871 status->deleted = 0;
1875 if (!zh->res || !zh->reg)
1877 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1878 "zebra_end_trans: no open transaction");
1881 if (zh->trans_no != zh->trans_w_no)
1884 if (zh->trans_no != 0)
1887 /* release read lock */
1889 zebra_unlock (zh->lock_normal);
1890 zebra_unlock (zh->lock_shadow);
1893 { /* release write lock */
1897 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1898 rval = res_get (zh->res, "shadow");
1900 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1902 zebra_flush_reg (zh);
1904 resultSetInvalidate (zh);
1906 zebra_register_close(zh->service, zh->reg);
1909 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1910 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1911 zh->records_processed, zh->records_inserted,
1912 zh->records_updated, zh->records_deleted);
1914 status->processed = zh->records_processed;
1915 status->inserted = zh->records_inserted;
1916 status->updated = zh->records_updated;
1917 status->deleted = zh->records_deleted;
1919 zebra_get_state (zh, &val, &seqno);
1922 BFiles bfs = bfs_create (rval, zh->path_reg);
1923 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1924 bf_commitClean (bfs, rval);
1929 zebra_set_state (zh, 'o', seqno);
1931 zebra_unlock (zh->lock_shadow);
1932 zebra_unlock (zh->lock_normal);
1935 #if HAVE_SYS_TIMES_H
1937 yaz_log (log_level, "user/system: %ld/%ld",
1938 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1939 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1941 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1942 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1947 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1951 yaz_log (log_level, "updating %s", path);
1953 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1954 return zebra_update_file_match(zh, path);
1956 return zebra_update_from_path(zh, path);
1959 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1963 yaz_log (log_level, "deleting %s", path);
1964 return zebra_delete_from_path(zh, path);
1967 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1971 yaz_log(log_level, "zebra_repository_show");
1972 repositoryShow (zh, path);
1976 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1982 ZEBRA_RES res = ZEBRA_OK;
1986 zebra_select_default_database(zh);
1989 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1992 rval = res_get(zh->res, "shadow");
1995 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1999 zebra_lock_w(zh->lock_normal);
2000 zebra_lock_r(zh->lock_shadow);
2002 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2005 zebra_unlock(zh->lock_shadow);
2006 zebra_unlock(zh->lock_normal);
2009 zebra_get_state(zh, &val, &seqno);
2013 /* shadow area is dirty and so we must throw it away */
2014 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2019 bf_cache (bfs, rval);
2020 if (bf_commitExists (bfs))
2023 zebra_set_state(zh, 'd', seqno);
2026 zebra_set_state(zh, 'c', seqno);
2028 yaz_log(YLOG_DEBUG, "commit start");
2029 if (bf_commitExec (bfs))
2032 if (res == ZEBRA_OK)
2035 zebra_set_state(zh, 'o', seqno);
2037 zebra_unlock(zh->lock_shadow);
2038 zebra_unlock(zh->lock_normal);
2040 zebra_lock_w(zh->lock_shadow);
2041 bf_commitClean(bfs, rval);
2042 zebra_unlock(zh->lock_shadow);
2046 zebra_unlock(zh->lock_shadow);
2047 zebra_unlock(zh->lock_normal);
2048 yaz_log(YLOG_WARN, "zebra_commit: failed");
2053 zebra_unlock(zh->lock_shadow);
2054 zebra_unlock(zh->lock_normal);
2055 yaz_log(log_level, "nothing to commit");
2062 ZEBRA_RES zebra_clean(ZebraHandle zh)
2064 yaz_log(log_level, "zebra_clean");
2065 ZEBRA_CHECK_HANDLE(zh);
2066 return zebra_commit_ex(zh, 1);
2069 ZEBRA_RES zebra_commit(ZebraHandle zh)
2071 yaz_log(log_level, "zebra_commit");
2072 ZEBRA_CHECK_HANDLE(zh);
2073 return zebra_commit_ex(zh, 0);
2077 ZEBRA_RES zebra_init(ZebraHandle zh)
2082 yaz_log(log_level, "zebra_init");
2084 ZEBRA_CHECK_HANDLE(zh);
2086 zebra_select_default_database(zh);
2089 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2090 "cannot select default database");
2093 rval = res_get (zh->res, "shadow");
2095 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2098 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2102 bf_cache (bfs, rval);
2106 zebra_set_state (zh, 'o', 0);
2110 ZEBRA_RES zebra_compact(ZebraHandle zh)
2114 yaz_log(log_level, "zebra_compact");
2115 ZEBRA_CHECK_HANDLE(zh);
2118 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2121 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2127 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2129 yaz_log(log_level, "zebra_result");
2132 *code = zh->errCode;
2133 *addinfo = zh->errString;
2137 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2138 *addinfo ="ZebraHandle is NULL";
2142 void zebra_shadow_enable(ZebraHandle zh, int value)
2145 yaz_log(log_level, "zebra_shadow_enable");
2146 zh->shadow_enable = value;
2149 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2151 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2152 ZEBRA_CHECK_HANDLE(zh);
2155 if (zh->iconv_to_utf8 != 0)
2156 yaz_iconv_close(zh->iconv_to_utf8);
2157 if (zh->iconv_from_utf8 != 0)
2158 yaz_iconv_close(zh->iconv_from_utf8);
2161 yaz_iconv_open ("UTF-8", encoding);
2162 if (zh->iconv_to_utf8 == 0)
2163 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2164 zh->iconv_from_utf8 =
2165 yaz_iconv_open (encoding, "UTF-8");
2166 if (zh->iconv_to_utf8 == 0)
2167 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2172 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2174 yaz_log(log_level, "zebra_record_encoding");
2175 ZEBRA_CHECK_HANDLE(zh);
2176 xfree(zh->record_encoding);
2177 zh->record_encoding = 0;
2179 zh->record_encoding = xstrdup (encoding);
2183 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2187 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2189 res_set(zh->res, name, value);
2192 const char *zebra_get_resource(ZebraHandle zh,
2193 const char *name, const char *defaultvalue)
2198 v = res_get_def (zh->res, name, (char *)defaultvalue);
2199 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2203 /* moved from zebra_api_ext.c by pop */
2204 /* FIXME: Should this really be public??? -Heikki */
2206 int zebra_trans_no (ZebraHandle zh)
2208 yaz_log(log_level, "zebra_trans_no");
2210 return zh->trans_no;
2213 int zebra_get_shadow_enable (ZebraHandle zh)
2215 yaz_log(log_level, "zebra_get_shadow_enable");
2217 return zh->shadow_enable;
2220 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2222 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2224 zh->shadow_enable = value;
2227 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2228 const char *buf, int buf_size)
2230 return zebra_update_record(zh, action_update,
2231 0 /* record type */,
2238 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2239 enum zebra_recctrl_action_t action,
2240 const char *recordType,
2241 zint *sysno, const char *match,
2243 const char *buf, int buf_size)
2247 ZEBRA_CHECK_HANDLE(zh);
2251 yaz_log(log_level, "zebra_update_record");
2253 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2256 buf_size = strlen(buf);
2258 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2260 res = zebra_buffer_extract_record(zh, buf, buf_size,
2267 if (zebra_end_trans(zh) != ZEBRA_OK)
2269 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2275 /* ---------------------------------------------------------------------------
2279 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2280 const char *setname, zint *hits)
2283 ZEBRA_RES res = ZEBRA_OK;
2288 ZEBRA_CHECK_HANDLE(zh);
2290 odr = odr_createmem(ODR_ENCODE);
2295 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2297 query = p_query_rpn(odr, pqf_query);
2301 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2302 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2306 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2310 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2318 /* ---------------------------------------------------------------------------
2319 Sort - a simplified interface, with optional read locks.
2321 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2322 const char *sort_spec,
2323 const char *output_setname,
2324 const char **input_setnames)
2326 int num_input_setnames = 0;
2327 int sort_status = 0;
2328 Z_SortKeySpecList *sort_sequence;
2330 ZEBRA_CHECK_HANDLE(zh);
2333 assert(output_setname);
2334 assert(input_setnames);
2335 sort_sequence = yaz_sort_spec (stream, sort_spec);
2336 yaz_log(log_level, "sort (FIXME) ");
2339 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2340 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2344 /* we can do this, since the perl typemap code for char** will
2345 put a NULL at the end of list */
2346 while (input_setnames[num_input_setnames]) num_input_setnames++;
2348 if (zebra_begin_read (zh))
2351 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2352 output_setname, sort_sequence, &sort_status);
2358 /* ---------------------------------------------------------------------------
2359 Get BFS for Zebra system (to make alternative storage methods)
2361 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2364 return zh->reg->bfs;
2369 /* ---------------------------------------------------------------------------
2370 Set limit for search/scan
2372 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2374 ZEBRA_CHECK_HANDLE(zh);
2375 zebra_limit_destroy(zh->m_limit);
2376 zh->m_limit = zebra_limit_create(complement_flag, ids);
2381 Set Error code + addinfo
2383 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2388 nmem_reset(zh->nmem_error);
2389 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2392 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2395 sprintf(vstr, ZINT_FORMAT, i);
2398 nmem_reset(zh->nmem_error);
2399 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2402 void zebra_lock_prefix(Res res, char *path)
2404 const char *lock_dir = res_get_def (res, "lockDir", "");
2406 strcpy(path, lock_dir);
2407 if (*path && path[strlen(path)-1] != '/')
2414 * indent-tabs-mode: nil
2416 * vim: shiftwidth=4 tabstop=8 expandtab