1 /* $Id: zebraapi.c,v 1.165 2005-05-06 18:59:35 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 /* simple asserts to validate the most essential input args */
42 #define ASSERTZH assert(zh && zh->service)
43 #define ASSERTZHRES assert(zh && zh->service && zh->res)
44 #define ASSERTZS assert(zs)
46 static int log_level = 0;
47 static int log_level_initialized = 0;
49 static Res zebra_open_res (ZebraHandle zh);
50 static void zebra_close_res (ZebraHandle zh);
52 static void zebra_chdir (ZebraService zs)
56 yaz_log(log_level, "zebra_chdir");
57 dir = res_get (zs->global_res, "chdir");
60 yaz_log (YLOG_DEBUG, "chdir %s", dir);
68 static void zebra_flush_reg (ZebraHandle zh)
71 yaz_log(log_level, "zebra_flush_reg");
73 zebraExplain_flush (zh->reg->zei, zh);
75 extract_flushWriteKeys (zh, 1 /* final */);
76 zebra_index_merge (zh );
79 static struct zebra_register *zebra_register_open (ZebraService zs,
81 int rw, int useshadow,
83 const char *reg_path);
84 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
86 ZebraHandle zebra_open (ZebraService zs)
89 const char *default_encoding;
90 if (!log_level_initialized)
92 log_level = yaz_log_module_level("zebraapi");
93 log_level_initialized = 1;
96 yaz_log(log_level, "zebra_open");
101 zh = (ZebraHandle) xmalloc(sizeof(*zh));
102 yaz_log (YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
105 zh->reg = 0; /* no register attached yet */
113 zh->reg_name = xstrdup ("");
115 zh->num_basenames = 0;
124 zh->shadow_enable = 1;
126 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
129 yaz_iconv_open ("UTF-8", default_encoding);
130 if (zh->iconv_to_utf8 == 0)
131 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported",
133 zh->iconv_from_utf8 =
134 yaz_iconv_open (default_encoding, "UTF-8");
135 if (zh->iconv_to_utf8 == 0)
136 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
139 zh->record_encoding = 0;
141 zebra_mutex_cond_lock (&zs->session_lock);
143 zh->next = zs->sessions;
146 zebra_mutex_cond_unlock (&zs->session_lock);
148 zh->store_data_buf = 0;
150 zh->m_limit = zebra_limit_create(0, 0);
155 ZebraService zebra_start (const char *configName)
157 return zebra_start_res(configName, 0, 0);
160 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
164 if (!log_level_initialized)
166 log_level = yaz_log_module_level("zebraapi");
167 log_level_initialized = 1;
170 yaz_log(log_level, "zebra_start %s",configName);
173 if ((res = res_open (configName, def_res, over_res)))
175 ZebraService zh = xmalloc(sizeof(*zh));
177 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
179 zh->global_res = res;
180 zh->configName = xstrdup(configName);
185 zebra_mutex_cond_init (&zh->session_lock);
186 if (!res_get (zh->global_res, "passwd"))
187 zh->passwd_db = NULL;
190 zh->passwd_db = passwd_db_open ();
192 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
194 passwd_db_file (zh->passwd_db,
195 res_get (zh->global_res, "passwd"));
197 zh->path_root = res_get (zh->global_res, "root");
198 zh->nmem = nmem_create();
199 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
205 void zebra_filter_info(ZebraService zs, void *cd,
206 void (*cb)(void *cd, const char *name))
210 recTypeClass_info(zs->record_classes, cd, cb);
213 void zebra_pidfname(ZebraService zs, char *path)
216 zebra_lock_prefix (zs->global_res, path);
217 strcat(path, "zebrasrv.pid");
220 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
221 int compact_flag, Res res)
223 int page_size = 4096;
224 char resource_str[200];
226 sprintf (resource_str, "dict.%.100s.pagesize", name);
230 v = res_get(res, resource_str);
234 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
237 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
242 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
243 int rw, int useshadow, Res res,
244 const char *reg_path)
246 struct zebra_register *reg;
247 int record_compression = REC_COMPRESS_NONE;
248 const char *recordCompression = 0;
249 const char *profilePath;
254 reg = xmalloc(sizeof(*reg));
257 reg->name = xstrdup (name);
264 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
265 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
267 reg->dh = data1_createx (DATA1_FLAG_XML);
270 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
273 data1_destroy(reg->dh);
277 bf_cache (reg->bfs, res_get (res, "shadow"));
279 getcwd(cwd, sizeof(cwd)-1);
280 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
281 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
283 data1_set_tabpath (reg->dh, profilePath);
284 data1_set_tabroot (reg->dh, reg_path);
285 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
287 reg->zebra_maps = zebra_maps_open (res, reg_path);
288 reg->rank_classes = NULL;
292 reg->keys.buf_max = 0;
294 reg->keys.codec_handle = iscz1_start();
296 reg->sortKeys.buf = 0;
297 reg->sortKeys.buf_max = 0;
308 reg->key_file_no = 0;
311 zebraRankInstall (reg, rank1_class);
312 zebraRankInstall (reg, rankzv_class);
314 recordCompression = res_get_def (res, "recordCompression", "none");
315 if (!strcmp (recordCompression, "none"))
316 record_compression = REC_COMPRESS_NONE;
317 if (!strcmp (recordCompression, "bzip2"))
318 record_compression = REC_COMPRESS_BZIP2;
320 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
322 yaz_log (YLOG_WARN, "rec_open failed");
327 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
329 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
331 yaz_log (YLOG_WARN, "dict_open failed");
334 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
336 yaz_log (YLOG_WARN, "sortIdx_open failed");
339 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
341 struct ISAMS_M_s isams_m;
342 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
343 key_isams_m(res, &isams_m))))
345 yaz_log (YLOG_WARN, "isams_open failed");
349 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
351 struct ISAMC_M_s isamc_m;
352 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
353 rw, key_isamc_m(res, &isamc_m))))
355 yaz_log (YLOG_WARN, "isamc_open failed");
359 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
361 struct ISAMC_M_s isamc_m;
363 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
364 rw, key_isamc_m(res, &isamc_m), 0)))
366 yaz_log (YLOG_WARN, "isamb_open failed");
370 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
372 struct ISAMC_M_s isamc_m;
374 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
375 rw, key_isamc_m(res, &isamc_m), 1)))
377 yaz_log (YLOG_WARN, "isamb_open failed");
381 if (res_get_match (res, "isam", "null", 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), -1)))
388 yaz_log (YLOG_WARN, "isamb_open failed");
392 reg->zei = zebraExplain_open (reg->records, reg->dh,
397 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
401 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
405 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
408 yaz_log(log_level, "zebra_admin_shutdown");
411 zebra_mutex_cond_lock (&zh->service->session_lock);
412 zh->service->stop_flag = 1;
413 zebra_mutex_cond_unlock (&zh->service->session_lock);
417 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
421 yaz_log(log_level, "zebra_admin_start");
424 zebra_mutex_cond_lock (&zs->session_lock);
425 zebra_mutex_cond_unlock (&zs->session_lock);
429 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
433 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
438 zebraExplain_close (reg->zei);
439 dict_close (reg->dict);
441 dict_close (reg->matchDict);
442 sortIdx_close (reg->sortIdx);
444 isams_close (reg->isams);
446 isamc_close (reg->isamc);
448 isamb_close (reg->isamb);
449 rec_close (®->records);
452 recTypes_destroy (reg->recTypes);
453 zebra_maps_close (reg->zebra_maps);
454 zebraRankDestroy (reg);
455 bfs_destroy (reg->bfs);
456 data1_destroy (reg->dh);
458 xfree(reg->sortKeys.buf);
459 xfree(reg->keys.buf);
460 if (reg->keys.codec_handle)
461 iscz1_stop(reg->keys.codec_handle);
467 ZEBRA_RES zebra_stop(ZebraService zs)
471 yaz_log (log_level, "zebra_stop");
475 zebra_close (zs->sessions);
478 zebra_mutex_cond_destroy (&zs->session_lock);
481 passwd_db_close (zs->passwd_db);
483 recTypeClass_destroy(zs->record_classes);
484 nmem_destroy(zs->nmem);
485 res_close (zs->global_res);
486 xfree(zs->configName);
491 ZEBRA_RES zebra_close (ZebraHandle zh)
494 struct zebra_session **sp;
497 yaz_log(log_level, "zebra_close");
504 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
505 resultSetDestroy (zh, -1, 0, 0);
508 zebra_register_close (zh->service, zh->reg);
509 zebra_close_res (zh);
511 xfree(zh->record_encoding);
513 for (i = 0; i < zh->num_basenames; i++)
514 xfree(zh->basenames[i]);
515 xfree(zh->basenames);
517 if (zh->iconv_to_utf8 != 0)
518 yaz_iconv_close (zh->iconv_to_utf8);
519 if (zh->iconv_from_utf8 != 0)
520 yaz_iconv_close (zh->iconv_from_utf8);
522 zebra_mutex_cond_lock (&zs->session_lock);
523 zebra_lock_destroy (zh->lock_normal);
524 zebra_lock_destroy (zh->lock_shadow);
536 zebra_mutex_cond_unlock (&zs->session_lock);
538 xfree(zh->user_perm);
539 zh->service = 0; /* more likely to trigger an assert */
541 zebra_limit_destroy(zh->m_limit);
548 struct map_baseinfo {
554 char **new_basenames;
558 static Res zebra_open_res (ZebraHandle zh)
567 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
568 res = res_open (fname, zh->service->global_res, 0);
570 res = zh->service->global_res;
572 else if (*zh->reg_name == 0)
574 res = zh->service->global_res;
578 yaz_log (YLOG_WARN, "no register root specified");
579 return 0; /* no path for register - fail! */
584 static void zebra_close_res (ZebraHandle zh)
588 if (zh->res != zh->service->global_res)
593 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
597 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
601 assert (zh->reg == 0);
602 assert (*zh->reg_name == 0);
608 resultSetInvalidate (zh);
609 zebra_register_close (zh->service, zh->reg);
615 zh->reg_name = xstrdup (new_reg);
619 if (zh->service->path_root)
621 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
622 strlen(zh->reg_name) + 3);
623 strcpy (zh->path_reg, zh->service->path_root);
626 strcat (zh->path_reg, "/");
627 strcat (zh->path_reg, zh->reg_name);
630 zh->res = zebra_open_res (zh);
633 zebra_lock_destroy (zh->lock_normal);
637 zebra_lock_destroy (zh->lock_shadow);
643 const char *lock_area =res_get (zh->res, "lockDir");
645 if (!lock_area && zh->path_reg)
646 res_set (zh->res, "lockDir", zh->path_reg);
647 sprintf (fname, "norm.%s.LCK", zh->reg_name);
649 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
651 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
653 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
655 if (!zh->lock_normal || !zh->lock_shadow)
659 zebra_lock_destroy(zh->lock_normal);
664 zebra_lock_destroy(zh->lock_shadow);
674 void map_basenames_func (void *vp, const char *name, const char *value)
676 struct map_baseinfo *p = (struct map_baseinfo *) vp;
678 char fromdb[128], todb[8][128];
685 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
686 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
687 todb[5], todb[6], todb[7]);
691 for (i = 0; i<p->num_bases; i++)
692 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
695 for (i = 0; i < no; i++)
697 if (p->new_num_bases == p->new_num_max)
699 p->new_basenames[(p->new_num_bases)++] =
700 nmem_strdup (p->mem, todb[i]);
706 int zebra_select_default_database(ZebraHandle zh)
710 /* no database has been selected - so we select based on
711 resource setting (including group)
713 const char *group = res_get(zh->service->global_res, "group");
714 const char *v = res_get_prefix(zh->service->global_res,
715 "database", group, "Default");
716 return zebra_select_database(zh, v);
721 void map_basenames (ZebraHandle zh, ODR stream,
722 int *num_bases, char ***basenames)
724 struct map_baseinfo info;
725 struct map_baseinfo *p = &info;
728 yaz_log(log_level, "map_basenames ");
735 info.num_bases = *num_bases;
736 info.basenames = *basenames;
737 info.new_num_max = 128;
738 info.new_num_bases = 0;
739 info.new_basenames = (char **)
740 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
741 info.mem = stream->mem;
743 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
745 for (i = 0; i<p->num_bases; i++)
746 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
748 p->new_basenames[(p->new_num_bases)++] =
749 nmem_strdup (p->mem, p->basenames[i]);
751 *num_bases = info.new_num_bases;
752 *basenames = info.new_basenames;
753 for (i = 0; i<*num_bases; i++)
754 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
757 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
760 yaz_log(log_level, "zebra_select_database %s",basename);
763 return zebra_select_databases (zh, 1, &basename);
766 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
767 const char **basenames)
776 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
777 num_bases,basenames[0]);
782 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
785 for (i = 0; i < zh->num_basenames; i++)
786 xfree(zh->basenames[i]);
787 xfree(zh->basenames);
789 zh->num_basenames = num_bases;
790 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
791 for (i = 0; i < zh->num_basenames; i++)
792 zh->basenames[i] = xstrdup (basenames[i]);
794 cp = strrchr(basenames[0], '/');
797 len = cp - basenames[0];
798 new_reg = xmalloc(len + 1);
799 memcpy (new_reg, basenames[0], len);
803 new_reg = xstrdup ("");
804 for (i = 1; i<num_bases; i++)
808 cp1 = strrchr (basenames[i], '/');
813 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
816 if (len != cp1 - basenames[i] ||
817 memcmp (basenames[i], new_reg, len))
819 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
827 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
832 zebra_select_register (zh, new_reg);
836 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
839 if (!zh->lock_normal || !zh->lock_shadow)
841 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
847 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
848 const char *setname, zint *hits)
856 yaz_log(log_level, "zebra_search_rpn");
861 if (zebra_begin_read(zh) == ZEBRA_FAIL)
864 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
865 zh->num_basenames, zh->basenames, setname);
871 ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream,
873 Z_RecordComposition *comp,
874 oid_value input_format, int num_recs,
875 ZebraRetrievalRecord *recs)
877 ZebraMetaRecord *poset;
879 ZEBRA_RES ret = ZEBRA_OK;
887 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
892 zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
893 zh->errString = odr_strdup(stream, setname);
899 if (zebra_begin_read (zh) == ZEBRA_FAIL)
902 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
903 for (i = 0; i<num_recs; i++)
904 pos_array[i] = recs[i].position;
905 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
908 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
909 zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
910 zh->errString = nmem_strdup (stream->mem, setname);
915 for (i = 0; i<num_recs; i++)
920 recs[i].format = VAL_SUTRS;
921 recs[i].len = strlen(poset[i].term);
922 recs[i].buf = poset[i].term;
923 recs[i].base = poset[i].db;
925 else if (poset[i].sysno)
930 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
931 stream, input_format, comp,
932 &recs[i].format, &buf, &len,
933 &recs[i].base, &recs[i].errString);
937 recs[i].buf = (char*) odr_malloc(stream, len);
938 memcpy(recs[i].buf, buf, len);
942 recs[i].score = poset[i].score;
943 recs[i].sysno = poset[i].sysno;
949 sprintf (num_str, ZINT_FORMAT, pos_array[i]);
950 zh->errCode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
951 zh->errString = odr_strdup (stream, num_str);
956 zebra_meta_records_destroy(zh, poset, num_recs);
963 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
965 int *num_entries, ZebraScanEntry **entries,
968 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
969 Z_AttributesPlusTerm *zapt;
973 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
976 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
979 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
980 position, num_entries, entries, is_partial);
981 yaz_pqf_destroy (pqf_parser);
985 ZEBRA_RES zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
986 oid_value attributeset,
988 int *num_entries, ZebraScanEntry **entries,
999 yaz_log(log_level, "zebra_scan");
1001 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1007 res = rpn_scan (zh, stream, zapt, attributeset,
1008 zh->num_basenames, zh->basenames, position,
1009 num_entries, entries, is_partial, 0, 0);
1010 zebra_end_read (zh);
1014 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1015 int num_input_setnames, const char **input_setnames,
1016 const char *output_setname,
1017 Z_SortKeySpecList *sort_sequence,
1022 assert(num_input_setnames>0);
1023 assert(input_setnames);
1024 assert(sort_sequence);
1025 assert(sort_status);
1026 yaz_log(log_level, "zebra_sort");
1028 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1030 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
1031 output_setname, sort_sequence, sort_status);
1032 return zebra_end_read(zh);
1035 int zebra_deleteResultSet(ZebraHandle zh, int function,
1036 int num_setnames, char **setnames,
1042 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1044 if (zebra_begin_read(zh))
1045 return Z_DeleteStatus_systemProblemAtTarget;
1048 case Z_DeleteResultSetRequest_list:
1049 assert(num_setnames>0);
1051 resultSetDestroy (zh, num_setnames, setnames, statuses);
1053 case Z_DeleteResultSetRequest_all:
1054 resultSetDestroy (zh, -1, 0, statuses);
1057 zebra_end_read (zh);
1058 status = Z_DeleteStatus_success;
1059 for (i = 0; i<num_setnames; i++)
1060 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1061 status = statuses[i];
1065 int zebra_errCode (ZebraHandle zh)
1069 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1072 yaz_log(log_level, "zebra_errCode: o");
1076 const char *zebra_errString (ZebraHandle zh)
1080 e= diagbib1_str (zh->errCode);
1081 yaz_log(log_level, "zebra_errString: %s",e);
1085 char *zebra_errAdd (ZebraHandle zh)
1090 yaz_log(log_level, "zebra_errAdd: %s",a);
1094 void zebra_clearError(ZebraHandle zh)
1103 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1113 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1114 p = res_get(zs->global_res, u);
1115 xfree(zh->user_perm);
1116 zh->user_perm = xstrdup(p ? p : "r");
1118 /* users that don't require a password .. */
1119 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1122 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1127 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1128 const char *record_type)
1131 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1132 database, record_type);
1134 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1136 return zebra_begin_trans(zh, 1);
1139 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1142 yaz_log(log_level, "zebra_admin_import_end");
1144 return zebra_end_trans(zh);
1147 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1149 ZEBRA_RES res = ZEBRA_OK;
1153 yaz_log(log_level, "zebra_admin_import_segment");
1155 for (i = 0; i<segment->num_segmentRecords; i++)
1157 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1159 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1161 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1162 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1164 Odr_oct *oct = fragment->u.notExternallyTagged;
1167 if ( zebra_update_record(zh,
1168 0, /* record Type */
1181 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1182 const char *rec_buf,
1184 const char *recid_buf, size_t recid_len,
1186 /* 1 = insert. Fail it already exists */
1187 /* 2 = replace. Fail it does not exist */
1188 /* 3 = delete. Fail if does not exist */
1189 /* 4 = update. Insert/replace */
1196 assert(action>0 && action <=4);
1199 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1202 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1205 memcpy (recid_z, recid_buf, recid_len);
1206 recid_z[recid_len] = 0;
1208 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1211 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1214 if (action == 1) /* fail if insert */
1216 zebra_end_trans(zh);
1220 memcpy (&sysno, rinfo+1, sizeof(sysno));
1224 if (action == 2 || action == 3) /* fail if delete or update */
1226 zebra_end_trans(zh);
1229 action = 1; /* make it an insert (if it's an update).. */
1231 res = buffer_extract_record (zh, rec_buf, rec_len,
1232 action == 3 ? 1 : 0 /* delete flag */,
1238 0, /* force update */
1239 1 /* allow update */
1243 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1245 else if (action == 3)
1247 dict_delete (zh->reg->matchDict, recid_z);
1249 zebra_end_trans(zh);
1253 int delete_w_handle(const char *info, void *handle)
1255 ZebraHandle zh = (ZebraHandle) handle;
1259 if (*info == sizeof(pos))
1261 memcpy (&pos, info+1, sizeof(pos));
1262 isamb_unlink(zh->reg->isamb, pos);
1267 static int delete_SU_handle(void *handle, int ord)
1269 ZebraHandle zh = (ZebraHandle) handle;
1273 ord_len = key_SU_encode (ord, ord_buf);
1274 ord_buf[ord_len] = '\0';
1276 assert (zh->reg->isamb);
1277 dict_delete_subtree(zh->reg->dict, ord_buf,
1278 zh, delete_w_handle);
1282 ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database)
1284 ZEBRA_RES ret = ZEBRA_OK;
1286 yaz_log(log_level, "zebra_drop_database");
1289 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1291 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1295 zebraExplain_curDatabase (zh->reg->zei, database);
1297 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1298 zebraExplain_removeDatabase(zh->reg->zei, zh);
1302 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1305 zebra_end_trans (zh);
1309 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database)
1312 yaz_log(log_level, "zebra_create_database %s", database);
1316 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1318 if (zebra_begin_trans (zh, 1))
1321 /* announce database */
1322 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1323 /* explainDatabase */))
1325 zebra_end_trans (zh);
1326 zh->errCode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
1327 zh->errString = "database already exist";
1330 return zebra_end_trans (zh);
1333 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1334 const char *input_str, int input_len,
1335 char *output_str, int output_len)
1341 yaz_log(log_level, "zebra_string_norm ");
1343 if (!zh->reg->zebra_maps)
1345 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1346 input_str, input_len);
1349 if (wrbuf_len(wrbuf) >= output_len)
1351 if (wrbuf_len(wrbuf))
1352 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1353 output_str[wrbuf_len(wrbuf)] = '\0';
1354 return wrbuf_len(wrbuf);
1357 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1359 char state_fname[256];
1364 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1367 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1368 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1369 f = fopen (fname, "w");
1371 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1372 fprintf (f, "%c %d %ld\n", val, seqno, p);
1377 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1379 char state_fname[256];
1384 yaz_log(log_level, "zebra_get_state ");
1386 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1387 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1388 f = fopen (fname, "r");
1394 fscanf (f, "%c %d", val, seqno);
1400 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1402 return zebra_begin_trans(zh, 0);
1405 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1407 return zebra_end_trans(zh);
1410 static void read_res_for_transaction(ZebraHandle zh)
1412 const char *group = res_get(zh->res, "group");
1414 /* FIXME - do we still use groups ?? */
1416 zh->m_group = group;
1417 v = res_get_prefix(zh->res, "followLinks", group, "1");
1418 zh->m_follow_links = atoi(v);
1420 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1421 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1423 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1424 zh->m_store_keys = atoi(v);
1426 v = res_get_prefix(zh->res, "storeData", group, "1");
1427 zh->m_store_data = atoi(v);
1429 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1430 zh->m_explain_database = atoi(v);
1432 v = res_get_prefix(zh->res, "openRW", group, "1");
1433 zh->m_flag_rw = atoi(v);
1435 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1436 zh->m_file_verbose_limit = atoi(v);
1439 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1442 zebra_select_default_database(zh);
1445 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1446 zh->errString = "zebra_begin_trans: no database selected";
1450 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1454 if (rw && !strchr(zh->user_perm, 'w'))
1457 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE;
1469 const char *rval = 0;
1474 read_res_for_transaction(zh);
1477 if (zh->trans_no != 1)
1479 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1480 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1485 resultSetInvalidate (zh);
1486 zebra_register_close (zh->service, zh->reg);
1488 zh->trans_w_no = zh->trans_no;
1492 zh->records_inserted = 0;
1493 zh->records_updated = 0;
1494 zh->records_deleted = 0;
1495 zh->records_processed = 0;
1497 #if HAVE_SYS_TIMES_H
1501 if (zh->shadow_enable)
1502 rval = res_get (zh->res, "shadow");
1504 for (pass = 0; pass < 2; pass++)
1508 zebra_lock_r (zh->lock_normal);
1509 zebra_lock_w (zh->lock_shadow);
1513 zebra_lock_w (zh->lock_normal);
1514 zebra_lock_w (zh->lock_shadow);
1517 zebra_get_state (zh, &val, &seqno);
1520 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1521 zebra_unlock (zh->lock_shadow);
1522 zebra_unlock (zh->lock_normal);
1526 else if (val == 'd')
1530 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1532 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1533 bf_commitClean (bfs, rval);
1538 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1545 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1549 zebra_set_state (zh, 'd', seqno);
1551 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1552 1, rval ? 1 : 0, zh->res,
1555 zh->reg->seqno = seqno;
1558 zebra_set_state (zh, 'o', seqno);
1560 zebra_unlock (zh->lock_shadow);
1561 zebra_unlock (zh->lock_normal);
1566 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1567 zh->errString = "zebra_begin_trans: cannot open register";
1568 yaz_log(YLOG_FATAL, zh->errString);
1580 if (zh->trans_no != 1)
1582 zebra_flush_reg (zh);
1586 #if HAVE_SYS_TIMES_H
1592 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1595 if (!zh->lock_normal || !zh->lock_shadow)
1598 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1601 zebra_get_state (zh, &val, &seqno);
1607 else if (seqno != zh->reg->seqno)
1609 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1610 seqno, zh->reg->seqno);
1613 else if (zh->reg->last_val != val)
1615 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1616 val, zh->reg->last_val);
1623 zebra_lock_r (zh->lock_shadow);
1625 zebra_lock_r (zh->lock_normal);
1629 resultSetInvalidate (zh);
1630 zebra_register_close (zh->service, zh->reg);
1632 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1633 0, val == 'c' ? 1 : 0,
1634 zh->res, zh->path_reg);
1637 zebra_unlock (zh->lock_normal);
1638 zebra_unlock (zh->lock_shadow);
1640 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1643 zh->reg->last_val = val;
1644 zh->reg->seqno = seqno;
1646 read_res_for_transaction(zh);
1650 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1652 ZebraTransactionStatus dummy;
1654 yaz_log(log_level, "zebra_end_trans");
1655 return zebra_end_transaction(zh, &dummy);
1658 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1666 yaz_log(log_level, "zebra_end_transaction");
1668 status->processed = 0;
1669 status->inserted = 0;
1670 status->updated = 0;
1671 status->deleted = 0;
1675 if (!zh->res || !zh->reg)
1677 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1678 zh->errString = "zebra_end_trans: no open transaction";
1681 if (zh->trans_no != zh->trans_w_no)
1684 if (zh->trans_no != 0)
1687 /* release read lock */
1689 zebra_unlock (zh->lock_normal);
1690 zebra_unlock (zh->lock_shadow);
1693 { /* release write lock */
1697 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1698 rval = res_get (zh->res, "shadow");
1700 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1702 zebra_flush_reg (zh);
1704 resultSetInvalidate (zh);
1706 zebra_register_close (zh->service, zh->reg);
1709 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1710 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1711 zh->records_processed, zh->records_inserted,
1712 zh->records_updated, zh->records_deleted);
1714 status->processed = (int) zh->records_processed;
1715 status->inserted = (int) zh->records_inserted;
1716 status->updated = (int) zh->records_updated;
1717 status->deleted = (int) zh->records_deleted;
1719 zebra_get_state (zh, &val, &seqno);
1722 BFiles bfs = bfs_create (rval, zh->path_reg);
1723 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1724 bf_commitClean (bfs, rval);
1729 zebra_set_state (zh, 'o', seqno);
1731 zebra_unlock (zh->lock_shadow);
1732 zebra_unlock (zh->lock_normal);
1735 #if HAVE_SYS_TIMES_H
1737 yaz_log (log_level, "user/system: %ld/%ld",
1738 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1739 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1741 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1742 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1747 int zebra_repository_update (ZebraHandle zh, const char *path)
1752 yaz_log (log_level, "updating %s", path);
1753 repositoryUpdate (zh, path);
1757 int zebra_repository_delete (ZebraHandle zh, const char *path)
1762 yaz_log (log_level, "deleting %s", path);
1763 repositoryDelete (zh, path);
1767 int zebra_repository_show (ZebraHandle zh, const char *path)
1771 yaz_log(log_level, "zebra_repository_show");
1773 repositoryShow (zh, path);
1777 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1786 zebra_select_default_database(zh);
1789 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1792 rval = res_get (zh->res, "shadow");
1795 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1799 zebra_lock_w (zh->lock_normal);
1800 zebra_lock_r (zh->lock_shadow);
1802 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1804 zebra_get_state (zh, &val, &seqno);
1807 bf_cache (bfs, rval);
1808 if (bf_commitExists (bfs))
1811 zebra_set_state (zh, 'd', seqno);
1814 zebra_set_state (zh, 'c', seqno);
1816 yaz_log (YLOG_DEBUG, "commit start");
1817 bf_commitExec (bfs);
1822 yaz_log (YLOG_DEBUG, "commit clean");
1823 bf_commitClean (bfs, rval);
1825 zebra_set_state (zh, 'o', seqno);
1829 yaz_log (log_level, "nothing to commit");
1833 zebra_unlock (zh->lock_shadow);
1834 zebra_unlock (zh->lock_normal);
1838 ZEBRA_RES zebra_clean(ZebraHandle zh)
1841 yaz_log(log_level, "zebra_clean");
1842 return zebra_commit_ex(zh, 1);
1845 ZEBRA_RES zebra_commit(ZebraHandle zh)
1848 yaz_log(log_level, "zebra_commit");
1849 return zebra_commit_ex(zh, 0);
1852 ZEBRA_RES zebra_init(ZebraHandle zh)
1857 yaz_log(log_level, "zebra_init");
1860 zebra_select_default_database(zh);
1863 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1866 rval = res_get (zh->res, "shadow");
1868 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1872 bf_cache (bfs, rval);
1876 zebra_set_state (zh, 'o', 0);
1880 ZEBRA_RES zebra_compact(ZebraHandle zh)
1884 yaz_log(log_level, "zebra_compact");
1888 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1891 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1897 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1900 yaz_log(log_level, "zebra_result");
1901 *code = zh->errCode;
1902 *addinfo = zh->errString;
1905 void zebra_shadow_enable(ZebraHandle zh, int value)
1908 yaz_log(log_level, "zebra_shadow_enable");
1910 zh->shadow_enable = value;
1913 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1917 yaz_log(log_level, "zebra_octet_term_encoding");
1920 if (zh->iconv_to_utf8 != 0)
1921 yaz_iconv_close(zh->iconv_to_utf8);
1922 if (zh->iconv_from_utf8 != 0)
1923 yaz_iconv_close(zh->iconv_from_utf8);
1926 yaz_iconv_open ("UTF-8", encoding);
1927 if (zh->iconv_to_utf8 == 0)
1928 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1929 zh->iconv_from_utf8 =
1930 yaz_iconv_open (encoding, "UTF-8");
1931 if (zh->iconv_to_utf8 == 0)
1932 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1937 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1940 yaz_log(log_level, "zebra_record_encoding");
1942 xfree(zh->record_encoding);
1943 zh->record_encoding = 0;
1945 zh->record_encoding = xstrdup (encoding);
1949 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1954 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
1956 res_set(zh->res, name, value);
1959 const char *zebra_get_resource(ZebraHandle zh,
1960 const char *name, const char *defaultvalue)
1965 assert(defaultvalue);
1966 v = res_get_def (zh->res, name, (char *)defaultvalue);
1968 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
1972 /* moved from zebra_api_ext.c by pop */
1973 /* FIXME: Should this really be public??? -Heikki */
1975 int zebra_trans_no (ZebraHandle zh)
1978 yaz_log(log_level, "zebra_trans_no");
1979 return zh->trans_no;
1982 int zebra_get_shadow_enable (ZebraHandle zh)
1985 yaz_log(log_level, "zebra_get_shadow_enable");
1986 return zh->shadow_enable;
1989 void zebra_set_shadow_enable (ZebraHandle zh, int value)
1992 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
1993 zh->shadow_enable = value;
1996 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
1997 so that it's identicical to the original api_records_retrieve */
1998 void api_records_retrieve (ZebraHandle zh, ODR stream,
1999 const char *setname, Z_RecordComposition *comp,
2000 oid_value input_format, int num_recs,
2001 ZebraRetrievalRecord *recs)
2003 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2007 /* ---------------------------------------------------------------------------
2008 Record insert(=update), delete
2010 If sysno is provided, then it's used to identify the record.
2011 If not, and match_criteria is provided, then sysno is guessed
2012 If not, and a record is provided, then sysno is got from there
2013 NOTE: Now returns 0 at success and updates sysno, which is an int*
2017 int zebra_add_record(ZebraHandle zh,
2018 const char *buf, int buf_size)
2021 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2024 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2025 const char *recordType,
2026 SYSNO *sysno, const char *match,
2028 const char *buf, int buf_size, int force_update)
2034 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2037 buf_size = strlen(buf);
2039 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2041 res = buffer_extract_record (zh, buf, buf_size,
2042 0, /* delete_flag */
2048 0); /* allow_update */
2049 zebra_end_trans(zh);
2053 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2054 const char *recordType,
2055 SYSNO* sysno, const char *match,
2057 const char *buf, int buf_size,
2065 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2067 if (buf_size < 1) buf_size = strlen(buf);
2069 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2071 res = buffer_extract_record (zh, buf, buf_size,
2072 0, /* delete_flag */
2078 1); /* allow_update */
2079 zebra_end_trans(zh);
2083 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2084 const char *recordType,
2085 SYSNO *sysno, const char *match,
2087 const char *buf, int buf_size,
2094 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2096 if (buf_size < 1) buf_size = strlen(buf);
2098 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2100 res = buffer_extract_record (zh, buf, buf_size,
2101 1, /* delete_flag */
2107 1); /* allow_update */
2108 zebra_end_trans(zh);
2112 /* ---------------------------------------------------------------------------
2116 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2117 const char *setname, zint *numhits)
2120 ZEBRA_RES res = ZEBRA_OK;
2122 ODR odr = odr_createmem(ODR_ENCODE);
2127 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2129 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2133 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2134 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2138 res = zebra_search_RPN(zh, odr, query, setname, &hits);
2142 yaz_log(log_level, "Hits: " ZINT_FORMAT, hits);
2150 /* ---------------------------------------------------------------------------
2151 Sort - a simplified interface, with optional read locks.
2153 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2154 const char *sort_spec,
2155 const char *output_setname,
2156 const char **input_setnames)
2158 int num_input_setnames = 0;
2159 int sort_status = 0;
2160 Z_SortKeySpecList *sort_sequence;
2164 assert(output_setname);
2165 assert(input_setnames);
2166 sort_sequence = yaz_sort_spec (stream, sort_spec);
2167 yaz_log(log_level, "sort (FIXME) ");
2170 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2171 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2175 /* we can do this, since the perl typemap code for char** will
2176 put a NULL at the end of list */
2177 while (input_setnames[num_input_setnames]) num_input_setnames++;
2179 if (zebra_begin_read (zh))
2182 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2183 output_setname, sort_sequence, &sort_status);
2189 /* ---------------------------------------------------------------------------
2190 Get BFS for Zebra system (to make alternative storage methods)
2192 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2195 return zh->reg->bfs;
2200 /* ---------------------------------------------------------------------------
2201 Set limit for search/scan
2203 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int exclude_flag, zint *ids)
2206 zebra_limit_destroy(zh->m_limit);
2207 zh->m_limit = zebra_limit_create(exclude_flag, ids);