1 /* $Id: zebraapi.c,v 1.190 2005-10-20 18:28:10 quinn 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");
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);
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.buf_max = 0;
330 reg->keys.codec_handle = iscz1_start();
332 reg->sortKeys.buf = 0;
333 reg->sortKeys.buf_max = 0;
335 reg->sortKeys.codec_handle = iscz1_start();
347 reg->key_file_no = 0;
350 zebraRankInstall (reg, rank_1_class);
351 zebraRankInstall (reg, rank_zv_class);
352 zebraRankInstall (reg, rank_static_class);
354 recordCompression = res_get_def (res, "recordCompression", "none");
355 if (!strcmp (recordCompression, "none"))
356 record_compression = REC_COMPRESS_NONE;
357 if (!strcmp (recordCompression, "bzip2"))
358 record_compression = REC_COMPRESS_BZIP2;
360 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
362 yaz_log (YLOG_WARN, "rec_open failed");
367 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
369 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
371 yaz_log (YLOG_WARN, "dict_open failed");
374 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
376 yaz_log (YLOG_WARN, "sortIdx_open failed");
379 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
381 struct ISAMS_M_s isams_m;
382 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
383 key_isams_m(res, &isams_m))))
385 yaz_log (YLOG_WARN, "isams_open failed");
389 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
391 struct ISAMC_M_s isamc_m;
392 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
393 rw, key_isamc_m(res, &isamc_m))))
395 yaz_log (YLOG_WARN, "isamc_open failed");
399 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
401 struct ISAMC_M_s isamc_m;
403 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
404 rw, key_isamc_m(res, &isamc_m), 0)))
406 yaz_log (YLOG_WARN, "isamb_open failed");
410 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
412 struct ISAMC_M_s isamc_m;
414 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
415 rw, key_isamc_m(res, &isamc_m), 1)))
417 yaz_log (YLOG_WARN, "isamb_open failed");
421 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
423 struct ISAMC_M_s isamc_m;
425 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
426 rw, key_isamc_m(res, &isamc_m), -1)))
428 yaz_log (YLOG_WARN, "isamb_open failed");
432 reg->zei = zebraExplain_open (reg->records, reg->dh,
437 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
441 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
445 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
448 yaz_log(log_level, "zebra_admin_shutdown");
449 zebra_clearError(zh);
451 zebra_mutex_cond_lock (&zh->service->session_lock);
452 zh->service->stop_flag = 1;
453 zebra_mutex_cond_unlock (&zh->service->session_lock);
457 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
461 yaz_log(log_level, "zebra_admin_start");
462 zebra_clearError(zh);
464 zebra_mutex_cond_lock (&zs->session_lock);
465 zebra_mutex_cond_unlock (&zs->session_lock);
469 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
473 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
478 zebraExplain_close (reg->zei);
479 dict_close (reg->dict);
481 dict_close (reg->matchDict);
482 sortIdx_close (reg->sortIdx);
484 isams_close (reg->isams);
486 isamc_close (reg->isamc);
488 isamb_close (reg->isamb);
489 rec_close (®->records);
492 recTypes_destroy (reg->recTypes);
493 zebra_maps_close (reg->zebra_maps);
494 zebraRankDestroy (reg);
495 bfs_destroy (reg->bfs);
496 data1_destroy (reg->dh);
498 xfree(reg->sortKeys.buf);
499 xfree(reg->keys.buf);
500 if (reg->keys.codec_handle)
501 iscz1_stop(reg->keys.codec_handle);
503 if (reg->sortKeys.codec_handle)
504 iscz1_stop(reg->sortKeys.codec_handle);
511 ZEBRA_RES zebra_stop(ZebraService zs)
515 yaz_log (log_level, "zebra_stop");
519 zebra_close (zs->sessions);
522 zebra_mutex_cond_destroy (&zs->session_lock);
525 passwd_db_close (zs->passwd_db);
527 recTypeClass_destroy(zs->record_classes);
528 nmem_destroy(zs->nmem);
529 res_close (zs->global_res);
534 ZEBRA_RES zebra_close (ZebraHandle zh)
537 struct zebra_session **sp;
540 yaz_log(log_level, "zebra_close");
547 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
548 resultSetDestroy (zh, -1, 0, 0);
551 zebra_register_close (zh->service, zh->reg);
552 zebra_close_res (zh);
553 res_close(zh->session_res);
555 xfree(zh->record_encoding);
557 if (zh->dbaccesslist)
558 xfree(zh->dbaccesslist);
560 for (i = 0; i < zh->num_basenames; i++)
561 xfree(zh->basenames[i]);
562 xfree(zh->basenames);
564 if (zh->iconv_to_utf8 != 0)
565 yaz_iconv_close (zh->iconv_to_utf8);
566 if (zh->iconv_from_utf8 != 0)
567 yaz_iconv_close (zh->iconv_from_utf8);
569 zebra_mutex_cond_lock (&zs->session_lock);
570 zebra_lock_destroy (zh->lock_normal);
571 zebra_lock_destroy (zh->lock_shadow);
583 zebra_mutex_cond_unlock (&zs->session_lock);
585 xfree(zh->user_perm);
586 zh->service = 0; /* more likely to trigger an assert */
588 zebra_limit_destroy(zh->m_limit);
590 nmem_destroy(zh->nmem_error);
597 struct map_baseinfo {
603 char **new_basenames;
607 static void zebra_open_res(ZebraHandle zh)
615 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
616 zh->res = res_open(zh->session_res, 0);
617 res_read_file(zh->res, fname);
619 else if (*zh->reg_name == 0)
621 zh->res = res_open(zh->session_res, 0);
625 yaz_log (YLOG_WARN, "no register root specified");
626 zh->res = 0; /* no path for register - fail! */
630 static void zebra_close_res (ZebraHandle zh)
638 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
642 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
646 assert (zh->reg == 0);
647 assert (*zh->reg_name == 0);
653 resultSetInvalidate (zh);
654 zebra_register_close (zh->service, zh->reg);
660 zh->reg_name = xstrdup (new_reg);
664 if (zh->service->path_root)
666 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
667 strlen(zh->reg_name) + 3);
668 strcpy (zh->path_reg, zh->service->path_root);
671 strcat (zh->path_reg, "/");
672 strcat (zh->path_reg, zh->reg_name);
678 zebra_lock_destroy (zh->lock_normal);
682 zebra_lock_destroy (zh->lock_shadow);
688 const char *lock_area = res_get (zh->res, "lockDir");
690 if (!lock_area && zh->path_reg)
691 res_set (zh->res, "lockDir", zh->path_reg);
692 sprintf (fname, "norm.%s.LCK", zh->reg_name);
694 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
696 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
698 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
700 if (!zh->lock_normal || !zh->lock_shadow)
704 zebra_lock_destroy(zh->lock_normal);
709 zebra_lock_destroy(zh->lock_shadow);
718 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
719 zebra_set_approx_limit(zh, approx);
723 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
724 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
726 yaz_log(YLOG_LOG, "static rank unset");
730 void map_basenames_func (void *vp, const char *name, const char *value)
732 struct map_baseinfo *p = (struct map_baseinfo *) vp;
734 char fromdb[128], todb[8][128];
741 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
742 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
743 todb[5], todb[6], todb[7]);
747 for (i = 0; i<p->num_bases; i++)
748 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
751 for (i = 0; i < no; i++)
753 if (p->new_num_bases == p->new_num_max)
755 p->new_basenames[(p->new_num_bases)++] =
756 nmem_strdup (p->mem, todb[i]);
762 int zebra_select_default_database(ZebraHandle zh)
766 /* no database has been selected - so we select based on
767 resource setting (including group)
769 const char *group = res_get(zh->session_res, "group");
770 const char *v = res_get_prefix(zh->session_res,
771 "database", group, "Default");
772 return zebra_select_database(zh, v);
777 void map_basenames (ZebraHandle zh, ODR stream,
778 int *num_bases, char ***basenames)
780 struct map_baseinfo info;
781 struct map_baseinfo *p = &info;
784 yaz_log(log_level, "map_basenames ");
789 info.num_bases = *num_bases;
790 info.basenames = *basenames;
791 info.new_num_max = 128;
792 info.new_num_bases = 0;
793 info.new_basenames = (char **)
794 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
795 info.mem = stream->mem;
797 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
799 for (i = 0; i<p->num_bases; i++)
800 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
802 p->new_basenames[(p->new_num_bases)++] =
803 nmem_strdup (p->mem, p->basenames[i]);
805 *num_bases = info.new_num_bases;
806 *basenames = info.new_basenames;
807 for (i = 0; i<*num_bases; i++)
808 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
811 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
814 yaz_log(log_level, "zebra_select_database %s",basename);
816 return zebra_select_databases (zh, 1, &basename);
819 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
820 const char **basenames)
829 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
830 num_bases,basenames[0]);
835 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
839 /* Check if the user has access to all databases (Seb) */
840 /* You could argue that this should happen later, after we have
841 * determined that the database(s) exist. */
842 if (zh->dbaccesslist) {
843 for (i = 0; i < num_bases; i++) {
844 const char *db = basenames[i];
846 for (p = zh->dbaccesslist; p && *p; p = pp) {
848 if ((pp = strchr(p, '+'))) {
854 if (len == strlen(db) && !strncmp(db, p, len))
858 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
864 for (i = 0; i < zh->num_basenames; i++)
865 xfree(zh->basenames[i]);
866 xfree(zh->basenames);
868 zh->num_basenames = num_bases;
869 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
870 for (i = 0; i < zh->num_basenames; i++)
871 zh->basenames[i] = xstrdup (basenames[i]);
873 cp = strrchr(basenames[0], '/');
876 len = cp - basenames[0];
877 new_reg = xmalloc(len + 1);
878 memcpy (new_reg, basenames[0], len);
882 new_reg = xstrdup ("");
883 for (i = 1; i<num_bases; i++)
887 cp1 = strrchr (basenames[i], '/');
892 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
895 if (len != cp1 - basenames[i] ||
896 memcmp (basenames[i], new_reg, len))
898 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
906 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
911 zebra_select_register (zh, new_reg);
915 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
918 if (!zh->lock_normal || !zh->lock_shadow)
920 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
926 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
928 if (approx_limit == 0)
929 approx_limit = DEFAULT_APPROX_LIMIT;
930 zh->approx_limit = approx_limit;
934 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
935 const char *setname, zint *hits)
943 yaz_log(log_level, "zebra_search_rpn");
944 zebra_clearError(zh);
948 if (zebra_begin_read(zh) == ZEBRA_FAIL)
951 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
952 zh->num_basenames, zh->basenames, setname);
958 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
960 Z_RecordComposition *comp,
961 oid_value input_format, int num_recs,
962 ZebraRetrievalRecord *recs)
964 ZebraMetaRecord *poset;
966 ZEBRA_RES ret = ZEBRA_OK;
974 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
976 zebra_clearError(zh);
979 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
984 if (zebra_begin_read (zh) == ZEBRA_FAIL)
987 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
988 for (i = 0; i<num_recs; i++)
989 pos_array[i] = recs[i].position;
990 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
993 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
994 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1000 for (i = 0; i<num_recs; i++)
1004 recs[i].errCode = 0;
1005 recs[i].format = VAL_SUTRS;
1006 recs[i].len = strlen(poset[i].term);
1007 recs[i].buf = poset[i].term;
1008 recs[i].base = poset[i].db;
1010 else if (poset[i].sysno)
1014 zebra_snippets *hit_snippet = zebra_snippets_create();
1016 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1020 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1022 stream, input_format, comp,
1023 &recs[i].format, &buf, &len,
1024 &recs[i].base, &recs[i].errString);
1029 recs[i].buf = (char*) odr_malloc(stream, len);
1030 memcpy(recs[i].buf, buf, len);
1034 recs[i].score = poset[i].score;
1035 recs[i].sysno = poset[i].sysno;
1036 zebra_snippets_destroy(hit_snippet);
1040 /* only need to set it once */
1041 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1043 zebra_setError_zint(zh,
1044 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1049 recs[i].buf = 0; /* no record and no error issued */
1051 recs[i].errCode = 0;
1052 recs[i].format = VAL_NONE;
1056 zebra_meta_records_destroy(zh, poset, num_recs);
1058 zebra_end_read (zh);
1063 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1065 int *num_entries, ZebraScanEntry **entries,
1067 const char *setname)
1069 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1070 Z_AttributesPlusTerm *zapt;
1074 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1077 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1080 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1081 position, num_entries, entries, is_partial,
1083 yaz_pqf_destroy (pqf_parser);
1087 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1088 oid_value attributeset,
1090 int *num_entries, ZebraScanEntry **entries,
1092 const char *setname)
1095 RSET limit_rset = 0;
1100 assert(num_entries);
1103 yaz_log(log_level, "zebra_scan");
1104 zebra_clearError(zh);
1105 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1113 limit_rset = resultSetRef(zh, setname);
1117 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1119 zebra_end_read (zh);
1123 res = rpn_scan (zh, stream, zapt, attributeset,
1124 zh->num_basenames, zh->basenames, position,
1125 num_entries, entries, is_partial, limit_rset, 0);
1126 zebra_end_read (zh);
1130 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1131 int num_input_setnames, const char **input_setnames,
1132 const char *output_setname,
1133 Z_SortKeySpecList *sort_sequence,
1139 assert(num_input_setnames>0);
1140 assert(input_setnames);
1141 assert(sort_sequence);
1142 assert(sort_status);
1143 yaz_log(log_level, "zebra_sort");
1144 zebra_clearError(zh);
1145 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1147 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1148 output_setname, sort_sequence, sort_status);
1153 int zebra_deleteResultSet(ZebraHandle zh, int function,
1154 int num_setnames, char **setnames,
1160 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1161 zebra_clearError(zh);;
1162 if (zebra_begin_read(zh))
1163 return Z_DeleteStatus_systemProblemAtTarget;
1166 case Z_DeleteResultSetRequest_list:
1167 assert(num_setnames>0);
1169 resultSetDestroy (zh, num_setnames, setnames, statuses);
1171 case Z_DeleteResultSetRequest_all:
1172 resultSetDestroy (zh, -1, 0, statuses);
1175 zebra_end_read (zh);
1176 status = Z_DeleteStatus_success;
1177 for (i = 0; i<num_setnames; i++)
1178 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1179 status = statuses[i];
1183 int zebra_errCode (ZebraHandle zh)
1187 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1190 yaz_log(log_level, "zebra_errCode: o");
1194 const char *zebra_errString (ZebraHandle zh)
1198 e= diagbib1_str (zh->errCode);
1199 yaz_log(log_level, "zebra_errString: %s",e);
1203 char *zebra_errAdd (ZebraHandle zh)
1208 yaz_log(log_level, "zebra_errAdd: %s",a);
1212 void zebra_clearError(ZebraHandle zh)
1221 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1224 const char *astring;
1229 zebra_clearError(zh);
1232 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1233 p = res_get(zs->global_res, u);
1234 xfree(zh->user_perm);
1235 zh->user_perm = xstrdup(p ? p : "r");
1237 /* Determine database access list */
1238 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1240 zh->dbaccesslist = xstrdup(astring);
1242 zh->dbaccesslist = NULL;
1244 /* users that don't require a password .. */
1245 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1248 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1253 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1254 const char *record_type)
1257 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1258 database, record_type);
1259 zebra_clearError(zh);
1260 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1262 return zebra_begin_trans(zh, 1);
1265 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1268 yaz_log(log_level, "zebra_admin_import_end");
1269 zebra_clearError(zh);
1270 return zebra_end_trans(zh);
1273 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1275 ZEBRA_RES res = ZEBRA_OK;
1279 yaz_log(log_level, "zebra_admin_import_segment");
1280 zebra_clearError(zh);
1281 for (i = 0; i<segment->num_segmentRecords; i++)
1283 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1285 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1287 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1288 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1290 Odr_oct *oct = fragment->u.notExternallyTagged;
1293 if (zebra_update_record(zh,
1294 0, /* record Type */
1298 (const char *) oct->buf, oct->len,
1307 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1308 const char *rec_buf,
1310 const char *recid_buf, size_t recid_len,
1312 /* 1 = insert. Fail it already exists */
1313 /* 2 = replace. Fail it does not exist */
1314 /* 3 = delete. Fail if does not exist */
1315 /* 4 = update. Insert/replace */
1322 assert(action>0 && action <=4);
1325 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1326 zebra_clearError(zh);
1328 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1331 memcpy (recid_z, recid_buf, recid_len);
1332 recid_z[recid_len] = 0;
1334 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1337 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1340 if (action == 1) /* fail if insert */
1342 zebra_end_trans(zh);
1346 memcpy (&sysno, rinfo+1, sizeof(sysno));
1350 if (action == 2 || action == 3) /* fail if delete or update */
1352 zebra_end_trans(zh);
1355 action = 1; /* make it an insert (if it's an update).. */
1357 res = buffer_extract_record (zh, rec_buf, rec_len,
1358 action == 3 ? 1 : 0 /* delete flag */,
1364 0, /* force update */
1365 1 /* allow update */
1369 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1371 else if (action == 3)
1373 dict_delete (zh->reg->matchDict, recid_z);
1375 zebra_end_trans(zh);
1379 int delete_w_handle(const char *info, void *handle)
1381 ZebraHandle zh = (ZebraHandle) handle;
1385 if (*info == sizeof(pos))
1387 memcpy (&pos, info+1, sizeof(pos));
1388 isamb_unlink(zh->reg->isamb, pos);
1393 static int delete_SU_handle(void *handle, int ord)
1395 ZebraHandle zh = (ZebraHandle) handle;
1399 ord_len = key_SU_encode (ord, ord_buf);
1400 ord_buf[ord_len] = '\0';
1402 assert (zh->reg->isamb);
1403 dict_delete_subtree(zh->reg->dict, ord_buf,
1404 zh, delete_w_handle);
1408 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1410 ZEBRA_RES ret = ZEBRA_OK;
1412 yaz_log(log_level, "zebra_drop_database %s", db);
1413 zebra_clearError(zh);
1415 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1417 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1421 zebraExplain_curDatabase (zh->reg->zei, db);
1423 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1424 zebraExplain_removeDatabase(zh->reg->zei, zh);
1428 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1431 zebra_end_trans (zh);
1435 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1438 yaz_log(log_level, "zebra_create_database %s", db);
1440 zebra_clearError(zh);
1442 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1444 if (zebra_begin_trans (zh, 1))
1447 /* announce database */
1448 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1449 /* explainDatabase */))
1451 zebra_end_trans (zh);
1452 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1455 return zebra_end_trans (zh);
1458 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1459 const char *input_str, int input_len,
1460 char *output_str, int output_len)
1466 yaz_log(log_level, "zebra_string_norm ");
1467 zebra_clearError(zh);
1468 if (!zh->reg->zebra_maps)
1470 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1471 input_str, input_len);
1474 if (wrbuf_len(wrbuf) >= output_len)
1476 if (wrbuf_len(wrbuf))
1477 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1478 output_str[wrbuf_len(wrbuf)] = '\0';
1479 return wrbuf_len(wrbuf);
1482 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1484 char state_fname[256];
1489 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1490 zebra_clearError(zh);
1492 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1493 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1494 f = fopen (fname, "w");
1496 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1497 fprintf (f, "%c %d %ld\n", val, seqno, p);
1502 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1504 char state_fname[256];
1509 yaz_log(log_level, "zebra_get_state ");
1510 zebra_clearError(zh);
1511 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1512 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1513 f = fopen (fname, "r");
1519 fscanf (f, "%c %d", val, seqno);
1525 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1527 return zebra_begin_trans(zh, 0);
1530 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1532 return zebra_end_trans(zh);
1535 static void read_res_for_transaction(ZebraHandle zh)
1537 const char *group = res_get(zh->res, "group");
1539 /* FIXME - do we still use groups ?? */
1541 zh->m_group = group;
1542 v = res_get_prefix(zh->res, "followLinks", group, "1");
1543 zh->m_follow_links = atoi(v);
1545 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1546 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1548 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1549 zh->m_store_keys = atoi(v);
1551 v = res_get_prefix(zh->res, "storeData", group, "1");
1552 zh->m_store_data = atoi(v);
1554 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1555 zh->m_explain_database = atoi(v);
1557 v = res_get_prefix(zh->res, "openRW", group, "1");
1558 zh->m_flag_rw = atoi(v);
1560 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1561 zh->m_file_verbose_limit = atoi(v);
1564 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1567 zebra_select_default_database(zh);
1570 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1571 "zebra_begin_trans: no database selected");
1575 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1579 if (rw && !strchr(zh->user_perm, 'w'))
1583 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1595 const char *rval = 0;
1600 read_res_for_transaction(zh);
1603 if (zh->trans_no != 1)
1605 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1606 "zebra_begin_trans: no write trans within read");
1611 resultSetInvalidate (zh);
1612 zebra_register_close (zh->service, zh->reg);
1614 zh->trans_w_no = zh->trans_no;
1616 zebra_clearError(zh);
1618 zh->records_inserted = 0;
1619 zh->records_updated = 0;
1620 zh->records_deleted = 0;
1621 zh->records_processed = 0;
1623 #if HAVE_SYS_TIMES_H
1627 if (zh->shadow_enable)
1628 rval = res_get (zh->res, "shadow");
1630 for (pass = 0; pass < 2; pass++)
1634 zebra_lock_r (zh->lock_normal);
1635 zebra_lock_w (zh->lock_shadow);
1639 zebra_lock_w (zh->lock_normal);
1640 zebra_lock_w (zh->lock_shadow);
1643 zebra_get_state (zh, &val, &seqno);
1646 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1647 zebra_unlock (zh->lock_shadow);
1648 zebra_unlock (zh->lock_normal);
1652 else if (val == 'd')
1656 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1658 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1659 bf_commitClean (bfs, rval);
1664 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1671 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1675 zebra_set_state (zh, 'd', seqno);
1677 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1678 1, rval ? 1 : 0, zh->res,
1681 zh->reg->seqno = seqno;
1684 zebra_set_state (zh, 'o', seqno);
1686 zebra_unlock (zh->lock_shadow);
1687 zebra_unlock (zh->lock_normal);
1692 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1693 "zebra_begin_trans: cannot open register");
1694 yaz_log(YLOG_FATAL, "%s", zh->errString);
1706 if (zh->trans_no != 1)
1708 zebra_flush_reg (zh);
1711 zebra_clearError(zh);
1712 #if HAVE_SYS_TIMES_H
1718 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1721 if (!zh->lock_normal || !zh->lock_shadow)
1724 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1727 zebra_get_state (zh, &val, &seqno);
1733 else if (seqno != zh->reg->seqno)
1735 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1736 seqno, zh->reg->seqno);
1739 else if (zh->reg->last_val != val)
1741 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1742 val, zh->reg->last_val);
1749 zebra_lock_r (zh->lock_shadow);
1751 zebra_lock_r (zh->lock_normal);
1755 resultSetInvalidate (zh);
1756 zebra_register_close (zh->service, zh->reg);
1758 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1759 0, val == 'c' ? 1 : 0,
1760 zh->res, zh->path_reg);
1763 zebra_unlock (zh->lock_normal);
1764 zebra_unlock (zh->lock_shadow);
1766 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1769 zh->reg->last_val = val;
1770 zh->reg->seqno = seqno;
1772 read_res_for_transaction(zh);
1776 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1778 ZebraTransactionStatus dummy;
1780 yaz_log(log_level, "zebra_end_trans");
1781 return zebra_end_transaction(zh, &dummy);
1784 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1792 yaz_log(log_level, "zebra_end_transaction");
1794 status->processed = 0;
1795 status->inserted = 0;
1796 status->updated = 0;
1797 status->deleted = 0;
1801 if (!zh->res || !zh->reg)
1803 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1804 "zebra_end_trans: no open transaction");
1807 if (zh->trans_no != zh->trans_w_no)
1810 if (zh->trans_no != 0)
1813 /* release read lock */
1815 zebra_unlock (zh->lock_normal);
1816 zebra_unlock (zh->lock_shadow);
1819 { /* release write lock */
1823 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1824 rval = res_get (zh->res, "shadow");
1826 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1828 zebra_flush_reg (zh);
1830 resultSetInvalidate (zh);
1832 zebra_register_close (zh->service, zh->reg);
1835 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1836 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1837 zh->records_processed, zh->records_inserted,
1838 zh->records_updated, zh->records_deleted);
1840 status->processed = (int) zh->records_processed;
1841 status->inserted = (int) zh->records_inserted;
1842 status->updated = (int) zh->records_updated;
1843 status->deleted = (int) zh->records_deleted;
1845 zebra_get_state (zh, &val, &seqno);
1848 BFiles bfs = bfs_create (rval, zh->path_reg);
1849 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1850 bf_commitClean (bfs, rval);
1855 zebra_set_state (zh, 'o', seqno);
1857 zebra_unlock (zh->lock_shadow);
1858 zebra_unlock (zh->lock_normal);
1861 #if HAVE_SYS_TIMES_H
1863 yaz_log (log_level, "user/system: %ld/%ld",
1864 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1865 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1867 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1868 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1873 int zebra_repository_update (ZebraHandle zh, const char *path)
1877 zebra_clearError(zh);
1878 yaz_log (log_level, "updating %s", path);
1879 repositoryUpdate (zh, path);
1883 int zebra_repository_delete (ZebraHandle zh, const char *path)
1887 zebra_clearError(zh);
1888 yaz_log (log_level, "deleting %s", path);
1889 repositoryDelete (zh, path);
1893 int zebra_repository_show (ZebraHandle zh, const char *path)
1897 yaz_log(log_level, "zebra_repository_show");
1898 zebra_clearError(zh);
1899 repositoryShow (zh, path);
1903 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1910 zebra_clearError(zh);
1912 zebra_select_default_database(zh);
1915 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1918 rval = res_get (zh->res, "shadow");
1921 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1925 zebra_lock_w (zh->lock_normal);
1926 zebra_lock_r (zh->lock_shadow);
1928 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1930 zebra_get_state (zh, &val, &seqno);
1933 bf_cache (bfs, rval);
1934 if (bf_commitExists (bfs))
1937 zebra_set_state (zh, 'd', seqno);
1940 zebra_set_state (zh, 'c', seqno);
1942 yaz_log (YLOG_DEBUG, "commit start");
1943 bf_commitExec (bfs);
1948 yaz_log (YLOG_DEBUG, "commit clean");
1949 bf_commitClean (bfs, rval);
1951 zebra_set_state (zh, 'o', seqno);
1955 yaz_log (log_level, "nothing to commit");
1959 zebra_unlock (zh->lock_shadow);
1960 zebra_unlock (zh->lock_normal);
1964 ZEBRA_RES zebra_clean(ZebraHandle zh)
1967 yaz_log(log_level, "zebra_clean");
1968 return zebra_commit_ex(zh, 1);
1971 ZEBRA_RES zebra_commit(ZebraHandle zh)
1974 yaz_log(log_level, "zebra_commit");
1975 return zebra_commit_ex(zh, 0);
1978 ZEBRA_RES zebra_init(ZebraHandle zh)
1983 yaz_log(log_level, "zebra_init");
1984 zebra_clearError(zh);
1986 zebra_select_default_database(zh);
1989 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1990 "cannot select default database");
1993 rval = res_get (zh->res, "shadow");
1995 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1998 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2002 bf_cache (bfs, rval);
2006 zebra_set_state (zh, 'o', 0);
2010 ZEBRA_RES zebra_compact(ZebraHandle zh)
2014 yaz_log(log_level, "zebra_compact");
2015 zebra_clearError(zh);
2018 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2021 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2027 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2030 yaz_log(log_level, "zebra_result");
2031 *code = zh->errCode;
2032 *addinfo = zh->errString;
2035 void zebra_shadow_enable(ZebraHandle zh, int value)
2038 yaz_log(log_level, "zebra_shadow_enable");
2039 zebra_clearError(zh);
2040 zh->shadow_enable = value;
2043 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2047 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2048 zebra_clearError(zh);
2050 if (zh->iconv_to_utf8 != 0)
2051 yaz_iconv_close(zh->iconv_to_utf8);
2052 if (zh->iconv_from_utf8 != 0)
2053 yaz_iconv_close(zh->iconv_from_utf8);
2056 yaz_iconv_open ("UTF-8", encoding);
2057 if (zh->iconv_to_utf8 == 0)
2058 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2059 zh->iconv_from_utf8 =
2060 yaz_iconv_open (encoding, "UTF-8");
2061 if (zh->iconv_to_utf8 == 0)
2062 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2067 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2070 yaz_log(log_level, "zebra_record_encoding");
2071 zebra_clearError(zh);
2072 xfree(zh->record_encoding);
2073 zh->record_encoding = 0;
2075 zh->record_encoding = xstrdup (encoding);
2079 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2084 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2085 zebra_clearError(zh);
2086 res_set(zh->res, name, value);
2089 const char *zebra_get_resource(ZebraHandle zh,
2090 const char *name, const char *defaultvalue)
2095 v = res_get_def (zh->res, name, (char *)defaultvalue);
2096 zebra_clearError(zh);
2097 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2101 /* moved from zebra_api_ext.c by pop */
2102 /* FIXME: Should this really be public??? -Heikki */
2104 int zebra_trans_no (ZebraHandle zh)
2107 yaz_log(log_level, "zebra_trans_no");
2108 return zh->trans_no;
2111 int zebra_get_shadow_enable (ZebraHandle zh)
2114 yaz_log(log_level, "zebra_get_shadow_enable");
2115 return zh->shadow_enable;
2118 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2121 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2122 zh->shadow_enable = value;
2125 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2126 so that it's identicical to the original api_records_retrieve */
2127 void api_records_retrieve (ZebraHandle zh, ODR stream,
2128 const char *setname, Z_RecordComposition *comp,
2129 oid_value input_format, int num_recs,
2130 ZebraRetrievalRecord *recs)
2132 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2136 /* ---------------------------------------------------------------------------
2137 Record insert(=update), delete
2139 If sysno is provided, then it's used to identify the record.
2140 If not, and match_criteria is provided, then sysno is guessed
2141 If not, and a record is provided, then sysno is got from there
2142 NOTE: Now returns 0 at success and updates sysno, which is an int*
2146 int zebra_add_record(ZebraHandle zh,
2147 const char *buf, int buf_size)
2150 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2153 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2154 const char *recordType,
2155 SYSNO *sysno, const char *match,
2157 const char *buf, int buf_size, int force_update)
2163 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2166 buf_size = strlen(buf);
2168 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2170 res = buffer_extract_record (zh, buf, buf_size,
2171 0, /* delete_flag */
2177 0); /* allow_update */
2178 zebra_end_trans(zh);
2182 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2183 const char *recordType,
2184 SYSNO* sysno, const char *match,
2186 const char *buf, int buf_size,
2194 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2196 if (buf_size < 1) buf_size = strlen(buf);
2198 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2200 res = buffer_extract_record (zh, buf, buf_size,
2201 0, /* delete_flag */
2207 1); /* allow_update */
2208 zebra_end_trans(zh);
2212 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2213 const char *recordType,
2214 SYSNO *sysno, const char *match,
2216 const char *buf, int buf_size,
2223 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2225 if (buf_size < 1) buf_size = strlen(buf);
2227 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2229 res = buffer_extract_record (zh, buf, buf_size,
2230 1, /* delete_flag */
2236 1); /* allow_update */
2237 zebra_end_trans(zh);
2241 /* ---------------------------------------------------------------------------
2245 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2246 const char *setname, zint *hits)
2249 ZEBRA_RES res = ZEBRA_OK;
2251 ODR odr = odr_createmem(ODR_ENCODE);
2256 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2258 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2262 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2263 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2267 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2271 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2279 /* ---------------------------------------------------------------------------
2280 Sort - a simplified interface, with optional read locks.
2282 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2283 const char *sort_spec,
2284 const char *output_setname,
2285 const char **input_setnames)
2287 int num_input_setnames = 0;
2288 int sort_status = 0;
2289 Z_SortKeySpecList *sort_sequence;
2293 assert(output_setname);
2294 assert(input_setnames);
2295 sort_sequence = yaz_sort_spec (stream, sort_spec);
2296 yaz_log(log_level, "sort (FIXME) ");
2299 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2300 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2304 /* we can do this, since the perl typemap code for char** will
2305 put a NULL at the end of list */
2306 while (input_setnames[num_input_setnames]) num_input_setnames++;
2308 if (zebra_begin_read (zh))
2311 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2312 output_setname, sort_sequence, &sort_status);
2318 /* ---------------------------------------------------------------------------
2319 Get BFS for Zebra system (to make alternative storage methods)
2321 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2324 return zh->reg->bfs;
2329 /* ---------------------------------------------------------------------------
2330 Set limit for search/scan
2332 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2335 zebra_limit_destroy(zh->m_limit);
2336 zh->m_limit = zebra_limit_create(complement_flag, ids);
2341 Set Error code + addinfo
2343 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2346 nmem_reset(zh->nmem_error);
2347 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2350 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2353 sprintf(vstr, ZINT_FORMAT, i);
2356 nmem_reset(zh->nmem_error);
2357 zh->errString = nmem_strdup(zh->nmem_error, vstr);