1 /* $Id: zebraapi.c,v 1.179 2005-06-23 06:45:46 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 Res 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)
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 */
116 zh->reg_name = xstrdup ("");
118 zh->num_basenames = 0;
121 zh->approx_limit = DEFAULT_APPROX_LIMIT;
128 zh->shadow_enable = 1;
130 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
133 yaz_iconv_open ("UTF-8", default_encoding);
134 if (zh->iconv_to_utf8 == 0)
135 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
137 zh->iconv_from_utf8 =
138 yaz_iconv_open (default_encoding, "UTF-8");
139 if (zh->iconv_to_utf8 == 0)
140 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
143 zh->record_encoding = 0;
145 zebra_mutex_cond_lock (&zs->session_lock);
147 zh->next = zs->sessions;
150 zebra_mutex_cond_unlock (&zs->session_lock);
152 zh->store_data_buf = 0;
154 zh->m_limit = zebra_limit_create(1, 0);
156 zh->nmem_error = nmem_create();
161 ZebraService zebra_start (const char *configName)
163 return zebra_start_res(configName, 0, 0);
166 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
170 if (!log_level_initialized)
172 log_level = yaz_log_module_level("zebraapi");
173 log_level_initialized = 1;
176 yaz_log(YLOG_LOG, "zebra_start %s %s",configName, ZEBRAVER);
179 if ((res = res_open (configName, def_res, over_res)))
181 const char *passwd_plain = 0;
182 const char *passwd_encrypt = 0;
183 ZebraService zh = xmalloc(sizeof(*zh));
185 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
187 zh->global_res = res;
188 zh->configName = xstrdup(configName);
193 zebra_mutex_cond_init (&zh->session_lock);
194 passwd_plain = res_get (zh->global_res, "passwd");
195 passwd_encrypt = res_get (zh->global_res, "passwd.c");
197 if (!passwd_plain && !passwd_encrypt)
198 zh->passwd_db = NULL;
201 zh->passwd_db = passwd_db_open();
203 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
207 passwd_db_file_plain(zh->passwd_db, passwd_plain);
209 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
212 zh->path_root = res_get (zh->global_res, "root");
213 zh->nmem = nmem_create();
214 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
220 void zebra_filter_info(ZebraService zs, void *cd,
221 void (*cb)(void *cd, const char *name))
225 recTypeClass_info(zs->record_classes, cd, cb);
228 void zebra_pidfname(ZebraService zs, char *path)
231 zebra_lock_prefix (zs->global_res, path);
232 strcat(path, "zebrasrv.pid");
235 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
236 int compact_flag, Res res)
238 int page_size = 4096;
239 char resource_str[200];
240 sprintf (resource_str, "dict.%.100s.pagesize", name);
244 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
245 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
247 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
251 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
252 int rw, int useshadow, Res res,
253 const char *reg_path)
255 struct zebra_register *reg;
256 int record_compression = REC_COMPRESS_NONE;
257 const char *recordCompression = 0;
258 const char *profilePath;
263 reg = xmalloc(sizeof(*reg));
266 reg->name = xstrdup (name);
273 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
274 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
276 reg->dh = data1_createx (DATA1_FLAG_XML);
282 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
285 data1_destroy(reg->dh);
291 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
293 bfs_destroy(reg->bfs);
294 data1_destroy(reg->dh);
300 getcwd(cwd, sizeof(cwd)-1);
301 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
302 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
304 data1_set_tabpath (reg->dh, profilePath);
305 data1_set_tabroot (reg->dh, reg_path);
306 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
308 reg->zebra_maps = zebra_maps_open (res, reg_path);
309 reg->rank_classes = NULL;
313 reg->keys.buf_max = 0;
315 reg->keys.codec_handle = iscz1_start();
317 reg->sortKeys.buf = 0;
318 reg->sortKeys.buf_max = 0;
320 reg->sortKeys.codec_handle = iscz1_start();
332 reg->key_file_no = 0;
335 zebraRankInstall (reg, rank1_class);
336 zebraRankInstall (reg, rankzv_class);
338 recordCompression = res_get_def (res, "recordCompression", "none");
339 if (!strcmp (recordCompression, "none"))
340 record_compression = REC_COMPRESS_NONE;
341 if (!strcmp (recordCompression, "bzip2"))
342 record_compression = REC_COMPRESS_BZIP2;
344 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
346 yaz_log (YLOG_WARN, "rec_open failed");
351 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
353 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
355 yaz_log (YLOG_WARN, "dict_open failed");
358 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
360 yaz_log (YLOG_WARN, "sortIdx_open failed");
363 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
365 struct ISAMS_M_s isams_m;
366 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
367 key_isams_m(res, &isams_m))))
369 yaz_log (YLOG_WARN, "isams_open failed");
373 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
375 struct ISAMC_M_s isamc_m;
376 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
377 rw, key_isamc_m(res, &isamc_m))))
379 yaz_log (YLOG_WARN, "isamc_open failed");
383 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
385 struct ISAMC_M_s isamc_m;
387 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
388 rw, key_isamc_m(res, &isamc_m), 0)))
390 yaz_log (YLOG_WARN, "isamb_open failed");
394 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
396 struct ISAMC_M_s isamc_m;
398 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
399 rw, key_isamc_m(res, &isamc_m), 1)))
401 yaz_log (YLOG_WARN, "isamb_open failed");
405 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
407 struct ISAMC_M_s isamc_m;
409 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
410 rw, key_isamc_m(res, &isamc_m), -1)))
412 yaz_log (YLOG_WARN, "isamb_open failed");
416 reg->zei = zebraExplain_open (reg->records, reg->dh,
421 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
425 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
429 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
432 yaz_log(log_level, "zebra_admin_shutdown");
433 zebra_clearError(zh);
435 zebra_mutex_cond_lock (&zh->service->session_lock);
436 zh->service->stop_flag = 1;
437 zebra_mutex_cond_unlock (&zh->service->session_lock);
441 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
445 yaz_log(log_level, "zebra_admin_start");
446 zebra_clearError(zh);
448 zebra_mutex_cond_lock (&zs->session_lock);
449 zebra_mutex_cond_unlock (&zs->session_lock);
453 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
457 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
462 zebraExplain_close (reg->zei);
463 dict_close (reg->dict);
465 dict_close (reg->matchDict);
466 sortIdx_close (reg->sortIdx);
468 isams_close (reg->isams);
470 isamc_close (reg->isamc);
472 isamb_close (reg->isamb);
473 rec_close (®->records);
476 recTypes_destroy (reg->recTypes);
477 zebra_maps_close (reg->zebra_maps);
478 zebraRankDestroy (reg);
479 bfs_destroy (reg->bfs);
480 data1_destroy (reg->dh);
482 xfree(reg->sortKeys.buf);
483 xfree(reg->keys.buf);
484 if (reg->keys.codec_handle)
485 iscz1_stop(reg->keys.codec_handle);
487 if (reg->sortKeys.codec_handle)
488 iscz1_stop(reg->sortKeys.codec_handle);
495 ZEBRA_RES zebra_stop(ZebraService zs)
499 yaz_log (log_level, "zebra_stop");
503 zebra_close (zs->sessions);
506 zebra_mutex_cond_destroy (&zs->session_lock);
509 passwd_db_close (zs->passwd_db);
511 recTypeClass_destroy(zs->record_classes);
512 nmem_destroy(zs->nmem);
513 res_close (zs->global_res);
514 xfree(zs->configName);
519 ZEBRA_RES zebra_close (ZebraHandle zh)
522 struct zebra_session **sp;
525 yaz_log(log_level, "zebra_close");
532 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
533 resultSetDestroy (zh, -1, 0, 0);
536 zebra_register_close (zh->service, zh->reg);
537 zebra_close_res (zh);
539 xfree(zh->record_encoding);
541 for (i = 0; i < zh->num_basenames; i++)
542 xfree(zh->basenames[i]);
543 xfree(zh->basenames);
545 if (zh->iconv_to_utf8 != 0)
546 yaz_iconv_close (zh->iconv_to_utf8);
547 if (zh->iconv_from_utf8 != 0)
548 yaz_iconv_close (zh->iconv_from_utf8);
550 zebra_mutex_cond_lock (&zs->session_lock);
551 zebra_lock_destroy (zh->lock_normal);
552 zebra_lock_destroy (zh->lock_shadow);
564 zebra_mutex_cond_unlock (&zs->session_lock);
566 xfree(zh->user_perm);
567 zh->service = 0; /* more likely to trigger an assert */
569 zebra_limit_destroy(zh->m_limit);
571 nmem_destroy(zh->nmem_error);
578 struct map_baseinfo {
584 char **new_basenames;
588 static Res zebra_open_res (ZebraHandle zh)
597 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
598 res = res_open (fname, zh->service->global_res, 0);
600 res = zh->service->global_res;
602 else if (*zh->reg_name == 0)
604 res = zh->service->global_res;
608 yaz_log (YLOG_WARN, "no register root specified");
609 return 0; /* no path for register - fail! */
614 static void zebra_close_res (ZebraHandle zh)
618 if (zh->res != zh->service->global_res)
623 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
627 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
631 assert (zh->reg == 0);
632 assert (*zh->reg_name == 0);
638 resultSetInvalidate (zh);
639 zebra_register_close (zh->service, zh->reg);
645 zh->reg_name = xstrdup (new_reg);
649 if (zh->service->path_root)
651 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
652 strlen(zh->reg_name) + 3);
653 strcpy (zh->path_reg, zh->service->path_root);
656 strcat (zh->path_reg, "/");
657 strcat (zh->path_reg, zh->reg_name);
660 zh->res = zebra_open_res (zh);
663 zebra_lock_destroy (zh->lock_normal);
667 zebra_lock_destroy (zh->lock_shadow);
673 const char *lock_area =res_get (zh->res, "lockDir");
675 if (!lock_area && zh->path_reg)
676 res_set (zh->res, "lockDir", zh->path_reg);
677 sprintf (fname, "norm.%s.LCK", zh->reg_name);
679 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
681 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
683 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
685 if (!zh->lock_normal || !zh->lock_shadow)
689 zebra_lock_destroy(zh->lock_normal);
694 zebra_lock_destroy(zh->lock_shadow);
703 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
704 zebra_set_approx_limit(zh, approx);
708 void map_basenames_func (void *vp, const char *name, const char *value)
710 struct map_baseinfo *p = (struct map_baseinfo *) vp;
712 char fromdb[128], todb[8][128];
719 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
720 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
721 todb[5], todb[6], todb[7]);
725 for (i = 0; i<p->num_bases; i++)
726 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
729 for (i = 0; i < no; i++)
731 if (p->new_num_bases == p->new_num_max)
733 p->new_basenames[(p->new_num_bases)++] =
734 nmem_strdup (p->mem, todb[i]);
740 int zebra_select_default_database(ZebraHandle zh)
744 /* no database has been selected - so we select based on
745 resource setting (including group)
747 const char *group = res_get(zh->service->global_res, "group");
748 const char *v = res_get_prefix(zh->service->global_res,
749 "database", group, "Default");
750 return zebra_select_database(zh, v);
755 void map_basenames (ZebraHandle zh, ODR stream,
756 int *num_bases, char ***basenames)
758 struct map_baseinfo info;
759 struct map_baseinfo *p = &info;
762 yaz_log(log_level, "map_basenames ");
767 info.num_bases = *num_bases;
768 info.basenames = *basenames;
769 info.new_num_max = 128;
770 info.new_num_bases = 0;
771 info.new_basenames = (char **)
772 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
773 info.mem = stream->mem;
775 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
777 for (i = 0; i<p->num_bases; i++)
778 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
780 p->new_basenames[(p->new_num_bases)++] =
781 nmem_strdup (p->mem, p->basenames[i]);
783 *num_bases = info.new_num_bases;
784 *basenames = info.new_basenames;
785 for (i = 0; i<*num_bases; i++)
786 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
789 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
792 yaz_log(log_level, "zebra_select_database %s",basename);
794 return zebra_select_databases (zh, 1, &basename);
797 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
798 const char **basenames)
807 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
808 num_bases,basenames[0]);
813 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
816 for (i = 0; i < zh->num_basenames; i++)
817 xfree(zh->basenames[i]);
818 xfree(zh->basenames);
820 zh->num_basenames = num_bases;
821 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
822 for (i = 0; i < zh->num_basenames; i++)
823 zh->basenames[i] = xstrdup (basenames[i]);
825 cp = strrchr(basenames[0], '/');
828 len = cp - basenames[0];
829 new_reg = xmalloc(len + 1);
830 memcpy (new_reg, basenames[0], len);
834 new_reg = xstrdup ("");
835 for (i = 1; i<num_bases; i++)
839 cp1 = strrchr (basenames[i], '/');
844 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
847 if (len != cp1 - basenames[i] ||
848 memcmp (basenames[i], new_reg, len))
850 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
858 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
863 zebra_select_register (zh, new_reg);
867 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
870 if (!zh->lock_normal || !zh->lock_shadow)
872 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
878 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
880 if (approx_limit == 0)
881 approx_limit = DEFAULT_APPROX_LIMIT;
882 zh->approx_limit = approx_limit;
886 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
887 const char *setname, zint *hits)
895 yaz_log(log_level, "zebra_search_rpn");
896 zebra_clearError(zh);
900 if (zebra_begin_read(zh) == ZEBRA_FAIL)
903 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
904 zh->num_basenames, zh->basenames, setname);
910 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
912 Z_RecordComposition *comp,
913 oid_value input_format, int num_recs,
914 ZebraRetrievalRecord *recs)
916 ZebraMetaRecord *poset;
918 ZEBRA_RES ret = ZEBRA_OK;
926 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
928 zebra_clearError(zh);
931 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
936 if (zebra_begin_read (zh) == ZEBRA_FAIL)
939 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
940 for (i = 0; i<num_recs; i++)
941 pos_array[i] = recs[i].position;
942 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
945 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
946 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
952 for (i = 0; i<num_recs; i++)
957 recs[i].format = VAL_SUTRS;
958 recs[i].len = strlen(poset[i].term);
959 recs[i].buf = poset[i].term;
960 recs[i].base = poset[i].db;
962 else if (poset[i].sysno)
966 zebra_snippets *hit_snippet = zebra_snippets_create();
968 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
972 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
974 stream, input_format, comp,
975 &recs[i].format, &buf, &len,
976 &recs[i].base, &recs[i].errString);
981 recs[i].buf = (char*) odr_malloc(stream, len);
982 memcpy(recs[i].buf, buf, len);
986 recs[i].score = poset[i].score;
987 recs[i].sysno = poset[i].sysno;
988 zebra_snippets_destroy(hit_snippet);
992 /* only need to set it once */
993 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
995 zebra_setError_zint(zh,
996 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1001 recs[i].buf = 0; /* no record and no error issued */
1003 recs[i].errCode = 0;
1004 recs[i].format = VAL_NONE;
1008 zebra_meta_records_destroy(zh, poset, num_recs);
1010 zebra_end_read (zh);
1015 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1017 int *num_entries, ZebraScanEntry **entries,
1020 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1021 Z_AttributesPlusTerm *zapt;
1025 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1028 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1031 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1032 position, num_entries, entries, is_partial);
1033 yaz_pqf_destroy (pqf_parser);
1037 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1038 oid_value attributeset,
1040 int *num_entries, ZebraScanEntry **entries,
1048 assert(num_entries);
1051 yaz_log(log_level, "zebra_scan");
1052 zebra_clearError(zh);
1053 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1059 res = rpn_scan (zh, stream, zapt, attributeset,
1060 zh->num_basenames, zh->basenames, position,
1061 num_entries, entries, is_partial, 0, 0);
1062 zebra_end_read (zh);
1066 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1067 int num_input_setnames, const char **input_setnames,
1068 const char *output_setname,
1069 Z_SortKeySpecList *sort_sequence,
1075 assert(num_input_setnames>0);
1076 assert(input_setnames);
1077 assert(sort_sequence);
1078 assert(sort_status);
1079 yaz_log(log_level, "zebra_sort");
1080 zebra_clearError(zh);
1081 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1083 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1084 output_setname, sort_sequence, sort_status);
1089 int zebra_deleteResultSet(ZebraHandle zh, int function,
1090 int num_setnames, char **setnames,
1096 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1097 zebra_clearError(zh);;
1098 if (zebra_begin_read(zh))
1099 return Z_DeleteStatus_systemProblemAtTarget;
1102 case Z_DeleteResultSetRequest_list:
1103 assert(num_setnames>0);
1105 resultSetDestroy (zh, num_setnames, setnames, statuses);
1107 case Z_DeleteResultSetRequest_all:
1108 resultSetDestroy (zh, -1, 0, statuses);
1111 zebra_end_read (zh);
1112 status = Z_DeleteStatus_success;
1113 for (i = 0; i<num_setnames; i++)
1114 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1115 status = statuses[i];
1119 int zebra_errCode (ZebraHandle zh)
1123 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1126 yaz_log(log_level, "zebra_errCode: o");
1130 const char *zebra_errString (ZebraHandle zh)
1134 e= diagbib1_str (zh->errCode);
1135 yaz_log(log_level, "zebra_errString: %s",e);
1139 char *zebra_errAdd (ZebraHandle zh)
1144 yaz_log(log_level, "zebra_errAdd: %s",a);
1148 void zebra_clearError(ZebraHandle zh)
1157 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1164 zebra_clearError(zh);
1167 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1168 p = res_get(zs->global_res, u);
1169 xfree(zh->user_perm);
1170 zh->user_perm = xstrdup(p ? p : "r");
1172 /* users that don't require a password .. */
1173 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1176 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1181 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1182 const char *record_type)
1185 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1186 database, record_type);
1187 zebra_clearError(zh);
1188 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1190 return zebra_begin_trans(zh, 1);
1193 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1196 yaz_log(log_level, "zebra_admin_import_end");
1197 zebra_clearError(zh);
1198 return zebra_end_trans(zh);
1201 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1203 ZEBRA_RES res = ZEBRA_OK;
1207 yaz_log(log_level, "zebra_admin_import_segment");
1208 zebra_clearError(zh);
1209 for (i = 0; i<segment->num_segmentRecords; i++)
1211 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1213 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1215 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1216 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1218 Odr_oct *oct = fragment->u.notExternallyTagged;
1221 if ( zebra_update_record(zh,
1222 0, /* record Type */
1235 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1236 const char *rec_buf,
1238 const char *recid_buf, size_t recid_len,
1240 /* 1 = insert. Fail it already exists */
1241 /* 2 = replace. Fail it does not exist */
1242 /* 3 = delete. Fail if does not exist */
1243 /* 4 = update. Insert/replace */
1250 assert(action>0 && action <=4);
1253 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1254 zebra_clearError(zh);
1256 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1259 memcpy (recid_z, recid_buf, recid_len);
1260 recid_z[recid_len] = 0;
1262 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1265 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1268 if (action == 1) /* fail if insert */
1270 zebra_end_trans(zh);
1274 memcpy (&sysno, rinfo+1, sizeof(sysno));
1278 if (action == 2 || action == 3) /* fail if delete or update */
1280 zebra_end_trans(zh);
1283 action = 1; /* make it an insert (if it's an update).. */
1285 res = buffer_extract_record (zh, rec_buf, rec_len,
1286 action == 3 ? 1 : 0 /* delete flag */,
1292 0, /* force update */
1293 1 /* allow update */
1297 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1299 else if (action == 3)
1301 dict_delete (zh->reg->matchDict, recid_z);
1303 zebra_end_trans(zh);
1307 int delete_w_handle(const char *info, void *handle)
1309 ZebraHandle zh = (ZebraHandle) handle;
1313 if (*info == sizeof(pos))
1315 memcpy (&pos, info+1, sizeof(pos));
1316 isamb_unlink(zh->reg->isamb, pos);
1321 static int delete_SU_handle(void *handle, int ord)
1323 ZebraHandle zh = (ZebraHandle) handle;
1327 ord_len = key_SU_encode (ord, ord_buf);
1328 ord_buf[ord_len] = '\0';
1330 assert (zh->reg->isamb);
1331 dict_delete_subtree(zh->reg->dict, ord_buf,
1332 zh, delete_w_handle);
1336 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1338 ZEBRA_RES ret = ZEBRA_OK;
1340 yaz_log(log_level, "zebra_drop_database %s", db);
1341 zebra_clearError(zh);
1343 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1345 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1349 zebraExplain_curDatabase (zh->reg->zei, db);
1351 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1352 zebraExplain_removeDatabase(zh->reg->zei, zh);
1356 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1359 zebra_end_trans (zh);
1363 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1366 yaz_log(log_level, "zebra_create_database %s", db);
1368 zebra_clearError(zh);
1370 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1372 if (zebra_begin_trans (zh, 1))
1375 /* announce database */
1376 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1377 /* explainDatabase */))
1379 zebra_end_trans (zh);
1380 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1383 return zebra_end_trans (zh);
1386 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1387 const char *input_str, int input_len,
1388 char *output_str, int output_len)
1394 yaz_log(log_level, "zebra_string_norm ");
1395 zebra_clearError(zh);
1396 if (!zh->reg->zebra_maps)
1398 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1399 input_str, input_len);
1402 if (wrbuf_len(wrbuf) >= output_len)
1404 if (wrbuf_len(wrbuf))
1405 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1406 output_str[wrbuf_len(wrbuf)] = '\0';
1407 return wrbuf_len(wrbuf);
1410 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1412 char state_fname[256];
1417 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1418 zebra_clearError(zh);
1420 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1421 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1422 f = fopen (fname, "w");
1424 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1425 fprintf (f, "%c %d %ld\n", val, seqno, p);
1430 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1432 char state_fname[256];
1437 yaz_log(log_level, "zebra_get_state ");
1438 zebra_clearError(zh);
1439 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1440 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1441 f = fopen (fname, "r");
1447 fscanf (f, "%c %d", val, seqno);
1453 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1455 return zebra_begin_trans(zh, 0);
1458 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1460 return zebra_end_trans(zh);
1463 static void read_res_for_transaction(ZebraHandle zh)
1465 const char *group = res_get(zh->res, "group");
1467 /* FIXME - do we still use groups ?? */
1469 zh->m_group = group;
1470 v = res_get_prefix(zh->res, "followLinks", group, "1");
1471 zh->m_follow_links = atoi(v);
1473 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1474 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1476 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1477 zh->m_store_keys = atoi(v);
1479 v = res_get_prefix(zh->res, "storeData", group, "1");
1480 zh->m_store_data = atoi(v);
1482 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1483 zh->m_explain_database = atoi(v);
1485 v = res_get_prefix(zh->res, "openRW", group, "1");
1486 zh->m_flag_rw = atoi(v);
1488 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1489 zh->m_file_verbose_limit = atoi(v);
1492 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1495 zebra_select_default_database(zh);
1498 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1499 "zebra_begin_trans: no database selected");
1503 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1507 if (rw && !strchr(zh->user_perm, 'w'))
1511 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1523 const char *rval = 0;
1528 read_res_for_transaction(zh);
1531 if (zh->trans_no != 1)
1533 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1534 "zebra_begin_trans: no write trans within read");
1539 resultSetInvalidate (zh);
1540 zebra_register_close (zh->service, zh->reg);
1542 zh->trans_w_no = zh->trans_no;
1544 zebra_clearError(zh);
1546 zh->records_inserted = 0;
1547 zh->records_updated = 0;
1548 zh->records_deleted = 0;
1549 zh->records_processed = 0;
1551 #if HAVE_SYS_TIMES_H
1555 if (zh->shadow_enable)
1556 rval = res_get (zh->res, "shadow");
1558 for (pass = 0; pass < 2; pass++)
1562 zebra_lock_r (zh->lock_normal);
1563 zebra_lock_w (zh->lock_shadow);
1567 zebra_lock_w (zh->lock_normal);
1568 zebra_lock_w (zh->lock_shadow);
1571 zebra_get_state (zh, &val, &seqno);
1574 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1575 zebra_unlock (zh->lock_shadow);
1576 zebra_unlock (zh->lock_normal);
1580 else if (val == 'd')
1584 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1586 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1587 bf_commitClean (bfs, rval);
1592 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1599 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1603 zebra_set_state (zh, 'd', seqno);
1605 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1606 1, rval ? 1 : 0, zh->res,
1609 zh->reg->seqno = seqno;
1612 zebra_set_state (zh, 'o', seqno);
1614 zebra_unlock (zh->lock_shadow);
1615 zebra_unlock (zh->lock_normal);
1620 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1621 "zebra_begin_trans: cannot open register");
1622 yaz_log(YLOG_FATAL, "%s", zh->errString);
1634 if (zh->trans_no != 1)
1636 zebra_flush_reg (zh);
1639 zebra_clearError(zh);
1640 #if HAVE_SYS_TIMES_H
1646 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1649 if (!zh->lock_normal || !zh->lock_shadow)
1652 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1655 zebra_get_state (zh, &val, &seqno);
1661 else if (seqno != zh->reg->seqno)
1663 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1664 seqno, zh->reg->seqno);
1667 else if (zh->reg->last_val != val)
1669 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1670 val, zh->reg->last_val);
1677 zebra_lock_r (zh->lock_shadow);
1679 zebra_lock_r (zh->lock_normal);
1683 resultSetInvalidate (zh);
1684 zebra_register_close (zh->service, zh->reg);
1686 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1687 0, val == 'c' ? 1 : 0,
1688 zh->res, zh->path_reg);
1691 zebra_unlock (zh->lock_normal);
1692 zebra_unlock (zh->lock_shadow);
1694 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1697 zh->reg->last_val = val;
1698 zh->reg->seqno = seqno;
1700 read_res_for_transaction(zh);
1704 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1706 ZebraTransactionStatus dummy;
1708 yaz_log(log_level, "zebra_end_trans");
1709 return zebra_end_transaction(zh, &dummy);
1712 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1720 yaz_log(log_level, "zebra_end_transaction");
1722 status->processed = 0;
1723 status->inserted = 0;
1724 status->updated = 0;
1725 status->deleted = 0;
1729 if (!zh->res || !zh->reg)
1731 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1732 "zebra_end_trans: no open transaction");
1735 if (zh->trans_no != zh->trans_w_no)
1738 if (zh->trans_no != 0)
1741 /* release read lock */
1743 zebra_unlock (zh->lock_normal);
1744 zebra_unlock (zh->lock_shadow);
1747 { /* release write lock */
1751 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1752 rval = res_get (zh->res, "shadow");
1754 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1756 zebra_flush_reg (zh);
1758 resultSetInvalidate (zh);
1760 zebra_register_close (zh->service, zh->reg);
1763 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1764 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1765 zh->records_processed, zh->records_inserted,
1766 zh->records_updated, zh->records_deleted);
1768 status->processed = (int) zh->records_processed;
1769 status->inserted = (int) zh->records_inserted;
1770 status->updated = (int) zh->records_updated;
1771 status->deleted = (int) zh->records_deleted;
1773 zebra_get_state (zh, &val, &seqno);
1776 BFiles bfs = bfs_create (rval, zh->path_reg);
1777 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1778 bf_commitClean (bfs, rval);
1783 zebra_set_state (zh, 'o', seqno);
1785 zebra_unlock (zh->lock_shadow);
1786 zebra_unlock (zh->lock_normal);
1789 #if HAVE_SYS_TIMES_H
1791 yaz_log (log_level, "user/system: %ld/%ld",
1792 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1793 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1795 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1796 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1801 int zebra_repository_update (ZebraHandle zh, const char *path)
1805 zebra_clearError(zh);
1806 yaz_log (log_level, "updating %s", path);
1807 repositoryUpdate (zh, path);
1811 int zebra_repository_delete (ZebraHandle zh, const char *path)
1815 zebra_clearError(zh);
1816 yaz_log (log_level, "deleting %s", path);
1817 repositoryDelete (zh, path);
1821 int zebra_repository_show (ZebraHandle zh, const char *path)
1825 yaz_log(log_level, "zebra_repository_show");
1826 zebra_clearError(zh);
1827 repositoryShow (zh, path);
1831 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1838 zebra_clearError(zh);
1840 zebra_select_default_database(zh);
1843 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1846 rval = res_get (zh->res, "shadow");
1849 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1853 zebra_lock_w (zh->lock_normal);
1854 zebra_lock_r (zh->lock_shadow);
1856 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1858 zebra_get_state (zh, &val, &seqno);
1861 bf_cache (bfs, rval);
1862 if (bf_commitExists (bfs))
1865 zebra_set_state (zh, 'd', seqno);
1868 zebra_set_state (zh, 'c', seqno);
1870 yaz_log (YLOG_DEBUG, "commit start");
1871 bf_commitExec (bfs);
1876 yaz_log (YLOG_DEBUG, "commit clean");
1877 bf_commitClean (bfs, rval);
1879 zebra_set_state (zh, 'o', seqno);
1883 yaz_log (log_level, "nothing to commit");
1887 zebra_unlock (zh->lock_shadow);
1888 zebra_unlock (zh->lock_normal);
1892 ZEBRA_RES zebra_clean(ZebraHandle zh)
1895 yaz_log(log_level, "zebra_clean");
1896 return zebra_commit_ex(zh, 1);
1899 ZEBRA_RES zebra_commit(ZebraHandle zh)
1902 yaz_log(log_level, "zebra_commit");
1903 return zebra_commit_ex(zh, 0);
1906 ZEBRA_RES zebra_init(ZebraHandle zh)
1911 yaz_log(log_level, "zebra_init");
1912 zebra_clearError(zh);
1914 zebra_select_default_database(zh);
1917 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1918 "cannot select default database");
1921 rval = res_get (zh->res, "shadow");
1923 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1926 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1930 bf_cache (bfs, rval);
1934 zebra_set_state (zh, 'o', 0);
1938 ZEBRA_RES zebra_compact(ZebraHandle zh)
1942 yaz_log(log_level, "zebra_compact");
1943 zebra_clearError(zh);
1946 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1949 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1955 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1958 yaz_log(log_level, "zebra_result");
1959 *code = zh->errCode;
1960 *addinfo = zh->errString;
1963 void zebra_shadow_enable(ZebraHandle zh, int value)
1966 yaz_log(log_level, "zebra_shadow_enable");
1967 zebra_clearError(zh);
1968 zh->shadow_enable = value;
1971 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1975 yaz_log(log_level, "zebra_octet_term_encoding");
1976 zebra_clearError(zh);
1978 if (zh->iconv_to_utf8 != 0)
1979 yaz_iconv_close(zh->iconv_to_utf8);
1980 if (zh->iconv_from_utf8 != 0)
1981 yaz_iconv_close(zh->iconv_from_utf8);
1984 yaz_iconv_open ("UTF-8", encoding);
1985 if (zh->iconv_to_utf8 == 0)
1986 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1987 zh->iconv_from_utf8 =
1988 yaz_iconv_open (encoding, "UTF-8");
1989 if (zh->iconv_to_utf8 == 0)
1990 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1995 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1998 yaz_log(log_level, "zebra_record_encoding");
1999 zebra_clearError(zh);
2000 xfree(zh->record_encoding);
2001 zh->record_encoding = 0;
2003 zh->record_encoding = xstrdup (encoding);
2007 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2012 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2013 zebra_clearError(zh);
2014 res_set(zh->res, name, value);
2017 const char *zebra_get_resource(ZebraHandle zh,
2018 const char *name, const char *defaultvalue)
2023 assert(defaultvalue);
2024 v = res_get_def (zh->res, name, (char *)defaultvalue);
2025 zebra_clearError(zh);
2026 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2030 /* moved from zebra_api_ext.c by pop */
2031 /* FIXME: Should this really be public??? -Heikki */
2033 int zebra_trans_no (ZebraHandle zh)
2036 yaz_log(log_level, "zebra_trans_no");
2037 return zh->trans_no;
2040 int zebra_get_shadow_enable (ZebraHandle zh)
2043 yaz_log(log_level, "zebra_get_shadow_enable");
2044 return zh->shadow_enable;
2047 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2050 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2051 zh->shadow_enable = value;
2054 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2055 so that it's identicical to the original api_records_retrieve */
2056 void api_records_retrieve (ZebraHandle zh, ODR stream,
2057 const char *setname, Z_RecordComposition *comp,
2058 oid_value input_format, int num_recs,
2059 ZebraRetrievalRecord *recs)
2061 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2065 /* ---------------------------------------------------------------------------
2066 Record insert(=update), delete
2068 If sysno is provided, then it's used to identify the record.
2069 If not, and match_criteria is provided, then sysno is guessed
2070 If not, and a record is provided, then sysno is got from there
2071 NOTE: Now returns 0 at success and updates sysno, which is an int*
2075 int zebra_add_record(ZebraHandle zh,
2076 const char *buf, int buf_size)
2079 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2082 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2083 const char *recordType,
2084 SYSNO *sysno, const char *match,
2086 const char *buf, int buf_size, int force_update)
2092 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2095 buf_size = strlen(buf);
2097 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2099 res = buffer_extract_record (zh, buf, buf_size,
2100 0, /* delete_flag */
2106 0); /* allow_update */
2107 zebra_end_trans(zh);
2111 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2112 const char *recordType,
2113 SYSNO* sysno, const char *match,
2115 const char *buf, int buf_size,
2123 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2125 if (buf_size < 1) buf_size = strlen(buf);
2127 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2129 res = buffer_extract_record (zh, buf, buf_size,
2130 0, /* delete_flag */
2136 1); /* allow_update */
2137 zebra_end_trans(zh);
2141 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2142 const char *recordType,
2143 SYSNO *sysno, const char *match,
2145 const char *buf, int buf_size,
2152 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2154 if (buf_size < 1) buf_size = strlen(buf);
2156 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2158 res = buffer_extract_record (zh, buf, buf_size,
2159 1, /* delete_flag */
2165 1); /* allow_update */
2166 zebra_end_trans(zh);
2170 /* ---------------------------------------------------------------------------
2174 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2175 const char *setname, zint *hits)
2178 ZEBRA_RES res = ZEBRA_OK;
2180 ODR odr = odr_createmem(ODR_ENCODE);
2185 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2187 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2191 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2192 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2196 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2200 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2208 /* ---------------------------------------------------------------------------
2209 Sort - a simplified interface, with optional read locks.
2211 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2212 const char *sort_spec,
2213 const char *output_setname,
2214 const char **input_setnames)
2216 int num_input_setnames = 0;
2217 int sort_status = 0;
2218 Z_SortKeySpecList *sort_sequence;
2222 assert(output_setname);
2223 assert(input_setnames);
2224 sort_sequence = yaz_sort_spec (stream, sort_spec);
2225 yaz_log(log_level, "sort (FIXME) ");
2228 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2229 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2233 /* we can do this, since the perl typemap code for char** will
2234 put a NULL at the end of list */
2235 while (input_setnames[num_input_setnames]) num_input_setnames++;
2237 if (zebra_begin_read (zh))
2240 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2241 output_setname, sort_sequence, &sort_status);
2247 /* ---------------------------------------------------------------------------
2248 Get BFS for Zebra system (to make alternative storage methods)
2250 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2253 return zh->reg->bfs;
2258 /* ---------------------------------------------------------------------------
2259 Set limit for search/scan
2261 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2264 zebra_limit_destroy(zh->m_limit);
2265 zh->m_limit = zebra_limit_create(complement_flag, ids);
2270 Set Error code + addinfo
2272 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2275 nmem_reset(zh->nmem_error);
2276 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2279 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2282 sprintf(vstr, ZINT_FORMAT, i);
2285 nmem_reset(zh->nmem_error);
2286 zh->errString = nmem_strdup(zh->nmem_error, vstr);