1 /* $Id: zebraapi.c,v 1.177 2005-06-13 10:29:20 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
34 #include <yaz/diagbib1.h>
35 #include <yaz/pquery.h>
36 #include <yaz/sortspec.h>
39 #include <idzebra/api.h>
41 #define DEFAULT_APPROX_LIMIT 2000000000
43 /* simple asserts to validate the most essential input args */
44 #define ASSERTZH assert(zh && zh->service)
45 #define ASSERTZHRES assert(zh && zh->service && zh->res)
46 #define ASSERTZS assert(zs)
48 static int log_level = 0;
49 static int log_level_initialized = 0;
51 static Res zebra_open_res (ZebraHandle zh);
52 static void zebra_close_res (ZebraHandle zh);
54 static void zebra_chdir (ZebraService zs)
58 yaz_log(log_level, "zebra_chdir");
59 dir = res_get (zs->global_res, "chdir");
62 yaz_log (YLOG_DEBUG, "chdir %s", dir);
70 static void zebra_flush_reg (ZebraHandle zh)
73 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)
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 */
115 zh->reg_name = xstrdup ("");
117 zh->num_basenames = 0;
120 zh->approx_limit = DEFAULT_APPROX_LIMIT;
127 zh->shadow_enable = 1;
129 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
132 yaz_iconv_open ("UTF-8", default_encoding);
133 if (zh->iconv_to_utf8 == 0)
134 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
136 zh->iconv_from_utf8 =
137 yaz_iconv_open (default_encoding, "UTF-8");
138 if (zh->iconv_to_utf8 == 0)
139 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
142 zh->record_encoding = 0;
144 zebra_mutex_cond_lock (&zs->session_lock);
146 zh->next = zs->sessions;
149 zebra_mutex_cond_unlock (&zs->session_lock);
151 zh->store_data_buf = 0;
153 zh->m_limit = zebra_limit_create(1, 0);
155 zh->nmem_error = nmem_create();
160 ZebraService zebra_start (const char *configName)
162 return zebra_start_res(configName, 0, 0);
165 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
169 if (!log_level_initialized)
171 log_level = yaz_log_module_level("zebraapi");
172 log_level_initialized = 1;
175 yaz_log(YLOG_LOG, "zebra_start %s %s",configName, ZEBRAVER);
178 if ((res = res_open (configName, def_res, over_res)))
180 const char *passwd_plain = 0;
181 const char *passwd_encrypt = 0;
182 ZebraService zh = xmalloc(sizeof(*zh));
184 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
186 zh->global_res = res;
187 zh->configName = xstrdup(configName);
192 zebra_mutex_cond_init (&zh->session_lock);
193 passwd_plain = res_get (zh->global_res, "passwd");
194 passwd_encrypt = res_get (zh->global_res, "passwd.c");
196 if (!passwd_plain && !passwd_encrypt)
197 zh->passwd_db = NULL;
200 zh->passwd_db = passwd_db_open();
202 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
206 passwd_db_file_plain(zh->passwd_db, passwd_plain);
208 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
211 zh->path_root = res_get (zh->global_res, "root");
212 zh->nmem = nmem_create();
213 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
219 void zebra_filter_info(ZebraService zs, void *cd,
220 void (*cb)(void *cd, const char *name))
224 recTypeClass_info(zs->record_classes, cd, cb);
227 void zebra_pidfname(ZebraService zs, char *path)
230 zebra_lock_prefix (zs->global_res, path);
231 strcat(path, "zebrasrv.pid");
234 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
235 int compact_flag, Res res)
237 int page_size = 4096;
238 char resource_str[200];
239 sprintf (resource_str, "dict.%.100s.pagesize", name);
243 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
244 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
246 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
250 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
251 int rw, int useshadow, Res res,
252 const char *reg_path)
254 struct zebra_register *reg;
255 int record_compression = REC_COMPRESS_NONE;
256 const char *recordCompression = 0;
257 const char *profilePath;
262 reg = xmalloc(sizeof(*reg));
265 reg->name = xstrdup (name);
272 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
273 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
275 reg->dh = data1_createx (DATA1_FLAG_XML);
281 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
284 data1_destroy(reg->dh);
290 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
292 bfs_destroy(reg->bfs);
293 data1_destroy(reg->dh);
299 getcwd(cwd, sizeof(cwd)-1);
300 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
301 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
303 data1_set_tabpath (reg->dh, profilePath);
304 data1_set_tabroot (reg->dh, reg_path);
305 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
307 reg->zebra_maps = zebra_maps_open (res, reg_path);
308 reg->rank_classes = NULL;
312 reg->keys.buf_max = 0;
314 reg->keys.codec_handle = iscz1_start();
316 reg->sortKeys.buf = 0;
317 reg->sortKeys.buf_max = 0;
328 reg->key_file_no = 0;
331 zebraRankInstall (reg, rank1_class);
332 zebraRankInstall (reg, rankzv_class);
334 recordCompression = res_get_def (res, "recordCompression", "none");
335 if (!strcmp (recordCompression, "none"))
336 record_compression = REC_COMPRESS_NONE;
337 if (!strcmp (recordCompression, "bzip2"))
338 record_compression = REC_COMPRESS_BZIP2;
340 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
342 yaz_log (YLOG_WARN, "rec_open failed");
347 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
349 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
351 yaz_log (YLOG_WARN, "dict_open failed");
354 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
356 yaz_log (YLOG_WARN, "sortIdx_open failed");
359 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
361 struct ISAMS_M_s isams_m;
362 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
363 key_isams_m(res, &isams_m))))
365 yaz_log (YLOG_WARN, "isams_open failed");
369 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
371 struct ISAMC_M_s isamc_m;
372 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
373 rw, key_isamc_m(res, &isamc_m))))
375 yaz_log (YLOG_WARN, "isamc_open failed");
379 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
381 struct ISAMC_M_s isamc_m;
383 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
384 rw, key_isamc_m(res, &isamc_m), 0)))
386 yaz_log (YLOG_WARN, "isamb_open failed");
390 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
392 struct ISAMC_M_s isamc_m;
394 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
395 rw, key_isamc_m(res, &isamc_m), 1)))
397 yaz_log (YLOG_WARN, "isamb_open failed");
401 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
403 struct ISAMC_M_s isamc_m;
405 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
406 rw, key_isamc_m(res, &isamc_m), -1)))
408 yaz_log (YLOG_WARN, "isamb_open failed");
412 reg->zei = zebraExplain_open (reg->records, reg->dh,
417 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
421 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
425 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
428 yaz_log(log_level, "zebra_admin_shutdown");
429 zebra_clearError(zh);
431 zebra_mutex_cond_lock (&zh->service->session_lock);
432 zh->service->stop_flag = 1;
433 zebra_mutex_cond_unlock (&zh->service->session_lock);
437 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
441 yaz_log(log_level, "zebra_admin_start");
442 zebra_clearError(zh);
444 zebra_mutex_cond_lock (&zs->session_lock);
445 zebra_mutex_cond_unlock (&zs->session_lock);
449 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
453 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
458 zebraExplain_close (reg->zei);
459 dict_close (reg->dict);
461 dict_close (reg->matchDict);
462 sortIdx_close (reg->sortIdx);
464 isams_close (reg->isams);
466 isamc_close (reg->isamc);
468 isamb_close (reg->isamb);
469 rec_close (®->records);
472 recTypes_destroy (reg->recTypes);
473 zebra_maps_close (reg->zebra_maps);
474 zebraRankDestroy (reg);
475 bfs_destroy (reg->bfs);
476 data1_destroy (reg->dh);
478 xfree(reg->sortKeys.buf);
479 xfree(reg->keys.buf);
480 if (reg->keys.codec_handle)
481 iscz1_stop(reg->keys.codec_handle);
487 ZEBRA_RES zebra_stop(ZebraService zs)
491 yaz_log (log_level, "zebra_stop");
495 zebra_close (zs->sessions);
498 zebra_mutex_cond_destroy (&zs->session_lock);
501 passwd_db_close (zs->passwd_db);
503 recTypeClass_destroy(zs->record_classes);
504 nmem_destroy(zs->nmem);
505 res_close (zs->global_res);
506 xfree(zs->configName);
511 ZEBRA_RES zebra_close (ZebraHandle zh)
514 struct zebra_session **sp;
517 yaz_log(log_level, "zebra_close");
524 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
525 resultSetDestroy (zh, -1, 0, 0);
528 zebra_register_close (zh->service, zh->reg);
529 zebra_close_res (zh);
531 xfree(zh->record_encoding);
533 for (i = 0; i < zh->num_basenames; i++)
534 xfree(zh->basenames[i]);
535 xfree(zh->basenames);
537 if (zh->iconv_to_utf8 != 0)
538 yaz_iconv_close (zh->iconv_to_utf8);
539 if (zh->iconv_from_utf8 != 0)
540 yaz_iconv_close (zh->iconv_from_utf8);
542 zebra_mutex_cond_lock (&zs->session_lock);
543 zebra_lock_destroy (zh->lock_normal);
544 zebra_lock_destroy (zh->lock_shadow);
556 zebra_mutex_cond_unlock (&zs->session_lock);
558 xfree(zh->user_perm);
559 zh->service = 0; /* more likely to trigger an assert */
561 zebra_limit_destroy(zh->m_limit);
563 nmem_destroy(zh->nmem_error);
570 struct map_baseinfo {
576 char **new_basenames;
580 static Res zebra_open_res (ZebraHandle zh)
589 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
590 res = res_open (fname, zh->service->global_res, 0);
592 res = zh->service->global_res;
594 else if (*zh->reg_name == 0)
596 res = zh->service->global_res;
600 yaz_log (YLOG_WARN, "no register root specified");
601 return 0; /* no path for register - fail! */
606 static void zebra_close_res (ZebraHandle zh)
610 if (zh->res != zh->service->global_res)
615 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
619 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
623 assert (zh->reg == 0);
624 assert (*zh->reg_name == 0);
630 resultSetInvalidate (zh);
631 zebra_register_close (zh->service, zh->reg);
637 zh->reg_name = xstrdup (new_reg);
641 if (zh->service->path_root)
643 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
644 strlen(zh->reg_name) + 3);
645 strcpy (zh->path_reg, zh->service->path_root);
648 strcat (zh->path_reg, "/");
649 strcat (zh->path_reg, zh->reg_name);
652 zh->res = zebra_open_res (zh);
655 zebra_lock_destroy (zh->lock_normal);
659 zebra_lock_destroy (zh->lock_shadow);
665 const char *lock_area =res_get (zh->res, "lockDir");
667 if (!lock_area && zh->path_reg)
668 res_set (zh->res, "lockDir", zh->path_reg);
669 sprintf (fname, "norm.%s.LCK", zh->reg_name);
671 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
673 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
675 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
677 if (!zh->lock_normal || !zh->lock_shadow)
681 zebra_lock_destroy(zh->lock_normal);
686 zebra_lock_destroy(zh->lock_shadow);
695 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
696 zebra_set_approx_limit(zh, approx);
700 void map_basenames_func (void *vp, const char *name, const char *value)
702 struct map_baseinfo *p = (struct map_baseinfo *) vp;
704 char fromdb[128], todb[8][128];
711 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
712 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
713 todb[5], todb[6], todb[7]);
717 for (i = 0; i<p->num_bases; i++)
718 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
721 for (i = 0; i < no; i++)
723 if (p->new_num_bases == p->new_num_max)
725 p->new_basenames[(p->new_num_bases)++] =
726 nmem_strdup (p->mem, todb[i]);
732 int zebra_select_default_database(ZebraHandle zh)
736 /* no database has been selected - so we select based on
737 resource setting (including group)
739 const char *group = res_get(zh->service->global_res, "group");
740 const char *v = res_get_prefix(zh->service->global_res,
741 "database", group, "Default");
742 return zebra_select_database(zh, v);
747 void map_basenames (ZebraHandle zh, ODR stream,
748 int *num_bases, char ***basenames)
750 struct map_baseinfo info;
751 struct map_baseinfo *p = &info;
754 yaz_log(log_level, "map_basenames ");
759 info.num_bases = *num_bases;
760 info.basenames = *basenames;
761 info.new_num_max = 128;
762 info.new_num_bases = 0;
763 info.new_basenames = (char **)
764 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
765 info.mem = stream->mem;
767 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
769 for (i = 0; i<p->num_bases; i++)
770 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
772 p->new_basenames[(p->new_num_bases)++] =
773 nmem_strdup (p->mem, p->basenames[i]);
775 *num_bases = info.new_num_bases;
776 *basenames = info.new_basenames;
777 for (i = 0; i<*num_bases; i++)
778 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
781 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
784 yaz_log(log_level, "zebra_select_database %s",basename);
786 return zebra_select_databases (zh, 1, &basename);
789 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
790 const char **basenames)
799 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
800 num_bases,basenames[0]);
805 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
808 for (i = 0; i < zh->num_basenames; i++)
809 xfree(zh->basenames[i]);
810 xfree(zh->basenames);
812 zh->num_basenames = num_bases;
813 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
814 for (i = 0; i < zh->num_basenames; i++)
815 zh->basenames[i] = xstrdup (basenames[i]);
817 cp = strrchr(basenames[0], '/');
820 len = cp - basenames[0];
821 new_reg = xmalloc(len + 1);
822 memcpy (new_reg, basenames[0], len);
826 new_reg = xstrdup ("");
827 for (i = 1; i<num_bases; i++)
831 cp1 = strrchr (basenames[i], '/');
836 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
839 if (len != cp1 - basenames[i] ||
840 memcmp (basenames[i], new_reg, len))
842 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
850 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
855 zebra_select_register (zh, new_reg);
859 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
862 if (!zh->lock_normal || !zh->lock_shadow)
864 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
870 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
872 if (approx_limit == 0)
873 approx_limit = DEFAULT_APPROX_LIMIT;
874 zh->approx_limit = approx_limit;
878 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
879 const char *setname, zint *hits)
887 yaz_log(log_level, "zebra_search_rpn");
888 zebra_clearError(zh);
892 if (zebra_begin_read(zh) == ZEBRA_FAIL)
895 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
896 zh->num_basenames, zh->basenames, setname);
902 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
904 Z_RecordComposition *comp,
905 oid_value input_format, int num_recs,
906 ZebraRetrievalRecord *recs)
908 ZebraMetaRecord *poset;
910 ZEBRA_RES ret = ZEBRA_OK;
918 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
920 zebra_clearError(zh);
923 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
928 if (zebra_begin_read (zh) == ZEBRA_FAIL)
931 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
932 for (i = 0; i<num_recs; i++)
933 pos_array[i] = recs[i].position;
934 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
937 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
938 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
944 for (i = 0; i<num_recs; i++)
949 recs[i].format = VAL_SUTRS;
950 recs[i].len = strlen(poset[i].term);
951 recs[i].buf = poset[i].term;
952 recs[i].base = poset[i].db;
954 else if (poset[i].sysno)
958 zebra_snippets *hit_snippet = zebra_snippets_create();
960 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
964 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
966 stream, input_format, comp,
967 &recs[i].format, &buf, &len,
968 &recs[i].base, &recs[i].errString);
973 recs[i].buf = (char*) odr_malloc(stream, len);
974 memcpy(recs[i].buf, buf, len);
978 recs[i].score = poset[i].score;
979 recs[i].sysno = poset[i].sysno;
980 zebra_snippets_destroy(hit_snippet);
984 /* only need to set it once */
985 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
987 zebra_setError_zint(zh,
988 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
993 recs[i].buf = 0; /* no record and no error issued */
996 recs[i].format = VAL_NONE;
1000 zebra_meta_records_destroy(zh, poset, num_recs);
1002 zebra_end_read (zh);
1007 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1009 int *num_entries, ZebraScanEntry **entries,
1012 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1013 Z_AttributesPlusTerm *zapt;
1017 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1020 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1023 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1024 position, num_entries, entries, is_partial);
1025 yaz_pqf_destroy (pqf_parser);
1029 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1030 oid_value attributeset,
1032 int *num_entries, ZebraScanEntry **entries,
1040 assert(num_entries);
1043 yaz_log(log_level, "zebra_scan");
1044 zebra_clearError(zh);
1045 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1051 res = rpn_scan (zh, stream, zapt, attributeset,
1052 zh->num_basenames, zh->basenames, position,
1053 num_entries, entries, is_partial, 0, 0);
1054 zebra_end_read (zh);
1058 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1059 int num_input_setnames, const char **input_setnames,
1060 const char *output_setname,
1061 Z_SortKeySpecList *sort_sequence,
1067 assert(num_input_setnames>0);
1068 assert(input_setnames);
1069 assert(sort_sequence);
1070 assert(sort_status);
1071 yaz_log(log_level, "zebra_sort");
1072 zebra_clearError(zh);
1073 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1075 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1076 output_setname, sort_sequence, sort_status);
1081 int zebra_deleteResultSet(ZebraHandle zh, int function,
1082 int num_setnames, char **setnames,
1088 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1089 zebra_clearError(zh);;
1090 if (zebra_begin_read(zh))
1091 return Z_DeleteStatus_systemProblemAtTarget;
1094 case Z_DeleteResultSetRequest_list:
1095 assert(num_setnames>0);
1097 resultSetDestroy (zh, num_setnames, setnames, statuses);
1099 case Z_DeleteResultSetRequest_all:
1100 resultSetDestroy (zh, -1, 0, statuses);
1103 zebra_end_read (zh);
1104 status = Z_DeleteStatus_success;
1105 for (i = 0; i<num_setnames; i++)
1106 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1107 status = statuses[i];
1111 int zebra_errCode (ZebraHandle zh)
1115 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1118 yaz_log(log_level, "zebra_errCode: o");
1122 const char *zebra_errString (ZebraHandle zh)
1126 e= diagbib1_str (zh->errCode);
1127 yaz_log(log_level, "zebra_errString: %s",e);
1131 char *zebra_errAdd (ZebraHandle zh)
1136 yaz_log(log_level, "zebra_errAdd: %s",a);
1140 void zebra_clearError(ZebraHandle zh)
1149 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1156 zebra_clearError(zh);
1159 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1160 p = res_get(zs->global_res, u);
1161 xfree(zh->user_perm);
1162 zh->user_perm = xstrdup(p ? p : "r");
1164 /* users that don't require a password .. */
1165 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1168 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1173 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1174 const char *record_type)
1177 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1178 database, record_type);
1179 zebra_clearError(zh);
1180 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1182 return zebra_begin_trans(zh, 1);
1185 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1188 yaz_log(log_level, "zebra_admin_import_end");
1189 zebra_clearError(zh);
1190 return zebra_end_trans(zh);
1193 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1195 ZEBRA_RES res = ZEBRA_OK;
1199 yaz_log(log_level, "zebra_admin_import_segment");
1200 zebra_clearError(zh);
1201 for (i = 0; i<segment->num_segmentRecords; i++)
1203 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1205 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1207 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1208 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1210 Odr_oct *oct = fragment->u.notExternallyTagged;
1213 if ( zebra_update_record(zh,
1214 0, /* record Type */
1227 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1228 const char *rec_buf,
1230 const char *recid_buf, size_t recid_len,
1232 /* 1 = insert. Fail it already exists */
1233 /* 2 = replace. Fail it does not exist */
1234 /* 3 = delete. Fail if does not exist */
1235 /* 4 = update. Insert/replace */
1242 assert(action>0 && action <=4);
1245 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1246 zebra_clearError(zh);
1248 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1251 memcpy (recid_z, recid_buf, recid_len);
1252 recid_z[recid_len] = 0;
1254 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1257 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1260 if (action == 1) /* fail if insert */
1262 zebra_end_trans(zh);
1266 memcpy (&sysno, rinfo+1, sizeof(sysno));
1270 if (action == 2 || action == 3) /* fail if delete or update */
1272 zebra_end_trans(zh);
1275 action = 1; /* make it an insert (if it's an update).. */
1277 res = buffer_extract_record (zh, rec_buf, rec_len,
1278 action == 3 ? 1 : 0 /* delete flag */,
1284 0, /* force update */
1285 1 /* allow update */
1289 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1291 else if (action == 3)
1293 dict_delete (zh->reg->matchDict, recid_z);
1295 zebra_end_trans(zh);
1299 int delete_w_handle(const char *info, void *handle)
1301 ZebraHandle zh = (ZebraHandle) handle;
1305 if (*info == sizeof(pos))
1307 memcpy (&pos, info+1, sizeof(pos));
1308 isamb_unlink(zh->reg->isamb, pos);
1313 static int delete_SU_handle(void *handle, int ord)
1315 ZebraHandle zh = (ZebraHandle) handle;
1319 ord_len = key_SU_encode (ord, ord_buf);
1320 ord_buf[ord_len] = '\0';
1322 assert (zh->reg->isamb);
1323 dict_delete_subtree(zh->reg->dict, ord_buf,
1324 zh, delete_w_handle);
1328 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1330 ZEBRA_RES ret = ZEBRA_OK;
1332 yaz_log(log_level, "zebra_drop_database %s", db);
1333 zebra_clearError(zh);
1335 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1337 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1341 zebraExplain_curDatabase (zh->reg->zei, db);
1343 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1344 zebraExplain_removeDatabase(zh->reg->zei, zh);
1348 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1351 zebra_end_trans (zh);
1355 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1358 yaz_log(log_level, "zebra_create_database %s", db);
1360 zebra_clearError(zh);
1362 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1364 if (zebra_begin_trans (zh, 1))
1367 /* announce database */
1368 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1369 /* explainDatabase */))
1371 zebra_end_trans (zh);
1372 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1375 return zebra_end_trans (zh);
1378 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1379 const char *input_str, int input_len,
1380 char *output_str, int output_len)
1386 yaz_log(log_level, "zebra_string_norm ");
1387 zebra_clearError(zh);
1388 if (!zh->reg->zebra_maps)
1390 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1391 input_str, input_len);
1394 if (wrbuf_len(wrbuf) >= output_len)
1396 if (wrbuf_len(wrbuf))
1397 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1398 output_str[wrbuf_len(wrbuf)] = '\0';
1399 return wrbuf_len(wrbuf);
1402 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1404 char state_fname[256];
1409 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1410 zebra_clearError(zh);
1412 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1413 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1414 f = fopen (fname, "w");
1416 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1417 fprintf (f, "%c %d %ld\n", val, seqno, p);
1422 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1424 char state_fname[256];
1429 yaz_log(log_level, "zebra_get_state ");
1430 zebra_clearError(zh);
1431 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1432 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1433 f = fopen (fname, "r");
1439 fscanf (f, "%c %d", val, seqno);
1445 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1447 return zebra_begin_trans(zh, 0);
1450 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1452 return zebra_end_trans(zh);
1455 static void read_res_for_transaction(ZebraHandle zh)
1457 const char *group = res_get(zh->res, "group");
1459 /* FIXME - do we still use groups ?? */
1461 zh->m_group = group;
1462 v = res_get_prefix(zh->res, "followLinks", group, "1");
1463 zh->m_follow_links = atoi(v);
1465 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1466 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1468 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1469 zh->m_store_keys = atoi(v);
1471 v = res_get_prefix(zh->res, "storeData", group, "1");
1472 zh->m_store_data = atoi(v);
1474 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1475 zh->m_explain_database = atoi(v);
1477 v = res_get_prefix(zh->res, "openRW", group, "1");
1478 zh->m_flag_rw = atoi(v);
1480 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1481 zh->m_file_verbose_limit = atoi(v);
1484 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1487 zebra_select_default_database(zh);
1490 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1491 "zebra_begin_trans: no database selected");
1495 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1499 if (rw && !strchr(zh->user_perm, 'w'))
1503 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1515 const char *rval = 0;
1520 read_res_for_transaction(zh);
1523 if (zh->trans_no != 1)
1525 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1526 "zebra_begin_trans: no write trans within read");
1531 resultSetInvalidate (zh);
1532 zebra_register_close (zh->service, zh->reg);
1534 zh->trans_w_no = zh->trans_no;
1536 zebra_clearError(zh);
1538 zh->records_inserted = 0;
1539 zh->records_updated = 0;
1540 zh->records_deleted = 0;
1541 zh->records_processed = 0;
1543 #if HAVE_SYS_TIMES_H
1547 if (zh->shadow_enable)
1548 rval = res_get (zh->res, "shadow");
1550 for (pass = 0; pass < 2; pass++)
1554 zebra_lock_r (zh->lock_normal);
1555 zebra_lock_w (zh->lock_shadow);
1559 zebra_lock_w (zh->lock_normal);
1560 zebra_lock_w (zh->lock_shadow);
1563 zebra_get_state (zh, &val, &seqno);
1566 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1567 zebra_unlock (zh->lock_shadow);
1568 zebra_unlock (zh->lock_normal);
1572 else if (val == 'd')
1576 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1578 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1579 bf_commitClean (bfs, rval);
1584 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1591 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1595 zebra_set_state (zh, 'd', seqno);
1597 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1598 1, rval ? 1 : 0, zh->res,
1601 zh->reg->seqno = seqno;
1604 zebra_set_state (zh, 'o', seqno);
1606 zebra_unlock (zh->lock_shadow);
1607 zebra_unlock (zh->lock_normal);
1612 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1613 "zebra_begin_trans: cannot open register");
1614 yaz_log(YLOG_FATAL, "%s", zh->errString);
1626 if (zh->trans_no != 1)
1628 zebra_flush_reg (zh);
1631 zebra_clearError(zh);
1632 #if HAVE_SYS_TIMES_H
1638 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1641 if (!zh->lock_normal || !zh->lock_shadow)
1644 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1647 zebra_get_state (zh, &val, &seqno);
1653 else if (seqno != zh->reg->seqno)
1655 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1656 seqno, zh->reg->seqno);
1659 else if (zh->reg->last_val != val)
1661 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1662 val, zh->reg->last_val);
1669 zebra_lock_r (zh->lock_shadow);
1671 zebra_lock_r (zh->lock_normal);
1675 resultSetInvalidate (zh);
1676 zebra_register_close (zh->service, zh->reg);
1678 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1679 0, val == 'c' ? 1 : 0,
1680 zh->res, zh->path_reg);
1683 zebra_unlock (zh->lock_normal);
1684 zebra_unlock (zh->lock_shadow);
1686 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1689 zh->reg->last_val = val;
1690 zh->reg->seqno = seqno;
1692 read_res_for_transaction(zh);
1696 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1698 ZebraTransactionStatus dummy;
1700 yaz_log(log_level, "zebra_end_trans");
1701 return zebra_end_transaction(zh, &dummy);
1704 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1712 yaz_log(log_level, "zebra_end_transaction");
1714 status->processed = 0;
1715 status->inserted = 0;
1716 status->updated = 0;
1717 status->deleted = 0;
1721 if (!zh->res || !zh->reg)
1723 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1724 "zebra_end_trans: no open transaction");
1727 if (zh->trans_no != zh->trans_w_no)
1730 if (zh->trans_no != 0)
1733 /* release read lock */
1735 zebra_unlock (zh->lock_normal);
1736 zebra_unlock (zh->lock_shadow);
1739 { /* release write lock */
1743 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1744 rval = res_get (zh->res, "shadow");
1746 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1748 zebra_flush_reg (zh);
1750 resultSetInvalidate (zh);
1752 zebra_register_close (zh->service, zh->reg);
1755 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1756 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1757 zh->records_processed, zh->records_inserted,
1758 zh->records_updated, zh->records_deleted);
1760 status->processed = (int) zh->records_processed;
1761 status->inserted = (int) zh->records_inserted;
1762 status->updated = (int) zh->records_updated;
1763 status->deleted = (int) zh->records_deleted;
1765 zebra_get_state (zh, &val, &seqno);
1768 BFiles bfs = bfs_create (rval, zh->path_reg);
1769 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1770 bf_commitClean (bfs, rval);
1775 zebra_set_state (zh, 'o', seqno);
1777 zebra_unlock (zh->lock_shadow);
1778 zebra_unlock (zh->lock_normal);
1781 #if HAVE_SYS_TIMES_H
1783 yaz_log (log_level, "user/system: %ld/%ld",
1784 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1785 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1787 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1788 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1793 int zebra_repository_update (ZebraHandle zh, const char *path)
1797 zebra_clearError(zh);
1798 yaz_log (log_level, "updating %s", path);
1799 repositoryUpdate (zh, path);
1803 int zebra_repository_delete (ZebraHandle zh, const char *path)
1807 zebra_clearError(zh);
1808 yaz_log (log_level, "deleting %s", path);
1809 repositoryDelete (zh, path);
1813 int zebra_repository_show (ZebraHandle zh, const char *path)
1817 yaz_log(log_level, "zebra_repository_show");
1818 zebra_clearError(zh);
1819 repositoryShow (zh, path);
1823 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1830 zebra_clearError(zh);
1832 zebra_select_default_database(zh);
1835 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1838 rval = res_get (zh->res, "shadow");
1841 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1845 zebra_lock_w (zh->lock_normal);
1846 zebra_lock_r (zh->lock_shadow);
1848 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1850 zebra_get_state (zh, &val, &seqno);
1853 bf_cache (bfs, rval);
1854 if (bf_commitExists (bfs))
1857 zebra_set_state (zh, 'd', seqno);
1860 zebra_set_state (zh, 'c', seqno);
1862 yaz_log (YLOG_DEBUG, "commit start");
1863 bf_commitExec (bfs);
1868 yaz_log (YLOG_DEBUG, "commit clean");
1869 bf_commitClean (bfs, rval);
1871 zebra_set_state (zh, 'o', seqno);
1875 yaz_log (log_level, "nothing to commit");
1879 zebra_unlock (zh->lock_shadow);
1880 zebra_unlock (zh->lock_normal);
1884 ZEBRA_RES zebra_clean(ZebraHandle zh)
1887 yaz_log(log_level, "zebra_clean");
1888 return zebra_commit_ex(zh, 1);
1891 ZEBRA_RES zebra_commit(ZebraHandle zh)
1894 yaz_log(log_level, "zebra_commit");
1895 return zebra_commit_ex(zh, 0);
1898 ZEBRA_RES zebra_init(ZebraHandle zh)
1903 yaz_log(log_level, "zebra_init");
1904 zebra_clearError(zh);
1906 zebra_select_default_database(zh);
1909 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1910 "cannot select default database");
1913 rval = res_get (zh->res, "shadow");
1915 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1918 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1922 bf_cache (bfs, rval);
1926 zebra_set_state (zh, 'o', 0);
1930 ZEBRA_RES zebra_compact(ZebraHandle zh)
1934 yaz_log(log_level, "zebra_compact");
1935 zebra_clearError(zh);
1938 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1941 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1947 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1950 yaz_log(log_level, "zebra_result");
1951 *code = zh->errCode;
1952 *addinfo = zh->errString;
1955 void zebra_shadow_enable(ZebraHandle zh, int value)
1958 yaz_log(log_level, "zebra_shadow_enable");
1959 zebra_clearError(zh);
1960 zh->shadow_enable = value;
1963 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1967 yaz_log(log_level, "zebra_octet_term_encoding");
1968 zebra_clearError(zh);
1970 if (zh->iconv_to_utf8 != 0)
1971 yaz_iconv_close(zh->iconv_to_utf8);
1972 if (zh->iconv_from_utf8 != 0)
1973 yaz_iconv_close(zh->iconv_from_utf8);
1976 yaz_iconv_open ("UTF-8", encoding);
1977 if (zh->iconv_to_utf8 == 0)
1978 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1979 zh->iconv_from_utf8 =
1980 yaz_iconv_open (encoding, "UTF-8");
1981 if (zh->iconv_to_utf8 == 0)
1982 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1987 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1990 yaz_log(log_level, "zebra_record_encoding");
1991 zebra_clearError(zh);
1992 xfree(zh->record_encoding);
1993 zh->record_encoding = 0;
1995 zh->record_encoding = xstrdup (encoding);
1999 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2004 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2005 zebra_clearError(zh);
2006 res_set(zh->res, name, value);
2009 const char *zebra_get_resource(ZebraHandle zh,
2010 const char *name, const char *defaultvalue)
2015 assert(defaultvalue);
2016 v = res_get_def (zh->res, name, (char *)defaultvalue);
2017 zebra_clearError(zh);
2018 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2022 /* moved from zebra_api_ext.c by pop */
2023 /* FIXME: Should this really be public??? -Heikki */
2025 int zebra_trans_no (ZebraHandle zh)
2028 yaz_log(log_level, "zebra_trans_no");
2029 return zh->trans_no;
2032 int zebra_get_shadow_enable (ZebraHandle zh)
2035 yaz_log(log_level, "zebra_get_shadow_enable");
2036 return zh->shadow_enable;
2039 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2042 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2043 zh->shadow_enable = value;
2046 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2047 so that it's identicical to the original api_records_retrieve */
2048 void api_records_retrieve (ZebraHandle zh, ODR stream,
2049 const char *setname, Z_RecordComposition *comp,
2050 oid_value input_format, int num_recs,
2051 ZebraRetrievalRecord *recs)
2053 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2057 /* ---------------------------------------------------------------------------
2058 Record insert(=update), delete
2060 If sysno is provided, then it's used to identify the record.
2061 If not, and match_criteria is provided, then sysno is guessed
2062 If not, and a record is provided, then sysno is got from there
2063 NOTE: Now returns 0 at success and updates sysno, which is an int*
2067 int zebra_add_record(ZebraHandle zh,
2068 const char *buf, int buf_size)
2071 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2074 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2075 const char *recordType,
2076 SYSNO *sysno, const char *match,
2078 const char *buf, int buf_size, int force_update)
2084 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2087 buf_size = strlen(buf);
2089 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2091 res = buffer_extract_record (zh, buf, buf_size,
2092 0, /* delete_flag */
2098 0); /* allow_update */
2099 zebra_end_trans(zh);
2103 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2104 const char *recordType,
2105 SYSNO* sysno, const char *match,
2107 const char *buf, int buf_size,
2115 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2117 if (buf_size < 1) buf_size = strlen(buf);
2119 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2121 res = buffer_extract_record (zh, buf, buf_size,
2122 0, /* delete_flag */
2128 1); /* allow_update */
2129 zebra_end_trans(zh);
2133 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2134 const char *recordType,
2135 SYSNO *sysno, const char *match,
2137 const char *buf, int buf_size,
2144 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2146 if (buf_size < 1) buf_size = strlen(buf);
2148 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2150 res = buffer_extract_record (zh, buf, buf_size,
2151 1, /* delete_flag */
2157 1); /* allow_update */
2158 zebra_end_trans(zh);
2162 /* ---------------------------------------------------------------------------
2166 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2167 const char *setname, zint *hits)
2170 ZEBRA_RES res = ZEBRA_OK;
2172 ODR odr = odr_createmem(ODR_ENCODE);
2177 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2179 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2183 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2184 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2188 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2192 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2200 /* ---------------------------------------------------------------------------
2201 Sort - a simplified interface, with optional read locks.
2203 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2204 const char *sort_spec,
2205 const char *output_setname,
2206 const char **input_setnames)
2208 int num_input_setnames = 0;
2209 int sort_status = 0;
2210 Z_SortKeySpecList *sort_sequence;
2214 assert(output_setname);
2215 assert(input_setnames);
2216 sort_sequence = yaz_sort_spec (stream, sort_spec);
2217 yaz_log(log_level, "sort (FIXME) ");
2220 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2221 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2225 /* we can do this, since the perl typemap code for char** will
2226 put a NULL at the end of list */
2227 while (input_setnames[num_input_setnames]) num_input_setnames++;
2229 if (zebra_begin_read (zh))
2232 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2233 output_setname, sort_sequence, &sort_status);
2239 /* ---------------------------------------------------------------------------
2240 Get BFS for Zebra system (to make alternative storage methods)
2242 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2245 return zh->reg->bfs;
2250 /* ---------------------------------------------------------------------------
2251 Set limit for search/scan
2253 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2256 zebra_limit_destroy(zh->m_limit);
2257 zh->m_limit = zebra_limit_create(complement_flag, ids);
2262 Set Error code + addinfo
2264 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2267 nmem_reset(zh->nmem_error);
2268 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2271 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2274 sprintf(vstr, ZINT_FORMAT, i);
2277 nmem_reset(zh->nmem_error);
2278 zh->errString = nmem_strdup(zh->nmem_error, vstr);