1 /* $Id: zebraapi.c,v 1.73 2002-09-17 12:27:12 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 static Res zebra_open_res (ZebraHandle zh);
40 static void zebra_close_res (ZebraHandle zh);
42 static void zebra_chdir (ZebraService zh)
44 const char *dir = res_get (zh->global_res, "chdir");
47 logf (LOG_DEBUG, "chdir %s", dir);
55 static void zebra_flush_reg (ZebraHandle zh)
57 zebraExplain_flush (zh->reg->zei, zh);
59 extract_flushWriteKeys (zh);
60 zebra_index_merge (zh);
63 static struct zebra_register *zebra_register_open (ZebraService zs,
65 int rw, int useshadow,
67 const char *reg_path);
68 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
70 ZebraHandle zebra_open (ZebraService zs)
73 const char *default_encoding;
78 zh = (ZebraHandle) xmalloc (sizeof(*zh));
79 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
82 zh->reg = 0; /* no register attached yet */
89 zh->reg_name = xstrdup ("");
91 zh->num_basenames = 0;
99 zh->admin_databaseName = 0;
101 zh->shadow_enable = 1;
103 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
104 zh->record_encoding = xstrdup (default_encoding);
107 yaz_iconv_open ("UTF-8", default_encoding);
108 if (zh->iconv_to_utf8 == 0)
109 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
111 zh->iconv_from_utf8 =
112 yaz_iconv_open (default_encoding, "UTF-8");
113 if (zh->iconv_to_utf8 == 0)
114 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
117 zebra_mutex_cond_lock (&zs->session_lock);
119 zh->next = zs->sessions;
122 zebra_mutex_cond_unlock (&zs->session_lock);
127 ZebraService zebra_start (const char *configName)
131 yaz_log (LOG_LOG, "zebra_start %s", configName);
133 if ((res = res_open (configName, 0)))
135 ZebraService zh = xmalloc (sizeof(*zh));
137 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
139 zh->global_res = res;
140 zh->configName = xstrdup(configName);
145 zebra_mutex_cond_init (&zh->session_lock);
146 if (!res_get (zh->global_res, "passwd"))
147 zh->passwd_db = NULL;
150 zh->passwd_db = passwd_db_open ();
152 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
154 passwd_db_file (zh->passwd_db,
155 res_get (zh->global_res, "passwd"));
157 zh->path_root = res_get (zh->global_res, "root");
164 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
165 int rw, int useshadow, Res res,
166 const char *reg_path)
168 struct zebra_register *reg;
169 int record_compression = REC_COMPRESS_NONE;
170 char *recordCompression = 0;
172 reg = xmalloc (sizeof(*reg));
175 reg->name = xstrdup (name);
182 yaz_log (LOG_DEBUG, "zebra_register_open rw = %d useshadow=%d p=%p",
185 reg->dh = data1_createx (DATA1_FLAG_XML);
188 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
191 data1_destroy(reg->dh);
195 bf_cache (reg->bfs, res_get (res, "shadow"));
196 data1_set_tabpath (reg->dh, res_get(res, "profilePath"));
197 data1_set_tabroot (reg->dh, reg_path);
198 reg->recTypes = recTypes_init (reg->dh);
199 recTypes_default_handlers (reg->recTypes);
201 reg->zebra_maps = zebra_maps_open (res, reg_path);
202 reg->rank_classes = NULL;
206 reg->keys.buf_max = 0;
221 zebraRankInstall (reg, rank1_class);
223 recordCompression = res_get_def (res, "recordCompression", "none");
224 if (!strcmp (recordCompression, "none"))
225 record_compression = REC_COMPRESS_NONE;
226 if (!strcmp (recordCompression, "bzip2"))
227 record_compression = REC_COMPRESS_BZIP2;
229 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
231 logf (LOG_WARN, "rec_open");
236 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
238 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
240 logf (LOG_WARN, "dict_open");
243 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
245 logf (LOG_WARN, "sortIdx_open");
248 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
250 struct ISAMS_M_s isams_m;
251 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
252 key_isams_m(res, &isams_m))))
254 logf (LOG_WARN, "isams_open");
258 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
260 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
261 sizeof (struct it_key), res)))
263 logf (LOG_WARN, "is_open");
267 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
269 struct ISAMC_M_s isamc_m;
270 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
271 rw, key_isamc_m(res, &isamc_m))))
273 logf (LOG_WARN, "isc_open");
277 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
279 struct ISAMD_M_s isamd_m;
281 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
282 rw, key_isamd_m(res, &isamd_m))))
284 logf (LOG_WARN, "isamd_open");
288 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
290 struct ISAMC_M_s isamc_m;
292 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
293 rw, key_isamc_m(res, &isamc_m), 0)))
295 logf (LOG_WARN, "isamb_open");
299 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
301 struct ISAMC_M_s isamc_m;
303 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
304 rw, key_isamc_m(res, &isamc_m), 1)))
306 logf (LOG_WARN, "isamb_open");
310 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
312 struct ISAMC_M_s isamc_m;
314 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
315 rw, key_isamc_m(res, &isamc_m), -1)))
317 logf (LOG_WARN, "isamb_open");
321 reg->zei = zebraExplain_open (reg->records, reg->dh,
326 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
330 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
334 void zebra_admin_shutdown (ZebraHandle zh)
336 zebra_mutex_cond_lock (&zh->service->session_lock);
337 zh->service->stop_flag = 1;
338 zebra_mutex_cond_unlock (&zh->service->session_lock);
341 void zebra_admin_start (ZebraHandle zh)
343 ZebraService zs = zh->service;
345 zebra_mutex_cond_lock (&zs->session_lock);
346 zebra_mutex_cond_unlock (&zs->session_lock);
349 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
351 yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
356 zebraExplain_close (reg->zei);
357 dict_close (reg->dict);
359 dict_close (reg->matchDict);
360 sortIdx_close (reg->sortIdx);
362 isams_close (reg->isams);
364 is_close (reg->isam);
366 isc_close (reg->isamc);
368 isamd_close (reg->isamd);
370 isamb_close (reg->isamb);
371 rec_close (®->records);
374 recTypes_destroy (reg->recTypes);
375 zebra_maps_close (reg->zebra_maps);
376 zebraRankDestroy (reg);
377 bfs_destroy (reg->bfs);
378 data1_destroy (reg->dh);
380 xfree (reg->key_buf);
383 yaz_log(LOG_DEBUG, "zebra_register_close 2");
386 void zebra_stop(ZebraService zs)
390 yaz_log (LOG_LOG, "zebra_stop");
392 zebra_mutex_cond_lock (&zs->session_lock);
395 zebra_close (zs->sessions);
398 zebra_mutex_cond_unlock (&zs->session_lock);
400 zebra_mutex_cond_destroy (&zs->session_lock);
403 passwd_db_close (zs->passwd_db);
405 res_close (zs->global_res);
406 xfree (zs->configName);
407 xfree (zs->path_root);
411 void zebra_close (ZebraHandle zh)
414 struct zebra_session **sp;
420 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
423 resultSetDestroy (zh, -1, 0, 0);
427 zebra_register_close (zh->service, zh->reg);
428 zebra_close_res (zh);
430 xfree (zh->record_encoding);
432 if (zh->iconv_to_utf8 != 0)
433 yaz_iconv_close (zh->iconv_to_utf8);
434 if (zh->iconv_from_utf8 != 0)
435 yaz_iconv_close (zh->iconv_from_utf8);
437 xfree (zh->admin_databaseName);
438 zebra_mutex_cond_lock (&zs->session_lock);
439 zebra_lock_destroy (zh->lock_normal);
440 zebra_lock_destroy (zh->lock_shadow);
452 // if (!zs->sessions && zs->stop_flag)
453 // zebra_register_deactivate(zs);
454 zebra_mutex_cond_unlock (&zs->session_lock);
455 xfree (zh->reg_name);
459 struct map_baseinfo {
465 char **new_basenames;
469 static Res zebra_open_res (ZebraHandle zh)
475 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
476 res = res_open (fname, zh->service->global_res);
478 res = zh->service->global_res;
480 else if (*zh->reg_name == 0)
482 res = zh->service->global_res;
486 yaz_log (LOG_WARN, "no register root specified");
487 return 0; /* no path for register - fail! */
492 static void zebra_close_res (ZebraHandle zh)
494 if (zh->res != zh->service->global_res)
499 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
501 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
505 assert (zh->reg == 0);
506 assert (*zh->reg_name == 0);
512 resultSetInvalidate (zh);
513 zebra_register_close (zh->service, zh->reg);
518 xfree (zh->reg_name);
519 zh->reg_name = xstrdup (new_reg);
521 xfree (zh->path_reg);
523 if (zh->service->path_root)
525 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
526 strlen(zh->reg_name) + 3);
527 strcpy (zh->path_reg, zh->service->path_root);
530 strcat (zh->path_reg, "/");
531 strcat (zh->path_reg, zh->reg_name);
534 zh->res = zebra_open_res (zh);
537 zebra_lock_destroy (zh->lock_normal);
541 zebra_lock_destroy (zh->lock_shadow);
547 const char *lock_area =res_get (zh->res, "lockDir");
549 if (!lock_area && zh->path_reg)
550 res_put (zh->res, "lockDir", zh->path_reg);
551 sprintf (fname, "norm.%s.LCK", zh->reg_name);
553 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
555 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
557 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
563 void map_basenames_func (void *vp, const char *name, const char *value)
565 struct map_baseinfo *p = (struct map_baseinfo *) vp;
567 char fromdb[128], todb[8][128];
570 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
571 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
572 todb[5], todb[6], todb[7]);
576 for (i = 0; i<p->num_bases; i++)
577 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
580 for (i = 0; i < no; i++)
582 if (p->new_num_bases == p->new_num_max)
584 p->new_basenames[(p->new_num_bases)++] =
585 nmem_strdup (p->mem, todb[i]);
591 void map_basenames (ZebraHandle zh, ODR stream,
592 int *num_bases, char ***basenames)
594 struct map_baseinfo info;
595 struct map_baseinfo *p = &info;
599 info.num_bases = *num_bases;
600 info.basenames = *basenames;
601 info.new_num_max = 128;
602 info.new_num_bases = 0;
603 info.new_basenames = (char **)
604 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
605 info.mem = stream->mem;
607 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
609 for (i = 0; i<p->num_bases; i++)
610 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
612 p->new_basenames[(p->new_num_bases)++] =
613 nmem_strdup (p->mem, p->basenames[i]);
615 *num_bases = info.new_num_bases;
616 *basenames = info.new_basenames;
617 for (i = 0; i<*num_bases; i++)
618 logf (LOG_LOG, "base %s", (*basenames)[i]);
621 int zebra_select_database (ZebraHandle zh, const char *basename)
623 return zebra_select_databases (zh, 1, &basename);
626 int zebra_select_databases (ZebraHandle zh, int num_bases,
627 const char **basenames)
639 for (i = 0; i < zh->num_basenames; i++)
640 xfree (zh->basenames[i]);
641 xfree (zh->basenames);
643 zh->num_basenames = num_bases;
644 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
645 for (i = 0; i < zh->num_basenames; i++)
646 zh->basenames[i] = xstrdup (basenames[i]);
648 cp = strrchr(basenames[0], '/');
651 len = cp - basenames[0];
652 new_reg = xmalloc (len + 1);
653 memcpy (new_reg, basenames[0], len);
657 new_reg = xstrdup ("");
658 for (i = 1; i<num_bases; i++)
662 cp1 = strrchr (basenames[i], '/');
670 if (len != cp1 - basenames[i] ||
671 memcmp (basenames[i], new_reg, len))
686 zebra_select_register (zh, new_reg);
693 if (!zh->lock_normal || !zh->lock_shadow)
701 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
702 Z_RPNQuery *query, const char *setname, int *hits)
707 if (zebra_begin_read (zh))
709 resultSetAddRPN (zh, decode, stream, query,
710 zh->num_basenames, zh->basenames, setname);
717 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
718 const char *setname, Z_RecordComposition *comp,
719 oid_value input_format, int num_recs,
720 ZebraRetrievalRecord *recs)
728 zh->errString = odr_strdup (stream, setname);
734 if (zebra_begin_read (zh))
737 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
738 for (i = 0; i<num_recs; i++)
739 pos_array[i] = recs[i].position;
740 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
743 logf (LOG_DEBUG, "zebraPosSetCreate error");
745 zh->errString = nmem_strdup (stream->mem, setname);
749 for (i = 0; i<num_recs; i++)
754 recs[i].format = VAL_SUTRS;
755 recs[i].len = strlen(poset[i].term);
756 recs[i].buf = poset[i].term;
757 recs[i].base = poset[i].db;
759 else if (poset[i].sysno)
762 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
763 stream, input_format, comp,
764 &recs[i].format, &recs[i].buf,
767 recs[i].errString = NULL;
773 sprintf (num_str, "%d", pos_array[i]);
775 zh->errString = odr_strdup (stream, num_str);
779 zebraPosSetDestroy (zh, poset, num_recs);
785 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
786 oid_value attributeset,
787 int *position, int *num_entries, ZebraScanEntry **entries,
790 if (zebra_begin_read (zh))
796 rpn_scan (zh, stream, zapt, attributeset,
797 zh->num_basenames, zh->basenames, position,
798 num_entries, entries, is_partial);
802 void zebra_sort (ZebraHandle zh, ODR stream,
803 int num_input_setnames, const char **input_setnames,
804 const char *output_setname, Z_SortKeySpecList *sort_sequence,
807 if (zebra_begin_read (zh))
809 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
810 output_setname, sort_sequence, sort_status);
814 int zebra_deleleResultSet(ZebraHandle zh, int function,
815 int num_setnames, char **setnames,
819 if (zebra_begin_read(zh))
820 return Z_DeleteStatus_systemProblemAtTarget;
823 case Z_DeleteRequest_list:
824 resultSetDestroy (zh, num_setnames, setnames, statuses);
826 case Z_DeleteRequest_all:
827 resultSetDestroy (zh, -1, 0, statuses);
831 status = Z_DeleteStatus_success;
832 for (i = 0; i<num_setnames; i++)
833 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
834 status = statuses[i];
838 int zebra_errCode (ZebraHandle zh)
843 const char *zebra_errString (ZebraHandle zh)
845 return diagbib1_str (zh->errCode);
848 char *zebra_errAdd (ZebraHandle zh)
850 return zh->errString;
853 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
855 ZebraService zs = zh->service;
856 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
858 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
862 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
866 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
867 const char *record_type)
869 if (zebra_select_database(zh, database))
871 zebra_begin_trans (zh);
872 xfree (zh->admin_databaseName);
873 zh->admin_databaseName = xstrdup(database);
876 void zebra_admin_import_end (ZebraHandle zh)
878 zebra_end_trans (zh);
881 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
885 for (i = 0; i<segment->num_segmentRecords; i++)
887 Z_NamePlusRecord *npr = segment->segmentRecords[i];
888 const char *databaseName = npr->databaseName;
891 databaseName = zh->admin_databaseName;
892 printf ("--------------%d--------------------\n", i);
893 if (npr->which == Z_NamePlusRecord_intermediateFragment)
895 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
896 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
898 Odr_oct *oct = fragment->u.notExternallyTagged;
899 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
903 extract_rec_in_mem (zh, "grs.sgml",
911 0 /* match criteria */);
917 int zebra_admin_exchange_record (ZebraHandle zh,
918 const char *database,
921 const char *recid_buf, size_t recid_len,
928 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
930 memcpy (recid_z, recid_buf, recid_len);
931 recid_z[recid_len] = 0;
933 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
936 if (action == 1) /* fail if insert */
938 memcpy (&sysno, rinfo+1, sizeof(sysno));
942 if (action == 2 || action == 3) /* fail if delete or update */
945 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
946 action == 3 ? 1 : 0 /* delete flag */,
950 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
952 else if (action == 3)
954 dict_delete (zh->reg->matchDict, recid_z);
959 void zebra_admin_create (ZebraHandle zh, const char *database)
963 if (zebra_select_database (zh, database))
965 zebra_begin_trans (zh);
968 /* announce database */
969 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
970 /* explainDatabase */))
973 zh->errString = "database already exist";
975 zebra_end_trans (zh);
978 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
979 const char *input_str, int input_len,
980 char *output_str, int output_len)
983 if (!zh->reg->zebra_maps)
985 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
986 input_str, input_len);
989 if (wrbuf_len(wrbuf) >= output_len)
991 if (wrbuf_len(wrbuf))
992 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
993 output_str[wrbuf_len(wrbuf)] = '\0';
994 return wrbuf_len(wrbuf);
998 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1000 char state_fname[256];
1005 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1006 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1007 f = fopen (fname, "w");
1009 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1010 fprintf (f, "%c %d %ld\n", val, seqno, p);
1015 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1017 char state_fname[256];
1021 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1022 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1023 f = fopen (fname, "r");
1029 fscanf (f, "%c %d", val, seqno);
1035 int zebra_begin_read (ZebraHandle zh)
1045 if (zh->trans_no != 1)
1047 zebra_flush_reg (zh);
1050 #if HAVE_SYS_TIMES_H
1059 if (!zh->lock_normal || !zh->lock_shadow)
1065 zebra_get_state (zh, &val, &seqno);
1071 else if (seqno != zh->reg->seqno)
1073 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1074 seqno, zh->reg->seqno);
1077 else if (zh->reg->last_val != val)
1079 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1080 val, zh->reg->last_val);
1087 zebra_lock_r (zh->lock_shadow);
1089 zebra_lock_r (zh->lock_normal);
1092 zebra_register_close (zh->service, zh->reg);
1093 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1094 0, val == 'c' ? 1 : 0,
1095 zh->res, zh->path_reg);
1101 zh->reg->last_val = val;
1102 zh->reg->seqno = seqno;
1107 void zebra_end_read (ZebraHandle zh)
1111 if (zh->trans_no != 0)
1114 #if HAVE_SYS_TIMES_H
1116 logf (LOG_LOG, "user/system: %ld/%ld",
1117 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1118 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1122 zebra_unlock (zh->lock_normal);
1123 zebra_unlock (zh->lock_shadow);
1126 void zebra_begin_trans (ZebraHandle zh)
1131 const char *rval = 0;
1136 if (zh->trans_no != 1)
1141 yaz_log (LOG_LOG, "zebra_begin_trans");
1143 zh->records_inserted = 0;
1144 zh->records_updated = 0;
1145 zh->records_deleted = 0;
1146 zh->records_processed = 0;
1148 #if HAVE_SYS_TIMES_H
1153 if (zh->shadow_enable)
1154 rval = res_get (zh->res, "shadow");
1156 for (pass = 0; pass < 2; pass++)
1160 zebra_lock_r (zh->lock_normal);
1161 zebra_lock_w (zh->lock_shadow);
1165 zebra_lock_w (zh->lock_normal);
1166 zebra_lock_w (zh->lock_shadow);
1169 zebra_get_state (zh, &val, &seqno);
1172 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1173 zebra_unlock (zh->lock_shadow);
1174 zebra_unlock (zh->lock_normal);
1178 else if (val == 'd')
1182 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1184 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1185 bf_commitClean (bfs, rval);
1190 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1197 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1201 zebra_set_state (zh, 'd', seqno);
1203 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1204 1, rval ? 1 : 0, zh->res,
1207 zh->reg->seqno = seqno;
1210 void zebra_end_trans (ZebraHandle zh)
1217 if (zh->trans_no != 0)
1220 yaz_log (LOG_LOG, "zebra_end_trans");
1221 rval = res_get (zh->res, "shadow");
1223 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1225 zebra_flush_reg (zh);
1227 zebra_register_close (zh->service, zh->reg);
1231 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1232 zh->records_processed, zh->records_inserted,
1233 zh->records_updated, zh->records_deleted);
1235 zebra_get_state (zh, &val, &seqno);
1238 BFiles bfs = bfs_create (rval, zh->path_reg);
1239 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1240 bf_commitClean (bfs, rval);
1245 zebra_set_state (zh, 'o', seqno);
1247 zebra_unlock (zh->lock_shadow);
1248 zebra_unlock (zh->lock_normal);
1250 #if HAVE_SYS_TIMES_H
1252 logf (LOG_LOG, "user/system: %ld/%ld",
1253 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1254 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1259 void zebra_repository_update (ZebraHandle zh)
1261 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1262 repositoryUpdate (zh);
1265 void zebra_repository_delete (ZebraHandle zh)
1267 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1268 repositoryDelete (zh);
1271 void zebra_repository_show (ZebraHandle zh)
1273 repositoryShow (zh);
1276 int zebra_commit (ZebraHandle zh)
1288 rval = res_get (zh->res, "shadow");
1291 logf (LOG_WARN, "Cannot perform commit");
1292 logf (LOG_WARN, "No shadow area defined");
1296 zebra_lock_w (zh->lock_normal);
1297 zebra_lock_r (zh->lock_shadow);
1299 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1301 zebra_get_state (zh, &val, &seqno);
1304 bf_cache (bfs, rval);
1305 if (bf_commitExists (bfs))
1307 zebra_set_state (zh, 'c', seqno);
1309 logf (LOG_LOG, "commit start");
1310 bf_commitExec (bfs);
1314 logf (LOG_LOG, "commit clean");
1315 bf_commitClean (bfs, rval);
1317 zebra_set_state (zh, 'o', seqno);
1321 logf (LOG_LOG, "nothing to commit");
1325 zebra_unlock (zh->lock_shadow);
1326 zebra_unlock (zh->lock_normal);
1330 int zebra_init (ZebraHandle zh)
1340 rval = res_get (zh->res, "shadow");
1342 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1345 bf_cache (bfs, rval);
1349 zebra_set_state (zh, 'o', 0);
1353 int zebra_compact (ZebraHandle zh)
1361 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1367 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1370 zebra_begin_trans (zh);
1371 extract_rec_in_mem (zh, "grs.sgml",
1373 "Default", /* database */
1374 0 /* delete_flag */,
1379 0 /* match criteria */);
1380 zebra_end_trans (zh);
1384 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1386 memcpy (&zh->rGroup, rg, sizeof(*rg));
1389 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1391 *code = zh->errCode;
1392 *addinfo = zh->errString;
1395 void zebra_shadow_enable (ZebraHandle zh, int value)
1397 zh->shadow_enable = value;
1400 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1402 xfree (zh->record_encoding);
1403 zh->record_encoding = xstrdup (encoding);