1 /* $Id: zebraapi.c,v 1.211 2006-04-04 00:00:18 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);
760 void map_basenames_func (void *vp, const char *name, const char *value)
762 struct map_baseinfo *p = (struct map_baseinfo *) vp;
764 char fromdb[128], todb[8][128];
771 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
772 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
773 todb[5], todb[6], todb[7]);
777 for (i = 0; i<p->num_bases; i++)
778 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
781 for (i = 0; i < no; i++)
783 if (p->new_num_bases == p->new_num_max)
785 p->new_basenames[(p->new_num_bases)++] =
786 nmem_strdup (p->mem, todb[i]);
792 int zebra_select_default_database(ZebraHandle zh)
796 /* no database has been selected - so we select based on
797 resource setting (including group)
799 const char *group = res_get(zh->session_res, "group");
800 const char *v = res_get_prefix(zh->session_res,
801 "database", group, "Default");
802 return zebra_select_database(zh, v);
807 void map_basenames (ZebraHandle zh, ODR stream,
808 int *num_bases, char ***basenames)
810 struct map_baseinfo info;
811 struct map_baseinfo *p = &info;
814 yaz_log(log_level, "map_basenames ");
819 info.num_bases = *num_bases;
820 info.basenames = *basenames;
821 info.new_num_max = 128;
822 info.new_num_bases = 0;
823 info.new_basenames = (char **)
824 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
825 info.mem = stream->mem;
827 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
829 for (i = 0; i<p->num_bases; i++)
830 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
832 p->new_basenames[(p->new_num_bases)++] =
833 nmem_strdup (p->mem, p->basenames[i]);
835 *num_bases = info.new_num_bases;
836 *basenames = info.new_basenames;
837 for (i = 0; i<*num_bases; i++)
838 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
841 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
844 yaz_log(log_level, "zebra_select_database %s",basename);
846 return zebra_select_databases (zh, 1, &basename);
849 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
850 const char **basenames)
859 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
860 num_bases,basenames[0]);
865 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
869 /* Check if the user has access to all databases (Seb) */
870 /* You could argue that this should happen later, after we have
871 * determined that the database(s) exist. */
872 if (zh->dbaccesslist) {
873 for (i = 0; i < num_bases; i++) {
874 const char *db = basenames[i];
876 for (p = zh->dbaccesslist; p && *p; p = pp) {
878 if ((pp = strchr(p, '+'))) {
884 if (len == strlen(db) && !strncmp(db, p, len))
888 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
894 for (i = 0; i < zh->num_basenames; i++)
895 xfree(zh->basenames[i]);
896 xfree(zh->basenames);
898 zh->num_basenames = num_bases;
899 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
900 for (i = 0; i < zh->num_basenames; i++)
901 zh->basenames[i] = xstrdup (basenames[i]);
903 cp = strrchr(basenames[0], '/');
906 len = cp - basenames[0];
907 new_reg = xmalloc(len + 1);
908 memcpy (new_reg, basenames[0], len);
912 new_reg = xstrdup ("");
913 for (i = 1; i<num_bases; i++)
917 cp1 = strrchr (basenames[i], '/');
922 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
925 if (len != cp1 - basenames[i] ||
926 memcmp (basenames[i], new_reg, len))
928 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
936 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
941 zebra_select_register (zh, new_reg);
945 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
948 if (!zh->lock_normal || !zh->lock_shadow)
950 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
956 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
958 if (approx_limit == 0)
959 approx_limit = DEFAULT_APPROX_LIMIT;
960 zh->approx_limit = approx_limit;
964 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
965 const char *setname, zint *hits)
973 yaz_log(log_level, "zebra_search_rpn");
977 if (zebra_begin_read(zh) == ZEBRA_FAIL)
980 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
981 zh->num_basenames, zh->basenames, setname);
987 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
989 Z_RecordComposition *comp,
990 oid_value input_format, int num_recs,
991 ZebraRetrievalRecord *recs)
993 ZebraMetaRecord *poset;
995 ZEBRA_RES ret = ZEBRA_OK;
1003 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1007 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1012 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1015 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1016 for (i = 0; i<num_recs; i++)
1017 pos_array[i] = recs[i].position;
1018 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1021 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1022 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1028 for (i = 0; i<num_recs; i++)
1032 recs[i].errCode = 0;
1033 recs[i].format = VAL_SUTRS;
1034 recs[i].len = strlen(poset[i].term);
1035 recs[i].buf = poset[i].term;
1036 recs[i].base = poset[i].db;
1038 else if (poset[i].sysno)
1042 zebra_snippets *hit_snippet = zebra_snippets_create();
1044 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1048 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1050 stream, input_format, comp,
1051 &recs[i].format, &buf, &len,
1052 &recs[i].base, &recs[i].errString);
1057 recs[i].buf = (char*) odr_malloc(stream, len);
1058 memcpy(recs[i].buf, buf, len);
1062 recs[i].score = poset[i].score;
1063 recs[i].sysno = poset[i].sysno;
1064 zebra_snippets_destroy(hit_snippet);
1068 /* only need to set it once */
1069 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1071 zebra_setError_zint(zh,
1072 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1077 recs[i].buf = 0; /* no record and no error issued */
1079 recs[i].errCode = 0;
1080 recs[i].format = VAL_NONE;
1084 zebra_meta_records_destroy(zh, poset, num_recs);
1086 zebra_end_read (zh);
1091 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1093 int *num_entries, ZebraScanEntry **entries,
1095 const char *setname)
1097 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1098 Z_AttributesPlusTerm *zapt;
1102 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1105 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1108 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1109 position, num_entries, entries, is_partial,
1111 yaz_pqf_destroy (pqf_parser);
1115 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1116 oid_value attributeset,
1118 int *num_entries, ZebraScanEntry **entries,
1120 const char *setname)
1123 RSET limit_rset = 0;
1128 assert(num_entries);
1131 yaz_log(log_level, "zebra_scan");
1133 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1141 limit_rset = resultSetRef(zh, setname);
1145 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1147 zebra_end_read (zh);
1151 res = rpn_scan (zh, stream, zapt, attributeset,
1152 zh->num_basenames, zh->basenames, position,
1153 num_entries, entries, is_partial, limit_rset, 0);
1154 zebra_end_read (zh);
1158 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1159 int num_input_setnames, const char **input_setnames,
1160 const char *output_setname,
1161 Z_SortKeySpecList *sort_sequence,
1167 assert(num_input_setnames>0);
1168 assert(input_setnames);
1169 assert(sort_sequence);
1170 assert(sort_status);
1171 yaz_log(log_level, "zebra_sort");
1173 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1175 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1176 output_setname, sort_sequence, sort_status);
1181 int zebra_deleteResultSet(ZebraHandle zh, int function,
1182 int num_setnames, char **setnames,
1188 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1190 if (zebra_begin_read(zh))
1191 return Z_DeleteStatus_systemProblemAtTarget;
1194 case Z_DeleteResultSetRequest_list:
1195 assert(num_setnames>0);
1197 resultSetDestroy (zh, num_setnames, setnames, statuses);
1199 case Z_DeleteResultSetRequest_all:
1200 resultSetDestroy (zh, -1, 0, statuses);
1203 zebra_end_read (zh);
1204 status = Z_DeleteStatus_success;
1205 for (i = 0; i<num_setnames; i++)
1206 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1207 status = statuses[i];
1211 int zebra_errCode (ZebraHandle zh)
1215 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1218 yaz_log(log_level, "zebra_errCode: o");
1222 const char *zebra_errString (ZebraHandle zh)
1226 e= diagbib1_str (zh->errCode);
1227 yaz_log(log_level, "zebra_errString: %s",e);
1231 char *zebra_errAdd (ZebraHandle zh)
1236 yaz_log(log_level, "zebra_errAdd: %s",a);
1240 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1243 const char *astring;
1251 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1252 p = res_get(zs->global_res, u);
1253 xfree(zh->user_perm);
1254 zh->user_perm = xstrdup(p ? p : "r");
1256 /* Determine database access list */
1257 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1259 zh->dbaccesslist = xstrdup(astring);
1261 zh->dbaccesslist = 0;
1263 /* users that don't require a password .. */
1264 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1267 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1272 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1273 const char *record_type)
1276 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1277 database, record_type);
1278 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1280 return zebra_begin_trans(zh, 1);
1283 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1286 yaz_log(log_level, "zebra_admin_import_end");
1287 return zebra_end_trans(zh);
1290 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1292 ZEBRA_RES res = ZEBRA_OK;
1296 yaz_log(log_level, "zebra_admin_import_segment");
1298 for (i = 0; i<segment->num_segmentRecords; i++)
1300 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1302 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1304 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1305 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1307 Odr_oct *oct = fragment->u.notExternallyTagged;
1310 if (zebra_update_record(zh,
1311 0, /* record Type */
1315 (const char *) oct->buf, oct->len,
1324 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1325 const char *rec_buf,
1327 const char *recid_buf, size_t recid_len,
1329 /* 1 = insert. Fail it already exists */
1330 /* 2 = replace. Fail it does not exist */
1331 /* 3 = delete. Fail if does not exist */
1332 /* 4 = update. Insert/replace */
1340 assert(action>0 && action <=4);
1343 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1345 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1347 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1348 "no record ID or empty record ID");
1352 memcpy (recid_z, recid_buf, recid_len);
1353 recid_z[recid_len] = 0;
1355 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1358 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1359 rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
1362 if (action == 1) /* fail if insert */
1364 if (zebra_end_trans(zh) != ZEBRA_OK)
1365 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1366 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1367 "Cannot insert record: already exist");
1371 memcpy (&sysno, rinfo+1, sizeof(sysno));
1375 if (action == 2 || action == 3) /* fail if delete or update */
1377 if (zebra_end_trans(zh) != ZEBRA_OK)
1378 yaz_log(YLOG_WARN, "zebra_end_trans failed");
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 if (zebra_end_trans(zh) != ZEBRA_OK)
1411 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1417 int delete_w_handle(const char *info, void *handle)
1419 ZebraHandle zh = (ZebraHandle) handle;
1423 if (*info == sizeof(pos))
1425 memcpy (&pos, info+1, sizeof(pos));
1426 isamb_unlink(zh->reg->isamb, pos);
1431 static int delete_SU_handle(void *handle, int ord)
1433 ZebraHandle zh = (ZebraHandle) handle;
1437 ord_len = key_SU_encode (ord, ord_buf);
1438 ord_buf[ord_len] = '\0';
1440 assert (zh->reg->isamb);
1441 dict_delete_subtree(zh->reg->dict, ord_buf,
1442 zh, delete_w_handle);
1446 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1448 ZEBRA_RES ret = ZEBRA_OK;
1450 yaz_log(log_level, "zebra_drop_database %s", db);
1452 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1454 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1459 zebraExplain_curDatabase (zh->reg->zei, db);
1460 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1461 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1462 0 /* handle */, 0 /* func */);
1463 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1464 zebraExplain_removeDatabase(zh->reg->zei, zh);
1468 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1469 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1470 "drop database only supported for isam:b");
1473 if (zebra_end_trans (zh) != ZEBRA_OK)
1475 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1481 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1484 yaz_log(log_level, "zebra_create_database %s", db);
1487 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1489 if (zebra_begin_trans (zh, 1))
1492 /* announce database */
1493 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1494 /* explainDatabase */))
1496 if (zebra_end_trans (zh) != ZEBRA_OK)
1498 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1500 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1503 return zebra_end_trans (zh);
1506 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1507 const char *input_str, int input_len,
1508 char *output_str, int output_len)
1514 yaz_log(log_level, "zebra_string_norm ");
1516 if (!zh->reg->zebra_maps)
1518 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1519 input_str, input_len);
1522 if (wrbuf_len(wrbuf) >= output_len)
1524 if (wrbuf_len(wrbuf))
1525 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1526 output_str[wrbuf_len(wrbuf)] = '\0';
1527 return wrbuf_len(wrbuf);
1530 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1532 char state_fname[256];
1537 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1539 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1540 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1541 f = fopen (fname, "w");
1543 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1544 fprintf (f, "%c %d %ld\n", val, seqno, p);
1549 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1551 char state_fname[256];
1556 yaz_log(log_level, "zebra_get_state ");
1558 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1559 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1560 f = fopen (fname, "r");
1566 fscanf (f, "%c %d", val, seqno);
1572 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1574 return zebra_begin_trans(zh, 0);
1577 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1579 return zebra_end_trans(zh);
1582 static void read_res_for_transaction(ZebraHandle zh)
1584 const char *group = res_get(zh->res, "group");
1586 /* FIXME - do we still use groups ?? */
1588 zh->m_group = group;
1589 v = res_get_prefix(zh->res, "followLinks", group, "1");
1590 zh->m_follow_links = atoi(v);
1592 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1593 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1595 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1596 zh->m_store_keys = atoi(v);
1598 v = res_get_prefix(zh->res, "storeData", group, "1");
1599 zh->m_store_data = atoi(v);
1601 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1602 zh->m_explain_database = atoi(v);
1604 v = res_get_prefix(zh->res, "openRW", group, "1");
1605 zh->m_flag_rw = atoi(v);
1607 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1608 zh->m_file_verbose_limit = atoi(v);
1611 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1614 zebra_select_default_database(zh);
1617 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1618 "zebra_begin_trans: no database selected");
1622 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1626 if (rw && !strchr(zh->user_perm, 'w'))
1630 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1642 const char *rval = 0;
1647 read_res_for_transaction(zh);
1650 if (zh->trans_no != 1)
1652 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1653 "zebra_begin_trans: no write trans within read");
1658 resultSetInvalidate (zh);
1659 zebra_register_close(zh->service, zh->reg);
1661 zh->trans_w_no = zh->trans_no;
1663 zh->records_inserted = 0;
1664 zh->records_updated = 0;
1665 zh->records_deleted = 0;
1666 zh->records_processed = 0;
1668 #if HAVE_SYS_TIMES_H
1672 if (zh->shadow_enable)
1673 rval = res_get (zh->res, "shadow");
1675 for (pass = 0; pass < 2; pass++)
1679 zebra_lock_r (zh->lock_normal);
1680 zebra_lock_w (zh->lock_shadow);
1684 zebra_lock_w (zh->lock_normal);
1685 zebra_lock_w (zh->lock_shadow);
1688 zebra_get_state (zh, &val, &seqno);
1691 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1692 zebra_unlock (zh->lock_shadow);
1693 zebra_unlock (zh->lock_normal);
1697 else if (val == 'd')
1701 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1703 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1704 bf_commitClean (bfs, rval);
1709 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1716 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1720 zebra_set_state (zh, 'd', seqno);
1722 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1723 1, rval ? 1 : 0, zh->res,
1726 zh->reg->seqno = seqno;
1729 zebra_set_state (zh, 'o', seqno);
1731 zebra_unlock (zh->lock_shadow);
1732 zebra_unlock (zh->lock_normal);
1737 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1738 "zebra_begin_trans: cannot open register");
1739 yaz_log(YLOG_FATAL, "%s", zh->errString);
1742 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1752 if (zh->trans_no != 1)
1754 zebra_flush_reg (zh);
1757 #if HAVE_SYS_TIMES_H
1763 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1766 if (!zh->lock_normal || !zh->lock_shadow)
1769 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1772 zebra_get_state (zh, &val, &seqno);
1778 else if (seqno != zh->reg->seqno)
1780 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1781 seqno, zh->reg->seqno);
1784 else if (zh->reg->last_val != val)
1786 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1787 val, zh->reg->last_val);
1794 zebra_lock_r (zh->lock_shadow);
1796 zebra_lock_r (zh->lock_normal);
1800 resultSetInvalidate (zh);
1801 zebra_register_close(zh->service, zh->reg);
1803 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1804 0, val == 'c' ? 1 : 0,
1805 zh->res, zh->path_reg);
1808 zebra_unlock (zh->lock_normal);
1809 zebra_unlock (zh->lock_shadow);
1811 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1814 zh->reg->last_val = val;
1815 zh->reg->seqno = seqno;
1817 read_res_for_transaction(zh);
1821 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1823 ZebraTransactionStatus dummy;
1825 yaz_log(log_level, "zebra_end_trans");
1826 return zebra_end_transaction(zh, &dummy);
1829 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1837 yaz_log(log_level, "zebra_end_transaction");
1839 status->processed = 0;
1840 status->inserted = 0;
1841 status->updated = 0;
1842 status->deleted = 0;
1846 if (!zh->res || !zh->reg)
1848 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1849 "zebra_end_trans: no open transaction");
1852 if (zh->trans_no != zh->trans_w_no)
1855 if (zh->trans_no != 0)
1858 /* release read lock */
1860 zebra_unlock (zh->lock_normal);
1861 zebra_unlock (zh->lock_shadow);
1864 { /* release write lock */
1868 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1869 rval = res_get (zh->res, "shadow");
1871 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1873 zebra_flush_reg (zh);
1875 resultSetInvalidate (zh);
1877 zebra_register_close(zh->service, zh->reg);
1880 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1881 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1882 zh->records_processed, zh->records_inserted,
1883 zh->records_updated, zh->records_deleted);
1885 status->processed = (int) zh->records_processed;
1886 status->inserted = (int) zh->records_inserted;
1887 status->updated = (int) zh->records_updated;
1888 status->deleted = (int) zh->records_deleted;
1890 zebra_get_state (zh, &val, &seqno);
1893 BFiles bfs = bfs_create (rval, zh->path_reg);
1894 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1895 bf_commitClean (bfs, rval);
1900 zebra_set_state (zh, 'o', seqno);
1902 zebra_unlock (zh->lock_shadow);
1903 zebra_unlock (zh->lock_normal);
1906 #if HAVE_SYS_TIMES_H
1908 yaz_log (log_level, "user/system: %ld/%ld",
1909 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1910 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1912 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1913 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1918 int zebra_repository_update (ZebraHandle zh, const char *path)
1922 yaz_log (log_level, "updating %s", path);
1923 repositoryUpdate (zh, path);
1927 int zebra_repository_delete (ZebraHandle zh, const char *path)
1931 yaz_log (log_level, "deleting %s", path);
1932 repositoryDelete (zh, path);
1936 int zebra_repository_show (ZebraHandle zh, const char *path)
1940 yaz_log(log_level, "zebra_repository_show");
1941 repositoryShow (zh, path);
1945 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1953 zebra_select_default_database(zh);
1956 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1959 rval = res_get (zh->res, "shadow");
1962 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1966 zebra_lock_w (zh->lock_normal);
1967 zebra_lock_r (zh->lock_shadow);
1969 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1971 zebra_get_state (zh, &val, &seqno);
1974 bf_cache (bfs, rval);
1975 if (bf_commitExists (bfs))
1978 zebra_set_state (zh, 'd', seqno);
1981 zebra_set_state (zh, 'c', seqno);
1983 yaz_log (YLOG_DEBUG, "commit start");
1984 bf_commitExec (bfs);
1990 zebra_set_state (zh, 'o', seqno);
1992 zebra_unlock (zh->lock_shadow);
1993 zebra_unlock (zh->lock_normal);
1995 zebra_lock_w(zh->lock_shadow);
1996 bf_commitClean (bfs, rval);
1997 zebra_unlock (zh->lock_normal);
2001 zebra_unlock(zh->lock_shadow);
2002 zebra_unlock(zh->lock_normal);
2003 yaz_log (log_level, "nothing to commit");
2010 ZEBRA_RES zebra_clean(ZebraHandle zh)
2013 yaz_log(log_level, "zebra_clean");
2014 return zebra_commit_ex(zh, 1);
2017 ZEBRA_RES zebra_commit(ZebraHandle zh)
2020 yaz_log(log_level, "zebra_commit");
2021 return zebra_commit_ex(zh, 0);
2024 ZEBRA_RES zebra_init(ZebraHandle zh)
2029 yaz_log(log_level, "zebra_init");
2031 zebra_select_default_database(zh);
2034 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2035 "cannot select default database");
2038 rval = res_get (zh->res, "shadow");
2040 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2043 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2047 bf_cache (bfs, rval);
2051 zebra_set_state (zh, 'o', 0);
2055 ZEBRA_RES zebra_compact(ZebraHandle zh)
2059 yaz_log(log_level, "zebra_compact");
2062 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2065 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2071 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2074 yaz_log(log_level, "zebra_result");
2075 *code = zh->errCode;
2076 *addinfo = zh->errString;
2079 void zebra_shadow_enable(ZebraHandle zh, int value)
2082 yaz_log(log_level, "zebra_shadow_enable");
2083 zh->shadow_enable = value;
2086 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2090 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2092 if (zh->iconv_to_utf8 != 0)
2093 yaz_iconv_close(zh->iconv_to_utf8);
2094 if (zh->iconv_from_utf8 != 0)
2095 yaz_iconv_close(zh->iconv_from_utf8);
2098 yaz_iconv_open ("UTF-8", encoding);
2099 if (zh->iconv_to_utf8 == 0)
2100 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2101 zh->iconv_from_utf8 =
2102 yaz_iconv_open (encoding, "UTF-8");
2103 if (zh->iconv_to_utf8 == 0)
2104 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2109 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2112 yaz_log(log_level, "zebra_record_encoding");
2113 xfree(zh->record_encoding);
2114 zh->record_encoding = 0;
2116 zh->record_encoding = xstrdup (encoding);
2120 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2125 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2126 res_set(zh->res, name, value);
2129 const char *zebra_get_resource(ZebraHandle zh,
2130 const char *name, const char *defaultvalue)
2135 v = res_get_def (zh->res, name, (char *)defaultvalue);
2136 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2140 /* moved from zebra_api_ext.c by pop */
2141 /* FIXME: Should this really be public??? -Heikki */
2143 int zebra_trans_no (ZebraHandle zh)
2146 yaz_log(log_level, "zebra_trans_no");
2147 return zh->trans_no;
2150 int zebra_get_shadow_enable (ZebraHandle zh)
2153 yaz_log(log_level, "zebra_get_shadow_enable");
2154 return zh->shadow_enable;
2157 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2160 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2161 zh->shadow_enable = value;
2164 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2165 so that it's identicical to the original api_records_retrieve */
2166 void api_records_retrieve (ZebraHandle zh, ODR stream,
2167 const char *setname, Z_RecordComposition *comp,
2168 oid_value input_format, int num_recs,
2169 ZebraRetrievalRecord *recs)
2171 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2175 /* ---------------------------------------------------------------------------
2176 Record insert(=update), delete
2178 If sysno is provided, then it's used to identify the record.
2179 If not, and match_criteria is provided, then sysno is guessed
2180 If not, and a record is provided, then sysno is got from there
2181 NOTE: Now returns 0 at success and updates sysno, which is an int*
2185 int zebra_add_record(ZebraHandle zh,
2186 const char *buf, int buf_size)
2188 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2191 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2192 const char *recordType,
2193 SYSNO *sysno, const char *match,
2195 const char *buf, int buf_size, int force_update)
2201 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2204 buf_size = strlen(buf);
2206 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2208 res = buffer_extract_record (zh, buf, buf_size,
2209 0, /* delete_flag */
2215 0); /* allow_update */
2216 if (zebra_end_trans(zh) != ZEBRA_OK)
2218 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2224 ZEBRA_RES zebra_update_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_update_record");
2237 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2239 if (buf_size < 1) buf_size = strlen(buf);
2241 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2243 res = buffer_extract_record (zh, buf, buf_size,
2244 0, /* delete_flag */
2250 1); /* allow_update */
2251 if (zebra_end_trans(zh) != ZEBRA_OK)
2253 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2259 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2260 const char *recordType,
2261 SYSNO *sysno, const char *match,
2263 const char *buf, int buf_size,
2270 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2272 if (buf_size < 1) buf_size = strlen(buf);
2274 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2276 res = buffer_extract_record (zh, buf, buf_size,
2277 1, /* delete_flag */
2283 1); /* allow_update */
2284 if (zebra_end_trans(zh) != ZEBRA_OK)
2286 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2292 /* ---------------------------------------------------------------------------
2296 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2297 const char *setname, zint *hits)
2300 ZEBRA_RES res = ZEBRA_OK;
2302 ODR odr = odr_createmem(ODR_ENCODE);
2307 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2309 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2313 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2314 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2318 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2322 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2330 /* ---------------------------------------------------------------------------
2331 Sort - a simplified interface, with optional read locks.
2333 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2334 const char *sort_spec,
2335 const char *output_setname,
2336 const char **input_setnames)
2338 int num_input_setnames = 0;
2339 int sort_status = 0;
2340 Z_SortKeySpecList *sort_sequence;
2344 assert(output_setname);
2345 assert(input_setnames);
2346 sort_sequence = yaz_sort_spec (stream, sort_spec);
2347 yaz_log(log_level, "sort (FIXME) ");
2350 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2351 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2355 /* we can do this, since the perl typemap code for char** will
2356 put a NULL at the end of list */
2357 while (input_setnames[num_input_setnames]) num_input_setnames++;
2359 if (zebra_begin_read (zh))
2362 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2363 output_setname, sort_sequence, &sort_status);
2369 /* ---------------------------------------------------------------------------
2370 Get BFS for Zebra system (to make alternative storage methods)
2372 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2375 return zh->reg->bfs;
2380 /* ---------------------------------------------------------------------------
2381 Set limit for search/scan
2383 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2386 zebra_limit_destroy(zh->m_limit);
2387 zh->m_limit = zebra_limit_create(complement_flag, ids);
2392 Set Error code + addinfo
2394 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2397 nmem_reset(zh->nmem_error);
2398 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2401 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2404 sprintf(vstr, ZINT_FORMAT, i);
2407 nmem_reset(zh->nmem_error);
2408 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2411 void zebra_lock_prefix (Res res, char *path)
2413 const char *lock_dir = res_get_def (res, "lockDir", "");
2415 strcpy (path, lock_dir);
2416 if (*path && path[strlen(path)-1] != '/')