1 /* $Id: zebraapi.c,v 1.224 2006-08-14 10:40:15 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <yaz/diagbib1.h>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
42 #include <idzebra/api.h>
44 #define DEFAULT_APPROX_LIMIT 2000000000
46 /* simple asserts to validate the most essential input args */
47 #define ASSERTZH assert(zh && zh->service)
48 #define ASSERTZHRES assert(zh && zh->service && zh->res)
49 #define ASSERTZS assert(zs)
51 static int log_level = 0;
52 static int log_level_initialized = 0;
54 static void zebra_open_res(ZebraHandle zh);
55 static void zebra_close_res(ZebraHandle zh);
57 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
64 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
66 static void zebra_chdir (ZebraService zs)
70 yaz_log(log_level, "zebra_chdir");
71 dir = res_get (zs->global_res, "chdir");
74 yaz_log (YLOG_DEBUG, "chdir %s", dir);
82 static ZEBRA_RES zebra_flush_reg (ZebraHandle zh)
84 ZEBRA_CHECK_HANDLE(zh);
85 yaz_log(log_level, "zebra_flush_reg");
86 zebraExplain_flush (zh->reg->zei, zh);
88 extract_flushWriteKeys (zh, 1 /* final */);
89 zebra_index_merge (zh );
93 static struct zebra_register *zebra_register_open(ZebraService zs,
95 int rw, int useshadow,
97 const char *reg_path);
98 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
100 ZebraHandle zebra_open(ZebraService zs, Res res)
103 const char *default_encoding;
104 if (!log_level_initialized)
106 log_level = yaz_log_module_level("zebraapi");
107 log_level_initialized = 1;
110 yaz_log(log_level, "zebra_open");
115 zh = (ZebraHandle) xmalloc(sizeof(*zh));
116 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
119 zh->reg = 0; /* no register attached yet */
125 zh->session_res = res_open(zs->global_res, res);
127 zh->dbaccesslist = 0;
129 zh->reg_name = xstrdup ("");
131 zh->num_basenames = 0;
134 zh->approx_limit = DEFAULT_APPROX_LIMIT;
141 zh->shadow_enable = 1;
142 zh->m_staticrank = 0;
144 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
147 yaz_iconv_open ("UTF-8", default_encoding);
148 if (zh->iconv_to_utf8 == 0)
149 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
151 zh->iconv_from_utf8 =
152 yaz_iconv_open (default_encoding, "UTF-8");
153 if (zh->iconv_to_utf8 == 0)
154 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
157 zh->record_encoding = 0;
159 zebra_mutex_cond_lock (&zs->session_lock);
161 zh->next = zs->sessions;
164 zebra_mutex_cond_unlock (&zs->session_lock);
166 zh->store_data_buf = 0;
168 zh->m_limit = zebra_limit_create(1, 0);
170 zh->nmem_error = nmem_create();
175 ZebraService zebra_start (const char *configName)
177 return zebra_start_res(configName, 0, 0);
180 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
186 if (!log_level_initialized)
188 log_level = yaz_log_module_level("zebraapi");
189 log_level_initialized = 1;
192 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
193 configName ? configName : "");
195 if ((res = res_open(def_res, over_res)))
197 const char *passwd_plain = 0;
198 const char *passwd_encrypt = 0;
199 const char *dbaccess = 0;
204 ZEBRA_RES ret = res_read_file(res, configName);
211 zh = xmalloc(sizeof(*zh));
212 zh->global_res = res;
217 zebra_mutex_cond_init (&zh->session_lock);
218 passwd_plain = res_get (zh->global_res, "passwd");
219 passwd_encrypt = res_get (zh->global_res, "passwd.c");
220 dbaccess = res_get (zh->global_res, "dbaccess");
222 if (!passwd_plain && !passwd_encrypt)
223 zh->passwd_db = NULL;
226 zh->passwd_db = passwd_db_open();
228 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
232 passwd_db_file_plain(zh->passwd_db, passwd_plain);
234 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
241 zh->dbaccess = res_open(NULL, NULL);
242 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
243 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
248 zh->path_root = res_get (zh->global_res, "root");
249 zh->nmem = nmem_create();
250 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
254 const char *module_path = res_get(res, "modulePath");
256 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
264 void zebra_filter_info(ZebraService zs, void *cd,
265 void (*cb)(void *cd, const char *name))
269 recTypeClass_info(zs->record_classes, cd, cb);
272 void zebra_pidfname(ZebraService zs, char *path)
275 zebra_lock_prefix (zs->global_res, path);
276 strcat(path, "zebrasrv.pid");
279 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
280 int compact_flag, Res res)
282 int page_size = 4096;
283 char resource_str[200];
284 sprintf (resource_str, "dict.%.100s.pagesize", name);
288 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
289 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
291 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
295 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
296 int rw, int useshadow, Res res,
297 const char *reg_path)
299 struct zebra_register *reg;
300 int record_compression = REC_COMPRESS_NONE;
301 const char *recordCompression = 0;
302 const char *profilePath;
304 ZEBRA_RES ret = ZEBRA_OK;
308 reg = xmalloc(sizeof(*reg));
311 reg->name = xstrdup (name);
318 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
319 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
321 reg->dh = data1_create();
328 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
331 data1_destroy(reg->dh);
338 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
340 bfs_destroy(reg->bfs);
341 data1_destroy(reg->dh);
348 getcwd(cwd, sizeof(cwd)-1);
349 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
350 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
352 data1_set_tabpath (reg->dh, profilePath);
353 data1_set_tabroot (reg->dh, reg_path);
354 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
357 zebra_maps_open(res, reg_path, profilePath);
358 if (!reg->zebra_maps)
360 recTypes_destroy(reg->recTypes);
361 bfs_destroy(reg->bfs);
362 data1_destroy(reg->dh);
367 reg->rank_classes = NULL;
371 reg->keys = zebra_rec_keys_open();
373 reg->sortKeys = zebra_rec_keys_open();
383 reg->key_file_no = 0;
386 /* installing rank classes */
387 zebraRankInstall (reg, rank_1_class);
388 zebraRankInstall (reg, rank_similarity_class);
389 zebraRankInstall (reg, rank_static_class);
391 recordCompression = res_get_def (res, "recordCompression", "none");
392 if (!strcmp (recordCompression, "none"))
393 record_compression = REC_COMPRESS_NONE;
394 if (!strcmp (recordCompression, "bzip2"))
395 record_compression = REC_COMPRESS_BZIP2;
399 const char *index_fname = res_get_def(res, "index", "default.idx");
400 if (index_fname && *index_fname)
402 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
407 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
409 yaz_log (YLOG_WARN, "rec_open failed");
414 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
416 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
418 yaz_log (YLOG_WARN, "dict_open failed");
421 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
423 yaz_log (YLOG_WARN, "sortIdx_open failed");
426 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
428 struct ISAMS_M_s isams_m;
429 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
430 key_isams_m(res, &isams_m))))
432 yaz_log (YLOG_WARN, "isams_open failed");
436 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
438 struct ISAMC_M_s isamc_m;
439 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
440 rw, key_isamc_m(res, &isamc_m))))
442 yaz_log (YLOG_WARN, "isamc_open failed");
446 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
448 struct ISAMC_M_s isamc_m;
450 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
451 rw, key_isamc_m(res, &isamc_m), 0)))
453 yaz_log (YLOG_WARN, "isamb_open failed");
457 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
459 struct ISAMC_M_s isamc_m;
461 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
462 rw, key_isamc_m(res, &isamc_m), 1)))
464 yaz_log (YLOG_WARN, "isamb_open failed");
468 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
470 struct ISAMC_M_s isamc_m;
472 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
473 rw, key_isamc_m(res, &isamc_m), -1)))
475 yaz_log (YLOG_WARN, "isamb_open failed");
481 reg->zei = zebraExplain_open(reg->records, reg->dh,
483 zebra_extract_explain);
486 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
493 zebra_register_close(zs, reg);
496 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
500 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
502 ZEBRA_CHECK_HANDLE(zh);
503 yaz_log(log_level, "zebra_admin_shutdown");
505 zebra_mutex_cond_lock (&zh->service->session_lock);
506 zh->service->stop_flag = 1;
507 zebra_mutex_cond_unlock (&zh->service->session_lock);
511 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
514 ZEBRA_CHECK_HANDLE(zh);
515 yaz_log(log_level, "zebra_admin_start");
517 zebra_mutex_cond_lock (&zs->session_lock);
518 zebra_mutex_cond_unlock (&zs->session_lock);
522 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
526 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
530 zebraExplain_close (reg->zei);
531 dict_close (reg->dict);
533 dict_close (reg->matchDict);
534 sortIdx_close (reg->sortIdx);
536 isams_close (reg->isams);
538 isamc_close (reg->isamc);
540 isamb_close (reg->isamb);
541 rec_close (®->records);
543 recTypes_destroy (reg->recTypes);
544 zebra_maps_close (reg->zebra_maps);
545 zebraRankDestroy (reg);
546 bfs_destroy (reg->bfs);
547 data1_destroy (reg->dh);
549 zebra_rec_keys_close(reg->keys);
550 zebra_rec_keys_close(reg->sortKeys);
557 ZEBRA_RES zebra_stop(ZebraService zs)
561 yaz_log (log_level, "zebra_stop");
565 zebra_close (zs->sessions);
568 zebra_mutex_cond_destroy (&zs->session_lock);
571 passwd_db_close (zs->passwd_db);
573 recTypeClass_destroy(zs->record_classes);
574 nmem_destroy(zs->nmem);
575 res_close (zs->global_res);
580 ZEBRA_RES zebra_close(ZebraHandle zh)
583 struct zebra_session **sp;
586 yaz_log(log_level, "zebra_close");
587 ZEBRA_CHECK_HANDLE(zh);
592 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
593 resultSetDestroy (zh, -1, 0, 0);
596 zebra_register_close(zh->service, zh->reg);
597 zebra_close_res (zh);
598 res_close(zh->session_res);
600 xfree(zh->record_encoding);
602 xfree(zh->dbaccesslist);
604 for (i = 0; i < zh->num_basenames; i++)
605 xfree(zh->basenames[i]);
606 xfree(zh->basenames);
608 if (zh->iconv_to_utf8 != 0)
609 yaz_iconv_close (zh->iconv_to_utf8);
610 if (zh->iconv_from_utf8 != 0)
611 yaz_iconv_close (zh->iconv_from_utf8);
613 zebra_mutex_cond_lock (&zs->session_lock);
614 zebra_lock_destroy (zh->lock_normal);
615 zebra_lock_destroy (zh->lock_shadow);
627 zebra_mutex_cond_unlock (&zs->session_lock);
629 xfree(zh->user_perm);
630 zh->service = 0; /* more likely to trigger an assert */
632 zebra_limit_destroy(zh->m_limit);
634 nmem_destroy(zh->nmem_error);
641 struct map_baseinfo {
647 char **new_basenames;
651 static void zebra_open_res(ZebraHandle zh)
659 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
660 zh->res = res_open(zh->session_res, 0);
661 res_read_file(zh->res, fname);
663 else if (*zh->reg_name == 0)
665 zh->res = res_open(zh->session_res, 0);
669 yaz_log (YLOG_WARN, "no register root specified");
670 zh->res = 0; /* no path for register - fail! */
674 static void zebra_close_res (ZebraHandle zh)
682 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
686 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
690 assert (zh->reg == 0);
691 assert (*zh->reg_name == 0);
697 resultSetInvalidate (zh);
698 zebra_register_close(zh->service, zh->reg);
704 zh->reg_name = xstrdup (new_reg);
708 if (zh->service->path_root)
710 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
711 strlen(zh->reg_name) + 3);
712 strcpy (zh->path_reg, zh->service->path_root);
715 strcat (zh->path_reg, "/");
716 strcat (zh->path_reg, zh->reg_name);
722 zebra_lock_destroy (zh->lock_normal);
726 zebra_lock_destroy (zh->lock_shadow);
732 const char *lock_area = res_get (zh->res, "lockDir");
734 if (!lock_area && zh->path_reg)
735 res_set (zh->res, "lockDir", zh->path_reg);
736 sprintf (fname, "norm.%s.LCK", zh->reg_name);
738 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
740 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
742 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
744 if (!zh->lock_normal || !zh->lock_shadow)
748 zebra_lock_destroy(zh->lock_normal);
753 zebra_lock_destroy(zh->lock_shadow);
762 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
763 zebra_set_approx_limit(zh, approx);
767 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
768 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
772 void map_basenames_func (void *vp, const char *name, const char *value)
774 struct map_baseinfo *p = (struct map_baseinfo *) vp;
776 char fromdb[128], todb[8][128];
783 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
784 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
785 todb[5], todb[6], todb[7]);
789 for (i = 0; i<p->num_bases; i++)
790 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
793 for (i = 0; i < no; i++)
795 if (p->new_num_bases == p->new_num_max)
797 p->new_basenames[(p->new_num_bases)++] =
798 nmem_strdup (p->mem, todb[i]);
804 int zebra_select_default_database(ZebraHandle zh)
808 /* no database has been selected - so we select based on
809 resource setting (including group)
811 const char *group = res_get(zh->session_res, "group");
812 const char *v = res_get_prefix(zh->session_res,
813 "database", group, "Default");
814 return zebra_select_database(zh, v);
819 void map_basenames (ZebraHandle zh, ODR stream,
820 int *num_bases, char ***basenames)
822 struct map_baseinfo info;
823 struct map_baseinfo *p = &info;
826 yaz_log(log_level, "map_basenames ");
831 info.num_bases = *num_bases;
832 info.basenames = *basenames;
833 info.new_num_max = 128;
834 info.new_num_bases = 0;
835 info.new_basenames = (char **)
836 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
837 info.mem = stream->mem;
839 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
841 for (i = 0; i<p->num_bases; i++)
842 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
844 p->new_basenames[(p->new_num_bases)++] =
845 nmem_strdup (p->mem, p->basenames[i]);
847 *num_bases = info.new_num_bases;
848 *basenames = info.new_basenames;
849 for (i = 0; i<*num_bases; i++)
850 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
853 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
855 ZEBRA_CHECK_HANDLE(zh);
857 yaz_log(log_level, "zebra_select_database %s",basename);
859 return zebra_select_databases (zh, 1, &basename);
862 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
863 const char **basenames)
870 ZEBRA_CHECK_HANDLE(zh);
873 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
874 num_bases,basenames[0]);
879 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
883 /* Check if the user has access to all databases (Seb) */
884 /* You could argue that this should happen later, after we have
885 * determined that the database(s) exist. */
886 if (zh->dbaccesslist) {
887 for (i = 0; i < num_bases; i++) {
888 const char *db = basenames[i];
890 for (p = zh->dbaccesslist; p && *p; p = pp) {
892 if ((pp = strchr(p, '+'))) {
898 if (len == strlen(db) && !strncmp(db, p, len))
902 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
908 for (i = 0; i < zh->num_basenames; i++)
909 xfree(zh->basenames[i]);
910 xfree(zh->basenames);
912 zh->num_basenames = num_bases;
913 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
914 for (i = 0; i < zh->num_basenames; i++)
915 zh->basenames[i] = xstrdup (basenames[i]);
917 cp = strrchr(basenames[0], '/');
920 len = cp - basenames[0];
921 new_reg = xmalloc(len + 1);
922 memcpy (new_reg, basenames[0], len);
926 new_reg = xstrdup ("");
927 for (i = 1; i<num_bases; i++)
931 cp1 = strrchr (basenames[i], '/');
936 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
939 if (len != cp1 - basenames[i] ||
940 memcmp (basenames[i], new_reg, len))
942 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
950 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
955 zebra_select_register (zh, new_reg);
959 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
962 if (!zh->lock_normal || !zh->lock_shadow)
964 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
970 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
972 if (approx_limit == 0)
973 approx_limit = DEFAULT_APPROX_LIMIT;
974 zh->approx_limit = approx_limit;
978 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
979 const char *setname, zint *hits)
983 ZEBRA_CHECK_HANDLE(zh);
989 yaz_log(log_level, "zebra_search_rpn");
993 if (zebra_begin_read(zh) == ZEBRA_FAIL)
996 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
997 zh->num_basenames, zh->basenames, setname);
1003 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1004 const char *setname,
1005 Z_RecordComposition *comp,
1006 oid_value input_format, int num_recs,
1007 ZebraRetrievalRecord *recs)
1009 ZebraMetaRecord *poset;
1011 ZEBRA_RES ret = ZEBRA_OK;
1014 ZEBRA_CHECK_HANDLE(zh);
1020 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1024 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1029 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1032 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1033 for (i = 0; i<num_recs; i++)
1034 pos_array[i] = recs[i].position;
1035 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1038 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1039 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1045 for (i = 0; i<num_recs; i++)
1049 recs[i].errCode = 0;
1050 recs[i].format = VAL_SUTRS;
1051 recs[i].len = strlen(poset[i].term);
1052 recs[i].buf = poset[i].term;
1053 recs[i].base = poset[i].db;
1055 else if (poset[i].sysno)
1059 zebra_snippets *hit_snippet = zebra_snippets_create();
1061 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1065 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1067 stream, input_format, comp,
1068 &recs[i].format, &buf, &len,
1069 &recs[i].base, &recs[i].errString);
1074 recs[i].buf = (char*) odr_malloc(stream, len);
1075 memcpy(recs[i].buf, buf, len);
1079 recs[i].score = poset[i].score;
1080 recs[i].sysno = poset[i].sysno;
1081 zebra_snippets_destroy(hit_snippet);
1085 /* only need to set it once */
1086 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1088 zebra_setError_zint(zh,
1089 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1094 recs[i].buf = 0; /* no record and no error issued */
1096 recs[i].errCode = 0;
1097 recs[i].format = VAL_NONE;
1101 zebra_meta_records_destroy(zh, poset, num_recs);
1103 zebra_end_read (zh);
1108 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1110 int *num_entries, ZebraScanEntry **entries,
1112 const char *setname)
1114 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1115 Z_AttributesPlusTerm *zapt;
1119 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1122 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1125 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1126 position, num_entries, entries, is_partial,
1128 yaz_pqf_destroy (pqf_parser);
1132 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1133 oid_value attributeset,
1135 int *num_entries, ZebraScanEntry **entries,
1137 const char *setname)
1140 RSET limit_rset = 0;
1142 ZEBRA_CHECK_HANDLE(zh);
1147 assert(num_entries);
1150 yaz_log(log_level, "zebra_scan");
1152 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1160 limit_rset = resultSetRef(zh, setname);
1164 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1166 zebra_end_read (zh);
1170 res = rpn_scan (zh, stream, zapt, attributeset,
1171 zh->num_basenames, zh->basenames, position,
1172 num_entries, entries, is_partial, limit_rset, 0);
1173 zebra_end_read (zh);
1177 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1178 int num_input_setnames, const char **input_setnames,
1179 const char *output_setname,
1180 Z_SortKeySpecList *sort_sequence,
1184 ZEBRA_CHECK_HANDLE(zh);
1186 assert(num_input_setnames>0);
1187 assert(input_setnames);
1188 assert(sort_sequence);
1189 assert(sort_status);
1190 yaz_log(log_level, "zebra_sort");
1192 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1194 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1195 output_setname, sort_sequence, sort_status);
1200 int zebra_deleteResultSet(ZebraHandle zh, int function,
1201 int num_setnames, char **setnames,
1207 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1209 if (zebra_begin_read(zh))
1210 return Z_DeleteStatus_systemProblemAtTarget;
1213 case Z_DeleteResultSetRequest_list:
1214 assert(num_setnames>0);
1216 resultSetDestroy (zh, num_setnames, setnames, statuses);
1218 case Z_DeleteResultSetRequest_all:
1219 resultSetDestroy (zh, -1, 0, statuses);
1222 zebra_end_read (zh);
1223 status = Z_DeleteStatus_success;
1224 for (i = 0; i<num_setnames; i++)
1225 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1226 status = statuses[i];
1230 int zebra_errCode (ZebraHandle zh)
1234 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1237 yaz_log(log_level, "zebra_errCode: o");
1241 const char *zebra_errString (ZebraHandle zh)
1245 e= diagbib1_str (zh->errCode);
1246 yaz_log(log_level, "zebra_errString: %s",e);
1250 char *zebra_errAdd (ZebraHandle zh)
1255 yaz_log(log_level, "zebra_errAdd: %s",a);
1259 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1262 const char *astring;
1266 ZEBRA_CHECK_HANDLE(zh);
1270 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1271 p = res_get(zs->global_res, u);
1272 xfree(zh->user_perm);
1273 zh->user_perm = xstrdup(p ? p : "r");
1275 /* Determine database access list */
1276 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1278 zh->dbaccesslist = xstrdup(astring);
1280 zh->dbaccesslist = 0;
1282 /* users that don't require a password .. */
1283 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1286 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1291 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1292 const char *record_type)
1294 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1295 database, record_type);
1296 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1298 return zebra_begin_trans(zh, 1);
1301 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1303 ZEBRA_CHECK_HANDLE(zh);
1304 yaz_log(log_level, "zebra_admin_import_end");
1305 return zebra_end_trans(zh);
1308 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1310 ZEBRA_RES res = ZEBRA_OK;
1313 ZEBRA_CHECK_HANDLE(zh);
1314 yaz_log(log_level, "zebra_admin_import_segment");
1316 for (i = 0; i<segment->num_segmentRecords; i++)
1318 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1320 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1322 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1323 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1325 Odr_oct *oct = fragment->u.notExternallyTagged;
1328 if (zebra_update_record(zh,
1329 0, /* record Type */
1333 (const char *) oct->buf, oct->len,
1342 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1343 const char *rec_buf,
1345 const char *recid_buf, size_t recid_len,
1347 /* 1 = insert. Fail it already exists */
1348 /* 2 = replace. Fail it does not exist */
1349 /* 3 = delete. Fail if does not exist */
1350 /* 4 = update. Insert/replace */
1357 ZEBRA_CHECK_HANDLE(zh);
1358 assert(action>0 && action <=4);
1361 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1363 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1365 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1366 "no record ID or empty record ID");
1370 memcpy (recid_z, recid_buf, recid_len);
1371 recid_z[recid_len] = 0;
1373 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1376 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1377 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1380 if (action == 1) /* fail if insert */
1382 if (zebra_end_trans(zh) != ZEBRA_OK)
1383 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1384 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1385 "Cannot insert record: already exist");
1389 memcpy (&sysno, rinfo+1, sizeof(sysno));
1393 if (action == 2 || action == 3) /* fail if delete or update */
1395 if (zebra_end_trans(zh) != ZEBRA_OK)
1396 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1397 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1398 "Cannot delete/update record: does not exist");
1401 action = 1; /* make it an insert (if it's an update).. */
1403 res = zebra_buffer_extract_record(zh, rec_buf, rec_len,
1404 action == 3 ? 1 : 0 /* delete flag */,
1410 0, /* force update */
1411 1 /* allow update */
1413 if (res == ZEBRA_FAIL)
1415 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1416 "Unable to parse record");
1420 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1421 sizeof(sysno), &sysno);
1423 else if (action == 3)
1425 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1427 if (zebra_end_trans(zh) != ZEBRA_OK)
1429 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1435 int delete_w_handle(const char *info, void *handle)
1437 ZebraHandle zh = (ZebraHandle) handle;
1441 if (*info == sizeof(pos))
1443 memcpy (&pos, info+1, sizeof(pos));
1444 isamb_unlink(zh->reg->isamb, pos);
1449 static int delete_SU_handle(void *handle, int ord)
1451 ZebraHandle zh = (ZebraHandle) handle;
1455 ord_len = key_SU_encode (ord, ord_buf);
1456 ord_buf[ord_len] = '\0';
1458 assert (zh->reg->isamb);
1459 dict_delete_subtree(zh->reg->dict, ord_buf,
1460 zh, delete_w_handle);
1464 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1466 ZEBRA_RES ret = ZEBRA_OK;
1468 yaz_log(log_level, "zebra_drop_database %s", db);
1469 ZEBRA_CHECK_HANDLE(zh);
1471 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1473 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1478 zebraExplain_curDatabase (zh->reg->zei, db);
1479 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1480 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1481 0 /* handle */, 0 /* func */);
1482 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1483 zebraExplain_removeDatabase(zh->reg->zei, zh);
1487 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1488 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1489 "drop database only supported for isam:b");
1492 if (zebra_end_trans (zh) != ZEBRA_OK)
1494 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1500 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1502 yaz_log(log_level, "zebra_create_database %s", db);
1503 ZEBRA_CHECK_HANDLE(zh);
1506 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1508 if (zebra_begin_trans (zh, 1))
1511 /* announce database */
1512 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1513 /* explainDatabase */))
1515 if (zebra_end_trans (zh) != ZEBRA_OK)
1517 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1519 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1522 return zebra_end_trans (zh);
1525 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1526 const char *input_str, int input_len,
1527 char *output_str, int output_len)
1533 yaz_log(log_level, "zebra_string_norm ");
1535 if (!zh->reg->zebra_maps)
1537 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1538 input_str, input_len);
1541 if (wrbuf_len(wrbuf) >= output_len)
1543 if (wrbuf_len(wrbuf))
1544 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1545 output_str[wrbuf_len(wrbuf)] = '\0';
1546 return wrbuf_len(wrbuf);
1549 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1551 char state_fname[256];
1556 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1558 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1559 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1560 f = fopen (fname, "w");
1562 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1563 fprintf (f, "%c %d %ld\n", val, seqno, p);
1568 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1570 char state_fname[256];
1575 yaz_log(log_level, "zebra_get_state ");
1577 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1578 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1579 f = fopen (fname, "r");
1585 fscanf (f, "%c %d", val, seqno);
1591 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1593 return zebra_begin_trans(zh, 0);
1596 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1598 return zebra_end_trans(zh);
1601 static void read_res_for_transaction(ZebraHandle zh)
1603 const char *group = res_get(zh->res, "group");
1605 /* FIXME - do we still use groups ?? */
1607 zh->m_group = group;
1608 v = res_get_prefix(zh->res, "followLinks", group, "1");
1609 zh->m_follow_links = atoi(v);
1611 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1612 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1614 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1615 zh->m_store_keys = atoi(v);
1617 v = res_get_prefix(zh->res, "storeData", group, "1");
1618 zh->m_store_data = atoi(v);
1620 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1621 zh->m_explain_database = atoi(v);
1623 v = res_get_prefix(zh->res, "openRW", group, "1");
1624 zh->m_flag_rw = atoi(v);
1626 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1627 zh->m_file_verbose_limit = atoi(v);
1630 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1632 ZEBRA_CHECK_HANDLE(zh);
1633 zebra_select_default_database(zh);
1636 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1637 "zebra_begin_trans: no database selected");
1641 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1645 if (rw && !strchr(zh->user_perm, 'w'))
1649 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1661 const char *rval = 0;
1666 read_res_for_transaction(zh);
1669 if (zh->trans_no != 1)
1671 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1672 "zebra_begin_trans: no write trans within read");
1677 resultSetInvalidate (zh);
1678 zebra_register_close(zh->service, zh->reg);
1680 zh->trans_w_no = zh->trans_no;
1682 zh->records_inserted = 0;
1683 zh->records_updated = 0;
1684 zh->records_deleted = 0;
1685 zh->records_processed = 0;
1687 #if HAVE_SYS_TIMES_H
1691 if (zh->shadow_enable)
1692 rval = res_get (zh->res, "shadow");
1694 for (pass = 0; pass < 2; pass++)
1698 zebra_lock_r (zh->lock_normal);
1699 zebra_lock_w (zh->lock_shadow);
1703 zebra_lock_w (zh->lock_normal);
1704 zebra_lock_w (zh->lock_shadow);
1707 zebra_get_state (zh, &val, &seqno);
1710 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1711 zebra_unlock (zh->lock_shadow);
1712 zebra_unlock (zh->lock_normal);
1716 else if (val == 'd')
1720 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1722 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1723 bf_commitClean (bfs, rval);
1728 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1735 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1739 zebra_set_state (zh, 'd', seqno);
1741 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1742 1, rval ? 1 : 0, zh->res,
1745 zh->reg->seqno = seqno;
1748 zebra_set_state (zh, 'o', seqno);
1750 zebra_unlock (zh->lock_shadow);
1751 zebra_unlock (zh->lock_normal);
1756 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1757 "zebra_begin_trans: cannot open register");
1758 yaz_log(YLOG_FATAL, "%s", zh->errString);
1761 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1771 if (zh->trans_no != 1)
1773 return zebra_flush_reg (zh);
1775 #if HAVE_SYS_TIMES_H
1781 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1784 if (!zh->lock_normal || !zh->lock_shadow)
1787 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1790 zebra_get_state (zh, &val, &seqno);
1796 else if (seqno != zh->reg->seqno)
1798 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1799 seqno, zh->reg->seqno);
1802 else if (zh->reg->last_val != val)
1804 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1805 val, zh->reg->last_val);
1812 zebra_lock_r (zh->lock_shadow);
1814 zebra_lock_r (zh->lock_normal);
1818 resultSetInvalidate (zh);
1819 zebra_register_close(zh->service, zh->reg);
1821 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1822 0, val == 'c' ? 1 : 0,
1823 zh->res, zh->path_reg);
1826 zebra_unlock (zh->lock_normal);
1827 zebra_unlock (zh->lock_shadow);
1829 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1832 zh->reg->last_val = val;
1833 zh->reg->seqno = seqno;
1835 read_res_for_transaction(zh);
1839 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1841 ZebraTransactionStatus dummy;
1843 yaz_log(log_level, "zebra_end_trans");
1844 ZEBRA_CHECK_HANDLE(zh);
1845 return zebra_end_transaction(zh, &dummy);
1848 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1854 ZEBRA_CHECK_HANDLE(zh);
1857 yaz_log(log_level, "zebra_end_transaction");
1859 status->processed = 0;
1860 status->inserted = 0;
1861 status->updated = 0;
1862 status->deleted = 0;
1866 if (!zh->res || !zh->reg)
1868 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1869 "zebra_end_trans: no open transaction");
1872 if (zh->trans_no != zh->trans_w_no)
1875 if (zh->trans_no != 0)
1878 /* release read lock */
1880 zebra_unlock (zh->lock_normal);
1881 zebra_unlock (zh->lock_shadow);
1884 { /* release write lock */
1888 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1889 rval = res_get (zh->res, "shadow");
1891 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1893 zebra_flush_reg (zh);
1895 resultSetInvalidate (zh);
1897 zebra_register_close(zh->service, zh->reg);
1900 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1901 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1902 zh->records_processed, zh->records_inserted,
1903 zh->records_updated, zh->records_deleted);
1905 status->processed = zh->records_processed;
1906 status->inserted = zh->records_inserted;
1907 status->updated = zh->records_updated;
1908 status->deleted = zh->records_deleted;
1910 zebra_get_state (zh, &val, &seqno);
1913 BFiles bfs = bfs_create (rval, zh->path_reg);
1914 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1915 bf_commitClean (bfs, rval);
1920 zebra_set_state (zh, 'o', seqno);
1922 zebra_unlock (zh->lock_shadow);
1923 zebra_unlock (zh->lock_normal);
1926 #if HAVE_SYS_TIMES_H
1928 yaz_log (log_level, "user/system: %ld/%ld",
1929 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1930 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1932 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1933 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1938 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
1942 yaz_log (log_level, "updating %s", path);
1944 if (zh->m_record_id && !strcmp (zh->m_record_id, "file"))
1945 return zebra_update_file_match(zh, path);
1947 return zebra_update_from_path(zh, path);
1950 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
1954 yaz_log (log_level, "deleting %s", path);
1955 return zebra_delete_from_path(zh, path);
1958 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
1962 yaz_log(log_level, "zebra_repository_show");
1963 repositoryShow (zh, path);
1967 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1975 zebra_select_default_database(zh);
1978 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1981 rval = res_get (zh->res, "shadow");
1984 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1988 zebra_lock_w (zh->lock_normal);
1989 zebra_lock_r (zh->lock_shadow);
1991 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1994 zebra_unlock(zh->lock_shadow);
1995 zebra_unlock(zh->lock_normal);
1998 zebra_get_state (zh, &val, &seqno);
2001 bf_cache (bfs, rval);
2002 if (bf_commitExists (bfs))
2005 zebra_set_state (zh, 'd', seqno);
2008 zebra_set_state (zh, 'c', seqno);
2010 yaz_log (YLOG_DEBUG, "commit start");
2011 bf_commitExec (bfs);
2017 zebra_set_state (zh, 'o', seqno);
2019 zebra_unlock (zh->lock_shadow);
2020 zebra_unlock (zh->lock_normal);
2022 zebra_lock_w(zh->lock_shadow);
2023 bf_commitClean (bfs, rval);
2024 zebra_unlock (zh->lock_shadow);
2028 zebra_unlock(zh->lock_shadow);
2029 zebra_unlock(zh->lock_normal);
2030 yaz_log (log_level, "nothing to commit");
2037 ZEBRA_RES zebra_clean(ZebraHandle zh)
2039 yaz_log(log_level, "zebra_clean");
2040 ZEBRA_CHECK_HANDLE(zh);
2041 return zebra_commit_ex(zh, 1);
2044 ZEBRA_RES zebra_commit(ZebraHandle zh)
2046 yaz_log(log_level, "zebra_commit");
2047 ZEBRA_CHECK_HANDLE(zh);
2048 return zebra_commit_ex(zh, 0);
2052 ZEBRA_RES zebra_init(ZebraHandle zh)
2057 yaz_log(log_level, "zebra_init");
2059 ZEBRA_CHECK_HANDLE(zh);
2061 zebra_select_default_database(zh);
2064 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2065 "cannot select default database");
2068 rval = res_get (zh->res, "shadow");
2070 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2073 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2077 bf_cache (bfs, rval);
2081 zebra_set_state (zh, 'o', 0);
2085 ZEBRA_RES zebra_compact(ZebraHandle zh)
2089 yaz_log(log_level, "zebra_compact");
2090 ZEBRA_CHECK_HANDLE(zh);
2093 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2096 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2102 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2104 yaz_log(log_level, "zebra_result");
2107 *code = zh->errCode;
2108 *addinfo = zh->errString;
2112 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2113 *addinfo ="ZebraHandle is NULL";
2117 void zebra_shadow_enable(ZebraHandle zh, int value)
2120 yaz_log(log_level, "zebra_shadow_enable");
2121 zh->shadow_enable = value;
2124 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2126 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2127 ZEBRA_CHECK_HANDLE(zh);
2130 if (zh->iconv_to_utf8 != 0)
2131 yaz_iconv_close(zh->iconv_to_utf8);
2132 if (zh->iconv_from_utf8 != 0)
2133 yaz_iconv_close(zh->iconv_from_utf8);
2136 yaz_iconv_open ("UTF-8", encoding);
2137 if (zh->iconv_to_utf8 == 0)
2138 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2139 zh->iconv_from_utf8 =
2140 yaz_iconv_open (encoding, "UTF-8");
2141 if (zh->iconv_to_utf8 == 0)
2142 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2147 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2149 yaz_log(log_level, "zebra_record_encoding");
2150 ZEBRA_CHECK_HANDLE(zh);
2151 xfree(zh->record_encoding);
2152 zh->record_encoding = 0;
2154 zh->record_encoding = xstrdup (encoding);
2158 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2162 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2164 res_set(zh->res, name, value);
2167 const char *zebra_get_resource(ZebraHandle zh,
2168 const char *name, const char *defaultvalue)
2173 v = res_get_def (zh->res, name, (char *)defaultvalue);
2174 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2178 /* moved from zebra_api_ext.c by pop */
2179 /* FIXME: Should this really be public??? -Heikki */
2181 int zebra_trans_no (ZebraHandle zh)
2183 yaz_log(log_level, "zebra_trans_no");
2185 return zh->trans_no;
2188 int zebra_get_shadow_enable (ZebraHandle zh)
2190 yaz_log(log_level, "zebra_get_shadow_enable");
2192 return zh->shadow_enable;
2195 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2197 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2199 zh->shadow_enable = value;
2202 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2203 const char *buf, int buf_size)
2205 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2208 ZEBRA_RES zebra_insert_record(ZebraHandle zh,
2209 const char *recordType,
2210 SYSNO *sysno, const char *match,
2212 const char *buf, int buf_size, int force_update)
2218 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2221 buf_size = strlen(buf);
2223 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2225 res = zebra_buffer_extract_record(zh, buf, buf_size,
2226 0, /* delete_flag */
2232 0); /* allow_update */
2233 if (zebra_end_trans(zh) != ZEBRA_OK)
2235 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2241 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2242 const char *recordType,
2243 SYSNO* sysno, const char *match,
2245 const char *buf, int buf_size,
2250 ZEBRA_CHECK_HANDLE(zh);
2254 yaz_log(log_level, "zebra_update_record");
2256 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2258 if (buf_size < 1) buf_size = strlen(buf);
2260 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2262 res = zebra_buffer_extract_record(zh, buf, buf_size,
2263 0, /* delete_flag */
2269 1); /* allow_update */
2270 if (zebra_end_trans(zh) != ZEBRA_OK)
2272 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2278 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2279 const char *recordType,
2280 SYSNO *sysno, const char *match,
2282 const char *buf, int buf_size,
2287 ZEBRA_CHECK_HANDLE(zh);
2290 yaz_log(log_level, "zebra_delete_record");
2292 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2294 if (buf_size < 1) buf_size = strlen(buf);
2296 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2298 res = zebra_buffer_extract_record(zh, buf, buf_size,
2299 1, /* delete_flag */
2305 1); /* allow_update */
2306 if (zebra_end_trans(zh) != ZEBRA_OK)
2308 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2314 /* ---------------------------------------------------------------------------
2318 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2319 const char *setname, zint *hits)
2322 ZEBRA_RES res = ZEBRA_OK;
2326 ZEBRA_CHECK_HANDLE(zh);
2328 odr = odr_createmem(ODR_ENCODE);
2333 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2335 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2339 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2340 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2344 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2348 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2356 /* ---------------------------------------------------------------------------
2357 Sort - a simplified interface, with optional read locks.
2359 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2360 const char *sort_spec,
2361 const char *output_setname,
2362 const char **input_setnames)
2364 int num_input_setnames = 0;
2365 int sort_status = 0;
2366 Z_SortKeySpecList *sort_sequence;
2368 ZEBRA_CHECK_HANDLE(zh);
2371 assert(output_setname);
2372 assert(input_setnames);
2373 sort_sequence = yaz_sort_spec (stream, sort_spec);
2374 yaz_log(log_level, "sort (FIXME) ");
2377 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2378 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2382 /* we can do this, since the perl typemap code for char** will
2383 put a NULL at the end of list */
2384 while (input_setnames[num_input_setnames]) num_input_setnames++;
2386 if (zebra_begin_read (zh))
2389 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2390 output_setname, sort_sequence, &sort_status);
2396 /* ---------------------------------------------------------------------------
2397 Get BFS for Zebra system (to make alternative storage methods)
2399 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2402 return zh->reg->bfs;
2407 /* ---------------------------------------------------------------------------
2408 Set limit for search/scan
2410 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2412 ZEBRA_CHECK_HANDLE(zh);
2413 zebra_limit_destroy(zh->m_limit);
2414 zh->m_limit = zebra_limit_create(complement_flag, ids);
2419 Set Error code + addinfo
2421 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2426 nmem_reset(zh->nmem_error);
2427 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2430 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2433 sprintf(vstr, ZINT_FORMAT, i);
2436 nmem_reset(zh->nmem_error);
2437 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2440 void zebra_lock_prefix (Res res, char *path)
2442 const char *lock_dir = res_get_def (res, "lockDir", "");
2444 strcpy (path, lock_dir);
2445 if (*path && path[strlen(path)-1] != '/')
2452 * indent-tabs-mode: nil
2454 * vim: shiftwidth=4 tabstop=8 expandtab