1 /* $Id: zebraapi.c,v 1.65 2002-08-02 19:26:55 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_LOG, "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 iconv_open ("UTF-8", default_encoding);
108 if (zh->iconv_to_utf8 == (iconv_t)(-1))
109 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
111 zh->iconv_from_utf8 =
112 iconv_open (default_encoding, "UTF-8");
113 if (zh->iconv_to_utf8 == (iconv_t)(-1))
114 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
118 zebra_mutex_cond_lock (&zs->session_lock);
120 zh->next = zs->sessions;
123 zebra_mutex_cond_unlock (&zs->session_lock);
128 ZebraService zebra_start (const char *configName)
132 yaz_log (LOG_LOG, "zebra_start %s", configName);
134 if (!(res = res_open (configName, 0)))
135 yaz_log (LOG_WARN, "Cannot read resources `%s'", configName);
138 ZebraService zh = xmalloc (sizeof(*zh));
140 yaz_log (LOG_LOG, "Read resources `%s'", configName);
142 zh->global_res = res;
143 zh->configName = xstrdup(configName);
148 zebra_mutex_cond_init (&zh->session_lock);
149 if (!res_get (zh->global_res, "passwd"))
150 zh->passwd_db = NULL;
153 zh->passwd_db = passwd_db_open ();
155 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
157 passwd_db_file (zh->passwd_db,
158 res_get (zh->global_res, "passwd"));
160 zh->path_root = res_get (zh->global_res, "root");
167 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
168 int rw, int useshadow, Res res,
169 const char *reg_path)
171 struct zebra_register *reg;
172 int record_compression = REC_COMPRESS_NONE;
173 char *recordCompression = 0;
175 reg = xmalloc (sizeof(*reg));
178 reg->name = xstrdup (name);
185 yaz_log (LOG_LOG, "zebra_register_open rw = %d useshadow=%d p=%p",
188 reg->dh = data1_createx (DATA1_FLAG_XML);
191 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
194 data1_destroy(reg->dh);
198 bf_cache (reg->bfs, res_get (res, "shadow"));
199 data1_set_tabpath (reg->dh, res_get(res, "profilePath"));
200 data1_set_tabroot (reg->dh, reg_path);
201 reg->recTypes = recTypes_init (reg->dh);
202 recTypes_default_handlers (reg->recTypes);
204 reg->zebra_maps = zebra_maps_open (res, reg_path);
205 reg->rank_classes = NULL;
209 reg->keys.buf_max = 0;
224 zebraRankInstall (reg, rank1_class);
226 recordCompression = res_get_def (res, "recordCompression", "none");
227 if (!strcmp (recordCompression, "none"))
228 record_compression = REC_COMPRESS_NONE;
229 if (!strcmp (recordCompression, "bzip2"))
230 record_compression = REC_COMPRESS_BZIP2;
232 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
234 logf (LOG_WARN, "rec_open");
239 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
241 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
243 logf (LOG_WARN, "dict_open");
246 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
248 logf (LOG_WARN, "sortIdx_open");
251 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
253 struct ISAMS_M_s isams_m;
254 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
255 key_isams_m(res, &isams_m))))
257 logf (LOG_WARN, "isams_open");
261 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
263 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
264 sizeof (struct it_key), res)))
266 logf (LOG_WARN, "is_open");
270 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
272 struct ISAMC_M_s isamc_m;
273 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
274 rw, key_isamc_m(res, &isamc_m))))
276 logf (LOG_WARN, "isc_open");
280 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
282 struct ISAMD_M_s isamd_m;
284 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
285 rw, key_isamd_m(res, &isamd_m))))
287 logf (LOG_WARN, "isamd_open");
291 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
293 struct ISAMC_M_s isamc_m;
295 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
296 rw, key_isamc_m(res, &isamc_m), 0)))
298 logf (LOG_WARN, "isamb_open");
302 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
304 struct ISAMC_M_s isamc_m;
306 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
307 rw, key_isamc_m(res, &isamc_m), 1)))
309 logf (LOG_WARN, "isamb_open");
313 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
315 struct ISAMC_M_s isamc_m;
317 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
318 rw, key_isamc_m(res, &isamc_m), -1)))
320 logf (LOG_WARN, "isamb_open");
324 reg->zei = zebraExplain_open (reg->records, reg->dh,
329 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
333 yaz_log (LOG_LOG, "zebra_register_open ok p=%p", reg);
337 void zebra_admin_shutdown (ZebraHandle zh)
339 zebra_mutex_cond_lock (&zh->service->session_lock);
340 zh->service->stop_flag = 1;
341 zebra_mutex_cond_unlock (&zh->service->session_lock);
344 void zebra_admin_start (ZebraHandle zh)
346 ZebraService zs = zh->service;
348 zebra_mutex_cond_lock (&zs->session_lock);
349 zebra_mutex_cond_unlock (&zs->session_lock);
352 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
354 yaz_log(LOG_LOG, "zebra_register_close p=%p", reg);
359 zebraExplain_close (reg->zei);
360 dict_close (reg->dict);
362 dict_close (reg->matchDict);
363 sortIdx_close (reg->sortIdx);
365 isams_close (reg->isams);
367 is_close (reg->isam);
369 isc_close (reg->isamc);
371 isamd_close (reg->isamd);
373 isamb_close (reg->isamb);
374 rec_close (®->records);
377 recTypes_destroy (reg->recTypes);
378 zebra_maps_close (reg->zebra_maps);
379 zebraRankDestroy (reg);
380 bfs_destroy (reg->bfs);
381 data1_destroy (reg->dh);
383 xfree (reg->key_buf);
386 yaz_log (LOG_LOG, "zebra_register_close 2");
389 void zebra_stop(ZebraService zs)
393 yaz_log (LOG_LOG, "zebra_stop");
395 zebra_mutex_cond_lock (&zs->session_lock);
398 zebra_close (zs->sessions);
401 zebra_mutex_cond_unlock (&zs->session_lock);
403 zebra_mutex_cond_destroy (&zs->session_lock);
406 passwd_db_close (zs->passwd_db);
408 res_close (zs->global_res);
409 xfree (zs->configName);
410 xfree (zs->path_root);
414 void zebra_close (ZebraHandle zh)
417 struct zebra_session **sp;
423 yaz_log (LOG_LOG, "zebra_close zh=%p", zh);
426 resultSetDestroy (zh, -1, 0, 0);
430 zebra_register_close (zh->service, zh->reg);
431 zebra_close_res (zh);
433 xfree (zh->record_encoding);
435 if (zh->iconv_to_utf8 != (iconv_t) (-1))
436 iconv_close (zh->iconv_to_utf8);
437 if (zh->iconv_from_utf8 != (iconv_t) (-1))
438 iconv_close (zh->iconv_from_utf8);
441 xfree (zh->admin_databaseName);
442 zebra_mutex_cond_lock (&zs->session_lock);
443 zebra_lock_destroy (zh->lock_normal);
444 zebra_lock_destroy (zh->lock_shadow);
456 // if (!zs->sessions && zs->stop_flag)
457 // zebra_register_deactivate(zs);
458 zebra_mutex_cond_unlock (&zs->session_lock);
459 xfree (zh->reg_name);
463 struct map_baseinfo {
469 char **new_basenames;
473 static Res zebra_open_res (ZebraHandle zh)
479 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
480 res = res_open (fname, zh->service->global_res);
482 res = zh->service->global_res;
484 else if (*zh->reg_name == 0)
486 res = zh->service->global_res;
490 yaz_log (LOG_WARN, "no register root specified");
491 return 0; /* no path for register - fail! */
496 static void zebra_close_res (ZebraHandle zh)
498 if (zh->res != zh->service->global_res)
503 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
505 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
509 assert (zh->reg == 0);
510 assert (*zh->reg_name == 0);
516 resultSetInvalidate (zh);
517 zebra_register_close (zh->service, zh->reg);
522 xfree (zh->reg_name);
523 zh->reg_name = xstrdup (new_reg);
525 xfree (zh->path_reg);
527 if (zh->service->path_root)
529 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
530 strlen(zh->reg_name) + 3);
531 strcpy (zh->path_reg, zh->service->path_root);
534 strcat (zh->path_reg, "/");
535 strcat (zh->path_reg, zh->reg_name);
538 zh->res = zebra_open_res (zh);
541 zebra_lock_destroy (zh->lock_normal);
545 zebra_lock_destroy (zh->lock_shadow);
551 const char *lock_area =res_get (zh->res, "lockDir");
553 if (!lock_area && zh->path_reg)
554 res_put (zh->res, "lockDir", zh->path_reg);
555 sprintf (fname, "norm.%s.LCK", zh->reg_name);
557 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
559 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
561 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
567 void map_basenames_func (void *vp, const char *name, const char *value)
569 struct map_baseinfo *p = (struct map_baseinfo *) vp;
571 char fromdb[128], todb[8][128];
574 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
575 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
576 todb[5], todb[6], todb[7]);
580 for (i = 0; i<p->num_bases; i++)
581 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
584 for (i = 0; i < no; i++)
586 if (p->new_num_bases == p->new_num_max)
588 p->new_basenames[(p->new_num_bases)++] =
589 nmem_strdup (p->mem, todb[i]);
595 void map_basenames (ZebraHandle zh, ODR stream,
596 int *num_bases, char ***basenames)
598 struct map_baseinfo info;
599 struct map_baseinfo *p = &info;
603 info.num_bases = *num_bases;
604 info.basenames = *basenames;
605 info.new_num_max = 128;
606 info.new_num_bases = 0;
607 info.new_basenames = (char **)
608 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
609 info.mem = stream->mem;
611 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
613 for (i = 0; i<p->num_bases; i++)
614 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
616 p->new_basenames[(p->new_num_bases)++] =
617 nmem_strdup (p->mem, p->basenames[i]);
619 *num_bases = info.new_num_bases;
620 *basenames = info.new_basenames;
621 for (i = 0; i<*num_bases; i++)
622 logf (LOG_LOG, "base %s", (*basenames)[i]);
625 int zebra_select_database (ZebraHandle zh, const char *basename)
627 return zebra_select_databases (zh, 1, &basename);
630 int zebra_select_databases (ZebraHandle zh, int num_bases,
631 const char **basenames)
643 for (i = 0; i < zh->num_basenames; i++)
644 xfree (zh->basenames[i]);
645 xfree (zh->basenames);
647 zh->num_basenames = num_bases;
648 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
649 for (i = 0; i < zh->num_basenames; i++)
650 zh->basenames[i] = xstrdup (basenames[i]);
652 cp = strrchr(basenames[0], '/');
655 len = cp - basenames[0];
656 new_reg = xmalloc (len + 1);
657 memcpy (new_reg, basenames[0], len);
661 new_reg = xstrdup ("");
662 for (i = 1; i<num_bases; i++)
666 cp1 = strrchr (basenames[i], '/');
674 if (len != cp1 - basenames[i] ||
675 memcmp (basenames[i], new_reg, len))
690 zebra_select_register (zh, new_reg);
700 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
701 Z_RPNQuery *query, const char *setname, int *hits)
706 if (zebra_begin_read (zh))
708 resultSetAddRPN (zh, decode, stream, query,
709 zh->num_basenames, zh->basenames, setname);
716 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
717 const char *setname, Z_RecordComposition *comp,
718 oid_value input_format, int num_recs,
719 ZebraRetrievalRecord *recs)
727 zh->errString = odr_strdup (stream, setname);
733 if (zebra_begin_read (zh))
736 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
737 for (i = 0; i<num_recs; i++)
738 pos_array[i] = recs[i].position;
739 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
742 logf (LOG_DEBUG, "zebraPosSetCreate error");
744 zh->errString = nmem_strdup (stream->mem, setname);
748 for (i = 0; i<num_recs; i++)
753 recs[i].format = VAL_SUTRS;
754 recs[i].len = strlen(poset[i].term);
755 recs[i].buf = poset[i].term;
756 recs[i].base = poset[i].db;
758 else if (poset[i].sysno)
761 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
762 stream, input_format, comp,
763 &recs[i].format, &recs[i].buf,
766 recs[i].errString = NULL;
772 sprintf (num_str, "%d", pos_array[i]);
774 zh->errString = odr_strdup (stream, num_str);
778 zebraPosSetDestroy (zh, poset, num_recs);
784 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
785 oid_value attributeset,
786 int *position, int *num_entries, ZebraScanEntry **entries,
789 if (zebra_begin_read (zh))
795 rpn_scan (zh, stream, zapt, attributeset,
796 zh->num_basenames, zh->basenames, position,
797 num_entries, entries, is_partial);
801 void zebra_sort (ZebraHandle zh, ODR stream,
802 int num_input_setnames, const char **input_setnames,
803 const char *output_setname, Z_SortKeySpecList *sort_sequence,
806 if (zebra_begin_read (zh))
808 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
809 output_setname, sort_sequence, sort_status);
813 int zebra_deleleResultSet(ZebraHandle zh, int function,
814 int num_setnames, char **setnames,
818 if (zebra_begin_read(zh))
819 return Z_DeleteStatus_systemProblemAtTarget;
822 case Z_DeleteRequest_list:
823 resultSetDestroy (zh, num_setnames, setnames, statuses);
825 case Z_DeleteRequest_all:
826 resultSetDestroy (zh, -1, 0, statuses);
830 status = Z_DeleteStatus_success;
831 for (i = 0; i<num_setnames; i++)
832 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
833 status = statuses[i];
837 int zebra_errCode (ZebraHandle zh)
842 const char *zebra_errString (ZebraHandle zh)
844 return diagbib1_str (zh->errCode);
847 char *zebra_errAdd (ZebraHandle zh)
849 return zh->errString;
852 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
854 ZebraService zs = zh->service;
855 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
857 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
861 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
865 void zebra_admin_import_begin (ZebraHandle zh, const char *database)
867 zebra_begin_trans (zh);
868 xfree (zh->admin_databaseName);
869 zh->admin_databaseName = xstrdup(database);
872 void zebra_admin_import_end (ZebraHandle zh)
874 zebra_end_trans (zh);
877 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
881 for (i = 0; i<segment->num_segmentRecords; i++)
883 Z_NamePlusRecord *npr = segment->segmentRecords[i];
884 const char *databaseName = npr->databaseName;
887 databaseName = zh->admin_databaseName;
888 printf ("--------------%d--------------------\n", i);
889 if (npr->which == Z_NamePlusRecord_intermediateFragment)
891 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
892 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
894 Odr_oct *oct = fragment->u.notExternallyTagged;
895 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
899 extract_rec_in_mem (zh, "grs.sgml",
907 0 /* match criteria */);
913 void zebra_admin_create (ZebraHandle zh, const char *database)
917 zebra_begin_trans (zh);
920 /* announce database */
921 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
922 /* explainDatabase */))
925 zh->errString = "Database already exist";
927 zebra_end_trans (zh);
930 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
931 const char *input_str, int input_len,
932 char *output_str, int output_len)
935 if (!zh->reg->zebra_maps)
937 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
938 input_str, input_len);
941 if (wrbuf_len(wrbuf) >= output_len)
943 if (wrbuf_len(wrbuf))
944 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
945 output_str[wrbuf_len(wrbuf)] = '\0';
946 return wrbuf_len(wrbuf);
950 void zebra_set_state (ZebraHandle zh, int val, int seqno)
952 char state_fname[256];
957 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
958 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
959 f = fopen (fname, "w");
961 yaz_log (LOG_LOG, "%c %d %ld", val, seqno, p);
962 fprintf (f, "%c %d %ld\n", val, seqno, p);
967 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
969 char state_fname[256];
973 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
974 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
975 f = fopen (fname, "r");
981 fscanf (f, "%c %d", val, seqno);
987 int zebra_begin_read (ZebraHandle zh)
997 if (zh->trans_no != 1)
999 zebra_flush_reg (zh);
1002 #if HAVE_SYS_TIMES_H
1011 zebra_get_state (zh, &val, &seqno);
1017 else if (seqno != zh->reg->seqno)
1019 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1020 seqno, zh->reg->seqno);
1023 else if (zh->reg->last_val != val)
1025 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1026 val, zh->reg->last_val);
1033 zebra_lock_r (zh->lock_shadow);
1035 zebra_lock_r (zh->lock_normal);
1038 zebra_register_close (zh->service, zh->reg);
1039 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1040 0, val == 'c' ? 1 : 0,
1041 zh->res, zh->path_reg);
1047 zh->reg->last_val = val;
1048 zh->reg->seqno = seqno;
1053 void zebra_end_read (ZebraHandle zh)
1057 if (zh->trans_no != 0)
1060 #if HAVE_SYS_TIMES_H
1062 logf (LOG_LOG, "user/system: %ld/%ld",
1063 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1064 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1068 zebra_unlock (zh->lock_normal);
1069 zebra_unlock (zh->lock_shadow);
1072 void zebra_begin_trans (ZebraHandle zh)
1077 const char *rval = 0;
1082 if (zh->trans_no != 1)
1087 yaz_log (LOG_LOG, "zebra_begin_trans");
1089 zh->records_inserted = 0;
1090 zh->records_updated = 0;
1091 zh->records_deleted = 0;
1092 zh->records_processed = 0;
1094 #if HAVE_SYS_TIMES_H
1099 if (zh->shadow_enable)
1100 rval = res_get (zh->res, "shadow");
1102 for (pass = 0; pass < 2; pass++)
1106 zebra_lock_r (zh->lock_normal);
1107 zebra_lock_w (zh->lock_shadow);
1111 zebra_lock_w (zh->lock_normal);
1112 zebra_lock_w (zh->lock_shadow);
1115 zebra_get_state (zh, &val, &seqno);
1118 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1119 zebra_unlock (zh->lock_shadow);
1120 zebra_unlock (zh->lock_normal);
1124 else if (val == 'd')
1128 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1130 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1131 bf_commitClean (bfs, rval);
1136 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1143 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1147 zebra_set_state (zh, 'd', seqno);
1149 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1150 1, rval ? 1 : 0, zh->res,
1153 zh->reg->seqno = seqno;
1156 void zebra_end_trans (ZebraHandle zh)
1163 if (zh->trans_no != 0)
1166 yaz_log (LOG_LOG, "zebra_end_trans");
1167 rval = res_get (zh->res, "shadow");
1169 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1171 zebra_flush_reg (zh);
1173 zebra_register_close (zh->service, zh->reg);
1177 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1178 zh->records_processed, zh->records_inserted,
1179 zh->records_updated, zh->records_deleted);
1181 zebra_get_state (zh, &val, &seqno);
1184 BFiles bfs = bfs_create (rval, zh->path_reg);
1185 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1186 bf_commitClean (bfs, rval);
1191 zebra_set_state (zh, 'o', seqno);
1193 zebra_unlock (zh->lock_shadow);
1194 zebra_unlock (zh->lock_normal);
1196 #if HAVE_SYS_TIMES_H
1198 logf (LOG_LOG, "user/system: %ld/%ld",
1199 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1200 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1205 void zebra_repository_update (ZebraHandle zh)
1207 zebra_begin_trans (zh);
1208 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1209 repositoryUpdate (zh);
1210 zebra_end_trans (zh);
1213 void zebra_repository_delete (ZebraHandle zh)
1215 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1216 repositoryDelete (zh);
1219 void zebra_repository_show (ZebraHandle zh)
1221 repositoryShow (zh);
1224 int zebra_commit (ZebraHandle zh)
1236 rval = res_get (zh->res, "shadow");
1239 logf (LOG_WARN, "Cannot perform commit");
1240 logf (LOG_WARN, "No shadow area defined");
1244 zebra_lock_w (zh->lock_normal);
1245 zebra_lock_r (zh->lock_shadow);
1247 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1249 zebra_get_state (zh, &val, &seqno);
1252 bf_cache (bfs, rval);
1253 if (bf_commitExists (bfs))
1255 zebra_set_state (zh, 'c', seqno);
1257 logf (LOG_LOG, "commit start");
1258 bf_commitExec (bfs);
1262 logf (LOG_LOG, "commit clean");
1263 bf_commitClean (bfs, rval);
1265 zebra_set_state (zh, 'o', seqno);
1269 logf (LOG_LOG, "nothing to commit");
1273 zebra_unlock (zh->lock_shadow);
1274 zebra_unlock (zh->lock_normal);
1278 int zebra_init (ZebraHandle zh)
1288 rval = res_get (zh->res, "shadow");
1290 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1293 bf_cache (bfs, rval);
1297 zebra_set_state (zh, 'o', 0);
1301 int zebra_compact (ZebraHandle zh)
1309 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1315 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1318 zebra_begin_trans (zh);
1319 extract_rec_in_mem (zh, "grs.sgml",
1321 "Default", /* database */
1322 0 /* delete_flag */,
1327 0 /* match criteria */);
1328 zebra_end_trans (zh);
1332 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1334 memcpy (&zh->rGroup, rg, sizeof(*rg));
1337 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1339 *code = zh->errCode;
1340 *addinfo = zh->errString;
1343 void zebra_shadow_enable (ZebraHandle zh, int value)
1345 zh->shadow_enable = value;
1348 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1350 xfree (zh->record_encoding);
1351 zh->record_encoding = xstrdup (encoding);