1 /* $Id: zebraapi.c,v 1.172 2005-05-31 17:10:06 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;
124 zh->shadow_enable = 1;
126 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
129 yaz_iconv_open ("UTF-8", default_encoding);
130 if (zh->iconv_to_utf8 == 0)
131 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
133 zh->iconv_from_utf8 =
134 yaz_iconv_open (default_encoding, "UTF-8");
135 if (zh->iconv_to_utf8 == 0)
136 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
139 zh->record_encoding = 0;
141 zebra_mutex_cond_lock (&zs->session_lock);
143 zh->next = zs->sessions;
146 zebra_mutex_cond_unlock (&zs->session_lock);
148 zh->store_data_buf = 0;
150 zh->m_limit = zebra_limit_create(1, 0);
152 zh->nmem_error = nmem_create();
157 ZebraService zebra_start (const char *configName)
159 return zebra_start_res(configName, 0, 0);
162 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
166 if (!log_level_initialized)
168 log_level = yaz_log_module_level("zebraapi");
169 log_level_initialized = 1;
172 yaz_log(YLOG_LOG, "zebra_start %s %s",configName, ZEBRAVER);
175 if ((res = res_open (configName, def_res, over_res)))
177 const char *passwd_plain = 0;
178 const char *passwd_encrypt = 0;
179 ZebraService zh = xmalloc(sizeof(*zh));
181 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
183 zh->global_res = res;
184 zh->configName = xstrdup(configName);
189 zebra_mutex_cond_init (&zh->session_lock);
190 passwd_plain = res_get (zh->global_res, "passwd");
191 passwd_encrypt = res_get (zh->global_res, "passwd.c");
193 if (!passwd_plain && !passwd_encrypt)
194 zh->passwd_db = NULL;
197 zh->passwd_db = passwd_db_open();
199 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
203 passwd_db_file_plain(zh->passwd_db, passwd_plain);
205 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
208 zh->path_root = res_get (zh->global_res, "root");
209 zh->nmem = nmem_create();
210 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
216 void zebra_filter_info(ZebraService zs, void *cd,
217 void (*cb)(void *cd, const char *name))
221 recTypeClass_info(zs->record_classes, cd, cb);
224 void zebra_pidfname(ZebraService zs, char *path)
227 zebra_lock_prefix (zs->global_res, path);
228 strcat(path, "zebrasrv.pid");
231 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
232 int compact_flag, Res res)
234 int page_size = 4096;
235 char resource_str[200];
237 sprintf (resource_str, "dict.%.100s.pagesize", name);
241 v = res_get(res, resource_str);
245 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
248 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
253 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
254 int rw, int useshadow, Res res,
255 const char *reg_path)
257 struct zebra_register *reg;
258 int record_compression = REC_COMPRESS_NONE;
259 const char *recordCompression = 0;
260 const char *profilePath;
265 reg = xmalloc(sizeof(*reg));
268 reg->name = xstrdup (name);
275 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
276 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
278 reg->dh = data1_createx (DATA1_FLAG_XML);
284 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
287 data1_destroy(reg->dh);
293 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
295 bfs_destroy(reg->bfs);
296 data1_destroy(reg->dh);
302 getcwd(cwd, sizeof(cwd)-1);
303 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
304 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
306 data1_set_tabpath (reg->dh, profilePath);
307 data1_set_tabroot (reg->dh, reg_path);
308 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
310 reg->zebra_maps = zebra_maps_open (res, reg_path);
311 reg->rank_classes = NULL;
315 reg->keys.buf_max = 0;
317 reg->keys.codec_handle = iscz1_start();
319 reg->sortKeys.buf = 0;
320 reg->sortKeys.buf_max = 0;
331 reg->key_file_no = 0;
334 zebraRankInstall (reg, rank1_class);
335 zebraRankInstall (reg, rankzv_class);
337 recordCompression = res_get_def (res, "recordCompression", "none");
338 if (!strcmp (recordCompression, "none"))
339 record_compression = REC_COMPRESS_NONE;
340 if (!strcmp (recordCompression, "bzip2"))
341 record_compression = REC_COMPRESS_BZIP2;
343 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
345 yaz_log (YLOG_WARN, "rec_open failed");
350 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
352 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
354 yaz_log (YLOG_WARN, "dict_open failed");
357 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
359 yaz_log (YLOG_WARN, "sortIdx_open failed");
362 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
364 struct ISAMS_M_s isams_m;
365 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
366 key_isams_m(res, &isams_m))))
368 yaz_log (YLOG_WARN, "isams_open failed");
372 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
374 struct ISAMC_M_s isamc_m;
375 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
376 rw, key_isamc_m(res, &isamc_m))))
378 yaz_log (YLOG_WARN, "isamc_open failed");
382 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
384 struct ISAMC_M_s isamc_m;
386 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
387 rw, key_isamc_m(res, &isamc_m), 0)))
389 yaz_log (YLOG_WARN, "isamb_open failed");
393 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
395 struct ISAMC_M_s isamc_m;
397 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
398 rw, key_isamc_m(res, &isamc_m), 1)))
400 yaz_log (YLOG_WARN, "isamb_open failed");
404 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
406 struct ISAMC_M_s isamc_m;
408 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
409 rw, key_isamc_m(res, &isamc_m), -1)))
411 yaz_log (YLOG_WARN, "isamb_open failed");
415 reg->zei = zebraExplain_open (reg->records, reg->dh,
420 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
424 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
428 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
431 yaz_log(log_level, "zebra_admin_shutdown");
432 zebra_clearError(zh);
434 zebra_mutex_cond_lock (&zh->service->session_lock);
435 zh->service->stop_flag = 1;
436 zebra_mutex_cond_unlock (&zh->service->session_lock);
440 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
444 yaz_log(log_level, "zebra_admin_start");
445 zebra_clearError(zh);
447 zebra_mutex_cond_lock (&zs->session_lock);
448 zebra_mutex_cond_unlock (&zs->session_lock);
452 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
456 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
461 zebraExplain_close (reg->zei);
462 dict_close (reg->dict);
464 dict_close (reg->matchDict);
465 sortIdx_close (reg->sortIdx);
467 isams_close (reg->isams);
469 isamc_close (reg->isamc);
471 isamb_close (reg->isamb);
472 rec_close (®->records);
475 recTypes_destroy (reg->recTypes);
476 zebra_maps_close (reg->zebra_maps);
477 zebraRankDestroy (reg);
478 bfs_destroy (reg->bfs);
479 data1_destroy (reg->dh);
481 xfree(reg->sortKeys.buf);
482 xfree(reg->keys.buf);
483 if (reg->keys.codec_handle)
484 iscz1_stop(reg->keys.codec_handle);
490 ZEBRA_RES zebra_stop(ZebraService zs)
494 yaz_log (log_level, "zebra_stop");
498 zebra_close (zs->sessions);
501 zebra_mutex_cond_destroy (&zs->session_lock);
504 passwd_db_close (zs->passwd_db);
506 recTypeClass_destroy(zs->record_classes);
507 nmem_destroy(zs->nmem);
508 res_close (zs->global_res);
509 xfree(zs->configName);
514 ZEBRA_RES zebra_close (ZebraHandle zh)
517 struct zebra_session **sp;
520 yaz_log(log_level, "zebra_close");
527 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
528 resultSetDestroy (zh, -1, 0, 0);
531 zebra_register_close (zh->service, zh->reg);
532 zebra_close_res (zh);
534 xfree(zh->record_encoding);
536 for (i = 0; i < zh->num_basenames; i++)
537 xfree(zh->basenames[i]);
538 xfree(zh->basenames);
540 if (zh->iconv_to_utf8 != 0)
541 yaz_iconv_close (zh->iconv_to_utf8);
542 if (zh->iconv_from_utf8 != 0)
543 yaz_iconv_close (zh->iconv_from_utf8);
545 zebra_mutex_cond_lock (&zs->session_lock);
546 zebra_lock_destroy (zh->lock_normal);
547 zebra_lock_destroy (zh->lock_shadow);
559 zebra_mutex_cond_unlock (&zs->session_lock);
561 xfree(zh->user_perm);
562 zh->service = 0; /* more likely to trigger an assert */
564 zebra_limit_destroy(zh->m_limit);
566 nmem_destroy(zh->nmem_error);
573 struct map_baseinfo {
579 char **new_basenames;
583 static Res zebra_open_res (ZebraHandle zh)
592 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
593 res = res_open (fname, zh->service->global_res, 0);
595 res = zh->service->global_res;
597 else if (*zh->reg_name == 0)
599 res = zh->service->global_res;
603 yaz_log (YLOG_WARN, "no register root specified");
604 return 0; /* no path for register - fail! */
609 static void zebra_close_res (ZebraHandle zh)
613 if (zh->res != zh->service->global_res)
618 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
622 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
626 assert (zh->reg == 0);
627 assert (*zh->reg_name == 0);
633 resultSetInvalidate (zh);
634 zebra_register_close (zh->service, zh->reg);
640 zh->reg_name = xstrdup (new_reg);
644 if (zh->service->path_root)
646 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
647 strlen(zh->reg_name) + 3);
648 strcpy (zh->path_reg, zh->service->path_root);
651 strcat (zh->path_reg, "/");
652 strcat (zh->path_reg, zh->reg_name);
655 zh->res = zebra_open_res (zh);
658 zebra_lock_destroy (zh->lock_normal);
662 zebra_lock_destroy (zh->lock_shadow);
668 const char *lock_area =res_get (zh->res, "lockDir");
670 if (!lock_area && zh->path_reg)
671 res_set (zh->res, "lockDir", zh->path_reg);
672 sprintf (fname, "norm.%s.LCK", zh->reg_name);
674 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
676 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
678 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
680 if (!zh->lock_normal || !zh->lock_shadow)
684 zebra_lock_destroy(zh->lock_normal);
689 zebra_lock_destroy(zh->lock_shadow);
697 void map_basenames_func (void *vp, const char *name, const char *value)
699 struct map_baseinfo *p = (struct map_baseinfo *) vp;
701 char fromdb[128], todb[8][128];
708 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
709 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
710 todb[5], todb[6], todb[7]);
714 for (i = 0; i<p->num_bases; i++)
715 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
718 for (i = 0; i < no; i++)
720 if (p->new_num_bases == p->new_num_max)
722 p->new_basenames[(p->new_num_bases)++] =
723 nmem_strdup (p->mem, todb[i]);
729 int zebra_select_default_database(ZebraHandle zh)
733 /* no database has been selected - so we select based on
734 resource setting (including group)
736 const char *group = res_get(zh->service->global_res, "group");
737 const char *v = res_get_prefix(zh->service->global_res,
738 "database", group, "Default");
739 return zebra_select_database(zh, v);
744 void map_basenames (ZebraHandle zh, ODR stream,
745 int *num_bases, char ***basenames)
747 struct map_baseinfo info;
748 struct map_baseinfo *p = &info;
751 yaz_log(log_level, "map_basenames ");
756 info.num_bases = *num_bases;
757 info.basenames = *basenames;
758 info.new_num_max = 128;
759 info.new_num_bases = 0;
760 info.new_basenames = (char **)
761 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
762 info.mem = stream->mem;
764 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
766 for (i = 0; i<p->num_bases; i++)
767 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
769 p->new_basenames[(p->new_num_bases)++] =
770 nmem_strdup (p->mem, p->basenames[i]);
772 *num_bases = info.new_num_bases;
773 *basenames = info.new_basenames;
774 for (i = 0; i<*num_bases; i++)
775 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
778 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
781 yaz_log(log_level, "zebra_select_database %s",basename);
783 return zebra_select_databases (zh, 1, &basename);
786 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
787 const char **basenames)
796 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
797 num_bases,basenames[0]);
802 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
805 for (i = 0; i < zh->num_basenames; i++)
806 xfree(zh->basenames[i]);
807 xfree(zh->basenames);
809 zh->num_basenames = num_bases;
810 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
811 for (i = 0; i < zh->num_basenames; i++)
812 zh->basenames[i] = xstrdup (basenames[i]);
814 cp = strrchr(basenames[0], '/');
817 len = cp - basenames[0];
818 new_reg = xmalloc(len + 1);
819 memcpy (new_reg, basenames[0], len);
823 new_reg = xstrdup ("");
824 for (i = 1; i<num_bases; i++)
828 cp1 = strrchr (basenames[i], '/');
833 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
836 if (len != cp1 - basenames[i] ||
837 memcmp (basenames[i], new_reg, len))
839 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
847 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
852 zebra_select_register (zh, new_reg);
856 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
859 if (!zh->lock_normal || !zh->lock_shadow)
861 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
867 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
868 const char *setname, zint *hits)
876 yaz_log(log_level, "zebra_search_rpn");
877 zebra_clearError(zh);
881 if (zebra_begin_read(zh) == ZEBRA_FAIL)
884 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
885 zh->num_basenames, zh->basenames, setname);
891 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
893 Z_RecordComposition *comp,
894 oid_value input_format, int num_recs,
895 ZebraRetrievalRecord *recs)
897 ZebraMetaRecord *poset;
899 ZEBRA_RES ret = ZEBRA_OK;
907 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
909 zebra_clearError(zh);
912 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
917 if (zebra_begin_read (zh) == ZEBRA_FAIL)
920 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
921 for (i = 0; i<num_recs; i++)
922 pos_array[i] = recs[i].position;
923 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
926 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
927 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
933 for (i = 0; i<num_recs; i++)
938 recs[i].format = VAL_SUTRS;
939 recs[i].len = strlen(poset[i].term);
940 recs[i].buf = poset[i].term;
941 recs[i].base = poset[i].db;
943 else if (poset[i].sysno)
948 zebra_get_hit_vector(zh, setname, poset[i].sysno);
951 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
952 stream, input_format, comp,
953 &recs[i].format, &buf, &len,
954 &recs[i].base, &recs[i].errString);
958 recs[i].buf = (char*) odr_malloc(stream, len);
959 memcpy(recs[i].buf, buf, len);
963 recs[i].score = poset[i].score;
964 recs[i].sysno = poset[i].sysno;
968 if (ret == ZEBRA_OK) /* only need to set it once */
969 zebra_setError_zint(zh,
970 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
976 zebra_meta_records_destroy(zh, poset, num_recs);
983 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
985 int *num_entries, ZebraScanEntry **entries,
988 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
989 Z_AttributesPlusTerm *zapt;
993 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
996 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
999 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1000 position, num_entries, entries, is_partial);
1001 yaz_pqf_destroy (pqf_parser);
1005 ZEBRA_RES zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1006 oid_value attributeset,
1008 int *num_entries, ZebraScanEntry **entries,
1016 assert(num_entries);
1019 yaz_log(log_level, "zebra_scan");
1020 zebra_clearError(zh);
1021 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1027 res = rpn_scan (zh, stream, zapt, attributeset,
1028 zh->num_basenames, zh->basenames, position,
1029 num_entries, entries, is_partial, 0, 0);
1030 zebra_end_read (zh);
1034 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1035 int num_input_setnames, const char **input_setnames,
1036 const char *output_setname,
1037 Z_SortKeySpecList *sort_sequence,
1043 assert(num_input_setnames>0);
1044 assert(input_setnames);
1045 assert(sort_sequence);
1046 assert(sort_status);
1047 yaz_log(log_level, "zebra_sort");
1048 zebra_clearError(zh);
1049 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1051 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1052 output_setname, sort_sequence, sort_status);
1057 int zebra_deleteResultSet(ZebraHandle zh, int function,
1058 int num_setnames, char **setnames,
1064 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1065 zebra_clearError(zh);;
1066 if (zebra_begin_read(zh))
1067 return Z_DeleteStatus_systemProblemAtTarget;
1070 case Z_DeleteResultSetRequest_list:
1071 assert(num_setnames>0);
1073 resultSetDestroy (zh, num_setnames, setnames, statuses);
1075 case Z_DeleteResultSetRequest_all:
1076 resultSetDestroy (zh, -1, 0, statuses);
1079 zebra_end_read (zh);
1080 status = Z_DeleteStatus_success;
1081 for (i = 0; i<num_setnames; i++)
1082 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1083 status = statuses[i];
1087 int zebra_errCode (ZebraHandle zh)
1091 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1094 yaz_log(log_level, "zebra_errCode: o");
1098 const char *zebra_errString (ZebraHandle zh)
1102 e= diagbib1_str (zh->errCode);
1103 yaz_log(log_level, "zebra_errString: %s",e);
1107 char *zebra_errAdd (ZebraHandle zh)
1112 yaz_log(log_level, "zebra_errAdd: %s",a);
1116 void zebra_clearError(ZebraHandle zh)
1125 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1132 zebra_clearError(zh);
1135 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1136 p = res_get(zs->global_res, u);
1137 xfree(zh->user_perm);
1138 zh->user_perm = xstrdup(p ? p : "r");
1140 /* users that don't require a password .. */
1141 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1144 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1149 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1150 const char *record_type)
1153 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1154 database, record_type);
1155 zebra_clearError(zh);
1156 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1158 return zebra_begin_trans(zh, 1);
1161 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1164 yaz_log(log_level, "zebra_admin_import_end");
1165 zebra_clearError(zh);
1166 return zebra_end_trans(zh);
1169 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1171 ZEBRA_RES res = ZEBRA_OK;
1175 yaz_log(log_level, "zebra_admin_import_segment");
1176 zebra_clearError(zh);
1177 for (i = 0; i<segment->num_segmentRecords; i++)
1179 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1181 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1183 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1184 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1186 Odr_oct *oct = fragment->u.notExternallyTagged;
1189 if ( zebra_update_record(zh,
1190 0, /* record Type */
1203 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1204 const char *rec_buf,
1206 const char *recid_buf, size_t recid_len,
1208 /* 1 = insert. Fail it already exists */
1209 /* 2 = replace. Fail it does not exist */
1210 /* 3 = delete. Fail if does not exist */
1211 /* 4 = update. Insert/replace */
1218 assert(action>0 && action <=4);
1221 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1222 zebra_clearError(zh);
1224 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1227 memcpy (recid_z, recid_buf, recid_len);
1228 recid_z[recid_len] = 0;
1230 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1233 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1236 if (action == 1) /* fail if insert */
1238 zebra_end_trans(zh);
1242 memcpy (&sysno, rinfo+1, sizeof(sysno));
1246 if (action == 2 || action == 3) /* fail if delete or update */
1248 zebra_end_trans(zh);
1251 action = 1; /* make it an insert (if it's an update).. */
1253 res = buffer_extract_record (zh, rec_buf, rec_len,
1254 action == 3 ? 1 : 0 /* delete flag */,
1260 0, /* force update */
1261 1 /* allow update */
1265 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1267 else if (action == 3)
1269 dict_delete (zh->reg->matchDict, recid_z);
1271 zebra_end_trans(zh);
1275 int delete_w_handle(const char *info, void *handle)
1277 ZebraHandle zh = (ZebraHandle) handle;
1281 if (*info == sizeof(pos))
1283 memcpy (&pos, info+1, sizeof(pos));
1284 isamb_unlink(zh->reg->isamb, pos);
1289 static int delete_SU_handle(void *handle, int ord)
1291 ZebraHandle zh = (ZebraHandle) handle;
1295 ord_len = key_SU_encode (ord, ord_buf);
1296 ord_buf[ord_len] = '\0';
1298 assert (zh->reg->isamb);
1299 dict_delete_subtree(zh->reg->dict, ord_buf,
1300 zh, delete_w_handle);
1304 ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database)
1306 ZEBRA_RES ret = ZEBRA_OK;
1308 yaz_log(log_level, "zebra_drop_database");
1309 zebra_clearError(zh);
1311 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1313 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1317 zebraExplain_curDatabase (zh->reg->zei, database);
1319 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1320 zebraExplain_removeDatabase(zh->reg->zei, zh);
1324 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1327 zebra_end_trans (zh);
1331 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database)
1334 yaz_log(log_level, "zebra_create_database %s", database);
1336 zebra_clearError(zh);
1338 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1340 if (zebra_begin_trans (zh, 1))
1343 /* announce database */
1344 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1345 /* explainDatabase */))
1347 zebra_end_trans (zh);
1348 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, database);
1351 return zebra_end_trans (zh);
1354 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1355 const char *input_str, int input_len,
1356 char *output_str, int output_len)
1362 yaz_log(log_level, "zebra_string_norm ");
1363 zebra_clearError(zh);
1364 if (!zh->reg->zebra_maps)
1366 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1367 input_str, input_len);
1370 if (wrbuf_len(wrbuf) >= output_len)
1372 if (wrbuf_len(wrbuf))
1373 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1374 output_str[wrbuf_len(wrbuf)] = '\0';
1375 return wrbuf_len(wrbuf);
1378 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1380 char state_fname[256];
1385 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1386 zebra_clearError(zh);
1388 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1389 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1390 f = fopen (fname, "w");
1392 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1393 fprintf (f, "%c %d %ld\n", val, seqno, p);
1398 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1400 char state_fname[256];
1405 yaz_log(log_level, "zebra_get_state ");
1406 zebra_clearError(zh);
1407 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1408 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1409 f = fopen (fname, "r");
1415 fscanf (f, "%c %d", val, seqno);
1421 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1423 return zebra_begin_trans(zh, 0);
1426 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1428 return zebra_end_trans(zh);
1431 static void read_res_for_transaction(ZebraHandle zh)
1433 const char *group = res_get(zh->res, "group");
1435 /* FIXME - do we still use groups ?? */
1437 zh->m_group = group;
1438 v = res_get_prefix(zh->res, "followLinks", group, "1");
1439 zh->m_follow_links = atoi(v);
1441 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1442 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1444 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1445 zh->m_store_keys = atoi(v);
1447 v = res_get_prefix(zh->res, "storeData", group, "1");
1448 zh->m_store_data = atoi(v);
1450 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1451 zh->m_explain_database = atoi(v);
1453 v = res_get_prefix(zh->res, "openRW", group, "1");
1454 zh->m_flag_rw = atoi(v);
1456 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1457 zh->m_file_verbose_limit = atoi(v);
1460 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1463 zebra_select_default_database(zh);
1466 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1467 "zebra_begin_trans: no database selected");
1471 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1475 if (rw && !strchr(zh->user_perm, 'w'))
1479 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1491 const char *rval = 0;
1496 read_res_for_transaction(zh);
1499 if (zh->trans_no != 1)
1501 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1502 "zebra_begin_trans: no write trans within read");
1507 resultSetInvalidate (zh);
1508 zebra_register_close (zh->service, zh->reg);
1510 zh->trans_w_no = zh->trans_no;
1512 zebra_clearError(zh);
1514 zh->records_inserted = 0;
1515 zh->records_updated = 0;
1516 zh->records_deleted = 0;
1517 zh->records_processed = 0;
1519 #if HAVE_SYS_TIMES_H
1523 if (zh->shadow_enable)
1524 rval = res_get (zh->res, "shadow");
1526 for (pass = 0; pass < 2; pass++)
1530 zebra_lock_r (zh->lock_normal);
1531 zebra_lock_w (zh->lock_shadow);
1535 zebra_lock_w (zh->lock_normal);
1536 zebra_lock_w (zh->lock_shadow);
1539 zebra_get_state (zh, &val, &seqno);
1542 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1543 zebra_unlock (zh->lock_shadow);
1544 zebra_unlock (zh->lock_normal);
1548 else if (val == 'd')
1552 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1554 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1555 bf_commitClean (bfs, rval);
1560 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1567 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1571 zebra_set_state (zh, 'd', seqno);
1573 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1574 1, rval ? 1 : 0, zh->res,
1577 zh->reg->seqno = seqno;
1580 zebra_set_state (zh, 'o', seqno);
1582 zebra_unlock (zh->lock_shadow);
1583 zebra_unlock (zh->lock_normal);
1588 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1589 "zebra_begin_trans: cannot open register");
1590 yaz_log(YLOG_FATAL, "%s", zh->errString);
1602 if (zh->trans_no != 1)
1604 zebra_flush_reg (zh);
1607 zebra_clearError(zh);
1608 #if HAVE_SYS_TIMES_H
1614 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1617 if (!zh->lock_normal || !zh->lock_shadow)
1620 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1623 zebra_get_state (zh, &val, &seqno);
1629 else if (seqno != zh->reg->seqno)
1631 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1632 seqno, zh->reg->seqno);
1635 else if (zh->reg->last_val != val)
1637 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1638 val, zh->reg->last_val);
1645 zebra_lock_r (zh->lock_shadow);
1647 zebra_lock_r (zh->lock_normal);
1651 resultSetInvalidate (zh);
1652 zebra_register_close (zh->service, zh->reg);
1654 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1655 0, val == 'c' ? 1 : 0,
1656 zh->res, zh->path_reg);
1659 zebra_unlock (zh->lock_normal);
1660 zebra_unlock (zh->lock_shadow);
1662 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1665 zh->reg->last_val = val;
1666 zh->reg->seqno = seqno;
1668 read_res_for_transaction(zh);
1672 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1674 ZebraTransactionStatus dummy;
1676 yaz_log(log_level, "zebra_end_trans");
1677 return zebra_end_transaction(zh, &dummy);
1680 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1688 yaz_log(log_level, "zebra_end_transaction");
1690 status->processed = 0;
1691 status->inserted = 0;
1692 status->updated = 0;
1693 status->deleted = 0;
1697 if (!zh->res || !zh->reg)
1699 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1700 "zebra_end_trans: no open transaction");
1703 if (zh->trans_no != zh->trans_w_no)
1706 if (zh->trans_no != 0)
1709 /* release read lock */
1711 zebra_unlock (zh->lock_normal);
1712 zebra_unlock (zh->lock_shadow);
1715 { /* release write lock */
1719 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1720 rval = res_get (zh->res, "shadow");
1722 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1724 zebra_flush_reg (zh);
1726 resultSetInvalidate (zh);
1728 zebra_register_close (zh->service, zh->reg);
1731 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1732 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1733 zh->records_processed, zh->records_inserted,
1734 zh->records_updated, zh->records_deleted);
1736 status->processed = (int) zh->records_processed;
1737 status->inserted = (int) zh->records_inserted;
1738 status->updated = (int) zh->records_updated;
1739 status->deleted = (int) zh->records_deleted;
1741 zebra_get_state (zh, &val, &seqno);
1744 BFiles bfs = bfs_create (rval, zh->path_reg);
1745 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1746 bf_commitClean (bfs, rval);
1751 zebra_set_state (zh, 'o', seqno);
1753 zebra_unlock (zh->lock_shadow);
1754 zebra_unlock (zh->lock_normal);
1757 #if HAVE_SYS_TIMES_H
1759 yaz_log (log_level, "user/system: %ld/%ld",
1760 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1761 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1763 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1764 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1769 int zebra_repository_update (ZebraHandle zh, const char *path)
1773 zebra_clearError(zh);
1774 yaz_log (log_level, "updating %s", path);
1775 repositoryUpdate (zh, path);
1779 int zebra_repository_delete (ZebraHandle zh, const char *path)
1783 zebra_clearError(zh);
1784 yaz_log (log_level, "deleting %s", path);
1785 repositoryDelete (zh, path);
1789 int zebra_repository_show (ZebraHandle zh, const char *path)
1793 yaz_log(log_level, "zebra_repository_show");
1794 zebra_clearError(zh);
1795 repositoryShow (zh, path);
1799 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1806 zebra_clearError(zh);
1808 zebra_select_default_database(zh);
1811 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1814 rval = res_get (zh->res, "shadow");
1817 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1821 zebra_lock_w (zh->lock_normal);
1822 zebra_lock_r (zh->lock_shadow);
1824 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1826 zebra_get_state (zh, &val, &seqno);
1829 bf_cache (bfs, rval);
1830 if (bf_commitExists (bfs))
1833 zebra_set_state (zh, 'd', seqno);
1836 zebra_set_state (zh, 'c', seqno);
1838 yaz_log (YLOG_DEBUG, "commit start");
1839 bf_commitExec (bfs);
1844 yaz_log (YLOG_DEBUG, "commit clean");
1845 bf_commitClean (bfs, rval);
1847 zebra_set_state (zh, 'o', seqno);
1851 yaz_log (log_level, "nothing to commit");
1855 zebra_unlock (zh->lock_shadow);
1856 zebra_unlock (zh->lock_normal);
1860 ZEBRA_RES zebra_clean(ZebraHandle zh)
1863 yaz_log(log_level, "zebra_clean");
1864 return zebra_commit_ex(zh, 1);
1867 ZEBRA_RES zebra_commit(ZebraHandle zh)
1870 yaz_log(log_level, "zebra_commit");
1871 return zebra_commit_ex(zh, 0);
1874 ZEBRA_RES zebra_init(ZebraHandle zh)
1879 yaz_log(log_level, "zebra_init");
1880 zebra_clearError(zh);
1882 zebra_select_default_database(zh);
1885 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1886 "cannot select default database");
1889 rval = res_get (zh->res, "shadow");
1891 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1894 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1898 bf_cache (bfs, rval);
1902 zebra_set_state (zh, 'o', 0);
1906 ZEBRA_RES zebra_compact(ZebraHandle zh)
1910 yaz_log(log_level, "zebra_compact");
1911 zebra_clearError(zh);
1914 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1917 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1923 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1926 yaz_log(log_level, "zebra_result");
1927 *code = zh->errCode;
1928 *addinfo = zh->errString;
1931 void zebra_shadow_enable(ZebraHandle zh, int value)
1934 yaz_log(log_level, "zebra_shadow_enable");
1935 zebra_clearError(zh);
1936 zh->shadow_enable = value;
1939 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1943 yaz_log(log_level, "zebra_octet_term_encoding");
1944 zebra_clearError(zh);
1946 if (zh->iconv_to_utf8 != 0)
1947 yaz_iconv_close(zh->iconv_to_utf8);
1948 if (zh->iconv_from_utf8 != 0)
1949 yaz_iconv_close(zh->iconv_from_utf8);
1952 yaz_iconv_open ("UTF-8", encoding);
1953 if (zh->iconv_to_utf8 == 0)
1954 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1955 zh->iconv_from_utf8 =
1956 yaz_iconv_open (encoding, "UTF-8");
1957 if (zh->iconv_to_utf8 == 0)
1958 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1963 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1966 yaz_log(log_level, "zebra_record_encoding");
1967 zebra_clearError(zh);
1968 xfree(zh->record_encoding);
1969 zh->record_encoding = 0;
1971 zh->record_encoding = xstrdup (encoding);
1975 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1980 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
1981 zebra_clearError(zh);
1982 res_set(zh->res, name, value);
1985 const char *zebra_get_resource(ZebraHandle zh,
1986 const char *name, const char *defaultvalue)
1991 assert(defaultvalue);
1992 v = res_get_def (zh->res, name, (char *)defaultvalue);
1993 zebra_clearError(zh);
1994 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
1998 /* moved from zebra_api_ext.c by pop */
1999 /* FIXME: Should this really be public??? -Heikki */
2001 int zebra_trans_no (ZebraHandle zh)
2004 yaz_log(log_level, "zebra_trans_no");
2005 return zh->trans_no;
2008 int zebra_get_shadow_enable (ZebraHandle zh)
2011 yaz_log(log_level, "zebra_get_shadow_enable");
2012 return zh->shadow_enable;
2015 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2018 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2019 zh->shadow_enable = value;
2022 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2023 so that it's identicical to the original api_records_retrieve */
2024 void api_records_retrieve (ZebraHandle zh, ODR stream,
2025 const char *setname, Z_RecordComposition *comp,
2026 oid_value input_format, int num_recs,
2027 ZebraRetrievalRecord *recs)
2029 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2033 /* ---------------------------------------------------------------------------
2034 Record insert(=update), delete
2036 If sysno is provided, then it's used to identify the record.
2037 If not, and match_criteria is provided, then sysno is guessed
2038 If not, and a record is provided, then sysno is got from there
2039 NOTE: Now returns 0 at success and updates sysno, which is an int*
2043 int zebra_add_record(ZebraHandle zh,
2044 const char *buf, int buf_size)
2047 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2050 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2051 const char *recordType,
2052 SYSNO *sysno, const char *match,
2054 const char *buf, int buf_size, int force_update)
2060 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2063 buf_size = strlen(buf);
2065 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2067 res = buffer_extract_record (zh, buf, buf_size,
2068 0, /* delete_flag */
2074 0); /* allow_update */
2075 zebra_end_trans(zh);
2079 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2080 const char *recordType,
2081 SYSNO* sysno, const char *match,
2083 const char *buf, int buf_size,
2091 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2093 if (buf_size < 1) buf_size = strlen(buf);
2095 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2097 res = buffer_extract_record (zh, buf, buf_size,
2098 0, /* delete_flag */
2104 1); /* allow_update */
2105 zebra_end_trans(zh);
2109 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2110 const char *recordType,
2111 SYSNO *sysno, const char *match,
2113 const char *buf, int buf_size,
2120 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2122 if (buf_size < 1) buf_size = strlen(buf);
2124 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2126 res = buffer_extract_record (zh, buf, buf_size,
2127 1, /* delete_flag */
2133 1); /* allow_update */
2134 zebra_end_trans(zh);
2138 /* ---------------------------------------------------------------------------
2142 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2143 const char *setname, zint *numhits)
2146 ZEBRA_RES res = ZEBRA_OK;
2148 ODR odr = odr_createmem(ODR_ENCODE);
2153 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2155 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2159 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2160 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2164 res = zebra_search_RPN(zh, odr, query, setname, &hits);
2168 yaz_log(log_level, "Hits: " ZINT_FORMAT, hits);
2176 /* ---------------------------------------------------------------------------
2177 Sort - a simplified interface, with optional read locks.
2179 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2180 const char *sort_spec,
2181 const char *output_setname,
2182 const char **input_setnames)
2184 int num_input_setnames = 0;
2185 int sort_status = 0;
2186 Z_SortKeySpecList *sort_sequence;
2190 assert(output_setname);
2191 assert(input_setnames);
2192 sort_sequence = yaz_sort_spec (stream, sort_spec);
2193 yaz_log(log_level, "sort (FIXME) ");
2196 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2197 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2201 /* we can do this, since the perl typemap code for char** will
2202 put a NULL at the end of list */
2203 while (input_setnames[num_input_setnames]) num_input_setnames++;
2205 if (zebra_begin_read (zh))
2208 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2209 output_setname, sort_sequence, &sort_status);
2215 /* ---------------------------------------------------------------------------
2216 Get BFS for Zebra system (to make alternative storage methods)
2218 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2221 return zh->reg->bfs;
2226 /* ---------------------------------------------------------------------------
2227 Set limit for search/scan
2229 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2232 zebra_limit_destroy(zh->m_limit);
2233 zh->m_limit = zebra_limit_create(complement_flag, ids);
2238 Set Error code + addinfo
2240 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2243 nmem_reset(zh->nmem_error);
2244 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2247 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2250 sprintf(vstr, ZINT_FORMAT, i);
2253 nmem_reset(zh->nmem_error);
2254 zh->errString = nmem_strdup(zh->nmem_error, vstr);