1 /* $Id: zebraapi.c,v 1.180 2005-08-09 09:35:25 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 #include <yaz/diagbib1.h>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
40 #include <idzebra/api.h>
42 #define DEFAULT_APPROX_LIMIT 2000000000
44 /* simple asserts to validate the most essential input args */
45 #define ASSERTZH assert(zh && zh->service)
46 #define ASSERTZHRES assert(zh && zh->service && zh->res)
47 #define ASSERTZS assert(zs)
49 static int log_level = 0;
50 static int log_level_initialized = 0;
52 static Res zebra_open_res (ZebraHandle zh);
53 static void zebra_close_res (ZebraHandle zh);
55 static void zebra_chdir (ZebraService zs)
59 yaz_log(log_level, "zebra_chdir");
60 dir = res_get (zs->global_res, "chdir");
63 yaz_log (YLOG_DEBUG, "chdir %s", dir);
71 static void zebra_flush_reg (ZebraHandle zh)
74 yaz_log(log_level, "zebra_flush_reg");
76 zebraExplain_flush (zh->reg->zei, zh);
78 extract_flushWriteKeys (zh, 1 /* final */);
79 zebra_index_merge (zh );
82 static struct zebra_register *zebra_register_open (ZebraService zs,
84 int rw, int useshadow,
86 const char *reg_path);
87 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
89 ZebraHandle zebra_open (ZebraService zs)
92 const char *default_encoding;
93 if (!log_level_initialized)
95 log_level = yaz_log_module_level("zebraapi");
96 log_level_initialized = 1;
99 yaz_log(log_level, "zebra_open");
104 zh = (ZebraHandle) xmalloc(sizeof(*zh));
105 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
108 zh->reg = 0; /* no register attached yet */
116 zh->reg_name = xstrdup ("");
118 zh->num_basenames = 0;
121 zh->approx_limit = DEFAULT_APPROX_LIMIT;
128 zh->shadow_enable = 1;
130 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
133 yaz_iconv_open ("UTF-8", default_encoding);
134 if (zh->iconv_to_utf8 == 0)
135 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
137 zh->iconv_from_utf8 =
138 yaz_iconv_open (default_encoding, "UTF-8");
139 if (zh->iconv_to_utf8 == 0)
140 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
143 zh->record_encoding = 0;
145 zebra_mutex_cond_lock (&zs->session_lock);
147 zh->next = zs->sessions;
150 zebra_mutex_cond_unlock (&zs->session_lock);
152 zh->store_data_buf = 0;
154 zh->m_limit = zebra_limit_create(1, 0);
156 zh->nmem_error = nmem_create();
161 ZebraService zebra_start (const char *configName)
163 return zebra_start_res(configName, 0, 0);
166 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
170 if (!log_level_initialized)
172 log_level = yaz_log_module_level("zebraapi");
173 log_level_initialized = 1;
176 yaz_log(YLOG_LOG, "zebra_start %s %s",configName, ZEBRAVER);
179 if ((res = res_open (configName, def_res, over_res)))
181 const char *passwd_plain = 0;
182 const char *passwd_encrypt = 0;
183 ZebraService zh = xmalloc(sizeof(*zh));
185 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
187 zh->global_res = res;
188 zh->configName = xstrdup(configName);
193 zebra_mutex_cond_init (&zh->session_lock);
194 passwd_plain = res_get (zh->global_res, "passwd");
195 passwd_encrypt = res_get (zh->global_res, "passwd.c");
197 if (!passwd_plain && !passwd_encrypt)
198 zh->passwd_db = NULL;
201 zh->passwd_db = passwd_db_open();
203 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
207 passwd_db_file_plain(zh->passwd_db, passwd_plain);
209 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
212 zh->path_root = res_get (zh->global_res, "root");
213 zh->nmem = nmem_create();
214 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
220 void zebra_filter_info(ZebraService zs, void *cd,
221 void (*cb)(void *cd, const char *name))
225 recTypeClass_info(zs->record_classes, cd, cb);
228 void zebra_pidfname(ZebraService zs, char *path)
231 zebra_lock_prefix (zs->global_res, path);
232 strcat(path, "zebrasrv.pid");
235 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
236 int compact_flag, Res res)
238 int page_size = 4096;
239 char resource_str[200];
240 sprintf (resource_str, "dict.%.100s.pagesize", name);
244 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
245 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
247 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
251 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
252 int rw, int useshadow, Res res,
253 const char *reg_path)
255 struct zebra_register *reg;
256 int record_compression = REC_COMPRESS_NONE;
257 const char *recordCompression = 0;
258 const char *profilePath;
263 reg = xmalloc(sizeof(*reg));
266 reg->name = xstrdup (name);
273 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
274 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
276 reg->dh = data1_createx (DATA1_FLAG_XML);
282 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
285 data1_destroy(reg->dh);
291 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
293 bfs_destroy(reg->bfs);
294 data1_destroy(reg->dh);
300 getcwd(cwd, sizeof(cwd)-1);
301 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
302 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
304 data1_set_tabpath (reg->dh, profilePath);
305 data1_set_tabroot (reg->dh, reg_path);
306 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
308 reg->zebra_maps = zebra_maps_open (res, reg_path);
309 reg->rank_classes = NULL;
313 reg->keys.buf_max = 0;
315 reg->keys.codec_handle = iscz1_start();
317 reg->sortKeys.buf = 0;
318 reg->sortKeys.buf_max = 0;
320 reg->sortKeys.codec_handle = iscz1_start();
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);
487 if (reg->sortKeys.codec_handle)
488 iscz1_stop(reg->sortKeys.codec_handle);
495 ZEBRA_RES zebra_stop(ZebraService zs)
499 yaz_log (log_level, "zebra_stop");
503 zebra_close (zs->sessions);
506 zebra_mutex_cond_destroy (&zs->session_lock);
509 passwd_db_close (zs->passwd_db);
511 recTypeClass_destroy(zs->record_classes);
512 nmem_destroy(zs->nmem);
513 res_close (zs->global_res);
514 xfree(zs->configName);
519 ZEBRA_RES zebra_close (ZebraHandle zh)
522 struct zebra_session **sp;
525 yaz_log(log_level, "zebra_close");
532 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
533 resultSetDestroy (zh, -1, 0, 0);
536 zebra_register_close (zh->service, zh->reg);
537 zebra_close_res (zh);
539 xfree(zh->record_encoding);
541 for (i = 0; i < zh->num_basenames; i++)
542 xfree(zh->basenames[i]);
543 xfree(zh->basenames);
545 if (zh->iconv_to_utf8 != 0)
546 yaz_iconv_close (zh->iconv_to_utf8);
547 if (zh->iconv_from_utf8 != 0)
548 yaz_iconv_close (zh->iconv_from_utf8);
550 zebra_mutex_cond_lock (&zs->session_lock);
551 zebra_lock_destroy (zh->lock_normal);
552 zebra_lock_destroy (zh->lock_shadow);
564 zebra_mutex_cond_unlock (&zs->session_lock);
566 xfree(zh->user_perm);
567 zh->service = 0; /* more likely to trigger an assert */
569 zebra_limit_destroy(zh->m_limit);
571 nmem_destroy(zh->nmem_error);
578 struct map_baseinfo {
584 char **new_basenames;
588 static Res zebra_open_res (ZebraHandle zh)
597 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
598 res = res_open (fname, zh->service->global_res, 0);
600 res = zh->service->global_res;
602 else if (*zh->reg_name == 0)
604 res = zh->service->global_res;
608 yaz_log (YLOG_WARN, "no register root specified");
609 return 0; /* no path for register - fail! */
614 static void zebra_close_res (ZebraHandle zh)
618 if (zh->res != zh->service->global_res)
623 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
627 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
631 assert (zh->reg == 0);
632 assert (*zh->reg_name == 0);
638 resultSetInvalidate (zh);
639 zebra_register_close (zh->service, zh->reg);
645 zh->reg_name = xstrdup (new_reg);
649 if (zh->service->path_root)
651 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
652 strlen(zh->reg_name) + 3);
653 strcpy (zh->path_reg, zh->service->path_root);
656 strcat (zh->path_reg, "/");
657 strcat (zh->path_reg, zh->reg_name);
660 zh->res = zebra_open_res (zh);
663 zebra_lock_destroy (zh->lock_normal);
667 zebra_lock_destroy (zh->lock_shadow);
673 const char *lock_area =res_get (zh->res, "lockDir");
675 if (!lock_area && zh->path_reg)
676 res_set (zh->res, "lockDir", zh->path_reg);
677 sprintf (fname, "norm.%s.LCK", zh->reg_name);
679 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
681 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
683 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
685 if (!zh->lock_normal || !zh->lock_shadow)
689 zebra_lock_destroy(zh->lock_normal);
694 zebra_lock_destroy(zh->lock_shadow);
703 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
704 zebra_set_approx_limit(zh, approx);
708 void map_basenames_func (void *vp, const char *name, const char *value)
710 struct map_baseinfo *p = (struct map_baseinfo *) vp;
712 char fromdb[128], todb[8][128];
719 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
720 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
721 todb[5], todb[6], todb[7]);
725 for (i = 0; i<p->num_bases; i++)
726 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
729 for (i = 0; i < no; i++)
731 if (p->new_num_bases == p->new_num_max)
733 p->new_basenames[(p->new_num_bases)++] =
734 nmem_strdup (p->mem, todb[i]);
740 int zebra_select_default_database(ZebraHandle zh)
744 /* no database has been selected - so we select based on
745 resource setting (including group)
747 const char *group = res_get(zh->service->global_res, "group");
748 const char *v = res_get_prefix(zh->service->global_res,
749 "database", group, "Default");
750 return zebra_select_database(zh, v);
755 void map_basenames (ZebraHandle zh, ODR stream,
756 int *num_bases, char ***basenames)
758 struct map_baseinfo info;
759 struct map_baseinfo *p = &info;
762 yaz_log(log_level, "map_basenames ");
767 info.num_bases = *num_bases;
768 info.basenames = *basenames;
769 info.new_num_max = 128;
770 info.new_num_bases = 0;
771 info.new_basenames = (char **)
772 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
773 info.mem = stream->mem;
775 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
777 for (i = 0; i<p->num_bases; i++)
778 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
780 p->new_basenames[(p->new_num_bases)++] =
781 nmem_strdup (p->mem, p->basenames[i]);
783 *num_bases = info.new_num_bases;
784 *basenames = info.new_basenames;
785 for (i = 0; i<*num_bases; i++)
786 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
789 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
792 yaz_log(log_level, "zebra_select_database %s",basename);
794 return zebra_select_databases (zh, 1, &basename);
797 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
798 const char **basenames)
807 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
808 num_bases,basenames[0]);
813 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
816 for (i = 0; i < zh->num_basenames; i++)
817 xfree(zh->basenames[i]);
818 xfree(zh->basenames);
820 zh->num_basenames = num_bases;
821 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
822 for (i = 0; i < zh->num_basenames; i++)
823 zh->basenames[i] = xstrdup (basenames[i]);
825 cp = strrchr(basenames[0], '/');
828 len = cp - basenames[0];
829 new_reg = xmalloc(len + 1);
830 memcpy (new_reg, basenames[0], len);
834 new_reg = xstrdup ("");
835 for (i = 1; i<num_bases; i++)
839 cp1 = strrchr (basenames[i], '/');
844 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
847 if (len != cp1 - basenames[i] ||
848 memcmp (basenames[i], new_reg, len))
850 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
858 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
863 zebra_select_register (zh, new_reg);
867 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
870 if (!zh->lock_normal || !zh->lock_shadow)
872 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
878 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
880 if (approx_limit == 0)
881 approx_limit = DEFAULT_APPROX_LIMIT;
882 zh->approx_limit = approx_limit;
886 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
887 const char *setname, zint *hits)
895 yaz_log(log_level, "zebra_search_rpn");
896 zebra_clearError(zh);
900 if (zebra_begin_read(zh) == ZEBRA_FAIL)
903 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
904 zh->num_basenames, zh->basenames, setname);
910 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
912 Z_RecordComposition *comp,
913 oid_value input_format, int num_recs,
914 ZebraRetrievalRecord *recs)
916 ZebraMetaRecord *poset;
918 ZEBRA_RES ret = ZEBRA_OK;
926 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
928 zebra_clearError(zh);
931 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
936 if (zebra_begin_read (zh) == ZEBRA_FAIL)
939 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
940 for (i = 0; i<num_recs; i++)
941 pos_array[i] = recs[i].position;
942 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
945 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
946 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
952 for (i = 0; i<num_recs; i++)
957 recs[i].format = VAL_SUTRS;
958 recs[i].len = strlen(poset[i].term);
959 recs[i].buf = poset[i].term;
960 recs[i].base = poset[i].db;
962 else if (poset[i].sysno)
966 zebra_snippets *hit_snippet = zebra_snippets_create();
968 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
972 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
974 stream, input_format, comp,
975 &recs[i].format, &buf, &len,
976 &recs[i].base, &recs[i].errString);
981 recs[i].buf = (char*) odr_malloc(stream, len);
982 memcpy(recs[i].buf, buf, len);
986 recs[i].score = poset[i].score;
987 recs[i].sysno = poset[i].sysno;
988 zebra_snippets_destroy(hit_snippet);
992 /* only need to set it once */
993 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
995 zebra_setError_zint(zh,
996 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1001 recs[i].buf = 0; /* no record and no error issued */
1003 recs[i].errCode = 0;
1004 recs[i].format = VAL_NONE;
1008 zebra_meta_records_destroy(zh, poset, num_recs);
1010 zebra_end_read (zh);
1015 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1017 int *num_entries, ZebraScanEntry **entries,
1019 const char *setname)
1021 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1022 Z_AttributesPlusTerm *zapt;
1026 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1029 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1032 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1033 position, num_entries, entries, is_partial,
1035 yaz_pqf_destroy (pqf_parser);
1039 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1040 oid_value attributeset,
1042 int *num_entries, ZebraScanEntry **entries,
1044 const char *setname)
1047 RSET limit_rset = 0;
1052 assert(num_entries);
1055 yaz_log(log_level, "zebra_scan");
1056 zebra_clearError(zh);
1057 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1065 limit_rset = resultSetRef(zh, setname);
1069 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1071 zebra_end_read (zh);
1075 res = rpn_scan (zh, stream, zapt, attributeset,
1076 zh->num_basenames, zh->basenames, position,
1077 num_entries, entries, is_partial, limit_rset, 0);
1078 zebra_end_read (zh);
1082 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1083 int num_input_setnames, const char **input_setnames,
1084 const char *output_setname,
1085 Z_SortKeySpecList *sort_sequence,
1091 assert(num_input_setnames>0);
1092 assert(input_setnames);
1093 assert(sort_sequence);
1094 assert(sort_status);
1095 yaz_log(log_level, "zebra_sort");
1096 zebra_clearError(zh);
1097 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1099 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1100 output_setname, sort_sequence, sort_status);
1105 int zebra_deleteResultSet(ZebraHandle zh, int function,
1106 int num_setnames, char **setnames,
1112 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1113 zebra_clearError(zh);;
1114 if (zebra_begin_read(zh))
1115 return Z_DeleteStatus_systemProblemAtTarget;
1118 case Z_DeleteResultSetRequest_list:
1119 assert(num_setnames>0);
1121 resultSetDestroy (zh, num_setnames, setnames, statuses);
1123 case Z_DeleteResultSetRequest_all:
1124 resultSetDestroy (zh, -1, 0, statuses);
1127 zebra_end_read (zh);
1128 status = Z_DeleteStatus_success;
1129 for (i = 0; i<num_setnames; i++)
1130 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1131 status = statuses[i];
1135 int zebra_errCode (ZebraHandle zh)
1139 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1142 yaz_log(log_level, "zebra_errCode: o");
1146 const char *zebra_errString (ZebraHandle zh)
1150 e= diagbib1_str (zh->errCode);
1151 yaz_log(log_level, "zebra_errString: %s",e);
1155 char *zebra_errAdd (ZebraHandle zh)
1160 yaz_log(log_level, "zebra_errAdd: %s",a);
1164 void zebra_clearError(ZebraHandle zh)
1173 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1180 zebra_clearError(zh);
1183 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1184 p = res_get(zs->global_res, u);
1185 xfree(zh->user_perm);
1186 zh->user_perm = xstrdup(p ? p : "r");
1188 /* users that don't require a password .. */
1189 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1192 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1197 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1198 const char *record_type)
1201 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1202 database, record_type);
1203 zebra_clearError(zh);
1204 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1206 return zebra_begin_trans(zh, 1);
1209 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1212 yaz_log(log_level, "zebra_admin_import_end");
1213 zebra_clearError(zh);
1214 return zebra_end_trans(zh);
1217 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1219 ZEBRA_RES res = ZEBRA_OK;
1223 yaz_log(log_level, "zebra_admin_import_segment");
1224 zebra_clearError(zh);
1225 for (i = 0; i<segment->num_segmentRecords; i++)
1227 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1229 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1231 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1232 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1234 Odr_oct *oct = fragment->u.notExternallyTagged;
1237 if ( zebra_update_record(zh,
1238 0, /* record Type */
1251 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1252 const char *rec_buf,
1254 const char *recid_buf, size_t recid_len,
1256 /* 1 = insert. Fail it already exists */
1257 /* 2 = replace. Fail it does not exist */
1258 /* 3 = delete. Fail if does not exist */
1259 /* 4 = update. Insert/replace */
1266 assert(action>0 && action <=4);
1269 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1270 zebra_clearError(zh);
1272 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1275 memcpy (recid_z, recid_buf, recid_len);
1276 recid_z[recid_len] = 0;
1278 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1281 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1284 if (action == 1) /* fail if insert */
1286 zebra_end_trans(zh);
1290 memcpy (&sysno, rinfo+1, sizeof(sysno));
1294 if (action == 2 || action == 3) /* fail if delete or update */
1296 zebra_end_trans(zh);
1299 action = 1; /* make it an insert (if it's an update).. */
1301 res = buffer_extract_record (zh, rec_buf, rec_len,
1302 action == 3 ? 1 : 0 /* delete flag */,
1308 0, /* force update */
1309 1 /* allow update */
1313 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1315 else if (action == 3)
1317 dict_delete (zh->reg->matchDict, recid_z);
1319 zebra_end_trans(zh);
1323 int delete_w_handle(const char *info, void *handle)
1325 ZebraHandle zh = (ZebraHandle) handle;
1329 if (*info == sizeof(pos))
1331 memcpy (&pos, info+1, sizeof(pos));
1332 isamb_unlink(zh->reg->isamb, pos);
1337 static int delete_SU_handle(void *handle, int ord)
1339 ZebraHandle zh = (ZebraHandle) handle;
1343 ord_len = key_SU_encode (ord, ord_buf);
1344 ord_buf[ord_len] = '\0';
1346 assert (zh->reg->isamb);
1347 dict_delete_subtree(zh->reg->dict, ord_buf,
1348 zh, delete_w_handle);
1352 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1354 ZEBRA_RES ret = ZEBRA_OK;
1356 yaz_log(log_level, "zebra_drop_database %s", db);
1357 zebra_clearError(zh);
1359 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1361 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1365 zebraExplain_curDatabase (zh->reg->zei, db);
1367 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1368 zebraExplain_removeDatabase(zh->reg->zei, zh);
1372 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1375 zebra_end_trans (zh);
1379 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1382 yaz_log(log_level, "zebra_create_database %s", db);
1384 zebra_clearError(zh);
1386 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1388 if (zebra_begin_trans (zh, 1))
1391 /* announce database */
1392 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1393 /* explainDatabase */))
1395 zebra_end_trans (zh);
1396 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1399 return zebra_end_trans (zh);
1402 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1403 const char *input_str, int input_len,
1404 char *output_str, int output_len)
1410 yaz_log(log_level, "zebra_string_norm ");
1411 zebra_clearError(zh);
1412 if (!zh->reg->zebra_maps)
1414 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1415 input_str, input_len);
1418 if (wrbuf_len(wrbuf) >= output_len)
1420 if (wrbuf_len(wrbuf))
1421 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1422 output_str[wrbuf_len(wrbuf)] = '\0';
1423 return wrbuf_len(wrbuf);
1426 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1428 char state_fname[256];
1433 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1434 zebra_clearError(zh);
1436 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1437 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1438 f = fopen (fname, "w");
1440 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1441 fprintf (f, "%c %d %ld\n", val, seqno, p);
1446 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1448 char state_fname[256];
1453 yaz_log(log_level, "zebra_get_state ");
1454 zebra_clearError(zh);
1455 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1456 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1457 f = fopen (fname, "r");
1463 fscanf (f, "%c %d", val, seqno);
1469 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1471 return zebra_begin_trans(zh, 0);
1474 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1476 return zebra_end_trans(zh);
1479 static void read_res_for_transaction(ZebraHandle zh)
1481 const char *group = res_get(zh->res, "group");
1483 /* FIXME - do we still use groups ?? */
1485 zh->m_group = group;
1486 v = res_get_prefix(zh->res, "followLinks", group, "1");
1487 zh->m_follow_links = atoi(v);
1489 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1490 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1492 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1493 zh->m_store_keys = atoi(v);
1495 v = res_get_prefix(zh->res, "storeData", group, "1");
1496 zh->m_store_data = atoi(v);
1498 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1499 zh->m_explain_database = atoi(v);
1501 v = res_get_prefix(zh->res, "openRW", group, "1");
1502 zh->m_flag_rw = atoi(v);
1504 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1505 zh->m_file_verbose_limit = atoi(v);
1508 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1511 zebra_select_default_database(zh);
1514 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1515 "zebra_begin_trans: no database selected");
1519 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1523 if (rw && !strchr(zh->user_perm, 'w'))
1527 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1539 const char *rval = 0;
1544 read_res_for_transaction(zh);
1547 if (zh->trans_no != 1)
1549 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1550 "zebra_begin_trans: no write trans within read");
1555 resultSetInvalidate (zh);
1556 zebra_register_close (zh->service, zh->reg);
1558 zh->trans_w_no = zh->trans_no;
1560 zebra_clearError(zh);
1562 zh->records_inserted = 0;
1563 zh->records_updated = 0;
1564 zh->records_deleted = 0;
1565 zh->records_processed = 0;
1567 #if HAVE_SYS_TIMES_H
1571 if (zh->shadow_enable)
1572 rval = res_get (zh->res, "shadow");
1574 for (pass = 0; pass < 2; pass++)
1578 zebra_lock_r (zh->lock_normal);
1579 zebra_lock_w (zh->lock_shadow);
1583 zebra_lock_w (zh->lock_normal);
1584 zebra_lock_w (zh->lock_shadow);
1587 zebra_get_state (zh, &val, &seqno);
1590 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1591 zebra_unlock (zh->lock_shadow);
1592 zebra_unlock (zh->lock_normal);
1596 else if (val == 'd')
1600 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1602 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1603 bf_commitClean (bfs, rval);
1608 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1615 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1619 zebra_set_state (zh, 'd', seqno);
1621 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1622 1, rval ? 1 : 0, zh->res,
1625 zh->reg->seqno = seqno;
1628 zebra_set_state (zh, 'o', seqno);
1630 zebra_unlock (zh->lock_shadow);
1631 zebra_unlock (zh->lock_normal);
1636 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1637 "zebra_begin_trans: cannot open register");
1638 yaz_log(YLOG_FATAL, "%s", zh->errString);
1650 if (zh->trans_no != 1)
1652 zebra_flush_reg (zh);
1655 zebra_clearError(zh);
1656 #if HAVE_SYS_TIMES_H
1662 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1665 if (!zh->lock_normal || !zh->lock_shadow)
1668 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1671 zebra_get_state (zh, &val, &seqno);
1677 else if (seqno != zh->reg->seqno)
1679 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1680 seqno, zh->reg->seqno);
1683 else if (zh->reg->last_val != val)
1685 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1686 val, zh->reg->last_val);
1693 zebra_lock_r (zh->lock_shadow);
1695 zebra_lock_r (zh->lock_normal);
1699 resultSetInvalidate (zh);
1700 zebra_register_close (zh->service, zh->reg);
1702 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1703 0, val == 'c' ? 1 : 0,
1704 zh->res, zh->path_reg);
1707 zebra_unlock (zh->lock_normal);
1708 zebra_unlock (zh->lock_shadow);
1710 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1713 zh->reg->last_val = val;
1714 zh->reg->seqno = seqno;
1716 read_res_for_transaction(zh);
1720 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1722 ZebraTransactionStatus dummy;
1724 yaz_log(log_level, "zebra_end_trans");
1725 return zebra_end_transaction(zh, &dummy);
1728 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1736 yaz_log(log_level, "zebra_end_transaction");
1738 status->processed = 0;
1739 status->inserted = 0;
1740 status->updated = 0;
1741 status->deleted = 0;
1745 if (!zh->res || !zh->reg)
1747 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1748 "zebra_end_trans: no open transaction");
1751 if (zh->trans_no != zh->trans_w_no)
1754 if (zh->trans_no != 0)
1757 /* release read lock */
1759 zebra_unlock (zh->lock_normal);
1760 zebra_unlock (zh->lock_shadow);
1763 { /* release write lock */
1767 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1768 rval = res_get (zh->res, "shadow");
1770 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1772 zebra_flush_reg (zh);
1774 resultSetInvalidate (zh);
1776 zebra_register_close (zh->service, zh->reg);
1779 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1780 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1781 zh->records_processed, zh->records_inserted,
1782 zh->records_updated, zh->records_deleted);
1784 status->processed = (int) zh->records_processed;
1785 status->inserted = (int) zh->records_inserted;
1786 status->updated = (int) zh->records_updated;
1787 status->deleted = (int) zh->records_deleted;
1789 zebra_get_state (zh, &val, &seqno);
1792 BFiles bfs = bfs_create (rval, zh->path_reg);
1793 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1794 bf_commitClean (bfs, rval);
1799 zebra_set_state (zh, 'o', seqno);
1801 zebra_unlock (zh->lock_shadow);
1802 zebra_unlock (zh->lock_normal);
1805 #if HAVE_SYS_TIMES_H
1807 yaz_log (log_level, "user/system: %ld/%ld",
1808 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1809 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1811 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1812 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1817 int zebra_repository_update (ZebraHandle zh, const char *path)
1821 zebra_clearError(zh);
1822 yaz_log (log_level, "updating %s", path);
1823 repositoryUpdate (zh, path);
1827 int zebra_repository_delete (ZebraHandle zh, const char *path)
1831 zebra_clearError(zh);
1832 yaz_log (log_level, "deleting %s", path);
1833 repositoryDelete (zh, path);
1837 int zebra_repository_show (ZebraHandle zh, const char *path)
1841 yaz_log(log_level, "zebra_repository_show");
1842 zebra_clearError(zh);
1843 repositoryShow (zh, path);
1847 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1854 zebra_clearError(zh);
1856 zebra_select_default_database(zh);
1859 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1862 rval = res_get (zh->res, "shadow");
1865 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1869 zebra_lock_w (zh->lock_normal);
1870 zebra_lock_r (zh->lock_shadow);
1872 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1874 zebra_get_state (zh, &val, &seqno);
1877 bf_cache (bfs, rval);
1878 if (bf_commitExists (bfs))
1881 zebra_set_state (zh, 'd', seqno);
1884 zebra_set_state (zh, 'c', seqno);
1886 yaz_log (YLOG_DEBUG, "commit start");
1887 bf_commitExec (bfs);
1892 yaz_log (YLOG_DEBUG, "commit clean");
1893 bf_commitClean (bfs, rval);
1895 zebra_set_state (zh, 'o', seqno);
1899 yaz_log (log_level, "nothing to commit");
1903 zebra_unlock (zh->lock_shadow);
1904 zebra_unlock (zh->lock_normal);
1908 ZEBRA_RES zebra_clean(ZebraHandle zh)
1911 yaz_log(log_level, "zebra_clean");
1912 return zebra_commit_ex(zh, 1);
1915 ZEBRA_RES zebra_commit(ZebraHandle zh)
1918 yaz_log(log_level, "zebra_commit");
1919 return zebra_commit_ex(zh, 0);
1922 ZEBRA_RES zebra_init(ZebraHandle zh)
1927 yaz_log(log_level, "zebra_init");
1928 zebra_clearError(zh);
1930 zebra_select_default_database(zh);
1933 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1934 "cannot select default database");
1937 rval = res_get (zh->res, "shadow");
1939 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1942 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1946 bf_cache (bfs, rval);
1950 zebra_set_state (zh, 'o', 0);
1954 ZEBRA_RES zebra_compact(ZebraHandle zh)
1958 yaz_log(log_level, "zebra_compact");
1959 zebra_clearError(zh);
1962 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1965 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1971 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1974 yaz_log(log_level, "zebra_result");
1975 *code = zh->errCode;
1976 *addinfo = zh->errString;
1979 void zebra_shadow_enable(ZebraHandle zh, int value)
1982 yaz_log(log_level, "zebra_shadow_enable");
1983 zebra_clearError(zh);
1984 zh->shadow_enable = value;
1987 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1991 yaz_log(log_level, "zebra_octet_term_encoding");
1992 zebra_clearError(zh);
1994 if (zh->iconv_to_utf8 != 0)
1995 yaz_iconv_close(zh->iconv_to_utf8);
1996 if (zh->iconv_from_utf8 != 0)
1997 yaz_iconv_close(zh->iconv_from_utf8);
2000 yaz_iconv_open ("UTF-8", encoding);
2001 if (zh->iconv_to_utf8 == 0)
2002 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2003 zh->iconv_from_utf8 =
2004 yaz_iconv_open (encoding, "UTF-8");
2005 if (zh->iconv_to_utf8 == 0)
2006 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2011 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2014 yaz_log(log_level, "zebra_record_encoding");
2015 zebra_clearError(zh);
2016 xfree(zh->record_encoding);
2017 zh->record_encoding = 0;
2019 zh->record_encoding = xstrdup (encoding);
2023 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2028 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2029 zebra_clearError(zh);
2030 res_set(zh->res, name, value);
2033 const char *zebra_get_resource(ZebraHandle zh,
2034 const char *name, const char *defaultvalue)
2039 assert(defaultvalue);
2040 v = res_get_def (zh->res, name, (char *)defaultvalue);
2041 zebra_clearError(zh);
2042 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2046 /* moved from zebra_api_ext.c by pop */
2047 /* FIXME: Should this really be public??? -Heikki */
2049 int zebra_trans_no (ZebraHandle zh)
2052 yaz_log(log_level, "zebra_trans_no");
2053 return zh->trans_no;
2056 int zebra_get_shadow_enable (ZebraHandle zh)
2059 yaz_log(log_level, "zebra_get_shadow_enable");
2060 return zh->shadow_enable;
2063 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2066 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2067 zh->shadow_enable = value;
2070 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2071 so that it's identicical to the original api_records_retrieve */
2072 void api_records_retrieve (ZebraHandle zh, ODR stream,
2073 const char *setname, Z_RecordComposition *comp,
2074 oid_value input_format, int num_recs,
2075 ZebraRetrievalRecord *recs)
2077 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2081 /* ---------------------------------------------------------------------------
2082 Record insert(=update), delete
2084 If sysno is provided, then it's used to identify the record.
2085 If not, and match_criteria is provided, then sysno is guessed
2086 If not, and a record is provided, then sysno is got from there
2087 NOTE: Now returns 0 at success and updates sysno, which is an int*
2091 int zebra_add_record(ZebraHandle zh,
2092 const char *buf, int buf_size)
2095 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2098 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2099 const char *recordType,
2100 SYSNO *sysno, const char *match,
2102 const char *buf, int buf_size, int force_update)
2108 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2111 buf_size = strlen(buf);
2113 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2115 res = buffer_extract_record (zh, buf, buf_size,
2116 0, /* delete_flag */
2122 0); /* allow_update */
2123 zebra_end_trans(zh);
2127 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2128 const char *recordType,
2129 SYSNO* sysno, const char *match,
2131 const char *buf, int buf_size,
2139 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2141 if (buf_size < 1) buf_size = strlen(buf);
2143 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2145 res = buffer_extract_record (zh, buf, buf_size,
2146 0, /* delete_flag */
2152 1); /* allow_update */
2153 zebra_end_trans(zh);
2157 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2158 const char *recordType,
2159 SYSNO *sysno, const char *match,
2161 const char *buf, int buf_size,
2168 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2170 if (buf_size < 1) buf_size = strlen(buf);
2172 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2174 res = buffer_extract_record (zh, buf, buf_size,
2175 1, /* delete_flag */
2181 1); /* allow_update */
2182 zebra_end_trans(zh);
2186 /* ---------------------------------------------------------------------------
2190 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2191 const char *setname, zint *hits)
2194 ZEBRA_RES res = ZEBRA_OK;
2196 ODR odr = odr_createmem(ODR_ENCODE);
2201 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2203 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2207 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2208 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2212 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2216 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2224 /* ---------------------------------------------------------------------------
2225 Sort - a simplified interface, with optional read locks.
2227 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2228 const char *sort_spec,
2229 const char *output_setname,
2230 const char **input_setnames)
2232 int num_input_setnames = 0;
2233 int sort_status = 0;
2234 Z_SortKeySpecList *sort_sequence;
2238 assert(output_setname);
2239 assert(input_setnames);
2240 sort_sequence = yaz_sort_spec (stream, sort_spec);
2241 yaz_log(log_level, "sort (FIXME) ");
2244 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2245 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2249 /* we can do this, since the perl typemap code for char** will
2250 put a NULL at the end of list */
2251 while (input_setnames[num_input_setnames]) num_input_setnames++;
2253 if (zebra_begin_read (zh))
2256 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2257 output_setname, sort_sequence, &sort_status);
2263 /* ---------------------------------------------------------------------------
2264 Get BFS for Zebra system (to make alternative storage methods)
2266 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2269 return zh->reg->bfs;
2274 /* ---------------------------------------------------------------------------
2275 Set limit for search/scan
2277 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2280 zebra_limit_destroy(zh->m_limit);
2281 zh->m_limit = zebra_limit_create(complement_flag, ids);
2286 Set Error code + addinfo
2288 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2291 nmem_reset(zh->nmem_error);
2292 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2295 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2298 sprintf(vstr, ZINT_FORMAT, i);
2301 nmem_reset(zh->nmem_error);
2302 zh->errString = nmem_strdup(zh->nmem_error, vstr);