1 /* $Id: zebraapi.c,v 1.120 2004-07-28 08:15:45 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;
252 reg->sortKeys.buf = 0;
253 reg->sortKeys.buf_max = 0;
266 reg->key_file_no = 0;
269 zebraRankInstall (reg, rank1_class);
270 zebraRankInstall (reg, rankzv_class);
271 zebraRankInstall (reg, rankliv_class);
273 recordCompression = res_get_def (res, "recordCompression", "none");
274 if (!strcmp (recordCompression, "none"))
275 record_compression = REC_COMPRESS_NONE;
276 if (!strcmp (recordCompression, "bzip2"))
277 record_compression = REC_COMPRESS_BZIP2;
279 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
281 logf (LOG_WARN, "rec_open");
286 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
288 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
290 logf (LOG_WARN, "dict_open");
293 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
295 logf (LOG_WARN, "sortIdx_open");
298 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
300 struct ISAMS_M_s isams_m;
301 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
302 key_isams_m(res, &isams_m))))
304 logf (LOG_WARN, "isams_open");
308 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
310 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
311 sizeof (struct it_key), res)))
313 logf (LOG_WARN, "is_open");
317 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
319 struct ISAMC_M_s isamc_m;
320 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
321 rw, key_isamc_m(res, &isamc_m))))
323 logf (LOG_WARN, "isc_open");
327 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
329 struct ISAMD_M_s isamd_m;
331 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
332 rw, key_isamd_m(res, &isamd_m))))
334 logf (LOG_WARN, "isamd_open");
338 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
340 struct ISAMC_M_s isamc_m;
342 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
343 rw, key_isamc_m(res, &isamc_m), 0)))
345 logf (LOG_WARN, "isamb_open");
349 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
351 struct ISAMC_M_s isamc_m;
353 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
354 rw, key_isamc_m(res, &isamc_m), 1)))
356 logf (LOG_WARN, "isamb_open");
360 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
362 struct ISAMC_M_s isamc_m;
364 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
365 rw, key_isamc_m(res, &isamc_m), -1)))
367 logf (LOG_WARN, "isamb_open");
371 reg->zei = zebraExplain_open (reg->records, reg->dh,
376 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
380 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
384 int zebra_admin_shutdown (ZebraHandle zh)
387 yaz_log(LOG_API,"zebra_admin_shutdown");
390 zebra_mutex_cond_lock (&zh->service->session_lock);
391 zh->service->stop_flag = 1;
392 zebra_mutex_cond_unlock (&zh->service->session_lock);
396 int zebra_admin_start (ZebraHandle zh)
400 yaz_log(LOG_API,"zebra_admin_start");
403 zebra_mutex_cond_lock (&zs->session_lock);
404 zebra_mutex_cond_unlock (&zs->session_lock);
408 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
411 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
416 zebraExplain_close (reg->zei);
417 dict_close (reg->dict);
419 dict_close (reg->matchDict);
420 sortIdx_close (reg->sortIdx);
422 isams_close (reg->isams);
424 is_close (reg->isam);
426 isc_close (reg->isamc);
428 isamd_close (reg->isamd);
430 isamb_close (reg->isamb);
431 rec_close (®->records);
434 recTypes_destroy (reg->recTypes);
435 zebra_maps_close (reg->zebra_maps);
436 zebraRankDestroy (reg);
437 bfs_destroy (reg->bfs);
438 data1_destroy (reg->dh);
440 xfree (reg->sortKeys.buf);
441 xfree (reg->keys.buf);
443 xfree (reg->key_buf);
448 int zebra_stop(ZebraService zs)
452 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
456 zebra_close (zs->sessions);
459 zebra_mutex_cond_destroy (&zs->session_lock);
462 passwd_db_close (zs->passwd_db);
464 res_close (zs->global_res);
465 xfree (zs->configName);
470 int zebra_close (ZebraHandle zh)
473 struct zebra_session **sp;
476 yaz_log(LOG_API,"zebra_close");
483 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
484 resultSetDestroy (zh, -1, 0, 0);
487 zebra_register_close (zh->service, zh->reg);
488 zebra_close_res (zh);
490 xfree (zh->record_encoding);
492 for (i = 0; i < zh->num_basenames; i++)
493 xfree (zh->basenames[i]);
494 xfree (zh->basenames);
496 if (zh->iconv_to_utf8 != 0)
497 yaz_iconv_close (zh->iconv_to_utf8);
498 if (zh->iconv_from_utf8 != 0)
499 yaz_iconv_close (zh->iconv_from_utf8);
501 zebra_mutex_cond_lock (&zs->session_lock);
502 zebra_lock_destroy (zh->lock_normal);
503 zebra_lock_destroy (zh->lock_shadow);
515 zebra_mutex_cond_unlock (&zs->session_lock);
516 xfree (zh->reg_name);
517 xfree (zh->user_perm);
518 zh->service=0; /* more likely to trigger an assert */
519 xfree (zh->path_reg);
524 struct map_baseinfo {
530 char **new_basenames;
534 static Res zebra_open_res (ZebraHandle zh)
543 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
544 res = res_open (fname, zh->service->global_res, 0);
546 res = zh->service->global_res;
548 else if (*zh->reg_name == 0)
550 res = zh->service->global_res;
554 yaz_log (LOG_WARN, "no register root specified");
555 return 0; /* no path for register - fail! */
560 static void zebra_close_res (ZebraHandle zh)
564 if (zh->res != zh->service->global_res)
569 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
573 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
577 assert (zh->reg == 0);
578 assert (*zh->reg_name == 0);
584 resultSetInvalidate (zh);
585 zebra_register_close (zh->service, zh->reg);
590 xfree (zh->reg_name);
591 zh->reg_name = xstrdup (new_reg);
593 xfree (zh->path_reg);
595 if (zh->service->path_root)
597 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
598 strlen(zh->reg_name) + 3);
599 strcpy (zh->path_reg, zh->service->path_root);
602 strcat (zh->path_reg, "/");
603 strcat (zh->path_reg, zh->reg_name);
606 zh->res = zebra_open_res (zh);
609 zebra_lock_destroy (zh->lock_normal);
613 zebra_lock_destroy (zh->lock_shadow);
619 const char *lock_area =res_get (zh->res, "lockDir");
621 if (!lock_area && zh->path_reg)
622 res_set (zh->res, "lockDir", zh->path_reg);
623 sprintf (fname, "norm.%s.LCK", zh->reg_name);
625 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
627 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
629 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
635 void map_basenames_func (void *vp, const char *name, const char *value)
637 struct map_baseinfo *p = (struct map_baseinfo *) vp;
639 char fromdb[128], todb[8][128];
642 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
643 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
644 todb[5], todb[6], todb[7]);
648 for (i = 0; i<p->num_bases; i++)
649 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
652 for (i = 0; i < no; i++)
654 if (p->new_num_bases == p->new_num_max)
656 p->new_basenames[(p->new_num_bases)++] =
657 nmem_strdup (p->mem, todb[i]);
663 void map_basenames (ZebraHandle zh, ODR stream,
664 int *num_bases, char ***basenames)
666 struct map_baseinfo info;
667 struct map_baseinfo *p = &info;
670 yaz_log(LOG_API,"map_basenames ");
674 info.num_bases = *num_bases;
675 info.basenames = *basenames;
676 info.new_num_max = 128;
677 info.new_num_bases = 0;
678 info.new_basenames = (char **)
679 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
680 info.mem = stream->mem;
682 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
684 for (i = 0; i<p->num_bases; i++)
685 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
687 p->new_basenames[(p->new_num_bases)++] =
688 nmem_strdup (p->mem, p->basenames[i]);
690 *num_bases = info.new_num_bases;
691 *basenames = info.new_basenames;
692 for (i = 0; i<*num_bases; i++)
693 logf (LOG_LOG, "base %s", (*basenames)[i]);
696 int zebra_select_database (ZebraHandle zh, const char *basename)
699 yaz_log(LOG_API,"zebra_select_database %s",basename);
701 return zebra_select_databases (zh, 1, &basename);
704 int zebra_select_databases (ZebraHandle zh, int num_bases,
705 const char **basenames)
712 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
713 num_bases,basenames[0]);
721 for (i = 0; i < zh->num_basenames; i++)
722 xfree (zh->basenames[i]);
723 xfree (zh->basenames);
725 zh->num_basenames = num_bases;
726 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
727 for (i = 0; i < zh->num_basenames; i++)
728 zh->basenames[i] = xstrdup (basenames[i]);
730 cp = strrchr(basenames[0], '/');
733 len = cp - basenames[0];
734 new_reg = xmalloc (len + 1);
735 memcpy (new_reg, basenames[0], len);
739 new_reg = xstrdup ("");
740 for (i = 1; i<num_bases; i++)
744 cp1 = strrchr (basenames[i], '/');
752 if (len != cp1 - basenames[i] ||
753 memcmp (basenames[i], new_reg, len))
768 zebra_select_register (zh, new_reg);
775 if (!zh->lock_normal || !zh->lock_shadow)
783 int zebra_search_RPN (ZebraHandle zh, ODR o,
784 Z_RPNQuery *query, const char *setname, int *hits)
787 yaz_log(LOG_API,"zebra_search_rpn");
792 if (zebra_begin_read (zh))
795 zebra_livcode_transform(zh, query);
797 resultSetAddRPN (zh, odr_extract_mem(o), query,
798 zh->num_basenames, zh->basenames, setname);
806 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
807 const char *setname, Z_RecordComposition *comp,
808 oid_value input_format, int num_recs,
809 ZebraRetrievalRecord *recs)
812 int i, *pos_array, ret = 0;
814 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
820 zh->errString = odr_strdup (stream, setname);
826 if (zebra_begin_read (zh))
829 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
830 for (i = 0; i<num_recs; i++)
831 pos_array[i] = recs[i].position;
832 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
835 logf (LOG_DEBUG, "zebraPosSetCreate error");
837 zh->errString = nmem_strdup (stream->mem, setname);
842 for (i = 0; i<num_recs; i++)
847 recs[i].format = VAL_SUTRS;
848 recs[i].len = strlen(poset[i].term);
849 recs[i].buf = poset[i].term;
850 recs[i].base = poset[i].db;
852 else if (poset[i].sysno)
855 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
856 stream, input_format, comp,
857 &recs[i].format, &recs[i].buf,
860 recs[i].errString = NULL;
866 sprintf (num_str, "%d", pos_array[i]);
868 zh->errString = odr_strdup (stream, num_str);
873 zebraPosSetDestroy (zh, poset, num_recs);
880 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
881 oid_value attributeset,
882 int *position, int *num_entries, ZebraScanEntry **entries,
886 yaz_log(LOG_API,"zebra_scan");
888 if (zebra_begin_read (zh))
894 rpn_scan (zh, stream, zapt, attributeset,
895 zh->num_basenames, zh->basenames, position,
896 num_entries, entries, is_partial, 0, 0);
901 int zebra_sort (ZebraHandle zh, ODR stream,
902 int num_input_setnames, const char **input_setnames,
903 const char *output_setname, Z_SortKeySpecList *sort_sequence,
907 yaz_log(LOG_API,"zebra_sort");
909 if (zebra_begin_read (zh))
911 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
912 output_setname, sort_sequence, sort_status);
917 int zebra_deleleResultSet(ZebraHandle zh, int function,
918 int num_setnames, char **setnames,
923 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
925 if (zebra_begin_read(zh))
926 return Z_DeleteStatus_systemProblemAtTarget;
929 case Z_DeleteResultSetRequest_list:
930 resultSetDestroy (zh, num_setnames, setnames, statuses);
932 case Z_DeleteResultSetRequest_all:
933 resultSetDestroy (zh, -1, 0, statuses);
937 status = Z_DeleteStatus_success;
938 for (i = 0; i<num_setnames; i++)
939 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
940 status = statuses[i];
944 int zebra_errCode (ZebraHandle zh)
948 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
951 yaz_log(LOG_API,"zebra_errCode: o");
955 const char *zebra_errString (ZebraHandle zh)
959 e= diagbib1_str (zh->errCode);
960 yaz_log(LOG_API,"zebra_errString: %s",e);
964 char *zebra_errAdd (ZebraHandle zh)
969 yaz_log(LOG_API,"zebra_errAdd: %s",a);
973 void zebra_clearError(ZebraHandle zh)
982 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
992 sprintf(u, "perm.%.30s", user ? user : "anonymous");
993 p = res_get(zs->global_res, u);
994 xfree (zh->user_perm);
995 zh->user_perm = xstrdup(p ? p : "r");
997 /* users that don't require a password .. */
998 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1001 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1006 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
1007 const char *record_type)
1010 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
1011 database, record_type);
1013 if (zebra_select_database(zh, database))
1015 if (zebra_begin_trans (zh, 1))
1020 int zebra_admin_import_end (ZebraHandle zh)
1023 yaz_log(LOG_API,"zebra_admin_import_end");
1025 zebra_end_trans (zh);
1029 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1034 yaz_log(LOG_API,"zebra_admin_import_segment");
1036 for (i = 0; i<segment->num_segmentRecords; i++)
1038 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1040 printf ("--------------%d--------------------\n", i);
1041 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1043 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1044 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1046 Odr_oct *oct = fragment->u.notExternallyTagged;
1047 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1052 zebra_update_record(zh,
1053 0, /* record Type */
1065 int zebra_admin_exchange_record (ZebraHandle zh,
1066 const char *rec_buf,
1068 const char *recid_buf, size_t recid_len,
1070 /* 1 = insert. Fail it already exists */
1071 /* 2 = replace. Fail it does not exist */
1072 /* 3 = delete. Fail if does not exist */
1073 /* 4 = update. Insert/replace */
1079 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1082 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1084 memcpy (recid_z, recid_buf, recid_len);
1085 recid_z[recid_len] = 0;
1087 if (zebra_begin_trans(zh, 1))
1090 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1093 if (action == 1) /* fail if insert */
1095 zebra_end_trans(zh);
1099 memcpy (&sysno, rinfo+1, sizeof(sysno));
1103 if (action == 2 || action == 3) /* fail if delete or update */
1105 zebra_end_trans(zh);
1108 action = 1; /* make it an insert (if it's an update).. */
1110 buffer_extract_record (zh, rec_buf, rec_len,
1111 action == 3 ? 1 : 0 /* delete flag */,
1117 0, /* force update */
1118 1 /* allow update */
1122 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1124 else if (action == 3)
1126 dict_delete (zh->reg->matchDict, recid_z);
1128 zebra_end_trans(zh);
1132 int delete_w_handle(const char *info, void *handle)
1134 ZebraHandle zh = (ZebraHandle) handle;
1137 if (*info == sizeof(pos))
1139 memcpy (&pos, info+1, sizeof(pos));
1140 isamb_unlink(zh->reg->isamb, pos);
1145 static int delete_SU_handle(void *handle, int ord)
1147 ZebraHandle zh = (ZebraHandle) handle;
1151 ord_len = key_SU_encode (ord, ord_buf);
1152 ord_buf[ord_len] = '\0';
1154 assert (zh->reg->isamb);
1155 dict_delete_subtree(zh->reg->dict, ord_buf,
1156 zh, delete_w_handle);
1160 int zebra_drop_database (ZebraHandle zh, const char *database)
1164 yaz_log(LOG_API,"zebra_drop_database");
1167 if (zebra_select_database (zh, database))
1169 if (zebra_begin_trans (zh, 1))
1173 zebraExplain_curDatabase (zh->reg->zei, database);
1175 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1176 zebraExplain_removeDatabase(zh->reg->zei, zh);
1180 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1183 zebra_end_trans (zh);
1187 int zebra_create_database (ZebraHandle zh, const char *database)
1190 yaz_log(LOG_API,"zebra_create_database");
1193 if (zebra_select_database (zh, database))
1195 if (zebra_begin_trans (zh, 1))
1198 /* announce database */
1199 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1200 /* explainDatabase */))
1202 zebra_end_trans (zh);
1204 zh->errString = "database already exist";
1207 zebra_end_trans (zh);
1211 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1212 const char *input_str, int input_len,
1213 char *output_str, int output_len)
1217 yaz_log(LOG_API,"zebra_string_norm ");
1219 if (!zh->reg->zebra_maps)
1221 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1222 input_str, input_len);
1225 if (wrbuf_len(wrbuf) >= output_len)
1227 if (wrbuf_len(wrbuf))
1228 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1229 output_str[wrbuf_len(wrbuf)] = '\0';
1230 return wrbuf_len(wrbuf);
1234 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1236 char state_fname[256];
1241 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1244 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1245 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1246 f = fopen (fname, "w");
1248 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1249 fprintf (f, "%c %d %ld\n", val, seqno, p);
1255 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1257 char state_fname[256];
1262 yaz_log(LOG_API,"zebra_get_state ");
1264 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1265 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1266 f = fopen (fname, "r");
1272 fscanf (f, "%c %d", val, seqno);
1279 int zebra_begin_read (ZebraHandle zh)
1281 return zebra_begin_trans(zh, 0);
1284 int zebra_end_read (ZebraHandle zh)
1286 return zebra_end_trans(zh);
1289 static void read_res_for_transaction(ZebraHandle zh)
1291 const char *group = res_get(zh->res, "group");
1294 zh->m_group = group;
1295 v = res_get_prefix(zh->res, "followLinks", group, "1");
1296 zh->m_follow_links = atoi(v);
1298 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1299 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1301 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1302 zh->m_store_keys = atoi(v);
1304 v = res_get_prefix(zh->res, "storeData", group, "1");
1305 zh->m_store_data = atoi(v);
1307 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1308 zh->m_explain_database = atoi(v);
1310 v = res_get_prefix(zh->res, "openRW", group, "1");
1311 zh->m_flag_rw = atoi(v);
1313 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1314 zh->m_file_verbose_limit = atoi(v);
1317 int zebra_begin_trans (ZebraHandle zh, int rw)
1322 zh->errString = "zebra_begin_trans: no database selected";
1326 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1330 if (rw && !strchr(zh->user_perm, 'w'))
1344 const char *rval = 0;
1349 read_res_for_transaction(zh);
1352 if (zh->trans_no != 1)
1355 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1360 resultSetInvalidate (zh);
1361 zebra_register_close (zh->service, zh->reg);
1363 zh->trans_w_no = zh->trans_no;
1367 zh->records_inserted = 0;
1368 zh->records_updated = 0;
1369 zh->records_deleted = 0;
1370 zh->records_processed = 0;
1372 #if HAVE_SYS_TIMES_H
1376 if (zh->shadow_enable)
1377 rval = res_get (zh->res, "shadow");
1379 for (pass = 0; pass < 2; pass++)
1383 zebra_lock_r (zh->lock_normal);
1384 zebra_lock_w (zh->lock_shadow);
1388 zebra_lock_w (zh->lock_normal);
1389 zebra_lock_w (zh->lock_shadow);
1392 zebra_get_state (zh, &val, &seqno);
1395 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1396 zebra_unlock (zh->lock_shadow);
1397 zebra_unlock (zh->lock_normal);
1401 else if (val == 'd')
1405 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1407 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1408 bf_commitClean (bfs, rval);
1413 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1420 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1424 zebra_set_state (zh, 'd', seqno);
1426 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1427 1, rval ? 1 : 0, zh->res,
1430 zh->reg->seqno = seqno;
1433 zebra_set_state (zh, 'o', seqno);
1435 zebra_unlock (zh->lock_shadow);
1436 zebra_unlock (zh->lock_normal);
1442 zh->errString = "zebra_begin_trans: cannot open register";
1443 yaz_log(LOG_FATAL, zh->errString);
1455 if (zh->trans_no != 1)
1457 zebra_flush_reg (zh);
1461 #if HAVE_SYS_TIMES_H
1470 if (!zh->lock_normal || !zh->lock_shadow)
1476 zebra_get_state (zh, &val, &seqno);
1482 else if (seqno != zh->reg->seqno)
1484 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1485 seqno, zh->reg->seqno);
1488 else if (zh->reg->last_val != val)
1490 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1491 val, zh->reg->last_val);
1498 zebra_lock_r (zh->lock_shadow);
1500 zebra_lock_r (zh->lock_normal);
1504 resultSetInvalidate (zh);
1505 zebra_register_close (zh->service, zh->reg);
1507 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1508 0, val == 'c' ? 1 : 0,
1509 zh->res, zh->path_reg);
1512 zebra_unlock (zh->lock_normal);
1513 zebra_unlock (zh->lock_shadow);
1518 zh->reg->last_val = val;
1519 zh->reg->seqno = seqno;
1521 read_res_for_transaction(zh);
1525 int zebra_end_trans (ZebraHandle zh)
1527 ZebraTransactionStatus dummy;
1528 yaz_log(LOG_API,"zebra_end_trans");
1529 return zebra_end_transaction(zh, &dummy);
1532 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1539 yaz_log(LOG_API,"zebra_end_transaction");
1541 status->processed = 0;
1542 status->inserted = 0;
1543 status->updated = 0;
1544 status->deleted = 0;
1548 if (!zh->res || !zh->reg)
1551 zh->errString = "zebra_end_trans: no open transaction";
1554 if (zh->trans_no != zh->trans_w_no)
1557 if (zh->trans_no != 0)
1560 /* release read lock */
1562 zebra_unlock (zh->lock_normal);
1563 zebra_unlock (zh->lock_shadow);
1566 { /* release write lock */
1570 yaz_log (LOG_LOG, "zebra_end_trans");
1571 rval = res_get (zh->res, "shadow");
1573 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1575 zebra_flush_reg (zh);
1577 resultSetInvalidate (zh);
1579 zebra_register_close (zh->service, zh->reg);
1582 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1583 zh->records_processed, zh->records_inserted,
1584 zh->records_updated, zh->records_deleted);
1586 status->processed = zh->records_processed;
1587 status->inserted = zh->records_inserted;
1588 status->updated = zh->records_updated;
1589 status->deleted = zh->records_deleted;
1591 zebra_get_state (zh, &val, &seqno);
1594 BFiles bfs = bfs_create (rval, zh->path_reg);
1595 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1596 bf_commitClean (bfs, rval);
1601 zebra_set_state (zh, 'o', seqno);
1603 zebra_unlock (zh->lock_shadow);
1604 zebra_unlock (zh->lock_normal);
1607 #if HAVE_SYS_TIMES_H
1609 logf (LOG_LOG, "user/system: %ld/%ld",
1610 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1611 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1613 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1614 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1619 int zebra_repository_update (ZebraHandle zh, const char *path)
1623 logf (LOG_LOG|LOG_API, "updating %s", path);
1624 repositoryUpdate (zh, path);
1628 int zebra_repository_delete (ZebraHandle zh, const char *path)
1632 logf (LOG_LOG|LOG_API, "deleting %s", path);
1633 repositoryDelete (zh, path);
1637 int zebra_repository_show (ZebraHandle zh, const char *path)
1640 yaz_log(LOG_API,"zebra_repository_show");
1642 repositoryShow (zh, path);
1646 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1660 rval = res_get (zh->res, "shadow");
1663 logf (LOG_WARN, "Cannot perform commit");
1664 logf (LOG_WARN, "No shadow area defined");
1668 zebra_lock_w (zh->lock_normal);
1669 zebra_lock_r (zh->lock_shadow);
1671 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1673 zebra_get_state (zh, &val, &seqno);
1676 bf_cache (bfs, rval);
1677 if (bf_commitExists (bfs))
1680 zebra_set_state (zh, 'd', seqno);
1683 zebra_set_state (zh, 'c', seqno);
1685 logf (LOG_LOG, "commit start");
1686 bf_commitExec (bfs);
1691 logf (LOG_LOG, "commit clean");
1692 bf_commitClean (bfs, rval);
1694 zebra_set_state (zh, 'o', seqno);
1698 logf (LOG_LOG, "nothing to commit");
1702 zebra_unlock (zh->lock_shadow);
1703 zebra_unlock (zh->lock_normal);
1707 int zebra_clean (ZebraHandle zh)
1709 yaz_log(LOG_API,"zebra_clean");
1710 return zebra_commit_ex(zh, 1);
1713 int zebra_commit (ZebraHandle zh)
1715 yaz_log(LOG_API,"zebra_commit");
1716 return zebra_commit_ex(zh, 0);
1719 int zebra_init (ZebraHandle zh)
1724 yaz_log(LOG_API,"zebra_init");
1732 rval = res_get (zh->res, "shadow");
1734 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1737 bf_cache (bfs, rval);
1741 zebra_set_state (zh, 'o', 0);
1745 int zebra_compact (ZebraHandle zh)
1749 yaz_log(LOG_API,"zebra_compact");
1756 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1762 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1765 yaz_log(LOG_API,"zebra_result");
1766 *code = zh->errCode;
1767 *addinfo = zh->errString;
1771 int zebra_shadow_enable (ZebraHandle zh, int value)
1774 yaz_log(LOG_API,"zebra_shadow_enable");
1776 zh->shadow_enable = value;
1780 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1783 yaz_log(LOG_API,"zebra_record_encoding");
1785 xfree (zh->record_encoding);
1789 * Something about charset aliases. Oleg???
1792 if (zh->iconv_to_utf8 != 0)
1793 yaz_iconv_close(zh->iconv_to_utf8);
1794 if (zh->iconv_from_utf8 != 0)
1795 yaz_iconv_close(zh->iconv_from_utf8);
1797 zh->record_encoding = xstrdup (encoding);
1799 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1802 yaz_iconv_open ("UTF-8", encoding);
1803 if (zh->iconv_to_utf8 == 0)
1804 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1805 zh->iconv_from_utf8 =
1806 yaz_iconv_open (encoding, "UTF-8");
1807 if (zh->iconv_to_utf8 == 0)
1808 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1813 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1816 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1818 res_set(zh->res, name, value);
1822 const char *zebra_get_resource(ZebraHandle zh,
1823 const char *name, const char *defaultvalue)
1827 v= res_get_def( zh->res, name, (char *)defaultvalue);
1829 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1833 /* moved from zebra_api_ext.c by pop */
1834 /* FIXME: Should this really be public??? -Heikki */
1836 int zebra_trans_no (ZebraHandle zh)
1839 yaz_log(LOG_API,"zebra_trans_no");
1840 return zh->trans_no;
1843 int zebra_get_shadow_enable (ZebraHandle zh)
1845 yaz_log(LOG_API,"zebra_get_shadow_enable");
1846 return (zh->shadow_enable);
1849 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1851 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1852 zh->shadow_enable = value;
1856 /* almost the same as zebra_records_retrieve ... but how did it work?
1857 I mean for multiple records ??? CHECK ??? */
1858 void api_records_retrieve (ZebraHandle zh, ODR stream,
1859 const char *setname, Z_RecordComposition *comp,
1860 oid_value input_format, int num_recs,
1861 ZebraRetrievalRecord *recs)
1865 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1870 zh->errString = odr_strdup (stream, setname);
1876 if (zebra_begin_read (zh))
1879 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1880 for (i = 0; i<num_recs; i++)
1881 pos_array[i] = recs[i].position;
1882 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1885 logf (LOG_DEBUG, "zebraPosSetCreate error");
1887 zh->errString = nmem_strdup (stream->mem, setname);
1891 for (i = 0; i<num_recs; i++)
1895 recs[i].errCode = 0;
1896 recs[i].format = VAL_SUTRS;
1897 recs[i].len = strlen(poset[i].term);
1898 recs[i].buf = poset[i].term;
1899 recs[i].base = poset[i].db;
1903 else if (poset[i].sysno)
1905 /* changed here ??? CHECK ??? */
1908 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1909 stream, input_format, comp,
1914 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1915 memcpy(recs[i].buf, b, recs[i].len);
1916 recs[i].errString = 0; /* Hmmm !!! we should get this */
1917 recs[i].sysno = poset[i].sysno;
1918 recs[i].score = poset[i].score;
1924 sprintf (num_str, "%d", pos_array[i]);
1926 zh->errString = odr_strdup (stream, num_str);
1931 zebraPosSetDestroy (zh, poset, num_recs);
1933 zebra_end_read (zh);
1938 /* ---------------------------------------------------------------------------
1939 Record insert(=update), delete
1941 If sysno is provided, then it's used to identify the record.
1942 If not, and match_criteria is provided, then sysno is guessed
1943 If not, and a record is provided, then sysno is got from there
1944 NOTE: Now returns 0 at success and updates sysno, which is an int*
1948 int zebra_add_record(ZebraHandle zh,
1949 const char *buf, int buf_size)
1952 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1955 int zebra_insert_record (ZebraHandle zh,
1956 const char *recordType,
1957 int *sysno, const char *match, const char *fname,
1958 const char *buf, int buf_size, int force_update)
1961 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
1963 if (buf_size < 1) buf_size = strlen(buf);
1965 if (zebra_begin_trans(zh, 1))
1967 res = buffer_extract_record (zh, buf, buf_size,
1968 0, /* delete_flag */
1974 0); /* allow_update */
1975 zebra_end_trans(zh);
1979 int zebra_update_record (ZebraHandle zh,
1980 const char *recordType,
1981 int* sysno, const char *match, const char *fname,
1982 const char *buf, int buf_size,
1987 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
1989 if (buf_size < 1) buf_size = strlen(buf);
1991 if (zebra_begin_trans(zh, 1))
1993 res = buffer_extract_record (zh, buf, buf_size,
1994 0, /* delete_flag */
2000 1); /* allow_update */
2001 yaz_log(LOG_LOG, "zebra_update_record returned res=%d", res);
2002 zebra_end_trans(zh);
2006 int zebra_delete_record (ZebraHandle zh,
2007 const char *recordType,
2008 int *sysno, const char *match, const char *fname,
2009 const char *buf, int buf_size,
2013 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2015 if (buf_size < 1) buf_size = strlen(buf);
2017 if (zebra_begin_trans(zh, 1))
2019 res = buffer_extract_record (zh, buf, buf_size,
2020 1, /* delete_flag */
2026 1); /* allow_update */
2027 zebra_end_trans(zh);
2031 /* ---------------------------------------------------------------------------
2035 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2036 const char *setname, int *numhits)
2041 ODR odr = odr_createmem(ODR_ENCODE);
2043 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2045 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2048 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2050 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2054 yaz_log(LOG_API,"Hits: %d",hits);
2062 /* ---------------------------------------------------------------------------
2063 Sort - a simplified interface, with optional read locks.
2064 FIXME - This is a horrible name, will conflict with half the applications
2066 int zebra_sort_by_specstr (ZebraHandle zh,
2068 const char *sort_spec,
2069 const char *output_setname,
2070 const char **input_setnames)
2072 int num_input_setnames = 0;
2073 int sort_status = 0;
2074 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2075 yaz_log(LOG_API,"sort (FIXME) ");
2078 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2083 /* we can do this, since the perl typemap code for char** will
2084 put a NULL at the end of list */
2085 while (input_setnames[num_input_setnames]) num_input_setnames++;
2087 if (zebra_begin_read (zh))
2090 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2091 output_setname, sort_sequence, &sort_status);