1 /* $Id: zebraapi.c,v 1.161 2005-04-15 10:47:48 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);
151 ZebraService zebra_start (const char *configName)
153 return zebra_start_res(configName, 0, 0);
156 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
160 if (!log_level_initialized)
162 log_level = yaz_log_module_level("zebraapi");
163 log_level_initialized = 1;
166 yaz_log(log_level, "zebra_start %s",configName);
169 if ((res = res_open (configName, def_res, over_res)))
171 ZebraService zh = xmalloc(sizeof(*zh));
173 yaz_log (YLOG_DEBUG, "Read resources `%s'", configName);
175 zh->global_res = res;
176 zh->configName = xstrdup(configName);
181 zebra_mutex_cond_init (&zh->session_lock);
182 if (!res_get (zh->global_res, "passwd"))
183 zh->passwd_db = NULL;
186 zh->passwd_db = passwd_db_open ();
188 yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
190 passwd_db_file (zh->passwd_db,
191 res_get (zh->global_res, "passwd"));
193 zh->path_root = res_get (zh->global_res, "root");
194 zh->nmem = nmem_create();
195 zh->record_classes = recTypeClass_create (zh->global_res, zh->nmem);
201 void zebra_filter_info(ZebraService zs, void *cd,
202 void (*cb)(void *cd, const char *name))
206 recTypeClass_info(zs->record_classes, cd, cb);
209 void zebra_pidfname(ZebraService zs, char *path)
212 zebra_lock_prefix (zs->global_res, path);
213 strcat(path, "zebrasrv.pid");
216 Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw,
217 int compact_flag, Res res)
219 int page_size = 4096;
220 char resource_str[200];
222 sprintf (resource_str, "dict.%.100s.pagesize", name);
226 v = res_get(res, resource_str);
230 yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
233 return dict_open(bfs, name, cache, rw, compact_flag, page_size);
238 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
239 int rw, int useshadow, Res res,
240 const char *reg_path)
242 struct zebra_register *reg;
243 int record_compression = REC_COMPRESS_NONE;
244 const char *recordCompression = 0;
245 const char *profilePath;
250 reg = xmalloc(sizeof(*reg));
253 reg->name = xstrdup (name);
260 yaz_log (YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
261 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
263 reg->dh = data1_createx (DATA1_FLAG_XML);
266 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
269 data1_destroy(reg->dh);
273 bf_cache (reg->bfs, res_get (res, "shadow"));
275 getcwd(cwd, sizeof(cwd)-1);
276 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
277 yaz_log(YLOG_DEBUG, "profilePath=%s cwd=%s", profilePath, cwd);
279 data1_set_tabpath (reg->dh, profilePath);
280 data1_set_tabroot (reg->dh, reg_path);
281 reg->recTypes = recTypes_init (zs->record_classes, reg->dh);
283 reg->zebra_maps = zebra_maps_open (res, reg_path);
284 reg->rank_classes = NULL;
288 reg->keys.buf_max = 0;
290 reg->keys.codec_handle = iscz1_start();
292 reg->sortKeys.buf = 0;
293 reg->sortKeys.buf_max = 0;
304 reg->key_file_no = 0;
307 zebraRankInstall (reg, rank1_class);
308 zebraRankInstall (reg, rankzv_class);
310 recordCompression = res_get_def (res, "recordCompression", "none");
311 if (!strcmp (recordCompression, "none"))
312 record_compression = REC_COMPRESS_NONE;
313 if (!strcmp (recordCompression, "bzip2"))
314 record_compression = REC_COMPRESS_BZIP2;
316 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
318 yaz_log (YLOG_WARN, "rec_open failed");
323 reg->matchDict = dict_open_res (reg->bfs, GMATCH_DICT, 20, 1, 0, res);
325 if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
327 yaz_log (YLOG_WARN, "dict_open failed");
330 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
332 yaz_log (YLOG_WARN, "sortIdx_open failed");
335 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
337 struct ISAMS_M_s isams_m;
338 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
339 key_isams_m(res, &isams_m))))
341 yaz_log (YLOG_WARN, "isams_open failed");
345 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
347 struct ISAMC_M_s isamc_m;
348 if (!(reg->isamc = isamc_open (reg->bfs, FNAME_ISAMC,
349 rw, key_isamc_m(res, &isamc_m))))
351 yaz_log (YLOG_WARN, "isamc_open failed");
355 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
357 struct ISAMC_M_s isamc_m;
359 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
360 rw, key_isamc_m(res, &isamc_m), 0)))
362 yaz_log (YLOG_WARN, "isamb_open failed");
366 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
368 struct ISAMC_M_s isamc_m;
370 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
371 rw, key_isamc_m(res, &isamc_m), 1)))
373 yaz_log (YLOG_WARN, "isamb_open failed");
377 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
379 struct ISAMC_M_s isamc_m;
381 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
382 rw, key_isamc_m(res, &isamc_m), -1)))
384 yaz_log (YLOG_WARN, "isamb_open failed");
388 reg->zei = zebraExplain_open (reg->records, reg->dh,
393 yaz_log (YLOG_WARN, "Cannot obtain EXPLAIN information");
397 yaz_log (YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
401 ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh)
404 yaz_log(log_level, "zebra_admin_shutdown");
407 zebra_mutex_cond_lock (&zh->service->session_lock);
408 zh->service->stop_flag = 1;
409 zebra_mutex_cond_unlock (&zh->service->session_lock);
413 ZEBRA_RES zebra_admin_start (ZebraHandle zh)
417 yaz_log(log_level, "zebra_admin_start");
420 zebra_mutex_cond_lock (&zs->session_lock);
421 zebra_mutex_cond_unlock (&zs->session_lock);
425 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
429 yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
434 zebraExplain_close (reg->zei);
435 dict_close (reg->dict);
437 dict_close (reg->matchDict);
438 sortIdx_close (reg->sortIdx);
440 isams_close (reg->isams);
442 isamc_close (reg->isamc);
444 isamb_close (reg->isamb);
445 rec_close (®->records);
448 recTypes_destroy (reg->recTypes);
449 zebra_maps_close (reg->zebra_maps);
450 zebraRankDestroy (reg);
451 bfs_destroy (reg->bfs);
452 data1_destroy (reg->dh);
454 xfree(reg->sortKeys.buf);
455 xfree(reg->keys.buf);
456 if (reg->keys.codec_handle)
457 iscz1_stop(reg->keys.codec_handle);
463 ZEBRA_RES zebra_stop(ZebraService zs)
467 yaz_log (log_level, "zebra_stop");
471 zebra_close (zs->sessions);
474 zebra_mutex_cond_destroy (&zs->session_lock);
477 passwd_db_close (zs->passwd_db);
479 recTypeClass_destroy(zs->record_classes);
480 nmem_destroy(zs->nmem);
481 res_close (zs->global_res);
482 xfree(zs->configName);
487 ZEBRA_RES zebra_close (ZebraHandle zh)
490 struct zebra_session **sp;
493 yaz_log(log_level, "zebra_close");
500 yaz_log (YLOG_DEBUG, "zebra_close zh=%p", zh);
501 resultSetDestroy (zh, -1, 0, 0);
504 zebra_register_close (zh->service, zh->reg);
505 zebra_close_res (zh);
507 xfree(zh->record_encoding);
509 for (i = 0; i < zh->num_basenames; i++)
510 xfree(zh->basenames[i]);
511 xfree(zh->basenames);
513 if (zh->iconv_to_utf8 != 0)
514 yaz_iconv_close (zh->iconv_to_utf8);
515 if (zh->iconv_from_utf8 != 0)
516 yaz_iconv_close (zh->iconv_from_utf8);
518 zebra_mutex_cond_lock (&zs->session_lock);
519 zebra_lock_destroy (zh->lock_normal);
520 zebra_lock_destroy (zh->lock_shadow);
532 zebra_mutex_cond_unlock (&zs->session_lock);
534 xfree(zh->user_perm);
535 zh->service = 0; /* more likely to trigger an assert */
541 struct map_baseinfo {
547 char **new_basenames;
551 static Res zebra_open_res (ZebraHandle zh)
560 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
561 res = res_open (fname, zh->service->global_res, 0);
563 res = zh->service->global_res;
565 else if (*zh->reg_name == 0)
567 res = zh->service->global_res;
571 yaz_log (YLOG_WARN, "no register root specified");
572 return 0; /* no path for register - fail! */
577 static void zebra_close_res (ZebraHandle zh)
581 if (zh->res != zh->service->global_res)
586 static void zebra_select_register (ZebraHandle zh, const char *new_reg)
590 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
594 assert (zh->reg == 0);
595 assert (*zh->reg_name == 0);
601 resultSetInvalidate (zh);
602 zebra_register_close (zh->service, zh->reg);
608 zh->reg_name = xstrdup (new_reg);
612 if (zh->service->path_root)
614 zh->path_reg = xmalloc(strlen(zh->service->path_root) +
615 strlen(zh->reg_name) + 3);
616 strcpy (zh->path_reg, zh->service->path_root);
619 strcat (zh->path_reg, "/");
620 strcat (zh->path_reg, zh->reg_name);
623 zh->res = zebra_open_res (zh);
626 zebra_lock_destroy (zh->lock_normal);
630 zebra_lock_destroy (zh->lock_shadow);
636 const char *lock_area =res_get (zh->res, "lockDir");
638 if (!lock_area && zh->path_reg)
639 res_set (zh->res, "lockDir", zh->path_reg);
640 sprintf (fname, "norm.%s.LCK", zh->reg_name);
642 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
644 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
646 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
651 void map_basenames_func (void *vp, const char *name, const char *value)
653 struct map_baseinfo *p = (struct map_baseinfo *) vp;
655 char fromdb[128], todb[8][128];
662 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
663 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
664 todb[5], todb[6], todb[7]);
668 for (i = 0; i<p->num_bases; i++)
669 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
672 for (i = 0; i < no; i++)
674 if (p->new_num_bases == p->new_num_max)
676 p->new_basenames[(p->new_num_bases)++] =
677 nmem_strdup (p->mem, todb[i]);
683 int zebra_select_default_database(ZebraHandle zh)
687 /* no database has been selected - so we select based on
688 resource setting (including group)
690 const char *group = res_get(zh->service->global_res, "group");
691 const char *v = res_get_prefix(zh->service->global_res,
692 "database", group, "Default");
693 zebra_select_database(zh, v);
698 void map_basenames (ZebraHandle zh, ODR stream,
699 int *num_bases, char ***basenames)
701 struct map_baseinfo info;
702 struct map_baseinfo *p = &info;
705 yaz_log(log_level, "map_basenames ");
712 info.num_bases = *num_bases;
713 info.basenames = *basenames;
714 info.new_num_max = 128;
715 info.new_num_bases = 0;
716 info.new_basenames = (char **)
717 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
718 info.mem = stream->mem;
720 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
722 for (i = 0; i<p->num_bases; i++)
723 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
725 p->new_basenames[(p->new_num_bases)++] =
726 nmem_strdup (p->mem, p->basenames[i]);
728 *num_bases = info.new_num_bases;
729 *basenames = info.new_basenames;
730 for (i = 0; i<*num_bases; i++)
731 yaz_log (YLOG_DEBUG, "base %s", (*basenames)[i]);
734 ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename)
737 yaz_log(log_level, "zebra_select_database %s",basename);
740 return zebra_select_databases (zh, 1, &basename);
743 ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases,
744 const char **basenames)
753 yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
754 num_bases,basenames[0]);
762 for (i = 0; i < zh->num_basenames; i++)
763 xfree(zh->basenames[i]);
764 xfree(zh->basenames);
766 zh->num_basenames = num_bases;
767 zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
768 for (i = 0; i < zh->num_basenames; i++)
769 zh->basenames[i] = xstrdup (basenames[i]);
771 cp = strrchr(basenames[0], '/');
774 len = cp - basenames[0];
775 new_reg = xmalloc(len + 1);
776 memcpy (new_reg, basenames[0], len);
780 new_reg = xstrdup ("");
781 for (i = 1; i<num_bases; i++)
785 cp1 = strrchr (basenames[i], '/');
793 if (len != cp1 - basenames[i] ||
794 memcmp (basenames[i], new_reg, len))
809 zebra_select_register (zh, new_reg);
816 if (!zh->lock_normal || !zh->lock_shadow)
824 ZEBRA_RES zebra_search_RPN (ZebraHandle zh, ODR o, Z_RPNQuery *query,
825 const char *setname, zint *hits)
833 yaz_log(log_level, "zebra_search_rpn");
838 if (zebra_begin_read(zh) == ZEBRA_FAIL)
841 r = resultSetAddRPN(zh, odr_extract_mem(o), query,
842 zh->num_basenames, zh->basenames, setname);
848 ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream,
850 Z_RecordComposition *comp,
851 oid_value input_format, int num_recs,
852 ZebraRetrievalRecord *recs)
854 ZebraMetaRecord *poset;
856 ZEBRA_RES ret = ZEBRA_OK;
864 yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
870 zh->errString = odr_strdup(stream, setname);
876 if (zebra_begin_read (zh) == ZEBRA_FAIL)
879 pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
880 for (i = 0; i<num_recs; i++)
881 pos_array[i] = recs[i].position;
882 poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
885 yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
887 zh->errString = nmem_strdup (stream->mem, setname);
892 for (i = 0; i<num_recs; i++)
897 recs[i].format = VAL_SUTRS;
898 recs[i].len = strlen(poset[i].term);
899 recs[i].buf = poset[i].term;
900 recs[i].base = poset[i].db;
902 else if (poset[i].sysno)
907 zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
908 stream, input_format, comp,
909 &recs[i].format, &buf, &len,
910 &recs[i].base, &recs[i].errString);
914 recs[i].buf = (char*) odr_malloc(stream, len);
915 memcpy(recs[i].buf, buf, len);
919 recs[i].score = poset[i].score;
920 recs[i].sysno = poset[i].sysno;
926 sprintf (num_str, ZINT_FORMAT, pos_array[i]);
928 zh->errString = odr_strdup (stream, num_str);
933 zebra_meta_records_destroy(zh, poset, num_recs);
940 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
942 int *num_entries, ZebraScanEntry **entries,
945 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
946 Z_AttributesPlusTerm *zapt;
949 if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
951 yaz_pqf_destroy (pqf_parser);
954 yaz_pqf_destroy (pqf_parser);
955 return zebra_scan(zh, stream, zapt, VAL_BIB1,
956 position, num_entries, entries, is_partial);
959 ZEBRA_RES zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
960 oid_value attributeset,
962 int *num_entries, ZebraScanEntry **entries,
973 yaz_log(log_level, "zebra_scan");
975 if (zebra_begin_read (zh) == ZEBRA_FAIL)
981 res = rpn_scan (zh, stream, zapt, attributeset,
982 zh->num_basenames, zh->basenames, position,
983 num_entries, entries, is_partial, 0, 0);
988 ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
989 int num_input_setnames, const char **input_setnames,
990 const char *output_setname,
991 Z_SortKeySpecList *sort_sequence,
996 assert(num_input_setnames>0);
997 assert(input_setnames);
998 assert(sort_sequence);
1000 yaz_log(log_level, "zebra_sort");
1002 if (zebra_begin_read (zh) == ZEBRA_FAIL)
1004 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
1005 output_setname, sort_sequence, sort_status);
1006 return zebra_end_read(zh);
1009 int zebra_deleteResultSet(ZebraHandle zh, int function,
1010 int num_setnames, char **setnames,
1016 yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
1018 if (zebra_begin_read(zh))
1019 return Z_DeleteStatus_systemProblemAtTarget;
1022 case Z_DeleteResultSetRequest_list:
1023 assert(num_setnames>0);
1025 resultSetDestroy (zh, num_setnames, setnames, statuses);
1027 case Z_DeleteResultSetRequest_all:
1028 resultSetDestroy (zh, -1, 0, statuses);
1031 zebra_end_read (zh);
1032 status = Z_DeleteStatus_success;
1033 for (i = 0; i<num_setnames; i++)
1034 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1035 status = statuses[i];
1039 int zebra_errCode (ZebraHandle zh)
1043 yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1046 yaz_log(log_level, "zebra_errCode: o");
1050 const char *zebra_errString (ZebraHandle zh)
1054 e= diagbib1_str (zh->errCode);
1055 yaz_log(log_level, "zebra_errString: %s",e);
1059 char *zebra_errAdd (ZebraHandle zh)
1064 yaz_log(log_level, "zebra_errAdd: %s",a);
1068 void zebra_clearError(ZebraHandle zh)
1077 ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1087 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1088 p = res_get(zs->global_res, u);
1089 xfree(zh->user_perm);
1090 zh->user_perm = xstrdup(p ? p : "r");
1092 /* users that don't require a password .. */
1093 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1096 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1101 ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
1102 const char *record_type)
1105 yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
1106 database, record_type);
1108 if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1110 return zebra_begin_trans(zh, 1);
1113 ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
1116 yaz_log(log_level, "zebra_admin_import_end");
1118 return zebra_end_trans(zh);
1121 ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1123 ZEBRA_RES res = ZEBRA_OK;
1127 yaz_log(log_level, "zebra_admin_import_segment");
1129 for (i = 0; i<segment->num_segmentRecords; i++)
1131 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1133 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1135 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1136 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1138 Odr_oct *oct = fragment->u.notExternallyTagged;
1141 if ( zebra_update_record(zh,
1142 0, /* record Type */
1155 ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh,
1156 const char *rec_buf,
1158 const char *recid_buf, size_t recid_len,
1160 /* 1 = insert. Fail it already exists */
1161 /* 2 = replace. Fail it does not exist */
1162 /* 3 = delete. Fail if does not exist */
1163 /* 4 = update. Insert/replace */
1170 assert(action>0 && action <=4);
1173 yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
1176 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1179 memcpy (recid_z, recid_buf, recid_len);
1180 recid_z[recid_len] = 0;
1182 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1185 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1188 if (action == 1) /* fail if insert */
1190 zebra_end_trans(zh);
1194 memcpy (&sysno, rinfo+1, sizeof(sysno));
1198 if (action == 2 || action == 3) /* fail if delete or update */
1200 zebra_end_trans(zh);
1203 action = 1; /* make it an insert (if it's an update).. */
1205 res = buffer_extract_record (zh, rec_buf, rec_len,
1206 action == 3 ? 1 : 0 /* delete flag */,
1212 0, /* force update */
1213 1 /* allow update */
1217 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1219 else if (action == 3)
1221 dict_delete (zh->reg->matchDict, recid_z);
1223 zebra_end_trans(zh);
1227 int delete_w_handle(const char *info, void *handle)
1229 ZebraHandle zh = (ZebraHandle) handle;
1233 if (*info == sizeof(pos))
1235 memcpy (&pos, info+1, sizeof(pos));
1236 isamb_unlink(zh->reg->isamb, pos);
1241 static int delete_SU_handle(void *handle, int ord)
1243 ZebraHandle zh = (ZebraHandle) handle;
1247 ord_len = key_SU_encode (ord, ord_buf);
1248 ord_buf[ord_len] = '\0';
1250 assert (zh->reg->isamb);
1251 dict_delete_subtree(zh->reg->dict, ord_buf,
1252 zh, delete_w_handle);
1256 ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database)
1258 ZEBRA_RES ret = ZEBRA_OK;
1260 yaz_log(log_level, "zebra_drop_database");
1263 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1265 if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
1269 zebraExplain_curDatabase (zh->reg->zei, database);
1271 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1272 zebraExplain_removeDatabase(zh->reg->zei, zh);
1276 yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1279 zebra_end_trans (zh);
1283 ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database)
1286 yaz_log(log_level, "zebra_create_database %s", database);
1290 if (zebra_select_database (zh, database) == ZEBRA_FAIL)
1292 if (zebra_begin_trans (zh, 1))
1295 /* announce database */
1296 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1297 /* explainDatabase */))
1299 zebra_end_trans (zh);
1301 zh->errString = "database already exist";
1304 return zebra_end_trans (zh);
1307 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1308 const char *input_str, int input_len,
1309 char *output_str, int output_len)
1315 yaz_log(log_level, "zebra_string_norm ");
1317 if (!zh->reg->zebra_maps)
1319 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1320 input_str, input_len);
1323 if (wrbuf_len(wrbuf) >= output_len)
1325 if (wrbuf_len(wrbuf))
1326 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1327 output_str[wrbuf_len(wrbuf)] = '\0';
1328 return wrbuf_len(wrbuf);
1331 static void zebra_set_state (ZebraHandle zh, int val, int seqno)
1333 char state_fname[256];
1338 yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
1341 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1342 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1343 f = fopen (fname, "w");
1345 yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1346 fprintf (f, "%c %d %ld\n", val, seqno, p);
1351 static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1353 char state_fname[256];
1358 yaz_log(log_level, "zebra_get_state ");
1360 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1361 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1362 f = fopen (fname, "r");
1368 fscanf (f, "%c %d", val, seqno);
1374 ZEBRA_RES zebra_begin_read (ZebraHandle zh)
1376 return zebra_begin_trans(zh, 0);
1379 ZEBRA_RES zebra_end_read (ZebraHandle zh)
1381 return zebra_end_trans(zh);
1384 static void read_res_for_transaction(ZebraHandle zh)
1386 const char *group = res_get(zh->res, "group");
1388 /* FIXME - do we still use groups ?? */
1390 zh->m_group = group;
1391 v = res_get_prefix(zh->res, "followLinks", group, "1");
1392 zh->m_follow_links = atoi(v);
1394 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1395 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1397 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1398 zh->m_store_keys = atoi(v);
1400 v = res_get_prefix(zh->res, "storeData", group, "1");
1401 zh->m_store_data = atoi(v);
1403 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1404 zh->m_explain_database = atoi(v);
1406 v = res_get_prefix(zh->res, "openRW", group, "1");
1407 zh->m_flag_rw = atoi(v);
1409 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1410 zh->m_file_verbose_limit = atoi(v);
1413 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1416 zebra_select_default_database(zh);
1420 zh->errString = "zebra_begin_trans: no database selected";
1424 yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1428 if (rw && !strchr(zh->user_perm, 'w'))
1442 const char *rval = 0;
1447 read_res_for_transaction(zh);
1450 if (zh->trans_no != 1)
1453 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1458 resultSetInvalidate (zh);
1459 zebra_register_close (zh->service, zh->reg);
1461 zh->trans_w_no = zh->trans_no;
1465 zh->records_inserted = 0;
1466 zh->records_updated = 0;
1467 zh->records_deleted = 0;
1468 zh->records_processed = 0;
1470 #if HAVE_SYS_TIMES_H
1474 if (zh->shadow_enable)
1475 rval = res_get (zh->res, "shadow");
1477 for (pass = 0; pass < 2; pass++)
1481 zebra_lock_r (zh->lock_normal);
1482 zebra_lock_w (zh->lock_shadow);
1486 zebra_lock_w (zh->lock_normal);
1487 zebra_lock_w (zh->lock_shadow);
1490 zebra_get_state (zh, &val, &seqno);
1493 yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
1494 zebra_unlock (zh->lock_shadow);
1495 zebra_unlock (zh->lock_normal);
1499 else if (val == 'd')
1503 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1505 yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
1506 bf_commitClean (bfs, rval);
1511 yaz_log (YLOG_WARN, "your previous transaction didn't finish");
1518 yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
1522 zebra_set_state (zh, 'd', seqno);
1524 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1525 1, rval ? 1 : 0, zh->res,
1528 zh->reg->seqno = seqno;
1531 zebra_set_state (zh, 'o', seqno);
1533 zebra_unlock (zh->lock_shadow);
1534 zebra_unlock (zh->lock_normal);
1540 zh->errString = "zebra_begin_trans: cannot open register";
1541 yaz_log(YLOG_FATAL, zh->errString);
1553 if (zh->trans_no != 1)
1555 zebra_flush_reg (zh);
1559 #if HAVE_SYS_TIMES_H
1568 if (!zh->lock_normal || !zh->lock_shadow)
1574 zebra_get_state (zh, &val, &seqno);
1580 else if (seqno != zh->reg->seqno)
1582 yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1583 seqno, zh->reg->seqno);
1586 else if (zh->reg->last_val != val)
1588 yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
1589 val, zh->reg->last_val);
1596 zebra_lock_r (zh->lock_shadow);
1598 zebra_lock_r (zh->lock_normal);
1602 resultSetInvalidate (zh);
1603 zebra_register_close (zh->service, zh->reg);
1605 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1606 0, val == 'c' ? 1 : 0,
1607 zh->res, zh->path_reg);
1610 zebra_unlock (zh->lock_normal);
1611 zebra_unlock (zh->lock_shadow);
1616 zh->reg->last_val = val;
1617 zh->reg->seqno = seqno;
1619 read_res_for_transaction(zh);
1623 ZEBRA_RES zebra_end_trans (ZebraHandle zh)
1625 ZebraTransactionStatus dummy;
1627 yaz_log(log_level, "zebra_end_trans");
1628 return zebra_end_transaction(zh, &dummy);
1631 ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1639 yaz_log(log_level, "zebra_end_transaction");
1641 status->processed = 0;
1642 status->inserted = 0;
1643 status->updated = 0;
1644 status->deleted = 0;
1648 if (!zh->res || !zh->reg)
1651 zh->errString = "zebra_end_trans: no open transaction";
1654 if (zh->trans_no != zh->trans_w_no)
1657 if (zh->trans_no != 0)
1660 /* release read lock */
1662 zebra_unlock (zh->lock_normal);
1663 zebra_unlock (zh->lock_shadow);
1666 { /* release write lock */
1670 yaz_log (YLOG_DEBUG, "zebra_end_trans");
1671 rval = res_get (zh->res, "shadow");
1673 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1675 zebra_flush_reg (zh);
1677 resultSetInvalidate (zh);
1679 zebra_register_close (zh->service, zh->reg);
1682 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1683 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1684 zh->records_processed, zh->records_inserted,
1685 zh->records_updated, zh->records_deleted);
1687 status->processed = (int) zh->records_processed;
1688 status->inserted = (int) zh->records_inserted;
1689 status->updated = (int) zh->records_updated;
1690 status->deleted = (int) zh->records_deleted;
1692 zebra_get_state (zh, &val, &seqno);
1695 BFiles bfs = bfs_create (rval, zh->path_reg);
1696 yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
1697 bf_commitClean (bfs, rval);
1702 zebra_set_state (zh, 'o', seqno);
1704 zebra_unlock (zh->lock_shadow);
1705 zebra_unlock (zh->lock_normal);
1708 #if HAVE_SYS_TIMES_H
1710 yaz_log (log_level, "user/system: %ld/%ld",
1711 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1712 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1714 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1715 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1720 int zebra_repository_update (ZebraHandle zh, const char *path)
1725 yaz_log (log_level, "updating %s", path);
1726 repositoryUpdate (zh, path);
1730 int zebra_repository_delete (ZebraHandle zh, const char *path)
1735 yaz_log (log_level, "deleting %s", path);
1736 repositoryDelete (zh, path);
1740 int zebra_repository_show (ZebraHandle zh, const char *path)
1744 yaz_log(log_level, "zebra_repository_show");
1746 repositoryShow (zh, path);
1750 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1759 zebra_select_default_database(zh);
1765 rval = res_get (zh->res, "shadow");
1768 yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
1772 zebra_lock_w (zh->lock_normal);
1773 zebra_lock_r (zh->lock_shadow);
1775 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1777 zebra_get_state (zh, &val, &seqno);
1780 bf_cache (bfs, rval);
1781 if (bf_commitExists (bfs))
1784 zebra_set_state (zh, 'd', seqno);
1787 zebra_set_state (zh, 'c', seqno);
1789 yaz_log (YLOG_DEBUG, "commit start");
1790 bf_commitExec (bfs);
1795 yaz_log (YLOG_DEBUG, "commit clean");
1796 bf_commitClean (bfs, rval);
1798 zebra_set_state (zh, 'o', seqno);
1802 yaz_log (log_level, "nothing to commit");
1806 zebra_unlock (zh->lock_shadow);
1807 zebra_unlock (zh->lock_normal);
1811 ZEBRA_RES zebra_clean (ZebraHandle zh)
1814 yaz_log(log_level, "zebra_clean");
1815 return zebra_commit_ex(zh, 1);
1818 ZEBRA_RES zebra_commit (ZebraHandle zh)
1821 yaz_log(log_level, "zebra_commit");
1822 return zebra_commit_ex(zh, 0);
1825 ZEBRA_RES zebra_init (ZebraHandle zh)
1830 yaz_log(log_level, "zebra_init");
1833 zebra_select_default_database(zh);
1839 rval = res_get (zh->res, "shadow");
1841 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1845 bf_cache (bfs, rval);
1849 zebra_set_state (zh, 'o', 0);
1853 int zebra_compact (ZebraHandle zh)
1857 yaz_log(log_level, "zebra_compact");
1864 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1870 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1873 yaz_log(log_level, "zebra_result");
1874 *code = zh->errCode;
1875 *addinfo = zh->errString;
1879 void zebra_shadow_enable (ZebraHandle zh, int value)
1882 yaz_log(log_level, "zebra_shadow_enable");
1884 zh->shadow_enable = value;
1887 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
1891 yaz_log(log_level, "zebra_octet_term_encoding");
1894 if (zh->iconv_to_utf8 != 0)
1895 yaz_iconv_close(zh->iconv_to_utf8);
1896 if (zh->iconv_from_utf8 != 0)
1897 yaz_iconv_close(zh->iconv_from_utf8);
1900 yaz_iconv_open ("UTF-8", encoding);
1901 if (zh->iconv_to_utf8 == 0)
1902 yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1903 zh->iconv_from_utf8 =
1904 yaz_iconv_open (encoding, "UTF-8");
1905 if (zh->iconv_to_utf8 == 0)
1906 yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1911 ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
1914 yaz_log(log_level, "zebra_record_encoding");
1916 xfree(zh->record_encoding);
1917 zh->record_encoding = 0;
1919 zh->record_encoding = xstrdup (encoding);
1923 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1928 yaz_log(log_level, "zebra_set_resource %s:%s",name,value);
1930 res_set(zh->res, name, value);
1933 const char *zebra_get_resource(ZebraHandle zh,
1934 const char *name, const char *defaultvalue)
1939 assert(defaultvalue);
1940 v= res_get_def( zh->res, name, (char *)defaultvalue);
1942 yaz_log(log_level, "zebra_get_resource %s:%s",name,v);
1946 /* moved from zebra_api_ext.c by pop */
1947 /* FIXME: Should this really be public??? -Heikki */
1949 int zebra_trans_no (ZebraHandle zh)
1952 yaz_log(log_level, "zebra_trans_no");
1953 return zh->trans_no;
1956 int zebra_get_shadow_enable (ZebraHandle zh)
1959 yaz_log(log_level, "zebra_get_shadow_enable");
1960 return (zh->shadow_enable);
1963 void zebra_set_shadow_enable (ZebraHandle zh, int value)
1966 yaz_log(log_level, "zebra_set_shadow_enable %d",value);
1967 zh->shadow_enable = value;
1970 /* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
1971 so that it's identicical to the original api_records_retrieve */
1972 void api_records_retrieve (ZebraHandle zh, ODR stream,
1973 const char *setname, Z_RecordComposition *comp,
1974 oid_value input_format, int num_recs,
1975 ZebraRetrievalRecord *recs)
1977 zebra_records_retrieve(zh, stream, setname, comp, input_format,
1981 /* ---------------------------------------------------------------------------
1982 Record insert(=update), delete
1984 If sysno is provided, then it's used to identify the record.
1985 If not, and match_criteria is provided, then sysno is guessed
1986 If not, and a record is provided, then sysno is got from there
1987 NOTE: Now returns 0 at success and updates sysno, which is an int*
1991 int zebra_add_record(ZebraHandle zh,
1992 const char *buf, int buf_size)
1995 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1998 ZEBRA_RES zebra_insert_record (ZebraHandle zh,
1999 const char *recordType,
2000 SYSNO *sysno, const char *match,
2002 const char *buf, int buf_size, int force_update)
2008 yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
2011 buf_size = strlen(buf);
2013 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2015 res = buffer_extract_record (zh, buf, buf_size,
2016 0, /* delete_flag */
2022 0); /* allow_update */
2023 zebra_end_trans(zh);
2027 ZEBRA_RES zebra_update_record (ZebraHandle zh,
2028 const char *recordType,
2029 SYSNO* sysno, const char *match,
2031 const char *buf, int buf_size,
2039 yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
2041 if (buf_size < 1) buf_size = strlen(buf);
2043 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2045 res = buffer_extract_record (zh, buf, buf_size,
2046 0, /* delete_flag */
2052 1); /* allow_update */
2053 zebra_end_trans(zh);
2057 ZEBRA_RES zebra_delete_record (ZebraHandle zh,
2058 const char *recordType,
2059 SYSNO *sysno, const char *match,
2061 const char *buf, int buf_size,
2068 yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2070 if (buf_size < 1) buf_size = strlen(buf);
2072 if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2074 res = buffer_extract_record (zh, buf, buf_size,
2075 1, /* delete_flag */
2081 1); /* allow_update */
2082 zebra_end_trans(zh);
2086 /* ---------------------------------------------------------------------------
2090 ZEBRA_RES zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2091 const char *setname, zint *numhits)
2094 ZEBRA_RES res = ZEBRA_OK;
2096 ODR odr = odr_createmem(ODR_ENCODE);
2101 yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2103 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2107 yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
2111 res = zebra_search_RPN (zh, odr, query, setname, &hits);
2115 yaz_log(log_level, "Hits: " ZINT_FORMAT, hits);
2123 /* ---------------------------------------------------------------------------
2124 Sort - a simplified interface, with optional read locks.
2126 int zebra_sort_by_specstr (ZebraHandle zh, ODR stream,
2127 const char *sort_spec,
2128 const char *output_setname,
2129 const char **input_setnames)
2131 int num_input_setnames = 0;
2132 int sort_status = 0;
2133 Z_SortKeySpecList *sort_sequence;
2137 assert(output_setname);
2138 assert(input_setnames);
2139 sort_sequence = yaz_sort_spec (stream, sort_spec);
2140 yaz_log(log_level, "sort (FIXME) ");
2143 yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2148 /* we can do this, since the perl typemap code for char** will
2149 put a NULL at the end of list */
2150 while (input_setnames[num_input_setnames]) num_input_setnames++;
2152 if (zebra_begin_read (zh))
2155 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2156 output_setname, sort_sequence, &sort_status);
2162 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2165 return zh->reg->bfs;