1 /* $Id: zebraapi.c,v 1.169 2005-05-17 08:50:49 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(log_level, "zebra_start %s",configName);
175 if ((res = res_open (configName, def_res, over_res)))
177 ZebraService zh = xmalloc(sizeof(*zh));
179 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
181 zh->global_res = res;
182 zh->configName = xstrdup(configName);
187 zebra_mutex_cond_init (&zh->session_lock);
188 if (!res_get (zh->global_res, "passwd"))
189 zh->passwd_db = NULL;
192 zh->passwd_db = passwd_db_open ();
194 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
196 passwd_db_file (zh->passwd_db,
197 res_get (zh->global_res, "passwd"));
199 zh->path_root = res_get (zh->global_res, "root");
200 zh->nmem = nmem_create();
201 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
207 void zebra_filter_info(ZebraService zs, void *cd,
208 void (*cb)(void *cd, const char *name))
212 recTypeClass_info(zs->record_classes, cd, cb);
215 void zebra_pidfname(ZebraService zs, char *path)
218 zebra_lock_prefix (zs->global_res, path);
219 strcat(path, "zebrasrv.pid");
222 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
223 int compact_flag, Res res)
225 int page_size = 4096;
226 char resource_str[200];
228 sprintf (resource_str, "dict.%.100s.pagesize", name);
232 v = res_get(res, resource_str);
236 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
239 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
244 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
245 int rw, int useshadow, Res res,
246 const char *reg_path)
248 struct zebra_register *reg;
249 int record_compression = REC_COMPRESS_NONE;
250 const char *recordCompression = 0;
251 const char *profilePath;
256 reg = xmalloc(sizeof(*reg));
259 reg->name = xstrdup (name);
266 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
267 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
269 reg->dh = data1_createx (DATA1_FLAG_XML);
275 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
278 data1_destroy(reg->dh);
284 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
286 bfs_destroy(reg->bfs);
287 data1_destroy(reg->dh);
293 getcwd(cwd, sizeof(cwd)-1);
294 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
295 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
297 data1_set_tabpath (reg->dh, profilePath);
298 data1_set_tabroot (reg->dh, reg_path);
299 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
301 reg->zebra_maps = zebra_maps_open (res, reg_path);
302 reg->rank_classes = NULL;
306 reg->keys.buf_max = 0;
308 reg->keys.codec_handle = iscz1_start();
310 reg->sortKeys.buf = 0;
311 reg->sortKeys.buf_max = 0;
322 reg->key_file_no = 0;
325 zebraRankInstall (reg, rank1_class);
326 zebraRankInstall (reg, rankzv_class);
328 recordCompression = res_get_def (res, "recordCompression", "none");
329 if (!strcmp (recordCompression, "none"))
330 record_compression = REC_COMPRESS_NONE;
331 if (!strcmp (recordCompression, "bzip2"))
332 record_compression = REC_COMPRESS_BZIP2;
334 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
336 yaz_log (YLOG_WARN, "rec_open failed");
341 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
343 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
345 yaz_log (YLOG_WARN, "dict_open failed");
348 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
350 yaz_log (YLOG_WARN, "sortIdx_open failed");
353 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
355 struct ISAMS_M_s isams_m;
356 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
357 key_isams_m(res, &isams_m))))
359 yaz_log (YLOG_WARN, "isams_open failed");
363 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
365 struct ISAMC_M_s isamc_m;
366 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
367 rw, key_isamc_m(res, &isamc_m))))
369 yaz_log (YLOG_WARN, "isamc_open failed");
373 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
375 struct ISAMC_M_s isamc_m;
377 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
378 rw, key_isamc_m(res, &isamc_m), 0)))
380 yaz_log (YLOG_WARN, "isamb_open failed");
384 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
386 struct ISAMC_M_s isamc_m;
388 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
389 rw, key_isamc_m(res, &isamc_m), 1)))
391 yaz_log (YLOG_WARN, "isamb_open failed");
395 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
397 struct ISAMC_M_s isamc_m;
399 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
400 rw, key_isamc_m(res, &isamc_m), -1)))
402 yaz_log (YLOG_WARN, "isamb_open failed");
406 reg->zei = zebraExplain_open (reg->records, reg->dh,
411 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
415 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
419 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
422 yaz_log(log_level, "zebra_admin_shutdown");
423 zebra_clearError(zh);
425 zebra_mutex_cond_lock (&zh->service->session_lock);
426 zh->service->stop_flag = 1;
427 zebra_mutex_cond_unlock (&zh->service->session_lock);
431 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
435 yaz_log(log_level, "zebra_admin_start");
436 zebra_clearError(zh);
438 zebra_mutex_cond_lock (&zs->session_lock);
439 zebra_mutex_cond_unlock (&zs->session_lock);
443 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
447 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
452 zebraExplain_close (reg->zei);
453 dict_close (reg->dict);
455 dict_close (reg->matchDict);
456 sortIdx_close (reg->sortIdx);
458 isams_close (reg->isams);
460 isamc_close (reg->isamc);
462 isamb_close (reg->isamb);
463 rec_close (®->records);
466 recTypes_destroy (reg->recTypes);
467 zebra_maps_close (reg->zebra_maps);
468 zebraRankDestroy (reg);
469 bfs_destroy (reg->bfs);
470 data1_destroy (reg->dh);
472 xfree(reg->sortKeys.buf);
473 xfree(reg->keys.buf);
474 if (reg->keys.codec_handle)
475 iscz1_stop(reg->keys.codec_handle);
481 ZEBRA_RES zebra_stop(ZebraService zs)
485 yaz_log (log_level, "zebra_stop");
489 zebra_close (zs->sessions);
492 zebra_mutex_cond_destroy (&zs->session_lock);
495 passwd_db_close (zs->passwd_db);
497 recTypeClass_destroy(zs->record_classes);
498 nmem_destroy(zs->nmem);
499 res_close (zs->global_res);
500 xfree(zs->configName);
505 ZEBRA_RES zebra_close (ZebraHandle zh)
508 struct zebra_session **sp;
511 yaz_log(log_level, "zebra_close");
518 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
519 resultSetDestroy (zh, -1, 0, 0);
522 zebra_register_close (zh->service, zh->reg);
523 zebra_close_res (zh);
525 xfree(zh->record_encoding);
527 for (i = 0; i < zh->num_basenames; i++)
528 xfree(zh->basenames[i]);
529 xfree(zh->basenames);
531 if (zh->iconv_to_utf8 != 0)
532 yaz_iconv_close (zh->iconv_to_utf8);
533 if (zh->iconv_from_utf8 != 0)
534 yaz_iconv_close (zh->iconv_from_utf8);
536 zebra_mutex_cond_lock (&zs->session_lock);
537 zebra_lock_destroy (zh->lock_normal);
538 zebra_lock_destroy (zh->lock_shadow);
550 zebra_mutex_cond_unlock (&zs->session_lock);
552 xfree(zh->user_perm);
553 zh->service = 0; /* more likely to trigger an assert */
555 zebra_limit_destroy(zh->m_limit);
557 nmem_destroy(zh->nmem_error);
564 struct map_baseinfo {
570 char **new_basenames;
574 static Res zebra_open_res (ZebraHandle zh)
583 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
584 res = res_open (fname, zh->service->global_res, 0);
586 res = zh->service->global_res;
588 else if (*zh->reg_name == 0)
590 res = zh->service->global_res;
594 yaz_log (YLOG_WARN, "no register root specified");
595 return 0; /* no path for register - fail! */
600 static void zebra_close_res (ZebraHandle zh)
604 if (zh->res != zh->service->global_res)
609 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
613 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
617 assert (zh->reg == 0);
618 assert (*zh->reg_name == 0);
624 resultSetInvalidate (zh);
625 zebra_register_close (zh->service, zh->reg);
631 zh->reg_name = xstrdup (new_reg);
635 if (zh->service->path_root)
637 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
638 strlen(zh->reg_name) + 3);
639 strcpy (zh->path_reg, zh->service->path_root);
642 strcat (zh->path_reg, "/");
643 strcat (zh->path_reg, zh->reg_name);
646 zh->res = zebra_open_res (zh);
649 zebra_lock_destroy (zh->lock_normal);
653 zebra_lock_destroy (zh->lock_shadow);
659 const char *lock_area =res_get (zh->res, "lockDir");
661 if (!lock_area && zh->path_reg)
662 res_set (zh->res, "lockDir", zh->path_reg);
663 sprintf (fname, "norm.%s.LCK", zh->reg_name);
665 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
667 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
669 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
671 if (!zh->lock_normal || !zh->lock_shadow)
675 zebra_lock_destroy(zh->lock_normal);
680 zebra_lock_destroy(zh->lock_shadow);
688 void map_basenames_func (void *vp, const char *name, const char *value)
690 struct map_baseinfo *p = (struct map_baseinfo *) vp;
692 char fromdb[128], todb[8][128];
699 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
700 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
701 todb[5], todb[6], todb[7]);
705 for (i = 0; i<p->num_bases; i++)
706 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
709 for (i = 0; i < no; i++)
711 if (p->new_num_bases == p->new_num_max)
713 p->new_basenames[(p->new_num_bases)++] =
714 nmem_strdup (p->mem, todb[i]);
720 int zebra_select_default_database(ZebraHandle zh)
724 /* no database has been selected - so we select based on
725 resource setting (including group)
727 const char *group = res_get(zh->service->global_res, "group");
728 const char *v = res_get_prefix(zh->service->global_res,
729 "database", group, "Default");
730 return zebra_select_database(zh, v);
735 void map_basenames (ZebraHandle zh, ODR stream,
736 int *num_bases, char ***basenames)
738 struct map_baseinfo info;
739 struct map_baseinfo *p = &info;
742 yaz_log(log_level, "map_basenames ");
747 info.num_bases = *num_bases;
748 info.basenames = *basenames;
749 info.new_num_max = 128;
750 info.new_num_bases = 0;
751 info.new_basenames = (char **)
752 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
753 info.mem = stream->mem;
755 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
757 for (i = 0; i<p->num_bases; i++)
758 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
760 p->new_basenames[(p->new_num_bases)++] =
761 nmem_strdup (p->mem, p->basenames[i]);
763 *num_bases = info.new_num_bases;
764 *basenames = info.new_basenames;
765 for (i = 0; i<*num_bases; i++)
766 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
769 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
772 yaz_log(log_level, "zebra_select_database %s",basename);
774 return zebra_select_databases (zh, 1, &basename);
777 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
778 const char **basenames)
787 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
788 num_bases,basenames[0]);
793 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
796 for (i = 0; i < zh->num_basenames; i++)
797 xfree(zh->basenames[i]);
798 xfree(zh->basenames);
800 zh->num_basenames = num_bases;
801 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
802 for (i = 0; i < zh->num_basenames; i++)
803 zh->basenames[i] = xstrdup (basenames[i]);
805 cp = strrchr(basenames[0], '/');
808 len = cp - basenames[0];
809 new_reg = xmalloc(len + 1);
810 memcpy (new_reg, basenames[0], len);
814 new_reg = xstrdup ("");
815 for (i = 1; i<num_bases; i++)
819 cp1 = strrchr (basenames[i], '/');
824 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
827 if (len != cp1 - basenames[i] ||
828 memcmp (basenames[i], new_reg, len))
830 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
838 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
843 zebra_select_register (zh, new_reg);
847 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
850 if (!zh->lock_normal || !zh->lock_shadow)
852 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
858 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
859 const char *setname, zint *hits)
867 yaz_log(log_level, "zebra_search_rpn");
868 zebra_clearError(zh);
872 if (zebra_begin_read(zh) == ZEBRA_FAIL)
875 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
876 zh->num_basenames, zh->basenames, setname);
882 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
884 Z_RecordComposition *comp,
885 oid_value input_format, int num_recs,
886 ZebraRetrievalRecord *recs)
888 ZebraMetaRecord *poset;
890 ZEBRA_RES ret = ZEBRA_OK;
898 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
900 zebra_clearError(zh);
903 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
908 if (zebra_begin_read (zh) == ZEBRA_FAIL)
911 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
912 for (i = 0; i<num_recs; i++)
913 pos_array[i] = recs[i].position;
914 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
917 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
918 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
924 for (i = 0; i<num_recs; i++)
929 recs[i].format = VAL_SUTRS;
930 recs[i].len = strlen(poset[i].term);
931 recs[i].buf = poset[i].term;
932 recs[i].base = poset[i].db;
934 else if (poset[i].sysno)
939 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
940 stream, input_format, comp,
941 &recs[i].format, &buf, &len,
942 &recs[i].base, &recs[i].errString);
946 recs[i].buf = (char*) odr_malloc(stream, len);
947 memcpy(recs[i].buf, buf, len);
951 recs[i].score = poset[i].score;
952 recs[i].sysno = poset[i].sysno;
956 if (ret == ZEBRA_OK) /* only need to set it once */
957 zebra_setError_zint(zh,
958 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
964 zebra_meta_records_destroy(zh, poset, num_recs);
971 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
973 int *num_entries, ZebraScanEntry **entries,
976 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
977 Z_AttributesPlusTerm *zapt;
981 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
984 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
987 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
988 position, num_entries, entries, is_partial);
989 yaz_pqf_destroy (pqf_parser);
993 ZEBRA_RES zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
994 oid_value attributeset,
996 int *num_entries, ZebraScanEntry **entries,
1004 assert(num_entries);
1007 yaz_log(log_level, "zebra_scan");
1008 zebra_clearError(zh);
1009 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1015 res = rpn_scan (zh, stream, zapt, attributeset,
1016 zh->num_basenames, zh->basenames, position,
1017 num_entries, entries, is_partial, 0, 0);
1018 zebra_end_read (zh);
1022 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1023 int num_input_setnames, const char **input_setnames,
1024 const char *output_setname,
1025 Z_SortKeySpecList *sort_sequence,
1031 assert(num_input_setnames>0);
1032 assert(input_setnames);
1033 assert(sort_sequence);
1034 assert(sort_status);
1035 yaz_log(log_level, "zebra_sort");
1036 zebra_clearError(zh);
1037 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1039 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1040 output_setname, sort_sequence, sort_status);
1045 int zebra_deleteResultSet(ZebraHandle zh, int function,
1046 int num_setnames, char **setnames,
1052 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1053 zebra_clearError(zh);;
1054 if (zebra_begin_read(zh))
1055 return Z_DeleteStatus_systemProblemAtTarget;
1058 case Z_DeleteResultSetRequest_list:
1059 assert(num_setnames>0);
1061 resultSetDestroy (zh, num_setnames, setnames, statuses);
1063 case Z_DeleteResultSetRequest_all:
1064 resultSetDestroy (zh, -1, 0, statuses);
1067 zebra_end_read (zh);
1068 status = Z_DeleteStatus_success;
1069 for (i = 0; i<num_setnames; i++)
1070 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1071 status = statuses[i];
1075 int zebra_errCode (ZebraHandle zh)
1079 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1082 yaz_log(log_level, "zebra_errCode: o");
1086 const char *zebra_errString (ZebraHandle zh)
1090 e= diagbib1_str (zh->errCode);
1091 yaz_log(log_level, "zebra_errString: %s",e);
1095 char *zebra_errAdd (ZebraHandle zh)
1100 yaz_log(log_level, "zebra_errAdd: %s",a);
1104 void zebra_clearError(ZebraHandle zh)
1113 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1120 zebra_clearError(zh);
1123 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1124 p = res_get(zs->global_res, u);
1125 xfree(zh->user_perm);
1126 zh->user_perm = xstrdup(p ? p : "r");
1128 /* users that don't require a password .. */
1129 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1132 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1137 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1138 const char *record_type)
1141 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1142 database, record_type);
1143 zebra_clearError(zh);
1144 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1146 return zebra_begin_trans(zh, 1);
1149 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1152 yaz_log(log_level, "zebra_admin_import_end");
1153 zebra_clearError(zh);
1154 return zebra_end_trans(zh);
1157 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1159 ZEBRA_RES res = ZEBRA_OK;
1163 yaz_log(log_level, "zebra_admin_import_segment");
1164 zebra_clearError(zh);
1165 for (i = 0; i<segment->num_segmentRecords; i++)
1167 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1169 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1171 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1172 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1174 Odr_oct *oct = fragment->u.notExternallyTagged;
1177 if ( zebra_update_record(zh,
1178 0, /* record Type */
1191 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1192 const char *rec_buf,
1194 const char *recid_buf, size_t recid_len,
1196 /* 1 = insert. Fail it already exists */
1197 /* 2 = replace. Fail it does not exist */
1198 /* 3 = delete. Fail if does not exist */
1199 /* 4 = update. Insert/replace */
1206 assert(action>0 && action <=4);
1209 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1210 zebra_clearError(zh);
1212 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1215 memcpy (recid_z, recid_buf, recid_len);
1216 recid_z[recid_len] = 0;
1218 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1221 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1224 if (action == 1) /* fail if insert */
1226 zebra_end_trans(zh);
1230 memcpy (&sysno, rinfo+1, sizeof(sysno));
1234 if (action == 2 || action == 3) /* fail if delete or update */
1236 zebra_end_trans(zh);
1239 action = 1; /* make it an insert (if it's an update).. */
1241 res = buffer_extract_record (zh, rec_buf, rec_len,
1242 action == 3 ? 1 : 0 /* delete flag */,
1248 0, /* force update */
1249 1 /* allow update */
1253 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1255 else if (action == 3)
1257 dict_delete (zh->reg->matchDict, recid_z);
1259 zebra_end_trans(zh);
1263 int delete_w_handle(const char *info, void *handle)
1265 ZebraHandle zh = (ZebraHandle) handle;
1269 if (*info == sizeof(pos))
1271 memcpy (&pos, info+1, sizeof(pos));
1272 isamb_unlink(zh->reg->isamb, pos);
1277 static int delete_SU_handle(void *handle, int ord)
1279 ZebraHandle zh = (ZebraHandle) handle;
1283 ord_len = key_SU_encode (ord, ord_buf);
1284 ord_buf[ord_len] = '\0';
1286 assert (zh->reg->isamb);
1287 dict_delete_subtree(zh->reg->dict, ord_buf,
1288 zh, delete_w_handle);
1292 ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database)
1294 ZEBRA_RES ret = ZEBRA_OK;
1296 yaz_log(log_level, "zebra_drop_database");
1297 zebra_clearError(zh);
1299 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1301 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1305 zebraExplain_curDatabase (zh->reg->zei, database);
1307 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1308 zebraExplain_removeDatabase(zh->reg->zei, zh);
1312 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1315 zebra_end_trans (zh);
1319 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database)
1322 yaz_log(log_level, "zebra_create_database %s", database);
1324 zebra_clearError(zh);
1326 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1328 if (zebra_begin_trans (zh, 1))
1331 /* announce database */
1332 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1333 /* explainDatabase */))
1335 zebra_end_trans (zh);
1336 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, database);
1339 return zebra_end_trans (zh);
1342 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1343 const char *input_str, int input_len,
1344 char *output_str, int output_len)
1350 yaz_log(log_level, "zebra_string_norm ");
1351 zebra_clearError(zh);
1352 if (!zh->reg->zebra_maps)
1354 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1355 input_str, input_len);
1358 if (wrbuf_len(wrbuf) >= output_len)
1360 if (wrbuf_len(wrbuf))
1361 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1362 output_str[wrbuf_len(wrbuf)] = '\0';
1363 return wrbuf_len(wrbuf);
1366 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1368 char state_fname[256];
1373 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1374 zebra_clearError(zh);
1376 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1377 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1378 f = fopen (fname, "w");
1380 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1381 fprintf (f, "%c %d %ld\n", val, seqno, p);
1386 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1388 char state_fname[256];
1393 yaz_log(log_level, "zebra_get_state ");
1394 zebra_clearError(zh);
1395 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1396 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1397 f = fopen (fname, "r");
1403 fscanf (f, "%c %d", val, seqno);
1409 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1411 return zebra_begin_trans(zh, 0);
1414 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1416 return zebra_end_trans(zh);
1419 static void read_res_for_transaction(ZebraHandle zh)
1421 const char *group = res_get(zh->res, "group");
1423 /* FIXME - do we still use groups ?? */
1425 zh->m_group = group;
1426 v = res_get_prefix(zh->res, "followLinks", group, "1");
1427 zh->m_follow_links = atoi(v);
1429 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1430 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1432 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1433 zh->m_store_keys = atoi(v);
1435 v = res_get_prefix(zh->res, "storeData", group, "1");
1436 zh->m_store_data = atoi(v);
1438 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1439 zh->m_explain_database = atoi(v);
1441 v = res_get_prefix(zh->res, "openRW", group, "1");
1442 zh->m_flag_rw = atoi(v);
1444 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1445 zh->m_file_verbose_limit = atoi(v);
1448 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1451 zebra_select_default_database(zh);
1454 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1455 "zebra_begin_trans: no database selected");
1459 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1463 if (rw && !strchr(zh->user_perm, 'w'))
1467 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1479 const char *rval = 0;
1484 read_res_for_transaction(zh);
1487 if (zh->trans_no != 1)
1489 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1490 "zebra_begin_trans: no write trans within read");
1495 resultSetInvalidate (zh);
1496 zebra_register_close (zh->service, zh->reg);
1498 zh->trans_w_no = zh->trans_no;
1500 zebra_clearError(zh);
1502 zh->records_inserted = 0;
1503 zh->records_updated = 0;
1504 zh->records_deleted = 0;
1505 zh->records_processed = 0;
1507 #if HAVE_SYS_TIMES_H
1511 if (zh->shadow_enable)
1512 rval = res_get (zh->res, "shadow");
1514 for (pass = 0; pass < 2; pass++)
1518 zebra_lock_r (zh->lock_normal);
1519 zebra_lock_w (zh->lock_shadow);
1523 zebra_lock_w (zh->lock_normal);
1524 zebra_lock_w (zh->lock_shadow);
1527 zebra_get_state (zh, &val, &seqno);
1530 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1531 zebra_unlock (zh->lock_shadow);
1532 zebra_unlock (zh->lock_normal);
1536 else if (val == 'd')
1540 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1542 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1543 bf_commitClean (bfs, rval);
1548 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1555 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1559 zebra_set_state (zh, 'd', seqno);
1561 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1562 1, rval ? 1 : 0, zh->res,
1565 zh->reg->seqno = seqno;
1568 zebra_set_state (zh, 'o', seqno);
1570 zebra_unlock (zh->lock_shadow);
1571 zebra_unlock (zh->lock_normal);
1576 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1577 "zebra_begin_trans: cannot open register");
1578 yaz_log(YLOG_FATAL, "%s", zh->errString);
1590 if (zh->trans_no != 1)
1592 zebra_flush_reg (zh);
1595 zebra_clearError(zh);
1596 #if HAVE_SYS_TIMES_H
1602 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1605 if (!zh->lock_normal || !zh->lock_shadow)
1608 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1611 zebra_get_state (zh, &val, &seqno);
1617 else if (seqno != zh->reg->seqno)
1619 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1620 seqno, zh->reg->seqno);
1623 else if (zh->reg->last_val != val)
1625 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1626 val, zh->reg->last_val);
1633 zebra_lock_r (zh->lock_shadow);
1635 zebra_lock_r (zh->lock_normal);
1639 resultSetInvalidate (zh);
1640 zebra_register_close (zh->service, zh->reg);
1642 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1643 0, val == 'c' ? 1 : 0,
1644 zh->res, zh->path_reg);
1647 zebra_unlock (zh->lock_normal);
1648 zebra_unlock (zh->lock_shadow);
1650 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1653 zh->reg->last_val = val;
1654 zh->reg->seqno = seqno;
1656 read_res_for_transaction(zh);
1660 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1662 ZebraTransactionStatus dummy;
1664 yaz_log(log_level, "zebra_end_trans");
1665 return zebra_end_transaction(zh, &dummy);
1668 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1676 yaz_log(log_level, "zebra_end_transaction");
1678 status->processed = 0;
1679 status->inserted = 0;
1680 status->updated = 0;
1681 status->deleted = 0;
1685 if (!zh->res || !zh->reg)
1687 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1688 "zebra_end_trans: no open transaction");
1691 if (zh->trans_no != zh->trans_w_no)
1694 if (zh->trans_no != 0)
1697 /* release read lock */
1699 zebra_unlock (zh->lock_normal);
1700 zebra_unlock (zh->lock_shadow);
1703 { /* release write lock */
1707 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1708 rval = res_get (zh->res, "shadow");
1710 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1712 zebra_flush_reg (zh);
1714 resultSetInvalidate (zh);
1716 zebra_register_close (zh->service, zh->reg);
1719 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1720 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1721 zh->records_processed, zh->records_inserted,
1722 zh->records_updated, zh->records_deleted);
1724 status->processed = (int) zh->records_processed;
1725 status->inserted = (int) zh->records_inserted;
1726 status->updated = (int) zh->records_updated;
1727 status->deleted = (int) zh->records_deleted;
1729 zebra_get_state (zh, &val, &seqno);
1732 BFiles bfs = bfs_create (rval, zh->path_reg);
1733 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1734 bf_commitClean (bfs, rval);
1739 zebra_set_state (zh, 'o', seqno);
1741 zebra_unlock (zh->lock_shadow);
1742 zebra_unlock (zh->lock_normal);
1745 #if HAVE_SYS_TIMES_H
1747 yaz_log (log_level, "user/system: %ld/%ld",
1748 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1749 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1751 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1752 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1757 int zebra_repository_update (ZebraHandle zh, const char *path)
1761 zebra_clearError(zh);
1762 yaz_log (log_level, "updating %s", path);
1763 repositoryUpdate (zh, path);
1767 int zebra_repository_delete (ZebraHandle zh, const char *path)
1771 zebra_clearError(zh);
1772 yaz_log (log_level, "deleting %s", path);
1773 repositoryDelete (zh, path);
1777 int zebra_repository_show (ZebraHandle zh, const char *path)
1781 yaz_log(log_level, "zebra_repository_show");
1782 zebra_clearError(zh);
1783 repositoryShow (zh, path);
1787 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1794 zebra_clearError(zh);
1796 zebra_select_default_database(zh);
1799 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1802 rval = res_get (zh->res, "shadow");
1805 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1809 zebra_lock_w (zh->lock_normal);
1810 zebra_lock_r (zh->lock_shadow);
1812 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1814 zebra_get_state (zh, &val, &seqno);
1817 bf_cache (bfs, rval);
1818 if (bf_commitExists (bfs))
1821 zebra_set_state (zh, 'd', seqno);
1824 zebra_set_state (zh, 'c', seqno);
1826 yaz_log (YLOG_DEBUG, "commit start");
1827 bf_commitExec (bfs);
1832 yaz_log (YLOG_DEBUG, "commit clean");
1833 bf_commitClean (bfs, rval);
1835 zebra_set_state (zh, 'o', seqno);
1839 yaz_log (log_level, "nothing to commit");
1843 zebra_unlock (zh->lock_shadow);
1844 zebra_unlock (zh->lock_normal);
1848 ZEBRA_RES zebra_clean(ZebraHandle zh)
1851 yaz_log(log_level, "zebra_clean");
1852 return zebra_commit_ex(zh, 1);
1855 ZEBRA_RES zebra_commit(ZebraHandle zh)
1858 yaz_log(log_level, "zebra_commit");
1859 return zebra_commit_ex(zh, 0);
1862 ZEBRA_RES zebra_init(ZebraHandle zh)
1867 yaz_log(log_level, "zebra_init");
1868 zebra_clearError(zh);
1870 zebra_select_default_database(zh);
1873 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1874 "cannot select default database");
1877 rval = res_get (zh->res, "shadow");
1879 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1882 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1886 bf_cache (bfs, rval);
1890 zebra_set_state (zh, 'o', 0);
1894 ZEBRA_RES zebra_compact(ZebraHandle zh)
1898 yaz_log(log_level, "zebra_compact");
1899 zebra_clearError(zh);
1902 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1905 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1911 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1914 yaz_log(log_level, "zebra_result");
1915 *code = zh->errCode;
1916 *addinfo = zh->errString;
1919 void zebra_shadow_enable(ZebraHandle zh, int value)
1922 yaz_log(log_level, "zebra_shadow_enable");
1923 zebra_clearError(zh);
1924 zh->shadow_enable = value;
1927 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1931 yaz_log(log_level, "zebra_octet_term_encoding");
1932 zebra_clearError(zh);
1934 if (zh->iconv_to_utf8 != 0)
1935 yaz_iconv_close(zh->iconv_to_utf8);
1936 if (zh->iconv_from_utf8 != 0)
1937 yaz_iconv_close(zh->iconv_from_utf8);
1940 yaz_iconv_open ("UTF-8", encoding);
1941 if (zh->iconv_to_utf8 == 0)
1942 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1943 zh->iconv_from_utf8 =
1944 yaz_iconv_open (encoding, "UTF-8");
1945 if (zh->iconv_to_utf8 == 0)
1946 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1951 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1954 yaz_log(log_level, "zebra_record_encoding");
1955 zebra_clearError(zh);
1956 xfree(zh->record_encoding);
1957 zh->record_encoding = 0;
1959 zh->record_encoding = xstrdup (encoding);
1963 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1968 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
1969 zebra_clearError(zh);
1970 res_set(zh->res, name, value);
1973 const char *zebra_get_resource(ZebraHandle zh,
1974 const char *name, const char *defaultvalue)
1979 assert(defaultvalue);
1980 v = res_get_def (zh->res, name, (char *)defaultvalue);
1981 zebra_clearError(zh);
1982 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
1986 /* moved from zebra_api_ext.c by pop */
1987 /* FIXME: Should this really be public??? -Heikki */
1989 int zebra_trans_no (ZebraHandle zh)
1992 yaz_log(log_level, "zebra_trans_no");
1993 return zh->trans_no;
1996 int zebra_get_shadow_enable (ZebraHandle zh)
1999 yaz_log(log_level, "zebra_get_shadow_enable");
2000 return zh->shadow_enable;
2003 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2006 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2007 zh->shadow_enable = value;
2010 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2011 so that it's identicical to the original api_records_retrieve */
2012 void api_records_retrieve (ZebraHandle zh, ODR stream,
2013 const char *setname, Z_RecordComposition *comp,
2014 oid_value input_format, int num_recs,
2015 ZebraRetrievalRecord *recs)
2017 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2021 /* ---------------------------------------------------------------------------
2022 Record insert(=update), delete
2024 If sysno is provided, then it's used to identify the record.
2025 If not, and match_criteria is provided, then sysno is guessed
2026 If not, and a record is provided, then sysno is got from there
2027 NOTE: Now returns 0 at success and updates sysno, which is an int*
2031 int zebra_add_record(ZebraHandle zh,
2032 const char *buf, int buf_size)
2035 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2038 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2039 const char *recordType,
2040 SYSNO *sysno, const char *match,
2042 const char *buf, int buf_size, int force_update)
2048 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2051 buf_size = strlen(buf);
2053 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2055 res = buffer_extract_record (zh, buf, buf_size,
2056 0, /* delete_flag */
2062 0); /* allow_update */
2063 zebra_end_trans(zh);
2067 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2068 const char *recordType,
2069 SYSNO* sysno, const char *match,
2071 const char *buf, int buf_size,
2079 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2081 if (buf_size < 1) buf_size = strlen(buf);
2083 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2085 res = buffer_extract_record (zh, buf, buf_size,
2086 0, /* delete_flag */
2092 1); /* allow_update */
2093 zebra_end_trans(zh);
2097 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2098 const char *recordType,
2099 SYSNO *sysno, const char *match,
2101 const char *buf, int buf_size,
2108 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2110 if (buf_size < 1) buf_size = strlen(buf);
2112 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2114 res = buffer_extract_record (zh, buf, buf_size,
2115 1, /* delete_flag */
2121 1); /* allow_update */
2122 zebra_end_trans(zh);
2126 /* ---------------------------------------------------------------------------
2130 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2131 const char *setname, zint *numhits)
2134 ZEBRA_RES res = ZEBRA_OK;
2136 ODR odr = odr_createmem(ODR_ENCODE);
2141 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2143 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2147 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2148 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2152 res = zebra_search_RPN(zh, odr, query, setname, &hits);
2156 yaz_log(log_level, "Hits: " ZINT_FORMAT, hits);
2164 /* ---------------------------------------------------------------------------
2165 Sort - a simplified interface, with optional read locks.
2167 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2168 const char *sort_spec,
2169 const char *output_setname,
2170 const char **input_setnames)
2172 int num_input_setnames = 0;
2173 int sort_status = 0;
2174 Z_SortKeySpecList *sort_sequence;
2178 assert(output_setname);
2179 assert(input_setnames);
2180 sort_sequence = yaz_sort_spec (stream, sort_spec);
2181 yaz_log(log_level, "sort (FIXME) ");
2184 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2185 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2189 /* we can do this, since the perl typemap code for char** will
2190 put a NULL at the end of list */
2191 while (input_setnames[num_input_setnames]) num_input_setnames++;
2193 if (zebra_begin_read (zh))
2196 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2197 output_setname, sort_sequence, &sort_status);
2203 /* ---------------------------------------------------------------------------
2204 Get BFS for Zebra system (to make alternative storage methods)
2206 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2209 return zh->reg->bfs;
2214 /* ---------------------------------------------------------------------------
2215 Set limit for search/scan
2217 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2220 zebra_limit_destroy(zh->m_limit);
2221 zh->m_limit = zebra_limit_create(complement_flag, ids);
2226 Set Error code + addinfo
2228 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2231 nmem_reset(zh->nmem_error);
2232 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2235 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2238 sprintf(vstr, ZINT_FORMAT, i);
2241 nmem_reset(zh->nmem_error);
2242 zh->errString = nmem_strdup(zh->nmem_error, vstr);