1 /* $Id: zebraapi.c,v 1.186 2005-09-15 09:27:18 pop 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;
134 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
137 yaz_iconv_open ("UTF-8", default_encoding);
138 if (zh->iconv_to_utf8 == 0)
139 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
141 zh->iconv_from_utf8 =
142 yaz_iconv_open (default_encoding, "UTF-8");
143 if (zh->iconv_to_utf8 == 0)
144 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
147 zh->record_encoding = 0;
149 zebra_mutex_cond_lock (&zs->session_lock);
151 zh->next = zs->sessions;
154 zebra_mutex_cond_unlock (&zs->session_lock);
156 zh->store_data_buf = 0;
158 zh->m_limit = zebra_limit_create(1, 0);
160 zh->nmem_error = nmem_create();
165 ZebraService zebra_start (const char *configName)
167 return zebra_start_res(configName, 0, 0);
170 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
174 if (!log_level_initialized)
176 log_level = yaz_log_module_level("zebraapi");
177 log_level_initialized = 1;
180 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
181 configName ? configName : "");
183 if ((res = res_open(def_res, over_res)))
185 const char *passwd_plain = 0;
186 const char *passwd_encrypt = 0;
187 ZebraService zh = xmalloc(sizeof(*zh));
190 res_read_file(res, configName);
192 zh->global_res = res;
197 zebra_mutex_cond_init (&zh->session_lock);
198 passwd_plain = res_get (zh->global_res, "passwd");
199 passwd_encrypt = res_get (zh->global_res, "passwd.c");
201 if (!passwd_plain && !passwd_encrypt)
202 zh->passwd_db = NULL;
205 zh->passwd_db = passwd_db_open();
207 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
211 passwd_db_file_plain(zh->passwd_db, passwd_plain);
213 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
216 zh->path_root = res_get (zh->global_res, "root");
217 zh->nmem = nmem_create();
218 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
224 void zebra_filter_info(ZebraService zs, void *cd,
225 void (*cb)(void *cd, const char *name))
229 recTypeClass_info(zs->record_classes, cd, cb);
232 void zebra_pidfname(ZebraService zs, char *path)
235 zebra_lock_prefix (zs->global_res, path);
236 strcat(path, "zebrasrv.pid");
239 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
240 int compact_flag, Res res)
242 int page_size = 4096;
243 char resource_str[200];
244 sprintf (resource_str, "dict.%.100s.pagesize", name);
248 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
249 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
251 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
255 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
256 int rw, int useshadow, Res res,
257 const char *reg_path)
259 struct zebra_register *reg;
260 int record_compression = REC_COMPRESS_NONE;
261 const char *recordCompression = 0;
262 const char *profilePath;
267 reg = xmalloc(sizeof(*reg));
270 reg->name = xstrdup (name);
277 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
278 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
280 reg->dh = data1_createx (DATA1_FLAG_XML);
286 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
289 data1_destroy(reg->dh);
295 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
297 bfs_destroy(reg->bfs);
298 data1_destroy(reg->dh);
304 getcwd(cwd, sizeof(cwd)-1);
305 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
306 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
308 data1_set_tabpath (reg->dh, profilePath);
309 data1_set_tabroot (reg->dh, reg_path);
310 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
312 reg->zebra_maps = zebra_maps_open (res, reg_path);
313 reg->rank_classes = NULL;
317 reg->keys.buf_max = 0;
319 reg->keys.codec_handle = iscz1_start();
321 reg->sortKeys.buf = 0;
322 reg->sortKeys.buf_max = 0;
324 reg->sortKeys.codec_handle = iscz1_start();
336 reg->key_file_no = 0;
339 zebraRankInstall (reg, rank_1_class);
340 zebraRankInstall (reg, rank_zv_class);
341 zebraRankInstall (reg, rank_static_class);
343 recordCompression = res_get_def (res, "recordCompression", "none");
344 if (!strcmp (recordCompression, "none"))
345 record_compression = REC_COMPRESS_NONE;
346 if (!strcmp (recordCompression, "bzip2"))
347 record_compression = REC_COMPRESS_BZIP2;
349 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
351 yaz_log (YLOG_WARN, "rec_open failed");
356 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
358 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
360 yaz_log (YLOG_WARN, "dict_open failed");
363 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
365 yaz_log (YLOG_WARN, "sortIdx_open failed");
368 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
370 struct ISAMS_M_s isams_m;
371 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
372 key_isams_m(res, &isams_m))))
374 yaz_log (YLOG_WARN, "isams_open failed");
378 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
380 struct ISAMC_M_s isamc_m;
381 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
382 rw, key_isamc_m(res, &isamc_m))))
384 yaz_log (YLOG_WARN, "isamc_open failed");
388 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
390 struct ISAMC_M_s isamc_m;
392 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
393 rw, key_isamc_m(res, &isamc_m), 0)))
395 yaz_log (YLOG_WARN, "isamb_open failed");
399 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
401 struct ISAMC_M_s isamc_m;
403 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
404 rw, key_isamc_m(res, &isamc_m), 1)))
406 yaz_log (YLOG_WARN, "isamb_open failed");
410 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
412 struct ISAMC_M_s isamc_m;
414 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
415 rw, key_isamc_m(res, &isamc_m), -1)))
417 yaz_log (YLOG_WARN, "isamb_open failed");
421 reg->zei = zebraExplain_open (reg->records, reg->dh,
426 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
430 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
434 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
437 yaz_log(log_level, "zebra_admin_shutdown");
438 zebra_clearError(zh);
440 zebra_mutex_cond_lock (&zh->service->session_lock);
441 zh->service->stop_flag = 1;
442 zebra_mutex_cond_unlock (&zh->service->session_lock);
446 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
450 yaz_log(log_level, "zebra_admin_start");
451 zebra_clearError(zh);
453 zebra_mutex_cond_lock (&zs->session_lock);
454 zebra_mutex_cond_unlock (&zs->session_lock);
458 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
462 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
467 zebraExplain_close (reg->zei);
468 dict_close (reg->dict);
470 dict_close (reg->matchDict);
471 sortIdx_close (reg->sortIdx);
473 isams_close (reg->isams);
475 isamc_close (reg->isamc);
477 isamb_close (reg->isamb);
478 rec_close (®->records);
481 recTypes_destroy (reg->recTypes);
482 zebra_maps_close (reg->zebra_maps);
483 zebraRankDestroy (reg);
484 bfs_destroy (reg->bfs);
485 data1_destroy (reg->dh);
487 xfree(reg->sortKeys.buf);
488 xfree(reg->keys.buf);
489 if (reg->keys.codec_handle)
490 iscz1_stop(reg->keys.codec_handle);
492 if (reg->sortKeys.codec_handle)
493 iscz1_stop(reg->sortKeys.codec_handle);
500 ZEBRA_RES zebra_stop(ZebraService zs)
504 yaz_log (log_level, "zebra_stop");
508 zebra_close (zs->sessions);
511 zebra_mutex_cond_destroy (&zs->session_lock);
514 passwd_db_close (zs->passwd_db);
516 recTypeClass_destroy(zs->record_classes);
517 nmem_destroy(zs->nmem);
518 res_close (zs->global_res);
523 ZEBRA_RES zebra_close (ZebraHandle zh)
526 struct zebra_session **sp;
529 yaz_log(log_level, "zebra_close");
536 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
537 resultSetDestroy (zh, -1, 0, 0);
540 zebra_register_close (zh->service, zh->reg);
541 zebra_close_res (zh);
542 res_close(zh->session_res);
544 xfree(zh->record_encoding);
546 for (i = 0; i < zh->num_basenames; i++)
547 xfree(zh->basenames[i]);
548 xfree(zh->basenames);
550 if (zh->iconv_to_utf8 != 0)
551 yaz_iconv_close (zh->iconv_to_utf8);
552 if (zh->iconv_from_utf8 != 0)
553 yaz_iconv_close (zh->iconv_from_utf8);
555 zebra_mutex_cond_lock (&zs->session_lock);
556 zebra_lock_destroy (zh->lock_normal);
557 zebra_lock_destroy (zh->lock_shadow);
569 zebra_mutex_cond_unlock (&zs->session_lock);
571 xfree(zh->user_perm);
572 zh->service = 0; /* more likely to trigger an assert */
574 zebra_limit_destroy(zh->m_limit);
576 nmem_destroy(zh->nmem_error);
583 struct map_baseinfo {
589 char **new_basenames;
593 static void zebra_open_res(ZebraHandle zh)
601 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
602 zh->res = res_open(zh->session_res, 0);
603 res_read_file(zh->res, fname);
605 else if (*zh->reg_name == 0)
607 zh->res = res_open(zh->session_res, 0);
611 yaz_log (YLOG_WARN, "no register root specified");
612 zh->res = 0; /* no path for register - fail! */
616 static void zebra_close_res (ZebraHandle zh)
624 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
628 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
632 assert (zh->reg == 0);
633 assert (*zh->reg_name == 0);
639 resultSetInvalidate (zh);
640 zebra_register_close (zh->service, zh->reg);
646 zh->reg_name = xstrdup (new_reg);
650 if (zh->service->path_root)
652 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
653 strlen(zh->reg_name) + 3);
654 strcpy (zh->path_reg, zh->service->path_root);
657 strcat (zh->path_reg, "/");
658 strcat (zh->path_reg, zh->reg_name);
664 zebra_lock_destroy (zh->lock_normal);
668 zebra_lock_destroy (zh->lock_shadow);
674 const char *lock_area = res_get (zh->res, "lockDir");
676 if (!lock_area && zh->path_reg)
677 res_set (zh->res, "lockDir", zh->path_reg);
678 sprintf (fname, "norm.%s.LCK", zh->reg_name);
680 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
682 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
684 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
686 if (!zh->lock_normal || !zh->lock_shadow)
690 zebra_lock_destroy(zh->lock_normal);
695 zebra_lock_destroy(zh->lock_shadow);
704 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
705 zebra_set_approx_limit(zh, approx);
709 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
710 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
712 yaz_log(YLOG_LOG, "static rank unset");
716 void map_basenames_func (void *vp, const char *name, const char *value)
718 struct map_baseinfo *p = (struct map_baseinfo *) vp;
720 char fromdb[128], todb[8][128];
727 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
728 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
729 todb[5], todb[6], todb[7]);
733 for (i = 0; i<p->num_bases; i++)
734 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
737 for (i = 0; i < no; i++)
739 if (p->new_num_bases == p->new_num_max)
741 p->new_basenames[(p->new_num_bases)++] =
742 nmem_strdup (p->mem, todb[i]);
748 int zebra_select_default_database(ZebraHandle zh)
752 /* no database has been selected - so we select based on
753 resource setting (including group)
755 const char *group = res_get(zh->session_res, "group");
756 const char *v = res_get_prefix(zh->session_res,
757 "database", group, "Default");
758 return zebra_select_database(zh, v);
763 void map_basenames (ZebraHandle zh, ODR stream,
764 int *num_bases, char ***basenames)
766 struct map_baseinfo info;
767 struct map_baseinfo *p = &info;
770 yaz_log(log_level, "map_basenames ");
775 info.num_bases = *num_bases;
776 info.basenames = *basenames;
777 info.new_num_max = 128;
778 info.new_num_bases = 0;
779 info.new_basenames = (char **)
780 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
781 info.mem = stream->mem;
783 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
785 for (i = 0; i<p->num_bases; i++)
786 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
788 p->new_basenames[(p->new_num_bases)++] =
789 nmem_strdup (p->mem, p->basenames[i]);
791 *num_bases = info.new_num_bases;
792 *basenames = info.new_basenames;
793 for (i = 0; i<*num_bases; i++)
794 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
797 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
800 yaz_log(log_level, "zebra_select_database %s",basename);
802 return zebra_select_databases (zh, 1, &basename);
805 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
806 const char **basenames)
815 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
816 num_bases,basenames[0]);
821 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
824 for (i = 0; i < zh->num_basenames; i++)
825 xfree(zh->basenames[i]);
826 xfree(zh->basenames);
828 zh->num_basenames = num_bases;
829 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
830 for (i = 0; i < zh->num_basenames; i++)
831 zh->basenames[i] = xstrdup (basenames[i]);
833 cp = strrchr(basenames[0], '/');
836 len = cp - basenames[0];
837 new_reg = xmalloc(len + 1);
838 memcpy (new_reg, basenames[0], len);
842 new_reg = xstrdup ("");
843 for (i = 1; i<num_bases; i++)
847 cp1 = strrchr (basenames[i], '/');
852 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
855 if (len != cp1 - basenames[i] ||
856 memcmp (basenames[i], new_reg, len))
858 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
866 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
871 zebra_select_register (zh, new_reg);
875 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
878 if (!zh->lock_normal || !zh->lock_shadow)
880 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
886 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
888 if (approx_limit == 0)
889 approx_limit = DEFAULT_APPROX_LIMIT;
890 zh->approx_limit = approx_limit;
894 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
895 const char *setname, zint *hits)
903 yaz_log(log_level, "zebra_search_rpn");
904 zebra_clearError(zh);
908 if (zebra_begin_read(zh) == ZEBRA_FAIL)
911 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
912 zh->num_basenames, zh->basenames, setname);
918 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
920 Z_RecordComposition *comp,
921 oid_value input_format, int num_recs,
922 ZebraRetrievalRecord *recs)
924 ZebraMetaRecord *poset;
926 ZEBRA_RES ret = ZEBRA_OK;
934 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
936 zebra_clearError(zh);
939 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
944 if (zebra_begin_read (zh) == ZEBRA_FAIL)
947 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
948 for (i = 0; i<num_recs; i++)
949 pos_array[i] = recs[i].position;
950 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
953 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
954 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
960 for (i = 0; i<num_recs; i++)
965 recs[i].format = VAL_SUTRS;
966 recs[i].len = strlen(poset[i].term);
967 recs[i].buf = poset[i].term;
968 recs[i].base = poset[i].db;
970 else if (poset[i].sysno)
974 zebra_snippets *hit_snippet = zebra_snippets_create();
976 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
980 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
982 stream, input_format, comp,
983 &recs[i].format, &buf, &len,
984 &recs[i].base, &recs[i].errString);
989 recs[i].buf = (char*) odr_malloc(stream, len);
990 memcpy(recs[i].buf, buf, len);
994 recs[i].score = poset[i].score;
995 recs[i].sysno = poset[i].sysno;
996 zebra_snippets_destroy(hit_snippet);
1000 /* only need to set it once */
1001 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1003 zebra_setError_zint(zh,
1004 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1009 recs[i].buf = 0; /* no record and no error issued */
1011 recs[i].errCode = 0;
1012 recs[i].format = VAL_NONE;
1016 zebra_meta_records_destroy(zh, poset, num_recs);
1018 zebra_end_read (zh);
1023 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1025 int *num_entries, ZebraScanEntry **entries,
1027 const char *setname)
1029 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1030 Z_AttributesPlusTerm *zapt;
1034 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1037 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1040 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1041 position, num_entries, entries, is_partial,
1043 yaz_pqf_destroy (pqf_parser);
1047 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1048 oid_value attributeset,
1050 int *num_entries, ZebraScanEntry **entries,
1052 const char *setname)
1055 RSET limit_rset = 0;
1060 assert(num_entries);
1063 yaz_log(log_level, "zebra_scan");
1064 zebra_clearError(zh);
1065 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1073 limit_rset = resultSetRef(zh, setname);
1077 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1079 zebra_end_read (zh);
1083 res = rpn_scan (zh, stream, zapt, attributeset,
1084 zh->num_basenames, zh->basenames, position,
1085 num_entries, entries, is_partial, limit_rset, 0);
1086 zebra_end_read (zh);
1090 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1091 int num_input_setnames, const char **input_setnames,
1092 const char *output_setname,
1093 Z_SortKeySpecList *sort_sequence,
1099 assert(num_input_setnames>0);
1100 assert(input_setnames);
1101 assert(sort_sequence);
1102 assert(sort_status);
1103 yaz_log(log_level, "zebra_sort");
1104 zebra_clearError(zh);
1105 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1107 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1108 output_setname, sort_sequence, sort_status);
1113 int zebra_deleteResultSet(ZebraHandle zh, int function,
1114 int num_setnames, char **setnames,
1120 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1121 zebra_clearError(zh);;
1122 if (zebra_begin_read(zh))
1123 return Z_DeleteStatus_systemProblemAtTarget;
1126 case Z_DeleteResultSetRequest_list:
1127 assert(num_setnames>0);
1129 resultSetDestroy (zh, num_setnames, setnames, statuses);
1131 case Z_DeleteResultSetRequest_all:
1132 resultSetDestroy (zh, -1, 0, statuses);
1135 zebra_end_read (zh);
1136 status = Z_DeleteStatus_success;
1137 for (i = 0; i<num_setnames; i++)
1138 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1139 status = statuses[i];
1143 int zebra_errCode (ZebraHandle zh)
1147 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1150 yaz_log(log_level, "zebra_errCode: o");
1154 const char *zebra_errString (ZebraHandle zh)
1158 e= diagbib1_str (zh->errCode);
1159 yaz_log(log_level, "zebra_errString: %s",e);
1163 char *zebra_errAdd (ZebraHandle zh)
1168 yaz_log(log_level, "zebra_errAdd: %s",a);
1172 void zebra_clearError(ZebraHandle zh)
1181 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1188 zebra_clearError(zh);
1191 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1192 p = res_get(zs->global_res, u);
1193 xfree(zh->user_perm);
1194 zh->user_perm = xstrdup(p ? p : "r");
1196 /* users that don't require a password .. */
1197 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1200 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1205 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1206 const char *record_type)
1209 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1210 database, record_type);
1211 zebra_clearError(zh);
1212 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1214 return zebra_begin_trans(zh, 1);
1217 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1220 yaz_log(log_level, "zebra_admin_import_end");
1221 zebra_clearError(zh);
1222 return zebra_end_trans(zh);
1225 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1227 ZEBRA_RES res = ZEBRA_OK;
1231 yaz_log(log_level, "zebra_admin_import_segment");
1232 zebra_clearError(zh);
1233 for (i = 0; i<segment->num_segmentRecords; i++)
1235 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1237 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1239 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1240 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1242 Odr_oct *oct = fragment->u.notExternallyTagged;
1245 if (zebra_update_record(zh,
1246 0, /* record Type */
1250 (const char *) oct->buf, oct->len,
1259 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1260 const char *rec_buf,
1262 const char *recid_buf, size_t recid_len,
1264 /* 1 = insert. Fail it already exists */
1265 /* 2 = replace. Fail it does not exist */
1266 /* 3 = delete. Fail if does not exist */
1267 /* 4 = update. Insert/replace */
1274 assert(action>0 && action <=4);
1277 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1278 zebra_clearError(zh);
1280 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1283 memcpy (recid_z, recid_buf, recid_len);
1284 recid_z[recid_len] = 0;
1286 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1289 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1292 if (action == 1) /* fail if insert */
1294 zebra_end_trans(zh);
1298 memcpy (&sysno, rinfo+1, sizeof(sysno));
1302 if (action == 2 || action == 3) /* fail if delete or update */
1304 zebra_end_trans(zh);
1307 action = 1; /* make it an insert (if it's an update).. */
1309 res = buffer_extract_record (zh, rec_buf, rec_len,
1310 action == 3 ? 1 : 0 /* delete flag */,
1316 0, /* force update */
1317 1 /* allow update */
1321 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1323 else if (action == 3)
1325 dict_delete (zh->reg->matchDict, recid_z);
1327 zebra_end_trans(zh);
1331 int delete_w_handle(const char *info, void *handle)
1333 ZebraHandle zh = (ZebraHandle) handle;
1337 if (*info == sizeof(pos))
1339 memcpy (&pos, info+1, sizeof(pos));
1340 isamb_unlink(zh->reg->isamb, pos);
1345 static int delete_SU_handle(void *handle, int ord)
1347 ZebraHandle zh = (ZebraHandle) handle;
1351 ord_len = key_SU_encode (ord, ord_buf);
1352 ord_buf[ord_len] = '\0';
1354 assert (zh->reg->isamb);
1355 dict_delete_subtree(zh->reg->dict, ord_buf,
1356 zh, delete_w_handle);
1360 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1362 ZEBRA_RES ret = ZEBRA_OK;
1364 yaz_log(log_level, "zebra_drop_database %s", db);
1365 zebra_clearError(zh);
1367 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1369 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1373 zebraExplain_curDatabase (zh->reg->zei, db);
1375 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1376 zebraExplain_removeDatabase(zh->reg->zei, zh);
1380 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1383 zebra_end_trans (zh);
1387 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1390 yaz_log(log_level, "zebra_create_database %s", db);
1392 zebra_clearError(zh);
1394 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1396 if (zebra_begin_trans (zh, 1))
1399 /* announce database */
1400 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1401 /* explainDatabase */))
1403 zebra_end_trans (zh);
1404 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1407 return zebra_end_trans (zh);
1410 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1411 const char *input_str, int input_len,
1412 char *output_str, int output_len)
1418 yaz_log(log_level, "zebra_string_norm ");
1419 zebra_clearError(zh);
1420 if (!zh->reg->zebra_maps)
1422 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1423 input_str, input_len);
1426 if (wrbuf_len(wrbuf) >= output_len)
1428 if (wrbuf_len(wrbuf))
1429 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1430 output_str[wrbuf_len(wrbuf)] = '\0';
1431 return wrbuf_len(wrbuf);
1434 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1436 char state_fname[256];
1441 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1442 zebra_clearError(zh);
1444 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1445 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1446 f = fopen (fname, "w");
1448 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1449 fprintf (f, "%c %d %ld\n", val, seqno, p);
1454 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1456 char state_fname[256];
1461 yaz_log(log_level, "zebra_get_state ");
1462 zebra_clearError(zh);
1463 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1464 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1465 f = fopen (fname, "r");
1471 fscanf (f, "%c %d", val, seqno);
1477 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1479 return zebra_begin_trans(zh, 0);
1482 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1484 return zebra_end_trans(zh);
1487 static void read_res_for_transaction(ZebraHandle zh)
1489 const char *group = res_get(zh->res, "group");
1491 /* FIXME - do we still use groups ?? */
1493 zh->m_group = group;
1494 v = res_get_prefix(zh->res, "followLinks", group, "1");
1495 zh->m_follow_links = atoi(v);
1497 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1498 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1500 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1501 zh->m_store_keys = atoi(v);
1503 v = res_get_prefix(zh->res, "storeData", group, "1");
1504 zh->m_store_data = atoi(v);
1506 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1507 zh->m_explain_database = atoi(v);
1509 v = res_get_prefix(zh->res, "openRW", group, "1");
1510 zh->m_flag_rw = atoi(v);
1512 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1513 zh->m_file_verbose_limit = atoi(v);
1516 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1519 zebra_select_default_database(zh);
1522 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1523 "zebra_begin_trans: no database selected");
1527 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1531 if (rw && !strchr(zh->user_perm, 'w'))
1535 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1547 const char *rval = 0;
1552 read_res_for_transaction(zh);
1555 if (zh->trans_no != 1)
1557 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1558 "zebra_begin_trans: no write trans within read");
1563 resultSetInvalidate (zh);
1564 zebra_register_close (zh->service, zh->reg);
1566 zh->trans_w_no = zh->trans_no;
1568 zebra_clearError(zh);
1570 zh->records_inserted = 0;
1571 zh->records_updated = 0;
1572 zh->records_deleted = 0;
1573 zh->records_processed = 0;
1575 #if HAVE_SYS_TIMES_H
1579 if (zh->shadow_enable)
1580 rval = res_get (zh->res, "shadow");
1582 for (pass = 0; pass < 2; pass++)
1586 zebra_lock_r (zh->lock_normal);
1587 zebra_lock_w (zh->lock_shadow);
1591 zebra_lock_w (zh->lock_normal);
1592 zebra_lock_w (zh->lock_shadow);
1595 zebra_get_state (zh, &val, &seqno);
1598 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1599 zebra_unlock (zh->lock_shadow);
1600 zebra_unlock (zh->lock_normal);
1604 else if (val == 'd')
1608 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1610 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1611 bf_commitClean (bfs, rval);
1616 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1623 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1627 zebra_set_state (zh, 'd', seqno);
1629 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1630 1, rval ? 1 : 0, zh->res,
1633 zh->reg->seqno = seqno;
1636 zebra_set_state (zh, 'o', seqno);
1638 zebra_unlock (zh->lock_shadow);
1639 zebra_unlock (zh->lock_normal);
1644 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1645 "zebra_begin_trans: cannot open register");
1646 yaz_log(YLOG_FATAL, "%s", zh->errString);
1658 if (zh->trans_no != 1)
1660 zebra_flush_reg (zh);
1663 zebra_clearError(zh);
1664 #if HAVE_SYS_TIMES_H
1670 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1673 if (!zh->lock_normal || !zh->lock_shadow)
1676 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1679 zebra_get_state (zh, &val, &seqno);
1685 else if (seqno != zh->reg->seqno)
1687 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1688 seqno, zh->reg->seqno);
1691 else if (zh->reg->last_val != val)
1693 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1694 val, zh->reg->last_val);
1701 zebra_lock_r (zh->lock_shadow);
1703 zebra_lock_r (zh->lock_normal);
1707 resultSetInvalidate (zh);
1708 zebra_register_close (zh->service, zh->reg);
1710 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1711 0, val == 'c' ? 1 : 0,
1712 zh->res, zh->path_reg);
1715 zebra_unlock (zh->lock_normal);
1716 zebra_unlock (zh->lock_shadow);
1718 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1721 zh->reg->last_val = val;
1722 zh->reg->seqno = seqno;
1724 read_res_for_transaction(zh);
1728 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1730 ZebraTransactionStatus dummy;
1732 yaz_log(log_level, "zebra_end_trans");
1733 return zebra_end_transaction(zh, &dummy);
1736 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1744 yaz_log(log_level, "zebra_end_transaction");
1746 status->processed = 0;
1747 status->inserted = 0;
1748 status->updated = 0;
1749 status->deleted = 0;
1753 if (!zh->res || !zh->reg)
1755 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1756 "zebra_end_trans: no open transaction");
1759 if (zh->trans_no != zh->trans_w_no)
1762 if (zh->trans_no != 0)
1765 /* release read lock */
1767 zebra_unlock (zh->lock_normal);
1768 zebra_unlock (zh->lock_shadow);
1771 { /* release write lock */
1775 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1776 rval = res_get (zh->res, "shadow");
1778 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1780 zebra_flush_reg (zh);
1782 resultSetInvalidate (zh);
1784 zebra_register_close (zh->service, zh->reg);
1787 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1788 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1789 zh->records_processed, zh->records_inserted,
1790 zh->records_updated, zh->records_deleted);
1792 status->processed = (int) zh->records_processed;
1793 status->inserted = (int) zh->records_inserted;
1794 status->updated = (int) zh->records_updated;
1795 status->deleted = (int) zh->records_deleted;
1797 zebra_get_state (zh, &val, &seqno);
1800 BFiles bfs = bfs_create (rval, zh->path_reg);
1801 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1802 bf_commitClean (bfs, rval);
1807 zebra_set_state (zh, 'o', seqno);
1809 zebra_unlock (zh->lock_shadow);
1810 zebra_unlock (zh->lock_normal);
1813 #if HAVE_SYS_TIMES_H
1815 yaz_log (log_level, "user/system: %ld/%ld",
1816 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1817 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1819 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1820 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1825 int zebra_repository_update (ZebraHandle zh, const char *path)
1829 zebra_clearError(zh);
1830 yaz_log (log_level, "updating %s", path);
1831 repositoryUpdate (zh, path);
1835 int zebra_repository_delete (ZebraHandle zh, const char *path)
1839 zebra_clearError(zh);
1840 yaz_log (log_level, "deleting %s", path);
1841 repositoryDelete (zh, path);
1845 int zebra_repository_show (ZebraHandle zh, const char *path)
1849 yaz_log(log_level, "zebra_repository_show");
1850 zebra_clearError(zh);
1851 repositoryShow (zh, path);
1855 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1862 zebra_clearError(zh);
1864 zebra_select_default_database(zh);
1867 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1870 rval = res_get (zh->res, "shadow");
1873 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1877 zebra_lock_w (zh->lock_normal);
1878 zebra_lock_r (zh->lock_shadow);
1880 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1882 zebra_get_state (zh, &val, &seqno);
1885 bf_cache (bfs, rval);
1886 if (bf_commitExists (bfs))
1889 zebra_set_state (zh, 'd', seqno);
1892 zebra_set_state (zh, 'c', seqno);
1894 yaz_log (YLOG_DEBUG, "commit start");
1895 bf_commitExec (bfs);
1900 yaz_log (YLOG_DEBUG, "commit clean");
1901 bf_commitClean (bfs, rval);
1903 zebra_set_state (zh, 'o', seqno);
1907 yaz_log (log_level, "nothing to commit");
1911 zebra_unlock (zh->lock_shadow);
1912 zebra_unlock (zh->lock_normal);
1916 ZEBRA_RES zebra_clean(ZebraHandle zh)
1919 yaz_log(log_level, "zebra_clean");
1920 return zebra_commit_ex(zh, 1);
1923 ZEBRA_RES zebra_commit(ZebraHandle zh)
1926 yaz_log(log_level, "zebra_commit");
1927 return zebra_commit_ex(zh, 0);
1930 ZEBRA_RES zebra_init(ZebraHandle zh)
1935 yaz_log(log_level, "zebra_init");
1936 zebra_clearError(zh);
1938 zebra_select_default_database(zh);
1941 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1942 "cannot select default database");
1945 rval = res_get (zh->res, "shadow");
1947 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1950 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1954 bf_cache (bfs, rval);
1958 zebra_set_state (zh, 'o', 0);
1962 ZEBRA_RES zebra_compact(ZebraHandle zh)
1966 yaz_log(log_level, "zebra_compact");
1967 zebra_clearError(zh);
1970 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1973 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1979 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1982 yaz_log(log_level, "zebra_result");
1983 *code = zh->errCode;
1984 *addinfo = zh->errString;
1987 void zebra_shadow_enable(ZebraHandle zh, int value)
1990 yaz_log(log_level, "zebra_shadow_enable");
1991 zebra_clearError(zh);
1992 zh->shadow_enable = value;
1995 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1999 yaz_log(log_level, "zebra_octet_term_encoding");
2000 zebra_clearError(zh);
2002 if (zh->iconv_to_utf8 != 0)
2003 yaz_iconv_close(zh->iconv_to_utf8);
2004 if (zh->iconv_from_utf8 != 0)
2005 yaz_iconv_close(zh->iconv_from_utf8);
2008 yaz_iconv_open ("UTF-8", encoding);
2009 if (zh->iconv_to_utf8 == 0)
2010 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2011 zh->iconv_from_utf8 =
2012 yaz_iconv_open (encoding, "UTF-8");
2013 if (zh->iconv_to_utf8 == 0)
2014 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2019 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2022 yaz_log(log_level, "zebra_record_encoding");
2023 zebra_clearError(zh);
2024 xfree(zh->record_encoding);
2025 zh->record_encoding = 0;
2027 zh->record_encoding = xstrdup (encoding);
2031 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2036 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2037 zebra_clearError(zh);
2038 res_set(zh->res, name, value);
2041 const char *zebra_get_resource(ZebraHandle zh,
2042 const char *name, const char *defaultvalue)
2047 v = res_get_def (zh->res, name, (char *)defaultvalue);
2048 zebra_clearError(zh);
2049 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2053 /* moved from zebra_api_ext.c by pop */
2054 /* FIXME: Should this really be public??? -Heikki */
2056 int zebra_trans_no (ZebraHandle zh)
2059 yaz_log(log_level, "zebra_trans_no");
2060 return zh->trans_no;
2063 int zebra_get_shadow_enable (ZebraHandle zh)
2066 yaz_log(log_level, "zebra_get_shadow_enable");
2067 return zh->shadow_enable;
2070 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2073 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2074 zh->shadow_enable = value;
2077 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2078 so that it's identicical to the original api_records_retrieve */
2079 void api_records_retrieve (ZebraHandle zh, ODR stream,
2080 const char *setname, Z_RecordComposition *comp,
2081 oid_value input_format, int num_recs,
2082 ZebraRetrievalRecord *recs)
2084 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2088 /* ---------------------------------------------------------------------------
2089 Record insert(=update), delete
2091 If sysno is provided, then it's used to identify the record.
2092 If not, and match_criteria is provided, then sysno is guessed
2093 If not, and a record is provided, then sysno is got from there
2094 NOTE: Now returns 0 at success and updates sysno, which is an int*
2098 int zebra_add_record(ZebraHandle zh,
2099 const char *buf, int buf_size)
2102 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2105 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2106 const char *recordType,
2107 SYSNO *sysno, const char *match,
2109 const char *buf, int buf_size, int force_update)
2115 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2118 buf_size = strlen(buf);
2120 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2122 res = buffer_extract_record (zh, buf, buf_size,
2123 0, /* delete_flag */
2129 0); /* allow_update */
2130 zebra_end_trans(zh);
2134 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2135 const char *recordType,
2136 SYSNO* sysno, const char *match,
2138 const char *buf, int buf_size,
2146 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2148 if (buf_size < 1) buf_size = strlen(buf);
2150 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2152 res = buffer_extract_record (zh, buf, buf_size,
2153 0, /* delete_flag */
2159 1); /* allow_update */
2160 zebra_end_trans(zh);
2164 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2165 const char *recordType,
2166 SYSNO *sysno, const char *match,
2168 const char *buf, int buf_size,
2175 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2177 if (buf_size < 1) buf_size = strlen(buf);
2179 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2181 res = buffer_extract_record (zh, buf, buf_size,
2182 1, /* delete_flag */
2188 1); /* allow_update */
2189 zebra_end_trans(zh);
2193 /* ---------------------------------------------------------------------------
2197 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2198 const char *setname, zint *hits)
2201 ZEBRA_RES res = ZEBRA_OK;
2203 ODR odr = odr_createmem(ODR_ENCODE);
2208 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2210 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2214 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2215 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2219 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2223 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2231 /* ---------------------------------------------------------------------------
2232 Sort - a simplified interface, with optional read locks.
2234 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2235 const char *sort_spec,
2236 const char *output_setname,
2237 const char **input_setnames)
2239 int num_input_setnames = 0;
2240 int sort_status = 0;
2241 Z_SortKeySpecList *sort_sequence;
2245 assert(output_setname);
2246 assert(input_setnames);
2247 sort_sequence = yaz_sort_spec (stream, sort_spec);
2248 yaz_log(log_level, "sort (FIXME) ");
2251 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2252 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2256 /* we can do this, since the perl typemap code for char** will
2257 put a NULL at the end of list */
2258 while (input_setnames[num_input_setnames]) num_input_setnames++;
2260 if (zebra_begin_read (zh))
2263 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2264 output_setname, sort_sequence, &sort_status);
2270 /* ---------------------------------------------------------------------------
2271 Get BFS for Zebra system (to make alternative storage methods)
2273 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2276 return zh->reg->bfs;
2281 /* ---------------------------------------------------------------------------
2282 Set limit for search/scan
2284 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2287 zebra_limit_destroy(zh->m_limit);
2288 zh->m_limit = zebra_limit_create(complement_flag, ids);
2293 Set Error code + addinfo
2295 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2298 nmem_reset(zh->nmem_error);
2299 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2302 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2305 sprintf(vstr, ZINT_FORMAT, i);
2308 nmem_reset(zh->nmem_error);
2309 zh->errString = nmem_strdup(zh->nmem_error, vstr);