1 /* $Id: zebraapi.c,v 1.210 2006-03-31 15:58:04 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 if (zebra_end_trans(zh) != ZEBRA_OK)
1367 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1368 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1369 "Cannot insert record: already exist");
1373 memcpy (&sysno, rinfo+1, sizeof(sysno));
1377 if (action == 2 || action == 3) /* fail if delete or update */
1379 if (zebra_end_trans(zh) != ZEBRA_OK)
1380 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1381 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1382 "Cannot delete/update record: does not exist");
1385 action = 1; /* make it an insert (if it's an update).. */
1387 res = buffer_extract_record (zh, rec_buf, rec_len,
1388 action == 3 ? 1 : 0 /* delete flag */,
1394 0, /* force update */
1395 1 /* allow update */
1397 if (res == ZEBRA_FAIL)
1399 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1400 "Unable to parse record");
1404 dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
1405 sizeof(sysno), &sysno);
1407 else if (action == 3)
1409 dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
1411 if (zebra_end_trans(zh) != ZEBRA_OK)
1413 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1419 int delete_w_handle(const char *info, void *handle)
1421 ZebraHandle zh = (ZebraHandle) handle;
1425 if (*info == sizeof(pos))
1427 memcpy (&pos, info+1, sizeof(pos));
1428 isamb_unlink(zh->reg->isamb, pos);
1433 static int delete_SU_handle(void *handle, int ord)
1435 ZebraHandle zh = (ZebraHandle) handle;
1439 ord_len = key_SU_encode (ord, ord_buf);
1440 ord_buf[ord_len] = '\0';
1442 assert (zh->reg->isamb);
1443 dict_delete_subtree(zh->reg->dict, ord_buf,
1444 zh, delete_w_handle);
1448 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1450 ZEBRA_RES ret = ZEBRA_OK;
1452 yaz_log(log_level, "zebra_drop_database %s", db);
1454 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1456 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1461 zebraExplain_curDatabase (zh->reg->zei, db);
1462 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1463 dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1464 0 /* handle */, 0 /* func */);
1465 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1466 zebraExplain_removeDatabase(zh->reg->zei, zh);
1470 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1471 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1472 "drop database only supported for isam:b");
1475 if (zebra_end_trans (zh) != ZEBRA_OK)
1477 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1483 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1486 yaz_log(log_level, "zebra_create_database %s", db);
1489 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1491 if (zebra_begin_trans (zh, 1))
1494 /* announce database */
1495 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1496 /* explainDatabase */))
1498 if (zebra_end_trans (zh) != ZEBRA_OK)
1500 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1502 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1505 return zebra_end_trans (zh);
1508 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1509 const char *input_str, int input_len,
1510 char *output_str, int output_len)
1516 yaz_log(log_level, "zebra_string_norm ");
1518 if (!zh->reg->zebra_maps)
1520 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1521 input_str, input_len);
1524 if (wrbuf_len(wrbuf) >= output_len)
1526 if (wrbuf_len(wrbuf))
1527 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1528 output_str[wrbuf_len(wrbuf)] = '\0';
1529 return wrbuf_len(wrbuf);
1532 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1534 char state_fname[256];
1539 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1541 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1542 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1543 f = fopen (fname, "w");
1545 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1546 fprintf (f, "%c %d %ld\n", val, seqno, p);
1551 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1553 char state_fname[256];
1558 yaz_log(log_level, "zebra_get_state ");
1560 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1561 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1562 f = fopen (fname, "r");
1568 fscanf (f, "%c %d", val, seqno);
1574 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1576 return zebra_begin_trans(zh, 0);
1579 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1581 return zebra_end_trans(zh);
1584 static void read_res_for_transaction(ZebraHandle zh)
1586 const char *group = res_get(zh->res, "group");
1588 /* FIXME - do we still use groups ?? */
1590 zh->m_group = group;
1591 v = res_get_prefix(zh->res, "followLinks", group, "1");
1592 zh->m_follow_links = atoi(v);
1594 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1595 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1597 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1598 zh->m_store_keys = atoi(v);
1600 v = res_get_prefix(zh->res, "storeData", group, "1");
1601 zh->m_store_data = atoi(v);
1603 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1604 zh->m_explain_database = atoi(v);
1606 v = res_get_prefix(zh->res, "openRW", group, "1");
1607 zh->m_flag_rw = atoi(v);
1609 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1610 zh->m_file_verbose_limit = atoi(v);
1613 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1616 zebra_select_default_database(zh);
1619 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1620 "zebra_begin_trans: no database selected");
1624 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1628 if (rw && !strchr(zh->user_perm, 'w'))
1632 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1644 const char *rval = 0;
1649 read_res_for_transaction(zh);
1652 if (zh->trans_no != 1)
1654 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1655 "zebra_begin_trans: no write trans within read");
1660 resultSetInvalidate (zh);
1661 zebra_register_close(zh->service, zh->reg);
1663 zh->trans_w_no = zh->trans_no;
1665 zh->records_inserted = 0;
1666 zh->records_updated = 0;
1667 zh->records_deleted = 0;
1668 zh->records_processed = 0;
1670 #if HAVE_SYS_TIMES_H
1674 if (zh->shadow_enable)
1675 rval = res_get (zh->res, "shadow");
1677 for (pass = 0; pass < 2; pass++)
1681 zebra_lock_r (zh->lock_normal);
1682 zebra_lock_w (zh->lock_shadow);
1686 zebra_lock_w (zh->lock_normal);
1687 zebra_lock_w (zh->lock_shadow);
1690 zebra_get_state (zh, &val, &seqno);
1693 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1694 zebra_unlock (zh->lock_shadow);
1695 zebra_unlock (zh->lock_normal);
1699 else if (val == 'd')
1703 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1705 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1706 bf_commitClean (bfs, rval);
1711 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1718 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1722 zebra_set_state (zh, 'd', seqno);
1724 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1725 1, rval ? 1 : 0, zh->res,
1728 zh->reg->seqno = seqno;
1731 zebra_set_state (zh, 'o', seqno);
1733 zebra_unlock (zh->lock_shadow);
1734 zebra_unlock (zh->lock_normal);
1739 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1740 "zebra_begin_trans: cannot open register");
1741 yaz_log(YLOG_FATAL, "%s", zh->errString);
1744 zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1754 if (zh->trans_no != 1)
1756 zebra_flush_reg (zh);
1759 #if HAVE_SYS_TIMES_H
1765 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1768 if (!zh->lock_normal || !zh->lock_shadow)
1771 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1774 zebra_get_state (zh, &val, &seqno);
1780 else if (seqno != zh->reg->seqno)
1782 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1783 seqno, zh->reg->seqno);
1786 else if (zh->reg->last_val != val)
1788 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1789 val, zh->reg->last_val);
1796 zebra_lock_r (zh->lock_shadow);
1798 zebra_lock_r (zh->lock_normal);
1802 resultSetInvalidate (zh);
1803 zebra_register_close(zh->service, zh->reg);
1805 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1806 0, val == 'c' ? 1 : 0,
1807 zh->res, zh->path_reg);
1810 zebra_unlock (zh->lock_normal);
1811 zebra_unlock (zh->lock_shadow);
1813 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1816 zh->reg->last_val = val;
1817 zh->reg->seqno = seqno;
1819 read_res_for_transaction(zh);
1823 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1825 ZebraTransactionStatus dummy;
1827 yaz_log(log_level, "zebra_end_trans");
1828 return zebra_end_transaction(zh, &dummy);
1831 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1839 yaz_log(log_level, "zebra_end_transaction");
1841 status->processed = 0;
1842 status->inserted = 0;
1843 status->updated = 0;
1844 status->deleted = 0;
1848 if (!zh->res || !zh->reg)
1850 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1851 "zebra_end_trans: no open transaction");
1854 if (zh->trans_no != zh->trans_w_no)
1857 if (zh->trans_no != 0)
1860 /* release read lock */
1862 zebra_unlock (zh->lock_normal);
1863 zebra_unlock (zh->lock_shadow);
1866 { /* release write lock */
1870 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1871 rval = res_get (zh->res, "shadow");
1873 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1875 zebra_flush_reg (zh);
1877 resultSetInvalidate (zh);
1879 zebra_register_close(zh->service, zh->reg);
1882 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1883 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1884 zh->records_processed, zh->records_inserted,
1885 zh->records_updated, zh->records_deleted);
1887 status->processed = (int) zh->records_processed;
1888 status->inserted = (int) zh->records_inserted;
1889 status->updated = (int) zh->records_updated;
1890 status->deleted = (int) zh->records_deleted;
1892 zebra_get_state (zh, &val, &seqno);
1895 BFiles bfs = bfs_create (rval, zh->path_reg);
1896 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1897 bf_commitClean (bfs, rval);
1902 zebra_set_state (zh, 'o', seqno);
1904 zebra_unlock (zh->lock_shadow);
1905 zebra_unlock (zh->lock_normal);
1908 #if HAVE_SYS_TIMES_H
1910 yaz_log (log_level, "user/system: %ld/%ld",
1911 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1912 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1914 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1915 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1920 int zebra_repository_update (ZebraHandle zh, const char *path)
1924 yaz_log (log_level, "updating %s", path);
1925 repositoryUpdate (zh, path);
1929 int zebra_repository_delete (ZebraHandle zh, const char *path)
1933 yaz_log (log_level, "deleting %s", path);
1934 repositoryDelete (zh, path);
1938 int zebra_repository_show (ZebraHandle zh, const char *path)
1942 yaz_log(log_level, "zebra_repository_show");
1943 repositoryShow (zh, path);
1947 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1955 zebra_select_default_database(zh);
1958 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1961 rval = res_get (zh->res, "shadow");
1964 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1968 zebra_lock_w (zh->lock_normal);
1969 zebra_lock_r (zh->lock_shadow);
1971 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1973 zebra_get_state (zh, &val, &seqno);
1976 bf_cache (bfs, rval);
1977 if (bf_commitExists (bfs))
1980 zebra_set_state (zh, 'd', seqno);
1983 zebra_set_state (zh, 'c', seqno);
1985 yaz_log (YLOG_DEBUG, "commit start");
1986 bf_commitExec (bfs);
1991 yaz_log (YLOG_DEBUG, "commit clean");
1992 bf_commitClean (bfs, rval);
1994 zebra_set_state (zh, 'o', seqno);
1998 yaz_log (log_level, "nothing to commit");
2002 zebra_unlock (zh->lock_shadow);
2003 zebra_unlock (zh->lock_normal);
2007 ZEBRA_RES zebra_clean(ZebraHandle zh)
2010 yaz_log(log_level, "zebra_clean");
2011 return zebra_commit_ex(zh, 1);
2014 ZEBRA_RES zebra_commit(ZebraHandle zh)
2017 yaz_log(log_level, "zebra_commit");
2018 return zebra_commit_ex(zh, 0);
2021 ZEBRA_RES zebra_init(ZebraHandle zh)
2026 yaz_log(log_level, "zebra_init");
2028 zebra_select_default_database(zh);
2031 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2032 "cannot select default database");
2035 rval = res_get (zh->res, "shadow");
2037 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2040 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2044 bf_cache (bfs, rval);
2048 zebra_set_state (zh, 'o', 0);
2052 ZEBRA_RES zebra_compact(ZebraHandle zh)
2056 yaz_log(log_level, "zebra_compact");
2059 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2062 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2068 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2071 yaz_log(log_level, "zebra_result");
2072 *code = zh->errCode;
2073 *addinfo = zh->errString;
2076 void zebra_shadow_enable(ZebraHandle zh, int value)
2079 yaz_log(log_level, "zebra_shadow_enable");
2080 zh->shadow_enable = value;
2083 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2087 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2089 if (zh->iconv_to_utf8 != 0)
2090 yaz_iconv_close(zh->iconv_to_utf8);
2091 if (zh->iconv_from_utf8 != 0)
2092 yaz_iconv_close(zh->iconv_from_utf8);
2095 yaz_iconv_open ("UTF-8", encoding);
2096 if (zh->iconv_to_utf8 == 0)
2097 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2098 zh->iconv_from_utf8 =
2099 yaz_iconv_open (encoding, "UTF-8");
2100 if (zh->iconv_to_utf8 == 0)
2101 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2106 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2109 yaz_log(log_level, "zebra_record_encoding");
2110 xfree(zh->record_encoding);
2111 zh->record_encoding = 0;
2113 zh->record_encoding = xstrdup (encoding);
2117 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2122 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2123 res_set(zh->res, name, value);
2126 const char *zebra_get_resource(ZebraHandle zh,
2127 const char *name, const char *defaultvalue)
2132 v = res_get_def (zh->res, name, (char *)defaultvalue);
2133 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2137 /* moved from zebra_api_ext.c by pop */
2138 /* FIXME: Should this really be public??? -Heikki */
2140 int zebra_trans_no (ZebraHandle zh)
2143 yaz_log(log_level, "zebra_trans_no");
2144 return zh->trans_no;
2147 int zebra_get_shadow_enable (ZebraHandle zh)
2150 yaz_log(log_level, "zebra_get_shadow_enable");
2151 return zh->shadow_enable;
2154 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2157 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2158 zh->shadow_enable = value;
2161 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2162 so that it's identicical to the original api_records_retrieve */
2163 void api_records_retrieve (ZebraHandle zh, ODR stream,
2164 const char *setname, Z_RecordComposition *comp,
2165 oid_value input_format, int num_recs,
2166 ZebraRetrievalRecord *recs)
2168 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2172 /* ---------------------------------------------------------------------------
2173 Record insert(=update), delete
2175 If sysno is provided, then it's used to identify the record.
2176 If not, and match_criteria is provided, then sysno is guessed
2177 If not, and a record is provided, then sysno is got from there
2178 NOTE: Now returns 0 at success and updates sysno, which is an int*
2182 int zebra_add_record(ZebraHandle zh,
2183 const char *buf, int buf_size)
2185 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2188 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2189 const char *recordType,
2190 SYSNO *sysno, const char *match,
2192 const char *buf, int buf_size, int force_update)
2198 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2201 buf_size = strlen(buf);
2203 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2205 res = buffer_extract_record (zh, buf, buf_size,
2206 0, /* delete_flag */
2212 0); /* allow_update */
2213 if (zebra_end_trans(zh) != ZEBRA_OK)
2215 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2221 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2222 const char *recordType,
2223 SYSNO* sysno, const char *match,
2225 const char *buf, int buf_size,
2232 yaz_log(log_level, "zebra_update_record");
2234 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2236 if (buf_size < 1) buf_size = strlen(buf);
2238 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2240 res = buffer_extract_record (zh, buf, buf_size,
2241 0, /* delete_flag */
2247 1); /* allow_update */
2248 if (zebra_end_trans(zh) != ZEBRA_OK)
2250 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2256 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2257 const char *recordType,
2258 SYSNO *sysno, const char *match,
2260 const char *buf, int buf_size,
2267 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2269 if (buf_size < 1) buf_size = strlen(buf);
2271 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2273 res = buffer_extract_record (zh, buf, buf_size,
2274 1, /* delete_flag */
2280 1); /* allow_update */
2281 if (zebra_end_trans(zh) != ZEBRA_OK)
2283 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2289 /* ---------------------------------------------------------------------------
2293 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2294 const char *setname, zint *hits)
2297 ZEBRA_RES res = ZEBRA_OK;
2299 ODR odr = odr_createmem(ODR_ENCODE);
2304 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2306 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2310 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2311 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2315 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2319 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2327 /* ---------------------------------------------------------------------------
2328 Sort - a simplified interface, with optional read locks.
2330 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2331 const char *sort_spec,
2332 const char *output_setname,
2333 const char **input_setnames)
2335 int num_input_setnames = 0;
2336 int sort_status = 0;
2337 Z_SortKeySpecList *sort_sequence;
2341 assert(output_setname);
2342 assert(input_setnames);
2343 sort_sequence = yaz_sort_spec (stream, sort_spec);
2344 yaz_log(log_level, "sort (FIXME) ");
2347 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2348 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2352 /* we can do this, since the perl typemap code for char** will
2353 put a NULL at the end of list */
2354 while (input_setnames[num_input_setnames]) num_input_setnames++;
2356 if (zebra_begin_read (zh))
2359 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2360 output_setname, sort_sequence, &sort_status);
2366 /* ---------------------------------------------------------------------------
2367 Get BFS for Zebra system (to make alternative storage methods)
2369 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2372 return zh->reg->bfs;
2377 /* ---------------------------------------------------------------------------
2378 Set limit for search/scan
2380 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2383 zebra_limit_destroy(zh->m_limit);
2384 zh->m_limit = zebra_limit_create(complement_flag, ids);
2389 Set Error code + addinfo
2391 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2394 nmem_reset(zh->nmem_error);
2395 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2398 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2401 sprintf(vstr, ZINT_FORMAT, i);
2404 nmem_reset(zh->nmem_error);
2405 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2408 void zebra_lock_prefix (Res res, char *path)
2410 const char *lock_dir = res_get_def (res, "lockDir", "");
2412 strcpy (path, lock_dir);
2413 if (*path && path[strlen(path)-1] != '/')