1 /* $Id: zebraapi.c,v 1.238 2006-12-18 23:40:07 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <yaz/diagbib1.h>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
42 #include <idzebra/api.h>
44 #define DEFAULT_APPROX_LIMIT 2000000000
46 /* simple asserts to validate the most essential input args */
47 #define ASSERTZH assert(zh && zh->service)
48 #define ASSERTZHRES assert(zh && zh->service && zh->res)
49 #define ASSERTZS assert(zs)
51 static int log_level = 0;
52 static int log_level_initialized = 0;
54 static void zebra_open_res(ZebraHandle zh);
55 static void zebra_close_res(ZebraHandle zh);
57 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
64 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
66 static void zebra_chdir (ZebraService zs)
70 yaz_log(log_level, "zebra_chdir");
71 dir = res_get (zs->global_res, "chdir");
74 yaz_log (YLOG_DEBUG, "chdir %s", dir);
82 static ZEBRA_RES zebra_flush_reg (ZebraHandle zh)
84 ZEBRA_CHECK_HANDLE(zh);
85 yaz_log(log_level, "zebra_flush_reg");
86 zebraExplain_flush (zh->reg->zei, zh);
88 key_block_flush(zh->reg->key_block, 1);
90 zebra_index_merge(zh);
94 static struct zebra_register *zebra_register_open(ZebraService zs,
96 int rw, int useshadow,
98 const char *reg_path);
99 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
101 ZebraHandle zebra_open(ZebraService zs, Res res)
104 const char *default_encoding;
105 if (!log_level_initialized)
107 log_level = yaz_log_module_level("zebraapi");
108 log_level_initialized = 1;
111 yaz_log(log_level, "zebra_open");
116 zh = (ZebraHandle) xmalloc(sizeof(*zh));
117 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
120 zh->reg = 0; /* no register attached yet */
126 zh->session_res = res_open(zs->global_res, res);
128 zh->dbaccesslist = 0;
130 zh->reg_name = xstrdup ("");
132 zh->num_basenames = 0;
135 zh->approx_limit = DEFAULT_APPROX_LIMIT;
142 zh->shadow_enable = 1;
143 zh->m_staticrank = 0;
144 zh->m_segment_indexing = 0;
146 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
149 yaz_iconv_open ("UTF-8", default_encoding);
150 if (zh->iconv_to_utf8 == 0)
151 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
153 zh->iconv_from_utf8 =
154 yaz_iconv_open (default_encoding, "UTF-8");
155 if (zh->iconv_to_utf8 == 0)
156 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
159 zh->record_encoding = 0;
161 zebra_mutex_cond_lock (&zs->session_lock);
163 zh->next = zs->sessions;
166 zebra_mutex_cond_unlock (&zs->session_lock);
168 zh->store_data_buf = 0;
170 zh->m_limit = zebra_limit_create(1, 0);
172 zh->nmem_error = nmem_create();
177 ZebraService zebra_start(const char *configName)
179 return zebra_start_res(configName, 0, 0);
182 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
188 if (!log_level_initialized)
190 log_level = yaz_log_module_level("zebraapi");
191 log_level_initialized = 1;
194 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
195 configName ? configName : "");
197 if ((res = res_open(def_res, over_res)))
199 const char *passwd_plain = 0;
200 const char *passwd_encrypt = 0;
201 const char *dbaccess = 0;
206 ZEBRA_RES ret = res_read_file(res, configName);
212 if (zebra_check_res(res))
214 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
221 zebra_check_res(res);
224 zh = xmalloc(sizeof(*zh));
225 zh->global_res = res;
230 zebra_mutex_cond_init (&zh->session_lock);
231 passwd_plain = res_get (zh->global_res, "passwd");
232 passwd_encrypt = res_get (zh->global_res, "passwd.c");
233 dbaccess = res_get (zh->global_res, "dbaccess");
235 if (!passwd_plain && !passwd_encrypt)
236 zh->passwd_db = NULL;
239 zh->passwd_db = passwd_db_open();
241 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
245 passwd_db_file_plain(zh->passwd_db, passwd_plain);
247 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
254 zh->dbaccess = res_open(NULL, NULL);
255 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
256 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
261 zh->path_root = res_get (zh->global_res, "root");
262 zh->nmem = nmem_create();
263 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
267 const char *module_path = res_get(res, "modulePath");
269 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
277 void zebra_filter_info(ZebraService zs, void *cd,
278 void (*cb)(void *cd, const char *name))
282 recTypeClass_info(zs->record_classes, cd, cb);
285 void zebra_pidfname(ZebraService zs, char *path)
288 zebra_lock_prefix (zs->global_res, path);
289 strcat(path, "zebrasrv.pid");
292 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
293 int compact_flag, Res res)
295 int page_size = 4096;
296 char resource_str[200];
297 sprintf (resource_str, "dict.%.100s.pagesize", name);
301 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
302 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
304 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
308 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
309 int rw, int useshadow, Res res,
310 const char *reg_path)
312 struct zebra_register *reg;
313 int record_compression = REC_COMPRESS_NONE;
314 const char *recordCompression = 0;
315 const char *profilePath;
317 int sort_type = ZEBRA_SORT_TYPE_FLAT;
318 ZEBRA_RES ret = ZEBRA_OK;
322 reg = xmalloc(sizeof(*reg));
325 reg->name = xstrdup (name);
332 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
333 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
335 reg->dh = data1_create();
342 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
345 data1_destroy(reg->dh);
352 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
354 bfs_destroy(reg->bfs);
355 data1_destroy(reg->dh);
362 getcwd(cwd, sizeof(cwd)-1);
363 profilePath = res_get_def(res, "profilePath", 0);
365 data1_set_tabpath (reg->dh, profilePath);
366 data1_set_tabroot (reg->dh, reg_path);
367 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
370 zebra_maps_open(res, reg_path, profilePath);
371 if (!reg->zebra_maps)
373 recTypes_destroy(reg->recTypes);
374 bfs_destroy(reg->bfs);
375 data1_destroy(reg->dh);
380 reg->rank_classes = NULL;
383 reg->keys = zebra_rec_keys_open();
385 reg->sortKeys = zebra_rec_keys_open();
396 /* installing rank classes */
397 zebraRankInstall (reg, rank_1_class);
398 zebraRankInstall (reg, rank_similarity_class);
399 zebraRankInstall (reg, rank_static_class);
401 recordCompression = res_get_def (res, "recordCompression", "none");
402 if (!strcmp (recordCompression, "none"))
403 record_compression = REC_COMPRESS_NONE;
404 if (!strcmp (recordCompression, "bzip2"))
405 record_compression = REC_COMPRESS_BZIP2;
409 const char *index_fname = res_get_def(res, "index", "default.idx");
410 if (index_fname && *index_fname)
412 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
417 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
419 yaz_log (YLOG_WARN, "rec_open failed");
424 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
426 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
428 yaz_log (YLOG_WARN, "dict_open failed");
433 if (res_get_match (res, "sortindex", "f", "f"))
434 sort_type = ZEBRA_SORT_TYPE_FLAT;
435 else if (res_get_match (res, "sortindex", "i", "f"))
436 sort_type = ZEBRA_SORT_TYPE_ISAMB;
439 yaz_log (YLOG_WARN, "bad_value for 'sort:'");
444 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
446 yaz_log (YLOG_WARN, "zebra_sort_open failed");
449 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
451 struct ISAMS_M_s isams_m;
452 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
453 key_isams_m(res, &isams_m))))
455 yaz_log (YLOG_WARN, "isams_open failed");
459 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
461 struct ISAMC_M_s isamc_m;
462 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
463 rw, key_isamc_m(res, &isamc_m))))
465 yaz_log (YLOG_WARN, "isamc_open failed");
469 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
471 struct ISAMC_M_s isamc_m;
473 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
474 rw, key_isamc_m(res, &isamc_m), 0)))
476 yaz_log (YLOG_WARN, "isamb_open failed");
480 if (res_get_match (res, "isam", "bc", 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), 1)))
487 yaz_log (YLOG_WARN, "isamb_open failed");
491 if (res_get_match (res, "isam", "null", 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");
504 reg->zei = zebraExplain_open(reg->records, reg->dh,
506 zebra_extract_explain);
509 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
516 zebra_register_close(zs, reg);
519 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
523 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
525 ZEBRA_CHECK_HANDLE(zh);
526 yaz_log(log_level, "zebra_admin_shutdown");
528 zebra_mutex_cond_lock (&zh->service->session_lock);
529 zh->service->stop_flag = 1;
530 zebra_mutex_cond_unlock (&zh->service->session_lock);
534 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
537 ZEBRA_CHECK_HANDLE(zh);
538 yaz_log(log_level, "zebra_admin_start");
540 zebra_mutex_cond_lock (&zs->session_lock);
541 zebra_mutex_cond_unlock (&zs->session_lock);
545 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
549 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
553 zebraExplain_close (reg->zei);
554 dict_close (reg->dict);
556 dict_close (reg->matchDict);
557 zebra_sort_close(reg->sort_index);
559 isams_close (reg->isams);
561 isamc_close (reg->isamc);
563 isamb_close (reg->isamb);
564 rec_close (®->records);
566 recTypes_destroy (reg->recTypes);
567 zebra_maps_close (reg->zebra_maps);
568 zebraRankDestroy (reg);
569 bfs_destroy (reg->bfs);
570 data1_destroy (reg->dh);
572 zebra_rec_keys_close(reg->keys);
573 zebra_rec_keys_close(reg->sortKeys);
575 key_block_destroy(®->key_block);
580 ZEBRA_RES zebra_stop(ZebraService zs)
584 yaz_log (log_level, "zebra_stop");
588 zebra_close (zs->sessions);
591 zebra_mutex_cond_destroy (&zs->session_lock);
594 passwd_db_close (zs->passwd_db);
596 recTypeClass_destroy(zs->record_classes);
597 nmem_destroy(zs->nmem);
598 res_close (zs->global_res);
603 ZEBRA_RES zebra_close(ZebraHandle zh)
606 struct zebra_session **sp;
609 yaz_log(log_level, "zebra_close");
610 ZEBRA_CHECK_HANDLE(zh);
615 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
616 resultSetDestroy (zh, -1, 0, 0);
619 zebra_register_close(zh->service, zh->reg);
620 zebra_close_res (zh);
621 res_close(zh->session_res);
623 xfree(zh->record_encoding);
625 xfree(zh->dbaccesslist);
627 for (i = 0; i < zh->num_basenames; i++)
628 xfree(zh->basenames[i]);
629 xfree(zh->basenames);
631 if (zh->iconv_to_utf8 != 0)
632 yaz_iconv_close (zh->iconv_to_utf8);
633 if (zh->iconv_from_utf8 != 0)
634 yaz_iconv_close (zh->iconv_from_utf8);
636 zebra_mutex_cond_lock (&zs->session_lock);
637 zebra_lock_destroy (zh->lock_normal);
638 zebra_lock_destroy (zh->lock_shadow);
650 zebra_mutex_cond_unlock (&zs->session_lock);
652 xfree(zh->user_perm);
653 zh->service = 0; /* more likely to trigger an assert */
655 zebra_limit_destroy(zh->m_limit);
657 nmem_destroy(zh->nmem_error);
664 struct map_baseinfo {
670 char **new_basenames;
674 static void zebra_open_res(ZebraHandle zh)
682 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
683 zh->res = res_open(zh->session_res, 0);
684 res_read_file(zh->res, fname);
686 else if (*zh->reg_name == 0)
688 zh->res = res_open(zh->session_res, 0);
692 yaz_log (YLOG_WARN, "no register root specified");
693 zh->res = 0; /* no path for register - fail! */
697 static void zebra_close_res (ZebraHandle zh)
705 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
709 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
713 assert (zh->reg == 0);
714 assert (*zh->reg_name == 0);
720 resultSetInvalidate (zh);
721 zebra_register_close(zh->service, zh->reg);
727 zh->reg_name = xstrdup (new_reg);
731 if (zh->service->path_root)
733 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
734 strlen(zh->reg_name) + 3);
735 strcpy (zh->path_reg, zh->service->path_root);
738 strcat (zh->path_reg, "/");
739 strcat (zh->path_reg, zh->reg_name);
745 zebra_lock_destroy (zh->lock_normal);
749 zebra_lock_destroy (zh->lock_shadow);
755 const char *lock_area = res_get (zh->res, "lockDir");
757 if (!lock_area && zh->path_reg)
758 res_set (zh->res, "lockDir", zh->path_reg);
759 sprintf (fname, "norm.%s.LCK", zh->reg_name);
761 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
763 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
765 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
767 if (!zh->lock_normal || !zh->lock_shadow)
771 zebra_lock_destroy(zh->lock_normal);
776 zebra_lock_destroy(zh->lock_shadow);
785 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
786 zebra_set_approx_limit(zh, approx);
790 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
791 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
795 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
798 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
799 zh->m_segment_indexing);
804 void map_basenames_func (void *vp, const char *name, const char *value)
806 struct map_baseinfo *p = (struct map_baseinfo *) vp;
808 char fromdb[128], todb[8][128];
815 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
816 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
817 todb[5], todb[6], todb[7]);
821 for (i = 0; i<p->num_bases; i++)
822 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
825 for (i = 0; i < no; i++)
827 if (p->new_num_bases == p->new_num_max)
829 p->new_basenames[(p->new_num_bases)++] =
830 nmem_strdup (p->mem, todb[i]);
836 int zebra_select_default_database(ZebraHandle zh)
840 /* no database has been selected - so we select based on
841 resource setting (including group)
843 const char *group = res_get(zh->session_res, "group");
844 const char *v = res_get_prefix(zh->session_res,
845 "database", group, "Default");
846 return zebra_select_database(zh, v);
851 void map_basenames (ZebraHandle zh, ODR stream,
852 int *num_bases, char ***basenames)
854 struct map_baseinfo info;
855 struct map_baseinfo *p = &info;
858 yaz_log(log_level, "map_basenames ");
863 info.num_bases = *num_bases;
864 info.basenames = *basenames;
865 info.new_num_max = 128;
866 info.new_num_bases = 0;
867 info.new_basenames = (char **)
868 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
869 info.mem = stream->mem;
871 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
873 for (i = 0; i<p->num_bases; i++)
874 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
876 p->new_basenames[(p->new_num_bases)++] =
877 nmem_strdup (p->mem, p->basenames[i]);
879 *num_bases = info.new_num_bases;
880 *basenames = info.new_basenames;
881 for (i = 0; i<*num_bases; i++)
882 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
885 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
887 ZEBRA_CHECK_HANDLE(zh);
889 yaz_log(log_level, "zebra_select_database %s",basename);
891 return zebra_select_databases (zh, 1, &basename);
894 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
895 const char **basenames)
902 ZEBRA_CHECK_HANDLE(zh);
905 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
906 num_bases,basenames[0]);
911 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
915 /* Check if the user has access to all databases (Seb) */
916 /* You could argue that this should happen later, after we have
917 * determined that the database(s) exist. */
918 if (zh->dbaccesslist) {
919 for (i = 0; i < num_bases; i++) {
920 const char *db = basenames[i];
922 for (p = zh->dbaccesslist; p && *p; p = pp) {
924 if ((pp = strchr(p, '+'))) {
930 if (len == strlen(db) && !strncmp(db, p, len))
934 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
940 for (i = 0; i < zh->num_basenames; i++)
941 xfree(zh->basenames[i]);
942 xfree(zh->basenames);
944 zh->num_basenames = num_bases;
945 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
946 for (i = 0; i < zh->num_basenames; i++)
947 zh->basenames[i] = xstrdup (basenames[i]);
949 cp = strrchr(basenames[0], '/');
952 len = cp - basenames[0];
953 new_reg = xmalloc(len + 1);
954 memcpy (new_reg, basenames[0], len);
958 new_reg = xstrdup ("");
959 for (i = 1; i<num_bases; i++)
963 cp1 = strrchr (basenames[i], '/');
968 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
971 if (len != cp1 - basenames[i] ||
972 memcmp (basenames[i], new_reg, len))
974 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
982 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
987 zebra_select_register (zh, new_reg);
991 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
994 if (!zh->lock_normal || !zh->lock_shadow)
996 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1002 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1004 if (approx_limit == 0)
1005 approx_limit = DEFAULT_APPROX_LIMIT;
1006 zh->approx_limit = approx_limit;
1010 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1011 const char *setname, zint *hits)
1015 ZEBRA_CHECK_HANDLE(zh);
1021 yaz_log(log_level, "zebra_search_rpn");
1025 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1028 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1029 zh->num_basenames, zh->basenames, setname);
1035 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1036 const char *setname,
1037 Z_RecordComposition *comp,
1038 oid_value input_format, int num_recs,
1039 ZebraRetrievalRecord *recs)
1041 ZebraMetaRecord *poset;
1043 ZEBRA_RES ret = ZEBRA_OK;
1046 ZEBRA_CHECK_HANDLE(zh);
1052 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1056 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1061 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1064 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1065 for (i = 0; i<num_recs; i++)
1066 pos_array[i] = recs[i].position;
1067 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1070 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1071 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1077 for (i = 0; i<num_recs; i++)
1081 recs[i].errCode = 0;
1082 recs[i].format = VAL_SUTRS;
1083 recs[i].len = strlen(poset[i].term);
1084 recs[i].buf = poset[i].term;
1085 recs[i].base = poset[i].db;
1087 else if (poset[i].sysno)
1091 zebra_snippets *hit_snippet = zebra_snippets_create();
1093 /* we disable hit snippets for now. It does not work well
1094 and it slows retrieval down a lot */
1096 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1100 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1102 stream, input_format, comp,
1103 &recs[i].format, &buf, &len,
1104 &recs[i].base, &recs[i].errString);
1109 recs[i].buf = (char*) odr_malloc(stream, len);
1110 memcpy(recs[i].buf, buf, len);
1114 recs[i].score = poset[i].score;
1115 recs[i].sysno = poset[i].sysno;
1116 zebra_snippets_destroy(hit_snippet);
1120 /* only need to set it once */
1121 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1123 zebra_setError_zint(zh,
1124 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1129 recs[i].buf = 0; /* no record and no error issued */
1131 recs[i].errCode = 0;
1132 recs[i].format = VAL_NONE;
1136 zebra_meta_records_destroy(zh, poset, num_recs);
1138 zebra_end_read (zh);
1143 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1145 int *num_entries, ZebraScanEntry **entries,
1147 const char *setname)
1149 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1150 Z_AttributesPlusTerm *zapt;
1154 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1157 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1160 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1161 position, num_entries, entries, is_partial,
1163 yaz_pqf_destroy (pqf_parser);
1167 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1168 oid_value attributeset,
1170 int *num_entries, ZebraScanEntry **entries,
1172 const char *setname)
1175 RSET limit_rset = 0;
1177 ZEBRA_CHECK_HANDLE(zh);
1182 assert(num_entries);
1185 yaz_log(log_level, "zebra_scan");
1187 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1195 limit_rset = resultSetRef(zh, setname);
1199 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1201 zebra_end_read (zh);
1205 res = rpn_scan(zh, stream, zapt, attributeset,
1206 zh->num_basenames, zh->basenames, position,
1207 num_entries, entries, is_partial, limit_rset);
1212 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1213 int num_input_setnames, const char **input_setnames,
1214 const char *output_setname,
1215 Z_SortKeySpecList *sort_sequence,
1219 ZEBRA_CHECK_HANDLE(zh);
1221 assert(num_input_setnames>0);
1222 assert(input_setnames);
1223 assert(sort_sequence);
1224 assert(sort_status);
1225 yaz_log(log_level, "zebra_sort");
1227 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1229 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1230 output_setname, sort_sequence, sort_status);
1235 int zebra_deleteResultSet(ZebraHandle zh, int function,
1236 int num_setnames, char **setnames,
1242 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1244 if (zebra_begin_read(zh))
1245 return Z_DeleteStatus_systemProblemAtTarget;
1248 case Z_DeleteResultSetRequest_list:
1249 assert(num_setnames>0);
1251 resultSetDestroy (zh, num_setnames, setnames, statuses);
1253 case Z_DeleteResultSetRequest_all:
1254 resultSetDestroy (zh, -1, 0, statuses);
1257 zebra_end_read (zh);
1258 status = Z_DeleteStatus_success;
1259 for (i = 0; i<num_setnames; i++)
1260 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1261 status = statuses[i];
1265 int zebra_errCode (ZebraHandle zh)
1269 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1272 yaz_log(log_level, "zebra_errCode: o");
1276 const char *zebra_errString (ZebraHandle zh)
1280 e= diagbib1_str (zh->errCode);
1281 yaz_log(log_level, "zebra_errString: %s",e);
1285 char *zebra_errAdd (ZebraHandle zh)
1290 yaz_log(log_level, "zebra_errAdd: %s",a);
1294 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1297 const char *astring;
1301 ZEBRA_CHECK_HANDLE(zh);
1305 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1306 p = res_get(zs->global_res, u);
1307 xfree(zh->user_perm);
1308 zh->user_perm = xstrdup(p ? p : "r");
1310 /* Determine database access list */
1311 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1313 zh->dbaccesslist = xstrdup(astring);
1315 zh->dbaccesslist = 0;
1317 /* users that don't require a password .. */
1318 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1321 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1326 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1327 const char *record_type)
1329 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1330 database, record_type);
1331 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1333 return zebra_begin_trans(zh, 1);
1336 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1338 ZEBRA_CHECK_HANDLE(zh);
1339 yaz_log(log_level, "zebra_admin_import_end");
1340 return zebra_end_trans(zh);
1343 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1345 ZEBRA_RES res = ZEBRA_OK;
1348 ZEBRA_CHECK_HANDLE(zh);
1349 yaz_log(log_level, "zebra_admin_import_segment");
1351 for (i = 0; i<segment->num_segmentRecords; i++)
1353 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1355 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1357 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1358 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1360 Odr_oct *oct = fragment->u.notExternallyTagged;
1363 if (zebra_update_record(zh,
1364 0, /* record Type */
1368 (const char *) oct->buf, oct->len,
1377 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1378 const char *rec_buf,
1380 const char *recid_buf, size_t recid_len,
1382 /* 1 = insert. Fail it already exists */
1383 /* 2 = replace. Fail it does not exist */
1384 /* 3 = delete. Fail if does not exist */
1385 /* 4 = update. Insert/replace */
1392 ZEBRA_CHECK_HANDLE(zh);
1393 assert(action>0 && action <=4);
1396 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1398 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1400 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1401 "no record ID or empty record ID");
1405 memcpy (recid_z, recid_buf, recid_len);
1406 recid_z[recid_len] = 0;
1408 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1411 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1412 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1415 if (action == 1) /* fail if insert */
1417 if (zebra_end_trans(zh) != ZEBRA_OK)
1418 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1419 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1420 "Cannot insert record: already exist");
1424 memcpy (&sysno, rinfo+1, sizeof(sysno));
1428 if (action == 2 || action == 3) /* fail if delete or update */
1430 if (zebra_end_trans(zh) != ZEBRA_OK)
1431 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1432 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1433 "Cannot delete/update record: does not exist");
1436 action = 1; /* make it an insert (if it's an update).. */
1438 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1439 action == 3 ? 1 : 0 /* delete flag */,
1445 0, /* force update */
1446 1 /* allow update */
1448 if (res == ZEBRA_FAIL)
1450 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1451 "Unable to parse record");
1455 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1456 sizeof(sysno), &sysno);
1458 else if (action == 3)
1460 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1462 if (zebra_end_trans(zh) != ZEBRA_OK)
1464 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1470 int delete_w_handle(const char *info, void *handle)
1472 ZebraHandle zh = (ZebraHandle) handle;
1476 if (*info == sizeof(pos))
1478 memcpy (&pos, info+1, sizeof(pos));
1479 isamb_unlink(zh->reg->isamb, pos);
1484 static int delete_SU_handle(void *handle, int ord)
1486 ZebraHandle zh = (ZebraHandle) handle;
1490 ord_len = key_SU_encode (ord, ord_buf);
1491 ord_buf[ord_len] = '\0';
1493 assert (zh->reg->isamb);
1494 dict_delete_subtree(zh->reg->dict, ord_buf,
1495 zh, delete_w_handle);
1499 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1501 ZEBRA_RES ret = ZEBRA_OK;
1503 yaz_log(log_level, "zebra_drop_database %s", db);
1504 ZEBRA_CHECK_HANDLE(zh);
1506 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1508 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1513 if (zebraExplain_curDatabase (zh->reg->zei, db))
1515 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1520 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1521 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1522 0 /* handle */, 0 /* func */);
1523 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1524 zebraExplain_removeDatabase(zh->reg->zei, zh);
1525 zebra_remove_file_match(zh);
1530 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1531 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1532 "drop database only supported for isam:b");
1535 if (zebra_end_trans (zh) != ZEBRA_OK)
1537 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1543 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1545 yaz_log(log_level, "zebra_create_database %s", db);
1546 ZEBRA_CHECK_HANDLE(zh);
1549 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1551 if (zebra_begin_trans (zh, 1))
1554 /* announce database */
1555 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1556 /* explainDatabase */))
1558 if (zebra_end_trans (zh) != ZEBRA_OK)
1560 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1562 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1565 return zebra_end_trans (zh);
1568 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1569 const char *input_str, int input_len,
1570 char *output_str, int output_len)
1576 yaz_log(log_level, "zebra_string_norm ");
1578 if (!zh->reg->zebra_maps)
1580 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1581 input_str, input_len);
1584 if (wrbuf_len(wrbuf) >= output_len)
1586 if (wrbuf_len(wrbuf))
1587 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1588 output_str[wrbuf_len(wrbuf)] = '\0';
1589 return wrbuf_len(wrbuf);
1592 /** \brief set register state (state*.LCK)
1593 \param zh Zebra handle
1595 \param seqno sequence number
1598 d=writing to shadow(dirty)
1602 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1604 char state_fname[256];
1609 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1611 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1612 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1613 f = fopen (fname, "w");
1615 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1616 fprintf (f, "%c %d %ld\n", val, seqno, p);
1621 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1623 char state_fname[256];
1628 yaz_log(log_level, "zebra_get_state ");
1630 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1631 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1632 f = fopen (fname, "r");
1638 fscanf (f, "%c %d", val, seqno);
1644 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1646 return zebra_begin_trans(zh, 0);
1649 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1651 return zebra_end_trans(zh);
1654 static void read_res_for_transaction(ZebraHandle zh)
1656 const char *group = res_get(zh->res, "group");
1658 /* FIXME - do we still use groups ?? */
1660 zh->m_group = group;
1661 v = res_get_prefix(zh->res, "followLinks", group, "1");
1662 zh->m_follow_links = atoi(v);
1664 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1665 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1667 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1668 zh->m_store_keys = atoi(v);
1670 v = res_get_prefix(zh->res, "storeData", group, "1");
1671 zh->m_store_data = atoi(v);
1673 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1674 zh->m_explain_database = atoi(v);
1676 v = res_get_prefix(zh->res, "openRW", group, "1");
1677 zh->m_flag_rw = atoi(v);
1679 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1680 zh->m_file_verbose_limit = atoi(v);
1683 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1685 ZEBRA_CHECK_HANDLE(zh);
1686 zebra_select_default_database(zh);
1689 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1690 "zebra_begin_trans: no database selected");
1694 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1698 if (rw && !strchr(zh->user_perm, 'w'))
1702 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1713 const char *rval = 0;
1718 read_res_for_transaction(zh);
1721 if (zh->trans_no != 1)
1723 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1724 "zebra_begin_trans: no write trans within read");
1729 resultSetInvalidate (zh);
1730 zebra_register_close(zh->service, zh->reg);
1732 zh->trans_w_no = zh->trans_no;
1734 zh->records_inserted = 0;
1735 zh->records_updated = 0;
1736 zh->records_deleted = 0;
1737 zh->records_processed = 0;
1739 #if HAVE_SYS_TIMES_H
1743 if (zh->shadow_enable)
1744 rval = res_get (zh->res, "shadow");
1748 zebra_lock_r(zh->lock_normal);
1749 zebra_lock_w(zh->lock_shadow);
1753 zebra_lock_w(zh->lock_normal);
1754 zebra_lock_w(zh->lock_shadow);
1756 zebra_get_state (zh, &val, &seqno);
1759 /* either we didn't finish commit or shadow is dirty */
1760 zebra_unlock (zh->lock_shadow);
1761 zebra_unlock (zh->lock_normal);
1762 if (zebra_commit (zh))
1770 zebra_lock_r(zh->lock_normal);
1771 zebra_lock_w(zh->lock_shadow);
1775 zebra_lock_w(zh->lock_normal);
1776 zebra_lock_w(zh->lock_shadow);
1780 zebra_set_state (zh, 'd', seqno);
1782 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1783 1, rval ? 1 : 0, zh->res,
1786 zh->reg->seqno = seqno;
1789 zebra_set_state (zh, 'o', seqno);
1791 zebra_unlock (zh->lock_shadow);
1792 zebra_unlock (zh->lock_normal);
1797 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1798 "zebra_begin_trans: cannot open register");
1799 yaz_log(YLOG_FATAL, "%s", zh->errString);
1802 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1812 if (zh->trans_no != 1)
1814 return zebra_flush_reg (zh);
1816 #if HAVE_SYS_TIMES_H
1822 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1825 if (!zh->lock_normal || !zh->lock_shadow)
1828 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1831 zebra_get_state (zh, &val, &seqno);
1837 else if (seqno != zh->reg->seqno)
1839 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1840 seqno, zh->reg->seqno);
1843 else if (zh->reg->last_val != val)
1845 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1846 val, zh->reg->last_val);
1853 zebra_lock_r (zh->lock_shadow);
1855 zebra_lock_r (zh->lock_normal);
1859 resultSetInvalidate (zh);
1860 zebra_register_close(zh->service, zh->reg);
1862 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1863 0, val == 'c' ? 1 : 0,
1864 zh->res, zh->path_reg);
1867 zebra_unlock (zh->lock_normal);
1868 zebra_unlock (zh->lock_shadow);
1870 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1873 zh->reg->last_val = val;
1874 zh->reg->seqno = seqno;
1876 read_res_for_transaction(zh);
1880 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1882 ZebraTransactionStatus dummy;
1884 yaz_log(log_level, "zebra_end_trans");
1885 ZEBRA_CHECK_HANDLE(zh);
1886 return zebra_end_transaction(zh, &dummy);
1889 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1895 ZEBRA_CHECK_HANDLE(zh);
1898 yaz_log(log_level, "zebra_end_transaction");
1900 status->processed = 0;
1901 status->inserted = 0;
1902 status->updated = 0;
1903 status->deleted = 0;
1907 if (!zh->res || !zh->reg)
1909 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1910 "zebra_end_trans: no open transaction");
1913 if (zh->trans_no != zh->trans_w_no)
1916 if (zh->trans_no != 0)
1919 /* release read lock */
1921 zebra_unlock (zh->lock_normal);
1922 zebra_unlock (zh->lock_shadow);
1925 { /* release write lock */
1929 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1930 rval = res_get (zh->res, "shadow");
1932 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1934 zebra_flush_reg (zh);
1936 resultSetInvalidate (zh);
1938 zebra_register_close(zh->service, zh->reg);
1941 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1942 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1943 zh->records_processed, zh->records_inserted,
1944 zh->records_updated, zh->records_deleted);
1946 status->processed = zh->records_processed;
1947 status->inserted = zh->records_inserted;
1948 status->updated = zh->records_updated;
1949 status->deleted = zh->records_deleted;
1951 zebra_get_state (zh, &val, &seqno);
1954 BFiles bfs = bfs_create (rval, zh->path_reg);
1955 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1956 bf_commitClean (bfs, rval);
1961 zebra_set_state (zh, 'o', seqno);
1963 zebra_unlock (zh->lock_shadow);
1964 zebra_unlock (zh->lock_normal);
1967 #if HAVE_SYS_TIMES_H
1969 yaz_log (log_level, "user/system: %ld/%ld",
1970 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1971 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1973 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1974 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1979 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1983 yaz_log (log_level, "updating %s", path);
1985 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1986 return zebra_update_file_match(zh, path);
1988 return zebra_update_from_path(zh, path);
1991 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1995 yaz_log (log_level, "deleting %s", path);
1996 return zebra_delete_from_path(zh, path);
1999 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2003 yaz_log(log_level, "zebra_repository_show");
2004 repositoryShow (zh, path);
2008 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2014 ZEBRA_RES res = ZEBRA_OK;
2018 zebra_select_default_database(zh);
2021 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2024 rval = res_get(zh->res, "shadow");
2027 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2031 zebra_lock_w(zh->lock_normal);
2032 zebra_lock_r(zh->lock_shadow);
2034 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2037 zebra_unlock(zh->lock_shadow);
2038 zebra_unlock(zh->lock_normal);
2041 zebra_get_state(zh, &val, &seqno);
2045 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2050 bf_cache (bfs, rval);
2051 if (bf_commitExists (bfs))
2054 zebra_set_state(zh, 'd', seqno);
2057 zebra_set_state(zh, 'c', seqno);
2059 yaz_log(YLOG_DEBUG, "commit start");
2060 if (bf_commitExec (bfs))
2063 if (res == ZEBRA_OK)
2066 zebra_set_state(zh, 'o', seqno);
2068 zebra_unlock(zh->lock_shadow);
2069 zebra_unlock(zh->lock_normal);
2071 zebra_lock_w(zh->lock_shadow);
2072 bf_commitClean(bfs, rval);
2073 zebra_unlock(zh->lock_shadow);
2077 zebra_unlock(zh->lock_shadow);
2078 zebra_unlock(zh->lock_normal);
2079 yaz_log(YLOG_WARN, "zebra_commit: failed");
2084 zebra_unlock(zh->lock_shadow);
2085 zebra_unlock(zh->lock_normal);
2086 yaz_log(log_level, "nothing to commit");
2093 ZEBRA_RES zebra_clean(ZebraHandle zh)
2095 yaz_log(log_level, "zebra_clean");
2096 ZEBRA_CHECK_HANDLE(zh);
2097 return zebra_commit_ex(zh, 1);
2100 ZEBRA_RES zebra_commit(ZebraHandle zh)
2102 yaz_log(log_level, "zebra_commit");
2103 ZEBRA_CHECK_HANDLE(zh);
2104 return zebra_commit_ex(zh, 0);
2108 ZEBRA_RES zebra_init(ZebraHandle zh)
2113 yaz_log(log_level, "zebra_init");
2115 ZEBRA_CHECK_HANDLE(zh);
2117 zebra_select_default_database(zh);
2120 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2121 "cannot select default database");
2124 rval = res_get (zh->res, "shadow");
2126 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2129 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2133 bf_cache (bfs, rval);
2137 zebra_set_state (zh, 'o', 0);
2141 ZEBRA_RES zebra_compact(ZebraHandle zh)
2145 yaz_log(log_level, "zebra_compact");
2146 ZEBRA_CHECK_HANDLE(zh);
2149 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2152 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2158 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2160 yaz_log(log_level, "zebra_result");
2163 *code = zh->errCode;
2164 *addinfo = zh->errString;
2168 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2169 *addinfo ="ZebraHandle is NULL";
2173 void zebra_shadow_enable(ZebraHandle zh, int value)
2176 yaz_log(log_level, "zebra_shadow_enable");
2177 zh->shadow_enable = value;
2180 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2182 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2183 ZEBRA_CHECK_HANDLE(zh);
2186 if (zh->iconv_to_utf8 != 0)
2187 yaz_iconv_close(zh->iconv_to_utf8);
2188 if (zh->iconv_from_utf8 != 0)
2189 yaz_iconv_close(zh->iconv_from_utf8);
2192 yaz_iconv_open ("UTF-8", encoding);
2193 if (zh->iconv_to_utf8 == 0)
2194 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2195 zh->iconv_from_utf8 =
2196 yaz_iconv_open (encoding, "UTF-8");
2197 if (zh->iconv_to_utf8 == 0)
2198 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2203 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2205 yaz_log(log_level, "zebra_record_encoding");
2206 ZEBRA_CHECK_HANDLE(zh);
2207 xfree(zh->record_encoding);
2208 zh->record_encoding = 0;
2210 zh->record_encoding = xstrdup (encoding);
2214 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2218 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2220 res_set(zh->res, name, value);
2223 const char *zebra_get_resource(ZebraHandle zh,
2224 const char *name, const char *defaultvalue)
2229 v = res_get_def (zh->res, name, (char *)defaultvalue);
2230 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2234 /* moved from zebra_api_ext.c by pop */
2235 /* FIXME: Should this really be public??? -Heikki */
2237 int zebra_trans_no (ZebraHandle zh)
2239 yaz_log(log_level, "zebra_trans_no");
2241 return zh->trans_no;
2244 int zebra_get_shadow_enable (ZebraHandle zh)
2246 yaz_log(log_level, "zebra_get_shadow_enable");
2248 return zh->shadow_enable;
2251 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2253 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2255 zh->shadow_enable = value;
2258 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2259 const char *buf, int buf_size)
2261 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2264 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2265 const char *recordType,
2266 zint *sysno, const char *match,
2268 const char *buf, int buf_size, int force_update)
2274 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2277 buf_size = strlen(buf);
2279 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2281 res = zebra_buffer_extract_record(zh, buf, buf_size,
2282 0, /* delete_flag */
2288 0); /* allow_update */
2289 if (zebra_end_trans(zh) != ZEBRA_OK)
2291 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2297 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2298 const char *recordType,
2299 zint *sysno, const char *match,
2301 const char *buf, int buf_size,
2306 ZEBRA_CHECK_HANDLE(zh);
2310 yaz_log(log_level, "zebra_update_record");
2312 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2314 if (buf_size < 1) buf_size = strlen(buf);
2316 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2318 res = zebra_buffer_extract_record(zh, buf, buf_size,
2319 0, /* delete_flag */
2325 1); /* allow_update */
2326 if (zebra_end_trans(zh) != ZEBRA_OK)
2328 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2334 ZEBRA_RES zebra_delete_record(ZebraHandle zh,
2335 const char *recordType,
2336 zint *sysno, const char *match,
2338 const char *buf, int buf_size,
2343 ZEBRA_CHECK_HANDLE(zh);
2346 yaz_log(log_level, "zebra_delete_record");
2348 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2350 if (buf_size < 1) buf_size = strlen(buf);
2352 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2354 res = zebra_buffer_extract_record(zh, buf, buf_size,
2355 1, /* delete_flag */
2361 1); /* allow_update */
2362 if (zebra_end_trans(zh) != ZEBRA_OK)
2364 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2370 /* ---------------------------------------------------------------------------
2374 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2375 const char *setname, zint *hits)
2378 ZEBRA_RES res = ZEBRA_OK;
2382 ZEBRA_CHECK_HANDLE(zh);
2384 odr = odr_createmem(ODR_ENCODE);
2389 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2391 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2395 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2396 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2400 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2404 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2412 /* ---------------------------------------------------------------------------
2413 Sort - a simplified interface, with optional read locks.
2415 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2416 const char *sort_spec,
2417 const char *output_setname,
2418 const char **input_setnames)
2420 int num_input_setnames = 0;
2421 int sort_status = 0;
2422 Z_SortKeySpecList *sort_sequence;
2424 ZEBRA_CHECK_HANDLE(zh);
2427 assert(output_setname);
2428 assert(input_setnames);
2429 sort_sequence = yaz_sort_spec (stream, sort_spec);
2430 yaz_log(log_level, "sort (FIXME) ");
2433 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2434 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2438 /* we can do this, since the perl typemap code for char** will
2439 put a NULL at the end of list */
2440 while (input_setnames[num_input_setnames]) num_input_setnames++;
2442 if (zebra_begin_read (zh))
2445 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2446 output_setname, sort_sequence, &sort_status);
2452 /* ---------------------------------------------------------------------------
2453 Get BFS for Zebra system (to make alternative storage methods)
2455 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2458 return zh->reg->bfs;
2463 /* ---------------------------------------------------------------------------
2464 Set limit for search/scan
2466 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2468 ZEBRA_CHECK_HANDLE(zh);
2469 zebra_limit_destroy(zh->m_limit);
2470 zh->m_limit = zebra_limit_create(complement_flag, ids);
2475 Set Error code + addinfo
2477 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2482 nmem_reset(zh->nmem_error);
2483 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2486 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2489 sprintf(vstr, ZINT_FORMAT, i);
2492 nmem_reset(zh->nmem_error);
2493 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2496 void zebra_lock_prefix(Res res, char *path)
2498 const char *lock_dir = res_get_def (res, "lockDir", "");
2500 strcpy(path, lock_dir);
2501 if (*path && path[strlen(path)-1] != '/')
2508 * indent-tabs-mode: nil
2510 * vim: shiftwidth=4 tabstop=8 expandtab