IDZEBRA 2.2.8
zebraapi.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18*/
19
20#if HAVE_CONFIG_H
21#include <config.h>
22#endif
23#include <assert.h>
24#include <stdio.h>
25#include <limits.h>
26#ifdef WIN32
27#include <io.h>
28#include <process.h>
29#include <direct.h>
30#endif
31#if HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34
35#include <yaz/diagbib1.h>
36#include <yaz/snprintf.h>
37#include <yaz/pquery.h>
38#include <yaz/sortspec.h>
39#include "index.h"
40#include "rank.h"
41#include "orddict.h"
42#include <charmap.h>
43#include <idzebra/api.h>
44#include <yaz/oid_db.h>
45
46#define DEFAULT_APPROX_LIMIT 2000000000
47
48/* simple asserts to validate the most essential input args */
49#define ASSERTZH assert(zh && zh->service)
50#define ASSERTZHRES assert(zh && zh->service && zh->res)
51#define ASSERTZS assert(zs)
52
53static int log_level = 0;
54static int log_level_initialized = 0;
55
56static void zebra_open_res(ZebraHandle zh);
57static void zebra_close_res(ZebraHandle zh);
58
60{
61 if (zh)
62 return ZEBRA_OK;
63 return ZEBRA_FAIL;
64}
65
66#define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
67
69{
70 const char *dir ;
71 int r;
73 yaz_log(log_level, "zebra_chdir");
74 dir = res_get(zs->global_res, "chdir");
75 if (!dir)
76 return 0;
77 yaz_log(YLOG_DEBUG, "chdir %s", dir);
78#ifdef WIN32
79 r = _chdir(dir);
80#else
81 r = chdir(dir);
82#endif
83 if (r)
84 yaz_log(YLOG_FATAL|YLOG_ERRNO, "chdir %s", dir);
85 return r;
86}
87
89{
91 yaz_log(log_level, "zebra_flush_reg");
92 zebraExplain_flush(zh->reg->zei, zh);
93
95
97 return ZEBRA_OK;
98}
99
101 const char *name,
102 int rw, int useshadow,
103 Res res,
104 const char *reg_path);
105static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
106
108{
109 assert(zh && zh->session_res);
110 return res_get_def(zh->session_res, "encoding", "ISO-8859-1");
111}
112
114{
115 ZebraHandle zh;
116 const char *default_encoding;
118 {
119 log_level = yaz_log_module_level("zebraapi");
121 }
122
123 yaz_log(log_level, "zebra_open");
124
125 if (!zs)
126 return 0;
127
128 zh = (ZebraHandle) xmalloc(sizeof(*zh));
129 yaz_log(YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
130
131 zh->service = zs;
132 zh->reg = 0; /* no register attached yet */
133 zh->sets = 0;
134 zh->destroyed = 0;
135 zh->errCode = 0;
136 zh->errString = 0;
137 zh->res = 0;
138 zh->session_res = res_open(zs->global_res, res);
139 zh->user_perm = 0;
140 zh->dbaccesslist = 0;
141
142 zh->reg_name = xstrdup("");
143 zh->path_reg = 0;
144 zh->num_basenames = 0;
145 zh->basenames = 0;
146
148 zh->trans_no = 0;
149 zh->trans_w_no = 0;
150
151 zh->lock_normal = 0;
152 zh->lock_shadow = 0;
153
154 zh->shadow_enable = 1;
155 zh->m_staticrank = 0;
156 zh->m_segment_indexing = 0;
157
158 zh->break_handler_func = 0;
159 zh->break_handler_data = 0;
160
161 default_encoding = zebra_get_encoding(zh);
162
163 zh->iconv_to_utf8 =
164 yaz_iconv_open("UTF-8", default_encoding);
165 if (zh->iconv_to_utf8 == 0)
166 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported",
167 default_encoding);
168 zh->iconv_from_utf8 =
169 yaz_iconv_open(default_encoding, "UTF-8");
170 if (zh->iconv_to_utf8 == 0)
171 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported",
172 default_encoding);
173
174 zh->record_encoding = 0;
175
177
178 zh->next = zs->sessions;
179 zs->sessions = zh;
180
182
183 zh->store_data_buf = 0;
184
185 zh->m_limit = zebra_limit_create(1, 0);
186
187 zh->nmem_error = nmem_create();
188
189 return zh;
190}
191
192ZebraService zebra_start(const char *configName)
193{
194 return zebra_start_res(configName, 0, 0);
195}
196
197ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
198{
199 Res res;
200 char version_str[16];
201 char system_str[80];
202
204
206 {
207 log_level = yaz_log_module_level("zebraapi");
209 }
210
211 *system_str = '\0';
212 *version_str = '\0';
213 zebra_get_version(version_str, system_str);
214
215 yaz_log(YLOG_LOG, "zebra_start %s %s", version_str, system_str);
216 if (configName)
217 yaz_log(YLOG_LOG, "config %s", configName);
218
219 yaz_log_xml_errors(0, YLOG_LOG);
220
221 if ((res = res_open(def_res, over_res)))
222 {
223 const char *passwd_plain = 0;
224 const char *passwd_encrypt = 0;
225 const char *dbaccess = 0;
226 ZebraService zh = 0;
227
228 if (configName)
229 {
230 ZEBRA_RES ret = res_read_file(res, configName);
231 if (ret != ZEBRA_OK)
232 {
233 res_close(res);
234 return 0;
235 }
236 if (zebra_check_res(res))
237 {
238 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
239 configName);
240 return 0;
241 }
242 }
243 else
244 {
245 zebra_check_res(res);
246 }
247
248 zh = xmalloc(sizeof(*zh));
249 zh->global_res = res;
250 zh->sessions = 0;
251
252 if (zebra_chdir(zh))
253 {
254 xfree(zh);
255 return 0;
256 }
257
259 passwd_plain = res_get(zh->global_res, "passwd");
260 passwd_encrypt = res_get(zh->global_res, "passwd.c");
261 dbaccess = res_get(zh->global_res, "dbaccess");
262
263 if (!passwd_plain && !passwd_encrypt)
264 zh->passwd_db = NULL;
265 else
266 {
268 if (!zh->passwd_db)
269 yaz_log(YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
270 else
271 {
272 if (passwd_plain)
273 passwd_db_file_plain(zh->passwd_db, passwd_plain);
274 if (passwd_encrypt)
275 passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
276 }
277 }
278
279 if (!dbaccess)
280 zh->dbaccess = NULL;
281 else {
282 zh->dbaccess = res_open(NULL, NULL);
283 if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
284 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
285 return NULL;
286 }
287 }
288
289 zh->timing = yaz_timing_create();
290 zh->path_root = res_get(zh->global_res, "root");
291 zh->nmem = nmem_create();
293
294 if (1)
295 {
296 const char *module_path = res_get(res, "modulePath");
297 if (module_path)
299 module_path);
300 }
301 return zh;
302 }
303 return 0;
304}
305
307 void(*cb)(void *cd, const char *name))
308{
309 ASSERTZS;
310 assert(cb);
312}
313
314void zebra_pidfname(ZebraService zs, char *path)
315{
316 ASSERTZS;
317 zebra_lock_prefix(zs->global_res, path);
318 strcat(path, "zebrasrv.pid");
319}
320
321Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
322 int compact_flag, Res res)
323{
324 int page_size = 4096;
325 char res_str[200];
326 yaz_snprintf(res_str, sizeof(res_str), "dict.%s.pagesize", name);
327 assert(bfs);
328 assert(name);
329
330 if (res_get_int(res, res_str, &page_size) == ZEBRA_OK)
331 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
332 page_size, name);
333 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
334}
335
336static
338 int rw, int useshadow, Res res,
339 const char *reg_path)
340{
341 struct zebra_register *reg;
342 int record_compression = REC_COMPRESS_NONE;
343 const char *compression_str = 0;
344 const char *profilePath;
345 int sort_type = ZEBRA_SORT_TYPE_FLAT;
346 ZEBRA_RES ret = ZEBRA_OK;
347
348 ASSERTZS;
349
350 reg = xmalloc(sizeof(*reg));
351
352 assert(name);
353 reg->name = xstrdup(name);
354
355 reg->seqno = 0;
356 reg->last_val = 0;
357
358 assert(res);
359
360 yaz_log(YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
361 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
362
363 reg->dh = data1_create();
364 if (!reg->dh)
365 {
366 xfree(reg->name);
367 xfree(reg);
368 return 0;
369 }
370 reg->bfs = bfs_create(res_get(res, "register"), reg_path);
371 if (!reg->bfs)
372 {
373 data1_destroy(reg->dh);
374 xfree(reg->name);
375 xfree(reg);
376 return 0;
377 }
378 if (useshadow)
379 {
380 if (bf_cache(reg->bfs, res_get(res, "shadow")) == ZEBRA_FAIL)
381 {
382 bfs_destroy(reg->bfs);
383 data1_destroy(reg->dh);
384 xfree(reg->name);
385 xfree(reg);
386 return 0;
387 }
388 }
389
390 profilePath = res_get_def(res, "profilePath", 0);
391
392 data1_set_tabpath(reg->dh, profilePath);
393 data1_set_tabroot(reg->dh, reg_path);
394 reg->recTypes = recTypes_init(zs->record_classes, reg->dh);
395
396 reg->zebra_maps =
397 zebra_maps_open(res, reg_path, profilePath);
398 if (!reg->zebra_maps)
399 {
401 bfs_destroy(reg->bfs);
402 data1_destroy(reg->dh);
403 xfree(reg->name);
404 xfree(reg);
405 return 0;
406 }
407 reg->rank_classes = NULL;
408
409 reg->key_block = 0;
410 reg->keys = zebra_rec_keys_open();
411
413
414 reg->records = 0;
415 reg->dict = 0;
416 reg->sort_index = 0;
417 reg->isams = 0;
418 reg->matchDict = 0;
419 reg->isamc = 0;
420 reg->isamb = 0;
421 reg->zei = 0;
422
423 /* installing rank classes */
428
429 compression_str = res_get_def(res, "recordCompression", "none");
430 if (!strcmp(compression_str, "none"))
431 record_compression = REC_COMPRESS_NONE;
432 else if (!strcmp(compression_str, "bzip2"))
433 record_compression = REC_COMPRESS_BZIP2;
434 else if (!strcmp(compression_str, "zlib"))
435 record_compression = REC_COMPRESS_ZLIB;
436 else
437 {
438 yaz_log(YLOG_FATAL, "invalid recordCompression: %s", compression_str);
439 ret = ZEBRA_FAIL;
440 }
441
442 if (!rec_check_compression_method(record_compression))
443 {
444 yaz_log(YLOG_FATAL, "unsupported recordCompression: %s",
445 compression_str);
446 ret = ZEBRA_FAIL;
447 }
448
449 {
450 const char *index_fname = res_get_def(res, "index", "default.idx");
451 if (index_fname && *index_fname && strcmp(index_fname, "none"))
452 {
453 if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
454 ret = ZEBRA_FAIL;
455 }
456 else
457 {
459 }
460 }
461
462 if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
463 {
464 yaz_log(YLOG_WARN, "rec_open failed");
465 ret = ZEBRA_FAIL;
466 }
467 if (rw)
468 {
469 reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
470 }
471 if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
472 {
473 yaz_log(YLOG_WARN, "dict_open failed");
474 ret = ZEBRA_FAIL;
475 }
476
477
478 if (res_get_match(res, "sortindex", "f", "f"))
479 sort_type = ZEBRA_SORT_TYPE_FLAT;
480 else if (res_get_match(res, "sortindex", "i", "f"))
481 sort_type = ZEBRA_SORT_TYPE_ISAMB;
482 else if (res_get_match(res, "sortindex", "m", "f"))
483 sort_type = ZEBRA_SORT_TYPE_MULTI;
484 else
485 {
486 yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
487 ret = ZEBRA_FAIL;
488 }
489
490
491 if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
492 {
493 yaz_log(YLOG_WARN, "zebra_sort_open failed");
494 ret = ZEBRA_FAIL;
495 }
496 if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
497 {
498 struct ISAMS_M_s isams_m;
499 if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
500 key_isams_m(res, &isams_m))))
501 {
502 yaz_log(YLOG_WARN, "isams_open failed");
503 ret = ZEBRA_FAIL;
504 }
505 }
506 if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
507 {
508 struct ISAMC_M_s isamc_m;
509 if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
510 rw, key_isamc_m(res, &isamc_m))))
511 {
512 yaz_log(YLOG_WARN, "isamc_open failed");
513 ret = ZEBRA_FAIL;
514 }
515 }
516 if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
517 {
518 struct ISAMC_M_s isamc_m;
519
520 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
521 rw, key_isamc_m(res, &isamc_m), 0)))
522 {
523 yaz_log(YLOG_WARN, "isamb_open failed");
524 ret = ZEBRA_FAIL;
525 }
526 }
527 if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
528 {
529 struct ISAMC_M_s isamc_m;
530
531 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
532 rw, key_isamc_m(res, &isamc_m), 1)))
533 {
534 yaz_log(YLOG_WARN, "isamb_open failed");
535 ret = ZEBRA_FAIL;
536 }
537 }
538 if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
539 {
540 struct ISAMC_M_s isamc_m;
541
542 if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
543 rw, key_isamc_m(res, &isamc_m), -1)))
544 {
545 yaz_log(YLOG_WARN, "isamb_open failed");
546 ret = ZEBRA_FAIL;
547 }
548 }
549 if (ret == ZEBRA_OK)
550 {
551 reg->zei = zebraExplain_open(reg->records, reg->dh,
552 res, rw, reg,
554 if (!reg->zei)
555 {
556 yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
557 ret = ZEBRA_FAIL;
558 }
559 }
560
561 if (ret != ZEBRA_OK)
562 {
563 zebra_register_close(zs, reg);
564 return 0;
565 }
566 yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
567 return reg;
568}
569
571{
573 yaz_log(log_level, "zebra_admin_shutdown");
574
576 zh->service->stop_flag = 1;
578 return ZEBRA_OK;
579}
580
582{
583 ZebraService zs;
585 yaz_log(log_level, "zebra_admin_start");
586 zs = zh->service;
589 return ZEBRA_OK;
590}
591
593{
594 ASSERTZS;
595 assert(reg);
596 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
597 reg->stop_flag = 0;
598 zebra_chdir(zs);
599
601 dict_close(reg->dict);
602 if (reg->matchDict)
603 dict_close(reg->matchDict);
605 if (reg->isams)
606 isams_close(reg->isams);
607 if (reg->isamc)
608 isamc_close(reg->isamc);
609 if (reg->isamb)
610 isamb_close(reg->isamb);
611 rec_close(&reg->records);
612
615 zebraRankDestroy(reg);
616 bfs_destroy(reg->bfs);
617 data1_destroy(reg->dh);
618
621
623 xfree(reg->name);
624 xfree(reg);
625}
626
628{
629 if (!zs)
630 return ZEBRA_OK;
631 while (zs->sessions)
632 {
634 }
635
637
638 if (zs->passwd_db)
640
642 nmem_destroy(zs->nmem);
644
645 yaz_timing_stop(zs->timing);
646 yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
647 yaz_timing_get_real(zs->timing),
648 yaz_timing_get_user(zs->timing),
649 yaz_timing_get_sys(zs->timing));
650
651
652 yaz_timing_destroy(&zs->timing);
653 xfree(zs);
654 return ZEBRA_OK;
655}
656
658{
659 ZebraService zs;
660 struct zebra_session **sp;
661 int i;
662
663 yaz_log(log_level, "zebra_close");
665
666 zh->errCode = 0;
667
668 zs = zh->service;
669 yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
670 resultSetDestroy(zh, -1, 0, 0);
671
672 if (zh->reg)
674 zebra_close_res(zh);
676
677 xfree(zh->record_encoding);
678
679 xfree(zh->dbaccesslist);
680
681 for (i = 0; i < zh->num_basenames; i++)
682 xfree(zh->basenames[i]);
683 xfree(zh->basenames);
684
685 if (zh->iconv_to_utf8 != 0)
686 yaz_iconv_close(zh->iconv_to_utf8);
687 if (zh->iconv_from_utf8 != 0)
688 yaz_iconv_close(zh->iconv_from_utf8);
689
693 sp = &zs->sessions;
694 while (1)
695 {
696 assert(*sp);
697 if (*sp == zh)
698 {
699 *sp = (*sp)->next;
700 break;
701 }
702 sp = &(*sp)->next;
703 }
705 xfree(zh->reg_name);
706 xfree(zh->user_perm);
707 zh->service = 0; /* more likely to trigger an assert */
708
710
711 nmem_destroy(zh->nmem_error);
712
713 xfree(zh->path_reg);
714 xfree(zh);
715 return ZEBRA_OK;
716}
717
727
729{
730 char fname[512];
731 ASSERTZH;
732 zh->errCode = 0;
733
734 if (zh->path_reg)
735 {
736 yaz_snprintf(fname, sizeof(fname), "%s/zebra.cfg", zh->path_reg);
737 zh->res = res_open(zh->session_res, 0);
738 res_read_file(zh->res, fname);
739 }
740 else if (*zh->reg_name == 0)
741 {
742 zh->res = res_open(zh->session_res, 0);
743 }
744 else
745 {
746 yaz_log(YLOG_WARN, "no register root specified");
747 zh->res = 0; /* no path for register - fail! */
748 }
749}
750
752{
753 ASSERTZH;
754 zh->errCode = 0;
755 res_close(zh->res);
756 zh->res = 0;
757}
758
759static void zebra_select_register(ZebraHandle zh, const char *new_reg)
760{
761 ASSERTZH;
762 zh->errCode = 0;
763 if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
764 return;
765 if (!zh->res)
766 {
767 assert(zh->reg == 0);
768 assert(*zh->reg_name == 0);
769 }
770 else
771 {
772 if (zh->reg)
773 {
776 zh->reg = 0;
777 }
778 zebra_close_res(zh);
779 }
780 xfree(zh->reg_name);
781 zh->reg_name = xstrdup(new_reg);
782
783 xfree(zh->path_reg);
784 zh->path_reg = 0;
785 if (zh->service->path_root)
786 {
787 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
788 strlen(zh->reg_name) + 3);
789 strcpy(zh->path_reg, zh->service->path_root);
790 if (*zh->reg_name)
791 {
792 strcat(zh->path_reg, "/");
793 strcat(zh->path_reg, zh->reg_name);
794 }
795 }
796 zebra_open_res(zh);
797
798 if (zh->lock_normal)
800 zh->lock_normal = 0;
801
802 if (zh->lock_shadow)
804 zh->lock_shadow = 0;
805
806 if (zh->res)
807 {
808 char fname[512];
809 const char *lock_area = res_get(zh->res, "lockDir");
810
811 if (!lock_area && zh->path_reg)
812 res_set(zh->res, "lockDir", zh->path_reg);
813 yaz_snprintf(fname, sizeof(fname), "norm.%s.LCK", zh->reg_name);
814 zh->lock_normal =
815 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
816
817 yaz_snprintf(fname, sizeof(fname), "shadow.%s.LCK", zh->reg_name);
818 zh->lock_shadow =
819 zebra_lock_create(res_get(zh->res, "lockDir"), fname);
820
821 if (!zh->lock_normal || !zh->lock_shadow)
822 {
823 if (zh->lock_normal)
824 {
826 zh->lock_normal = 0;
827 }
828 if (zh->lock_shadow)
829 {
831 zh->lock_shadow = 0;
832 }
833 zebra_close_res(zh);
834 }
835 }
836 if (zh->res)
837 {
838 int approx = 0;
839 if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
840 zebra_set_approx_limit(zh, approx);
841 }
842 if (zh->res)
843 {
844 if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
845 yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
846 }
847 if (zh->res)
848 {
849 if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) ==
850 ZEBRA_OK)
851 {
852 yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
854 }
855 }
856}
857
858void map_basenames_func(void *vp, const char *name, const char *value)
859{
860 struct map_baseinfo *p = (struct map_baseinfo *) vp;
861 int i, no;
862 char fromdb[128], todb[8][128];
863
864 assert(value);
865 assert(name);
866 assert(vp);
867
868 no =
869 sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
870 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
871 todb[5], todb[6], todb[7]);
872 if (no < 2)
873 return ;
874 no--;
875 for (i = 0; i<p->num_bases; i++)
876 if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
877 {
878 p->basenames[i] = 0;
879 for (i = 0; i < no; i++)
880 {
881 if (p->new_num_bases == p->new_num_max)
882 return;
883 p->new_basenames[(p->new_num_bases)++] =
884 nmem_strdup(p->mem, todb[i]);
885 }
886 return;
887 }
888}
889
891{
892 if (!zh->res)
893 {
894 /* no database has been selected - so we select based on
895 resource setting (including group)
896 */
897 const char *group = res_get(zh->session_res, "group");
898 const char *v = res_get_prefix(zh->session_res,
899 "database", group, "Default");
900 return zebra_select_database(zh, v);
901 }
902 return 0;
903}
904
905void map_basenames(ZebraHandle zh, ODR stream,
906 int *num_bases, char ***basenames)
907{
908 struct map_baseinfo info;
909 struct map_baseinfo *p = &info;
910 int i;
911 ASSERTZH;
912 yaz_log(log_level, "map_basenames ");
913 assert(stream);
914
915 info.zh = zh;
916
917 info.num_bases = *num_bases;
918 info.basenames = *basenames;
919 info.new_num_max = 128;
920 info.new_num_bases = 0;
921 info.new_basenames = (char **)
922 odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
923 info.mem = stream->mem;
924
925 res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
926
927 for (i = 0; i<p->num_bases; i++)
928 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
929 {
930 p->new_basenames[(p->new_num_bases)++] =
931 nmem_strdup(p->mem, p->basenames[i]);
932 }
933 *num_bases = info.new_num_bases;
934 *basenames = info.new_basenames;
935 for (i = 0; i<*num_bases; i++)
936 yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
937}
938
940{
942
943 yaz_log(log_level, "zebra_select_database %s",basename);
944 assert(basename);
945 return zebra_select_databases(zh, 1, &basename);
946}
947
949 const char **basenames)
950{
951 int i;
952 const char *cp;
953 int len = 0;
954 char *new_reg = 0;
955
957 assert(basenames);
958
959 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
961 zh->errCode = 0;
962
963 if (num_bases < 1)
964 {
965 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
966 return ZEBRA_FAIL;
967 }
968
969 /* Check if the user has access to all databases (Seb) */
970 /* You could argue that this should happen later, after we have
971 * determined that the database(s) exist. */
972 if (zh->dbaccesslist) {
973 for (i = 0; i < num_bases; i++) {
974 const char *db = basenames[i];
975 char *p, *pp;
976 for (p = zh->dbaccesslist; p && *p; p = pp) {
977 int len;
978 if ((pp = strchr(p, '+'))) {
979 len = pp - p;
980 pp++;
981 }
982 else
983 len = strlen(p);
984 if (len == strlen(db) && !strncmp(db, p, len))
985 break;
986 }
987 if (!p) {
988 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
989 return ZEBRA_FAIL;
990 }
991 }
992 }
993
994 for (i = 0; i < zh->num_basenames; i++)
995 xfree(zh->basenames[i]);
996 xfree(zh->basenames);
997
999 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
1000 for (i = 0; i < zh->num_basenames; i++)
1001 zh->basenames[i] = xstrdup(basenames[i]);
1002
1003 cp = strrchr(basenames[0], '/');
1004 if (cp)
1005 {
1006 len = cp - basenames[0];
1007 new_reg = xmalloc(len + 1);
1008 memcpy(new_reg, basenames[0], len);
1009 new_reg[len] = '\0';
1010 }
1011 else
1012 new_reg = xstrdup("");
1013 for (i = 1; i<num_bases; i++)
1014 {
1015 const char *cp1;
1016
1017 cp1 = strrchr(basenames[i], '/');
1018 if (cp)
1019 {
1020 if (!cp1)
1021 {
1022 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1023 return -1;
1024 }
1025 if (len != cp1 - basenames[i] ||
1026 memcmp(basenames[i], new_reg, len))
1027 {
1028 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1029 return -1;
1030 }
1031 }
1032 else
1033 {
1034 if (cp1)
1035 {
1036 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1037 return ZEBRA_FAIL;
1038 }
1039 }
1040 }
1041 zebra_select_register(zh, new_reg);
1042 xfree(new_reg);
1043 if (!zh->res)
1044 {
1045 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1046 return ZEBRA_FAIL;
1047 }
1048 if (!zh->lock_normal || !zh->lock_shadow)
1049 {
1050 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1051 return ZEBRA_FAIL;
1052 }
1053 return ZEBRA_OK;
1054}
1055
1057{
1058 if (approx_limit == 0)
1059 approx_limit = DEFAULT_APPROX_LIMIT;
1060 zh->approx_limit = approx_limit;
1061 return ZEBRA_OK;
1062}
1063
1068
1069
1071 int (*f)(void *client_data),
1072 void *client_data)
1073{
1075 zh->break_handler_data = client_data;
1076 return ZEBRA_OK;
1077}
1078
1079ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1080 const char *setname, zint *hits,
1081 int *estimated_hit_count,
1082 int *partial_resultset)
1083{
1084 ZEBRA_RES r;
1085
1087
1088 assert(o);
1089 assert(query);
1090 assert(hits);
1091 assert(setname);
1092 yaz_log(log_level, "zebra_search_rpn");
1093
1094 zh->partial_result = 0;
1095
1097 return ZEBRA_FAIL;
1098
1099 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
1100 zh->num_basenames, zh->basenames, setname,
1101 hits, estimated_hit_count);
1102
1103 *partial_resultset = zh->partial_result;
1105 return r;
1106}
1107
1108ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1109 const char *setname, zint *hits)
1110{
1111 int estimated_hit_count;
1112 int partial_resultset;
1113 return zebra_search_RPN_x(zh, o, query, setname, hits,
1114 &estimated_hit_count,
1115 &partial_resultset);
1116}
1117
1119 const char *setname,
1120 Z_RecordComposition *comp,
1121 const Odr_oid *input_format, int num_recs,
1123{
1124 ZebraMetaRecord *poset;
1125 int i;
1126 ZEBRA_RES ret = ZEBRA_OK;
1127 zint *pos_array;
1128
1130 assert(stream);
1131 assert(setname);
1132 assert(recs);
1133 assert(num_recs>0);
1134
1135 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1136
1137 if (!zh->res)
1138 {
1139 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1140 setname);
1141 return ZEBRA_FAIL;
1142 }
1143
1145 return ZEBRA_FAIL;
1146
1147 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1148 for (i = 0; i<num_recs; i++)
1149 pos_array[i] = recs[i].position;
1150 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1151 if (!poset)
1152 {
1153 yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1154 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1155 setname);
1156 ret = ZEBRA_FAIL;
1157 }
1158 else
1159 {
1160 WRBUF addinfo_w = wrbuf_alloc();
1161 for (i = 0; i < num_recs; i++)
1162 {
1163 recs[i].errCode = 0;
1164 recs[i].errString = 0;
1165 recs[i].format = 0;
1166 recs[i].len = 0;
1167 recs[i].buf = 0;
1168 recs[i].base = 0;
1169 recs[i].sysno = poset[i].sysno;
1170 if (poset[i].term)
1171 {
1172 recs[i].format = yaz_oid_recsyn_sutrs;
1173 recs[i].len = strlen(poset[i].term);
1174 recs[i].buf = poset[i].term;
1175 recs[i].base = poset[i].db;
1176 }
1177 else if (poset[i].sysno)
1178 {
1179 char *buf;
1180 int len = 0;
1181 zebra_snippets *hit_snippet = zebra_snippets_create();
1182
1183 /* we disable hit snippets for now. It does not work well
1184 and it slows retrieval down a lot */
1185#if 0
1186 zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
1187 hit_snippet);
1188#endif
1189 wrbuf_rewind(addinfo_w);
1190 recs[i].errCode =
1191 zebra_record_fetch(zh, setname,
1192 poset[i].sysno, poset[i].score,
1193 stream, input_format, comp,
1194 &recs[i].format, &buf, &len,
1195 &recs[i].base, addinfo_w);
1196
1197 if (wrbuf_len(addinfo_w))
1198 recs[i].errString =
1199 odr_strdup(stream, wrbuf_cstr(addinfo_w));
1200 recs[i].len = len;
1201 if (len > 0)
1202 {
1203 recs[i].buf = (char*) odr_malloc(stream, len);
1204 memcpy(recs[i].buf, buf, len);
1205 }
1206 else
1207 recs[i].buf = buf;
1208 recs[i].score = poset[i].score;
1209 zebra_snippets_destroy(hit_snippet);
1210 }
1211 else
1212 {
1213 /* only need to set it once */
1214 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1215 {
1217 YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1218 pos_array[i]);
1219 ret = ZEBRA_FAIL;
1220 break;
1221 }
1222 }
1223 }
1224 zebra_meta_records_destroy(zh, poset, num_recs);
1225 wrbuf_destroy(addinfo_w);
1226 }
1228 xfree(pos_array);
1229 return ret;
1230}
1231
1232ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1233 int *position,
1234 int *num_entries, ZebraScanEntry **entries,
1235 int *is_partial,
1236 const char *setname)
1237{
1238 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1239 Z_AttributesPlusTerm *zapt;
1240 Odr_oid *attributeSet;
1241 ZEBRA_RES res;
1242
1243 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1244 {
1245 res = ZEBRA_FAIL;
1246 zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1247 }
1248 else
1249 {
1250 res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1251 position, num_entries, entries, is_partial,
1252 setname);
1253 }
1254 yaz_pqf_destroy(pqf_parser);
1255 return res;
1256}
1257
1258ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1259 const Odr_oid *attributeset,
1260 int *position,
1261 int *num_entries, ZebraScanEntry **entries,
1262 int *is_partial,
1263 const char *setname)
1264{
1265 ZEBRA_RES res;
1266
1268
1269 assert(stream);
1270 assert(zapt);
1271 assert(position);
1272 assert(num_entries);
1273 assert(is_partial);
1274 assert(entries);
1275 yaz_log(log_level, "zebra_scan");
1276
1278 {
1279 *entries = 0;
1280 *num_entries = 0;
1281 return ZEBRA_FAIL;
1282 }
1283
1284 res = rpn_scan(zh, stream, zapt, attributeset,
1285 zh->num_basenames, zh->basenames, position,
1286 num_entries, entries, is_partial, setname);
1288 return res;
1289}
1290
1292 int num_input_setnames, const char **input_setnames,
1293 const char *output_setname,
1294 Z_SortKeySpecList *sort_sequence,
1295 int *sort_status)
1296{
1297 ZEBRA_RES res;
1299 assert(stream);
1300 assert(num_input_setnames>0);
1301 assert(input_setnames);
1302 assert(sort_sequence);
1303 assert(sort_status);
1304 yaz_log(log_level, "zebra_sort");
1305
1307 return ZEBRA_FAIL;
1308 res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1309 output_setname, sort_sequence, sort_status);
1311 return res;
1312}
1313
1315 int num_setnames, char **setnames,
1316 int *statuses)
1317{
1318 int i, status;
1319 ASSERTZH;
1320 yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1321
1322 if (zebra_begin_read(zh))
1323 return Z_DeleteStatus_systemProblemAtTarget;
1324 switch (function)
1325 {
1326 case Z_DeleteResultSetRequest_list:
1327 assert(num_setnames>0);
1328 assert(setnames);
1329 resultSetDestroy(zh, num_setnames, setnames, statuses);
1330 break;
1331 case Z_DeleteResultSetRequest_all:
1332 resultSetDestroy(zh, -1, 0, statuses);
1333 break;
1334 }
1336 status = Z_DeleteStatus_success;
1337 for (i = 0; i<num_setnames; i++)
1338 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1339 status = statuses[i];
1340 return status;
1341}
1342
1344{
1345 if (zh)
1346 {
1347 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1348 return zh->errCode;
1349 }
1350 yaz_log(log_level, "zebra_errCode: o");
1351 return 0;
1352}
1353
1355{
1356 const char *e = 0;
1357 if (zh)
1358 e= diagbib1_str(zh->errCode);
1359 yaz_log(log_level, "zebra_errString: %s",e);
1360 return e;
1361}
1362
1364{
1365 char *a = 0;
1366 if (zh)
1367 a= zh->errString;
1368 yaz_log(log_level, "zebra_errAdd: %s",a);
1369 return a;
1370}
1371
1372ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1373{
1374 const char *p;
1375 const char *astring;
1376 char u[40];
1377 ZebraService zs;
1378
1380
1381 zs = zh->service;
1382
1383 yaz_snprintf(u, sizeof(u), "perm.%s", user ? user : "anonymous");
1384 p = res_get(zs->global_res, u);
1385 xfree(zh->user_perm);
1386 zh->user_perm = xstrdup(p ? p : "r");
1387
1388 /* Determine database access list */
1389 astring = res_get(zs->dbaccess, user ? user : "anonymous");
1390 if (astring)
1391 zh->dbaccesslist = xstrdup(astring);
1392 else
1393 zh->dbaccesslist = 0;
1394
1395 /* users that don't require a password .. */
1396 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1397 return ZEBRA_OK;
1398
1399 if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1400 return ZEBRA_OK;
1401 return ZEBRA_FAIL;
1402}
1403
1405 const char *record_type)
1406{
1407 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1408 database, record_type);
1409 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1410 return ZEBRA_FAIL;
1411 return zebra_begin_trans(zh, 1);
1412}
1413
1415{
1417 yaz_log(log_level, "zebra_admin_import_end");
1418 return zebra_end_trans(zh);
1419}
1420
1422{
1423 ZEBRA_RES res = ZEBRA_OK;
1424 zint sysno;
1425 int i;
1427 yaz_log(log_level, "zebra_admin_import_segment");
1428
1429 for (i = 0; i<segment->num_segmentRecords; i++)
1430 {
1431 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1432
1433 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1434 {
1435 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1436 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1437 {
1438 Odr_oct *oct = fragment->u.notExternallyTagged;
1439 sysno = 0;
1440
1442 zh,
1444 0, /* record Type */
1445 &sysno,
1446 0, /* match */
1447 0, /* fname */
1448 (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1449 res = ZEBRA_FAIL;
1450 }
1451 }
1452 }
1453 return res;
1454}
1455
1456int delete_w_handle(const char *info, void *handle)
1457{
1458 ZebraHandle zh = (ZebraHandle) handle;
1459 ISAM_P pos;
1460
1461 if (*info == sizeof(pos))
1462 {
1463 memcpy(&pos, info+1, sizeof(pos));
1464 isamb_unlink(zh->reg->isamb, pos);
1465 }
1466 return 0;
1467}
1468
1469int delete_w_all_handle(const char *info, void *handle)
1470{
1471 ZebraHandle zh = (ZebraHandle) handle;
1472 ISAM_P pos;
1473
1474 if (*info == sizeof(pos))
1475 {
1476 ISAMB_PP pt;
1477 memcpy(&pos, info+1, sizeof(pos));
1478 pt = isamb_pp_open(zh->reg->isamb, pos, 2);
1479 if (pt)
1480 {
1481 struct it_key key;
1482 key.mem[0] = 0;
1483 while (isamb_pp_read(pt, &key))
1484 {
1485 Record rec;
1486 rec = rec_get(zh->reg->records, key.mem[0]);
1487 rec_del(zh->reg->records, &rec);
1488 }
1489 isamb_pp_close(pt);
1490 }
1491 }
1492 return delete_w_handle(info, handle);
1493}
1494
1495static int delete_SU_handle(void *handle, int ord,
1496 const char *index_type, const char *string_index,
1498{
1499 ZebraHandle zh = (ZebraHandle) handle;
1500 char ord_buf[20];
1501 int ord_len;
1502#if 0
1503 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s cat=%d", ord,
1504 index_type, string_index, (int) cat);
1505#endif
1506 ord_len = key_SU_encode(ord, ord_buf);
1507 ord_buf[ord_len] = '\0';
1508
1509 assert(zh->reg->isamb);
1510 assert(zh->reg->records);
1511 dict_delete_subtree(zh->reg->dict, ord_buf,
1512 zh,
1513 !strcmp(string_index, "_ALLRECORDS") ?
1515 return 0;
1516}
1517
1519{
1520 ZEBRA_RES ret = ZEBRA_OK;
1521
1522 yaz_log(log_level, "zebra_drop_database %s", db);
1524
1525 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1526 return ZEBRA_FAIL;
1527 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1528 return ZEBRA_FAIL;
1529 if (zh->reg->isamb)
1530 {
1531 int db_ord;
1532 if (zebraExplain_curDatabase(zh->reg->zei, db))
1533 {
1534 zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1535 ret = ZEBRA_FAIL;
1536 }
1537 else
1538 {
1539 db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1541 0 /* handle */, 0 /* func */);
1545 }
1546 }
1547 else
1548 {
1549 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1550 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1551 "drop database only supported for isam:b");
1552 ret = ZEBRA_FAIL;
1553 }
1554 if (zebra_end_trans(zh) != ZEBRA_OK)
1555 {
1556 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1557 ret = ZEBRA_FAIL;
1558 }
1559 return ret;
1560}
1561
1563{
1564 yaz_log(log_level, "zebra_create_database %s", db);
1566 assert(db);
1567
1568 if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1569 return ZEBRA_FAIL;
1570 if (zebra_begin_trans(zh, 1))
1571 return ZEBRA_FAIL;
1572
1573 /* announce database */
1574 if (zebraExplain_newDatabase(zh->reg->zei, db, 0
1575 /* explainDatabase */))
1576 {
1577 if (zebra_end_trans(zh) != ZEBRA_OK)
1578 {
1579 yaz_log(YLOG_WARN, "zebra_end_trans failed");
1580 }
1581 zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1582 return ZEBRA_FAIL;
1583 }
1584 return zebra_end_trans(zh);
1585}
1586
1587int zebra_string_norm(ZebraHandle zh, const char *index_type,
1588 const char *input_str, int input_len,
1589 char *output_str, int output_len)
1590{
1591 WRBUF wrbuf;
1592 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1593 ASSERTZH;
1594 assert(input_str);
1595 assert(output_str);
1596 yaz_log(log_level, "zebra_string_norm ");
1597
1598 if (!zh->reg->zebra_maps)
1599 return -1;
1600 wrbuf = zebra_replace(zm, "", input_str, input_len);
1601 if (!wrbuf)
1602 return -2;
1603 if (wrbuf_len(wrbuf) >= output_len)
1604 return -3;
1605 if (wrbuf_len(wrbuf))
1606 memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1607 output_str[wrbuf_len(wrbuf)] = '\0';
1608 return wrbuf_len(wrbuf);
1609}
1610
1621static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1622{
1623 char state_fname[256];
1624 char *fname;
1625 long p = getpid();
1626 FILE *f;
1627 ASSERTZH;
1628 yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1629
1630 yaz_snprintf(state_fname, sizeof(state_fname), "state.%s.LCK", zh->reg_name);
1631 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1632 f = fopen(fname, "w");
1633 if (!f)
1634 {
1635 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s w", state_fname);
1636 exit(1);
1637 }
1638 yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1639 fprintf(f, "%c %d %ld\n", val, seqno, p);
1640 fclose(f);
1641 xfree(fname);
1642}
1643
1644static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1645{
1646 char state_fname[256];
1647 char *fname;
1648 FILE *f;
1649
1650 ASSERTZH;
1651 yaz_log(log_level, "zebra_get_state ");
1652
1653 yaz_snprintf(state_fname, sizeof(state_fname), "state.%s.LCK", zh->reg_name);
1654 fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1655 f = fopen(fname, "r");
1656 *val = 'o';
1657 *seqno = 0;
1658
1659 if (f)
1660 {
1661 if (fscanf(f, "%c %d", val, seqno) != 2)
1662 {
1663 yaz_log(YLOG_ERRNO|YLOG_WARN, "fscan fail %s",
1664 state_fname);
1665 }
1666 fclose(f);
1667 }
1668 xfree(fname);
1669}
1670
1672{
1673 return zebra_begin_trans(zh, 0);
1674}
1675
1677{
1678 return zebra_end_trans(zh);
1679}
1680
1682{
1683 const char *group = res_get(zh->res, "group");
1684 const char *v;
1685 /* FIXME - do we still use groups ?? */
1686
1687 zh->m_group = group;
1688 v = res_get_prefix(zh->res, "followLinks", group, "1");
1689 zh->m_follow_links = atoi(v);
1690
1691 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1692 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1693
1694 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1695 zh->m_store_keys = atoi(v);
1696
1697 v = res_get_prefix(zh->res, "storeData", group, "1");
1698 zh->m_store_data = atoi(v);
1699
1700 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1701 zh->m_explain_database = atoi(v);
1702
1703 v = res_get_prefix(zh->res, "openRW", group, "1");
1704 zh->m_flag_rw = atoi(v);
1705
1706 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1707 zh->m_file_verbose_limit = atoi(v);
1708}
1709
1711{
1714 if (!zh->res)
1715 {
1716 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1717 "zebra_begin_trans: no database selected");
1718 return ZEBRA_FAIL;
1719 }
1721 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1722
1723 if (zh->user_perm)
1724 {
1725 if (rw && !strchr(zh->user_perm, 'w'))
1726 {
1728 zh,
1729 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1730 0);
1731 return ZEBRA_FAIL;
1732 }
1733 }
1734
1735 assert(zh->res);
1736 if (rw)
1737 {
1738 int seqno = 0;
1739 char val = '?';
1740 const char *rval = 0;
1741
1742 (zh->trans_no++);
1743 if (zh->trans_w_no)
1744 {
1746 return 0;
1747 }
1748 if (zh->trans_no != 1)
1749 {
1750 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1751 "zebra_begin_trans: no write trans within read");
1752 return ZEBRA_FAIL;
1753 }
1754 if (zh->reg)
1755 {
1758 }
1759 zh->trans_w_no = zh->trans_no;
1760
1761 zh->records_inserted = 0;
1762 zh->records_updated = 0;
1763 zh->records_deleted = 0;
1764 zh->records_processed = 0;
1765 zh->records_skipped = 0;
1766
1767#if HAVE_SYS_TIMES_H
1768 times(&zh->tms1);
1769#endif
1770 /* lock */
1771 if (zh->shadow_enable)
1772 rval = res_get(zh->res, "shadow");
1773
1774 if (rval)
1775 {
1778 }
1779 else
1780 {
1783 }
1784 zebra_get_state(zh, &val, &seqno);
1785 if (val != 'o')
1786 {
1787 /* either we didn't finish commit or shadow is dirty */
1788 if (!rval)
1789 {
1790 yaz_log(YLOG_WARN, "previous transaction did not finish "
1791 "(shadow disabled)");
1792 }
1795 if (zebra_commit(zh))
1796 {
1797 zh->trans_no--;
1798 zh->trans_w_no = 0;
1799 return ZEBRA_FAIL;
1800 }
1801 if (rval)
1802 {
1805 }
1806 else
1807 {
1810 }
1811 }
1812
1813 zebra_set_state(zh, 'd', seqno);
1814
1815 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1816 1, rval ? 1 : 0, zh->res,
1817 zh->path_reg);
1818 if (!zh->reg)
1819 {
1822
1823 zh->trans_no--;
1824 zh->trans_w_no = 0;
1825
1826 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1827 "zebra_begin_trans: cannot open register");
1828 yaz_log(YLOG_FATAL, "%s", zh->errString);
1829 return ZEBRA_FAIL;
1830 }
1831 zh->reg->seqno = seqno;
1833 }
1834 else
1835 {
1836 int dirty = 0;
1837 char val;
1838 int seqno;
1839
1840 (zh->trans_no)++;
1841
1842 if (zh->trans_no != 1)
1843 {
1844 return zebra_flush_reg(zh);
1845 }
1846#if HAVE_SYS_TIMES_H
1847 times(&zh->tms1);
1848#endif
1849 if (!zh->res)
1850 {
1851 (zh->trans_no)--;
1852 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1853 return ZEBRA_FAIL;
1854 }
1855 if (!zh->lock_normal || !zh->lock_shadow)
1856 {
1857 (zh->trans_no)--;
1858 zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1859 return ZEBRA_FAIL;
1860 }
1861 zebra_get_state(zh, &val, &seqno);
1862 if (val == 'd')
1863 val = 'o';
1864
1865 if (!zh->reg)
1866 dirty = 1;
1867 else if (seqno != zh->reg->seqno)
1868 {
1869 yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1870 seqno, zh->reg->seqno);
1871 dirty = 1;
1872 }
1873 else if (zh->reg->last_val != val)
1874 {
1875 yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1876 val, zh->reg->last_val);
1877 dirty = 1;
1878 }
1879 if (!dirty)
1880 return ZEBRA_OK;
1881
1882 if (val == 'c')
1884 else
1886
1887 if (zh->reg)
1888 {
1891 }
1892 zh->reg = zebra_register_open(zh->service, zh->reg_name,
1893 0, val == 'c' ? 1 : 0,
1894 zh->res, zh->path_reg);
1895 if (!zh->reg)
1896 {
1899 zh->trans_no--;
1900 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1901 return ZEBRA_FAIL;
1902 }
1903 zh->reg->last_val = val;
1904 zh->reg->seqno = seqno;
1905 }
1907 return ZEBRA_OK;
1908}
1909
1911{
1913
1914 yaz_log(log_level, "zebra_end_trans");
1916 return zebra_end_transaction(zh, &dummy);
1917}
1918
1920{
1921 char val;
1922 int seqno;
1923 const char *rval;
1924
1926
1927 assert(status);
1928 yaz_log(log_level, "zebra_end_transaction");
1929
1930 status->processed = 0;
1931 status->inserted = 0;
1932 status->updated = 0;
1933 status->deleted = 0;
1934 status->utime = 0;
1935 status->stime = 0;
1936
1937 if (!zh->res || !zh->reg)
1938 {
1939 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1940 "zebra_end_trans: no open transaction");
1941 return ZEBRA_FAIL;
1942 }
1943 if (zh->trans_no != zh->trans_w_no)
1944 {
1945 zh->trans_no--;
1946 if (zh->trans_no != 0)
1947 return ZEBRA_OK;
1948
1949 /* release read lock */
1950
1953 }
1954 else
1955 { /* release write lock */
1956 zh->trans_no--;
1957 zh->trans_w_no = 0;
1958
1959 yaz_log(YLOG_DEBUG, "zebra_end_trans");
1960 rval = res_get(zh->res, "shadow");
1961
1963
1964 zebra_flush_reg(zh);
1965
1967
1969 zh->reg = 0;
1970
1971 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1975
1976 status->processed = zh->records_processed;
1977 status->inserted = zh->records_inserted;
1978 status->updated = zh->records_updated;
1979 status->deleted = zh->records_deleted;
1980
1981 zebra_get_state(zh, &val, &seqno);
1982 if (val != 'd')
1983 {
1984 BFiles bfs = bfs_create(rval, zh->path_reg);
1985 bf_commitClean(bfs, rval);
1986 bfs_destroy(bfs);
1987 }
1988 if (!rval)
1989 seqno++;
1990 zebra_set_state(zh, 'o', seqno);
1993
1994 }
1995#if HAVE_SYS_TIMES_H
1996 times(&zh->tms2);
1997 yaz_log(log_level, "user/system: %ld/%ld",
1998 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1999 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
2000
2001 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
2002 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
2003#endif
2004 return ZEBRA_OK;
2005}
2006
2008{
2009 return zebra_repository_index(zh, path, action_update);
2010}
2011
2013{
2014 return zebra_repository_index(zh, path, action_delete);
2015}
2016
2018 enum zebra_recctrl_action_t action)
2019{
2020 ASSERTZH;
2021 assert(path);
2022
2023 if (action == action_update)
2024 yaz_log(log_level, "updating %s", path);
2025 else if (action == action_delete)
2026 yaz_log(log_level, "deleting %s", path);
2027 else if (action == action_a_delete)
2028 yaz_log(log_level, "attempt deleting %s", path);
2029 else
2030 yaz_log(log_level, "update action=%d", (int) action);
2031
2032 if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
2033 return zebra_update_file_match(zh, path);
2034 else
2035 return zebra_update_from_path(zh, path, action);
2036}
2037
2039{
2040 ASSERTZH;
2041 assert(path);
2042 yaz_log(log_level, "zebra_repository_show");
2043 repositoryShow(zh, path);
2044 return ZEBRA_OK;
2045}
2046
2047static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2048{
2049 int seqno;
2050 char val;
2051 const char *rval;
2052 BFiles bfs;
2053 ZEBRA_RES res = ZEBRA_OK;
2054
2055 ASSERTZH;
2056
2057 yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
2059 if (!zh->res)
2060 {
2061 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2062 return ZEBRA_FAIL;
2063 }
2064 rval = res_get(zh->res, "shadow");
2065 if (!rval)
2066 {
2067 yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2068 return ZEBRA_OK;
2069 }
2070
2073
2074 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2075 if (!bfs)
2076 {
2079 return ZEBRA_FAIL;
2080 }
2081 zebra_get_state(zh, &val, &seqno);
2082
2083 if (val == 'd')
2084 {
2085 /* shadow area is dirty and so we must throw it away */
2086 yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2087 clean_only = 1;
2088 }
2089 else if (val == 'c')
2090 {
2091 /* commit has started. We can not remove it anymore */
2092 clean_only = 0;
2093 }
2094
2095 if (rval && *rval)
2096 bf_cache(bfs, rval);
2097 if (bf_commitExists(bfs))
2098 {
2099 if (clean_only)
2100 zebra_set_state(zh, 'd', seqno);
2101 else
2102 {
2103 zebra_set_state(zh, 'c', seqno);
2104
2105 yaz_log(log_level, "commit start");
2106 if (bf_commitExec(bfs))
2107 res = ZEBRA_FAIL;
2108 }
2109 if (res == ZEBRA_OK)
2110 {
2111 seqno++;
2112 zebra_set_state(zh, 'o', seqno);
2113
2116
2118 bf_commitClean(bfs, rval);
2120 }
2121 else
2122 {
2125 yaz_log(YLOG_WARN, "zebra_commit: failed");
2126 }
2127 }
2128 else
2129 {
2132 yaz_log(log_level, "nothing to commit");
2133 }
2134 bfs_destroy(bfs);
2135
2136 return res;
2137}
2138
2140{
2141 yaz_log(log_level, "zebra_clean");
2143 return zebra_commit_ex(zh, 1);
2144}
2145
2147{
2148 yaz_log(log_level, "zebra_commit");
2150 return zebra_commit_ex(zh, 0);
2151}
2152
2153
2155{
2156 const char *rval;
2157 BFiles bfs = 0;
2158
2159 yaz_log(log_level, "zebra_init");
2160
2162
2164 if (!zh->res)
2165 {
2166 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2167 "cannot select default database");
2168 return ZEBRA_FAIL;
2169 }
2170 rval = res_get(zh->res, "shadow");
2171
2172 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2173 if (!bfs)
2174 {
2175 zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2176 return ZEBRA_FAIL;
2177 }
2178 if (rval && *rval)
2179 bf_cache(bfs, rval);
2180
2181 bf_reset(bfs);
2182 bfs_destroy(bfs);
2183 zebra_set_state(zh, 'o', 0);
2184 return ZEBRA_OK;
2185}
2186
2188{
2189 BFiles bfs;
2190
2191 yaz_log(log_level, "zebra_compact");
2193 if (!zh->res)
2194 {
2195 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2196 return ZEBRA_FAIL;
2197 }
2198 bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2199 inv_compact(bfs);
2200 bfs_destroy(bfs);
2201 return ZEBRA_OK;
2202}
2203
2204#define ZEBRA_CHECK_DICT 1
2205#define ZEBRA_CHECK_ISAM 2
2206
2208 zint *no_keys, int message_limit,
2209 unsigned flags,
2210 zint *no_long_dict_entries,
2211 zint *no_failed_dict_lookups,
2212 zint *no_invalid_keys,
2213 zint *no_invalid_dict_infos,
2214 zint *no_invalid_isam_entries)
2215{
2216 ZEBRA_RES res = ZEBRA_OK;
2219 rec->size[recInfo_delKeys], 0);
2220
2221 *no_keys = 0;
2222 if (!zebra_rec_keys_rewind(keys))
2223 {
2224 ;
2225 }
2226 else
2227 {
2228 size_t slen;
2229 const char *str;
2230 struct it_key key_in;
2231 NMEM nmem = nmem_create();
2232
2233 while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
2234 {
2235 int do_fail = 0;
2236 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
2237 char ord_buf[IT_MAX_WORD+20];
2238 int ord_len = key_SU_encode(ord, ord_buf);
2239 char *info = 0;
2240
2241 (*no_keys)++;
2242
2243 if (key_in.len < 2 || key_in.len > IT_KEY_LEVEL_MAX)
2244 {
2245 res = ZEBRA_FAIL;
2246 (*no_invalid_keys)++;
2247 if (*no_invalid_keys <= message_limit)
2248 {
2249 do_fail = 1;
2250 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2251 ": unexpected key length %d",
2252 rec->sysno, key_in.len);
2253 }
2254 }
2255 if (ord_len + slen >= sizeof(ord_buf)-1)
2256 {
2257 res = ZEBRA_FAIL;
2258 (*no_long_dict_entries)++;
2259 if (*no_long_dict_entries <= message_limit)
2260 {
2261 do_fail = 1;
2262 /* so bad it can not fit into our ord_buf */
2263 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2264 ": long dictionary entry %d + %d",
2265 rec->sysno, ord_len, (int) slen);
2266 }
2267 continue;
2268 }
2269 memcpy(ord_buf + ord_len, str, slen);
2270 ord_buf[ord_len + slen] = '\0';
2271 if (slen > IT_MAX_WORD || ord_len > 4)
2272 {
2273 res = ZEBRA_FAIL;
2274 (*no_long_dict_entries)++;
2275 if (*no_long_dict_entries <= message_limit)
2276 {
2277 do_fail = 1;
2278 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2279 ": long dictionary entry %d + %d",
2280 rec->sysno, (int) ord_len, (int) slen);
2281 }
2282 }
2283 if ((flags & ZEBRA_CHECK_DICT) == 0)
2284 continue;
2285 info = dict_lookup(zh->reg->dict, ord_buf);
2286 if (!info)
2287 {
2288 res = ZEBRA_FAIL;
2289 (*no_failed_dict_lookups)++;
2290 if (*no_failed_dict_lookups <= message_limit)
2291 {
2292 do_fail = 1;
2293 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2294 ": term do not exist in dictionary", rec->sysno);
2295 }
2296 }
2297 else if (flags & ZEBRA_CHECK_ISAM)
2298 {
2299 ISAM_P pos;
2300
2301 if (*info != sizeof(pos))
2302 {
2303 res = ZEBRA_FAIL;
2304 (*no_invalid_dict_infos)++;
2305 if (*no_invalid_dict_infos <= message_limit)
2306 {
2307 do_fail = 1;
2308 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2309 ": long dictionary entry %d + %d",
2310 rec->sysno, (int) ord_len, (int) slen);
2311 }
2312 }
2313 else
2314 {
2315 int scope = 1;
2316 memcpy(&pos, info+1, sizeof(pos));
2317 if (zh->reg->isamb)
2318 {
2319 ISAMB_PP ispt = isamb_pp_open(zh->reg->isamb, pos,
2320 scope);
2321 if (!ispt)
2322 {
2323 res = ZEBRA_FAIL;
2324 (*no_invalid_isam_entries)++;
2325 if (*no_invalid_isam_entries <= message_limit)
2326 {
2327 do_fail = 1;
2328 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2329 ": isamb_pp_open entry " ZINT_FORMAT
2330 " not found",
2331 rec->sysno, pos);
2332 }
2333 }
2334 else if (zh->m_staticrank)
2335 {
2336 isamb_pp_close(ispt);
2337 }
2338 else
2339 {
2340 struct it_key until_key;
2341 struct it_key isam_key;
2342 int r;
2343 int i = 0;
2344
2345 until_key.len = key_in.len - 1;
2346 for (i = 0; i < until_key.len; i++)
2347 until_key.mem[i] = key_in.mem[i+1];
2348
2349 if (until_key.mem[0] == 0)
2350 until_key.mem[0] = rec->sysno;
2351 r = isamb_pp_forward(ispt, &isam_key, &until_key);
2352 if (r != 1)
2353 {
2354 res = ZEBRA_FAIL;
2355 (*no_invalid_isam_entries)++;
2356 if (*no_invalid_isam_entries <= message_limit)
2357 {
2358 do_fail = 1;
2359 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2360 ": isamb_pp_forward " ZINT_FORMAT
2361 " returned no entry",
2362 rec->sysno, pos);
2363 }
2364 }
2365 else
2366 {
2367 int cmp = key_compare(&until_key, &isam_key);
2368 if (cmp != 0)
2369 {
2370 res = ZEBRA_FAIL;
2371 (*no_invalid_isam_entries)++;
2372 if (*no_invalid_isam_entries
2373 <= message_limit)
2374 {
2375 do_fail = 1;
2376 yaz_log(YLOG_WARN, "Record "
2378 ": isamb_pp_forward "
2380 " returned different entry",
2381 rec->sysno, pos);
2382
2383 key_logdump_txt(YLOG_LOG,
2384 &until_key,
2385 "until");
2386
2387 key_logdump_txt(YLOG_LOG,
2388 &isam_key,
2389 "isam");
2390
2391 }
2392 }
2393 }
2394 isamb_pp_close(ispt);
2395 }
2396
2397 }
2398 }
2399 }
2400 if (do_fail)
2401 {
2402 zebra_it_key_str_dump(zh, &key_in, str,
2403 slen, nmem, YLOG_LOG);
2404 nmem_reset(nmem);
2405 }
2406 }
2407 nmem_destroy(nmem);
2408 }
2410 return res;
2411}
2412
2414{
2415 ZEBRA_RES res = ZEBRA_FAIL;
2416 unsigned flags = 0;
2417
2418 if (!spec || *spec == '\0'
2419 || !strcmp(spec, "dict") || !strcmp(spec, "default"))
2420 flags = ZEBRA_CHECK_DICT;
2421 else if (!strcmp(spec, "isam") || !strcmp(spec, "full"))
2423 else if (!strcmp(spec, "quick"))
2424 flags = 0;
2425 else
2426 {
2427 yaz_log(YLOG_WARN, "Unknown check spec: %s", spec);
2428 return ZEBRA_FAIL;
2429 }
2430
2431 yaz_log(YLOG_LOG, "zebra_register_check begin flags=%u", flags);
2432 if (zebra_begin_read(zh) == ZEBRA_OK)
2433 {
2434 zint no_records_total = 0;
2435 zint no_records_fail = 0;
2436 zint total_keys = 0;
2437 int message_limit = zh->m_file_verbose_limit;
2438
2439 if (zh->reg)
2440 {
2441 Record rec = rec_get_root(zh->reg->records);
2442
2443 zint no_long_dict_entries = 0;
2444 zint no_failed_dict_lookups = 0;
2445 zint no_invalid_keys = 0;
2446 zint no_invalid_dict_infos = 0;
2447 zint no_invalid_isam_entries = 0;
2448
2449 res = ZEBRA_OK;
2450 while (rec)
2451 {
2452 Record r1;
2453 zint no_keys;
2454
2455 if (zebra_record_check(zh, rec, &no_keys, message_limit,
2456 flags,
2457 &no_long_dict_entries,
2458 &no_failed_dict_lookups,
2459 &no_invalid_keys,
2460 &no_invalid_dict_infos,
2461 &no_invalid_isam_entries
2462 )
2463 != ZEBRA_OK)
2464 {
2465 res = ZEBRA_FAIL;
2466 no_records_fail++;
2467 }
2468
2469 r1 = rec_get_next(zh->reg->records, rec);
2470 rec_free(&rec);
2471 rec = r1;
2472 no_records_total++;
2473 total_keys += no_keys;
2474 }
2475 yaz_log(YLOG_LOG, "records total: " ZINT_FORMAT,
2476 no_records_total);
2477 yaz_log(YLOG_LOG, "records fail: " ZINT_FORMAT,
2478 no_records_fail);
2479 yaz_log(YLOG_LOG, "total keys: " ZINT_FORMAT,
2480 total_keys);
2481 yaz_log(YLOG_LOG, "long dict entries: " ZINT_FORMAT,
2482 no_long_dict_entries);
2483 if (flags & ZEBRA_CHECK_DICT)
2484 {
2485 yaz_log(YLOG_LOG, "failed dict lookups: " ZINT_FORMAT,
2486 no_failed_dict_lookups);
2487 yaz_log(YLOG_LOG, "invalid dict infos: " ZINT_FORMAT,
2488 no_invalid_dict_infos);
2489 }
2490 if (flags & ZEBRA_CHECK_ISAM)
2491 yaz_log(YLOG_LOG, "invalid isam entries: " ZINT_FORMAT,
2492 no_invalid_isam_entries);
2493 }
2494 zebra_end_read(zh);
2495 }
2496 yaz_log(YLOG_LOG, "zebra_register_check end ret=%d", res);
2497 return res;
2498}
2499
2500void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2501{
2502 yaz_log(log_level, "zebra_result");
2503 if (zh)
2504 {
2505 *code = zh->errCode;
2506 *addinfo = zh->errString;
2507 }
2508 else
2509 {
2510 *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2511 *addinfo ="ZebraHandle is NULL";
2512 }
2513}
2514
2516{
2517 ASSERTZH;
2518 yaz_log(log_level, "zebra_shadow_enable");
2519 zh->shadow_enable = value;
2520}
2521
2523{
2524 yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2526 assert(encoding);
2527
2528 if (zh->iconv_to_utf8 != 0)
2529 yaz_iconv_close(zh->iconv_to_utf8);
2530 if (zh->iconv_from_utf8 != 0)
2531 yaz_iconv_close(zh->iconv_from_utf8);
2532
2533 zh->iconv_to_utf8 =
2534 yaz_iconv_open("UTF-8", encoding);
2535 if (zh->iconv_to_utf8 == 0)
2536 yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2537 zh->iconv_from_utf8 =
2538 yaz_iconv_open(encoding, "UTF-8");
2539 if (zh->iconv_to_utf8 == 0)
2540 yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2541
2542 return ZEBRA_OK;
2543}
2544
2546{
2547 yaz_log(log_level, "zebra_record_encoding");
2549 xfree(zh->record_encoding);
2550 zh->record_encoding = 0;
2551 if (encoding)
2552 zh->record_encoding = xstrdup(encoding);
2553 return ZEBRA_OK;
2554}
2555
2556void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2557{
2558 assert(name);
2559 assert(value);
2560 yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2561 ASSERTZH;
2562 res_set(zh->res, name, value);
2563}
2564
2566 const char *name, const char *defaultvalue)
2567{
2568 const char *v;
2569 ASSERTZH;
2570 assert(name);
2571 v = res_get_def(zh->res, name,(char *)defaultvalue);
2572 yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2573 return v;
2574}
2575
2576/* moved from zebra_api_ext.c by pop */
2577/* FIXME: Should this really be public??? -Heikki */
2578
2580{
2581 yaz_log(log_level, "zebra_trans_no");
2582 ASSERTZH;
2583 return zh->trans_no;
2584}
2585
2587{
2588 yaz_log(log_level, "zebra_get_shadow_enable");
2589 ASSERTZH;
2590 return zh->shadow_enable;
2591}
2592
2594{
2595 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2596 ASSERTZH;
2597 zh->shadow_enable = value;
2598}
2599
2601 const char *buf, int buf_size)
2602{
2604 0 /* record type */,
2605 0 /* sysno */ ,
2606 0 /* match */,
2607 0 /* fname */,
2608 buf, buf_size);
2609}
2610
2612 enum zebra_recctrl_action_t action,
2613 const char *recordType,
2614 zint *sysno, const char *match,
2615 const char *fname,
2616 const char *buf, int buf_size)
2617{
2618 ZEBRA_RES res;
2619
2621
2622 assert(buf);
2623
2624 yaz_log(log_level, "zebra_update_record");
2625 if (sysno)
2626 yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2627
2628 if (buf_size < 1)
2629 buf_size = strlen(buf);
2630
2631 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2632 return ZEBRA_FAIL;
2633 res = zebra_buffer_extract_record(zh, buf, buf_size,
2634 action,
2635 recordType,
2636 sysno,
2637 match,
2638 fname);
2639 if (zebra_end_trans(zh) != ZEBRA_OK)
2640 {
2641 yaz_log(YLOG_WARN, "zebra_end_trans failed");
2642 res = ZEBRA_FAIL;
2643 }
2644 return res;
2645}
2646
2647/* ---------------------------------------------------------------------------
2648 Searching
2649*/
2650
2651ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2652 const char *setname, zint *hits)
2653{
2654 zint lhits = 0;
2655 ZEBRA_RES res = ZEBRA_OK;
2656 Z_RPNQuery *query;
2657 ODR odr;
2658
2659
2661
2662 odr = odr_createmem(ODR_ENCODE);
2663
2664 assert(pqf_query);
2665 assert(setname);
2666
2667 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2668
2669 query = p_query_rpn(odr, pqf_query);
2670
2671 if (!query)
2672 {
2673 yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2674 zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2675 res = ZEBRA_FAIL;
2676 }
2677 else
2678 res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2679
2680 odr_destroy(odr);
2681
2682 yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2683
2684 if (hits)
2685 *hits = lhits;
2686
2687 return res;
2688}
2689
2690/* ---------------------------------------------------------------------------
2691 Sort - a simplified interface, with optional read locks.
2692*/
2694 const char *sort_spec,
2695 const char *output_setname,
2696 const char **input_setnames)
2697{
2698 int num_input_setnames = 0;
2699 int sort_status = 0;
2700 Z_SortKeySpecList *sort_sequence;
2701
2703 assert(stream);
2704 assert(sort_spec);
2705 assert(output_setname);
2706 assert(input_setnames);
2707 sort_sequence = yaz_sort_spec(stream, sort_spec);
2708 yaz_log(log_level, "sort (FIXME) ");
2709 if (!sort_sequence)
2710 {
2711 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2712 zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2713 return -1;
2714 }
2715
2716 /* we can do this, since the perl typemap code for char** will
2717 put a NULL at the end of list */
2718 while (input_setnames[num_input_setnames]) num_input_setnames++;
2719
2720 if (zebra_begin_read(zh))
2721 return -1;
2722
2723 resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2724 output_setname, sort_sequence, &sort_status);
2725
2726 zebra_end_read(zh);
2727 return sort_status;
2728}
2729
2730/* ---------------------------------------------------------------------------
2731 Get BFS for Zebra system (to make alternative storage methods)
2732*/
2734{
2735 if (zh && zh->reg)
2736 return zh->reg->bfs;
2737 return 0;
2738}
2739
2740
2741/* ---------------------------------------------------------------------------
2742 Set limit for search/scan
2743*/
2744ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2745{
2748 zh->m_limit = zebra_limit_create(complement_flag, ids);
2749 return ZEBRA_OK;
2750}
2751
2752/*
2753 Set Error code + addinfo
2754*/
2755void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2756{
2757 if (!zh)
2758 return;
2759 zh->errCode = code;
2760 nmem_reset(zh->nmem_error);
2761 zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2762}
2763
2765{
2766 char vstr[60];
2767 yaz_snprintf(vstr, sizeof(vstr), ZINT_FORMAT, i);
2768
2769 zh->errCode = code;
2770 nmem_reset(zh->nmem_error);
2771 zh->errString = nmem_strdup(zh->nmem_error, vstr);
2772}
2773
2774void zebra_lock_prefix(Res res, char *path)
2775{
2776 const char *lock_dir = res_get_def(res, "lockDir", "");
2777
2778 strcpy(path, lock_dir);
2779 if (*path && path[strlen(path)-1] != '/')
2780 strcat(path, "/");
2781}
2782
2783/*
2784 * Local variables:
2785 * c-basic-offset: 4
2786 * c-file-style: "Stroustrup"
2787 * indent-tabs-mode: nil
2788 * End:
2789 * vim: shiftwidth=4 tabstop=8 expandtab
2790 */
2791
Zebra API.
struct zebra_session * ZebraHandle
a Zebra Handle - (session)
Definition api.h:71
void zebra_meta_records_destroy(ZebraHandle zh, ZebraMetaRecord *records, int num)
Definition zsets.c:547
ZebraMetaRecord * zebra_meta_records_create(ZebraHandle zh, const char *name, int num, zint *positions)
Definition zsets.c:418
ZEBRA_RES bf_cache(BFiles bfs, const char *spec)
enables or disables shadow for block files
Definition bfile.c:95
int bf_commitExec(BFiles bfs) ZEBRA_GCC_ATTR((warn_unused_result))
Executes commit operation.
Definition bfile.c:277
void bf_reset(BFiles bfs)
Removes register and shadow completely.
Definition bfile.c:268
void bf_commitClean(BFiles bfs, const char *spec)
Cleans shadow files (remove them)
Definition bfile.c:321
BFiles bfs_create(const char *spec, const char *base)
creates a Block files collection
Definition bfile.c:56
void bfs_destroy(BFiles bfiles)
destroys a block files handle
Definition bfile.c:73
int bf_commitExists(BFiles bfs)
Check if there is content in shadow area (to be committed).
Definition bfile.c:255
int zebra_check_res(Res res)
Definition check_res.c:30
void inv_compact(BFiles bfs)
Definition compact.c:31
void data1_set_tabroot(data1_handle dp, const char *p)
Definition d1_handle.c:129
data1_handle data1_create(void)
Definition d1_handle.c:48
void data1_set_tabpath(data1_handle dh, const char *path)
Definition d1_handle.c:121
void data1_destroy(data1_handle dh)
Definition d1_handle.c:81
Dict dict_open(BFiles bfs, const char *name, int cache, int rw, int compact_flag, int page_size)
open dictionary
Definition open.c:50
int dict_delete_subtree(Dict dict, const char *p, void *client, int(*f)(const char *info, void *client))
delete items with a given prefix from dictionary
Definition delete.c:266
char * dict_lookup(Dict dict, const char *p)
lookup item in dictionary
Definition lookup.c:100
int dict_close(Dict dict)
closes dictionary
Definition close.c:32
void zebra_it_key_str_dump(ZebraHandle zh, struct it_key *key, const char *str, size_t slen, NMEM nmem, int level)
Definition extract.c:1299
ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
Definition extract.c:1224
ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh, const char *buf, size_t buf_size, enum zebra_recctrl_action_t action, const char *recordType, zint *sysno, const char *match_criteria, const char *fname)
Definition extract.c:725
ZebraLockHandle zebra_lock_create(const char *dir, const char *file)
Definition flock.c:106
void zebra_flock_init(void)
Definition flock.c:380
char * zebra_mk_fname(const char *dir, const char *name)
Definition flock.c:84
int zebra_lock_r(ZebraLockHandle h)
Definition flock.c:277
int zebra_lock_w(ZebraLockHandle h)
Definition flock.c:240
void zebra_lock_destroy(ZebraLockHandle h)
Definition flock.c:177
int zebra_unlock(ZebraLockHandle h)
Definition flock.c:313
int zebra_record_fetch(ZebraHandle zh, const char *setname, zint sysno, int score, ODR stream, const Odr_oid *input_format, Z_RecordComposition *comp, const Odr_oid **output_format, char **rec_bufp, int *rec_lenp, char **basenamep, WRBUF addinfo_w)
Definition retrieve.c:1197
void resultSetDestroy(ZebraHandle zh, int num_names, char **names, int *statuses)
Definition zsets.c:344
ZEBRA_RES zebra_remove_file_match(ZebraHandle zh)
ZEBRA_RES zebra_update_file_match(ZebraHandle zh, const char *path)
ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path, enum zebra_recctrl_action_t action)
#define FNAME_ISAMS
Definition index.h:101
ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, const Odr_oid *attributeset, int num_bases, char **basenames, int *position, int *num_entries, ZebraScanEntry **list, int *is_partial, const char *set_name)
Definition rpnscan.c:470
#define GMATCH_DICT
Definition index.h:104
#define ISAM_DEFAULT
Definition index.h:53
ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem, int num_input_setnames, const char **input_setnames, const char *output_setname, Z_SortKeySpecList *sort_sequence, int *sort_status)
Definition zsets.c:826
#define FNAME_ISAMC
Definition index.h:100
void zebra_index_merge(ZebraHandle zh)
Definition kinput.c:709
ISAMS_M * key_isams_m(Res res, ISAMS_M *me)
ZEBRA_RES resultSetAddRPN(ZebraHandle zh, NMEM m, Z_RPNQuery *rpn, int num_bases, char **basenames, const char *setname, zint *hits, int *estimated_hit_count)
Definition zsets.c:148
#define FNAME_DICT
Definition index.h:98
void resultSetInvalidate(ZebraHandle zh)
Definition zsets.c:322
struct zebra_limit * zebra_limit_create(int exclude_flag, zint *ids)
Definition limit.c:46
void zebra_limit_destroy(struct zebra_limit *zl)
Definition limit.c:37
void repositoryShow(ZebraHandle zh, const char *path)
Definition update_path.c:77
ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname, zint sysno, zebra_snippets *snippets)
Definition zsets.c:1340
ISAMC_M * key_isamc_m(Res res, ISAMC_M *me)
ISAMB_PP isamb_pp_open(ISAMB isamb, ISAM_P pos, int scope)
Definition isamb.c:1387
ISAMB isamb_open(BFiles bfs, const char *name, int writeflag, ISAMC_M *method, int cache)
Definition isamb.c:351
void isamb_close(ISAMB isamb)
Definition isamb.c:455
int isamb_pp_forward(ISAMB_PP pp, void *buf, const void *untilbuf)
Definition isamb.c:1525
void isamb_pp_close(ISAMB_PP pp)
Definition isamb.c:1429
int isamb_unlink(ISAMB b, ISAM_P pos)
Definition isamb.c:1225
int isamb_pp_read(ISAMB_PP pp, void *buf)
Definition isamb.c:1503
ISAMC isamc_open(BFiles bfs, const char *name, int writeflag, ISAMC_M *method)
Definition isamc.c:77
int isamc_close(ISAMC is)
Definition isamc.c:187
zint ISAM_P
Definition isamc.h:28
int isams_close(ISAMS is)
Definition isams.c:99
ISAMS isams_open(BFiles bfs, const char *name, int writeflag, ISAMS_M *method)
Definition isams.c:76
int key_compare(const void *p1, const void *p2)
Definition it_key.c:74
#define IT_KEY_LEVEL_MAX
Definition it_key.h:29
void key_logdump_txt(int logmask, const void *p, const char *txt)
Definition it_key.c:38
#define IT_MAX_WORD
Definition it_key.h:27
int key_SU_encode(int ch, char *out)
Definition su_codec.c:31
void key_block_flush(zebra_key_block_t p, int is_final)
Definition key_block.c:370
void key_block_destroy(zebra_key_block_t *pp)
Definition key_block.c:226
int dict_delete_subtree_ord(Dict d, int ord, void *client, int(*f)(const char *info, void *client))
Definition orddict.c:67
void passwd_db_close(Passwd_db db)
Definition passwddb.c:106
Passwd_db passwd_db_open(void)
Definition passwddb.c:52
int passwd_db_auth(Passwd_db db, const char *user, const char *pass)
Definition passwddb.c:128
int passwd_db_file_plain(Passwd_db db, const char *fname)
Definition passwddb.c:181
int passwd_db_file_crypt(Passwd_db db, const char *fname)
Definition passwddb.c:172
void zebraRankInstall(struct zebra_register *reg, struct rank_control *ctrl)
Definition zsets.c:1207
void zebraRankDestroy(struct zebra_register *reg)
Definition zsets.c:1218
struct rank_control * rank_static_class
Definition rankstatic.c:185
struct rank_control * rank_similarity_class
struct rank_control * rank_2_class
Definition rank1.c:266
struct rank_control * rank_1_class
Definition rank1.c:254
void recTypeClass_load_modules(RecTypeClass *rts, NMEM nmem, const char *module_path)
Definition recctrl.c:177
void recTypeClass_destroy(RecTypeClass rtc)
Definition recctrl.c:223
zebra_recctrl_action_t
Definition recctrl.h:87
@ action_a_delete
Definition recctrl.h:97
@ action_delete
Definition recctrl.h:93
@ action_update
Definition recctrl.h:95
RecTypeClass recTypeClass_create(Res res, NMEM nmem)
Definition recctrl.c:58
void recTypes_destroy(RecTypes recTypes)
Definition recctrl.c:253
void recTypeClass_info(RecTypeClass rtc, void *cd, void(*cb)(void *cd, const char *s))
Definition recctrl.c:216
RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
Definition recctrl.c:234
ZEBRA_RES rec_close(Records *p)
Definition records.c:721
Records rec_open(BFiles bfs, int rw, int compression_method)
opens records system
Definition records.c:309
Record rec_get(Records p, zint sysno)
gets record - with given system number
Definition records.c:928
#define REC_COMPRESS_BZIP2
BZIP2 compression (slow and requires big chunks)
Definition recindex.h:113
@ recInfo_delKeys
Definition recindex.h:121
#define REC_COMPRESS_ZLIB
zlib compression (faster and works off small chunks)
Definition recindex.h:115
void rec_free(Record *recpp)
frees record (from memory)
Definition records.c:1044
Record rec_get_next(Records p, Record rec)
gets next record - with given records
Definition records.c:943
#define REC_COMPRESS_NONE
No compression ("none")
Definition recindex.h:111
ZEBRA_RES rec_del(Records p, Record *recpp)
marks record for deletion (on file storage)
Definition records.c:1001
int rec_check_compression_method(int compression_method)
check whether a compression method is supported
Definition records.c:287
Record rec_get_root(Records p)
gets root record
Definition records.c:938
int zebra_rec_keys_read(zebra_rec_keys_t keys, const char **str, size_t *slen, struct it_key *key)
Definition reckeys.c:259
int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
Definition reckeys.c:240
zebra_rec_keys_t zebra_rec_keys_open(void)
Definition reckeys.c:88
void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz, int copy_buf)
Definition reckeys.c:109
void zebra_rec_keys_close(zebra_rec_keys_t p)
Definition reckeys.c:143
void res_set(Res r, const char *name, const char *value)
Definition res.c:338
void res_close(Res r)
Definition res.c:261
const char * res_get_prefix(Res r, const char *name, const char *prefix, const char *def)
Definition res.c:272
const char * res_get_def(Res r, const char *name, const char *def)
Definition res.c:313
ZEBRA_RES res_read_file(Res r, const char *fname)
Definition res.c:146
Res res_open(Res res_def, Res over_res)
Definition res.c:234
const char * res_get(Res r, const char *name)
Definition res.c:294
int res_get_match(Res r, const char *name, const char *value, const char *s)
Definition res.c:327
ZEBRA_RES res_get_int(Res r, const char *name, int *val)
Definition res.c:432
int res_trav(Res r, const char *prefix, void *p, void(*f)(void *p, const char *name, const char *value))
Definition res.c:357
void zebra_snippets_destroy(zebra_snippets *l)
Definition snippet.c:45
zebra_snippets * zebra_snippets_create(void)
Definition snippet.c:36
void zebra_sort_close(zebra_sort_index_t si)
frees sort handle
Definition sortidx.c:209
#define ZEBRA_SORT_TYPE_ISAMB
Definition sortidx.h:37
zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int sort_type)
creates sort handle
Definition sortidx.c:197
#define ZEBRA_SORT_TYPE_FLAT
Definition sortidx.h:36
#define ZEBRA_SORT_TYPE_MULTI
Definition sortidx.h:38
char * term
Definition api.h:481
zint sysno
Definition api.h:483
char * db
Definition api.h:482
const Odr_oid * format
Definition api.h:55
char * errString
Definition api.h:51
int len
Definition it_key.h:31
zint mem[IT_KEY_LEVEL_MAX]
Definition it_key.h:32
char ** basenames
Definition zebraapi.c:722
char ** new_basenames
Definition zebraapi.c:724
int new_num_max
Definition zebraapi.c:725
ZebraHandle zh
Definition zebraapi.c:719
int new_num_bases
Definition zebraapi.c:723
char * info[REC_NO_INFO]
Definition recindex.h:34
size_t size[REC_NO_INFO]
Definition recindex.h:35
zint sysno
Definition recindex.h:32
ZebraExplainInfo zei
Definition index.h:139
char * name
Definition index.h:127
zebra_rec_keys_t sortKeys
Definition index.h:151
zebra_key_block_t key_block
Definition index.h:153
RecTypes recTypes
Definition index.h:145
BFiles bfs
Definition index.h:137
data1_handle dh
Definition index.h:142
zebra_maps_t zebra_maps
Definition index.h:143
ISAMS isams
Definition index.h:129
ISAMB isamb
Definition index.h:131
ZebraRankClass rank_classes
Definition index.h:144
Records records
Definition index.h:138
ISAMC isamc
Definition index.h:130
int stop_flag
Definition index.h:148
zebra_sort_index_t sort_index
Definition index.h:134
Dict matchDict
Definition index.h:133
zebra_rec_keys_t keys
Definition index.h:150
const char * path_root
Definition index.h:164
Res global_res
Definition index.h:158
Res dbaccess
Definition index.h:163
NMEM nmem
Definition index.h:166
Zebra_mutex_cond session_lock
Definition index.h:161
int stop_flag
Definition index.h:157
struct zebra_session * sessions
Definition index.h:159
RecTypeClass record_classes
Definition index.h:165
yaz_timing_t timing
Definition index.h:167
Passwd_db passwd_db
Definition index.h:162
struct zebra_session * next
Definition index.h:172
zint records_skipped
Definition index.h:212
struct zebra_register * reg
Definition index.h:174
zint records_processed
Definition index.h:211
void * store_data_buf
Definition index.h:228
zint records_updated
Definition index.h:209
char ** basenames
Definition index.h:178
zint records_deleted
Definition index.h:210
int m_flag_rw
Definition index.h:225
char * path_reg
Definition index.h:182
char * user_perm
Definition index.h:194
zint approx_limit
Definition index.h:180
int errCode
Definition index.h:196
int m_follow_links
Definition index.h:218
int trans_w_no
Definition index.h:188
char * dbaccesslist
Definition index.h:195
int m_staticrank
Definition index.h:205
struct zebra_service * service
Definition index.h:173
int num_basenames
Definition index.h:179
const char * m_record_type
Definition index.h:221
int m_store_data
Definition index.h:222
ZebraLockHandle lock_shadow
Definition index.h:185
int trans_no
Definition index.h:187
int shadow_enable
Definition index.h:203
int m_segment_indexing
Definition index.h:206
void * break_handler_data
Definition index.h:235
ZebraLockHandle lock_normal
Definition index.h:184
int m_explain_database
Definition index.h:224
char * record_encoding
Definition index.h:213
int partial_result
Definition index.h:198
ZebraSet sets
Definition index.h:191
struct zebra_limit * m_limit
Definition index.h:232
int m_file_verbose_limit
Definition index.h:226
NMEM nmem_error
Definition index.h:230
int(* break_handler_func)(void *client_data)
Definition index.h:234
char * errString
Definition index.h:197
const char * m_group
Definition index.h:219
yaz_iconv_t iconv_from_utf8
Definition index.h:216
yaz_iconv_t iconv_to_utf8
Definition index.h:215
const char * m_record_id
Definition index.h:220
int m_store_keys
Definition index.h:223
Res session_res
Definition index.h:193
zint records_inserted
Definition index.h:208
int destroyed
Definition index.h:190
char * reg_name
Definition index.h:181
const char * scope
long zint
Zebra integer.
Definition util.h:66
#define ZEBRA_FAIL
Definition util.h:81
#define ZINT_FORMAT
Definition util.h:72
#define CAST_ZINT_TO_INT(x)
Definition util.h:96
#define ZEBRA_OK
Definition util.h:82
short ZEBRA_RES
Common return type for Zebra API.
Definition util.h:80
void zebra_get_version(char *version_str, char *sha1_str)
Returns Zebra version and SHA1 ID as generated by Git.
Definition version.c:33
int zebra_mutex_cond_init(Zebra_mutex_cond *p)
Definition zebra-lock.c:174
int zebra_mutex_cond_lock(Zebra_mutex_cond *p)
Definition zebra-lock.c:192
int zebra_mutex_cond_unlock(Zebra_mutex_cond *p)
Definition zebra-lock.c:201
int zebra_mutex_cond_destroy(Zebra_mutex_cond *p)
Definition zebra-lock.c:183
ZEBRA_RES zebra_compact(ZebraHandle zh)
Definition zebraapi.c:2187
ZEBRA_RES zebra_init(ZebraHandle zh)
Definition zebraapi.c:2154
ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query, int *position, int *num_entries, ZebraScanEntry **entries, int *is_partial, const char *setname)
performs Scan (taking PQF string)
Definition zebraapi.c:1232
ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
Definition zebraapi.c:570
int zebra_get_shadow_enable(ZebraHandle zh)
Definition zebraapi.c:2586
#define ZEBRA_CHECK_HANDLE(zh)
Definition zebraapi.c:66
ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query, const char *setname, zint *hits)
Search using PQF Query String.
Definition zebraapi.c:2651
ZEBRA_RES zebra_close(ZebraHandle zh)
Destroys Zebra session handle.
Definition zebraapi.c:657
Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw, int compact_flag, Res res)
Definition zebraapi.c:321
static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
Definition zebraapi.c:1644
ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
Definition zebraapi.c:2012
void map_basenames_func(void *vp, const char *name, const char *value)
Definition zebraapi.c:858
ZEBRA_RES zebra_admin_start(ZebraHandle zh)
Definition zebraapi.c:581
#define ZEBRA_CHECK_DICT
Definition zebraapi.c:2204
ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
Definition zebraapi.c:1421
int delete_w_all_handle(const char *info, void *handle)
Definition zebraapi.c:1469
ZEBRA_RES zebra_end_read(ZebraHandle zh)
Definition zebraapi.c:1676
ZEBRA_RES zebra_register_check(ZebraHandle zh, const char *spec)
Definition zebraapi.c:2413
ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
Deletes a database (drop)
Definition zebraapi.c:1518
ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
Definition zebraapi.c:2545
ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream, const char *setname, Z_RecordComposition *comp, const Odr_oid *input_format, int num_recs, ZebraRetrievalRecord *recs)
Retrieve records from result set (after search)
Definition zebraapi.c:1118
static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
Definition zebraapi.c:2047
void zebra_set_partial_result(ZebraHandle zh)
Definition zebraapi.c:1064
ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
Creates a Zebra service with resources.
Definition zebraapi.c:197
void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
Definition zebraapi.c:2556
ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream, int num_input_setnames, const char **input_setnames, const char *output_setname, Z_SortKeySpecList *sort_sequence, int *sort_status)
Definition zebraapi.c:1291
static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
Definition zebraapi.c:592
ZEBRA_RES zebra_add_record(ZebraHandle zh, const char *buf, int buf_size)
Simple update record.
Definition zebraapi.c:2600
ZebraService zebra_start(const char *configName)
Creates a Zebra Service.
Definition zebraapi.c:192
ZEBRA_RES zebra_clean(ZebraHandle zh)
Definition zebraapi.c:2139
const char * zebra_get_encoding(ZebraHandle zh)
Returns character set encoding for session.
Definition zebraapi.c:107
static void zebra_close_res(ZebraHandle zh)
Definition zebraapi.c:751
ZEBRA_RES zebra_update_record(ZebraHandle zh, enum zebra_recctrl_action_t action, const char *recordType, zint *sysno, const char *match, const char *fname, const char *buf, int buf_size)
Updates record.
Definition zebraapi.c:2611
static int delete_SU_handle(void *handle, int ord, const char *index_type, const char *string_index, zinfo_index_category_t cat)
Definition zebraapi.c:1495
static struct zebra_register * zebra_register_open(ZebraService zs, const char *name, int rw, int useshadow, Res res, const char *reg_path)
Definition zebraapi.c:337
ZEBRA_RES zebra_end_trans(ZebraHandle zh)
Definition zebraapi.c:1910
char * zebra_errAdd(ZebraHandle zh)
Returns additional info for last error.
Definition zebraapi.c:1363
ZEBRA_RES zebra_commit(ZebraHandle zh)
Definition zebraapi.c:2146
ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
Definition zebraapi.c:939
ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
Creates a database.
Definition zebraapi.c:1562
ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
Definition zebraapi.c:2007
static ZEBRA_RES zebra_flush_reg(ZebraHandle zh)
Definition zebraapi.c:88
void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
Definition zebraapi.c:2755
void zebra_setError_zint(ZebraHandle zh, int code, zint i)
Definition zebraapi.c:2764
int zebra_string_norm(ZebraHandle zh, const char *index_type, const char *input_str, int input_len, char *output_str, int output_len)
Normalize zebra term for register (subject to change!)
Definition zebraapi.c:1587
ZEBRA_RES zebra_begin_read(ZebraHandle zh)
Definition zebraapi.c:1671
ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
Definition zebraapi.c:2522
#define ZEBRA_CHECK_ISAM
Definition zebraapi.c:2205
int zebra_errCode(ZebraHandle zh)
Returns error code for last error.
Definition zebraapi.c:1343
void zebra_result(ZebraHandle zh, int *code, char **addinfo)
Returns error code and additional info for last error.
Definition zebraapi.c:2500
int zebra_deleteResultSet(ZebraHandle zh, int function, int num_setnames, char **setnames, int *statuses)
Deletes one or more resultsets.
Definition zebraapi.c:1314
#define ASSERTZHRES
Definition zebraapi.c:50
static int zebra_chdir(ZebraService zs)
Definition zebraapi.c:68
static void zebra_select_register(ZebraHandle zh, const char *new_reg)
Definition zebraapi.c:759
ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
authenticate user. Returns 0 if OK, != 0 on failure
Definition zebraapi.c:1372
ZEBRA_RES zebra_set_break_handler(ZebraHandle zh, int(*f)(void *client_data), void *client_data)
Definition zebraapi.c:1070
int delete_w_handle(const char *info, void *handle)
Definition zebraapi.c:1456
ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
Set limit before Zebra does approx hit count.
Definition zebraapi.c:1056
void zebra_lock_prefix(Res res, char *path)
Definition zebraapi.c:2774
static ZEBRA_RES zebra_record_check(ZebraHandle zh, Record rec, zint *no_keys, int message_limit, unsigned flags, zint *no_long_dict_entries, zint *no_failed_dict_lookups, zint *no_invalid_keys, zint *no_invalid_dict_infos, zint *no_invalid_isam_entries)
Definition zebraapi.c:2207
ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path, enum zebra_recctrl_action_t action)
Definition zebraapi.c:2017
ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
Definition zebraapi.c:1710
const char * zebra_errString(ZebraHandle zh)
Returns error string for last error.
Definition zebraapi.c:1354
ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query, const char *setname, zint *hits)
Search using RPN Query structure (from ASN.1)
Definition zebraapi.c:1108
ZEBRA_RES zebra_stop(ZebraService zs)
stops a Zebra service.
Definition zebraapi.c:627
struct BFiles_struct * zebra_get_bfs(ZebraHandle zh)
Definition zebraapi.c:2733
#define ASSERTZS
Definition zebraapi.c:51
#define DEFAULT_APPROX_LIMIT
Definition zebraapi.c:46
ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
Definition zebraapi.c:1414
#define ASSERTZH
Definition zebraapi.c:49
static int log_level
Definition zebraapi.c:53
ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database, const char *record_type)
Definition zebraapi.c:1404
ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases, const char **basenames)
Definition zebraapi.c:948
static void zebra_open_res(ZebraHandle zh)
Definition zebraapi.c:728
int zebra_select_default_database(ZebraHandle zh)
Definition zebraapi.c:890
static void read_res_for_transaction(ZebraHandle zh)
Definition zebraapi.c:1681
ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, const Odr_oid *attributeset, int *position, int *num_entries, ZebraScanEntry **entries, int *is_partial, const char *setname)
performs Scan (Z39.50 style)
Definition zebraapi.c:1258
static void zebra_set_state(ZebraHandle zh, int val, int seqno)
set register state (state*.LCK)
Definition zebraapi.c:1621
ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
Definition zebraapi.c:2744
ZebraHandle zebra_open(ZebraService zs, Res res)
Creates a Zebra session handle within service.
Definition zebraapi.c:113
void zebra_filter_info(ZebraService zs, void *cd, void(*cb)(void *cd, const char *name))
Lists enabled Zebra filters.
Definition zebraapi.c:306
static int log_level_initialized
Definition zebraapi.c:54
static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
Definition zebraapi.c:59
ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query, const char *setname, zint *hits, int *estimated_hit_count, int *partial_resultset)
Search using RPN Query structure (from ASN.1)
Definition zebraapi.c:1079
int zebra_sort_by_specstr(ZebraHandle zh, ODR stream, const char *sort_spec, const char *output_setname, const char **input_setnames)
Definition zebraapi.c:2693
void zebra_set_shadow_enable(ZebraHandle zh, int value)
Definition zebraapi.c:2593
void map_basenames(ZebraHandle zh, ODR stream, int *num_bases, char ***basenames)
Definition zebraapi.c:905
const char * zebra_get_resource(ZebraHandle zh, const char *name, const char *defaultvalue)
Definition zebraapi.c:2565
int zebra_trans_no(ZebraHandle zh)
Definition zebraapi.c:2579
ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
Definition zebraapi.c:1919
void zebra_shadow_enable(ZebraHandle zh, int value)
Definition zebraapi.c:2515
ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
Definition zebraapi.c:2038
void zebra_pidfname(ZebraService zs, char *path)
Definition zebraapi.c:314
zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
Definition zebramap.c:355
void zebra_maps_define_default_sort(zebra_maps_t zms)
Definition zebramap.c:349
void zebra_maps_close(zebra_maps_t zm)
Definition zebramap.c:84
ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
Definition zebramap.c:295
WRBUF zebra_replace(zebra_map_t zm, const char *ex_list, const char *input_str, int input_len)
Definition zebramap.c:619
zebra_maps_t zebra_maps_open(Res res, const char *base_path, const char *profile_path)
Definition zebramap.c:324
int zebraExplain_curDatabase(ZebraExplainInfo zei, const char *database)
Definition zinfo.c:790
ZebraExplainInfo zebraExplain_open(Records records, data1_handle dh, Res res, int writeFlag, void *updateHandle, ZebraExplainUpdateFunc *updateFunc)
Definition zinfo.c:331
int zebraExplain_trav_ord(ZebraExplainInfo zei, void *handle, int(*f)(void *handle, int ord, const char *index_type, const char *string_index, zinfo_index_category_t cat))
Definition zinfo.c:1380
zint zebraExplain_runNumberIncrement(ZebraExplainInfo zei, int adjust_num)
Definition zinfo.c:1585
int zebraExplain_get_database_ord(ZebraExplainInfo zei)
Definition zinfo.c:1620
void zebraExplain_close(ZebraExplainInfo zei)
Definition zinfo.c:209
int zebraExplain_removeDatabase(ZebraExplainInfo zei, void *update_handle)
Definition zinfo.c:751
int zebraExplain_newDatabase(ZebraExplainInfo zei, const char *database, int explain_database)
Definition zinfo.c:882
void zebraExplain_flush(ZebraExplainInfo zei, void *handle)
Definition zinfo.c:168
zinfo_index_category_t
Definition zinfo.h:37
#define STRCASECMP
Definition zinfo.h:32