1 /* $Id: zebraapi.c,v 1.195 2005-12-07 17:00:46 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 void 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, Res res)
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 */
114 zh->session_res = res_open(zs->global_res, res);
116 zh->dbaccesslist = 0;
118 zh->reg_name = xstrdup ("");
120 zh->num_basenames = 0;
123 zh->approx_limit = DEFAULT_APPROX_LIMIT;
130 zh->shadow_enable = 1;
131 zh->m_staticrank = 0;
133 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
136 yaz_iconv_open ("UTF-8", default_encoding);
137 if (zh->iconv_to_utf8 == 0)
138 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
140 zh->iconv_from_utf8 =
141 yaz_iconv_open (default_encoding, "UTF-8");
142 if (zh->iconv_to_utf8 == 0)
143 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
146 zh->record_encoding = 0;
148 zebra_mutex_cond_lock (&zs->session_lock);
150 zh->next = zs->sessions;
153 zebra_mutex_cond_unlock (&zs->session_lock);
155 zh->store_data_buf = 0;
157 zh->m_limit = zebra_limit_create(1, 0);
159 zh->nmem_error = nmem_create();
164 ZebraService zebra_start (const char *configName)
166 return zebra_start_res(configName, 0, 0);
169 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
173 if (!log_level_initialized)
175 log_level = yaz_log_module_level("zebraapi");
176 log_level_initialized = 1;
179 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
180 configName ? configName : "");
182 if ((res = res_open(def_res, over_res)))
184 const char *passwd_plain = 0;
185 const char *passwd_encrypt = 0;
186 const char *dbaccess = 0;
187 ZebraService zh = xmalloc(sizeof(*zh));
190 res_read_file(res, configName);
192 zh->global_res = res;
197 zebra_mutex_cond_init (&zh->session_lock);
198 passwd_plain = res_get (zh->global_res, "passwd");
199 passwd_encrypt = res_get (zh->global_res, "passwd.c");
200 dbaccess = res_get (zh->global_res, "dbaccess");
202 if (!passwd_plain && !passwd_encrypt)
203 zh->passwd_db = NULL;
206 zh->passwd_db = passwd_db_open();
208 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
212 passwd_db_file_plain(zh->passwd_db, passwd_plain);
214 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
221 zh->dbaccess = res_open(NULL, NULL);
222 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
223 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
228 zh->path_root = res_get (zh->global_res, "root");
229 zh->nmem = nmem_create();
230 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
236 void zebra_filter_info(ZebraService zs, void *cd,
237 void (*cb)(void *cd, const char *name))
241 recTypeClass_info(zs->record_classes, cd, cb);
244 void zebra_pidfname(ZebraService zs, char *path)
247 zebra_lock_prefix (zs->global_res, path);
248 strcat(path, "zebrasrv.pid");
251 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
252 int compact_flag, Res res)
254 int page_size = 4096;
255 char resource_str[200];
256 sprintf (resource_str, "dict.%.100s.pagesize", name);
260 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
261 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
263 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
267 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
268 int rw, int useshadow, Res res,
269 const char *reg_path)
271 struct zebra_register *reg;
272 int record_compression = REC_COMPRESS_NONE;
273 const char *recordCompression = 0;
274 const char *profilePath;
279 reg = xmalloc(sizeof(*reg));
282 reg->name = xstrdup (name);
289 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
290 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
292 reg->dh = data1_createx (DATA1_FLAG_XML);
298 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
301 data1_destroy(reg->dh);
307 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
309 bfs_destroy(reg->bfs);
310 data1_destroy(reg->dh);
316 getcwd(cwd, sizeof(cwd)-1);
317 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
318 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
320 data1_set_tabpath (reg->dh, profilePath);
321 data1_set_tabroot (reg->dh, reg_path);
322 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
324 reg->zebra_maps = zebra_maps_open (res, reg_path);
325 reg->rank_classes = NULL;
329 reg->keys = zebra_rec_keys_open();
332 reg->sortKeys = zebra_rec_keys_open();
334 reg->sortKeys.buf = 0;
335 reg->sortKeys.buf_max = 0;
347 reg->key_file_no = 0;
350 zebraRankInstall (reg, rank_1_class);
351 zebraRankInstall (reg, rank_zv_class);
352 zebraRankInstall (reg, rank_static_class);
354 recordCompression = res_get_def (res, "recordCompression", "none");
355 if (!strcmp (recordCompression, "none"))
356 record_compression = REC_COMPRESS_NONE;
357 if (!strcmp (recordCompression, "bzip2"))
358 record_compression = REC_COMPRESS_BZIP2;
360 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
362 yaz_log (YLOG_WARN, "rec_open failed");
367 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
369 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
371 yaz_log (YLOG_WARN, "dict_open failed");
374 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
376 yaz_log (YLOG_WARN, "sortIdx_open failed");
379 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
381 struct ISAMS_M_s isams_m;
382 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
383 key_isams_m(res, &isams_m))))
385 yaz_log (YLOG_WARN, "isams_open failed");
389 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
391 struct ISAMC_M_s isamc_m;
392 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
393 rw, key_isamc_m(res, &isamc_m))))
395 yaz_log (YLOG_WARN, "isamc_open failed");
399 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
401 struct ISAMC_M_s isamc_m;
403 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
404 rw, key_isamc_m(res, &isamc_m), 0)))
406 yaz_log (YLOG_WARN, "isamb_open failed");
410 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
412 struct ISAMC_M_s isamc_m;
414 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
415 rw, key_isamc_m(res, &isamc_m), 1)))
417 yaz_log (YLOG_WARN, "isamb_open failed");
421 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
423 struct ISAMC_M_s isamc_m;
425 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
426 rw, key_isamc_m(res, &isamc_m), -1)))
428 yaz_log (YLOG_WARN, "isamb_open failed");
432 reg->zei = zebraExplain_open (reg->records, reg->dh,
437 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
441 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
445 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
448 yaz_log(log_level, "zebra_admin_shutdown");
449 zebra_clearError(zh);
451 zebra_mutex_cond_lock (&zh->service->session_lock);
452 zh->service->stop_flag = 1;
453 zebra_mutex_cond_unlock (&zh->service->session_lock);
457 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
461 yaz_log(log_level, "zebra_admin_start");
462 zebra_clearError(zh);
464 zebra_mutex_cond_lock (&zs->session_lock);
465 zebra_mutex_cond_unlock (&zs->session_lock);
469 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
473 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
478 zebraExplain_close (reg->zei);
479 dict_close (reg->dict);
481 dict_close (reg->matchDict);
482 sortIdx_close (reg->sortIdx);
484 isams_close (reg->isams);
486 isamc_close (reg->isamc);
488 isamb_close (reg->isamb);
489 rec_close (®->records);
492 recTypes_destroy (reg->recTypes);
493 zebra_maps_close (reg->zebra_maps);
494 zebraRankDestroy (reg);
495 bfs_destroy (reg->bfs);
496 data1_destroy (reg->dh);
498 zebra_rec_keys_close(reg->keys);
500 zebra_rec_keys_close(reg->sortKeys);
502 xfree(reg->sortKeys.buf);
510 ZEBRA_RES zebra_stop(ZebraService zs)
514 yaz_log (log_level, "zebra_stop");
518 zebra_close (zs->sessions);
521 zebra_mutex_cond_destroy (&zs->session_lock);
524 passwd_db_close (zs->passwd_db);
526 recTypeClass_destroy(zs->record_classes);
527 nmem_destroy(zs->nmem);
528 res_close (zs->global_res);
533 ZEBRA_RES zebra_close (ZebraHandle zh)
536 struct zebra_session **sp;
539 yaz_log(log_level, "zebra_close");
546 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
547 resultSetDestroy (zh, -1, 0, 0);
550 zebra_register_close (zh->service, zh->reg);
551 zebra_close_res (zh);
552 res_close(zh->session_res);
554 xfree(zh->record_encoding);
556 xfree(zh->dbaccesslist);
558 for (i = 0; i < zh->num_basenames; i++)
559 xfree(zh->basenames[i]);
560 xfree(zh->basenames);
562 if (zh->iconv_to_utf8 != 0)
563 yaz_iconv_close (zh->iconv_to_utf8);
564 if (zh->iconv_from_utf8 != 0)
565 yaz_iconv_close (zh->iconv_from_utf8);
567 zebra_mutex_cond_lock (&zs->session_lock);
568 zebra_lock_destroy (zh->lock_normal);
569 zebra_lock_destroy (zh->lock_shadow);
581 zebra_mutex_cond_unlock (&zs->session_lock);
583 xfree(zh->user_perm);
584 zh->service = 0; /* more likely to trigger an assert */
586 zebra_limit_destroy(zh->m_limit);
588 nmem_destroy(zh->nmem_error);
595 struct map_baseinfo {
601 char **new_basenames;
605 static void zebra_open_res(ZebraHandle zh)
613 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
614 zh->res = res_open(zh->session_res, 0);
615 res_read_file(zh->res, fname);
617 else if (*zh->reg_name == 0)
619 zh->res = res_open(zh->session_res, 0);
623 yaz_log (YLOG_WARN, "no register root specified");
624 zh->res = 0; /* no path for register - fail! */
628 static void zebra_close_res (ZebraHandle zh)
636 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
640 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
644 assert (zh->reg == 0);
645 assert (*zh->reg_name == 0);
651 resultSetInvalidate (zh);
652 zebra_register_close (zh->service, zh->reg);
658 zh->reg_name = xstrdup (new_reg);
662 if (zh->service->path_root)
664 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
665 strlen(zh->reg_name) + 3);
666 strcpy (zh->path_reg, zh->service->path_root);
669 strcat (zh->path_reg, "/");
670 strcat (zh->path_reg, zh->reg_name);
676 zebra_lock_destroy (zh->lock_normal);
680 zebra_lock_destroy (zh->lock_shadow);
686 const char *lock_area = res_get (zh->res, "lockDir");
688 if (!lock_area && zh->path_reg)
689 res_set (zh->res, "lockDir", zh->path_reg);
690 sprintf (fname, "norm.%s.LCK", zh->reg_name);
692 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
694 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
696 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
698 if (!zh->lock_normal || !zh->lock_shadow)
702 zebra_lock_destroy(zh->lock_normal);
707 zebra_lock_destroy(zh->lock_shadow);
716 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
717 zebra_set_approx_limit(zh, approx);
721 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
722 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
724 yaz_log(YLOG_LOG, "static rank unset");
728 void map_basenames_func (void *vp, const char *name, const char *value)
730 struct map_baseinfo *p = (struct map_baseinfo *) vp;
732 char fromdb[128], todb[8][128];
739 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
740 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
741 todb[5], todb[6], todb[7]);
745 for (i = 0; i<p->num_bases; i++)
746 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
749 for (i = 0; i < no; i++)
751 if (p->new_num_bases == p->new_num_max)
753 p->new_basenames[(p->new_num_bases)++] =
754 nmem_strdup (p->mem, todb[i]);
760 int zebra_select_default_database(ZebraHandle zh)
764 /* no database has been selected - so we select based on
765 resource setting (including group)
767 const char *group = res_get(zh->session_res, "group");
768 const char *v = res_get_prefix(zh->session_res,
769 "database", group, "Default");
770 return zebra_select_database(zh, v);
775 void map_basenames (ZebraHandle zh, ODR stream,
776 int *num_bases, char ***basenames)
778 struct map_baseinfo info;
779 struct map_baseinfo *p = &info;
782 yaz_log(log_level, "map_basenames ");
787 info.num_bases = *num_bases;
788 info.basenames = *basenames;
789 info.new_num_max = 128;
790 info.new_num_bases = 0;
791 info.new_basenames = (char **)
792 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
793 info.mem = stream->mem;
795 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
797 for (i = 0; i<p->num_bases; i++)
798 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
800 p->new_basenames[(p->new_num_bases)++] =
801 nmem_strdup (p->mem, p->basenames[i]);
803 *num_bases = info.new_num_bases;
804 *basenames = info.new_basenames;
805 for (i = 0; i<*num_bases; i++)
806 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
809 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
812 yaz_log(log_level, "zebra_select_database %s",basename);
814 return zebra_select_databases (zh, 1, &basename);
817 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
818 const char **basenames)
827 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
828 num_bases,basenames[0]);
833 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
837 /* Check if the user has access to all databases (Seb) */
838 /* You could argue that this should happen later, after we have
839 * determined that the database(s) exist. */
840 if (zh->dbaccesslist) {
841 for (i = 0; i < num_bases; i++) {
842 const char *db = basenames[i];
844 for (p = zh->dbaccesslist; p && *p; p = pp) {
846 if ((pp = strchr(p, '+'))) {
852 if (len == strlen(db) && !strncmp(db, p, len))
856 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
862 for (i = 0; i < zh->num_basenames; i++)
863 xfree(zh->basenames[i]);
864 xfree(zh->basenames);
866 zh->num_basenames = num_bases;
867 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
868 for (i = 0; i < zh->num_basenames; i++)
869 zh->basenames[i] = xstrdup (basenames[i]);
871 cp = strrchr(basenames[0], '/');
874 len = cp - basenames[0];
875 new_reg = xmalloc(len + 1);
876 memcpy (new_reg, basenames[0], len);
880 new_reg = xstrdup ("");
881 for (i = 1; i<num_bases; i++)
885 cp1 = strrchr (basenames[i], '/');
890 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
893 if (len != cp1 - basenames[i] ||
894 memcmp (basenames[i], new_reg, len))
896 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
904 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
909 zebra_select_register (zh, new_reg);
913 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
916 if (!zh->lock_normal || !zh->lock_shadow)
918 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
924 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
926 if (approx_limit == 0)
927 approx_limit = DEFAULT_APPROX_LIMIT;
928 zh->approx_limit = approx_limit;
932 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
933 const char *setname, zint *hits)
941 yaz_log(log_level, "zebra_search_rpn");
942 zebra_clearError(zh);
946 if (zebra_begin_read(zh) == ZEBRA_FAIL)
949 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
950 zh->num_basenames, zh->basenames, setname);
956 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
958 Z_RecordComposition *comp,
959 oid_value input_format, int num_recs,
960 ZebraRetrievalRecord *recs)
962 ZebraMetaRecord *poset;
964 ZEBRA_RES ret = ZEBRA_OK;
972 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
974 zebra_clearError(zh);
977 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
982 if (zebra_begin_read (zh) == ZEBRA_FAIL)
985 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
986 for (i = 0; i<num_recs; i++)
987 pos_array[i] = recs[i].position;
988 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
991 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
992 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
998 for (i = 0; i<num_recs; i++)
1002 recs[i].errCode = 0;
1003 recs[i].format = VAL_SUTRS;
1004 recs[i].len = strlen(poset[i].term);
1005 recs[i].buf = poset[i].term;
1006 recs[i].base = poset[i].db;
1008 else if (poset[i].sysno)
1012 zebra_snippets *hit_snippet = zebra_snippets_create();
1014 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1018 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1020 stream, input_format, comp,
1021 &recs[i].format, &buf, &len,
1022 &recs[i].base, &recs[i].errString);
1027 recs[i].buf = (char*) odr_malloc(stream, len);
1028 memcpy(recs[i].buf, buf, len);
1032 recs[i].score = poset[i].score;
1033 recs[i].sysno = poset[i].sysno;
1034 zebra_snippets_destroy(hit_snippet);
1038 /* only need to set it once */
1039 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1041 zebra_setError_zint(zh,
1042 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1047 recs[i].buf = 0; /* no record and no error issued */
1049 recs[i].errCode = 0;
1050 recs[i].format = VAL_NONE;
1054 zebra_meta_records_destroy(zh, poset, num_recs);
1056 zebra_end_read (zh);
1061 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1063 int *num_entries, ZebraScanEntry **entries,
1065 const char *setname)
1067 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1068 Z_AttributesPlusTerm *zapt;
1072 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1075 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1078 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1079 position, num_entries, entries, is_partial,
1081 yaz_pqf_destroy (pqf_parser);
1085 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1086 oid_value attributeset,
1088 int *num_entries, ZebraScanEntry **entries,
1090 const char *setname)
1093 RSET limit_rset = 0;
1098 assert(num_entries);
1101 yaz_log(log_level, "zebra_scan");
1102 zebra_clearError(zh);
1103 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1111 limit_rset = resultSetRef(zh, setname);
1115 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1117 zebra_end_read (zh);
1121 res = rpn_scan (zh, stream, zapt, attributeset,
1122 zh->num_basenames, zh->basenames, position,
1123 num_entries, entries, is_partial, limit_rset, 0);
1124 zebra_end_read (zh);
1128 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1129 int num_input_setnames, const char **input_setnames,
1130 const char *output_setname,
1131 Z_SortKeySpecList *sort_sequence,
1137 assert(num_input_setnames>0);
1138 assert(input_setnames);
1139 assert(sort_sequence);
1140 assert(sort_status);
1141 yaz_log(log_level, "zebra_sort");
1142 zebra_clearError(zh);
1143 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1145 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1146 output_setname, sort_sequence, sort_status);
1151 int zebra_deleteResultSet(ZebraHandle zh, int function,
1152 int num_setnames, char **setnames,
1158 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1159 zebra_clearError(zh);;
1160 if (zebra_begin_read(zh))
1161 return Z_DeleteStatus_systemProblemAtTarget;
1164 case Z_DeleteResultSetRequest_list:
1165 assert(num_setnames>0);
1167 resultSetDestroy (zh, num_setnames, setnames, statuses);
1169 case Z_DeleteResultSetRequest_all:
1170 resultSetDestroy (zh, -1, 0, statuses);
1173 zebra_end_read (zh);
1174 status = Z_DeleteStatus_success;
1175 for (i = 0; i<num_setnames; i++)
1176 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1177 status = statuses[i];
1181 int zebra_errCode (ZebraHandle zh)
1185 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1188 yaz_log(log_level, "zebra_errCode: o");
1192 const char *zebra_errString (ZebraHandle zh)
1196 e= diagbib1_str (zh->errCode);
1197 yaz_log(log_level, "zebra_errString: %s",e);
1201 char *zebra_errAdd (ZebraHandle zh)
1206 yaz_log(log_level, "zebra_errAdd: %s",a);
1210 void zebra_clearError(ZebraHandle zh)
1219 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1222 const char *astring;
1227 zebra_clearError(zh);
1230 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1231 p = res_get(zs->global_res, u);
1232 xfree(zh->user_perm);
1233 zh->user_perm = xstrdup(p ? p : "r");
1235 /* Determine database access list */
1236 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1238 zh->dbaccesslist = xstrdup(astring);
1240 zh->dbaccesslist = 0;
1242 /* users that don't require a password .. */
1243 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1246 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1251 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1252 const char *record_type)
1255 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1256 database, record_type);
1257 zebra_clearError(zh);
1258 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1260 return zebra_begin_trans(zh, 1);
1263 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1266 yaz_log(log_level, "zebra_admin_import_end");
1267 zebra_clearError(zh);
1268 return zebra_end_trans(zh);
1271 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1273 ZEBRA_RES res = ZEBRA_OK;
1277 yaz_log(log_level, "zebra_admin_import_segment");
1278 zebra_clearError(zh);
1279 for (i = 0; i<segment->num_segmentRecords; i++)
1281 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1283 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1285 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1286 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1288 Odr_oct *oct = fragment->u.notExternallyTagged;
1291 if (zebra_update_record(zh,
1292 0, /* record Type */
1296 (const char *) oct->buf, oct->len,
1305 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1306 const char *rec_buf,
1308 const char *recid_buf, size_t recid_len,
1310 /* 1 = insert. Fail it already exists */
1311 /* 2 = replace. Fail it does not exist */
1312 /* 3 = delete. Fail if does not exist */
1313 /* 4 = update. Insert/replace */
1320 assert(action>0 && action <=4);
1323 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1324 zebra_clearError(zh);
1326 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1328 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1329 "no record ID or empty record ID");
1333 memcpy (recid_z, recid_buf, recid_len);
1334 recid_z[recid_len] = 0;
1336 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1339 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1342 if (action == 1) /* fail if insert */
1344 zebra_end_trans(zh);
1345 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1346 "Cannot insert record: already exist");
1350 memcpy (&sysno, rinfo+1, sizeof(sysno));
1354 if (action == 2 || action == 3) /* fail if delete or update */
1356 zebra_end_trans(zh);
1357 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1358 "Cannot delete/update record: does not exist");
1361 action = 1; /* make it an insert (if it's an update).. */
1363 res = buffer_extract_record (zh, rec_buf, rec_len,
1364 action == 3 ? 1 : 0 /* delete flag */,
1370 0, /* force update */
1371 1 /* allow update */
1373 if (res == ZEBRA_FAIL)
1375 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1376 "Unable to parse record");
1380 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1382 else if (action == 3)
1384 dict_delete (zh->reg->matchDict, recid_z);
1386 zebra_end_trans(zh);
1390 int delete_w_handle(const char *info, void *handle)
1392 ZebraHandle zh = (ZebraHandle) handle;
1396 if (*info == sizeof(pos))
1398 memcpy (&pos, info+1, sizeof(pos));
1399 isamb_unlink(zh->reg->isamb, pos);
1404 static int delete_SU_handle(void *handle, int ord)
1406 ZebraHandle zh = (ZebraHandle) handle;
1410 ord_len = key_SU_encode (ord, ord_buf);
1411 ord_buf[ord_len] = '\0';
1413 assert (zh->reg->isamb);
1414 dict_delete_subtree(zh->reg->dict, ord_buf,
1415 zh, delete_w_handle);
1419 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1421 ZEBRA_RES ret = ZEBRA_OK;
1423 yaz_log(log_level, "zebra_drop_database %s", db);
1424 zebra_clearError(zh);
1426 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1428 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1432 zebraExplain_curDatabase (zh->reg->zei, db);
1434 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1435 zebraExplain_removeDatabase(zh->reg->zei, zh);
1439 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1442 zebra_end_trans (zh);
1446 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1449 yaz_log(log_level, "zebra_create_database %s", db);
1451 zebra_clearError(zh);
1453 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1455 if (zebra_begin_trans (zh, 1))
1458 /* announce database */
1459 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1460 /* explainDatabase */))
1462 zebra_end_trans (zh);
1463 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1466 return zebra_end_trans (zh);
1469 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1470 const char *input_str, int input_len,
1471 char *output_str, int output_len)
1477 yaz_log(log_level, "zebra_string_norm ");
1478 zebra_clearError(zh);
1479 if (!zh->reg->zebra_maps)
1481 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1482 input_str, input_len);
1485 if (wrbuf_len(wrbuf) >= output_len)
1487 if (wrbuf_len(wrbuf))
1488 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1489 output_str[wrbuf_len(wrbuf)] = '\0';
1490 return wrbuf_len(wrbuf);
1493 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1495 char state_fname[256];
1500 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1501 zebra_clearError(zh);
1503 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1504 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1505 f = fopen (fname, "w");
1507 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1508 fprintf (f, "%c %d %ld\n", val, seqno, p);
1513 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1515 char state_fname[256];
1520 yaz_log(log_level, "zebra_get_state ");
1521 zebra_clearError(zh);
1522 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1523 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1524 f = fopen (fname, "r");
1530 fscanf (f, "%c %d", val, seqno);
1536 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1538 return zebra_begin_trans(zh, 0);
1541 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1543 return zebra_end_trans(zh);
1546 static void read_res_for_transaction(ZebraHandle zh)
1548 const char *group = res_get(zh->res, "group");
1550 /* FIXME - do we still use groups ?? */
1552 zh->m_group = group;
1553 v = res_get_prefix(zh->res, "followLinks", group, "1");
1554 zh->m_follow_links = atoi(v);
1556 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1557 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1559 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1560 zh->m_store_keys = atoi(v);
1562 v = res_get_prefix(zh->res, "storeData", group, "1");
1563 zh->m_store_data = atoi(v);
1565 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1566 zh->m_explain_database = atoi(v);
1568 v = res_get_prefix(zh->res, "openRW", group, "1");
1569 zh->m_flag_rw = atoi(v);
1571 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1572 zh->m_file_verbose_limit = atoi(v);
1575 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1578 zebra_select_default_database(zh);
1581 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1582 "zebra_begin_trans: no database selected");
1586 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1590 if (rw && !strchr(zh->user_perm, 'w'))
1594 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1606 const char *rval = 0;
1611 read_res_for_transaction(zh);
1614 if (zh->trans_no != 1)
1616 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1617 "zebra_begin_trans: no write trans within read");
1622 resultSetInvalidate (zh);
1623 zebra_register_close (zh->service, zh->reg);
1625 zh->trans_w_no = zh->trans_no;
1627 zebra_clearError(zh);
1629 zh->records_inserted = 0;
1630 zh->records_updated = 0;
1631 zh->records_deleted = 0;
1632 zh->records_processed = 0;
1634 #if HAVE_SYS_TIMES_H
1638 if (zh->shadow_enable)
1639 rval = res_get (zh->res, "shadow");
1641 for (pass = 0; pass < 2; pass++)
1645 zebra_lock_r (zh->lock_normal);
1646 zebra_lock_w (zh->lock_shadow);
1650 zebra_lock_w (zh->lock_normal);
1651 zebra_lock_w (zh->lock_shadow);
1654 zebra_get_state (zh, &val, &seqno);
1657 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1658 zebra_unlock (zh->lock_shadow);
1659 zebra_unlock (zh->lock_normal);
1663 else if (val == 'd')
1667 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1669 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1670 bf_commitClean (bfs, rval);
1675 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1682 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1686 zebra_set_state (zh, 'd', seqno);
1688 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1689 1, rval ? 1 : 0, zh->res,
1692 zh->reg->seqno = seqno;
1695 zebra_set_state (zh, 'o', seqno);
1697 zebra_unlock (zh->lock_shadow);
1698 zebra_unlock (zh->lock_normal);
1703 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1704 "zebra_begin_trans: cannot open register");
1705 yaz_log(YLOG_FATAL, "%s", zh->errString);
1717 if (zh->trans_no != 1)
1719 zebra_flush_reg (zh);
1722 zebra_clearError(zh);
1723 #if HAVE_SYS_TIMES_H
1729 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1732 if (!zh->lock_normal || !zh->lock_shadow)
1735 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1738 zebra_get_state (zh, &val, &seqno);
1744 else if (seqno != zh->reg->seqno)
1746 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1747 seqno, zh->reg->seqno);
1750 else if (zh->reg->last_val != val)
1752 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1753 val, zh->reg->last_val);
1760 zebra_lock_r (zh->lock_shadow);
1762 zebra_lock_r (zh->lock_normal);
1766 resultSetInvalidate (zh);
1767 zebra_register_close (zh->service, zh->reg);
1769 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1770 0, val == 'c' ? 1 : 0,
1771 zh->res, zh->path_reg);
1774 zebra_unlock (zh->lock_normal);
1775 zebra_unlock (zh->lock_shadow);
1777 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1780 zh->reg->last_val = val;
1781 zh->reg->seqno = seqno;
1783 read_res_for_transaction(zh);
1787 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1789 ZebraTransactionStatus dummy;
1791 yaz_log(log_level, "zebra_end_trans");
1792 return zebra_end_transaction(zh, &dummy);
1795 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1803 yaz_log(log_level, "zebra_end_transaction");
1805 status->processed = 0;
1806 status->inserted = 0;
1807 status->updated = 0;
1808 status->deleted = 0;
1812 if (!zh->res || !zh->reg)
1814 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1815 "zebra_end_trans: no open transaction");
1818 if (zh->trans_no != zh->trans_w_no)
1821 if (zh->trans_no != 0)
1824 /* release read lock */
1826 zebra_unlock (zh->lock_normal);
1827 zebra_unlock (zh->lock_shadow);
1830 { /* release write lock */
1834 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1835 rval = res_get (zh->res, "shadow");
1837 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1839 zebra_flush_reg (zh);
1841 resultSetInvalidate (zh);
1843 zebra_register_close (zh->service, zh->reg);
1846 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1847 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1848 zh->records_processed, zh->records_inserted,
1849 zh->records_updated, zh->records_deleted);
1851 status->processed = (int) zh->records_processed;
1852 status->inserted = (int) zh->records_inserted;
1853 status->updated = (int) zh->records_updated;
1854 status->deleted = (int) zh->records_deleted;
1856 zebra_get_state (zh, &val, &seqno);
1859 BFiles bfs = bfs_create (rval, zh->path_reg);
1860 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1861 bf_commitClean (bfs, rval);
1866 zebra_set_state (zh, 'o', seqno);
1868 zebra_unlock (zh->lock_shadow);
1869 zebra_unlock (zh->lock_normal);
1872 #if HAVE_SYS_TIMES_H
1874 yaz_log (log_level, "user/system: %ld/%ld",
1875 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1876 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1878 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1879 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1884 int zebra_repository_update (ZebraHandle zh, const char *path)
1888 zebra_clearError(zh);
1889 yaz_log (log_level, "updating %s", path);
1890 repositoryUpdate (zh, path);
1894 int zebra_repository_delete (ZebraHandle zh, const char *path)
1898 zebra_clearError(zh);
1899 yaz_log (log_level, "deleting %s", path);
1900 repositoryDelete (zh, path);
1904 int zebra_repository_show (ZebraHandle zh, const char *path)
1908 yaz_log(log_level, "zebra_repository_show");
1909 zebra_clearError(zh);
1910 repositoryShow (zh, path);
1914 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1921 zebra_clearError(zh);
1923 zebra_select_default_database(zh);
1926 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1929 rval = res_get (zh->res, "shadow");
1932 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1936 zebra_lock_w (zh->lock_normal);
1937 zebra_lock_r (zh->lock_shadow);
1939 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1941 zebra_get_state (zh, &val, &seqno);
1944 bf_cache (bfs, rval);
1945 if (bf_commitExists (bfs))
1948 zebra_set_state (zh, 'd', seqno);
1951 zebra_set_state (zh, 'c', seqno);
1953 yaz_log (YLOG_DEBUG, "commit start");
1954 bf_commitExec (bfs);
1959 yaz_log (YLOG_DEBUG, "commit clean");
1960 bf_commitClean (bfs, rval);
1962 zebra_set_state (zh, 'o', seqno);
1966 yaz_log (log_level, "nothing to commit");
1970 zebra_unlock (zh->lock_shadow);
1971 zebra_unlock (zh->lock_normal);
1975 ZEBRA_RES zebra_clean(ZebraHandle zh)
1978 yaz_log(log_level, "zebra_clean");
1979 return zebra_commit_ex(zh, 1);
1982 ZEBRA_RES zebra_commit(ZebraHandle zh)
1985 yaz_log(log_level, "zebra_commit");
1986 return zebra_commit_ex(zh, 0);
1989 ZEBRA_RES zebra_init(ZebraHandle zh)
1994 yaz_log(log_level, "zebra_init");
1995 zebra_clearError(zh);
1997 zebra_select_default_database(zh);
2000 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2001 "cannot select default database");
2004 rval = res_get (zh->res, "shadow");
2006 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2009 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2013 bf_cache (bfs, rval);
2017 zebra_set_state (zh, 'o', 0);
2021 ZEBRA_RES zebra_compact(ZebraHandle zh)
2025 yaz_log(log_level, "zebra_compact");
2026 zebra_clearError(zh);
2029 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2032 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2038 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2041 yaz_log(log_level, "zebra_result");
2042 *code = zh->errCode;
2043 *addinfo = zh->errString;
2046 void zebra_shadow_enable(ZebraHandle zh, int value)
2049 yaz_log(log_level, "zebra_shadow_enable");
2050 zebra_clearError(zh);
2051 zh->shadow_enable = value;
2054 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2058 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2059 zebra_clearError(zh);
2061 if (zh->iconv_to_utf8 != 0)
2062 yaz_iconv_close(zh->iconv_to_utf8);
2063 if (zh->iconv_from_utf8 != 0)
2064 yaz_iconv_close(zh->iconv_from_utf8);
2067 yaz_iconv_open ("UTF-8", encoding);
2068 if (zh->iconv_to_utf8 == 0)
2069 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2070 zh->iconv_from_utf8 =
2071 yaz_iconv_open (encoding, "UTF-8");
2072 if (zh->iconv_to_utf8 == 0)
2073 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2078 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2081 yaz_log(log_level, "zebra_record_encoding");
2082 zebra_clearError(zh);
2083 xfree(zh->record_encoding);
2084 zh->record_encoding = 0;
2086 zh->record_encoding = xstrdup (encoding);
2090 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2095 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2096 zebra_clearError(zh);
2097 res_set(zh->res, name, value);
2100 const char *zebra_get_resource(ZebraHandle zh,
2101 const char *name, const char *defaultvalue)
2106 v = res_get_def (zh->res, name, (char *)defaultvalue);
2107 zebra_clearError(zh);
2108 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2112 /* moved from zebra_api_ext.c by pop */
2113 /* FIXME: Should this really be public??? -Heikki */
2115 int zebra_trans_no (ZebraHandle zh)
2118 yaz_log(log_level, "zebra_trans_no");
2119 return zh->trans_no;
2122 int zebra_get_shadow_enable (ZebraHandle zh)
2125 yaz_log(log_level, "zebra_get_shadow_enable");
2126 return zh->shadow_enable;
2129 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2132 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2133 zh->shadow_enable = value;
2136 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2137 so that it's identicical to the original api_records_retrieve */
2138 void api_records_retrieve (ZebraHandle zh, ODR stream,
2139 const char *setname, Z_RecordComposition *comp,
2140 oid_value input_format, int num_recs,
2141 ZebraRetrievalRecord *recs)
2143 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2147 /* ---------------------------------------------------------------------------
2148 Record insert(=update), delete
2150 If sysno is provided, then it's used to identify the record.
2151 If not, and match_criteria is provided, then sysno is guessed
2152 If not, and a record is provided, then sysno is got from there
2153 NOTE: Now returns 0 at success and updates sysno, which is an int*
2157 int zebra_add_record(ZebraHandle zh,
2158 const char *buf, int buf_size)
2161 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2164 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2165 const char *recordType,
2166 SYSNO *sysno, const char *match,
2168 const char *buf, int buf_size, int force_update)
2174 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2177 buf_size = strlen(buf);
2179 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2181 res = buffer_extract_record (zh, buf, buf_size,
2182 0, /* delete_flag */
2188 0); /* allow_update */
2189 zebra_end_trans(zh);
2193 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2194 const char *recordType,
2195 SYSNO* sysno, const char *match,
2197 const char *buf, int buf_size,
2205 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2207 if (buf_size < 1) buf_size = strlen(buf);
2209 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2211 res = buffer_extract_record (zh, buf, buf_size,
2212 0, /* delete_flag */
2218 1); /* allow_update */
2219 zebra_end_trans(zh);
2223 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2224 const char *recordType,
2225 SYSNO *sysno, const char *match,
2227 const char *buf, int buf_size,
2234 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2236 if (buf_size < 1) buf_size = strlen(buf);
2238 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2240 res = buffer_extract_record (zh, buf, buf_size,
2241 1, /* delete_flag */
2247 1); /* allow_update */
2248 zebra_end_trans(zh);
2252 /* ---------------------------------------------------------------------------
2256 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2257 const char *setname, zint *hits)
2260 ZEBRA_RES res = ZEBRA_OK;
2262 ODR odr = odr_createmem(ODR_ENCODE);
2267 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2269 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2273 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2274 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2278 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2282 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2290 /* ---------------------------------------------------------------------------
2291 Sort - a simplified interface, with optional read locks.
2293 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2294 const char *sort_spec,
2295 const char *output_setname,
2296 const char **input_setnames)
2298 int num_input_setnames = 0;
2299 int sort_status = 0;
2300 Z_SortKeySpecList *sort_sequence;
2304 assert(output_setname);
2305 assert(input_setnames);
2306 sort_sequence = yaz_sort_spec (stream, sort_spec);
2307 yaz_log(log_level, "sort (FIXME) ");
2310 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2311 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2315 /* we can do this, since the perl typemap code for char** will
2316 put a NULL at the end of list */
2317 while (input_setnames[num_input_setnames]) num_input_setnames++;
2319 if (zebra_begin_read (zh))
2322 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2323 output_setname, sort_sequence, &sort_status);
2329 /* ---------------------------------------------------------------------------
2330 Get BFS for Zebra system (to make alternative storage methods)
2332 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2335 return zh->reg->bfs;
2340 /* ---------------------------------------------------------------------------
2341 Set limit for search/scan
2343 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2346 zebra_limit_destroy(zh->m_limit);
2347 zh->m_limit = zebra_limit_create(complement_flag, ids);
2352 Set Error code + addinfo
2354 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2357 nmem_reset(zh->nmem_error);
2358 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2361 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2364 sprintf(vstr, ZINT_FORMAT, i);
2367 nmem_reset(zh->nmem_error);
2368 zh->errString = nmem_strdup(zh->nmem_error, vstr);