1 /* $Id: zebraapi.c,v 1.187 2005-09-15 12:48:42 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>
40 #include <idzebra/api.h>
42 #define DEFAULT_APPROX_LIMIT 2000000000
44 /* simple asserts to validate the most essential input args */
45 #define ASSERTZH assert(zh && zh->service)
46 #define ASSERTZHRES assert(zh && zh->service && zh->res)
47 #define ASSERTZS assert(zs)
49 static int log_level = 0;
50 static int log_level_initialized = 0;
52 static void zebra_open_res(ZebraHandle zh);
53 static void zebra_close_res(ZebraHandle zh);
55 static void zebra_chdir (ZebraService zs)
59 yaz_log(log_level, "zebra_chdir");
60 dir = res_get (zs->global_res, "chdir");
63 yaz_log (YLOG_DEBUG, "chdir %s", dir);
71 static void zebra_flush_reg (ZebraHandle zh)
74 yaz_log(log_level, "zebra_flush_reg");
76 zebraExplain_flush (zh->reg->zei, zh);
78 extract_flushWriteKeys (zh, 1 /* final */);
79 zebra_index_merge (zh );
82 static struct zebra_register *zebra_register_open(ZebraService zs,
84 int rw, int useshadow,
86 const char *reg_path);
87 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
89 ZebraHandle zebra_open(ZebraService zs, Res res)
92 const char *default_encoding;
93 if (!log_level_initialized)
95 log_level = yaz_log_module_level("zebraapi");
96 log_level_initialized = 1;
99 yaz_log(log_level, "zebra_open");
104 zh = (ZebraHandle) xmalloc(sizeof(*zh));
105 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
108 zh->reg = 0; /* no register attached yet */
114 zh->session_res = res_open(zs->global_res, res);
117 zh->reg_name = xstrdup ("");
119 zh->num_basenames = 0;
122 zh->approx_limit = DEFAULT_APPROX_LIMIT;
129 zh->shadow_enable = 1;
130 zh->m_staticrank = 0;
132 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
135 yaz_iconv_open ("UTF-8", default_encoding);
136 if (zh->iconv_to_utf8 == 0)
137 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
139 zh->iconv_from_utf8 =
140 yaz_iconv_open (default_encoding, "UTF-8");
141 if (zh->iconv_to_utf8 == 0)
142 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
145 zh->record_encoding = 0;
147 zebra_mutex_cond_lock (&zs->session_lock);
149 zh->next = zs->sessions;
152 zebra_mutex_cond_unlock (&zs->session_lock);
154 zh->store_data_buf = 0;
156 zh->m_limit = zebra_limit_create(1, 0);
158 zh->nmem_error = nmem_create();
163 ZebraService zebra_start (const char *configName)
165 return zebra_start_res(configName, 0, 0);
168 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
172 if (!log_level_initialized)
174 log_level = yaz_log_module_level("zebraapi");
175 log_level_initialized = 1;
178 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
179 configName ? configName : "");
181 if ((res = res_open(def_res, over_res)))
183 const char *passwd_plain = 0;
184 const char *passwd_encrypt = 0;
185 ZebraService zh = xmalloc(sizeof(*zh));
188 res_read_file(res, configName);
190 zh->global_res = res;
195 zebra_mutex_cond_init (&zh->session_lock);
196 passwd_plain = res_get (zh->global_res, "passwd");
197 passwd_encrypt = res_get (zh->global_res, "passwd.c");
199 if (!passwd_plain && !passwd_encrypt)
200 zh->passwd_db = NULL;
203 zh->passwd_db = passwd_db_open();
205 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
209 passwd_db_file_plain(zh->passwd_db, passwd_plain);
211 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
214 zh->path_root = res_get (zh->global_res, "root");
215 zh->nmem = nmem_create();
216 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
222 void zebra_filter_info(ZebraService zs, void *cd,
223 void (*cb)(void *cd, const char *name))
227 recTypeClass_info(zs->record_classes, cd, cb);
230 void zebra_pidfname(ZebraService zs, char *path)
233 zebra_lock_prefix (zs->global_res, path);
234 strcat(path, "zebrasrv.pid");
237 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
238 int compact_flag, Res res)
240 int page_size = 4096;
241 char resource_str[200];
242 sprintf (resource_str, "dict.%.100s.pagesize", name);
246 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
247 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
249 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
253 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
254 int rw, int useshadow, Res res,
255 const char *reg_path)
257 struct zebra_register *reg;
258 int record_compression = REC_COMPRESS_NONE;
259 const char *recordCompression = 0;
260 const char *profilePath;
265 reg = xmalloc(sizeof(*reg));
268 reg->name = xstrdup (name);
275 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
276 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
278 reg->dh = data1_createx (DATA1_FLAG_XML);
284 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
287 data1_destroy(reg->dh);
293 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
295 bfs_destroy(reg->bfs);
296 data1_destroy(reg->dh);
302 getcwd(cwd, sizeof(cwd)-1);
303 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
304 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
306 data1_set_tabpath (reg->dh, profilePath);
307 data1_set_tabroot (reg->dh, reg_path);
308 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
310 reg->zebra_maps = zebra_maps_open (res, reg_path);
311 reg->rank_classes = NULL;
315 reg->keys.buf_max = 0;
317 reg->keys.codec_handle = iscz1_start();
319 reg->sortKeys.buf = 0;
320 reg->sortKeys.buf_max = 0;
322 reg->sortKeys.codec_handle = iscz1_start();
334 reg->key_file_no = 0;
337 zebraRankInstall (reg, rank_1_class);
338 zebraRankInstall (reg, rank_zv_class);
339 zebraRankInstall (reg, rank_static_class);
341 recordCompression = res_get_def (res, "recordCompression", "none");
342 if (!strcmp (recordCompression, "none"))
343 record_compression = REC_COMPRESS_NONE;
344 if (!strcmp (recordCompression, "bzip2"))
345 record_compression = REC_COMPRESS_BZIP2;
347 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
349 yaz_log (YLOG_WARN, "rec_open failed");
354 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
356 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
358 yaz_log (YLOG_WARN, "dict_open failed");
361 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
363 yaz_log (YLOG_WARN, "sortIdx_open failed");
366 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
368 struct ISAMS_M_s isams_m;
369 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
370 key_isams_m(res, &isams_m))))
372 yaz_log (YLOG_WARN, "isams_open failed");
376 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
378 struct ISAMC_M_s isamc_m;
379 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
380 rw, key_isamc_m(res, &isamc_m))))
382 yaz_log (YLOG_WARN, "isamc_open failed");
386 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
388 struct ISAMC_M_s isamc_m;
390 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
391 rw, key_isamc_m(res, &isamc_m), 0)))
393 yaz_log (YLOG_WARN, "isamb_open failed");
397 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
399 struct ISAMC_M_s isamc_m;
401 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
402 rw, key_isamc_m(res, &isamc_m), 1)))
404 yaz_log (YLOG_WARN, "isamb_open failed");
408 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
410 struct ISAMC_M_s isamc_m;
412 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
413 rw, key_isamc_m(res, &isamc_m), -1)))
415 yaz_log (YLOG_WARN, "isamb_open failed");
419 reg->zei = zebraExplain_open (reg->records, reg->dh,
424 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
428 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
432 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
435 yaz_log(log_level, "zebra_admin_shutdown");
436 zebra_clearError(zh);
438 zebra_mutex_cond_lock (&zh->service->session_lock);
439 zh->service->stop_flag = 1;
440 zebra_mutex_cond_unlock (&zh->service->session_lock);
444 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
448 yaz_log(log_level, "zebra_admin_start");
449 zebra_clearError(zh);
451 zebra_mutex_cond_lock (&zs->session_lock);
452 zebra_mutex_cond_unlock (&zs->session_lock);
456 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
460 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
465 zebraExplain_close (reg->zei);
466 dict_close (reg->dict);
468 dict_close (reg->matchDict);
469 sortIdx_close (reg->sortIdx);
471 isams_close (reg->isams);
473 isamc_close (reg->isamc);
475 isamb_close (reg->isamb);
476 rec_close (®->records);
479 recTypes_destroy (reg->recTypes);
480 zebra_maps_close (reg->zebra_maps);
481 zebraRankDestroy (reg);
482 bfs_destroy (reg->bfs);
483 data1_destroy (reg->dh);
485 xfree(reg->sortKeys.buf);
486 xfree(reg->keys.buf);
487 if (reg->keys.codec_handle)
488 iscz1_stop(reg->keys.codec_handle);
490 if (reg->sortKeys.codec_handle)
491 iscz1_stop(reg->sortKeys.codec_handle);
498 ZEBRA_RES zebra_stop(ZebraService zs)
502 yaz_log (log_level, "zebra_stop");
506 zebra_close (zs->sessions);
509 zebra_mutex_cond_destroy (&zs->session_lock);
512 passwd_db_close (zs->passwd_db);
514 recTypeClass_destroy(zs->record_classes);
515 nmem_destroy(zs->nmem);
516 res_close (zs->global_res);
521 ZEBRA_RES zebra_close (ZebraHandle zh)
524 struct zebra_session **sp;
527 yaz_log(log_level, "zebra_close");
534 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
535 resultSetDestroy (zh, -1, 0, 0);
538 zebra_register_close (zh->service, zh->reg);
539 zebra_close_res (zh);
540 res_close(zh->session_res);
542 xfree(zh->record_encoding);
544 for (i = 0; i < zh->num_basenames; i++)
545 xfree(zh->basenames[i]);
546 xfree(zh->basenames);
548 if (zh->iconv_to_utf8 != 0)
549 yaz_iconv_close (zh->iconv_to_utf8);
550 if (zh->iconv_from_utf8 != 0)
551 yaz_iconv_close (zh->iconv_from_utf8);
553 zebra_mutex_cond_lock (&zs->session_lock);
554 zebra_lock_destroy (zh->lock_normal);
555 zebra_lock_destroy (zh->lock_shadow);
567 zebra_mutex_cond_unlock (&zs->session_lock);
569 xfree(zh->user_perm);
570 zh->service = 0; /* more likely to trigger an assert */
572 zebra_limit_destroy(zh->m_limit);
574 nmem_destroy(zh->nmem_error);
581 struct map_baseinfo {
587 char **new_basenames;
591 static void zebra_open_res(ZebraHandle zh)
599 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
600 zh->res = res_open(zh->session_res, 0);
601 res_read_file(zh->res, fname);
603 else if (*zh->reg_name == 0)
605 zh->res = res_open(zh->session_res, 0);
609 yaz_log (YLOG_WARN, "no register root specified");
610 zh->res = 0; /* no path for register - fail! */
614 static void zebra_close_res (ZebraHandle zh)
622 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
626 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
630 assert (zh->reg == 0);
631 assert (*zh->reg_name == 0);
637 resultSetInvalidate (zh);
638 zebra_register_close (zh->service, zh->reg);
644 zh->reg_name = xstrdup (new_reg);
648 if (zh->service->path_root)
650 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
651 strlen(zh->reg_name) + 3);
652 strcpy (zh->path_reg, zh->service->path_root);
655 strcat (zh->path_reg, "/");
656 strcat (zh->path_reg, zh->reg_name);
662 zebra_lock_destroy (zh->lock_normal);
666 zebra_lock_destroy (zh->lock_shadow);
672 const char *lock_area = res_get (zh->res, "lockDir");
674 if (!lock_area && zh->path_reg)
675 res_set (zh->res, "lockDir", zh->path_reg);
676 sprintf (fname, "norm.%s.LCK", zh->reg_name);
678 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
680 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
682 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
684 if (!zh->lock_normal || !zh->lock_shadow)
688 zebra_lock_destroy(zh->lock_normal);
693 zebra_lock_destroy(zh->lock_shadow);
702 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
703 zebra_set_approx_limit(zh, approx);
707 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
708 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
710 yaz_log(YLOG_LOG, "static rank unset");
714 void map_basenames_func (void *vp, const char *name, const char *value)
716 struct map_baseinfo *p = (struct map_baseinfo *) vp;
718 char fromdb[128], todb[8][128];
725 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
726 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
727 todb[5], todb[6], todb[7]);
731 for (i = 0; i<p->num_bases; i++)
732 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
735 for (i = 0; i < no; i++)
737 if (p->new_num_bases == p->new_num_max)
739 p->new_basenames[(p->new_num_bases)++] =
740 nmem_strdup (p->mem, todb[i]);
746 int zebra_select_default_database(ZebraHandle zh)
750 /* no database has been selected - so we select based on
751 resource setting (including group)
753 const char *group = res_get(zh->session_res, "group");
754 const char *v = res_get_prefix(zh->session_res,
755 "database", group, "Default");
756 return zebra_select_database(zh, v);
761 void map_basenames (ZebraHandle zh, ODR stream,
762 int *num_bases, char ***basenames)
764 struct map_baseinfo info;
765 struct map_baseinfo *p = &info;
768 yaz_log(log_level, "map_basenames ");
773 info.num_bases = *num_bases;
774 info.basenames = *basenames;
775 info.new_num_max = 128;
776 info.new_num_bases = 0;
777 info.new_basenames = (char **)
778 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
779 info.mem = stream->mem;
781 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
783 for (i = 0; i<p->num_bases; i++)
784 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
786 p->new_basenames[(p->new_num_bases)++] =
787 nmem_strdup (p->mem, p->basenames[i]);
789 *num_bases = info.new_num_bases;
790 *basenames = info.new_basenames;
791 for (i = 0; i<*num_bases; i++)
792 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
795 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
798 yaz_log(log_level, "zebra_select_database %s",basename);
800 return zebra_select_databases (zh, 1, &basename);
803 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
804 const char **basenames)
813 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
814 num_bases,basenames[0]);
819 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
822 for (i = 0; i < zh->num_basenames; i++)
823 xfree(zh->basenames[i]);
824 xfree(zh->basenames);
826 zh->num_basenames = num_bases;
827 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
828 for (i = 0; i < zh->num_basenames; i++)
829 zh->basenames[i] = xstrdup (basenames[i]);
831 cp = strrchr(basenames[0], '/');
834 len = cp - basenames[0];
835 new_reg = xmalloc(len + 1);
836 memcpy (new_reg, basenames[0], len);
840 new_reg = xstrdup ("");
841 for (i = 1; i<num_bases; i++)
845 cp1 = strrchr (basenames[i], '/');
850 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
853 if (len != cp1 - basenames[i] ||
854 memcmp (basenames[i], new_reg, len))
856 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
864 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
869 zebra_select_register (zh, new_reg);
873 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
876 if (!zh->lock_normal || !zh->lock_shadow)
878 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
884 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
886 if (approx_limit == 0)
887 approx_limit = DEFAULT_APPROX_LIMIT;
888 zh->approx_limit = approx_limit;
892 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
893 const char *setname, zint *hits)
901 yaz_log(log_level, "zebra_search_rpn");
902 zebra_clearError(zh);
906 if (zebra_begin_read(zh) == ZEBRA_FAIL)
909 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
910 zh->num_basenames, zh->basenames, setname);
916 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
918 Z_RecordComposition *comp,
919 oid_value input_format, int num_recs,
920 ZebraRetrievalRecord *recs)
922 ZebraMetaRecord *poset;
924 ZEBRA_RES ret = ZEBRA_OK;
932 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
934 zebra_clearError(zh);
937 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
942 if (zebra_begin_read (zh) == ZEBRA_FAIL)
945 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
946 for (i = 0; i<num_recs; i++)
947 pos_array[i] = recs[i].position;
948 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
951 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
952 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
958 for (i = 0; i<num_recs; i++)
963 recs[i].format = VAL_SUTRS;
964 recs[i].len = strlen(poset[i].term);
965 recs[i].buf = poset[i].term;
966 recs[i].base = poset[i].db;
968 else if (poset[i].sysno)
972 zebra_snippets *hit_snippet = zebra_snippets_create();
974 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
978 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
980 stream, input_format, comp,
981 &recs[i].format, &buf, &len,
982 &recs[i].base, &recs[i].errString);
987 recs[i].buf = (char*) odr_malloc(stream, len);
988 memcpy(recs[i].buf, buf, len);
992 recs[i].score = poset[i].score;
993 recs[i].sysno = poset[i].sysno;
994 zebra_snippets_destroy(hit_snippet);
998 /* only need to set it once */
999 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1001 zebra_setError_zint(zh,
1002 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1007 recs[i].buf = 0; /* no record and no error issued */
1009 recs[i].errCode = 0;
1010 recs[i].format = VAL_NONE;
1014 zebra_meta_records_destroy(zh, poset, num_recs);
1016 zebra_end_read (zh);
1021 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1023 int *num_entries, ZebraScanEntry **entries,
1025 const char *setname)
1027 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1028 Z_AttributesPlusTerm *zapt;
1032 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1035 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1038 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1039 position, num_entries, entries, is_partial,
1041 yaz_pqf_destroy (pqf_parser);
1045 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1046 oid_value attributeset,
1048 int *num_entries, ZebraScanEntry **entries,
1050 const char *setname)
1053 RSET limit_rset = 0;
1058 assert(num_entries);
1061 yaz_log(log_level, "zebra_scan");
1062 zebra_clearError(zh);
1063 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1071 limit_rset = resultSetRef(zh, setname);
1075 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1077 zebra_end_read (zh);
1081 res = rpn_scan (zh, stream, zapt, attributeset,
1082 zh->num_basenames, zh->basenames, position,
1083 num_entries, entries, is_partial, limit_rset, 0);
1084 zebra_end_read (zh);
1088 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1089 int num_input_setnames, const char **input_setnames,
1090 const char *output_setname,
1091 Z_SortKeySpecList *sort_sequence,
1097 assert(num_input_setnames>0);
1098 assert(input_setnames);
1099 assert(sort_sequence);
1100 assert(sort_status);
1101 yaz_log(log_level, "zebra_sort");
1102 zebra_clearError(zh);
1103 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1105 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1106 output_setname, sort_sequence, sort_status);
1111 int zebra_deleteResultSet(ZebraHandle zh, int function,
1112 int num_setnames, char **setnames,
1118 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1119 zebra_clearError(zh);;
1120 if (zebra_begin_read(zh))
1121 return Z_DeleteStatus_systemProblemAtTarget;
1124 case Z_DeleteResultSetRequest_list:
1125 assert(num_setnames>0);
1127 resultSetDestroy (zh, num_setnames, setnames, statuses);
1129 case Z_DeleteResultSetRequest_all:
1130 resultSetDestroy (zh, -1, 0, statuses);
1133 zebra_end_read (zh);
1134 status = Z_DeleteStatus_success;
1135 for (i = 0; i<num_setnames; i++)
1136 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1137 status = statuses[i];
1141 int zebra_errCode (ZebraHandle zh)
1145 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1148 yaz_log(log_level, "zebra_errCode: o");
1152 const char *zebra_errString (ZebraHandle zh)
1156 e= diagbib1_str (zh->errCode);
1157 yaz_log(log_level, "zebra_errString: %s",e);
1161 char *zebra_errAdd (ZebraHandle zh)
1166 yaz_log(log_level, "zebra_errAdd: %s",a);
1170 void zebra_clearError(ZebraHandle zh)
1179 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1186 zebra_clearError(zh);
1189 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1190 p = res_get(zs->global_res, u);
1191 xfree(zh->user_perm);
1192 zh->user_perm = xstrdup(p ? p : "r");
1194 /* users that don't require a password .. */
1195 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1198 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1203 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1204 const char *record_type)
1207 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1208 database, record_type);
1209 zebra_clearError(zh);
1210 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1212 return zebra_begin_trans(zh, 1);
1215 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1218 yaz_log(log_level, "zebra_admin_import_end");
1219 zebra_clearError(zh);
1220 return zebra_end_trans(zh);
1223 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1225 ZEBRA_RES res = ZEBRA_OK;
1229 yaz_log(log_level, "zebra_admin_import_segment");
1230 zebra_clearError(zh);
1231 for (i = 0; i<segment->num_segmentRecords; i++)
1233 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1235 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1237 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1238 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1240 Odr_oct *oct = fragment->u.notExternallyTagged;
1243 if (zebra_update_record(zh,
1244 0, /* record Type */
1248 (const char *) oct->buf, oct->len,
1257 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1258 const char *rec_buf,
1260 const char *recid_buf, size_t recid_len,
1262 /* 1 = insert. Fail it already exists */
1263 /* 2 = replace. Fail it does not exist */
1264 /* 3 = delete. Fail if does not exist */
1265 /* 4 = update. Insert/replace */
1272 assert(action>0 && action <=4);
1275 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1276 zebra_clearError(zh);
1278 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1281 memcpy (recid_z, recid_buf, recid_len);
1282 recid_z[recid_len] = 0;
1284 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1287 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1290 if (action == 1) /* fail if insert */
1292 zebra_end_trans(zh);
1296 memcpy (&sysno, rinfo+1, sizeof(sysno));
1300 if (action == 2 || action == 3) /* fail if delete or update */
1302 zebra_end_trans(zh);
1305 action = 1; /* make it an insert (if it's an update).. */
1307 res = buffer_extract_record (zh, rec_buf, rec_len,
1308 action == 3 ? 1 : 0 /* delete flag */,
1314 0, /* force update */
1315 1 /* allow update */
1319 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1321 else if (action == 3)
1323 dict_delete (zh->reg->matchDict, recid_z);
1325 zebra_end_trans(zh);
1329 int delete_w_handle(const char *info, void *handle)
1331 ZebraHandle zh = (ZebraHandle) handle;
1335 if (*info == sizeof(pos))
1337 memcpy (&pos, info+1, sizeof(pos));
1338 isamb_unlink(zh->reg->isamb, pos);
1343 static int delete_SU_handle(void *handle, int ord)
1345 ZebraHandle zh = (ZebraHandle) handle;
1349 ord_len = key_SU_encode (ord, ord_buf);
1350 ord_buf[ord_len] = '\0';
1352 assert (zh->reg->isamb);
1353 dict_delete_subtree(zh->reg->dict, ord_buf,
1354 zh, delete_w_handle);
1358 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1360 ZEBRA_RES ret = ZEBRA_OK;
1362 yaz_log(log_level, "zebra_drop_database %s", db);
1363 zebra_clearError(zh);
1365 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1367 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1371 zebraExplain_curDatabase (zh->reg->zei, db);
1373 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1374 zebraExplain_removeDatabase(zh->reg->zei, zh);
1378 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1381 zebra_end_trans (zh);
1385 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1388 yaz_log(log_level, "zebra_create_database %s", db);
1390 zebra_clearError(zh);
1392 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1394 if (zebra_begin_trans (zh, 1))
1397 /* announce database */
1398 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1399 /* explainDatabase */))
1401 zebra_end_trans (zh);
1402 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1405 return zebra_end_trans (zh);
1408 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1409 const char *input_str, int input_len,
1410 char *output_str, int output_len)
1416 yaz_log(log_level, "zebra_string_norm ");
1417 zebra_clearError(zh);
1418 if (!zh->reg->zebra_maps)
1420 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1421 input_str, input_len);
1424 if (wrbuf_len(wrbuf) >= output_len)
1426 if (wrbuf_len(wrbuf))
1427 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1428 output_str[wrbuf_len(wrbuf)] = '\0';
1429 return wrbuf_len(wrbuf);
1432 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1434 char state_fname[256];
1439 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1440 zebra_clearError(zh);
1442 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1443 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1444 f = fopen (fname, "w");
1446 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1447 fprintf (f, "%c %d %ld\n", val, seqno, p);
1452 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1454 char state_fname[256];
1459 yaz_log(log_level, "zebra_get_state ");
1460 zebra_clearError(zh);
1461 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1462 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1463 f = fopen (fname, "r");
1469 fscanf (f, "%c %d", val, seqno);
1475 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1477 return zebra_begin_trans(zh, 0);
1480 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1482 return zebra_end_trans(zh);
1485 static void read_res_for_transaction(ZebraHandle zh)
1487 const char *group = res_get(zh->res, "group");
1489 /* FIXME - do we still use groups ?? */
1491 zh->m_group = group;
1492 v = res_get_prefix(zh->res, "followLinks", group, "1");
1493 zh->m_follow_links = atoi(v);
1495 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1496 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1498 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1499 zh->m_store_keys = atoi(v);
1501 v = res_get_prefix(zh->res, "storeData", group, "1");
1502 zh->m_store_data = atoi(v);
1504 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1505 zh->m_explain_database = atoi(v);
1507 v = res_get_prefix(zh->res, "openRW", group, "1");
1508 zh->m_flag_rw = atoi(v);
1510 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1511 zh->m_file_verbose_limit = atoi(v);
1514 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1517 zebra_select_default_database(zh);
1520 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1521 "zebra_begin_trans: no database selected");
1525 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1529 if (rw && !strchr(zh->user_perm, 'w'))
1533 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1545 const char *rval = 0;
1550 read_res_for_transaction(zh);
1553 if (zh->trans_no != 1)
1555 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1556 "zebra_begin_trans: no write trans within read");
1561 resultSetInvalidate (zh);
1562 zebra_register_close (zh->service, zh->reg);
1564 zh->trans_w_no = zh->trans_no;
1566 zebra_clearError(zh);
1568 zh->records_inserted = 0;
1569 zh->records_updated = 0;
1570 zh->records_deleted = 0;
1571 zh->records_processed = 0;
1573 #if HAVE_SYS_TIMES_H
1577 if (zh->shadow_enable)
1578 rval = res_get (zh->res, "shadow");
1580 for (pass = 0; pass < 2; pass++)
1584 zebra_lock_r (zh->lock_normal);
1585 zebra_lock_w (zh->lock_shadow);
1589 zebra_lock_w (zh->lock_normal);
1590 zebra_lock_w (zh->lock_shadow);
1593 zebra_get_state (zh, &val, &seqno);
1596 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1597 zebra_unlock (zh->lock_shadow);
1598 zebra_unlock (zh->lock_normal);
1602 else if (val == 'd')
1606 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1608 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1609 bf_commitClean (bfs, rval);
1614 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1621 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1625 zebra_set_state (zh, 'd', seqno);
1627 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1628 1, rval ? 1 : 0, zh->res,
1631 zh->reg->seqno = seqno;
1634 zebra_set_state (zh, 'o', seqno);
1636 zebra_unlock (zh->lock_shadow);
1637 zebra_unlock (zh->lock_normal);
1642 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1643 "zebra_begin_trans: cannot open register");
1644 yaz_log(YLOG_FATAL, "%s", zh->errString);
1656 if (zh->trans_no != 1)
1658 zebra_flush_reg (zh);
1661 zebra_clearError(zh);
1662 #if HAVE_SYS_TIMES_H
1668 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1671 if (!zh->lock_normal || !zh->lock_shadow)
1674 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1677 zebra_get_state (zh, &val, &seqno);
1683 else if (seqno != zh->reg->seqno)
1685 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1686 seqno, zh->reg->seqno);
1689 else if (zh->reg->last_val != val)
1691 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1692 val, zh->reg->last_val);
1699 zebra_lock_r (zh->lock_shadow);
1701 zebra_lock_r (zh->lock_normal);
1705 resultSetInvalidate (zh);
1706 zebra_register_close (zh->service, zh->reg);
1708 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1709 0, val == 'c' ? 1 : 0,
1710 zh->res, zh->path_reg);
1713 zebra_unlock (zh->lock_normal);
1714 zebra_unlock (zh->lock_shadow);
1716 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1719 zh->reg->last_val = val;
1720 zh->reg->seqno = seqno;
1722 read_res_for_transaction(zh);
1726 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1728 ZebraTransactionStatus dummy;
1730 yaz_log(log_level, "zebra_end_trans");
1731 return zebra_end_transaction(zh, &dummy);
1734 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1742 yaz_log(log_level, "zebra_end_transaction");
1744 status->processed = 0;
1745 status->inserted = 0;
1746 status->updated = 0;
1747 status->deleted = 0;
1751 if (!zh->res || !zh->reg)
1753 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1754 "zebra_end_trans: no open transaction");
1757 if (zh->trans_no != zh->trans_w_no)
1760 if (zh->trans_no != 0)
1763 /* release read lock */
1765 zebra_unlock (zh->lock_normal);
1766 zebra_unlock (zh->lock_shadow);
1769 { /* release write lock */
1773 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1774 rval = res_get (zh->res, "shadow");
1776 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1778 zebra_flush_reg (zh);
1780 resultSetInvalidate (zh);
1782 zebra_register_close (zh->service, zh->reg);
1785 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1786 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1787 zh->records_processed, zh->records_inserted,
1788 zh->records_updated, zh->records_deleted);
1790 status->processed = (int) zh->records_processed;
1791 status->inserted = (int) zh->records_inserted;
1792 status->updated = (int) zh->records_updated;
1793 status->deleted = (int) zh->records_deleted;
1795 zebra_get_state (zh, &val, &seqno);
1798 BFiles bfs = bfs_create (rval, zh->path_reg);
1799 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1800 bf_commitClean (bfs, rval);
1805 zebra_set_state (zh, 'o', seqno);
1807 zebra_unlock (zh->lock_shadow);
1808 zebra_unlock (zh->lock_normal);
1811 #if HAVE_SYS_TIMES_H
1813 yaz_log (log_level, "user/system: %ld/%ld",
1814 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1815 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1817 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1818 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1823 int zebra_repository_update (ZebraHandle zh, const char *path)
1827 zebra_clearError(zh);
1828 yaz_log (log_level, "updating %s", path);
1829 repositoryUpdate (zh, path);
1833 int zebra_repository_delete (ZebraHandle zh, const char *path)
1837 zebra_clearError(zh);
1838 yaz_log (log_level, "deleting %s", path);
1839 repositoryDelete (zh, path);
1843 int zebra_repository_show (ZebraHandle zh, const char *path)
1847 yaz_log(log_level, "zebra_repository_show");
1848 zebra_clearError(zh);
1849 repositoryShow (zh, path);
1853 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1860 zebra_clearError(zh);
1862 zebra_select_default_database(zh);
1865 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1868 rval = res_get (zh->res, "shadow");
1871 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1875 zebra_lock_w (zh->lock_normal);
1876 zebra_lock_r (zh->lock_shadow);
1878 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1880 zebra_get_state (zh, &val, &seqno);
1883 bf_cache (bfs, rval);
1884 if (bf_commitExists (bfs))
1887 zebra_set_state (zh, 'd', seqno);
1890 zebra_set_state (zh, 'c', seqno);
1892 yaz_log (YLOG_DEBUG, "commit start");
1893 bf_commitExec (bfs);
1898 yaz_log (YLOG_DEBUG, "commit clean");
1899 bf_commitClean (bfs, rval);
1901 zebra_set_state (zh, 'o', seqno);
1905 yaz_log (log_level, "nothing to commit");
1909 zebra_unlock (zh->lock_shadow);
1910 zebra_unlock (zh->lock_normal);
1914 ZEBRA_RES zebra_clean(ZebraHandle zh)
1917 yaz_log(log_level, "zebra_clean");
1918 return zebra_commit_ex(zh, 1);
1921 ZEBRA_RES zebra_commit(ZebraHandle zh)
1924 yaz_log(log_level, "zebra_commit");
1925 return zebra_commit_ex(zh, 0);
1928 ZEBRA_RES zebra_init(ZebraHandle zh)
1933 yaz_log(log_level, "zebra_init");
1934 zebra_clearError(zh);
1936 zebra_select_default_database(zh);
1939 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1940 "cannot select default database");
1943 rval = res_get (zh->res, "shadow");
1945 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1948 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1952 bf_cache (bfs, rval);
1956 zebra_set_state (zh, 'o', 0);
1960 ZEBRA_RES zebra_compact(ZebraHandle zh)
1964 yaz_log(log_level, "zebra_compact");
1965 zebra_clearError(zh);
1968 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1971 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1977 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1980 yaz_log(log_level, "zebra_result");
1981 *code = zh->errCode;
1982 *addinfo = zh->errString;
1985 void zebra_shadow_enable(ZebraHandle zh, int value)
1988 yaz_log(log_level, "zebra_shadow_enable");
1989 zebra_clearError(zh);
1990 zh->shadow_enable = value;
1993 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1997 yaz_log(log_level, "zebra_octet_term_encoding");
1998 zebra_clearError(zh);
2000 if (zh->iconv_to_utf8 != 0)
2001 yaz_iconv_close(zh->iconv_to_utf8);
2002 if (zh->iconv_from_utf8 != 0)
2003 yaz_iconv_close(zh->iconv_from_utf8);
2006 yaz_iconv_open ("UTF-8", encoding);
2007 if (zh->iconv_to_utf8 == 0)
2008 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2009 zh->iconv_from_utf8 =
2010 yaz_iconv_open (encoding, "UTF-8");
2011 if (zh->iconv_to_utf8 == 0)
2012 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2017 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2020 yaz_log(log_level, "zebra_record_encoding");
2021 zebra_clearError(zh);
2022 xfree(zh->record_encoding);
2023 zh->record_encoding = 0;
2025 zh->record_encoding = xstrdup (encoding);
2029 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2034 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2035 zebra_clearError(zh);
2036 res_set(zh->res, name, value);
2039 const char *zebra_get_resource(ZebraHandle zh,
2040 const char *name, const char *defaultvalue)
2045 v = res_get_def (zh->res, name, (char *)defaultvalue);
2046 zebra_clearError(zh);
2047 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2051 /* moved from zebra_api_ext.c by pop */
2052 /* FIXME: Should this really be public??? -Heikki */
2054 int zebra_trans_no (ZebraHandle zh)
2057 yaz_log(log_level, "zebra_trans_no");
2058 return zh->trans_no;
2061 int zebra_get_shadow_enable (ZebraHandle zh)
2064 yaz_log(log_level, "zebra_get_shadow_enable");
2065 return zh->shadow_enable;
2068 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2071 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2072 zh->shadow_enable = value;
2075 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2076 so that it's identicical to the original api_records_retrieve */
2077 void api_records_retrieve (ZebraHandle zh, ODR stream,
2078 const char *setname, Z_RecordComposition *comp,
2079 oid_value input_format, int num_recs,
2080 ZebraRetrievalRecord *recs)
2082 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2086 /* ---------------------------------------------------------------------------
2087 Record insert(=update), delete
2089 If sysno is provided, then it's used to identify the record.
2090 If not, and match_criteria is provided, then sysno is guessed
2091 If not, and a record is provided, then sysno is got from there
2092 NOTE: Now returns 0 at success and updates sysno, which is an int*
2096 int zebra_add_record(ZebraHandle zh,
2097 const char *buf, int buf_size)
2100 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2103 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2104 const char *recordType,
2105 SYSNO *sysno, const char *match,
2107 const char *buf, int buf_size, int force_update)
2113 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2116 buf_size = strlen(buf);
2118 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2120 res = buffer_extract_record (zh, buf, buf_size,
2121 0, /* delete_flag */
2127 0); /* allow_update */
2128 zebra_end_trans(zh);
2132 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2133 const char *recordType,
2134 SYSNO* sysno, const char *match,
2136 const char *buf, int buf_size,
2144 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2146 if (buf_size < 1) buf_size = strlen(buf);
2148 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2150 res = buffer_extract_record (zh, buf, buf_size,
2151 0, /* delete_flag */
2157 1); /* allow_update */
2158 zebra_end_trans(zh);
2162 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2163 const char *recordType,
2164 SYSNO *sysno, const char *match,
2166 const char *buf, int buf_size,
2173 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2175 if (buf_size < 1) buf_size = strlen(buf);
2177 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2179 res = buffer_extract_record (zh, buf, buf_size,
2180 1, /* delete_flag */
2186 1); /* allow_update */
2187 zebra_end_trans(zh);
2191 /* ---------------------------------------------------------------------------
2195 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2196 const char *setname, zint *hits)
2199 ZEBRA_RES res = ZEBRA_OK;
2201 ODR odr = odr_createmem(ODR_ENCODE);
2206 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2208 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2212 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2213 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2217 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2221 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2229 /* ---------------------------------------------------------------------------
2230 Sort - a simplified interface, with optional read locks.
2232 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2233 const char *sort_spec,
2234 const char *output_setname,
2235 const char **input_setnames)
2237 int num_input_setnames = 0;
2238 int sort_status = 0;
2239 Z_SortKeySpecList *sort_sequence;
2243 assert(output_setname);
2244 assert(input_setnames);
2245 sort_sequence = yaz_sort_spec (stream, sort_spec);
2246 yaz_log(log_level, "sort (FIXME) ");
2249 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2250 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2254 /* we can do this, since the perl typemap code for char** will
2255 put a NULL at the end of list */
2256 while (input_setnames[num_input_setnames]) num_input_setnames++;
2258 if (zebra_begin_read (zh))
2261 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2262 output_setname, sort_sequence, &sort_status);
2268 /* ---------------------------------------------------------------------------
2269 Get BFS for Zebra system (to make alternative storage methods)
2271 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2274 return zh->reg->bfs;
2279 /* ---------------------------------------------------------------------------
2280 Set limit for search/scan
2282 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2285 zebra_limit_destroy(zh->m_limit);
2286 zh->m_limit = zebra_limit_create(complement_flag, ids);
2291 Set Error code + addinfo
2293 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2296 nmem_reset(zh->nmem_error);
2297 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2300 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2303 sprintf(vstr, ZINT_FORMAT, i);
2306 nmem_reset(zh->nmem_error);
2307 zh->errString = nmem_strdup(zh->nmem_error, vstr);