1 /* $Id: zebraapi.c,v 1.123 2004-08-06 13:14:46 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
33 #include <yaz/diagbib1.h>
34 #include <yaz/pquery.h>
35 #include <yaz/sortspec.h>
40 /* simple asserts to validate the most essential input args */
41 #define ASSERTZH assert(zh && zh->service)
42 #define ASSERTZHRES assert(zh && zh->service && zh->res)
43 #define ASSERTZS assert(zs)
45 /* A simple log macro */
46 /* don't break with older yazen that lack log_app2 */
48 #define LOG_API LOG_APP2
50 #define LOG_API LOG_DEBUG
53 static Res zebra_open_res (ZebraHandle zh);
54 static void zebra_close_res (ZebraHandle zh);
57 static void zebra_chdir (ZebraService zs)
61 yaz_log(LOG_API,"zebra_chdir");
62 dir = res_get (zs->global_res, "chdir");
65 logf (LOG_DEBUG, "chdir %s", dir);
73 static void zebra_flush_reg (ZebraHandle zh)
76 yaz_log(LOG_API,"zebra_flush_reg");
78 zebraExplain_flush (zh->reg->zei, zh);
80 extract_flushWriteKeys (zh,1 /* final */);
81 zebra_index_merge (zh );
84 static struct zebra_register *zebra_register_open (ZebraService zs,
86 int rw, int useshadow,
88 const char *reg_path);
89 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
91 ZebraHandle zebra_open (ZebraService zs)
94 const char *default_encoding;
96 yaz_log(LOG_API,"zebra_open");
101 zh = (ZebraHandle) xmalloc (sizeof(*zh));
102 yaz_log (LOG_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");
127 zh->record_encoding = xstrdup (default_encoding);
130 yaz_iconv_open ("UTF-8", default_encoding);
131 if (zh->iconv_to_utf8 == 0)
132 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
134 zh->iconv_from_utf8 =
135 yaz_iconv_open (default_encoding, "UTF-8");
136 if (zh->iconv_to_utf8 == 0)
137 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
140 zebra_mutex_cond_lock (&zs->session_lock);
142 zh->next = zs->sessions;
145 zebra_mutex_cond_unlock (&zs->session_lock);
150 ZebraService zebra_start (const char *configName)
152 return zebra_start_res(configName, 0, 0);
155 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
159 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
161 if ((res = res_open (configName, def_res, over_res)))
163 ZebraService zh = xmalloc (sizeof(*zh));
165 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
167 zh->global_res = res;
168 zh->configName = xstrdup(configName);
173 zebra_mutex_cond_init (&zh->session_lock);
174 if (!res_get (zh->global_res, "passwd"))
175 zh->passwd_db = NULL;
178 zh->passwd_db = passwd_db_open ();
180 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
182 passwd_db_file (zh->passwd_db,
183 res_get (zh->global_res, "passwd"));
185 zh->path_root = res_get (zh->global_res, "root");
192 void zebra_pidfname(ZebraService zs, char *path)
194 zebra_lock_prefix (zs->global_res, path);
195 strcat(path, "zebrasrv.pid");
199 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
200 int rw, int useshadow, Res res,
201 const char *reg_path)
203 struct zebra_register *reg;
204 int record_compression = REC_COMPRESS_NONE;
205 const char *recordCompression = 0;
206 const char *profilePath;
211 reg = xmalloc (sizeof(*reg));
214 reg->name = xstrdup (name);
221 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
222 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
224 reg->dh = data1_createx (DATA1_FLAG_XML);
227 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
230 data1_destroy(reg->dh);
234 bf_cache (reg->bfs, res_get (res, "shadow"));
236 getcwd(cwd, sizeof(cwd)-1);
237 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
238 yaz_log(LOG_LOG, "profilePath=%s cwd=%s", profilePath, cwd);
240 data1_set_tabpath (reg->dh, profilePath);
241 data1_set_tabroot (reg->dh, reg_path);
242 reg->recTypes = recTypes_init (reg->dh);
243 recTypes_default_handlers (reg->recTypes);
245 reg->zebra_maps = zebra_maps_open (res, reg_path);
246 reg->rank_classes = NULL;
250 reg->keys.buf_max = 0;
253 reg->keys.codec_handle = iscz1_start();
256 reg->sortKeys.buf = 0;
257 reg->sortKeys.buf_max = 0;
268 reg->key_file_no = 0;
271 zebraRankInstall (reg, rank1_class);
272 zebraRankInstall (reg, rankzv_class);
273 zebraRankInstall (reg, rankliv_class);
275 recordCompression = res_get_def (res, "recordCompression", "none");
276 if (!strcmp (recordCompression, "none"))
277 record_compression = REC_COMPRESS_NONE;
278 if (!strcmp (recordCompression, "bzip2"))
279 record_compression = REC_COMPRESS_BZIP2;
281 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
283 logf (LOG_WARN, "rec_open");
288 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
290 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
292 logf (LOG_WARN, "dict_open");
295 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
297 logf (LOG_WARN, "sortIdx_open");
300 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
302 struct ISAMS_M_s isams_m;
303 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
304 key_isams_m(res, &isams_m))))
306 logf (LOG_WARN, "isams_open");
310 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
312 struct ISAMC_M_s isamc_m;
313 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
314 rw, key_isamc_m(res, &isamc_m))))
316 logf (LOG_WARN, "isc_open");
320 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
322 struct ISAMC_M_s isamc_m;
324 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
325 rw, key_isamc_m(res, &isamc_m), 0)))
327 logf (LOG_WARN, "isamb_open");
331 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
333 struct ISAMC_M_s isamc_m;
335 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
336 rw, key_isamc_m(res, &isamc_m), 1)))
338 logf (LOG_WARN, "isamb_open");
342 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
344 struct ISAMC_M_s isamc_m;
346 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
347 rw, key_isamc_m(res, &isamc_m), -1)))
349 logf (LOG_WARN, "isamb_open");
353 reg->zei = zebraExplain_open (reg->records, reg->dh,
358 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
362 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
366 int zebra_admin_shutdown (ZebraHandle zh)
369 yaz_log(LOG_API,"zebra_admin_shutdown");
372 zebra_mutex_cond_lock (&zh->service->session_lock);
373 zh->service->stop_flag = 1;
374 zebra_mutex_cond_unlock (&zh->service->session_lock);
378 int zebra_admin_start (ZebraHandle zh)
382 yaz_log(LOG_API,"zebra_admin_start");
385 zebra_mutex_cond_lock (&zs->session_lock);
386 zebra_mutex_cond_unlock (&zs->session_lock);
390 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
393 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
398 zebraExplain_close (reg->zei);
399 dict_close (reg->dict);
401 dict_close (reg->matchDict);
402 sortIdx_close (reg->sortIdx);
404 isams_close (reg->isams);
406 isc_close (reg->isamc);
408 isamb_close (reg->isamb);
409 rec_close (®->records);
412 recTypes_destroy (reg->recTypes);
413 zebra_maps_close (reg->zebra_maps);
414 zebraRankDestroy (reg);
415 bfs_destroy (reg->bfs);
416 data1_destroy (reg->dh);
418 xfree (reg->sortKeys.buf);
419 xfree (reg->keys.buf);
421 if (reg->keys.codec_handle)
422 iscz1_stop(reg->keys.codec_handle);
425 xfree (reg->key_buf);
430 int zebra_stop(ZebraService zs)
434 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
438 zebra_close (zs->sessions);
441 zebra_mutex_cond_destroy (&zs->session_lock);
444 passwd_db_close (zs->passwd_db);
446 res_close (zs->global_res);
447 xfree (zs->configName);
452 int zebra_close (ZebraHandle zh)
455 struct zebra_session **sp;
458 yaz_log(LOG_API,"zebra_close");
465 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
466 resultSetDestroy (zh, -1, 0, 0);
469 zebra_register_close (zh->service, zh->reg);
470 zebra_close_res (zh);
472 xfree (zh->record_encoding);
474 for (i = 0; i < zh->num_basenames; i++)
475 xfree (zh->basenames[i]);
476 xfree (zh->basenames);
478 if (zh->iconv_to_utf8 != 0)
479 yaz_iconv_close (zh->iconv_to_utf8);
480 if (zh->iconv_from_utf8 != 0)
481 yaz_iconv_close (zh->iconv_from_utf8);
483 zebra_mutex_cond_lock (&zs->session_lock);
484 zebra_lock_destroy (zh->lock_normal);
485 zebra_lock_destroy (zh->lock_shadow);
497 zebra_mutex_cond_unlock (&zs->session_lock);
498 xfree (zh->reg_name);
499 xfree (zh->user_perm);
500 zh->service=0; /* more likely to trigger an assert */
501 xfree (zh->path_reg);
506 struct map_baseinfo {
512 char **new_basenames;
516 static Res zebra_open_res (ZebraHandle zh)
525 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
526 res = res_open (fname, zh->service->global_res, 0);
528 res = zh->service->global_res;
530 else if (*zh->reg_name == 0)
532 res = zh->service->global_res;
536 yaz_log (LOG_WARN, "no register root specified");
537 return 0; /* no path for register - fail! */
542 static void zebra_close_res (ZebraHandle zh)
546 if (zh->res != zh->service->global_res)
551 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
555 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
559 assert (zh->reg == 0);
560 assert (*zh->reg_name == 0);
566 resultSetInvalidate (zh);
567 zebra_register_close (zh->service, zh->reg);
572 xfree (zh->reg_name);
573 zh->reg_name = xstrdup (new_reg);
575 xfree (zh->path_reg);
577 if (zh->service->path_root)
579 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
580 strlen(zh->reg_name) + 3);
581 strcpy (zh->path_reg, zh->service->path_root);
584 strcat (zh->path_reg, "/");
585 strcat (zh->path_reg, zh->reg_name);
588 zh->res = zebra_open_res (zh);
591 zebra_lock_destroy (zh->lock_normal);
595 zebra_lock_destroy (zh->lock_shadow);
601 const char *lock_area =res_get (zh->res, "lockDir");
603 if (!lock_area && zh->path_reg)
604 res_set (zh->res, "lockDir", zh->path_reg);
605 sprintf (fname, "norm.%s.LCK", zh->reg_name);
607 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
609 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
611 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
617 void map_basenames_func (void *vp, const char *name, const char *value)
619 struct map_baseinfo *p = (struct map_baseinfo *) vp;
621 char fromdb[128], todb[8][128];
624 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
625 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
626 todb[5], todb[6], todb[7]);
630 for (i = 0; i<p->num_bases; i++)
631 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
634 for (i = 0; i < no; i++)
636 if (p->new_num_bases == p->new_num_max)
638 p->new_basenames[(p->new_num_bases)++] =
639 nmem_strdup (p->mem, todb[i]);
645 void map_basenames (ZebraHandle zh, ODR stream,
646 int *num_bases, char ***basenames)
648 struct map_baseinfo info;
649 struct map_baseinfo *p = &info;
652 yaz_log(LOG_API,"map_basenames ");
656 info.num_bases = *num_bases;
657 info.basenames = *basenames;
658 info.new_num_max = 128;
659 info.new_num_bases = 0;
660 info.new_basenames = (char **)
661 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
662 info.mem = stream->mem;
664 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
666 for (i = 0; i<p->num_bases; i++)
667 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
669 p->new_basenames[(p->new_num_bases)++] =
670 nmem_strdup (p->mem, p->basenames[i]);
672 *num_bases = info.new_num_bases;
673 *basenames = info.new_basenames;
674 for (i = 0; i<*num_bases; i++)
675 logf (LOG_LOG, "base %s", (*basenames)[i]);
678 int zebra_select_database (ZebraHandle zh, const char *basename)
681 yaz_log(LOG_API,"zebra_select_database %s",basename);
683 return zebra_select_databases (zh, 1, &basename);
686 int zebra_select_databases (ZebraHandle zh, int num_bases,
687 const char **basenames)
694 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
695 num_bases,basenames[0]);
703 for (i = 0; i < zh->num_basenames; i++)
704 xfree (zh->basenames[i]);
705 xfree (zh->basenames);
707 zh->num_basenames = num_bases;
708 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
709 for (i = 0; i < zh->num_basenames; i++)
710 zh->basenames[i] = xstrdup (basenames[i]);
712 cp = strrchr(basenames[0], '/');
715 len = cp - basenames[0];
716 new_reg = xmalloc (len + 1);
717 memcpy (new_reg, basenames[0], len);
721 new_reg = xstrdup ("");
722 for (i = 1; i<num_bases; i++)
726 cp1 = strrchr (basenames[i], '/');
734 if (len != cp1 - basenames[i] ||
735 memcmp (basenames[i], new_reg, len))
750 zebra_select_register (zh, new_reg);
757 if (!zh->lock_normal || !zh->lock_shadow)
765 int zebra_search_RPN (ZebraHandle zh, ODR o,
766 Z_RPNQuery *query, const char *setname, int *hits)
769 yaz_log(LOG_API,"zebra_search_rpn");
774 if (zebra_begin_read (zh))
777 zebra_livcode_transform(zh, query);
779 resultSetAddRPN (zh, odr_extract_mem(o), query,
780 zh->num_basenames, zh->basenames, setname);
788 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
789 const char *setname, Z_RecordComposition *comp,
790 oid_value input_format, int num_recs,
791 ZebraRetrievalRecord *recs)
794 int i, *pos_array, ret = 0;
796 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
802 zh->errString = odr_strdup (stream, setname);
808 if (zebra_begin_read (zh))
811 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
812 for (i = 0; i<num_recs; i++)
813 pos_array[i] = recs[i].position;
814 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
817 logf (LOG_DEBUG, "zebraPosSetCreate error");
819 zh->errString = nmem_strdup (stream->mem, setname);
824 for (i = 0; i<num_recs; i++)
829 recs[i].format = VAL_SUTRS;
830 recs[i].len = strlen(poset[i].term);
831 recs[i].buf = poset[i].term;
832 recs[i].base = poset[i].db;
834 else if (poset[i].sysno)
837 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
838 stream, input_format, comp,
839 &recs[i].format, &recs[i].buf,
842 recs[i].errString = NULL;
848 sprintf (num_str, "%d", pos_array[i]);
850 zh->errString = odr_strdup (stream, num_str);
855 zebraPosSetDestroy (zh, poset, num_recs);
862 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
863 oid_value attributeset,
864 int *position, int *num_entries, ZebraScanEntry **entries,
868 yaz_log(LOG_API,"zebra_scan");
870 if (zebra_begin_read (zh))
876 rpn_scan (zh, stream, zapt, attributeset,
877 zh->num_basenames, zh->basenames, position,
878 num_entries, entries, is_partial, 0, 0);
883 int zebra_sort (ZebraHandle zh, ODR stream,
884 int num_input_setnames, const char **input_setnames,
885 const char *output_setname, Z_SortKeySpecList *sort_sequence,
889 yaz_log(LOG_API,"zebra_sort");
891 if (zebra_begin_read (zh))
893 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
894 output_setname, sort_sequence, sort_status);
899 int zebra_deleleResultSet(ZebraHandle zh, int function,
900 int num_setnames, char **setnames,
905 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
907 if (zebra_begin_read(zh))
908 return Z_DeleteStatus_systemProblemAtTarget;
911 case Z_DeleteResultSetRequest_list:
912 resultSetDestroy (zh, num_setnames, setnames, statuses);
914 case Z_DeleteResultSetRequest_all:
915 resultSetDestroy (zh, -1, 0, statuses);
919 status = Z_DeleteStatus_success;
920 for (i = 0; i<num_setnames; i++)
921 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
922 status = statuses[i];
926 int zebra_errCode (ZebraHandle zh)
930 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
933 yaz_log(LOG_API,"zebra_errCode: o");
937 const char *zebra_errString (ZebraHandle zh)
941 e= diagbib1_str (zh->errCode);
942 yaz_log(LOG_API,"zebra_errString: %s",e);
946 char *zebra_errAdd (ZebraHandle zh)
951 yaz_log(LOG_API,"zebra_errAdd: %s",a);
955 void zebra_clearError(ZebraHandle zh)
964 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
974 sprintf(u, "perm.%.30s", user ? user : "anonymous");
975 p = res_get(zs->global_res, u);
976 xfree (zh->user_perm);
977 zh->user_perm = xstrdup(p ? p : "r");
979 /* users that don't require a password .. */
980 if (zh->user_perm && strchr(zh->user_perm, 'a'))
983 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
988 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
989 const char *record_type)
992 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
993 database, record_type);
995 if (zebra_select_database(zh, database))
997 if (zebra_begin_trans (zh, 1))
1002 int zebra_admin_import_end (ZebraHandle zh)
1005 yaz_log(LOG_API,"zebra_admin_import_end");
1007 zebra_end_trans (zh);
1011 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1016 yaz_log(LOG_API,"zebra_admin_import_segment");
1018 for (i = 0; i<segment->num_segmentRecords; i++)
1020 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1022 printf ("--------------%d--------------------\n", i);
1023 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1025 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1026 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1028 Odr_oct *oct = fragment->u.notExternallyTagged;
1029 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1034 zebra_update_record(zh,
1035 0, /* record Type */
1047 int zebra_admin_exchange_record (ZebraHandle zh,
1048 const char *rec_buf,
1050 const char *recid_buf, size_t recid_len,
1052 /* 1 = insert. Fail it already exists */
1053 /* 2 = replace. Fail it does not exist */
1054 /* 3 = delete. Fail if does not exist */
1055 /* 4 = update. Insert/replace */
1061 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1064 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1066 memcpy (recid_z, recid_buf, recid_len);
1067 recid_z[recid_len] = 0;
1069 if (zebra_begin_trans(zh, 1))
1072 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1075 if (action == 1) /* fail if insert */
1077 zebra_end_trans(zh);
1081 memcpy (&sysno, rinfo+1, sizeof(sysno));
1085 if (action == 2 || action == 3) /* fail if delete or update */
1087 zebra_end_trans(zh);
1090 action = 1; /* make it an insert (if it's an update).. */
1092 buffer_extract_record (zh, rec_buf, rec_len,
1093 action == 3 ? 1 : 0 /* delete flag */,
1099 0, /* force update */
1100 1 /* allow update */
1104 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1106 else if (action == 3)
1108 dict_delete (zh->reg->matchDict, recid_z);
1110 zebra_end_trans(zh);
1114 int delete_w_handle(const char *info, void *handle)
1116 ZebraHandle zh = (ZebraHandle) handle;
1119 if (*info == sizeof(pos))
1121 memcpy (&pos, info+1, sizeof(pos));
1122 isamb_unlink(zh->reg->isamb, pos);
1127 static int delete_SU_handle(void *handle, int ord)
1129 ZebraHandle zh = (ZebraHandle) handle;
1133 ord_len = key_SU_encode (ord, ord_buf);
1134 ord_buf[ord_len] = '\0';
1136 assert (zh->reg->isamb);
1137 dict_delete_subtree(zh->reg->dict, ord_buf,
1138 zh, delete_w_handle);
1142 int zebra_drop_database (ZebraHandle zh, const char *database)
1146 yaz_log(LOG_API,"zebra_drop_database");
1149 if (zebra_select_database (zh, database))
1151 if (zebra_begin_trans (zh, 1))
1155 zebraExplain_curDatabase (zh->reg->zei, database);
1157 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1158 zebraExplain_removeDatabase(zh->reg->zei, zh);
1162 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1165 zebra_end_trans (zh);
1169 int zebra_create_database (ZebraHandle zh, const char *database)
1172 yaz_log(LOG_API,"zebra_create_database");
1175 if (zebra_select_database (zh, database))
1177 if (zebra_begin_trans (zh, 1))
1180 /* announce database */
1181 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1182 /* explainDatabase */))
1184 zebra_end_trans (zh);
1186 zh->errString = "database already exist";
1189 zebra_end_trans (zh);
1193 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1194 const char *input_str, int input_len,
1195 char *output_str, int output_len)
1199 yaz_log(LOG_API,"zebra_string_norm ");
1201 if (!zh->reg->zebra_maps)
1203 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1204 input_str, input_len);
1207 if (wrbuf_len(wrbuf) >= output_len)
1209 if (wrbuf_len(wrbuf))
1210 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1211 output_str[wrbuf_len(wrbuf)] = '\0';
1212 return wrbuf_len(wrbuf);
1216 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1218 char state_fname[256];
1223 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1226 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1227 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1228 f = fopen (fname, "w");
1230 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1231 fprintf (f, "%c %d %ld\n", val, seqno, p);
1237 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1239 char state_fname[256];
1244 yaz_log(LOG_API,"zebra_get_state ");
1246 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1247 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1248 f = fopen (fname, "r");
1254 fscanf (f, "%c %d", val, seqno);
1261 int zebra_begin_read (ZebraHandle zh)
1263 return zebra_begin_trans(zh, 0);
1266 int zebra_end_read (ZebraHandle zh)
1268 return zebra_end_trans(zh);
1271 static void read_res_for_transaction(ZebraHandle zh)
1273 const char *group = res_get(zh->res, "group");
1276 zh->m_group = group;
1277 v = res_get_prefix(zh->res, "followLinks", group, "1");
1278 zh->m_follow_links = atoi(v);
1280 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1281 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1283 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1284 zh->m_store_keys = atoi(v);
1286 v = res_get_prefix(zh->res, "storeData", group, "1");
1287 zh->m_store_data = atoi(v);
1289 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1290 zh->m_explain_database = atoi(v);
1292 v = res_get_prefix(zh->res, "openRW", group, "1");
1293 zh->m_flag_rw = atoi(v);
1295 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1296 zh->m_file_verbose_limit = atoi(v);
1299 int zebra_begin_trans (ZebraHandle zh, int rw)
1304 zh->errString = "zebra_begin_trans: no database selected";
1308 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1312 if (rw && !strchr(zh->user_perm, 'w'))
1326 const char *rval = 0;
1331 read_res_for_transaction(zh);
1334 if (zh->trans_no != 1)
1337 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1342 resultSetInvalidate (zh);
1343 zebra_register_close (zh->service, zh->reg);
1345 zh->trans_w_no = zh->trans_no;
1349 zh->records_inserted = 0;
1350 zh->records_updated = 0;
1351 zh->records_deleted = 0;
1352 zh->records_processed = 0;
1354 #if HAVE_SYS_TIMES_H
1358 if (zh->shadow_enable)
1359 rval = res_get (zh->res, "shadow");
1361 for (pass = 0; pass < 2; pass++)
1365 zebra_lock_r (zh->lock_normal);
1366 zebra_lock_w (zh->lock_shadow);
1370 zebra_lock_w (zh->lock_normal);
1371 zebra_lock_w (zh->lock_shadow);
1374 zebra_get_state (zh, &val, &seqno);
1377 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1378 zebra_unlock (zh->lock_shadow);
1379 zebra_unlock (zh->lock_normal);
1383 else if (val == 'd')
1387 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1389 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1390 bf_commitClean (bfs, rval);
1395 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1402 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1406 zebra_set_state (zh, 'd', seqno);
1408 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1409 1, rval ? 1 : 0, zh->res,
1412 zh->reg->seqno = seqno;
1415 zebra_set_state (zh, 'o', seqno);
1417 zebra_unlock (zh->lock_shadow);
1418 zebra_unlock (zh->lock_normal);
1424 zh->errString = "zebra_begin_trans: cannot open register";
1425 yaz_log(LOG_FATAL, zh->errString);
1437 if (zh->trans_no != 1)
1439 zebra_flush_reg (zh);
1443 #if HAVE_SYS_TIMES_H
1452 if (!zh->lock_normal || !zh->lock_shadow)
1458 zebra_get_state (zh, &val, &seqno);
1464 else if (seqno != zh->reg->seqno)
1466 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1467 seqno, zh->reg->seqno);
1470 else if (zh->reg->last_val != val)
1472 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1473 val, zh->reg->last_val);
1480 zebra_lock_r (zh->lock_shadow);
1482 zebra_lock_r (zh->lock_normal);
1486 resultSetInvalidate (zh);
1487 zebra_register_close (zh->service, zh->reg);
1489 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1490 0, val == 'c' ? 1 : 0,
1491 zh->res, zh->path_reg);
1494 zebra_unlock (zh->lock_normal);
1495 zebra_unlock (zh->lock_shadow);
1500 zh->reg->last_val = val;
1501 zh->reg->seqno = seqno;
1503 read_res_for_transaction(zh);
1507 int zebra_end_trans (ZebraHandle zh)
1509 ZebraTransactionStatus dummy;
1510 yaz_log(LOG_API,"zebra_end_trans");
1511 return zebra_end_transaction(zh, &dummy);
1514 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1521 yaz_log(LOG_API,"zebra_end_transaction");
1523 status->processed = 0;
1524 status->inserted = 0;
1525 status->updated = 0;
1526 status->deleted = 0;
1530 if (!zh->res || !zh->reg)
1533 zh->errString = "zebra_end_trans: no open transaction";
1536 if (zh->trans_no != zh->trans_w_no)
1539 if (zh->trans_no != 0)
1542 /* release read lock */
1544 zebra_unlock (zh->lock_normal);
1545 zebra_unlock (zh->lock_shadow);
1548 { /* release write lock */
1552 yaz_log (LOG_LOG, "zebra_end_trans");
1553 rval = res_get (zh->res, "shadow");
1555 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1557 zebra_flush_reg (zh);
1559 resultSetInvalidate (zh);
1561 zebra_register_close (zh->service, zh->reg);
1564 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1565 zh->records_processed, zh->records_inserted,
1566 zh->records_updated, zh->records_deleted);
1568 status->processed = zh->records_processed;
1569 status->inserted = zh->records_inserted;
1570 status->updated = zh->records_updated;
1571 status->deleted = zh->records_deleted;
1573 zebra_get_state (zh, &val, &seqno);
1576 BFiles bfs = bfs_create (rval, zh->path_reg);
1577 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1578 bf_commitClean (bfs, rval);
1583 zebra_set_state (zh, 'o', seqno);
1585 zebra_unlock (zh->lock_shadow);
1586 zebra_unlock (zh->lock_normal);
1589 #if HAVE_SYS_TIMES_H
1591 logf (LOG_LOG, "user/system: %ld/%ld",
1592 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1593 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1595 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1596 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1601 int zebra_repository_update (ZebraHandle zh, const char *path)
1605 logf (LOG_LOG|LOG_API, "updating %s", path);
1606 repositoryUpdate (zh, path);
1610 int zebra_repository_delete (ZebraHandle zh, const char *path)
1614 logf (LOG_LOG|LOG_API, "deleting %s", path);
1615 repositoryDelete (zh, path);
1619 int zebra_repository_show (ZebraHandle zh, const char *path)
1622 yaz_log(LOG_API,"zebra_repository_show");
1624 repositoryShow (zh, path);
1628 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1642 rval = res_get (zh->res, "shadow");
1645 logf (LOG_WARN, "Cannot perform commit");
1646 logf (LOG_WARN, "No shadow area defined");
1650 zebra_lock_w (zh->lock_normal);
1651 zebra_lock_r (zh->lock_shadow);
1653 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1655 zebra_get_state (zh, &val, &seqno);
1658 bf_cache (bfs, rval);
1659 if (bf_commitExists (bfs))
1662 zebra_set_state (zh, 'd', seqno);
1665 zebra_set_state (zh, 'c', seqno);
1667 logf (LOG_LOG, "commit start");
1668 bf_commitExec (bfs);
1673 logf (LOG_LOG, "commit clean");
1674 bf_commitClean (bfs, rval);
1676 zebra_set_state (zh, 'o', seqno);
1680 logf (LOG_LOG, "nothing to commit");
1684 zebra_unlock (zh->lock_shadow);
1685 zebra_unlock (zh->lock_normal);
1689 int zebra_clean (ZebraHandle zh)
1691 yaz_log(LOG_API,"zebra_clean");
1692 return zebra_commit_ex(zh, 1);
1695 int zebra_commit (ZebraHandle zh)
1697 yaz_log(LOG_API,"zebra_commit");
1698 return zebra_commit_ex(zh, 0);
1701 int zebra_init (ZebraHandle zh)
1706 yaz_log(LOG_API,"zebra_init");
1714 rval = res_get (zh->res, "shadow");
1716 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1719 bf_cache (bfs, rval);
1723 zebra_set_state (zh, 'o', 0);
1727 int zebra_compact (ZebraHandle zh)
1731 yaz_log(LOG_API,"zebra_compact");
1738 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1744 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1747 yaz_log(LOG_API,"zebra_result");
1748 *code = zh->errCode;
1749 *addinfo = zh->errString;
1753 int zebra_shadow_enable (ZebraHandle zh, int value)
1756 yaz_log(LOG_API,"zebra_shadow_enable");
1758 zh->shadow_enable = value;
1762 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1765 yaz_log(LOG_API,"zebra_record_encoding");
1767 xfree (zh->record_encoding);
1771 * Something about charset aliases. Oleg???
1774 if (zh->iconv_to_utf8 != 0)
1775 yaz_iconv_close(zh->iconv_to_utf8);
1776 if (zh->iconv_from_utf8 != 0)
1777 yaz_iconv_close(zh->iconv_from_utf8);
1779 zh->record_encoding = xstrdup (encoding);
1781 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1784 yaz_iconv_open ("UTF-8", encoding);
1785 if (zh->iconv_to_utf8 == 0)
1786 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1787 zh->iconv_from_utf8 =
1788 yaz_iconv_open (encoding, "UTF-8");
1789 if (zh->iconv_to_utf8 == 0)
1790 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1795 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1798 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1800 res_set(zh->res, name, value);
1804 const char *zebra_get_resource(ZebraHandle zh,
1805 const char *name, const char *defaultvalue)
1809 v= res_get_def( zh->res, name, (char *)defaultvalue);
1811 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1815 /* moved from zebra_api_ext.c by pop */
1816 /* FIXME: Should this really be public??? -Heikki */
1818 int zebra_trans_no (ZebraHandle zh)
1821 yaz_log(LOG_API,"zebra_trans_no");
1822 return zh->trans_no;
1825 int zebra_get_shadow_enable (ZebraHandle zh)
1827 yaz_log(LOG_API,"zebra_get_shadow_enable");
1828 return (zh->shadow_enable);
1831 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1833 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1834 zh->shadow_enable = value;
1838 /* almost the same as zebra_records_retrieve ... but how did it work?
1839 I mean for multiple records ??? CHECK ??? */
1840 void api_records_retrieve (ZebraHandle zh, ODR stream,
1841 const char *setname, Z_RecordComposition *comp,
1842 oid_value input_format, int num_recs,
1843 ZebraRetrievalRecord *recs)
1847 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1852 zh->errString = odr_strdup (stream, setname);
1858 if (zebra_begin_read (zh))
1861 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1862 for (i = 0; i<num_recs; i++)
1863 pos_array[i] = recs[i].position;
1864 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1867 logf (LOG_DEBUG, "zebraPosSetCreate error");
1869 zh->errString = nmem_strdup (stream->mem, setname);
1873 for (i = 0; i<num_recs; i++)
1877 recs[i].errCode = 0;
1878 recs[i].format = VAL_SUTRS;
1879 recs[i].len = strlen(poset[i].term);
1880 recs[i].buf = poset[i].term;
1881 recs[i].base = poset[i].db;
1885 else if (poset[i].sysno)
1887 /* changed here ??? CHECK ??? */
1890 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1891 stream, input_format, comp,
1896 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1897 memcpy(recs[i].buf, b, recs[i].len);
1898 recs[i].errString = 0; /* Hmmm !!! we should get this */
1899 recs[i].sysno = poset[i].sysno;
1900 recs[i].score = poset[i].score;
1906 sprintf (num_str, "%d", pos_array[i]);
1908 zh->errString = odr_strdup (stream, num_str);
1913 zebraPosSetDestroy (zh, poset, num_recs);
1915 zebra_end_read (zh);
1920 /* ---------------------------------------------------------------------------
1921 Record insert(=update), delete
1923 If sysno is provided, then it's used to identify the record.
1924 If not, and match_criteria is provided, then sysno is guessed
1925 If not, and a record is provided, then sysno is got from there
1926 NOTE: Now returns 0 at success and updates sysno, which is an int*
1930 int zebra_add_record(ZebraHandle zh,
1931 const char *buf, int buf_size)
1934 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1937 int zebra_insert_record (ZebraHandle zh,
1938 const char *recordType,
1939 SYSNO *sysno, const char *match, const char *fname,
1940 const char *buf, int buf_size, int force_update)
1943 yaz_log(LOG_API,"zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
1945 if (buf_size < 1) buf_size = strlen(buf);
1947 if (zebra_begin_trans(zh, 1))
1949 res = buffer_extract_record (zh, buf, buf_size,
1950 0, /* delete_flag */
1956 0); /* allow_update */
1957 zebra_end_trans(zh);
1961 int zebra_update_record (ZebraHandle zh,
1962 const char *recordType,
1963 SYSNO* sysno, const char *match, const char *fname,
1964 const char *buf, int buf_size,
1969 yaz_log(LOG_API,"zebra_update_record sysno=" ZINT_FORMAT, *sysno);
1971 if (buf_size < 1) buf_size = strlen(buf);
1973 if (zebra_begin_trans(zh, 1))
1975 res = buffer_extract_record (zh, buf, buf_size,
1976 0, /* delete_flag */
1982 1); /* allow_update */
1983 zebra_end_trans(zh);
1987 int zebra_delete_record (ZebraHandle zh,
1988 const char *recordType,
1989 SYSNO *sysno, const char *match, const char *fname,
1990 const char *buf, int buf_size,
1994 yaz_log(LOG_API,"zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
1996 if (buf_size < 1) buf_size = strlen(buf);
1998 if (zebra_begin_trans(zh, 1))
2000 res = buffer_extract_record (zh, buf, buf_size,
2001 1, /* delete_flag */
2007 1); /* allow_update */
2008 zebra_end_trans(zh);
2012 /* ---------------------------------------------------------------------------
2016 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2017 const char *setname, int *numhits)
2022 ODR odr = odr_createmem(ODR_ENCODE);
2024 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2026 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2029 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2031 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2035 yaz_log(LOG_API,"Hits: %d",hits);
2043 /* ---------------------------------------------------------------------------
2044 Sort - a simplified interface, with optional read locks.
2045 FIXME - This is a horrible name, will conflict with half the applications
2047 int zebra_sort_by_specstr (ZebraHandle zh,
2049 const char *sort_spec,
2050 const char *output_setname,
2051 const char **input_setnames)
2053 int num_input_setnames = 0;
2054 int sort_status = 0;
2055 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2056 yaz_log(LOG_API,"sort (FIXME) ");
2059 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2064 /* we can do this, since the perl typemap code for char** will
2065 put a NULL at the end of list */
2066 while (input_setnames[num_input_setnames]) num_input_setnames++;
2068 if (zebra_begin_read (zh))
2071 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2072 output_setname, sort_sequence, &sort_status);