1 /* $Id: zebraapi.c,v 1.181 2005-08-17 21:29:44 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 Res 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)
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 */
116 zh->reg_name = xstrdup ("");
118 zh->num_basenames = 0;
121 zh->approx_limit = DEFAULT_APPROX_LIMIT;
128 zh->shadow_enable = 1;
130 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
133 yaz_iconv_open ("UTF-8", default_encoding);
134 if (zh->iconv_to_utf8 == 0)
135 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
137 zh->iconv_from_utf8 =
138 yaz_iconv_open (default_encoding, "UTF-8");
139 if (zh->iconv_to_utf8 == 0)
140 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
143 zh->record_encoding = 0;
145 zebra_mutex_cond_lock (&zs->session_lock);
147 zh->next = zs->sessions;
150 zebra_mutex_cond_unlock (&zs->session_lock);
152 zh->store_data_buf = 0;
154 zh->m_limit = zebra_limit_create(1, 0);
156 zh->nmem_error = nmem_create();
161 ZebraService zebra_start (const char *configName)
163 return zebra_start_res(configName, 0, 0);
166 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
170 if (!log_level_initialized)
172 log_level = yaz_log_module_level("zebraapi");
173 log_level_initialized = 1;
176 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
177 configName ? configName : "");
179 if ((res = res_open (configName, def_res, over_res)))
181 const char *passwd_plain = 0;
182 const char *passwd_encrypt = 0;
183 ZebraService zh = xmalloc(sizeof(*zh));
185 zh->global_res = res;
190 zebra_mutex_cond_init (&zh->session_lock);
191 passwd_plain = res_get (zh->global_res, "passwd");
192 passwd_encrypt = res_get (zh->global_res, "passwd.c");
194 if (!passwd_plain && !passwd_encrypt)
195 zh->passwd_db = NULL;
198 zh->passwd_db = passwd_db_open();
200 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
204 passwd_db_file_plain(zh->passwd_db, passwd_plain);
206 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
209 zh->path_root = res_get (zh->global_res, "root");
210 zh->nmem = nmem_create();
211 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
217 void zebra_filter_info(ZebraService zs, void *cd,
218 void (*cb)(void *cd, const char *name))
222 recTypeClass_info(zs->record_classes, cd, cb);
225 void zebra_pidfname(ZebraService zs, char *path)
228 zebra_lock_prefix (zs->global_res, path);
229 strcat(path, "zebrasrv.pid");
232 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
233 int compact_flag, Res res)
235 int page_size = 4096;
236 char resource_str[200];
237 sprintf (resource_str, "dict.%.100s.pagesize", name);
241 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
242 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
244 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
248 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
249 int rw, int useshadow, Res res,
250 const char *reg_path)
252 struct zebra_register *reg;
253 int record_compression = REC_COMPRESS_NONE;
254 const char *recordCompression = 0;
255 const char *profilePath;
260 reg = xmalloc(sizeof(*reg));
263 reg->name = xstrdup (name);
270 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
271 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
273 reg->dh = data1_createx (DATA1_FLAG_XML);
279 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
282 data1_destroy(reg->dh);
288 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
290 bfs_destroy(reg->bfs);
291 data1_destroy(reg->dh);
297 getcwd(cwd, sizeof(cwd)-1);
298 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
299 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
301 data1_set_tabpath (reg->dh, profilePath);
302 data1_set_tabroot (reg->dh, reg_path);
303 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
305 reg->zebra_maps = zebra_maps_open (res, reg_path);
306 reg->rank_classes = NULL;
310 reg->keys.buf_max = 0;
312 reg->keys.codec_handle = iscz1_start();
314 reg->sortKeys.buf = 0;
315 reg->sortKeys.buf_max = 0;
317 reg->sortKeys.codec_handle = iscz1_start();
329 reg->key_file_no = 0;
332 zebraRankInstall (reg, rank1_class);
333 zebraRankInstall (reg, rankzv_class);
335 recordCompression = res_get_def (res, "recordCompression", "none");
336 if (!strcmp (recordCompression, "none"))
337 record_compression = REC_COMPRESS_NONE;
338 if (!strcmp (recordCompression, "bzip2"))
339 record_compression = REC_COMPRESS_BZIP2;
341 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
343 yaz_log (YLOG_WARN, "rec_open failed");
348 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
350 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
352 yaz_log (YLOG_WARN, "dict_open failed");
355 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
357 yaz_log (YLOG_WARN, "sortIdx_open failed");
360 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
362 struct ISAMS_M_s isams_m;
363 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
364 key_isams_m(res, &isams_m))))
366 yaz_log (YLOG_WARN, "isams_open failed");
370 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
372 struct ISAMC_M_s isamc_m;
373 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
374 rw, key_isamc_m(res, &isamc_m))))
376 yaz_log (YLOG_WARN, "isamc_open failed");
380 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
382 struct ISAMC_M_s isamc_m;
384 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
385 rw, key_isamc_m(res, &isamc_m), 0)))
387 yaz_log (YLOG_WARN, "isamb_open failed");
391 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
393 struct ISAMC_M_s isamc_m;
395 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
396 rw, key_isamc_m(res, &isamc_m), 1)))
398 yaz_log (YLOG_WARN, "isamb_open failed");
402 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
404 struct ISAMC_M_s isamc_m;
406 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
407 rw, key_isamc_m(res, &isamc_m), -1)))
409 yaz_log (YLOG_WARN, "isamb_open failed");
413 reg->zei = zebraExplain_open (reg->records, reg->dh,
418 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
422 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
426 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
429 yaz_log(log_level, "zebra_admin_shutdown");
430 zebra_clearError(zh);
432 zebra_mutex_cond_lock (&zh->service->session_lock);
433 zh->service->stop_flag = 1;
434 zebra_mutex_cond_unlock (&zh->service->session_lock);
438 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
442 yaz_log(log_level, "zebra_admin_start");
443 zebra_clearError(zh);
445 zebra_mutex_cond_lock (&zs->session_lock);
446 zebra_mutex_cond_unlock (&zs->session_lock);
450 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
454 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
459 zebraExplain_close (reg->zei);
460 dict_close (reg->dict);
462 dict_close (reg->matchDict);
463 sortIdx_close (reg->sortIdx);
465 isams_close (reg->isams);
467 isamc_close (reg->isamc);
469 isamb_close (reg->isamb);
470 rec_close (®->records);
473 recTypes_destroy (reg->recTypes);
474 zebra_maps_close (reg->zebra_maps);
475 zebraRankDestroy (reg);
476 bfs_destroy (reg->bfs);
477 data1_destroy (reg->dh);
479 xfree(reg->sortKeys.buf);
480 xfree(reg->keys.buf);
481 if (reg->keys.codec_handle)
482 iscz1_stop(reg->keys.codec_handle);
484 if (reg->sortKeys.codec_handle)
485 iscz1_stop(reg->sortKeys.codec_handle);
492 ZEBRA_RES zebra_stop(ZebraService zs)
496 yaz_log (log_level, "zebra_stop");
500 zebra_close (zs->sessions);
503 zebra_mutex_cond_destroy (&zs->session_lock);
506 passwd_db_close (zs->passwd_db);
508 recTypeClass_destroy(zs->record_classes);
509 nmem_destroy(zs->nmem);
510 res_close (zs->global_res);
515 ZEBRA_RES zebra_close (ZebraHandle zh)
518 struct zebra_session **sp;
521 yaz_log(log_level, "zebra_close");
528 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
529 resultSetDestroy (zh, -1, 0, 0);
532 zebra_register_close (zh->service, zh->reg);
533 zebra_close_res (zh);
535 xfree(zh->record_encoding);
537 for (i = 0; i < zh->num_basenames; i++)
538 xfree(zh->basenames[i]);
539 xfree(zh->basenames);
541 if (zh->iconv_to_utf8 != 0)
542 yaz_iconv_close (zh->iconv_to_utf8);
543 if (zh->iconv_from_utf8 != 0)
544 yaz_iconv_close (zh->iconv_from_utf8);
546 zebra_mutex_cond_lock (&zs->session_lock);
547 zebra_lock_destroy (zh->lock_normal);
548 zebra_lock_destroy (zh->lock_shadow);
560 zebra_mutex_cond_unlock (&zs->session_lock);
562 xfree(zh->user_perm);
563 zh->service = 0; /* more likely to trigger an assert */
565 zebra_limit_destroy(zh->m_limit);
567 nmem_destroy(zh->nmem_error);
574 struct map_baseinfo {
580 char **new_basenames;
584 static Res zebra_open_res (ZebraHandle zh)
593 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
594 res = res_open (fname, zh->service->global_res, 0);
596 res = zh->service->global_res;
598 else if (*zh->reg_name == 0)
600 res = zh->service->global_res;
604 yaz_log (YLOG_WARN, "no register root specified");
605 return 0; /* no path for register - fail! */
610 static void zebra_close_res (ZebraHandle zh)
614 if (zh->res != zh->service->global_res)
619 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
623 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
627 assert (zh->reg == 0);
628 assert (*zh->reg_name == 0);
634 resultSetInvalidate (zh);
635 zebra_register_close (zh->service, zh->reg);
641 zh->reg_name = xstrdup (new_reg);
645 if (zh->service->path_root)
647 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
648 strlen(zh->reg_name) + 3);
649 strcpy (zh->path_reg, zh->service->path_root);
652 strcat (zh->path_reg, "/");
653 strcat (zh->path_reg, zh->reg_name);
656 zh->res = zebra_open_res (zh);
659 zebra_lock_destroy (zh->lock_normal);
663 zebra_lock_destroy (zh->lock_shadow);
669 const char *lock_area =res_get (zh->res, "lockDir");
671 if (!lock_area && zh->path_reg)
672 res_set (zh->res, "lockDir", zh->path_reg);
673 sprintf (fname, "norm.%s.LCK", zh->reg_name);
675 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
677 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
679 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
681 if (!zh->lock_normal || !zh->lock_shadow)
685 zebra_lock_destroy(zh->lock_normal);
690 zebra_lock_destroy(zh->lock_shadow);
699 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
700 zebra_set_approx_limit(zh, approx);
704 void map_basenames_func (void *vp, const char *name, const char *value)
706 struct map_baseinfo *p = (struct map_baseinfo *) vp;
708 char fromdb[128], todb[8][128];
715 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
716 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
717 todb[5], todb[6], todb[7]);
721 for (i = 0; i<p->num_bases; i++)
722 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
725 for (i = 0; i < no; i++)
727 if (p->new_num_bases == p->new_num_max)
729 p->new_basenames[(p->new_num_bases)++] =
730 nmem_strdup (p->mem, todb[i]);
736 int zebra_select_default_database(ZebraHandle zh)
740 /* no database has been selected - so we select based on
741 resource setting (including group)
743 const char *group = res_get(zh->service->global_res, "group");
744 const char *v = res_get_prefix(zh->service->global_res,
745 "database", group, "Default");
746 return zebra_select_database(zh, v);
751 void map_basenames (ZebraHandle zh, ODR stream,
752 int *num_bases, char ***basenames)
754 struct map_baseinfo info;
755 struct map_baseinfo *p = &info;
758 yaz_log(log_level, "map_basenames ");
763 info.num_bases = *num_bases;
764 info.basenames = *basenames;
765 info.new_num_max = 128;
766 info.new_num_bases = 0;
767 info.new_basenames = (char **)
768 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
769 info.mem = stream->mem;
771 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
773 for (i = 0; i<p->num_bases; i++)
774 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
776 p->new_basenames[(p->new_num_bases)++] =
777 nmem_strdup (p->mem, p->basenames[i]);
779 *num_bases = info.new_num_bases;
780 *basenames = info.new_basenames;
781 for (i = 0; i<*num_bases; i++)
782 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
785 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
788 yaz_log(log_level, "zebra_select_database %s",basename);
790 return zebra_select_databases (zh, 1, &basename);
793 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
794 const char **basenames)
803 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
804 num_bases,basenames[0]);
809 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
812 for (i = 0; i < zh->num_basenames; i++)
813 xfree(zh->basenames[i]);
814 xfree(zh->basenames);
816 zh->num_basenames = num_bases;
817 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
818 for (i = 0; i < zh->num_basenames; i++)
819 zh->basenames[i] = xstrdup (basenames[i]);
821 cp = strrchr(basenames[0], '/');
824 len = cp - basenames[0];
825 new_reg = xmalloc(len + 1);
826 memcpy (new_reg, basenames[0], len);
830 new_reg = xstrdup ("");
831 for (i = 1; i<num_bases; i++)
835 cp1 = strrchr (basenames[i], '/');
840 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
843 if (len != cp1 - basenames[i] ||
844 memcmp (basenames[i], new_reg, len))
846 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
854 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
859 zebra_select_register (zh, new_reg);
863 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
866 if (!zh->lock_normal || !zh->lock_shadow)
868 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
874 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
876 if (approx_limit == 0)
877 approx_limit = DEFAULT_APPROX_LIMIT;
878 zh->approx_limit = approx_limit;
882 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
883 const char *setname, zint *hits)
891 yaz_log(log_level, "zebra_search_rpn");
892 zebra_clearError(zh);
896 if (zebra_begin_read(zh) == ZEBRA_FAIL)
899 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
900 zh->num_basenames, zh->basenames, setname);
906 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
908 Z_RecordComposition *comp,
909 oid_value input_format, int num_recs,
910 ZebraRetrievalRecord *recs)
912 ZebraMetaRecord *poset;
914 ZEBRA_RES ret = ZEBRA_OK;
922 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
924 zebra_clearError(zh);
927 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
932 if (zebra_begin_read (zh) == ZEBRA_FAIL)
935 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
936 for (i = 0; i<num_recs; i++)
937 pos_array[i] = recs[i].position;
938 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
941 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
942 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
948 for (i = 0; i<num_recs; i++)
953 recs[i].format = VAL_SUTRS;
954 recs[i].len = strlen(poset[i].term);
955 recs[i].buf = poset[i].term;
956 recs[i].base = poset[i].db;
958 else if (poset[i].sysno)
962 zebra_snippets *hit_snippet = zebra_snippets_create();
964 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
968 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
970 stream, input_format, comp,
971 &recs[i].format, &buf, &len,
972 &recs[i].base, &recs[i].errString);
977 recs[i].buf = (char*) odr_malloc(stream, len);
978 memcpy(recs[i].buf, buf, len);
982 recs[i].score = poset[i].score;
983 recs[i].sysno = poset[i].sysno;
984 zebra_snippets_destroy(hit_snippet);
988 /* only need to set it once */
989 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
991 zebra_setError_zint(zh,
992 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
997 recs[i].buf = 0; /* no record and no error issued */
1000 recs[i].format = VAL_NONE;
1004 zebra_meta_records_destroy(zh, poset, num_recs);
1006 zebra_end_read (zh);
1011 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1013 int *num_entries, ZebraScanEntry **entries,
1015 const char *setname)
1017 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1018 Z_AttributesPlusTerm *zapt;
1022 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1025 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1028 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1029 position, num_entries, entries, is_partial,
1031 yaz_pqf_destroy (pqf_parser);
1035 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1036 oid_value attributeset,
1038 int *num_entries, ZebraScanEntry **entries,
1040 const char *setname)
1043 RSET limit_rset = 0;
1048 assert(num_entries);
1051 yaz_log(log_level, "zebra_scan");
1052 zebra_clearError(zh);
1053 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1061 limit_rset = resultSetRef(zh, setname);
1065 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1067 zebra_end_read (zh);
1071 res = rpn_scan (zh, stream, zapt, attributeset,
1072 zh->num_basenames, zh->basenames, position,
1073 num_entries, entries, is_partial, limit_rset, 0);
1074 zebra_end_read (zh);
1078 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1079 int num_input_setnames, const char **input_setnames,
1080 const char *output_setname,
1081 Z_SortKeySpecList *sort_sequence,
1087 assert(num_input_setnames>0);
1088 assert(input_setnames);
1089 assert(sort_sequence);
1090 assert(sort_status);
1091 yaz_log(log_level, "zebra_sort");
1092 zebra_clearError(zh);
1093 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1095 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1096 output_setname, sort_sequence, sort_status);
1101 int zebra_deleteResultSet(ZebraHandle zh, int function,
1102 int num_setnames, char **setnames,
1108 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1109 zebra_clearError(zh);;
1110 if (zebra_begin_read(zh))
1111 return Z_DeleteStatus_systemProblemAtTarget;
1114 case Z_DeleteResultSetRequest_list:
1115 assert(num_setnames>0);
1117 resultSetDestroy (zh, num_setnames, setnames, statuses);
1119 case Z_DeleteResultSetRequest_all:
1120 resultSetDestroy (zh, -1, 0, statuses);
1123 zebra_end_read (zh);
1124 status = Z_DeleteStatus_success;
1125 for (i = 0; i<num_setnames; i++)
1126 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1127 status = statuses[i];
1131 int zebra_errCode (ZebraHandle zh)
1135 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1138 yaz_log(log_level, "zebra_errCode: o");
1142 const char *zebra_errString (ZebraHandle zh)
1146 e= diagbib1_str (zh->errCode);
1147 yaz_log(log_level, "zebra_errString: %s",e);
1151 char *zebra_errAdd (ZebraHandle zh)
1156 yaz_log(log_level, "zebra_errAdd: %s",a);
1160 void zebra_clearError(ZebraHandle zh)
1169 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1176 zebra_clearError(zh);
1179 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1180 p = res_get(zs->global_res, u);
1181 xfree(zh->user_perm);
1182 zh->user_perm = xstrdup(p ? p : "r");
1184 /* users that don't require a password .. */
1185 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1188 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1193 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1194 const char *record_type)
1197 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1198 database, record_type);
1199 zebra_clearError(zh);
1200 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1202 return zebra_begin_trans(zh, 1);
1205 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1208 yaz_log(log_level, "zebra_admin_import_end");
1209 zebra_clearError(zh);
1210 return zebra_end_trans(zh);
1213 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1215 ZEBRA_RES res = ZEBRA_OK;
1219 yaz_log(log_level, "zebra_admin_import_segment");
1220 zebra_clearError(zh);
1221 for (i = 0; i<segment->num_segmentRecords; i++)
1223 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1225 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1227 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1228 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1230 Odr_oct *oct = fragment->u.notExternallyTagged;
1233 if ( zebra_update_record(zh,
1234 0, /* record Type */
1247 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1248 const char *rec_buf,
1250 const char *recid_buf, size_t recid_len,
1252 /* 1 = insert. Fail it already exists */
1253 /* 2 = replace. Fail it does not exist */
1254 /* 3 = delete. Fail if does not exist */
1255 /* 4 = update. Insert/replace */
1262 assert(action>0 && action <=4);
1265 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1266 zebra_clearError(zh);
1268 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1271 memcpy (recid_z, recid_buf, recid_len);
1272 recid_z[recid_len] = 0;
1274 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1277 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1280 if (action == 1) /* fail if insert */
1282 zebra_end_trans(zh);
1286 memcpy (&sysno, rinfo+1, sizeof(sysno));
1290 if (action == 2 || action == 3) /* fail if delete or update */
1292 zebra_end_trans(zh);
1295 action = 1; /* make it an insert (if it's an update).. */
1297 res = buffer_extract_record (zh, rec_buf, rec_len,
1298 action == 3 ? 1 : 0 /* delete flag */,
1304 0, /* force update */
1305 1 /* allow update */
1309 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1311 else if (action == 3)
1313 dict_delete (zh->reg->matchDict, recid_z);
1315 zebra_end_trans(zh);
1319 int delete_w_handle(const char *info, void *handle)
1321 ZebraHandle zh = (ZebraHandle) handle;
1325 if (*info == sizeof(pos))
1327 memcpy (&pos, info+1, sizeof(pos));
1328 isamb_unlink(zh->reg->isamb, pos);
1333 static int delete_SU_handle(void *handle, int ord)
1335 ZebraHandle zh = (ZebraHandle) handle;
1339 ord_len = key_SU_encode (ord, ord_buf);
1340 ord_buf[ord_len] = '\0';
1342 assert (zh->reg->isamb);
1343 dict_delete_subtree(zh->reg->dict, ord_buf,
1344 zh, delete_w_handle);
1348 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1350 ZEBRA_RES ret = ZEBRA_OK;
1352 yaz_log(log_level, "zebra_drop_database %s", db);
1353 zebra_clearError(zh);
1355 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1357 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1361 zebraExplain_curDatabase (zh->reg->zei, db);
1363 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1364 zebraExplain_removeDatabase(zh->reg->zei, zh);
1368 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1371 zebra_end_trans (zh);
1375 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1378 yaz_log(log_level, "zebra_create_database %s", db);
1380 zebra_clearError(zh);
1382 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1384 if (zebra_begin_trans (zh, 1))
1387 /* announce database */
1388 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1389 /* explainDatabase */))
1391 zebra_end_trans (zh);
1392 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1395 return zebra_end_trans (zh);
1398 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1399 const char *input_str, int input_len,
1400 char *output_str, int output_len)
1406 yaz_log(log_level, "zebra_string_norm ");
1407 zebra_clearError(zh);
1408 if (!zh->reg->zebra_maps)
1410 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1411 input_str, input_len);
1414 if (wrbuf_len(wrbuf) >= output_len)
1416 if (wrbuf_len(wrbuf))
1417 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1418 output_str[wrbuf_len(wrbuf)] = '\0';
1419 return wrbuf_len(wrbuf);
1422 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1424 char state_fname[256];
1429 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1430 zebra_clearError(zh);
1432 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1433 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1434 f = fopen (fname, "w");
1436 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1437 fprintf (f, "%c %d %ld\n", val, seqno, p);
1442 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1444 char state_fname[256];
1449 yaz_log(log_level, "zebra_get_state ");
1450 zebra_clearError(zh);
1451 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1452 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1453 f = fopen (fname, "r");
1459 fscanf (f, "%c %d", val, seqno);
1465 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1467 return zebra_begin_trans(zh, 0);
1470 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1472 return zebra_end_trans(zh);
1475 static void read_res_for_transaction(ZebraHandle zh)
1477 const char *group = res_get(zh->res, "group");
1479 /* FIXME - do we still use groups ?? */
1481 zh->m_group = group;
1482 v = res_get_prefix(zh->res, "followLinks", group, "1");
1483 zh->m_follow_links = atoi(v);
1485 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1486 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1488 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1489 zh->m_store_keys = atoi(v);
1491 v = res_get_prefix(zh->res, "storeData", group, "1");
1492 zh->m_store_data = atoi(v);
1494 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1495 zh->m_explain_database = atoi(v);
1497 v = res_get_prefix(zh->res, "openRW", group, "1");
1498 zh->m_flag_rw = atoi(v);
1500 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1501 zh->m_file_verbose_limit = atoi(v);
1504 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1507 zebra_select_default_database(zh);
1510 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1511 "zebra_begin_trans: no database selected");
1515 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1519 if (rw && !strchr(zh->user_perm, 'w'))
1523 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1535 const char *rval = 0;
1540 read_res_for_transaction(zh);
1543 if (zh->trans_no != 1)
1545 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1546 "zebra_begin_trans: no write trans within read");
1551 resultSetInvalidate (zh);
1552 zebra_register_close (zh->service, zh->reg);
1554 zh->trans_w_no = zh->trans_no;
1556 zebra_clearError(zh);
1558 zh->records_inserted = 0;
1559 zh->records_updated = 0;
1560 zh->records_deleted = 0;
1561 zh->records_processed = 0;
1563 #if HAVE_SYS_TIMES_H
1567 if (zh->shadow_enable)
1568 rval = res_get (zh->res, "shadow");
1570 for (pass = 0; pass < 2; pass++)
1574 zebra_lock_r (zh->lock_normal);
1575 zebra_lock_w (zh->lock_shadow);
1579 zebra_lock_w (zh->lock_normal);
1580 zebra_lock_w (zh->lock_shadow);
1583 zebra_get_state (zh, &val, &seqno);
1586 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1587 zebra_unlock (zh->lock_shadow);
1588 zebra_unlock (zh->lock_normal);
1592 else if (val == 'd')
1596 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1598 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1599 bf_commitClean (bfs, rval);
1604 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1611 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1615 zebra_set_state (zh, 'd', seqno);
1617 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1618 1, rval ? 1 : 0, zh->res,
1621 zh->reg->seqno = seqno;
1624 zebra_set_state (zh, 'o', seqno);
1626 zebra_unlock (zh->lock_shadow);
1627 zebra_unlock (zh->lock_normal);
1632 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1633 "zebra_begin_trans: cannot open register");
1634 yaz_log(YLOG_FATAL, "%s", zh->errString);
1646 if (zh->trans_no != 1)
1648 zebra_flush_reg (zh);
1651 zebra_clearError(zh);
1652 #if HAVE_SYS_TIMES_H
1658 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1661 if (!zh->lock_normal || !zh->lock_shadow)
1664 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1667 zebra_get_state (zh, &val, &seqno);
1673 else if (seqno != zh->reg->seqno)
1675 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1676 seqno, zh->reg->seqno);
1679 else if (zh->reg->last_val != val)
1681 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1682 val, zh->reg->last_val);
1689 zebra_lock_r (zh->lock_shadow);
1691 zebra_lock_r (zh->lock_normal);
1695 resultSetInvalidate (zh);
1696 zebra_register_close (zh->service, zh->reg);
1698 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1699 0, val == 'c' ? 1 : 0,
1700 zh->res, zh->path_reg);
1703 zebra_unlock (zh->lock_normal);
1704 zebra_unlock (zh->lock_shadow);
1706 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1709 zh->reg->last_val = val;
1710 zh->reg->seqno = seqno;
1712 read_res_for_transaction(zh);
1716 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1718 ZebraTransactionStatus dummy;
1720 yaz_log(log_level, "zebra_end_trans");
1721 return zebra_end_transaction(zh, &dummy);
1724 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1732 yaz_log(log_level, "zebra_end_transaction");
1734 status->processed = 0;
1735 status->inserted = 0;
1736 status->updated = 0;
1737 status->deleted = 0;
1741 if (!zh->res || !zh->reg)
1743 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1744 "zebra_end_trans: no open transaction");
1747 if (zh->trans_no != zh->trans_w_no)
1750 if (zh->trans_no != 0)
1753 /* release read lock */
1755 zebra_unlock (zh->lock_normal);
1756 zebra_unlock (zh->lock_shadow);
1759 { /* release write lock */
1763 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1764 rval = res_get (zh->res, "shadow");
1766 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1768 zebra_flush_reg (zh);
1770 resultSetInvalidate (zh);
1772 zebra_register_close (zh->service, zh->reg);
1775 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1776 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1777 zh->records_processed, zh->records_inserted,
1778 zh->records_updated, zh->records_deleted);
1780 status->processed = (int) zh->records_processed;
1781 status->inserted = (int) zh->records_inserted;
1782 status->updated = (int) zh->records_updated;
1783 status->deleted = (int) zh->records_deleted;
1785 zebra_get_state (zh, &val, &seqno);
1788 BFiles bfs = bfs_create (rval, zh->path_reg);
1789 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1790 bf_commitClean (bfs, rval);
1795 zebra_set_state (zh, 'o', seqno);
1797 zebra_unlock (zh->lock_shadow);
1798 zebra_unlock (zh->lock_normal);
1801 #if HAVE_SYS_TIMES_H
1803 yaz_log (log_level, "user/system: %ld/%ld",
1804 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1805 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1807 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1808 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1813 int zebra_repository_update (ZebraHandle zh, const char *path)
1817 zebra_clearError(zh);
1818 yaz_log (log_level, "updating %s", path);
1819 repositoryUpdate (zh, path);
1823 int zebra_repository_delete (ZebraHandle zh, const char *path)
1827 zebra_clearError(zh);
1828 yaz_log (log_level, "deleting %s", path);
1829 repositoryDelete (zh, path);
1833 int zebra_repository_show (ZebraHandle zh, const char *path)
1837 yaz_log(log_level, "zebra_repository_show");
1838 zebra_clearError(zh);
1839 repositoryShow (zh, path);
1843 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1850 zebra_clearError(zh);
1852 zebra_select_default_database(zh);
1855 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1858 rval = res_get (zh->res, "shadow");
1861 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1865 zebra_lock_w (zh->lock_normal);
1866 zebra_lock_r (zh->lock_shadow);
1868 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1870 zebra_get_state (zh, &val, &seqno);
1873 bf_cache (bfs, rval);
1874 if (bf_commitExists (bfs))
1877 zebra_set_state (zh, 'd', seqno);
1880 zebra_set_state (zh, 'c', seqno);
1882 yaz_log (YLOG_DEBUG, "commit start");
1883 bf_commitExec (bfs);
1888 yaz_log (YLOG_DEBUG, "commit clean");
1889 bf_commitClean (bfs, rval);
1891 zebra_set_state (zh, 'o', seqno);
1895 yaz_log (log_level, "nothing to commit");
1899 zebra_unlock (zh->lock_shadow);
1900 zebra_unlock (zh->lock_normal);
1904 ZEBRA_RES zebra_clean(ZebraHandle zh)
1907 yaz_log(log_level, "zebra_clean");
1908 return zebra_commit_ex(zh, 1);
1911 ZEBRA_RES zebra_commit(ZebraHandle zh)
1914 yaz_log(log_level, "zebra_commit");
1915 return zebra_commit_ex(zh, 0);
1918 ZEBRA_RES zebra_init(ZebraHandle zh)
1923 yaz_log(log_level, "zebra_init");
1924 zebra_clearError(zh);
1926 zebra_select_default_database(zh);
1929 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1930 "cannot select default database");
1933 rval = res_get (zh->res, "shadow");
1935 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1938 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1942 bf_cache (bfs, rval);
1946 zebra_set_state (zh, 'o', 0);
1950 ZEBRA_RES zebra_compact(ZebraHandle zh)
1954 yaz_log(log_level, "zebra_compact");
1955 zebra_clearError(zh);
1958 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1961 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1967 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1970 yaz_log(log_level, "zebra_result");
1971 *code = zh->errCode;
1972 *addinfo = zh->errString;
1975 void zebra_shadow_enable(ZebraHandle zh, int value)
1978 yaz_log(log_level, "zebra_shadow_enable");
1979 zebra_clearError(zh);
1980 zh->shadow_enable = value;
1983 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1987 yaz_log(log_level, "zebra_octet_term_encoding");
1988 zebra_clearError(zh);
1990 if (zh->iconv_to_utf8 != 0)
1991 yaz_iconv_close(zh->iconv_to_utf8);
1992 if (zh->iconv_from_utf8 != 0)
1993 yaz_iconv_close(zh->iconv_from_utf8);
1996 yaz_iconv_open ("UTF-8", encoding);
1997 if (zh->iconv_to_utf8 == 0)
1998 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1999 zh->iconv_from_utf8 =
2000 yaz_iconv_open (encoding, "UTF-8");
2001 if (zh->iconv_to_utf8 == 0)
2002 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2007 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2010 yaz_log(log_level, "zebra_record_encoding");
2011 zebra_clearError(zh);
2012 xfree(zh->record_encoding);
2013 zh->record_encoding = 0;
2015 zh->record_encoding = xstrdup (encoding);
2019 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2024 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2025 zebra_clearError(zh);
2026 res_set(zh->res, name, value);
2029 const char *zebra_get_resource(ZebraHandle zh,
2030 const char *name, const char *defaultvalue)
2035 v = res_get_def (zh->res, name, (char *)defaultvalue);
2036 zebra_clearError(zh);
2037 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2041 /* moved from zebra_api_ext.c by pop */
2042 /* FIXME: Should this really be public??? -Heikki */
2044 int zebra_trans_no (ZebraHandle zh)
2047 yaz_log(log_level, "zebra_trans_no");
2048 return zh->trans_no;
2051 int zebra_get_shadow_enable (ZebraHandle zh)
2054 yaz_log(log_level, "zebra_get_shadow_enable");
2055 return zh->shadow_enable;
2058 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2061 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2062 zh->shadow_enable = value;
2065 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2066 so that it's identicical to the original api_records_retrieve */
2067 void api_records_retrieve (ZebraHandle zh, ODR stream,
2068 const char *setname, Z_RecordComposition *comp,
2069 oid_value input_format, int num_recs,
2070 ZebraRetrievalRecord *recs)
2072 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2076 /* ---------------------------------------------------------------------------
2077 Record insert(=update), delete
2079 If sysno is provided, then it's used to identify the record.
2080 If not, and match_criteria is provided, then sysno is guessed
2081 If not, and a record is provided, then sysno is got from there
2082 NOTE: Now returns 0 at success and updates sysno, which is an int*
2086 int zebra_add_record(ZebraHandle zh,
2087 const char *buf, int buf_size)
2090 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2093 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2094 const char *recordType,
2095 SYSNO *sysno, const char *match,
2097 const char *buf, int buf_size, int force_update)
2103 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2106 buf_size = strlen(buf);
2108 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2110 res = buffer_extract_record (zh, buf, buf_size,
2111 0, /* delete_flag */
2117 0); /* allow_update */
2118 zebra_end_trans(zh);
2122 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2123 const char *recordType,
2124 SYSNO* sysno, const char *match,
2126 const char *buf, int buf_size,
2134 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2136 if (buf_size < 1) buf_size = strlen(buf);
2138 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2140 res = buffer_extract_record (zh, buf, buf_size,
2141 0, /* delete_flag */
2147 1); /* allow_update */
2148 zebra_end_trans(zh);
2152 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2153 const char *recordType,
2154 SYSNO *sysno, const char *match,
2156 const char *buf, int buf_size,
2163 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2165 if (buf_size < 1) buf_size = strlen(buf);
2167 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2169 res = buffer_extract_record (zh, buf, buf_size,
2170 1, /* delete_flag */
2176 1); /* allow_update */
2177 zebra_end_trans(zh);
2181 /* ---------------------------------------------------------------------------
2185 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2186 const char *setname, zint *hits)
2189 ZEBRA_RES res = ZEBRA_OK;
2191 ODR odr = odr_createmem(ODR_ENCODE);
2196 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2198 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2202 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2203 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2207 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2211 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2219 /* ---------------------------------------------------------------------------
2220 Sort - a simplified interface, with optional read locks.
2222 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2223 const char *sort_spec,
2224 const char *output_setname,
2225 const char **input_setnames)
2227 int num_input_setnames = 0;
2228 int sort_status = 0;
2229 Z_SortKeySpecList *sort_sequence;
2233 assert(output_setname);
2234 assert(input_setnames);
2235 sort_sequence = yaz_sort_spec (stream, sort_spec);
2236 yaz_log(log_level, "sort (FIXME) ");
2239 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2240 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2244 /* we can do this, since the perl typemap code for char** will
2245 put a NULL at the end of list */
2246 while (input_setnames[num_input_setnames]) num_input_setnames++;
2248 if (zebra_begin_read (zh))
2251 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2252 output_setname, sort_sequence, &sort_status);
2258 /* ---------------------------------------------------------------------------
2259 Get BFS for Zebra system (to make alternative storage methods)
2261 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2264 return zh->reg->bfs;
2269 /* ---------------------------------------------------------------------------
2270 Set limit for search/scan
2272 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2275 zebra_limit_destroy(zh->m_limit);
2276 zh->m_limit = zebra_limit_create(complement_flag, ids);
2281 Set Error code + addinfo
2283 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2286 nmem_reset(zh->nmem_error);
2287 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2290 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2293 sprintf(vstr, ZINT_FORMAT, i);
2296 nmem_reset(zh->nmem_error);
2297 zh->errString = nmem_strdup(zh->nmem_error, vstr);