1 /* $Id: zebraapi.c,v 1.205 2006-03-25 15:33:29 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 void zebra_chdir (ZebraService zs)
60 yaz_log(log_level, "zebra_chdir");
61 dir = res_get (zs->global_res, "chdir");
64 yaz_log (YLOG_DEBUG, "chdir %s", dir);
72 static void zebra_flush_reg (ZebraHandle zh)
75 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;
191 ZEBRA_RES ret = res_read_file(res, configName);
198 zh = xmalloc(sizeof(*zh));
199 zh->global_res = res;
204 zebra_mutex_cond_init (&zh->session_lock);
205 passwd_plain = res_get (zh->global_res, "passwd");
206 passwd_encrypt = res_get (zh->global_res, "passwd.c");
207 dbaccess = res_get (zh->global_res, "dbaccess");
209 if (!passwd_plain && !passwd_encrypt)
210 zh->passwd_db = NULL;
213 zh->passwd_db = passwd_db_open();
215 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
219 passwd_db_file_plain(zh->passwd_db, passwd_plain);
221 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
228 zh->dbaccess = res_open(NULL, NULL);
229 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
230 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
235 zh->path_root = res_get (zh->global_res, "root");
236 zh->nmem = nmem_create();
237 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
243 void zebra_filter_info(ZebraService zs, void *cd,
244 void (*cb)(void *cd, const char *name))
248 recTypeClass_info(zs->record_classes, cd, cb);
251 void zebra_pidfname(ZebraService zs, char *path)
254 zebra_lock_prefix (zs->global_res, path);
255 strcat(path, "zebrasrv.pid");
258 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
259 int compact_flag, Res res)
261 int page_size = 4096;
262 char resource_str[200];
263 sprintf (resource_str, "dict.%.100s.pagesize", name);
267 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
268 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
270 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
274 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
275 int rw, int useshadow, Res res,
276 const char *reg_path)
278 struct zebra_register *reg;
279 int record_compression = REC_COMPRESS_NONE;
280 const char *recordCompression = 0;
281 const char *profilePath;
286 reg = xmalloc(sizeof(*reg));
289 reg->name = xstrdup (name);
296 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
297 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
299 reg->dh = data1_createx (DATA1_FLAG_XML);
306 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
309 data1_destroy(reg->dh);
316 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
318 bfs_destroy(reg->bfs);
319 data1_destroy(reg->dh);
326 getcwd(cwd, sizeof(cwd)-1);
327 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
328 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
330 data1_set_tabpath (reg->dh, profilePath);
331 data1_set_tabroot (reg->dh, reg_path);
332 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
334 if ((reg->zebra_maps = zebra_maps_open (res, reg_path)) == 0)
336 recTypes_destroy(reg->recTypes);
337 bfs_destroy(reg->bfs);
338 data1_destroy(reg->dh);
344 reg->rank_classes = NULL;
348 reg->keys = zebra_rec_keys_open();
351 reg->sortKeys = zebra_rec_keys_open();
353 reg->sortKeys.buf = 0;
354 reg->sortKeys.buf_max = 0;
365 reg->key_file_no = 0;
368 zebraRankInstall (reg, rank_1_class);
369 zebraRankInstall (reg, rank_zv_class);
370 zebraRankInstall (reg, rank_static_class);
372 recordCompression = res_get_def (res, "recordCompression", "none");
373 if (!strcmp (recordCompression, "none"))
374 record_compression = REC_COMPRESS_NONE;
375 if (!strcmp (recordCompression, "bzip2"))
376 record_compression = REC_COMPRESS_BZIP2;
378 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
380 yaz_log (YLOG_WARN, "rec_open failed");
385 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
387 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
389 yaz_log (YLOG_WARN, "dict_open failed");
392 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
394 yaz_log (YLOG_WARN, "sortIdx_open failed");
397 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
399 struct ISAMS_M_s isams_m;
400 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
401 key_isams_m(res, &isams_m))))
403 yaz_log (YLOG_WARN, "isams_open failed");
407 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
409 struct ISAMC_M_s isamc_m;
410 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
411 rw, key_isamc_m(res, &isamc_m))))
413 yaz_log (YLOG_WARN, "isamc_open failed");
417 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
419 struct ISAMC_M_s isamc_m;
421 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
422 rw, key_isamc_m(res, &isamc_m), 0)))
424 yaz_log (YLOG_WARN, "isamb_open failed");
428 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
430 struct ISAMC_M_s isamc_m;
432 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
433 rw, key_isamc_m(res, &isamc_m), 1)))
435 yaz_log (YLOG_WARN, "isamb_open failed");
439 if (res_get_match (res, "isam", "null", 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), -1)))
446 yaz_log (YLOG_WARN, "isamb_open failed");
450 reg->zei = zebraExplain_open (reg->records, reg->dh,
455 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
460 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
464 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
467 yaz_log(log_level, "zebra_admin_shutdown");
469 zebra_mutex_cond_lock (&zh->service->session_lock);
470 zh->service->stop_flag = 1;
471 zebra_mutex_cond_unlock (&zh->service->session_lock);
475 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
479 yaz_log(log_level, "zebra_admin_start");
481 zebra_mutex_cond_lock (&zs->session_lock);
482 zebra_mutex_cond_unlock (&zs->session_lock);
486 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
490 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
495 zebraExplain_close (reg->zei);
496 dict_close (reg->dict);
498 dict_close (reg->matchDict);
499 sortIdx_close (reg->sortIdx);
501 isams_close (reg->isams);
503 isamc_close (reg->isamc);
505 isamb_close (reg->isamb);
506 rec_close (®->records);
509 recTypes_destroy (reg->recTypes);
510 zebra_maps_close (reg->zebra_maps);
511 zebraRankDestroy (reg);
512 bfs_destroy (reg->bfs);
513 data1_destroy (reg->dh);
515 zebra_rec_keys_close(reg->keys);
517 zebra_rec_keys_close(reg->sortKeys);
519 xfree(reg->sortKeys.buf);
527 ZEBRA_RES zebra_stop(ZebraService zs)
531 yaz_log (log_level, "zebra_stop");
535 zebra_close (zs->sessions);
538 zebra_mutex_cond_destroy (&zs->session_lock);
541 passwd_db_close (zs->passwd_db);
543 recTypeClass_destroy(zs->record_classes);
544 nmem_destroy(zs->nmem);
545 res_close (zs->global_res);
550 ZEBRA_RES zebra_close (ZebraHandle zh)
553 struct zebra_session **sp;
556 yaz_log(log_level, "zebra_close");
563 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
564 resultSetDestroy (zh, -1, 0, 0);
567 zebra_register_close (zh->service, zh->reg);
568 zebra_close_res (zh);
569 res_close(zh->session_res);
571 xfree(zh->record_encoding);
573 xfree(zh->dbaccesslist);
575 for (i = 0; i < zh->num_basenames; i++)
576 xfree(zh->basenames[i]);
577 xfree(zh->basenames);
579 if (zh->iconv_to_utf8 != 0)
580 yaz_iconv_close (zh->iconv_to_utf8);
581 if (zh->iconv_from_utf8 != 0)
582 yaz_iconv_close (zh->iconv_from_utf8);
584 zebra_mutex_cond_lock (&zs->session_lock);
585 zebra_lock_destroy (zh->lock_normal);
586 zebra_lock_destroy (zh->lock_shadow);
598 zebra_mutex_cond_unlock (&zs->session_lock);
600 xfree(zh->user_perm);
601 zh->service = 0; /* more likely to trigger an assert */
603 zebra_limit_destroy(zh->m_limit);
605 nmem_destroy(zh->nmem_error);
612 struct map_baseinfo {
618 char **new_basenames;
622 static void zebra_open_res(ZebraHandle zh)
630 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
631 zh->res = res_open(zh->session_res, 0);
632 res_read_file(zh->res, fname);
634 else if (*zh->reg_name == 0)
636 zh->res = res_open(zh->session_res, 0);
640 yaz_log (YLOG_WARN, "no register root specified");
641 zh->res = 0; /* no path for register - fail! */
645 static void zebra_close_res (ZebraHandle zh)
653 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
657 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
661 assert (zh->reg == 0);
662 assert (*zh->reg_name == 0);
668 resultSetInvalidate (zh);
669 zebra_register_close (zh->service, zh->reg);
675 zh->reg_name = xstrdup (new_reg);
679 if (zh->service->path_root)
681 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
682 strlen(zh->reg_name) + 3);
683 strcpy (zh->path_reg, zh->service->path_root);
686 strcat (zh->path_reg, "/");
687 strcat (zh->path_reg, zh->reg_name);
693 zebra_lock_destroy (zh->lock_normal);
697 zebra_lock_destroy (zh->lock_shadow);
703 const char *lock_area = res_get (zh->res, "lockDir");
705 if (!lock_area && zh->path_reg)
706 res_set (zh->res, "lockDir", zh->path_reg);
707 sprintf (fname, "norm.%s.LCK", zh->reg_name);
709 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
711 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
713 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
715 if (!zh->lock_normal || !zh->lock_shadow)
719 zebra_lock_destroy(zh->lock_normal);
724 zebra_lock_destroy(zh->lock_shadow);
733 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
734 zebra_set_approx_limit(zh, approx);
738 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
739 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
741 yaz_log(YLOG_LOG, "static rank unset");
745 void map_basenames_func (void *vp, const char *name, const char *value)
747 struct map_baseinfo *p = (struct map_baseinfo *) vp;
749 char fromdb[128], todb[8][128];
756 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
757 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
758 todb[5], todb[6], todb[7]);
762 for (i = 0; i<p->num_bases; i++)
763 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
766 for (i = 0; i < no; i++)
768 if (p->new_num_bases == p->new_num_max)
770 p->new_basenames[(p->new_num_bases)++] =
771 nmem_strdup (p->mem, todb[i]);
777 int zebra_select_default_database(ZebraHandle zh)
781 /* no database has been selected - so we select based on
782 resource setting (including group)
784 const char *group = res_get(zh->session_res, "group");
785 const char *v = res_get_prefix(zh->session_res,
786 "database", group, "Default");
787 return zebra_select_database(zh, v);
792 void map_basenames (ZebraHandle zh, ODR stream,
793 int *num_bases, char ***basenames)
795 struct map_baseinfo info;
796 struct map_baseinfo *p = &info;
799 yaz_log(log_level, "map_basenames ");
804 info.num_bases = *num_bases;
805 info.basenames = *basenames;
806 info.new_num_max = 128;
807 info.new_num_bases = 0;
808 info.new_basenames = (char **)
809 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
810 info.mem = stream->mem;
812 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
814 for (i = 0; i<p->num_bases; i++)
815 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
817 p->new_basenames[(p->new_num_bases)++] =
818 nmem_strdup (p->mem, p->basenames[i]);
820 *num_bases = info.new_num_bases;
821 *basenames = info.new_basenames;
822 for (i = 0; i<*num_bases; i++)
823 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
826 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
829 yaz_log(log_level, "zebra_select_database %s",basename);
831 return zebra_select_databases (zh, 1, &basename);
834 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
835 const char **basenames)
844 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
845 num_bases,basenames[0]);
850 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
854 /* Check if the user has access to all databases (Seb) */
855 /* You could argue that this should happen later, after we have
856 * determined that the database(s) exist. */
857 if (zh->dbaccesslist) {
858 for (i = 0; i < num_bases; i++) {
859 const char *db = basenames[i];
861 for (p = zh->dbaccesslist; p && *p; p = pp) {
863 if ((pp = strchr(p, '+'))) {
869 if (len == strlen(db) && !strncmp(db, p, len))
873 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
879 for (i = 0; i < zh->num_basenames; i++)
880 xfree(zh->basenames[i]);
881 xfree(zh->basenames);
883 zh->num_basenames = num_bases;
884 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
885 for (i = 0; i < zh->num_basenames; i++)
886 zh->basenames[i] = xstrdup (basenames[i]);
888 cp = strrchr(basenames[0], '/');
891 len = cp - basenames[0];
892 new_reg = xmalloc(len + 1);
893 memcpy (new_reg, basenames[0], len);
897 new_reg = xstrdup ("");
898 for (i = 1; i<num_bases; i++)
902 cp1 = strrchr (basenames[i], '/');
907 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
910 if (len != cp1 - basenames[i] ||
911 memcmp (basenames[i], new_reg, len))
913 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
921 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
926 zebra_select_register (zh, new_reg);
930 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
933 if (!zh->lock_normal || !zh->lock_shadow)
935 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
941 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
943 if (approx_limit == 0)
944 approx_limit = DEFAULT_APPROX_LIMIT;
945 zh->approx_limit = approx_limit;
949 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
950 const char *setname, zint *hits)
958 yaz_log(log_level, "zebra_search_rpn");
962 if (zebra_begin_read(zh) == ZEBRA_FAIL)
965 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
966 zh->num_basenames, zh->basenames, setname);
972 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
974 Z_RecordComposition *comp,
975 oid_value input_format, int num_recs,
976 ZebraRetrievalRecord *recs)
978 ZebraMetaRecord *poset;
980 ZEBRA_RES ret = ZEBRA_OK;
988 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
992 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
997 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1000 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1001 for (i = 0; i<num_recs; i++)
1002 pos_array[i] = recs[i].position;
1003 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1006 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1007 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1013 for (i = 0; i<num_recs; i++)
1017 recs[i].errCode = 0;
1018 recs[i].format = VAL_SUTRS;
1019 recs[i].len = strlen(poset[i].term);
1020 recs[i].buf = poset[i].term;
1021 recs[i].base = poset[i].db;
1023 else if (poset[i].sysno)
1027 zebra_snippets *hit_snippet = zebra_snippets_create();
1029 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1033 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1035 stream, input_format, comp,
1036 &recs[i].format, &buf, &len,
1037 &recs[i].base, &recs[i].errString);
1042 recs[i].buf = (char*) odr_malloc(stream, len);
1043 memcpy(recs[i].buf, buf, len);
1047 recs[i].score = poset[i].score;
1048 recs[i].sysno = poset[i].sysno;
1049 zebra_snippets_destroy(hit_snippet);
1053 /* only need to set it once */
1054 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1056 zebra_setError_zint(zh,
1057 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1062 recs[i].buf = 0; /* no record and no error issued */
1064 recs[i].errCode = 0;
1065 recs[i].format = VAL_NONE;
1069 zebra_meta_records_destroy(zh, poset, num_recs);
1071 zebra_end_read (zh);
1076 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1078 int *num_entries, ZebraScanEntry **entries,
1080 const char *setname)
1082 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1083 Z_AttributesPlusTerm *zapt;
1087 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1090 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1093 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1094 position, num_entries, entries, is_partial,
1096 yaz_pqf_destroy (pqf_parser);
1100 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1101 oid_value attributeset,
1103 int *num_entries, ZebraScanEntry **entries,
1105 const char *setname)
1108 RSET limit_rset = 0;
1113 assert(num_entries);
1116 yaz_log(log_level, "zebra_scan");
1118 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1126 limit_rset = resultSetRef(zh, setname);
1130 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1132 zebra_end_read (zh);
1136 res = rpn_scan (zh, stream, zapt, attributeset,
1137 zh->num_basenames, zh->basenames, position,
1138 num_entries, entries, is_partial, limit_rset, 0);
1139 zebra_end_read (zh);
1143 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1144 int num_input_setnames, const char **input_setnames,
1145 const char *output_setname,
1146 Z_SortKeySpecList *sort_sequence,
1152 assert(num_input_setnames>0);
1153 assert(input_setnames);
1154 assert(sort_sequence);
1155 assert(sort_status);
1156 yaz_log(log_level, "zebra_sort");
1158 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1160 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1161 output_setname, sort_sequence, sort_status);
1166 int zebra_deleteResultSet(ZebraHandle zh, int function,
1167 int num_setnames, char **setnames,
1173 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1175 if (zebra_begin_read(zh))
1176 return Z_DeleteStatus_systemProblemAtTarget;
1179 case Z_DeleteResultSetRequest_list:
1180 assert(num_setnames>0);
1182 resultSetDestroy (zh, num_setnames, setnames, statuses);
1184 case Z_DeleteResultSetRequest_all:
1185 resultSetDestroy (zh, -1, 0, statuses);
1188 zebra_end_read (zh);
1189 status = Z_DeleteStatus_success;
1190 for (i = 0; i<num_setnames; i++)
1191 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1192 status = statuses[i];
1196 int zebra_errCode (ZebraHandle zh)
1200 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1203 yaz_log(log_level, "zebra_errCode: o");
1207 const char *zebra_errString (ZebraHandle zh)
1211 e= diagbib1_str (zh->errCode);
1212 yaz_log(log_level, "zebra_errString: %s",e);
1216 char *zebra_errAdd (ZebraHandle zh)
1221 yaz_log(log_level, "zebra_errAdd: %s",a);
1225 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1228 const char *astring;
1236 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1237 p = res_get(zs->global_res, u);
1238 xfree(zh->user_perm);
1239 zh->user_perm = xstrdup(p ? p : "r");
1241 /* Determine database access list */
1242 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1244 zh->dbaccesslist = xstrdup(astring);
1246 zh->dbaccesslist = 0;
1248 /* users that don't require a password .. */
1249 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1252 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1257 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1258 const char *record_type)
1261 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1262 database, record_type);
1263 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1265 return zebra_begin_trans(zh, 1);
1268 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1271 yaz_log(log_level, "zebra_admin_import_end");
1272 return zebra_end_trans(zh);
1275 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1277 ZEBRA_RES res = ZEBRA_OK;
1281 yaz_log(log_level, "zebra_admin_import_segment");
1283 for (i = 0; i<segment->num_segmentRecords; i++)
1285 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1287 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1289 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1290 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1292 Odr_oct *oct = fragment->u.notExternallyTagged;
1295 if (zebra_update_record(zh,
1296 0, /* record Type */
1300 (const char *) oct->buf, oct->len,
1309 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1310 const char *rec_buf,
1312 const char *recid_buf, size_t recid_len,
1314 /* 1 = insert. Fail it already exists */
1315 /* 2 = replace. Fail it does not exist */
1316 /* 3 = delete. Fail if does not exist */
1317 /* 4 = update. Insert/replace */
1325 assert(action>0 && action <=4);
1328 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1330 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1332 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1333 "no record ID or empty record ID");
1337 memcpy (recid_z, recid_buf, recid_len);
1338 recid_z[recid_len] = 0;
1340 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1343 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1344 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1347 if (action == 1) /* fail if insert */
1349 zebra_end_trans(zh);
1350 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1351 "Cannot insert record: already exist");
1355 memcpy (&sysno, rinfo+1, sizeof(sysno));
1359 if (action == 2 || action == 3) /* fail if delete or update */
1361 zebra_end_trans(zh);
1362 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1363 "Cannot delete/update record: does not exist");
1366 action = 1; /* make it an insert (if it's an update).. */
1368 res = buffer_extract_record (zh, rec_buf, rec_len,
1369 action == 3 ? 1 : 0 /* delete flag */,
1375 0, /* force update */
1376 1 /* allow update */
1378 if (res == ZEBRA_FAIL)
1380 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1381 "Unable to parse record");
1385 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1386 sizeof(sysno), &sysno);
1388 else if (action == 3)
1390 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1392 zebra_end_trans(zh);
1396 int delete_w_handle(const char *info, void *handle)
1398 ZebraHandle zh = (ZebraHandle) handle;
1402 if (*info == sizeof(pos))
1404 memcpy (&pos, info+1, sizeof(pos));
1405 isamb_unlink(zh->reg->isamb, pos);
1410 static int delete_SU_handle(void *handle, int ord)
1412 ZebraHandle zh = (ZebraHandle) handle;
1416 ord_len = key_SU_encode (ord, ord_buf);
1417 ord_buf[ord_len] = '\0';
1419 assert (zh->reg->isamb);
1420 dict_delete_subtree(zh->reg->dict, ord_buf,
1421 zh, delete_w_handle);
1425 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1427 ZEBRA_RES ret = ZEBRA_OK;
1429 yaz_log(log_level, "zebra_drop_database %s", db);
1431 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1433 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1438 zebraExplain_curDatabase (zh->reg->zei, db);
1439 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1440 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1441 0 /* handle */, 0 /* func */);
1442 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1443 zebraExplain_removeDatabase(zh->reg->zei, zh);
1447 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1448 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1449 "drop database only supported for isam:b");
1452 zebra_end_trans (zh);
1456 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1459 yaz_log(log_level, "zebra_create_database %s", db);
1462 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1464 if (zebra_begin_trans (zh, 1))
1467 /* announce database */
1468 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1469 /* explainDatabase */))
1471 zebra_end_trans (zh);
1472 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1475 return zebra_end_trans (zh);
1478 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1479 const char *input_str, int input_len,
1480 char *output_str, int output_len)
1486 yaz_log(log_level, "zebra_string_norm ");
1488 if (!zh->reg->zebra_maps)
1490 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1491 input_str, input_len);
1494 if (wrbuf_len(wrbuf) >= output_len)
1496 if (wrbuf_len(wrbuf))
1497 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1498 output_str[wrbuf_len(wrbuf)] = '\0';
1499 return wrbuf_len(wrbuf);
1502 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1504 char state_fname[256];
1509 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1511 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1512 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1513 f = fopen (fname, "w");
1515 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1516 fprintf (f, "%c %d %ld\n", val, seqno, p);
1521 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1523 char state_fname[256];
1528 yaz_log(log_level, "zebra_get_state ");
1530 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1531 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1532 f = fopen (fname, "r");
1538 fscanf (f, "%c %d", val, seqno);
1544 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1546 return zebra_begin_trans(zh, 0);
1549 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1551 return zebra_end_trans(zh);
1554 static void read_res_for_transaction(ZebraHandle zh)
1556 const char *group = res_get(zh->res, "group");
1558 /* FIXME - do we still use groups ?? */
1560 zh->m_group = group;
1561 v = res_get_prefix(zh->res, "followLinks", group, "1");
1562 zh->m_follow_links = atoi(v);
1564 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1565 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1567 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1568 zh->m_store_keys = atoi(v);
1570 v = res_get_prefix(zh->res, "storeData", group, "1");
1571 zh->m_store_data = atoi(v);
1573 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1574 zh->m_explain_database = atoi(v);
1576 v = res_get_prefix(zh->res, "openRW", group, "1");
1577 zh->m_flag_rw = atoi(v);
1579 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1580 zh->m_file_verbose_limit = atoi(v);
1583 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1586 zebra_select_default_database(zh);
1589 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1590 "zebra_begin_trans: no database selected");
1594 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1598 if (rw && !strchr(zh->user_perm, 'w'))
1602 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1614 const char *rval = 0;
1619 read_res_for_transaction(zh);
1622 if (zh->trans_no != 1)
1624 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1625 "zebra_begin_trans: no write trans within read");
1630 resultSetInvalidate (zh);
1631 zebra_register_close (zh->service, zh->reg);
1633 zh->trans_w_no = zh->trans_no;
1635 zh->records_inserted = 0;
1636 zh->records_updated = 0;
1637 zh->records_deleted = 0;
1638 zh->records_processed = 0;
1640 #if HAVE_SYS_TIMES_H
1644 if (zh->shadow_enable)
1645 rval = res_get (zh->res, "shadow");
1647 for (pass = 0; pass < 2; pass++)
1651 zebra_lock_r (zh->lock_normal);
1652 zebra_lock_w (zh->lock_shadow);
1656 zebra_lock_w (zh->lock_normal);
1657 zebra_lock_w (zh->lock_shadow);
1660 zebra_get_state (zh, &val, &seqno);
1663 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1664 zebra_unlock (zh->lock_shadow);
1665 zebra_unlock (zh->lock_normal);
1669 else if (val == 'd')
1673 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1675 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1676 bf_commitClean (bfs, rval);
1681 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1688 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1692 zebra_set_state (zh, 'd', seqno);
1694 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1695 1, rval ? 1 : 0, zh->res,
1698 zh->reg->seqno = seqno;
1701 zebra_set_state (zh, 'o', seqno);
1703 zebra_unlock (zh->lock_shadow);
1704 zebra_unlock (zh->lock_normal);
1709 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1710 "zebra_begin_trans: cannot open register");
1711 yaz_log(YLOG_FATAL, "%s", zh->errString);
1714 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1724 if (zh->trans_no != 1)
1726 zebra_flush_reg (zh);
1729 #if HAVE_SYS_TIMES_H
1735 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1738 if (!zh->lock_normal || !zh->lock_shadow)
1741 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1744 zebra_get_state (zh, &val, &seqno);
1750 else if (seqno != zh->reg->seqno)
1752 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1753 seqno, zh->reg->seqno);
1756 else if (zh->reg->last_val != val)
1758 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1759 val, zh->reg->last_val);
1766 zebra_lock_r (zh->lock_shadow);
1768 zebra_lock_r (zh->lock_normal);
1772 resultSetInvalidate (zh);
1773 zebra_register_close (zh->service, zh->reg);
1775 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1776 0, val == 'c' ? 1 : 0,
1777 zh->res, zh->path_reg);
1780 zebra_unlock (zh->lock_normal);
1781 zebra_unlock (zh->lock_shadow);
1783 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1786 zh->reg->last_val = val;
1787 zh->reg->seqno = seqno;
1789 read_res_for_transaction(zh);
1793 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1795 ZebraTransactionStatus dummy;
1797 yaz_log(log_level, "zebra_end_trans");
1798 return zebra_end_transaction(zh, &dummy);
1801 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1809 yaz_log(log_level, "zebra_end_transaction");
1811 status->processed = 0;
1812 status->inserted = 0;
1813 status->updated = 0;
1814 status->deleted = 0;
1818 if (!zh->res || !zh->reg)
1820 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1821 "zebra_end_trans: no open transaction");
1824 if (zh->trans_no != zh->trans_w_no)
1827 if (zh->trans_no != 0)
1830 /* release read lock */
1832 zebra_unlock (zh->lock_normal);
1833 zebra_unlock (zh->lock_shadow);
1836 { /* release write lock */
1840 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1841 rval = res_get (zh->res, "shadow");
1843 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1845 zebra_flush_reg (zh);
1847 resultSetInvalidate (zh);
1849 zebra_register_close (zh->service, zh->reg);
1852 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1853 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1854 zh->records_processed, zh->records_inserted,
1855 zh->records_updated, zh->records_deleted);
1857 status->processed = (int) zh->records_processed;
1858 status->inserted = (int) zh->records_inserted;
1859 status->updated = (int) zh->records_updated;
1860 status->deleted = (int) zh->records_deleted;
1862 zebra_get_state (zh, &val, &seqno);
1865 BFiles bfs = bfs_create (rval, zh->path_reg);
1866 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1867 bf_commitClean (bfs, rval);
1872 zebra_set_state (zh, 'o', seqno);
1874 zebra_unlock (zh->lock_shadow);
1875 zebra_unlock (zh->lock_normal);
1878 #if HAVE_SYS_TIMES_H
1880 yaz_log (log_level, "user/system: %ld/%ld",
1881 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1882 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1884 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1885 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1890 int zebra_repository_update (ZebraHandle zh, const char *path)
1894 yaz_log (log_level, "updating %s", path);
1895 repositoryUpdate (zh, path);
1899 int zebra_repository_delete (ZebraHandle zh, const char *path)
1903 yaz_log (log_level, "deleting %s", path);
1904 repositoryDelete (zh, path);
1908 int zebra_repository_show (ZebraHandle zh, const char *path)
1912 yaz_log(log_level, "zebra_repository_show");
1913 repositoryShow (zh, path);
1917 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1925 zebra_select_default_database(zh);
1928 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1931 rval = res_get (zh->res, "shadow");
1934 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1938 zebra_lock_w (zh->lock_normal);
1939 zebra_lock_r (zh->lock_shadow);
1941 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1943 zebra_get_state (zh, &val, &seqno);
1946 bf_cache (bfs, rval);
1947 if (bf_commitExists (bfs))
1950 zebra_set_state (zh, 'd', seqno);
1953 zebra_set_state (zh, 'c', seqno);
1955 yaz_log (YLOG_DEBUG, "commit start");
1956 bf_commitExec (bfs);
1961 yaz_log (YLOG_DEBUG, "commit clean");
1962 bf_commitClean (bfs, rval);
1964 zebra_set_state (zh, 'o', seqno);
1968 yaz_log (log_level, "nothing to commit");
1972 zebra_unlock (zh->lock_shadow);
1973 zebra_unlock (zh->lock_normal);
1977 ZEBRA_RES zebra_clean(ZebraHandle zh)
1980 yaz_log(log_level, "zebra_clean");
1981 return zebra_commit_ex(zh, 1);
1984 ZEBRA_RES zebra_commit(ZebraHandle zh)
1987 yaz_log(log_level, "zebra_commit");
1988 return zebra_commit_ex(zh, 0);
1991 ZEBRA_RES zebra_init(ZebraHandle zh)
1996 yaz_log(log_level, "zebra_init");
1998 zebra_select_default_database(zh);
2001 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2002 "cannot select default database");
2005 rval = res_get (zh->res, "shadow");
2007 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2010 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2014 bf_cache (bfs, rval);
2018 zebra_set_state (zh, 'o', 0);
2022 ZEBRA_RES zebra_compact(ZebraHandle zh)
2026 yaz_log(log_level, "zebra_compact");
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 zh->shadow_enable = value;
2053 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2057 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2059 if (zh->iconv_to_utf8 != 0)
2060 yaz_iconv_close(zh->iconv_to_utf8);
2061 if (zh->iconv_from_utf8 != 0)
2062 yaz_iconv_close(zh->iconv_from_utf8);
2065 yaz_iconv_open ("UTF-8", encoding);
2066 if (zh->iconv_to_utf8 == 0)
2067 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2068 zh->iconv_from_utf8 =
2069 yaz_iconv_open (encoding, "UTF-8");
2070 if (zh->iconv_to_utf8 == 0)
2071 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2076 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2079 yaz_log(log_level, "zebra_record_encoding");
2080 xfree(zh->record_encoding);
2081 zh->record_encoding = 0;
2083 zh->record_encoding = xstrdup (encoding);
2087 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2092 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2093 res_set(zh->res, name, value);
2096 const char *zebra_get_resource(ZebraHandle zh,
2097 const char *name, const char *defaultvalue)
2102 v = res_get_def (zh->res, name, (char *)defaultvalue);
2103 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2107 /* moved from zebra_api_ext.c by pop */
2108 /* FIXME: Should this really be public??? -Heikki */
2110 int zebra_trans_no (ZebraHandle zh)
2113 yaz_log(log_level, "zebra_trans_no");
2114 return zh->trans_no;
2117 int zebra_get_shadow_enable (ZebraHandle zh)
2120 yaz_log(log_level, "zebra_get_shadow_enable");
2121 return zh->shadow_enable;
2124 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2127 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2128 zh->shadow_enable = value;
2131 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2132 so that it's identicical to the original api_records_retrieve */
2133 void api_records_retrieve (ZebraHandle zh, ODR stream,
2134 const char *setname, Z_RecordComposition *comp,
2135 oid_value input_format, int num_recs,
2136 ZebraRetrievalRecord *recs)
2138 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2142 /* ---------------------------------------------------------------------------
2143 Record insert(=update), delete
2145 If sysno is provided, then it's used to identify the record.
2146 If not, and match_criteria is provided, then sysno is guessed
2147 If not, and a record is provided, then sysno is got from there
2148 NOTE: Now returns 0 at success and updates sysno, which is an int*
2152 int zebra_add_record(ZebraHandle zh,
2153 const char *buf, int buf_size)
2155 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2158 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2159 const char *recordType,
2160 SYSNO *sysno, const char *match,
2162 const char *buf, int buf_size, int force_update)
2168 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2171 buf_size = strlen(buf);
2173 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2175 res = buffer_extract_record (zh, buf, buf_size,
2176 0, /* delete_flag */
2182 0); /* allow_update */
2183 zebra_end_trans(zh);
2187 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2188 const char *recordType,
2189 SYSNO* sysno, const char *match,
2191 const char *buf, int buf_size,
2198 yaz_log(log_level, "zebra_update_record");
2200 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2202 if (buf_size < 1) buf_size = strlen(buf);
2204 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2206 res = buffer_extract_record (zh, buf, buf_size,
2207 0, /* delete_flag */
2213 1); /* allow_update */
2214 zebra_end_trans(zh);
2218 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2219 const char *recordType,
2220 SYSNO *sysno, const char *match,
2222 const char *buf, int buf_size,
2229 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2231 if (buf_size < 1) buf_size = strlen(buf);
2233 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2235 res = buffer_extract_record (zh, buf, buf_size,
2236 1, /* delete_flag */
2242 1); /* allow_update */
2243 zebra_end_trans(zh);
2247 /* ---------------------------------------------------------------------------
2251 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2252 const char *setname, zint *hits)
2255 ZEBRA_RES res = ZEBRA_OK;
2257 ODR odr = odr_createmem(ODR_ENCODE);
2262 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2264 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2268 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2269 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2273 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2277 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2285 /* ---------------------------------------------------------------------------
2286 Sort - a simplified interface, with optional read locks.
2288 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2289 const char *sort_spec,
2290 const char *output_setname,
2291 const char **input_setnames)
2293 int num_input_setnames = 0;
2294 int sort_status = 0;
2295 Z_SortKeySpecList *sort_sequence;
2299 assert(output_setname);
2300 assert(input_setnames);
2301 sort_sequence = yaz_sort_spec (stream, sort_spec);
2302 yaz_log(log_level, "sort (FIXME) ");
2305 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2306 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2310 /* we can do this, since the perl typemap code for char** will
2311 put a NULL at the end of list */
2312 while (input_setnames[num_input_setnames]) num_input_setnames++;
2314 if (zebra_begin_read (zh))
2317 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2318 output_setname, sort_sequence, &sort_status);
2324 /* ---------------------------------------------------------------------------
2325 Get BFS for Zebra system (to make alternative storage methods)
2327 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2330 return zh->reg->bfs;
2335 /* ---------------------------------------------------------------------------
2336 Set limit for search/scan
2338 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2341 zebra_limit_destroy(zh->m_limit);
2342 zh->m_limit = zebra_limit_create(complement_flag, ids);
2347 Set Error code + addinfo
2349 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2352 nmem_reset(zh->nmem_error);
2353 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2356 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2359 sprintf(vstr, ZINT_FORMAT, i);
2362 nmem_reset(zh->nmem_error);
2363 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2366 void zebra_lock_prefix (Res res, char *path)
2368 const char *lock_dir = res_get_def (res, "lockDir", "");
2370 strcpy (path, lock_dir);
2371 if (*path && path[strlen(path)-1] != '/')