1 /* $Id: zebraapi.c,v 1.199 2005-12-09 11:33:32 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>
40 #include <idzebra/api.h>
42 #define DEFAULT_APPROX_LIMIT 2000000000
44 /* simple asserts to validate the most essential input args */
45 #define ASSERTZH assert(zh && zh->service)
46 #define ASSERTZHRES assert(zh && zh->service && zh->res)
47 #define ASSERTZS assert(zs)
49 static int log_level = 0;
50 static int log_level_initialized = 0;
52 static void zebra_open_res(ZebraHandle zh);
53 static void zebra_close_res(ZebraHandle zh);
55 static void zebra_chdir (ZebraService zs)
59 yaz_log(log_level, "zebra_chdir");
60 dir = res_get (zs->global_res, "chdir");
63 yaz_log (YLOG_DEBUG, "chdir %s", dir);
71 static void zebra_flush_reg (ZebraHandle zh)
74 yaz_log(log_level, "zebra_flush_reg");
75 zebraExplain_flush (zh->reg->zei, zh);
77 extract_flushWriteKeys (zh, 1 /* final */);
78 zebra_index_merge (zh );
81 static struct zebra_register *zebra_register_open(ZebraService zs,
83 int rw, int useshadow,
85 const char *reg_path);
86 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
88 ZebraHandle zebra_open(ZebraService zs, Res res)
91 const char *default_encoding;
92 if (!log_level_initialized)
94 log_level = yaz_log_module_level("zebraapi");
95 log_level_initialized = 1;
98 yaz_log(log_level, "zebra_open");
103 zh = (ZebraHandle) xmalloc(sizeof(*zh));
104 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
107 zh->reg = 0; /* no register attached yet */
113 zh->session_res = res_open(zs->global_res, res);
115 zh->dbaccesslist = 0;
117 zh->reg_name = xstrdup ("");
119 zh->num_basenames = 0;
122 zh->approx_limit = DEFAULT_APPROX_LIMIT;
129 zh->shadow_enable = 1;
130 zh->m_staticrank = 0;
132 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
135 yaz_iconv_open ("UTF-8", default_encoding);
136 if (zh->iconv_to_utf8 == 0)
137 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
139 zh->iconv_from_utf8 =
140 yaz_iconv_open (default_encoding, "UTF-8");
141 if (zh->iconv_to_utf8 == 0)
142 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
145 zh->record_encoding = 0;
147 zebra_mutex_cond_lock (&zs->session_lock);
149 zh->next = zs->sessions;
152 zebra_mutex_cond_unlock (&zs->session_lock);
154 zh->store_data_buf = 0;
156 zh->m_limit = zebra_limit_create(1, 0);
158 zh->nmem_error = nmem_create();
163 ZebraService zebra_start (const char *configName)
165 return zebra_start_res(configName, 0, 0);
168 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
172 if (!log_level_initialized)
174 log_level = yaz_log_module_level("zebraapi");
175 log_level_initialized = 1;
178 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
179 configName ? configName : "");
181 if ((res = res_open(def_res, over_res)))
183 const char *passwd_plain = 0;
184 const char *passwd_encrypt = 0;
185 const char *dbaccess = 0;
186 ZebraService zh = xmalloc(sizeof(*zh));
189 res_read_file(res, configName);
191 zh->global_res = res;
196 zebra_mutex_cond_init (&zh->session_lock);
197 passwd_plain = res_get (zh->global_res, "passwd");
198 passwd_encrypt = res_get (zh->global_res, "passwd.c");
199 dbaccess = res_get (zh->global_res, "dbaccess");
201 if (!passwd_plain && !passwd_encrypt)
202 zh->passwd_db = NULL;
205 zh->passwd_db = passwd_db_open();
207 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
211 passwd_db_file_plain(zh->passwd_db, passwd_plain);
213 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
220 zh->dbaccess = res_open(NULL, NULL);
221 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
222 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
227 zh->path_root = res_get (zh->global_res, "root");
228 zh->nmem = nmem_create();
229 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
235 void zebra_filter_info(ZebraService zs, void *cd,
236 void (*cb)(void *cd, const char *name))
240 recTypeClass_info(zs->record_classes, cd, cb);
243 void zebra_pidfname(ZebraService zs, char *path)
246 zebra_lock_prefix (zs->global_res, path);
247 strcat(path, "zebrasrv.pid");
250 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
251 int compact_flag, Res res)
253 int page_size = 4096;
254 char resource_str[200];
255 sprintf (resource_str, "dict.%.100s.pagesize", name);
259 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
260 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
262 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
266 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
267 int rw, int useshadow, Res res,
268 const char *reg_path)
270 struct zebra_register *reg;
271 int record_compression = REC_COMPRESS_NONE;
272 const char *recordCompression = 0;
273 const char *profilePath;
278 reg = xmalloc(sizeof(*reg));
281 reg->name = xstrdup (name);
288 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
289 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
291 reg->dh = data1_createx (DATA1_FLAG_XML);
297 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
300 data1_destroy(reg->dh);
306 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
308 bfs_destroy(reg->bfs);
309 data1_destroy(reg->dh);
315 getcwd(cwd, sizeof(cwd)-1);
316 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
317 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
319 data1_set_tabpath (reg->dh, profilePath);
320 data1_set_tabroot (reg->dh, reg_path);
321 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
323 reg->zebra_maps = zebra_maps_open (res, reg_path);
324 reg->rank_classes = NULL;
328 reg->keys = zebra_rec_keys_open();
331 reg->sortKeys = zebra_rec_keys_open();
333 reg->sortKeys.buf = 0;
334 reg->sortKeys.buf_max = 0;
346 reg->key_file_no = 0;
349 zebraRankInstall (reg, rank_1_class);
350 zebraRankInstall (reg, rank_zv_class);
351 zebraRankInstall (reg, rank_static_class);
353 recordCompression = res_get_def (res, "recordCompression", "none");
354 if (!strcmp (recordCompression, "none"))
355 record_compression = REC_COMPRESS_NONE;
356 if (!strcmp (recordCompression, "bzip2"))
357 record_compression = REC_COMPRESS_BZIP2;
359 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
361 yaz_log (YLOG_WARN, "rec_open failed");
366 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
368 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
370 yaz_log (YLOG_WARN, "dict_open failed");
373 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
375 yaz_log (YLOG_WARN, "sortIdx_open failed");
378 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
380 struct ISAMS_M_s isams_m;
381 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
382 key_isams_m(res, &isams_m))))
384 yaz_log (YLOG_WARN, "isams_open failed");
388 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
390 struct ISAMC_M_s isamc_m;
391 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
392 rw, key_isamc_m(res, &isamc_m))))
394 yaz_log (YLOG_WARN, "isamc_open failed");
398 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
400 struct ISAMC_M_s isamc_m;
402 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
403 rw, key_isamc_m(res, &isamc_m), 0)))
405 yaz_log (YLOG_WARN, "isamb_open failed");
409 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
411 struct ISAMC_M_s isamc_m;
413 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
414 rw, key_isamc_m(res, &isamc_m), 1)))
416 yaz_log (YLOG_WARN, "isamb_open failed");
420 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
422 struct ISAMC_M_s isamc_m;
424 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
425 rw, key_isamc_m(res, &isamc_m), -1)))
427 yaz_log (YLOG_WARN, "isamb_open failed");
431 reg->zei = zebraExplain_open (reg->records, reg->dh,
436 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
440 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
444 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
447 yaz_log(log_level, "zebra_admin_shutdown");
449 zebra_mutex_cond_lock (&zh->service->session_lock);
450 zh->service->stop_flag = 1;
451 zebra_mutex_cond_unlock (&zh->service->session_lock);
455 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
459 yaz_log(log_level, "zebra_admin_start");
461 zebra_mutex_cond_lock (&zs->session_lock);
462 zebra_mutex_cond_unlock (&zs->session_lock);
466 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
470 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
475 zebraExplain_close (reg->zei);
476 dict_close (reg->dict);
478 dict_close (reg->matchDict);
479 sortIdx_close (reg->sortIdx);
481 isams_close (reg->isams);
483 isamc_close (reg->isamc);
485 isamb_close (reg->isamb);
486 rec_close (®->records);
489 recTypes_destroy (reg->recTypes);
490 zebra_maps_close (reg->zebra_maps);
491 zebraRankDestroy (reg);
492 bfs_destroy (reg->bfs);
493 data1_destroy (reg->dh);
495 zebra_rec_keys_close(reg->keys);
497 zebra_rec_keys_close(reg->sortKeys);
499 xfree(reg->sortKeys.buf);
507 ZEBRA_RES zebra_stop(ZebraService zs)
511 yaz_log (log_level, "zebra_stop");
515 zebra_close (zs->sessions);
518 zebra_mutex_cond_destroy (&zs->session_lock);
521 passwd_db_close (zs->passwd_db);
523 recTypeClass_destroy(zs->record_classes);
524 nmem_destroy(zs->nmem);
525 res_close (zs->global_res);
530 ZEBRA_RES zebra_close (ZebraHandle zh)
533 struct zebra_session **sp;
536 yaz_log(log_level, "zebra_close");
543 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
544 resultSetDestroy (zh, -1, 0, 0);
547 zebra_register_close (zh->service, zh->reg);
548 zebra_close_res (zh);
549 res_close(zh->session_res);
551 xfree(zh->record_encoding);
553 xfree(zh->dbaccesslist);
555 for (i = 0; i < zh->num_basenames; i++)
556 xfree(zh->basenames[i]);
557 xfree(zh->basenames);
559 if (zh->iconv_to_utf8 != 0)
560 yaz_iconv_close (zh->iconv_to_utf8);
561 if (zh->iconv_from_utf8 != 0)
562 yaz_iconv_close (zh->iconv_from_utf8);
564 zebra_mutex_cond_lock (&zs->session_lock);
565 zebra_lock_destroy (zh->lock_normal);
566 zebra_lock_destroy (zh->lock_shadow);
578 zebra_mutex_cond_unlock (&zs->session_lock);
580 xfree(zh->user_perm);
581 zh->service = 0; /* more likely to trigger an assert */
583 zebra_limit_destroy(zh->m_limit);
585 nmem_destroy(zh->nmem_error);
592 struct map_baseinfo {
598 char **new_basenames;
602 static void zebra_open_res(ZebraHandle zh)
610 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
611 zh->res = res_open(zh->session_res, 0);
612 res_read_file(zh->res, fname);
614 else if (*zh->reg_name == 0)
616 zh->res = res_open(zh->session_res, 0);
620 yaz_log (YLOG_WARN, "no register root specified");
621 zh->res = 0; /* no path for register - fail! */
625 static void zebra_close_res (ZebraHandle zh)
633 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
637 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
641 assert (zh->reg == 0);
642 assert (*zh->reg_name == 0);
648 resultSetInvalidate (zh);
649 zebra_register_close (zh->service, zh->reg);
655 zh->reg_name = xstrdup (new_reg);
659 if (zh->service->path_root)
661 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
662 strlen(zh->reg_name) + 3);
663 strcpy (zh->path_reg, zh->service->path_root);
666 strcat (zh->path_reg, "/");
667 strcat (zh->path_reg, zh->reg_name);
673 zebra_lock_destroy (zh->lock_normal);
677 zebra_lock_destroy (zh->lock_shadow);
683 const char *lock_area = res_get (zh->res, "lockDir");
685 if (!lock_area && zh->path_reg)
686 res_set (zh->res, "lockDir", zh->path_reg);
687 sprintf (fname, "norm.%s.LCK", zh->reg_name);
689 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
691 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
693 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
695 if (!zh->lock_normal || !zh->lock_shadow)
699 zebra_lock_destroy(zh->lock_normal);
704 zebra_lock_destroy(zh->lock_shadow);
713 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
714 zebra_set_approx_limit(zh, approx);
718 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
719 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
721 yaz_log(YLOG_LOG, "static rank unset");
725 void map_basenames_func (void *vp, const char *name, const char *value)
727 struct map_baseinfo *p = (struct map_baseinfo *) vp;
729 char fromdb[128], todb[8][128];
736 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
737 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
738 todb[5], todb[6], todb[7]);
742 for (i = 0; i<p->num_bases; i++)
743 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
746 for (i = 0; i < no; i++)
748 if (p->new_num_bases == p->new_num_max)
750 p->new_basenames[(p->new_num_bases)++] =
751 nmem_strdup (p->mem, todb[i]);
757 int zebra_select_default_database(ZebraHandle zh)
761 /* no database has been selected - so we select based on
762 resource setting (including group)
764 const char *group = res_get(zh->session_res, "group");
765 const char *v = res_get_prefix(zh->session_res,
766 "database", group, "Default");
767 return zebra_select_database(zh, v);
772 void map_basenames (ZebraHandle zh, ODR stream,
773 int *num_bases, char ***basenames)
775 struct map_baseinfo info;
776 struct map_baseinfo *p = &info;
779 yaz_log(log_level, "map_basenames ");
784 info.num_bases = *num_bases;
785 info.basenames = *basenames;
786 info.new_num_max = 128;
787 info.new_num_bases = 0;
788 info.new_basenames = (char **)
789 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
790 info.mem = stream->mem;
792 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
794 for (i = 0; i<p->num_bases; i++)
795 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
797 p->new_basenames[(p->new_num_bases)++] =
798 nmem_strdup (p->mem, p->basenames[i]);
800 *num_bases = info.new_num_bases;
801 *basenames = info.new_basenames;
802 for (i = 0; i<*num_bases; i++)
803 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
806 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
809 yaz_log(log_level, "zebra_select_database %s",basename);
811 return zebra_select_databases (zh, 1, &basename);
814 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
815 const char **basenames)
824 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
825 num_bases,basenames[0]);
830 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
834 /* Check if the user has access to all databases (Seb) */
835 /* You could argue that this should happen later, after we have
836 * determined that the database(s) exist. */
837 if (zh->dbaccesslist) {
838 for (i = 0; i < num_bases; i++) {
839 const char *db = basenames[i];
841 for (p = zh->dbaccesslist; p && *p; p = pp) {
843 if ((pp = strchr(p, '+'))) {
849 if (len == strlen(db) && !strncmp(db, p, len))
853 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
859 for (i = 0; i < zh->num_basenames; i++)
860 xfree(zh->basenames[i]);
861 xfree(zh->basenames);
863 zh->num_basenames = num_bases;
864 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
865 for (i = 0; i < zh->num_basenames; i++)
866 zh->basenames[i] = xstrdup (basenames[i]);
868 cp = strrchr(basenames[0], '/');
871 len = cp - basenames[0];
872 new_reg = xmalloc(len + 1);
873 memcpy (new_reg, basenames[0], len);
877 new_reg = xstrdup ("");
878 for (i = 1; i<num_bases; i++)
882 cp1 = strrchr (basenames[i], '/');
887 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
890 if (len != cp1 - basenames[i] ||
891 memcmp (basenames[i], new_reg, len))
893 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
901 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
906 zebra_select_register (zh, new_reg);
910 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
913 if (!zh->lock_normal || !zh->lock_shadow)
915 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
921 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
923 if (approx_limit == 0)
924 approx_limit = DEFAULT_APPROX_LIMIT;
925 zh->approx_limit = approx_limit;
929 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
930 const char *setname, zint *hits)
938 yaz_log(log_level, "zebra_search_rpn");
942 if (zebra_begin_read(zh) == ZEBRA_FAIL)
945 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
946 zh->num_basenames, zh->basenames, setname);
952 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
954 Z_RecordComposition *comp,
955 oid_value input_format, int num_recs,
956 ZebraRetrievalRecord *recs)
958 ZebraMetaRecord *poset;
960 ZEBRA_RES ret = ZEBRA_OK;
968 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
972 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
977 if (zebra_begin_read (zh) == ZEBRA_FAIL)
980 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
981 for (i = 0; i<num_recs; i++)
982 pos_array[i] = recs[i].position;
983 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
986 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
987 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
993 for (i = 0; i<num_recs; i++)
998 recs[i].format = VAL_SUTRS;
999 recs[i].len = strlen(poset[i].term);
1000 recs[i].buf = poset[i].term;
1001 recs[i].base = poset[i].db;
1003 else if (poset[i].sysno)
1007 zebra_snippets *hit_snippet = zebra_snippets_create();
1009 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1013 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1015 stream, input_format, comp,
1016 &recs[i].format, &buf, &len,
1017 &recs[i].base, &recs[i].errString);
1022 recs[i].buf = (char*) odr_malloc(stream, len);
1023 memcpy(recs[i].buf, buf, len);
1027 recs[i].score = poset[i].score;
1028 recs[i].sysno = poset[i].sysno;
1029 zebra_snippets_destroy(hit_snippet);
1033 /* only need to set it once */
1034 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1036 zebra_setError_zint(zh,
1037 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1042 recs[i].buf = 0; /* no record and no error issued */
1044 recs[i].errCode = 0;
1045 recs[i].format = VAL_NONE;
1049 zebra_meta_records_destroy(zh, poset, num_recs);
1051 zebra_end_read (zh);
1056 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1058 int *num_entries, ZebraScanEntry **entries,
1060 const char *setname)
1062 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1063 Z_AttributesPlusTerm *zapt;
1067 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1070 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1073 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1074 position, num_entries, entries, is_partial,
1076 yaz_pqf_destroy (pqf_parser);
1080 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1081 oid_value attributeset,
1083 int *num_entries, ZebraScanEntry **entries,
1085 const char *setname)
1088 RSET limit_rset = 0;
1093 assert(num_entries);
1096 yaz_log(log_level, "zebra_scan");
1098 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1106 limit_rset = resultSetRef(zh, setname);
1110 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1112 zebra_end_read (zh);
1116 res = rpn_scan (zh, stream, zapt, attributeset,
1117 zh->num_basenames, zh->basenames, position,
1118 num_entries, entries, is_partial, limit_rset, 0);
1119 zebra_end_read (zh);
1123 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1124 int num_input_setnames, const char **input_setnames,
1125 const char *output_setname,
1126 Z_SortKeySpecList *sort_sequence,
1132 assert(num_input_setnames>0);
1133 assert(input_setnames);
1134 assert(sort_sequence);
1135 assert(sort_status);
1136 yaz_log(log_level, "zebra_sort");
1138 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1140 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1141 output_setname, sort_sequence, sort_status);
1146 int zebra_deleteResultSet(ZebraHandle zh, int function,
1147 int num_setnames, char **setnames,
1153 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1155 if (zebra_begin_read(zh))
1156 return Z_DeleteStatus_systemProblemAtTarget;
1159 case Z_DeleteResultSetRequest_list:
1160 assert(num_setnames>0);
1162 resultSetDestroy (zh, num_setnames, setnames, statuses);
1164 case Z_DeleteResultSetRequest_all:
1165 resultSetDestroy (zh, -1, 0, statuses);
1168 zebra_end_read (zh);
1169 status = Z_DeleteStatus_success;
1170 for (i = 0; i<num_setnames; i++)
1171 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1172 status = statuses[i];
1176 int zebra_errCode (ZebraHandle zh)
1180 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1183 yaz_log(log_level, "zebra_errCode: o");
1187 const char *zebra_errString (ZebraHandle zh)
1191 e= diagbib1_str (zh->errCode);
1192 yaz_log(log_level, "zebra_errString: %s",e);
1196 char *zebra_errAdd (ZebraHandle zh)
1201 yaz_log(log_level, "zebra_errAdd: %s",a);
1205 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1208 const char *astring;
1216 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1217 p = res_get(zs->global_res, u);
1218 xfree(zh->user_perm);
1219 zh->user_perm = xstrdup(p ? p : "r");
1221 /* Determine database access list */
1222 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1224 zh->dbaccesslist = xstrdup(astring);
1226 zh->dbaccesslist = 0;
1228 /* users that don't require a password .. */
1229 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1232 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1237 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1238 const char *record_type)
1241 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1242 database, record_type);
1243 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1245 return zebra_begin_trans(zh, 1);
1248 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1251 yaz_log(log_level, "zebra_admin_import_end");
1252 return zebra_end_trans(zh);
1255 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1257 ZEBRA_RES res = ZEBRA_OK;
1261 yaz_log(log_level, "zebra_admin_import_segment");
1263 for (i = 0; i<segment->num_segmentRecords; i++)
1265 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1267 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1269 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1270 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1272 Odr_oct *oct = fragment->u.notExternallyTagged;
1275 if (zebra_update_record(zh,
1276 0, /* record Type */
1280 (const char *) oct->buf, oct->len,
1289 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1290 const char *rec_buf,
1292 const char *recid_buf, size_t recid_len,
1294 /* 1 = insert. Fail it already exists */
1295 /* 2 = replace. Fail it does not exist */
1296 /* 3 = delete. Fail if does not exist */
1297 /* 4 = update. Insert/replace */
1304 assert(action>0 && action <=4);
1307 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1309 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1311 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1312 "no record ID or empty record ID");
1316 memcpy (recid_z, recid_buf, recid_len);
1317 recid_z[recid_len] = 0;
1319 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1322 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1325 if (action == 1) /* fail if insert */
1327 zebra_end_trans(zh);
1328 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1329 "Cannot insert record: already exist");
1333 memcpy (&sysno, rinfo+1, sizeof(sysno));
1337 if (action == 2 || action == 3) /* fail if delete or update */
1339 zebra_end_trans(zh);
1340 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1341 "Cannot delete/update record: does not exist");
1344 action = 1; /* make it an insert (if it's an update).. */
1346 res = buffer_extract_record (zh, rec_buf, rec_len,
1347 action == 3 ? 1 : 0 /* delete flag */,
1353 0, /* force update */
1354 1 /* allow update */
1356 if (res == ZEBRA_FAIL)
1358 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1359 "Unable to parse record");
1363 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1365 else if (action == 3)
1367 dict_delete (zh->reg->matchDict, recid_z);
1369 zebra_end_trans(zh);
1373 int delete_w_handle(const char *info, void *handle)
1375 ZebraHandle zh = (ZebraHandle) handle;
1379 if (*info == sizeof(pos))
1381 memcpy (&pos, info+1, sizeof(pos));
1382 isamb_unlink(zh->reg->isamb, pos);
1387 static int delete_SU_handle(void *handle, int ord)
1389 ZebraHandle zh = (ZebraHandle) handle;
1393 ord_len = key_SU_encode (ord, ord_buf);
1394 ord_buf[ord_len] = '\0';
1396 assert (zh->reg->isamb);
1397 dict_delete_subtree(zh->reg->dict, ord_buf,
1398 zh, delete_w_handle);
1402 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1404 ZEBRA_RES ret = ZEBRA_OK;
1406 yaz_log(log_level, "zebra_drop_database %s", db);
1408 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1410 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1414 zebraExplain_curDatabase (zh->reg->zei, db);
1416 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1417 zebraExplain_removeDatabase(zh->reg->zei, zh);
1421 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1422 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1423 "drop database only supported for isam:b");
1426 zebra_end_trans (zh);
1430 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1433 yaz_log(log_level, "zebra_create_database %s", db);
1436 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1438 if (zebra_begin_trans (zh, 1))
1441 /* announce database */
1442 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1443 /* explainDatabase */))
1445 zebra_end_trans (zh);
1446 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1449 return zebra_end_trans (zh);
1452 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1453 const char *input_str, int input_len,
1454 char *output_str, int output_len)
1460 yaz_log(log_level, "zebra_string_norm ");
1462 if (!zh->reg->zebra_maps)
1464 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1465 input_str, input_len);
1468 if (wrbuf_len(wrbuf) >= output_len)
1470 if (wrbuf_len(wrbuf))
1471 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1472 output_str[wrbuf_len(wrbuf)] = '\0';
1473 return wrbuf_len(wrbuf);
1476 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1478 char state_fname[256];
1483 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1485 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1486 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1487 f = fopen (fname, "w");
1489 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1490 fprintf (f, "%c %d %ld\n", val, seqno, p);
1495 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1497 char state_fname[256];
1502 yaz_log(log_level, "zebra_get_state ");
1504 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1505 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1506 f = fopen (fname, "r");
1512 fscanf (f, "%c %d", val, seqno);
1518 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1520 return zebra_begin_trans(zh, 0);
1523 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1525 return zebra_end_trans(zh);
1528 static void read_res_for_transaction(ZebraHandle zh)
1530 const char *group = res_get(zh->res, "group");
1532 /* FIXME - do we still use groups ?? */
1534 zh->m_group = group;
1535 v = res_get_prefix(zh->res, "followLinks", group, "1");
1536 zh->m_follow_links = atoi(v);
1538 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1539 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1541 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1542 zh->m_store_keys = atoi(v);
1544 v = res_get_prefix(zh->res, "storeData", group, "1");
1545 zh->m_store_data = atoi(v);
1547 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1548 zh->m_explain_database = atoi(v);
1550 v = res_get_prefix(zh->res, "openRW", group, "1");
1551 zh->m_flag_rw = atoi(v);
1553 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1554 zh->m_file_verbose_limit = atoi(v);
1557 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1560 zebra_select_default_database(zh);
1563 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1564 "zebra_begin_trans: no database selected");
1568 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1572 if (rw && !strchr(zh->user_perm, 'w'))
1576 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1588 const char *rval = 0;
1593 read_res_for_transaction(zh);
1596 if (zh->trans_no != 1)
1598 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1599 "zebra_begin_trans: no write trans within read");
1604 resultSetInvalidate (zh);
1605 zebra_register_close (zh->service, zh->reg);
1607 zh->trans_w_no = zh->trans_no;
1609 zh->records_inserted = 0;
1610 zh->records_updated = 0;
1611 zh->records_deleted = 0;
1612 zh->records_processed = 0;
1614 #if HAVE_SYS_TIMES_H
1618 if (zh->shadow_enable)
1619 rval = res_get (zh->res, "shadow");
1621 for (pass = 0; pass < 2; pass++)
1625 zebra_lock_r (zh->lock_normal);
1626 zebra_lock_w (zh->lock_shadow);
1630 zebra_lock_w (zh->lock_normal);
1631 zebra_lock_w (zh->lock_shadow);
1634 zebra_get_state (zh, &val, &seqno);
1637 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1638 zebra_unlock (zh->lock_shadow);
1639 zebra_unlock (zh->lock_normal);
1643 else if (val == 'd')
1647 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1649 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1650 bf_commitClean (bfs, rval);
1655 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1662 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1666 zebra_set_state (zh, 'd', seqno);
1668 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1669 1, rval ? 1 : 0, zh->res,
1672 zh->reg->seqno = seqno;
1675 zebra_set_state (zh, 'o', seqno);
1677 zebra_unlock (zh->lock_shadow);
1678 zebra_unlock (zh->lock_normal);
1683 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1684 "zebra_begin_trans: cannot open register");
1685 yaz_log(YLOG_FATAL, "%s", zh->errString);
1697 if (zh->trans_no != 1)
1699 zebra_flush_reg (zh);
1702 #if HAVE_SYS_TIMES_H
1708 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1711 if (!zh->lock_normal || !zh->lock_shadow)
1714 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1717 zebra_get_state (zh, &val, &seqno);
1723 else if (seqno != zh->reg->seqno)
1725 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1726 seqno, zh->reg->seqno);
1729 else if (zh->reg->last_val != val)
1731 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1732 val, zh->reg->last_val);
1739 zebra_lock_r (zh->lock_shadow);
1741 zebra_lock_r (zh->lock_normal);
1745 resultSetInvalidate (zh);
1746 zebra_register_close (zh->service, zh->reg);
1748 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1749 0, val == 'c' ? 1 : 0,
1750 zh->res, zh->path_reg);
1753 zebra_unlock (zh->lock_normal);
1754 zebra_unlock (zh->lock_shadow);
1756 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1759 zh->reg->last_val = val;
1760 zh->reg->seqno = seqno;
1762 read_res_for_transaction(zh);
1766 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1768 ZebraTransactionStatus dummy;
1770 yaz_log(log_level, "zebra_end_trans");
1771 return zebra_end_transaction(zh, &dummy);
1774 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1782 yaz_log(log_level, "zebra_end_transaction");
1784 status->processed = 0;
1785 status->inserted = 0;
1786 status->updated = 0;
1787 status->deleted = 0;
1791 if (!zh->res || !zh->reg)
1793 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1794 "zebra_end_trans: no open transaction");
1797 if (zh->trans_no != zh->trans_w_no)
1800 if (zh->trans_no != 0)
1803 /* release read lock */
1805 zebra_unlock (zh->lock_normal);
1806 zebra_unlock (zh->lock_shadow);
1809 { /* release write lock */
1813 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1814 rval = res_get (zh->res, "shadow");
1816 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1818 zebra_flush_reg (zh);
1820 resultSetInvalidate (zh);
1822 zebra_register_close (zh->service, zh->reg);
1825 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1826 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1827 zh->records_processed, zh->records_inserted,
1828 zh->records_updated, zh->records_deleted);
1830 status->processed = (int) zh->records_processed;
1831 status->inserted = (int) zh->records_inserted;
1832 status->updated = (int) zh->records_updated;
1833 status->deleted = (int) zh->records_deleted;
1835 zebra_get_state (zh, &val, &seqno);
1838 BFiles bfs = bfs_create (rval, zh->path_reg);
1839 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1840 bf_commitClean (bfs, rval);
1845 zebra_set_state (zh, 'o', seqno);
1847 zebra_unlock (zh->lock_shadow);
1848 zebra_unlock (zh->lock_normal);
1851 #if HAVE_SYS_TIMES_H
1853 yaz_log (log_level, "user/system: %ld/%ld",
1854 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1855 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1857 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1858 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1863 int zebra_repository_update (ZebraHandle zh, const char *path)
1867 yaz_log (log_level, "updating %s", path);
1868 repositoryUpdate (zh, path);
1872 int zebra_repository_delete (ZebraHandle zh, const char *path)
1876 yaz_log (log_level, "deleting %s", path);
1877 repositoryDelete (zh, path);
1881 int zebra_repository_show (ZebraHandle zh, const char *path)
1885 yaz_log(log_level, "zebra_repository_show");
1886 repositoryShow (zh, path);
1890 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1898 zebra_select_default_database(zh);
1901 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1904 rval = res_get (zh->res, "shadow");
1907 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1911 zebra_lock_w (zh->lock_normal);
1912 zebra_lock_r (zh->lock_shadow);
1914 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1916 zebra_get_state (zh, &val, &seqno);
1919 bf_cache (bfs, rval);
1920 if (bf_commitExists (bfs))
1923 zebra_set_state (zh, 'd', seqno);
1926 zebra_set_state (zh, 'c', seqno);
1928 yaz_log (YLOG_DEBUG, "commit start");
1929 bf_commitExec (bfs);
1934 yaz_log (YLOG_DEBUG, "commit clean");
1935 bf_commitClean (bfs, rval);
1937 zebra_set_state (zh, 'o', seqno);
1941 yaz_log (log_level, "nothing to commit");
1945 zebra_unlock (zh->lock_shadow);
1946 zebra_unlock (zh->lock_normal);
1950 ZEBRA_RES zebra_clean(ZebraHandle zh)
1953 yaz_log(log_level, "zebra_clean");
1954 return zebra_commit_ex(zh, 1);
1957 ZEBRA_RES zebra_commit(ZebraHandle zh)
1960 yaz_log(log_level, "zebra_commit");
1961 return zebra_commit_ex(zh, 0);
1964 ZEBRA_RES zebra_init(ZebraHandle zh)
1969 yaz_log(log_level, "zebra_init");
1971 zebra_select_default_database(zh);
1974 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1975 "cannot select default database");
1978 rval = res_get (zh->res, "shadow");
1980 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1983 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1987 bf_cache (bfs, rval);
1991 zebra_set_state (zh, 'o', 0);
1995 ZEBRA_RES zebra_compact(ZebraHandle zh)
1999 yaz_log(log_level, "zebra_compact");
2002 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2005 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2011 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2014 yaz_log(log_level, "zebra_result");
2015 *code = zh->errCode;
2016 *addinfo = zh->errString;
2019 void zebra_shadow_enable(ZebraHandle zh, int value)
2022 yaz_log(log_level, "zebra_shadow_enable");
2023 zh->shadow_enable = value;
2026 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2030 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2032 if (zh->iconv_to_utf8 != 0)
2033 yaz_iconv_close(zh->iconv_to_utf8);
2034 if (zh->iconv_from_utf8 != 0)
2035 yaz_iconv_close(zh->iconv_from_utf8);
2038 yaz_iconv_open ("UTF-8", encoding);
2039 if (zh->iconv_to_utf8 == 0)
2040 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2041 zh->iconv_from_utf8 =
2042 yaz_iconv_open (encoding, "UTF-8");
2043 if (zh->iconv_to_utf8 == 0)
2044 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2049 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2052 yaz_log(log_level, "zebra_record_encoding");
2053 xfree(zh->record_encoding);
2054 zh->record_encoding = 0;
2056 zh->record_encoding = xstrdup (encoding);
2060 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2065 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2066 res_set(zh->res, name, value);
2069 const char *zebra_get_resource(ZebraHandle zh,
2070 const char *name, const char *defaultvalue)
2075 v = res_get_def (zh->res, name, (char *)defaultvalue);
2076 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2080 /* moved from zebra_api_ext.c by pop */
2081 /* FIXME: Should this really be public??? -Heikki */
2083 int zebra_trans_no (ZebraHandle zh)
2086 yaz_log(log_level, "zebra_trans_no");
2087 return zh->trans_no;
2090 int zebra_get_shadow_enable (ZebraHandle zh)
2093 yaz_log(log_level, "zebra_get_shadow_enable");
2094 return zh->shadow_enable;
2097 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2100 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2101 zh->shadow_enable = value;
2104 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2105 so that it's identicical to the original api_records_retrieve */
2106 void api_records_retrieve (ZebraHandle zh, ODR stream,
2107 const char *setname, Z_RecordComposition *comp,
2108 oid_value input_format, int num_recs,
2109 ZebraRetrievalRecord *recs)
2111 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2115 /* ---------------------------------------------------------------------------
2116 Record insert(=update), delete
2118 If sysno is provided, then it's used to identify the record.
2119 If not, and match_criteria is provided, then sysno is guessed
2120 If not, and a record is provided, then sysno is got from there
2121 NOTE: Now returns 0 at success and updates sysno, which is an int*
2125 int zebra_add_record(ZebraHandle zh,
2126 const char *buf, int buf_size)
2128 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2131 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2132 const char *recordType,
2133 SYSNO *sysno, const char *match,
2135 const char *buf, int buf_size, int force_update)
2141 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2144 buf_size = strlen(buf);
2146 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2148 res = buffer_extract_record (zh, buf, buf_size,
2149 0, /* delete_flag */
2155 0); /* allow_update */
2156 zebra_end_trans(zh);
2160 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2161 const char *recordType,
2162 SYSNO* sysno, const char *match,
2164 const char *buf, int buf_size,
2171 yaz_log(log_level, "zebra_update_record");
2173 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2175 if (buf_size < 1) buf_size = strlen(buf);
2177 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2179 res = buffer_extract_record (zh, buf, buf_size,
2180 0, /* delete_flag */
2186 1); /* allow_update */
2187 zebra_end_trans(zh);
2191 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2192 const char *recordType,
2193 SYSNO *sysno, const char *match,
2195 const char *buf, int buf_size,
2202 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2204 if (buf_size < 1) buf_size = strlen(buf);
2206 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2208 res = buffer_extract_record (zh, buf, buf_size,
2209 1, /* delete_flag */
2215 1); /* allow_update */
2216 zebra_end_trans(zh);
2220 /* ---------------------------------------------------------------------------
2224 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2225 const char *setname, zint *hits)
2228 ZEBRA_RES res = ZEBRA_OK;
2230 ODR odr = odr_createmem(ODR_ENCODE);
2235 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2237 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2241 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2242 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2246 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2250 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2258 /* ---------------------------------------------------------------------------
2259 Sort - a simplified interface, with optional read locks.
2261 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2262 const char *sort_spec,
2263 const char *output_setname,
2264 const char **input_setnames)
2266 int num_input_setnames = 0;
2267 int sort_status = 0;
2268 Z_SortKeySpecList *sort_sequence;
2272 assert(output_setname);
2273 assert(input_setnames);
2274 sort_sequence = yaz_sort_spec (stream, sort_spec);
2275 yaz_log(log_level, "sort (FIXME) ");
2278 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2279 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2283 /* we can do this, since the perl typemap code for char** will
2284 put a NULL at the end of list */
2285 while (input_setnames[num_input_setnames]) num_input_setnames++;
2287 if (zebra_begin_read (zh))
2290 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2291 output_setname, sort_sequence, &sort_status);
2297 /* ---------------------------------------------------------------------------
2298 Get BFS for Zebra system (to make alternative storage methods)
2300 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2303 return zh->reg->bfs;
2308 /* ---------------------------------------------------------------------------
2309 Set limit for search/scan
2311 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2314 zebra_limit_destroy(zh->m_limit);
2315 zh->m_limit = zebra_limit_create(complement_flag, ids);
2320 Set Error code + addinfo
2322 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2325 nmem_reset(zh->nmem_error);
2326 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2329 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2332 sprintf(vstr, ZINT_FORMAT, i);
2335 nmem_reset(zh->nmem_error);
2336 zh->errString = nmem_strdup(zh->nmem_error, vstr);