1 /* $Id: zebraapi.c,v 1.204 2006-03-23 09:15:25 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);
305 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
308 data1_destroy(reg->dh);
314 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
316 bfs_destroy(reg->bfs);
317 data1_destroy(reg->dh);
323 getcwd(cwd, sizeof(cwd)-1);
324 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
325 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
327 data1_set_tabpath (reg->dh, profilePath);
328 data1_set_tabroot (reg->dh, reg_path);
329 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
331 if ((reg->zebra_maps = zebra_maps_open (res, reg_path)) == 0) {
332 /* ### Do we need to destroy reg->recTypes? */
333 bfs_destroy(reg->bfs);
334 data1_destroy(reg->dh);
339 reg->rank_classes = NULL;
343 reg->keys = zebra_rec_keys_open();
346 reg->sortKeys = zebra_rec_keys_open();
348 reg->sortKeys.buf = 0;
349 reg->sortKeys.buf_max = 0;
360 reg->key_file_no = 0;
363 zebraRankInstall (reg, rank_1_class);
364 zebraRankInstall (reg, rank_zv_class);
365 zebraRankInstall (reg, rank_static_class);
367 recordCompression = res_get_def (res, "recordCompression", "none");
368 if (!strcmp (recordCompression, "none"))
369 record_compression = REC_COMPRESS_NONE;
370 if (!strcmp (recordCompression, "bzip2"))
371 record_compression = REC_COMPRESS_BZIP2;
373 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
375 yaz_log (YLOG_WARN, "rec_open failed");
380 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
382 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
384 yaz_log (YLOG_WARN, "dict_open failed");
387 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
389 yaz_log (YLOG_WARN, "sortIdx_open failed");
392 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
394 struct ISAMS_M_s isams_m;
395 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
396 key_isams_m(res, &isams_m))))
398 yaz_log (YLOG_WARN, "isams_open failed");
402 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
404 struct ISAMC_M_s isamc_m;
405 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
406 rw, key_isamc_m(res, &isamc_m))))
408 yaz_log (YLOG_WARN, "isamc_open failed");
412 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
414 struct ISAMC_M_s isamc_m;
416 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
417 rw, key_isamc_m(res, &isamc_m), 0)))
419 yaz_log (YLOG_WARN, "isamb_open failed");
423 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
425 struct ISAMC_M_s isamc_m;
427 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
428 rw, key_isamc_m(res, &isamc_m), 1)))
430 yaz_log (YLOG_WARN, "isamb_open failed");
434 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
436 struct ISAMC_M_s isamc_m;
438 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
439 rw, key_isamc_m(res, &isamc_m), -1)))
441 yaz_log (YLOG_WARN, "isamb_open failed");
445 reg->zei = zebraExplain_open (reg->records, reg->dh,
450 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
455 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
459 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
462 yaz_log(log_level, "zebra_admin_shutdown");
464 zebra_mutex_cond_lock (&zh->service->session_lock);
465 zh->service->stop_flag = 1;
466 zebra_mutex_cond_unlock (&zh->service->session_lock);
470 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
474 yaz_log(log_level, "zebra_admin_start");
476 zebra_mutex_cond_lock (&zs->session_lock);
477 zebra_mutex_cond_unlock (&zs->session_lock);
481 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
485 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
490 zebraExplain_close (reg->zei);
491 dict_close (reg->dict);
493 dict_close (reg->matchDict);
494 sortIdx_close (reg->sortIdx);
496 isams_close (reg->isams);
498 isamc_close (reg->isamc);
500 isamb_close (reg->isamb);
501 rec_close (®->records);
504 recTypes_destroy (reg->recTypes);
505 zebra_maps_close (reg->zebra_maps);
506 zebraRankDestroy (reg);
507 bfs_destroy (reg->bfs);
508 data1_destroy (reg->dh);
510 zebra_rec_keys_close(reg->keys);
512 zebra_rec_keys_close(reg->sortKeys);
514 xfree(reg->sortKeys.buf);
522 ZEBRA_RES zebra_stop(ZebraService zs)
526 yaz_log (log_level, "zebra_stop");
530 zebra_close (zs->sessions);
533 zebra_mutex_cond_destroy (&zs->session_lock);
536 passwd_db_close (zs->passwd_db);
538 recTypeClass_destroy(zs->record_classes);
539 nmem_destroy(zs->nmem);
540 res_close (zs->global_res);
545 ZEBRA_RES zebra_close (ZebraHandle zh)
548 struct zebra_session **sp;
551 yaz_log(log_level, "zebra_close");
558 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
559 resultSetDestroy (zh, -1, 0, 0);
562 zebra_register_close (zh->service, zh->reg);
563 zebra_close_res (zh);
564 res_close(zh->session_res);
566 xfree(zh->record_encoding);
568 xfree(zh->dbaccesslist);
570 for (i = 0; i < zh->num_basenames; i++)
571 xfree(zh->basenames[i]);
572 xfree(zh->basenames);
574 if (zh->iconv_to_utf8 != 0)
575 yaz_iconv_close (zh->iconv_to_utf8);
576 if (zh->iconv_from_utf8 != 0)
577 yaz_iconv_close (zh->iconv_from_utf8);
579 zebra_mutex_cond_lock (&zs->session_lock);
580 zebra_lock_destroy (zh->lock_normal);
581 zebra_lock_destroy (zh->lock_shadow);
593 zebra_mutex_cond_unlock (&zs->session_lock);
595 xfree(zh->user_perm);
596 zh->service = 0; /* more likely to trigger an assert */
598 zebra_limit_destroy(zh->m_limit);
600 nmem_destroy(zh->nmem_error);
607 struct map_baseinfo {
613 char **new_basenames;
617 static void zebra_open_res(ZebraHandle zh)
625 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
626 zh->res = res_open(zh->session_res, 0);
627 res_read_file(zh->res, fname);
629 else if (*zh->reg_name == 0)
631 zh->res = res_open(zh->session_res, 0);
635 yaz_log (YLOG_WARN, "no register root specified");
636 zh->res = 0; /* no path for register - fail! */
640 static void zebra_close_res (ZebraHandle zh)
648 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
652 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
656 assert (zh->reg == 0);
657 assert (*zh->reg_name == 0);
663 resultSetInvalidate (zh);
664 zebra_register_close (zh->service, zh->reg);
670 zh->reg_name = xstrdup (new_reg);
674 if (zh->service->path_root)
676 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
677 strlen(zh->reg_name) + 3);
678 strcpy (zh->path_reg, zh->service->path_root);
681 strcat (zh->path_reg, "/");
682 strcat (zh->path_reg, zh->reg_name);
688 zebra_lock_destroy (zh->lock_normal);
692 zebra_lock_destroy (zh->lock_shadow);
698 const char *lock_area = res_get (zh->res, "lockDir");
700 if (!lock_area && zh->path_reg)
701 res_set (zh->res, "lockDir", zh->path_reg);
702 sprintf (fname, "norm.%s.LCK", zh->reg_name);
704 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
706 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
708 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
710 if (!zh->lock_normal || !zh->lock_shadow)
714 zebra_lock_destroy(zh->lock_normal);
719 zebra_lock_destroy(zh->lock_shadow);
728 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
729 zebra_set_approx_limit(zh, approx);
733 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
734 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
736 yaz_log(YLOG_LOG, "static rank unset");
740 void map_basenames_func (void *vp, const char *name, const char *value)
742 struct map_baseinfo *p = (struct map_baseinfo *) vp;
744 char fromdb[128], todb[8][128];
751 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
752 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
753 todb[5], todb[6], todb[7]);
757 for (i = 0; i<p->num_bases; i++)
758 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
761 for (i = 0; i < no; i++)
763 if (p->new_num_bases == p->new_num_max)
765 p->new_basenames[(p->new_num_bases)++] =
766 nmem_strdup (p->mem, todb[i]);
772 int zebra_select_default_database(ZebraHandle zh)
776 /* no database has been selected - so we select based on
777 resource setting (including group)
779 const char *group = res_get(zh->session_res, "group");
780 const char *v = res_get_prefix(zh->session_res,
781 "database", group, "Default");
782 return zebra_select_database(zh, v);
787 void map_basenames (ZebraHandle zh, ODR stream,
788 int *num_bases, char ***basenames)
790 struct map_baseinfo info;
791 struct map_baseinfo *p = &info;
794 yaz_log(log_level, "map_basenames ");
799 info.num_bases = *num_bases;
800 info.basenames = *basenames;
801 info.new_num_max = 128;
802 info.new_num_bases = 0;
803 info.new_basenames = (char **)
804 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
805 info.mem = stream->mem;
807 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
809 for (i = 0; i<p->num_bases; i++)
810 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
812 p->new_basenames[(p->new_num_bases)++] =
813 nmem_strdup (p->mem, p->basenames[i]);
815 *num_bases = info.new_num_bases;
816 *basenames = info.new_basenames;
817 for (i = 0; i<*num_bases; i++)
818 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
821 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
824 yaz_log(log_level, "zebra_select_database %s",basename);
826 return zebra_select_databases (zh, 1, &basename);
829 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
830 const char **basenames)
839 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
840 num_bases,basenames[0]);
845 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
849 /* Check if the user has access to all databases (Seb) */
850 /* You could argue that this should happen later, after we have
851 * determined that the database(s) exist. */
852 if (zh->dbaccesslist) {
853 for (i = 0; i < num_bases; i++) {
854 const char *db = basenames[i];
856 for (p = zh->dbaccesslist; p && *p; p = pp) {
858 if ((pp = strchr(p, '+'))) {
864 if (len == strlen(db) && !strncmp(db, p, len))
868 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
874 for (i = 0; i < zh->num_basenames; i++)
875 xfree(zh->basenames[i]);
876 xfree(zh->basenames);
878 zh->num_basenames = num_bases;
879 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
880 for (i = 0; i < zh->num_basenames; i++)
881 zh->basenames[i] = xstrdup (basenames[i]);
883 cp = strrchr(basenames[0], '/');
886 len = cp - basenames[0];
887 new_reg = xmalloc(len + 1);
888 memcpy (new_reg, basenames[0], len);
892 new_reg = xstrdup ("");
893 for (i = 1; i<num_bases; i++)
897 cp1 = strrchr (basenames[i], '/');
902 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
905 if (len != cp1 - basenames[i] ||
906 memcmp (basenames[i], new_reg, len))
908 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
916 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
921 zebra_select_register (zh, new_reg);
925 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
928 if (!zh->lock_normal || !zh->lock_shadow)
930 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
936 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
938 if (approx_limit == 0)
939 approx_limit = DEFAULT_APPROX_LIMIT;
940 zh->approx_limit = approx_limit;
944 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
945 const char *setname, zint *hits)
953 yaz_log(log_level, "zebra_search_rpn");
957 if (zebra_begin_read(zh) == ZEBRA_FAIL)
960 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
961 zh->num_basenames, zh->basenames, setname);
967 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
969 Z_RecordComposition *comp,
970 oid_value input_format, int num_recs,
971 ZebraRetrievalRecord *recs)
973 ZebraMetaRecord *poset;
975 ZEBRA_RES ret = ZEBRA_OK;
983 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
987 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
992 if (zebra_begin_read (zh) == ZEBRA_FAIL)
995 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
996 for (i = 0; i<num_recs; i++)
997 pos_array[i] = recs[i].position;
998 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1001 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1002 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1008 for (i = 0; i<num_recs; i++)
1012 recs[i].errCode = 0;
1013 recs[i].format = VAL_SUTRS;
1014 recs[i].len = strlen(poset[i].term);
1015 recs[i].buf = poset[i].term;
1016 recs[i].base = poset[i].db;
1018 else if (poset[i].sysno)
1022 zebra_snippets *hit_snippet = zebra_snippets_create();
1024 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1028 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1030 stream, input_format, comp,
1031 &recs[i].format, &buf, &len,
1032 &recs[i].base, &recs[i].errString);
1037 recs[i].buf = (char*) odr_malloc(stream, len);
1038 memcpy(recs[i].buf, buf, len);
1042 recs[i].score = poset[i].score;
1043 recs[i].sysno = poset[i].sysno;
1044 zebra_snippets_destroy(hit_snippet);
1048 /* only need to set it once */
1049 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1051 zebra_setError_zint(zh,
1052 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1057 recs[i].buf = 0; /* no record and no error issued */
1059 recs[i].errCode = 0;
1060 recs[i].format = VAL_NONE;
1064 zebra_meta_records_destroy(zh, poset, num_recs);
1066 zebra_end_read (zh);
1071 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1073 int *num_entries, ZebraScanEntry **entries,
1075 const char *setname)
1077 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1078 Z_AttributesPlusTerm *zapt;
1082 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1085 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1088 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1089 position, num_entries, entries, is_partial,
1091 yaz_pqf_destroy (pqf_parser);
1095 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1096 oid_value attributeset,
1098 int *num_entries, ZebraScanEntry **entries,
1100 const char *setname)
1103 RSET limit_rset = 0;
1108 assert(num_entries);
1111 yaz_log(log_level, "zebra_scan");
1113 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1121 limit_rset = resultSetRef(zh, setname);
1125 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1127 zebra_end_read (zh);
1131 res = rpn_scan (zh, stream, zapt, attributeset,
1132 zh->num_basenames, zh->basenames, position,
1133 num_entries, entries, is_partial, limit_rset, 0);
1134 zebra_end_read (zh);
1138 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1139 int num_input_setnames, const char **input_setnames,
1140 const char *output_setname,
1141 Z_SortKeySpecList *sort_sequence,
1147 assert(num_input_setnames>0);
1148 assert(input_setnames);
1149 assert(sort_sequence);
1150 assert(sort_status);
1151 yaz_log(log_level, "zebra_sort");
1153 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1155 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1156 output_setname, sort_sequence, sort_status);
1161 int zebra_deleteResultSet(ZebraHandle zh, int function,
1162 int num_setnames, char **setnames,
1168 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1170 if (zebra_begin_read(zh))
1171 return Z_DeleteStatus_systemProblemAtTarget;
1174 case Z_DeleteResultSetRequest_list:
1175 assert(num_setnames>0);
1177 resultSetDestroy (zh, num_setnames, setnames, statuses);
1179 case Z_DeleteResultSetRequest_all:
1180 resultSetDestroy (zh, -1, 0, statuses);
1183 zebra_end_read (zh);
1184 status = Z_DeleteStatus_success;
1185 for (i = 0; i<num_setnames; i++)
1186 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1187 status = statuses[i];
1191 int zebra_errCode (ZebraHandle zh)
1195 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1198 yaz_log(log_level, "zebra_errCode: o");
1202 const char *zebra_errString (ZebraHandle zh)
1206 e= diagbib1_str (zh->errCode);
1207 yaz_log(log_level, "zebra_errString: %s",e);
1211 char *zebra_errAdd (ZebraHandle zh)
1216 yaz_log(log_level, "zebra_errAdd: %s",a);
1220 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1223 const char *astring;
1231 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1232 p = res_get(zs->global_res, u);
1233 xfree(zh->user_perm);
1234 zh->user_perm = xstrdup(p ? p : "r");
1236 /* Determine database access list */
1237 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1239 zh->dbaccesslist = xstrdup(astring);
1241 zh->dbaccesslist = 0;
1243 /* users that don't require a password .. */
1244 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1247 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1252 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1253 const char *record_type)
1256 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1257 database, record_type);
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 return zebra_end_trans(zh);
1270 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1272 ZEBRA_RES res = ZEBRA_OK;
1276 yaz_log(log_level, "zebra_admin_import_segment");
1278 for (i = 0; i<segment->num_segmentRecords; i++)
1280 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1282 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1284 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1285 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1287 Odr_oct *oct = fragment->u.notExternallyTagged;
1290 if (zebra_update_record(zh,
1291 0, /* record Type */
1295 (const char *) oct->buf, oct->len,
1304 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1305 const char *rec_buf,
1307 const char *recid_buf, size_t recid_len,
1309 /* 1 = insert. Fail it already exists */
1310 /* 2 = replace. Fail it does not exist */
1311 /* 3 = delete. Fail if does not exist */
1312 /* 4 = update. Insert/replace */
1320 assert(action>0 && action <=4);
1323 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1325 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1327 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1328 "no record ID or empty record ID");
1332 memcpy (recid_z, recid_buf, recid_len);
1333 recid_z[recid_len] = 0;
1335 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1338 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1339 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, 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_ord(zh->reg->matchDict, db_ord, recid_z,
1381 sizeof(sysno), &sysno);
1383 else if (action == 3)
1385 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1387 zebra_end_trans(zh);
1391 int delete_w_handle(const char *info, void *handle)
1393 ZebraHandle zh = (ZebraHandle) handle;
1397 if (*info == sizeof(pos))
1399 memcpy (&pos, info+1, sizeof(pos));
1400 isamb_unlink(zh->reg->isamb, pos);
1405 static int delete_SU_handle(void *handle, int ord)
1407 ZebraHandle zh = (ZebraHandle) handle;
1411 ord_len = key_SU_encode (ord, ord_buf);
1412 ord_buf[ord_len] = '\0';
1414 assert (zh->reg->isamb);
1415 dict_delete_subtree(zh->reg->dict, ord_buf,
1416 zh, delete_w_handle);
1420 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1422 ZEBRA_RES ret = ZEBRA_OK;
1424 yaz_log(log_level, "zebra_drop_database %s", db);
1426 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1428 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1433 zebraExplain_curDatabase (zh->reg->zei, db);
1434 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1435 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1436 0 /* handle */, 0 /* func */);
1437 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1438 zebraExplain_removeDatabase(zh->reg->zei, zh);
1442 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1443 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1444 "drop database only supported for isam:b");
1447 zebra_end_trans (zh);
1451 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1454 yaz_log(log_level, "zebra_create_database %s", db);
1457 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1459 if (zebra_begin_trans (zh, 1))
1462 /* announce database */
1463 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1464 /* explainDatabase */))
1466 zebra_end_trans (zh);
1467 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1470 return zebra_end_trans (zh);
1473 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1474 const char *input_str, int input_len,
1475 char *output_str, int output_len)
1481 yaz_log(log_level, "zebra_string_norm ");
1483 if (!zh->reg->zebra_maps)
1485 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1486 input_str, input_len);
1489 if (wrbuf_len(wrbuf) >= output_len)
1491 if (wrbuf_len(wrbuf))
1492 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1493 output_str[wrbuf_len(wrbuf)] = '\0';
1494 return wrbuf_len(wrbuf);
1497 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1499 char state_fname[256];
1504 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1506 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1507 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1508 f = fopen (fname, "w");
1510 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1511 fprintf (f, "%c %d %ld\n", val, seqno, p);
1516 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1518 char state_fname[256];
1523 yaz_log(log_level, "zebra_get_state ");
1525 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1526 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1527 f = fopen (fname, "r");
1533 fscanf (f, "%c %d", val, seqno);
1539 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1541 return zebra_begin_trans(zh, 0);
1544 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1546 return zebra_end_trans(zh);
1549 static void read_res_for_transaction(ZebraHandle zh)
1551 const char *group = res_get(zh->res, "group");
1553 /* FIXME - do we still use groups ?? */
1555 zh->m_group = group;
1556 v = res_get_prefix(zh->res, "followLinks", group, "1");
1557 zh->m_follow_links = atoi(v);
1559 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1560 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1562 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1563 zh->m_store_keys = atoi(v);
1565 v = res_get_prefix(zh->res, "storeData", group, "1");
1566 zh->m_store_data = atoi(v);
1568 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1569 zh->m_explain_database = atoi(v);
1571 v = res_get_prefix(zh->res, "openRW", group, "1");
1572 zh->m_flag_rw = atoi(v);
1574 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1575 zh->m_file_verbose_limit = atoi(v);
1578 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1581 zebra_select_default_database(zh);
1584 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1585 "zebra_begin_trans: no database selected");
1589 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1593 if (rw && !strchr(zh->user_perm, 'w'))
1597 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1609 const char *rval = 0;
1614 read_res_for_transaction(zh);
1617 if (zh->trans_no != 1)
1619 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1620 "zebra_begin_trans: no write trans within read");
1625 resultSetInvalidate (zh);
1626 zebra_register_close (zh->service, zh->reg);
1628 zh->trans_w_no = zh->trans_no;
1630 zh->records_inserted = 0;
1631 zh->records_updated = 0;
1632 zh->records_deleted = 0;
1633 zh->records_processed = 0;
1635 #if HAVE_SYS_TIMES_H
1639 if (zh->shadow_enable)
1640 rval = res_get (zh->res, "shadow");
1642 for (pass = 0; pass < 2; pass++)
1646 zebra_lock_r (zh->lock_normal);
1647 zebra_lock_w (zh->lock_shadow);
1651 zebra_lock_w (zh->lock_normal);
1652 zebra_lock_w (zh->lock_shadow);
1655 zebra_get_state (zh, &val, &seqno);
1658 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1659 zebra_unlock (zh->lock_shadow);
1660 zebra_unlock (zh->lock_normal);
1664 else if (val == 'd')
1668 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1670 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1671 bf_commitClean (bfs, rval);
1676 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1683 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1687 zebra_set_state (zh, 'd', seqno);
1689 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1690 1, rval ? 1 : 0, zh->res,
1693 zh->reg->seqno = seqno;
1696 zebra_set_state (zh, 'o', seqno);
1698 zebra_unlock (zh->lock_shadow);
1699 zebra_unlock (zh->lock_normal);
1704 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1705 "zebra_begin_trans: cannot open register");
1706 yaz_log(YLOG_FATAL, "%s", zh->errString);
1709 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1719 if (zh->trans_no != 1)
1721 zebra_flush_reg (zh);
1724 #if HAVE_SYS_TIMES_H
1730 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1733 if (!zh->lock_normal || !zh->lock_shadow)
1736 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1739 zebra_get_state (zh, &val, &seqno);
1745 else if (seqno != zh->reg->seqno)
1747 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1748 seqno, zh->reg->seqno);
1751 else if (zh->reg->last_val != val)
1753 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1754 val, zh->reg->last_val);
1761 zebra_lock_r (zh->lock_shadow);
1763 zebra_lock_r (zh->lock_normal);
1767 resultSetInvalidate (zh);
1768 zebra_register_close (zh->service, zh->reg);
1770 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1771 0, val == 'c' ? 1 : 0,
1772 zh->res, zh->path_reg);
1775 zebra_unlock (zh->lock_normal);
1776 zebra_unlock (zh->lock_shadow);
1778 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1781 zh->reg->last_val = val;
1782 zh->reg->seqno = seqno;
1784 read_res_for_transaction(zh);
1788 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1790 ZebraTransactionStatus dummy;
1792 yaz_log(log_level, "zebra_end_trans");
1793 return zebra_end_transaction(zh, &dummy);
1796 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1804 yaz_log(log_level, "zebra_end_transaction");
1806 status->processed = 0;
1807 status->inserted = 0;
1808 status->updated = 0;
1809 status->deleted = 0;
1813 if (!zh->res || !zh->reg)
1815 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1816 "zebra_end_trans: no open transaction");
1819 if (zh->trans_no != zh->trans_w_no)
1822 if (zh->trans_no != 0)
1825 /* release read lock */
1827 zebra_unlock (zh->lock_normal);
1828 zebra_unlock (zh->lock_shadow);
1831 { /* release write lock */
1835 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1836 rval = res_get (zh->res, "shadow");
1838 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1840 zebra_flush_reg (zh);
1842 resultSetInvalidate (zh);
1844 zebra_register_close (zh->service, zh->reg);
1847 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1848 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1849 zh->records_processed, zh->records_inserted,
1850 zh->records_updated, zh->records_deleted);
1852 status->processed = (int) zh->records_processed;
1853 status->inserted = (int) zh->records_inserted;
1854 status->updated = (int) zh->records_updated;
1855 status->deleted = (int) zh->records_deleted;
1857 zebra_get_state (zh, &val, &seqno);
1860 BFiles bfs = bfs_create (rval, zh->path_reg);
1861 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1862 bf_commitClean (bfs, rval);
1867 zebra_set_state (zh, 'o', seqno);
1869 zebra_unlock (zh->lock_shadow);
1870 zebra_unlock (zh->lock_normal);
1873 #if HAVE_SYS_TIMES_H
1875 yaz_log (log_level, "user/system: %ld/%ld",
1876 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1877 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1879 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1880 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1885 int zebra_repository_update (ZebraHandle zh, const char *path)
1889 yaz_log (log_level, "updating %s", path);
1890 repositoryUpdate (zh, path);
1894 int zebra_repository_delete (ZebraHandle zh, const char *path)
1898 yaz_log (log_level, "deleting %s", path);
1899 repositoryDelete (zh, path);
1903 int zebra_repository_show (ZebraHandle zh, const char *path)
1907 yaz_log(log_level, "zebra_repository_show");
1908 repositoryShow (zh, path);
1912 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1920 zebra_select_default_database(zh);
1923 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1926 rval = res_get (zh->res, "shadow");
1929 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1933 zebra_lock_w (zh->lock_normal);
1934 zebra_lock_r (zh->lock_shadow);
1936 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1938 zebra_get_state (zh, &val, &seqno);
1941 bf_cache (bfs, rval);
1942 if (bf_commitExists (bfs))
1945 zebra_set_state (zh, 'd', seqno);
1948 zebra_set_state (zh, 'c', seqno);
1950 yaz_log (YLOG_DEBUG, "commit start");
1951 bf_commitExec (bfs);
1956 yaz_log (YLOG_DEBUG, "commit clean");
1957 bf_commitClean (bfs, rval);
1959 zebra_set_state (zh, 'o', seqno);
1963 yaz_log (log_level, "nothing to commit");
1967 zebra_unlock (zh->lock_shadow);
1968 zebra_unlock (zh->lock_normal);
1972 ZEBRA_RES zebra_clean(ZebraHandle zh)
1975 yaz_log(log_level, "zebra_clean");
1976 return zebra_commit_ex(zh, 1);
1979 ZEBRA_RES zebra_commit(ZebraHandle zh)
1982 yaz_log(log_level, "zebra_commit");
1983 return zebra_commit_ex(zh, 0);
1986 ZEBRA_RES zebra_init(ZebraHandle zh)
1991 yaz_log(log_level, "zebra_init");
1993 zebra_select_default_database(zh);
1996 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1997 "cannot select default database");
2000 rval = res_get (zh->res, "shadow");
2002 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2005 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2009 bf_cache (bfs, rval);
2013 zebra_set_state (zh, 'o', 0);
2017 ZEBRA_RES zebra_compact(ZebraHandle zh)
2021 yaz_log(log_level, "zebra_compact");
2024 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2027 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2033 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2036 yaz_log(log_level, "zebra_result");
2037 *code = zh->errCode;
2038 *addinfo = zh->errString;
2041 void zebra_shadow_enable(ZebraHandle zh, int value)
2044 yaz_log(log_level, "zebra_shadow_enable");
2045 zh->shadow_enable = value;
2048 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2052 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2054 if (zh->iconv_to_utf8 != 0)
2055 yaz_iconv_close(zh->iconv_to_utf8);
2056 if (zh->iconv_from_utf8 != 0)
2057 yaz_iconv_close(zh->iconv_from_utf8);
2060 yaz_iconv_open ("UTF-8", encoding);
2061 if (zh->iconv_to_utf8 == 0)
2062 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2063 zh->iconv_from_utf8 =
2064 yaz_iconv_open (encoding, "UTF-8");
2065 if (zh->iconv_to_utf8 == 0)
2066 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2071 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2074 yaz_log(log_level, "zebra_record_encoding");
2075 xfree(zh->record_encoding);
2076 zh->record_encoding = 0;
2078 zh->record_encoding = xstrdup (encoding);
2082 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2087 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2088 res_set(zh->res, name, value);
2091 const char *zebra_get_resource(ZebraHandle zh,
2092 const char *name, const char *defaultvalue)
2097 v = res_get_def (zh->res, name, (char *)defaultvalue);
2098 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2102 /* moved from zebra_api_ext.c by pop */
2103 /* FIXME: Should this really be public??? -Heikki */
2105 int zebra_trans_no (ZebraHandle zh)
2108 yaz_log(log_level, "zebra_trans_no");
2109 return zh->trans_no;
2112 int zebra_get_shadow_enable (ZebraHandle zh)
2115 yaz_log(log_level, "zebra_get_shadow_enable");
2116 return zh->shadow_enable;
2119 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2122 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2123 zh->shadow_enable = value;
2126 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2127 so that it's identicical to the original api_records_retrieve */
2128 void api_records_retrieve (ZebraHandle zh, ODR stream,
2129 const char *setname, Z_RecordComposition *comp,
2130 oid_value input_format, int num_recs,
2131 ZebraRetrievalRecord *recs)
2133 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2137 /* ---------------------------------------------------------------------------
2138 Record insert(=update), delete
2140 If sysno is provided, then it's used to identify the record.
2141 If not, and match_criteria is provided, then sysno is guessed
2142 If not, and a record is provided, then sysno is got from there
2143 NOTE: Now returns 0 at success and updates sysno, which is an int*
2147 int zebra_add_record(ZebraHandle zh,
2148 const char *buf, int buf_size)
2150 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2153 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2154 const char *recordType,
2155 SYSNO *sysno, const char *match,
2157 const char *buf, int buf_size, int force_update)
2163 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2166 buf_size = strlen(buf);
2168 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2170 res = buffer_extract_record (zh, buf, buf_size,
2171 0, /* delete_flag */
2177 0); /* allow_update */
2178 zebra_end_trans(zh);
2182 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2183 const char *recordType,
2184 SYSNO* sysno, const char *match,
2186 const char *buf, int buf_size,
2193 yaz_log(log_level, "zebra_update_record");
2195 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2197 if (buf_size < 1) buf_size = strlen(buf);
2199 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2201 res = buffer_extract_record (zh, buf, buf_size,
2202 0, /* delete_flag */
2208 1); /* allow_update */
2209 zebra_end_trans(zh);
2213 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2214 const char *recordType,
2215 SYSNO *sysno, const char *match,
2217 const char *buf, int buf_size,
2224 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2226 if (buf_size < 1) buf_size = strlen(buf);
2228 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2230 res = buffer_extract_record (zh, buf, buf_size,
2231 1, /* delete_flag */
2237 1); /* allow_update */
2238 zebra_end_trans(zh);
2242 /* ---------------------------------------------------------------------------
2246 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2247 const char *setname, zint *hits)
2250 ZEBRA_RES res = ZEBRA_OK;
2252 ODR odr = odr_createmem(ODR_ENCODE);
2257 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2259 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2263 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2264 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2268 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2272 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2280 /* ---------------------------------------------------------------------------
2281 Sort - a simplified interface, with optional read locks.
2283 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2284 const char *sort_spec,
2285 const char *output_setname,
2286 const char **input_setnames)
2288 int num_input_setnames = 0;
2289 int sort_status = 0;
2290 Z_SortKeySpecList *sort_sequence;
2294 assert(output_setname);
2295 assert(input_setnames);
2296 sort_sequence = yaz_sort_spec (stream, sort_spec);
2297 yaz_log(log_level, "sort (FIXME) ");
2300 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2301 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2305 /* we can do this, since the perl typemap code for char** will
2306 put a NULL at the end of list */
2307 while (input_setnames[num_input_setnames]) num_input_setnames++;
2309 if (zebra_begin_read (zh))
2312 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2313 output_setname, sort_sequence, &sort_status);
2319 /* ---------------------------------------------------------------------------
2320 Get BFS for Zebra system (to make alternative storage methods)
2322 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2325 return zh->reg->bfs;
2330 /* ---------------------------------------------------------------------------
2331 Set limit for search/scan
2333 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2336 zebra_limit_destroy(zh->m_limit);
2337 zh->m_limit = zebra_limit_create(complement_flag, ids);
2342 Set Error code + addinfo
2344 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2347 nmem_reset(zh->nmem_error);
2348 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2351 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2354 sprintf(vstr, ZINT_FORMAT, i);
2357 nmem_reset(zh->nmem_error);
2358 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2361 void zebra_lock_prefix (Res res, char *path)
2363 const char *lock_dir = res_get_def (res, "lockDir", "");
2365 strcpy (path, lock_dir);
2366 if (*path && path[strlen(path)-1] != '/')