1 /* $Id: zebraapi.c,v 1.228 2006-09-21 20:22:34 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 extract_flushWriteKeys (zh, 1 /* final */);
89 zebra_index_merge (zh );
93 static struct zebra_register *zebra_register_open(ZebraService zs,
95 int rw, int useshadow,
97 const char *reg_path);
98 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
100 ZebraHandle zebra_open(ZebraService zs, Res res)
103 const char *default_encoding;
104 if (!log_level_initialized)
106 log_level = yaz_log_module_level("zebraapi");
107 log_level_initialized = 1;
110 yaz_log(log_level, "zebra_open");
115 zh = (ZebraHandle) xmalloc(sizeof(*zh));
116 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
119 zh->reg = 0; /* no register attached yet */
125 zh->session_res = res_open(zs->global_res, res);
127 zh->dbaccesslist = 0;
129 zh->reg_name = xstrdup ("");
131 zh->num_basenames = 0;
134 zh->approx_limit = DEFAULT_APPROX_LIMIT;
141 zh->shadow_enable = 1;
142 zh->m_staticrank = 0;
143 zh->m_segment_indexing = 0;
145 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
148 yaz_iconv_open ("UTF-8", default_encoding);
149 if (zh->iconv_to_utf8 == 0)
150 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
152 zh->iconv_from_utf8 =
153 yaz_iconv_open (default_encoding, "UTF-8");
154 if (zh->iconv_to_utf8 == 0)
155 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
158 zh->record_encoding = 0;
160 zebra_mutex_cond_lock (&zs->session_lock);
162 zh->next = zs->sessions;
165 zebra_mutex_cond_unlock (&zs->session_lock);
167 zh->store_data_buf = 0;
169 zh->m_limit = zebra_limit_create(1, 0);
171 zh->nmem_error = nmem_create();
176 ZebraService zebra_start (const char *configName)
178 return zebra_start_res(configName, 0, 0);
181 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
187 if (!log_level_initialized)
189 log_level = yaz_log_module_level("zebraapi");
190 log_level_initialized = 1;
193 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
194 configName ? configName : "");
196 if ((res = res_open(def_res, over_res)))
198 const char *passwd_plain = 0;
199 const char *passwd_encrypt = 0;
200 const char *dbaccess = 0;
205 ZEBRA_RES ret = res_read_file(res, configName);
212 zh = xmalloc(sizeof(*zh));
213 zh->global_res = res;
218 zebra_mutex_cond_init (&zh->session_lock);
219 passwd_plain = res_get (zh->global_res, "passwd");
220 passwd_encrypt = res_get (zh->global_res, "passwd.c");
221 dbaccess = res_get (zh->global_res, "dbaccess");
223 if (!passwd_plain && !passwd_encrypt)
224 zh->passwd_db = NULL;
227 zh->passwd_db = passwd_db_open();
229 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
233 passwd_db_file_plain(zh->passwd_db, passwd_plain);
235 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
242 zh->dbaccess = res_open(NULL, NULL);
243 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
244 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
249 zh->path_root = res_get (zh->global_res, "root");
250 zh->nmem = nmem_create();
251 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
255 const char *module_path = res_get(res, "modulePath");
257 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
265 void zebra_filter_info(ZebraService zs, void *cd,
266 void (*cb)(void *cd, const char *name))
270 recTypeClass_info(zs->record_classes, cd, cb);
273 void zebra_pidfname(ZebraService zs, char *path)
276 zebra_lock_prefix (zs->global_res, path);
277 strcat(path, "zebrasrv.pid");
280 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
281 int compact_flag, Res res)
283 int page_size = 4096;
284 char resource_str[200];
285 sprintf (resource_str, "dict.%.100s.pagesize", name);
289 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
290 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
292 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
296 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
297 int rw, int useshadow, Res res,
298 const char *reg_path)
300 struct zebra_register *reg;
301 int record_compression = REC_COMPRESS_NONE;
302 const char *recordCompression = 0;
303 const char *profilePath;
305 ZEBRA_RES ret = ZEBRA_OK;
309 reg = xmalloc(sizeof(*reg));
312 reg->name = xstrdup (name);
319 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
320 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
322 reg->dh = data1_create();
329 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
332 data1_destroy(reg->dh);
339 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
341 bfs_destroy(reg->bfs);
342 data1_destroy(reg->dh);
349 getcwd(cwd, sizeof(cwd)-1);
350 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
351 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
353 data1_set_tabpath (reg->dh, profilePath);
354 data1_set_tabroot (reg->dh, reg_path);
355 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
358 zebra_maps_open(res, reg_path, profilePath);
359 if (!reg->zebra_maps)
361 recTypes_destroy(reg->recTypes);
362 bfs_destroy(reg->bfs);
363 data1_destroy(reg->dh);
368 reg->rank_classes = NULL;
372 reg->keys = zebra_rec_keys_open();
374 reg->sortKeys = zebra_rec_keys_open();
384 reg->key_file_no = 0;
387 /* installing rank classes */
388 zebraRankInstall (reg, rank_1_class);
389 zebraRankInstall (reg, rank_similarity_class);
390 zebraRankInstall (reg, rank_static_class);
392 recordCompression = res_get_def (res, "recordCompression", "none");
393 if (!strcmp (recordCompression, "none"))
394 record_compression = REC_COMPRESS_NONE;
395 if (!strcmp (recordCompression, "bzip2"))
396 record_compression = REC_COMPRESS_BZIP2;
400 const char *index_fname = res_get_def(res, "index", "default.idx");
401 if (index_fname && *index_fname)
403 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
408 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
410 yaz_log (YLOG_WARN, "rec_open failed");
415 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
417 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
419 yaz_log (YLOG_WARN, "dict_open failed");
422 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
424 yaz_log (YLOG_WARN, "sortIdx_open failed");
427 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
429 struct ISAMS_M_s isams_m;
430 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
431 key_isams_m(res, &isams_m))))
433 yaz_log (YLOG_WARN, "isams_open failed");
437 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
439 struct ISAMC_M_s isamc_m;
440 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
441 rw, key_isamc_m(res, &isamc_m))))
443 yaz_log (YLOG_WARN, "isamc_open failed");
447 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
449 struct ISAMC_M_s isamc_m;
451 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
452 rw, key_isamc_m(res, &isamc_m), 0)))
454 yaz_log (YLOG_WARN, "isamb_open failed");
458 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
460 struct ISAMC_M_s isamc_m;
462 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
463 rw, key_isamc_m(res, &isamc_m), 1)))
465 yaz_log (YLOG_WARN, "isamb_open failed");
469 if (res_get_match (res, "isam", "null", 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), -1)))
476 yaz_log (YLOG_WARN, "isamb_open failed");
482 reg->zei = zebraExplain_open(reg->records, reg->dh,
484 zebra_extract_explain);
487 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
494 zebra_register_close(zs, reg);
497 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
501 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
503 ZEBRA_CHECK_HANDLE(zh);
504 yaz_log(log_level, "zebra_admin_shutdown");
506 zebra_mutex_cond_lock (&zh->service->session_lock);
507 zh->service->stop_flag = 1;
508 zebra_mutex_cond_unlock (&zh->service->session_lock);
512 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
515 ZEBRA_CHECK_HANDLE(zh);
516 yaz_log(log_level, "zebra_admin_start");
518 zebra_mutex_cond_lock (&zs->session_lock);
519 zebra_mutex_cond_unlock (&zs->session_lock);
523 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
527 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
531 zebraExplain_close (reg->zei);
532 dict_close (reg->dict);
534 dict_close (reg->matchDict);
535 sortIdx_close (reg->sortIdx);
537 isams_close (reg->isams);
539 isamc_close (reg->isamc);
541 isamb_close (reg->isamb);
542 rec_close (®->records);
544 recTypes_destroy (reg->recTypes);
545 zebra_maps_close (reg->zebra_maps);
546 zebraRankDestroy (reg);
547 bfs_destroy (reg->bfs);
548 data1_destroy (reg->dh);
550 zebra_rec_keys_close(reg->keys);
551 zebra_rec_keys_close(reg->sortKeys);
558 ZEBRA_RES zebra_stop(ZebraService zs)
562 yaz_log (log_level, "zebra_stop");
566 zebra_close (zs->sessions);
569 zebra_mutex_cond_destroy (&zs->session_lock);
572 passwd_db_close (zs->passwd_db);
574 recTypeClass_destroy(zs->record_classes);
575 nmem_destroy(zs->nmem);
576 res_close (zs->global_res);
581 ZEBRA_RES zebra_close(ZebraHandle zh)
584 struct zebra_session **sp;
587 yaz_log(log_level, "zebra_close");
588 ZEBRA_CHECK_HANDLE(zh);
593 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
594 resultSetDestroy (zh, -1, 0, 0);
597 zebra_register_close(zh->service, zh->reg);
598 zebra_close_res (zh);
599 res_close(zh->session_res);
601 xfree(zh->record_encoding);
603 xfree(zh->dbaccesslist);
605 for (i = 0; i < zh->num_basenames; i++)
606 xfree(zh->basenames[i]);
607 xfree(zh->basenames);
609 if (zh->iconv_to_utf8 != 0)
610 yaz_iconv_close (zh->iconv_to_utf8);
611 if (zh->iconv_from_utf8 != 0)
612 yaz_iconv_close (zh->iconv_from_utf8);
614 zebra_mutex_cond_lock (&zs->session_lock);
615 zebra_lock_destroy (zh->lock_normal);
616 zebra_lock_destroy (zh->lock_shadow);
628 zebra_mutex_cond_unlock (&zs->session_lock);
630 xfree(zh->user_perm);
631 zh->service = 0; /* more likely to trigger an assert */
633 zebra_limit_destroy(zh->m_limit);
635 nmem_destroy(zh->nmem_error);
642 struct map_baseinfo {
648 char **new_basenames;
652 static void zebra_open_res(ZebraHandle zh)
660 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
661 zh->res = res_open(zh->session_res, 0);
662 res_read_file(zh->res, fname);
664 else if (*zh->reg_name == 0)
666 zh->res = res_open(zh->session_res, 0);
670 yaz_log (YLOG_WARN, "no register root specified");
671 zh->res = 0; /* no path for register - fail! */
675 static void zebra_close_res (ZebraHandle zh)
683 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
687 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
691 assert (zh->reg == 0);
692 assert (*zh->reg_name == 0);
698 resultSetInvalidate (zh);
699 zebra_register_close(zh->service, zh->reg);
705 zh->reg_name = xstrdup (new_reg);
709 if (zh->service->path_root)
711 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
712 strlen(zh->reg_name) + 3);
713 strcpy (zh->path_reg, zh->service->path_root);
716 strcat (zh->path_reg, "/");
717 strcat (zh->path_reg, zh->reg_name);
723 zebra_lock_destroy (zh->lock_normal);
727 zebra_lock_destroy (zh->lock_shadow);
733 const char *lock_area = res_get (zh->res, "lockDir");
735 if (!lock_area && zh->path_reg)
736 res_set (zh->res, "lockDir", zh->path_reg);
737 sprintf (fname, "norm.%s.LCK", zh->reg_name);
739 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
741 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
743 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
745 if (!zh->lock_normal || !zh->lock_shadow)
749 zebra_lock_destroy(zh->lock_normal);
754 zebra_lock_destroy(zh->lock_shadow);
763 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
764 zebra_set_approx_limit(zh, approx);
768 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
769 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
773 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
776 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
777 zh->m_segment_indexing);
782 void map_basenames_func (void *vp, const char *name, const char *value)
784 struct map_baseinfo *p = (struct map_baseinfo *) vp;
786 char fromdb[128], todb[8][128];
793 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
794 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
795 todb[5], todb[6], todb[7]);
799 for (i = 0; i<p->num_bases; i++)
800 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
803 for (i = 0; i < no; i++)
805 if (p->new_num_bases == p->new_num_max)
807 p->new_basenames[(p->new_num_bases)++] =
808 nmem_strdup (p->mem, todb[i]);
814 int zebra_select_default_database(ZebraHandle zh)
818 /* no database has been selected - so we select based on
819 resource setting (including group)
821 const char *group = res_get(zh->session_res, "group");
822 const char *v = res_get_prefix(zh->session_res,
823 "database", group, "Default");
824 return zebra_select_database(zh, v);
829 void map_basenames (ZebraHandle zh, ODR stream,
830 int *num_bases, char ***basenames)
832 struct map_baseinfo info;
833 struct map_baseinfo *p = &info;
836 yaz_log(log_level, "map_basenames ");
841 info.num_bases = *num_bases;
842 info.basenames = *basenames;
843 info.new_num_max = 128;
844 info.new_num_bases = 0;
845 info.new_basenames = (char **)
846 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
847 info.mem = stream->mem;
849 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
851 for (i = 0; i<p->num_bases; i++)
852 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
854 p->new_basenames[(p->new_num_bases)++] =
855 nmem_strdup (p->mem, p->basenames[i]);
857 *num_bases = info.new_num_bases;
858 *basenames = info.new_basenames;
859 for (i = 0; i<*num_bases; i++)
860 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
863 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
865 ZEBRA_CHECK_HANDLE(zh);
867 yaz_log(log_level, "zebra_select_database %s",basename);
869 return zebra_select_databases (zh, 1, &basename);
872 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
873 const char **basenames)
880 ZEBRA_CHECK_HANDLE(zh);
883 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
884 num_bases,basenames[0]);
889 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
893 /* Check if the user has access to all databases (Seb) */
894 /* You could argue that this should happen later, after we have
895 * determined that the database(s) exist. */
896 if (zh->dbaccesslist) {
897 for (i = 0; i < num_bases; i++) {
898 const char *db = basenames[i];
900 for (p = zh->dbaccesslist; p && *p; p = pp) {
902 if ((pp = strchr(p, '+'))) {
908 if (len == strlen(db) && !strncmp(db, p, len))
912 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
918 for (i = 0; i < zh->num_basenames; i++)
919 xfree(zh->basenames[i]);
920 xfree(zh->basenames);
922 zh->num_basenames = num_bases;
923 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
924 for (i = 0; i < zh->num_basenames; i++)
925 zh->basenames[i] = xstrdup (basenames[i]);
927 cp = strrchr(basenames[0], '/');
930 len = cp - basenames[0];
931 new_reg = xmalloc(len + 1);
932 memcpy (new_reg, basenames[0], len);
936 new_reg = xstrdup ("");
937 for (i = 1; i<num_bases; i++)
941 cp1 = strrchr (basenames[i], '/');
946 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
949 if (len != cp1 - basenames[i] ||
950 memcmp (basenames[i], new_reg, len))
952 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
960 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
965 zebra_select_register (zh, new_reg);
969 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
972 if (!zh->lock_normal || !zh->lock_shadow)
974 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
980 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
982 if (approx_limit == 0)
983 approx_limit = DEFAULT_APPROX_LIMIT;
984 zh->approx_limit = approx_limit;
988 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
989 const char *setname, zint *hits)
993 ZEBRA_CHECK_HANDLE(zh);
999 yaz_log(log_level, "zebra_search_rpn");
1003 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1006 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1007 zh->num_basenames, zh->basenames, setname);
1013 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1014 const char *setname,
1015 Z_RecordComposition *comp,
1016 oid_value input_format, int num_recs,
1017 ZebraRetrievalRecord *recs)
1019 ZebraMetaRecord *poset;
1021 ZEBRA_RES ret = ZEBRA_OK;
1024 ZEBRA_CHECK_HANDLE(zh);
1030 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1034 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1039 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1042 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1043 for (i = 0; i<num_recs; i++)
1044 pos_array[i] = recs[i].position;
1045 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1048 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1049 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1055 for (i = 0; i<num_recs; i++)
1059 recs[i].errCode = 0;
1060 recs[i].format = VAL_SUTRS;
1061 recs[i].len = strlen(poset[i].term);
1062 recs[i].buf = poset[i].term;
1063 recs[i].base = poset[i].db;
1065 else if (poset[i].sysno)
1069 zebra_snippets *hit_snippet = zebra_snippets_create();
1071 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1075 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1077 stream, input_format, comp,
1078 &recs[i].format, &buf, &len,
1079 &recs[i].base, &recs[i].errString);
1084 recs[i].buf = (char*) odr_malloc(stream, len);
1085 memcpy(recs[i].buf, buf, len);
1089 recs[i].score = poset[i].score;
1090 recs[i].sysno = poset[i].sysno;
1091 zebra_snippets_destroy(hit_snippet);
1095 /* only need to set it once */
1096 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1098 zebra_setError_zint(zh,
1099 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1104 recs[i].buf = 0; /* no record and no error issued */
1106 recs[i].errCode = 0;
1107 recs[i].format = VAL_NONE;
1111 zebra_meta_records_destroy(zh, poset, num_recs);
1113 zebra_end_read (zh);
1118 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1120 int *num_entries, ZebraScanEntry **entries,
1122 const char *setname)
1124 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1125 Z_AttributesPlusTerm *zapt;
1129 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1132 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1135 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1136 position, num_entries, entries, is_partial,
1138 yaz_pqf_destroy (pqf_parser);
1142 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1143 oid_value attributeset,
1145 int *num_entries, ZebraScanEntry **entries,
1147 const char *setname)
1150 RSET limit_rset = 0;
1152 ZEBRA_CHECK_HANDLE(zh);
1157 assert(num_entries);
1160 yaz_log(log_level, "zebra_scan");
1162 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1170 limit_rset = resultSetRef(zh, setname);
1174 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1176 zebra_end_read (zh);
1180 res = rpn_scan(zh, stream, zapt, attributeset,
1181 zh->num_basenames, zh->basenames, position,
1182 num_entries, entries, is_partial, limit_rset);
1187 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1188 int num_input_setnames, const char **input_setnames,
1189 const char *output_setname,
1190 Z_SortKeySpecList *sort_sequence,
1194 ZEBRA_CHECK_HANDLE(zh);
1196 assert(num_input_setnames>0);
1197 assert(input_setnames);
1198 assert(sort_sequence);
1199 assert(sort_status);
1200 yaz_log(log_level, "zebra_sort");
1202 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1204 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1205 output_setname, sort_sequence, sort_status);
1210 int zebra_deleteResultSet(ZebraHandle zh, int function,
1211 int num_setnames, char **setnames,
1217 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1219 if (zebra_begin_read(zh))
1220 return Z_DeleteStatus_systemProblemAtTarget;
1223 case Z_DeleteResultSetRequest_list:
1224 assert(num_setnames>0);
1226 resultSetDestroy (zh, num_setnames, setnames, statuses);
1228 case Z_DeleteResultSetRequest_all:
1229 resultSetDestroy (zh, -1, 0, statuses);
1232 zebra_end_read (zh);
1233 status = Z_DeleteStatus_success;
1234 for (i = 0; i<num_setnames; i++)
1235 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1236 status = statuses[i];
1240 int zebra_errCode (ZebraHandle zh)
1244 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1247 yaz_log(log_level, "zebra_errCode: o");
1251 const char *zebra_errString (ZebraHandle zh)
1255 e= diagbib1_str (zh->errCode);
1256 yaz_log(log_level, "zebra_errString: %s",e);
1260 char *zebra_errAdd (ZebraHandle zh)
1265 yaz_log(log_level, "zebra_errAdd: %s",a);
1269 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1272 const char *astring;
1276 ZEBRA_CHECK_HANDLE(zh);
1280 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1281 p = res_get(zs->global_res, u);
1282 xfree(zh->user_perm);
1283 zh->user_perm = xstrdup(p ? p : "r");
1285 /* Determine database access list */
1286 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1288 zh->dbaccesslist = xstrdup(astring);
1290 zh->dbaccesslist = 0;
1292 /* users that don't require a password .. */
1293 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1296 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1301 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1302 const char *record_type)
1304 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1305 database, record_type);
1306 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1308 return zebra_begin_trans(zh, 1);
1311 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1313 ZEBRA_CHECK_HANDLE(zh);
1314 yaz_log(log_level, "zebra_admin_import_end");
1315 return zebra_end_trans(zh);
1318 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1320 ZEBRA_RES res = ZEBRA_OK;
1323 ZEBRA_CHECK_HANDLE(zh);
1324 yaz_log(log_level, "zebra_admin_import_segment");
1326 for (i = 0; i<segment->num_segmentRecords; i++)
1328 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1330 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1332 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1333 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1335 Odr_oct *oct = fragment->u.notExternallyTagged;
1338 if (zebra_update_record(zh,
1339 0, /* record Type */
1343 (const char *) oct->buf, oct->len,
1352 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1353 const char *rec_buf,
1355 const char *recid_buf, size_t recid_len,
1357 /* 1 = insert. Fail it already exists */
1358 /* 2 = replace. Fail it does not exist */
1359 /* 3 = delete. Fail if does not exist */
1360 /* 4 = update. Insert/replace */
1367 ZEBRA_CHECK_HANDLE(zh);
1368 assert(action>0 && action <=4);
1371 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1373 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1375 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1376 "no record ID or empty record ID");
1380 memcpy (recid_z, recid_buf, recid_len);
1381 recid_z[recid_len] = 0;
1383 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1386 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1387 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1390 if (action == 1) /* fail if insert */
1392 if (zebra_end_trans(zh) != ZEBRA_OK)
1393 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1394 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1395 "Cannot insert record: already exist");
1399 memcpy (&sysno, rinfo+1, sizeof(sysno));
1403 if (action == 2 || action == 3) /* fail if delete or update */
1405 if (zebra_end_trans(zh) != ZEBRA_OK)
1406 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1407 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1408 "Cannot delete/update record: does not exist");
1411 action = 1; /* make it an insert (if it's an update).. */
1413 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1414 action == 3 ? 1 : 0 /* delete flag */,
1420 0, /* force update */
1421 1 /* allow update */
1423 if (res == ZEBRA_FAIL)
1425 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1426 "Unable to parse record");
1430 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1431 sizeof(sysno), &sysno);
1433 else if (action == 3)
1435 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1437 if (zebra_end_trans(zh) != ZEBRA_OK)
1439 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1445 int delete_w_handle(const char *info, void *handle)
1447 ZebraHandle zh = (ZebraHandle) handle;
1451 if (*info == sizeof(pos))
1453 memcpy (&pos, info+1, sizeof(pos));
1454 isamb_unlink(zh->reg->isamb, pos);
1459 static int delete_SU_handle(void *handle, int ord)
1461 ZebraHandle zh = (ZebraHandle) handle;
1465 ord_len = key_SU_encode (ord, ord_buf);
1466 ord_buf[ord_len] = '\0';
1468 assert (zh->reg->isamb);
1469 dict_delete_subtree(zh->reg->dict, ord_buf,
1470 zh, delete_w_handle);
1474 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1476 ZEBRA_RES ret = ZEBRA_OK;
1478 yaz_log(log_level, "zebra_drop_database %s", db);
1479 ZEBRA_CHECK_HANDLE(zh);
1481 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1483 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1488 if (zebraExplain_curDatabase (zh->reg->zei, db))
1490 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1495 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1496 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1497 0 /* handle */, 0 /* func */);
1498 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1499 zebraExplain_removeDatabase(zh->reg->zei, zh);
1500 zebra_remove_file_match(zh);
1505 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1506 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1507 "drop database only supported for isam:b");
1510 if (zebra_end_trans (zh) != ZEBRA_OK)
1512 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1518 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1520 yaz_log(log_level, "zebra_create_database %s", db);
1521 ZEBRA_CHECK_HANDLE(zh);
1524 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1526 if (zebra_begin_trans (zh, 1))
1529 /* announce database */
1530 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1531 /* explainDatabase */))
1533 if (zebra_end_trans (zh) != ZEBRA_OK)
1535 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1537 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1540 return zebra_end_trans (zh);
1543 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1544 const char *input_str, int input_len,
1545 char *output_str, int output_len)
1551 yaz_log(log_level, "zebra_string_norm ");
1553 if (!zh->reg->zebra_maps)
1555 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1556 input_str, input_len);
1559 if (wrbuf_len(wrbuf) >= output_len)
1561 if (wrbuf_len(wrbuf))
1562 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1563 output_str[wrbuf_len(wrbuf)] = '\0';
1564 return wrbuf_len(wrbuf);
1567 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1569 char state_fname[256];
1574 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1576 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1577 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1578 f = fopen (fname, "w");
1580 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1581 fprintf (f, "%c %d %ld\n", val, seqno, p);
1586 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1588 char state_fname[256];
1593 yaz_log(log_level, "zebra_get_state ");
1595 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1596 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1597 f = fopen (fname, "r");
1603 fscanf (f, "%c %d", val, seqno);
1609 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1611 return zebra_begin_trans(zh, 0);
1614 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1616 return zebra_end_trans(zh);
1619 static void read_res_for_transaction(ZebraHandle zh)
1621 const char *group = res_get(zh->res, "group");
1623 /* FIXME - do we still use groups ?? */
1625 zh->m_group = group;
1626 v = res_get_prefix(zh->res, "followLinks", group, "1");
1627 zh->m_follow_links = atoi(v);
1629 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1630 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1632 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1633 zh->m_store_keys = atoi(v);
1635 v = res_get_prefix(zh->res, "storeData", group, "1");
1636 zh->m_store_data = atoi(v);
1638 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1639 zh->m_explain_database = atoi(v);
1641 v = res_get_prefix(zh->res, "openRW", group, "1");
1642 zh->m_flag_rw = atoi(v);
1644 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1645 zh->m_file_verbose_limit = atoi(v);
1648 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1650 ZEBRA_CHECK_HANDLE(zh);
1651 zebra_select_default_database(zh);
1654 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1655 "zebra_begin_trans: no database selected");
1659 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1663 if (rw && !strchr(zh->user_perm, 'w'))
1667 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1679 const char *rval = 0;
1684 read_res_for_transaction(zh);
1687 if (zh->trans_no != 1)
1689 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1690 "zebra_begin_trans: no write trans within read");
1695 resultSetInvalidate (zh);
1696 zebra_register_close(zh->service, zh->reg);
1698 zh->trans_w_no = zh->trans_no;
1700 zh->records_inserted = 0;
1701 zh->records_updated = 0;
1702 zh->records_deleted = 0;
1703 zh->records_processed = 0;
1705 #if HAVE_SYS_TIMES_H
1709 if (zh->shadow_enable)
1710 rval = res_get (zh->res, "shadow");
1712 for (pass = 0; pass < 2; pass++)
1716 zebra_lock_r (zh->lock_normal);
1717 zebra_lock_w (zh->lock_shadow);
1721 zebra_lock_w (zh->lock_normal);
1722 zebra_lock_w (zh->lock_shadow);
1725 zebra_get_state (zh, &val, &seqno);
1728 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1729 zebra_unlock (zh->lock_shadow);
1730 zebra_unlock (zh->lock_normal);
1734 else if (val == 'd')
1738 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1740 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1741 bf_commitClean (bfs, rval);
1746 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1753 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1757 zebra_set_state (zh, 'd', seqno);
1759 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1760 1, rval ? 1 : 0, zh->res,
1763 zh->reg->seqno = seqno;
1766 zebra_set_state (zh, 'o', seqno);
1768 zebra_unlock (zh->lock_shadow);
1769 zebra_unlock (zh->lock_normal);
1774 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1775 "zebra_begin_trans: cannot open register");
1776 yaz_log(YLOG_FATAL, "%s", zh->errString);
1779 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1789 if (zh->trans_no != 1)
1791 return zebra_flush_reg (zh);
1793 #if HAVE_SYS_TIMES_H
1799 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1802 if (!zh->lock_normal || !zh->lock_shadow)
1805 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1808 zebra_get_state (zh, &val, &seqno);
1814 else if (seqno != zh->reg->seqno)
1816 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1817 seqno, zh->reg->seqno);
1820 else if (zh->reg->last_val != val)
1822 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1823 val, zh->reg->last_val);
1830 zebra_lock_r (zh->lock_shadow);
1832 zebra_lock_r (zh->lock_normal);
1836 resultSetInvalidate (zh);
1837 zebra_register_close(zh->service, zh->reg);
1839 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1840 0, val == 'c' ? 1 : 0,
1841 zh->res, zh->path_reg);
1844 zebra_unlock (zh->lock_normal);
1845 zebra_unlock (zh->lock_shadow);
1847 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1850 zh->reg->last_val = val;
1851 zh->reg->seqno = seqno;
1853 read_res_for_transaction(zh);
1857 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1859 ZebraTransactionStatus dummy;
1861 yaz_log(log_level, "zebra_end_trans");
1862 ZEBRA_CHECK_HANDLE(zh);
1863 return zebra_end_transaction(zh, &dummy);
1866 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1872 ZEBRA_CHECK_HANDLE(zh);
1875 yaz_log(log_level, "zebra_end_transaction");
1877 status->processed = 0;
1878 status->inserted = 0;
1879 status->updated = 0;
1880 status->deleted = 0;
1884 if (!zh->res || !zh->reg)
1886 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1887 "zebra_end_trans: no open transaction");
1890 if (zh->trans_no != zh->trans_w_no)
1893 if (zh->trans_no != 0)
1896 /* release read lock */
1898 zebra_unlock (zh->lock_normal);
1899 zebra_unlock (zh->lock_shadow);
1902 { /* release write lock */
1906 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1907 rval = res_get (zh->res, "shadow");
1909 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1911 zebra_flush_reg (zh);
1913 resultSetInvalidate (zh);
1915 zebra_register_close(zh->service, zh->reg);
1918 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1919 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1920 zh->records_processed, zh->records_inserted,
1921 zh->records_updated, zh->records_deleted);
1923 status->processed = zh->records_processed;
1924 status->inserted = zh->records_inserted;
1925 status->updated = zh->records_updated;
1926 status->deleted = zh->records_deleted;
1928 zebra_get_state (zh, &val, &seqno);
1931 BFiles bfs = bfs_create (rval, zh->path_reg);
1932 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1933 bf_commitClean (bfs, rval);
1938 zebra_set_state (zh, 'o', seqno);
1940 zebra_unlock (zh->lock_shadow);
1941 zebra_unlock (zh->lock_normal);
1944 #if HAVE_SYS_TIMES_H
1946 yaz_log (log_level, "user/system: %ld/%ld",
1947 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1948 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1950 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1951 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1956 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1960 yaz_log (log_level, "updating %s", path);
1962 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1963 return zebra_update_file_match(zh, path);
1965 return zebra_update_from_path(zh, path);
1968 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1972 yaz_log (log_level, "deleting %s", path);
1973 return zebra_delete_from_path(zh, path);
1976 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1980 yaz_log(log_level, "zebra_repository_show");
1981 repositoryShow (zh, path);
1985 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1993 zebra_select_default_database(zh);
1996 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1999 rval = res_get (zh->res, "shadow");
2002 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2006 zebra_lock_w (zh->lock_normal);
2007 zebra_lock_r (zh->lock_shadow);
2009 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2012 zebra_unlock(zh->lock_shadow);
2013 zebra_unlock(zh->lock_normal);
2016 zebra_get_state (zh, &val, &seqno);
2019 bf_cache (bfs, rval);
2020 if (bf_commitExists (bfs))
2023 zebra_set_state (zh, 'd', seqno);
2026 zebra_set_state (zh, 'c', seqno);
2028 yaz_log (YLOG_DEBUG, "commit start");
2029 bf_commitExec (bfs);
2035 zebra_set_state (zh, 'o', seqno);
2037 zebra_unlock (zh->lock_shadow);
2038 zebra_unlock (zh->lock_normal);
2040 zebra_lock_w(zh->lock_shadow);
2041 bf_commitClean (bfs, rval);
2042 zebra_unlock (zh->lock_shadow);
2046 zebra_unlock(zh->lock_shadow);
2047 zebra_unlock(zh->lock_normal);
2048 yaz_log (log_level, "nothing to commit");
2055 ZEBRA_RES zebra_clean(ZebraHandle zh)
2057 yaz_log(log_level, "zebra_clean");
2058 ZEBRA_CHECK_HANDLE(zh);
2059 return zebra_commit_ex(zh, 1);
2062 ZEBRA_RES zebra_commit(ZebraHandle zh)
2064 yaz_log(log_level, "zebra_commit");
2065 ZEBRA_CHECK_HANDLE(zh);
2066 return zebra_commit_ex(zh, 0);
2070 ZEBRA_RES zebra_init(ZebraHandle zh)
2075 yaz_log(log_level, "zebra_init");
2077 ZEBRA_CHECK_HANDLE(zh);
2079 zebra_select_default_database(zh);
2082 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2083 "cannot select default database");
2086 rval = res_get (zh->res, "shadow");
2088 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2091 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2095 bf_cache (bfs, rval);
2099 zebra_set_state (zh, 'o', 0);
2103 ZEBRA_RES zebra_compact(ZebraHandle zh)
2107 yaz_log(log_level, "zebra_compact");
2108 ZEBRA_CHECK_HANDLE(zh);
2111 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2114 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2120 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2122 yaz_log(log_level, "zebra_result");
2125 *code = zh->errCode;
2126 *addinfo = zh->errString;
2130 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2131 *addinfo ="ZebraHandle is NULL";
2135 void zebra_shadow_enable(ZebraHandle zh, int value)
2138 yaz_log(log_level, "zebra_shadow_enable");
2139 zh->shadow_enable = value;
2142 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2144 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2145 ZEBRA_CHECK_HANDLE(zh);
2148 if (zh->iconv_to_utf8 != 0)
2149 yaz_iconv_close(zh->iconv_to_utf8);
2150 if (zh->iconv_from_utf8 != 0)
2151 yaz_iconv_close(zh->iconv_from_utf8);
2154 yaz_iconv_open ("UTF-8", encoding);
2155 if (zh->iconv_to_utf8 == 0)
2156 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2157 zh->iconv_from_utf8 =
2158 yaz_iconv_open (encoding, "UTF-8");
2159 if (zh->iconv_to_utf8 == 0)
2160 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2165 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2167 yaz_log(log_level, "zebra_record_encoding");
2168 ZEBRA_CHECK_HANDLE(zh);
2169 xfree(zh->record_encoding);
2170 zh->record_encoding = 0;
2172 zh->record_encoding = xstrdup (encoding);
2176 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2180 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2182 res_set(zh->res, name, value);
2185 const char *zebra_get_resource(ZebraHandle zh,
2186 const char *name, const char *defaultvalue)
2191 v = res_get_def (zh->res, name, (char *)defaultvalue);
2192 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2196 /* moved from zebra_api_ext.c by pop */
2197 /* FIXME: Should this really be public??? -Heikki */
2199 int zebra_trans_no (ZebraHandle zh)
2201 yaz_log(log_level, "zebra_trans_no");
2203 return zh->trans_no;
2206 int zebra_get_shadow_enable (ZebraHandle zh)
2208 yaz_log(log_level, "zebra_get_shadow_enable");
2210 return zh->shadow_enable;
2213 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2215 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2217 zh->shadow_enable = value;
2220 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2221 const char *buf, int buf_size)
2223 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2226 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2227 const char *recordType,
2228 SYSNO *sysno, const char *match,
2230 const char *buf, int buf_size, int force_update)
2236 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2239 buf_size = strlen(buf);
2241 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2243 res = zebra_buffer_extract_record(zh, buf, buf_size,
2244 0, /* delete_flag */
2250 0); /* allow_update */
2251 if (zebra_end_trans(zh) != ZEBRA_OK)
2253 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2259 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2260 const char *recordType,
2261 SYSNO* sysno, const char *match,
2263 const char *buf, int buf_size,
2268 ZEBRA_CHECK_HANDLE(zh);
2272 yaz_log(log_level, "zebra_update_record");
2274 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2276 if (buf_size < 1) buf_size = strlen(buf);
2278 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2280 res = zebra_buffer_extract_record(zh, buf, buf_size,
2281 0, /* delete_flag */
2287 1); /* allow_update */
2288 if (zebra_end_trans(zh) != ZEBRA_OK)
2290 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2296 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2297 const char *recordType,
2298 SYSNO *sysno, const char *match,
2300 const char *buf, int buf_size,
2305 ZEBRA_CHECK_HANDLE(zh);
2308 yaz_log(log_level, "zebra_delete_record");
2310 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2312 if (buf_size < 1) buf_size = strlen(buf);
2314 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2316 res = zebra_buffer_extract_record(zh, buf, buf_size,
2317 1, /* delete_flag */
2323 1); /* allow_update */
2324 if (zebra_end_trans(zh) != ZEBRA_OK)
2326 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2332 /* ---------------------------------------------------------------------------
2336 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2337 const char *setname, zint *hits)
2340 ZEBRA_RES res = ZEBRA_OK;
2344 ZEBRA_CHECK_HANDLE(zh);
2346 odr = odr_createmem(ODR_ENCODE);
2351 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2353 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2357 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2358 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2362 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2366 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2374 /* ---------------------------------------------------------------------------
2375 Sort - a simplified interface, with optional read locks.
2377 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2378 const char *sort_spec,
2379 const char *output_setname,
2380 const char **input_setnames)
2382 int num_input_setnames = 0;
2383 int sort_status = 0;
2384 Z_SortKeySpecList *sort_sequence;
2386 ZEBRA_CHECK_HANDLE(zh);
2389 assert(output_setname);
2390 assert(input_setnames);
2391 sort_sequence = yaz_sort_spec (stream, sort_spec);
2392 yaz_log(log_level, "sort (FIXME) ");
2395 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2396 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2400 /* we can do this, since the perl typemap code for char** will
2401 put a NULL at the end of list */
2402 while (input_setnames[num_input_setnames]) num_input_setnames++;
2404 if (zebra_begin_read (zh))
2407 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2408 output_setname, sort_sequence, &sort_status);
2414 /* ---------------------------------------------------------------------------
2415 Get BFS for Zebra system (to make alternative storage methods)
2417 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2420 return zh->reg->bfs;
2425 /* ---------------------------------------------------------------------------
2426 Set limit for search/scan
2428 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2430 ZEBRA_CHECK_HANDLE(zh);
2431 zebra_limit_destroy(zh->m_limit);
2432 zh->m_limit = zebra_limit_create(complement_flag, ids);
2437 Set Error code + addinfo
2439 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2444 nmem_reset(zh->nmem_error);
2445 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2448 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2451 sprintf(vstr, ZINT_FORMAT, i);
2454 nmem_reset(zh->nmem_error);
2455 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2458 void zebra_lock_prefix (Res res, char *path)
2460 const char *lock_dir = res_get_def (res, "lockDir", "");
2462 strcpy (path, lock_dir);
2463 if (*path && path[strlen(path)-1] != '/')
2470 * indent-tabs-mode: nil
2472 * vim: shiftwidth=4 tabstop=8 expandtab