1 /* $Id: zebraapi.c,v 1.218 2006-05-18 12:03:05 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();
371 reg->sortKeys = zebra_rec_keys_open();
381 reg->key_file_no = 0;
384 /* installing rank classes */
385 zebraRankInstall (reg, rank_1_class);
386 zebraRankInstall (reg, rank_zv_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,
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);
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 void map_basenames_func (void *vp, const char *name, const char *value)
773 struct map_baseinfo *p = (struct map_baseinfo *) vp;
775 char fromdb[128], todb[8][128];
782 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
783 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
784 todb[5], todb[6], todb[7]);
788 for (i = 0; i<p->num_bases; i++)
789 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
792 for (i = 0; i < no; i++)
794 if (p->new_num_bases == p->new_num_max)
796 p->new_basenames[(p->new_num_bases)++] =
797 nmem_strdup (p->mem, todb[i]);
803 int zebra_select_default_database(ZebraHandle zh)
807 /* no database has been selected - so we select based on
808 resource setting (including group)
810 const char *group = res_get(zh->session_res, "group");
811 const char *v = res_get_prefix(zh->session_res,
812 "database", group, "Default");
813 return zebra_select_database(zh, v);
818 void map_basenames (ZebraHandle zh, ODR stream,
819 int *num_bases, char ***basenames)
821 struct map_baseinfo info;
822 struct map_baseinfo *p = &info;
825 yaz_log(log_level, "map_basenames ");
830 info.num_bases = *num_bases;
831 info.basenames = *basenames;
832 info.new_num_max = 128;
833 info.new_num_bases = 0;
834 info.new_basenames = (char **)
835 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
836 info.mem = stream->mem;
838 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
840 for (i = 0; i<p->num_bases; i++)
841 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
843 p->new_basenames[(p->new_num_bases)++] =
844 nmem_strdup (p->mem, p->basenames[i]);
846 *num_bases = info.new_num_bases;
847 *basenames = info.new_basenames;
848 for (i = 0; i<*num_bases; i++)
849 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
852 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
854 ZEBRA_CHECK_HANDLE(zh);
856 yaz_log(log_level, "zebra_select_database %s",basename);
858 return zebra_select_databases (zh, 1, &basename);
861 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
862 const char **basenames)
869 ZEBRA_CHECK_HANDLE(zh);
872 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
873 num_bases,basenames[0]);
878 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
882 /* Check if the user has access to all databases (Seb) */
883 /* You could argue that this should happen later, after we have
884 * determined that the database(s) exist. */
885 if (zh->dbaccesslist) {
886 for (i = 0; i < num_bases; i++) {
887 const char *db = basenames[i];
889 for (p = zh->dbaccesslist; p && *p; p = pp) {
891 if ((pp = strchr(p, '+'))) {
897 if (len == strlen(db) && !strncmp(db, p, len))
901 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
907 for (i = 0; i < zh->num_basenames; i++)
908 xfree(zh->basenames[i]);
909 xfree(zh->basenames);
911 zh->num_basenames = num_bases;
912 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
913 for (i = 0; i < zh->num_basenames; i++)
914 zh->basenames[i] = xstrdup (basenames[i]);
916 cp = strrchr(basenames[0], '/');
919 len = cp - basenames[0];
920 new_reg = xmalloc(len + 1);
921 memcpy (new_reg, basenames[0], len);
925 new_reg = xstrdup ("");
926 for (i = 1; i<num_bases; i++)
930 cp1 = strrchr (basenames[i], '/');
935 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
938 if (len != cp1 - basenames[i] ||
939 memcmp (basenames[i], new_reg, len))
941 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
949 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
954 zebra_select_register (zh, new_reg);
958 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
961 if (!zh->lock_normal || !zh->lock_shadow)
963 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
969 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
971 if (approx_limit == 0)
972 approx_limit = DEFAULT_APPROX_LIMIT;
973 zh->approx_limit = approx_limit;
977 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
978 const char *setname, zint *hits)
982 ZEBRA_CHECK_HANDLE(zh);
988 yaz_log(log_level, "zebra_search_rpn");
992 if (zebra_begin_read(zh) == ZEBRA_FAIL)
995 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
996 zh->num_basenames, zh->basenames, setname);
1002 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1003 const char *setname,
1004 Z_RecordComposition *comp,
1005 oid_value input_format, int num_recs,
1006 ZebraRetrievalRecord *recs)
1008 ZebraMetaRecord *poset;
1010 ZEBRA_RES ret = ZEBRA_OK;
1013 ZEBRA_CHECK_HANDLE(zh);
1019 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1023 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1028 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1031 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1032 for (i = 0; i<num_recs; i++)
1033 pos_array[i] = recs[i].position;
1034 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1037 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1038 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1044 for (i = 0; i<num_recs; i++)
1048 recs[i].errCode = 0;
1049 recs[i].format = VAL_SUTRS;
1050 recs[i].len = strlen(poset[i].term);
1051 recs[i].buf = poset[i].term;
1052 recs[i].base = poset[i].db;
1054 else if (poset[i].sysno)
1058 zebra_snippets *hit_snippet = zebra_snippets_create();
1060 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1064 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1066 stream, input_format, comp,
1067 &recs[i].format, &buf, &len,
1068 &recs[i].base, &recs[i].errString);
1073 recs[i].buf = (char*) odr_malloc(stream, len);
1074 memcpy(recs[i].buf, buf, len);
1078 recs[i].score = poset[i].score;
1079 recs[i].sysno = poset[i].sysno;
1080 zebra_snippets_destroy(hit_snippet);
1084 /* only need to set it once */
1085 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1087 zebra_setError_zint(zh,
1088 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1093 recs[i].buf = 0; /* no record and no error issued */
1095 recs[i].errCode = 0;
1096 recs[i].format = VAL_NONE;
1100 zebra_meta_records_destroy(zh, poset, num_recs);
1102 zebra_end_read (zh);
1107 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1109 int *num_entries, ZebraScanEntry **entries,
1111 const char *setname)
1113 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1114 Z_AttributesPlusTerm *zapt;
1118 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1121 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1124 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1125 position, num_entries, entries, is_partial,
1127 yaz_pqf_destroy (pqf_parser);
1131 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1132 oid_value attributeset,
1134 int *num_entries, ZebraScanEntry **entries,
1136 const char *setname)
1139 RSET limit_rset = 0;
1141 ZEBRA_CHECK_HANDLE(zh);
1146 assert(num_entries);
1149 yaz_log(log_level, "zebra_scan");
1151 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1159 limit_rset = resultSetRef(zh, setname);
1163 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1165 zebra_end_read (zh);
1169 res = rpn_scan (zh, stream, zapt, attributeset,
1170 zh->num_basenames, zh->basenames, position,
1171 num_entries, entries, is_partial, limit_rset, 0);
1172 zebra_end_read (zh);
1176 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1177 int num_input_setnames, const char **input_setnames,
1178 const char *output_setname,
1179 Z_SortKeySpecList *sort_sequence,
1183 ZEBRA_CHECK_HANDLE(zh);
1185 assert(num_input_setnames>0);
1186 assert(input_setnames);
1187 assert(sort_sequence);
1188 assert(sort_status);
1189 yaz_log(log_level, "zebra_sort");
1191 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1193 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1194 output_setname, sort_sequence, sort_status);
1199 int zebra_deleteResultSet(ZebraHandle zh, int function,
1200 int num_setnames, char **setnames,
1206 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1208 if (zebra_begin_read(zh))
1209 return Z_DeleteStatus_systemProblemAtTarget;
1212 case Z_DeleteResultSetRequest_list:
1213 assert(num_setnames>0);
1215 resultSetDestroy (zh, num_setnames, setnames, statuses);
1217 case Z_DeleteResultSetRequest_all:
1218 resultSetDestroy (zh, -1, 0, statuses);
1221 zebra_end_read (zh);
1222 status = Z_DeleteStatus_success;
1223 for (i = 0; i<num_setnames; i++)
1224 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1225 status = statuses[i];
1229 int zebra_errCode (ZebraHandle zh)
1233 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1236 yaz_log(log_level, "zebra_errCode: o");
1240 const char *zebra_errString (ZebraHandle zh)
1244 e= diagbib1_str (zh->errCode);
1245 yaz_log(log_level, "zebra_errString: %s",e);
1249 char *zebra_errAdd (ZebraHandle zh)
1254 yaz_log(log_level, "zebra_errAdd: %s",a);
1258 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1261 const char *astring;
1265 ZEBRA_CHECK_HANDLE(zh);
1269 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1270 p = res_get(zs->global_res, u);
1271 xfree(zh->user_perm);
1272 zh->user_perm = xstrdup(p ? p : "r");
1274 /* Determine database access list */
1275 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1277 zh->dbaccesslist = xstrdup(astring);
1279 zh->dbaccesslist = 0;
1281 /* users that don't require a password .. */
1282 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1285 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1290 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1291 const char *record_type)
1293 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1294 database, record_type);
1295 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1297 return zebra_begin_trans(zh, 1);
1300 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1302 ZEBRA_CHECK_HANDLE(zh);
1303 yaz_log(log_level, "zebra_admin_import_end");
1304 return zebra_end_trans(zh);
1307 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1309 ZEBRA_RES res = ZEBRA_OK;
1312 ZEBRA_CHECK_HANDLE(zh);
1313 yaz_log(log_level, "zebra_admin_import_segment");
1315 for (i = 0; i<segment->num_segmentRecords; i++)
1317 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1319 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1321 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1322 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1324 Odr_oct *oct = fragment->u.notExternallyTagged;
1327 if (zebra_update_record(zh,
1328 0, /* record Type */
1332 (const char *) oct->buf, oct->len,
1341 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1342 const char *rec_buf,
1344 const char *recid_buf, size_t recid_len,
1346 /* 1 = insert. Fail it already exists */
1347 /* 2 = replace. Fail it does not exist */
1348 /* 3 = delete. Fail if does not exist */
1349 /* 4 = update. Insert/replace */
1356 ZEBRA_CHECK_HANDLE(zh);
1357 assert(action>0 && action <=4);
1360 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1362 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1364 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1365 "no record ID or empty record ID");
1369 memcpy (recid_z, recid_buf, recid_len);
1370 recid_z[recid_len] = 0;
1372 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1375 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1376 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1379 if (action == 1) /* fail if insert */
1381 if (zebra_end_trans(zh) != ZEBRA_OK)
1382 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1383 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1384 "Cannot insert record: already exist");
1388 memcpy (&sysno, rinfo+1, sizeof(sysno));
1392 if (action == 2 || action == 3) /* fail if delete or update */
1394 if (zebra_end_trans(zh) != ZEBRA_OK)
1395 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1396 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1397 "Cannot delete/update record: does not exist");
1400 action = 1; /* make it an insert (if it's an update).. */
1402 res = buffer_extract_record (zh, rec_buf, rec_len,
1403 action == 3 ? 1 : 0 /* delete flag */,
1409 0, /* force update */
1410 1 /* allow update */
1412 if (res == ZEBRA_FAIL)
1414 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1415 "Unable to parse record");
1419 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1420 sizeof(sysno), &sysno);
1422 else if (action == 3)
1424 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1426 if (zebra_end_trans(zh) != ZEBRA_OK)
1428 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1434 int delete_w_handle(const char *info, void *handle)
1436 ZebraHandle zh = (ZebraHandle) handle;
1440 if (*info == sizeof(pos))
1442 memcpy (&pos, info+1, sizeof(pos));
1443 isamb_unlink(zh->reg->isamb, pos);
1448 static int delete_SU_handle(void *handle, int ord)
1450 ZebraHandle zh = (ZebraHandle) handle;
1454 ord_len = key_SU_encode (ord, ord_buf);
1455 ord_buf[ord_len] = '\0';
1457 assert (zh->reg->isamb);
1458 dict_delete_subtree(zh->reg->dict, ord_buf,
1459 zh, delete_w_handle);
1463 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1465 ZEBRA_RES ret = ZEBRA_OK;
1467 yaz_log(log_level, "zebra_drop_database %s", db);
1468 ZEBRA_CHECK_HANDLE(zh);
1470 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1472 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1477 zebraExplain_curDatabase (zh->reg->zei, db);
1478 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1479 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1480 0 /* handle */, 0 /* func */);
1481 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1482 zebraExplain_removeDatabase(zh->reg->zei, zh);
1486 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1487 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1488 "drop database only supported for isam:b");
1491 if (zebra_end_trans (zh) != ZEBRA_OK)
1493 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1499 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1501 yaz_log(log_level, "zebra_create_database %s", db);
1502 ZEBRA_CHECK_HANDLE(zh);
1505 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1507 if (zebra_begin_trans (zh, 1))
1510 /* announce database */
1511 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1512 /* explainDatabase */))
1514 if (zebra_end_trans (zh) != ZEBRA_OK)
1516 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1518 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1521 return zebra_end_trans (zh);
1524 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1525 const char *input_str, int input_len,
1526 char *output_str, int output_len)
1532 yaz_log(log_level, "zebra_string_norm ");
1534 if (!zh->reg->zebra_maps)
1536 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1537 input_str, input_len);
1540 if (wrbuf_len(wrbuf) >= output_len)
1542 if (wrbuf_len(wrbuf))
1543 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1544 output_str[wrbuf_len(wrbuf)] = '\0';
1545 return wrbuf_len(wrbuf);
1548 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1550 char state_fname[256];
1555 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1557 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1558 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1559 f = fopen (fname, "w");
1561 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1562 fprintf (f, "%c %d %ld\n", val, seqno, p);
1567 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1569 char state_fname[256];
1574 yaz_log(log_level, "zebra_get_state ");
1576 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1577 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1578 f = fopen (fname, "r");
1584 fscanf (f, "%c %d", val, seqno);
1590 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1592 return zebra_begin_trans(zh, 0);
1595 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1597 return zebra_end_trans(zh);
1600 static void read_res_for_transaction(ZebraHandle zh)
1602 const char *group = res_get(zh->res, "group");
1604 /* FIXME - do we still use groups ?? */
1606 zh->m_group = group;
1607 v = res_get_prefix(zh->res, "followLinks", group, "1");
1608 zh->m_follow_links = atoi(v);
1610 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1611 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1613 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1614 zh->m_store_keys = atoi(v);
1616 v = res_get_prefix(zh->res, "storeData", group, "1");
1617 zh->m_store_data = atoi(v);
1619 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1620 zh->m_explain_database = atoi(v);
1622 v = res_get_prefix(zh->res, "openRW", group, "1");
1623 zh->m_flag_rw = atoi(v);
1625 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1626 zh->m_file_verbose_limit = atoi(v);
1629 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1631 ZEBRA_CHECK_HANDLE(zh);
1632 zebra_select_default_database(zh);
1635 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1636 "zebra_begin_trans: no database selected");
1640 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1644 if (rw && !strchr(zh->user_perm, 'w'))
1648 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1660 const char *rval = 0;
1665 read_res_for_transaction(zh);
1668 if (zh->trans_no != 1)
1670 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1671 "zebra_begin_trans: no write trans within read");
1676 resultSetInvalidate (zh);
1677 zebra_register_close(zh->service, zh->reg);
1679 zh->trans_w_no = zh->trans_no;
1681 zh->records_inserted = 0;
1682 zh->records_updated = 0;
1683 zh->records_deleted = 0;
1684 zh->records_processed = 0;
1686 #if HAVE_SYS_TIMES_H
1690 if (zh->shadow_enable)
1691 rval = res_get (zh->res, "shadow");
1693 for (pass = 0; pass < 2; pass++)
1697 zebra_lock_r (zh->lock_normal);
1698 zebra_lock_w (zh->lock_shadow);
1702 zebra_lock_w (zh->lock_normal);
1703 zebra_lock_w (zh->lock_shadow);
1706 zebra_get_state (zh, &val, &seqno);
1709 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1710 zebra_unlock (zh->lock_shadow);
1711 zebra_unlock (zh->lock_normal);
1715 else if (val == 'd')
1719 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1721 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1722 bf_commitClean (bfs, rval);
1727 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1734 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1738 zebra_set_state (zh, 'd', seqno);
1740 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1741 1, rval ? 1 : 0, zh->res,
1744 zh->reg->seqno = seqno;
1747 zebra_set_state (zh, 'o', seqno);
1749 zebra_unlock (zh->lock_shadow);
1750 zebra_unlock (zh->lock_normal);
1755 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1756 "zebra_begin_trans: cannot open register");
1757 yaz_log(YLOG_FATAL, "%s", zh->errString);
1760 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1770 if (zh->trans_no != 1)
1772 return zebra_flush_reg (zh);
1774 #if HAVE_SYS_TIMES_H
1780 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1783 if (!zh->lock_normal || !zh->lock_shadow)
1786 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1789 zebra_get_state (zh, &val, &seqno);
1795 else if (seqno != zh->reg->seqno)
1797 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1798 seqno, zh->reg->seqno);
1801 else if (zh->reg->last_val != val)
1803 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1804 val, zh->reg->last_val);
1811 zebra_lock_r (zh->lock_shadow);
1813 zebra_lock_r (zh->lock_normal);
1817 resultSetInvalidate (zh);
1818 zebra_register_close(zh->service, zh->reg);
1820 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1821 0, val == 'c' ? 1 : 0,
1822 zh->res, zh->path_reg);
1825 zebra_unlock (zh->lock_normal);
1826 zebra_unlock (zh->lock_shadow);
1828 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1831 zh->reg->last_val = val;
1832 zh->reg->seqno = seqno;
1834 read_res_for_transaction(zh);
1838 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1840 ZebraTransactionStatus dummy;
1842 yaz_log(log_level, "zebra_end_trans");
1843 ZEBRA_CHECK_HANDLE(zh);
1844 return zebra_end_transaction(zh, &dummy);
1847 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1853 ZEBRA_CHECK_HANDLE(zh);
1856 yaz_log(log_level, "zebra_end_transaction");
1858 status->processed = 0;
1859 status->inserted = 0;
1860 status->updated = 0;
1861 status->deleted = 0;
1865 if (!zh->res || !zh->reg)
1867 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1868 "zebra_end_trans: no open transaction");
1871 if (zh->trans_no != zh->trans_w_no)
1874 if (zh->trans_no != 0)
1877 /* release read lock */
1879 zebra_unlock (zh->lock_normal);
1880 zebra_unlock (zh->lock_shadow);
1883 { /* release write lock */
1887 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1888 rval = res_get (zh->res, "shadow");
1890 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1892 zebra_flush_reg (zh);
1894 resultSetInvalidate (zh);
1896 zebra_register_close(zh->service, zh->reg);
1899 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1900 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1901 zh->records_processed, zh->records_inserted,
1902 zh->records_updated, zh->records_deleted);
1904 status->processed = (int) zh->records_processed;
1905 status->inserted = (int) zh->records_inserted;
1906 status->updated = (int) zh->records_updated;
1907 status->deleted = (int) zh->records_deleted;
1909 zebra_get_state (zh, &val, &seqno);
1912 BFiles bfs = bfs_create (rval, zh->path_reg);
1913 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1914 bf_commitClean (bfs, rval);
1919 zebra_set_state (zh, 'o', seqno);
1921 zebra_unlock (zh->lock_shadow);
1922 zebra_unlock (zh->lock_normal);
1925 #if HAVE_SYS_TIMES_H
1927 yaz_log (log_level, "user/system: %ld/%ld",
1928 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1929 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1931 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1932 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1937 int zebra_repository_update (ZebraHandle zh, const char *path)
1941 yaz_log (log_level, "updating %s", path);
1942 repositoryUpdate (zh, path);
1946 int zebra_repository_delete (ZebraHandle zh, const char *path)
1950 yaz_log (log_level, "deleting %s", path);
1951 repositoryDelete (zh, path);
1955 int zebra_repository_show (ZebraHandle zh, const char *path)
1959 yaz_log(log_level, "zebra_repository_show");
1960 repositoryShow (zh, path);
1964 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1972 zebra_select_default_database(zh);
1975 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1978 rval = res_get (zh->res, "shadow");
1981 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1985 zebra_lock_w (zh->lock_normal);
1986 zebra_lock_r (zh->lock_shadow);
1988 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1991 zebra_unlock(zh->lock_shadow);
1992 zebra_unlock(zh->lock_normal);
1995 zebra_get_state (zh, &val, &seqno);
1998 bf_cache (bfs, rval);
1999 if (bf_commitExists (bfs))
2002 zebra_set_state (zh, 'd', seqno);
2005 zebra_set_state (zh, 'c', seqno);
2007 yaz_log (YLOG_DEBUG, "commit start");
2008 bf_commitExec (bfs);
2014 zebra_set_state (zh, 'o', seqno);
2016 zebra_unlock (zh->lock_shadow);
2017 zebra_unlock (zh->lock_normal);
2019 zebra_lock_w(zh->lock_shadow);
2020 bf_commitClean (bfs, rval);
2021 zebra_unlock (zh->lock_normal);
2025 zebra_unlock(zh->lock_shadow);
2026 zebra_unlock(zh->lock_normal);
2027 yaz_log (log_level, "nothing to commit");
2034 ZEBRA_RES zebra_clean(ZebraHandle zh)
2036 yaz_log(log_level, "zebra_clean");
2037 ZEBRA_CHECK_HANDLE(zh);
2038 return zebra_commit_ex(zh, 1);
2041 ZEBRA_RES zebra_commit(ZebraHandle zh)
2043 yaz_log(log_level, "zebra_commit");
2044 ZEBRA_CHECK_HANDLE(zh);
2045 return zebra_commit_ex(zh, 0);
2049 ZEBRA_RES zebra_init(ZebraHandle zh)
2054 yaz_log(log_level, "zebra_init");
2056 ZEBRA_CHECK_HANDLE(zh);
2058 zebra_select_default_database(zh);
2061 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2062 "cannot select default database");
2065 rval = res_get (zh->res, "shadow");
2067 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2070 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2074 bf_cache (bfs, rval);
2078 zebra_set_state (zh, 'o', 0);
2082 ZEBRA_RES zebra_compact(ZebraHandle zh)
2086 yaz_log(log_level, "zebra_compact");
2087 ZEBRA_CHECK_HANDLE(zh);
2090 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2093 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2099 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2101 yaz_log(log_level, "zebra_result");
2104 *code = zh->errCode;
2105 *addinfo = zh->errString;
2109 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2110 *addinfo ="ZebraHandle is NULL";
2114 void zebra_shadow_enable(ZebraHandle zh, int value)
2117 yaz_log(log_level, "zebra_shadow_enable");
2118 zh->shadow_enable = value;
2121 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2123 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2124 ZEBRA_CHECK_HANDLE(zh);
2127 if (zh->iconv_to_utf8 != 0)
2128 yaz_iconv_close(zh->iconv_to_utf8);
2129 if (zh->iconv_from_utf8 != 0)
2130 yaz_iconv_close(zh->iconv_from_utf8);
2133 yaz_iconv_open ("UTF-8", encoding);
2134 if (zh->iconv_to_utf8 == 0)
2135 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2136 zh->iconv_from_utf8 =
2137 yaz_iconv_open (encoding, "UTF-8");
2138 if (zh->iconv_to_utf8 == 0)
2139 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2144 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2146 yaz_log(log_level, "zebra_record_encoding");
2147 ZEBRA_CHECK_HANDLE(zh);
2148 xfree(zh->record_encoding);
2149 zh->record_encoding = 0;
2151 zh->record_encoding = xstrdup (encoding);
2155 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2159 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2161 res_set(zh->res, name, value);
2164 const char *zebra_get_resource(ZebraHandle zh,
2165 const char *name, const char *defaultvalue)
2170 v = res_get_def (zh->res, name, (char *)defaultvalue);
2171 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2175 /* moved from zebra_api_ext.c by pop */
2176 /* FIXME: Should this really be public??? -Heikki */
2178 int zebra_trans_no (ZebraHandle zh)
2180 yaz_log(log_level, "zebra_trans_no");
2182 return zh->trans_no;
2185 int zebra_get_shadow_enable (ZebraHandle zh)
2187 yaz_log(log_level, "zebra_get_shadow_enable");
2189 return zh->shadow_enable;
2192 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2194 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2196 zh->shadow_enable = value;
2199 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2200 so that it's identicical to the original api_records_retrieve */
2201 void api_records_retrieve (ZebraHandle zh, ODR stream,
2202 const char *setname, Z_RecordComposition *comp,
2203 oid_value input_format, int num_recs,
2204 ZebraRetrievalRecord *recs)
2206 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2210 /* ---------------------------------------------------------------------------
2211 Record insert(=update), delete
2213 If sysno is provided, then it's used to identify the record.
2214 If not, and match_criteria is provided, then sysno is guessed
2215 If not, and a record is provided, then sysno is got from there
2216 NOTE: Now returns 0 at success and updates sysno, which is an int*
2220 int zebra_add_record(ZebraHandle zh,
2221 const char *buf, int buf_size)
2223 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2226 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2227 const char *recordType,
2228 SYSNO *sysno, const char *match,
2230 const char *buf, int buf_size, int force_update)
2236 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2239 buf_size = strlen(buf);
2241 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2243 res = buffer_extract_record (zh, buf, buf_size,
2244 0, /* delete_flag */
2250 0); /* allow_update */
2251 if (zebra_end_trans(zh) != ZEBRA_OK)
2253 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2259 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2260 const char *recordType,
2261 SYSNO* sysno, const char *match,
2263 const char *buf, int buf_size,
2268 ZEBRA_CHECK_HANDLE(zh);
2272 yaz_log(log_level, "zebra_update_record");
2274 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2276 if (buf_size < 1) buf_size = strlen(buf);
2278 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2280 res = buffer_extract_record (zh, buf, buf_size,
2281 0, /* delete_flag */
2287 1); /* allow_update */
2288 if (zebra_end_trans(zh) != ZEBRA_OK)
2290 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2296 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2297 const char *recordType,
2298 SYSNO *sysno, const char *match,
2300 const char *buf, int buf_size,
2305 ZEBRA_CHECK_HANDLE(zh);
2308 yaz_log(log_level, "zebra_delete_record");
2310 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2312 if (buf_size < 1) buf_size = strlen(buf);
2314 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2316 res = buffer_extract_record (zh, buf, buf_size,
2317 1, /* delete_flag */
2323 1); /* allow_update */
2324 if (zebra_end_trans(zh) != ZEBRA_OK)
2326 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2332 /* ---------------------------------------------------------------------------
2336 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2337 const char *setname, zint *hits)
2340 ZEBRA_RES res = ZEBRA_OK;
2344 ZEBRA_CHECK_HANDLE(zh);
2346 odr = odr_createmem(ODR_ENCODE);
2351 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2353 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2357 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2358 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2362 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2366 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2374 /* ---------------------------------------------------------------------------
2375 Sort - a simplified interface, with optional read locks.
2377 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2378 const char *sort_spec,
2379 const char *output_setname,
2380 const char **input_setnames)
2382 int num_input_setnames = 0;
2383 int sort_status = 0;
2384 Z_SortKeySpecList *sort_sequence;
2386 ZEBRA_CHECK_HANDLE(zh);
2389 assert(output_setname);
2390 assert(input_setnames);
2391 sort_sequence = yaz_sort_spec (stream, sort_spec);
2392 yaz_log(log_level, "sort (FIXME) ");
2395 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2396 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2400 /* we can do this, since the perl typemap code for char** will
2401 put a NULL at the end of list */
2402 while (input_setnames[num_input_setnames]) num_input_setnames++;
2404 if (zebra_begin_read (zh))
2407 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2408 output_setname, sort_sequence, &sort_status);
2414 /* ---------------------------------------------------------------------------
2415 Get BFS for Zebra system (to make alternative storage methods)
2417 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2420 return zh->reg->bfs;
2425 /* ---------------------------------------------------------------------------
2426 Set limit for search/scan
2428 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2430 ZEBRA_CHECK_HANDLE(zh);
2431 zebra_limit_destroy(zh->m_limit);
2432 zh->m_limit = zebra_limit_create(complement_flag, ids);
2437 Set Error code + addinfo
2439 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2444 nmem_reset(zh->nmem_error);
2445 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2448 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2451 sprintf(vstr, ZINT_FORMAT, i);
2454 nmem_reset(zh->nmem_error);
2455 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2458 void zebra_lock_prefix (Res res, char *path)
2460 const char *lock_dir = res_get_def (res, "lockDir", "");
2462 strcpy (path, lock_dir);
2463 if (*path && path[strlen(path)-1] != '/')
2470 * indent-tabs-mode: nil
2472 * vim: shiftwidth=4 tabstop=8 expandtab