1 /* $Id: zebraapi.c,v 1.217 2006-05-10 08:13:23 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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;
144 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
147 yaz_iconv_open ("UTF-8", default_encoding);
148 if (zh->iconv_to_utf8 == 0)
149 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
151 zh->iconv_from_utf8 =
152 yaz_iconv_open (default_encoding, "UTF-8");
153 if (zh->iconv_to_utf8 == 0)
154 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
157 zh->record_encoding = 0;
159 zebra_mutex_cond_lock (&zs->session_lock);
161 zh->next = zs->sessions;
164 zebra_mutex_cond_unlock (&zs->session_lock);
166 zh->store_data_buf = 0;
168 zh->m_limit = zebra_limit_create(1, 0);
170 zh->nmem_error = nmem_create();
175 ZebraService zebra_start (const char *configName)
177 return zebra_start_res(configName, 0, 0);
180 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
184 if (!log_level_initialized)
186 log_level = yaz_log_module_level("zebraapi");
187 log_level_initialized = 1;
190 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
191 configName ? configName : "");
193 if ((res = res_open(def_res, over_res)))
195 const char *passwd_plain = 0;
196 const char *passwd_encrypt = 0;
197 const char *dbaccess = 0;
202 ZEBRA_RES ret = res_read_file(res, configName);
209 zh = xmalloc(sizeof(*zh));
210 zh->global_res = res;
215 zebra_mutex_cond_init (&zh->session_lock);
216 passwd_plain = res_get (zh->global_res, "passwd");
217 passwd_encrypt = res_get (zh->global_res, "passwd.c");
218 dbaccess = res_get (zh->global_res, "dbaccess");
220 if (!passwd_plain && !passwd_encrypt)
221 zh->passwd_db = NULL;
224 zh->passwd_db = passwd_db_open();
226 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
230 passwd_db_file_plain(zh->passwd_db, passwd_plain);
232 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
239 zh->dbaccess = res_open(NULL, NULL);
240 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
241 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
246 zh->path_root = res_get (zh->global_res, "root");
247 zh->nmem = nmem_create();
248 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
252 const char *module_path = res_get(res, "modulePath");
254 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
262 void zebra_filter_info(ZebraService zs, void *cd,
263 void (*cb)(void *cd, const char *name))
267 recTypeClass_info(zs->record_classes, cd, cb);
270 void zebra_pidfname(ZebraService zs, char *path)
273 zebra_lock_prefix (zs->global_res, path);
274 strcat(path, "zebrasrv.pid");
277 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
278 int compact_flag, Res res)
280 int page_size = 4096;
281 char resource_str[200];
282 sprintf (resource_str, "dict.%.100s.pagesize", name);
286 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
287 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
289 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
293 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
294 int rw, int useshadow, Res res,
295 const char *reg_path)
297 struct zebra_register *reg;
298 int record_compression = REC_COMPRESS_NONE;
299 const char *recordCompression = 0;
300 const char *profilePath;
302 ZEBRA_RES ret = ZEBRA_OK;
306 reg = xmalloc(sizeof(*reg));
309 reg->name = xstrdup (name);
316 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
317 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
319 reg->dh = data1_create();
326 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
329 data1_destroy(reg->dh);
336 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
338 bfs_destroy(reg->bfs);
339 data1_destroy(reg->dh);
346 getcwd(cwd, sizeof(cwd)-1);
347 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
348 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
350 data1_set_tabpath (reg->dh, profilePath);
351 data1_set_tabroot (reg->dh, reg_path);
352 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
355 zebra_maps_open(res, reg_path, profilePath);
356 if (!reg->zebra_maps)
358 recTypes_destroy(reg->recTypes);
359 bfs_destroy(reg->bfs);
360 data1_destroy(reg->dh);
365 reg->rank_classes = NULL;
369 reg->keys = zebra_rec_keys_open();
372 reg->sortKeys = zebra_rec_keys_open();
374 reg->sortKeys.buf = 0;
375 reg->sortKeys.buf_max = 0;
386 reg->key_file_no = 0;
389 /* installing rank classes */
390 zebraRankInstall (reg, rank_1_class);
391 zebraRankInstall (reg, rank_zv_class);
392 zebraRankInstall (reg, rank_similarity_class);
393 zebraRankInstall (reg, rank_static_class);
395 recordCompression = res_get_def (res, "recordCompression", "none");
396 if (!strcmp (recordCompression, "none"))
397 record_compression = REC_COMPRESS_NONE;
398 if (!strcmp (recordCompression, "bzip2"))
399 record_compression = REC_COMPRESS_BZIP2;
403 const char *index_fname = res_get_def(res, "index", "default.idx");
404 if (index_fname && *index_fname)
406 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
411 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
413 yaz_log (YLOG_WARN, "rec_open failed");
418 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
420 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
422 yaz_log (YLOG_WARN, "dict_open failed");
425 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
427 yaz_log (YLOG_WARN, "sortIdx_open failed");
430 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
432 struct ISAMS_M_s isams_m;
433 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
434 key_isams_m(res, &isams_m))))
436 yaz_log (YLOG_WARN, "isams_open failed");
440 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
442 struct ISAMC_M_s isamc_m;
443 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
444 rw, key_isamc_m(res, &isamc_m))))
446 yaz_log (YLOG_WARN, "isamc_open failed");
450 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
452 struct ISAMC_M_s isamc_m;
454 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
455 rw, key_isamc_m(res, &isamc_m), 0)))
457 yaz_log (YLOG_WARN, "isamb_open failed");
461 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
463 struct ISAMC_M_s isamc_m;
465 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
466 rw, key_isamc_m(res, &isamc_m), 1)))
468 yaz_log (YLOG_WARN, "isamb_open failed");
472 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
474 struct ISAMC_M_s isamc_m;
476 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
477 rw, key_isamc_m(res, &isamc_m), -1)))
479 yaz_log (YLOG_WARN, "isamb_open failed");
485 reg->zei = zebraExplain_open(reg->records, reg->dh,
490 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
497 zebra_register_close(zs, reg);
500 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
504 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
506 ZEBRA_CHECK_HANDLE(zh);
507 yaz_log(log_level, "zebra_admin_shutdown");
509 zebra_mutex_cond_lock (&zh->service->session_lock);
510 zh->service->stop_flag = 1;
511 zebra_mutex_cond_unlock (&zh->service->session_lock);
515 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
518 ZEBRA_CHECK_HANDLE(zh);
519 yaz_log(log_level, "zebra_admin_start");
521 zebra_mutex_cond_lock (&zs->session_lock);
522 zebra_mutex_cond_unlock (&zs->session_lock);
526 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
530 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
534 zebraExplain_close (reg->zei);
535 dict_close (reg->dict);
537 dict_close (reg->matchDict);
538 sortIdx_close (reg->sortIdx);
540 isams_close (reg->isams);
542 isamc_close (reg->isamc);
544 isamb_close (reg->isamb);
545 rec_close (®->records);
547 recTypes_destroy (reg->recTypes);
548 zebra_maps_close (reg->zebra_maps);
549 zebraRankDestroy (reg);
550 bfs_destroy (reg->bfs);
551 data1_destroy (reg->dh);
553 zebra_rec_keys_close(reg->keys);
555 zebra_rec_keys_close(reg->sortKeys);
557 xfree(reg->sortKeys.buf);
565 ZEBRA_RES zebra_stop(ZebraService zs)
569 yaz_log (log_level, "zebra_stop");
573 zebra_close (zs->sessions);
576 zebra_mutex_cond_destroy (&zs->session_lock);
579 passwd_db_close (zs->passwd_db);
581 recTypeClass_destroy(zs->record_classes);
582 nmem_destroy(zs->nmem);
583 res_close (zs->global_res);
588 ZEBRA_RES zebra_close (ZebraHandle zh)
591 struct zebra_session **sp;
594 yaz_log(log_level, "zebra_close");
595 ZEBRA_CHECK_HANDLE(zh);
600 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
601 resultSetDestroy (zh, -1, 0, 0);
604 zebra_register_close(zh->service, zh->reg);
605 zebra_close_res (zh);
606 res_close(zh->session_res);
608 xfree(zh->record_encoding);
610 xfree(zh->dbaccesslist);
612 for (i = 0; i < zh->num_basenames; i++)
613 xfree(zh->basenames[i]);
614 xfree(zh->basenames);
616 if (zh->iconv_to_utf8 != 0)
617 yaz_iconv_close (zh->iconv_to_utf8);
618 if (zh->iconv_from_utf8 != 0)
619 yaz_iconv_close (zh->iconv_from_utf8);
621 zebra_mutex_cond_lock (&zs->session_lock);
622 zebra_lock_destroy (zh->lock_normal);
623 zebra_lock_destroy (zh->lock_shadow);
635 zebra_mutex_cond_unlock (&zs->session_lock);
637 xfree(zh->user_perm);
638 zh->service = 0; /* more likely to trigger an assert */
640 zebra_limit_destroy(zh->m_limit);
642 nmem_destroy(zh->nmem_error);
649 struct map_baseinfo {
655 char **new_basenames;
659 static void zebra_open_res(ZebraHandle zh)
667 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
668 zh->res = res_open(zh->session_res, 0);
669 res_read_file(zh->res, fname);
671 else if (*zh->reg_name == 0)
673 zh->res = res_open(zh->session_res, 0);
677 yaz_log (YLOG_WARN, "no register root specified");
678 zh->res = 0; /* no path for register - fail! */
682 static void zebra_close_res (ZebraHandle zh)
690 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
694 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
698 assert (zh->reg == 0);
699 assert (*zh->reg_name == 0);
705 resultSetInvalidate (zh);
706 zebra_register_close(zh->service, zh->reg);
712 zh->reg_name = xstrdup (new_reg);
716 if (zh->service->path_root)
718 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
719 strlen(zh->reg_name) + 3);
720 strcpy (zh->path_reg, zh->service->path_root);
723 strcat (zh->path_reg, "/");
724 strcat (zh->path_reg, zh->reg_name);
730 zebra_lock_destroy (zh->lock_normal);
734 zebra_lock_destroy (zh->lock_shadow);
740 const char *lock_area = res_get (zh->res, "lockDir");
742 if (!lock_area && zh->path_reg)
743 res_set (zh->res, "lockDir", zh->path_reg);
744 sprintf (fname, "norm.%s.LCK", zh->reg_name);
746 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
748 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
750 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
752 if (!zh->lock_normal || !zh->lock_shadow)
756 zebra_lock_destroy(zh->lock_normal);
761 zebra_lock_destroy(zh->lock_shadow);
770 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
771 zebra_set_approx_limit(zh, approx);
775 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
776 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
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 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1073 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1075 stream, input_format, comp,
1076 &recs[i].format, &buf, &len,
1077 &recs[i].base, &recs[i].errString);
1082 recs[i].buf = (char*) odr_malloc(stream, len);
1083 memcpy(recs[i].buf, buf, len);
1087 recs[i].score = poset[i].score;
1088 recs[i].sysno = poset[i].sysno;
1089 zebra_snippets_destroy(hit_snippet);
1093 /* only need to set it once */
1094 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1096 zebra_setError_zint(zh,
1097 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1102 recs[i].buf = 0; /* no record and no error issued */
1104 recs[i].errCode = 0;
1105 recs[i].format = VAL_NONE;
1109 zebra_meta_records_destroy(zh, poset, num_recs);
1111 zebra_end_read (zh);
1116 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1118 int *num_entries, ZebraScanEntry **entries,
1120 const char *setname)
1122 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1123 Z_AttributesPlusTerm *zapt;
1127 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1130 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1133 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1134 position, num_entries, entries, is_partial,
1136 yaz_pqf_destroy (pqf_parser);
1140 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1141 oid_value attributeset,
1143 int *num_entries, ZebraScanEntry **entries,
1145 const char *setname)
1148 RSET limit_rset = 0;
1150 ZEBRA_CHECK_HANDLE(zh);
1155 assert(num_entries);
1158 yaz_log(log_level, "zebra_scan");
1160 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1168 limit_rset = resultSetRef(zh, setname);
1172 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1174 zebra_end_read (zh);
1178 res = rpn_scan (zh, stream, zapt, attributeset,
1179 zh->num_basenames, zh->basenames, position,
1180 num_entries, entries, is_partial, limit_rset, 0);
1181 zebra_end_read (zh);
1185 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1186 int num_input_setnames, const char **input_setnames,
1187 const char *output_setname,
1188 Z_SortKeySpecList *sort_sequence,
1192 ZEBRA_CHECK_HANDLE(zh);
1194 assert(num_input_setnames>0);
1195 assert(input_setnames);
1196 assert(sort_sequence);
1197 assert(sort_status);
1198 yaz_log(log_level, "zebra_sort");
1200 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1202 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1203 output_setname, sort_sequence, sort_status);
1208 int zebra_deleteResultSet(ZebraHandle zh, int function,
1209 int num_setnames, char **setnames,
1215 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1217 if (zebra_begin_read(zh))
1218 return Z_DeleteStatus_systemProblemAtTarget;
1221 case Z_DeleteResultSetRequest_list:
1222 assert(num_setnames>0);
1224 resultSetDestroy (zh, num_setnames, setnames, statuses);
1226 case Z_DeleteResultSetRequest_all:
1227 resultSetDestroy (zh, -1, 0, statuses);
1230 zebra_end_read (zh);
1231 status = Z_DeleteStatus_success;
1232 for (i = 0; i<num_setnames; i++)
1233 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1234 status = statuses[i];
1238 int zebra_errCode (ZebraHandle zh)
1242 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1245 yaz_log(log_level, "zebra_errCode: o");
1249 const char *zebra_errString (ZebraHandle zh)
1253 e= diagbib1_str (zh->errCode);
1254 yaz_log(log_level, "zebra_errString: %s",e);
1258 char *zebra_errAdd (ZebraHandle zh)
1263 yaz_log(log_level, "zebra_errAdd: %s",a);
1267 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1270 const char *astring;
1274 ZEBRA_CHECK_HANDLE(zh);
1278 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1279 p = res_get(zs->global_res, u);
1280 xfree(zh->user_perm);
1281 zh->user_perm = xstrdup(p ? p : "r");
1283 /* Determine database access list */
1284 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1286 zh->dbaccesslist = xstrdup(astring);
1288 zh->dbaccesslist = 0;
1290 /* users that don't require a password .. */
1291 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1294 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1299 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1300 const char *record_type)
1302 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1303 database, record_type);
1304 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1306 return zebra_begin_trans(zh, 1);
1309 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1311 ZEBRA_CHECK_HANDLE(zh);
1312 yaz_log(log_level, "zebra_admin_import_end");
1313 return zebra_end_trans(zh);
1316 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1318 ZEBRA_RES res = ZEBRA_OK;
1321 ZEBRA_CHECK_HANDLE(zh);
1322 yaz_log(log_level, "zebra_admin_import_segment");
1324 for (i = 0; i<segment->num_segmentRecords; i++)
1326 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1328 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1330 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1331 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1333 Odr_oct *oct = fragment->u.notExternallyTagged;
1336 if (zebra_update_record(zh,
1337 0, /* record Type */
1341 (const char *) oct->buf, oct->len,
1350 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1351 const char *rec_buf,
1353 const char *recid_buf, size_t recid_len,
1355 /* 1 = insert. Fail it already exists */
1356 /* 2 = replace. Fail it does not exist */
1357 /* 3 = delete. Fail if does not exist */
1358 /* 4 = update. Insert/replace */
1365 ZEBRA_CHECK_HANDLE(zh);
1366 assert(action>0 && action <=4);
1369 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1371 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1373 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1374 "no record ID or empty record ID");
1378 memcpy (recid_z, recid_buf, recid_len);
1379 recid_z[recid_len] = 0;
1381 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1384 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1385 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1388 if (action == 1) /* fail if insert */
1390 if (zebra_end_trans(zh) != ZEBRA_OK)
1391 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1392 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1393 "Cannot insert record: already exist");
1397 memcpy (&sysno, rinfo+1, sizeof(sysno));
1401 if (action == 2 || action == 3) /* fail if delete or update */
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 delete/update record: does not exist");
1409 action = 1; /* make it an insert (if it's an update).. */
1411 res = buffer_extract_record (zh, rec_buf, rec_len,
1412 action == 3 ? 1 : 0 /* delete flag */,
1418 0, /* force update */
1419 1 /* allow update */
1421 if (res == ZEBRA_FAIL)
1423 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1424 "Unable to parse record");
1428 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1429 sizeof(sysno), &sysno);
1431 else if (action == 3)
1433 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1435 if (zebra_end_trans(zh) != ZEBRA_OK)
1437 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1443 int delete_w_handle(const char *info, void *handle)
1445 ZebraHandle zh = (ZebraHandle) handle;
1449 if (*info == sizeof(pos))
1451 memcpy (&pos, info+1, sizeof(pos));
1452 isamb_unlink(zh->reg->isamb, pos);
1457 static int delete_SU_handle(void *handle, int ord)
1459 ZebraHandle zh = (ZebraHandle) handle;
1463 ord_len = key_SU_encode (ord, ord_buf);
1464 ord_buf[ord_len] = '\0';
1466 assert (zh->reg->isamb);
1467 dict_delete_subtree(zh->reg->dict, ord_buf,
1468 zh, delete_w_handle);
1472 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1474 ZEBRA_RES ret = ZEBRA_OK;
1476 yaz_log(log_level, "zebra_drop_database %s", db);
1477 ZEBRA_CHECK_HANDLE(zh);
1479 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1481 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1486 zebraExplain_curDatabase (zh->reg->zei, db);
1487 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1488 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1489 0 /* handle */, 0 /* func */);
1490 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1491 zebraExplain_removeDatabase(zh->reg->zei, zh);
1495 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1496 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1497 "drop database only supported for isam:b");
1500 if (zebra_end_trans (zh) != ZEBRA_OK)
1502 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1508 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1510 yaz_log(log_level, "zebra_create_database %s", db);
1511 ZEBRA_CHECK_HANDLE(zh);
1514 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1516 if (zebra_begin_trans (zh, 1))
1519 /* announce database */
1520 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1521 /* explainDatabase */))
1523 if (zebra_end_trans (zh) != ZEBRA_OK)
1525 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1527 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1530 return zebra_end_trans (zh);
1533 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1534 const char *input_str, int input_len,
1535 char *output_str, int output_len)
1541 yaz_log(log_level, "zebra_string_norm ");
1543 if (!zh->reg->zebra_maps)
1545 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1546 input_str, input_len);
1549 if (wrbuf_len(wrbuf) >= output_len)
1551 if (wrbuf_len(wrbuf))
1552 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1553 output_str[wrbuf_len(wrbuf)] = '\0';
1554 return wrbuf_len(wrbuf);
1557 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1559 char state_fname[256];
1564 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1566 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1567 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1568 f = fopen (fname, "w");
1570 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1571 fprintf (f, "%c %d %ld\n", val, seqno, p);
1576 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1578 char state_fname[256];
1583 yaz_log(log_level, "zebra_get_state ");
1585 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1586 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1587 f = fopen (fname, "r");
1593 fscanf (f, "%c %d", val, seqno);
1599 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1601 return zebra_begin_trans(zh, 0);
1604 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1606 return zebra_end_trans(zh);
1609 static void read_res_for_transaction(ZebraHandle zh)
1611 const char *group = res_get(zh->res, "group");
1613 /* FIXME - do we still use groups ?? */
1615 zh->m_group = group;
1616 v = res_get_prefix(zh->res, "followLinks", group, "1");
1617 zh->m_follow_links = atoi(v);
1619 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1620 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1622 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1623 zh->m_store_keys = atoi(v);
1625 v = res_get_prefix(zh->res, "storeData", group, "1");
1626 zh->m_store_data = atoi(v);
1628 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1629 zh->m_explain_database = atoi(v);
1631 v = res_get_prefix(zh->res, "openRW", group, "1");
1632 zh->m_flag_rw = atoi(v);
1634 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1635 zh->m_file_verbose_limit = atoi(v);
1638 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1640 ZEBRA_CHECK_HANDLE(zh);
1641 zebra_select_default_database(zh);
1644 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1645 "zebra_begin_trans: no database selected");
1649 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1653 if (rw && !strchr(zh->user_perm, 'w'))
1657 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1669 const char *rval = 0;
1674 read_res_for_transaction(zh);
1677 if (zh->trans_no != 1)
1679 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1680 "zebra_begin_trans: no write trans within read");
1685 resultSetInvalidate (zh);
1686 zebra_register_close(zh->service, zh->reg);
1688 zh->trans_w_no = zh->trans_no;
1690 zh->records_inserted = 0;
1691 zh->records_updated = 0;
1692 zh->records_deleted = 0;
1693 zh->records_processed = 0;
1695 #if HAVE_SYS_TIMES_H
1699 if (zh->shadow_enable)
1700 rval = res_get (zh->res, "shadow");
1702 for (pass = 0; pass < 2; pass++)
1706 zebra_lock_r (zh->lock_normal);
1707 zebra_lock_w (zh->lock_shadow);
1711 zebra_lock_w (zh->lock_normal);
1712 zebra_lock_w (zh->lock_shadow);
1715 zebra_get_state (zh, &val, &seqno);
1718 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1719 zebra_unlock (zh->lock_shadow);
1720 zebra_unlock (zh->lock_normal);
1724 else if (val == 'd')
1728 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1730 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1731 bf_commitClean (bfs, rval);
1736 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1743 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1747 zebra_set_state (zh, 'd', seqno);
1749 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1750 1, rval ? 1 : 0, zh->res,
1753 zh->reg->seqno = seqno;
1756 zebra_set_state (zh, 'o', seqno);
1758 zebra_unlock (zh->lock_shadow);
1759 zebra_unlock (zh->lock_normal);
1764 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1765 "zebra_begin_trans: cannot open register");
1766 yaz_log(YLOG_FATAL, "%s", zh->errString);
1769 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1779 if (zh->trans_no != 1)
1781 return zebra_flush_reg (zh);
1783 #if HAVE_SYS_TIMES_H
1789 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1792 if (!zh->lock_normal || !zh->lock_shadow)
1795 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1798 zebra_get_state (zh, &val, &seqno);
1804 else if (seqno != zh->reg->seqno)
1806 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1807 seqno, zh->reg->seqno);
1810 else if (zh->reg->last_val != val)
1812 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1813 val, zh->reg->last_val);
1820 zebra_lock_r (zh->lock_shadow);
1822 zebra_lock_r (zh->lock_normal);
1826 resultSetInvalidate (zh);
1827 zebra_register_close(zh->service, zh->reg);
1829 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1830 0, val == 'c' ? 1 : 0,
1831 zh->res, zh->path_reg);
1834 zebra_unlock (zh->lock_normal);
1835 zebra_unlock (zh->lock_shadow);
1837 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1840 zh->reg->last_val = val;
1841 zh->reg->seqno = seqno;
1843 read_res_for_transaction(zh);
1847 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1849 ZebraTransactionStatus dummy;
1851 yaz_log(log_level, "zebra_end_trans");
1852 ZEBRA_CHECK_HANDLE(zh);
1853 return zebra_end_transaction(zh, &dummy);
1856 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1862 ZEBRA_CHECK_HANDLE(zh);
1865 yaz_log(log_level, "zebra_end_transaction");
1867 status->processed = 0;
1868 status->inserted = 0;
1869 status->updated = 0;
1870 status->deleted = 0;
1874 if (!zh->res || !zh->reg)
1876 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1877 "zebra_end_trans: no open transaction");
1880 if (zh->trans_no != zh->trans_w_no)
1883 if (zh->trans_no != 0)
1886 /* release read lock */
1888 zebra_unlock (zh->lock_normal);
1889 zebra_unlock (zh->lock_shadow);
1892 { /* release write lock */
1896 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1897 rval = res_get (zh->res, "shadow");
1899 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1901 zebra_flush_reg (zh);
1903 resultSetInvalidate (zh);
1905 zebra_register_close(zh->service, zh->reg);
1908 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1909 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1910 zh->records_processed, zh->records_inserted,
1911 zh->records_updated, zh->records_deleted);
1913 status->processed = (int) zh->records_processed;
1914 status->inserted = (int) zh->records_inserted;
1915 status->updated = (int) zh->records_updated;
1916 status->deleted = (int) zh->records_deleted;
1918 zebra_get_state (zh, &val, &seqno);
1921 BFiles bfs = bfs_create (rval, zh->path_reg);
1922 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1923 bf_commitClean (bfs, rval);
1928 zebra_set_state (zh, 'o', seqno);
1930 zebra_unlock (zh->lock_shadow);
1931 zebra_unlock (zh->lock_normal);
1934 #if HAVE_SYS_TIMES_H
1936 yaz_log (log_level, "user/system: %ld/%ld",
1937 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1938 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1940 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1941 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1946 int zebra_repository_update (ZebraHandle zh, const char *path)
1950 yaz_log (log_level, "updating %s", path);
1951 repositoryUpdate (zh, path);
1955 int zebra_repository_delete (ZebraHandle zh, const char *path)
1959 yaz_log (log_level, "deleting %s", path);
1960 repositoryDelete (zh, path);
1964 int zebra_repository_show (ZebraHandle zh, const char *path)
1968 yaz_log(log_level, "zebra_repository_show");
1969 repositoryShow (zh, path);
1973 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1981 zebra_select_default_database(zh);
1984 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1987 rval = res_get (zh->res, "shadow");
1990 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1994 zebra_lock_w (zh->lock_normal);
1995 zebra_lock_r (zh->lock_shadow);
1997 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2000 zebra_unlock(zh->lock_shadow);
2001 zebra_unlock(zh->lock_normal);
2004 zebra_get_state (zh, &val, &seqno);
2007 bf_cache (bfs, rval);
2008 if (bf_commitExists (bfs))
2011 zebra_set_state (zh, 'd', seqno);
2014 zebra_set_state (zh, 'c', seqno);
2016 yaz_log (YLOG_DEBUG, "commit start");
2017 bf_commitExec (bfs);
2023 zebra_set_state (zh, 'o', seqno);
2025 zebra_unlock (zh->lock_shadow);
2026 zebra_unlock (zh->lock_normal);
2028 zebra_lock_w(zh->lock_shadow);
2029 bf_commitClean (bfs, rval);
2030 zebra_unlock (zh->lock_normal);
2034 zebra_unlock(zh->lock_shadow);
2035 zebra_unlock(zh->lock_normal);
2036 yaz_log (log_level, "nothing to commit");
2043 ZEBRA_RES zebra_clean(ZebraHandle zh)
2045 yaz_log(log_level, "zebra_clean");
2046 ZEBRA_CHECK_HANDLE(zh);
2047 return zebra_commit_ex(zh, 1);
2050 ZEBRA_RES zebra_commit(ZebraHandle zh)
2052 yaz_log(log_level, "zebra_commit");
2053 ZEBRA_CHECK_HANDLE(zh);
2054 return zebra_commit_ex(zh, 0);
2058 ZEBRA_RES zebra_init(ZebraHandle zh)
2063 yaz_log(log_level, "zebra_init");
2065 ZEBRA_CHECK_HANDLE(zh);
2067 zebra_select_default_database(zh);
2070 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2071 "cannot select default database");
2074 rval = res_get (zh->res, "shadow");
2076 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2079 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2083 bf_cache (bfs, rval);
2087 zebra_set_state (zh, 'o', 0);
2091 ZEBRA_RES zebra_compact(ZebraHandle zh)
2095 yaz_log(log_level, "zebra_compact");
2096 ZEBRA_CHECK_HANDLE(zh);
2099 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2102 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2108 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2110 yaz_log(log_level, "zebra_result");
2113 *code = zh->errCode;
2114 *addinfo = zh->errString;
2118 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2119 *addinfo ="ZebraHandle is NULL";
2123 void zebra_shadow_enable(ZebraHandle zh, int value)
2126 yaz_log(log_level, "zebra_shadow_enable");
2127 zh->shadow_enable = value;
2130 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2132 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2133 ZEBRA_CHECK_HANDLE(zh);
2136 if (zh->iconv_to_utf8 != 0)
2137 yaz_iconv_close(zh->iconv_to_utf8);
2138 if (zh->iconv_from_utf8 != 0)
2139 yaz_iconv_close(zh->iconv_from_utf8);
2142 yaz_iconv_open ("UTF-8", encoding);
2143 if (zh->iconv_to_utf8 == 0)
2144 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2145 zh->iconv_from_utf8 =
2146 yaz_iconv_open (encoding, "UTF-8");
2147 if (zh->iconv_to_utf8 == 0)
2148 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2153 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2155 yaz_log(log_level, "zebra_record_encoding");
2156 ZEBRA_CHECK_HANDLE(zh);
2157 xfree(zh->record_encoding);
2158 zh->record_encoding = 0;
2160 zh->record_encoding = xstrdup (encoding);
2164 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2168 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2170 res_set(zh->res, name, value);
2173 const char *zebra_get_resource(ZebraHandle zh,
2174 const char *name, const char *defaultvalue)
2179 v = res_get_def (zh->res, name, (char *)defaultvalue);
2180 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2184 /* moved from zebra_api_ext.c by pop */
2185 /* FIXME: Should this really be public??? -Heikki */
2187 int zebra_trans_no (ZebraHandle zh)
2189 yaz_log(log_level, "zebra_trans_no");
2191 return zh->trans_no;
2194 int zebra_get_shadow_enable (ZebraHandle zh)
2196 yaz_log(log_level, "zebra_get_shadow_enable");
2198 return zh->shadow_enable;
2201 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2203 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2205 zh->shadow_enable = value;
2208 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2209 so that it's identicical to the original api_records_retrieve */
2210 void api_records_retrieve (ZebraHandle zh, ODR stream,
2211 const char *setname, Z_RecordComposition *comp,
2212 oid_value input_format, int num_recs,
2213 ZebraRetrievalRecord *recs)
2215 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2219 /* ---------------------------------------------------------------------------
2220 Record insert(=update), delete
2222 If sysno is provided, then it's used to identify the record.
2223 If not, and match_criteria is provided, then sysno is guessed
2224 If not, and a record is provided, then sysno is got from there
2225 NOTE: Now returns 0 at success and updates sysno, which is an int*
2229 int zebra_add_record(ZebraHandle zh,
2230 const char *buf, int buf_size)
2232 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2235 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2236 const char *recordType,
2237 SYSNO *sysno, const char *match,
2239 const char *buf, int buf_size, int force_update)
2245 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2248 buf_size = strlen(buf);
2250 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2252 res = buffer_extract_record (zh, buf, buf_size,
2253 0, /* delete_flag */
2259 0); /* allow_update */
2260 if (zebra_end_trans(zh) != ZEBRA_OK)
2262 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2268 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2269 const char *recordType,
2270 SYSNO* sysno, const char *match,
2272 const char *buf, int buf_size,
2277 ZEBRA_CHECK_HANDLE(zh);
2281 yaz_log(log_level, "zebra_update_record");
2283 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2285 if (buf_size < 1) buf_size = strlen(buf);
2287 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2289 res = buffer_extract_record (zh, buf, buf_size,
2290 0, /* delete_flag */
2296 1); /* allow_update */
2297 if (zebra_end_trans(zh) != ZEBRA_OK)
2299 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2305 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2306 const char *recordType,
2307 SYSNO *sysno, const char *match,
2309 const char *buf, int buf_size,
2314 ZEBRA_CHECK_HANDLE(zh);
2317 yaz_log(log_level, "zebra_delete_record");
2319 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2321 if (buf_size < 1) buf_size = strlen(buf);
2323 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2325 res = buffer_extract_record (zh, buf, buf_size,
2326 1, /* delete_flag */
2332 1); /* allow_update */
2333 if (zebra_end_trans(zh) != ZEBRA_OK)
2335 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2341 /* ---------------------------------------------------------------------------
2345 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2346 const char *setname, zint *hits)
2349 ZEBRA_RES res = ZEBRA_OK;
2353 ZEBRA_CHECK_HANDLE(zh);
2355 odr = odr_createmem(ODR_ENCODE);
2360 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2362 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2366 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2367 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2371 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2375 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2383 /* ---------------------------------------------------------------------------
2384 Sort - a simplified interface, with optional read locks.
2386 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2387 const char *sort_spec,
2388 const char *output_setname,
2389 const char **input_setnames)
2391 int num_input_setnames = 0;
2392 int sort_status = 0;
2393 Z_SortKeySpecList *sort_sequence;
2395 ZEBRA_CHECK_HANDLE(zh);
2398 assert(output_setname);
2399 assert(input_setnames);
2400 sort_sequence = yaz_sort_spec (stream, sort_spec);
2401 yaz_log(log_level, "sort (FIXME) ");
2404 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2405 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2409 /* we can do this, since the perl typemap code for char** will
2410 put a NULL at the end of list */
2411 while (input_setnames[num_input_setnames]) num_input_setnames++;
2413 if (zebra_begin_read (zh))
2416 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2417 output_setname, sort_sequence, &sort_status);
2423 /* ---------------------------------------------------------------------------
2424 Get BFS for Zebra system (to make alternative storage methods)
2426 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2429 return zh->reg->bfs;
2434 /* ---------------------------------------------------------------------------
2435 Set limit for search/scan
2437 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2439 ZEBRA_CHECK_HANDLE(zh);
2440 zebra_limit_destroy(zh->m_limit);
2441 zh->m_limit = zebra_limit_create(complement_flag, ids);
2446 Set Error code + addinfo
2448 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2453 nmem_reset(zh->nmem_error);
2454 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2457 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2460 sprintf(vstr, ZINT_FORMAT, i);
2463 nmem_reset(zh->nmem_error);
2464 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2467 void zebra_lock_prefix (Res res, char *path)
2469 const char *lock_dir = res_get_def (res, "lockDir", "");
2471 strcpy (path, lock_dir);
2472 if (*path && path[strlen(path)-1] != '/')
2479 * indent-tabs-mode: nil
2481 * vim: shiftwidth=4 tabstop=8 expandtab