1 /* $Id: zebraapi.c,v 1.208 2006-03-29 10:43:24 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_create();
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);
336 zebra_maps_open(res, reg_path, profilePath);
337 if (!reg->zebra_maps)
339 recTypes_destroy(reg->recTypes);
340 bfs_destroy(reg->bfs);
341 data1_destroy(reg->dh);
346 reg->rank_classes = NULL;
350 reg->keys = zebra_rec_keys_open();
353 reg->sortKeys = zebra_rec_keys_open();
355 reg->sortKeys.buf = 0;
356 reg->sortKeys.buf_max = 0;
367 reg->key_file_no = 0;
370 zebraRankInstall (reg, rank_1_class);
371 zebraRankInstall (reg, rank_zv_class);
372 zebraRankInstall (reg, rank_static_class);
374 recordCompression = res_get_def (res, "recordCompression", "none");
375 if (!strcmp (recordCompression, "none"))
376 record_compression = REC_COMPRESS_NONE;
377 if (!strcmp (recordCompression, "bzip2"))
378 record_compression = REC_COMPRESS_BZIP2;
382 const char *index_fname = res_get_def(res, "index", "default.idx");
383 if (index_fname && *index_fname)
385 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
390 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
392 yaz_log (YLOG_WARN, "rec_open failed");
397 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
399 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
401 yaz_log (YLOG_WARN, "dict_open failed");
404 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
406 yaz_log (YLOG_WARN, "sortIdx_open failed");
409 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
411 struct ISAMS_M_s isams_m;
412 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
413 key_isams_m(res, &isams_m))))
415 yaz_log (YLOG_WARN, "isams_open failed");
419 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
421 struct ISAMC_M_s isamc_m;
422 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
423 rw, key_isamc_m(res, &isamc_m))))
425 yaz_log (YLOG_WARN, "isamc_open failed");
429 if (res_get_match (res, "isam", "b", 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), 0)))
436 yaz_log (YLOG_WARN, "isamb_open failed");
440 if (res_get_match (res, "isam", "bc", 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");
451 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
453 struct ISAMC_M_s isamc_m;
455 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
456 rw, key_isamc_m(res, &isamc_m), -1)))
458 yaz_log (YLOG_WARN, "isamb_open failed");
464 reg->zei = zebraExplain_open(reg->records, reg->dh,
469 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
476 zebra_register_close(zs, reg);
479 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
483 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
486 yaz_log(log_level, "zebra_admin_shutdown");
488 zebra_mutex_cond_lock (&zh->service->session_lock);
489 zh->service->stop_flag = 1;
490 zebra_mutex_cond_unlock (&zh->service->session_lock);
494 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
498 yaz_log(log_level, "zebra_admin_start");
500 zebra_mutex_cond_lock (&zs->session_lock);
501 zebra_mutex_cond_unlock (&zs->session_lock);
505 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
509 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
513 zebraExplain_close (reg->zei);
514 dict_close (reg->dict);
516 dict_close (reg->matchDict);
517 sortIdx_close (reg->sortIdx);
519 isams_close (reg->isams);
521 isamc_close (reg->isamc);
523 isamb_close (reg->isamb);
524 rec_close (®->records);
526 recTypes_destroy (reg->recTypes);
527 zebra_maps_close (reg->zebra_maps);
528 zebraRankDestroy (reg);
529 bfs_destroy (reg->bfs);
530 data1_destroy (reg->dh);
532 zebra_rec_keys_close(reg->keys);
534 zebra_rec_keys_close(reg->sortKeys);
536 xfree(reg->sortKeys.buf);
544 ZEBRA_RES zebra_stop(ZebraService zs)
548 yaz_log (log_level, "zebra_stop");
552 zebra_close (zs->sessions);
555 zebra_mutex_cond_destroy (&zs->session_lock);
558 passwd_db_close (zs->passwd_db);
560 recTypeClass_destroy(zs->record_classes);
561 nmem_destroy(zs->nmem);
562 res_close (zs->global_res);
567 ZEBRA_RES zebra_close (ZebraHandle zh)
570 struct zebra_session **sp;
573 yaz_log(log_level, "zebra_close");
580 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
581 resultSetDestroy (zh, -1, 0, 0);
584 zebra_register_close(zh->service, zh->reg);
585 zebra_close_res (zh);
586 res_close(zh->session_res);
588 xfree(zh->record_encoding);
590 xfree(zh->dbaccesslist);
592 for (i = 0; i < zh->num_basenames; i++)
593 xfree(zh->basenames[i]);
594 xfree(zh->basenames);
596 if (zh->iconv_to_utf8 != 0)
597 yaz_iconv_close (zh->iconv_to_utf8);
598 if (zh->iconv_from_utf8 != 0)
599 yaz_iconv_close (zh->iconv_from_utf8);
601 zebra_mutex_cond_lock (&zs->session_lock);
602 zebra_lock_destroy (zh->lock_normal);
603 zebra_lock_destroy (zh->lock_shadow);
615 zebra_mutex_cond_unlock (&zs->session_lock);
617 xfree(zh->user_perm);
618 zh->service = 0; /* more likely to trigger an assert */
620 zebra_limit_destroy(zh->m_limit);
622 nmem_destroy(zh->nmem_error);
629 struct map_baseinfo {
635 char **new_basenames;
639 static void zebra_open_res(ZebraHandle zh)
647 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
648 zh->res = res_open(zh->session_res, 0);
649 res_read_file(zh->res, fname);
651 else if (*zh->reg_name == 0)
653 zh->res = res_open(zh->session_res, 0);
657 yaz_log (YLOG_WARN, "no register root specified");
658 zh->res = 0; /* no path for register - fail! */
662 static void zebra_close_res (ZebraHandle zh)
670 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
674 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
678 assert (zh->reg == 0);
679 assert (*zh->reg_name == 0);
685 resultSetInvalidate (zh);
686 zebra_register_close(zh->service, zh->reg);
692 zh->reg_name = xstrdup (new_reg);
696 if (zh->service->path_root)
698 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
699 strlen(zh->reg_name) + 3);
700 strcpy (zh->path_reg, zh->service->path_root);
703 strcat (zh->path_reg, "/");
704 strcat (zh->path_reg, zh->reg_name);
710 zebra_lock_destroy (zh->lock_normal);
714 zebra_lock_destroy (zh->lock_shadow);
720 const char *lock_area = res_get (zh->res, "lockDir");
722 if (!lock_area && zh->path_reg)
723 res_set (zh->res, "lockDir", zh->path_reg);
724 sprintf (fname, "norm.%s.LCK", zh->reg_name);
726 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
728 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
730 zebra_lock_create (res_get(zh->res, "lockDir"), fname);
732 if (!zh->lock_normal || !zh->lock_shadow)
736 zebra_lock_destroy(zh->lock_normal);
741 zebra_lock_destroy(zh->lock_shadow);
750 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
751 zebra_set_approx_limit(zh, approx);
755 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
756 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
758 yaz_log(YLOG_LOG, "static rank unset");
762 void map_basenames_func (void *vp, const char *name, const char *value)
764 struct map_baseinfo *p = (struct map_baseinfo *) vp;
766 char fromdb[128], todb[8][128];
773 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
774 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
775 todb[5], todb[6], todb[7]);
779 for (i = 0; i<p->num_bases; i++)
780 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
783 for (i = 0; i < no; i++)
785 if (p->new_num_bases == p->new_num_max)
787 p->new_basenames[(p->new_num_bases)++] =
788 nmem_strdup (p->mem, todb[i]);
794 int zebra_select_default_database(ZebraHandle zh)
798 /* no database has been selected - so we select based on
799 resource setting (including group)
801 const char *group = res_get(zh->session_res, "group");
802 const char *v = res_get_prefix(zh->session_res,
803 "database", group, "Default");
804 return zebra_select_database(zh, v);
809 void map_basenames (ZebraHandle zh, ODR stream,
810 int *num_bases, char ***basenames)
812 struct map_baseinfo info;
813 struct map_baseinfo *p = &info;
816 yaz_log(log_level, "map_basenames ");
821 info.num_bases = *num_bases;
822 info.basenames = *basenames;
823 info.new_num_max = 128;
824 info.new_num_bases = 0;
825 info.new_basenames = (char **)
826 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
827 info.mem = stream->mem;
829 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
831 for (i = 0; i<p->num_bases; i++)
832 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
834 p->new_basenames[(p->new_num_bases)++] =
835 nmem_strdup (p->mem, p->basenames[i]);
837 *num_bases = info.new_num_bases;
838 *basenames = info.new_basenames;
839 for (i = 0; i<*num_bases; i++)
840 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
843 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
846 yaz_log(log_level, "zebra_select_database %s",basename);
848 return zebra_select_databases (zh, 1, &basename);
851 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
852 const char **basenames)
861 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
862 num_bases,basenames[0]);
867 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
871 /* Check if the user has access to all databases (Seb) */
872 /* You could argue that this should happen later, after we have
873 * determined that the database(s) exist. */
874 if (zh->dbaccesslist) {
875 for (i = 0; i < num_bases; i++) {
876 const char *db = basenames[i];
878 for (p = zh->dbaccesslist; p && *p; p = pp) {
880 if ((pp = strchr(p, '+'))) {
886 if (len == strlen(db) && !strncmp(db, p, len))
890 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
896 for (i = 0; i < zh->num_basenames; i++)
897 xfree(zh->basenames[i]);
898 xfree(zh->basenames);
900 zh->num_basenames = num_bases;
901 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
902 for (i = 0; i < zh->num_basenames; i++)
903 zh->basenames[i] = xstrdup (basenames[i]);
905 cp = strrchr(basenames[0], '/');
908 len = cp - basenames[0];
909 new_reg = xmalloc(len + 1);
910 memcpy (new_reg, basenames[0], len);
914 new_reg = xstrdup ("");
915 for (i = 1; i<num_bases; i++)
919 cp1 = strrchr (basenames[i], '/');
924 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
927 if (len != cp1 - basenames[i] ||
928 memcmp (basenames[i], new_reg, len))
930 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
938 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
943 zebra_select_register (zh, new_reg);
947 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
950 if (!zh->lock_normal || !zh->lock_shadow)
952 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
958 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
960 if (approx_limit == 0)
961 approx_limit = DEFAULT_APPROX_LIMIT;
962 zh->approx_limit = approx_limit;
966 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
967 const char *setname, zint *hits)
975 yaz_log(log_level, "zebra_search_rpn");
979 if (zebra_begin_read(zh) == ZEBRA_FAIL)
982 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
983 zh->num_basenames, zh->basenames, setname);
989 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
991 Z_RecordComposition *comp,
992 oid_value input_format, int num_recs,
993 ZebraRetrievalRecord *recs)
995 ZebraMetaRecord *poset;
997 ZEBRA_RES ret = ZEBRA_OK;
1005 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1009 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1014 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1017 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1018 for (i = 0; i<num_recs; i++)
1019 pos_array[i] = recs[i].position;
1020 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1023 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1024 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1030 for (i = 0; i<num_recs; i++)
1034 recs[i].errCode = 0;
1035 recs[i].format = VAL_SUTRS;
1036 recs[i].len = strlen(poset[i].term);
1037 recs[i].buf = poset[i].term;
1038 recs[i].base = poset[i].db;
1040 else if (poset[i].sysno)
1044 zebra_snippets *hit_snippet = zebra_snippets_create();
1046 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1050 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1052 stream, input_format, comp,
1053 &recs[i].format, &buf, &len,
1054 &recs[i].base, &recs[i].errString);
1059 recs[i].buf = (char*) odr_malloc(stream, len);
1060 memcpy(recs[i].buf, buf, len);
1064 recs[i].score = poset[i].score;
1065 recs[i].sysno = poset[i].sysno;
1066 zebra_snippets_destroy(hit_snippet);
1070 /* only need to set it once */
1071 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1073 zebra_setError_zint(zh,
1074 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1079 recs[i].buf = 0; /* no record and no error issued */
1081 recs[i].errCode = 0;
1082 recs[i].format = VAL_NONE;
1086 zebra_meta_records_destroy(zh, poset, num_recs);
1088 zebra_end_read (zh);
1093 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1095 int *num_entries, ZebraScanEntry **entries,
1097 const char *setname)
1099 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1100 Z_AttributesPlusTerm *zapt;
1104 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1107 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1110 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1111 position, num_entries, entries, is_partial,
1113 yaz_pqf_destroy (pqf_parser);
1117 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1118 oid_value attributeset,
1120 int *num_entries, ZebraScanEntry **entries,
1122 const char *setname)
1125 RSET limit_rset = 0;
1130 assert(num_entries);
1133 yaz_log(log_level, "zebra_scan");
1135 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1143 limit_rset = resultSetRef(zh, setname);
1147 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1149 zebra_end_read (zh);
1153 res = rpn_scan (zh, stream, zapt, attributeset,
1154 zh->num_basenames, zh->basenames, position,
1155 num_entries, entries, is_partial, limit_rset, 0);
1156 zebra_end_read (zh);
1160 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1161 int num_input_setnames, const char **input_setnames,
1162 const char *output_setname,
1163 Z_SortKeySpecList *sort_sequence,
1169 assert(num_input_setnames>0);
1170 assert(input_setnames);
1171 assert(sort_sequence);
1172 assert(sort_status);
1173 yaz_log(log_level, "zebra_sort");
1175 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1177 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1178 output_setname, sort_sequence, sort_status);
1183 int zebra_deleteResultSet(ZebraHandle zh, int function,
1184 int num_setnames, char **setnames,
1190 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1192 if (zebra_begin_read(zh))
1193 return Z_DeleteStatus_systemProblemAtTarget;
1196 case Z_DeleteResultSetRequest_list:
1197 assert(num_setnames>0);
1199 resultSetDestroy (zh, num_setnames, setnames, statuses);
1201 case Z_DeleteResultSetRequest_all:
1202 resultSetDestroy (zh, -1, 0, statuses);
1205 zebra_end_read (zh);
1206 status = Z_DeleteStatus_success;
1207 for (i = 0; i<num_setnames; i++)
1208 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1209 status = statuses[i];
1213 int zebra_errCode (ZebraHandle zh)
1217 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1220 yaz_log(log_level, "zebra_errCode: o");
1224 const char *zebra_errString (ZebraHandle zh)
1228 e= diagbib1_str (zh->errCode);
1229 yaz_log(log_level, "zebra_errString: %s",e);
1233 char *zebra_errAdd (ZebraHandle zh)
1238 yaz_log(log_level, "zebra_errAdd: %s",a);
1242 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1245 const char *astring;
1253 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1254 p = res_get(zs->global_res, u);
1255 xfree(zh->user_perm);
1256 zh->user_perm = xstrdup(p ? p : "r");
1258 /* Determine database access list */
1259 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1261 zh->dbaccesslist = xstrdup(astring);
1263 zh->dbaccesslist = 0;
1265 /* users that don't require a password .. */
1266 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1269 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1274 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1275 const char *record_type)
1278 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1279 database, record_type);
1280 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1282 return zebra_begin_trans(zh, 1);
1285 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1288 yaz_log(log_level, "zebra_admin_import_end");
1289 return zebra_end_trans(zh);
1292 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1294 ZEBRA_RES res = ZEBRA_OK;
1298 yaz_log(log_level, "zebra_admin_import_segment");
1300 for (i = 0; i<segment->num_segmentRecords; i++)
1302 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1304 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1306 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1307 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1309 Odr_oct *oct = fragment->u.notExternallyTagged;
1312 if (zebra_update_record(zh,
1313 0, /* record Type */
1317 (const char *) oct->buf, oct->len,
1326 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1327 const char *rec_buf,
1329 const char *recid_buf, size_t recid_len,
1331 /* 1 = insert. Fail it already exists */
1332 /* 2 = replace. Fail it does not exist */
1333 /* 3 = delete. Fail if does not exist */
1334 /* 4 = update. Insert/replace */
1342 assert(action>0 && action <=4);
1345 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1347 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1349 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1350 "no record ID or empty record ID");
1354 memcpy (recid_z, recid_buf, recid_len);
1355 recid_z[recid_len] = 0;
1357 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1360 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1361 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1364 if (action == 1) /* fail if insert */
1366 zebra_end_trans(zh);
1367 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1368 "Cannot insert record: already exist");
1372 memcpy (&sysno, rinfo+1, sizeof(sysno));
1376 if (action == 2 || action == 3) /* fail if delete or update */
1378 zebra_end_trans(zh);
1379 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1380 "Cannot delete/update record: does not exist");
1383 action = 1; /* make it an insert (if it's an update).. */
1385 res = buffer_extract_record (zh, rec_buf, rec_len,
1386 action == 3 ? 1 : 0 /* delete flag */,
1392 0, /* force update */
1393 1 /* allow update */
1395 if (res == ZEBRA_FAIL)
1397 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1398 "Unable to parse record");
1402 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1403 sizeof(sysno), &sysno);
1405 else if (action == 3)
1407 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1409 zebra_end_trans(zh);
1413 int delete_w_handle(const char *info, void *handle)
1415 ZebraHandle zh = (ZebraHandle) handle;
1419 if (*info == sizeof(pos))
1421 memcpy (&pos, info+1, sizeof(pos));
1422 isamb_unlink(zh->reg->isamb, pos);
1427 static int delete_SU_handle(void *handle, int ord)
1429 ZebraHandle zh = (ZebraHandle) handle;
1433 ord_len = key_SU_encode (ord, ord_buf);
1434 ord_buf[ord_len] = '\0';
1436 assert (zh->reg->isamb);
1437 dict_delete_subtree(zh->reg->dict, ord_buf,
1438 zh, delete_w_handle);
1442 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1444 ZEBRA_RES ret = ZEBRA_OK;
1446 yaz_log(log_level, "zebra_drop_database %s", db);
1448 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1450 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1455 zebraExplain_curDatabase (zh->reg->zei, db);
1456 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1457 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1458 0 /* handle */, 0 /* func */);
1459 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1460 zebraExplain_removeDatabase(zh->reg->zei, zh);
1464 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1465 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1466 "drop database only supported for isam:b");
1469 zebra_end_trans (zh);
1473 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1476 yaz_log(log_level, "zebra_create_database %s", db);
1479 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1481 if (zebra_begin_trans (zh, 1))
1484 /* announce database */
1485 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1486 /* explainDatabase */))
1488 zebra_end_trans (zh);
1489 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1492 return zebra_end_trans (zh);
1495 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1496 const char *input_str, int input_len,
1497 char *output_str, int output_len)
1503 yaz_log(log_level, "zebra_string_norm ");
1505 if (!zh->reg->zebra_maps)
1507 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1508 input_str, input_len);
1511 if (wrbuf_len(wrbuf) >= output_len)
1513 if (wrbuf_len(wrbuf))
1514 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1515 output_str[wrbuf_len(wrbuf)] = '\0';
1516 return wrbuf_len(wrbuf);
1519 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1521 char state_fname[256];
1526 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1528 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1529 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1530 f = fopen (fname, "w");
1532 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1533 fprintf (f, "%c %d %ld\n", val, seqno, p);
1538 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1540 char state_fname[256];
1545 yaz_log(log_level, "zebra_get_state ");
1547 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1548 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1549 f = fopen (fname, "r");
1555 fscanf (f, "%c %d", val, seqno);
1561 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1563 return zebra_begin_trans(zh, 0);
1566 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1568 return zebra_end_trans(zh);
1571 static void read_res_for_transaction(ZebraHandle zh)
1573 const char *group = res_get(zh->res, "group");
1575 /* FIXME - do we still use groups ?? */
1577 zh->m_group = group;
1578 v = res_get_prefix(zh->res, "followLinks", group, "1");
1579 zh->m_follow_links = atoi(v);
1581 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1582 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1584 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1585 zh->m_store_keys = atoi(v);
1587 v = res_get_prefix(zh->res, "storeData", group, "1");
1588 zh->m_store_data = atoi(v);
1590 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1591 zh->m_explain_database = atoi(v);
1593 v = res_get_prefix(zh->res, "openRW", group, "1");
1594 zh->m_flag_rw = atoi(v);
1596 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1597 zh->m_file_verbose_limit = atoi(v);
1600 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1603 zebra_select_default_database(zh);
1606 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1607 "zebra_begin_trans: no database selected");
1611 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1615 if (rw && !strchr(zh->user_perm, 'w'))
1619 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1631 const char *rval = 0;
1636 read_res_for_transaction(zh);
1639 if (zh->trans_no != 1)
1641 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1642 "zebra_begin_trans: no write trans within read");
1647 resultSetInvalidate (zh);
1648 zebra_register_close(zh->service, zh->reg);
1650 zh->trans_w_no = zh->trans_no;
1652 zh->records_inserted = 0;
1653 zh->records_updated = 0;
1654 zh->records_deleted = 0;
1655 zh->records_processed = 0;
1657 #if HAVE_SYS_TIMES_H
1661 if (zh->shadow_enable)
1662 rval = res_get (zh->res, "shadow");
1664 for (pass = 0; pass < 2; pass++)
1668 zebra_lock_r (zh->lock_normal);
1669 zebra_lock_w (zh->lock_shadow);
1673 zebra_lock_w (zh->lock_normal);
1674 zebra_lock_w (zh->lock_shadow);
1677 zebra_get_state (zh, &val, &seqno);
1680 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1681 zebra_unlock (zh->lock_shadow);
1682 zebra_unlock (zh->lock_normal);
1686 else if (val == 'd')
1690 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1692 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1693 bf_commitClean (bfs, rval);
1698 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1705 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1709 zebra_set_state (zh, 'd', seqno);
1711 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1712 1, rval ? 1 : 0, zh->res,
1715 zh->reg->seqno = seqno;
1718 zebra_set_state (zh, 'o', seqno);
1720 zebra_unlock (zh->lock_shadow);
1721 zebra_unlock (zh->lock_normal);
1726 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1727 "zebra_begin_trans: cannot open register");
1728 yaz_log(YLOG_FATAL, "%s", zh->errString);
1731 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1741 if (zh->trans_no != 1)
1743 zebra_flush_reg (zh);
1746 #if HAVE_SYS_TIMES_H
1752 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1755 if (!zh->lock_normal || !zh->lock_shadow)
1758 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1761 zebra_get_state (zh, &val, &seqno);
1767 else if (seqno != zh->reg->seqno)
1769 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1770 seqno, zh->reg->seqno);
1773 else if (zh->reg->last_val != val)
1775 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1776 val, zh->reg->last_val);
1783 zebra_lock_r (zh->lock_shadow);
1785 zebra_lock_r (zh->lock_normal);
1789 resultSetInvalidate (zh);
1790 zebra_register_close(zh->service, zh->reg);
1792 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1793 0, val == 'c' ? 1 : 0,
1794 zh->res, zh->path_reg);
1797 zebra_unlock (zh->lock_normal);
1798 zebra_unlock (zh->lock_shadow);
1800 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1803 zh->reg->last_val = val;
1804 zh->reg->seqno = seqno;
1806 read_res_for_transaction(zh);
1810 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1812 ZebraTransactionStatus dummy;
1814 yaz_log(log_level, "zebra_end_trans");
1815 return zebra_end_transaction(zh, &dummy);
1818 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1826 yaz_log(log_level, "zebra_end_transaction");
1828 status->processed = 0;
1829 status->inserted = 0;
1830 status->updated = 0;
1831 status->deleted = 0;
1835 if (!zh->res || !zh->reg)
1837 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1838 "zebra_end_trans: no open transaction");
1841 if (zh->trans_no != zh->trans_w_no)
1844 if (zh->trans_no != 0)
1847 /* release read lock */
1849 zebra_unlock (zh->lock_normal);
1850 zebra_unlock (zh->lock_shadow);
1853 { /* release write lock */
1857 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1858 rval = res_get (zh->res, "shadow");
1860 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1862 zebra_flush_reg (zh);
1864 resultSetInvalidate (zh);
1866 zebra_register_close(zh->service, zh->reg);
1869 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1870 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1871 zh->records_processed, zh->records_inserted,
1872 zh->records_updated, zh->records_deleted);
1874 status->processed = (int) zh->records_processed;
1875 status->inserted = (int) zh->records_inserted;
1876 status->updated = (int) zh->records_updated;
1877 status->deleted = (int) zh->records_deleted;
1879 zebra_get_state (zh, &val, &seqno);
1882 BFiles bfs = bfs_create (rval, zh->path_reg);
1883 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1884 bf_commitClean (bfs, rval);
1889 zebra_set_state (zh, 'o', seqno);
1891 zebra_unlock (zh->lock_shadow);
1892 zebra_unlock (zh->lock_normal);
1895 #if HAVE_SYS_TIMES_H
1897 yaz_log (log_level, "user/system: %ld/%ld",
1898 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1899 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1901 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1902 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1907 int zebra_repository_update (ZebraHandle zh, const char *path)
1911 yaz_log (log_level, "updating %s", path);
1912 repositoryUpdate (zh, path);
1916 int zebra_repository_delete (ZebraHandle zh, const char *path)
1920 yaz_log (log_level, "deleting %s", path);
1921 repositoryDelete (zh, path);
1925 int zebra_repository_show (ZebraHandle zh, const char *path)
1929 yaz_log(log_level, "zebra_repository_show");
1930 repositoryShow (zh, path);
1934 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1942 zebra_select_default_database(zh);
1945 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1948 rval = res_get (zh->res, "shadow");
1951 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1955 zebra_lock_w (zh->lock_normal);
1956 zebra_lock_r (zh->lock_shadow);
1958 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1960 zebra_get_state (zh, &val, &seqno);
1963 bf_cache (bfs, rval);
1964 if (bf_commitExists (bfs))
1967 zebra_set_state (zh, 'd', seqno);
1970 zebra_set_state (zh, 'c', seqno);
1972 yaz_log (YLOG_DEBUG, "commit start");
1973 bf_commitExec (bfs);
1978 yaz_log (YLOG_DEBUG, "commit clean");
1979 bf_commitClean (bfs, rval);
1981 zebra_set_state (zh, 'o', seqno);
1985 yaz_log (log_level, "nothing to commit");
1989 zebra_unlock (zh->lock_shadow);
1990 zebra_unlock (zh->lock_normal);
1994 ZEBRA_RES zebra_clean(ZebraHandle zh)
1997 yaz_log(log_level, "zebra_clean");
1998 return zebra_commit_ex(zh, 1);
2001 ZEBRA_RES zebra_commit(ZebraHandle zh)
2004 yaz_log(log_level, "zebra_commit");
2005 return zebra_commit_ex(zh, 0);
2008 ZEBRA_RES zebra_init(ZebraHandle zh)
2013 yaz_log(log_level, "zebra_init");
2015 zebra_select_default_database(zh);
2018 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2019 "cannot select default database");
2022 rval = res_get (zh->res, "shadow");
2024 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2027 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2031 bf_cache (bfs, rval);
2035 zebra_set_state (zh, 'o', 0);
2039 ZEBRA_RES zebra_compact(ZebraHandle zh)
2043 yaz_log(log_level, "zebra_compact");
2046 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2049 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2055 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2058 yaz_log(log_level, "zebra_result");
2059 *code = zh->errCode;
2060 *addinfo = zh->errString;
2063 void zebra_shadow_enable(ZebraHandle zh, int value)
2066 yaz_log(log_level, "zebra_shadow_enable");
2067 zh->shadow_enable = value;
2070 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2074 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2076 if (zh->iconv_to_utf8 != 0)
2077 yaz_iconv_close(zh->iconv_to_utf8);
2078 if (zh->iconv_from_utf8 != 0)
2079 yaz_iconv_close(zh->iconv_from_utf8);
2082 yaz_iconv_open ("UTF-8", encoding);
2083 if (zh->iconv_to_utf8 == 0)
2084 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2085 zh->iconv_from_utf8 =
2086 yaz_iconv_open (encoding, "UTF-8");
2087 if (zh->iconv_to_utf8 == 0)
2088 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2093 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2096 yaz_log(log_level, "zebra_record_encoding");
2097 xfree(zh->record_encoding);
2098 zh->record_encoding = 0;
2100 zh->record_encoding = xstrdup (encoding);
2104 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2109 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2110 res_set(zh->res, name, value);
2113 const char *zebra_get_resource(ZebraHandle zh,
2114 const char *name, const char *defaultvalue)
2119 v = res_get_def (zh->res, name, (char *)defaultvalue);
2120 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2124 /* moved from zebra_api_ext.c by pop */
2125 /* FIXME: Should this really be public??? -Heikki */
2127 int zebra_trans_no (ZebraHandle zh)
2130 yaz_log(log_level, "zebra_trans_no");
2131 return zh->trans_no;
2134 int zebra_get_shadow_enable (ZebraHandle zh)
2137 yaz_log(log_level, "zebra_get_shadow_enable");
2138 return zh->shadow_enable;
2141 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2144 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2145 zh->shadow_enable = value;
2148 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2149 so that it's identicical to the original api_records_retrieve */
2150 void api_records_retrieve (ZebraHandle zh, ODR stream,
2151 const char *setname, Z_RecordComposition *comp,
2152 oid_value input_format, int num_recs,
2153 ZebraRetrievalRecord *recs)
2155 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2159 /* ---------------------------------------------------------------------------
2160 Record insert(=update), delete
2162 If sysno is provided, then it's used to identify the record.
2163 If not, and match_criteria is provided, then sysno is guessed
2164 If not, and a record is provided, then sysno is got from there
2165 NOTE: Now returns 0 at success and updates sysno, which is an int*
2169 int zebra_add_record(ZebraHandle zh,
2170 const char *buf, int buf_size)
2172 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2175 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2176 const char *recordType,
2177 SYSNO *sysno, const char *match,
2179 const char *buf, int buf_size, int force_update)
2185 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2188 buf_size = strlen(buf);
2190 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2192 res = buffer_extract_record (zh, buf, buf_size,
2193 0, /* delete_flag */
2199 0); /* allow_update */
2200 zebra_end_trans(zh);
2204 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2205 const char *recordType,
2206 SYSNO* sysno, const char *match,
2208 const char *buf, int buf_size,
2215 yaz_log(log_level, "zebra_update_record");
2217 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2219 if (buf_size < 1) buf_size = strlen(buf);
2221 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2223 res = buffer_extract_record (zh, buf, buf_size,
2224 0, /* delete_flag */
2230 1); /* allow_update */
2231 zebra_end_trans(zh);
2235 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2236 const char *recordType,
2237 SYSNO *sysno, const char *match,
2239 const char *buf, int buf_size,
2246 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2248 if (buf_size < 1) buf_size = strlen(buf);
2250 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2252 res = buffer_extract_record (zh, buf, buf_size,
2253 1, /* delete_flag */
2259 1); /* allow_update */
2260 zebra_end_trans(zh);
2264 /* ---------------------------------------------------------------------------
2268 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2269 const char *setname, zint *hits)
2272 ZEBRA_RES res = ZEBRA_OK;
2274 ODR odr = odr_createmem(ODR_ENCODE);
2279 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2281 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2285 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2286 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2290 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2294 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2302 /* ---------------------------------------------------------------------------
2303 Sort - a simplified interface, with optional read locks.
2305 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2306 const char *sort_spec,
2307 const char *output_setname,
2308 const char **input_setnames)
2310 int num_input_setnames = 0;
2311 int sort_status = 0;
2312 Z_SortKeySpecList *sort_sequence;
2316 assert(output_setname);
2317 assert(input_setnames);
2318 sort_sequence = yaz_sort_spec (stream, sort_spec);
2319 yaz_log(log_level, "sort (FIXME) ");
2322 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2323 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2327 /* we can do this, since the perl typemap code for char** will
2328 put a NULL at the end of list */
2329 while (input_setnames[num_input_setnames]) num_input_setnames++;
2331 if (zebra_begin_read (zh))
2334 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2335 output_setname, sort_sequence, &sort_status);
2341 /* ---------------------------------------------------------------------------
2342 Get BFS for Zebra system (to make alternative storage methods)
2344 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2347 return zh->reg->bfs;
2352 /* ---------------------------------------------------------------------------
2353 Set limit for search/scan
2355 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2358 zebra_limit_destroy(zh->m_limit);
2359 zh->m_limit = zebra_limit_create(complement_flag, ids);
2364 Set Error code + addinfo
2366 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2369 nmem_reset(zh->nmem_error);
2370 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2373 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2376 sprintf(vstr, ZINT_FORMAT, i);
2379 nmem_reset(zh->nmem_error);
2380 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2383 void zebra_lock_prefix (Res res, char *path)
2385 const char *lock_dir = res_get_def (res, "lockDir", "");
2387 strcpy (path, lock_dir);
2388 if (*path && path[strlen(path)-1] != '/')