1 /* $Id: zebraapi.c,v 1.206 2006-03-26 14:17:01 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;
283 ZEBRA_RES ret = ZEBRA_OK;
287 reg = xmalloc(sizeof(*reg));
290 reg->name = xstrdup (name);
297 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
298 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
300 reg->dh = data1_createx (DATA1_FLAG_XML);
307 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
310 data1_destroy(reg->dh);
317 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
319 bfs_destroy(reg->bfs);
320 data1_destroy(reg->dh);
327 getcwd(cwd, sizeof(cwd)-1);
328 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
329 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
331 data1_set_tabpath (reg->dh, profilePath);
332 data1_set_tabroot (reg->dh, reg_path);
333 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
335 if ((reg->zebra_maps = zebra_maps_open (res, reg_path)) == 0)
337 recTypes_destroy(reg->recTypes);
338 bfs_destroy(reg->bfs);
339 data1_destroy(reg->dh);
345 reg->rank_classes = NULL;
349 reg->keys = zebra_rec_keys_open();
352 reg->sortKeys = zebra_rec_keys_open();
354 reg->sortKeys.buf = 0;
355 reg->sortKeys.buf_max = 0;
366 reg->key_file_no = 0;
369 zebraRankInstall (reg, rank_1_class);
370 zebraRankInstall (reg, rank_zv_class);
371 zebraRankInstall (reg, rank_static_class);
373 recordCompression = res_get_def (res, "recordCompression", "none");
374 if (!strcmp (recordCompression, "none"))
375 record_compression = REC_COMPRESS_NONE;
376 if (!strcmp (recordCompression, "bzip2"))
377 record_compression = REC_COMPRESS_BZIP2;
379 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
381 yaz_log (YLOG_WARN, "rec_open failed");
386 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
388 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
390 yaz_log (YLOG_WARN, "dict_open failed");
393 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
395 yaz_log (YLOG_WARN, "sortIdx_open failed");
398 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
400 struct ISAMS_M_s isams_m;
401 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
402 key_isams_m(res, &isams_m))))
404 yaz_log (YLOG_WARN, "isams_open failed");
408 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
410 struct ISAMC_M_s isamc_m;
411 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
412 rw, key_isamc_m(res, &isamc_m))))
414 yaz_log (YLOG_WARN, "isamc_open failed");
418 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
420 struct ISAMC_M_s isamc_m;
422 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
423 rw, key_isamc_m(res, &isamc_m), 0)))
425 yaz_log (YLOG_WARN, "isamb_open failed");
429 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
431 struct ISAMC_M_s isamc_m;
433 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
434 rw, key_isamc_m(res, &isamc_m), 1)))
436 yaz_log (YLOG_WARN, "isamb_open failed");
440 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
442 struct ISAMC_M_s isamc_m;
444 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
445 rw, key_isamc_m(res, &isamc_m), -1)))
447 yaz_log (YLOG_WARN, "isamb_open failed");
453 reg->zei = zebraExplain_open(reg->records, reg->dh,
458 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
465 zebra_register_close(zs, reg);
468 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
472 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
475 yaz_log(log_level, "zebra_admin_shutdown");
477 zebra_mutex_cond_lock (&zh->service->session_lock);
478 zh->service->stop_flag = 1;
479 zebra_mutex_cond_unlock (&zh->service->session_lock);
483 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
487 yaz_log(log_level, "zebra_admin_start");
489 zebra_mutex_cond_lock (&zs->session_lock);
490 zebra_mutex_cond_unlock (&zs->session_lock);
494 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
498 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
502 zebraExplain_close (reg->zei);
503 dict_close (reg->dict);
505 dict_close (reg->matchDict);
506 sortIdx_close (reg->sortIdx);
508 isams_close (reg->isams);
510 isamc_close (reg->isamc);
512 isamb_close (reg->isamb);
513 rec_close (®->records);
515 recTypes_destroy (reg->recTypes);
516 zebra_maps_close (reg->zebra_maps);
517 zebraRankDestroy (reg);
518 bfs_destroy (reg->bfs);
519 data1_destroy (reg->dh);
521 zebra_rec_keys_close(reg->keys);
523 zebra_rec_keys_close(reg->sortKeys);
525 xfree(reg->sortKeys.buf);
533 ZEBRA_RES zebra_stop(ZebraService zs)
537 yaz_log (log_level, "zebra_stop");
541 zebra_close (zs->sessions);
544 zebra_mutex_cond_destroy (&zs->session_lock);
547 passwd_db_close (zs->passwd_db);
549 recTypeClass_destroy(zs->record_classes);
550 nmem_destroy(zs->nmem);
551 res_close (zs->global_res);
556 ZEBRA_RES zebra_close (ZebraHandle zh)
559 struct zebra_session **sp;
562 yaz_log(log_level, "zebra_close");
569 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
570 resultSetDestroy (zh, -1, 0, 0);
573 zebra_register_close(zh->service, zh->reg);
574 zebra_close_res (zh);
575 res_close(zh->session_res);
577 xfree(zh->record_encoding);
579 xfree(zh->dbaccesslist);
581 for (i = 0; i < zh->num_basenames; i++)
582 xfree(zh->basenames[i]);
583 xfree(zh->basenames);
585 if (zh->iconv_to_utf8 != 0)
586 yaz_iconv_close (zh->iconv_to_utf8);
587 if (zh->iconv_from_utf8 != 0)
588 yaz_iconv_close (zh->iconv_from_utf8);
590 zebra_mutex_cond_lock (&zs->session_lock);
591 zebra_lock_destroy (zh->lock_normal);
592 zebra_lock_destroy (zh->lock_shadow);
604 zebra_mutex_cond_unlock (&zs->session_lock);
606 xfree(zh->user_perm);
607 zh->service = 0; /* more likely to trigger an assert */
609 zebra_limit_destroy(zh->m_limit);
611 nmem_destroy(zh->nmem_error);
618 struct map_baseinfo {
624 char **new_basenames;
628 static void zebra_open_res(ZebraHandle zh)
636 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
637 zh->res = res_open(zh->session_res, 0);
638 res_read_file(zh->res, fname);
640 else if (*zh->reg_name == 0)
642 zh->res = res_open(zh->session_res, 0);
646 yaz_log (YLOG_WARN, "no register root specified");
647 zh->res = 0; /* no path for register - fail! */
651 static void zebra_close_res (ZebraHandle zh)
659 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
663 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
667 assert (zh->reg == 0);
668 assert (*zh->reg_name == 0);
674 resultSetInvalidate (zh);
675 zebra_register_close(zh->service, zh->reg);
681 zh->reg_name = xstrdup (new_reg);
685 if (zh->service->path_root)
687 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
688 strlen(zh->reg_name) + 3);
689 strcpy (zh->path_reg, zh->service->path_root);
692 strcat (zh->path_reg, "/");
693 strcat (zh->path_reg, zh->reg_name);
699 zebra_lock_destroy (zh->lock_normal);
703 zebra_lock_destroy (zh->lock_shadow);
709 const char *lock_area = res_get (zh->res, "lockDir");
711 if (!lock_area && zh->path_reg)
712 res_set (zh->res, "lockDir", zh->path_reg);
713 sprintf (fname, "norm.%s.LCK", zh->reg_name);
715 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
717 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
719 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
721 if (!zh->lock_normal || !zh->lock_shadow)
725 zebra_lock_destroy(zh->lock_normal);
730 zebra_lock_destroy(zh->lock_shadow);
739 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
740 zebra_set_approx_limit(zh, approx);
744 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
745 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
747 yaz_log(YLOG_LOG, "static rank unset");
751 void map_basenames_func (void *vp, const char *name, const char *value)
753 struct map_baseinfo *p = (struct map_baseinfo *) vp;
755 char fromdb[128], todb[8][128];
762 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
763 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
764 todb[5], todb[6], todb[7]);
768 for (i = 0; i<p->num_bases; i++)
769 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
772 for (i = 0; i < no; i++)
774 if (p->new_num_bases == p->new_num_max)
776 p->new_basenames[(p->new_num_bases)++] =
777 nmem_strdup (p->mem, todb[i]);
783 int zebra_select_default_database(ZebraHandle zh)
787 /* no database has been selected - so we select based on
788 resource setting (including group)
790 const char *group = res_get(zh->session_res, "group");
791 const char *v = res_get_prefix(zh->session_res,
792 "database", group, "Default");
793 return zebra_select_database(zh, v);
798 void map_basenames (ZebraHandle zh, ODR stream,
799 int *num_bases, char ***basenames)
801 struct map_baseinfo info;
802 struct map_baseinfo *p = &info;
805 yaz_log(log_level, "map_basenames ");
810 info.num_bases = *num_bases;
811 info.basenames = *basenames;
812 info.new_num_max = 128;
813 info.new_num_bases = 0;
814 info.new_basenames = (char **)
815 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
816 info.mem = stream->mem;
818 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
820 for (i = 0; i<p->num_bases; i++)
821 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
823 p->new_basenames[(p->new_num_bases)++] =
824 nmem_strdup (p->mem, p->basenames[i]);
826 *num_bases = info.new_num_bases;
827 *basenames = info.new_basenames;
828 for (i = 0; i<*num_bases; i++)
829 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
832 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
835 yaz_log(log_level, "zebra_select_database %s",basename);
837 return zebra_select_databases (zh, 1, &basename);
840 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
841 const char **basenames)
850 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
851 num_bases,basenames[0]);
856 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
860 /* Check if the user has access to all databases (Seb) */
861 /* You could argue that this should happen later, after we have
862 * determined that the database(s) exist. */
863 if (zh->dbaccesslist) {
864 for (i = 0; i < num_bases; i++) {
865 const char *db = basenames[i];
867 for (p = zh->dbaccesslist; p && *p; p = pp) {
869 if ((pp = strchr(p, '+'))) {
875 if (len == strlen(db) && !strncmp(db, p, len))
879 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
885 for (i = 0; i < zh->num_basenames; i++)
886 xfree(zh->basenames[i]);
887 xfree(zh->basenames);
889 zh->num_basenames = num_bases;
890 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
891 for (i = 0; i < zh->num_basenames; i++)
892 zh->basenames[i] = xstrdup (basenames[i]);
894 cp = strrchr(basenames[0], '/');
897 len = cp - basenames[0];
898 new_reg = xmalloc(len + 1);
899 memcpy (new_reg, basenames[0], len);
903 new_reg = xstrdup ("");
904 for (i = 1; i<num_bases; i++)
908 cp1 = strrchr (basenames[i], '/');
913 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
916 if (len != cp1 - basenames[i] ||
917 memcmp (basenames[i], new_reg, len))
919 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
927 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
932 zebra_select_register (zh, new_reg);
936 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
939 if (!zh->lock_normal || !zh->lock_shadow)
941 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
947 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
949 if (approx_limit == 0)
950 approx_limit = DEFAULT_APPROX_LIMIT;
951 zh->approx_limit = approx_limit;
955 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
956 const char *setname, zint *hits)
964 yaz_log(log_level, "zebra_search_rpn");
968 if (zebra_begin_read(zh) == ZEBRA_FAIL)
971 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
972 zh->num_basenames, zh->basenames, setname);
978 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
980 Z_RecordComposition *comp,
981 oid_value input_format, int num_recs,
982 ZebraRetrievalRecord *recs)
984 ZebraMetaRecord *poset;
986 ZEBRA_RES ret = ZEBRA_OK;
994 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
998 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1003 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1006 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1007 for (i = 0; i<num_recs; i++)
1008 pos_array[i] = recs[i].position;
1009 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1012 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1013 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1019 for (i = 0; i<num_recs; i++)
1023 recs[i].errCode = 0;
1024 recs[i].format = VAL_SUTRS;
1025 recs[i].len = strlen(poset[i].term);
1026 recs[i].buf = poset[i].term;
1027 recs[i].base = poset[i].db;
1029 else if (poset[i].sysno)
1033 zebra_snippets *hit_snippet = zebra_snippets_create();
1035 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1039 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1041 stream, input_format, comp,
1042 &recs[i].format, &buf, &len,
1043 &recs[i].base, &recs[i].errString);
1048 recs[i].buf = (char*) odr_malloc(stream, len);
1049 memcpy(recs[i].buf, buf, len);
1053 recs[i].score = poset[i].score;
1054 recs[i].sysno = poset[i].sysno;
1055 zebra_snippets_destroy(hit_snippet);
1059 /* only need to set it once */
1060 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1062 zebra_setError_zint(zh,
1063 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1068 recs[i].buf = 0; /* no record and no error issued */
1070 recs[i].errCode = 0;
1071 recs[i].format = VAL_NONE;
1075 zebra_meta_records_destroy(zh, poset, num_recs);
1077 zebra_end_read (zh);
1082 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1084 int *num_entries, ZebraScanEntry **entries,
1086 const char *setname)
1088 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1089 Z_AttributesPlusTerm *zapt;
1093 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1096 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1099 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1100 position, num_entries, entries, is_partial,
1102 yaz_pqf_destroy (pqf_parser);
1106 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1107 oid_value attributeset,
1109 int *num_entries, ZebraScanEntry **entries,
1111 const char *setname)
1114 RSET limit_rset = 0;
1119 assert(num_entries);
1122 yaz_log(log_level, "zebra_scan");
1124 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1132 limit_rset = resultSetRef(zh, setname);
1136 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1138 zebra_end_read (zh);
1142 res = rpn_scan (zh, stream, zapt, attributeset,
1143 zh->num_basenames, zh->basenames, position,
1144 num_entries, entries, is_partial, limit_rset, 0);
1145 zebra_end_read (zh);
1149 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1150 int num_input_setnames, const char **input_setnames,
1151 const char *output_setname,
1152 Z_SortKeySpecList *sort_sequence,
1158 assert(num_input_setnames>0);
1159 assert(input_setnames);
1160 assert(sort_sequence);
1161 assert(sort_status);
1162 yaz_log(log_level, "zebra_sort");
1164 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1166 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1167 output_setname, sort_sequence, sort_status);
1172 int zebra_deleteResultSet(ZebraHandle zh, int function,
1173 int num_setnames, char **setnames,
1179 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1181 if (zebra_begin_read(zh))
1182 return Z_DeleteStatus_systemProblemAtTarget;
1185 case Z_DeleteResultSetRequest_list:
1186 assert(num_setnames>0);
1188 resultSetDestroy (zh, num_setnames, setnames, statuses);
1190 case Z_DeleteResultSetRequest_all:
1191 resultSetDestroy (zh, -1, 0, statuses);
1194 zebra_end_read (zh);
1195 status = Z_DeleteStatus_success;
1196 for (i = 0; i<num_setnames; i++)
1197 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1198 status = statuses[i];
1202 int zebra_errCode (ZebraHandle zh)
1206 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1209 yaz_log(log_level, "zebra_errCode: o");
1213 const char *zebra_errString (ZebraHandle zh)
1217 e= diagbib1_str (zh->errCode);
1218 yaz_log(log_level, "zebra_errString: %s",e);
1222 char *zebra_errAdd (ZebraHandle zh)
1227 yaz_log(log_level, "zebra_errAdd: %s",a);
1231 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1234 const char *astring;
1242 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1243 p = res_get(zs->global_res, u);
1244 xfree(zh->user_perm);
1245 zh->user_perm = xstrdup(p ? p : "r");
1247 /* Determine database access list */
1248 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1250 zh->dbaccesslist = xstrdup(astring);
1252 zh->dbaccesslist = 0;
1254 /* users that don't require a password .. */
1255 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1258 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1263 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1264 const char *record_type)
1267 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1268 database, record_type);
1269 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1271 return zebra_begin_trans(zh, 1);
1274 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1277 yaz_log(log_level, "zebra_admin_import_end");
1278 return zebra_end_trans(zh);
1281 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1283 ZEBRA_RES res = ZEBRA_OK;
1287 yaz_log(log_level, "zebra_admin_import_segment");
1289 for (i = 0; i<segment->num_segmentRecords; i++)
1291 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1293 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1295 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1296 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1298 Odr_oct *oct = fragment->u.notExternallyTagged;
1301 if (zebra_update_record(zh,
1302 0, /* record Type */
1306 (const char *) oct->buf, oct->len,
1315 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1316 const char *rec_buf,
1318 const char *recid_buf, size_t recid_len,
1320 /* 1 = insert. Fail it already exists */
1321 /* 2 = replace. Fail it does not exist */
1322 /* 3 = delete. Fail if does not exist */
1323 /* 4 = update. Insert/replace */
1331 assert(action>0 && action <=4);
1334 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1336 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1338 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1339 "no record ID or empty record ID");
1343 memcpy (recid_z, recid_buf, recid_len);
1344 recid_z[recid_len] = 0;
1346 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1349 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1350 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1353 if (action == 1) /* fail if insert */
1355 zebra_end_trans(zh);
1356 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1357 "Cannot insert record: already exist");
1361 memcpy (&sysno, rinfo+1, sizeof(sysno));
1365 if (action == 2 || action == 3) /* fail if delete or update */
1367 zebra_end_trans(zh);
1368 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1369 "Cannot delete/update record: does not exist");
1372 action = 1; /* make it an insert (if it's an update).. */
1374 res = buffer_extract_record (zh, rec_buf, rec_len,
1375 action == 3 ? 1 : 0 /* delete flag */,
1381 0, /* force update */
1382 1 /* allow update */
1384 if (res == ZEBRA_FAIL)
1386 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1387 "Unable to parse record");
1391 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1392 sizeof(sysno), &sysno);
1394 else if (action == 3)
1396 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1398 zebra_end_trans(zh);
1402 int delete_w_handle(const char *info, void *handle)
1404 ZebraHandle zh = (ZebraHandle) handle;
1408 if (*info == sizeof(pos))
1410 memcpy (&pos, info+1, sizeof(pos));
1411 isamb_unlink(zh->reg->isamb, pos);
1416 static int delete_SU_handle(void *handle, int ord)
1418 ZebraHandle zh = (ZebraHandle) handle;
1422 ord_len = key_SU_encode (ord, ord_buf);
1423 ord_buf[ord_len] = '\0';
1425 assert (zh->reg->isamb);
1426 dict_delete_subtree(zh->reg->dict, ord_buf,
1427 zh, delete_w_handle);
1431 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1433 ZEBRA_RES ret = ZEBRA_OK;
1435 yaz_log(log_level, "zebra_drop_database %s", db);
1437 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1439 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1444 zebraExplain_curDatabase (zh->reg->zei, db);
1445 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1446 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1447 0 /* handle */, 0 /* func */);
1448 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1449 zebraExplain_removeDatabase(zh->reg->zei, zh);
1453 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1454 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1455 "drop database only supported for isam:b");
1458 zebra_end_trans (zh);
1462 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1465 yaz_log(log_level, "zebra_create_database %s", db);
1468 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1470 if (zebra_begin_trans (zh, 1))
1473 /* announce database */
1474 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1475 /* explainDatabase */))
1477 zebra_end_trans (zh);
1478 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1481 return zebra_end_trans (zh);
1484 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1485 const char *input_str, int input_len,
1486 char *output_str, int output_len)
1492 yaz_log(log_level, "zebra_string_norm ");
1494 if (!zh->reg->zebra_maps)
1496 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1497 input_str, input_len);
1500 if (wrbuf_len(wrbuf) >= output_len)
1502 if (wrbuf_len(wrbuf))
1503 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1504 output_str[wrbuf_len(wrbuf)] = '\0';
1505 return wrbuf_len(wrbuf);
1508 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1510 char state_fname[256];
1515 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1517 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1518 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1519 f = fopen (fname, "w");
1521 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1522 fprintf (f, "%c %d %ld\n", val, seqno, p);
1527 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1529 char state_fname[256];
1534 yaz_log(log_level, "zebra_get_state ");
1536 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1537 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1538 f = fopen (fname, "r");
1544 fscanf (f, "%c %d", val, seqno);
1550 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1552 return zebra_begin_trans(zh, 0);
1555 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1557 return zebra_end_trans(zh);
1560 static void read_res_for_transaction(ZebraHandle zh)
1562 const char *group = res_get(zh->res, "group");
1564 /* FIXME - do we still use groups ?? */
1566 zh->m_group = group;
1567 v = res_get_prefix(zh->res, "followLinks", group, "1");
1568 zh->m_follow_links = atoi(v);
1570 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1571 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1573 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1574 zh->m_store_keys = atoi(v);
1576 v = res_get_prefix(zh->res, "storeData", group, "1");
1577 zh->m_store_data = atoi(v);
1579 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1580 zh->m_explain_database = atoi(v);
1582 v = res_get_prefix(zh->res, "openRW", group, "1");
1583 zh->m_flag_rw = atoi(v);
1585 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1586 zh->m_file_verbose_limit = atoi(v);
1589 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1592 zebra_select_default_database(zh);
1595 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1596 "zebra_begin_trans: no database selected");
1600 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1604 if (rw && !strchr(zh->user_perm, 'w'))
1608 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1620 const char *rval = 0;
1625 read_res_for_transaction(zh);
1628 if (zh->trans_no != 1)
1630 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1631 "zebra_begin_trans: no write trans within read");
1636 resultSetInvalidate (zh);
1637 zebra_register_close(zh->service, zh->reg);
1639 zh->trans_w_no = zh->trans_no;
1641 zh->records_inserted = 0;
1642 zh->records_updated = 0;
1643 zh->records_deleted = 0;
1644 zh->records_processed = 0;
1646 #if HAVE_SYS_TIMES_H
1650 if (zh->shadow_enable)
1651 rval = res_get (zh->res, "shadow");
1653 for (pass = 0; pass < 2; pass++)
1657 zebra_lock_r (zh->lock_normal);
1658 zebra_lock_w (zh->lock_shadow);
1662 zebra_lock_w (zh->lock_normal);
1663 zebra_lock_w (zh->lock_shadow);
1666 zebra_get_state (zh, &val, &seqno);
1669 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1670 zebra_unlock (zh->lock_shadow);
1671 zebra_unlock (zh->lock_normal);
1675 else if (val == 'd')
1679 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1681 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1682 bf_commitClean (bfs, rval);
1687 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1694 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1698 zebra_set_state (zh, 'd', seqno);
1700 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1701 1, rval ? 1 : 0, zh->res,
1704 zh->reg->seqno = seqno;
1707 zebra_set_state (zh, 'o', seqno);
1709 zebra_unlock (zh->lock_shadow);
1710 zebra_unlock (zh->lock_normal);
1715 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1716 "zebra_begin_trans: cannot open register");
1717 yaz_log(YLOG_FATAL, "%s", zh->errString);
1720 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1730 if (zh->trans_no != 1)
1732 zebra_flush_reg (zh);
1735 #if HAVE_SYS_TIMES_H
1741 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1744 if (!zh->lock_normal || !zh->lock_shadow)
1747 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1750 zebra_get_state (zh, &val, &seqno);
1756 else if (seqno != zh->reg->seqno)
1758 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1759 seqno, zh->reg->seqno);
1762 else if (zh->reg->last_val != val)
1764 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1765 val, zh->reg->last_val);
1772 zebra_lock_r (zh->lock_shadow);
1774 zebra_lock_r (zh->lock_normal);
1778 resultSetInvalidate (zh);
1779 zebra_register_close(zh->service, zh->reg);
1781 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1782 0, val == 'c' ? 1 : 0,
1783 zh->res, zh->path_reg);
1786 zebra_unlock (zh->lock_normal);
1787 zebra_unlock (zh->lock_shadow);
1789 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1792 zh->reg->last_val = val;
1793 zh->reg->seqno = seqno;
1795 read_res_for_transaction(zh);
1799 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1801 ZebraTransactionStatus dummy;
1803 yaz_log(log_level, "zebra_end_trans");
1804 return zebra_end_transaction(zh, &dummy);
1807 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1815 yaz_log(log_level, "zebra_end_transaction");
1817 status->processed = 0;
1818 status->inserted = 0;
1819 status->updated = 0;
1820 status->deleted = 0;
1824 if (!zh->res || !zh->reg)
1826 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1827 "zebra_end_trans: no open transaction");
1830 if (zh->trans_no != zh->trans_w_no)
1833 if (zh->trans_no != 0)
1836 /* release read lock */
1838 zebra_unlock (zh->lock_normal);
1839 zebra_unlock (zh->lock_shadow);
1842 { /* release write lock */
1846 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1847 rval = res_get (zh->res, "shadow");
1849 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1851 zebra_flush_reg (zh);
1853 resultSetInvalidate (zh);
1855 zebra_register_close(zh->service, zh->reg);
1858 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1859 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1860 zh->records_processed, zh->records_inserted,
1861 zh->records_updated, zh->records_deleted);
1863 status->processed = (int) zh->records_processed;
1864 status->inserted = (int) zh->records_inserted;
1865 status->updated = (int) zh->records_updated;
1866 status->deleted = (int) zh->records_deleted;
1868 zebra_get_state (zh, &val, &seqno);
1871 BFiles bfs = bfs_create (rval, zh->path_reg);
1872 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1873 bf_commitClean (bfs, rval);
1878 zebra_set_state (zh, 'o', seqno);
1880 zebra_unlock (zh->lock_shadow);
1881 zebra_unlock (zh->lock_normal);
1884 #if HAVE_SYS_TIMES_H
1886 yaz_log (log_level, "user/system: %ld/%ld",
1887 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1888 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1890 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1891 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1896 int zebra_repository_update (ZebraHandle zh, const char *path)
1900 yaz_log (log_level, "updating %s", path);
1901 repositoryUpdate (zh, path);
1905 int zebra_repository_delete (ZebraHandle zh, const char *path)
1909 yaz_log (log_level, "deleting %s", path);
1910 repositoryDelete (zh, path);
1914 int zebra_repository_show (ZebraHandle zh, const char *path)
1918 yaz_log(log_level, "zebra_repository_show");
1919 repositoryShow (zh, path);
1923 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1931 zebra_select_default_database(zh);
1934 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1937 rval = res_get (zh->res, "shadow");
1940 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1944 zebra_lock_w (zh->lock_normal);
1945 zebra_lock_r (zh->lock_shadow);
1947 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1949 zebra_get_state (zh, &val, &seqno);
1952 bf_cache (bfs, rval);
1953 if (bf_commitExists (bfs))
1956 zebra_set_state (zh, 'd', seqno);
1959 zebra_set_state (zh, 'c', seqno);
1961 yaz_log (YLOG_DEBUG, "commit start");
1962 bf_commitExec (bfs);
1967 yaz_log (YLOG_DEBUG, "commit clean");
1968 bf_commitClean (bfs, rval);
1970 zebra_set_state (zh, 'o', seqno);
1974 yaz_log (log_level, "nothing to commit");
1978 zebra_unlock (zh->lock_shadow);
1979 zebra_unlock (zh->lock_normal);
1983 ZEBRA_RES zebra_clean(ZebraHandle zh)
1986 yaz_log(log_level, "zebra_clean");
1987 return zebra_commit_ex(zh, 1);
1990 ZEBRA_RES zebra_commit(ZebraHandle zh)
1993 yaz_log(log_level, "zebra_commit");
1994 return zebra_commit_ex(zh, 0);
1997 ZEBRA_RES zebra_init(ZebraHandle zh)
2002 yaz_log(log_level, "zebra_init");
2004 zebra_select_default_database(zh);
2007 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2008 "cannot select default database");
2011 rval = res_get (zh->res, "shadow");
2013 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2016 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2020 bf_cache (bfs, rval);
2024 zebra_set_state (zh, 'o', 0);
2028 ZEBRA_RES zebra_compact(ZebraHandle zh)
2032 yaz_log(log_level, "zebra_compact");
2035 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2038 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2044 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2047 yaz_log(log_level, "zebra_result");
2048 *code = zh->errCode;
2049 *addinfo = zh->errString;
2052 void zebra_shadow_enable(ZebraHandle zh, int value)
2055 yaz_log(log_level, "zebra_shadow_enable");
2056 zh->shadow_enable = value;
2059 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2063 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2065 if (zh->iconv_to_utf8 != 0)
2066 yaz_iconv_close(zh->iconv_to_utf8);
2067 if (zh->iconv_from_utf8 != 0)
2068 yaz_iconv_close(zh->iconv_from_utf8);
2071 yaz_iconv_open ("UTF-8", encoding);
2072 if (zh->iconv_to_utf8 == 0)
2073 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2074 zh->iconv_from_utf8 =
2075 yaz_iconv_open (encoding, "UTF-8");
2076 if (zh->iconv_to_utf8 == 0)
2077 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2082 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2085 yaz_log(log_level, "zebra_record_encoding");
2086 xfree(zh->record_encoding);
2087 zh->record_encoding = 0;
2089 zh->record_encoding = xstrdup (encoding);
2093 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2098 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2099 res_set(zh->res, name, value);
2102 const char *zebra_get_resource(ZebraHandle zh,
2103 const char *name, const char *defaultvalue)
2108 v = res_get_def (zh->res, name, (char *)defaultvalue);
2109 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2113 /* moved from zebra_api_ext.c by pop */
2114 /* FIXME: Should this really be public??? -Heikki */
2116 int zebra_trans_no (ZebraHandle zh)
2119 yaz_log(log_level, "zebra_trans_no");
2120 return zh->trans_no;
2123 int zebra_get_shadow_enable (ZebraHandle zh)
2126 yaz_log(log_level, "zebra_get_shadow_enable");
2127 return zh->shadow_enable;
2130 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2133 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2134 zh->shadow_enable = value;
2137 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2138 so that it's identicical to the original api_records_retrieve */
2139 void api_records_retrieve (ZebraHandle zh, ODR stream,
2140 const char *setname, Z_RecordComposition *comp,
2141 oid_value input_format, int num_recs,
2142 ZebraRetrievalRecord *recs)
2144 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2148 /* ---------------------------------------------------------------------------
2149 Record insert(=update), delete
2151 If sysno is provided, then it's used to identify the record.
2152 If not, and match_criteria is provided, then sysno is guessed
2153 If not, and a record is provided, then sysno is got from there
2154 NOTE: Now returns 0 at success and updates sysno, which is an int*
2158 int zebra_add_record(ZebraHandle zh,
2159 const char *buf, int buf_size)
2161 return zebra_update_record(zh, 0, 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,
2204 yaz_log(log_level, "zebra_update_record");
2206 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2208 if (buf_size < 1) buf_size = strlen(buf);
2210 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2212 res = buffer_extract_record (zh, buf, buf_size,
2213 0, /* delete_flag */
2219 1); /* allow_update */
2220 zebra_end_trans(zh);
2224 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2225 const char *recordType,
2226 SYSNO *sysno, const char *match,
2228 const char *buf, int buf_size,
2235 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2237 if (buf_size < 1) buf_size = strlen(buf);
2239 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2241 res = buffer_extract_record (zh, buf, buf_size,
2242 1, /* delete_flag */
2248 1); /* allow_update */
2249 zebra_end_trans(zh);
2253 /* ---------------------------------------------------------------------------
2257 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2258 const char *setname, zint *hits)
2261 ZEBRA_RES res = ZEBRA_OK;
2263 ODR odr = odr_createmem(ODR_ENCODE);
2268 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2270 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2274 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2275 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2279 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2283 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2291 /* ---------------------------------------------------------------------------
2292 Sort - a simplified interface, with optional read locks.
2294 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2295 const char *sort_spec,
2296 const char *output_setname,
2297 const char **input_setnames)
2299 int num_input_setnames = 0;
2300 int sort_status = 0;
2301 Z_SortKeySpecList *sort_sequence;
2305 assert(output_setname);
2306 assert(input_setnames);
2307 sort_sequence = yaz_sort_spec (stream, sort_spec);
2308 yaz_log(log_level, "sort (FIXME) ");
2311 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2312 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2316 /* we can do this, since the perl typemap code for char** will
2317 put a NULL at the end of list */
2318 while (input_setnames[num_input_setnames]) num_input_setnames++;
2320 if (zebra_begin_read (zh))
2323 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2324 output_setname, sort_sequence, &sort_status);
2330 /* ---------------------------------------------------------------------------
2331 Get BFS for Zebra system (to make alternative storage methods)
2333 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2336 return zh->reg->bfs;
2341 /* ---------------------------------------------------------------------------
2342 Set limit for search/scan
2344 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2347 zebra_limit_destroy(zh->m_limit);
2348 zh->m_limit = zebra_limit_create(complement_flag, ids);
2353 Set Error code + addinfo
2355 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2358 nmem_reset(zh->nmem_error);
2359 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2362 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2365 sprintf(vstr, ZINT_FORMAT, i);
2368 nmem_reset(zh->nmem_error);
2369 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2372 void zebra_lock_prefix (Res res, char *path)
2374 const char *lock_dir = res_get_def (res, "lockDir", "");
2376 strcpy (path, lock_dir);
2377 if (*path && path[strlen(path)-1] != '/')