1 /* $Id: zebraapi.c,v 1.168 2005-05-11 12:39:37 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(1, 0);
152 zh->nmem_error = nmem_create();
157 ZebraService zebra_start (const char *configName)
159 return zebra_start_res(configName, 0, 0);
162 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
166 if (!log_level_initialized)
168 log_level = yaz_log_module_level("zebraapi");
169 log_level_initialized = 1;
172 yaz_log(log_level, "zebra_start %s",configName);
175 if ((res = res_open (configName, def_res, over_res)))
177 ZebraService zh = xmalloc(sizeof(*zh));
179 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
181 zh->global_res = res;
182 zh->configName = xstrdup(configName);
187 zebra_mutex_cond_init (&zh->session_lock);
188 if (!res_get (zh->global_res, "passwd"))
189 zh->passwd_db = NULL;
192 zh->passwd_db = passwd_db_open ();
194 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
196 passwd_db_file (zh->passwd_db,
197 res_get (zh->global_res, "passwd"));
199 zh->path_root = res_get (zh->global_res, "root");
200 zh->nmem = nmem_create();
201 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
207 void zebra_filter_info(ZebraService zs, void *cd,
208 void (*cb)(void *cd, const char *name))
212 recTypeClass_info(zs->record_classes, cd, cb);
215 void zebra_pidfname(ZebraService zs, char *path)
218 zebra_lock_prefix (zs->global_res, path);
219 strcat(path, "zebrasrv.pid");
222 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
223 int compact_flag, Res res)
225 int page_size = 4096;
226 char resource_str[200];
228 sprintf (resource_str, "dict.%.100s.pagesize", name);
232 v = res_get(res, resource_str);
236 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
239 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
244 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
245 int rw, int useshadow, Res res,
246 const char *reg_path)
248 struct zebra_register *reg;
249 int record_compression = REC_COMPRESS_NONE;
250 const char *recordCompression = 0;
251 const char *profilePath;
256 reg = xmalloc(sizeof(*reg));
259 reg->name = xstrdup (name);
266 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
267 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
269 reg->dh = data1_createx (DATA1_FLAG_XML);
272 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
275 data1_destroy(reg->dh);
279 bf_cache (reg->bfs, res_get (res, "shadow"));
281 getcwd(cwd, sizeof(cwd)-1);
282 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
283 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
285 data1_set_tabpath (reg->dh, profilePath);
286 data1_set_tabroot (reg->dh, reg_path);
287 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
289 reg->zebra_maps = zebra_maps_open (res, reg_path);
290 reg->rank_classes = NULL;
294 reg->keys.buf_max = 0;
296 reg->keys.codec_handle = iscz1_start();
298 reg->sortKeys.buf = 0;
299 reg->sortKeys.buf_max = 0;
310 reg->key_file_no = 0;
313 zebraRankInstall (reg, rank1_class);
314 zebraRankInstall (reg, rankzv_class);
316 recordCompression = res_get_def (res, "recordCompression", "none");
317 if (!strcmp (recordCompression, "none"))
318 record_compression = REC_COMPRESS_NONE;
319 if (!strcmp (recordCompression, "bzip2"))
320 record_compression = REC_COMPRESS_BZIP2;
322 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
324 yaz_log (YLOG_WARN, "rec_open failed");
329 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
331 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
333 yaz_log (YLOG_WARN, "dict_open failed");
336 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
338 yaz_log (YLOG_WARN, "sortIdx_open failed");
341 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
343 struct ISAMS_M_s isams_m;
344 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
345 key_isams_m(res, &isams_m))))
347 yaz_log (YLOG_WARN, "isams_open failed");
351 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
353 struct ISAMC_M_s isamc_m;
354 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
355 rw, key_isamc_m(res, &isamc_m))))
357 yaz_log (YLOG_WARN, "isamc_open failed");
361 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
363 struct ISAMC_M_s isamc_m;
365 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
366 rw, key_isamc_m(res, &isamc_m), 0)))
368 yaz_log (YLOG_WARN, "isamb_open failed");
372 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
374 struct ISAMC_M_s isamc_m;
376 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
377 rw, key_isamc_m(res, &isamc_m), 1)))
379 yaz_log (YLOG_WARN, "isamb_open failed");
383 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
385 struct ISAMC_M_s isamc_m;
387 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
388 rw, key_isamc_m(res, &isamc_m), -1)))
390 yaz_log (YLOG_WARN, "isamb_open failed");
394 reg->zei = zebraExplain_open (reg->records, reg->dh,
399 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
403 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
407 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
410 yaz_log(log_level, "zebra_admin_shutdown");
411 zebra_clearError(zh);
413 zebra_mutex_cond_lock (&zh->service->session_lock);
414 zh->service->stop_flag = 1;
415 zebra_mutex_cond_unlock (&zh->service->session_lock);
419 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
423 yaz_log(log_level, "zebra_admin_start");
424 zebra_clearError(zh);
426 zebra_mutex_cond_lock (&zs->session_lock);
427 zebra_mutex_cond_unlock (&zs->session_lock);
431 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
435 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
440 zebraExplain_close (reg->zei);
441 dict_close (reg->dict);
443 dict_close (reg->matchDict);
444 sortIdx_close (reg->sortIdx);
446 isams_close (reg->isams);
448 isamc_close (reg->isamc);
450 isamb_close (reg->isamb);
451 rec_close (®->records);
454 recTypes_destroy (reg->recTypes);
455 zebra_maps_close (reg->zebra_maps);
456 zebraRankDestroy (reg);
457 bfs_destroy (reg->bfs);
458 data1_destroy (reg->dh);
460 xfree(reg->sortKeys.buf);
461 xfree(reg->keys.buf);
462 if (reg->keys.codec_handle)
463 iscz1_stop(reg->keys.codec_handle);
469 ZEBRA_RES zebra_stop(ZebraService zs)
473 yaz_log (log_level, "zebra_stop");
477 zebra_close (zs->sessions);
480 zebra_mutex_cond_destroy (&zs->session_lock);
483 passwd_db_close (zs->passwd_db);
485 recTypeClass_destroy(zs->record_classes);
486 nmem_destroy(zs->nmem);
487 res_close (zs->global_res);
488 xfree(zs->configName);
493 ZEBRA_RES zebra_close (ZebraHandle zh)
496 struct zebra_session **sp;
499 yaz_log(log_level, "zebra_close");
506 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
507 resultSetDestroy (zh, -1, 0, 0);
510 zebra_register_close (zh->service, zh->reg);
511 zebra_close_res (zh);
513 xfree(zh->record_encoding);
515 for (i = 0; i < zh->num_basenames; i++)
516 xfree(zh->basenames[i]);
517 xfree(zh->basenames);
519 if (zh->iconv_to_utf8 != 0)
520 yaz_iconv_close (zh->iconv_to_utf8);
521 if (zh->iconv_from_utf8 != 0)
522 yaz_iconv_close (zh->iconv_from_utf8);
524 zebra_mutex_cond_lock (&zs->session_lock);
525 zebra_lock_destroy (zh->lock_normal);
526 zebra_lock_destroy (zh->lock_shadow);
538 zebra_mutex_cond_unlock (&zs->session_lock);
540 xfree(zh->user_perm);
541 zh->service = 0; /* more likely to trigger an assert */
543 zebra_limit_destroy(zh->m_limit);
545 nmem_destroy(zh->nmem_error);
552 struct map_baseinfo {
558 char **new_basenames;
562 static Res zebra_open_res (ZebraHandle zh)
571 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
572 res = res_open (fname, zh->service->global_res, 0);
574 res = zh->service->global_res;
576 else if (*zh->reg_name == 0)
578 res = zh->service->global_res;
582 yaz_log (YLOG_WARN, "no register root specified");
583 return 0; /* no path for register - fail! */
588 static void zebra_close_res (ZebraHandle zh)
592 if (zh->res != zh->service->global_res)
597 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
601 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
605 assert (zh->reg == 0);
606 assert (*zh->reg_name == 0);
612 resultSetInvalidate (zh);
613 zebra_register_close (zh->service, zh->reg);
619 zh->reg_name = xstrdup (new_reg);
623 if (zh->service->path_root)
625 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
626 strlen(zh->reg_name) + 3);
627 strcpy (zh->path_reg, zh->service->path_root);
630 strcat (zh->path_reg, "/");
631 strcat (zh->path_reg, zh->reg_name);
634 zh->res = zebra_open_res (zh);
637 zebra_lock_destroy (zh->lock_normal);
641 zebra_lock_destroy (zh->lock_shadow);
647 const char *lock_area =res_get (zh->res, "lockDir");
649 if (!lock_area && zh->path_reg)
650 res_set (zh->res, "lockDir", zh->path_reg);
651 sprintf (fname, "norm.%s.LCK", zh->reg_name);
653 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
655 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
657 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
659 if (!zh->lock_normal || !zh->lock_shadow)
663 zebra_lock_destroy(zh->lock_normal);
668 zebra_lock_destroy(zh->lock_shadow);
676 void map_basenames_func (void *vp, const char *name, const char *value)
678 struct map_baseinfo *p = (struct map_baseinfo *) vp;
680 char fromdb[128], todb[8][128];
687 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
688 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
689 todb[5], todb[6], todb[7]);
693 for (i = 0; i<p->num_bases; i++)
694 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
697 for (i = 0; i < no; i++)
699 if (p->new_num_bases == p->new_num_max)
701 p->new_basenames[(p->new_num_bases)++] =
702 nmem_strdup (p->mem, todb[i]);
708 int zebra_select_default_database(ZebraHandle zh)
712 /* no database has been selected - so we select based on
713 resource setting (including group)
715 const char *group = res_get(zh->service->global_res, "group");
716 const char *v = res_get_prefix(zh->service->global_res,
717 "database", group, "Default");
718 return zebra_select_database(zh, v);
723 void map_basenames (ZebraHandle zh, ODR stream,
724 int *num_bases, char ***basenames)
726 struct map_baseinfo info;
727 struct map_baseinfo *p = &info;
730 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);
762 return zebra_select_databases (zh, 1, &basename);
765 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
766 const char **basenames)
775 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
776 num_bases,basenames[0]);
781 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
784 for (i = 0; i < zh->num_basenames; i++)
785 xfree(zh->basenames[i]);
786 xfree(zh->basenames);
788 zh->num_basenames = num_bases;
789 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
790 for (i = 0; i < zh->num_basenames; i++)
791 zh->basenames[i] = xstrdup (basenames[i]);
793 cp = strrchr(basenames[0], '/');
796 len = cp - basenames[0];
797 new_reg = xmalloc(len + 1);
798 memcpy (new_reg, basenames[0], len);
802 new_reg = xstrdup ("");
803 for (i = 1; i<num_bases; i++)
807 cp1 = strrchr (basenames[i], '/');
812 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
815 if (len != cp1 - basenames[i] ||
816 memcmp (basenames[i], new_reg, len))
818 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
826 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
831 zebra_select_register (zh, new_reg);
835 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
838 if (!zh->lock_normal || !zh->lock_shadow)
840 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
846 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
847 const char *setname, zint *hits)
855 yaz_log(log_level, "zebra_search_rpn");
856 zebra_clearError(zh);
860 if (zebra_begin_read(zh) == ZEBRA_FAIL)
863 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
864 zh->num_basenames, zh->basenames, setname);
870 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
872 Z_RecordComposition *comp,
873 oid_value input_format, int num_recs,
874 ZebraRetrievalRecord *recs)
876 ZebraMetaRecord *poset;
878 ZEBRA_RES ret = ZEBRA_OK;
886 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
888 zebra_clearError(zh);
891 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
896 if (zebra_begin_read (zh) == ZEBRA_FAIL)
899 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
900 for (i = 0; i<num_recs; i++)
901 pos_array[i] = recs[i].position;
902 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
905 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
906 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
912 for (i = 0; i<num_recs; i++)
917 recs[i].format = VAL_SUTRS;
918 recs[i].len = strlen(poset[i].term);
919 recs[i].buf = poset[i].term;
920 recs[i].base = poset[i].db;
922 else if (poset[i].sysno)
927 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
928 stream, input_format, comp,
929 &recs[i].format, &buf, &len,
930 &recs[i].base, &recs[i].errString);
934 recs[i].buf = (char*) odr_malloc(stream, len);
935 memcpy(recs[i].buf, buf, len);
939 recs[i].score = poset[i].score;
940 recs[i].sysno = poset[i].sysno;
944 if (ret == ZEBRA_OK) /* only need to set it once */
945 zebra_setError_zint(zh,
946 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
952 zebra_meta_records_destroy(zh, poset, num_recs);
959 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
961 int *num_entries, ZebraScanEntry **entries,
964 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
965 Z_AttributesPlusTerm *zapt;
969 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
972 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
975 res = zebra_scan(zh, stream, zapt, VAL_BIB1,
976 position, num_entries, entries, is_partial);
977 yaz_pqf_destroy (pqf_parser);
981 ZEBRA_RES zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
982 oid_value attributeset,
984 int *num_entries, ZebraScanEntry **entries,
995 yaz_log(log_level, "zebra_scan");
996 zebra_clearError(zh);
997 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1003 res = rpn_scan (zh, stream, zapt, attributeset,
1004 zh->num_basenames, zh->basenames, position,
1005 num_entries, entries, is_partial, 0, 0);
1006 zebra_end_read (zh);
1010 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
1011 int num_input_setnames, const char **input_setnames,
1012 const char *output_setname,
1013 Z_SortKeySpecList *sort_sequence,
1019 assert(num_input_setnames>0);
1020 assert(input_setnames);
1021 assert(sort_sequence);
1022 assert(sort_status);
1023 yaz_log(log_level, "zebra_sort");
1024 zebra_clearError(zh);
1025 if (zebra_begin_read(zh) == ZEBRA_FAIL)
1027 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1028 output_setname, sort_sequence, sort_status);
1033 int zebra_deleteResultSet(ZebraHandle zh, int function,
1034 int num_setnames, char **setnames,
1040 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1041 zebra_clearError(zh);;
1042 if (zebra_begin_read(zh))
1043 return Z_DeleteStatus_systemProblemAtTarget;
1046 case Z_DeleteResultSetRequest_list:
1047 assert(num_setnames>0);
1049 resultSetDestroy (zh, num_setnames, setnames, statuses);
1051 case Z_DeleteResultSetRequest_all:
1052 resultSetDestroy (zh, -1, 0, statuses);
1055 zebra_end_read (zh);
1056 status = Z_DeleteStatus_success;
1057 for (i = 0; i<num_setnames; i++)
1058 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1059 status = statuses[i];
1063 int zebra_errCode (ZebraHandle zh)
1067 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1070 yaz_log(log_level, "zebra_errCode: o");
1074 const char *zebra_errString (ZebraHandle zh)
1078 e= diagbib1_str (zh->errCode);
1079 yaz_log(log_level, "zebra_errString: %s",e);
1083 char *zebra_errAdd (ZebraHandle zh)
1088 yaz_log(log_level, "zebra_errAdd: %s",a);
1092 void zebra_clearError(ZebraHandle zh)
1101 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1108 zebra_clearError(zh);
1111 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1112 p = res_get(zs->global_res, u);
1113 xfree(zh->user_perm);
1114 zh->user_perm = xstrdup(p ? p : "r");
1116 /* users that don't require a password .. */
1117 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1120 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1125 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1126 const char *record_type)
1129 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1130 database, record_type);
1131 zebra_clearError(zh);
1132 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1134 return zebra_begin_trans(zh, 1);
1137 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1140 yaz_log(log_level, "zebra_admin_import_end");
1141 zebra_clearError(zh);
1142 return zebra_end_trans(zh);
1145 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1147 ZEBRA_RES res = ZEBRA_OK;
1151 yaz_log(log_level, "zebra_admin_import_segment");
1152 zebra_clearError(zh);
1153 for (i = 0; i<segment->num_segmentRecords; i++)
1155 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1157 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1159 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1160 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1162 Odr_oct *oct = fragment->u.notExternallyTagged;
1165 if ( zebra_update_record(zh,
1166 0, /* record Type */
1179 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1180 const char *rec_buf,
1182 const char *recid_buf, size_t recid_len,
1184 /* 1 = insert. Fail it already exists */
1185 /* 2 = replace. Fail it does not exist */
1186 /* 3 = delete. Fail if does not exist */
1187 /* 4 = update. Insert/replace */
1194 assert(action>0 && action <=4);
1197 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1198 zebra_clearError(zh);
1200 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1203 memcpy (recid_z, recid_buf, recid_len);
1204 recid_z[recid_len] = 0;
1206 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1209 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1212 if (action == 1) /* fail if insert */
1214 zebra_end_trans(zh);
1218 memcpy (&sysno, rinfo+1, sizeof(sysno));
1222 if (action == 2 || action == 3) /* fail if delete or update */
1224 zebra_end_trans(zh);
1227 action = 1; /* make it an insert (if it's an update).. */
1229 res = buffer_extract_record (zh, rec_buf, rec_len,
1230 action == 3 ? 1 : 0 /* delete flag */,
1236 0, /* force update */
1237 1 /* allow update */
1241 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1243 else if (action == 3)
1245 dict_delete (zh->reg->matchDict, recid_z);
1247 zebra_end_trans(zh);
1251 int delete_w_handle(const char *info, void *handle)
1253 ZebraHandle zh = (ZebraHandle) handle;
1257 if (*info == sizeof(pos))
1259 memcpy (&pos, info+1, sizeof(pos));
1260 isamb_unlink(zh->reg->isamb, pos);
1265 static int delete_SU_handle(void *handle, int ord)
1267 ZebraHandle zh = (ZebraHandle) handle;
1271 ord_len = key_SU_encode (ord, ord_buf);
1272 ord_buf[ord_len] = '\0';
1274 assert (zh->reg->isamb);
1275 dict_delete_subtree(zh->reg->dict, ord_buf,
1276 zh, delete_w_handle);
1280 ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database)
1282 ZEBRA_RES ret = ZEBRA_OK;
1284 yaz_log(log_level, "zebra_drop_database");
1285 zebra_clearError(zh);
1287 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1289 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1293 zebraExplain_curDatabase (zh->reg->zei, database);
1295 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1296 zebraExplain_removeDatabase(zh->reg->zei, zh);
1300 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1303 zebra_end_trans (zh);
1307 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database)
1310 yaz_log(log_level, "zebra_create_database %s", database);
1312 zebra_clearError(zh);
1314 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1316 if (zebra_begin_trans (zh, 1))
1319 /* announce database */
1320 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1321 /* explainDatabase */))
1323 zebra_end_trans (zh);
1324 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, database);
1327 return zebra_end_trans (zh);
1330 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1331 const char *input_str, int input_len,
1332 char *output_str, int output_len)
1338 yaz_log(log_level, "zebra_string_norm ");
1339 zebra_clearError(zh);
1340 if (!zh->reg->zebra_maps)
1342 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1343 input_str, input_len);
1346 if (wrbuf_len(wrbuf) >= output_len)
1348 if (wrbuf_len(wrbuf))
1349 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1350 output_str[wrbuf_len(wrbuf)] = '\0';
1351 return wrbuf_len(wrbuf);
1354 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1356 char state_fname[256];
1361 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1362 zebra_clearError(zh);
1364 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1365 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1366 f = fopen (fname, "w");
1368 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1369 fprintf (f, "%c %d %ld\n", val, seqno, p);
1374 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1376 char state_fname[256];
1381 yaz_log(log_level, "zebra_get_state ");
1382 zebra_clearError(zh);
1383 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1384 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1385 f = fopen (fname, "r");
1391 fscanf (f, "%c %d", val, seqno);
1397 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1399 return zebra_begin_trans(zh, 0);
1402 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1404 return zebra_end_trans(zh);
1407 static void read_res_for_transaction(ZebraHandle zh)
1409 const char *group = res_get(zh->res, "group");
1411 /* FIXME - do we still use groups ?? */
1413 zh->m_group = group;
1414 v = res_get_prefix(zh->res, "followLinks", group, "1");
1415 zh->m_follow_links = atoi(v);
1417 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1418 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1420 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1421 zh->m_store_keys = atoi(v);
1423 v = res_get_prefix(zh->res, "storeData", group, "1");
1424 zh->m_store_data = atoi(v);
1426 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1427 zh->m_explain_database = atoi(v);
1429 v = res_get_prefix(zh->res, "openRW", group, "1");
1430 zh->m_flag_rw = atoi(v);
1432 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1433 zh->m_file_verbose_limit = atoi(v);
1436 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1439 zebra_select_default_database(zh);
1442 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1443 "zebra_begin_trans: no database selected");
1447 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1451 if (rw && !strchr(zh->user_perm, 'w'))
1455 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1467 const char *rval = 0;
1472 read_res_for_transaction(zh);
1475 if (zh->trans_no != 1)
1477 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1478 "zebra_begin_trans: no write trans within read");
1483 resultSetInvalidate (zh);
1484 zebra_register_close (zh->service, zh->reg);
1486 zh->trans_w_no = zh->trans_no;
1488 zebra_clearError(zh);
1490 zh->records_inserted = 0;
1491 zh->records_updated = 0;
1492 zh->records_deleted = 0;
1493 zh->records_processed = 0;
1495 #if HAVE_SYS_TIMES_H
1499 if (zh->shadow_enable)
1500 rval = res_get (zh->res, "shadow");
1502 for (pass = 0; pass < 2; pass++)
1506 zebra_lock_r (zh->lock_normal);
1507 zebra_lock_w (zh->lock_shadow);
1511 zebra_lock_w (zh->lock_normal);
1512 zebra_lock_w (zh->lock_shadow);
1515 zebra_get_state (zh, &val, &seqno);
1518 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1519 zebra_unlock (zh->lock_shadow);
1520 zebra_unlock (zh->lock_normal);
1524 else if (val == 'd')
1528 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1530 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1531 bf_commitClean (bfs, rval);
1536 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1543 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1547 zebra_set_state (zh, 'd', seqno);
1549 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1550 1, rval ? 1 : 0, zh->res,
1553 zh->reg->seqno = seqno;
1556 zebra_set_state (zh, 'o', seqno);
1558 zebra_unlock (zh->lock_shadow);
1559 zebra_unlock (zh->lock_normal);
1564 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1565 "zebra_begin_trans: cannot open register");
1566 yaz_log(YLOG_FATAL, "%s", zh->errString);
1578 if (zh->trans_no != 1)
1580 zebra_flush_reg (zh);
1583 zebra_clearError(zh);
1584 #if HAVE_SYS_TIMES_H
1590 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1593 if (!zh->lock_normal || !zh->lock_shadow)
1596 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1599 zebra_get_state (zh, &val, &seqno);
1605 else if (seqno != zh->reg->seqno)
1607 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1608 seqno, zh->reg->seqno);
1611 else if (zh->reg->last_val != val)
1613 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1614 val, zh->reg->last_val);
1621 zebra_lock_r (zh->lock_shadow);
1623 zebra_lock_r (zh->lock_normal);
1627 resultSetInvalidate (zh);
1628 zebra_register_close (zh->service, zh->reg);
1630 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1631 0, val == 'c' ? 1 : 0,
1632 zh->res, zh->path_reg);
1635 zebra_unlock (zh->lock_normal);
1636 zebra_unlock (zh->lock_shadow);
1638 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1641 zh->reg->last_val = val;
1642 zh->reg->seqno = seqno;
1644 read_res_for_transaction(zh);
1648 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1650 ZebraTransactionStatus dummy;
1652 yaz_log(log_level, "zebra_end_trans");
1653 return zebra_end_transaction(zh, &dummy);
1656 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1664 yaz_log(log_level, "zebra_end_transaction");
1666 status->processed = 0;
1667 status->inserted = 0;
1668 status->updated = 0;
1669 status->deleted = 0;
1673 if (!zh->res || !zh->reg)
1675 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1676 "zebra_end_trans: no open transaction");
1679 if (zh->trans_no != zh->trans_w_no)
1682 if (zh->trans_no != 0)
1685 /* release read lock */
1687 zebra_unlock (zh->lock_normal);
1688 zebra_unlock (zh->lock_shadow);
1691 { /* release write lock */
1695 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1696 rval = res_get (zh->res, "shadow");
1698 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1700 zebra_flush_reg (zh);
1702 resultSetInvalidate (zh);
1704 zebra_register_close (zh->service, zh->reg);
1707 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1708 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1709 zh->records_processed, zh->records_inserted,
1710 zh->records_updated, zh->records_deleted);
1712 status->processed = (int) zh->records_processed;
1713 status->inserted = (int) zh->records_inserted;
1714 status->updated = (int) zh->records_updated;
1715 status->deleted = (int) zh->records_deleted;
1717 zebra_get_state (zh, &val, &seqno);
1720 BFiles bfs = bfs_create (rval, zh->path_reg);
1721 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1722 bf_commitClean (bfs, rval);
1727 zebra_set_state (zh, 'o', seqno);
1729 zebra_unlock (zh->lock_shadow);
1730 zebra_unlock (zh->lock_normal);
1733 #if HAVE_SYS_TIMES_H
1735 yaz_log (log_level, "user/system: %ld/%ld",
1736 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1737 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1739 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1740 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1745 int zebra_repository_update (ZebraHandle zh, const char *path)
1749 zebra_clearError(zh);
1750 yaz_log (log_level, "updating %s", path);
1751 repositoryUpdate (zh, path);
1755 int zebra_repository_delete (ZebraHandle zh, const char *path)
1759 zebra_clearError(zh);
1760 yaz_log (log_level, "deleting %s", path);
1761 repositoryDelete (zh, path);
1765 int zebra_repository_show (ZebraHandle zh, const char *path)
1769 yaz_log(log_level, "zebra_repository_show");
1770 zebra_clearError(zh);
1771 repositoryShow (zh, path);
1775 static int zebra_commit_ex(ZebraHandle zh, int clean_only)
1782 zebra_clearError(zh);
1784 zebra_select_default_database(zh);
1787 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1790 rval = res_get (zh->res, "shadow");
1793 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1797 zebra_lock_w (zh->lock_normal);
1798 zebra_lock_r (zh->lock_shadow);
1800 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1802 zebra_get_state (zh, &val, &seqno);
1805 bf_cache (bfs, rval);
1806 if (bf_commitExists (bfs))
1809 zebra_set_state (zh, 'd', seqno);
1812 zebra_set_state (zh, 'c', seqno);
1814 yaz_log (YLOG_DEBUG, "commit start");
1815 bf_commitExec (bfs);
1820 yaz_log (YLOG_DEBUG, "commit clean");
1821 bf_commitClean (bfs, rval);
1823 zebra_set_state (zh, 'o', seqno);
1827 yaz_log (log_level, "nothing to commit");
1831 zebra_unlock (zh->lock_shadow);
1832 zebra_unlock (zh->lock_normal);
1836 ZEBRA_RES zebra_clean(ZebraHandle zh)
1839 yaz_log(log_level, "zebra_clean");
1840 return zebra_commit_ex(zh, 1);
1843 ZEBRA_RES zebra_commit(ZebraHandle zh)
1846 yaz_log(log_level, "zebra_commit");
1847 return zebra_commit_ex(zh, 0);
1850 ZEBRA_RES zebra_init(ZebraHandle zh)
1855 yaz_log(log_level, "zebra_init");
1856 zebra_clearError(zh);
1858 zebra_select_default_database(zh);
1861 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1862 "cannot select default database");
1865 rval = res_get (zh->res, "shadow");
1867 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1870 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
1874 bf_cache (bfs, rval);
1878 zebra_set_state (zh, 'o', 0);
1882 ZEBRA_RES zebra_compact(ZebraHandle zh)
1886 yaz_log(log_level, "zebra_compact");
1887 zebra_clearError(zh);
1890 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1893 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1899 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
1902 yaz_log(log_level, "zebra_result");
1903 *code = zh->errCode;
1904 *addinfo = zh->errString;
1907 void zebra_shadow_enable(ZebraHandle zh, int value)
1910 yaz_log(log_level, "zebra_shadow_enable");
1911 zebra_clearError(zh);
1912 zh->shadow_enable = value;
1915 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1919 yaz_log(log_level, "zebra_octet_term_encoding");
1920 zebra_clearError(zh);
1922 if (zh->iconv_to_utf8 != 0)
1923 yaz_iconv_close(zh->iconv_to_utf8);
1924 if (zh->iconv_from_utf8 != 0)
1925 yaz_iconv_close(zh->iconv_from_utf8);
1928 yaz_iconv_open ("UTF-8", encoding);
1929 if (zh->iconv_to_utf8 == 0)
1930 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1931 zh->iconv_from_utf8 =
1932 yaz_iconv_open (encoding, "UTF-8");
1933 if (zh->iconv_to_utf8 == 0)
1934 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1939 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1942 yaz_log(log_level, "zebra_record_encoding");
1943 zebra_clearError(zh);
1944 xfree(zh->record_encoding);
1945 zh->record_encoding = 0;
1947 zh->record_encoding = xstrdup (encoding);
1951 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1956 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
1957 zebra_clearError(zh);
1958 res_set(zh->res, name, value);
1961 const char *zebra_get_resource(ZebraHandle zh,
1962 const char *name, const char *defaultvalue)
1967 assert(defaultvalue);
1968 v = res_get_def (zh->res, name, (char *)defaultvalue);
1969 zebra_clearError(zh);
1970 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
1974 /* moved from zebra_api_ext.c by pop */
1975 /* FIXME: Should this really be public??? -Heikki */
1977 int zebra_trans_no (ZebraHandle zh)
1980 yaz_log(log_level, "zebra_trans_no");
1981 return zh->trans_no;
1984 int zebra_get_shadow_enable (ZebraHandle zh)
1987 yaz_log(log_level, "zebra_get_shadow_enable");
1988 return zh->shadow_enable;
1991 void zebra_set_shadow_enable (ZebraHandle zh, int value)
1994 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
1995 zh->shadow_enable = value;
1998 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
1999 so that it's identicical to the original api_records_retrieve */
2000 void api_records_retrieve (ZebraHandle zh, ODR stream,
2001 const char *setname, Z_RecordComposition *comp,
2002 oid_value input_format, int num_recs,
2003 ZebraRetrievalRecord *recs)
2005 zebra_records_retrieve(zh, stream, setname, comp, input_format,
2009 /* ---------------------------------------------------------------------------
2010 Record insert(=update), delete
2012 If sysno is provided, then it's used to identify the record.
2013 If not, and match_criteria is provided, then sysno is guessed
2014 If not, and a record is provided, then sysno is got from there
2015 NOTE: Now returns 0 at success and updates sysno, which is an int*
2019 int zebra_add_record(ZebraHandle zh,
2020 const char *buf, int buf_size)
2023 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
2026 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
2027 const char *recordType,
2028 SYSNO *sysno, const char *match,
2030 const char *buf, int buf_size, int force_update)
2036 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2039 buf_size = strlen(buf);
2041 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2043 res = buffer_extract_record (zh, buf, buf_size,
2044 0, /* delete_flag */
2050 0); /* allow_update */
2051 zebra_end_trans(zh);
2055 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2056 const char *recordType,
2057 SYSNO* sysno, const char *match,
2059 const char *buf, int buf_size,
2067 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2069 if (buf_size < 1) buf_size = strlen(buf);
2071 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2073 res = buffer_extract_record (zh, buf, buf_size,
2074 0, /* delete_flag */
2080 1); /* allow_update */
2081 zebra_end_trans(zh);
2085 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2086 const char *recordType,
2087 SYSNO *sysno, const char *match,
2089 const char *buf, int buf_size,
2096 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2098 if (buf_size < 1) buf_size = strlen(buf);
2100 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2102 res = buffer_extract_record (zh, buf, buf_size,
2103 1, /* delete_flag */
2109 1); /* allow_update */
2110 zebra_end_trans(zh);
2114 /* ---------------------------------------------------------------------------
2118 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2119 const char *setname, zint *numhits)
2122 ZEBRA_RES res = ZEBRA_OK;
2124 ODR odr = odr_createmem(ODR_ENCODE);
2129 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2131 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2135 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2136 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2140 res = zebra_search_RPN(zh, odr, query, setname, &hits);
2144 yaz_log(log_level, "Hits: " ZINT_FORMAT, hits);
2152 /* ---------------------------------------------------------------------------
2153 Sort - a simplified interface, with optional read locks.
2155 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2156 const char *sort_spec,
2157 const char *output_setname,
2158 const char **input_setnames)
2160 int num_input_setnames = 0;
2161 int sort_status = 0;
2162 Z_SortKeySpecList *sort_sequence;
2166 assert(output_setname);
2167 assert(input_setnames);
2168 sort_sequence = yaz_sort_spec (stream, sort_spec);
2169 yaz_log(log_level, "sort (FIXME) ");
2172 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2173 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2177 /* we can do this, since the perl typemap code for char** will
2178 put a NULL at the end of list */
2179 while (input_setnames[num_input_setnames]) num_input_setnames++;
2181 if (zebra_begin_read (zh))
2184 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2185 output_setname, sort_sequence, &sort_status);
2191 /* ---------------------------------------------------------------------------
2192 Get BFS for Zebra system (to make alternative storage methods)
2194 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2197 return zh->reg->bfs;
2202 /* ---------------------------------------------------------------------------
2203 Set limit for search/scan
2205 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2208 zebra_limit_destroy(zh->m_limit);
2209 zh->m_limit = zebra_limit_create(complement_flag, ids);
2214 Set Error code + addinfo
2216 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2219 nmem_reset(zh->nmem_error);
2220 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2223 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2226 sprintf(vstr, ZINT_FORMAT, i);
2229 nmem_reset(zh->nmem_error);
2230 zh->errString = nmem_strdup(zh->nmem_error, vstr);