1 /* $Id: zebraapi.c,v 1.121 2004-08-04 08:35:23 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;
269 reg->key_file_no = 0;
272 zebraRankInstall (reg, rank1_class);
273 zebraRankInstall (reg, rankzv_class);
274 zebraRankInstall (reg, rankliv_class);
276 recordCompression = res_get_def (res, "recordCompression", "none");
277 if (!strcmp (recordCompression, "none"))
278 record_compression = REC_COMPRESS_NONE;
279 if (!strcmp (recordCompression, "bzip2"))
280 record_compression = REC_COMPRESS_BZIP2;
282 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
284 logf (LOG_WARN, "rec_open");
289 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
291 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
293 logf (LOG_WARN, "dict_open");
296 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
298 logf (LOG_WARN, "sortIdx_open");
301 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
303 struct ISAMS_M_s isams_m;
304 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
305 key_isams_m(res, &isams_m))))
307 logf (LOG_WARN, "isams_open");
311 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
313 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
314 sizeof (struct it_key), res)))
316 logf (LOG_WARN, "is_open");
320 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
322 struct ISAMC_M_s isamc_m;
323 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
324 rw, key_isamc_m(res, &isamc_m))))
326 logf (LOG_WARN, "isc_open");
330 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
332 struct ISAMC_M_s isamc_m;
334 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
335 rw, key_isamc_m(res, &isamc_m), 0)))
337 logf (LOG_WARN, "isamb_open");
341 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
343 struct ISAMC_M_s isamc_m;
345 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
346 rw, key_isamc_m(res, &isamc_m), 1)))
348 logf (LOG_WARN, "isamb_open");
352 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
354 struct ISAMC_M_s isamc_m;
356 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
357 rw, key_isamc_m(res, &isamc_m), -1)))
359 logf (LOG_WARN, "isamb_open");
363 reg->zei = zebraExplain_open (reg->records, reg->dh,
368 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
372 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
376 int zebra_admin_shutdown (ZebraHandle zh)
379 yaz_log(LOG_API,"zebra_admin_shutdown");
382 zebra_mutex_cond_lock (&zh->service->session_lock);
383 zh->service->stop_flag = 1;
384 zebra_mutex_cond_unlock (&zh->service->session_lock);
388 int zebra_admin_start (ZebraHandle zh)
392 yaz_log(LOG_API,"zebra_admin_start");
395 zebra_mutex_cond_lock (&zs->session_lock);
396 zebra_mutex_cond_unlock (&zs->session_lock);
400 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
403 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
408 zebraExplain_close (reg->zei);
409 dict_close (reg->dict);
411 dict_close (reg->matchDict);
412 sortIdx_close (reg->sortIdx);
414 isams_close (reg->isams);
416 is_close (reg->isam);
418 isc_close (reg->isamc);
420 isamb_close (reg->isamb);
421 rec_close (®->records);
424 recTypes_destroy (reg->recTypes);
425 zebra_maps_close (reg->zebra_maps);
426 zebraRankDestroy (reg);
427 bfs_destroy (reg->bfs);
428 data1_destroy (reg->dh);
430 xfree (reg->sortKeys.buf);
431 xfree (reg->keys.buf);
433 if (reg->keys.codec_handle)
434 iscz1_stop(reg->keys.codec_handle);
437 xfree (reg->key_buf);
442 int zebra_stop(ZebraService zs)
446 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
450 zebra_close (zs->sessions);
453 zebra_mutex_cond_destroy (&zs->session_lock);
456 passwd_db_close (zs->passwd_db);
458 res_close (zs->global_res);
459 xfree (zs->configName);
464 int zebra_close (ZebraHandle zh)
467 struct zebra_session **sp;
470 yaz_log(LOG_API,"zebra_close");
477 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
478 resultSetDestroy (zh, -1, 0, 0);
481 zebra_register_close (zh->service, zh->reg);
482 zebra_close_res (zh);
484 xfree (zh->record_encoding);
486 for (i = 0; i < zh->num_basenames; i++)
487 xfree (zh->basenames[i]);
488 xfree (zh->basenames);
490 if (zh->iconv_to_utf8 != 0)
491 yaz_iconv_close (zh->iconv_to_utf8);
492 if (zh->iconv_from_utf8 != 0)
493 yaz_iconv_close (zh->iconv_from_utf8);
495 zebra_mutex_cond_lock (&zs->session_lock);
496 zebra_lock_destroy (zh->lock_normal);
497 zebra_lock_destroy (zh->lock_shadow);
509 zebra_mutex_cond_unlock (&zs->session_lock);
510 xfree (zh->reg_name);
511 xfree (zh->user_perm);
512 zh->service=0; /* more likely to trigger an assert */
513 xfree (zh->path_reg);
518 struct map_baseinfo {
524 char **new_basenames;
528 static Res zebra_open_res (ZebraHandle zh)
537 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
538 res = res_open (fname, zh->service->global_res, 0);
540 res = zh->service->global_res;
542 else if (*zh->reg_name == 0)
544 res = zh->service->global_res;
548 yaz_log (LOG_WARN, "no register root specified");
549 return 0; /* no path for register - fail! */
554 static void zebra_close_res (ZebraHandle zh)
558 if (zh->res != zh->service->global_res)
563 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
567 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
571 assert (zh->reg == 0);
572 assert (*zh->reg_name == 0);
578 resultSetInvalidate (zh);
579 zebra_register_close (zh->service, zh->reg);
584 xfree (zh->reg_name);
585 zh->reg_name = xstrdup (new_reg);
587 xfree (zh->path_reg);
589 if (zh->service->path_root)
591 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
592 strlen(zh->reg_name) + 3);
593 strcpy (zh->path_reg, zh->service->path_root);
596 strcat (zh->path_reg, "/");
597 strcat (zh->path_reg, zh->reg_name);
600 zh->res = zebra_open_res (zh);
603 zebra_lock_destroy (zh->lock_normal);
607 zebra_lock_destroy (zh->lock_shadow);
613 const char *lock_area =res_get (zh->res, "lockDir");
615 if (!lock_area && zh->path_reg)
616 res_set (zh->res, "lockDir", zh->path_reg);
617 sprintf (fname, "norm.%s.LCK", zh->reg_name);
619 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
621 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
623 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
629 void map_basenames_func (void *vp, const char *name, const char *value)
631 struct map_baseinfo *p = (struct map_baseinfo *) vp;
633 char fromdb[128], todb[8][128];
636 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
637 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
638 todb[5], todb[6], todb[7]);
642 for (i = 0; i<p->num_bases; i++)
643 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
646 for (i = 0; i < no; i++)
648 if (p->new_num_bases == p->new_num_max)
650 p->new_basenames[(p->new_num_bases)++] =
651 nmem_strdup (p->mem, todb[i]);
657 void map_basenames (ZebraHandle zh, ODR stream,
658 int *num_bases, char ***basenames)
660 struct map_baseinfo info;
661 struct map_baseinfo *p = &info;
664 yaz_log(LOG_API,"map_basenames ");
668 info.num_bases = *num_bases;
669 info.basenames = *basenames;
670 info.new_num_max = 128;
671 info.new_num_bases = 0;
672 info.new_basenames = (char **)
673 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
674 info.mem = stream->mem;
676 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
678 for (i = 0; i<p->num_bases; i++)
679 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
681 p->new_basenames[(p->new_num_bases)++] =
682 nmem_strdup (p->mem, p->basenames[i]);
684 *num_bases = info.new_num_bases;
685 *basenames = info.new_basenames;
686 for (i = 0; i<*num_bases; i++)
687 logf (LOG_LOG, "base %s", (*basenames)[i]);
690 int zebra_select_database (ZebraHandle zh, const char *basename)
693 yaz_log(LOG_API,"zebra_select_database %s",basename);
695 return zebra_select_databases (zh, 1, &basename);
698 int zebra_select_databases (ZebraHandle zh, int num_bases,
699 const char **basenames)
706 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
707 num_bases,basenames[0]);
715 for (i = 0; i < zh->num_basenames; i++)
716 xfree (zh->basenames[i]);
717 xfree (zh->basenames);
719 zh->num_basenames = num_bases;
720 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
721 for (i = 0; i < zh->num_basenames; i++)
722 zh->basenames[i] = xstrdup (basenames[i]);
724 cp = strrchr(basenames[0], '/');
727 len = cp - basenames[0];
728 new_reg = xmalloc (len + 1);
729 memcpy (new_reg, basenames[0], len);
733 new_reg = xstrdup ("");
734 for (i = 1; i<num_bases; i++)
738 cp1 = strrchr (basenames[i], '/');
746 if (len != cp1 - basenames[i] ||
747 memcmp (basenames[i], new_reg, len))
762 zebra_select_register (zh, new_reg);
769 if (!zh->lock_normal || !zh->lock_shadow)
777 int zebra_search_RPN (ZebraHandle zh, ODR o,
778 Z_RPNQuery *query, const char *setname, int *hits)
781 yaz_log(LOG_API,"zebra_search_rpn");
786 if (zebra_begin_read (zh))
789 zebra_livcode_transform(zh, query);
791 resultSetAddRPN (zh, odr_extract_mem(o), query,
792 zh->num_basenames, zh->basenames, setname);
800 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
801 const char *setname, Z_RecordComposition *comp,
802 oid_value input_format, int num_recs,
803 ZebraRetrievalRecord *recs)
806 int i, *pos_array, ret = 0;
808 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
814 zh->errString = odr_strdup (stream, setname);
820 if (zebra_begin_read (zh))
823 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
824 for (i = 0; i<num_recs; i++)
825 pos_array[i] = recs[i].position;
826 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
829 logf (LOG_DEBUG, "zebraPosSetCreate error");
831 zh->errString = nmem_strdup (stream->mem, setname);
836 for (i = 0; i<num_recs; i++)
841 recs[i].format = VAL_SUTRS;
842 recs[i].len = strlen(poset[i].term);
843 recs[i].buf = poset[i].term;
844 recs[i].base = poset[i].db;
846 else if (poset[i].sysno)
849 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
850 stream, input_format, comp,
851 &recs[i].format, &recs[i].buf,
854 recs[i].errString = NULL;
860 sprintf (num_str, "%d", pos_array[i]);
862 zh->errString = odr_strdup (stream, num_str);
867 zebraPosSetDestroy (zh, poset, num_recs);
874 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
875 oid_value attributeset,
876 int *position, int *num_entries, ZebraScanEntry **entries,
880 yaz_log(LOG_API,"zebra_scan");
882 if (zebra_begin_read (zh))
888 rpn_scan (zh, stream, zapt, attributeset,
889 zh->num_basenames, zh->basenames, position,
890 num_entries, entries, is_partial, 0, 0);
895 int zebra_sort (ZebraHandle zh, ODR stream,
896 int num_input_setnames, const char **input_setnames,
897 const char *output_setname, Z_SortKeySpecList *sort_sequence,
901 yaz_log(LOG_API,"zebra_sort");
903 if (zebra_begin_read (zh))
905 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
906 output_setname, sort_sequence, sort_status);
911 int zebra_deleleResultSet(ZebraHandle zh, int function,
912 int num_setnames, char **setnames,
917 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
919 if (zebra_begin_read(zh))
920 return Z_DeleteStatus_systemProblemAtTarget;
923 case Z_DeleteResultSetRequest_list:
924 resultSetDestroy (zh, num_setnames, setnames, statuses);
926 case Z_DeleteResultSetRequest_all:
927 resultSetDestroy (zh, -1, 0, statuses);
931 status = Z_DeleteStatus_success;
932 for (i = 0; i<num_setnames; i++)
933 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
934 status = statuses[i];
938 int zebra_errCode (ZebraHandle zh)
942 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
945 yaz_log(LOG_API,"zebra_errCode: o");
949 const char *zebra_errString (ZebraHandle zh)
953 e= diagbib1_str (zh->errCode);
954 yaz_log(LOG_API,"zebra_errString: %s",e);
958 char *zebra_errAdd (ZebraHandle zh)
963 yaz_log(LOG_API,"zebra_errAdd: %s",a);
967 void zebra_clearError(ZebraHandle zh)
976 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
986 sprintf(u, "perm.%.30s", user ? user : "anonymous");
987 p = res_get(zs->global_res, u);
988 xfree (zh->user_perm);
989 zh->user_perm = xstrdup(p ? p : "r");
991 /* users that don't require a password .. */
992 if (zh->user_perm && strchr(zh->user_perm, 'a'))
995 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1000 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
1001 const char *record_type)
1004 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
1005 database, record_type);
1007 if (zebra_select_database(zh, database))
1009 if (zebra_begin_trans (zh, 1))
1014 int zebra_admin_import_end (ZebraHandle zh)
1017 yaz_log(LOG_API,"zebra_admin_import_end");
1019 zebra_end_trans (zh);
1023 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1028 yaz_log(LOG_API,"zebra_admin_import_segment");
1030 for (i = 0; i<segment->num_segmentRecords; i++)
1032 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1034 printf ("--------------%d--------------------\n", i);
1035 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1037 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1038 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1040 Odr_oct *oct = fragment->u.notExternallyTagged;
1041 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1046 zebra_update_record(zh,
1047 0, /* record Type */
1059 int zebra_admin_exchange_record (ZebraHandle zh,
1060 const char *rec_buf,
1062 const char *recid_buf, size_t recid_len,
1064 /* 1 = insert. Fail it already exists */
1065 /* 2 = replace. Fail it does not exist */
1066 /* 3 = delete. Fail if does not exist */
1067 /* 4 = update. Insert/replace */
1073 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1076 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1078 memcpy (recid_z, recid_buf, recid_len);
1079 recid_z[recid_len] = 0;
1081 if (zebra_begin_trans(zh, 1))
1084 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1087 if (action == 1) /* fail if insert */
1089 zebra_end_trans(zh);
1093 memcpy (&sysno, rinfo+1, sizeof(sysno));
1097 if (action == 2 || action == 3) /* fail if delete or update */
1099 zebra_end_trans(zh);
1102 action = 1; /* make it an insert (if it's an update).. */
1104 buffer_extract_record (zh, rec_buf, rec_len,
1105 action == 3 ? 1 : 0 /* delete flag */,
1111 0, /* force update */
1112 1 /* allow update */
1116 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1118 else if (action == 3)
1120 dict_delete (zh->reg->matchDict, recid_z);
1122 zebra_end_trans(zh);
1126 int delete_w_handle(const char *info, void *handle)
1128 ZebraHandle zh = (ZebraHandle) handle;
1131 if (*info == sizeof(pos))
1133 memcpy (&pos, info+1, sizeof(pos));
1134 isamb_unlink(zh->reg->isamb, pos);
1139 static int delete_SU_handle(void *handle, int ord)
1141 ZebraHandle zh = (ZebraHandle) handle;
1145 ord_len = key_SU_encode (ord, ord_buf);
1146 ord_buf[ord_len] = '\0';
1148 assert (zh->reg->isamb);
1149 dict_delete_subtree(zh->reg->dict, ord_buf,
1150 zh, delete_w_handle);
1154 int zebra_drop_database (ZebraHandle zh, const char *database)
1158 yaz_log(LOG_API,"zebra_drop_database");
1161 if (zebra_select_database (zh, database))
1163 if (zebra_begin_trans (zh, 1))
1167 zebraExplain_curDatabase (zh->reg->zei, database);
1169 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1170 zebraExplain_removeDatabase(zh->reg->zei, zh);
1174 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1177 zebra_end_trans (zh);
1181 int zebra_create_database (ZebraHandle zh, const char *database)
1184 yaz_log(LOG_API,"zebra_create_database");
1187 if (zebra_select_database (zh, database))
1189 if (zebra_begin_trans (zh, 1))
1192 /* announce database */
1193 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1194 /* explainDatabase */))
1196 zebra_end_trans (zh);
1198 zh->errString = "database already exist";
1201 zebra_end_trans (zh);
1205 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1206 const char *input_str, int input_len,
1207 char *output_str, int output_len)
1211 yaz_log(LOG_API,"zebra_string_norm ");
1213 if (!zh->reg->zebra_maps)
1215 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1216 input_str, input_len);
1219 if (wrbuf_len(wrbuf) >= output_len)
1221 if (wrbuf_len(wrbuf))
1222 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1223 output_str[wrbuf_len(wrbuf)] = '\0';
1224 return wrbuf_len(wrbuf);
1228 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1230 char state_fname[256];
1235 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1238 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1239 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1240 f = fopen (fname, "w");
1242 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1243 fprintf (f, "%c %d %ld\n", val, seqno, p);
1249 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1251 char state_fname[256];
1256 yaz_log(LOG_API,"zebra_get_state ");
1258 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1259 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1260 f = fopen (fname, "r");
1266 fscanf (f, "%c %d", val, seqno);
1273 int zebra_begin_read (ZebraHandle zh)
1275 return zebra_begin_trans(zh, 0);
1278 int zebra_end_read (ZebraHandle zh)
1280 return zebra_end_trans(zh);
1283 static void read_res_for_transaction(ZebraHandle zh)
1285 const char *group = res_get(zh->res, "group");
1288 zh->m_group = group;
1289 v = res_get_prefix(zh->res, "followLinks", group, "1");
1290 zh->m_follow_links = atoi(v);
1292 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1293 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1295 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1296 zh->m_store_keys = atoi(v);
1298 v = res_get_prefix(zh->res, "storeData", group, "1");
1299 zh->m_store_data = atoi(v);
1301 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1302 zh->m_explain_database = atoi(v);
1304 v = res_get_prefix(zh->res, "openRW", group, "1");
1305 zh->m_flag_rw = atoi(v);
1307 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1308 zh->m_file_verbose_limit = atoi(v);
1311 int zebra_begin_trans (ZebraHandle zh, int rw)
1316 zh->errString = "zebra_begin_trans: no database selected";
1320 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1324 if (rw && !strchr(zh->user_perm, 'w'))
1338 const char *rval = 0;
1343 read_res_for_transaction(zh);
1346 if (zh->trans_no != 1)
1349 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1354 resultSetInvalidate (zh);
1355 zebra_register_close (zh->service, zh->reg);
1357 zh->trans_w_no = zh->trans_no;
1361 zh->records_inserted = 0;
1362 zh->records_updated = 0;
1363 zh->records_deleted = 0;
1364 zh->records_processed = 0;
1366 #if HAVE_SYS_TIMES_H
1370 if (zh->shadow_enable)
1371 rval = res_get (zh->res, "shadow");
1373 for (pass = 0; pass < 2; pass++)
1377 zebra_lock_r (zh->lock_normal);
1378 zebra_lock_w (zh->lock_shadow);
1382 zebra_lock_w (zh->lock_normal);
1383 zebra_lock_w (zh->lock_shadow);
1386 zebra_get_state (zh, &val, &seqno);
1389 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1390 zebra_unlock (zh->lock_shadow);
1391 zebra_unlock (zh->lock_normal);
1395 else if (val == 'd')
1399 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1401 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1402 bf_commitClean (bfs, rval);
1407 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1414 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1418 zebra_set_state (zh, 'd', seqno);
1420 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1421 1, rval ? 1 : 0, zh->res,
1424 zh->reg->seqno = seqno;
1427 zebra_set_state (zh, 'o', seqno);
1429 zebra_unlock (zh->lock_shadow);
1430 zebra_unlock (zh->lock_normal);
1436 zh->errString = "zebra_begin_trans: cannot open register";
1437 yaz_log(LOG_FATAL, zh->errString);
1449 if (zh->trans_no != 1)
1451 zebra_flush_reg (zh);
1455 #if HAVE_SYS_TIMES_H
1464 if (!zh->lock_normal || !zh->lock_shadow)
1470 zebra_get_state (zh, &val, &seqno);
1476 else if (seqno != zh->reg->seqno)
1478 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1479 seqno, zh->reg->seqno);
1482 else if (zh->reg->last_val != val)
1484 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1485 val, zh->reg->last_val);
1492 zebra_lock_r (zh->lock_shadow);
1494 zebra_lock_r (zh->lock_normal);
1498 resultSetInvalidate (zh);
1499 zebra_register_close (zh->service, zh->reg);
1501 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1502 0, val == 'c' ? 1 : 0,
1503 zh->res, zh->path_reg);
1506 zebra_unlock (zh->lock_normal);
1507 zebra_unlock (zh->lock_shadow);
1512 zh->reg->last_val = val;
1513 zh->reg->seqno = seqno;
1515 read_res_for_transaction(zh);
1519 int zebra_end_trans (ZebraHandle zh)
1521 ZebraTransactionStatus dummy;
1522 yaz_log(LOG_API,"zebra_end_trans");
1523 return zebra_end_transaction(zh, &dummy);
1526 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1533 yaz_log(LOG_API,"zebra_end_transaction");
1535 status->processed = 0;
1536 status->inserted = 0;
1537 status->updated = 0;
1538 status->deleted = 0;
1542 if (!zh->res || !zh->reg)
1545 zh->errString = "zebra_end_trans: no open transaction";
1548 if (zh->trans_no != zh->trans_w_no)
1551 if (zh->trans_no != 0)
1554 /* release read lock */
1556 zebra_unlock (zh->lock_normal);
1557 zebra_unlock (zh->lock_shadow);
1560 { /* release write lock */
1564 yaz_log (LOG_LOG, "zebra_end_trans");
1565 rval = res_get (zh->res, "shadow");
1567 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1569 zebra_flush_reg (zh);
1571 resultSetInvalidate (zh);
1573 zebra_register_close (zh->service, zh->reg);
1576 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1577 zh->records_processed, zh->records_inserted,
1578 zh->records_updated, zh->records_deleted);
1580 status->processed = zh->records_processed;
1581 status->inserted = zh->records_inserted;
1582 status->updated = zh->records_updated;
1583 status->deleted = zh->records_deleted;
1585 zebra_get_state (zh, &val, &seqno);
1588 BFiles bfs = bfs_create (rval, zh->path_reg);
1589 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1590 bf_commitClean (bfs, rval);
1595 zebra_set_state (zh, 'o', seqno);
1597 zebra_unlock (zh->lock_shadow);
1598 zebra_unlock (zh->lock_normal);
1601 #if HAVE_SYS_TIMES_H
1603 logf (LOG_LOG, "user/system: %ld/%ld",
1604 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1605 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1607 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1608 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1613 int zebra_repository_update (ZebraHandle zh, const char *path)
1617 logf (LOG_LOG|LOG_API, "updating %s", path);
1618 repositoryUpdate (zh, path);
1622 int zebra_repository_delete (ZebraHandle zh, const char *path)
1626 logf (LOG_LOG|LOG_API, "deleting %s", path);
1627 repositoryDelete (zh, path);
1631 int zebra_repository_show (ZebraHandle zh, const char *path)
1634 yaz_log(LOG_API,"zebra_repository_show");
1636 repositoryShow (zh, path);
1640 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1654 rval = res_get (zh->res, "shadow");
1657 logf (LOG_WARN, "Cannot perform commit");
1658 logf (LOG_WARN, "No shadow area defined");
1662 zebra_lock_w (zh->lock_normal);
1663 zebra_lock_r (zh->lock_shadow);
1665 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1667 zebra_get_state (zh, &val, &seqno);
1670 bf_cache (bfs, rval);
1671 if (bf_commitExists (bfs))
1674 zebra_set_state (zh, 'd', seqno);
1677 zebra_set_state (zh, 'c', seqno);
1679 logf (LOG_LOG, "commit start");
1680 bf_commitExec (bfs);
1685 logf (LOG_LOG, "commit clean");
1686 bf_commitClean (bfs, rval);
1688 zebra_set_state (zh, 'o', seqno);
1692 logf (LOG_LOG, "nothing to commit");
1696 zebra_unlock (zh->lock_shadow);
1697 zebra_unlock (zh->lock_normal);
1701 int zebra_clean (ZebraHandle zh)
1703 yaz_log(LOG_API,"zebra_clean");
1704 return zebra_commit_ex(zh, 1);
1707 int zebra_commit (ZebraHandle zh)
1709 yaz_log(LOG_API,"zebra_commit");
1710 return zebra_commit_ex(zh, 0);
1713 int zebra_init (ZebraHandle zh)
1718 yaz_log(LOG_API,"zebra_init");
1726 rval = res_get (zh->res, "shadow");
1728 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1731 bf_cache (bfs, rval);
1735 zebra_set_state (zh, 'o', 0);
1739 int zebra_compact (ZebraHandle zh)
1743 yaz_log(LOG_API,"zebra_compact");
1750 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1756 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1759 yaz_log(LOG_API,"zebra_result");
1760 *code = zh->errCode;
1761 *addinfo = zh->errString;
1765 int zebra_shadow_enable (ZebraHandle zh, int value)
1768 yaz_log(LOG_API,"zebra_shadow_enable");
1770 zh->shadow_enable = value;
1774 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1777 yaz_log(LOG_API,"zebra_record_encoding");
1779 xfree (zh->record_encoding);
1783 * Something about charset aliases. Oleg???
1786 if (zh->iconv_to_utf8 != 0)
1787 yaz_iconv_close(zh->iconv_to_utf8);
1788 if (zh->iconv_from_utf8 != 0)
1789 yaz_iconv_close(zh->iconv_from_utf8);
1791 zh->record_encoding = xstrdup (encoding);
1793 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1796 yaz_iconv_open ("UTF-8", encoding);
1797 if (zh->iconv_to_utf8 == 0)
1798 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1799 zh->iconv_from_utf8 =
1800 yaz_iconv_open (encoding, "UTF-8");
1801 if (zh->iconv_to_utf8 == 0)
1802 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1807 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1810 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1812 res_set(zh->res, name, value);
1816 const char *zebra_get_resource(ZebraHandle zh,
1817 const char *name, const char *defaultvalue)
1821 v= res_get_def( zh->res, name, (char *)defaultvalue);
1823 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1827 /* moved from zebra_api_ext.c by pop */
1828 /* FIXME: Should this really be public??? -Heikki */
1830 int zebra_trans_no (ZebraHandle zh)
1833 yaz_log(LOG_API,"zebra_trans_no");
1834 return zh->trans_no;
1837 int zebra_get_shadow_enable (ZebraHandle zh)
1839 yaz_log(LOG_API,"zebra_get_shadow_enable");
1840 return (zh->shadow_enable);
1843 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1845 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1846 zh->shadow_enable = value;
1850 /* almost the same as zebra_records_retrieve ... but how did it work?
1851 I mean for multiple records ??? CHECK ??? */
1852 void api_records_retrieve (ZebraHandle zh, ODR stream,
1853 const char *setname, Z_RecordComposition *comp,
1854 oid_value input_format, int num_recs,
1855 ZebraRetrievalRecord *recs)
1859 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1864 zh->errString = odr_strdup (stream, setname);
1870 if (zebra_begin_read (zh))
1873 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1874 for (i = 0; i<num_recs; i++)
1875 pos_array[i] = recs[i].position;
1876 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1879 logf (LOG_DEBUG, "zebraPosSetCreate error");
1881 zh->errString = nmem_strdup (stream->mem, setname);
1885 for (i = 0; i<num_recs; i++)
1889 recs[i].errCode = 0;
1890 recs[i].format = VAL_SUTRS;
1891 recs[i].len = strlen(poset[i].term);
1892 recs[i].buf = poset[i].term;
1893 recs[i].base = poset[i].db;
1897 else if (poset[i].sysno)
1899 /* changed here ??? CHECK ??? */
1902 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1903 stream, input_format, comp,
1908 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1909 memcpy(recs[i].buf, b, recs[i].len);
1910 recs[i].errString = 0; /* Hmmm !!! we should get this */
1911 recs[i].sysno = poset[i].sysno;
1912 recs[i].score = poset[i].score;
1918 sprintf (num_str, "%d", pos_array[i]);
1920 zh->errString = odr_strdup (stream, num_str);
1925 zebraPosSetDestroy (zh, poset, num_recs);
1927 zebra_end_read (zh);
1932 /* ---------------------------------------------------------------------------
1933 Record insert(=update), delete
1935 If sysno is provided, then it's used to identify the record.
1936 If not, and match_criteria is provided, then sysno is guessed
1937 If not, and a record is provided, then sysno is got from there
1938 NOTE: Now returns 0 at success and updates sysno, which is an int*
1942 int zebra_add_record(ZebraHandle zh,
1943 const char *buf, int buf_size)
1946 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1949 int zebra_insert_record (ZebraHandle zh,
1950 const char *recordType,
1951 SYSNO *sysno, const char *match, const char *fname,
1952 const char *buf, int buf_size, int force_update)
1955 yaz_log(LOG_API,"zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
1957 if (buf_size < 1) buf_size = strlen(buf);
1959 if (zebra_begin_trans(zh, 1))
1961 res = buffer_extract_record (zh, buf, buf_size,
1962 0, /* delete_flag */
1968 0); /* allow_update */
1969 zebra_end_trans(zh);
1973 int zebra_update_record (ZebraHandle zh,
1974 const char *recordType,
1975 SYSNO* sysno, const char *match, const char *fname,
1976 const char *buf, int buf_size,
1981 yaz_log(LOG_API,"zebra_update_record sysno=" ZINT_FORMAT, *sysno);
1983 if (buf_size < 1) buf_size = strlen(buf);
1985 if (zebra_begin_trans(zh, 1))
1987 res = buffer_extract_record (zh, buf, buf_size,
1988 0, /* delete_flag */
1994 1); /* allow_update */
1995 yaz_log(LOG_LOG, "zebra_update_record returned res=%d", res);
1996 zebra_end_trans(zh);
2000 int zebra_delete_record (ZebraHandle zh,
2001 const char *recordType,
2002 SYSNO *sysno, const char *match, const char *fname,
2003 const char *buf, int buf_size,
2007 yaz_log(LOG_API,"zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2009 if (buf_size < 1) buf_size = strlen(buf);
2011 if (zebra_begin_trans(zh, 1))
2013 res = buffer_extract_record (zh, buf, buf_size,
2014 1, /* delete_flag */
2020 1); /* allow_update */
2021 zebra_end_trans(zh);
2025 /* ---------------------------------------------------------------------------
2029 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2030 const char *setname, int *numhits)
2035 ODR odr = odr_createmem(ODR_ENCODE);
2037 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2039 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2042 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2044 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2048 yaz_log(LOG_API,"Hits: %d",hits);
2056 /* ---------------------------------------------------------------------------
2057 Sort - a simplified interface, with optional read locks.
2058 FIXME - This is a horrible name, will conflict with half the applications
2060 int zebra_sort_by_specstr (ZebraHandle zh,
2062 const char *sort_spec,
2063 const char *output_setname,
2064 const char **input_setnames)
2066 int num_input_setnames = 0;
2067 int sort_status = 0;
2068 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2069 yaz_log(LOG_API,"sort (FIXME) ");
2072 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2077 /* we can do this, since the perl typemap code for char** will
2078 put a NULL at the end of list */
2079 while (input_setnames[num_input_setnames]) num_input_setnames++;
2081 if (zebra_begin_read (zh))
2084 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2085 output_setname, sort_sequence, &sort_status);