1 /* $Id: zebraapi.c,v 1.182 2005-08-18 12:50:17 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;
129 zh->m_staticrank = 0;
131 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
134 yaz_iconv_open ("UTF-8", default_encoding);
135 if (zh->iconv_to_utf8 == 0)
136 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
138 zh->iconv_from_utf8 =
139 yaz_iconv_open (default_encoding, "UTF-8");
140 if (zh->iconv_to_utf8 == 0)
141 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
144 zh->record_encoding = 0;
146 zebra_mutex_cond_lock (&zs->session_lock);
148 zh->next = zs->sessions;
151 zebra_mutex_cond_unlock (&zs->session_lock);
153 zh->store_data_buf = 0;
155 zh->m_limit = zebra_limit_create(1, 0);
157 zh->nmem_error = nmem_create();
162 ZebraService zebra_start (const char *configName)
164 return zebra_start_res(configName, 0, 0);
167 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
171 if (!log_level_initialized)
173 log_level = yaz_log_module_level("zebraapi");
174 log_level_initialized = 1;
177 yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER,
178 configName ? configName : "");
180 if ((res = res_open (configName, def_res, over_res)))
182 const char *passwd_plain = 0;
183 const char *passwd_encrypt = 0;
184 ZebraService zh = xmalloc(sizeof(*zh));
186 zh->global_res = res;
191 zebra_mutex_cond_init (&zh->session_lock);
192 passwd_plain = res_get (zh->global_res, "passwd");
193 passwd_encrypt = res_get (zh->global_res, "passwd.c");
195 if (!passwd_plain && !passwd_encrypt)
196 zh->passwd_db = NULL;
199 zh->passwd_db = passwd_db_open();
201 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
205 passwd_db_file_plain(zh->passwd_db, passwd_plain);
207 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
210 zh->path_root = res_get (zh->global_res, "root");
211 zh->nmem = nmem_create();
212 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
218 void zebra_filter_info(ZebraService zs, void *cd,
219 void (*cb)(void *cd, const char *name))
223 recTypeClass_info(zs->record_classes, cd, cb);
226 void zebra_pidfname(ZebraService zs, char *path)
229 zebra_lock_prefix (zs->global_res, path);
230 strcat(path, "zebrasrv.pid");
233 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
234 int compact_flag, Res res)
236 int page_size = 4096;
237 char resource_str[200];
238 sprintf (resource_str, "dict.%.100s.pagesize", name);
242 if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
243 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
245 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
249 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
250 int rw, int useshadow, Res res,
251 const char *reg_path)
253 struct zebra_register *reg;
254 int record_compression = REC_COMPRESS_NONE;
255 const char *recordCompression = 0;
256 const char *profilePath;
261 reg = xmalloc(sizeof(*reg));
264 reg->name = xstrdup (name);
271 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
272 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
274 reg->dh = data1_createx (DATA1_FLAG_XML);
280 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
283 data1_destroy(reg->dh);
289 if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL)
291 bfs_destroy(reg->bfs);
292 data1_destroy(reg->dh);
298 getcwd(cwd, sizeof(cwd)-1);
299 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
300 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
302 data1_set_tabpath (reg->dh, profilePath);
303 data1_set_tabroot (reg->dh, reg_path);
304 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
306 reg->zebra_maps = zebra_maps_open (res, reg_path);
307 reg->rank_classes = NULL;
311 reg->keys.buf_max = 0;
313 reg->keys.codec_handle = iscz1_start();
315 reg->sortKeys.buf = 0;
316 reg->sortKeys.buf_max = 0;
318 reg->sortKeys.codec_handle = iscz1_start();
330 reg->key_file_no = 0;
333 zebraRankInstall (reg, rank1_class);
334 zebraRankInstall (reg, rankzv_class);
336 recordCompression = res_get_def (res, "recordCompression", "none");
337 if (!strcmp (recordCompression, "none"))
338 record_compression = REC_COMPRESS_NONE;
339 if (!strcmp (recordCompression, "bzip2"))
340 record_compression = REC_COMPRESS_BZIP2;
342 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
344 yaz_log (YLOG_WARN, "rec_open failed");
349 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
351 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
353 yaz_log (YLOG_WARN, "dict_open failed");
356 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
358 yaz_log (YLOG_WARN, "sortIdx_open failed");
361 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
363 struct ISAMS_M_s isams_m;
364 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
365 key_isams_m(res, &isams_m))))
367 yaz_log (YLOG_WARN, "isams_open failed");
371 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
373 struct ISAMC_M_s isamc_m;
374 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
375 rw, key_isamc_m(res, &isamc_m))))
377 yaz_log (YLOG_WARN, "isamc_open failed");
381 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
383 struct ISAMC_M_s isamc_m;
385 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
386 rw, key_isamc_m(res, &isamc_m), 0)))
388 yaz_log (YLOG_WARN, "isamb_open failed");
392 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
394 struct ISAMC_M_s isamc_m;
396 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
397 rw, key_isamc_m(res, &isamc_m), 1)))
399 yaz_log (YLOG_WARN, "isamb_open failed");
403 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
405 struct ISAMC_M_s isamc_m;
407 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
408 rw, key_isamc_m(res, &isamc_m), -1)))
410 yaz_log (YLOG_WARN, "isamb_open failed");
414 reg->zei = zebraExplain_open (reg->records, reg->dh,
419 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
423 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
427 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
430 yaz_log(log_level, "zebra_admin_shutdown");
431 zebra_clearError(zh);
433 zebra_mutex_cond_lock (&zh->service->session_lock);
434 zh->service->stop_flag = 1;
435 zebra_mutex_cond_unlock (&zh->service->session_lock);
439 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
443 yaz_log(log_level, "zebra_admin_start");
444 zebra_clearError(zh);
446 zebra_mutex_cond_lock (&zs->session_lock);
447 zebra_mutex_cond_unlock (&zs->session_lock);
451 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
455 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
460 zebraExplain_close (reg->zei);
461 dict_close (reg->dict);
463 dict_close (reg->matchDict);
464 sortIdx_close (reg->sortIdx);
466 isams_close (reg->isams);
468 isamc_close (reg->isamc);
470 isamb_close (reg->isamb);
471 rec_close (®->records);
474 recTypes_destroy (reg->recTypes);
475 zebra_maps_close (reg->zebra_maps);
476 zebraRankDestroy (reg);
477 bfs_destroy (reg->bfs);
478 data1_destroy (reg->dh);
480 xfree(reg->sortKeys.buf);
481 xfree(reg->keys.buf);
482 if (reg->keys.codec_handle)
483 iscz1_stop(reg->keys.codec_handle);
485 if (reg->sortKeys.codec_handle)
486 iscz1_stop(reg->sortKeys.codec_handle);
493 ZEBRA_RES zebra_stop(ZebraService zs)
497 yaz_log (log_level, "zebra_stop");
501 zebra_close (zs->sessions);
504 zebra_mutex_cond_destroy (&zs->session_lock);
507 passwd_db_close (zs->passwd_db);
509 recTypeClass_destroy(zs->record_classes);
510 nmem_destroy(zs->nmem);
511 res_close (zs->global_res);
516 ZEBRA_RES zebra_close (ZebraHandle zh)
519 struct zebra_session **sp;
522 yaz_log(log_level, "zebra_close");
529 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
530 resultSetDestroy (zh, -1, 0, 0);
533 zebra_register_close (zh->service, zh->reg);
534 zebra_close_res (zh);
536 xfree(zh->record_encoding);
538 for (i = 0; i < zh->num_basenames; i++)
539 xfree(zh->basenames[i]);
540 xfree(zh->basenames);
542 if (zh->iconv_to_utf8 != 0)
543 yaz_iconv_close (zh->iconv_to_utf8);
544 if (zh->iconv_from_utf8 != 0)
545 yaz_iconv_close (zh->iconv_from_utf8);
547 zebra_mutex_cond_lock (&zs->session_lock);
548 zebra_lock_destroy (zh->lock_normal);
549 zebra_lock_destroy (zh->lock_shadow);
561 zebra_mutex_cond_unlock (&zs->session_lock);
563 xfree(zh->user_perm);
564 zh->service = 0; /* more likely to trigger an assert */
566 zebra_limit_destroy(zh->m_limit);
568 nmem_destroy(zh->nmem_error);
575 struct map_baseinfo {
581 char **new_basenames;
585 static Res zebra_open_res (ZebraHandle zh)
594 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
595 res = res_open (fname, zh->service->global_res, 0);
597 res = zh->service->global_res;
599 else if (*zh->reg_name == 0)
601 res = zh->service->global_res;
605 yaz_log (YLOG_WARN, "no register root specified");
606 return 0; /* no path for register - fail! */
611 static void zebra_close_res (ZebraHandle zh)
615 if (zh->res != zh->service->global_res)
620 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
624 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
628 assert (zh->reg == 0);
629 assert (*zh->reg_name == 0);
635 resultSetInvalidate (zh);
636 zebra_register_close (zh->service, zh->reg);
642 zh->reg_name = xstrdup (new_reg);
646 if (zh->service->path_root)
648 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
649 strlen(zh->reg_name) + 3);
650 strcpy (zh->path_reg, zh->service->path_root);
653 strcat (zh->path_reg, "/");
654 strcat (zh->path_reg, zh->reg_name);
657 zh->res = zebra_open_res (zh);
660 zebra_lock_destroy (zh->lock_normal);
664 zebra_lock_destroy (zh->lock_shadow);
670 const char *lock_area =res_get (zh->res, "lockDir");
672 if (!lock_area && zh->path_reg)
673 res_set (zh->res, "lockDir", zh->path_reg);
674 sprintf (fname, "norm.%s.LCK", zh->reg_name);
676 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
678 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
680 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
682 if (!zh->lock_normal || !zh->lock_shadow)
686 zebra_lock_destroy(zh->lock_normal);
691 zebra_lock_destroy(zh->lock_shadow);
700 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
701 zebra_set_approx_limit(zh, approx);
705 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
706 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
708 yaz_log(YLOG_LOG, "static rank unset");
712 void map_basenames_func (void *vp, const char *name, const char *value)
714 struct map_baseinfo *p = (struct map_baseinfo *) vp;
716 char fromdb[128], todb[8][128];
723 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
724 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
725 todb[5], todb[6], todb[7]);
729 for (i = 0; i<p->num_bases; i++)
730 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
733 for (i = 0; i < no; i++)
735 if (p->new_num_bases == p->new_num_max)
737 p->new_basenames[(p->new_num_bases)++] =
738 nmem_strdup (p->mem, todb[i]);
744 int zebra_select_default_database(ZebraHandle zh)
748 /* no database has been selected - so we select based on
749 resource setting (including group)
751 const char *group = res_get(zh->service->global_res, "group");
752 const char *v = res_get_prefix(zh->service->global_res,
753 "database", group, "Default");
754 return zebra_select_database(zh, v);
759 void map_basenames (ZebraHandle zh, ODR stream,
760 int *num_bases, char ***basenames)
762 struct map_baseinfo info;
763 struct map_baseinfo *p = &info;
766 yaz_log(log_level, "map_basenames ");
771 info.num_bases = *num_bases;
772 info.basenames = *basenames;
773 info.new_num_max = 128;
774 info.new_num_bases = 0;
775 info.new_basenames = (char **)
776 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
777 info.mem = stream->mem;
779 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
781 for (i = 0; i<p->num_bases; i++)
782 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
784 p->new_basenames[(p->new_num_bases)++] =
785 nmem_strdup (p->mem, p->basenames[i]);
787 *num_bases = info.new_num_bases;
788 *basenames = info.new_basenames;
789 for (i = 0; i<*num_bases; i++)
790 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
793 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
796 yaz_log(log_level, "zebra_select_database %s",basename);
798 return zebra_select_databases (zh, 1, &basename);
801 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
802 const char **basenames)
811 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
812 num_bases,basenames[0]);
817 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
820 for (i = 0; i < zh->num_basenames; i++)
821 xfree(zh->basenames[i]);
822 xfree(zh->basenames);
824 zh->num_basenames = num_bases;
825 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
826 for (i = 0; i < zh->num_basenames; i++)
827 zh->basenames[i] = xstrdup (basenames[i]);
829 cp = strrchr(basenames[0], '/');
832 len = cp - basenames[0];
833 new_reg = xmalloc(len + 1);
834 memcpy (new_reg, basenames[0], len);
838 new_reg = xstrdup ("");
839 for (i = 1; i<num_bases; i++)
843 cp1 = strrchr (basenames[i], '/');
848 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
851 if (len != cp1 - basenames[i] ||
852 memcmp (basenames[i], new_reg, len))
854 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
862 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
867 zebra_select_register (zh, new_reg);
871 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
874 if (!zh->lock_normal || !zh->lock_shadow)
876 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
882 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
884 if (approx_limit == 0)
885 approx_limit = DEFAULT_APPROX_LIMIT;
886 zh->approx_limit = approx_limit;
890 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
891 const char *setname, zint *hits)
899 yaz_log(log_level, "zebra_search_rpn");
900 zebra_clearError(zh);
904 if (zebra_begin_read(zh) == ZEBRA_FAIL)
907 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
908 zh->num_basenames, zh->basenames, setname);
914 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
916 Z_RecordComposition *comp,
917 oid_value input_format, int num_recs,
918 ZebraRetrievalRecord *recs)
920 ZebraMetaRecord *poset;
922 ZEBRA_RES ret = ZEBRA_OK;
930 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
932 zebra_clearError(zh);
935 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
940 if (zebra_begin_read (zh) == ZEBRA_FAIL)
943 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
944 for (i = 0; i<num_recs; i++)
945 pos_array[i] = recs[i].position;
946 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
949 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
950 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
956 for (i = 0; i<num_recs; i++)
961 recs[i].format = VAL_SUTRS;
962 recs[i].len = strlen(poset[i].term);
963 recs[i].buf = poset[i].term;
964 recs[i].base = poset[i].db;
966 else if (poset[i].sysno)
970 zebra_snippets *hit_snippet = zebra_snippets_create();
972 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
976 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
978 stream, input_format, comp,
979 &recs[i].format, &buf, &len,
980 &recs[i].base, &recs[i].errString);
985 recs[i].buf = (char*) odr_malloc(stream, len);
986 memcpy(recs[i].buf, buf, len);
990 recs[i].score = poset[i].score;
991 recs[i].sysno = poset[i].sysno;
992 zebra_snippets_destroy(hit_snippet);
996 /* only need to set it once */
997 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
999 zebra_setError_zint(zh,
1000 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1005 recs[i].buf = 0; /* no record and no error issued */
1007 recs[i].errCode = 0;
1008 recs[i].format = VAL_NONE;
1012 zebra_meta_records_destroy(zh, poset, num_recs);
1014 zebra_end_read (zh);
1019 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1021 int *num_entries, ZebraScanEntry **entries,
1023 const char *setname)
1025 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1026 Z_AttributesPlusTerm *zapt;
1030 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1033 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1036 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
1037 position, num_entries, entries, is_partial,
1039 yaz_pqf_destroy (pqf_parser);
1043 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1044 oid_value attributeset,
1046 int *num_entries, ZebraScanEntry **entries,
1048 const char *setname)
1051 RSET limit_rset = 0;
1056 assert(num_entries);
1059 yaz_log(log_level, "zebra_scan");
1060 zebra_clearError(zh);
1061 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1069 limit_rset = resultSetRef(zh, setname);
1073 YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1075 zebra_end_read (zh);
1079 res = rpn_scan (zh, stream, zapt, attributeset,
1080 zh->num_basenames, zh->basenames, position,
1081 num_entries, entries, is_partial, limit_rset, 0);
1082 zebra_end_read (zh);
1086 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1087 int num_input_setnames, const char **input_setnames,
1088 const char *output_setname,
1089 Z_SortKeySpecList *sort_sequence,
1095 assert(num_input_setnames>0);
1096 assert(input_setnames);
1097 assert(sort_sequence);
1098 assert(sort_status);
1099 yaz_log(log_level, "zebra_sort");
1100 zebra_clearError(zh);
1101 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1103 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1104 output_setname, sort_sequence, sort_status);
1109 int zebra_deleteResultSet(ZebraHandle zh, int function,
1110 int num_setnames, char **setnames,
1116 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1117 zebra_clearError(zh);;
1118 if (zebra_begin_read(zh))
1119 return Z_DeleteStatus_systemProblemAtTarget;
1122 case Z_DeleteResultSetRequest_list:
1123 assert(num_setnames>0);
1125 resultSetDestroy (zh, num_setnames, setnames, statuses);
1127 case Z_DeleteResultSetRequest_all:
1128 resultSetDestroy (zh, -1, 0, statuses);
1131 zebra_end_read (zh);
1132 status = Z_DeleteStatus_success;
1133 for (i = 0; i<num_setnames; i++)
1134 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1135 status = statuses[i];
1139 int zebra_errCode (ZebraHandle zh)
1143 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1146 yaz_log(log_level, "zebra_errCode: o");
1150 const char *zebra_errString (ZebraHandle zh)
1154 e= diagbib1_str (zh->errCode);
1155 yaz_log(log_level, "zebra_errString: %s",e);
1159 char *zebra_errAdd (ZebraHandle zh)
1164 yaz_log(log_level, "zebra_errAdd: %s",a);
1168 void zebra_clearError(ZebraHandle zh)
1177 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1184 zebra_clearError(zh);
1187 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1188 p = res_get(zs->global_res, u);
1189 xfree(zh->user_perm);
1190 zh->user_perm = xstrdup(p ? p : "r");
1192 /* users that don't require a password .. */
1193 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1196 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1201 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1202 const char *record_type)
1205 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1206 database, record_type);
1207 zebra_clearError(zh);
1208 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1210 return zebra_begin_trans(zh, 1);
1213 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1216 yaz_log(log_level, "zebra_admin_import_end");
1217 zebra_clearError(zh);
1218 return zebra_end_trans(zh);
1221 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1223 ZEBRA_RES res = ZEBRA_OK;
1227 yaz_log(log_level, "zebra_admin_import_segment");
1228 zebra_clearError(zh);
1229 for (i = 0; i<segment->num_segmentRecords; i++)
1231 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1233 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1235 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1236 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1238 Odr_oct *oct = fragment->u.notExternallyTagged;
1241 if ( zebra_update_record(zh,
1242 0, /* record Type */
1255 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1256 const char *rec_buf,
1258 const char *recid_buf, size_t recid_len,
1260 /* 1 = insert. Fail it already exists */
1261 /* 2 = replace. Fail it does not exist */
1262 /* 3 = delete. Fail if does not exist */
1263 /* 4 = update. Insert/replace */
1270 assert(action>0 && action <=4);
1273 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1274 zebra_clearError(zh);
1276 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1279 memcpy (recid_z, recid_buf, recid_len);
1280 recid_z[recid_len] = 0;
1282 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1285 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1288 if (action == 1) /* fail if insert */
1290 zebra_end_trans(zh);
1294 memcpy (&sysno, rinfo+1, sizeof(sysno));
1298 if (action == 2 || action == 3) /* fail if delete or update */
1300 zebra_end_trans(zh);
1303 action = 1; /* make it an insert (if it's an update).. */
1305 res = buffer_extract_record (zh, rec_buf, rec_len,
1306 action == 3 ? 1 : 0 /* delete flag */,
1312 0, /* force update */
1313 1 /* allow update */
1317 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1319 else if (action == 3)
1321 dict_delete (zh->reg->matchDict, recid_z);
1323 zebra_end_trans(zh);
1327 int delete_w_handle(const char *info, void *handle)
1329 ZebraHandle zh = (ZebraHandle) handle;
1333 if (*info == sizeof(pos))
1335 memcpy (&pos, info+1, sizeof(pos));
1336 isamb_unlink(zh->reg->isamb, pos);
1341 static int delete_SU_handle(void *handle, int ord)
1343 ZebraHandle zh = (ZebraHandle) handle;
1347 ord_len = key_SU_encode (ord, ord_buf);
1348 ord_buf[ord_len] = '\0';
1350 assert (zh->reg->isamb);
1351 dict_delete_subtree(zh->reg->dict, ord_buf,
1352 zh, delete_w_handle);
1356 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1358 ZEBRA_RES ret = ZEBRA_OK;
1360 yaz_log(log_level, "zebra_drop_database %s", db);
1361 zebra_clearError(zh);
1363 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1365 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1369 zebraExplain_curDatabase (zh->reg->zei, db);
1371 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1372 zebraExplain_removeDatabase(zh->reg->zei, zh);
1376 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1379 zebra_end_trans (zh);
1383 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
1386 yaz_log(log_level, "zebra_create_database %s", db);
1388 zebra_clearError(zh);
1390 if (zebra_select_database (zh, db) == ZEBRA_FAIL)
1392 if (zebra_begin_trans (zh, 1))
1395 /* announce database */
1396 if (zebraExplain_newDatabase (zh->reg->zei, db, 0
1397 /* explainDatabase */))
1399 zebra_end_trans (zh);
1400 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1403 return zebra_end_trans (zh);
1406 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1407 const char *input_str, int input_len,
1408 char *output_str, int output_len)
1414 yaz_log(log_level, "zebra_string_norm ");
1415 zebra_clearError(zh);
1416 if (!zh->reg->zebra_maps)
1418 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1419 input_str, input_len);
1422 if (wrbuf_len(wrbuf) >= output_len)
1424 if (wrbuf_len(wrbuf))
1425 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1426 output_str[wrbuf_len(wrbuf)] = '\0';
1427 return wrbuf_len(wrbuf);
1430 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1432 char state_fname[256];
1437 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1438 zebra_clearError(zh);
1440 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1441 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1442 f = fopen (fname, "w");
1444 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1445 fprintf (f, "%c %d %ld\n", val, seqno, p);
1450 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1452 char state_fname[256];
1457 yaz_log(log_level, "zebra_get_state ");
1458 zebra_clearError(zh);
1459 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1460 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1461 f = fopen (fname, "r");
1467 fscanf (f, "%c %d", val, seqno);
1473 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1475 return zebra_begin_trans(zh, 0);
1478 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1480 return zebra_end_trans(zh);
1483 static void read_res_for_transaction(ZebraHandle zh)
1485 const char *group = res_get(zh->res, "group");
1487 /* FIXME - do we still use groups ?? */
1489 zh->m_group = group;
1490 v = res_get_prefix(zh->res, "followLinks", group, "1");
1491 zh->m_follow_links = atoi(v);
1493 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1494 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1496 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1497 zh->m_store_keys = atoi(v);
1499 v = res_get_prefix(zh->res, "storeData", group, "1");
1500 zh->m_store_data = atoi(v);
1502 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1503 zh->m_explain_database = atoi(v);
1505 v = res_get_prefix(zh->res, "openRW", group, "1");
1506 zh->m_flag_rw = atoi(v);
1508 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1509 zh->m_file_verbose_limit = atoi(v);
1512 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1515 zebra_select_default_database(zh);
1518 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1519 "zebra_begin_trans: no database selected");
1523 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1527 if (rw && !strchr(zh->user_perm, 'w'))
1531 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1543 const char *rval = 0;
1548 read_res_for_transaction(zh);
1551 if (zh->trans_no != 1)
1553 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1554 "zebra_begin_trans: no write trans within read");
1559 resultSetInvalidate (zh);
1560 zebra_register_close (zh->service, zh->reg);
1562 zh->trans_w_no = zh->trans_no;
1564 zebra_clearError(zh);
1566 zh->records_inserted = 0;
1567 zh->records_updated = 0;
1568 zh->records_deleted = 0;
1569 zh->records_processed = 0;
1571 #if HAVE_SYS_TIMES_H
1575 if (zh->shadow_enable)
1576 rval = res_get (zh->res, "shadow");
1578 for (pass = 0; pass < 2; pass++)
1582 zebra_lock_r (zh->lock_normal);
1583 zebra_lock_w (zh->lock_shadow);
1587 zebra_lock_w (zh->lock_normal);
1588 zebra_lock_w (zh->lock_shadow);
1591 zebra_get_state (zh, &val, &seqno);
1594 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1595 zebra_unlock (zh->lock_shadow);
1596 zebra_unlock (zh->lock_normal);
1600 else if (val == 'd')
1604 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1606 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1607 bf_commitClean (bfs, rval);
1612 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1619 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1623 zebra_set_state (zh, 'd', seqno);
1625 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1626 1, rval ? 1 : 0, zh->res,
1629 zh->reg->seqno = seqno;
1632 zebra_set_state (zh, 'o', seqno);
1634 zebra_unlock (zh->lock_shadow);
1635 zebra_unlock (zh->lock_normal);
1640 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1641 "zebra_begin_trans: cannot open register");
1642 yaz_log(YLOG_FATAL, "%s", zh->errString);
1654 if (zh->trans_no != 1)
1656 zebra_flush_reg (zh);
1659 zebra_clearError(zh);
1660 #if HAVE_SYS_TIMES_H
1666 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1669 if (!zh->lock_normal || !zh->lock_shadow)
1672 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1675 zebra_get_state (zh, &val, &seqno);
1681 else if (seqno != zh->reg->seqno)
1683 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1684 seqno, zh->reg->seqno);
1687 else if (zh->reg->last_val != val)
1689 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1690 val, zh->reg->last_val);
1697 zebra_lock_r (zh->lock_shadow);
1699 zebra_lock_r (zh->lock_normal);
1703 resultSetInvalidate (zh);
1704 zebra_register_close (zh->service, zh->reg);
1706 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1707 0, val == 'c' ? 1 : 0,
1708 zh->res, zh->path_reg);
1711 zebra_unlock (zh->lock_normal);
1712 zebra_unlock (zh->lock_shadow);
1714 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1717 zh->reg->last_val = val;
1718 zh->reg->seqno = seqno;
1720 read_res_for_transaction(zh);
1724 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1726 ZebraTransactionStatus dummy;
1728 yaz_log(log_level, "zebra_end_trans");
1729 return zebra_end_transaction(zh, &dummy);
1732 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1740 yaz_log(log_level, "zebra_end_transaction");
1742 status->processed = 0;
1743 status->inserted = 0;
1744 status->updated = 0;
1745 status->deleted = 0;
1749 if (!zh->res || !zh->reg)
1751 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1752 "zebra_end_trans: no open transaction");
1755 if (zh->trans_no != zh->trans_w_no)
1758 if (zh->trans_no != 0)
1761 /* release read lock */
1763 zebra_unlock (zh->lock_normal);
1764 zebra_unlock (zh->lock_shadow);
1767 { /* release write lock */
1771 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1772 rval = res_get (zh->res, "shadow");
1774 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1776 zebra_flush_reg (zh);
1778 resultSetInvalidate (zh);
1780 zebra_register_close (zh->service, zh->reg);
1783 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1784 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1785 zh->records_processed, zh->records_inserted,
1786 zh->records_updated, zh->records_deleted);
1788 status->processed = (int) zh->records_processed;
1789 status->inserted = (int) zh->records_inserted;
1790 status->updated = (int) zh->records_updated;
1791 status->deleted = (int) zh->records_deleted;
1793 zebra_get_state (zh, &val, &seqno);
1796 BFiles bfs = bfs_create (rval, zh->path_reg);
1797 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1798 bf_commitClean (bfs, rval);
1803 zebra_set_state (zh, 'o', seqno);
1805 zebra_unlock (zh->lock_shadow);
1806 zebra_unlock (zh->lock_normal);
1809 #if HAVE_SYS_TIMES_H
1811 yaz_log (log_level, "user/system: %ld/%ld",
1812 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1813 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1815 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1816 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1821 int zebra_repository_update (ZebraHandle zh, const char *path)
1825 zebra_clearError(zh);
1826 yaz_log (log_level, "updating %s", path);
1827 repositoryUpdate (zh, path);
1831 int zebra_repository_delete (ZebraHandle zh, const char *path)
1835 zebra_clearError(zh);
1836 yaz_log (log_level, "deleting %s", path);
1837 repositoryDelete (zh, path);
1841 int zebra_repository_show (ZebraHandle zh, const char *path)
1845 yaz_log(log_level, "zebra_repository_show");
1846 zebra_clearError(zh);
1847 repositoryShow (zh, path);
1851 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1858 zebra_clearError(zh);
1860 zebra_select_default_database(zh);
1863 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1866 rval = res_get (zh->res, "shadow");
1869 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1873 zebra_lock_w (zh->lock_normal);
1874 zebra_lock_r (zh->lock_shadow);
1876 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1878 zebra_get_state (zh, &val, &seqno);
1881 bf_cache (bfs, rval);
1882 if (bf_commitExists (bfs))
1885 zebra_set_state (zh, 'd', seqno);
1888 zebra_set_state (zh, 'c', seqno);
1890 yaz_log (YLOG_DEBUG, "commit start");
1891 bf_commitExec (bfs);
1896 yaz_log (YLOG_DEBUG, "commit clean");
1897 bf_commitClean (bfs, rval);
1899 zebra_set_state (zh, 'o', seqno);
1903 yaz_log (log_level, "nothing to commit");
1907 zebra_unlock (zh->lock_shadow);
1908 zebra_unlock (zh->lock_normal);
1912 ZEBRA_RES zebra_clean(ZebraHandle zh)
1915 yaz_log(log_level, "zebra_clean");
1916 return zebra_commit_ex(zh, 1);
1919 ZEBRA_RES zebra_commit(ZebraHandle zh)
1922 yaz_log(log_level, "zebra_commit");
1923 return zebra_commit_ex(zh, 0);
1926 ZEBRA_RES zebra_init(ZebraHandle zh)
1931 yaz_log(log_level, "zebra_init");
1932 zebra_clearError(zh);
1934 zebra_select_default_database(zh);
1937 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1938 "cannot select default database");
1941 rval = res_get (zh->res, "shadow");
1943 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1946 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1950 bf_cache (bfs, rval);
1954 zebra_set_state (zh, 'o', 0);
1958 ZEBRA_RES zebra_compact(ZebraHandle zh)
1962 yaz_log(log_level, "zebra_compact");
1963 zebra_clearError(zh);
1966 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1969 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1975 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1978 yaz_log(log_level, "zebra_result");
1979 *code = zh->errCode;
1980 *addinfo = zh->errString;
1983 void zebra_shadow_enable(ZebraHandle zh, int value)
1986 yaz_log(log_level, "zebra_shadow_enable");
1987 zebra_clearError(zh);
1988 zh->shadow_enable = value;
1991 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1995 yaz_log(log_level, "zebra_octet_term_encoding");
1996 zebra_clearError(zh);
1998 if (zh->iconv_to_utf8 != 0)
1999 yaz_iconv_close(zh->iconv_to_utf8);
2000 if (zh->iconv_from_utf8 != 0)
2001 yaz_iconv_close(zh->iconv_from_utf8);
2004 yaz_iconv_open ("UTF-8", encoding);
2005 if (zh->iconv_to_utf8 == 0)
2006 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2007 zh->iconv_from_utf8 =
2008 yaz_iconv_open (encoding, "UTF-8");
2009 if (zh->iconv_to_utf8 == 0)
2010 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2015 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
2018 yaz_log(log_level, "zebra_record_encoding");
2019 zebra_clearError(zh);
2020 xfree(zh->record_encoding);
2021 zh->record_encoding = 0;
2023 zh->record_encoding = xstrdup (encoding);
2027 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2032 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2033 zebra_clearError(zh);
2034 res_set(zh->res, name, value);
2037 const char *zebra_get_resource(ZebraHandle zh,
2038 const char *name, const char *defaultvalue)
2043 v = res_get_def (zh->res, name, (char *)defaultvalue);
2044 zebra_clearError(zh);
2045 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2049 /* moved from zebra_api_ext.c by pop */
2050 /* FIXME: Should this really be public??? -Heikki */
2052 int zebra_trans_no (ZebraHandle zh)
2055 yaz_log(log_level, "zebra_trans_no");
2056 return zh->trans_no;
2059 int zebra_get_shadow_enable (ZebraHandle zh)
2062 yaz_log(log_level, "zebra_get_shadow_enable");
2063 return zh->shadow_enable;
2066 void zebra_set_shadow_enable (ZebraHandle zh, int value)
2069 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2070 zh->shadow_enable = value;
2073 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
2074 so that it's identicical to the original api_records_retrieve */
2075 void api_records_retrieve (ZebraHandle zh, ODR stream,
2076 const char *setname, Z_RecordComposition *comp,
2077 oid_value input_format, int num_recs,
2078 ZebraRetrievalRecord *recs)
2080 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2084 /* ---------------------------------------------------------------------------
2085 Record insert(=update), delete
2087 If sysno is provided, then it's used to identify the record.
2088 If not, and match_criteria is provided, then sysno is guessed
2089 If not, and a record is provided, then sysno is got from there
2090 NOTE: Now returns 0 at success and updates sysno, which is an int*
2094 int zebra_add_record(ZebraHandle zh,
2095 const char *buf, int buf_size)
2098 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2101 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2102 const char *recordType,
2103 SYSNO *sysno, const char *match,
2105 const char *buf, int buf_size, int force_update)
2111 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2114 buf_size = strlen(buf);
2116 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2118 res = buffer_extract_record (zh, buf, buf_size,
2119 0, /* delete_flag */
2125 0); /* allow_update */
2126 zebra_end_trans(zh);
2130 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2131 const char *recordType,
2132 SYSNO* sysno, const char *match,
2134 const char *buf, int buf_size,
2142 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2144 if (buf_size < 1) buf_size = strlen(buf);
2146 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2148 res = buffer_extract_record (zh, buf, buf_size,
2149 0, /* delete_flag */
2155 1); /* allow_update */
2156 zebra_end_trans(zh);
2160 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2161 const char *recordType,
2162 SYSNO *sysno, const char *match,
2164 const char *buf, int buf_size,
2171 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2173 if (buf_size < 1) buf_size = strlen(buf);
2175 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2177 res = buffer_extract_record (zh, buf, buf_size,
2178 1, /* delete_flag */
2184 1); /* allow_update */
2185 zebra_end_trans(zh);
2189 /* ---------------------------------------------------------------------------
2193 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2194 const char *setname, zint *hits)
2197 ZEBRA_RES res = ZEBRA_OK;
2199 ODR odr = odr_createmem(ODR_ENCODE);
2204 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2206 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2210 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2211 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2215 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2219 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2227 /* ---------------------------------------------------------------------------
2228 Sort - a simplified interface, with optional read locks.
2230 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2231 const char *sort_spec,
2232 const char *output_setname,
2233 const char **input_setnames)
2235 int num_input_setnames = 0;
2236 int sort_status = 0;
2237 Z_SortKeySpecList *sort_sequence;
2241 assert(output_setname);
2242 assert(input_setnames);
2243 sort_sequence = yaz_sort_spec (stream, sort_spec);
2244 yaz_log(log_level, "sort (FIXME) ");
2247 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2248 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2252 /* we can do this, since the perl typemap code for char** will
2253 put a NULL at the end of list */
2254 while (input_setnames[num_input_setnames]) num_input_setnames++;
2256 if (zebra_begin_read (zh))
2259 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2260 output_setname, sort_sequence, &sort_status);
2266 /* ---------------------------------------------------------------------------
2267 Get BFS for Zebra system (to make alternative storage methods)
2269 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2272 return zh->reg->bfs;
2277 /* ---------------------------------------------------------------------------
2278 Set limit for search/scan
2280 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2283 zebra_limit_destroy(zh->m_limit);
2284 zh->m_limit = zebra_limit_create(complement_flag, ids);
2289 Set Error code + addinfo
2291 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2294 nmem_reset(zh->nmem_error);
2295 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2298 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2301 sprintf(vstr, ZINT_FORMAT, i);
2304 nmem_reset(zh->nmem_error);
2305 zh->errString = nmem_strdup(zh->nmem_error, vstr);