1 /* $Id: zebraapi.c,v 1.235 2006-11-27 09:17:01 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);
213 zh = xmalloc(sizeof(*zh));
214 zh->global_res = res;
219 zebra_mutex_cond_init (&zh->session_lock);
220 passwd_plain = res_get (zh->global_res, "passwd");
221 passwd_encrypt = res_get (zh->global_res, "passwd.c");
222 dbaccess = res_get (zh->global_res, "dbaccess");
224 if (!passwd_plain && !passwd_encrypt)
225 zh->passwd_db = NULL;
228 zh->passwd_db = passwd_db_open();
230 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
234 passwd_db_file_plain(zh->passwd_db, passwd_plain);
236 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
243 zh->dbaccess = res_open(NULL, NULL);
244 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
245 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
250 zh->path_root = res_get (zh->global_res, "root");
251 zh->nmem = nmem_create();
252 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
256 const char *module_path = res_get(res, "modulePath");
258 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
266 void zebra_filter_info(ZebraService zs, void *cd,
267 void (*cb)(void *cd, const char *name))
271 recTypeClass_info(zs->record_classes, cd, cb);
274 void zebra_pidfname(ZebraService zs, char *path)
277 zebra_lock_prefix (zs->global_res, path);
278 strcat(path, "zebrasrv.pid");
281 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
282 int compact_flag, Res res)
284 int page_size = 4096;
285 char resource_str[200];
286 sprintf (resource_str, "dict.%.100s.pagesize", name);
290 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
291 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
293 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
297 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
298 int rw, int useshadow, Res res,
299 const char *reg_path)
301 struct zebra_register *reg;
302 int record_compression = REC_COMPRESS_NONE;
303 const char *recordCompression = 0;
304 const char *profilePath;
306 ZEBRA_RES ret = ZEBRA_OK;
310 reg = xmalloc(sizeof(*reg));
313 reg->name = xstrdup (name);
320 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
321 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
323 reg->dh = data1_create();
330 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
333 data1_destroy(reg->dh);
340 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
342 bfs_destroy(reg->bfs);
343 data1_destroy(reg->dh);
350 getcwd(cwd, sizeof(cwd)-1);
351 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
352 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
354 data1_set_tabpath (reg->dh, profilePath);
355 data1_set_tabroot (reg->dh, reg_path);
356 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
359 zebra_maps_open(res, reg_path, profilePath);
360 if (!reg->zebra_maps)
362 recTypes_destroy(reg->recTypes);
363 bfs_destroy(reg->bfs);
364 data1_destroy(reg->dh);
369 reg->rank_classes = NULL;
372 reg->keys = zebra_rec_keys_open();
374 reg->sortKeys = zebra_rec_keys_open();
385 /* installing rank classes */
386 zebraRankInstall (reg, rank_1_class);
387 zebraRankInstall (reg, rank_similarity_class);
388 zebraRankInstall (reg, rank_static_class);
390 recordCompression = res_get_def (res, "recordCompression", "none");
391 if (!strcmp (recordCompression, "none"))
392 record_compression = REC_COMPRESS_NONE;
393 if (!strcmp (recordCompression, "bzip2"))
394 record_compression = REC_COMPRESS_BZIP2;
398 const char *index_fname = res_get_def(res, "index", "default.idx");
399 if (index_fname && *index_fname)
401 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
406 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
408 yaz_log (YLOG_WARN, "rec_open failed");
413 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
415 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
417 yaz_log (YLOG_WARN, "dict_open failed");
420 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
422 yaz_log (YLOG_WARN, "sortIdx_open failed");
425 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
427 struct ISAMS_M_s isams_m;
428 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
429 key_isams_m(res, &isams_m))))
431 yaz_log (YLOG_WARN, "isams_open failed");
435 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
437 struct ISAMC_M_s isamc_m;
438 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
439 rw, key_isamc_m(res, &isamc_m))))
441 yaz_log (YLOG_WARN, "isamc_open failed");
445 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
447 struct ISAMC_M_s isamc_m;
449 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
450 rw, key_isamc_m(res, &isamc_m), 0)))
452 yaz_log (YLOG_WARN, "isamb_open failed");
456 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
458 struct ISAMC_M_s isamc_m;
460 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
461 rw, key_isamc_m(res, &isamc_m), 1)))
463 yaz_log (YLOG_WARN, "isamb_open failed");
467 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
469 struct ISAMC_M_s isamc_m;
471 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
472 rw, key_isamc_m(res, &isamc_m), -1)))
474 yaz_log (YLOG_WARN, "isamb_open failed");
480 reg->zei = zebraExplain_open(reg->records, reg->dh,
482 zebra_extract_explain);
485 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
492 zebra_register_close(zs, reg);
495 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
499 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
501 ZEBRA_CHECK_HANDLE(zh);
502 yaz_log(log_level, "zebra_admin_shutdown");
504 zebra_mutex_cond_lock (&zh->service->session_lock);
505 zh->service->stop_flag = 1;
506 zebra_mutex_cond_unlock (&zh->service->session_lock);
510 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
513 ZEBRA_CHECK_HANDLE(zh);
514 yaz_log(log_level, "zebra_admin_start");
516 zebra_mutex_cond_lock (&zs->session_lock);
517 zebra_mutex_cond_unlock (&zs->session_lock);
521 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
525 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
529 zebraExplain_close (reg->zei);
530 dict_close (reg->dict);
532 dict_close (reg->matchDict);
533 sortIdx_close (reg->sortIdx);
535 isams_close (reg->isams);
537 isamc_close (reg->isamc);
539 isamb_close (reg->isamb);
540 rec_close (®->records);
542 recTypes_destroy (reg->recTypes);
543 zebra_maps_close (reg->zebra_maps);
544 zebraRankDestroy (reg);
545 bfs_destroy (reg->bfs);
546 data1_destroy (reg->dh);
548 zebra_rec_keys_close(reg->keys);
549 zebra_rec_keys_close(reg->sortKeys);
551 key_block_destroy(®->key_block);
556 ZEBRA_RES zebra_stop(ZebraService zs)
560 yaz_log (log_level, "zebra_stop");
564 zebra_close (zs->sessions);
567 zebra_mutex_cond_destroy (&zs->session_lock);
570 passwd_db_close (zs->passwd_db);
572 recTypeClass_destroy(zs->record_classes);
573 nmem_destroy(zs->nmem);
574 res_close (zs->global_res);
579 ZEBRA_RES zebra_close(ZebraHandle zh)
582 struct zebra_session **sp;
585 yaz_log(log_level, "zebra_close");
586 ZEBRA_CHECK_HANDLE(zh);
591 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
592 resultSetDestroy (zh, -1, 0, 0);
595 zebra_register_close(zh->service, zh->reg);
596 zebra_close_res (zh);
597 res_close(zh->session_res);
599 xfree(zh->record_encoding);
601 xfree(zh->dbaccesslist);
603 for (i = 0; i < zh->num_basenames; i++)
604 xfree(zh->basenames[i]);
605 xfree(zh->basenames);
607 if (zh->iconv_to_utf8 != 0)
608 yaz_iconv_close (zh->iconv_to_utf8);
609 if (zh->iconv_from_utf8 != 0)
610 yaz_iconv_close (zh->iconv_from_utf8);
612 zebra_mutex_cond_lock (&zs->session_lock);
613 zebra_lock_destroy (zh->lock_normal);
614 zebra_lock_destroy (zh->lock_shadow);
626 zebra_mutex_cond_unlock (&zs->session_lock);
628 xfree(zh->user_perm);
629 zh->service = 0; /* more likely to trigger an assert */
631 zebra_limit_destroy(zh->m_limit);
633 nmem_destroy(zh->nmem_error);
640 struct map_baseinfo {
646 char **new_basenames;
650 static void zebra_open_res(ZebraHandle zh)
658 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
659 zh->res = res_open(zh->session_res, 0);
660 res_read_file(zh->res, fname);
662 else if (*zh->reg_name == 0)
664 zh->res = res_open(zh->session_res, 0);
668 yaz_log (YLOG_WARN, "no register root specified");
669 zh->res = 0; /* no path for register - fail! */
673 static void zebra_close_res (ZebraHandle zh)
681 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
685 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
689 assert (zh->reg == 0);
690 assert (*zh->reg_name == 0);
696 resultSetInvalidate (zh);
697 zebra_register_close(zh->service, zh->reg);
703 zh->reg_name = xstrdup (new_reg);
707 if (zh->service->path_root)
709 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
710 strlen(zh->reg_name) + 3);
711 strcpy (zh->path_reg, zh->service->path_root);
714 strcat (zh->path_reg, "/");
715 strcat (zh->path_reg, zh->reg_name);
721 zebra_lock_destroy (zh->lock_normal);
725 zebra_lock_destroy (zh->lock_shadow);
731 const char *lock_area = res_get (zh->res, "lockDir");
733 if (!lock_area && zh->path_reg)
734 res_set (zh->res, "lockDir", zh->path_reg);
735 sprintf (fname, "norm.%s.LCK", zh->reg_name);
737 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
739 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
741 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
743 if (!zh->lock_normal || !zh->lock_shadow)
747 zebra_lock_destroy(zh->lock_normal);
752 zebra_lock_destroy(zh->lock_shadow);
761 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
762 zebra_set_approx_limit(zh, approx);
766 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
767 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
771 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
774 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
775 zh->m_segment_indexing);
780 void map_basenames_func (void *vp, const char *name, const char *value)
782 struct map_baseinfo *p = (struct map_baseinfo *) vp;
784 char fromdb[128], todb[8][128];
791 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
792 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
793 todb[5], todb[6], todb[7]);
797 for (i = 0; i<p->num_bases; i++)
798 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
801 for (i = 0; i < no; i++)
803 if (p->new_num_bases == p->new_num_max)
805 p->new_basenames[(p->new_num_bases)++] =
806 nmem_strdup (p->mem, todb[i]);
812 int zebra_select_default_database(ZebraHandle zh)
816 /* no database has been selected - so we select based on
817 resource setting (including group)
819 const char *group = res_get(zh->session_res, "group");
820 const char *v = res_get_prefix(zh->session_res,
821 "database", group, "Default");
822 return zebra_select_database(zh, v);
827 void map_basenames (ZebraHandle zh, ODR stream,
828 int *num_bases, char ***basenames)
830 struct map_baseinfo info;
831 struct map_baseinfo *p = &info;
834 yaz_log(log_level, "map_basenames ");
839 info.num_bases = *num_bases;
840 info.basenames = *basenames;
841 info.new_num_max = 128;
842 info.new_num_bases = 0;
843 info.new_basenames = (char **)
844 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
845 info.mem = stream->mem;
847 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
849 for (i = 0; i<p->num_bases; i++)
850 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
852 p->new_basenames[(p->new_num_bases)++] =
853 nmem_strdup (p->mem, p->basenames[i]);
855 *num_bases = info.new_num_bases;
856 *basenames = info.new_basenames;
857 for (i = 0; i<*num_bases; i++)
858 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
861 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
863 ZEBRA_CHECK_HANDLE(zh);
865 yaz_log(log_level, "zebra_select_database %s",basename);
867 return zebra_select_databases (zh, 1, &basename);
870 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
871 const char **basenames)
878 ZEBRA_CHECK_HANDLE(zh);
881 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
882 num_bases,basenames[0]);
887 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
891 /* Check if the user has access to all databases (Seb) */
892 /* You could argue that this should happen later, after we have
893 * determined that the database(s) exist. */
894 if (zh->dbaccesslist) {
895 for (i = 0; i < num_bases; i++) {
896 const char *db = basenames[i];
898 for (p = zh->dbaccesslist; p && *p; p = pp) {
900 if ((pp = strchr(p, '+'))) {
906 if (len == strlen(db) && !strncmp(db, p, len))
910 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
916 for (i = 0; i < zh->num_basenames; i++)
917 xfree(zh->basenames[i]);
918 xfree(zh->basenames);
920 zh->num_basenames = num_bases;
921 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
922 for (i = 0; i < zh->num_basenames; i++)
923 zh->basenames[i] = xstrdup (basenames[i]);
925 cp = strrchr(basenames[0], '/');
928 len = cp - basenames[0];
929 new_reg = xmalloc(len + 1);
930 memcpy (new_reg, basenames[0], len);
934 new_reg = xstrdup ("");
935 for (i = 1; i<num_bases; i++)
939 cp1 = strrchr (basenames[i], '/');
944 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
947 if (len != cp1 - basenames[i] ||
948 memcmp (basenames[i], new_reg, len))
950 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
958 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
963 zebra_select_register (zh, new_reg);
967 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
970 if (!zh->lock_normal || !zh->lock_shadow)
972 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
978 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
980 if (approx_limit == 0)
981 approx_limit = DEFAULT_APPROX_LIMIT;
982 zh->approx_limit = approx_limit;
986 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
987 const char *setname, zint *hits)
991 ZEBRA_CHECK_HANDLE(zh);
997 yaz_log(log_level, "zebra_search_rpn");
1001 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1004 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1005 zh->num_basenames, zh->basenames, setname);
1011 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1012 const char *setname,
1013 Z_RecordComposition *comp,
1014 oid_value input_format, int num_recs,
1015 ZebraRetrievalRecord *recs)
1017 ZebraMetaRecord *poset;
1019 ZEBRA_RES ret = ZEBRA_OK;
1022 ZEBRA_CHECK_HANDLE(zh);
1028 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1032 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1037 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1040 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1041 for (i = 0; i<num_recs; i++)
1042 pos_array[i] = recs[i].position;
1043 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1046 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1047 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1053 for (i = 0; i<num_recs; i++)
1057 recs[i].errCode = 0;
1058 recs[i].format = VAL_SUTRS;
1059 recs[i].len = strlen(poset[i].term);
1060 recs[i].buf = poset[i].term;
1061 recs[i].base = poset[i].db;
1063 else if (poset[i].sysno)
1067 zebra_snippets *hit_snippet = zebra_snippets_create();
1069 /* we disable hit snippets for now. It does not work well
1070 and it slows retrieval down a lot */
1072 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1076 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1078 stream, input_format, comp,
1079 &recs[i].format, &buf, &len,
1080 &recs[i].base, &recs[i].errString);
1085 recs[i].buf = (char*) odr_malloc(stream, len);
1086 memcpy(recs[i].buf, buf, len);
1090 recs[i].score = poset[i].score;
1091 recs[i].sysno = poset[i].sysno;
1092 zebra_snippets_destroy(hit_snippet);
1096 /* only need to set it once */
1097 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1099 zebra_setError_zint(zh,
1100 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1105 recs[i].buf = 0; /* no record and no error issued */
1107 recs[i].errCode = 0;
1108 recs[i].format = VAL_NONE;
1112 zebra_meta_records_destroy(zh, poset, num_recs);
1114 zebra_end_read (zh);
1119 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1121 int *num_entries, ZebraScanEntry **entries,
1123 const char *setname)
1125 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1126 Z_AttributesPlusTerm *zapt;
1130 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1133 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1136 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1137 position, num_entries, entries, is_partial,
1139 yaz_pqf_destroy (pqf_parser);
1143 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1144 oid_value attributeset,
1146 int *num_entries, ZebraScanEntry **entries,
1148 const char *setname)
1151 RSET limit_rset = 0;
1153 ZEBRA_CHECK_HANDLE(zh);
1158 assert(num_entries);
1161 yaz_log(log_level, "zebra_scan");
1163 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1171 limit_rset = resultSetRef(zh, setname);
1175 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1177 zebra_end_read (zh);
1181 res = rpn_scan(zh, stream, zapt, attributeset,
1182 zh->num_basenames, zh->basenames, position,
1183 num_entries, entries, is_partial, limit_rset);
1188 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1189 int num_input_setnames, const char **input_setnames,
1190 const char *output_setname,
1191 Z_SortKeySpecList *sort_sequence,
1195 ZEBRA_CHECK_HANDLE(zh);
1197 assert(num_input_setnames>0);
1198 assert(input_setnames);
1199 assert(sort_sequence);
1200 assert(sort_status);
1201 yaz_log(log_level, "zebra_sort");
1203 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1205 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1206 output_setname, sort_sequence, sort_status);
1211 int zebra_deleteResultSet(ZebraHandle zh, int function,
1212 int num_setnames, char **setnames,
1218 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1220 if (zebra_begin_read(zh))
1221 return Z_DeleteStatus_systemProblemAtTarget;
1224 case Z_DeleteResultSetRequest_list:
1225 assert(num_setnames>0);
1227 resultSetDestroy (zh, num_setnames, setnames, statuses);
1229 case Z_DeleteResultSetRequest_all:
1230 resultSetDestroy (zh, -1, 0, statuses);
1233 zebra_end_read (zh);
1234 status = Z_DeleteStatus_success;
1235 for (i = 0; i<num_setnames; i++)
1236 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1237 status = statuses[i];
1241 int zebra_errCode (ZebraHandle zh)
1245 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1248 yaz_log(log_level, "zebra_errCode: o");
1252 const char *zebra_errString (ZebraHandle zh)
1256 e= diagbib1_str (zh->errCode);
1257 yaz_log(log_level, "zebra_errString: %s",e);
1261 char *zebra_errAdd (ZebraHandle zh)
1266 yaz_log(log_level, "zebra_errAdd: %s",a);
1270 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1273 const char *astring;
1277 ZEBRA_CHECK_HANDLE(zh);
1281 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1282 p = res_get(zs->global_res, u);
1283 xfree(zh->user_perm);
1284 zh->user_perm = xstrdup(p ? p : "r");
1286 /* Determine database access list */
1287 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1289 zh->dbaccesslist = xstrdup(astring);
1291 zh->dbaccesslist = 0;
1293 /* users that don't require a password .. */
1294 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1297 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1302 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1303 const char *record_type)
1305 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1306 database, record_type);
1307 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1309 return zebra_begin_trans(zh, 1);
1312 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1314 ZEBRA_CHECK_HANDLE(zh);
1315 yaz_log(log_level, "zebra_admin_import_end");
1316 return zebra_end_trans(zh);
1319 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1321 ZEBRA_RES res = ZEBRA_OK;
1324 ZEBRA_CHECK_HANDLE(zh);
1325 yaz_log(log_level, "zebra_admin_import_segment");
1327 for (i = 0; i<segment->num_segmentRecords; i++)
1329 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1331 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1333 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1334 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1336 Odr_oct *oct = fragment->u.notExternallyTagged;
1339 if (zebra_update_record(zh,
1340 0, /* record Type */
1344 (const char *) oct->buf, oct->len,
1353 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1354 const char *rec_buf,
1356 const char *recid_buf, size_t recid_len,
1358 /* 1 = insert. Fail it already exists */
1359 /* 2 = replace. Fail it does not exist */
1360 /* 3 = delete. Fail if does not exist */
1361 /* 4 = update. Insert/replace */
1368 ZEBRA_CHECK_HANDLE(zh);
1369 assert(action>0 && action <=4);
1372 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1374 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1376 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1377 "no record ID or empty record ID");
1381 memcpy (recid_z, recid_buf, recid_len);
1382 recid_z[recid_len] = 0;
1384 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1387 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1388 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1391 if (action == 1) /* fail if insert */
1393 if (zebra_end_trans(zh) != ZEBRA_OK)
1394 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1395 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1396 "Cannot insert record: already exist");
1400 memcpy (&sysno, rinfo+1, sizeof(sysno));
1404 if (action == 2 || action == 3) /* fail if delete or update */
1406 if (zebra_end_trans(zh) != ZEBRA_OK)
1407 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1408 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1409 "Cannot delete/update record: does not exist");
1412 action = 1; /* make it an insert (if it's an update).. */
1414 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1415 action == 3 ? 1 : 0 /* delete flag */,
1421 0, /* force update */
1422 1 /* allow update */
1424 if (res == ZEBRA_FAIL)
1426 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1427 "Unable to parse record");
1431 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1432 sizeof(sysno), &sysno);
1434 else if (action == 3)
1436 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1438 if (zebra_end_trans(zh) != ZEBRA_OK)
1440 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1446 int delete_w_handle(const char *info, void *handle)
1448 ZebraHandle zh = (ZebraHandle) handle;
1452 if (*info == sizeof(pos))
1454 memcpy (&pos, info+1, sizeof(pos));
1455 isamb_unlink(zh->reg->isamb, pos);
1460 static int delete_SU_handle(void *handle, int ord)
1462 ZebraHandle zh = (ZebraHandle) handle;
1466 ord_len = key_SU_encode (ord, ord_buf);
1467 ord_buf[ord_len] = '\0';
1469 assert (zh->reg->isamb);
1470 dict_delete_subtree(zh->reg->dict, ord_buf,
1471 zh, delete_w_handle);
1475 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1477 ZEBRA_RES ret = ZEBRA_OK;
1479 yaz_log(log_level, "zebra_drop_database %s", db);
1480 ZEBRA_CHECK_HANDLE(zh);
1482 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1484 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1489 if (zebraExplain_curDatabase (zh->reg->zei, db))
1491 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1496 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1497 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1498 0 /* handle */, 0 /* func */);
1499 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1500 zebraExplain_removeDatabase(zh->reg->zei, zh);
1501 zebra_remove_file_match(zh);
1506 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1507 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1508 "drop database only supported for isam:b");
1511 if (zebra_end_trans (zh) != ZEBRA_OK)
1513 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1519 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1521 yaz_log(log_level, "zebra_create_database %s", db);
1522 ZEBRA_CHECK_HANDLE(zh);
1525 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1527 if (zebra_begin_trans (zh, 1))
1530 /* announce database */
1531 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1532 /* explainDatabase */))
1534 if (zebra_end_trans (zh) != ZEBRA_OK)
1536 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1538 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1541 return zebra_end_trans (zh);
1544 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1545 const char *input_str, int input_len,
1546 char *output_str, int output_len)
1552 yaz_log(log_level, "zebra_string_norm ");
1554 if (!zh->reg->zebra_maps)
1556 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1557 input_str, input_len);
1560 if (wrbuf_len(wrbuf) >= output_len)
1562 if (wrbuf_len(wrbuf))
1563 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1564 output_str[wrbuf_len(wrbuf)] = '\0';
1565 return wrbuf_len(wrbuf);
1568 /** \brief set register state (state*.LCK)
1569 \param zh Zebra handle
1571 \param seqno sequence number
1574 d=writing to shadow(dirty)
1578 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1580 char state_fname[256];
1585 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1587 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1588 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1589 f = fopen (fname, "w");
1591 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1592 fprintf (f, "%c %d %ld\n", val, seqno, p);
1597 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1599 char state_fname[256];
1604 yaz_log(log_level, "zebra_get_state ");
1606 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1607 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1608 f = fopen (fname, "r");
1614 fscanf (f, "%c %d", val, seqno);
1620 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1622 return zebra_begin_trans(zh, 0);
1625 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1627 return zebra_end_trans(zh);
1630 static void read_res_for_transaction(ZebraHandle zh)
1632 const char *group = res_get(zh->res, "group");
1634 /* FIXME - do we still use groups ?? */
1636 zh->m_group = group;
1637 v = res_get_prefix(zh->res, "followLinks", group, "1");
1638 zh->m_follow_links = atoi(v);
1640 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1641 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1643 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1644 zh->m_store_keys = atoi(v);
1646 v = res_get_prefix(zh->res, "storeData", group, "1");
1647 zh->m_store_data = atoi(v);
1649 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1650 zh->m_explain_database = atoi(v);
1652 v = res_get_prefix(zh->res, "openRW", group, "1");
1653 zh->m_flag_rw = atoi(v);
1655 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1656 zh->m_file_verbose_limit = atoi(v);
1659 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1661 ZEBRA_CHECK_HANDLE(zh);
1662 zebra_select_default_database(zh);
1665 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1666 "zebra_begin_trans: no database selected");
1670 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1674 if (rw && !strchr(zh->user_perm, 'w'))
1678 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1689 const char *rval = 0;
1694 read_res_for_transaction(zh);
1697 if (zh->trans_no != 1)
1699 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1700 "zebra_begin_trans: no write trans within read");
1705 resultSetInvalidate (zh);
1706 zebra_register_close(zh->service, zh->reg);
1708 zh->trans_w_no = zh->trans_no;
1710 zh->records_inserted = 0;
1711 zh->records_updated = 0;
1712 zh->records_deleted = 0;
1713 zh->records_processed = 0;
1715 #if HAVE_SYS_TIMES_H
1719 if (zh->shadow_enable)
1720 rval = res_get (zh->res, "shadow");
1724 zebra_lock_r(zh->lock_normal);
1725 zebra_lock_w(zh->lock_shadow);
1729 zebra_lock_w(zh->lock_normal);
1730 zebra_lock_w(zh->lock_shadow);
1732 zebra_get_state (zh, &val, &seqno);
1735 /* either we didn't finish commit or shadow is dirty */
1736 zebra_unlock (zh->lock_shadow);
1737 zebra_unlock (zh->lock_normal);
1738 if (zebra_commit (zh))
1746 zebra_lock_r(zh->lock_normal);
1747 zebra_lock_w(zh->lock_shadow);
1751 zebra_lock_w(zh->lock_normal);
1752 zebra_lock_w(zh->lock_shadow);
1756 zebra_set_state (zh, 'd', seqno);
1758 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1759 1, rval ? 1 : 0, zh->res,
1762 zh->reg->seqno = seqno;
1765 zebra_set_state (zh, 'o', seqno);
1767 zebra_unlock (zh->lock_shadow);
1768 zebra_unlock (zh->lock_normal);
1773 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1774 "zebra_begin_trans: cannot open register");
1775 yaz_log(YLOG_FATAL, "%s", zh->errString);
1778 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1788 if (zh->trans_no != 1)
1790 return zebra_flush_reg (zh);
1792 #if HAVE_SYS_TIMES_H
1798 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1801 if (!zh->lock_normal || !zh->lock_shadow)
1804 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1807 zebra_get_state (zh, &val, &seqno);
1813 else if (seqno != zh->reg->seqno)
1815 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1816 seqno, zh->reg->seqno);
1819 else if (zh->reg->last_val != val)
1821 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1822 val, zh->reg->last_val);
1829 zebra_lock_r (zh->lock_shadow);
1831 zebra_lock_r (zh->lock_normal);
1835 resultSetInvalidate (zh);
1836 zebra_register_close(zh->service, zh->reg);
1838 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1839 0, val == 'c' ? 1 : 0,
1840 zh->res, zh->path_reg);
1843 zebra_unlock (zh->lock_normal);
1844 zebra_unlock (zh->lock_shadow);
1846 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1849 zh->reg->last_val = val;
1850 zh->reg->seqno = seqno;
1852 read_res_for_transaction(zh);
1856 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1858 ZebraTransactionStatus dummy;
1860 yaz_log(log_level, "zebra_end_trans");
1861 ZEBRA_CHECK_HANDLE(zh);
1862 return zebra_end_transaction(zh, &dummy);
1865 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1871 ZEBRA_CHECK_HANDLE(zh);
1874 yaz_log(log_level, "zebra_end_transaction");
1876 status->processed = 0;
1877 status->inserted = 0;
1878 status->updated = 0;
1879 status->deleted = 0;
1883 if (!zh->res || !zh->reg)
1885 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1886 "zebra_end_trans: no open transaction");
1889 if (zh->trans_no != zh->trans_w_no)
1892 if (zh->trans_no != 0)
1895 /* release read lock */
1897 zebra_unlock (zh->lock_normal);
1898 zebra_unlock (zh->lock_shadow);
1901 { /* release write lock */
1905 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1906 rval = res_get (zh->res, "shadow");
1908 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1910 zebra_flush_reg (zh);
1912 resultSetInvalidate (zh);
1914 zebra_register_close(zh->service, zh->reg);
1917 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1918 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1919 zh->records_processed, zh->records_inserted,
1920 zh->records_updated, zh->records_deleted);
1922 status->processed = zh->records_processed;
1923 status->inserted = zh->records_inserted;
1924 status->updated = zh->records_updated;
1925 status->deleted = zh->records_deleted;
1927 zebra_get_state (zh, &val, &seqno);
1930 BFiles bfs = bfs_create (rval, zh->path_reg);
1931 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1932 bf_commitClean (bfs, rval);
1937 zebra_set_state (zh, 'o', seqno);
1939 zebra_unlock (zh->lock_shadow);
1940 zebra_unlock (zh->lock_normal);
1943 #if HAVE_SYS_TIMES_H
1945 yaz_log (log_level, "user/system: %ld/%ld",
1946 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1947 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1949 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1950 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1955 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1959 yaz_log (log_level, "updating %s", path);
1961 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1962 return zebra_update_file_match(zh, path);
1964 return zebra_update_from_path(zh, path);
1967 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1971 yaz_log (log_level, "deleting %s", path);
1972 return zebra_delete_from_path(zh, path);
1975 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1979 yaz_log(log_level, "zebra_repository_show");
1980 repositoryShow (zh, path);
1984 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1990 ZEBRA_RES res = ZEBRA_OK;
1994 zebra_select_default_database(zh);
1997 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2000 rval = res_get(zh->res, "shadow");
2003 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
2007 zebra_lock_w(zh->lock_normal);
2008 zebra_lock_r(zh->lock_shadow);
2010 bfs = bfs_create(res_get (zh->res, "register"), zh->path_reg);
2013 zebra_unlock(zh->lock_shadow);
2014 zebra_unlock(zh->lock_normal);
2017 zebra_get_state(zh, &val, &seqno);
2021 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2026 bf_cache (bfs, rval);
2027 if (bf_commitExists (bfs))
2030 zebra_set_state(zh, 'd', seqno);
2033 zebra_set_state(zh, 'c', seqno);
2035 yaz_log(YLOG_DEBUG, "commit start");
2036 if (bf_commitExec (bfs))
2039 if (res == ZEBRA_OK)
2042 zebra_set_state(zh, 'o', seqno);
2044 zebra_unlock(zh->lock_shadow);
2045 zebra_unlock(zh->lock_normal);
2047 zebra_lock_w(zh->lock_shadow);
2048 bf_commitClean(bfs, rval);
2049 zebra_unlock(zh->lock_shadow);
2053 zebra_unlock(zh->lock_shadow);
2054 zebra_unlock(zh->lock_normal);
2055 yaz_log(YLOG_WARN, "zebra_commit: failed");
2060 zebra_unlock(zh->lock_shadow);
2061 zebra_unlock(zh->lock_normal);
2062 yaz_log(log_level, "nothing to commit");
2069 ZEBRA_RES zebra_clean(ZebraHandle zh)
2071 yaz_log(log_level, "zebra_clean");
2072 ZEBRA_CHECK_HANDLE(zh);
2073 return zebra_commit_ex(zh, 1);
2076 ZEBRA_RES zebra_commit(ZebraHandle zh)
2078 yaz_log(log_level, "zebra_commit");
2079 ZEBRA_CHECK_HANDLE(zh);
2080 return zebra_commit_ex(zh, 0);
2084 ZEBRA_RES zebra_init(ZebraHandle zh)
2089 yaz_log(log_level, "zebra_init");
2091 ZEBRA_CHECK_HANDLE(zh);
2093 zebra_select_default_database(zh);
2096 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2097 "cannot select default database");
2100 rval = res_get (zh->res, "shadow");
2102 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2105 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2109 bf_cache (bfs, rval);
2113 zebra_set_state (zh, 'o', 0);
2117 ZEBRA_RES zebra_compact(ZebraHandle zh)
2121 yaz_log(log_level, "zebra_compact");
2122 ZEBRA_CHECK_HANDLE(zh);
2125 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2128 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2134 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2136 yaz_log(log_level, "zebra_result");
2139 *code = zh->errCode;
2140 *addinfo = zh->errString;
2144 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2145 *addinfo ="ZebraHandle is NULL";
2149 void zebra_shadow_enable(ZebraHandle zh, int value)
2152 yaz_log(log_level, "zebra_shadow_enable");
2153 zh->shadow_enable = value;
2156 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2158 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2159 ZEBRA_CHECK_HANDLE(zh);
2162 if (zh->iconv_to_utf8 != 0)
2163 yaz_iconv_close(zh->iconv_to_utf8);
2164 if (zh->iconv_from_utf8 != 0)
2165 yaz_iconv_close(zh->iconv_from_utf8);
2168 yaz_iconv_open ("UTF-8", encoding);
2169 if (zh->iconv_to_utf8 == 0)
2170 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2171 zh->iconv_from_utf8 =
2172 yaz_iconv_open (encoding, "UTF-8");
2173 if (zh->iconv_to_utf8 == 0)
2174 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2179 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2181 yaz_log(log_level, "zebra_record_encoding");
2182 ZEBRA_CHECK_HANDLE(zh);
2183 xfree(zh->record_encoding);
2184 zh->record_encoding = 0;
2186 zh->record_encoding = xstrdup (encoding);
2190 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2194 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2196 res_set(zh->res, name, value);
2199 const char *zebra_get_resource(ZebraHandle zh,
2200 const char *name, const char *defaultvalue)
2205 v = res_get_def (zh->res, name, (char *)defaultvalue);
2206 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2210 /* moved from zebra_api_ext.c by pop */
2211 /* FIXME: Should this really be public??? -Heikki */
2213 int zebra_trans_no (ZebraHandle zh)
2215 yaz_log(log_level, "zebra_trans_no");
2217 return zh->trans_no;
2220 int zebra_get_shadow_enable (ZebraHandle zh)
2222 yaz_log(log_level, "zebra_get_shadow_enable");
2224 return zh->shadow_enable;
2227 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2229 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2231 zh->shadow_enable = value;
2234 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2235 const char *buf, int buf_size)
2237 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2240 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2241 const char *recordType,
2242 zint *sysno, const char *match,
2244 const char *buf, int buf_size, int force_update)
2250 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2253 buf_size = strlen(buf);
2255 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2257 res = zebra_buffer_extract_record(zh, buf, buf_size,
2258 0, /* delete_flag */
2264 0); /* allow_update */
2265 if (zebra_end_trans(zh) != ZEBRA_OK)
2267 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2273 ZEBRA_RES zebra_update_record(ZebraHandle zh,
2274 const char *recordType,
2275 zint *sysno, const char *match,
2277 const char *buf, int buf_size,
2282 ZEBRA_CHECK_HANDLE(zh);
2286 yaz_log(log_level, "zebra_update_record");
2288 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2290 if (buf_size < 1) buf_size = strlen(buf);
2292 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2294 res = zebra_buffer_extract_record(zh, buf, buf_size,
2295 0, /* delete_flag */
2301 1); /* allow_update */
2302 if (zebra_end_trans(zh) != ZEBRA_OK)
2304 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2310 ZEBRA_RES zebra_delete_record(ZebraHandle zh,
2311 const char *recordType,
2312 zint *sysno, const char *match,
2314 const char *buf, int buf_size,
2319 ZEBRA_CHECK_HANDLE(zh);
2322 yaz_log(log_level, "zebra_delete_record");
2324 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2326 if (buf_size < 1) buf_size = strlen(buf);
2328 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2330 res = zebra_buffer_extract_record(zh, buf, buf_size,
2331 1, /* delete_flag */
2337 1); /* allow_update */
2338 if (zebra_end_trans(zh) != ZEBRA_OK)
2340 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2346 /* ---------------------------------------------------------------------------
2350 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2351 const char *setname, zint *hits)
2354 ZEBRA_RES res = ZEBRA_OK;
2358 ZEBRA_CHECK_HANDLE(zh);
2360 odr = odr_createmem(ODR_ENCODE);
2365 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2367 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2371 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2372 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2376 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2380 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2388 /* ---------------------------------------------------------------------------
2389 Sort - a simplified interface, with optional read locks.
2391 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2392 const char *sort_spec,
2393 const char *output_setname,
2394 const char **input_setnames)
2396 int num_input_setnames = 0;
2397 int sort_status = 0;
2398 Z_SortKeySpecList *sort_sequence;
2400 ZEBRA_CHECK_HANDLE(zh);
2403 assert(output_setname);
2404 assert(input_setnames);
2405 sort_sequence = yaz_sort_spec (stream, sort_spec);
2406 yaz_log(log_level, "sort (FIXME) ");
2409 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2410 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2414 /* we can do this, since the perl typemap code for char** will
2415 put a NULL at the end of list */
2416 while (input_setnames[num_input_setnames]) num_input_setnames++;
2418 if (zebra_begin_read (zh))
2421 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2422 output_setname, sort_sequence, &sort_status);
2428 /* ---------------------------------------------------------------------------
2429 Get BFS for Zebra system (to make alternative storage methods)
2431 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2434 return zh->reg->bfs;
2439 /* ---------------------------------------------------------------------------
2440 Set limit for search/scan
2442 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2444 ZEBRA_CHECK_HANDLE(zh);
2445 zebra_limit_destroy(zh->m_limit);
2446 zh->m_limit = zebra_limit_create(complement_flag, ids);
2451 Set Error code + addinfo
2453 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2458 nmem_reset(zh->nmem_error);
2459 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2462 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2465 sprintf(vstr, ZINT_FORMAT, i);
2468 nmem_reset(zh->nmem_error);
2469 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2472 void zebra_lock_prefix(Res res, char *path)
2474 const char *lock_dir = res_get_def (res, "lockDir", "");
2476 strcpy(path, lock_dir);
2477 if (*path && path[strlen(path)-1] != '/')
2484 * indent-tabs-mode: nil
2486 * vim: shiftwidth=4 tabstop=8 expandtab