1 /* $Id: zebraapi.c,v 1.200 2006-01-19 13:30:02 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;
345 reg->key_file_no = 0;
348 zebraRankInstall (reg, rank_1_class);
349 zebraRankInstall (reg, rank_zv_class);
350 zebraRankInstall (reg, rank_static_class);
352 recordCompression = res_get_def (res, "recordCompression", "none");
353 if (!strcmp (recordCompression, "none"))
354 record_compression = REC_COMPRESS_NONE;
355 if (!strcmp (recordCompression, "bzip2"))
356 record_compression = REC_COMPRESS_BZIP2;
358 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
360 yaz_log (YLOG_WARN, "rec_open failed");
365 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
367 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
369 yaz_log (YLOG_WARN, "dict_open failed");
372 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
374 yaz_log (YLOG_WARN, "sortIdx_open failed");
377 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
379 struct ISAMS_M_s isams_m;
380 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
381 key_isams_m(res, &isams_m))))
383 yaz_log (YLOG_WARN, "isams_open failed");
387 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
389 struct ISAMC_M_s isamc_m;
390 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
391 rw, key_isamc_m(res, &isamc_m))))
393 yaz_log (YLOG_WARN, "isamc_open failed");
397 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
399 struct ISAMC_M_s isamc_m;
401 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
402 rw, key_isamc_m(res, &isamc_m), 0)))
404 yaz_log (YLOG_WARN, "isamb_open failed");
408 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
410 struct ISAMC_M_s isamc_m;
412 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
413 rw, key_isamc_m(res, &isamc_m), 1)))
415 yaz_log (YLOG_WARN, "isamb_open failed");
419 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
421 struct ISAMC_M_s isamc_m;
423 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
424 rw, key_isamc_m(res, &isamc_m), -1)))
426 yaz_log (YLOG_WARN, "isamb_open failed");
430 reg->zei = zebraExplain_open (reg->records, reg->dh,
435 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
439 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
443 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
446 yaz_log(log_level, "zebra_admin_shutdown");
448 zebra_mutex_cond_lock (&zh->service->session_lock);
449 zh->service->stop_flag = 1;
450 zebra_mutex_cond_unlock (&zh->service->session_lock);
454 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
458 yaz_log(log_level, "zebra_admin_start");
460 zebra_mutex_cond_lock (&zs->session_lock);
461 zebra_mutex_cond_unlock (&zs->session_lock);
465 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
469 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
474 zebraExplain_close (reg->zei);
475 dict_close (reg->dict);
477 dict_close (reg->matchDict);
478 sortIdx_close (reg->sortIdx);
480 isams_close (reg->isams);
482 isamc_close (reg->isamc);
484 isamb_close (reg->isamb);
485 rec_close (®->records);
488 recTypes_destroy (reg->recTypes);
489 zebra_maps_close (reg->zebra_maps);
490 zebraRankDestroy (reg);
491 bfs_destroy (reg->bfs);
492 data1_destroy (reg->dh);
494 zebra_rec_keys_close(reg->keys);
496 zebra_rec_keys_close(reg->sortKeys);
498 xfree(reg->sortKeys.buf);
506 ZEBRA_RES zebra_stop(ZebraService zs)
510 yaz_log (log_level, "zebra_stop");
514 zebra_close (zs->sessions);
517 zebra_mutex_cond_destroy (&zs->session_lock);
520 passwd_db_close (zs->passwd_db);
522 recTypeClass_destroy(zs->record_classes);
523 nmem_destroy(zs->nmem);
524 res_close (zs->global_res);
529 ZEBRA_RES zebra_close (ZebraHandle zh)
532 struct zebra_session **sp;
535 yaz_log(log_level, "zebra_close");
542 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
543 resultSetDestroy (zh, -1, 0, 0);
546 zebra_register_close (zh->service, zh->reg);
547 zebra_close_res (zh);
548 res_close(zh->session_res);
550 xfree(zh->record_encoding);
552 xfree(zh->dbaccesslist);
554 for (i = 0; i < zh->num_basenames; i++)
555 xfree(zh->basenames[i]);
556 xfree(zh->basenames);
558 if (zh->iconv_to_utf8 != 0)
559 yaz_iconv_close (zh->iconv_to_utf8);
560 if (zh->iconv_from_utf8 != 0)
561 yaz_iconv_close (zh->iconv_from_utf8);
563 zebra_mutex_cond_lock (&zs->session_lock);
564 zebra_lock_destroy (zh->lock_normal);
565 zebra_lock_destroy (zh->lock_shadow);
577 zebra_mutex_cond_unlock (&zs->session_lock);
579 xfree(zh->user_perm);
580 zh->service = 0; /* more likely to trigger an assert */
582 zebra_limit_destroy(zh->m_limit);
584 nmem_destroy(zh->nmem_error);
591 struct map_baseinfo {
597 char **new_basenames;
601 static void zebra_open_res(ZebraHandle zh)
609 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
610 zh->res = res_open(zh->session_res, 0);
611 res_read_file(zh->res, fname);
613 else if (*zh->reg_name == 0)
615 zh->res = res_open(zh->session_res, 0);
619 yaz_log (YLOG_WARN, "no register root specified");
620 zh->res = 0; /* no path for register - fail! */
624 static void zebra_close_res (ZebraHandle zh)
632 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
636 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
640 assert (zh->reg == 0);
641 assert (*zh->reg_name == 0);
647 resultSetInvalidate (zh);
648 zebra_register_close (zh->service, zh->reg);
654 zh->reg_name = xstrdup (new_reg);
658 if (zh->service->path_root)
660 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
661 strlen(zh->reg_name) + 3);
662 strcpy (zh->path_reg, zh->service->path_root);
665 strcat (zh->path_reg, "/");
666 strcat (zh->path_reg, zh->reg_name);
672 zebra_lock_destroy (zh->lock_normal);
676 zebra_lock_destroy (zh->lock_shadow);
682 const char *lock_area = res_get (zh->res, "lockDir");
684 if (!lock_area && zh->path_reg)
685 res_set (zh->res, "lockDir", zh->path_reg);
686 sprintf (fname, "norm.%s.LCK", zh->reg_name);
688 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
690 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
692 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
694 if (!zh->lock_normal || !zh->lock_shadow)
698 zebra_lock_destroy(zh->lock_normal);
703 zebra_lock_destroy(zh->lock_shadow);
712 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
713 zebra_set_approx_limit(zh, approx);
717 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
718 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
720 yaz_log(YLOG_LOG, "static rank unset");
724 void map_basenames_func (void *vp, const char *name, const char *value)
726 struct map_baseinfo *p = (struct map_baseinfo *) vp;
728 char fromdb[128], todb[8][128];
735 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
736 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
737 todb[5], todb[6], todb[7]);
741 for (i = 0; i<p->num_bases; i++)
742 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
745 for (i = 0; i < no; i++)
747 if (p->new_num_bases == p->new_num_max)
749 p->new_basenames[(p->new_num_bases)++] =
750 nmem_strdup (p->mem, todb[i]);
756 int zebra_select_default_database(ZebraHandle zh)
760 /* no database has been selected - so we select based on
761 resource setting (including group)
763 const char *group = res_get(zh->session_res, "group");
764 const char *v = res_get_prefix(zh->session_res,
765 "database", group, "Default");
766 return zebra_select_database(zh, v);
771 void map_basenames (ZebraHandle zh, ODR stream,
772 int *num_bases, char ***basenames)
774 struct map_baseinfo info;
775 struct map_baseinfo *p = &info;
778 yaz_log(log_level, "map_basenames ");
783 info.num_bases = *num_bases;
784 info.basenames = *basenames;
785 info.new_num_max = 128;
786 info.new_num_bases = 0;
787 info.new_basenames = (char **)
788 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
789 info.mem = stream->mem;
791 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
793 for (i = 0; i<p->num_bases; i++)
794 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
796 p->new_basenames[(p->new_num_bases)++] =
797 nmem_strdup (p->mem, p->basenames[i]);
799 *num_bases = info.new_num_bases;
800 *basenames = info.new_basenames;
801 for (i = 0; i<*num_bases; i++)
802 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
805 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
808 yaz_log(log_level, "zebra_select_database %s",basename);
810 return zebra_select_databases (zh, 1, &basename);
813 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
814 const char **basenames)
823 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
824 num_bases,basenames[0]);
829 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
833 /* Check if the user has access to all databases (Seb) */
834 /* You could argue that this should happen later, after we have
835 * determined that the database(s) exist. */
836 if (zh->dbaccesslist) {
837 for (i = 0; i < num_bases; i++) {
838 const char *db = basenames[i];
840 for (p = zh->dbaccesslist; p && *p; p = pp) {
842 if ((pp = strchr(p, '+'))) {
848 if (len == strlen(db) && !strncmp(db, p, len))
852 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
858 for (i = 0; i < zh->num_basenames; i++)
859 xfree(zh->basenames[i]);
860 xfree(zh->basenames);
862 zh->num_basenames = num_bases;
863 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
864 for (i = 0; i < zh->num_basenames; i++)
865 zh->basenames[i] = xstrdup (basenames[i]);
867 cp = strrchr(basenames[0], '/');
870 len = cp - basenames[0];
871 new_reg = xmalloc(len + 1);
872 memcpy (new_reg, basenames[0], len);
876 new_reg = xstrdup ("");
877 for (i = 1; i<num_bases; i++)
881 cp1 = strrchr (basenames[i], '/');
886 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
889 if (len != cp1 - basenames[i] ||
890 memcmp (basenames[i], new_reg, len))
892 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
900 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
905 zebra_select_register (zh, new_reg);
909 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
912 if (!zh->lock_normal || !zh->lock_shadow)
914 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
920 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
922 if (approx_limit == 0)
923 approx_limit = DEFAULT_APPROX_LIMIT;
924 zh->approx_limit = approx_limit;
928 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
929 const char *setname, zint *hits)
937 yaz_log(log_level, "zebra_search_rpn");
941 if (zebra_begin_read(zh) == ZEBRA_FAIL)
944 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
945 zh->num_basenames, zh->basenames, setname);
951 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
953 Z_RecordComposition *comp,
954 oid_value input_format, int num_recs,
955 ZebraRetrievalRecord *recs)
957 ZebraMetaRecord *poset;
959 ZEBRA_RES ret = ZEBRA_OK;
967 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
971 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
976 if (zebra_begin_read (zh) == ZEBRA_FAIL)
979 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
980 for (i = 0; i<num_recs; i++)
981 pos_array[i] = recs[i].position;
982 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
985 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
986 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
992 for (i = 0; i<num_recs; i++)
997 recs[i].format = VAL_SUTRS;
998 recs[i].len = strlen(poset[i].term);
999 recs[i].buf = poset[i].term;
1000 recs[i].base = poset[i].db;
1002 else if (poset[i].sysno)
1006 zebra_snippets *hit_snippet = zebra_snippets_create();
1008 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1012 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1014 stream, input_format, comp,
1015 &recs[i].format, &buf, &len,
1016 &recs[i].base, &recs[i].errString);
1021 recs[i].buf = (char*) odr_malloc(stream, len);
1022 memcpy(recs[i].buf, buf, len);
1026 recs[i].score = poset[i].score;
1027 recs[i].sysno = poset[i].sysno;
1028 zebra_snippets_destroy(hit_snippet);
1032 /* only need to set it once */
1033 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1035 zebra_setError_zint(zh,
1036 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1041 recs[i].buf = 0; /* no record and no error issued */
1043 recs[i].errCode = 0;
1044 recs[i].format = VAL_NONE;
1048 zebra_meta_records_destroy(zh, poset, num_recs);
1050 zebra_end_read (zh);
1055 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1057 int *num_entries, ZebraScanEntry **entries,
1059 const char *setname)
1061 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1062 Z_AttributesPlusTerm *zapt;
1066 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1069 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1072 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1073 position, num_entries, entries, is_partial,
1075 yaz_pqf_destroy (pqf_parser);
1079 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1080 oid_value attributeset,
1082 int *num_entries, ZebraScanEntry **entries,
1084 const char *setname)
1087 RSET limit_rset = 0;
1092 assert(num_entries);
1095 yaz_log(log_level, "zebra_scan");
1097 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1105 limit_rset = resultSetRef(zh, setname);
1109 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1111 zebra_end_read (zh);
1115 res = rpn_scan (zh, stream, zapt, attributeset,
1116 zh->num_basenames, zh->basenames, position,
1117 num_entries, entries, is_partial, limit_rset, 0);
1118 zebra_end_read (zh);
1122 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1123 int num_input_setnames, const char **input_setnames,
1124 const char *output_setname,
1125 Z_SortKeySpecList *sort_sequence,
1131 assert(num_input_setnames>0);
1132 assert(input_setnames);
1133 assert(sort_sequence);
1134 assert(sort_status);
1135 yaz_log(log_level, "zebra_sort");
1137 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1139 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1140 output_setname, sort_sequence, sort_status);
1145 int zebra_deleteResultSet(ZebraHandle zh, int function,
1146 int num_setnames, char **setnames,
1152 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1154 if (zebra_begin_read(zh))
1155 return Z_DeleteStatus_systemProblemAtTarget;
1158 case Z_DeleteResultSetRequest_list:
1159 assert(num_setnames>0);
1161 resultSetDestroy (zh, num_setnames, setnames, statuses);
1163 case Z_DeleteResultSetRequest_all:
1164 resultSetDestroy (zh, -1, 0, statuses);
1167 zebra_end_read (zh);
1168 status = Z_DeleteStatus_success;
1169 for (i = 0; i<num_setnames; i++)
1170 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1171 status = statuses[i];
1175 int zebra_errCode (ZebraHandle zh)
1179 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1182 yaz_log(log_level, "zebra_errCode: o");
1186 const char *zebra_errString (ZebraHandle zh)
1190 e= diagbib1_str (zh->errCode);
1191 yaz_log(log_level, "zebra_errString: %s",e);
1195 char *zebra_errAdd (ZebraHandle zh)
1200 yaz_log(log_level, "zebra_errAdd: %s",a);
1204 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1207 const char *astring;
1215 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1216 p = res_get(zs->global_res, u);
1217 xfree(zh->user_perm);
1218 zh->user_perm = xstrdup(p ? p : "r");
1220 /* Determine database access list */
1221 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1223 zh->dbaccesslist = xstrdup(astring);
1225 zh->dbaccesslist = 0;
1227 /* users that don't require a password .. */
1228 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1231 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1236 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1237 const char *record_type)
1240 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1241 database, record_type);
1242 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1244 return zebra_begin_trans(zh, 1);
1247 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1250 yaz_log(log_level, "zebra_admin_import_end");
1251 return zebra_end_trans(zh);
1254 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1256 ZEBRA_RES res = ZEBRA_OK;
1260 yaz_log(log_level, "zebra_admin_import_segment");
1262 for (i = 0; i<segment->num_segmentRecords; i++)
1264 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1266 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1268 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1269 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1271 Odr_oct *oct = fragment->u.notExternallyTagged;
1274 if (zebra_update_record(zh,
1275 0, /* record Type */
1279 (const char *) oct->buf, oct->len,
1288 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
1289 const char *rec_buf,
1291 const char *recid_buf, size_t recid_len,
1293 /* 1 = insert. Fail it already exists */
1294 /* 2 = replace. Fail it does not exist */
1295 /* 3 = delete. Fail if does not exist */
1296 /* 4 = update. Insert/replace */
1303 assert(action>0 && action <=4);
1306 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1308 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1310 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1311 "no record ID or empty record ID");
1315 memcpy (recid_z, recid_buf, recid_len);
1316 recid_z[recid_len] = 0;
1318 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1321 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1324 if (action == 1) /* fail if insert */
1326 zebra_end_trans(zh);
1327 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1328 "Cannot insert record: already exist");
1332 memcpy (&sysno, rinfo+1, sizeof(sysno));
1336 if (action == 2 || action == 3) /* fail if delete or update */
1338 zebra_end_trans(zh);
1339 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1340 "Cannot delete/update record: does not exist");
1343 action = 1; /* make it an insert (if it's an update).. */
1345 res = buffer_extract_record (zh, rec_buf, rec_len,
1346 action == 3 ? 1 : 0 /* delete flag */,
1352 0, /* force update */
1353 1 /* allow update */
1355 if (res == ZEBRA_FAIL)
1357 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1358 "Unable to parse record");
1362 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1364 else if (action == 3)
1366 dict_delete (zh->reg->matchDict, recid_z);
1368 zebra_end_trans(zh);
1372 int delete_w_handle(const char *info, void *handle)
1374 ZebraHandle zh = (ZebraHandle) handle;
1378 if (*info == sizeof(pos))
1380 memcpy (&pos, info+1, sizeof(pos));
1381 isamb_unlink(zh->reg->isamb, pos);
1386 static int delete_SU_handle(void *handle, int ord)
1388 ZebraHandle zh = (ZebraHandle) handle;
1392 ord_len = key_SU_encode (ord, ord_buf);
1393 ord_buf[ord_len] = '\0';
1395 assert (zh->reg->isamb);
1396 dict_delete_subtree(zh->reg->dict, ord_buf,
1397 zh, delete_w_handle);
1401 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1403 ZEBRA_RES ret = ZEBRA_OK;
1405 yaz_log(log_level, "zebra_drop_database %s", db);
1407 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1409 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1413 zebraExplain_curDatabase (zh->reg->zei, db);
1415 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1416 zebraExplain_removeDatabase(zh->reg->zei, zh);
1420 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1421 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1422 "drop database only supported for isam:b");
1425 zebra_end_trans (zh);
1429 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1432 yaz_log(log_level, "zebra_create_database %s", db);
1435 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1437 if (zebra_begin_trans (zh, 1))
1440 /* announce database */
1441 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1442 /* explainDatabase */))
1444 zebra_end_trans (zh);
1445 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1448 return zebra_end_trans (zh);
1451 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1452 const char *input_str, int input_len,
1453 char *output_str, int output_len)
1459 yaz_log(log_level, "zebra_string_norm ");
1461 if (!zh->reg->zebra_maps)
1463 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1464 input_str, input_len);
1467 if (wrbuf_len(wrbuf) >= output_len)
1469 if (wrbuf_len(wrbuf))
1470 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1471 output_str[wrbuf_len(wrbuf)] = '\0';
1472 return wrbuf_len(wrbuf);
1475 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1477 char state_fname[256];
1482 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1484 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1485 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1486 f = fopen (fname, "w");
1488 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1489 fprintf (f, "%c %d %ld\n", val, seqno, p);
1494 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1496 char state_fname[256];
1501 yaz_log(log_level, "zebra_get_state ");
1503 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1504 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1505 f = fopen (fname, "r");
1511 fscanf (f, "%c %d", val, seqno);
1517 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1519 return zebra_begin_trans(zh, 0);
1522 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1524 return zebra_end_trans(zh);
1527 static void read_res_for_transaction(ZebraHandle zh)
1529 const char *group = res_get(zh->res, "group");
1531 /* FIXME - do we still use groups ?? */
1533 zh->m_group = group;
1534 v = res_get_prefix(zh->res, "followLinks", group, "1");
1535 zh->m_follow_links = atoi(v);
1537 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1538 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1540 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1541 zh->m_store_keys = atoi(v);
1543 v = res_get_prefix(zh->res, "storeData", group, "1");
1544 zh->m_store_data = atoi(v);
1546 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1547 zh->m_explain_database = atoi(v);
1549 v = res_get_prefix(zh->res, "openRW", group, "1");
1550 zh->m_flag_rw = atoi(v);
1552 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1553 zh->m_file_verbose_limit = atoi(v);
1556 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1559 zebra_select_default_database(zh);
1562 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1563 "zebra_begin_trans: no database selected");
1567 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1571 if (rw && !strchr(zh->user_perm, 'w'))
1575 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1587 const char *rval = 0;
1592 read_res_for_transaction(zh);
1595 if (zh->trans_no != 1)
1597 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1598 "zebra_begin_trans: no write trans within read");
1603 resultSetInvalidate (zh);
1604 zebra_register_close (zh->service, zh->reg);
1606 zh->trans_w_no = zh->trans_no;
1608 zh->records_inserted = 0;
1609 zh->records_updated = 0;
1610 zh->records_deleted = 0;
1611 zh->records_processed = 0;
1613 #if HAVE_SYS_TIMES_H
1617 if (zh->shadow_enable)
1618 rval = res_get (zh->res, "shadow");
1620 for (pass = 0; pass < 2; pass++)
1624 zebra_lock_r (zh->lock_normal);
1625 zebra_lock_w (zh->lock_shadow);
1629 zebra_lock_w (zh->lock_normal);
1630 zebra_lock_w (zh->lock_shadow);
1633 zebra_get_state (zh, &val, &seqno);
1636 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1637 zebra_unlock (zh->lock_shadow);
1638 zebra_unlock (zh->lock_normal);
1642 else if (val == 'd')
1646 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1648 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1649 bf_commitClean (bfs, rval);
1654 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1661 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1665 zebra_set_state (zh, 'd', seqno);
1667 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1668 1, rval ? 1 : 0, zh->res,
1671 zh->reg->seqno = seqno;
1674 zebra_set_state (zh, 'o', seqno);
1676 zebra_unlock (zh->lock_shadow);
1677 zebra_unlock (zh->lock_normal);
1682 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1683 "zebra_begin_trans: cannot open register");
1684 yaz_log(YLOG_FATAL, "%s", zh->errString);
1696 if (zh->trans_no != 1)
1698 zebra_flush_reg (zh);
1701 #if HAVE_SYS_TIMES_H
1707 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1710 if (!zh->lock_normal || !zh->lock_shadow)
1713 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1716 zebra_get_state (zh, &val, &seqno);
1722 else if (seqno != zh->reg->seqno)
1724 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1725 seqno, zh->reg->seqno);
1728 else if (zh->reg->last_val != val)
1730 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1731 val, zh->reg->last_val);
1738 zebra_lock_r (zh->lock_shadow);
1740 zebra_lock_r (zh->lock_normal);
1744 resultSetInvalidate (zh);
1745 zebra_register_close (zh->service, zh->reg);
1747 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1748 0, val == 'c' ? 1 : 0,
1749 zh->res, zh->path_reg);
1752 zebra_unlock (zh->lock_normal);
1753 zebra_unlock (zh->lock_shadow);
1755 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1758 zh->reg->last_val = val;
1759 zh->reg->seqno = seqno;
1761 read_res_for_transaction(zh);
1765 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1767 ZebraTransactionStatus dummy;
1769 yaz_log(log_level, "zebra_end_trans");
1770 return zebra_end_transaction(zh, &dummy);
1773 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1781 yaz_log(log_level, "zebra_end_transaction");
1783 status->processed = 0;
1784 status->inserted = 0;
1785 status->updated = 0;
1786 status->deleted = 0;
1790 if (!zh->res || !zh->reg)
1792 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1793 "zebra_end_trans: no open transaction");
1796 if (zh->trans_no != zh->trans_w_no)
1799 if (zh->trans_no != 0)
1802 /* release read lock */
1804 zebra_unlock (zh->lock_normal);
1805 zebra_unlock (zh->lock_shadow);
1808 { /* release write lock */
1812 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1813 rval = res_get (zh->res, "shadow");
1815 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1817 zebra_flush_reg (zh);
1819 resultSetInvalidate (zh);
1821 zebra_register_close (zh->service, zh->reg);
1824 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1825 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1826 zh->records_processed, zh->records_inserted,
1827 zh->records_updated, zh->records_deleted);
1829 status->processed = (int) zh->records_processed;
1830 status->inserted = (int) zh->records_inserted;
1831 status->updated = (int) zh->records_updated;
1832 status->deleted = (int) zh->records_deleted;
1834 zebra_get_state (zh, &val, &seqno);
1837 BFiles bfs = bfs_create (rval, zh->path_reg);
1838 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1839 bf_commitClean (bfs, rval);
1844 zebra_set_state (zh, 'o', seqno);
1846 zebra_unlock (zh->lock_shadow);
1847 zebra_unlock (zh->lock_normal);
1850 #if HAVE_SYS_TIMES_H
1852 yaz_log (log_level, "user/system: %ld/%ld",
1853 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1854 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1856 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1857 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1862 int zebra_repository_update (ZebraHandle zh, const char *path)
1866 yaz_log (log_level, "updating %s", path);
1867 repositoryUpdate (zh, path);
1871 int zebra_repository_delete (ZebraHandle zh, const char *path)
1875 yaz_log (log_level, "deleting %s", path);
1876 repositoryDelete (zh, path);
1880 int zebra_repository_show (ZebraHandle zh, const char *path)
1884 yaz_log(log_level, "zebra_repository_show");
1885 repositoryShow (zh, path);
1889 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
1897 zebra_select_default_database(zh);
1900 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1903 rval = res_get (zh->res, "shadow");
1906 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1910 zebra_lock_w (zh->lock_normal);
1911 zebra_lock_r (zh->lock_shadow);
1913 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1915 zebra_get_state (zh, &val, &seqno);
1918 bf_cache (bfs, rval);
1919 if (bf_commitExists (bfs))
1922 zebra_set_state (zh, 'd', seqno);
1925 zebra_set_state (zh, 'c', seqno);
1927 yaz_log (YLOG_DEBUG, "commit start");
1928 bf_commitExec (bfs);
1933 yaz_log (YLOG_DEBUG, "commit clean");
1934 bf_commitClean (bfs, rval);
1936 zebra_set_state (zh, 'o', seqno);
1940 yaz_log (log_level, "nothing to commit");
1944 zebra_unlock (zh->lock_shadow);
1945 zebra_unlock (zh->lock_normal);
1949 ZEBRA_RES zebra_clean(ZebraHandle zh)
1952 yaz_log(log_level, "zebra_clean");
1953 return zebra_commit_ex(zh, 1);
1956 ZEBRA_RES zebra_commit(ZebraHandle zh)
1959 yaz_log(log_level, "zebra_commit");
1960 return zebra_commit_ex(zh, 0);
1963 ZEBRA_RES zebra_init(ZebraHandle zh)
1968 yaz_log(log_level, "zebra_init");
1970 zebra_select_default_database(zh);
1973 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1974 "cannot select default database");
1977 rval = res_get (zh->res, "shadow");
1979 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1982 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1986 bf_cache (bfs, rval);
1990 zebra_set_state (zh, 'o', 0);
1994 ZEBRA_RES zebra_compact(ZebraHandle zh)
1998 yaz_log(log_level, "zebra_compact");
2001 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2004 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2010 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2013 yaz_log(log_level, "zebra_result");
2014 *code = zh->errCode;
2015 *addinfo = zh->errString;
2018 void zebra_shadow_enable(ZebraHandle zh, int value)
2021 yaz_log(log_level, "zebra_shadow_enable");
2022 zh->shadow_enable = value;
2025 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2029 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2031 if (zh->iconv_to_utf8 != 0)
2032 yaz_iconv_close(zh->iconv_to_utf8);
2033 if (zh->iconv_from_utf8 != 0)
2034 yaz_iconv_close(zh->iconv_from_utf8);
2037 yaz_iconv_open ("UTF-8", encoding);
2038 if (zh->iconv_to_utf8 == 0)
2039 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2040 zh->iconv_from_utf8 =
2041 yaz_iconv_open (encoding, "UTF-8");
2042 if (zh->iconv_to_utf8 == 0)
2043 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2048 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2051 yaz_log(log_level, "zebra_record_encoding");
2052 xfree(zh->record_encoding);
2053 zh->record_encoding = 0;
2055 zh->record_encoding = xstrdup (encoding);
2059 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2064 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2065 res_set(zh->res, name, value);
2068 const char *zebra_get_resource(ZebraHandle zh,
2069 const char *name, const char *defaultvalue)
2074 v = res_get_def (zh->res, name, (char *)defaultvalue);
2075 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2079 /* moved from zebra_api_ext.c by pop */
2080 /* FIXME: Should this really be public??? -Heikki */
2082 int zebra_trans_no (ZebraHandle zh)
2085 yaz_log(log_level, "zebra_trans_no");
2086 return zh->trans_no;
2089 int zebra_get_shadow_enable (ZebraHandle zh)
2092 yaz_log(log_level, "zebra_get_shadow_enable");
2093 return zh->shadow_enable;
2096 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2099 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2100 zh->shadow_enable = value;
2103 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2104 so that it's identicical to the original api_records_retrieve */
2105 void api_records_retrieve (ZebraHandle zh, ODR stream,
2106 const char *setname, Z_RecordComposition *comp,
2107 oid_value input_format, int num_recs,
2108 ZebraRetrievalRecord *recs)
2110 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2114 /* ---------------------------------------------------------------------------
2115 Record insert(=update), delete
2117 If sysno is provided, then it's used to identify the record.
2118 If not, and match_criteria is provided, then sysno is guessed
2119 If not, and a record is provided, then sysno is got from there
2120 NOTE: Now returns 0 at success and updates sysno, which is an int*
2124 int zebra_add_record(ZebraHandle zh,
2125 const char *buf, int buf_size)
2127 return zebra_update_record(zh, 0, 0 /* sysno */, 0, 0, buf, buf_size, 0);
2130 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2131 const char *recordType,
2132 SYSNO *sysno, const char *match,
2134 const char *buf, int buf_size, int force_update)
2140 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2143 buf_size = strlen(buf);
2145 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2147 res = buffer_extract_record (zh, buf, buf_size,
2148 0, /* delete_flag */
2154 0); /* allow_update */
2155 zebra_end_trans(zh);
2159 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2160 const char *recordType,
2161 SYSNO* sysno, const char *match,
2163 const char *buf, int buf_size,
2170 yaz_log(log_level, "zebra_update_record");
2172 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2174 if (buf_size < 1) buf_size = strlen(buf);
2176 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2178 res = buffer_extract_record (zh, buf, buf_size,
2179 0, /* delete_flag */
2185 1); /* allow_update */
2186 zebra_end_trans(zh);
2190 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2191 const char *recordType,
2192 SYSNO *sysno, const char *match,
2194 const char *buf, int buf_size,
2201 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2203 if (buf_size < 1) buf_size = strlen(buf);
2205 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2207 res = buffer_extract_record (zh, buf, buf_size,
2208 1, /* delete_flag */
2214 1); /* allow_update */
2215 zebra_end_trans(zh);
2219 /* ---------------------------------------------------------------------------
2223 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2224 const char *setname, zint *hits)
2227 ZEBRA_RES res = ZEBRA_OK;
2229 ODR odr = odr_createmem(ODR_ENCODE);
2234 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2236 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2240 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2241 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2245 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2249 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2257 /* ---------------------------------------------------------------------------
2258 Sort - a simplified interface, with optional read locks.
2260 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2261 const char *sort_spec,
2262 const char *output_setname,
2263 const char **input_setnames)
2265 int num_input_setnames = 0;
2266 int sort_status = 0;
2267 Z_SortKeySpecList *sort_sequence;
2271 assert(output_setname);
2272 assert(input_setnames);
2273 sort_sequence = yaz_sort_spec (stream, sort_spec);
2274 yaz_log(log_level, "sort (FIXME) ");
2277 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2278 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2282 /* we can do this, since the perl typemap code for char** will
2283 put a NULL at the end of list */
2284 while (input_setnames[num_input_setnames]) num_input_setnames++;
2286 if (zebra_begin_read (zh))
2289 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2290 output_setname, sort_sequence, &sort_status);
2296 /* ---------------------------------------------------------------------------
2297 Get BFS for Zebra system (to make alternative storage methods)
2299 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2302 return zh->reg->bfs;
2307 /* ---------------------------------------------------------------------------
2308 Set limit for search/scan
2310 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2313 zebra_limit_destroy(zh->m_limit);
2314 zh->m_limit = zebra_limit_create(complement_flag, ids);
2319 Set Error code + addinfo
2321 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2324 nmem_reset(zh->nmem_error);
2325 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2328 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2331 sprintf(vstr, ZINT_FORMAT, i);
2334 nmem_reset(zh->nmem_error);
2335 zh->errString = nmem_strdup(zh->nmem_error, vstr);