1 /* $Id: zebraapi.c,v 1.67 2002-08-18 10:20:35 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 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_DEBUG, "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_DEBUG, "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_DEBUG, "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_DEBUG, "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_DEBUG, "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_DEBUG, "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);
697 if (!zh->lock_normal || !zh->lock_shadow)
705 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
706 Z_RPNQuery *query, const char *setname, int *hits)
711 if (zebra_begin_read (zh))
713 resultSetAddRPN (zh, decode, stream, query,
714 zh->num_basenames, zh->basenames, setname);
721 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
722 const char *setname, Z_RecordComposition *comp,
723 oid_value input_format, int num_recs,
724 ZebraRetrievalRecord *recs)
732 zh->errString = odr_strdup (stream, setname);
738 if (zebra_begin_read (zh))
741 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
742 for (i = 0; i<num_recs; i++)
743 pos_array[i] = recs[i].position;
744 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
747 logf (LOG_DEBUG, "zebraPosSetCreate error");
749 zh->errString = nmem_strdup (stream->mem, setname);
753 for (i = 0; i<num_recs; i++)
758 recs[i].format = VAL_SUTRS;
759 recs[i].len = strlen(poset[i].term);
760 recs[i].buf = poset[i].term;
761 recs[i].base = poset[i].db;
763 else if (poset[i].sysno)
766 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
767 stream, input_format, comp,
768 &recs[i].format, &recs[i].buf,
771 recs[i].errString = NULL;
777 sprintf (num_str, "%d", pos_array[i]);
779 zh->errString = odr_strdup (stream, num_str);
783 zebraPosSetDestroy (zh, poset, num_recs);
789 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
790 oid_value attributeset,
791 int *position, int *num_entries, ZebraScanEntry **entries,
794 if (zebra_begin_read (zh))
800 rpn_scan (zh, stream, zapt, attributeset,
801 zh->num_basenames, zh->basenames, position,
802 num_entries, entries, is_partial);
806 void zebra_sort (ZebraHandle zh, ODR stream,
807 int num_input_setnames, const char **input_setnames,
808 const char *output_setname, Z_SortKeySpecList *sort_sequence,
811 if (zebra_begin_read (zh))
813 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
814 output_setname, sort_sequence, sort_status);
818 int zebra_deleleResultSet(ZebraHandle zh, int function,
819 int num_setnames, char **setnames,
823 if (zebra_begin_read(zh))
824 return Z_DeleteStatus_systemProblemAtTarget;
827 case Z_DeleteRequest_list:
828 resultSetDestroy (zh, num_setnames, setnames, statuses);
830 case Z_DeleteRequest_all:
831 resultSetDestroy (zh, -1, 0, statuses);
835 status = Z_DeleteStatus_success;
836 for (i = 0; i<num_setnames; i++)
837 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
838 status = statuses[i];
842 int zebra_errCode (ZebraHandle zh)
847 const char *zebra_errString (ZebraHandle zh)
849 return diagbib1_str (zh->errCode);
852 char *zebra_errAdd (ZebraHandle zh)
854 return zh->errString;
857 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
859 ZebraService zs = zh->service;
860 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
862 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
866 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
870 void zebra_admin_import_begin (ZebraHandle zh, const char *database)
872 zebra_begin_trans (zh);
873 xfree (zh->admin_databaseName);
874 zh->admin_databaseName = xstrdup(database);
877 void zebra_admin_import_end (ZebraHandle zh)
879 zebra_end_trans (zh);
882 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
886 for (i = 0; i<segment->num_segmentRecords; i++)
888 Z_NamePlusRecord *npr = segment->segmentRecords[i];
889 const char *databaseName = npr->databaseName;
892 databaseName = zh->admin_databaseName;
893 printf ("--------------%d--------------------\n", i);
894 if (npr->which == Z_NamePlusRecord_intermediateFragment)
896 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
897 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
899 Odr_oct *oct = fragment->u.notExternallyTagged;
900 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
904 extract_rec_in_mem (zh, "grs.sgml",
912 0 /* match criteria */);
918 void zebra_admin_create (ZebraHandle zh, const char *database)
922 zebra_begin_trans (zh);
925 /* announce database */
926 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
927 /* explainDatabase */))
930 zh->errString = "Database already exist";
932 zebra_end_trans (zh);
935 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
936 const char *input_str, int input_len,
937 char *output_str, int output_len)
940 if (!zh->reg->zebra_maps)
942 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
943 input_str, input_len);
946 if (wrbuf_len(wrbuf) >= output_len)
948 if (wrbuf_len(wrbuf))
949 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
950 output_str[wrbuf_len(wrbuf)] = '\0';
951 return wrbuf_len(wrbuf);
955 void zebra_set_state (ZebraHandle zh, int val, int seqno)
957 char state_fname[256];
962 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
963 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
964 f = fopen (fname, "w");
966 yaz_log (LOG_LOG, "%c %d %ld", val, seqno, p);
967 fprintf (f, "%c %d %ld\n", val, seqno, p);
972 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
974 char state_fname[256];
978 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
979 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
980 f = fopen (fname, "r");
986 fscanf (f, "%c %d", val, seqno);
992 int zebra_begin_read (ZebraHandle zh)
1002 if (zh->trans_no != 1)
1004 zebra_flush_reg (zh);
1007 #if HAVE_SYS_TIMES_H
1016 if (!zh->lock_normal || !zh->lock_shadow)
1022 zebra_get_state (zh, &val, &seqno);
1028 else if (seqno != zh->reg->seqno)
1030 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1031 seqno, zh->reg->seqno);
1034 else if (zh->reg->last_val != val)
1036 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1037 val, zh->reg->last_val);
1044 zebra_lock_r (zh->lock_shadow);
1046 zebra_lock_r (zh->lock_normal);
1049 zebra_register_close (zh->service, zh->reg);
1050 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1051 0, val == 'c' ? 1 : 0,
1052 zh->res, zh->path_reg);
1058 zh->reg->last_val = val;
1059 zh->reg->seqno = seqno;
1064 void zebra_end_read (ZebraHandle zh)
1068 if (zh->trans_no != 0)
1071 #if HAVE_SYS_TIMES_H
1073 logf (LOG_LOG, "user/system: %ld/%ld",
1074 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1075 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1079 zebra_unlock (zh->lock_normal);
1080 zebra_unlock (zh->lock_shadow);
1083 void zebra_begin_trans (ZebraHandle zh)
1088 const char *rval = 0;
1093 if (zh->trans_no != 1)
1098 yaz_log (LOG_LOG, "zebra_begin_trans");
1100 zh->records_inserted = 0;
1101 zh->records_updated = 0;
1102 zh->records_deleted = 0;
1103 zh->records_processed = 0;
1105 #if HAVE_SYS_TIMES_H
1110 if (zh->shadow_enable)
1111 rval = res_get (zh->res, "shadow");
1113 for (pass = 0; pass < 2; pass++)
1117 zebra_lock_r (zh->lock_normal);
1118 zebra_lock_w (zh->lock_shadow);
1122 zebra_lock_w (zh->lock_normal);
1123 zebra_lock_w (zh->lock_shadow);
1126 zebra_get_state (zh, &val, &seqno);
1129 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1130 zebra_unlock (zh->lock_shadow);
1131 zebra_unlock (zh->lock_normal);
1135 else if (val == 'd')
1139 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1141 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1142 bf_commitClean (bfs, rval);
1147 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1154 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1158 zebra_set_state (zh, 'd', seqno);
1160 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1161 1, rval ? 1 : 0, zh->res,
1164 zh->reg->seqno = seqno;
1167 void zebra_end_trans (ZebraHandle zh)
1174 if (zh->trans_no != 0)
1177 yaz_log (LOG_LOG, "zebra_end_trans");
1178 rval = res_get (zh->res, "shadow");
1180 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1182 zebra_flush_reg (zh);
1184 zebra_register_close (zh->service, zh->reg);
1188 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1189 zh->records_processed, zh->records_inserted,
1190 zh->records_updated, zh->records_deleted);
1192 zebra_get_state (zh, &val, &seqno);
1195 BFiles bfs = bfs_create (rval, zh->path_reg);
1196 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1197 bf_commitClean (bfs, rval);
1202 zebra_set_state (zh, 'o', seqno);
1204 zebra_unlock (zh->lock_shadow);
1205 zebra_unlock (zh->lock_normal);
1207 #if HAVE_SYS_TIMES_H
1209 logf (LOG_LOG, "user/system: %ld/%ld",
1210 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1211 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1216 void zebra_repository_update (ZebraHandle zh)
1218 zebra_begin_trans (zh);
1219 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1220 repositoryUpdate (zh);
1221 zebra_end_trans (zh);
1224 void zebra_repository_delete (ZebraHandle zh)
1226 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1227 repositoryDelete (zh);
1230 void zebra_repository_show (ZebraHandle zh)
1232 repositoryShow (zh);
1235 int zebra_commit (ZebraHandle zh)
1247 rval = res_get (zh->res, "shadow");
1250 logf (LOG_WARN, "Cannot perform commit");
1251 logf (LOG_WARN, "No shadow area defined");
1255 zebra_lock_w (zh->lock_normal);
1256 zebra_lock_r (zh->lock_shadow);
1258 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1260 zebra_get_state (zh, &val, &seqno);
1263 bf_cache (bfs, rval);
1264 if (bf_commitExists (bfs))
1266 zebra_set_state (zh, 'c', seqno);
1268 logf (LOG_LOG, "commit start");
1269 bf_commitExec (bfs);
1273 logf (LOG_LOG, "commit clean");
1274 bf_commitClean (bfs, rval);
1276 zebra_set_state (zh, 'o', seqno);
1280 logf (LOG_LOG, "nothing to commit");
1284 zebra_unlock (zh->lock_shadow);
1285 zebra_unlock (zh->lock_normal);
1289 int zebra_init (ZebraHandle zh)
1299 rval = res_get (zh->res, "shadow");
1301 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1304 bf_cache (bfs, rval);
1308 zebra_set_state (zh, 'o', 0);
1312 int zebra_compact (ZebraHandle zh)
1320 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1326 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1329 zebra_begin_trans (zh);
1330 extract_rec_in_mem (zh, "grs.sgml",
1332 "Default", /* database */
1333 0 /* delete_flag */,
1338 0 /* match criteria */);
1339 zebra_end_trans (zh);
1343 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1345 memcpy (&zh->rGroup, rg, sizeof(*rg));
1348 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1350 *code = zh->errCode;
1351 *addinfo = zh->errString;
1354 void zebra_shadow_enable (ZebraHandle zh, int value)
1356 zh->shadow_enable = value;
1359 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1361 xfree (zh->record_encoding);
1362 zh->record_encoding = xstrdup (encoding);