1 /* $Id: zebraapi.c,v 1.164 2005-05-03 09:11:34 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);
658 void map_basenames_func (void *vp, const char *name, const char *value)
660 struct map_baseinfo *p = (struct map_baseinfo *) vp;
662 char fromdb[128], todb[8][128];
669 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
670 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
671 todb[5], todb[6], todb[7]);
675 for (i = 0; i<p->num_bases; i++)
676 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
679 for (i = 0; i < no; i++)
681 if (p->new_num_bases == p->new_num_max)
683 p->new_basenames[(p->new_num_bases)++] =
684 nmem_strdup (p->mem, todb[i]);
690 int zebra_select_default_database(ZebraHandle zh)
694 /* no database has been selected - so we select based on
695 resource setting (including group)
697 const char *group = res_get(zh->service->global_res, "group");
698 const char *v = res_get_prefix(zh->service->global_res,
699 "database", group, "Default");
700 zebra_select_database(zh, v);
705 void map_basenames (ZebraHandle zh, ODR stream,
706 int *num_bases, char ***basenames)
708 struct map_baseinfo info;
709 struct map_baseinfo *p = &info;
712 yaz_log(log_level, "map_basenames ");
719 info.num_bases = *num_bases;
720 info.basenames = *basenames;
721 info.new_num_max = 128;
722 info.new_num_bases = 0;
723 info.new_basenames = (char **)
724 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
725 info.mem = stream->mem;
727 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
729 for (i = 0; i<p->num_bases; i++)
730 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
732 p->new_basenames[(p->new_num_bases)++] =
733 nmem_strdup (p->mem, p->basenames[i]);
735 *num_bases = info.new_num_bases;
736 *basenames = info.new_basenames;
737 for (i = 0; i<*num_bases; i++)
738 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
741 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
744 yaz_log(log_level, "zebra_select_database %s",basename);
747 return zebra_select_databases (zh, 1, &basename);
750 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
751 const char **basenames)
760 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
761 num_bases,basenames[0]);
766 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
769 for (i = 0; i < zh->num_basenames; i++)
770 xfree(zh->basenames[i]);
771 xfree(zh->basenames);
773 zh->num_basenames = num_bases;
774 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
775 for (i = 0; i < zh->num_basenames; i++)
776 zh->basenames[i] = xstrdup (basenames[i]);
778 cp = strrchr(basenames[0], '/');
781 len = cp - basenames[0];
782 new_reg = xmalloc(len + 1);
783 memcpy (new_reg, basenames[0], len);
787 new_reg = xstrdup ("");
788 for (i = 1; i<num_bases; i++)
792 cp1 = strrchr (basenames[i], '/');
797 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
800 if (len != cp1 - basenames[i] ||
801 memcmp (basenames[i], new_reg, len))
803 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
811 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
816 zebra_select_register (zh, new_reg);
820 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
823 if (!zh->lock_normal || !zh->lock_shadow)
825 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
831 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
832 const char *setname, zint *hits)
840 yaz_log(log_level, "zebra_search_rpn");
845 if (zebra_begin_read(zh) == ZEBRA_FAIL)
848 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
849 zh->num_basenames, zh->basenames, setname);
855 ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream,
857 Z_RecordComposition *comp,
858 oid_value input_format, int num_recs,
859 ZebraRetrievalRecord *recs)
861 ZebraMetaRecord *poset;
863 ZEBRA_RES ret = ZEBRA_OK;
871 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
876 zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
877 zh->errString = odr_strdup(stream, setname);
883 if (zebra_begin_read (zh) == ZEBRA_FAIL)
886 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
887 for (i = 0; i<num_recs; i++)
888 pos_array[i] = recs[i].position;
889 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
892 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
893 zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
894 zh->errString = nmem_strdup (stream->mem, setname);
899 for (i = 0; i<num_recs; i++)
904 recs[i].format = VAL_SUTRS;
905 recs[i].len = strlen(poset[i].term);
906 recs[i].buf = poset[i].term;
907 recs[i].base = poset[i].db;
909 else if (poset[i].sysno)
914 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
915 stream, input_format, comp,
916 &recs[i].format, &buf, &len,
917 &recs[i].base, &recs[i].errString);
921 recs[i].buf = (char*) odr_malloc(stream, len);
922 memcpy(recs[i].buf, buf, len);
926 recs[i].score = poset[i].score;
927 recs[i].sysno = poset[i].sysno;
933 sprintf (num_str, ZINT_FORMAT, pos_array[i]);
934 zh->errCode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
935 zh->errString = odr_strdup (stream, num_str);
940 zebra_meta_records_destroy(zh, poset, num_recs);
947 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
949 int *num_entries, ZebraScanEntry **entries,
952 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
953 Z_AttributesPlusTerm *zapt;
957 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
960 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
963 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
964 position, num_entries, entries, is_partial);
965 yaz_pqf_destroy (pqf_parser);
969 ZEBRA_RES zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
970 oid_value attributeset,
972 int *num_entries, ZebraScanEntry **entries,
983 yaz_log(log_level, "zebra_scan");
985 if (zebra_begin_read (zh) == ZEBRA_FAIL)
991 res = rpn_scan (zh, stream, zapt, attributeset,
992 zh->num_basenames, zh->basenames, position,
993 num_entries, entries, is_partial, 0, 0);
998 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
999 int num_input_setnames, const char **input_setnames,
1000 const char *output_setname,
1001 Z_SortKeySpecList *sort_sequence,
1006 assert(num_input_setnames>0);
1007 assert(input_setnames);
1008 assert(sort_sequence);
1009 assert(sort_status);
1010 yaz_log(log_level, "zebra_sort");
1012 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1014 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
1015 output_setname, sort_sequence, sort_status);
1016 return zebra_end_read(zh);
1019 int zebra_deleteResultSet(ZebraHandle zh, int function,
1020 int num_setnames, char **setnames,
1026 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1028 if (zebra_begin_read(zh))
1029 return Z_DeleteStatus_systemProblemAtTarget;
1032 case Z_DeleteResultSetRequest_list:
1033 assert(num_setnames>0);
1035 resultSetDestroy (zh, num_setnames, setnames, statuses);
1037 case Z_DeleteResultSetRequest_all:
1038 resultSetDestroy (zh, -1, 0, statuses);
1041 zebra_end_read (zh);
1042 status = Z_DeleteStatus_success;
1043 for (i = 0; i<num_setnames; i++)
1044 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1045 status = statuses[i];
1049 int zebra_errCode (ZebraHandle zh)
1053 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1056 yaz_log(log_level, "zebra_errCode: o");
1060 const char *zebra_errString (ZebraHandle zh)
1064 e= diagbib1_str (zh->errCode);
1065 yaz_log(log_level, "zebra_errString: %s",e);
1069 char *zebra_errAdd (ZebraHandle zh)
1074 yaz_log(log_level, "zebra_errAdd: %s",a);
1078 void zebra_clearError(ZebraHandle zh)
1087 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1097 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1098 p = res_get(zs->global_res, u);
1099 xfree(zh->user_perm);
1100 zh->user_perm = xstrdup(p ? p : "r");
1102 /* users that don't require a password .. */
1103 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1106 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1111 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1112 const char *record_type)
1115 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1116 database, record_type);
1118 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1120 return zebra_begin_trans(zh, 1);
1123 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1126 yaz_log(log_level, "zebra_admin_import_end");
1128 return zebra_end_trans(zh);
1131 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1133 ZEBRA_RES res = ZEBRA_OK;
1137 yaz_log(log_level, "zebra_admin_import_segment");
1139 for (i = 0; i<segment->num_segmentRecords; i++)
1141 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1143 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1145 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1146 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1148 Odr_oct *oct = fragment->u.notExternallyTagged;
1151 if ( zebra_update_record(zh,
1152 0, /* record Type */
1165 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1166 const char *rec_buf,
1168 const char *recid_buf, size_t recid_len,
1170 /* 1 = insert. Fail it already exists */
1171 /* 2 = replace. Fail it does not exist */
1172 /* 3 = delete. Fail if does not exist */
1173 /* 4 = update. Insert/replace */
1180 assert(action>0 && action <=4);
1183 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1186 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1189 memcpy (recid_z, recid_buf, recid_len);
1190 recid_z[recid_len] = 0;
1192 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1195 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1198 if (action == 1) /* fail if insert */
1200 zebra_end_trans(zh);
1204 memcpy (&sysno, rinfo+1, sizeof(sysno));
1208 if (action == 2 || action == 3) /* fail if delete or update */
1210 zebra_end_trans(zh);
1213 action = 1; /* make it an insert (if it's an update).. */
1215 res = buffer_extract_record (zh, rec_buf, rec_len,
1216 action == 3 ? 1 : 0 /* delete flag */,
1222 0, /* force update */
1223 1 /* allow update */
1227 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1229 else if (action == 3)
1231 dict_delete (zh->reg->matchDict, recid_z);
1233 zebra_end_trans(zh);
1237 int delete_w_handle(const char *info, void *handle)
1239 ZebraHandle zh = (ZebraHandle) handle;
1243 if (*info == sizeof(pos))
1245 memcpy (&pos, info+1, sizeof(pos));
1246 isamb_unlink(zh->reg->isamb, pos);
1251 static int delete_SU_handle(void *handle, int ord)
1253 ZebraHandle zh = (ZebraHandle) handle;
1257 ord_len = key_SU_encode (ord, ord_buf);
1258 ord_buf[ord_len] = '\0';
1260 assert (zh->reg->isamb);
1261 dict_delete_subtree(zh->reg->dict, ord_buf,
1262 zh, delete_w_handle);
1266 ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database)
1268 ZEBRA_RES ret = ZEBRA_OK;
1270 yaz_log(log_level, "zebra_drop_database");
1273 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1275 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1279 zebraExplain_curDatabase (zh->reg->zei, database);
1281 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1282 zebraExplain_removeDatabase(zh->reg->zei, zh);
1286 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1289 zebra_end_trans (zh);
1293 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database)
1296 yaz_log(log_level, "zebra_create_database %s", database);
1300 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1302 if (zebra_begin_trans (zh, 1))
1305 /* announce database */
1306 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1307 /* explainDatabase */))
1309 zebra_end_trans (zh);
1310 zh->errCode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
1311 zh->errString = "database already exist";
1314 return zebra_end_trans (zh);
1317 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1318 const char *input_str, int input_len,
1319 char *output_str, int output_len)
1325 yaz_log(log_level, "zebra_string_norm ");
1327 if (!zh->reg->zebra_maps)
1329 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1330 input_str, input_len);
1333 if (wrbuf_len(wrbuf) >= output_len)
1335 if (wrbuf_len(wrbuf))
1336 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1337 output_str[wrbuf_len(wrbuf)] = '\0';
1338 return wrbuf_len(wrbuf);
1341 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1343 char state_fname[256];
1348 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1351 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1352 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1353 f = fopen (fname, "w");
1355 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1356 fprintf (f, "%c %d %ld\n", val, seqno, p);
1361 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1363 char state_fname[256];
1368 yaz_log(log_level, "zebra_get_state ");
1370 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1371 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1372 f = fopen (fname, "r");
1378 fscanf (f, "%c %d", val, seqno);
1384 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1386 return zebra_begin_trans(zh, 0);
1389 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1391 return zebra_end_trans(zh);
1394 static void read_res_for_transaction(ZebraHandle zh)
1396 const char *group = res_get(zh->res, "group");
1398 /* FIXME - do we still use groups ?? */
1400 zh->m_group = group;
1401 v = res_get_prefix(zh->res, "followLinks", group, "1");
1402 zh->m_follow_links = atoi(v);
1404 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1405 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1407 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1408 zh->m_store_keys = atoi(v);
1410 v = res_get_prefix(zh->res, "storeData", group, "1");
1411 zh->m_store_data = atoi(v);
1413 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1414 zh->m_explain_database = atoi(v);
1416 v = res_get_prefix(zh->res, "openRW", group, "1");
1417 zh->m_flag_rw = atoi(v);
1419 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1420 zh->m_file_verbose_limit = atoi(v);
1423 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1426 zebra_select_default_database(zh);
1429 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1430 zh->errString = "zebra_begin_trans: no database selected";
1434 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1438 if (rw && !strchr(zh->user_perm, 'w'))
1441 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE;
1453 const char *rval = 0;
1458 read_res_for_transaction(zh);
1461 if (zh->trans_no != 1)
1463 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1464 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1469 resultSetInvalidate (zh);
1470 zebra_register_close (zh->service, zh->reg);
1472 zh->trans_w_no = zh->trans_no;
1476 zh->records_inserted = 0;
1477 zh->records_updated = 0;
1478 zh->records_deleted = 0;
1479 zh->records_processed = 0;
1481 #if HAVE_SYS_TIMES_H
1485 if (zh->shadow_enable)
1486 rval = res_get (zh->res, "shadow");
1488 for (pass = 0; pass < 2; pass++)
1492 zebra_lock_r (zh->lock_normal);
1493 zebra_lock_w (zh->lock_shadow);
1497 zebra_lock_w (zh->lock_normal);
1498 zebra_lock_w (zh->lock_shadow);
1501 zebra_get_state (zh, &val, &seqno);
1504 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1505 zebra_unlock (zh->lock_shadow);
1506 zebra_unlock (zh->lock_normal);
1510 else if (val == 'd')
1514 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1516 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1517 bf_commitClean (bfs, rval);
1522 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1529 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1533 zebra_set_state (zh, 'd', seqno);
1535 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1536 1, rval ? 1 : 0, zh->res,
1539 zh->reg->seqno = seqno;
1542 zebra_set_state (zh, 'o', seqno);
1544 zebra_unlock (zh->lock_shadow);
1545 zebra_unlock (zh->lock_normal);
1550 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1551 zh->errString = "zebra_begin_trans: cannot open register";
1552 yaz_log(YLOG_FATAL, zh->errString);
1564 if (zh->trans_no != 1)
1566 zebra_flush_reg (zh);
1570 #if HAVE_SYS_TIMES_H
1576 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1579 if (!zh->lock_normal || !zh->lock_shadow)
1582 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1585 zebra_get_state (zh, &val, &seqno);
1591 else if (seqno != zh->reg->seqno)
1593 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1594 seqno, zh->reg->seqno);
1597 else if (zh->reg->last_val != val)
1599 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1600 val, zh->reg->last_val);
1607 zebra_lock_r (zh->lock_shadow);
1609 zebra_lock_r (zh->lock_normal);
1613 resultSetInvalidate (zh);
1614 zebra_register_close (zh->service, zh->reg);
1616 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1617 0, val == 'c' ? 1 : 0,
1618 zh->res, zh->path_reg);
1621 zebra_unlock (zh->lock_normal);
1622 zebra_unlock (zh->lock_shadow);
1624 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1627 zh->reg->last_val = val;
1628 zh->reg->seqno = seqno;
1630 read_res_for_transaction(zh);
1634 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1636 ZebraTransactionStatus dummy;
1638 yaz_log(log_level, "zebra_end_trans");
1639 return zebra_end_transaction(zh, &dummy);
1642 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1650 yaz_log(log_level, "zebra_end_transaction");
1652 status->processed = 0;
1653 status->inserted = 0;
1654 status->updated = 0;
1655 status->deleted = 0;
1659 if (!zh->res || !zh->reg)
1661 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1662 zh->errString = "zebra_end_trans: no open transaction";
1665 if (zh->trans_no != zh->trans_w_no)
1668 if (zh->trans_no != 0)
1671 /* release read lock */
1673 zebra_unlock (zh->lock_normal);
1674 zebra_unlock (zh->lock_shadow);
1677 { /* release write lock */
1681 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1682 rval = res_get (zh->res, "shadow");
1684 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1686 zebra_flush_reg (zh);
1688 resultSetInvalidate (zh);
1690 zebra_register_close (zh->service, zh->reg);
1693 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1694 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1695 zh->records_processed, zh->records_inserted,
1696 zh->records_updated, zh->records_deleted);
1698 status->processed = (int) zh->records_processed;
1699 status->inserted = (int) zh->records_inserted;
1700 status->updated = (int) zh->records_updated;
1701 status->deleted = (int) zh->records_deleted;
1703 zebra_get_state (zh, &val, &seqno);
1706 BFiles bfs = bfs_create (rval, zh->path_reg);
1707 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1708 bf_commitClean (bfs, rval);
1713 zebra_set_state (zh, 'o', seqno);
1715 zebra_unlock (zh->lock_shadow);
1716 zebra_unlock (zh->lock_normal);
1719 #if HAVE_SYS_TIMES_H
1721 yaz_log (log_level, "user/system: %ld/%ld",
1722 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1723 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1725 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1726 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1731 int zebra_repository_update (ZebraHandle zh, const char *path)
1736 yaz_log (log_level, "updating %s", path);
1737 repositoryUpdate (zh, path);
1741 int zebra_repository_delete (ZebraHandle zh, const char *path)
1746 yaz_log (log_level, "deleting %s", path);
1747 repositoryDelete (zh, path);
1751 int zebra_repository_show (ZebraHandle zh, const char *path)
1755 yaz_log(log_level, "zebra_repository_show");
1757 repositoryShow (zh, path);
1761 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1770 zebra_select_default_database(zh);
1773 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1776 rval = res_get (zh->res, "shadow");
1779 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1783 zebra_lock_w (zh->lock_normal);
1784 zebra_lock_r (zh->lock_shadow);
1786 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1788 zebra_get_state (zh, &val, &seqno);
1791 bf_cache (bfs, rval);
1792 if (bf_commitExists (bfs))
1795 zebra_set_state (zh, 'd', seqno);
1798 zebra_set_state (zh, 'c', seqno);
1800 yaz_log (YLOG_DEBUG, "commit start");
1801 bf_commitExec (bfs);
1806 yaz_log (YLOG_DEBUG, "commit clean");
1807 bf_commitClean (bfs, rval);
1809 zebra_set_state (zh, 'o', seqno);
1813 yaz_log (log_level, "nothing to commit");
1817 zebra_unlock (zh->lock_shadow);
1818 zebra_unlock (zh->lock_normal);
1822 ZEBRA_RES zebra_clean(ZebraHandle zh)
1825 yaz_log(log_level, "zebra_clean");
1826 return zebra_commit_ex(zh, 1);
1829 ZEBRA_RES zebra_commit(ZebraHandle zh)
1832 yaz_log(log_level, "zebra_commit");
1833 return zebra_commit_ex(zh, 0);
1836 ZEBRA_RES zebra_init(ZebraHandle zh)
1841 yaz_log(log_level, "zebra_init");
1844 zebra_select_default_database(zh);
1847 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1850 rval = res_get (zh->res, "shadow");
1852 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1856 bf_cache (bfs, rval);
1860 zebra_set_state (zh, 'o', 0);
1864 ZEBRA_RES zebra_compact(ZebraHandle zh)
1868 yaz_log(log_level, "zebra_compact");
1872 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1875 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1881 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1884 yaz_log(log_level, "zebra_result");
1885 *code = zh->errCode;
1886 *addinfo = zh->errString;
1889 void zebra_shadow_enable(ZebraHandle zh, int value)
1892 yaz_log(log_level, "zebra_shadow_enable");
1894 zh->shadow_enable = value;
1897 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1901 yaz_log(log_level, "zebra_octet_term_encoding");
1904 if (zh->iconv_to_utf8 != 0)
1905 yaz_iconv_close(zh->iconv_to_utf8);
1906 if (zh->iconv_from_utf8 != 0)
1907 yaz_iconv_close(zh->iconv_from_utf8);
1910 yaz_iconv_open ("UTF-8", encoding);
1911 if (zh->iconv_to_utf8 == 0)
1912 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1913 zh->iconv_from_utf8 =
1914 yaz_iconv_open (encoding, "UTF-8");
1915 if (zh->iconv_to_utf8 == 0)
1916 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1921 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1924 yaz_log(log_level, "zebra_record_encoding");
1926 xfree(zh->record_encoding);
1927 zh->record_encoding = 0;
1929 zh->record_encoding = xstrdup (encoding);
1933 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1938 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
1940 res_set(zh->res, name, value);
1943 const char *zebra_get_resource(ZebraHandle zh,
1944 const char *name, const char *defaultvalue)
1949 assert(defaultvalue);
1950 v = res_get_def (zh->res, name, (char *)defaultvalue);
1952 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
1956 /* moved from zebra_api_ext.c by pop */
1957 /* FIXME: Should this really be public??? -Heikki */
1959 int zebra_trans_no (ZebraHandle zh)
1962 yaz_log(log_level, "zebra_trans_no");
1963 return zh->trans_no;
1966 int zebra_get_shadow_enable (ZebraHandle zh)
1969 yaz_log(log_level, "zebra_get_shadow_enable");
1970 return zh->shadow_enable;
1973 void zebra_set_shadow_enable (ZebraHandle zh, int value)
1976 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
1977 zh->shadow_enable = value;
1980 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
1981 so that it's identicical to the original api_records_retrieve */
1982 void api_records_retrieve (ZebraHandle zh, ODR stream,
1983 const char *setname, Z_RecordComposition *comp,
1984 oid_value input_format, int num_recs,
1985 ZebraRetrievalRecord *recs)
1987 zebra_records_retrieve(zh, stream, setname, comp, input_format,
1991 /* ---------------------------------------------------------------------------
1992 Record insert(=update), delete
1994 If sysno is provided, then it's used to identify the record.
1995 If not, and match_criteria is provided, then sysno is guessed
1996 If not, and a record is provided, then sysno is got from there
1997 NOTE: Now returns 0 at success and updates sysno, which is an int*
2001 int zebra_add_record(ZebraHandle zh,
2002 const char *buf, int buf_size)
2005 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2008 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2009 const char *recordType,
2010 SYSNO *sysno, const char *match,
2012 const char *buf, int buf_size, int force_update)
2018 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2021 buf_size = strlen(buf);
2023 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2025 res = buffer_extract_record (zh, buf, buf_size,
2026 0, /* delete_flag */
2032 0); /* allow_update */
2033 zebra_end_trans(zh);
2037 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2038 const char *recordType,
2039 SYSNO* sysno, const char *match,
2041 const char *buf, int buf_size,
2049 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2051 if (buf_size < 1) buf_size = strlen(buf);
2053 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2055 res = buffer_extract_record (zh, buf, buf_size,
2056 0, /* delete_flag */
2062 1); /* allow_update */
2063 zebra_end_trans(zh);
2067 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2068 const char *recordType,
2069 SYSNO *sysno, const char *match,
2071 const char *buf, int buf_size,
2078 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2080 if (buf_size < 1) buf_size = strlen(buf);
2082 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2084 res = buffer_extract_record (zh, buf, buf_size,
2085 1, /* delete_flag */
2091 1); /* allow_update */
2092 zebra_end_trans(zh);
2096 /* ---------------------------------------------------------------------------
2100 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2101 const char *setname, zint *numhits)
2104 ZEBRA_RES res = ZEBRA_OK;
2106 ODR odr = odr_createmem(ODR_ENCODE);
2111 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2113 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2117 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2118 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2122 res = zebra_search_RPN(zh, odr, query, setname, &hits);
2126 yaz_log(log_level, "Hits: " ZINT_FORMAT, hits);
2134 /* ---------------------------------------------------------------------------
2135 Sort - a simplified interface, with optional read locks.
2137 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2138 const char *sort_spec,
2139 const char *output_setname,
2140 const char **input_setnames)
2142 int num_input_setnames = 0;
2143 int sort_status = 0;
2144 Z_SortKeySpecList *sort_sequence;
2148 assert(output_setname);
2149 assert(input_setnames);
2150 sort_sequence = yaz_sort_spec (stream, sort_spec);
2151 yaz_log(log_level, "sort (FIXME) ");
2154 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2155 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2159 /* we can do this, since the perl typemap code for char** will
2160 put a NULL at the end of list */
2161 while (input_setnames[num_input_setnames]) num_input_setnames++;
2163 if (zebra_begin_read (zh))
2166 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2167 output_setname, sort_sequence, &sort_status);
2173 /* ---------------------------------------------------------------------------
2174 Get BFS for Zebra system (to make alternative storage methods)
2176 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2179 return zh->reg->bfs;
2184 /* ---------------------------------------------------------------------------
2185 Set limit for search/scan
2187 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int exclude_flag, zint *ids)
2190 zebra_limit_destroy(zh->m_limit);
2191 zh->m_limit = zebra_limit_create(exclude_flag, ids);