1 /* $Id: zebraapi.c,v 1.116 2004-01-22 11:27:21 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);
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 */
112 zh->reg_name = xstrdup ("");
114 zh->num_basenames = 0;
123 zh->shadow_enable = 1;
125 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
126 zh->record_encoding = xstrdup (default_encoding);
129 yaz_iconv_open ("UTF-8", default_encoding);
130 if (zh->iconv_to_utf8 == 0)
131 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
133 zh->iconv_from_utf8 =
134 yaz_iconv_open (default_encoding, "UTF-8");
135 if (zh->iconv_to_utf8 == 0)
136 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
139 zebra_mutex_cond_lock (&zs->session_lock);
141 zh->next = zs->sessions;
144 zebra_mutex_cond_unlock (&zs->session_lock);
149 ZebraService zebra_start (const char *configName, Res def_res, Res over_res)
153 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
155 if ((res = res_open (configName, def_res, over_res)))
157 ZebraService zh = xmalloc (sizeof(*zh));
159 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
161 zh->global_res = res;
162 zh->configName = xstrdup(configName);
167 zebra_mutex_cond_init (&zh->session_lock);
168 if (!res_get (zh->global_res, "passwd"))
169 zh->passwd_db = NULL;
172 zh->passwd_db = passwd_db_open ();
174 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
176 passwd_db_file (zh->passwd_db,
177 res_get (zh->global_res, "passwd"));
179 zh->path_root = res_get (zh->global_res, "root");
186 void zebra_pidfname(ZebraService zs, char *path)
188 zebra_lock_prefix (zs->global_res, path);
189 strcat(path, "zebrasrv.pid");
193 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
194 int rw, int useshadow, Res res,
195 const char *reg_path)
197 struct zebra_register *reg;
198 int record_compression = REC_COMPRESS_NONE;
199 const char *recordCompression = 0;
200 const char *profilePath;
205 reg = xmalloc (sizeof(*reg));
208 reg->name = xstrdup (name);
215 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
216 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
218 reg->dh = data1_createx (DATA1_FLAG_XML);
221 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
224 data1_destroy(reg->dh);
228 bf_cache (reg->bfs, res_get (res, "shadow"));
230 getcwd(cwd, sizeof(cwd)-1);
231 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
232 yaz_log(LOG_LOG, "profilePath=%s cwd=%s", profilePath, cwd);
234 data1_set_tabpath (reg->dh, profilePath);
235 data1_set_tabroot (reg->dh, reg_path);
236 reg->recTypes = recTypes_init (reg->dh);
237 recTypes_default_handlers (reg->recTypes);
239 reg->zebra_maps = zebra_maps_open (res, reg_path);
240 reg->rank_classes = NULL;
244 reg->keys.buf_max = 0;
246 reg->sortKeys.buf = 0;
247 reg->sortKeys.buf_max = 0;
260 reg->key_file_no = 0;
262 zebraRankInstall (reg, rank1_class);
263 zebraRankInstall (reg, rankzv_class);
264 zebraRankInstall (reg, rankliv_class);
266 recordCompression = res_get_def (res, "recordCompression", "none");
267 if (!strcmp (recordCompression, "none"))
268 record_compression = REC_COMPRESS_NONE;
269 if (!strcmp (recordCompression, "bzip2"))
270 record_compression = REC_COMPRESS_BZIP2;
272 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
274 logf (LOG_WARN, "rec_open");
279 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
281 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
283 logf (LOG_WARN, "dict_open");
286 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
288 logf (LOG_WARN, "sortIdx_open");
291 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
293 struct ISAMS_M_s isams_m;
294 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
295 key_isams_m(res, &isams_m))))
297 logf (LOG_WARN, "isams_open");
301 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
303 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
304 sizeof (struct it_key), res)))
306 logf (LOG_WARN, "is_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", "d", ISAM_DEFAULT))
322 struct ISAMD_M_s isamd_m;
324 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
325 rw, key_isamd_m(res, &isamd_m))))
327 logf (LOG_WARN, "isamd_open");
331 if (res_get_match (res, "isam", "b", 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), 0)))
338 logf (LOG_WARN, "isamb_open");
342 if (res_get_match (res, "isam", "bc", 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 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
355 struct ISAMC_M_s isamc_m;
357 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
358 rw, key_isamc_m(res, &isamc_m), -1)))
360 logf (LOG_WARN, "isamb_open");
364 reg->zei = zebraExplain_open (reg->records, reg->dh,
369 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
373 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
377 int zebra_admin_shutdown (ZebraHandle zh)
380 yaz_log(LOG_API,"zebra_admin_shutdown");
383 zebra_mutex_cond_lock (&zh->service->session_lock);
384 zh->service->stop_flag = 1;
385 zebra_mutex_cond_unlock (&zh->service->session_lock);
389 int zebra_admin_start (ZebraHandle zh)
393 yaz_log(LOG_API,"zebra_admin_start");
396 zebra_mutex_cond_lock (&zs->session_lock);
397 zebra_mutex_cond_unlock (&zs->session_lock);
401 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
404 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
409 zebraExplain_close (reg->zei);
410 dict_close (reg->dict);
412 dict_close (reg->matchDict);
413 sortIdx_close (reg->sortIdx);
415 isams_close (reg->isams);
417 is_close (reg->isam);
419 isc_close (reg->isamc);
421 isamd_close (reg->isamd);
423 isamb_close (reg->isamb);
424 rec_close (®->records);
427 recTypes_destroy (reg->recTypes);
428 zebra_maps_close (reg->zebra_maps);
429 zebraRankDestroy (reg);
430 bfs_destroy (reg->bfs);
431 data1_destroy (reg->dh);
433 xfree (reg->sortKeys.buf);
434 xfree (reg->keys.buf);
436 xfree (reg->key_buf);
441 int zebra_stop(ZebraService zs)
445 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
449 zebra_close (zs->sessions);
452 zebra_mutex_cond_destroy (&zs->session_lock);
455 passwd_db_close (zs->passwd_db);
457 res_close (zs->global_res);
458 xfree (zs->configName);
463 int zebra_close (ZebraHandle zh)
466 struct zebra_session **sp;
469 yaz_log(LOG_API,"zebra_close");
476 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
477 resultSetDestroy (zh, -1, 0, 0);
480 zebra_register_close (zh->service, zh->reg);
481 zebra_close_res (zh);
483 xfree (zh->record_encoding);
485 for (i = 0; i < zh->num_basenames; i++)
486 xfree (zh->basenames[i]);
487 xfree (zh->basenames);
489 if (zh->iconv_to_utf8 != 0)
490 yaz_iconv_close (zh->iconv_to_utf8);
491 if (zh->iconv_from_utf8 != 0)
492 yaz_iconv_close (zh->iconv_from_utf8);
494 zebra_mutex_cond_lock (&zs->session_lock);
495 zebra_lock_destroy (zh->lock_normal);
496 zebra_lock_destroy (zh->lock_shadow);
508 zebra_mutex_cond_unlock (&zs->session_lock);
509 xfree (zh->reg_name);
510 zh->service=0; /* more likely to trigger an assert */
511 xfree (zh->path_reg);
516 struct map_baseinfo {
522 char **new_basenames;
526 static Res zebra_open_res (ZebraHandle zh)
535 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
536 res = res_open (fname, zh->service->global_res, 0);
538 res = zh->service->global_res;
540 else if (*zh->reg_name == 0)
542 res = zh->service->global_res;
546 yaz_log (LOG_WARN, "no register root specified");
547 return 0; /* no path for register - fail! */
552 static void zebra_close_res (ZebraHandle zh)
556 if (zh->res != zh->service->global_res)
561 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
565 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
569 assert (zh->reg == 0);
570 assert (*zh->reg_name == 0);
576 resultSetInvalidate (zh);
577 zebra_register_close (zh->service, zh->reg);
582 xfree (zh->reg_name);
583 zh->reg_name = xstrdup (new_reg);
585 xfree (zh->path_reg);
587 if (zh->service->path_root)
589 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
590 strlen(zh->reg_name) + 3);
591 strcpy (zh->path_reg, zh->service->path_root);
594 strcat (zh->path_reg, "/");
595 strcat (zh->path_reg, zh->reg_name);
598 zh->res = zebra_open_res (zh);
601 zebra_lock_destroy (zh->lock_normal);
605 zebra_lock_destroy (zh->lock_shadow);
611 const char *lock_area =res_get (zh->res, "lockDir");
613 if (!lock_area && zh->path_reg)
614 res_set (zh->res, "lockDir", zh->path_reg);
615 sprintf (fname, "norm.%s.LCK", zh->reg_name);
617 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
619 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
621 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
627 void map_basenames_func (void *vp, const char *name, const char *value)
629 struct map_baseinfo *p = (struct map_baseinfo *) vp;
631 char fromdb[128], todb[8][128];
634 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
635 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
636 todb[5], todb[6], todb[7]);
640 for (i = 0; i<p->num_bases; i++)
641 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
644 for (i = 0; i < no; i++)
646 if (p->new_num_bases == p->new_num_max)
648 p->new_basenames[(p->new_num_bases)++] =
649 nmem_strdup (p->mem, todb[i]);
655 void map_basenames (ZebraHandle zh, ODR stream,
656 int *num_bases, char ***basenames)
658 struct map_baseinfo info;
659 struct map_baseinfo *p = &info;
662 yaz_log(LOG_API,"map_basenames ");
666 info.num_bases = *num_bases;
667 info.basenames = *basenames;
668 info.new_num_max = 128;
669 info.new_num_bases = 0;
670 info.new_basenames = (char **)
671 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
672 info.mem = stream->mem;
674 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
676 for (i = 0; i<p->num_bases; i++)
677 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
679 p->new_basenames[(p->new_num_bases)++] =
680 nmem_strdup (p->mem, p->basenames[i]);
682 *num_bases = info.new_num_bases;
683 *basenames = info.new_basenames;
684 for (i = 0; i<*num_bases; i++)
685 logf (LOG_LOG, "base %s", (*basenames)[i]);
688 int zebra_select_database (ZebraHandle zh, const char *basename)
691 yaz_log(LOG_API,"zebra_select_database %s",basename);
693 return zebra_select_databases (zh, 1, &basename);
696 int zebra_select_databases (ZebraHandle zh, int num_bases,
697 const char **basenames)
704 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
705 num_bases,basenames[0]);
713 for (i = 0; i < zh->num_basenames; i++)
714 xfree (zh->basenames[i]);
715 xfree (zh->basenames);
717 zh->num_basenames = num_bases;
718 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
719 for (i = 0; i < zh->num_basenames; i++)
720 zh->basenames[i] = xstrdup (basenames[i]);
722 cp = strrchr(basenames[0], '/');
725 len = cp - basenames[0];
726 new_reg = xmalloc (len + 1);
727 memcpy (new_reg, basenames[0], len);
731 new_reg = xstrdup ("");
732 for (i = 1; i<num_bases; i++)
736 cp1 = strrchr (basenames[i], '/');
744 if (len != cp1 - basenames[i] ||
745 memcmp (basenames[i], new_reg, len))
760 zebra_select_register (zh, new_reg);
767 if (!zh->lock_normal || !zh->lock_shadow)
775 int zebra_search_RPN (ZebraHandle zh, ODR o,
776 Z_RPNQuery *query, const char *setname, int *hits)
779 yaz_log(LOG_API,"zebra_search_rpn");
784 if (zebra_begin_read (zh))
787 zebra_livcode_transform(zh, query);
789 resultSetAddRPN (zh, odr_extract_mem(o), query,
790 zh->num_basenames, zh->basenames, setname);
798 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
799 const char *setname, Z_RecordComposition *comp,
800 oid_value input_format, int num_recs,
801 ZebraRetrievalRecord *recs)
804 int i, *pos_array, ret = 0;
806 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
812 zh->errString = odr_strdup (stream, setname);
818 if (zebra_begin_read (zh))
821 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
822 for (i = 0; i<num_recs; i++)
823 pos_array[i] = recs[i].position;
824 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
827 logf (LOG_DEBUG, "zebraPosSetCreate error");
829 zh->errString = nmem_strdup (stream->mem, setname);
834 for (i = 0; i<num_recs; i++)
839 recs[i].format = VAL_SUTRS;
840 recs[i].len = strlen(poset[i].term);
841 recs[i].buf = poset[i].term;
842 recs[i].base = poset[i].db;
844 else if (poset[i].sysno)
847 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
848 stream, input_format, comp,
849 &recs[i].format, &recs[i].buf,
852 recs[i].errString = NULL;
858 sprintf (num_str, "%d", pos_array[i]);
860 zh->errString = odr_strdup (stream, num_str);
865 zebraPosSetDestroy (zh, poset, num_recs);
872 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
873 oid_value attributeset,
874 int *position, int *num_entries, ZebraScanEntry **entries,
878 yaz_log(LOG_API,"zebra_scan");
880 if (zebra_begin_read (zh))
886 rpn_scan (zh, stream, zapt, attributeset,
887 zh->num_basenames, zh->basenames, position,
888 num_entries, entries, is_partial, 0, 0);
893 int zebra_sort (ZebraHandle zh, ODR stream,
894 int num_input_setnames, const char **input_setnames,
895 const char *output_setname, Z_SortKeySpecList *sort_sequence,
899 yaz_log(LOG_API,"zebra_sort");
901 if (zebra_begin_read (zh))
903 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
904 output_setname, sort_sequence, sort_status);
909 int zebra_deleleResultSet(ZebraHandle zh, int function,
910 int num_setnames, char **setnames,
915 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
917 if (zebra_begin_read(zh))
918 return Z_DeleteStatus_systemProblemAtTarget;
921 case Z_DeleteRequest_list:
922 resultSetDestroy (zh, num_setnames, setnames, statuses);
924 case Z_DeleteRequest_all:
925 resultSetDestroy (zh, -1, 0, statuses);
929 status = Z_DeleteStatus_success;
930 for (i = 0; i<num_setnames; i++)
931 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
932 status = statuses[i];
936 int zebra_errCode (ZebraHandle zh)
940 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
943 yaz_log(LOG_API,"zebra_errCode: o");
947 const char *zebra_errString (ZebraHandle zh)
951 e= diagbib1_str (zh->errCode);
952 yaz_log(LOG_API,"zebra_errString: %s",e);
956 char *zebra_errAdd (ZebraHandle zh)
961 yaz_log(LOG_API,"zebra_errAdd: %s",a);
965 void zebra_clearError(ZebraHandle zh)
974 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
978 yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
981 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
983 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
987 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
991 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
992 const char *record_type)
995 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
996 database, record_type);
998 if (zebra_select_database(zh, database))
1000 zebra_begin_trans (zh, 1);
1004 int zebra_admin_import_end (ZebraHandle zh)
1007 yaz_log(LOG_API,"zebra_admin_import_end");
1009 zebra_end_trans (zh);
1013 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1018 yaz_log(LOG_API,"zebra_admin_import_segment");
1020 for (i = 0; i<segment->num_segmentRecords; i++)
1022 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1024 printf ("--------------%d--------------------\n", i);
1025 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1027 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1028 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1030 Odr_oct *oct = fragment->u.notExternallyTagged;
1031 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1036 zebra_update_record(zh,
1037 0, /* record Type */
1049 int zebra_admin_exchange_record (ZebraHandle zh,
1050 const char *rec_buf,
1052 const char *recid_buf, size_t recid_len,
1054 /* 1 = insert. Fail it already exists */
1055 /* 2 = replace. Fail it does not exist */
1056 /* 3 = delete. Fail if does not exist */
1057 /* 4 = update. Insert/replace */
1063 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1066 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1068 memcpy (recid_z, recid_buf, recid_len);
1069 recid_z[recid_len] = 0;
1071 zebra_begin_trans(zh,1);
1073 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1076 if (action == 1) /* fail if insert */
1078 zebra_end_trans(zh);
1082 memcpy (&sysno, rinfo+1, sizeof(sysno));
1086 if (action == 2 || action == 3) /* fail if delete or update */
1088 zebra_end_trans(zh);
1091 action = 1; /* make it an insert (if it's an update).. */
1093 buffer_extract_record (zh, rec_buf, rec_len,
1094 action == 3 ? 1 : 0 /* delete flag */,
1100 0, /* force update */
1101 1 /* allow update */
1105 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1107 else if (action == 3)
1109 dict_delete (zh->reg->matchDict, recid_z);
1111 zebra_end_trans(zh);
1115 int delete_w_handle(const char *info, void *handle)
1117 ZebraHandle zh = (ZebraHandle) handle;
1120 if (*info == sizeof(pos))
1122 memcpy (&pos, info+1, sizeof(pos));
1123 isamb_unlink(zh->reg->isamb, pos);
1128 static int delete_SU_handle(void *handle, int ord)
1130 ZebraHandle zh = (ZebraHandle) handle;
1134 ord_len = key_SU_encode (ord, ord_buf);
1135 ord_buf[ord_len] = '\0';
1137 assert (zh->reg->isamb);
1138 dict_delete_subtree(zh->reg->dict, ord_buf,
1139 zh, delete_w_handle);
1143 int zebra_drop_database (ZebraHandle zh, const char *database)
1147 yaz_log(LOG_API,"zebra_drop_database");
1150 if (zebra_select_database (zh, database))
1152 if (zebra_begin_trans (zh, 1))
1156 zebraExplain_curDatabase (zh->reg->zei, database);
1158 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1159 zebraExplain_removeDatabase(zh->reg->zei, zh);
1163 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1166 zebra_end_trans (zh);
1170 int zebra_create_database (ZebraHandle zh, const char *database)
1173 yaz_log(LOG_API,"zebra_create_database");
1176 if (zebra_select_database (zh, database))
1178 if (zebra_begin_trans (zh, 1))
1181 /* announce database */
1182 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1183 /* explainDatabase */))
1185 zebra_end_trans (zh);
1187 zh->errString = "database already exist";
1190 zebra_end_trans (zh);
1194 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1195 const char *input_str, int input_len,
1196 char *output_str, int output_len)
1200 yaz_log(LOG_API,"zebra_string_norm ");
1202 if (!zh->reg->zebra_maps)
1204 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1205 input_str, input_len);
1208 if (wrbuf_len(wrbuf) >= output_len)
1210 if (wrbuf_len(wrbuf))
1211 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1212 output_str[wrbuf_len(wrbuf)] = '\0';
1213 return wrbuf_len(wrbuf);
1217 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1219 char state_fname[256];
1224 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1227 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1228 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1229 f = fopen (fname, "w");
1231 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1232 fprintf (f, "%c %d %ld\n", val, seqno, p);
1238 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1240 char state_fname[256];
1245 yaz_log(LOG_API,"zebra_get_state ");
1247 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1248 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1249 f = fopen (fname, "r");
1255 fscanf (f, "%c %d", val, seqno);
1262 int zebra_begin_read (ZebraHandle zh)
1264 return zebra_begin_trans(zh, 0);
1267 int zebra_end_read (ZebraHandle zh)
1269 return zebra_end_trans(zh);
1272 static void read_res_for_transaction(ZebraHandle zh)
1274 const char *group = res_get(zh->res, "group");
1277 zh->m_group = group;
1278 v = res_get_prefix(zh->res, "followLinks", group, "1");
1279 zh->m_follow_links = atoi(v);
1281 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1282 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1284 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1285 zh->m_store_keys = atoi(v);
1287 v = res_get_prefix(zh->res, "storeData", group, "1");
1288 zh->m_store_data = atoi(v);
1290 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1291 zh->m_explain_database = atoi(v);
1293 v = res_get_prefix(zh->res, "openRW", group, "1");
1294 zh->m_flag_rw = atoi(v);
1296 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1297 zh->m_file_verbose_limit = atoi(v);
1300 int zebra_begin_trans (ZebraHandle zh, int rw)
1305 zh->errString = "zebra_begin_trans: no database selected";
1309 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1316 const char *rval = 0;
1321 if (zh->trans_no != 1)
1324 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1329 resultSetInvalidate (zh);
1330 zebra_register_close (zh->service, zh->reg);
1332 zh->trans_w_no = zh->trans_no;
1336 zh->records_inserted = 0;
1337 zh->records_updated = 0;
1338 zh->records_deleted = 0;
1339 zh->records_processed = 0;
1341 #if HAVE_SYS_TIMES_H
1345 if (zh->shadow_enable)
1346 rval = res_get (zh->res, "shadow");
1348 for (pass = 0; pass < 2; pass++)
1352 zebra_lock_r (zh->lock_normal);
1353 zebra_lock_w (zh->lock_shadow);
1357 zebra_lock_w (zh->lock_normal);
1358 zebra_lock_w (zh->lock_shadow);
1361 zebra_get_state (zh, &val, &seqno);
1364 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1365 zebra_unlock (zh->lock_shadow);
1366 zebra_unlock (zh->lock_normal);
1370 else if (val == 'd')
1374 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1376 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1377 bf_commitClean (bfs, rval);
1382 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1389 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1393 zebra_set_state (zh, 'd', seqno);
1395 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1396 1, rval ? 1 : 0, zh->res,
1399 zh->reg->seqno = seqno;
1402 zebra_set_state (zh, 'o', seqno);
1404 zebra_unlock (zh->lock_shadow);
1405 zebra_unlock (zh->lock_normal);
1411 zh->errString = "zebra_begin_trans: cannot open register";
1412 yaz_log(LOG_FATAL, zh->errString);
1424 if (zh->trans_no != 1)
1426 zebra_flush_reg (zh);
1430 #if HAVE_SYS_TIMES_H
1439 if (!zh->lock_normal || !zh->lock_shadow)
1445 zebra_get_state (zh, &val, &seqno);
1451 else if (seqno != zh->reg->seqno)
1453 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1454 seqno, zh->reg->seqno);
1457 else if (zh->reg->last_val != val)
1459 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1460 val, zh->reg->last_val);
1467 zebra_lock_r (zh->lock_shadow);
1469 zebra_lock_r (zh->lock_normal);
1473 resultSetInvalidate (zh);
1474 zebra_register_close (zh->service, zh->reg);
1476 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1477 0, val == 'c' ? 1 : 0,
1478 zh->res, zh->path_reg);
1481 zebra_unlock (zh->lock_normal);
1482 zebra_unlock (zh->lock_shadow);
1487 zh->reg->last_val = val;
1488 zh->reg->seqno = seqno;
1490 read_res_for_transaction(zh);
1494 int zebra_end_trans (ZebraHandle zh)
1496 ZebraTransactionStatus dummy;
1497 yaz_log(LOG_API,"zebra_end_trans");
1498 return zebra_end_transaction(zh, &dummy);
1501 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1508 yaz_log(LOG_API,"zebra_end_transaction");
1510 status->processed = 0;
1511 status->inserted = 0;
1512 status->updated = 0;
1513 status->deleted = 0;
1517 if (!zh->res || !zh->reg)
1520 zh->errString = "zebra_end_trans: no open transaction";
1523 if (zh->trans_no != zh->trans_w_no)
1526 if (zh->trans_no != 0)
1529 /* release read lock */
1531 zebra_unlock (zh->lock_normal);
1532 zebra_unlock (zh->lock_shadow);
1535 { /* release write lock */
1539 yaz_log (LOG_LOG, "zebra_end_trans");
1540 rval = res_get (zh->res, "shadow");
1542 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1544 zebra_flush_reg (zh);
1546 resultSetInvalidate (zh);
1548 zebra_register_close (zh->service, zh->reg);
1551 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1552 zh->records_processed, zh->records_inserted,
1553 zh->records_updated, zh->records_deleted);
1555 status->processed = zh->records_processed;
1556 status->inserted = zh->records_inserted;
1557 status->updated = zh->records_updated;
1558 status->deleted = zh->records_deleted;
1560 zebra_get_state (zh, &val, &seqno);
1563 BFiles bfs = bfs_create (rval, zh->path_reg);
1564 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1565 bf_commitClean (bfs, rval);
1570 zebra_set_state (zh, 'o', seqno);
1572 zebra_unlock (zh->lock_shadow);
1573 zebra_unlock (zh->lock_normal);
1576 #if HAVE_SYS_TIMES_H
1578 logf (LOG_LOG, "user/system: %ld/%ld",
1579 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1580 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1582 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1583 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1588 int zebra_repository_update (ZebraHandle zh, const char *path)
1592 logf (LOG_LOG|LOG_API, "updating %s", path);
1593 repositoryUpdate (zh, path);
1597 int zebra_repository_delete (ZebraHandle zh, const char *path)
1601 logf (LOG_LOG|LOG_API, "deleting %s", path);
1602 repositoryDelete (zh, path);
1606 int zebra_repository_show (ZebraHandle zh, const char *path)
1609 yaz_log(LOG_API,"zebra_repository_show");
1611 repositoryShow (zh, path);
1615 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1629 rval = res_get (zh->res, "shadow");
1632 logf (LOG_WARN, "Cannot perform commit");
1633 logf (LOG_WARN, "No shadow area defined");
1637 zebra_lock_w (zh->lock_normal);
1638 zebra_lock_r (zh->lock_shadow);
1640 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1642 zebra_get_state (zh, &val, &seqno);
1645 bf_cache (bfs, rval);
1646 if (bf_commitExists (bfs))
1649 zebra_set_state (zh, 'd', seqno);
1652 zebra_set_state (zh, 'c', seqno);
1654 logf (LOG_LOG, "commit start");
1655 bf_commitExec (bfs);
1660 logf (LOG_LOG, "commit clean");
1661 bf_commitClean (bfs, rval);
1663 zebra_set_state (zh, 'o', seqno);
1667 logf (LOG_LOG, "nothing to commit");
1671 zebra_unlock (zh->lock_shadow);
1672 zebra_unlock (zh->lock_normal);
1676 int zebra_clean (ZebraHandle zh)
1678 yaz_log(LOG_API,"zebra_clean");
1679 return zebra_commit_ex(zh, 1);
1682 int zebra_commit (ZebraHandle zh)
1684 yaz_log(LOG_API,"zebra_commit");
1685 return zebra_commit_ex(zh, 0);
1688 int zebra_init (ZebraHandle zh)
1693 yaz_log(LOG_API,"zebra_init");
1701 rval = res_get (zh->res, "shadow");
1703 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1706 bf_cache (bfs, rval);
1710 zebra_set_state (zh, 'o', 0);
1714 int zebra_compact (ZebraHandle zh)
1718 yaz_log(LOG_API,"zebra_compact");
1725 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1731 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1734 yaz_log(LOG_API,"zebra_result");
1735 *code = zh->errCode;
1736 *addinfo = zh->errString;
1740 int zebra_shadow_enable (ZebraHandle zh, int value)
1743 yaz_log(LOG_API,"zebra_shadow_enable");
1745 zh->shadow_enable = value;
1749 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1752 yaz_log(LOG_API,"zebra_record_encoding");
1754 xfree (zh->record_encoding);
1758 * Something about charset aliases. Oleg???
1761 if (zh->iconv_to_utf8 != 0)
1762 yaz_iconv_close(zh->iconv_to_utf8);
1763 if (zh->iconv_from_utf8 != 0)
1764 yaz_iconv_close(zh->iconv_from_utf8);
1766 zh->record_encoding = xstrdup (encoding);
1768 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1771 yaz_iconv_open ("UTF-8", encoding);
1772 if (zh->iconv_to_utf8 == 0)
1773 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1774 zh->iconv_from_utf8 =
1775 yaz_iconv_open (encoding, "UTF-8");
1776 if (zh->iconv_to_utf8 == 0)
1777 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1782 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1785 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1787 res_set(zh->res, name, value);
1791 const char *zebra_get_resource(ZebraHandle zh,
1792 const char *name, const char *defaultvalue)
1796 v= res_get_def( zh->res, name, (char *)defaultvalue);
1798 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1802 /* moved from zebra_api_ext.c by pop */
1803 /* FIXME: Should this really be public??? -Heikki */
1805 int zebra_trans_no (ZebraHandle zh)
1808 yaz_log(LOG_API,"zebra_trans_no");
1809 return zh->trans_no;
1812 int zebra_get_shadow_enable (ZebraHandle zh)
1814 yaz_log(LOG_API,"zebra_get_shadow_enable");
1815 return (zh->shadow_enable);
1818 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1820 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1821 zh->shadow_enable = value;
1825 /* almost the same as zebra_records_retrieve ... but how did it work?
1826 I mean for multiple records ??? CHECK ??? */
1827 void api_records_retrieve (ZebraHandle zh, ODR stream,
1828 const char *setname, Z_RecordComposition *comp,
1829 oid_value input_format, int num_recs,
1830 ZebraRetrievalRecord *recs)
1834 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1839 zh->errString = odr_strdup (stream, setname);
1845 if (zebra_begin_read (zh))
1848 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1849 for (i = 0; i<num_recs; i++)
1850 pos_array[i] = recs[i].position;
1851 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1854 logf (LOG_DEBUG, "zebraPosSetCreate error");
1856 zh->errString = nmem_strdup (stream->mem, setname);
1860 for (i = 0; i<num_recs; i++)
1864 recs[i].errCode = 0;
1865 recs[i].format = VAL_SUTRS;
1866 recs[i].len = strlen(poset[i].term);
1867 recs[i].buf = poset[i].term;
1868 recs[i].base = poset[i].db;
1872 else if (poset[i].sysno)
1874 /* changed here ??? CHECK ??? */
1877 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1878 stream, input_format, comp,
1883 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1884 memcpy(recs[i].buf, b, recs[i].len);
1885 recs[i].errString = 0; /* Hmmm !!! we should get this */
1886 recs[i].sysno = poset[i].sysno;
1887 recs[i].score = poset[i].score;
1893 sprintf (num_str, "%d", pos_array[i]);
1895 zh->errString = odr_strdup (stream, num_str);
1900 zebraPosSetDestroy (zh, poset, num_recs);
1902 zebra_end_read (zh);
1907 /* ---------------------------------------------------------------------------
1908 Record insert(=update), delete
1910 If sysno is provided, then it's used to identify the record.
1911 If not, and match_criteria is provided, then sysno is guessed
1912 If not, and a record is provided, then sysno is got from there
1913 NOTE: Now returns 0 at success and updates sysno, which is an int*
1917 int zebra_add_record(ZebraHandle zh,
1918 const char *buf, int buf_size)
1921 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1924 int zebra_insert_record (ZebraHandle zh,
1925 const char *recordType,
1926 int *sysno, const char *match, const char *fname,
1927 const char *buf, int buf_size)
1930 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
1932 if (buf_size < 1) buf_size = strlen(buf);
1934 zebra_begin_trans(zh, 1);
1935 res = buffer_extract_record (zh, buf, buf_size,
1936 0, /* delete_flag */
1942 0); /* allow_update */
1943 zebra_end_trans(zh);
1947 int zebra_update_record (ZebraHandle zh,
1948 const char *recordType,
1949 int* sysno, const char *match, const char *fname,
1950 const char *buf, int buf_size,
1955 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
1957 if (buf_size < 1) buf_size = strlen(buf);
1959 zebra_begin_trans(zh, 1);
1960 res = buffer_extract_record (zh, buf, buf_size,
1961 0, /* delete_flag */
1967 1); /* allow_update */
1968 zebra_end_trans(zh);
1972 int zebra_delete_record (ZebraHandle zh,
1973 const char *recordType,
1974 int *sysno, const char *match, const char *fname,
1975 const char *buf, int buf_size,
1979 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
1981 if (buf_size < 1) buf_size = strlen(buf);
1983 zebra_begin_trans(zh, 1);
1984 res = buffer_extract_record (zh, buf, buf_size,
1985 1, /* delete_flag */
1991 1); /* allow_update */
1992 zebra_end_trans(zh);
1996 /* ---------------------------------------------------------------------------
2000 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2001 const char *setname, int *numhits)
2006 ODR odr = odr_createmem(ODR_ENCODE);
2008 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2010 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2013 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2015 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2019 yaz_log(LOG_API,"Hits: %d",hits);
2027 /* ---------------------------------------------------------------------------
2028 Sort - a simplified interface, with optional read locks.
2029 FIXME - This is a horrible name, will conflict with half the applications
2031 int zebra_sort_by_specstr (ZebraHandle zh,
2033 const char *sort_spec,
2034 const char *output_setname,
2035 const char **input_setnames)
2037 int num_input_setnames = 0;
2038 int sort_status = 0;
2039 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2040 yaz_log(LOG_API,"sort (FIXME) ");
2043 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2048 /* we can do this, since the perl typemap code for char** will
2049 put a NULL at the end of list */
2050 while (input_setnames[num_input_setnames]) num_input_setnames++;
2052 if (zebra_begin_read (zh))
2055 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2056 output_setname, sort_sequence, &sort_status);