1 /* $Id: zebraapi.c,v 1.184 2005-08-22 09:04:18 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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;
129 zh->m_staticrank = 0;
131 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
134 yaz_iconv_open ("UTF-8", default_encoding);
135 if (zh->iconv_to_utf8 == 0)
136 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
138 zh->iconv_from_utf8 =
139 yaz_iconv_open (default_encoding, "UTF-8");
140 if (zh->iconv_to_utf8 == 0)
141 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
144 zh->record_encoding = 0;
146 zebra_mutex_cond_lock (&zs->session_lock);
148 zh->next = zs->sessions;
151 zebra_mutex_cond_unlock (&zs->session_lock);
153 zh->store_data_buf = 0;
155 zh->m_limit = zebra_limit_create(1, 0);
157 zh->nmem_error = nmem_create();
162 ZebraService zebra_start (const char *configName)
164 return zebra_start_res(configName, 0, 0);
167 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
171 if (!log_level_initialized)
173 log_level = yaz_log_module_level("zebraapi");
174 log_level_initialized = 1;
177 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
178 configName ? configName : "");
180 if ((res = res_open (configName, def_res, over_res)))
182 const char *passwd_plain = 0;
183 const char *passwd_encrypt = 0;
184 ZebraService zh = xmalloc(sizeof(*zh));
186 zh->global_res = res;
191 zebra_mutex_cond_init (&zh->session_lock);
192 passwd_plain = res_get (zh->global_res, "passwd");
193 passwd_encrypt = res_get (zh->global_res, "passwd.c");
195 if (!passwd_plain && !passwd_encrypt)
196 zh->passwd_db = NULL;
199 zh->passwd_db = passwd_db_open();
201 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
205 passwd_db_file_plain(zh->passwd_db, passwd_plain);
207 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
210 zh->path_root = res_get (zh->global_res, "root");
211 zh->nmem = nmem_create();
212 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
218 void zebra_filter_info(ZebraService zs, void *cd,
219 void (*cb)(void *cd, const char *name))
223 recTypeClass_info(zs->record_classes, cd, cb);
226 void zebra_pidfname(ZebraService zs, char *path)
229 zebra_lock_prefix (zs->global_res, path);
230 strcat(path, "zebrasrv.pid");
233 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
234 int compact_flag, Res res)
236 int page_size = 4096;
237 char resource_str[200];
238 sprintf (resource_str, "dict.%.100s.pagesize", name);
242 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
243 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
245 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
249 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
250 int rw, int useshadow, Res res,
251 const char *reg_path)
253 struct zebra_register *reg;
254 int record_compression = REC_COMPRESS_NONE;
255 const char *recordCompression = 0;
256 const char *profilePath;
261 reg = xmalloc(sizeof(*reg));
264 reg->name = xstrdup (name);
271 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
272 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
274 reg->dh = data1_createx (DATA1_FLAG_XML);
280 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
283 data1_destroy(reg->dh);
289 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
291 bfs_destroy(reg->bfs);
292 data1_destroy(reg->dh);
298 getcwd(cwd, sizeof(cwd)-1);
299 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
300 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
302 data1_set_tabpath (reg->dh, profilePath);
303 data1_set_tabroot (reg->dh, reg_path);
304 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
306 reg->zebra_maps = zebra_maps_open (res, reg_path);
307 reg->rank_classes = NULL;
311 reg->keys.buf_max = 0;
313 reg->keys.codec_handle = iscz1_start();
315 reg->sortKeys.buf = 0;
316 reg->sortKeys.buf_max = 0;
318 reg->sortKeys.codec_handle = iscz1_start();
330 reg->key_file_no = 0;
333 zebraRankInstall (reg, rank_1_class);
334 zebraRankInstall (reg, rank_zv_class);
335 zebraRankInstall (reg, rank_static_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);
486 if (reg->sortKeys.codec_handle)
487 iscz1_stop(reg->sortKeys.codec_handle);
494 ZEBRA_RES zebra_stop(ZebraService zs)
498 yaz_log (log_level, "zebra_stop");
502 zebra_close (zs->sessions);
505 zebra_mutex_cond_destroy (&zs->session_lock);
508 passwd_db_close (zs->passwd_db);
510 recTypeClass_destroy(zs->record_classes);
511 nmem_destroy(zs->nmem);
512 res_close (zs->global_res);
517 ZEBRA_RES zebra_close (ZebraHandle zh)
520 struct zebra_session **sp;
523 yaz_log(log_level, "zebra_close");
530 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
531 resultSetDestroy (zh, -1, 0, 0);
534 zebra_register_close (zh->service, zh->reg);
535 zebra_close_res (zh);
537 xfree(zh->record_encoding);
539 for (i = 0; i < zh->num_basenames; i++)
540 xfree(zh->basenames[i]);
541 xfree(zh->basenames);
543 if (zh->iconv_to_utf8 != 0)
544 yaz_iconv_close (zh->iconv_to_utf8);
545 if (zh->iconv_from_utf8 != 0)
546 yaz_iconv_close (zh->iconv_from_utf8);
548 zebra_mutex_cond_lock (&zs->session_lock);
549 zebra_lock_destroy (zh->lock_normal);
550 zebra_lock_destroy (zh->lock_shadow);
562 zebra_mutex_cond_unlock (&zs->session_lock);
564 xfree(zh->user_perm);
565 zh->service = 0; /* more likely to trigger an assert */
567 zebra_limit_destroy(zh->m_limit);
569 nmem_destroy(zh->nmem_error);
576 struct map_baseinfo {
582 char **new_basenames;
586 static Res zebra_open_res (ZebraHandle zh)
595 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
596 res = res_open (fname, zh->service->global_res, 0);
598 res = zh->service->global_res;
600 else if (*zh->reg_name == 0)
602 res = zh->service->global_res;
606 yaz_log (YLOG_WARN, "no register root specified");
607 return 0; /* no path for register - fail! */
612 static void zebra_close_res (ZebraHandle zh)
616 if (zh->res != zh->service->global_res)
621 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
625 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
629 assert (zh->reg == 0);
630 assert (*zh->reg_name == 0);
636 resultSetInvalidate (zh);
637 zebra_register_close (zh->service, zh->reg);
643 zh->reg_name = xstrdup (new_reg);
647 if (zh->service->path_root)
649 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
650 strlen(zh->reg_name) + 3);
651 strcpy (zh->path_reg, zh->service->path_root);
654 strcat (zh->path_reg, "/");
655 strcat (zh->path_reg, zh->reg_name);
658 zh->res = zebra_open_res (zh);
661 zebra_lock_destroy (zh->lock_normal);
665 zebra_lock_destroy (zh->lock_shadow);
671 const char *lock_area =res_get (zh->res, "lockDir");
673 if (!lock_area && zh->path_reg)
674 res_set (zh->res, "lockDir", zh->path_reg);
675 sprintf (fname, "norm.%s.LCK", zh->reg_name);
677 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
679 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
681 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
683 if (!zh->lock_normal || !zh->lock_shadow)
687 zebra_lock_destroy(zh->lock_normal);
692 zebra_lock_destroy(zh->lock_shadow);
701 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
702 zebra_set_approx_limit(zh, approx);
706 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
707 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
709 yaz_log(YLOG_LOG, "static rank unset");
713 void map_basenames_func (void *vp, const char *name, const char *value)
715 struct map_baseinfo *p = (struct map_baseinfo *) vp;
717 char fromdb[128], todb[8][128];
724 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
725 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
726 todb[5], todb[6], todb[7]);
730 for (i = 0; i<p->num_bases; i++)
731 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
734 for (i = 0; i < no; i++)
736 if (p->new_num_bases == p->new_num_max)
738 p->new_basenames[(p->new_num_bases)++] =
739 nmem_strdup (p->mem, todb[i]);
745 int zebra_select_default_database(ZebraHandle zh)
749 /* no database has been selected - so we select based on
750 resource setting (including group)
752 const char *group = res_get(zh->service->global_res, "group");
753 const char *v = res_get_prefix(zh->service->global_res,
754 "database", group, "Default");
755 return zebra_select_database(zh, v);
760 void map_basenames (ZebraHandle zh, ODR stream,
761 int *num_bases, char ***basenames)
763 struct map_baseinfo info;
764 struct map_baseinfo *p = &info;
767 yaz_log(log_level, "map_basenames ");
772 info.num_bases = *num_bases;
773 info.basenames = *basenames;
774 info.new_num_max = 128;
775 info.new_num_bases = 0;
776 info.new_basenames = (char **)
777 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
778 info.mem = stream->mem;
780 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
782 for (i = 0; i<p->num_bases; i++)
783 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
785 p->new_basenames[(p->new_num_bases)++] =
786 nmem_strdup (p->mem, p->basenames[i]);
788 *num_bases = info.new_num_bases;
789 *basenames = info.new_basenames;
790 for (i = 0; i<*num_bases; i++)
791 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
794 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
797 yaz_log(log_level, "zebra_select_database %s",basename);
799 return zebra_select_databases (zh, 1, &basename);
802 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
803 const char **basenames)
812 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
813 num_bases,basenames[0]);
818 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
821 for (i = 0; i < zh->num_basenames; i++)
822 xfree(zh->basenames[i]);
823 xfree(zh->basenames);
825 zh->num_basenames = num_bases;
826 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
827 for (i = 0; i < zh->num_basenames; i++)
828 zh->basenames[i] = xstrdup (basenames[i]);
830 cp = strrchr(basenames[0], '/');
833 len = cp - basenames[0];
834 new_reg = xmalloc(len + 1);
835 memcpy (new_reg, basenames[0], len);
839 new_reg = xstrdup ("");
840 for (i = 1; i<num_bases; i++)
844 cp1 = strrchr (basenames[i], '/');
849 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
852 if (len != cp1 - basenames[i] ||
853 memcmp (basenames[i], new_reg, len))
855 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
863 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
868 zebra_select_register (zh, new_reg);
872 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
875 if (!zh->lock_normal || !zh->lock_shadow)
877 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
883 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
885 if (approx_limit == 0)
886 approx_limit = DEFAULT_APPROX_LIMIT;
887 zh->approx_limit = approx_limit;
891 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
892 const char *setname, zint *hits)
900 yaz_log(log_level, "zebra_search_rpn");
901 zebra_clearError(zh);
905 if (zebra_begin_read(zh) == ZEBRA_FAIL)
908 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
909 zh->num_basenames, zh->basenames, setname);
915 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
917 Z_RecordComposition *comp,
918 oid_value input_format, int num_recs,
919 ZebraRetrievalRecord *recs)
921 ZebraMetaRecord *poset;
923 ZEBRA_RES ret = ZEBRA_OK;
931 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
933 zebra_clearError(zh);
936 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
941 if (zebra_begin_read (zh) == ZEBRA_FAIL)
944 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
945 for (i = 0; i<num_recs; i++)
946 pos_array[i] = recs[i].position;
947 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
950 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
951 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
957 for (i = 0; i<num_recs; i++)
962 recs[i].format = VAL_SUTRS;
963 recs[i].len = strlen(poset[i].term);
964 recs[i].buf = poset[i].term;
965 recs[i].base = poset[i].db;
967 else if (poset[i].sysno)
971 zebra_snippets *hit_snippet = zebra_snippets_create();
973 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
977 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
979 stream, input_format, comp,
980 &recs[i].format, &buf, &len,
981 &recs[i].base, &recs[i].errString);
986 recs[i].buf = (char*) odr_malloc(stream, len);
987 memcpy(recs[i].buf, buf, len);
991 recs[i].score = poset[i].score;
992 recs[i].sysno = poset[i].sysno;
993 zebra_snippets_destroy(hit_snippet);
997 /* only need to set it once */
998 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1000 zebra_setError_zint(zh,
1001 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1006 recs[i].buf = 0; /* no record and no error issued */
1008 recs[i].errCode = 0;
1009 recs[i].format = VAL_NONE;
1013 zebra_meta_records_destroy(zh, poset, num_recs);
1015 zebra_end_read (zh);
1020 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1022 int *num_entries, ZebraScanEntry **entries,
1024 const char *setname)
1026 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1027 Z_AttributesPlusTerm *zapt;
1031 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1034 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1037 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1038 position, num_entries, entries, is_partial,
1040 yaz_pqf_destroy (pqf_parser);
1044 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1045 oid_value attributeset,
1047 int *num_entries, ZebraScanEntry **entries,
1049 const char *setname)
1052 RSET limit_rset = 0;
1057 assert(num_entries);
1060 yaz_log(log_level, "zebra_scan");
1061 zebra_clearError(zh);
1062 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1070 limit_rset = resultSetRef(zh, setname);
1074 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1076 zebra_end_read (zh);
1080 res = rpn_scan (zh, stream, zapt, attributeset,
1081 zh->num_basenames, zh->basenames, position,
1082 num_entries, entries, is_partial, limit_rset, 0);
1083 zebra_end_read (zh);
1087 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1088 int num_input_setnames, const char **input_setnames,
1089 const char *output_setname,
1090 Z_SortKeySpecList *sort_sequence,
1096 assert(num_input_setnames>0);
1097 assert(input_setnames);
1098 assert(sort_sequence);
1099 assert(sort_status);
1100 yaz_log(log_level, "zebra_sort");
1101 zebra_clearError(zh);
1102 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1104 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1105 output_setname, sort_sequence, sort_status);
1110 int zebra_deleteResultSet(ZebraHandle zh, int function,
1111 int num_setnames, char **setnames,
1117 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1118 zebra_clearError(zh);;
1119 if (zebra_begin_read(zh))
1120 return Z_DeleteStatus_systemProblemAtTarget;
1123 case Z_DeleteResultSetRequest_list:
1124 assert(num_setnames>0);
1126 resultSetDestroy (zh, num_setnames, setnames, statuses);
1128 case Z_DeleteResultSetRequest_all:
1129 resultSetDestroy (zh, -1, 0, statuses);
1132 zebra_end_read (zh);
1133 status = Z_DeleteStatus_success;
1134 for (i = 0; i<num_setnames; i++)
1135 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1136 status = statuses[i];
1140 int zebra_errCode (ZebraHandle zh)
1144 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1147 yaz_log(log_level, "zebra_errCode: o");
1151 const char *zebra_errString (ZebraHandle zh)
1155 e= diagbib1_str (zh->errCode);
1156 yaz_log(log_level, "zebra_errString: %s",e);
1160 char *zebra_errAdd (ZebraHandle zh)
1165 yaz_log(log_level, "zebra_errAdd: %s",a);
1169 void zebra_clearError(ZebraHandle zh)
1178 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1185 zebra_clearError(zh);
1188 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1189 p = res_get(zs->global_res, u);
1190 xfree(zh->user_perm);
1191 zh->user_perm = xstrdup(p ? p : "r");
1193 /* users that don't require a password .. */
1194 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1197 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1202 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1203 const char *record_type)
1206 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1207 database, record_type);
1208 zebra_clearError(zh);
1209 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1211 return zebra_begin_trans(zh, 1);
1214 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1217 yaz_log(log_level, "zebra_admin_import_end");
1218 zebra_clearError(zh);
1219 return zebra_end_trans(zh);
1222 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1224 ZEBRA_RES res = ZEBRA_OK;
1228 yaz_log(log_level, "zebra_admin_import_segment");
1229 zebra_clearError(zh);
1230 for (i = 0; i<segment->num_segmentRecords; i++)
1232 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1234 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1236 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1237 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1239 Odr_oct *oct = fragment->u.notExternallyTagged;
1242 if (zebra_update_record(zh,
1243 0, /* record Type */
1247 (const char *) oct->buf, oct->len,
1256 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1257 const char *rec_buf,
1259 const char *recid_buf, size_t recid_len,
1261 /* 1 = insert. Fail it already exists */
1262 /* 2 = replace. Fail it does not exist */
1263 /* 3 = delete. Fail if does not exist */
1264 /* 4 = update. Insert/replace */
1271 assert(action>0 && action <=4);
1274 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1275 zebra_clearError(zh);
1277 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1280 memcpy (recid_z, recid_buf, recid_len);
1281 recid_z[recid_len] = 0;
1283 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1286 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1289 if (action == 1) /* fail if insert */
1291 zebra_end_trans(zh);
1295 memcpy (&sysno, rinfo+1, sizeof(sysno));
1299 if (action == 2 || action == 3) /* fail if delete or update */
1301 zebra_end_trans(zh);
1304 action = 1; /* make it an insert (if it's an update).. */
1306 res = buffer_extract_record (zh, rec_buf, rec_len,
1307 action == 3 ? 1 : 0 /* delete flag */,
1313 0, /* force update */
1314 1 /* allow update */
1318 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1320 else if (action == 3)
1322 dict_delete (zh->reg->matchDict, recid_z);
1324 zebra_end_trans(zh);
1328 int delete_w_handle(const char *info, void *handle)
1330 ZebraHandle zh = (ZebraHandle) handle;
1334 if (*info == sizeof(pos))
1336 memcpy (&pos, info+1, sizeof(pos));
1337 isamb_unlink(zh->reg->isamb, pos);
1342 static int delete_SU_handle(void *handle, int ord)
1344 ZebraHandle zh = (ZebraHandle) handle;
1348 ord_len = key_SU_encode (ord, ord_buf);
1349 ord_buf[ord_len] = '\0';
1351 assert (zh->reg->isamb);
1352 dict_delete_subtree(zh->reg->dict, ord_buf,
1353 zh, delete_w_handle);
1357 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1359 ZEBRA_RES ret = ZEBRA_OK;
1361 yaz_log(log_level, "zebra_drop_database %s", db);
1362 zebra_clearError(zh);
1364 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1366 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1370 zebraExplain_curDatabase (zh->reg->zei, db);
1372 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1373 zebraExplain_removeDatabase(zh->reg->zei, zh);
1377 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1380 zebra_end_trans (zh);
1384 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1387 yaz_log(log_level, "zebra_create_database %s", db);
1389 zebra_clearError(zh);
1391 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1393 if (zebra_begin_trans (zh, 1))
1396 /* announce database */
1397 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1398 /* explainDatabase */))
1400 zebra_end_trans (zh);
1401 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1404 return zebra_end_trans (zh);
1407 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1408 const char *input_str, int input_len,
1409 char *output_str, int output_len)
1415 yaz_log(log_level, "zebra_string_norm ");
1416 zebra_clearError(zh);
1417 if (!zh->reg->zebra_maps)
1419 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1420 input_str, input_len);
1423 if (wrbuf_len(wrbuf) >= output_len)
1425 if (wrbuf_len(wrbuf))
1426 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1427 output_str[wrbuf_len(wrbuf)] = '\0';
1428 return wrbuf_len(wrbuf);
1431 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1433 char state_fname[256];
1438 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1439 zebra_clearError(zh);
1441 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1442 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1443 f = fopen (fname, "w");
1445 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1446 fprintf (f, "%c %d %ld\n", val, seqno, p);
1451 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1453 char state_fname[256];
1458 yaz_log(log_level, "zebra_get_state ");
1459 zebra_clearError(zh);
1460 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1461 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1462 f = fopen (fname, "r");
1468 fscanf (f, "%c %d", val, seqno);
1474 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1476 return zebra_begin_trans(zh, 0);
1479 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1481 return zebra_end_trans(zh);
1484 static void read_res_for_transaction(ZebraHandle zh)
1486 const char *group = res_get(zh->res, "group");
1488 /* FIXME - do we still use groups ?? */
1490 zh->m_group = group;
1491 v = res_get_prefix(zh->res, "followLinks", group, "1");
1492 zh->m_follow_links = atoi(v);
1494 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1495 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1497 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1498 zh->m_store_keys = atoi(v);
1500 v = res_get_prefix(zh->res, "storeData", group, "1");
1501 zh->m_store_data = atoi(v);
1503 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1504 zh->m_explain_database = atoi(v);
1506 v = res_get_prefix(zh->res, "openRW", group, "1");
1507 zh->m_flag_rw = atoi(v);
1509 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1510 zh->m_file_verbose_limit = atoi(v);
1513 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1516 zebra_select_default_database(zh);
1519 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1520 "zebra_begin_trans: no database selected");
1524 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1528 if (rw && !strchr(zh->user_perm, 'w'))
1532 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1544 const char *rval = 0;
1549 read_res_for_transaction(zh);
1552 if (zh->trans_no != 1)
1554 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1555 "zebra_begin_trans: no write trans within read");
1560 resultSetInvalidate (zh);
1561 zebra_register_close (zh->service, zh->reg);
1563 zh->trans_w_no = zh->trans_no;
1565 zebra_clearError(zh);
1567 zh->records_inserted = 0;
1568 zh->records_updated = 0;
1569 zh->records_deleted = 0;
1570 zh->records_processed = 0;
1572 #if HAVE_SYS_TIMES_H
1576 if (zh->shadow_enable)
1577 rval = res_get (zh->res, "shadow");
1579 for (pass = 0; pass < 2; pass++)
1583 zebra_lock_r (zh->lock_normal);
1584 zebra_lock_w (zh->lock_shadow);
1588 zebra_lock_w (zh->lock_normal);
1589 zebra_lock_w (zh->lock_shadow);
1592 zebra_get_state (zh, &val, &seqno);
1595 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1596 zebra_unlock (zh->lock_shadow);
1597 zebra_unlock (zh->lock_normal);
1601 else if (val == 'd')
1605 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1607 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1608 bf_commitClean (bfs, rval);
1613 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1620 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1624 zebra_set_state (zh, 'd', seqno);
1626 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1627 1, rval ? 1 : 0, zh->res,
1630 zh->reg->seqno = seqno;
1633 zebra_set_state (zh, 'o', seqno);
1635 zebra_unlock (zh->lock_shadow);
1636 zebra_unlock (zh->lock_normal);
1641 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1642 "zebra_begin_trans: cannot open register");
1643 yaz_log(YLOG_FATAL, "%s", zh->errString);
1655 if (zh->trans_no != 1)
1657 zebra_flush_reg (zh);
1660 zebra_clearError(zh);
1661 #if HAVE_SYS_TIMES_H
1667 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1670 if (!zh->lock_normal || !zh->lock_shadow)
1673 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1676 zebra_get_state (zh, &val, &seqno);
1682 else if (seqno != zh->reg->seqno)
1684 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1685 seqno, zh->reg->seqno);
1688 else if (zh->reg->last_val != val)
1690 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1691 val, zh->reg->last_val);
1698 zebra_lock_r (zh->lock_shadow);
1700 zebra_lock_r (zh->lock_normal);
1704 resultSetInvalidate (zh);
1705 zebra_register_close (zh->service, zh->reg);
1707 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1708 0, val == 'c' ? 1 : 0,
1709 zh->res, zh->path_reg);
1712 zebra_unlock (zh->lock_normal);
1713 zebra_unlock (zh->lock_shadow);
1715 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1718 zh->reg->last_val = val;
1719 zh->reg->seqno = seqno;
1721 read_res_for_transaction(zh);
1725 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1727 ZebraTransactionStatus dummy;
1729 yaz_log(log_level, "zebra_end_trans");
1730 return zebra_end_transaction(zh, &dummy);
1733 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1741 yaz_log(log_level, "zebra_end_transaction");
1743 status->processed = 0;
1744 status->inserted = 0;
1745 status->updated = 0;
1746 status->deleted = 0;
1750 if (!zh->res || !zh->reg)
1752 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1753 "zebra_end_trans: no open transaction");
1756 if (zh->trans_no != zh->trans_w_no)
1759 if (zh->trans_no != 0)
1762 /* release read lock */
1764 zebra_unlock (zh->lock_normal);
1765 zebra_unlock (zh->lock_shadow);
1768 { /* release write lock */
1772 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1773 rval = res_get (zh->res, "shadow");
1775 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1777 zebra_flush_reg (zh);
1779 resultSetInvalidate (zh);
1781 zebra_register_close (zh->service, zh->reg);
1784 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1785 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1786 zh->records_processed, zh->records_inserted,
1787 zh->records_updated, zh->records_deleted);
1789 status->processed = (int) zh->records_processed;
1790 status->inserted = (int) zh->records_inserted;
1791 status->updated = (int) zh->records_updated;
1792 status->deleted = (int) zh->records_deleted;
1794 zebra_get_state (zh, &val, &seqno);
1797 BFiles bfs = bfs_create (rval, zh->path_reg);
1798 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1799 bf_commitClean (bfs, rval);
1804 zebra_set_state (zh, 'o', seqno);
1806 zebra_unlock (zh->lock_shadow);
1807 zebra_unlock (zh->lock_normal);
1810 #if HAVE_SYS_TIMES_H
1812 yaz_log (log_level, "user/system: %ld/%ld",
1813 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1814 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1816 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1817 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1822 int zebra_repository_update (ZebraHandle zh, const char *path)
1826 zebra_clearError(zh);
1827 yaz_log (log_level, "updating %s", path);
1828 repositoryUpdate (zh, path);
1832 int zebra_repository_delete (ZebraHandle zh, const char *path)
1836 zebra_clearError(zh);
1837 yaz_log (log_level, "deleting %s", path);
1838 repositoryDelete (zh, path);
1842 int zebra_repository_show (ZebraHandle zh, const char *path)
1846 yaz_log(log_level, "zebra_repository_show");
1847 zebra_clearError(zh);
1848 repositoryShow (zh, path);
1852 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1859 zebra_clearError(zh);
1861 zebra_select_default_database(zh);
1864 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1867 rval = res_get (zh->res, "shadow");
1870 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1874 zebra_lock_w (zh->lock_normal);
1875 zebra_lock_r (zh->lock_shadow);
1877 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1879 zebra_get_state (zh, &val, &seqno);
1882 bf_cache (bfs, rval);
1883 if (bf_commitExists (bfs))
1886 zebra_set_state (zh, 'd', seqno);
1889 zebra_set_state (zh, 'c', seqno);
1891 yaz_log (YLOG_DEBUG, "commit start");
1892 bf_commitExec (bfs);
1897 yaz_log (YLOG_DEBUG, "commit clean");
1898 bf_commitClean (bfs, rval);
1900 zebra_set_state (zh, 'o', seqno);
1904 yaz_log (log_level, "nothing to commit");
1908 zebra_unlock (zh->lock_shadow);
1909 zebra_unlock (zh->lock_normal);
1913 ZEBRA_RES zebra_clean(ZebraHandle zh)
1916 yaz_log(log_level, "zebra_clean");
1917 return zebra_commit_ex(zh, 1);
1920 ZEBRA_RES zebra_commit(ZebraHandle zh)
1923 yaz_log(log_level, "zebra_commit");
1924 return zebra_commit_ex(zh, 0);
1927 ZEBRA_RES zebra_init(ZebraHandle zh)
1932 yaz_log(log_level, "zebra_init");
1933 zebra_clearError(zh);
1935 zebra_select_default_database(zh);
1938 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1939 "cannot select default database");
1942 rval = res_get (zh->res, "shadow");
1944 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1947 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1951 bf_cache (bfs, rval);
1955 zebra_set_state (zh, 'o', 0);
1959 ZEBRA_RES zebra_compact(ZebraHandle zh)
1963 yaz_log(log_level, "zebra_compact");
1964 zebra_clearError(zh);
1967 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1970 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1976 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1979 yaz_log(log_level, "zebra_result");
1980 *code = zh->errCode;
1981 *addinfo = zh->errString;
1984 void zebra_shadow_enable(ZebraHandle zh, int value)
1987 yaz_log(log_level, "zebra_shadow_enable");
1988 zebra_clearError(zh);
1989 zh->shadow_enable = value;
1992 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1996 yaz_log(log_level, "zebra_octet_term_encoding");
1997 zebra_clearError(zh);
1999 if (zh->iconv_to_utf8 != 0)
2000 yaz_iconv_close(zh->iconv_to_utf8);
2001 if (zh->iconv_from_utf8 != 0)
2002 yaz_iconv_close(zh->iconv_from_utf8);
2005 yaz_iconv_open ("UTF-8", encoding);
2006 if (zh->iconv_to_utf8 == 0)
2007 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2008 zh->iconv_from_utf8 =
2009 yaz_iconv_open (encoding, "UTF-8");
2010 if (zh->iconv_to_utf8 == 0)
2011 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2016 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2019 yaz_log(log_level, "zebra_record_encoding");
2020 zebra_clearError(zh);
2021 xfree(zh->record_encoding);
2022 zh->record_encoding = 0;
2024 zh->record_encoding = xstrdup (encoding);
2028 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2033 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2034 zebra_clearError(zh);
2035 res_set(zh->res, name, value);
2038 const char *zebra_get_resource(ZebraHandle zh,
2039 const char *name, const char *defaultvalue)
2044 v = res_get_def (zh->res, name, (char *)defaultvalue);
2045 zebra_clearError(zh);
2046 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2050 /* moved from zebra_api_ext.c by pop */
2051 /* FIXME: Should this really be public??? -Heikki */
2053 int zebra_trans_no (ZebraHandle zh)
2056 yaz_log(log_level, "zebra_trans_no");
2057 return zh->trans_no;
2060 int zebra_get_shadow_enable (ZebraHandle zh)
2063 yaz_log(log_level, "zebra_get_shadow_enable");
2064 return zh->shadow_enable;
2067 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2070 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2071 zh->shadow_enable = value;
2074 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2075 so that it's identicical to the original api_records_retrieve */
2076 void api_records_retrieve (ZebraHandle zh, ODR stream,
2077 const char *setname, Z_RecordComposition *comp,
2078 oid_value input_format, int num_recs,
2079 ZebraRetrievalRecord *recs)
2081 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2085 /* ---------------------------------------------------------------------------
2086 Record insert(=update), delete
2088 If sysno is provided, then it's used to identify the record.
2089 If not, and match_criteria is provided, then sysno is guessed
2090 If not, and a record is provided, then sysno is got from there
2091 NOTE: Now returns 0 at success and updates sysno, which is an int*
2095 int zebra_add_record(ZebraHandle zh,
2096 const char *buf, int buf_size)
2099 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2102 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2103 const char *recordType,
2104 SYSNO *sysno, const char *match,
2106 const char *buf, int buf_size, int force_update)
2112 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2115 buf_size = strlen(buf);
2117 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2119 res = buffer_extract_record (zh, buf, buf_size,
2120 0, /* delete_flag */
2126 0); /* allow_update */
2127 zebra_end_trans(zh);
2131 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2132 const char *recordType,
2133 SYSNO* sysno, const char *match,
2135 const char *buf, int buf_size,
2143 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2145 if (buf_size < 1) buf_size = strlen(buf);
2147 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2149 res = buffer_extract_record (zh, buf, buf_size,
2150 0, /* delete_flag */
2156 1); /* allow_update */
2157 zebra_end_trans(zh);
2161 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2162 const char *recordType,
2163 SYSNO *sysno, const char *match,
2165 const char *buf, int buf_size,
2172 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2174 if (buf_size < 1) buf_size = strlen(buf);
2176 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2178 res = buffer_extract_record (zh, buf, buf_size,
2179 1, /* delete_flag */
2185 1); /* allow_update */
2186 zebra_end_trans(zh);
2190 /* ---------------------------------------------------------------------------
2194 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2195 const char *setname, zint *hits)
2198 ZEBRA_RES res = ZEBRA_OK;
2200 ODR odr = odr_createmem(ODR_ENCODE);
2205 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2207 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2211 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2212 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2216 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2220 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2228 /* ---------------------------------------------------------------------------
2229 Sort - a simplified interface, with optional read locks.
2231 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2232 const char *sort_spec,
2233 const char *output_setname,
2234 const char **input_setnames)
2236 int num_input_setnames = 0;
2237 int sort_status = 0;
2238 Z_SortKeySpecList *sort_sequence;
2242 assert(output_setname);
2243 assert(input_setnames);
2244 sort_sequence = yaz_sort_spec (stream, sort_spec);
2245 yaz_log(log_level, "sort (FIXME) ");
2248 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2249 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2253 /* we can do this, since the perl typemap code for char** will
2254 put a NULL at the end of list */
2255 while (input_setnames[num_input_setnames]) num_input_setnames++;
2257 if (zebra_begin_read (zh))
2260 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2261 output_setname, sort_sequence, &sort_status);
2267 /* ---------------------------------------------------------------------------
2268 Get BFS for Zebra system (to make alternative storage methods)
2270 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2273 return zh->reg->bfs;
2278 /* ---------------------------------------------------------------------------
2279 Set limit for search/scan
2281 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2284 zebra_limit_destroy(zh->m_limit);
2285 zh->m_limit = zebra_limit_create(complement_flag, ids);
2290 Set Error code + addinfo
2292 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2295 nmem_reset(zh->nmem_error);
2296 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2299 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2302 sprintf(vstr, ZINT_FORMAT, i);
2305 nmem_reset(zh->nmem_error);
2306 zh->errString = nmem_strdup(zh->nmem_error, vstr);