1 /* $Id: zebraapi.c,v 1.176 2005-06-10 10:55:18 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
34 #include <yaz/diagbib1.h>
35 #include <yaz/pquery.h>
36 #include <yaz/sortspec.h>
39 #include <idzebra/api.h>
41 /* simple asserts to validate the most essential input args */
42 #define ASSERTZH assert(zh && zh->service)
43 #define ASSERTZHRES assert(zh && zh->service && zh->res)
44 #define ASSERTZS assert(zs)
46 static int log_level = 0;
47 static int log_level_initialized = 0;
49 static Res zebra_open_res (ZebraHandle zh);
50 static void zebra_close_res (ZebraHandle zh);
52 static void zebra_chdir (ZebraService zs)
56 yaz_log(log_level, "zebra_chdir");
57 dir = res_get (zs->global_res, "chdir");
60 yaz_log (YLOG_DEBUG, "chdir %s", dir);
68 static void zebra_flush_reg (ZebraHandle zh)
71 yaz_log(log_level, "zebra_flush_reg");
73 zebraExplain_flush (zh->reg->zei, zh);
75 extract_flushWriteKeys (zh, 1 /* final */);
76 zebra_index_merge (zh );
79 static struct zebra_register *zebra_register_open (ZebraService zs,
81 int rw, int useshadow,
83 const char *reg_path);
84 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
86 ZebraHandle zebra_open (ZebraService zs)
89 const char *default_encoding;
90 if (!log_level_initialized)
92 log_level = yaz_log_module_level("zebraapi");
93 log_level_initialized = 1;
96 yaz_log(log_level, "zebra_open");
101 zh = (ZebraHandle) xmalloc(sizeof(*zh));
102 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
105 zh->reg = 0; /* no register attached yet */
113 zh->reg_name = xstrdup ("");
115 zh->num_basenames = 0;
118 zh->approx_limit = 1000000000;
125 zh->shadow_enable = 1;
127 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
130 yaz_iconv_open ("UTF-8", default_encoding);
131 if (zh->iconv_to_utf8 == 0)
132 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
134 zh->iconv_from_utf8 =
135 yaz_iconv_open (default_encoding, "UTF-8");
136 if (zh->iconv_to_utf8 == 0)
137 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
140 zh->record_encoding = 0;
142 zebra_mutex_cond_lock (&zs->session_lock);
144 zh->next = zs->sessions;
147 zebra_mutex_cond_unlock (&zs->session_lock);
149 zh->store_data_buf = 0;
151 zh->m_limit = zebra_limit_create(1, 0);
153 zh->nmem_error = nmem_create();
158 ZebraService zebra_start (const char *configName)
160 return zebra_start_res(configName, 0, 0);
163 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
167 if (!log_level_initialized)
169 log_level = yaz_log_module_level("zebraapi");
170 log_level_initialized = 1;
173 yaz_log(YLOG_LOG, "zebra_start %s %s",configName, ZEBRAVER);
176 if ((res = res_open (configName, def_res, over_res)))
178 const char *passwd_plain = 0;
179 const char *passwd_encrypt = 0;
180 ZebraService zh = xmalloc(sizeof(*zh));
182 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
184 zh->global_res = res;
185 zh->configName = xstrdup(configName);
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];
238 sprintf (resource_str, "dict.%.100s.pagesize", name);
242 v = res_get(res, resource_str);
246 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
249 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
254 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
255 int rw, int useshadow, Res res,
256 const char *reg_path)
258 struct zebra_register *reg;
259 int record_compression = REC_COMPRESS_NONE;
260 const char *recordCompression = 0;
261 const char *profilePath;
266 reg = xmalloc(sizeof(*reg));
269 reg->name = xstrdup (name);
276 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
277 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
279 reg->dh = data1_createx (DATA1_FLAG_XML);
285 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
288 data1_destroy(reg->dh);
294 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
296 bfs_destroy(reg->bfs);
297 data1_destroy(reg->dh);
303 getcwd(cwd, sizeof(cwd)-1);
304 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
305 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
307 data1_set_tabpath (reg->dh, profilePath);
308 data1_set_tabroot (reg->dh, reg_path);
309 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
311 reg->zebra_maps = zebra_maps_open (res, reg_path);
312 reg->rank_classes = NULL;
316 reg->keys.buf_max = 0;
318 reg->keys.codec_handle = iscz1_start();
320 reg->sortKeys.buf = 0;
321 reg->sortKeys.buf_max = 0;
332 reg->key_file_no = 0;
335 zebraRankInstall (reg, rank1_class);
336 zebraRankInstall (reg, rankzv_class);
338 recordCompression = res_get_def (res, "recordCompression", "none");
339 if (!strcmp (recordCompression, "none"))
340 record_compression = REC_COMPRESS_NONE;
341 if (!strcmp (recordCompression, "bzip2"))
342 record_compression = REC_COMPRESS_BZIP2;
344 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
346 yaz_log (YLOG_WARN, "rec_open failed");
351 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
353 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
355 yaz_log (YLOG_WARN, "dict_open failed");
358 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
360 yaz_log (YLOG_WARN, "sortIdx_open failed");
363 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
365 struct ISAMS_M_s isams_m;
366 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
367 key_isams_m(res, &isams_m))))
369 yaz_log (YLOG_WARN, "isams_open failed");
373 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
375 struct ISAMC_M_s isamc_m;
376 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
377 rw, key_isamc_m(res, &isamc_m))))
379 yaz_log (YLOG_WARN, "isamc_open failed");
383 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
385 struct ISAMC_M_s isamc_m;
387 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
388 rw, key_isamc_m(res, &isamc_m), 0)))
390 yaz_log (YLOG_WARN, "isamb_open failed");
394 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
396 struct ISAMC_M_s isamc_m;
398 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
399 rw, key_isamc_m(res, &isamc_m), 1)))
401 yaz_log (YLOG_WARN, "isamb_open failed");
405 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
407 struct ISAMC_M_s isamc_m;
409 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
410 rw, key_isamc_m(res, &isamc_m), -1)))
412 yaz_log (YLOG_WARN, "isamb_open failed");
416 reg->zei = zebraExplain_open (reg->records, reg->dh,
421 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
425 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
429 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
432 yaz_log(log_level, "zebra_admin_shutdown");
433 zebra_clearError(zh);
435 zebra_mutex_cond_lock (&zh->service->session_lock);
436 zh->service->stop_flag = 1;
437 zebra_mutex_cond_unlock (&zh->service->session_lock);
441 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
445 yaz_log(log_level, "zebra_admin_start");
446 zebra_clearError(zh);
448 zebra_mutex_cond_lock (&zs->session_lock);
449 zebra_mutex_cond_unlock (&zs->session_lock);
453 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
457 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
462 zebraExplain_close (reg->zei);
463 dict_close (reg->dict);
465 dict_close (reg->matchDict);
466 sortIdx_close (reg->sortIdx);
468 isams_close (reg->isams);
470 isamc_close (reg->isamc);
472 isamb_close (reg->isamb);
473 rec_close (®->records);
476 recTypes_destroy (reg->recTypes);
477 zebra_maps_close (reg->zebra_maps);
478 zebraRankDestroy (reg);
479 bfs_destroy (reg->bfs);
480 data1_destroy (reg->dh);
482 xfree(reg->sortKeys.buf);
483 xfree(reg->keys.buf);
484 if (reg->keys.codec_handle)
485 iscz1_stop(reg->keys.codec_handle);
491 ZEBRA_RES zebra_stop(ZebraService zs)
495 yaz_log (log_level, "zebra_stop");
499 zebra_close (zs->sessions);
502 zebra_mutex_cond_destroy (&zs->session_lock);
505 passwd_db_close (zs->passwd_db);
507 recTypeClass_destroy(zs->record_classes);
508 nmem_destroy(zs->nmem);
509 res_close (zs->global_res);
510 xfree(zs->configName);
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);
698 void map_basenames_func (void *vp, const char *name, const char *value)
700 struct map_baseinfo *p = (struct map_baseinfo *) vp;
702 char fromdb[128], todb[8][128];
709 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
710 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
711 todb[5], todb[6], todb[7]);
715 for (i = 0; i<p->num_bases; i++)
716 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
719 for (i = 0; i < no; i++)
721 if (p->new_num_bases == p->new_num_max)
723 p->new_basenames[(p->new_num_bases)++] =
724 nmem_strdup (p->mem, todb[i]);
730 int zebra_select_default_database(ZebraHandle zh)
734 /* no database has been selected - so we select based on
735 resource setting (including group)
737 const char *group = res_get(zh->service->global_res, "group");
738 const char *v = res_get_prefix(zh->service->global_res,
739 "database", group, "Default");
740 return zebra_select_database(zh, v);
745 void map_basenames (ZebraHandle zh, ODR stream,
746 int *num_bases, char ***basenames)
748 struct map_baseinfo info;
749 struct map_baseinfo *p = &info;
752 yaz_log(log_level, "map_basenames ");
757 info.num_bases = *num_bases;
758 info.basenames = *basenames;
759 info.new_num_max = 128;
760 info.new_num_bases = 0;
761 info.new_basenames = (char **)
762 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
763 info.mem = stream->mem;
765 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
767 for (i = 0; i<p->num_bases; i++)
768 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
770 p->new_basenames[(p->new_num_bases)++] =
771 nmem_strdup (p->mem, p->basenames[i]);
773 *num_bases = info.new_num_bases;
774 *basenames = info.new_basenames;
775 for (i = 0; i<*num_bases; i++)
776 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
779 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
782 yaz_log(log_level, "zebra_select_database %s",basename);
784 return zebra_select_databases (zh, 1, &basename);
787 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
788 const char **basenames)
797 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
798 num_bases,basenames[0]);
803 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
806 for (i = 0; i < zh->num_basenames; i++)
807 xfree(zh->basenames[i]);
808 xfree(zh->basenames);
810 zh->num_basenames = num_bases;
811 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
812 for (i = 0; i < zh->num_basenames; i++)
813 zh->basenames[i] = xstrdup (basenames[i]);
815 cp = strrchr(basenames[0], '/');
818 len = cp - basenames[0];
819 new_reg = xmalloc(len + 1);
820 memcpy (new_reg, basenames[0], len);
824 new_reg = xstrdup ("");
825 for (i = 1; i<num_bases; i++)
829 cp1 = strrchr (basenames[i], '/');
834 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
837 if (len != cp1 - basenames[i] ||
838 memcmp (basenames[i], new_reg, len))
840 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
848 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
853 zebra_select_register (zh, new_reg);
857 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
860 if (!zh->lock_normal || !zh->lock_shadow)
862 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
868 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
870 if (approx_limit == 0)
871 approx_limit = 2000000000;
872 zh->approx_limit = approx_limit;
876 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
877 const char *setname, zint *hits)
885 yaz_log(log_level, "zebra_search_rpn");
886 zebra_clearError(zh);
890 if (zebra_begin_read(zh) == ZEBRA_FAIL)
893 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
894 zh->num_basenames, zh->basenames, setname);
900 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
902 Z_RecordComposition *comp,
903 oid_value input_format, int num_recs,
904 ZebraRetrievalRecord *recs)
906 ZebraMetaRecord *poset;
908 ZEBRA_RES ret = ZEBRA_OK;
916 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
918 zebra_clearError(zh);
921 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
926 if (zebra_begin_read (zh) == ZEBRA_FAIL)
929 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
930 for (i = 0; i<num_recs; i++)
931 pos_array[i] = recs[i].position;
932 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
935 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
936 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
942 for (i = 0; i<num_recs; i++)
947 recs[i].format = VAL_SUTRS;
948 recs[i].len = strlen(poset[i].term);
949 recs[i].buf = poset[i].term;
950 recs[i].base = poset[i].db;
952 else if (poset[i].sysno)
956 zebra_snippets *hit_snippet = zebra_snippets_create();
958 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
962 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
964 stream, input_format, comp,
965 &recs[i].format, &buf, &len,
966 &recs[i].base, &recs[i].errString);
971 recs[i].buf = (char*) odr_malloc(stream, len);
972 memcpy(recs[i].buf, buf, len);
976 recs[i].score = poset[i].score;
977 recs[i].sysno = poset[i].sysno;
978 zebra_snippets_destroy(hit_snippet);
982 if (ret == ZEBRA_OK) /* only need to set it once */
983 zebra_setError_zint(zh,
984 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
990 zebra_meta_records_destroy(zh, poset, num_recs);
997 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
999 int *num_entries, ZebraScanEntry **entries,
1002 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1003 Z_AttributesPlusTerm *zapt;
1007 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1010 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1013 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1014 position, num_entries, entries, is_partial);
1015 yaz_pqf_destroy (pqf_parser);
1019 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1020 oid_value attributeset,
1022 int *num_entries, ZebraScanEntry **entries,
1030 assert(num_entries);
1033 yaz_log(log_level, "zebra_scan");
1034 zebra_clearError(zh);
1035 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1041 res = rpn_scan (zh, stream, zapt, attributeset,
1042 zh->num_basenames, zh->basenames, position,
1043 num_entries, entries, is_partial, 0, 0);
1044 zebra_end_read (zh);
1048 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1049 int num_input_setnames, const char **input_setnames,
1050 const char *output_setname,
1051 Z_SortKeySpecList *sort_sequence,
1057 assert(num_input_setnames>0);
1058 assert(input_setnames);
1059 assert(sort_sequence);
1060 assert(sort_status);
1061 yaz_log(log_level, "zebra_sort");
1062 zebra_clearError(zh);
1063 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1065 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1066 output_setname, sort_sequence, sort_status);
1071 int zebra_deleteResultSet(ZebraHandle zh, int function,
1072 int num_setnames, char **setnames,
1078 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1079 zebra_clearError(zh);;
1080 if (zebra_begin_read(zh))
1081 return Z_DeleteStatus_systemProblemAtTarget;
1084 case Z_DeleteResultSetRequest_list:
1085 assert(num_setnames>0);
1087 resultSetDestroy (zh, num_setnames, setnames, statuses);
1089 case Z_DeleteResultSetRequest_all:
1090 resultSetDestroy (zh, -1, 0, statuses);
1093 zebra_end_read (zh);
1094 status = Z_DeleteStatus_success;
1095 for (i = 0; i<num_setnames; i++)
1096 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1097 status = statuses[i];
1101 int zebra_errCode (ZebraHandle zh)
1105 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1108 yaz_log(log_level, "zebra_errCode: o");
1112 const char *zebra_errString (ZebraHandle zh)
1116 e= diagbib1_str (zh->errCode);
1117 yaz_log(log_level, "zebra_errString: %s",e);
1121 char *zebra_errAdd (ZebraHandle zh)
1126 yaz_log(log_level, "zebra_errAdd: %s",a);
1130 void zebra_clearError(ZebraHandle zh)
1139 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1146 zebra_clearError(zh);
1149 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1150 p = res_get(zs->global_res, u);
1151 xfree(zh->user_perm);
1152 zh->user_perm = xstrdup(p ? p : "r");
1154 /* users that don't require a password .. */
1155 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1158 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1163 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1164 const char *record_type)
1167 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1168 database, record_type);
1169 zebra_clearError(zh);
1170 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1172 return zebra_begin_trans(zh, 1);
1175 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1178 yaz_log(log_level, "zebra_admin_import_end");
1179 zebra_clearError(zh);
1180 return zebra_end_trans(zh);
1183 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1185 ZEBRA_RES res = ZEBRA_OK;
1189 yaz_log(log_level, "zebra_admin_import_segment");
1190 zebra_clearError(zh);
1191 for (i = 0; i<segment->num_segmentRecords; i++)
1193 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1195 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1197 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1198 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1200 Odr_oct *oct = fragment->u.notExternallyTagged;
1203 if ( zebra_update_record(zh,
1204 0, /* record Type */
1217 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1218 const char *rec_buf,
1220 const char *recid_buf, size_t recid_len,
1222 /* 1 = insert. Fail it already exists */
1223 /* 2 = replace. Fail it does not exist */
1224 /* 3 = delete. Fail if does not exist */
1225 /* 4 = update. Insert/replace */
1232 assert(action>0 && action <=4);
1235 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1236 zebra_clearError(zh);
1238 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1241 memcpy (recid_z, recid_buf, recid_len);
1242 recid_z[recid_len] = 0;
1244 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1247 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1250 if (action == 1) /* fail if insert */
1252 zebra_end_trans(zh);
1256 memcpy (&sysno, rinfo+1, sizeof(sysno));
1260 if (action == 2 || action == 3) /* fail if delete or update */
1262 zebra_end_trans(zh);
1265 action = 1; /* make it an insert (if it's an update).. */
1267 res = buffer_extract_record (zh, rec_buf, rec_len,
1268 action == 3 ? 1 : 0 /* delete flag */,
1274 0, /* force update */
1275 1 /* allow update */
1279 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1281 else if (action == 3)
1283 dict_delete (zh->reg->matchDict, recid_z);
1285 zebra_end_trans(zh);
1289 int delete_w_handle(const char *info, void *handle)
1291 ZebraHandle zh = (ZebraHandle) handle;
1295 if (*info == sizeof(pos))
1297 memcpy (&pos, info+1, sizeof(pos));
1298 isamb_unlink(zh->reg->isamb, pos);
1303 static int delete_SU_handle(void *handle, int ord)
1305 ZebraHandle zh = (ZebraHandle) handle;
1309 ord_len = key_SU_encode (ord, ord_buf);
1310 ord_buf[ord_len] = '\0';
1312 assert (zh->reg->isamb);
1313 dict_delete_subtree(zh->reg->dict, ord_buf,
1314 zh, delete_w_handle);
1318 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1320 ZEBRA_RES ret = ZEBRA_OK;
1322 yaz_log(log_level, "zebra_drop_database %s", db);
1323 zebra_clearError(zh);
1325 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1327 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1331 zebraExplain_curDatabase (zh->reg->zei, db);
1333 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1334 zebraExplain_removeDatabase(zh->reg->zei, zh);
1338 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1341 zebra_end_trans (zh);
1345 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1348 yaz_log(log_level, "zebra_create_database %s", db);
1350 zebra_clearError(zh);
1352 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1354 if (zebra_begin_trans (zh, 1))
1357 /* announce database */
1358 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1359 /* explainDatabase */))
1361 zebra_end_trans (zh);
1362 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1365 return zebra_end_trans (zh);
1368 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1369 const char *input_str, int input_len,
1370 char *output_str, int output_len)
1376 yaz_log(log_level, "zebra_string_norm ");
1377 zebra_clearError(zh);
1378 if (!zh->reg->zebra_maps)
1380 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1381 input_str, input_len);
1384 if (wrbuf_len(wrbuf) >= output_len)
1386 if (wrbuf_len(wrbuf))
1387 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1388 output_str[wrbuf_len(wrbuf)] = '\0';
1389 return wrbuf_len(wrbuf);
1392 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1394 char state_fname[256];
1399 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1400 zebra_clearError(zh);
1402 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1403 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1404 f = fopen (fname, "w");
1406 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1407 fprintf (f, "%c %d %ld\n", val, seqno, p);
1412 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1414 char state_fname[256];
1419 yaz_log(log_level, "zebra_get_state ");
1420 zebra_clearError(zh);
1421 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1422 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1423 f = fopen (fname, "r");
1429 fscanf (f, "%c %d", val, seqno);
1435 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1437 return zebra_begin_trans(zh, 0);
1440 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1442 return zebra_end_trans(zh);
1445 static void read_res_for_transaction(ZebraHandle zh)
1447 const char *group = res_get(zh->res, "group");
1449 /* FIXME - do we still use groups ?? */
1451 zh->m_group = group;
1452 v = res_get_prefix(zh->res, "followLinks", group, "1");
1453 zh->m_follow_links = atoi(v);
1455 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1456 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1458 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1459 zh->m_store_keys = atoi(v);
1461 v = res_get_prefix(zh->res, "storeData", group, "1");
1462 zh->m_store_data = atoi(v);
1464 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1465 zh->m_explain_database = atoi(v);
1467 v = res_get_prefix(zh->res, "openRW", group, "1");
1468 zh->m_flag_rw = atoi(v);
1470 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1471 zh->m_file_verbose_limit = atoi(v);
1474 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1477 zebra_select_default_database(zh);
1480 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1481 "zebra_begin_trans: no database selected");
1485 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1489 if (rw && !strchr(zh->user_perm, 'w'))
1493 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1505 const char *rval = 0;
1510 read_res_for_transaction(zh);
1513 if (zh->trans_no != 1)
1515 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1516 "zebra_begin_trans: no write trans within read");
1521 resultSetInvalidate (zh);
1522 zebra_register_close (zh->service, zh->reg);
1524 zh->trans_w_no = zh->trans_no;
1526 zebra_clearError(zh);
1528 zh->records_inserted = 0;
1529 zh->records_updated = 0;
1530 zh->records_deleted = 0;
1531 zh->records_processed = 0;
1533 #if HAVE_SYS_TIMES_H
1537 if (zh->shadow_enable)
1538 rval = res_get (zh->res, "shadow");
1540 for (pass = 0; pass < 2; pass++)
1544 zebra_lock_r (zh->lock_normal);
1545 zebra_lock_w (zh->lock_shadow);
1549 zebra_lock_w (zh->lock_normal);
1550 zebra_lock_w (zh->lock_shadow);
1553 zebra_get_state (zh, &val, &seqno);
1556 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1557 zebra_unlock (zh->lock_shadow);
1558 zebra_unlock (zh->lock_normal);
1562 else if (val == 'd')
1566 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1568 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1569 bf_commitClean (bfs, rval);
1574 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1581 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1585 zebra_set_state (zh, 'd', seqno);
1587 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1588 1, rval ? 1 : 0, zh->res,
1591 zh->reg->seqno = seqno;
1594 zebra_set_state (zh, 'o', seqno);
1596 zebra_unlock (zh->lock_shadow);
1597 zebra_unlock (zh->lock_normal);
1602 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1603 "zebra_begin_trans: cannot open register");
1604 yaz_log(YLOG_FATAL, "%s", zh->errString);
1616 if (zh->trans_no != 1)
1618 zebra_flush_reg (zh);
1621 zebra_clearError(zh);
1622 #if HAVE_SYS_TIMES_H
1628 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1631 if (!zh->lock_normal || !zh->lock_shadow)
1634 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1637 zebra_get_state (zh, &val, &seqno);
1643 else if (seqno != zh->reg->seqno)
1645 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1646 seqno, zh->reg->seqno);
1649 else if (zh->reg->last_val != val)
1651 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1652 val, zh->reg->last_val);
1659 zebra_lock_r (zh->lock_shadow);
1661 zebra_lock_r (zh->lock_normal);
1665 resultSetInvalidate (zh);
1666 zebra_register_close (zh->service, zh->reg);
1668 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1669 0, val == 'c' ? 1 : 0,
1670 zh->res, zh->path_reg);
1673 zebra_unlock (zh->lock_normal);
1674 zebra_unlock (zh->lock_shadow);
1676 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1679 zh->reg->last_val = val;
1680 zh->reg->seqno = seqno;
1682 read_res_for_transaction(zh);
1686 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1688 ZebraTransactionStatus dummy;
1690 yaz_log(log_level, "zebra_end_trans");
1691 return zebra_end_transaction(zh, &dummy);
1694 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1702 yaz_log(log_level, "zebra_end_transaction");
1704 status->processed = 0;
1705 status->inserted = 0;
1706 status->updated = 0;
1707 status->deleted = 0;
1711 if (!zh->res || !zh->reg)
1713 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1714 "zebra_end_trans: no open transaction");
1717 if (zh->trans_no != zh->trans_w_no)
1720 if (zh->trans_no != 0)
1723 /* release read lock */
1725 zebra_unlock (zh->lock_normal);
1726 zebra_unlock (zh->lock_shadow);
1729 { /* release write lock */
1733 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1734 rval = res_get (zh->res, "shadow");
1736 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1738 zebra_flush_reg (zh);
1740 resultSetInvalidate (zh);
1742 zebra_register_close (zh->service, zh->reg);
1745 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1746 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1747 zh->records_processed, zh->records_inserted,
1748 zh->records_updated, zh->records_deleted);
1750 status->processed = (int) zh->records_processed;
1751 status->inserted = (int) zh->records_inserted;
1752 status->updated = (int) zh->records_updated;
1753 status->deleted = (int) zh->records_deleted;
1755 zebra_get_state (zh, &val, &seqno);
1758 BFiles bfs = bfs_create (rval, zh->path_reg);
1759 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1760 bf_commitClean (bfs, rval);
1765 zebra_set_state (zh, 'o', seqno);
1767 zebra_unlock (zh->lock_shadow);
1768 zebra_unlock (zh->lock_normal);
1771 #if HAVE_SYS_TIMES_H
1773 yaz_log (log_level, "user/system: %ld/%ld",
1774 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1775 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1777 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1778 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1783 int zebra_repository_update (ZebraHandle zh, const char *path)
1787 zebra_clearError(zh);
1788 yaz_log (log_level, "updating %s", path);
1789 repositoryUpdate (zh, path);
1793 int zebra_repository_delete (ZebraHandle zh, const char *path)
1797 zebra_clearError(zh);
1798 yaz_log (log_level, "deleting %s", path);
1799 repositoryDelete (zh, path);
1803 int zebra_repository_show (ZebraHandle zh, const char *path)
1807 yaz_log(log_level, "zebra_repository_show");
1808 zebra_clearError(zh);
1809 repositoryShow (zh, path);
1813 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1820 zebra_clearError(zh);
1822 zebra_select_default_database(zh);
1825 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1828 rval = res_get (zh->res, "shadow");
1831 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1835 zebra_lock_w (zh->lock_normal);
1836 zebra_lock_r (zh->lock_shadow);
1838 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1840 zebra_get_state (zh, &val, &seqno);
1843 bf_cache (bfs, rval);
1844 if (bf_commitExists (bfs))
1847 zebra_set_state (zh, 'd', seqno);
1850 zebra_set_state (zh, 'c', seqno);
1852 yaz_log (YLOG_DEBUG, "commit start");
1853 bf_commitExec (bfs);
1858 yaz_log (YLOG_DEBUG, "commit clean");
1859 bf_commitClean (bfs, rval);
1861 zebra_set_state (zh, 'o', seqno);
1865 yaz_log (log_level, "nothing to commit");
1869 zebra_unlock (zh->lock_shadow);
1870 zebra_unlock (zh->lock_normal);
1874 ZEBRA_RES zebra_clean(ZebraHandle zh)
1877 yaz_log(log_level, "zebra_clean");
1878 return zebra_commit_ex(zh, 1);
1881 ZEBRA_RES zebra_commit(ZebraHandle zh)
1884 yaz_log(log_level, "zebra_commit");
1885 return zebra_commit_ex(zh, 0);
1888 ZEBRA_RES zebra_init(ZebraHandle zh)
1893 yaz_log(log_level, "zebra_init");
1894 zebra_clearError(zh);
1896 zebra_select_default_database(zh);
1899 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1900 "cannot select default database");
1903 rval = res_get (zh->res, "shadow");
1905 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1908 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1912 bf_cache (bfs, rval);
1916 zebra_set_state (zh, 'o', 0);
1920 ZEBRA_RES zebra_compact(ZebraHandle zh)
1924 yaz_log(log_level, "zebra_compact");
1925 zebra_clearError(zh);
1928 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1931 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1937 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1940 yaz_log(log_level, "zebra_result");
1941 *code = zh->errCode;
1942 *addinfo = zh->errString;
1945 void zebra_shadow_enable(ZebraHandle zh, int value)
1948 yaz_log(log_level, "zebra_shadow_enable");
1949 zebra_clearError(zh);
1950 zh->shadow_enable = value;
1953 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1957 yaz_log(log_level, "zebra_octet_term_encoding");
1958 zebra_clearError(zh);
1960 if (zh->iconv_to_utf8 != 0)
1961 yaz_iconv_close(zh->iconv_to_utf8);
1962 if (zh->iconv_from_utf8 != 0)
1963 yaz_iconv_close(zh->iconv_from_utf8);
1966 yaz_iconv_open ("UTF-8", encoding);
1967 if (zh->iconv_to_utf8 == 0)
1968 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1969 zh->iconv_from_utf8 =
1970 yaz_iconv_open (encoding, "UTF-8");
1971 if (zh->iconv_to_utf8 == 0)
1972 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1977 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1980 yaz_log(log_level, "zebra_record_encoding");
1981 zebra_clearError(zh);
1982 xfree(zh->record_encoding);
1983 zh->record_encoding = 0;
1985 zh->record_encoding = xstrdup (encoding);
1989 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1994 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
1995 zebra_clearError(zh);
1996 res_set(zh->res, name, value);
1999 const char *zebra_get_resource(ZebraHandle zh,
2000 const char *name, const char *defaultvalue)
2005 assert(defaultvalue);
2006 v = res_get_def (zh->res, name, (char *)defaultvalue);
2007 zebra_clearError(zh);
2008 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2012 /* moved from zebra_api_ext.c by pop */
2013 /* FIXME: Should this really be public??? -Heikki */
2015 int zebra_trans_no (ZebraHandle zh)
2018 yaz_log(log_level, "zebra_trans_no");
2019 return zh->trans_no;
2022 int zebra_get_shadow_enable (ZebraHandle zh)
2025 yaz_log(log_level, "zebra_get_shadow_enable");
2026 return zh->shadow_enable;
2029 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2032 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2033 zh->shadow_enable = value;
2036 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2037 so that it's identicical to the original api_records_retrieve */
2038 void api_records_retrieve (ZebraHandle zh, ODR stream,
2039 const char *setname, Z_RecordComposition *comp,
2040 oid_value input_format, int num_recs,
2041 ZebraRetrievalRecord *recs)
2043 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2047 /* ---------------------------------------------------------------------------
2048 Record insert(=update), delete
2050 If sysno is provided, then it's used to identify the record.
2051 If not, and match_criteria is provided, then sysno is guessed
2052 If not, and a record is provided, then sysno is got from there
2053 NOTE: Now returns 0 at success and updates sysno, which is an int*
2057 int zebra_add_record(ZebraHandle zh,
2058 const char *buf, int buf_size)
2061 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2064 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2065 const char *recordType,
2066 SYSNO *sysno, const char *match,
2068 const char *buf, int buf_size, int force_update)
2074 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2077 buf_size = strlen(buf);
2079 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2081 res = buffer_extract_record (zh, buf, buf_size,
2082 0, /* delete_flag */
2088 0); /* allow_update */
2089 zebra_end_trans(zh);
2093 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2094 const char *recordType,
2095 SYSNO* sysno, const char *match,
2097 const char *buf, int buf_size,
2105 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2107 if (buf_size < 1) buf_size = strlen(buf);
2109 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2111 res = buffer_extract_record (zh, buf, buf_size,
2112 0, /* delete_flag */
2118 1); /* allow_update */
2119 zebra_end_trans(zh);
2123 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2124 const char *recordType,
2125 SYSNO *sysno, const char *match,
2127 const char *buf, int buf_size,
2134 yaz_log(log_level, "zebra_delete_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 1, /* delete_flag */
2147 1); /* allow_update */
2148 zebra_end_trans(zh);
2152 /* ---------------------------------------------------------------------------
2156 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2157 const char *setname, zint *hits)
2160 ZEBRA_RES res = ZEBRA_OK;
2162 ODR odr = odr_createmem(ODR_ENCODE);
2167 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2169 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2173 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2174 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2178 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2182 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2190 /* ---------------------------------------------------------------------------
2191 Sort - a simplified interface, with optional read locks.
2193 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2194 const char *sort_spec,
2195 const char *output_setname,
2196 const char **input_setnames)
2198 int num_input_setnames = 0;
2199 int sort_status = 0;
2200 Z_SortKeySpecList *sort_sequence;
2204 assert(output_setname);
2205 assert(input_setnames);
2206 sort_sequence = yaz_sort_spec (stream, sort_spec);
2207 yaz_log(log_level, "sort (FIXME) ");
2210 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2211 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2215 /* we can do this, since the perl typemap code for char** will
2216 put a NULL at the end of list */
2217 while (input_setnames[num_input_setnames]) num_input_setnames++;
2219 if (zebra_begin_read (zh))
2222 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2223 output_setname, sort_sequence, &sort_status);
2229 /* ---------------------------------------------------------------------------
2230 Get BFS for Zebra system (to make alternative storage methods)
2232 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2235 return zh->reg->bfs;
2240 /* ---------------------------------------------------------------------------
2241 Set limit for search/scan
2243 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2246 zebra_limit_destroy(zh->m_limit);
2247 zh->m_limit = zebra_limit_create(complement_flag, ids);
2252 Set Error code + addinfo
2254 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2257 nmem_reset(zh->nmem_error);
2258 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2261 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2264 sprintf(vstr, ZINT_FORMAT, i);
2267 nmem_reset(zh->nmem_error);
2268 zh->errString = nmem_strdup(zh->nmem_error, vstr);