1 /* $Id: zebraapi.c,v 1.237 2006-12-05 14:06:29 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 ZEBRA_RES ret = ZEBRA_OK;
321 reg = xmalloc(sizeof(*reg));
324 reg->name = xstrdup (name);
331 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
332 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
334 reg->dh = data1_create();
341 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
344 data1_destroy(reg->dh);
351 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
353 bfs_destroy(reg->bfs);
354 data1_destroy(reg->dh);
361 getcwd(cwd, sizeof(cwd)-1);
362 profilePath = res_get_def(res, "profilePath", 0);
364 data1_set_tabpath (reg->dh, profilePath);
365 data1_set_tabroot (reg->dh, reg_path);
366 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
369 zebra_maps_open(res, reg_path, profilePath);
370 if (!reg->zebra_maps)
372 recTypes_destroy(reg->recTypes);
373 bfs_destroy(reg->bfs);
374 data1_destroy(reg->dh);
379 reg->rank_classes = NULL;
382 reg->keys = zebra_rec_keys_open();
384 reg->sortKeys = zebra_rec_keys_open();
395 /* installing rank classes */
396 zebraRankInstall (reg, rank_1_class);
397 zebraRankInstall (reg, rank_similarity_class);
398 zebraRankInstall (reg, rank_static_class);
400 recordCompression = res_get_def (res, "recordCompression", "none");
401 if (!strcmp (recordCompression, "none"))
402 record_compression = REC_COMPRESS_NONE;
403 if (!strcmp (recordCompression, "bzip2"))
404 record_compression = REC_COMPRESS_BZIP2;
408 const char *index_fname = res_get_def(res, "index", "default.idx");
409 if (index_fname && *index_fname)
411 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
416 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
418 yaz_log (YLOG_WARN, "rec_open failed");
423 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
425 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
427 yaz_log (YLOG_WARN, "dict_open failed");
430 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
432 yaz_log (YLOG_WARN, "sortIdx_open failed");
435 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
437 struct ISAMS_M_s isams_m;
438 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
439 key_isams_m(res, &isams_m))))
441 yaz_log (YLOG_WARN, "isams_open failed");
445 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
447 struct ISAMC_M_s isamc_m;
448 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
449 rw, key_isamc_m(res, &isamc_m))))
451 yaz_log (YLOG_WARN, "isamc_open failed");
455 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
457 struct ISAMC_M_s isamc_m;
459 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
460 rw, key_isamc_m(res, &isamc_m), 0)))
462 yaz_log (YLOG_WARN, "isamb_open failed");
466 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
468 struct ISAMC_M_s isamc_m;
470 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
471 rw, key_isamc_m(res, &isamc_m), 1)))
473 yaz_log (YLOG_WARN, "isamb_open failed");
477 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
479 struct ISAMC_M_s isamc_m;
481 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
482 rw, key_isamc_m(res, &isamc_m), -1)))
484 yaz_log (YLOG_WARN, "isamb_open failed");
490 reg->zei = zebraExplain_open(reg->records, reg->dh,
492 zebra_extract_explain);
495 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
502 zebra_register_close(zs, reg);
505 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
509 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
511 ZEBRA_CHECK_HANDLE(zh);
512 yaz_log(log_level, "zebra_admin_shutdown");
514 zebra_mutex_cond_lock (&zh->service->session_lock);
515 zh->service->stop_flag = 1;
516 zebra_mutex_cond_unlock (&zh->service->session_lock);
520 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
523 ZEBRA_CHECK_HANDLE(zh);
524 yaz_log(log_level, "zebra_admin_start");
526 zebra_mutex_cond_lock (&zs->session_lock);
527 zebra_mutex_cond_unlock (&zs->session_lock);
531 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
535 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
539 zebraExplain_close (reg->zei);
540 dict_close (reg->dict);
542 dict_close (reg->matchDict);
543 sortIdx_close (reg->sortIdx);
545 isams_close (reg->isams);
547 isamc_close (reg->isamc);
549 isamb_close (reg->isamb);
550 rec_close (®->records);
552 recTypes_destroy (reg->recTypes);
553 zebra_maps_close (reg->zebra_maps);
554 zebraRankDestroy (reg);
555 bfs_destroy (reg->bfs);
556 data1_destroy (reg->dh);
558 zebra_rec_keys_close(reg->keys);
559 zebra_rec_keys_close(reg->sortKeys);
561 key_block_destroy(®->key_block);
566 ZEBRA_RES zebra_stop(ZebraService zs)
570 yaz_log (log_level, "zebra_stop");
574 zebra_close (zs->sessions);
577 zebra_mutex_cond_destroy (&zs->session_lock);
580 passwd_db_close (zs->passwd_db);
582 recTypeClass_destroy(zs->record_classes);
583 nmem_destroy(zs->nmem);
584 res_close (zs->global_res);
589 ZEBRA_RES zebra_close(ZebraHandle zh)
592 struct zebra_session **sp;
595 yaz_log(log_level, "zebra_close");
596 ZEBRA_CHECK_HANDLE(zh);
601 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
602 resultSetDestroy (zh, -1, 0, 0);
605 zebra_register_close(zh->service, zh->reg);
606 zebra_close_res (zh);
607 res_close(zh->session_res);
609 xfree(zh->record_encoding);
611 xfree(zh->dbaccesslist);
613 for (i = 0; i < zh->num_basenames; i++)
614 xfree(zh->basenames[i]);
615 xfree(zh->basenames);
617 if (zh->iconv_to_utf8 != 0)
618 yaz_iconv_close (zh->iconv_to_utf8);
619 if (zh->iconv_from_utf8 != 0)
620 yaz_iconv_close (zh->iconv_from_utf8);
622 zebra_mutex_cond_lock (&zs->session_lock);
623 zebra_lock_destroy (zh->lock_normal);
624 zebra_lock_destroy (zh->lock_shadow);
636 zebra_mutex_cond_unlock (&zs->session_lock);
638 xfree(zh->user_perm);
639 zh->service = 0; /* more likely to trigger an assert */
641 zebra_limit_destroy(zh->m_limit);
643 nmem_destroy(zh->nmem_error);
650 struct map_baseinfo {
656 char **new_basenames;
660 static void zebra_open_res(ZebraHandle zh)
668 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
669 zh->res = res_open(zh->session_res, 0);
670 res_read_file(zh->res, fname);
672 else if (*zh->reg_name == 0)
674 zh->res = res_open(zh->session_res, 0);
678 yaz_log (YLOG_WARN, "no register root specified");
679 zh->res = 0; /* no path for register - fail! */
683 static void zebra_close_res (ZebraHandle zh)
691 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
695 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
699 assert (zh->reg == 0);
700 assert (*zh->reg_name == 0);
706 resultSetInvalidate (zh);
707 zebra_register_close(zh->service, zh->reg);
713 zh->reg_name = xstrdup (new_reg);
717 if (zh->service->path_root)
719 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
720 strlen(zh->reg_name) + 3);
721 strcpy (zh->path_reg, zh->service->path_root);
724 strcat (zh->path_reg, "/");
725 strcat (zh->path_reg, zh->reg_name);
731 zebra_lock_destroy (zh->lock_normal);
735 zebra_lock_destroy (zh->lock_shadow);
741 const char *lock_area = res_get (zh->res, "lockDir");
743 if (!lock_area && zh->path_reg)
744 res_set (zh->res, "lockDir", zh->path_reg);
745 sprintf (fname, "norm.%s.LCK", zh->reg_name);
747 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
749 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
751 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
753 if (!zh->lock_normal || !zh->lock_shadow)
757 zebra_lock_destroy(zh->lock_normal);
762 zebra_lock_destroy(zh->lock_shadow);
771 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
772 zebra_set_approx_limit(zh, approx);
776 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
777 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
781 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
784 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
785 zh->m_segment_indexing);
790 void map_basenames_func (void *vp, const char *name, const char *value)
792 struct map_baseinfo *p = (struct map_baseinfo *) vp;
794 char fromdb[128], todb[8][128];
801 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
802 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
803 todb[5], todb[6], todb[7]);
807 for (i = 0; i<p->num_bases; i++)
808 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
811 for (i = 0; i < no; i++)
813 if (p->new_num_bases == p->new_num_max)
815 p->new_basenames[(p->new_num_bases)++] =
816 nmem_strdup (p->mem, todb[i]);
822 int zebra_select_default_database(ZebraHandle zh)
826 /* no database has been selected - so we select based on
827 resource setting (including group)
829 const char *group = res_get(zh->session_res, "group");
830 const char *v = res_get_prefix(zh->session_res,
831 "database", group, "Default");
832 return zebra_select_database(zh, v);
837 void map_basenames (ZebraHandle zh, ODR stream,
838 int *num_bases, char ***basenames)
840 struct map_baseinfo info;
841 struct map_baseinfo *p = &info;
844 yaz_log(log_level, "map_basenames ");
849 info.num_bases = *num_bases;
850 info.basenames = *basenames;
851 info.new_num_max = 128;
852 info.new_num_bases = 0;
853 info.new_basenames = (char **)
854 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
855 info.mem = stream->mem;
857 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
859 for (i = 0; i<p->num_bases; i++)
860 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
862 p->new_basenames[(p->new_num_bases)++] =
863 nmem_strdup (p->mem, p->basenames[i]);
865 *num_bases = info.new_num_bases;
866 *basenames = info.new_basenames;
867 for (i = 0; i<*num_bases; i++)
868 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
871 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
873 ZEBRA_CHECK_HANDLE(zh);
875 yaz_log(log_level, "zebra_select_database %s",basename);
877 return zebra_select_databases (zh, 1, &basename);
880 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
881 const char **basenames)
888 ZEBRA_CHECK_HANDLE(zh);
891 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
892 num_bases,basenames[0]);
897 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
901 /* Check if the user has access to all databases (Seb) */
902 /* You could argue that this should happen later, after we have
903 * determined that the database(s) exist. */
904 if (zh->dbaccesslist) {
905 for (i = 0; i < num_bases; i++) {
906 const char *db = basenames[i];
908 for (p = zh->dbaccesslist; p && *p; p = pp) {
910 if ((pp = strchr(p, '+'))) {
916 if (len == strlen(db) && !strncmp(db, p, len))
920 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
926 for (i = 0; i < zh->num_basenames; i++)
927 xfree(zh->basenames[i]);
928 xfree(zh->basenames);
930 zh->num_basenames = num_bases;
931 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
932 for (i = 0; i < zh->num_basenames; i++)
933 zh->basenames[i] = xstrdup (basenames[i]);
935 cp = strrchr(basenames[0], '/');
938 len = cp - basenames[0];
939 new_reg = xmalloc(len + 1);
940 memcpy (new_reg, basenames[0], len);
944 new_reg = xstrdup ("");
945 for (i = 1; i<num_bases; i++)
949 cp1 = strrchr (basenames[i], '/');
954 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
957 if (len != cp1 - basenames[i] ||
958 memcmp (basenames[i], new_reg, len))
960 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
968 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
973 zebra_select_register (zh, new_reg);
977 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
980 if (!zh->lock_normal || !zh->lock_shadow)
982 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
988 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
990 if (approx_limit == 0)
991 approx_limit = DEFAULT_APPROX_LIMIT;
992 zh->approx_limit = approx_limit;
996 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
997 const char *setname, zint *hits)
1001 ZEBRA_CHECK_HANDLE(zh);
1007 yaz_log(log_level, "zebra_search_rpn");
1011 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1014 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1015 zh->num_basenames, zh->basenames, setname);
1021 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1022 const char *setname,
1023 Z_RecordComposition *comp,
1024 oid_value input_format, int num_recs,
1025 ZebraRetrievalRecord *recs)
1027 ZebraMetaRecord *poset;
1029 ZEBRA_RES ret = ZEBRA_OK;
1032 ZEBRA_CHECK_HANDLE(zh);
1038 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1042 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1047 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1050 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1051 for (i = 0; i<num_recs; i++)
1052 pos_array[i] = recs[i].position;
1053 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1056 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1057 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1063 for (i = 0; i<num_recs; i++)
1067 recs[i].errCode = 0;
1068 recs[i].format = VAL_SUTRS;
1069 recs[i].len = strlen(poset[i].term);
1070 recs[i].buf = poset[i].term;
1071 recs[i].base = poset[i].db;
1073 else if (poset[i].sysno)
1077 zebra_snippets *hit_snippet = zebra_snippets_create();
1079 /* we disable hit snippets for now. It does not work well
1080 and it slows retrieval down a lot */
1082 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1086 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1088 stream, input_format, comp,
1089 &recs[i].format, &buf, &len,
1090 &recs[i].base, &recs[i].errString);
1095 recs[i].buf = (char*) odr_malloc(stream, len);
1096 memcpy(recs[i].buf, buf, len);
1100 recs[i].score = poset[i].score;
1101 recs[i].sysno = poset[i].sysno;
1102 zebra_snippets_destroy(hit_snippet);
1106 /* only need to set it once */
1107 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1109 zebra_setError_zint(zh,
1110 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1115 recs[i].buf = 0; /* no record and no error issued */
1117 recs[i].errCode = 0;
1118 recs[i].format = VAL_NONE;
1122 zebra_meta_records_destroy(zh, poset, num_recs);
1124 zebra_end_read (zh);
1129 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1131 int *num_entries, ZebraScanEntry **entries,
1133 const char *setname)
1135 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1136 Z_AttributesPlusTerm *zapt;
1140 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1143 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1146 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1147 position, num_entries, entries, is_partial,
1149 yaz_pqf_destroy (pqf_parser);
1153 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1154 oid_value attributeset,
1156 int *num_entries, ZebraScanEntry **entries,
1158 const char *setname)
1161 RSET limit_rset = 0;
1163 ZEBRA_CHECK_HANDLE(zh);
1168 assert(num_entries);
1171 yaz_log(log_level, "zebra_scan");
1173 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1181 limit_rset = resultSetRef(zh, setname);
1185 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1187 zebra_end_read (zh);
1191 res = rpn_scan(zh, stream, zapt, attributeset,
1192 zh->num_basenames, zh->basenames, position,
1193 num_entries, entries, is_partial, limit_rset);
1198 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1199 int num_input_setnames, const char **input_setnames,
1200 const char *output_setname,
1201 Z_SortKeySpecList *sort_sequence,
1205 ZEBRA_CHECK_HANDLE(zh);
1207 assert(num_input_setnames>0);
1208 assert(input_setnames);
1209 assert(sort_sequence);
1210 assert(sort_status);
1211 yaz_log(log_level, "zebra_sort");
1213 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1215 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1216 output_setname, sort_sequence, sort_status);
1221 int zebra_deleteResultSet(ZebraHandle zh, int function,
1222 int num_setnames, char **setnames,
1228 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1230 if (zebra_begin_read(zh))
1231 return Z_DeleteStatus_systemProblemAtTarget;
1234 case Z_DeleteResultSetRequest_list:
1235 assert(num_setnames>0);
1237 resultSetDestroy (zh, num_setnames, setnames, statuses);
1239 case Z_DeleteResultSetRequest_all:
1240 resultSetDestroy (zh, -1, 0, statuses);
1243 zebra_end_read (zh);
1244 status = Z_DeleteStatus_success;
1245 for (i = 0; i<num_setnames; i++)
1246 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1247 status = statuses[i];
1251 int zebra_errCode (ZebraHandle zh)
1255 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1258 yaz_log(log_level, "zebra_errCode: o");
1262 const char *zebra_errString (ZebraHandle zh)
1266 e= diagbib1_str (zh->errCode);
1267 yaz_log(log_level, "zebra_errString: %s",e);
1271 char *zebra_errAdd (ZebraHandle zh)
1276 yaz_log(log_level, "zebra_errAdd: %s",a);
1280 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1283 const char *astring;
1287 ZEBRA_CHECK_HANDLE(zh);
1291 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1292 p = res_get(zs->global_res, u);
1293 xfree(zh->user_perm);
1294 zh->user_perm = xstrdup(p ? p : "r");
1296 /* Determine database access list */
1297 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1299 zh->dbaccesslist = xstrdup(astring);
1301 zh->dbaccesslist = 0;
1303 /* users that don't require a password .. */
1304 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1307 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1312 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1313 const char *record_type)
1315 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1316 database, record_type);
1317 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1319 return zebra_begin_trans(zh, 1);
1322 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1324 ZEBRA_CHECK_HANDLE(zh);
1325 yaz_log(log_level, "zebra_admin_import_end");
1326 return zebra_end_trans(zh);
1329 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1331 ZEBRA_RES res = ZEBRA_OK;
1334 ZEBRA_CHECK_HANDLE(zh);
1335 yaz_log(log_level, "zebra_admin_import_segment");
1337 for (i = 0; i<segment->num_segmentRecords; i++)
1339 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1341 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1343 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1344 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1346 Odr_oct *oct = fragment->u.notExternallyTagged;
1349 if (zebra_update_record(zh,
1350 0, /* record Type */
1354 (const char *) oct->buf, oct->len,
1363 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1364 const char *rec_buf,
1366 const char *recid_buf, size_t recid_len,
1368 /* 1 = insert. Fail it already exists */
1369 /* 2 = replace. Fail it does not exist */
1370 /* 3 = delete. Fail if does not exist */
1371 /* 4 = update. Insert/replace */
1378 ZEBRA_CHECK_HANDLE(zh);
1379 assert(action>0 && action <=4);
1382 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1384 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1386 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1387 "no record ID or empty record ID");
1391 memcpy (recid_z, recid_buf, recid_len);
1392 recid_z[recid_len] = 0;
1394 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1397 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1398 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1401 if (action == 1) /* fail if insert */
1403 if (zebra_end_trans(zh) != ZEBRA_OK)
1404 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1405 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1406 "Cannot insert record: already exist");
1410 memcpy (&sysno, rinfo+1, sizeof(sysno));
1414 if (action == 2 || action == 3) /* fail if delete or update */
1416 if (zebra_end_trans(zh) != ZEBRA_OK)
1417 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1418 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1419 "Cannot delete/update record: does not exist");
1422 action = 1; /* make it an insert (if it's an update).. */
1424 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1425 action == 3 ? 1 : 0 /* delete flag */,
1431 0, /* force update */
1432 1 /* allow update */
1434 if (res == ZEBRA_FAIL)
1436 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1437 "Unable to parse record");
1441 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1442 sizeof(sysno), &sysno);
1444 else if (action == 3)
1446 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1448 if (zebra_end_trans(zh) != ZEBRA_OK)
1450 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1456 int delete_w_handle(const char *info, void *handle)
1458 ZebraHandle zh = (ZebraHandle) handle;
1462 if (*info == sizeof(pos))
1464 memcpy (&pos, info+1, sizeof(pos));
1465 isamb_unlink(zh->reg->isamb, pos);
1470 static int delete_SU_handle(void *handle, int ord)
1472 ZebraHandle zh = (ZebraHandle) handle;
1476 ord_len = key_SU_encode (ord, ord_buf);
1477 ord_buf[ord_len] = '\0';
1479 assert (zh->reg->isamb);
1480 dict_delete_subtree(zh->reg->dict, ord_buf,
1481 zh, delete_w_handle);
1485 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1487 ZEBRA_RES ret = ZEBRA_OK;
1489 yaz_log(log_level, "zebra_drop_database %s", db);
1490 ZEBRA_CHECK_HANDLE(zh);
1492 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1494 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1499 if (zebraExplain_curDatabase (zh->reg->zei, db))
1501 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1506 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1507 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1508 0 /* handle */, 0 /* func */);
1509 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1510 zebraExplain_removeDatabase(zh->reg->zei, zh);
1511 zebra_remove_file_match(zh);
1516 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1517 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1518 "drop database only supported for isam:b");
1521 if (zebra_end_trans (zh) != ZEBRA_OK)
1523 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1529 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1531 yaz_log(log_level, "zebra_create_database %s", db);
1532 ZEBRA_CHECK_HANDLE(zh);
1535 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1537 if (zebra_begin_trans (zh, 1))
1540 /* announce database */
1541 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1542 /* explainDatabase */))
1544 if (zebra_end_trans (zh) != ZEBRA_OK)
1546 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1548 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1551 return zebra_end_trans (zh);
1554 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1555 const char *input_str, int input_len,
1556 char *output_str, int output_len)
1562 yaz_log(log_level, "zebra_string_norm ");
1564 if (!zh->reg->zebra_maps)
1566 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1567 input_str, input_len);
1570 if (wrbuf_len(wrbuf) >= output_len)
1572 if (wrbuf_len(wrbuf))
1573 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1574 output_str[wrbuf_len(wrbuf)] = '\0';
1575 return wrbuf_len(wrbuf);
1578 /** \brief set register state (state*.LCK)
1579 \param zh Zebra handle
1581 \param seqno sequence number
1584 d=writing to shadow(dirty)
1588 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1590 char state_fname[256];
1595 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1597 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1598 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1599 f = fopen (fname, "w");
1601 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1602 fprintf (f, "%c %d %ld\n", val, seqno, p);
1607 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1609 char state_fname[256];
1614 yaz_log(log_level, "zebra_get_state ");
1616 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1617 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1618 f = fopen (fname, "r");
1624 fscanf (f, "%c %d", val, seqno);
1630 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1632 return zebra_begin_trans(zh, 0);
1635 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1637 return zebra_end_trans(zh);
1640 static void read_res_for_transaction(ZebraHandle zh)
1642 const char *group = res_get(zh->res, "group");
1644 /* FIXME - do we still use groups ?? */
1646 zh->m_group = group;
1647 v = res_get_prefix(zh->res, "followLinks", group, "1");
1648 zh->m_follow_links = atoi(v);
1650 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1651 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1653 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1654 zh->m_store_keys = atoi(v);
1656 v = res_get_prefix(zh->res, "storeData", group, "1");
1657 zh->m_store_data = atoi(v);
1659 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1660 zh->m_explain_database = atoi(v);
1662 v = res_get_prefix(zh->res, "openRW", group, "1");
1663 zh->m_flag_rw = atoi(v);
1665 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1666 zh->m_file_verbose_limit = atoi(v);
1669 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1671 ZEBRA_CHECK_HANDLE(zh);
1672 zebra_select_default_database(zh);
1675 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1676 "zebra_begin_trans: no database selected");
1680 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1684 if (rw && !strchr(zh->user_perm, 'w'))
1688 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1699 const char *rval = 0;
1704 read_res_for_transaction(zh);
1707 if (zh->trans_no != 1)
1709 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1710 "zebra_begin_trans: no write trans within read");
1715 resultSetInvalidate (zh);
1716 zebra_register_close(zh->service, zh->reg);
1718 zh->trans_w_no = zh->trans_no;
1720 zh->records_inserted = 0;
1721 zh->records_updated = 0;
1722 zh->records_deleted = 0;
1723 zh->records_processed = 0;
1725 #if HAVE_SYS_TIMES_H
1729 if (zh->shadow_enable)
1730 rval = res_get (zh->res, "shadow");
1734 zebra_lock_r(zh->lock_normal);
1735 zebra_lock_w(zh->lock_shadow);
1739 zebra_lock_w(zh->lock_normal);
1740 zebra_lock_w(zh->lock_shadow);
1742 zebra_get_state (zh, &val, &seqno);
1745 /* either we didn't finish commit or shadow is dirty */
1746 zebra_unlock (zh->lock_shadow);
1747 zebra_unlock (zh->lock_normal);
1748 if (zebra_commit (zh))
1756 zebra_lock_r(zh->lock_normal);
1757 zebra_lock_w(zh->lock_shadow);
1761 zebra_lock_w(zh->lock_normal);
1762 zebra_lock_w(zh->lock_shadow);
1766 zebra_set_state (zh, 'd', seqno);
1768 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1769 1, rval ? 1 : 0, zh->res,
1772 zh->reg->seqno = seqno;
1775 zebra_set_state (zh, 'o', seqno);
1777 zebra_unlock (zh->lock_shadow);
1778 zebra_unlock (zh->lock_normal);
1783 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1784 "zebra_begin_trans: cannot open register");
1785 yaz_log(YLOG_FATAL, "%s", zh->errString);
1788 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1798 if (zh->trans_no != 1)
1800 return zebra_flush_reg (zh);
1802 #if HAVE_SYS_TIMES_H
1808 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1811 if (!zh->lock_normal || !zh->lock_shadow)
1814 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1817 zebra_get_state (zh, &val, &seqno);
1823 else if (seqno != zh->reg->seqno)
1825 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1826 seqno, zh->reg->seqno);
1829 else if (zh->reg->last_val != val)
1831 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1832 val, zh->reg->last_val);
1839 zebra_lock_r (zh->lock_shadow);
1841 zebra_lock_r (zh->lock_normal);
1845 resultSetInvalidate (zh);
1846 zebra_register_close(zh->service, zh->reg);
1848 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1849 0, val == 'c' ? 1 : 0,
1850 zh->res, zh->path_reg);
1853 zebra_unlock (zh->lock_normal);
1854 zebra_unlock (zh->lock_shadow);
1856 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1859 zh->reg->last_val = val;
1860 zh->reg->seqno = seqno;
1862 read_res_for_transaction(zh);
1866 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1868 ZebraTransactionStatus dummy;
1870 yaz_log(log_level, "zebra_end_trans");
1871 ZEBRA_CHECK_HANDLE(zh);
1872 return zebra_end_transaction(zh, &dummy);
1875 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1881 ZEBRA_CHECK_HANDLE(zh);
1884 yaz_log(log_level, "zebra_end_transaction");
1886 status->processed = 0;
1887 status->inserted = 0;
1888 status->updated = 0;
1889 status->deleted = 0;
1893 if (!zh->res || !zh->reg)
1895 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1896 "zebra_end_trans: no open transaction");
1899 if (zh->trans_no != zh->trans_w_no)
1902 if (zh->trans_no != 0)
1905 /* release read lock */
1907 zebra_unlock (zh->lock_normal);
1908 zebra_unlock (zh->lock_shadow);
1911 { /* release write lock */
1915 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1916 rval = res_get (zh->res, "shadow");
1918 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1920 zebra_flush_reg (zh);
1922 resultSetInvalidate (zh);
1924 zebra_register_close(zh->service, zh->reg);
1927 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1928 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1929 zh->records_processed, zh->records_inserted,
1930 zh->records_updated, zh->records_deleted);
1932 status->processed = zh->records_processed;
1933 status->inserted = zh->records_inserted;
1934 status->updated = zh->records_updated;
1935 status->deleted = zh->records_deleted;
1937 zebra_get_state (zh, &val, &seqno);
1940 BFiles bfs = bfs_create (rval, zh->path_reg);
1941 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1942 bf_commitClean (bfs, rval);
1947 zebra_set_state (zh, 'o', seqno);
1949 zebra_unlock (zh->lock_shadow);
1950 zebra_unlock (zh->lock_normal);
1953 #if HAVE_SYS_TIMES_H
1955 yaz_log (log_level, "user/system: %ld/%ld",
1956 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1957 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1959 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1960 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1965 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1969 yaz_log (log_level, "updating %s", path);
1971 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1972 return zebra_update_file_match(zh, path);
1974 return zebra_update_from_path(zh, path);
1977 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1981 yaz_log (log_level, "deleting %s", path);
1982 return zebra_delete_from_path(zh, path);
1985 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1989 yaz_log(log_level, "zebra_repository_show");
1990 repositoryShow (zh, path);
1994 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2000 ZEBRA_RES res = ZEBRA_OK;
2004 zebra_select_default_database(zh);
2007 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2010 rval = res_get(zh->res, "shadow");
2013 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2017 zebra_lock_w(zh->lock_normal);
2018 zebra_lock_r(zh->lock_shadow);
2020 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2023 zebra_unlock(zh->lock_shadow);
2024 zebra_unlock(zh->lock_normal);
2027 zebra_get_state(zh, &val, &seqno);
2031 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2036 bf_cache (bfs, rval);
2037 if (bf_commitExists (bfs))
2040 zebra_set_state(zh, 'd', seqno);
2043 zebra_set_state(zh, 'c', seqno);
2045 yaz_log(YLOG_DEBUG, "commit start");
2046 if (bf_commitExec (bfs))
2049 if (res == ZEBRA_OK)
2052 zebra_set_state(zh, 'o', seqno);
2054 zebra_unlock(zh->lock_shadow);
2055 zebra_unlock(zh->lock_normal);
2057 zebra_lock_w(zh->lock_shadow);
2058 bf_commitClean(bfs, rval);
2059 zebra_unlock(zh->lock_shadow);
2063 zebra_unlock(zh->lock_shadow);
2064 zebra_unlock(zh->lock_normal);
2065 yaz_log(YLOG_WARN, "zebra_commit: failed");
2070 zebra_unlock(zh->lock_shadow);
2071 zebra_unlock(zh->lock_normal);
2072 yaz_log(log_level, "nothing to commit");
2079 ZEBRA_RES zebra_clean(ZebraHandle zh)
2081 yaz_log(log_level, "zebra_clean");
2082 ZEBRA_CHECK_HANDLE(zh);
2083 return zebra_commit_ex(zh, 1);
2086 ZEBRA_RES zebra_commit(ZebraHandle zh)
2088 yaz_log(log_level, "zebra_commit");
2089 ZEBRA_CHECK_HANDLE(zh);
2090 return zebra_commit_ex(zh, 0);
2094 ZEBRA_RES zebra_init(ZebraHandle zh)
2099 yaz_log(log_level, "zebra_init");
2101 ZEBRA_CHECK_HANDLE(zh);
2103 zebra_select_default_database(zh);
2106 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2107 "cannot select default database");
2110 rval = res_get (zh->res, "shadow");
2112 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2115 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2119 bf_cache (bfs, rval);
2123 zebra_set_state (zh, 'o', 0);
2127 ZEBRA_RES zebra_compact(ZebraHandle zh)
2131 yaz_log(log_level, "zebra_compact");
2132 ZEBRA_CHECK_HANDLE(zh);
2135 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2138 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2144 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2146 yaz_log(log_level, "zebra_result");
2149 *code = zh->errCode;
2150 *addinfo = zh->errString;
2154 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2155 *addinfo ="ZebraHandle is NULL";
2159 void zebra_shadow_enable(ZebraHandle zh, int value)
2162 yaz_log(log_level, "zebra_shadow_enable");
2163 zh->shadow_enable = value;
2166 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2168 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2169 ZEBRA_CHECK_HANDLE(zh);
2172 if (zh->iconv_to_utf8 != 0)
2173 yaz_iconv_close(zh->iconv_to_utf8);
2174 if (zh->iconv_from_utf8 != 0)
2175 yaz_iconv_close(zh->iconv_from_utf8);
2178 yaz_iconv_open ("UTF-8", encoding);
2179 if (zh->iconv_to_utf8 == 0)
2180 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2181 zh->iconv_from_utf8 =
2182 yaz_iconv_open (encoding, "UTF-8");
2183 if (zh->iconv_to_utf8 == 0)
2184 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2189 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2191 yaz_log(log_level, "zebra_record_encoding");
2192 ZEBRA_CHECK_HANDLE(zh);
2193 xfree(zh->record_encoding);
2194 zh->record_encoding = 0;
2196 zh->record_encoding = xstrdup (encoding);
2200 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2204 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2206 res_set(zh->res, name, value);
2209 const char *zebra_get_resource(ZebraHandle zh,
2210 const char *name, const char *defaultvalue)
2215 v = res_get_def (zh->res, name, (char *)defaultvalue);
2216 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2220 /* moved from zebra_api_ext.c by pop */
2221 /* FIXME: Should this really be public??? -Heikki */
2223 int zebra_trans_no (ZebraHandle zh)
2225 yaz_log(log_level, "zebra_trans_no");
2227 return zh->trans_no;
2230 int zebra_get_shadow_enable (ZebraHandle zh)
2232 yaz_log(log_level, "zebra_get_shadow_enable");
2234 return zh->shadow_enable;
2237 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2239 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2241 zh->shadow_enable = value;
2244 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2245 const char *buf, int buf_size)
2247 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2250 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2251 const char *recordType,
2252 zint *sysno, const char *match,
2254 const char *buf, int buf_size, int force_update)
2260 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2263 buf_size = strlen(buf);
2265 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2267 res = zebra_buffer_extract_record(zh, buf, buf_size,
2268 0, /* delete_flag */
2274 0); /* allow_update */
2275 if (zebra_end_trans(zh) != ZEBRA_OK)
2277 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2283 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2284 const char *recordType,
2285 zint *sysno, const char *match,
2287 const char *buf, int buf_size,
2292 ZEBRA_CHECK_HANDLE(zh);
2296 yaz_log(log_level, "zebra_update_record");
2298 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2300 if (buf_size < 1) buf_size = strlen(buf);
2302 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2304 res = zebra_buffer_extract_record(zh, buf, buf_size,
2305 0, /* delete_flag */
2311 1); /* allow_update */
2312 if (zebra_end_trans(zh) != ZEBRA_OK)
2314 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2320 ZEBRA_RES zebra_delete_record(ZebraHandle zh,
2321 const char *recordType,
2322 zint *sysno, const char *match,
2324 const char *buf, int buf_size,
2329 ZEBRA_CHECK_HANDLE(zh);
2332 yaz_log(log_level, "zebra_delete_record");
2334 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2336 if (buf_size < 1) buf_size = strlen(buf);
2338 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2340 res = zebra_buffer_extract_record(zh, buf, buf_size,
2341 1, /* delete_flag */
2347 1); /* allow_update */
2348 if (zebra_end_trans(zh) != ZEBRA_OK)
2350 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2356 /* ---------------------------------------------------------------------------
2360 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2361 const char *setname, zint *hits)
2364 ZEBRA_RES res = ZEBRA_OK;
2368 ZEBRA_CHECK_HANDLE(zh);
2370 odr = odr_createmem(ODR_ENCODE);
2375 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2377 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2381 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2382 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2386 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2390 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2398 /* ---------------------------------------------------------------------------
2399 Sort - a simplified interface, with optional read locks.
2401 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2402 const char *sort_spec,
2403 const char *output_setname,
2404 const char **input_setnames)
2406 int num_input_setnames = 0;
2407 int sort_status = 0;
2408 Z_SortKeySpecList *sort_sequence;
2410 ZEBRA_CHECK_HANDLE(zh);
2413 assert(output_setname);
2414 assert(input_setnames);
2415 sort_sequence = yaz_sort_spec (stream, sort_spec);
2416 yaz_log(log_level, "sort (FIXME) ");
2419 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2420 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2424 /* we can do this, since the perl typemap code for char** will
2425 put a NULL at the end of list */
2426 while (input_setnames[num_input_setnames]) num_input_setnames++;
2428 if (zebra_begin_read (zh))
2431 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2432 output_setname, sort_sequence, &sort_status);
2438 /* ---------------------------------------------------------------------------
2439 Get BFS for Zebra system (to make alternative storage methods)
2441 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2444 return zh->reg->bfs;
2449 /* ---------------------------------------------------------------------------
2450 Set limit for search/scan
2452 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2454 ZEBRA_CHECK_HANDLE(zh);
2455 zebra_limit_destroy(zh->m_limit);
2456 zh->m_limit = zebra_limit_create(complement_flag, ids);
2461 Set Error code + addinfo
2463 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2468 nmem_reset(zh->nmem_error);
2469 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2472 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2475 sprintf(vstr, ZINT_FORMAT, i);
2478 nmem_reset(zh->nmem_error);
2479 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2482 void zebra_lock_prefix(Res res, char *path)
2484 const char *lock_dir = res_get_def (res, "lockDir", "");
2486 strcpy(path, lock_dir);
2487 if (*path && path[strlen(path)-1] != '/')
2494 * indent-tabs-mode: nil
2496 * vim: shiftwidth=4 tabstop=8 expandtab