1 /* $Id: zebraapi.c,v 1.85 2003-02-27 22:55:40 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
35 #include <yaz/diagbib1.h>
39 /* simple asserts to validate the most essential input args */
40 #define ASSERTZH assert(zh && zh->service)
41 #define ASSERTZHRES assert(zh && zh->service && zh->res)
42 #define ASSERTZS assert(zs)
44 static Res zebra_open_res (ZebraHandle zh);
45 static void zebra_close_res (ZebraHandle zh);
48 static void zebra_chdir (ZebraService zs)
52 dir = res_get (zs->global_res, "chdir");
55 logf (LOG_DEBUG, "chdir %s", dir);
63 static void zebra_flush_reg (ZebraHandle zh)
67 zebraExplain_flush (zh->reg->zei, zh);
69 extract_flushWriteKeys (zh);
70 zebra_index_merge (zh);
73 static struct zebra_register *zebra_register_open (ZebraService zs,
75 int rw, int useshadow,
77 const char *reg_path);
78 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
80 ZebraHandle zebra_open (ZebraService zs)
83 const char *default_encoding;
89 zh = (ZebraHandle) xmalloc (sizeof(*zh));
90 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
93 zh->reg = 0; /* no register attached yet */
100 zh->reg_name = xstrdup ("");
102 zh->num_basenames = 0;
110 zh->admin_databaseName = 0;
112 zh->shadow_enable = 1;
114 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
115 zh->record_encoding = xstrdup (default_encoding);
118 yaz_iconv_open ("UTF-8", default_encoding);
119 if (zh->iconv_to_utf8 == 0)
120 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
122 zh->iconv_from_utf8 =
123 yaz_iconv_open (default_encoding, "UTF-8");
124 if (zh->iconv_to_utf8 == 0)
125 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
128 zebra_mutex_cond_lock (&zs->session_lock);
130 zh->next = zs->sessions;
133 zebra_mutex_cond_unlock (&zs->session_lock);
138 ZebraService zebra_start (const char *configName)
142 yaz_log (LOG_LOG, "zebra_start %s", configName);
144 if ((res = res_open (configName, 0)))
146 ZebraService zh = xmalloc (sizeof(*zh));
148 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
150 zh->global_res = res;
151 zh->configName = xstrdup(configName);
156 zebra_mutex_cond_init (&zh->session_lock);
157 if (!res_get (zh->global_res, "passwd"))
158 zh->passwd_db = NULL;
161 zh->passwd_db = passwd_db_open ();
163 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
165 passwd_db_file (zh->passwd_db,
166 res_get (zh->global_res, "passwd"));
168 zh->path_root = res_get (zh->global_res, "root");
175 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
176 int rw, int useshadow, Res res,
177 const char *reg_path)
179 struct zebra_register *reg;
180 int record_compression = REC_COMPRESS_NONE;
181 char *recordCompression = 0;
185 reg = xmalloc (sizeof(*reg));
188 reg->name = xstrdup (name);
195 yaz_log (LOG_DEBUG, "zebra_register_open rw = %d useshadow=%d p=%p",
198 reg->dh = data1_createx (DATA1_FLAG_XML);
201 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
204 data1_destroy(reg->dh);
208 bf_cache (reg->bfs, res_get (res, "shadow"));
209 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
210 DEFAULT_PROFILE_PATH));
211 data1_set_tabroot (reg->dh, reg_path);
212 reg->recTypes = recTypes_init (reg->dh);
213 recTypes_default_handlers (reg->recTypes);
215 reg->zebra_maps = zebra_maps_open (res, reg_path);
216 reg->rank_classes = NULL;
220 reg->keys.buf_max = 0;
222 reg->sortKeys.buf = 0;
223 reg->sortKeys.buf_max = 0;
236 reg->key_file_no = 0;
238 zebraRankInstall (reg, rank1_class);
239 zebraRankInstall (reg, rankzv_class);
241 recordCompression = res_get_def (res, "recordCompression", "none");
242 if (!strcmp (recordCompression, "none"))
243 record_compression = REC_COMPRESS_NONE;
244 if (!strcmp (recordCompression, "bzip2"))
245 record_compression = REC_COMPRESS_BZIP2;
247 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
249 logf (LOG_WARN, "rec_open");
254 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
256 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
258 logf (LOG_WARN, "dict_open");
261 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
263 logf (LOG_WARN, "sortIdx_open");
266 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
268 struct ISAMS_M_s isams_m;
269 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
270 key_isams_m(res, &isams_m))))
272 logf (LOG_WARN, "isams_open");
276 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
278 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
279 sizeof (struct it_key), res)))
281 logf (LOG_WARN, "is_open");
285 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
287 struct ISAMC_M_s isamc_m;
288 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
289 rw, key_isamc_m(res, &isamc_m))))
291 logf (LOG_WARN, "isc_open");
295 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
297 struct ISAMD_M_s isamd_m;
299 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
300 rw, key_isamd_m(res, &isamd_m))))
302 logf (LOG_WARN, "isamd_open");
306 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
308 struct ISAMC_M_s isamc_m;
310 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
311 rw, key_isamc_m(res, &isamc_m), 0)))
313 logf (LOG_WARN, "isamb_open");
317 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
319 struct ISAMC_M_s isamc_m;
321 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
322 rw, key_isamc_m(res, &isamc_m), 1)))
324 logf (LOG_WARN, "isamb_open");
328 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
330 struct ISAMC_M_s isamc_m;
332 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
333 rw, key_isamc_m(res, &isamc_m), -1)))
335 logf (LOG_WARN, "isamb_open");
339 reg->zei = zebraExplain_open (reg->records, reg->dh,
344 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
348 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
352 void zebra_admin_shutdown (ZebraHandle zh)
357 zebra_mutex_cond_lock (&zh->service->session_lock);
358 zh->service->stop_flag = 1;
359 zebra_mutex_cond_unlock (&zh->service->session_lock);
362 void zebra_admin_start (ZebraHandle zh)
368 zebra_mutex_cond_lock (&zs->session_lock);
369 zebra_mutex_cond_unlock (&zs->session_lock);
372 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
375 yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
380 zebraExplain_close (reg->zei);
381 dict_close (reg->dict);
383 dict_close (reg->matchDict);
384 sortIdx_close (reg->sortIdx);
386 isams_close (reg->isams);
388 is_close (reg->isam);
390 isc_close (reg->isamc);
392 isamd_close (reg->isamd);
394 isamb_close (reg->isamb);
395 rec_close (®->records);
398 recTypes_destroy (reg->recTypes);
399 zebra_maps_close (reg->zebra_maps);
400 zebraRankDestroy (reg);
401 bfs_destroy (reg->bfs);
402 data1_destroy (reg->dh);
404 xfree (reg->sortKeys.buf);
405 xfree (reg->keys.buf);
407 xfree (reg->key_buf);
410 yaz_log(LOG_DEBUG, "zebra_register_close 2");
413 void zebra_stop(ZebraService zs)
417 yaz_log (LOG_LOG, "zebra_stop");
421 zebra_close (zs->sessions);
424 zebra_mutex_cond_destroy (&zs->session_lock);
427 passwd_db_close (zs->passwd_db);
429 res_close (zs->global_res);
430 xfree (zs->configName);
431 xfree (zs->path_root);
435 void zebra_close (ZebraHandle zh)
438 struct zebra_session **sp;
447 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
450 resultSetDestroy (zh, -1, 0, 0);
453 zebra_register_close (zh->service, zh->reg);
454 zebra_close_res (zh);
456 xfree (zh->record_encoding);
458 for (i = 0; i < zh->num_basenames; i++)
459 xfree (zh->basenames[i]);
460 xfree (zh->basenames);
462 if (zh->iconv_to_utf8 != 0)
463 yaz_iconv_close (zh->iconv_to_utf8);
464 if (zh->iconv_from_utf8 != 0)
465 yaz_iconv_close (zh->iconv_from_utf8);
467 xfree (zh->admin_databaseName);
468 zebra_mutex_cond_lock (&zs->session_lock);
469 zebra_lock_destroy (zh->lock_normal);
470 zebra_lock_destroy (zh->lock_shadow);
482 zebra_mutex_cond_unlock (&zs->session_lock);
483 xfree (zh->reg_name);
484 zh->service=0; /* more likely to trigger an assert */
488 struct map_baseinfo {
494 char **new_basenames;
498 static Res zebra_open_res (ZebraHandle zh)
507 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
508 res = res_open (fname, zh->service->global_res);
510 res = zh->service->global_res;
512 else if (*zh->reg_name == 0)
514 res = zh->service->global_res;
518 yaz_log (LOG_WARN, "no register root specified");
519 return 0; /* no path for register - fail! */
524 static void zebra_close_res (ZebraHandle zh)
528 if (zh->res != zh->service->global_res)
533 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
537 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
541 assert (zh->reg == 0);
542 assert (*zh->reg_name == 0);
548 resultSetInvalidate (zh);
549 zebra_register_close (zh->service, zh->reg);
554 xfree (zh->reg_name);
555 zh->reg_name = xstrdup (new_reg);
557 xfree (zh->path_reg);
559 if (zh->service->path_root)
561 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
562 strlen(zh->reg_name) + 3);
563 strcpy (zh->path_reg, zh->service->path_root);
566 strcat (zh->path_reg, "/");
567 strcat (zh->path_reg, zh->reg_name);
570 zh->res = zebra_open_res (zh);
573 zebra_lock_destroy (zh->lock_normal);
577 zebra_lock_destroy (zh->lock_shadow);
583 const char *lock_area =res_get (zh->res, "lockDir");
585 if (!lock_area && zh->path_reg)
586 res_put (zh->res, "lockDir", zh->path_reg);
587 sprintf (fname, "norm.%s.LCK", zh->reg_name);
589 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
591 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
593 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
599 void map_basenames_func (void *vp, const char *name, const char *value)
601 struct map_baseinfo *p = (struct map_baseinfo *) vp;
603 char fromdb[128], todb[8][128];
606 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
607 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
608 todb[5], todb[6], todb[7]);
612 for (i = 0; i<p->num_bases; i++)
613 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
616 for (i = 0; i < no; i++)
618 if (p->new_num_bases == p->new_num_max)
620 p->new_basenames[(p->new_num_bases)++] =
621 nmem_strdup (p->mem, todb[i]);
627 void map_basenames (ZebraHandle zh, ODR stream,
628 int *num_bases, char ***basenames)
630 struct map_baseinfo info;
631 struct map_baseinfo *p = &info;
637 info.num_bases = *num_bases;
638 info.basenames = *basenames;
639 info.new_num_max = 128;
640 info.new_num_bases = 0;
641 info.new_basenames = (char **)
642 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
643 info.mem = stream->mem;
645 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
647 for (i = 0; i<p->num_bases; i++)
648 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
650 p->new_basenames[(p->new_num_bases)++] =
651 nmem_strdup (p->mem, p->basenames[i]);
653 *num_bases = info.new_num_bases;
654 *basenames = info.new_basenames;
655 for (i = 0; i<*num_bases; i++)
656 logf (LOG_LOG, "base %s", (*basenames)[i]);
659 int zebra_select_database (ZebraHandle zh, const char *basename)
663 return zebra_select_databases (zh, 1, &basename);
666 int zebra_select_databases (ZebraHandle zh, int num_bases,
667 const char **basenames)
681 for (i = 0; i < zh->num_basenames; i++)
682 xfree (zh->basenames[i]);
683 xfree (zh->basenames);
685 zh->num_basenames = num_bases;
686 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
687 for (i = 0; i < zh->num_basenames; i++)
688 zh->basenames[i] = xstrdup (basenames[i]);
690 cp = strrchr(basenames[0], '/');
693 len = cp - basenames[0];
694 new_reg = xmalloc (len + 1);
695 memcpy (new_reg, basenames[0], len);
699 new_reg = xstrdup ("");
700 for (i = 1; i<num_bases; i++)
704 cp1 = strrchr (basenames[i], '/');
712 if (len != cp1 - basenames[i] ||
713 memcmp (basenames[i], new_reg, len))
728 zebra_select_register (zh, new_reg);
735 if (!zh->lock_normal || !zh->lock_shadow)
743 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
744 Z_RPNQuery *query, const char *setname, int *hits)
751 if (zebra_begin_read (zh))
753 resultSetAddRPN (zh, decode, stream, query,
754 zh->num_basenames, zh->basenames, setname);
761 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
762 const char *setname, Z_RecordComposition *comp,
763 oid_value input_format, int num_recs,
764 ZebraRetrievalRecord *recs)
774 zh->errString = odr_strdup (stream, setname);
780 if (zebra_begin_read (zh))
783 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
784 for (i = 0; i<num_recs; i++)
785 pos_array[i] = recs[i].position;
786 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
789 logf (LOG_DEBUG, "zebraPosSetCreate error");
791 zh->errString = nmem_strdup (stream->mem, setname);
795 for (i = 0; i<num_recs; i++)
800 recs[i].format = VAL_SUTRS;
801 recs[i].len = strlen(poset[i].term);
802 recs[i].buf = poset[i].term;
803 recs[i].base = poset[i].db;
805 else if (poset[i].sysno)
808 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
809 stream, input_format, comp,
810 &recs[i].format, &recs[i].buf,
813 recs[i].errString = NULL;
819 sprintf (num_str, "%d", pos_array[i]);
821 zh->errString = odr_strdup (stream, num_str);
825 zebraPosSetDestroy (zh, poset, num_recs);
831 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
832 oid_value attributeset,
833 int *position, int *num_entries, ZebraScanEntry **entries,
838 if (zebra_begin_read (zh))
844 rpn_scan (zh, stream, zapt, attributeset,
845 zh->num_basenames, zh->basenames, position,
846 num_entries, entries, is_partial);
850 void zebra_sort (ZebraHandle zh, ODR stream,
851 int num_input_setnames, const char **input_setnames,
852 const char *output_setname, Z_SortKeySpecList *sort_sequence,
857 if (zebra_begin_read (zh))
859 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
860 output_setname, sort_sequence, sort_status);
864 int zebra_deleleResultSet(ZebraHandle zh, int function,
865 int num_setnames, char **setnames,
871 if (zebra_begin_read(zh))
872 return Z_DeleteStatus_systemProblemAtTarget;
875 case Z_DeleteRequest_list:
876 resultSetDestroy (zh, num_setnames, setnames, statuses);
878 case Z_DeleteRequest_all:
879 resultSetDestroy (zh, -1, 0, statuses);
883 status = Z_DeleteStatus_success;
884 for (i = 0; i<num_setnames; i++)
885 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
886 status = statuses[i];
890 int zebra_errCode (ZebraHandle zh)
897 const char *zebra_errString (ZebraHandle zh)
900 return diagbib1_str (zh->errCode);
904 char *zebra_errAdd (ZebraHandle zh)
907 return zh->errString;
911 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
917 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
919 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
923 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
927 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
928 const char *record_type)
932 if (zebra_select_database(zh, database))
934 zebra_begin_trans (zh);
935 xfree (zh->admin_databaseName);
936 zh->admin_databaseName = xstrdup(database);
939 void zebra_admin_import_end (ZebraHandle zh)
943 zebra_end_trans (zh);
946 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
952 for (i = 0; i<segment->num_segmentRecords; i++)
954 Z_NamePlusRecord *npr = segment->segmentRecords[i];
955 const char *databaseName = npr->databaseName;
958 databaseName = zh->admin_databaseName;
959 printf ("--------------%d--------------------\n", i);
960 if (npr->which == Z_NamePlusRecord_intermediateFragment)
962 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
963 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
965 Odr_oct *oct = fragment->u.notExternallyTagged;
966 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
970 extract_rec_in_mem (zh, "grs.sgml",
978 0 /* match criteria */);
984 int zebra_admin_exchange_record (ZebraHandle zh,
985 const char *database,
988 const char *recid_buf, size_t recid_len,
997 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
999 memcpy (recid_z, recid_buf, recid_len);
1000 recid_z[recid_len] = 0;
1002 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1005 if (action == 1) /* fail if insert */
1007 memcpy (&sysno, rinfo+1, sizeof(sysno));
1011 if (action == 2 || action == 3) /* fail if delete or update */
1014 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1015 action == 3 ? 1 : 0 /* delete flag */,
1016 0, &sysno, 1, 1, 0);
1019 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1021 else if (action == 3)
1023 dict_delete (zh->reg->matchDict, recid_z);
1028 void zebra_admin_create (ZebraHandle zh, const char *database)
1034 if (zebra_select_database (zh, database))
1036 zebra_begin_trans (zh);
1039 /* announce database */
1040 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1041 /* explainDatabase */))
1044 zh->errString = "database already exist";
1046 zebra_end_trans (zh);
1049 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1050 const char *input_str, int input_len,
1051 char *output_str, int output_len)
1056 if (!zh->reg->zebra_maps)
1058 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1059 input_str, input_len);
1062 if (wrbuf_len(wrbuf) >= output_len)
1064 if (wrbuf_len(wrbuf))
1065 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1066 output_str[wrbuf_len(wrbuf)] = '\0';
1067 return wrbuf_len(wrbuf);
1071 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1073 char state_fname[256];
1080 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1081 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1082 f = fopen (fname, "w");
1084 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1085 fprintf (f, "%c %d %ld\n", val, seqno, p);
1090 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1092 char state_fname[256];
1098 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1099 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1100 f = fopen (fname, "r");
1106 fscanf (f, "%c %d", val, seqno);
1112 int zebra_begin_read (ZebraHandle zh)
1123 if (zh->trans_no != 1)
1125 zebra_flush_reg (zh);
1129 #if HAVE_SYS_TIMES_H
1138 if (!zh->lock_normal || !zh->lock_shadow)
1144 zebra_get_state (zh, &val, &seqno);
1150 else if (seqno != zh->reg->seqno)
1152 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1153 seqno, zh->reg->seqno);
1156 else if (zh->reg->last_val != val)
1158 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1159 val, zh->reg->last_val);
1166 zebra_lock_r (zh->lock_shadow);
1168 zebra_lock_r (zh->lock_normal);
1171 zebra_register_close (zh->service, zh->reg);
1172 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1173 0, val == 'c' ? 1 : 0,
1174 zh->res, zh->path_reg);
1180 zh->reg->last_val = val;
1181 zh->reg->seqno = seqno;
1186 void zebra_end_read (ZebraHandle zh)
1191 if (zh->trans_no != 0)
1193 #if HAVE_SYS_TIMES_H
1195 logf (LOG_LOG, "user/system: %ld/%ld",
1196 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1197 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1201 zebra_unlock (zh->lock_normal);
1202 zebra_unlock (zh->lock_shadow);
1205 void zebra_begin_trans (ZebraHandle zh)
1210 const char *rval = 0;
1216 if (zh->trans_no != 1)
1222 yaz_log (LOG_LOG, "zebra_begin_trans");
1224 zh->records_inserted = 0;
1225 zh->records_updated = 0;
1226 zh->records_deleted = 0;
1227 zh->records_processed = 0;
1229 #if HAVE_SYS_TIMES_H
1234 if (zh->shadow_enable)
1235 rval = res_get (zh->res, "shadow");
1237 for (pass = 0; pass < 2; pass++)
1241 zebra_lock_r (zh->lock_normal);
1242 zebra_lock_w (zh->lock_shadow);
1246 zebra_lock_w (zh->lock_normal);
1247 zebra_lock_w (zh->lock_shadow);
1250 zebra_get_state (zh, &val, &seqno);
1253 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1254 zebra_unlock (zh->lock_shadow);
1255 zebra_unlock (zh->lock_normal);
1259 else if (val == 'd')
1263 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1265 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1266 bf_commitClean (bfs, rval);
1271 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1278 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1282 zebra_set_state (zh, 'd', seqno);
1284 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1285 1, rval ? 1 : 0, zh->res,
1288 zh->reg->seqno = seqno;
1291 void zebra_end_trans (ZebraHandle zh)
1299 if (zh->trans_no != 0)
1301 yaz_log (LOG_LOG, "zebra_end_trans");
1302 rval = res_get (zh->res, "shadow");
1304 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1306 zebra_flush_reg (zh);
1308 zebra_register_close (zh->service, zh->reg);
1311 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1312 zh->records_processed, zh->records_inserted,
1313 zh->records_updated, zh->records_deleted);
1315 zebra_get_state (zh, &val, &seqno);
1318 BFiles bfs = bfs_create (rval, zh->path_reg);
1319 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1320 bf_commitClean (bfs, rval);
1325 zebra_set_state (zh, 'o', seqno);
1327 zebra_unlock (zh->lock_shadow);
1328 zebra_unlock (zh->lock_normal);
1330 #if HAVE_SYS_TIMES_H
1332 logf (LOG_LOG, "user/system: %ld/%ld",
1333 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1334 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1339 void zebra_repository_update (ZebraHandle zh)
1343 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1344 repositoryUpdate (zh);
1347 void zebra_repository_delete (ZebraHandle zh)
1351 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1352 repositoryDelete (zh);
1355 void zebra_repository_show (ZebraHandle zh)
1359 repositoryShow (zh);
1362 int zebra_commit (ZebraHandle zh)
1376 rval = res_get (zh->res, "shadow");
1379 logf (LOG_WARN, "Cannot perform commit");
1380 logf (LOG_WARN, "No shadow area defined");
1384 zebra_lock_w (zh->lock_normal);
1385 zebra_lock_r (zh->lock_shadow);
1387 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1389 zebra_get_state (zh, &val, &seqno);
1392 bf_cache (bfs, rval);
1393 if (bf_commitExists (bfs))
1395 zebra_set_state (zh, 'c', seqno);
1397 logf (LOG_LOG, "commit start");
1398 bf_commitExec (bfs);
1402 logf (LOG_LOG, "commit clean");
1403 bf_commitClean (bfs, rval);
1405 zebra_set_state (zh, 'o', seqno);
1409 logf (LOG_LOG, "nothing to commit");
1413 zebra_unlock (zh->lock_shadow);
1414 zebra_unlock (zh->lock_normal);
1418 int zebra_init (ZebraHandle zh)
1430 rval = res_get (zh->res, "shadow");
1432 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1435 bf_cache (bfs, rval);
1439 zebra_set_state (zh, 'o', 0);
1443 int zebra_compact (ZebraHandle zh)
1453 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1459 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1465 zebra_begin_trans (zh);
1467 return 0; /* bad sysno */
1468 extract_rec_in_mem (zh, "grs.sgml",
1470 "Default", /* database */
1471 0 /* delete_flag */,
1476 0 /* match criteria */);
1478 zebra_end_trans (zh);
1484 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1488 memcpy (&zh->rGroup, rg, sizeof(*rg));
1491 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1494 *code = zh->errCode;
1495 *addinfo = zh->errString;
1498 void zebra_shadow_enable (ZebraHandle zh, int value)
1502 zh->shadow_enable = value;
1505 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1509 xfree (zh->record_encoding);
1515 if (zh->iconv_to_utf8 != 0)
1516 yaz_iconv_close(zh->iconv_to_utf8);
1517 if (zh->iconv_from_utf8 != 0)
1518 yaz_iconv_close(zh->iconv_from_utf8);
1520 zh->record_encoding = xstrdup (encoding);
1522 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1525 yaz_iconv_open ("UTF-8", encoding);
1526 if (zh->iconv_to_utf8 == 0)
1527 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1528 zh->iconv_from_utf8 =
1529 yaz_iconv_open (encoding, "UTF-8");
1530 if (zh->iconv_to_utf8 == 0)
1531 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1536 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1540 res_put(zh->res, name, value);
1543 const char *zebra_get_resource(ZebraHandle zh,
1544 const char *name, const char *defaultvalue)
1548 return res_get_def( zh->res, name, (char *)defaultvalue);