1 /* $Id: zebraapi.c,v 1.215 2006-05-03 09:31:26 marc 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);
254 void zebra_filter_info(ZebraService zs, void *cd,
255 void (*cb)(void *cd, const char *name))
259 recTypeClass_info(zs->record_classes, cd, cb);
262 void zebra_pidfname(ZebraService zs, char *path)
265 zebra_lock_prefix (zs->global_res, path);
266 strcat(path, "zebrasrv.pid");
269 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
270 int compact_flag, Res res)
272 int page_size = 4096;
273 char resource_str[200];
274 sprintf (resource_str, "dict.%.100s.pagesize", name);
278 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
279 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
281 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
285 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
286 int rw, int useshadow, Res res,
287 const char *reg_path)
289 struct zebra_register *reg;
290 int record_compression = REC_COMPRESS_NONE;
291 const char *recordCompression = 0;
292 const char *profilePath;
294 ZEBRA_RES ret = ZEBRA_OK;
298 reg = xmalloc(sizeof(*reg));
301 reg->name = xstrdup (name);
308 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
309 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
311 reg->dh = data1_create();
318 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
321 data1_destroy(reg->dh);
328 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
330 bfs_destroy(reg->bfs);
331 data1_destroy(reg->dh);
338 getcwd(cwd, sizeof(cwd)-1);
339 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
340 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
342 data1_set_tabpath (reg->dh, profilePath);
343 data1_set_tabroot (reg->dh, reg_path);
344 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
347 zebra_maps_open(res, reg_path, profilePath);
348 if (!reg->zebra_maps)
350 recTypes_destroy(reg->recTypes);
351 bfs_destroy(reg->bfs);
352 data1_destroy(reg->dh);
357 reg->rank_classes = NULL;
361 reg->keys = zebra_rec_keys_open();
364 reg->sortKeys = zebra_rec_keys_open();
366 reg->sortKeys.buf = 0;
367 reg->sortKeys.buf_max = 0;
378 reg->key_file_no = 0;
381 /* installing rank classes */
382 zebraRankInstall (reg, rank_1_class);
383 zebraRankInstall (reg, rank_zv_class);
384 zebraRankInstall (reg, rank_similarity_class);
385 zebraRankInstall (reg, rank_static_class);
387 recordCompression = res_get_def (res, "recordCompression", "none");
388 if (!strcmp (recordCompression, "none"))
389 record_compression = REC_COMPRESS_NONE;
390 if (!strcmp (recordCompression, "bzip2"))
391 record_compression = REC_COMPRESS_BZIP2;
395 const char *index_fname = res_get_def(res, "index", "default.idx");
396 if (index_fname && *index_fname)
398 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
403 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
405 yaz_log (YLOG_WARN, "rec_open failed");
410 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
412 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
414 yaz_log (YLOG_WARN, "dict_open failed");
417 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
419 yaz_log (YLOG_WARN, "sortIdx_open failed");
422 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
424 struct ISAMS_M_s isams_m;
425 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
426 key_isams_m(res, &isams_m))))
428 yaz_log (YLOG_WARN, "isams_open failed");
432 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
434 struct ISAMC_M_s isamc_m;
435 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
436 rw, key_isamc_m(res, &isamc_m))))
438 yaz_log (YLOG_WARN, "isamc_open failed");
442 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
444 struct ISAMC_M_s isamc_m;
446 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
447 rw, key_isamc_m(res, &isamc_m), 0)))
449 yaz_log (YLOG_WARN, "isamb_open failed");
453 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
455 struct ISAMC_M_s isamc_m;
457 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
458 rw, key_isamc_m(res, &isamc_m), 1)))
460 yaz_log (YLOG_WARN, "isamb_open failed");
464 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
466 struct ISAMC_M_s isamc_m;
468 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
469 rw, key_isamc_m(res, &isamc_m), -1)))
471 yaz_log (YLOG_WARN, "isamb_open failed");
477 reg->zei = zebraExplain_open(reg->records, reg->dh,
482 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
489 zebra_register_close(zs, reg);
492 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
496 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
498 ZEBRA_CHECK_HANDLE(zh);
499 yaz_log(log_level, "zebra_admin_shutdown");
501 zebra_mutex_cond_lock (&zh->service->session_lock);
502 zh->service->stop_flag = 1;
503 zebra_mutex_cond_unlock (&zh->service->session_lock);
507 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
510 ZEBRA_CHECK_HANDLE(zh);
511 yaz_log(log_level, "zebra_admin_start");
513 zebra_mutex_cond_lock (&zs->session_lock);
514 zebra_mutex_cond_unlock (&zs->session_lock);
518 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
522 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
526 zebraExplain_close (reg->zei);
527 dict_close (reg->dict);
529 dict_close (reg->matchDict);
530 sortIdx_close (reg->sortIdx);
532 isams_close (reg->isams);
534 isamc_close (reg->isamc);
536 isamb_close (reg->isamb);
537 rec_close (®->records);
539 recTypes_destroy (reg->recTypes);
540 zebra_maps_close (reg->zebra_maps);
541 zebraRankDestroy (reg);
542 bfs_destroy (reg->bfs);
543 data1_destroy (reg->dh);
545 zebra_rec_keys_close(reg->keys);
547 zebra_rec_keys_close(reg->sortKeys);
549 xfree(reg->sortKeys.buf);
557 ZEBRA_RES zebra_stop(ZebraService zs)
561 yaz_log (log_level, "zebra_stop");
565 zebra_close (zs->sessions);
568 zebra_mutex_cond_destroy (&zs->session_lock);
571 passwd_db_close (zs->passwd_db);
573 recTypeClass_destroy(zs->record_classes);
574 nmem_destroy(zs->nmem);
575 res_close (zs->global_res);
580 ZEBRA_RES zebra_close (ZebraHandle zh)
583 struct zebra_session **sp;
586 yaz_log(log_level, "zebra_close");
587 ZEBRA_CHECK_HANDLE(zh);
592 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
593 resultSetDestroy (zh, -1, 0, 0);
596 zebra_register_close(zh->service, zh->reg);
597 zebra_close_res (zh);
598 res_close(zh->session_res);
600 xfree(zh->record_encoding);
602 xfree(zh->dbaccesslist);
604 for (i = 0; i < zh->num_basenames; i++)
605 xfree(zh->basenames[i]);
606 xfree(zh->basenames);
608 if (zh->iconv_to_utf8 != 0)
609 yaz_iconv_close (zh->iconv_to_utf8);
610 if (zh->iconv_from_utf8 != 0)
611 yaz_iconv_close (zh->iconv_from_utf8);
613 zebra_mutex_cond_lock (&zs->session_lock);
614 zebra_lock_destroy (zh->lock_normal);
615 zebra_lock_destroy (zh->lock_shadow);
627 zebra_mutex_cond_unlock (&zs->session_lock);
629 xfree(zh->user_perm);
630 zh->service = 0; /* more likely to trigger an assert */
632 zebra_limit_destroy(zh->m_limit);
634 nmem_destroy(zh->nmem_error);
641 struct map_baseinfo {
647 char **new_basenames;
651 static void zebra_open_res(ZebraHandle zh)
659 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
660 zh->res = res_open(zh->session_res, 0);
661 res_read_file(zh->res, fname);
663 else if (*zh->reg_name == 0)
665 zh->res = res_open(zh->session_res, 0);
669 yaz_log (YLOG_WARN, "no register root specified");
670 zh->res = 0; /* no path for register - fail! */
674 static void zebra_close_res (ZebraHandle zh)
682 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
686 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
690 assert (zh->reg == 0);
691 assert (*zh->reg_name == 0);
697 resultSetInvalidate (zh);
698 zebra_register_close(zh->service, zh->reg);
704 zh->reg_name = xstrdup (new_reg);
708 if (zh->service->path_root)
710 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
711 strlen(zh->reg_name) + 3);
712 strcpy (zh->path_reg, zh->service->path_root);
715 strcat (zh->path_reg, "/");
716 strcat (zh->path_reg, zh->reg_name);
722 zebra_lock_destroy (zh->lock_normal);
726 zebra_lock_destroy (zh->lock_shadow);
732 const char *lock_area = res_get (zh->res, "lockDir");
734 if (!lock_area && zh->path_reg)
735 res_set (zh->res, "lockDir", zh->path_reg);
736 sprintf (fname, "norm.%s.LCK", zh->reg_name);
738 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
740 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
742 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
744 if (!zh->lock_normal || !zh->lock_shadow)
748 zebra_lock_destroy(zh->lock_normal);
753 zebra_lock_destroy(zh->lock_shadow);
762 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
763 zebra_set_approx_limit(zh, approx);
767 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
768 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
772 void map_basenames_func (void *vp, const char *name, const char *value)
774 struct map_baseinfo *p = (struct map_baseinfo *) vp;
776 char fromdb[128], todb[8][128];
783 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
784 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
785 todb[5], todb[6], todb[7]);
789 for (i = 0; i<p->num_bases; i++)
790 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
793 for (i = 0; i < no; i++)
795 if (p->new_num_bases == p->new_num_max)
797 p->new_basenames[(p->new_num_bases)++] =
798 nmem_strdup (p->mem, todb[i]);
804 int zebra_select_default_database(ZebraHandle zh)
808 /* no database has been selected - so we select based on
809 resource setting (including group)
811 const char *group = res_get(zh->session_res, "group");
812 const char *v = res_get_prefix(zh->session_res,
813 "database", group, "Default");
814 return zebra_select_database(zh, v);
819 void map_basenames (ZebraHandle zh, ODR stream,
820 int *num_bases, char ***basenames)
822 struct map_baseinfo info;
823 struct map_baseinfo *p = &info;
826 yaz_log(log_level, "map_basenames ");
831 info.num_bases = *num_bases;
832 info.basenames = *basenames;
833 info.new_num_max = 128;
834 info.new_num_bases = 0;
835 info.new_basenames = (char **)
836 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
837 info.mem = stream->mem;
839 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
841 for (i = 0; i<p->num_bases; i++)
842 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
844 p->new_basenames[(p->new_num_bases)++] =
845 nmem_strdup (p->mem, p->basenames[i]);
847 *num_bases = info.new_num_bases;
848 *basenames = info.new_basenames;
849 for (i = 0; i<*num_bases; i++)
850 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
853 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
855 ZEBRA_CHECK_HANDLE(zh);
857 yaz_log(log_level, "zebra_select_database %s",basename);
859 return zebra_select_databases (zh, 1, &basename);
862 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
863 const char **basenames)
870 ZEBRA_CHECK_HANDLE(zh);
873 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
874 num_bases,basenames[0]);
879 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
883 /* Check if the user has access to all databases (Seb) */
884 /* You could argue that this should happen later, after we have
885 * determined that the database(s) exist. */
886 if (zh->dbaccesslist) {
887 for (i = 0; i < num_bases; i++) {
888 const char *db = basenames[i];
890 for (p = zh->dbaccesslist; p && *p; p = pp) {
892 if ((pp = strchr(p, '+'))) {
898 if (len == strlen(db) && !strncmp(db, p, len))
902 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
908 for (i = 0; i < zh->num_basenames; i++)
909 xfree(zh->basenames[i]);
910 xfree(zh->basenames);
912 zh->num_basenames = num_bases;
913 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
914 for (i = 0; i < zh->num_basenames; i++)
915 zh->basenames[i] = xstrdup (basenames[i]);
917 cp = strrchr(basenames[0], '/');
920 len = cp - basenames[0];
921 new_reg = xmalloc(len + 1);
922 memcpy (new_reg, basenames[0], len);
926 new_reg = xstrdup ("");
927 for (i = 1; i<num_bases; i++)
931 cp1 = strrchr (basenames[i], '/');
936 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
939 if (len != cp1 - basenames[i] ||
940 memcmp (basenames[i], new_reg, len))
942 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
950 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
955 zebra_select_register (zh, new_reg);
959 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
962 if (!zh->lock_normal || !zh->lock_shadow)
964 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
970 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
972 if (approx_limit == 0)
973 approx_limit = DEFAULT_APPROX_LIMIT;
974 zh->approx_limit = approx_limit;
978 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
979 const char *setname, zint *hits)
983 ZEBRA_CHECK_HANDLE(zh);
989 yaz_log(log_level, "zebra_search_rpn");
993 if (zebra_begin_read(zh) == ZEBRA_FAIL)
996 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
997 zh->num_basenames, zh->basenames, setname);
1003 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1004 const char *setname,
1005 Z_RecordComposition *comp,
1006 oid_value input_format, int num_recs,
1007 ZebraRetrievalRecord *recs)
1009 ZebraMetaRecord *poset;
1011 ZEBRA_RES ret = ZEBRA_OK;
1014 ZEBRA_CHECK_HANDLE(zh);
1020 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1024 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1029 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1032 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1033 for (i = 0; i<num_recs; i++)
1034 pos_array[i] = recs[i].position;
1035 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1038 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1039 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1045 for (i = 0; i<num_recs; i++)
1049 recs[i].errCode = 0;
1050 recs[i].format = VAL_SUTRS;
1051 recs[i].len = strlen(poset[i].term);
1052 recs[i].buf = poset[i].term;
1053 recs[i].base = poset[i].db;
1055 else if (poset[i].sysno)
1059 zebra_snippets *hit_snippet = zebra_snippets_create();
1061 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1065 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1067 stream, input_format, comp,
1068 &recs[i].format, &buf, &len,
1069 &recs[i].base, &recs[i].errString);
1074 recs[i].buf = (char*) odr_malloc(stream, len);
1075 memcpy(recs[i].buf, buf, len);
1079 recs[i].score = poset[i].score;
1080 recs[i].sysno = poset[i].sysno;
1081 zebra_snippets_destroy(hit_snippet);
1085 /* only need to set it once */
1086 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1088 zebra_setError_zint(zh,
1089 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1094 recs[i].buf = 0; /* no record and no error issued */
1096 recs[i].errCode = 0;
1097 recs[i].format = VAL_NONE;
1101 zebra_meta_records_destroy(zh, poset, num_recs);
1103 zebra_end_read (zh);
1108 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1110 int *num_entries, ZebraScanEntry **entries,
1112 const char *setname)
1114 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1115 Z_AttributesPlusTerm *zapt;
1119 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1122 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1125 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1126 position, num_entries, entries, is_partial,
1128 yaz_pqf_destroy (pqf_parser);
1132 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1133 oid_value attributeset,
1135 int *num_entries, ZebraScanEntry **entries,
1137 const char *setname)
1140 RSET limit_rset = 0;
1142 ZEBRA_CHECK_HANDLE(zh);
1147 assert(num_entries);
1150 yaz_log(log_level, "zebra_scan");
1152 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1160 limit_rset = resultSetRef(zh, setname);
1164 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1166 zebra_end_read (zh);
1170 res = rpn_scan (zh, stream, zapt, attributeset,
1171 zh->num_basenames, zh->basenames, position,
1172 num_entries, entries, is_partial, limit_rset, 0);
1173 zebra_end_read (zh);
1177 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1178 int num_input_setnames, const char **input_setnames,
1179 const char *output_setname,
1180 Z_SortKeySpecList *sort_sequence,
1184 ZEBRA_CHECK_HANDLE(zh);
1186 assert(num_input_setnames>0);
1187 assert(input_setnames);
1188 assert(sort_sequence);
1189 assert(sort_status);
1190 yaz_log(log_level, "zebra_sort");
1192 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1194 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1195 output_setname, sort_sequence, sort_status);
1200 int zebra_deleteResultSet(ZebraHandle zh, int function,
1201 int num_setnames, char **setnames,
1207 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1209 if (zebra_begin_read(zh))
1210 return Z_DeleteStatus_systemProblemAtTarget;
1213 case Z_DeleteResultSetRequest_list:
1214 assert(num_setnames>0);
1216 resultSetDestroy (zh, num_setnames, setnames, statuses);
1218 case Z_DeleteResultSetRequest_all:
1219 resultSetDestroy (zh, -1, 0, statuses);
1222 zebra_end_read (zh);
1223 status = Z_DeleteStatus_success;
1224 for (i = 0; i<num_setnames; i++)
1225 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1226 status = statuses[i];
1230 int zebra_errCode (ZebraHandle zh)
1234 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1237 yaz_log(log_level, "zebra_errCode: o");
1241 const char *zebra_errString (ZebraHandle zh)
1245 e= diagbib1_str (zh->errCode);
1246 yaz_log(log_level, "zebra_errString: %s",e);
1250 char *zebra_errAdd (ZebraHandle zh)
1255 yaz_log(log_level, "zebra_errAdd: %s",a);
1259 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1262 const char *astring;
1266 ZEBRA_CHECK_HANDLE(zh);
1270 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1271 p = res_get(zs->global_res, u);
1272 xfree(zh->user_perm);
1273 zh->user_perm = xstrdup(p ? p : "r");
1275 /* Determine database access list */
1276 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1278 zh->dbaccesslist = xstrdup(astring);
1280 zh->dbaccesslist = 0;
1282 /* users that don't require a password .. */
1283 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1286 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1291 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1292 const char *record_type)
1294 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1295 database, record_type);
1296 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1298 return zebra_begin_trans(zh, 1);
1301 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1303 ZEBRA_CHECK_HANDLE(zh);
1304 yaz_log(log_level, "zebra_admin_import_end");
1305 return zebra_end_trans(zh);
1308 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1310 ZEBRA_RES res = ZEBRA_OK;
1313 ZEBRA_CHECK_HANDLE(zh);
1314 yaz_log(log_level, "zebra_admin_import_segment");
1316 for (i = 0; i<segment->num_segmentRecords; i++)
1318 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1320 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1322 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1323 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1325 Odr_oct *oct = fragment->u.notExternallyTagged;
1328 if (zebra_update_record(zh,
1329 0, /* record Type */
1333 (const char *) oct->buf, oct->len,
1342 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1343 const char *rec_buf,
1345 const char *recid_buf, size_t recid_len,
1347 /* 1 = insert. Fail it already exists */
1348 /* 2 = replace. Fail it does not exist */
1349 /* 3 = delete. Fail if does not exist */
1350 /* 4 = update. Insert/replace */
1357 ZEBRA_CHECK_HANDLE(zh);
1358 assert(action>0 && action <=4);
1361 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1363 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1365 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1366 "no record ID or empty record ID");
1370 memcpy (recid_z, recid_buf, recid_len);
1371 recid_z[recid_len] = 0;
1373 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1376 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1377 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1380 if (action == 1) /* fail if insert */
1382 if (zebra_end_trans(zh) != ZEBRA_OK)
1383 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1384 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1385 "Cannot insert record: already exist");
1389 memcpy (&sysno, rinfo+1, sizeof(sysno));
1393 if (action == 2 || action == 3) /* fail if delete or update */
1395 if (zebra_end_trans(zh) != ZEBRA_OK)
1396 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1397 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1398 "Cannot delete/update record: does not exist");
1401 action = 1; /* make it an insert (if it's an update).. */
1403 res = buffer_extract_record (zh, rec_buf, rec_len,
1404 action == 3 ? 1 : 0 /* delete flag */,
1410 0, /* force update */
1411 1 /* allow update */
1413 if (res == ZEBRA_FAIL)
1415 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1416 "Unable to parse record");
1420 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1421 sizeof(sysno), &sysno);
1423 else if (action == 3)
1425 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1427 if (zebra_end_trans(zh) != ZEBRA_OK)
1429 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1435 int delete_w_handle(const char *info, void *handle)
1437 ZebraHandle zh = (ZebraHandle) handle;
1441 if (*info == sizeof(pos))
1443 memcpy (&pos, info+1, sizeof(pos));
1444 isamb_unlink(zh->reg->isamb, pos);
1449 static int delete_SU_handle(void *handle, int ord)
1451 ZebraHandle zh = (ZebraHandle) handle;
1455 ord_len = key_SU_encode (ord, ord_buf);
1456 ord_buf[ord_len] = '\0';
1458 assert (zh->reg->isamb);
1459 dict_delete_subtree(zh->reg->dict, ord_buf,
1460 zh, delete_w_handle);
1464 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1466 ZEBRA_RES ret = ZEBRA_OK;
1468 yaz_log(log_level, "zebra_drop_database %s", db);
1469 ZEBRA_CHECK_HANDLE(zh);
1471 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1473 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1478 zebraExplain_curDatabase (zh->reg->zei, db);
1479 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1480 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1481 0 /* handle */, 0 /* func */);
1482 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1483 zebraExplain_removeDatabase(zh->reg->zei, zh);
1487 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1488 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1489 "drop database only supported for isam:b");
1492 if (zebra_end_trans (zh) != ZEBRA_OK)
1494 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1500 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1502 yaz_log(log_level, "zebra_create_database %s", db);
1503 ZEBRA_CHECK_HANDLE(zh);
1506 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1508 if (zebra_begin_trans (zh, 1))
1511 /* announce database */
1512 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1513 /* explainDatabase */))
1515 if (zebra_end_trans (zh) != ZEBRA_OK)
1517 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1519 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1522 return zebra_end_trans (zh);
1525 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1526 const char *input_str, int input_len,
1527 char *output_str, int output_len)
1533 yaz_log(log_level, "zebra_string_norm ");
1535 if (!zh->reg->zebra_maps)
1537 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1538 input_str, input_len);
1541 if (wrbuf_len(wrbuf) >= output_len)
1543 if (wrbuf_len(wrbuf))
1544 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1545 output_str[wrbuf_len(wrbuf)] = '\0';
1546 return wrbuf_len(wrbuf);
1549 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1551 char state_fname[256];
1556 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1558 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1559 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1560 f = fopen (fname, "w");
1562 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1563 fprintf (f, "%c %d %ld\n", val, seqno, p);
1568 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1570 char state_fname[256];
1575 yaz_log(log_level, "zebra_get_state ");
1577 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1578 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1579 f = fopen (fname, "r");
1585 fscanf (f, "%c %d", val, seqno);
1591 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1593 return zebra_begin_trans(zh, 0);
1596 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1598 return zebra_end_trans(zh);
1601 static void read_res_for_transaction(ZebraHandle zh)
1603 const char *group = res_get(zh->res, "group");
1605 /* FIXME - do we still use groups ?? */
1607 zh->m_group = group;
1608 v = res_get_prefix(zh->res, "followLinks", group, "1");
1609 zh->m_follow_links = atoi(v);
1611 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1612 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1614 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1615 zh->m_store_keys = atoi(v);
1617 v = res_get_prefix(zh->res, "storeData", group, "1");
1618 zh->m_store_data = atoi(v);
1620 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1621 zh->m_explain_database = atoi(v);
1623 v = res_get_prefix(zh->res, "openRW", group, "1");
1624 zh->m_flag_rw = atoi(v);
1626 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1627 zh->m_file_verbose_limit = atoi(v);
1630 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1632 ZEBRA_CHECK_HANDLE(zh);
1633 zebra_select_default_database(zh);
1636 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1637 "zebra_begin_trans: no database selected");
1641 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1645 if (rw && !strchr(zh->user_perm, 'w'))
1649 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1661 const char *rval = 0;
1666 read_res_for_transaction(zh);
1669 if (zh->trans_no != 1)
1671 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1672 "zebra_begin_trans: no write trans within read");
1677 resultSetInvalidate (zh);
1678 zebra_register_close(zh->service, zh->reg);
1680 zh->trans_w_no = zh->trans_no;
1682 zh->records_inserted = 0;
1683 zh->records_updated = 0;
1684 zh->records_deleted = 0;
1685 zh->records_processed = 0;
1687 #if HAVE_SYS_TIMES_H
1691 if (zh->shadow_enable)
1692 rval = res_get (zh->res, "shadow");
1694 for (pass = 0; pass < 2; pass++)
1698 zebra_lock_r (zh->lock_normal);
1699 zebra_lock_w (zh->lock_shadow);
1703 zebra_lock_w (zh->lock_normal);
1704 zebra_lock_w (zh->lock_shadow);
1707 zebra_get_state (zh, &val, &seqno);
1710 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1711 zebra_unlock (zh->lock_shadow);
1712 zebra_unlock (zh->lock_normal);
1716 else if (val == 'd')
1720 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1722 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1723 bf_commitClean (bfs, rval);
1728 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1735 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1739 zebra_set_state (zh, 'd', seqno);
1741 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1742 1, rval ? 1 : 0, zh->res,
1745 zh->reg->seqno = seqno;
1748 zebra_set_state (zh, 'o', seqno);
1750 zebra_unlock (zh->lock_shadow);
1751 zebra_unlock (zh->lock_normal);
1756 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1757 "zebra_begin_trans: cannot open register");
1758 yaz_log(YLOG_FATAL, "%s", zh->errString);
1761 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1771 if (zh->trans_no != 1)
1773 return zebra_flush_reg (zh);
1775 #if HAVE_SYS_TIMES_H
1781 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1784 if (!zh->lock_normal || !zh->lock_shadow)
1787 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1790 zebra_get_state (zh, &val, &seqno);
1796 else if (seqno != zh->reg->seqno)
1798 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1799 seqno, zh->reg->seqno);
1802 else if (zh->reg->last_val != val)
1804 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1805 val, zh->reg->last_val);
1812 zebra_lock_r (zh->lock_shadow);
1814 zebra_lock_r (zh->lock_normal);
1818 resultSetInvalidate (zh);
1819 zebra_register_close(zh->service, zh->reg);
1821 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1822 0, val == 'c' ? 1 : 0,
1823 zh->res, zh->path_reg);
1826 zebra_unlock (zh->lock_normal);
1827 zebra_unlock (zh->lock_shadow);
1829 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1832 zh->reg->last_val = val;
1833 zh->reg->seqno = seqno;
1835 read_res_for_transaction(zh);
1839 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1841 ZebraTransactionStatus dummy;
1843 yaz_log(log_level, "zebra_end_trans");
1844 ZEBRA_CHECK_HANDLE(zh);
1845 return zebra_end_transaction(zh, &dummy);
1848 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1854 ZEBRA_CHECK_HANDLE(zh);
1857 yaz_log(log_level, "zebra_end_transaction");
1859 status->processed = 0;
1860 status->inserted = 0;
1861 status->updated = 0;
1862 status->deleted = 0;
1866 if (!zh->res || !zh->reg)
1868 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1869 "zebra_end_trans: no open transaction");
1872 if (zh->trans_no != zh->trans_w_no)
1875 if (zh->trans_no != 0)
1878 /* release read lock */
1880 zebra_unlock (zh->lock_normal);
1881 zebra_unlock (zh->lock_shadow);
1884 { /* release write lock */
1888 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1889 rval = res_get (zh->res, "shadow");
1891 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1893 zebra_flush_reg (zh);
1895 resultSetInvalidate (zh);
1897 zebra_register_close(zh->service, zh->reg);
1900 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1901 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1902 zh->records_processed, zh->records_inserted,
1903 zh->records_updated, zh->records_deleted);
1905 status->processed = (int) zh->records_processed;
1906 status->inserted = (int) zh->records_inserted;
1907 status->updated = (int) zh->records_updated;
1908 status->deleted = (int) zh->records_deleted;
1910 zebra_get_state (zh, &val, &seqno);
1913 BFiles bfs = bfs_create (rval, zh->path_reg);
1914 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1915 bf_commitClean (bfs, rval);
1920 zebra_set_state (zh, 'o', seqno);
1922 zebra_unlock (zh->lock_shadow);
1923 zebra_unlock (zh->lock_normal);
1926 #if HAVE_SYS_TIMES_H
1928 yaz_log (log_level, "user/system: %ld/%ld",
1929 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1930 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1932 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1933 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1938 int zebra_repository_update (ZebraHandle zh, const char *path)
1942 yaz_log (log_level, "updating %s", path);
1943 repositoryUpdate (zh, path);
1947 int zebra_repository_delete (ZebraHandle zh, const char *path)
1951 yaz_log (log_level, "deleting %s", path);
1952 repositoryDelete (zh, path);
1956 int zebra_repository_show (ZebraHandle zh, const char *path)
1960 yaz_log(log_level, "zebra_repository_show");
1961 repositoryShow (zh, path);
1965 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1973 zebra_select_default_database(zh);
1976 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1979 rval = res_get (zh->res, "shadow");
1982 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1986 zebra_lock_w (zh->lock_normal);
1987 zebra_lock_r (zh->lock_shadow);
1989 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1992 zebra_unlock(zh->lock_shadow);
1993 zebra_unlock(zh->lock_normal);
1996 zebra_get_state (zh, &val, &seqno);
1999 bf_cache (bfs, rval);
2000 if (bf_commitExists (bfs))
2003 zebra_set_state (zh, 'd', seqno);
2006 zebra_set_state (zh, 'c', seqno);
2008 yaz_log (YLOG_DEBUG, "commit start");
2009 bf_commitExec (bfs);
2015 zebra_set_state (zh, 'o', seqno);
2017 zebra_unlock (zh->lock_shadow);
2018 zebra_unlock (zh->lock_normal);
2020 zebra_lock_w(zh->lock_shadow);
2021 bf_commitClean (bfs, rval);
2022 zebra_unlock (zh->lock_normal);
2026 zebra_unlock(zh->lock_shadow);
2027 zebra_unlock(zh->lock_normal);
2028 yaz_log (log_level, "nothing to commit");
2035 ZEBRA_RES zebra_clean(ZebraHandle zh)
2037 yaz_log(log_level, "zebra_clean");
2038 ZEBRA_CHECK_HANDLE(zh);
2039 return zebra_commit_ex(zh, 1);
2042 ZEBRA_RES zebra_commit(ZebraHandle zh)
2044 yaz_log(log_level, "zebra_commit");
2045 ZEBRA_CHECK_HANDLE(zh);
2046 return zebra_commit_ex(zh, 0);
2050 ZEBRA_RES zebra_init(ZebraHandle zh)
2055 yaz_log(log_level, "zebra_init");
2057 ZEBRA_CHECK_HANDLE(zh);
2059 zebra_select_default_database(zh);
2062 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2063 "cannot select default database");
2066 rval = res_get (zh->res, "shadow");
2068 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2071 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2075 bf_cache (bfs, rval);
2079 zebra_set_state (zh, 'o', 0);
2083 ZEBRA_RES zebra_compact(ZebraHandle zh)
2087 yaz_log(log_level, "zebra_compact");
2088 ZEBRA_CHECK_HANDLE(zh);
2091 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2094 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2100 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2102 yaz_log(log_level, "zebra_result");
2105 *code = zh->errCode;
2106 *addinfo = zh->errString;
2110 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2111 *addinfo ="ZebraHandle is NULL";
2115 void zebra_shadow_enable(ZebraHandle zh, int value)
2118 yaz_log(log_level, "zebra_shadow_enable");
2119 zh->shadow_enable = value;
2122 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2124 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2125 ZEBRA_CHECK_HANDLE(zh);
2128 if (zh->iconv_to_utf8 != 0)
2129 yaz_iconv_close(zh->iconv_to_utf8);
2130 if (zh->iconv_from_utf8 != 0)
2131 yaz_iconv_close(zh->iconv_from_utf8);
2134 yaz_iconv_open ("UTF-8", encoding);
2135 if (zh->iconv_to_utf8 == 0)
2136 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2137 zh->iconv_from_utf8 =
2138 yaz_iconv_open (encoding, "UTF-8");
2139 if (zh->iconv_to_utf8 == 0)
2140 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2145 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2147 yaz_log(log_level, "zebra_record_encoding");
2148 ZEBRA_CHECK_HANDLE(zh);
2149 xfree(zh->record_encoding);
2150 zh->record_encoding = 0;
2152 zh->record_encoding = xstrdup (encoding);
2156 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2160 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2162 res_set(zh->res, name, value);
2165 const char *zebra_get_resource(ZebraHandle zh,
2166 const char *name, const char *defaultvalue)
2171 v = res_get_def (zh->res, name, (char *)defaultvalue);
2172 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2176 /* moved from zebra_api_ext.c by pop */
2177 /* FIXME: Should this really be public??? -Heikki */
2179 int zebra_trans_no (ZebraHandle zh)
2181 yaz_log(log_level, "zebra_trans_no");
2183 return zh->trans_no;
2186 int zebra_get_shadow_enable (ZebraHandle zh)
2188 yaz_log(log_level, "zebra_get_shadow_enable");
2190 return zh->shadow_enable;
2193 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2195 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2197 zh->shadow_enable = value;
2200 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2201 so that it's identicical to the original api_records_retrieve */
2202 void api_records_retrieve (ZebraHandle zh, ODR stream,
2203 const char *setname, Z_RecordComposition *comp,
2204 oid_value input_format, int num_recs,
2205 ZebraRetrievalRecord *recs)
2207 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2211 /* ---------------------------------------------------------------------------
2212 Record insert(=update), delete
2214 If sysno is provided, then it's used to identify the record.
2215 If not, and match_criteria is provided, then sysno is guessed
2216 If not, and a record is provided, then sysno is got from there
2217 NOTE: Now returns 0 at success and updates sysno, which is an int*
2221 int zebra_add_record(ZebraHandle zh,
2222 const char *buf, int buf_size)
2224 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2227 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2228 const char *recordType,
2229 SYSNO *sysno, const char *match,
2231 const char *buf, int buf_size, int force_update)
2237 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2240 buf_size = strlen(buf);
2242 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2244 res = buffer_extract_record (zh, buf, buf_size,
2245 0, /* delete_flag */
2251 0); /* allow_update */
2252 if (zebra_end_trans(zh) != ZEBRA_OK)
2254 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2260 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2261 const char *recordType,
2262 SYSNO* sysno, const char *match,
2264 const char *buf, int buf_size,
2269 ZEBRA_CHECK_HANDLE(zh);
2273 yaz_log(log_level, "zebra_update_record");
2275 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2277 if (buf_size < 1) buf_size = strlen(buf);
2279 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2281 res = buffer_extract_record (zh, buf, buf_size,
2282 0, /* delete_flag */
2288 1); /* allow_update */
2289 if (zebra_end_trans(zh) != ZEBRA_OK)
2291 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2297 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2298 const char *recordType,
2299 SYSNO *sysno, const char *match,
2301 const char *buf, int buf_size,
2306 ZEBRA_CHECK_HANDLE(zh);
2309 yaz_log(log_level, "zebra_delete_record");
2311 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2313 if (buf_size < 1) buf_size = strlen(buf);
2315 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2317 res = buffer_extract_record (zh, buf, buf_size,
2318 1, /* delete_flag */
2324 1); /* allow_update */
2325 if (zebra_end_trans(zh) != ZEBRA_OK)
2327 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2333 /* ---------------------------------------------------------------------------
2337 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2338 const char *setname, zint *hits)
2341 ZEBRA_RES res = ZEBRA_OK;
2345 ZEBRA_CHECK_HANDLE(zh);
2347 odr = odr_createmem(ODR_ENCODE);
2352 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2354 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2358 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2359 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2363 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2367 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2375 /* ---------------------------------------------------------------------------
2376 Sort - a simplified interface, with optional read locks.
2378 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2379 const char *sort_spec,
2380 const char *output_setname,
2381 const char **input_setnames)
2383 int num_input_setnames = 0;
2384 int sort_status = 0;
2385 Z_SortKeySpecList *sort_sequence;
2387 ZEBRA_CHECK_HANDLE(zh);
2390 assert(output_setname);
2391 assert(input_setnames);
2392 sort_sequence = yaz_sort_spec (stream, sort_spec);
2393 yaz_log(log_level, "sort (FIXME) ");
2396 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2397 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2401 /* we can do this, since the perl typemap code for char** will
2402 put a NULL at the end of list */
2403 while (input_setnames[num_input_setnames]) num_input_setnames++;
2405 if (zebra_begin_read (zh))
2408 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2409 output_setname, sort_sequence, &sort_status);
2415 /* ---------------------------------------------------------------------------
2416 Get BFS for Zebra system (to make alternative storage methods)
2418 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2421 return zh->reg->bfs;
2426 /* ---------------------------------------------------------------------------
2427 Set limit for search/scan
2429 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2431 ZEBRA_CHECK_HANDLE(zh);
2432 zebra_limit_destroy(zh->m_limit);
2433 zh->m_limit = zebra_limit_create(complement_flag, ids);
2438 Set Error code + addinfo
2440 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2445 nmem_reset(zh->nmem_error);
2446 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2449 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2452 sprintf(vstr, ZINT_FORMAT, i);
2455 nmem_reset(zh->nmem_error);
2456 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2459 void zebra_lock_prefix (Res res, char *path)
2461 const char *lock_dir = res_get_def (res, "lockDir", "");
2463 strcpy (path, lock_dir);
2464 if (*path && path[strlen(path)-1] != '/')