1 /* $Id: zebraapi.c,v 1.192 2005-10-28 07:25:30 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");
76 zebraExplain_flush (zh->reg->zei, zh);
78 extract_flushWriteKeys (zh, 1 /* final */);
79 zebra_index_merge (zh );
82 static struct zebra_register *zebra_register_open(ZebraService zs,
84 int rw, int useshadow,
86 const char *reg_path);
87 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
89 ZebraHandle zebra_open(ZebraService zs, Res res)
92 const char *default_encoding;
93 if (!log_level_initialized)
95 log_level = yaz_log_module_level("zebraapi");
96 log_level_initialized = 1;
99 yaz_log(log_level, "zebra_open");
104 zh = (ZebraHandle) xmalloc(sizeof(*zh));
105 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
108 zh->reg = 0; /* no register attached yet */
114 zh->session_res = res_open(zs->global_res, res);
116 zh->dbaccesslist = 0;
118 zh->reg_name = xstrdup ("");
120 zh->num_basenames = 0;
123 zh->approx_limit = DEFAULT_APPROX_LIMIT;
130 zh->shadow_enable = 1;
131 zh->m_staticrank = 0;
133 default_encoding = res_get_def(zh->session_res, "encoding", "ISO-8859-1");
136 yaz_iconv_open ("UTF-8", default_encoding);
137 if (zh->iconv_to_utf8 == 0)
138 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
140 zh->iconv_from_utf8 =
141 yaz_iconv_open (default_encoding, "UTF-8");
142 if (zh->iconv_to_utf8 == 0)
143 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
146 zh->record_encoding = 0;
148 zebra_mutex_cond_lock (&zs->session_lock);
150 zh->next = zs->sessions;
153 zebra_mutex_cond_unlock (&zs->session_lock);
155 zh->store_data_buf = 0;
157 zh->m_limit = zebra_limit_create(1, 0);
159 zh->nmem_error = nmem_create();
164 ZebraService zebra_start (const char *configName)
166 return zebra_start_res(configName, 0, 0);
169 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
173 if (!log_level_initialized)
175 log_level = yaz_log_module_level("zebraapi");
176 log_level_initialized = 1;
179 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
180 configName ? configName : "");
182 if ((res = res_open(def_res, over_res)))
184 const char *passwd_plain = 0;
185 const char *passwd_encrypt = 0;
186 const char *dbaccess = 0;
187 ZebraService zh = xmalloc(sizeof(*zh));
190 res_read_file(res, configName);
192 zh->global_res = res;
197 zebra_mutex_cond_init (&zh->session_lock);
198 passwd_plain = res_get (zh->global_res, "passwd");
199 passwd_encrypt = res_get (zh->global_res, "passwd.c");
200 dbaccess = res_get (zh->global_res, "dbaccess");
202 if (!passwd_plain && !passwd_encrypt)
203 zh->passwd_db = NULL;
206 zh->passwd_db = passwd_db_open();
208 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
212 passwd_db_file_plain(zh->passwd_db, passwd_plain);
214 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
221 zh->dbaccess = res_open(NULL, NULL);
222 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
223 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
228 zh->path_root = res_get (zh->global_res, "root");
229 zh->nmem = nmem_create();
230 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
236 void zebra_filter_info(ZebraService zs, void *cd,
237 void (*cb)(void *cd, const char *name))
241 recTypeClass_info(zs->record_classes, cd, cb);
244 void zebra_pidfname(ZebraService zs, char *path)
247 zebra_lock_prefix (zs->global_res, path);
248 strcat(path, "zebrasrv.pid");
251 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
252 int compact_flag, Res res)
254 int page_size = 4096;
255 char resource_str[200];
256 sprintf (resource_str, "dict.%.100s.pagesize", name);
260 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
261 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
263 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
267 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
268 int rw, int useshadow, Res res,
269 const char *reg_path)
271 struct zebra_register *reg;
272 int record_compression = REC_COMPRESS_NONE;
273 const char *recordCompression = 0;
274 const char *profilePath;
279 reg = xmalloc(sizeof(*reg));
282 reg->name = xstrdup (name);
289 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
290 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
292 reg->dh = data1_createx (DATA1_FLAG_XML);
298 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
301 data1_destroy(reg->dh);
307 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
309 bfs_destroy(reg->bfs);
310 data1_destroy(reg->dh);
316 getcwd(cwd, sizeof(cwd)-1);
317 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
318 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
320 data1_set_tabpath (reg->dh, profilePath);
321 data1_set_tabroot (reg->dh, reg_path);
322 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
324 reg->zebra_maps = zebra_maps_open (res, reg_path);
325 reg->rank_classes = NULL;
330 reg->keys = zebra_rec_keys_open();
332 reg->keys.buf_max = 0;
334 reg->keys.codec_handle = iscz1_start();
337 reg->sortKeys.buf = 0;
338 reg->sortKeys.buf_max = 0;
340 reg->sortKeys.codec_handle = iscz1_start();
352 reg->key_file_no = 0;
355 zebraRankInstall (reg, rank_1_class);
356 zebraRankInstall (reg, rank_zv_class);
357 zebraRankInstall (reg, rank_static_class);
359 recordCompression = res_get_def (res, "recordCompression", "none");
360 if (!strcmp (recordCompression, "none"))
361 record_compression = REC_COMPRESS_NONE;
362 if (!strcmp (recordCompression, "bzip2"))
363 record_compression = REC_COMPRESS_BZIP2;
365 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
367 yaz_log (YLOG_WARN, "rec_open failed");
372 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
374 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
376 yaz_log (YLOG_WARN, "dict_open failed");
379 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
381 yaz_log (YLOG_WARN, "sortIdx_open failed");
384 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
386 struct ISAMS_M_s isams_m;
387 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
388 key_isams_m(res, &isams_m))))
390 yaz_log (YLOG_WARN, "isams_open failed");
394 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
396 struct ISAMC_M_s isamc_m;
397 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
398 rw, key_isamc_m(res, &isamc_m))))
400 yaz_log (YLOG_WARN, "isamc_open failed");
404 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
406 struct ISAMC_M_s isamc_m;
408 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
409 rw, key_isamc_m(res, &isamc_m), 0)))
411 yaz_log (YLOG_WARN, "isamb_open failed");
415 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
417 struct ISAMC_M_s isamc_m;
419 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
420 rw, key_isamc_m(res, &isamc_m), 1)))
422 yaz_log (YLOG_WARN, "isamb_open failed");
426 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
428 struct ISAMC_M_s isamc_m;
430 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
431 rw, key_isamc_m(res, &isamc_m), -1)))
433 yaz_log (YLOG_WARN, "isamb_open failed");
437 reg->zei = zebraExplain_open (reg->records, reg->dh,
442 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
446 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
450 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
453 yaz_log(log_level, "zebra_admin_shutdown");
454 zebra_clearError(zh);
456 zebra_mutex_cond_lock (&zh->service->session_lock);
457 zh->service->stop_flag = 1;
458 zebra_mutex_cond_unlock (&zh->service->session_lock);
462 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
466 yaz_log(log_level, "zebra_admin_start");
467 zebra_clearError(zh);
469 zebra_mutex_cond_lock (&zs->session_lock);
470 zebra_mutex_cond_unlock (&zs->session_lock);
474 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
478 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
483 zebraExplain_close (reg->zei);
484 dict_close (reg->dict);
486 dict_close (reg->matchDict);
487 sortIdx_close (reg->sortIdx);
489 isams_close (reg->isams);
491 isamc_close (reg->isamc);
493 isamb_close (reg->isamb);
494 rec_close (®->records);
497 recTypes_destroy (reg->recTypes);
498 zebra_maps_close (reg->zebra_maps);
499 zebraRankDestroy (reg);
500 bfs_destroy (reg->bfs);
501 data1_destroy (reg->dh);
503 xfree(reg->sortKeys.buf);
505 zebra_rec_keys_close(reg->keys);
507 xfree(reg->keys.buf);
508 if (reg->keys.codec_handle)
509 iscz1_stop(reg->keys.codec_handle);
511 if (reg->sortKeys.codec_handle)
512 iscz1_stop(reg->sortKeys.codec_handle);
521 ZEBRA_RES zebra_stop(ZebraService zs)
525 yaz_log (log_level, "zebra_stop");
529 zebra_close (zs->sessions);
532 zebra_mutex_cond_destroy (&zs->session_lock);
535 passwd_db_close (zs->passwd_db);
537 recTypeClass_destroy(zs->record_classes);
538 nmem_destroy(zs->nmem);
539 res_close (zs->global_res);
544 ZEBRA_RES zebra_close (ZebraHandle zh)
547 struct zebra_session **sp;
550 yaz_log(log_level, "zebra_close");
557 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
558 resultSetDestroy (zh, -1, 0, 0);
561 zebra_register_close (zh->service, zh->reg);
562 zebra_close_res (zh);
563 res_close(zh->session_res);
565 xfree(zh->record_encoding);
567 xfree(zh->dbaccesslist);
569 for (i = 0; i < zh->num_basenames; i++)
570 xfree(zh->basenames[i]);
571 xfree(zh->basenames);
573 if (zh->iconv_to_utf8 != 0)
574 yaz_iconv_close (zh->iconv_to_utf8);
575 if (zh->iconv_from_utf8 != 0)
576 yaz_iconv_close (zh->iconv_from_utf8);
578 zebra_mutex_cond_lock (&zs->session_lock);
579 zebra_lock_destroy (zh->lock_normal);
580 zebra_lock_destroy (zh->lock_shadow);
592 zebra_mutex_cond_unlock (&zs->session_lock);
594 xfree(zh->user_perm);
595 zh->service = 0; /* more likely to trigger an assert */
597 zebra_limit_destroy(zh->m_limit);
599 nmem_destroy(zh->nmem_error);
606 struct map_baseinfo {
612 char **new_basenames;
616 static void zebra_open_res(ZebraHandle zh)
624 sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
625 zh->res = res_open(zh->session_res, 0);
626 res_read_file(zh->res, fname);
628 else if (*zh->reg_name == 0)
630 zh->res = res_open(zh->session_res, 0);
634 yaz_log (YLOG_WARN, "no register root specified");
635 zh->res = 0; /* no path for register - fail! */
639 static void zebra_close_res (ZebraHandle zh)
647 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
651 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
655 assert (zh->reg == 0);
656 assert (*zh->reg_name == 0);
662 resultSetInvalidate (zh);
663 zebra_register_close (zh->service, zh->reg);
669 zh->reg_name = xstrdup (new_reg);
673 if (zh->service->path_root)
675 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
676 strlen(zh->reg_name) + 3);
677 strcpy (zh->path_reg, zh->service->path_root);
680 strcat (zh->path_reg, "/");
681 strcat (zh->path_reg, zh->reg_name);
687 zebra_lock_destroy (zh->lock_normal);
691 zebra_lock_destroy (zh->lock_shadow);
697 const char *lock_area = res_get (zh->res, "lockDir");
699 if (!lock_area && zh->path_reg)
700 res_set (zh->res, "lockDir", zh->path_reg);
701 sprintf (fname, "norm.%s.LCK", zh->reg_name);
703 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
705 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
707 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
709 if (!zh->lock_normal || !zh->lock_shadow)
713 zebra_lock_destroy(zh->lock_normal);
718 zebra_lock_destroy(zh->lock_shadow);
727 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
728 zebra_set_approx_limit(zh, approx);
732 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
733 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
735 yaz_log(YLOG_LOG, "static rank unset");
739 void map_basenames_func (void *vp, const char *name, const char *value)
741 struct map_baseinfo *p = (struct map_baseinfo *) vp;
743 char fromdb[128], todb[8][128];
750 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
751 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
752 todb[5], todb[6], todb[7]);
756 for (i = 0; i<p->num_bases; i++)
757 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
760 for (i = 0; i < no; i++)
762 if (p->new_num_bases == p->new_num_max)
764 p->new_basenames[(p->new_num_bases)++] =
765 nmem_strdup (p->mem, todb[i]);
771 int zebra_select_default_database(ZebraHandle zh)
775 /* no database has been selected - so we select based on
776 resource setting (including group)
778 const char *group = res_get(zh->session_res, "group");
779 const char *v = res_get_prefix(zh->session_res,
780 "database", group, "Default");
781 return zebra_select_database(zh, v);
786 void map_basenames (ZebraHandle zh, ODR stream,
787 int *num_bases, char ***basenames)
789 struct map_baseinfo info;
790 struct map_baseinfo *p = &info;
793 yaz_log(log_level, "map_basenames ");
798 info.num_bases = *num_bases;
799 info.basenames = *basenames;
800 info.new_num_max = 128;
801 info.new_num_bases = 0;
802 info.new_basenames = (char **)
803 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
804 info.mem = stream->mem;
806 res_trav (zh->session_res, "mapdb", &info, map_basenames_func);
808 for (i = 0; i<p->num_bases; i++)
809 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
811 p->new_basenames[(p->new_num_bases)++] =
812 nmem_strdup (p->mem, p->basenames[i]);
814 *num_bases = info.new_num_bases;
815 *basenames = info.new_basenames;
816 for (i = 0; i<*num_bases; i++)
817 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
820 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
823 yaz_log(log_level, "zebra_select_database %s",basename);
825 return zebra_select_databases (zh, 1, &basename);
828 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
829 const char **basenames)
838 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
839 num_bases,basenames[0]);
844 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
848 /* Check if the user has access to all databases (Seb) */
849 /* You could argue that this should happen later, after we have
850 * determined that the database(s) exist. */
851 if (zh->dbaccesslist) {
852 for (i = 0; i < num_bases; i++) {
853 const char *db = basenames[i];
855 for (p = zh->dbaccesslist; p && *p; p = pp) {
857 if ((pp = strchr(p, '+'))) {
863 if (len == strlen(db) && !strncmp(db, p, len))
867 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
873 for (i = 0; i < zh->num_basenames; i++)
874 xfree(zh->basenames[i]);
875 xfree(zh->basenames);
877 zh->num_basenames = num_bases;
878 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
879 for (i = 0; i < zh->num_basenames; i++)
880 zh->basenames[i] = xstrdup (basenames[i]);
882 cp = strrchr(basenames[0], '/');
885 len = cp - basenames[0];
886 new_reg = xmalloc(len + 1);
887 memcpy (new_reg, basenames[0], len);
891 new_reg = xstrdup ("");
892 for (i = 1; i<num_bases; i++)
896 cp1 = strrchr (basenames[i], '/');
901 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
904 if (len != cp1 - basenames[i] ||
905 memcmp (basenames[i], new_reg, len))
907 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
915 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
920 zebra_select_register (zh, new_reg);
924 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
927 if (!zh->lock_normal || !zh->lock_shadow)
929 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
935 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
937 if (approx_limit == 0)
938 approx_limit = DEFAULT_APPROX_LIMIT;
939 zh->approx_limit = approx_limit;
943 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
944 const char *setname, zint *hits)
952 yaz_log(log_level, "zebra_search_rpn");
953 zebra_clearError(zh);
957 if (zebra_begin_read(zh) == ZEBRA_FAIL)
960 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
961 zh->num_basenames, zh->basenames, setname);
967 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
969 Z_RecordComposition *comp,
970 oid_value input_format, int num_recs,
971 ZebraRetrievalRecord *recs)
973 ZebraMetaRecord *poset;
975 ZEBRA_RES ret = ZEBRA_OK;
983 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
985 zebra_clearError(zh);
988 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
993 if (zebra_begin_read (zh) == ZEBRA_FAIL)
996 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
997 for (i = 0; i<num_recs; i++)
998 pos_array[i] = recs[i].position;
999 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1002 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
1003 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1009 for (i = 0; i<num_recs; i++)
1013 recs[i].errCode = 0;
1014 recs[i].format = VAL_SUTRS;
1015 recs[i].len = strlen(poset[i].term);
1016 recs[i].buf = poset[i].term;
1017 recs[i].base = poset[i].db;
1019 else if (poset[i].sysno)
1023 zebra_snippets *hit_snippet = zebra_snippets_create();
1025 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1029 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
1031 stream, input_format, comp,
1032 &recs[i].format, &buf, &len,
1033 &recs[i].base, &recs[i].errString);
1038 recs[i].buf = (char*) odr_malloc(stream, len);
1039 memcpy(recs[i].buf, buf, len);
1043 recs[i].score = poset[i].score;
1044 recs[i].sysno = poset[i].sysno;
1045 zebra_snippets_destroy(hit_snippet);
1049 /* only need to set it once */
1050 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1052 zebra_setError_zint(zh,
1053 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1058 recs[i].buf = 0; /* no record and no error issued */
1060 recs[i].errCode = 0;
1061 recs[i].format = VAL_NONE;
1065 zebra_meta_records_destroy(zh, poset, num_recs);
1067 zebra_end_read (zh);
1072 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1074 int *num_entries, ZebraScanEntry **entries,
1076 const char *setname)
1078 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1079 Z_AttributesPlusTerm *zapt;
1083 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1086 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1089 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1090 position, num_entries, entries, is_partial,
1092 yaz_pqf_destroy (pqf_parser);
1096 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1097 oid_value attributeset,
1099 int *num_entries, ZebraScanEntry **entries,
1101 const char *setname)
1104 RSET limit_rset = 0;
1109 assert(num_entries);
1112 yaz_log(log_level, "zebra_scan");
1113 zebra_clearError(zh);
1114 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1122 limit_rset = resultSetRef(zh, setname);
1126 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1128 zebra_end_read (zh);
1132 res = rpn_scan (zh, stream, zapt, attributeset,
1133 zh->num_basenames, zh->basenames, position,
1134 num_entries, entries, is_partial, limit_rset, 0);
1135 zebra_end_read (zh);
1139 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1140 int num_input_setnames, const char **input_setnames,
1141 const char *output_setname,
1142 Z_SortKeySpecList *sort_sequence,
1148 assert(num_input_setnames>0);
1149 assert(input_setnames);
1150 assert(sort_sequence);
1151 assert(sort_status);
1152 yaz_log(log_level, "zebra_sort");
1153 zebra_clearError(zh);
1154 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1156 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1157 output_setname, sort_sequence, sort_status);
1162 int zebra_deleteResultSet(ZebraHandle zh, int function,
1163 int num_setnames, char **setnames,
1169 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1170 zebra_clearError(zh);;
1171 if (zebra_begin_read(zh))
1172 return Z_DeleteStatus_systemProblemAtTarget;
1175 case Z_DeleteResultSetRequest_list:
1176 assert(num_setnames>0);
1178 resultSetDestroy (zh, num_setnames, setnames, statuses);
1180 case Z_DeleteResultSetRequest_all:
1181 resultSetDestroy (zh, -1, 0, statuses);
1184 zebra_end_read (zh);
1185 status = Z_DeleteStatus_success;
1186 for (i = 0; i<num_setnames; i++)
1187 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1188 status = statuses[i];
1192 int zebra_errCode (ZebraHandle zh)
1196 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1199 yaz_log(log_level, "zebra_errCode: o");
1203 const char *zebra_errString (ZebraHandle zh)
1207 e= diagbib1_str (zh->errCode);
1208 yaz_log(log_level, "zebra_errString: %s",e);
1212 char *zebra_errAdd (ZebraHandle zh)
1217 yaz_log(log_level, "zebra_errAdd: %s",a);
1221 void zebra_clearError(ZebraHandle zh)
1230 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1233 const char *astring;
1238 zebra_clearError(zh);
1241 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1242 p = res_get(zs->global_res, u);
1243 xfree(zh->user_perm);
1244 zh->user_perm = xstrdup(p ? p : "r");
1246 /* Determine database access list */
1247 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1249 zh->dbaccesslist = xstrdup(astring);
1251 zh->dbaccesslist = 0;
1253 /* users that don't require a password .. */
1254 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1257 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1262 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1263 const char *record_type)
1266 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1267 database, record_type);
1268 zebra_clearError(zh);
1269 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1271 return zebra_begin_trans(zh, 1);
1274 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1277 yaz_log(log_level, "zebra_admin_import_end");
1278 zebra_clearError(zh);
1279 return zebra_end_trans(zh);
1282 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1284 ZEBRA_RES res = ZEBRA_OK;
1288 yaz_log(log_level, "zebra_admin_import_segment");
1289 zebra_clearError(zh);
1290 for (i = 0; i<segment->num_segmentRecords; i++)
1292 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1294 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1296 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1297 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1299 Odr_oct *oct = fragment->u.notExternallyTagged;
1302 if (zebra_update_record(zh,
1303 0, /* record Type */
1307 (const char *) oct->buf, oct->len,
1316 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1317 const char *rec_buf,
1319 const char *recid_buf, size_t recid_len,
1321 /* 1 = insert. Fail it already exists */
1322 /* 2 = replace. Fail it does not exist */
1323 /* 3 = delete. Fail if does not exist */
1324 /* 4 = update. Insert/replace */
1331 assert(action>0 && action <=4);
1334 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1335 zebra_clearError(zh);
1337 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1340 memcpy (recid_z, recid_buf, recid_len);
1341 recid_z[recid_len] = 0;
1343 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1346 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1349 if (action == 1) /* fail if insert */
1351 zebra_end_trans(zh);
1355 memcpy (&sysno, rinfo+1, sizeof(sysno));
1359 if (action == 2 || action == 3) /* fail if delete or update */
1361 zebra_end_trans(zh);
1364 action = 1; /* make it an insert (if it's an update).. */
1366 res = buffer_extract_record (zh, rec_buf, rec_len,
1367 action == 3 ? 1 : 0 /* delete flag */,
1373 0, /* force update */
1374 1 /* allow update */
1378 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1380 else if (action == 3)
1382 dict_delete (zh->reg->matchDict, recid_z);
1384 zebra_end_trans(zh);
1388 int delete_w_handle(const char *info, void *handle)
1390 ZebraHandle zh = (ZebraHandle) handle;
1394 if (*info == sizeof(pos))
1396 memcpy (&pos, info+1, sizeof(pos));
1397 isamb_unlink(zh->reg->isamb, pos);
1402 static int delete_SU_handle(void *handle, int ord)
1404 ZebraHandle zh = (ZebraHandle) handle;
1408 ord_len = key_SU_encode (ord, ord_buf);
1409 ord_buf[ord_len] = '\0';
1411 assert (zh->reg->isamb);
1412 dict_delete_subtree(zh->reg->dict, ord_buf,
1413 zh, delete_w_handle);
1417 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1419 ZEBRA_RES ret = ZEBRA_OK;
1421 yaz_log(log_level, "zebra_drop_database %s", db);
1422 zebra_clearError(zh);
1424 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1426 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1430 zebraExplain_curDatabase (zh->reg->zei, db);
1432 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1433 zebraExplain_removeDatabase(zh->reg->zei, zh);
1437 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1440 zebra_end_trans (zh);
1444 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1447 yaz_log(log_level, "zebra_create_database %s", db);
1449 zebra_clearError(zh);
1451 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1453 if (zebra_begin_trans (zh, 1))
1456 /* announce database */
1457 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1458 /* explainDatabase */))
1460 zebra_end_trans (zh);
1461 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1464 return zebra_end_trans (zh);
1467 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1468 const char *input_str, int input_len,
1469 char *output_str, int output_len)
1475 yaz_log(log_level, "zebra_string_norm ");
1476 zebra_clearError(zh);
1477 if (!zh->reg->zebra_maps)
1479 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1480 input_str, input_len);
1483 if (wrbuf_len(wrbuf) >= output_len)
1485 if (wrbuf_len(wrbuf))
1486 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1487 output_str[wrbuf_len(wrbuf)] = '\0';
1488 return wrbuf_len(wrbuf);
1491 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1493 char state_fname[256];
1498 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1499 zebra_clearError(zh);
1501 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1502 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1503 f = fopen (fname, "w");
1505 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1506 fprintf (f, "%c %d %ld\n", val, seqno, p);
1511 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1513 char state_fname[256];
1518 yaz_log(log_level, "zebra_get_state ");
1519 zebra_clearError(zh);
1520 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1521 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1522 f = fopen (fname, "r");
1528 fscanf (f, "%c %d", val, seqno);
1534 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1536 return zebra_begin_trans(zh, 0);
1539 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1541 return zebra_end_trans(zh);
1544 static void read_res_for_transaction(ZebraHandle zh)
1546 const char *group = res_get(zh->res, "group");
1548 /* FIXME - do we still use groups ?? */
1550 zh->m_group = group;
1551 v = res_get_prefix(zh->res, "followLinks", group, "1");
1552 zh->m_follow_links = atoi(v);
1554 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1555 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1557 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1558 zh->m_store_keys = atoi(v);
1560 v = res_get_prefix(zh->res, "storeData", group, "1");
1561 zh->m_store_data = atoi(v);
1563 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1564 zh->m_explain_database = atoi(v);
1566 v = res_get_prefix(zh->res, "openRW", group, "1");
1567 zh->m_flag_rw = atoi(v);
1569 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1570 zh->m_file_verbose_limit = atoi(v);
1573 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1576 zebra_select_default_database(zh);
1579 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1580 "zebra_begin_trans: no database selected");
1584 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1588 if (rw && !strchr(zh->user_perm, 'w'))
1592 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1604 const char *rval = 0;
1609 read_res_for_transaction(zh);
1612 if (zh->trans_no != 1)
1614 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1615 "zebra_begin_trans: no write trans within read");
1620 resultSetInvalidate (zh);
1621 zebra_register_close (zh->service, zh->reg);
1623 zh->trans_w_no = zh->trans_no;
1625 zebra_clearError(zh);
1627 zh->records_inserted = 0;
1628 zh->records_updated = 0;
1629 zh->records_deleted = 0;
1630 zh->records_processed = 0;
1632 #if HAVE_SYS_TIMES_H
1636 if (zh->shadow_enable)
1637 rval = res_get (zh->res, "shadow");
1639 for (pass = 0; pass < 2; pass++)
1643 zebra_lock_r (zh->lock_normal);
1644 zebra_lock_w (zh->lock_shadow);
1648 zebra_lock_w (zh->lock_normal);
1649 zebra_lock_w (zh->lock_shadow);
1652 zebra_get_state (zh, &val, &seqno);
1655 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1656 zebra_unlock (zh->lock_shadow);
1657 zebra_unlock (zh->lock_normal);
1661 else if (val == 'd')
1665 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1667 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1668 bf_commitClean (bfs, rval);
1673 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1680 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1684 zebra_set_state (zh, 'd', seqno);
1686 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1687 1, rval ? 1 : 0, zh->res,
1690 zh->reg->seqno = seqno;
1693 zebra_set_state (zh, 'o', seqno);
1695 zebra_unlock (zh->lock_shadow);
1696 zebra_unlock (zh->lock_normal);
1701 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1702 "zebra_begin_trans: cannot open register");
1703 yaz_log(YLOG_FATAL, "%s", zh->errString);
1715 if (zh->trans_no != 1)
1717 zebra_flush_reg (zh);
1720 zebra_clearError(zh);
1721 #if HAVE_SYS_TIMES_H
1727 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1730 if (!zh->lock_normal || !zh->lock_shadow)
1733 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1736 zebra_get_state (zh, &val, &seqno);
1742 else if (seqno != zh->reg->seqno)
1744 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1745 seqno, zh->reg->seqno);
1748 else if (zh->reg->last_val != val)
1750 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1751 val, zh->reg->last_val);
1758 zebra_lock_r (zh->lock_shadow);
1760 zebra_lock_r (zh->lock_normal);
1764 resultSetInvalidate (zh);
1765 zebra_register_close (zh->service, zh->reg);
1767 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1768 0, val == 'c' ? 1 : 0,
1769 zh->res, zh->path_reg);
1772 zebra_unlock (zh->lock_normal);
1773 zebra_unlock (zh->lock_shadow);
1775 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1778 zh->reg->last_val = val;
1779 zh->reg->seqno = seqno;
1781 read_res_for_transaction(zh);
1785 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1787 ZebraTransactionStatus dummy;
1789 yaz_log(log_level, "zebra_end_trans");
1790 return zebra_end_transaction(zh, &dummy);
1793 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1801 yaz_log(log_level, "zebra_end_transaction");
1803 status->processed = 0;
1804 status->inserted = 0;
1805 status->updated = 0;
1806 status->deleted = 0;
1810 if (!zh->res || !zh->reg)
1812 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1813 "zebra_end_trans: no open transaction");
1816 if (zh->trans_no != zh->trans_w_no)
1819 if (zh->trans_no != 0)
1822 /* release read lock */
1824 zebra_unlock (zh->lock_normal);
1825 zebra_unlock (zh->lock_shadow);
1828 { /* release write lock */
1832 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1833 rval = res_get (zh->res, "shadow");
1835 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1837 zebra_flush_reg (zh);
1839 resultSetInvalidate (zh);
1841 zebra_register_close (zh->service, zh->reg);
1844 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1845 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1846 zh->records_processed, zh->records_inserted,
1847 zh->records_updated, zh->records_deleted);
1849 status->processed = (int) zh->records_processed;
1850 status->inserted = (int) zh->records_inserted;
1851 status->updated = (int) zh->records_updated;
1852 status->deleted = (int) zh->records_deleted;
1854 zebra_get_state (zh, &val, &seqno);
1857 BFiles bfs = bfs_create (rval, zh->path_reg);
1858 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1859 bf_commitClean (bfs, rval);
1864 zebra_set_state (zh, 'o', seqno);
1866 zebra_unlock (zh->lock_shadow);
1867 zebra_unlock (zh->lock_normal);
1870 #if HAVE_SYS_TIMES_H
1872 yaz_log (log_level, "user/system: %ld/%ld",
1873 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1874 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1876 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1877 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1882 int zebra_repository_update (ZebraHandle zh, const char *path)
1886 zebra_clearError(zh);
1887 yaz_log (log_level, "updating %s", path);
1888 repositoryUpdate (zh, path);
1892 int zebra_repository_delete (ZebraHandle zh, const char *path)
1896 zebra_clearError(zh);
1897 yaz_log (log_level, "deleting %s", path);
1898 repositoryDelete (zh, path);
1902 int zebra_repository_show (ZebraHandle zh, const char *path)
1906 yaz_log(log_level, "zebra_repository_show");
1907 zebra_clearError(zh);
1908 repositoryShow (zh, path);
1912 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1919 zebra_clearError(zh);
1921 zebra_select_default_database(zh);
1924 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1927 rval = res_get (zh->res, "shadow");
1930 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1934 zebra_lock_w (zh->lock_normal);
1935 zebra_lock_r (zh->lock_shadow);
1937 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1939 zebra_get_state (zh, &val, &seqno);
1942 bf_cache (bfs, rval);
1943 if (bf_commitExists (bfs))
1946 zebra_set_state (zh, 'd', seqno);
1949 zebra_set_state (zh, 'c', seqno);
1951 yaz_log (YLOG_DEBUG, "commit start");
1952 bf_commitExec (bfs);
1957 yaz_log (YLOG_DEBUG, "commit clean");
1958 bf_commitClean (bfs, rval);
1960 zebra_set_state (zh, 'o', seqno);
1964 yaz_log (log_level, "nothing to commit");
1968 zebra_unlock (zh->lock_shadow);
1969 zebra_unlock (zh->lock_normal);
1973 ZEBRA_RES zebra_clean(ZebraHandle zh)
1976 yaz_log(log_level, "zebra_clean");
1977 return zebra_commit_ex(zh, 1);
1980 ZEBRA_RES zebra_commit(ZebraHandle zh)
1983 yaz_log(log_level, "zebra_commit");
1984 return zebra_commit_ex(zh, 0);
1987 ZEBRA_RES zebra_init(ZebraHandle zh)
1992 yaz_log(log_level, "zebra_init");
1993 zebra_clearError(zh);
1995 zebra_select_default_database(zh);
1998 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1999 "cannot select default database");
2002 rval = res_get (zh->res, "shadow");
2004 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2007 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2011 bf_cache (bfs, rval);
2015 zebra_set_state (zh, 'o', 0);
2019 ZEBRA_RES zebra_compact(ZebraHandle zh)
2023 yaz_log(log_level, "zebra_compact");
2024 zebra_clearError(zh);
2027 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2030 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
2036 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2039 yaz_log(log_level, "zebra_result");
2040 *code = zh->errCode;
2041 *addinfo = zh->errString;
2044 void zebra_shadow_enable(ZebraHandle zh, int value)
2047 yaz_log(log_level, "zebra_shadow_enable");
2048 zebra_clearError(zh);
2049 zh->shadow_enable = value;
2052 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2056 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2057 zebra_clearError(zh);
2059 if (zh->iconv_to_utf8 != 0)
2060 yaz_iconv_close(zh->iconv_to_utf8);
2061 if (zh->iconv_from_utf8 != 0)
2062 yaz_iconv_close(zh->iconv_from_utf8);
2065 yaz_iconv_open ("UTF-8", encoding);
2066 if (zh->iconv_to_utf8 == 0)
2067 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2068 zh->iconv_from_utf8 =
2069 yaz_iconv_open (encoding, "UTF-8");
2070 if (zh->iconv_to_utf8 == 0)
2071 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2076 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2079 yaz_log(log_level, "zebra_record_encoding");
2080 zebra_clearError(zh);
2081 xfree(zh->record_encoding);
2082 zh->record_encoding = 0;
2084 zh->record_encoding = xstrdup (encoding);
2088 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2093 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2094 zebra_clearError(zh);
2095 res_set(zh->res, name, value);
2098 const char *zebra_get_resource(ZebraHandle zh,
2099 const char *name, const char *defaultvalue)
2104 v = res_get_def (zh->res, name, (char *)defaultvalue);
2105 zebra_clearError(zh);
2106 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2110 /* moved from zebra_api_ext.c by pop */
2111 /* FIXME: Should this really be public??? -Heikki */
2113 int zebra_trans_no (ZebraHandle zh)
2116 yaz_log(log_level, "zebra_trans_no");
2117 return zh->trans_no;
2120 int zebra_get_shadow_enable (ZebraHandle zh)
2123 yaz_log(log_level, "zebra_get_shadow_enable");
2124 return zh->shadow_enable;
2127 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2130 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2131 zh->shadow_enable = value;
2134 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2135 so that it's identicical to the original api_records_retrieve */
2136 void api_records_retrieve (ZebraHandle zh, ODR stream,
2137 const char *setname, Z_RecordComposition *comp,
2138 oid_value input_format, int num_recs,
2139 ZebraRetrievalRecord *recs)
2141 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2145 /* ---------------------------------------------------------------------------
2146 Record insert(=update), delete
2148 If sysno is provided, then it's used to identify the record.
2149 If not, and match_criteria is provided, then sysno is guessed
2150 If not, and a record is provided, then sysno is got from there
2151 NOTE: Now returns 0 at success and updates sysno, which is an int*
2155 int zebra_add_record(ZebraHandle zh,
2156 const char *buf, int buf_size)
2159 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2162 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2163 const char *recordType,
2164 SYSNO *sysno, const char *match,
2166 const char *buf, int buf_size, int force_update)
2172 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2175 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 0); /* allow_update */
2187 zebra_end_trans(zh);
2191 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2192 const char *recordType,
2193 SYSNO* sysno, const char *match,
2195 const char *buf, int buf_size,
2203 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2205 if (buf_size < 1) buf_size = strlen(buf);
2207 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2209 res = buffer_extract_record (zh, buf, buf_size,
2210 0, /* delete_flag */
2216 1); /* allow_update */
2217 zebra_end_trans(zh);
2221 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2222 const char *recordType,
2223 SYSNO *sysno, const char *match,
2225 const char *buf, int buf_size,
2232 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2234 if (buf_size < 1) buf_size = strlen(buf);
2236 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2238 res = buffer_extract_record (zh, buf, buf_size,
2239 1, /* delete_flag */
2245 1); /* allow_update */
2246 zebra_end_trans(zh);
2250 /* ---------------------------------------------------------------------------
2254 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2255 const char *setname, zint *hits)
2258 ZEBRA_RES res = ZEBRA_OK;
2260 ODR odr = odr_createmem(ODR_ENCODE);
2265 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2267 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2271 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2272 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2276 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2280 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2288 /* ---------------------------------------------------------------------------
2289 Sort - a simplified interface, with optional read locks.
2291 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2292 const char *sort_spec,
2293 const char *output_setname,
2294 const char **input_setnames)
2296 int num_input_setnames = 0;
2297 int sort_status = 0;
2298 Z_SortKeySpecList *sort_sequence;
2302 assert(output_setname);
2303 assert(input_setnames);
2304 sort_sequence = yaz_sort_spec (stream, sort_spec);
2305 yaz_log(log_level, "sort (FIXME) ");
2308 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2309 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2313 /* we can do this, since the perl typemap code for char** will
2314 put a NULL at the end of list */
2315 while (input_setnames[num_input_setnames]) num_input_setnames++;
2317 if (zebra_begin_read (zh))
2320 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2321 output_setname, sort_sequence, &sort_status);
2327 /* ---------------------------------------------------------------------------
2328 Get BFS for Zebra system (to make alternative storage methods)
2330 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2333 return zh->reg->bfs;
2338 /* ---------------------------------------------------------------------------
2339 Set limit for search/scan
2341 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2344 zebra_limit_destroy(zh->m_limit);
2345 zh->m_limit = zebra_limit_create(complement_flag, ids);
2350 Set Error code + addinfo
2352 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2355 nmem_reset(zh->nmem_error);
2356 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2359 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2362 sprintf(vstr, ZINT_FORMAT, i);
2365 nmem_reset(zh->nmem_error);
2366 zh->errString = nmem_strdup(zh->nmem_error, vstr);