1 /* $Id: zebraapi.c,v 1.119 2004-05-10 08:47:54 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, Res def_res, Res over_res)
154 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
156 if ((res = res_open (configName, def_res, over_res)))
158 ZebraService zh = xmalloc (sizeof(*zh));
160 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
162 zh->global_res = res;
163 zh->configName = xstrdup(configName);
168 zebra_mutex_cond_init (&zh->session_lock);
169 if (!res_get (zh->global_res, "passwd"))
170 zh->passwd_db = NULL;
173 zh->passwd_db = passwd_db_open ();
175 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
177 passwd_db_file (zh->passwd_db,
178 res_get (zh->global_res, "passwd"));
180 zh->path_root = res_get (zh->global_res, "root");
187 void zebra_pidfname(ZebraService zs, char *path)
189 zebra_lock_prefix (zs->global_res, path);
190 strcat(path, "zebrasrv.pid");
194 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
195 int rw, int useshadow, Res res,
196 const char *reg_path)
198 struct zebra_register *reg;
199 int record_compression = REC_COMPRESS_NONE;
200 const char *recordCompression = 0;
201 const char *profilePath;
206 reg = xmalloc (sizeof(*reg));
209 reg->name = xstrdup (name);
216 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
217 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
219 reg->dh = data1_createx (DATA1_FLAG_XML);
222 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
225 data1_destroy(reg->dh);
229 bf_cache (reg->bfs, res_get (res, "shadow"));
231 getcwd(cwd, sizeof(cwd)-1);
232 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
233 yaz_log(LOG_LOG, "profilePath=%s cwd=%s", profilePath, cwd);
235 data1_set_tabpath (reg->dh, profilePath);
236 data1_set_tabroot (reg->dh, reg_path);
237 reg->recTypes = recTypes_init (reg->dh);
238 recTypes_default_handlers (reg->recTypes);
240 reg->zebra_maps = zebra_maps_open (res, reg_path);
241 reg->rank_classes = NULL;
245 reg->keys.buf_max = 0;
247 reg->sortKeys.buf = 0;
248 reg->sortKeys.buf_max = 0;
261 reg->key_file_no = 0;
264 zebraRankInstall (reg, rank1_class);
265 zebraRankInstall (reg, rankzv_class);
266 zebraRankInstall (reg, rankliv_class);
268 recordCompression = res_get_def (res, "recordCompression", "none");
269 if (!strcmp (recordCompression, "none"))
270 record_compression = REC_COMPRESS_NONE;
271 if (!strcmp (recordCompression, "bzip2"))
272 record_compression = REC_COMPRESS_BZIP2;
274 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
276 logf (LOG_WARN, "rec_open");
281 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
283 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
285 logf (LOG_WARN, "dict_open");
288 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
290 logf (LOG_WARN, "sortIdx_open");
293 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
295 struct ISAMS_M_s isams_m;
296 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
297 key_isams_m(res, &isams_m))))
299 logf (LOG_WARN, "isams_open");
303 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
305 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
306 sizeof (struct it_key), res)))
308 logf (LOG_WARN, "is_open");
312 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
314 struct ISAMC_M_s isamc_m;
315 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
316 rw, key_isamc_m(res, &isamc_m))))
318 logf (LOG_WARN, "isc_open");
322 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
324 struct ISAMD_M_s isamd_m;
326 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
327 rw, key_isamd_m(res, &isamd_m))))
329 logf (LOG_WARN, "isamd_open");
333 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
335 struct ISAMC_M_s isamc_m;
337 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
338 rw, key_isamc_m(res, &isamc_m), 0)))
340 logf (LOG_WARN, "isamb_open");
344 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
346 struct ISAMC_M_s isamc_m;
348 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
349 rw, key_isamc_m(res, &isamc_m), 1)))
351 logf (LOG_WARN, "isamb_open");
355 if (res_get_match (res, "isam", "null", 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), -1)))
362 logf (LOG_WARN, "isamb_open");
366 reg->zei = zebraExplain_open (reg->records, reg->dh,
371 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
375 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
379 int zebra_admin_shutdown (ZebraHandle zh)
382 yaz_log(LOG_API,"zebra_admin_shutdown");
385 zebra_mutex_cond_lock (&zh->service->session_lock);
386 zh->service->stop_flag = 1;
387 zebra_mutex_cond_unlock (&zh->service->session_lock);
391 int zebra_admin_start (ZebraHandle zh)
395 yaz_log(LOG_API,"zebra_admin_start");
398 zebra_mutex_cond_lock (&zs->session_lock);
399 zebra_mutex_cond_unlock (&zs->session_lock);
403 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
406 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
411 zebraExplain_close (reg->zei);
412 dict_close (reg->dict);
414 dict_close (reg->matchDict);
415 sortIdx_close (reg->sortIdx);
417 isams_close (reg->isams);
419 is_close (reg->isam);
421 isc_close (reg->isamc);
423 isamd_close (reg->isamd);
425 isamb_close (reg->isamb);
426 rec_close (®->records);
429 recTypes_destroy (reg->recTypes);
430 zebra_maps_close (reg->zebra_maps);
431 zebraRankDestroy (reg);
432 bfs_destroy (reg->bfs);
433 data1_destroy (reg->dh);
435 xfree (reg->sortKeys.buf);
436 xfree (reg->keys.buf);
438 xfree (reg->key_buf);
443 int zebra_stop(ZebraService zs)
447 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
451 zebra_close (zs->sessions);
454 zebra_mutex_cond_destroy (&zs->session_lock);
457 passwd_db_close (zs->passwd_db);
459 res_close (zs->global_res);
460 xfree (zs->configName);
465 int zebra_close (ZebraHandle zh)
468 struct zebra_session **sp;
471 yaz_log(LOG_API,"zebra_close");
478 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
479 resultSetDestroy (zh, -1, 0, 0);
482 zebra_register_close (zh->service, zh->reg);
483 zebra_close_res (zh);
485 xfree (zh->record_encoding);
487 for (i = 0; i < zh->num_basenames; i++)
488 xfree (zh->basenames[i]);
489 xfree (zh->basenames);
491 if (zh->iconv_to_utf8 != 0)
492 yaz_iconv_close (zh->iconv_to_utf8);
493 if (zh->iconv_from_utf8 != 0)
494 yaz_iconv_close (zh->iconv_from_utf8);
496 zebra_mutex_cond_lock (&zs->session_lock);
497 zebra_lock_destroy (zh->lock_normal);
498 zebra_lock_destroy (zh->lock_shadow);
510 zebra_mutex_cond_unlock (&zs->session_lock);
511 xfree (zh->reg_name);
512 xfree (zh->user_perm);
513 zh->service=0; /* more likely to trigger an assert */
514 xfree (zh->path_reg);
519 struct map_baseinfo {
525 char **new_basenames;
529 static Res zebra_open_res (ZebraHandle zh)
538 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
539 res = res_open (fname, zh->service->global_res, 0);
541 res = zh->service->global_res;
543 else if (*zh->reg_name == 0)
545 res = zh->service->global_res;
549 yaz_log (LOG_WARN, "no register root specified");
550 return 0; /* no path for register - fail! */
555 static void zebra_close_res (ZebraHandle zh)
559 if (zh->res != zh->service->global_res)
564 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
568 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
572 assert (zh->reg == 0);
573 assert (*zh->reg_name == 0);
579 resultSetInvalidate (zh);
580 zebra_register_close (zh->service, zh->reg);
585 xfree (zh->reg_name);
586 zh->reg_name = xstrdup (new_reg);
588 xfree (zh->path_reg);
590 if (zh->service->path_root)
592 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
593 strlen(zh->reg_name) + 3);
594 strcpy (zh->path_reg, zh->service->path_root);
597 strcat (zh->path_reg, "/");
598 strcat (zh->path_reg, zh->reg_name);
601 zh->res = zebra_open_res (zh);
604 zebra_lock_destroy (zh->lock_normal);
608 zebra_lock_destroy (zh->lock_shadow);
614 const char *lock_area =res_get (zh->res, "lockDir");
616 if (!lock_area && zh->path_reg)
617 res_set (zh->res, "lockDir", zh->path_reg);
618 sprintf (fname, "norm.%s.LCK", zh->reg_name);
620 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
622 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
624 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
630 void map_basenames_func (void *vp, const char *name, const char *value)
632 struct map_baseinfo *p = (struct map_baseinfo *) vp;
634 char fromdb[128], todb[8][128];
637 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
638 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
639 todb[5], todb[6], todb[7]);
643 for (i = 0; i<p->num_bases; i++)
644 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
647 for (i = 0; i < no; i++)
649 if (p->new_num_bases == p->new_num_max)
651 p->new_basenames[(p->new_num_bases)++] =
652 nmem_strdup (p->mem, todb[i]);
658 void map_basenames (ZebraHandle zh, ODR stream,
659 int *num_bases, char ***basenames)
661 struct map_baseinfo info;
662 struct map_baseinfo *p = &info;
665 yaz_log(LOG_API,"map_basenames ");
669 info.num_bases = *num_bases;
670 info.basenames = *basenames;
671 info.new_num_max = 128;
672 info.new_num_bases = 0;
673 info.new_basenames = (char **)
674 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
675 info.mem = stream->mem;
677 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
679 for (i = 0; i<p->num_bases; i++)
680 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
682 p->new_basenames[(p->new_num_bases)++] =
683 nmem_strdup (p->mem, p->basenames[i]);
685 *num_bases = info.new_num_bases;
686 *basenames = info.new_basenames;
687 for (i = 0; i<*num_bases; i++)
688 logf (LOG_LOG, "base %s", (*basenames)[i]);
691 int zebra_select_database (ZebraHandle zh, const char *basename)
694 yaz_log(LOG_API,"zebra_select_database %s",basename);
696 return zebra_select_databases (zh, 1, &basename);
699 int zebra_select_databases (ZebraHandle zh, int num_bases,
700 const char **basenames)
707 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
708 num_bases,basenames[0]);
716 for (i = 0; i < zh->num_basenames; i++)
717 xfree (zh->basenames[i]);
718 xfree (zh->basenames);
720 zh->num_basenames = num_bases;
721 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
722 for (i = 0; i < zh->num_basenames; i++)
723 zh->basenames[i] = xstrdup (basenames[i]);
725 cp = strrchr(basenames[0], '/');
728 len = cp - basenames[0];
729 new_reg = xmalloc (len + 1);
730 memcpy (new_reg, basenames[0], len);
734 new_reg = xstrdup ("");
735 for (i = 1; i<num_bases; i++)
739 cp1 = strrchr (basenames[i], '/');
747 if (len != cp1 - basenames[i] ||
748 memcmp (basenames[i], new_reg, len))
763 zebra_select_register (zh, new_reg);
770 if (!zh->lock_normal || !zh->lock_shadow)
778 int zebra_search_RPN (ZebraHandle zh, ODR o,
779 Z_RPNQuery *query, const char *setname, int *hits)
782 yaz_log(LOG_API,"zebra_search_rpn");
787 if (zebra_begin_read (zh))
790 zebra_livcode_transform(zh, query);
792 resultSetAddRPN (zh, odr_extract_mem(o), query,
793 zh->num_basenames, zh->basenames, setname);
801 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
802 const char *setname, Z_RecordComposition *comp,
803 oid_value input_format, int num_recs,
804 ZebraRetrievalRecord *recs)
807 int i, *pos_array, ret = 0;
809 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
815 zh->errString = odr_strdup (stream, setname);
821 if (zebra_begin_read (zh))
824 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
825 for (i = 0; i<num_recs; i++)
826 pos_array[i] = recs[i].position;
827 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
830 logf (LOG_DEBUG, "zebraPosSetCreate error");
832 zh->errString = nmem_strdup (stream->mem, setname);
837 for (i = 0; i<num_recs; i++)
842 recs[i].format = VAL_SUTRS;
843 recs[i].len = strlen(poset[i].term);
844 recs[i].buf = poset[i].term;
845 recs[i].base = poset[i].db;
847 else if (poset[i].sysno)
850 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
851 stream, input_format, comp,
852 &recs[i].format, &recs[i].buf,
855 recs[i].errString = NULL;
861 sprintf (num_str, "%d", pos_array[i]);
863 zh->errString = odr_strdup (stream, num_str);
868 zebraPosSetDestroy (zh, poset, num_recs);
875 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
876 oid_value attributeset,
877 int *position, int *num_entries, ZebraScanEntry **entries,
881 yaz_log(LOG_API,"zebra_scan");
883 if (zebra_begin_read (zh))
889 rpn_scan (zh, stream, zapt, attributeset,
890 zh->num_basenames, zh->basenames, position,
891 num_entries, entries, is_partial, 0, 0);
896 int zebra_sort (ZebraHandle zh, ODR stream,
897 int num_input_setnames, const char **input_setnames,
898 const char *output_setname, Z_SortKeySpecList *sort_sequence,
902 yaz_log(LOG_API,"zebra_sort");
904 if (zebra_begin_read (zh))
906 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
907 output_setname, sort_sequence, sort_status);
912 int zebra_deleleResultSet(ZebraHandle zh, int function,
913 int num_setnames, char **setnames,
918 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
920 if (zebra_begin_read(zh))
921 return Z_DeleteStatus_systemProblemAtTarget;
924 case Z_DeleteResultSetRequest_list:
925 resultSetDestroy (zh, num_setnames, setnames, statuses);
927 case Z_DeleteResultSetRequest_all:
928 resultSetDestroy (zh, -1, 0, statuses);
932 status = Z_DeleteStatus_success;
933 for (i = 0; i<num_setnames; i++)
934 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
935 status = statuses[i];
939 int zebra_errCode (ZebraHandle zh)
943 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
946 yaz_log(LOG_API,"zebra_errCode: o");
950 const char *zebra_errString (ZebraHandle zh)
954 e= diagbib1_str (zh->errCode);
955 yaz_log(LOG_API,"zebra_errString: %s",e);
959 char *zebra_errAdd (ZebraHandle zh)
964 yaz_log(LOG_API,"zebra_errAdd: %s",a);
968 void zebra_clearError(ZebraHandle zh)
977 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
987 sprintf(u, "perm.%.30s", user ? user : "anonymous");
988 p = res_get(zs->global_res, u);
989 xfree (zh->user_perm);
990 zh->user_perm = xstrdup(p ? p : "r");
992 /* users that don't require a password .. */
993 if (zh->user_perm && strchr(zh->user_perm, 'a'))
996 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1001 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
1002 const char *record_type)
1005 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
1006 database, record_type);
1008 if (zebra_select_database(zh, database))
1010 if (zebra_begin_trans (zh, 1))
1015 int zebra_admin_import_end (ZebraHandle zh)
1018 yaz_log(LOG_API,"zebra_admin_import_end");
1020 zebra_end_trans (zh);
1024 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1029 yaz_log(LOG_API,"zebra_admin_import_segment");
1031 for (i = 0; i<segment->num_segmentRecords; i++)
1033 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1035 printf ("--------------%d--------------------\n", i);
1036 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1038 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1039 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1041 Odr_oct *oct = fragment->u.notExternallyTagged;
1042 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1047 zebra_update_record(zh,
1048 0, /* record Type */
1060 int zebra_admin_exchange_record (ZebraHandle zh,
1061 const char *rec_buf,
1063 const char *recid_buf, size_t recid_len,
1065 /* 1 = insert. Fail it already exists */
1066 /* 2 = replace. Fail it does not exist */
1067 /* 3 = delete. Fail if does not exist */
1068 /* 4 = update. Insert/replace */
1074 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1077 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1079 memcpy (recid_z, recid_buf, recid_len);
1080 recid_z[recid_len] = 0;
1082 if (zebra_begin_trans(zh, 1))
1085 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1088 if (action == 1) /* fail if insert */
1090 zebra_end_trans(zh);
1094 memcpy (&sysno, rinfo+1, sizeof(sysno));
1098 if (action == 2 || action == 3) /* fail if delete or update */
1100 zebra_end_trans(zh);
1103 action = 1; /* make it an insert (if it's an update).. */
1105 buffer_extract_record (zh, rec_buf, rec_len,
1106 action == 3 ? 1 : 0 /* delete flag */,
1112 0, /* force update */
1113 1 /* allow update */
1117 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1119 else if (action == 3)
1121 dict_delete (zh->reg->matchDict, recid_z);
1123 zebra_end_trans(zh);
1127 int delete_w_handle(const char *info, void *handle)
1129 ZebraHandle zh = (ZebraHandle) handle;
1132 if (*info == sizeof(pos))
1134 memcpy (&pos, info+1, sizeof(pos));
1135 isamb_unlink(zh->reg->isamb, pos);
1140 static int delete_SU_handle(void *handle, int ord)
1142 ZebraHandle zh = (ZebraHandle) handle;
1146 ord_len = key_SU_encode (ord, ord_buf);
1147 ord_buf[ord_len] = '\0';
1149 assert (zh->reg->isamb);
1150 dict_delete_subtree(zh->reg->dict, ord_buf,
1151 zh, delete_w_handle);
1155 int zebra_drop_database (ZebraHandle zh, const char *database)
1159 yaz_log(LOG_API,"zebra_drop_database");
1162 if (zebra_select_database (zh, database))
1164 if (zebra_begin_trans (zh, 1))
1168 zebraExplain_curDatabase (zh->reg->zei, database);
1170 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1171 zebraExplain_removeDatabase(zh->reg->zei, zh);
1175 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1178 zebra_end_trans (zh);
1182 int zebra_create_database (ZebraHandle zh, const char *database)
1185 yaz_log(LOG_API,"zebra_create_database");
1188 if (zebra_select_database (zh, database))
1190 if (zebra_begin_trans (zh, 1))
1193 /* announce database */
1194 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1195 /* explainDatabase */))
1197 zebra_end_trans (zh);
1199 zh->errString = "database already exist";
1202 zebra_end_trans (zh);
1206 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1207 const char *input_str, int input_len,
1208 char *output_str, int output_len)
1212 yaz_log(LOG_API,"zebra_string_norm ");
1214 if (!zh->reg->zebra_maps)
1216 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1217 input_str, input_len);
1220 if (wrbuf_len(wrbuf) >= output_len)
1222 if (wrbuf_len(wrbuf))
1223 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1224 output_str[wrbuf_len(wrbuf)] = '\0';
1225 return wrbuf_len(wrbuf);
1229 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1231 char state_fname[256];
1236 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1239 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1240 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1241 f = fopen (fname, "w");
1243 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1244 fprintf (f, "%c %d %ld\n", val, seqno, p);
1250 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1252 char state_fname[256];
1257 yaz_log(LOG_API,"zebra_get_state ");
1259 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1260 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1261 f = fopen (fname, "r");
1267 fscanf (f, "%c %d", val, seqno);
1274 int zebra_begin_read (ZebraHandle zh)
1276 return zebra_begin_trans(zh, 0);
1279 int zebra_end_read (ZebraHandle zh)
1281 return zebra_end_trans(zh);
1284 static void read_res_for_transaction(ZebraHandle zh)
1286 const char *group = res_get(zh->res, "group");
1289 zh->m_group = group;
1290 v = res_get_prefix(zh->res, "followLinks", group, "1");
1291 zh->m_follow_links = atoi(v);
1293 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1294 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1296 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1297 zh->m_store_keys = atoi(v);
1299 v = res_get_prefix(zh->res, "storeData", group, "1");
1300 zh->m_store_data = atoi(v);
1302 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1303 zh->m_explain_database = atoi(v);
1305 v = res_get_prefix(zh->res, "openRW", group, "1");
1306 zh->m_flag_rw = atoi(v);
1308 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1309 zh->m_file_verbose_limit = atoi(v);
1312 int zebra_begin_trans (ZebraHandle zh, int rw)
1317 zh->errString = "zebra_begin_trans: no database selected";
1321 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1325 if (rw && !strchr(zh->user_perm, 'w'))
1339 const char *rval = 0;
1344 if (zh->trans_no != 1)
1347 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1352 resultSetInvalidate (zh);
1353 zebra_register_close (zh->service, zh->reg);
1355 zh->trans_w_no = zh->trans_no;
1359 zh->records_inserted = 0;
1360 zh->records_updated = 0;
1361 zh->records_deleted = 0;
1362 zh->records_processed = 0;
1364 #if HAVE_SYS_TIMES_H
1368 if (zh->shadow_enable)
1369 rval = res_get (zh->res, "shadow");
1371 for (pass = 0; pass < 2; pass++)
1375 zebra_lock_r (zh->lock_normal);
1376 zebra_lock_w (zh->lock_shadow);
1380 zebra_lock_w (zh->lock_normal);
1381 zebra_lock_w (zh->lock_shadow);
1384 zebra_get_state (zh, &val, &seqno);
1387 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1388 zebra_unlock (zh->lock_shadow);
1389 zebra_unlock (zh->lock_normal);
1393 else if (val == 'd')
1397 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1399 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1400 bf_commitClean (bfs, rval);
1405 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1412 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1416 zebra_set_state (zh, 'd', seqno);
1418 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1419 1, rval ? 1 : 0, zh->res,
1422 zh->reg->seqno = seqno;
1425 zebra_set_state (zh, 'o', seqno);
1427 zebra_unlock (zh->lock_shadow);
1428 zebra_unlock (zh->lock_normal);
1434 zh->errString = "zebra_begin_trans: cannot open register";
1435 yaz_log(LOG_FATAL, zh->errString);
1447 if (zh->trans_no != 1)
1449 zebra_flush_reg (zh);
1453 #if HAVE_SYS_TIMES_H
1462 if (!zh->lock_normal || !zh->lock_shadow)
1468 zebra_get_state (zh, &val, &seqno);
1474 else if (seqno != zh->reg->seqno)
1476 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1477 seqno, zh->reg->seqno);
1480 else if (zh->reg->last_val != val)
1482 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1483 val, zh->reg->last_val);
1490 zebra_lock_r (zh->lock_shadow);
1492 zebra_lock_r (zh->lock_normal);
1496 resultSetInvalidate (zh);
1497 zebra_register_close (zh->service, zh->reg);
1499 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1500 0, val == 'c' ? 1 : 0,
1501 zh->res, zh->path_reg);
1504 zebra_unlock (zh->lock_normal);
1505 zebra_unlock (zh->lock_shadow);
1510 zh->reg->last_val = val;
1511 zh->reg->seqno = seqno;
1513 read_res_for_transaction(zh);
1517 int zebra_end_trans (ZebraHandle zh)
1519 ZebraTransactionStatus dummy;
1520 yaz_log(LOG_API,"zebra_end_trans");
1521 return zebra_end_transaction(zh, &dummy);
1524 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1531 yaz_log(LOG_API,"zebra_end_transaction");
1533 status->processed = 0;
1534 status->inserted = 0;
1535 status->updated = 0;
1536 status->deleted = 0;
1540 if (!zh->res || !zh->reg)
1543 zh->errString = "zebra_end_trans: no open transaction";
1546 if (zh->trans_no != zh->trans_w_no)
1549 if (zh->trans_no != 0)
1552 /* release read lock */
1554 zebra_unlock (zh->lock_normal);
1555 zebra_unlock (zh->lock_shadow);
1558 { /* release write lock */
1562 yaz_log (LOG_LOG, "zebra_end_trans");
1563 rval = res_get (zh->res, "shadow");
1565 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1567 zebra_flush_reg (zh);
1569 resultSetInvalidate (zh);
1571 zebra_register_close (zh->service, zh->reg);
1574 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1575 zh->records_processed, zh->records_inserted,
1576 zh->records_updated, zh->records_deleted);
1578 status->processed = zh->records_processed;
1579 status->inserted = zh->records_inserted;
1580 status->updated = zh->records_updated;
1581 status->deleted = zh->records_deleted;
1583 zebra_get_state (zh, &val, &seqno);
1586 BFiles bfs = bfs_create (rval, zh->path_reg);
1587 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1588 bf_commitClean (bfs, rval);
1593 zebra_set_state (zh, 'o', seqno);
1595 zebra_unlock (zh->lock_shadow);
1596 zebra_unlock (zh->lock_normal);
1599 #if HAVE_SYS_TIMES_H
1601 logf (LOG_LOG, "user/system: %ld/%ld",
1602 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1603 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1605 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1606 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1611 int zebra_repository_update (ZebraHandle zh, const char *path)
1615 logf (LOG_LOG|LOG_API, "updating %s", path);
1616 repositoryUpdate (zh, path);
1620 int zebra_repository_delete (ZebraHandle zh, const char *path)
1624 logf (LOG_LOG|LOG_API, "deleting %s", path);
1625 repositoryDelete (zh, path);
1629 int zebra_repository_show (ZebraHandle zh, const char *path)
1632 yaz_log(LOG_API,"zebra_repository_show");
1634 repositoryShow (zh, path);
1638 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1652 rval = res_get (zh->res, "shadow");
1655 logf (LOG_WARN, "Cannot perform commit");
1656 logf (LOG_WARN, "No shadow area defined");
1660 zebra_lock_w (zh->lock_normal);
1661 zebra_lock_r (zh->lock_shadow);
1663 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1665 zebra_get_state (zh, &val, &seqno);
1668 bf_cache (bfs, rval);
1669 if (bf_commitExists (bfs))
1672 zebra_set_state (zh, 'd', seqno);
1675 zebra_set_state (zh, 'c', seqno);
1677 logf (LOG_LOG, "commit start");
1678 bf_commitExec (bfs);
1683 logf (LOG_LOG, "commit clean");
1684 bf_commitClean (bfs, rval);
1686 zebra_set_state (zh, 'o', seqno);
1690 logf (LOG_LOG, "nothing to commit");
1694 zebra_unlock (zh->lock_shadow);
1695 zebra_unlock (zh->lock_normal);
1699 int zebra_clean (ZebraHandle zh)
1701 yaz_log(LOG_API,"zebra_clean");
1702 return zebra_commit_ex(zh, 1);
1705 int zebra_commit (ZebraHandle zh)
1707 yaz_log(LOG_API,"zebra_commit");
1708 return zebra_commit_ex(zh, 0);
1711 int zebra_init (ZebraHandle zh)
1716 yaz_log(LOG_API,"zebra_init");
1724 rval = res_get (zh->res, "shadow");
1726 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1729 bf_cache (bfs, rval);
1733 zebra_set_state (zh, 'o', 0);
1737 int zebra_compact (ZebraHandle zh)
1741 yaz_log(LOG_API,"zebra_compact");
1748 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1754 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1757 yaz_log(LOG_API,"zebra_result");
1758 *code = zh->errCode;
1759 *addinfo = zh->errString;
1763 int zebra_shadow_enable (ZebraHandle zh, int value)
1766 yaz_log(LOG_API,"zebra_shadow_enable");
1768 zh->shadow_enable = value;
1772 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1775 yaz_log(LOG_API,"zebra_record_encoding");
1777 xfree (zh->record_encoding);
1781 * Something about charset aliases. Oleg???
1784 if (zh->iconv_to_utf8 != 0)
1785 yaz_iconv_close(zh->iconv_to_utf8);
1786 if (zh->iconv_from_utf8 != 0)
1787 yaz_iconv_close(zh->iconv_from_utf8);
1789 zh->record_encoding = xstrdup (encoding);
1791 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1794 yaz_iconv_open ("UTF-8", encoding);
1795 if (zh->iconv_to_utf8 == 0)
1796 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1797 zh->iconv_from_utf8 =
1798 yaz_iconv_open (encoding, "UTF-8");
1799 if (zh->iconv_to_utf8 == 0)
1800 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1805 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1808 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1810 res_set(zh->res, name, value);
1814 const char *zebra_get_resource(ZebraHandle zh,
1815 const char *name, const char *defaultvalue)
1819 v= res_get_def( zh->res, name, (char *)defaultvalue);
1821 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1825 /* moved from zebra_api_ext.c by pop */
1826 /* FIXME: Should this really be public??? -Heikki */
1828 int zebra_trans_no (ZebraHandle zh)
1831 yaz_log(LOG_API,"zebra_trans_no");
1832 return zh->trans_no;
1835 int zebra_get_shadow_enable (ZebraHandle zh)
1837 yaz_log(LOG_API,"zebra_get_shadow_enable");
1838 return (zh->shadow_enable);
1841 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1843 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1844 zh->shadow_enable = value;
1848 /* almost the same as zebra_records_retrieve ... but how did it work?
1849 I mean for multiple records ??? CHECK ??? */
1850 void api_records_retrieve (ZebraHandle zh, ODR stream,
1851 const char *setname, Z_RecordComposition *comp,
1852 oid_value input_format, int num_recs,
1853 ZebraRetrievalRecord *recs)
1857 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1862 zh->errString = odr_strdup (stream, setname);
1868 if (zebra_begin_read (zh))
1871 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1872 for (i = 0; i<num_recs; i++)
1873 pos_array[i] = recs[i].position;
1874 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1877 logf (LOG_DEBUG, "zebraPosSetCreate error");
1879 zh->errString = nmem_strdup (stream->mem, setname);
1883 for (i = 0; i<num_recs; i++)
1887 recs[i].errCode = 0;
1888 recs[i].format = VAL_SUTRS;
1889 recs[i].len = strlen(poset[i].term);
1890 recs[i].buf = poset[i].term;
1891 recs[i].base = poset[i].db;
1895 else if (poset[i].sysno)
1897 /* changed here ??? CHECK ??? */
1900 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1901 stream, input_format, comp,
1906 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1907 memcpy(recs[i].buf, b, recs[i].len);
1908 recs[i].errString = 0; /* Hmmm !!! we should get this */
1909 recs[i].sysno = poset[i].sysno;
1910 recs[i].score = poset[i].score;
1916 sprintf (num_str, "%d", pos_array[i]);
1918 zh->errString = odr_strdup (stream, num_str);
1923 zebraPosSetDestroy (zh, poset, num_recs);
1925 zebra_end_read (zh);
1930 /* ---------------------------------------------------------------------------
1931 Record insert(=update), delete
1933 If sysno is provided, then it's used to identify the record.
1934 If not, and match_criteria is provided, then sysno is guessed
1935 If not, and a record is provided, then sysno is got from there
1936 NOTE: Now returns 0 at success and updates sysno, which is an int*
1940 int zebra_add_record(ZebraHandle zh,
1941 const char *buf, int buf_size)
1944 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1947 int zebra_insert_record (ZebraHandle zh,
1948 const char *recordType,
1949 int *sysno, const char *match, const char *fname,
1950 const char *buf, int buf_size)
1953 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
1955 if (buf_size < 1) buf_size = strlen(buf);
1957 if (zebra_begin_trans(zh, 1))
1959 res = buffer_extract_record (zh, buf, buf_size,
1960 0, /* delete_flag */
1966 0); /* allow_update */
1967 zebra_end_trans(zh);
1971 int zebra_update_record (ZebraHandle zh,
1972 const char *recordType,
1973 int* sysno, const char *match, const char *fname,
1974 const char *buf, int buf_size,
1979 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
1981 if (buf_size < 1) buf_size = strlen(buf);
1983 if (zebra_begin_trans(zh, 1))
1985 res = buffer_extract_record (zh, buf, buf_size,
1986 0, /* delete_flag */
1992 1); /* allow_update */
1993 zebra_end_trans(zh);
1997 int zebra_delete_record (ZebraHandle zh,
1998 const char *recordType,
1999 int *sysno, const char *match, const char *fname,
2000 const char *buf, int buf_size,
2004 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2006 if (buf_size < 1) buf_size = strlen(buf);
2008 if (zebra_begin_trans(zh, 1))
2010 res = buffer_extract_record (zh, buf, buf_size,
2011 1, /* delete_flag */
2017 1); /* allow_update */
2018 zebra_end_trans(zh);
2022 /* ---------------------------------------------------------------------------
2026 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2027 const char *setname, int *numhits)
2032 ODR odr = odr_createmem(ODR_ENCODE);
2034 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2036 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2039 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2041 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2045 yaz_log(LOG_API,"Hits: %d",hits);
2053 /* ---------------------------------------------------------------------------
2054 Sort - a simplified interface, with optional read locks.
2055 FIXME - This is a horrible name, will conflict with half the applications
2057 int zebra_sort_by_specstr (ZebraHandle zh,
2059 const char *sort_spec,
2060 const char *output_setname,
2061 const char **input_setnames)
2063 int num_input_setnames = 0;
2064 int sort_status = 0;
2065 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2066 yaz_log(LOG_API,"sort (FIXME) ");
2069 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2074 /* we can do this, since the perl typemap code for char** will
2075 put a NULL at the end of list */
2076 while (input_setnames[num_input_setnames]) num_input_setnames++;
2078 if (zebra_begin_read (zh))
2081 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2082 output_setname, sort_sequence, &sort_status);