1 /* $Id: zebraapi.c,v 1.212 2006-04-05 02:10:20 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>
41 #include <idzebra/api.h>
43 #define DEFAULT_APPROX_LIMIT 2000000000
45 /* simple asserts to validate the most essential input args */
46 #define ASSERTZH assert(zh && zh->service)
47 #define ASSERTZHRES assert(zh && zh->service && zh->res)
48 #define ASSERTZS assert(zs)
50 static int log_level = 0;
51 static int log_level_initialized = 0;
53 static void zebra_open_res(ZebraHandle zh);
54 static void zebra_close_res(ZebraHandle zh);
56 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
63 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
65 static void zebra_chdir (ZebraService zs)
69 yaz_log(log_level, "zebra_chdir");
70 dir = res_get (zs->global_res, "chdir");
73 yaz_log (YLOG_DEBUG, "chdir %s", dir);
81 static ZEBRA_RES zebra_flush_reg (ZebraHandle zh)
83 ZEBRA_CHECK_HANDLE(zh);
84 yaz_log(log_level, "zebra_flush_reg");
85 zebraExplain_flush (zh->reg->zei, zh);
87 extract_flushWriteKeys (zh, 1 /* final */);
88 zebra_index_merge (zh );
92 static struct zebra_register *zebra_register_open(ZebraService zs,
94 int rw, int useshadow,
96 const char *reg_path);
97 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
99 ZebraHandle zebra_open(ZebraService zs, Res res)
102 const char *default_encoding;
103 if (!log_level_initialized)
105 log_level = yaz_log_module_level("zebraapi");
106 log_level_initialized = 1;
109 yaz_log(log_level, "zebra_open");
114 zh = (ZebraHandle) xmalloc(sizeof(*zh));
115 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
118 zh->reg = 0; /* no register attached yet */
124 zh->session_res = res_open(zs->global_res, res);
126 zh->dbaccesslist = 0;
128 zh->reg_name = xstrdup ("");
130 zh->num_basenames = 0;
133 zh->approx_limit = DEFAULT_APPROX_LIMIT;
140 zh->shadow_enable = 1;
141 zh->m_staticrank = 0;
143 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
146 yaz_iconv_open ("UTF-8", default_encoding);
147 if (zh->iconv_to_utf8 == 0)
148 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
150 zh->iconv_from_utf8 =
151 yaz_iconv_open (default_encoding, "UTF-8");
152 if (zh->iconv_to_utf8 == 0)
153 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
156 zh->record_encoding = 0;
158 zebra_mutex_cond_lock (&zs->session_lock);
160 zh->next = zs->sessions;
163 zebra_mutex_cond_unlock (&zs->session_lock);
165 zh->store_data_buf = 0;
167 zh->m_limit = zebra_limit_create(1, 0);
169 zh->nmem_error = nmem_create();
174 ZebraService zebra_start (const char *configName)
176 return zebra_start_res(configName, 0, 0);
179 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
183 if (!log_level_initialized)
185 log_level = yaz_log_module_level("zebraapi");
186 log_level_initialized = 1;
189 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
190 configName ? configName : "");
192 if ((res = res_open(def_res, over_res)))
194 const char *passwd_plain = 0;
195 const char *passwd_encrypt = 0;
196 const char *dbaccess = 0;
201 ZEBRA_RES ret = res_read_file(res, configName);
208 zh = xmalloc(sizeof(*zh));
209 zh->global_res = res;
214 zebra_mutex_cond_init (&zh->session_lock);
215 passwd_plain = res_get (zh->global_res, "passwd");
216 passwd_encrypt = res_get (zh->global_res, "passwd.c");
217 dbaccess = res_get (zh->global_res, "dbaccess");
219 if (!passwd_plain && !passwd_encrypt)
220 zh->passwd_db = NULL;
223 zh->passwd_db = passwd_db_open();
225 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
229 passwd_db_file_plain(zh->passwd_db, passwd_plain);
231 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
238 zh->dbaccess = res_open(NULL, NULL);
239 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
240 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
245 zh->path_root = res_get (zh->global_res, "root");
246 zh->nmem = nmem_create();
247 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
253 void zebra_filter_info(ZebraService zs, void *cd,
254 void (*cb)(void *cd, const char *name))
258 recTypeClass_info(zs->record_classes, cd, cb);
261 void zebra_pidfname(ZebraService zs, char *path)
264 zebra_lock_prefix (zs->global_res, path);
265 strcat(path, "zebrasrv.pid");
268 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
269 int compact_flag, Res res)
271 int page_size = 4096;
272 char resource_str[200];
273 sprintf (resource_str, "dict.%.100s.pagesize", name);
277 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
278 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
280 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
284 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
285 int rw, int useshadow, Res res,
286 const char *reg_path)
288 struct zebra_register *reg;
289 int record_compression = REC_COMPRESS_NONE;
290 const char *recordCompression = 0;
291 const char *profilePath;
293 ZEBRA_RES ret = ZEBRA_OK;
297 reg = xmalloc(sizeof(*reg));
300 reg->name = xstrdup (name);
307 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
308 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
310 reg->dh = data1_create();
317 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
320 data1_destroy(reg->dh);
327 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
329 bfs_destroy(reg->bfs);
330 data1_destroy(reg->dh);
337 getcwd(cwd, sizeof(cwd)-1);
338 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
339 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
341 data1_set_tabpath (reg->dh, profilePath);
342 data1_set_tabroot (reg->dh, reg_path);
343 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
346 zebra_maps_open(res, reg_path, profilePath);
347 if (!reg->zebra_maps)
349 recTypes_destroy(reg->recTypes);
350 bfs_destroy(reg->bfs);
351 data1_destroy(reg->dh);
356 reg->rank_classes = NULL;
360 reg->keys = zebra_rec_keys_open();
363 reg->sortKeys = zebra_rec_keys_open();
365 reg->sortKeys.buf = 0;
366 reg->sortKeys.buf_max = 0;
377 reg->key_file_no = 0;
380 zebraRankInstall (reg, rank_1_class);
381 zebraRankInstall (reg, rank_zv_class);
382 zebraRankInstall (reg, rank_static_class);
384 recordCompression = res_get_def (res, "recordCompression", "none");
385 if (!strcmp (recordCompression, "none"))
386 record_compression = REC_COMPRESS_NONE;
387 if (!strcmp (recordCompression, "bzip2"))
388 record_compression = REC_COMPRESS_BZIP2;
392 const char *index_fname = res_get_def(res, "index", "default.idx");
393 if (index_fname && *index_fname)
395 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
400 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
402 yaz_log (YLOG_WARN, "rec_open failed");
407 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
409 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
411 yaz_log (YLOG_WARN, "dict_open failed");
414 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
416 yaz_log (YLOG_WARN, "sortIdx_open failed");
419 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
421 struct ISAMS_M_s isams_m;
422 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
423 key_isams_m(res, &isams_m))))
425 yaz_log (YLOG_WARN, "isams_open failed");
429 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
431 struct ISAMC_M_s isamc_m;
432 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
433 rw, key_isamc_m(res, &isamc_m))))
435 yaz_log (YLOG_WARN, "isamc_open failed");
439 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
441 struct ISAMC_M_s isamc_m;
443 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
444 rw, key_isamc_m(res, &isamc_m), 0)))
446 yaz_log (YLOG_WARN, "isamb_open failed");
450 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
452 struct ISAMC_M_s isamc_m;
454 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
455 rw, key_isamc_m(res, &isamc_m), 1)))
457 yaz_log (YLOG_WARN, "isamb_open failed");
461 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
463 struct ISAMC_M_s isamc_m;
465 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
466 rw, key_isamc_m(res, &isamc_m), -1)))
468 yaz_log (YLOG_WARN, "isamb_open failed");
474 reg->zei = zebraExplain_open(reg->records, reg->dh,
479 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
486 zebra_register_close(zs, reg);
489 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
493 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
495 ZEBRA_CHECK_HANDLE(zh);
496 yaz_log(log_level, "zebra_admin_shutdown");
498 zebra_mutex_cond_lock (&zh->service->session_lock);
499 zh->service->stop_flag = 1;
500 zebra_mutex_cond_unlock (&zh->service->session_lock);
504 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
507 ZEBRA_CHECK_HANDLE(zh);
508 yaz_log(log_level, "zebra_admin_start");
510 zebra_mutex_cond_lock (&zs->session_lock);
511 zebra_mutex_cond_unlock (&zs->session_lock);
515 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
519 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
523 zebraExplain_close (reg->zei);
524 dict_close (reg->dict);
526 dict_close (reg->matchDict);
527 sortIdx_close (reg->sortIdx);
529 isams_close (reg->isams);
531 isamc_close (reg->isamc);
533 isamb_close (reg->isamb);
534 rec_close (®->records);
536 recTypes_destroy (reg->recTypes);
537 zebra_maps_close (reg->zebra_maps);
538 zebraRankDestroy (reg);
539 bfs_destroy (reg->bfs);
540 data1_destroy (reg->dh);
542 zebra_rec_keys_close(reg->keys);
544 zebra_rec_keys_close(reg->sortKeys);
546 xfree(reg->sortKeys.buf);
554 ZEBRA_RES zebra_stop(ZebraService zs)
558 yaz_log (log_level, "zebra_stop");
562 zebra_close (zs->sessions);
565 zebra_mutex_cond_destroy (&zs->session_lock);
568 passwd_db_close (zs->passwd_db);
570 recTypeClass_destroy(zs->record_classes);
571 nmem_destroy(zs->nmem);
572 res_close (zs->global_res);
577 ZEBRA_RES zebra_close (ZebraHandle zh)
580 struct zebra_session **sp;
583 yaz_log(log_level, "zebra_close");
584 ZEBRA_CHECK_HANDLE(zh);
589 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
590 resultSetDestroy (zh, -1, 0, 0);
593 zebra_register_close(zh->service, zh->reg);
594 zebra_close_res (zh);
595 res_close(zh->session_res);
597 xfree(zh->record_encoding);
599 xfree(zh->dbaccesslist);
601 for (i = 0; i < zh->num_basenames; i++)
602 xfree(zh->basenames[i]);
603 xfree(zh->basenames);
605 if (zh->iconv_to_utf8 != 0)
606 yaz_iconv_close (zh->iconv_to_utf8);
607 if (zh->iconv_from_utf8 != 0)
608 yaz_iconv_close (zh->iconv_from_utf8);
610 zebra_mutex_cond_lock (&zs->session_lock);
611 zebra_lock_destroy (zh->lock_normal);
612 zebra_lock_destroy (zh->lock_shadow);
624 zebra_mutex_cond_unlock (&zs->session_lock);
626 xfree(zh->user_perm);
627 zh->service = 0; /* more likely to trigger an assert */
629 zebra_limit_destroy(zh->m_limit);
631 nmem_destroy(zh->nmem_error);
638 struct map_baseinfo {
644 char **new_basenames;
648 static void zebra_open_res(ZebraHandle zh)
656 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
657 zh->res = res_open(zh->session_res, 0);
658 res_read_file(zh->res, fname);
660 else if (*zh->reg_name == 0)
662 zh->res = res_open(zh->session_res, 0);
666 yaz_log (YLOG_WARN, "no register root specified");
667 zh->res = 0; /* no path for register - fail! */
671 static void zebra_close_res (ZebraHandle zh)
679 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
683 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
687 assert (zh->reg == 0);
688 assert (*zh->reg_name == 0);
694 resultSetInvalidate (zh);
695 zebra_register_close(zh->service, zh->reg);
701 zh->reg_name = xstrdup (new_reg);
705 if (zh->service->path_root)
707 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
708 strlen(zh->reg_name) + 3);
709 strcpy (zh->path_reg, zh->service->path_root);
712 strcat (zh->path_reg, "/");
713 strcat (zh->path_reg, zh->reg_name);
719 zebra_lock_destroy (zh->lock_normal);
723 zebra_lock_destroy (zh->lock_shadow);
729 const char *lock_area = res_get (zh->res, "lockDir");
731 if (!lock_area && zh->path_reg)
732 res_set (zh->res, "lockDir", zh->path_reg);
733 sprintf (fname, "norm.%s.LCK", zh->reg_name);
735 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
737 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
739 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
741 if (!zh->lock_normal || !zh->lock_shadow)
745 zebra_lock_destroy(zh->lock_normal);
750 zebra_lock_destroy(zh->lock_shadow);
759 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
760 zebra_set_approx_limit(zh, approx);
764 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
765 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
769 void map_basenames_func (void *vp, const char *name, const char *value)
771 struct map_baseinfo *p = (struct map_baseinfo *) vp;
773 char fromdb[128], todb[8][128];
780 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
781 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
782 todb[5], todb[6], todb[7]);
786 for (i = 0; i<p->num_bases; i++)
787 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
790 for (i = 0; i < no; i++)
792 if (p->new_num_bases == p->new_num_max)
794 p->new_basenames[(p->new_num_bases)++] =
795 nmem_strdup (p->mem, todb[i]);
801 int zebra_select_default_database(ZebraHandle zh)
805 /* no database has been selected - so we select based on
806 resource setting (including group)
808 const char *group = res_get(zh->session_res, "group");
809 const char *v = res_get_prefix(zh->session_res,
810 "database", group, "Default");
811 return zebra_select_database(zh, v);
816 void map_basenames (ZebraHandle zh, ODR stream,
817 int *num_bases, char ***basenames)
819 struct map_baseinfo info;
820 struct map_baseinfo *p = &info;
823 yaz_log(log_level, "map_basenames ");
828 info.num_bases = *num_bases;
829 info.basenames = *basenames;
830 info.new_num_max = 128;
831 info.new_num_bases = 0;
832 info.new_basenames = (char **)
833 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
834 info.mem = stream->mem;
836 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
838 for (i = 0; i<p->num_bases; i++)
839 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
841 p->new_basenames[(p->new_num_bases)++] =
842 nmem_strdup (p->mem, p->basenames[i]);
844 *num_bases = info.new_num_bases;
845 *basenames = info.new_basenames;
846 for (i = 0; i<*num_bases; i++)
847 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
850 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
852 ZEBRA_CHECK_HANDLE(zh);
854 yaz_log(log_level, "zebra_select_database %s",basename);
856 return zebra_select_databases (zh, 1, &basename);
859 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
860 const char **basenames)
867 ZEBRA_CHECK_HANDLE(zh);
870 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
871 num_bases,basenames[0]);
876 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
880 /* Check if the user has access to all databases (Seb) */
881 /* You could argue that this should happen later, after we have
882 * determined that the database(s) exist. */
883 if (zh->dbaccesslist) {
884 for (i = 0; i < num_bases; i++) {
885 const char *db = basenames[i];
887 for (p = zh->dbaccesslist; p && *p; p = pp) {
889 if ((pp = strchr(p, '+'))) {
895 if (len == strlen(db) && !strncmp(db, p, len))
899 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
905 for (i = 0; i < zh->num_basenames; i++)
906 xfree(zh->basenames[i]);
907 xfree(zh->basenames);
909 zh->num_basenames = num_bases;
910 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
911 for (i = 0; i < zh->num_basenames; i++)
912 zh->basenames[i] = xstrdup (basenames[i]);
914 cp = strrchr(basenames[0], '/');
917 len = cp - basenames[0];
918 new_reg = xmalloc(len + 1);
919 memcpy (new_reg, basenames[0], len);
923 new_reg = xstrdup ("");
924 for (i = 1; i<num_bases; i++)
928 cp1 = strrchr (basenames[i], '/');
933 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
936 if (len != cp1 - basenames[i] ||
937 memcmp (basenames[i], new_reg, len))
939 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
947 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
952 zebra_select_register (zh, new_reg);
956 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
959 if (!zh->lock_normal || !zh->lock_shadow)
961 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
967 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
969 if (approx_limit == 0)
970 approx_limit = DEFAULT_APPROX_LIMIT;
971 zh->approx_limit = approx_limit;
975 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
976 const char *setname, zint *hits)
980 ZEBRA_CHECK_HANDLE(zh);
986 yaz_log(log_level, "zebra_search_rpn");
990 if (zebra_begin_read(zh) == ZEBRA_FAIL)
993 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
994 zh->num_basenames, zh->basenames, setname);
1000 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1001 const char *setname,
1002 Z_RecordComposition *comp,
1003 oid_value input_format, int num_recs,
1004 ZebraRetrievalRecord *recs)
1006 ZebraMetaRecord *poset;
1008 ZEBRA_RES ret = ZEBRA_OK;
1011 ZEBRA_CHECK_HANDLE(zh);
1017 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1021 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1026 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1029 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1030 for (i = 0; i<num_recs; i++)
1031 pos_array[i] = recs[i].position;
1032 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1035 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1036 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1042 for (i = 0; i<num_recs; i++)
1046 recs[i].errCode = 0;
1047 recs[i].format = VAL_SUTRS;
1048 recs[i].len = strlen(poset[i].term);
1049 recs[i].buf = poset[i].term;
1050 recs[i].base = poset[i].db;
1052 else if (poset[i].sysno)
1056 zebra_snippets *hit_snippet = zebra_snippets_create();
1058 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1062 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1064 stream, input_format, comp,
1065 &recs[i].format, &buf, &len,
1066 &recs[i].base, &recs[i].errString);
1071 recs[i].buf = (char*) odr_malloc(stream, len);
1072 memcpy(recs[i].buf, buf, len);
1076 recs[i].score = poset[i].score;
1077 recs[i].sysno = poset[i].sysno;
1078 zebra_snippets_destroy(hit_snippet);
1082 /* only need to set it once */
1083 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1085 zebra_setError_zint(zh,
1086 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1091 recs[i].buf = 0; /* no record and no error issued */
1093 recs[i].errCode = 0;
1094 recs[i].format = VAL_NONE;
1098 zebra_meta_records_destroy(zh, poset, num_recs);
1100 zebra_end_read (zh);
1105 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1107 int *num_entries, ZebraScanEntry **entries,
1109 const char *setname)
1111 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1112 Z_AttributesPlusTerm *zapt;
1116 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1119 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1122 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1123 position, num_entries, entries, is_partial,
1125 yaz_pqf_destroy (pqf_parser);
1129 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1130 oid_value attributeset,
1132 int *num_entries, ZebraScanEntry **entries,
1134 const char *setname)
1137 RSET limit_rset = 0;
1139 ZEBRA_CHECK_HANDLE(zh);
1144 assert(num_entries);
1147 yaz_log(log_level, "zebra_scan");
1149 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1157 limit_rset = resultSetRef(zh, setname);
1161 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1163 zebra_end_read (zh);
1167 res = rpn_scan (zh, stream, zapt, attributeset,
1168 zh->num_basenames, zh->basenames, position,
1169 num_entries, entries, is_partial, limit_rset, 0);
1170 zebra_end_read (zh);
1174 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1175 int num_input_setnames, const char **input_setnames,
1176 const char *output_setname,
1177 Z_SortKeySpecList *sort_sequence,
1181 ZEBRA_CHECK_HANDLE(zh);
1183 assert(num_input_setnames>0);
1184 assert(input_setnames);
1185 assert(sort_sequence);
1186 assert(sort_status);
1187 yaz_log(log_level, "zebra_sort");
1189 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1191 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1192 output_setname, sort_sequence, sort_status);
1197 int zebra_deleteResultSet(ZebraHandle zh, int function,
1198 int num_setnames, char **setnames,
1204 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1206 if (zebra_begin_read(zh))
1207 return Z_DeleteStatus_systemProblemAtTarget;
1210 case Z_DeleteResultSetRequest_list:
1211 assert(num_setnames>0);
1213 resultSetDestroy (zh, num_setnames, setnames, statuses);
1215 case Z_DeleteResultSetRequest_all:
1216 resultSetDestroy (zh, -1, 0, statuses);
1219 zebra_end_read (zh);
1220 status = Z_DeleteStatus_success;
1221 for (i = 0; i<num_setnames; i++)
1222 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1223 status = statuses[i];
1227 int zebra_errCode (ZebraHandle zh)
1231 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1234 yaz_log(log_level, "zebra_errCode: o");
1238 const char *zebra_errString (ZebraHandle zh)
1242 e= diagbib1_str (zh->errCode);
1243 yaz_log(log_level, "zebra_errString: %s",e);
1247 char *zebra_errAdd (ZebraHandle zh)
1252 yaz_log(log_level, "zebra_errAdd: %s",a);
1256 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1259 const char *astring;
1263 ZEBRA_CHECK_HANDLE(zh);
1267 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1268 p = res_get(zs->global_res, u);
1269 xfree(zh->user_perm);
1270 zh->user_perm = xstrdup(p ? p : "r");
1272 /* Determine database access list */
1273 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1275 zh->dbaccesslist = xstrdup(astring);
1277 zh->dbaccesslist = 0;
1279 /* users that don't require a password .. */
1280 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1283 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1288 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1289 const char *record_type)
1291 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1292 database, record_type);
1293 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1295 return zebra_begin_trans(zh, 1);
1298 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1300 ZEBRA_CHECK_HANDLE(zh);
1301 yaz_log(log_level, "zebra_admin_import_end");
1302 return zebra_end_trans(zh);
1305 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1307 ZEBRA_RES res = ZEBRA_OK;
1310 ZEBRA_CHECK_HANDLE(zh);
1311 yaz_log(log_level, "zebra_admin_import_segment");
1313 for (i = 0; i<segment->num_segmentRecords; i++)
1315 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1317 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1319 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1320 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1322 Odr_oct *oct = fragment->u.notExternallyTagged;
1325 if (zebra_update_record(zh,
1326 0, /* record Type */
1330 (const char *) oct->buf, oct->len,
1339 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1340 const char *rec_buf,
1342 const char *recid_buf, size_t recid_len,
1344 /* 1 = insert. Fail it already exists */
1345 /* 2 = replace. Fail it does not exist */
1346 /* 3 = delete. Fail if does not exist */
1347 /* 4 = update. Insert/replace */
1354 ZEBRA_CHECK_HANDLE(zh);
1355 assert(action>0 && action <=4);
1358 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1360 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1362 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1363 "no record ID or empty record ID");
1367 memcpy (recid_z, recid_buf, recid_len);
1368 recid_z[recid_len] = 0;
1370 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1373 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1374 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1377 if (action == 1) /* fail if insert */
1379 if (zebra_end_trans(zh) != ZEBRA_OK)
1380 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1381 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1382 "Cannot insert record: already exist");
1386 memcpy (&sysno, rinfo+1, sizeof(sysno));
1390 if (action == 2 || action == 3) /* fail if delete or update */
1392 if (zebra_end_trans(zh) != ZEBRA_OK)
1393 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1394 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1395 "Cannot delete/update record: does not exist");
1398 action = 1; /* make it an insert (if it's an update).. */
1400 res = buffer_extract_record (zh, rec_buf, rec_len,
1401 action == 3 ? 1 : 0 /* delete flag */,
1407 0, /* force update */
1408 1 /* allow update */
1410 if (res == ZEBRA_FAIL)
1412 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1413 "Unable to parse record");
1417 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1418 sizeof(sysno), &sysno);
1420 else if (action == 3)
1422 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1424 if (zebra_end_trans(zh) != ZEBRA_OK)
1426 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1432 int delete_w_handle(const char *info, void *handle)
1434 ZebraHandle zh = (ZebraHandle) handle;
1438 if (*info == sizeof(pos))
1440 memcpy (&pos, info+1, sizeof(pos));
1441 isamb_unlink(zh->reg->isamb, pos);
1446 static int delete_SU_handle(void *handle, int ord)
1448 ZebraHandle zh = (ZebraHandle) handle;
1452 ord_len = key_SU_encode (ord, ord_buf);
1453 ord_buf[ord_len] = '\0';
1455 assert (zh->reg->isamb);
1456 dict_delete_subtree(zh->reg->dict, ord_buf,
1457 zh, delete_w_handle);
1461 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1463 ZEBRA_RES ret = ZEBRA_OK;
1465 yaz_log(log_level, "zebra_drop_database %s", db);
1466 ZEBRA_CHECK_HANDLE(zh);
1468 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1470 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1475 zebraExplain_curDatabase (zh->reg->zei, db);
1476 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1477 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1478 0 /* handle */, 0 /* func */);
1479 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1480 zebraExplain_removeDatabase(zh->reg->zei, zh);
1484 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1485 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1486 "drop database only supported for isam:b");
1489 if (zebra_end_trans (zh) != ZEBRA_OK)
1491 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1497 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1499 yaz_log(log_level, "zebra_create_database %s", db);
1500 ZEBRA_CHECK_HANDLE(zh);
1503 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1505 if (zebra_begin_trans (zh, 1))
1508 /* announce database */
1509 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1510 /* explainDatabase */))
1512 if (zebra_end_trans (zh) != ZEBRA_OK)
1514 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1516 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1519 return zebra_end_trans (zh);
1522 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1523 const char *input_str, int input_len,
1524 char *output_str, int output_len)
1530 yaz_log(log_level, "zebra_string_norm ");
1532 if (!zh->reg->zebra_maps)
1534 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1535 input_str, input_len);
1538 if (wrbuf_len(wrbuf) >= output_len)
1540 if (wrbuf_len(wrbuf))
1541 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1542 output_str[wrbuf_len(wrbuf)] = '\0';
1543 return wrbuf_len(wrbuf);
1546 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1548 char state_fname[256];
1553 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1555 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1556 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1557 f = fopen (fname, "w");
1559 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1560 fprintf (f, "%c %d %ld\n", val, seqno, p);
1565 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1567 char state_fname[256];
1572 yaz_log(log_level, "zebra_get_state ");
1574 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1575 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1576 f = fopen (fname, "r");
1582 fscanf (f, "%c %d", val, seqno);
1588 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1590 return zebra_begin_trans(zh, 0);
1593 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1595 return zebra_end_trans(zh);
1598 static void read_res_for_transaction(ZebraHandle zh)
1600 const char *group = res_get(zh->res, "group");
1602 /* FIXME - do we still use groups ?? */
1604 zh->m_group = group;
1605 v = res_get_prefix(zh->res, "followLinks", group, "1");
1606 zh->m_follow_links = atoi(v);
1608 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1609 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1611 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1612 zh->m_store_keys = atoi(v);
1614 v = res_get_prefix(zh->res, "storeData", group, "1");
1615 zh->m_store_data = atoi(v);
1617 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1618 zh->m_explain_database = atoi(v);
1620 v = res_get_prefix(zh->res, "openRW", group, "1");
1621 zh->m_flag_rw = atoi(v);
1623 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1624 zh->m_file_verbose_limit = atoi(v);
1627 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1629 ZEBRA_CHECK_HANDLE(zh);
1630 zebra_select_default_database(zh);
1633 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1634 "zebra_begin_trans: no database selected");
1638 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1642 if (rw && !strchr(zh->user_perm, 'w'))
1646 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1658 const char *rval = 0;
1663 read_res_for_transaction(zh);
1666 if (zh->trans_no != 1)
1668 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1669 "zebra_begin_trans: no write trans within read");
1674 resultSetInvalidate (zh);
1675 zebra_register_close(zh->service, zh->reg);
1677 zh->trans_w_no = zh->trans_no;
1679 zh->records_inserted = 0;
1680 zh->records_updated = 0;
1681 zh->records_deleted = 0;
1682 zh->records_processed = 0;
1684 #if HAVE_SYS_TIMES_H
1688 if (zh->shadow_enable)
1689 rval = res_get (zh->res, "shadow");
1691 for (pass = 0; pass < 2; pass++)
1695 zebra_lock_r (zh->lock_normal);
1696 zebra_lock_w (zh->lock_shadow);
1700 zebra_lock_w (zh->lock_normal);
1701 zebra_lock_w (zh->lock_shadow);
1704 zebra_get_state (zh, &val, &seqno);
1707 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1708 zebra_unlock (zh->lock_shadow);
1709 zebra_unlock (zh->lock_normal);
1713 else if (val == 'd')
1717 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1719 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1720 bf_commitClean (bfs, rval);
1725 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1732 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1736 zebra_set_state (zh, 'd', seqno);
1738 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1739 1, rval ? 1 : 0, zh->res,
1742 zh->reg->seqno = seqno;
1745 zebra_set_state (zh, 'o', seqno);
1747 zebra_unlock (zh->lock_shadow);
1748 zebra_unlock (zh->lock_normal);
1753 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1754 "zebra_begin_trans: cannot open register");
1755 yaz_log(YLOG_FATAL, "%s", zh->errString);
1758 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1768 if (zh->trans_no != 1)
1770 return zebra_flush_reg (zh);
1772 #if HAVE_SYS_TIMES_H
1778 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1781 if (!zh->lock_normal || !zh->lock_shadow)
1784 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1787 zebra_get_state (zh, &val, &seqno);
1793 else if (seqno != zh->reg->seqno)
1795 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1796 seqno, zh->reg->seqno);
1799 else if (zh->reg->last_val != val)
1801 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1802 val, zh->reg->last_val);
1809 zebra_lock_r (zh->lock_shadow);
1811 zebra_lock_r (zh->lock_normal);
1815 resultSetInvalidate (zh);
1816 zebra_register_close(zh->service, zh->reg);
1818 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1819 0, val == 'c' ? 1 : 0,
1820 zh->res, zh->path_reg);
1823 zebra_unlock (zh->lock_normal);
1824 zebra_unlock (zh->lock_shadow);
1826 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1829 zh->reg->last_val = val;
1830 zh->reg->seqno = seqno;
1832 read_res_for_transaction(zh);
1836 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1838 ZebraTransactionStatus dummy;
1840 yaz_log(log_level, "zebra_end_trans");
1841 ZEBRA_CHECK_HANDLE(zh);
1842 return zebra_end_transaction(zh, &dummy);
1845 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1851 ZEBRA_CHECK_HANDLE(zh);
1854 yaz_log(log_level, "zebra_end_transaction");
1856 status->processed = 0;
1857 status->inserted = 0;
1858 status->updated = 0;
1859 status->deleted = 0;
1863 if (!zh->res || !zh->reg)
1865 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1866 "zebra_end_trans: no open transaction");
1869 if (zh->trans_no != zh->trans_w_no)
1872 if (zh->trans_no != 0)
1875 /* release read lock */
1877 zebra_unlock (zh->lock_normal);
1878 zebra_unlock (zh->lock_shadow);
1881 { /* release write lock */
1885 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1886 rval = res_get (zh->res, "shadow");
1888 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1890 zebra_flush_reg (zh);
1892 resultSetInvalidate (zh);
1894 zebra_register_close(zh->service, zh->reg);
1897 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1898 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1899 zh->records_processed, zh->records_inserted,
1900 zh->records_updated, zh->records_deleted);
1902 status->processed = (int) zh->records_processed;
1903 status->inserted = (int) zh->records_inserted;
1904 status->updated = (int) zh->records_updated;
1905 status->deleted = (int) zh->records_deleted;
1907 zebra_get_state (zh, &val, &seqno);
1910 BFiles bfs = bfs_create (rval, zh->path_reg);
1911 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1912 bf_commitClean (bfs, rval);
1917 zebra_set_state (zh, 'o', seqno);
1919 zebra_unlock (zh->lock_shadow);
1920 zebra_unlock (zh->lock_normal);
1923 #if HAVE_SYS_TIMES_H
1925 yaz_log (log_level, "user/system: %ld/%ld",
1926 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1927 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1929 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1930 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1935 int zebra_repository_update (ZebraHandle zh, const char *path)
1939 yaz_log (log_level, "updating %s", path);
1940 repositoryUpdate (zh, path);
1944 int zebra_repository_delete (ZebraHandle zh, const char *path)
1948 yaz_log (log_level, "deleting %s", path);
1949 repositoryDelete (zh, path);
1953 int zebra_repository_show (ZebraHandle zh, const char *path)
1957 yaz_log(log_level, "zebra_repository_show");
1958 repositoryShow (zh, path);
1962 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1970 zebra_select_default_database(zh);
1973 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1976 rval = res_get (zh->res, "shadow");
1979 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1983 zebra_lock_w (zh->lock_normal);
1984 zebra_lock_r (zh->lock_shadow);
1986 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1988 zebra_get_state (zh, &val, &seqno);
1991 bf_cache (bfs, rval);
1992 if (bf_commitExists (bfs))
1995 zebra_set_state (zh, 'd', seqno);
1998 zebra_set_state (zh, 'c', seqno);
2000 yaz_log (YLOG_DEBUG, "commit start");
2001 bf_commitExec (bfs);
2007 zebra_set_state (zh, 'o', seqno);
2009 zebra_unlock (zh->lock_shadow);
2010 zebra_unlock (zh->lock_normal);
2012 zebra_lock_w(zh->lock_shadow);
2013 bf_commitClean (bfs, rval);
2014 zebra_unlock (zh->lock_normal);
2018 zebra_unlock(zh->lock_shadow);
2019 zebra_unlock(zh->lock_normal);
2020 yaz_log (log_level, "nothing to commit");
2027 ZEBRA_RES zebra_clean(ZebraHandle zh)
2029 yaz_log(log_level, "zebra_clean");
2030 ZEBRA_CHECK_HANDLE(zh);
2031 return zebra_commit_ex(zh, 1);
2034 ZEBRA_RES zebra_commit(ZebraHandle zh)
2036 yaz_log(log_level, "zebra_commit");
2037 ZEBRA_CHECK_HANDLE(zh);
2038 return zebra_commit_ex(zh, 0);
2042 ZEBRA_RES zebra_init(ZebraHandle zh)
2047 yaz_log(log_level, "zebra_init");
2049 ZEBRA_CHECK_HANDLE(zh);
2051 zebra_select_default_database(zh);
2054 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2055 "cannot select default database");
2058 rval = res_get (zh->res, "shadow");
2060 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2063 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2067 bf_cache (bfs, rval);
2071 zebra_set_state (zh, 'o', 0);
2075 ZEBRA_RES zebra_compact(ZebraHandle zh)
2079 yaz_log(log_level, "zebra_compact");
2080 ZEBRA_CHECK_HANDLE(zh);
2083 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2086 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2092 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2094 yaz_log(log_level, "zebra_result");
2097 *code = zh->errCode;
2098 *addinfo = zh->errString;
2102 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2103 *addinfo ="ZebraHandle is NULL";
2107 void zebra_shadow_enable(ZebraHandle zh, int value)
2110 yaz_log(log_level, "zebra_shadow_enable");
2111 zh->shadow_enable = value;
2114 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2116 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2117 ZEBRA_CHECK_HANDLE(zh);
2120 if (zh->iconv_to_utf8 != 0)
2121 yaz_iconv_close(zh->iconv_to_utf8);
2122 if (zh->iconv_from_utf8 != 0)
2123 yaz_iconv_close(zh->iconv_from_utf8);
2126 yaz_iconv_open ("UTF-8", encoding);
2127 if (zh->iconv_to_utf8 == 0)
2128 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2129 zh->iconv_from_utf8 =
2130 yaz_iconv_open (encoding, "UTF-8");
2131 if (zh->iconv_to_utf8 == 0)
2132 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2137 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2139 yaz_log(log_level, "zebra_record_encoding");
2140 ZEBRA_CHECK_HANDLE(zh);
2141 xfree(zh->record_encoding);
2142 zh->record_encoding = 0;
2144 zh->record_encoding = xstrdup (encoding);
2148 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2152 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2154 res_set(zh->res, name, value);
2157 const char *zebra_get_resource(ZebraHandle zh,
2158 const char *name, const char *defaultvalue)
2163 v = res_get_def (zh->res, name, (char *)defaultvalue);
2164 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2168 /* moved from zebra_api_ext.c by pop */
2169 /* FIXME: Should this really be public??? -Heikki */
2171 int zebra_trans_no (ZebraHandle zh)
2173 yaz_log(log_level, "zebra_trans_no");
2175 return zh->trans_no;
2178 int zebra_get_shadow_enable (ZebraHandle zh)
2180 yaz_log(log_level, "zebra_get_shadow_enable");
2182 return zh->shadow_enable;
2185 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2187 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2189 zh->shadow_enable = value;
2192 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2193 so that it's identicical to the original api_records_retrieve */
2194 void api_records_retrieve (ZebraHandle zh, ODR stream,
2195 const char *setname, Z_RecordComposition *comp,
2196 oid_value input_format, int num_recs,
2197 ZebraRetrievalRecord *recs)
2199 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2203 /* ---------------------------------------------------------------------------
2204 Record insert(=update), delete
2206 If sysno is provided, then it's used to identify the record.
2207 If not, and match_criteria is provided, then sysno is guessed
2208 If not, and a record is provided, then sysno is got from there
2209 NOTE: Now returns 0 at success and updates sysno, which is an int*
2213 int zebra_add_record(ZebraHandle zh,
2214 const char *buf, int buf_size)
2216 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2219 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2220 const char *recordType,
2221 SYSNO *sysno, const char *match,
2223 const char *buf, int buf_size, int force_update)
2229 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2232 buf_size = strlen(buf);
2234 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2236 res = buffer_extract_record (zh, buf, buf_size,
2237 0, /* delete_flag */
2243 0); /* allow_update */
2244 if (zebra_end_trans(zh) != ZEBRA_OK)
2246 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2252 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2253 const char *recordType,
2254 SYSNO* sysno, const char *match,
2256 const char *buf, int buf_size,
2261 ZEBRA_CHECK_HANDLE(zh);
2265 yaz_log(log_level, "zebra_update_record");
2267 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2269 if (buf_size < 1) buf_size = strlen(buf);
2271 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2273 res = buffer_extract_record (zh, buf, buf_size,
2274 0, /* delete_flag */
2280 1); /* allow_update */
2281 if (zebra_end_trans(zh) != ZEBRA_OK)
2283 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2289 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2290 const char *recordType,
2291 SYSNO *sysno, const char *match,
2293 const char *buf, int buf_size,
2298 ZEBRA_CHECK_HANDLE(zh);
2302 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2304 if (buf_size < 1) buf_size = strlen(buf);
2306 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2308 res = buffer_extract_record (zh, buf, buf_size,
2309 1, /* delete_flag */
2315 1); /* allow_update */
2316 if (zebra_end_trans(zh) != ZEBRA_OK)
2318 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2324 /* ---------------------------------------------------------------------------
2328 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2329 const char *setname, zint *hits)
2332 ZEBRA_RES res = ZEBRA_OK;
2336 ZEBRA_CHECK_HANDLE(zh);
2338 odr = odr_createmem(ODR_ENCODE);
2343 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2345 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2349 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2350 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2354 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2358 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2366 /* ---------------------------------------------------------------------------
2367 Sort - a simplified interface, with optional read locks.
2369 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2370 const char *sort_spec,
2371 const char *output_setname,
2372 const char **input_setnames)
2374 int num_input_setnames = 0;
2375 int sort_status = 0;
2376 Z_SortKeySpecList *sort_sequence;
2378 ZEBRA_CHECK_HANDLE(zh);
2381 assert(output_setname);
2382 assert(input_setnames);
2383 sort_sequence = yaz_sort_spec (stream, sort_spec);
2384 yaz_log(log_level, "sort (FIXME) ");
2387 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2388 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2392 /* we can do this, since the perl typemap code for char** will
2393 put a NULL at the end of list */
2394 while (input_setnames[num_input_setnames]) num_input_setnames++;
2396 if (zebra_begin_read (zh))
2399 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2400 output_setname, sort_sequence, &sort_status);
2406 /* ---------------------------------------------------------------------------
2407 Get BFS for Zebra system (to make alternative storage methods)
2409 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2412 return zh->reg->bfs;
2417 /* ---------------------------------------------------------------------------
2418 Set limit for search/scan
2420 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2422 ZEBRA_CHECK_HANDLE(zh);
2423 zebra_limit_destroy(zh->m_limit);
2424 zh->m_limit = zebra_limit_create(complement_flag, ids);
2429 Set Error code + addinfo
2431 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2436 nmem_reset(zh->nmem_error);
2437 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2440 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2443 sprintf(vstr, ZINT_FORMAT, i);
2446 nmem_reset(zh->nmem_error);
2447 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2450 void zebra_lock_prefix (Res res, char *path)
2452 const char *lock_dir = res_get_def (res, "lockDir", "");
2454 strcpy (path, lock_dir);
2455 if (*path && path[strlen(path)-1] != '/')