1 /* $Id: zebraapi.c,v 1.86 2003-02-27 23:08:10 pop 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);
412 void zebra_stop(ZebraService zs)
416 yaz_log (LOG_LOG, "zebra_stop");
420 zebra_close (zs->sessions);
423 zebra_mutex_cond_destroy (&zs->session_lock);
426 passwd_db_close (zs->passwd_db);
428 res_close (zs->global_res);
429 xfree (zs->configName);
430 xfree (zs->path_root);
434 void zebra_close (ZebraHandle zh)
437 struct zebra_session **sp;
446 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
449 resultSetDestroy (zh, -1, 0, 0);
452 zebra_register_close (zh->service, zh->reg);
453 zebra_close_res (zh);
455 xfree (zh->record_encoding);
457 for (i = 0; i < zh->num_basenames; i++)
458 xfree (zh->basenames[i]);
459 xfree (zh->basenames);
461 if (zh->iconv_to_utf8 != 0)
462 yaz_iconv_close (zh->iconv_to_utf8);
463 if (zh->iconv_from_utf8 != 0)
464 yaz_iconv_close (zh->iconv_from_utf8);
466 xfree (zh->admin_databaseName);
467 zebra_mutex_cond_lock (&zs->session_lock);
468 zebra_lock_destroy (zh->lock_normal);
469 zebra_lock_destroy (zh->lock_shadow);
481 zebra_mutex_cond_unlock (&zs->session_lock);
482 xfree (zh->reg_name);
483 zh->service=0; /* more likely to trigger an assert */
487 struct map_baseinfo {
493 char **new_basenames;
497 static Res zebra_open_res (ZebraHandle zh)
506 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
507 res = res_open (fname, zh->service->global_res);
509 res = zh->service->global_res;
511 else if (*zh->reg_name == 0)
513 res = zh->service->global_res;
517 yaz_log (LOG_WARN, "no register root specified");
518 return 0; /* no path for register - fail! */
523 static void zebra_close_res (ZebraHandle zh)
527 if (zh->res != zh->service->global_res)
532 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
536 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
540 assert (zh->reg == 0);
541 assert (*zh->reg_name == 0);
547 resultSetInvalidate (zh);
548 zebra_register_close (zh->service, zh->reg);
553 xfree (zh->reg_name);
554 zh->reg_name = xstrdup (new_reg);
556 xfree (zh->path_reg);
558 if (zh->service->path_root)
560 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
561 strlen(zh->reg_name) + 3);
562 strcpy (zh->path_reg, zh->service->path_root);
565 strcat (zh->path_reg, "/");
566 strcat (zh->path_reg, zh->reg_name);
569 zh->res = zebra_open_res (zh);
572 zebra_lock_destroy (zh->lock_normal);
576 zebra_lock_destroy (zh->lock_shadow);
582 const char *lock_area =res_get (zh->res, "lockDir");
584 if (!lock_area && zh->path_reg)
585 res_put (zh->res, "lockDir", zh->path_reg);
586 sprintf (fname, "norm.%s.LCK", zh->reg_name);
588 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
590 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
592 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
598 void map_basenames_func (void *vp, const char *name, const char *value)
600 struct map_baseinfo *p = (struct map_baseinfo *) vp;
602 char fromdb[128], todb[8][128];
605 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
606 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
607 todb[5], todb[6], todb[7]);
611 for (i = 0; i<p->num_bases; i++)
612 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
615 for (i = 0; i < no; i++)
617 if (p->new_num_bases == p->new_num_max)
619 p->new_basenames[(p->new_num_bases)++] =
620 nmem_strdup (p->mem, todb[i]);
626 void map_basenames (ZebraHandle zh, ODR stream,
627 int *num_bases, char ***basenames)
629 struct map_baseinfo info;
630 struct map_baseinfo *p = &info;
636 info.num_bases = *num_bases;
637 info.basenames = *basenames;
638 info.new_num_max = 128;
639 info.new_num_bases = 0;
640 info.new_basenames = (char **)
641 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
642 info.mem = stream->mem;
644 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
646 for (i = 0; i<p->num_bases; i++)
647 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
649 p->new_basenames[(p->new_num_bases)++] =
650 nmem_strdup (p->mem, p->basenames[i]);
652 *num_bases = info.new_num_bases;
653 *basenames = info.new_basenames;
654 for (i = 0; i<*num_bases; i++)
655 logf (LOG_LOG, "base %s", (*basenames)[i]);
658 int zebra_select_database (ZebraHandle zh, const char *basename)
662 return zebra_select_databases (zh, 1, &basename);
665 int zebra_select_databases (ZebraHandle zh, int num_bases,
666 const char **basenames)
680 for (i = 0; i < zh->num_basenames; i++)
681 xfree (zh->basenames[i]);
682 xfree (zh->basenames);
684 zh->num_basenames = num_bases;
685 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
686 for (i = 0; i < zh->num_basenames; i++)
687 zh->basenames[i] = xstrdup (basenames[i]);
689 cp = strrchr(basenames[0], '/');
692 len = cp - basenames[0];
693 new_reg = xmalloc (len + 1);
694 memcpy (new_reg, basenames[0], len);
698 new_reg = xstrdup ("");
699 for (i = 1; i<num_bases; i++)
703 cp1 = strrchr (basenames[i], '/');
711 if (len != cp1 - basenames[i] ||
712 memcmp (basenames[i], new_reg, len))
727 zebra_select_register (zh, new_reg);
734 if (!zh->lock_normal || !zh->lock_shadow)
742 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
743 Z_RPNQuery *query, const char *setname, int *hits)
750 if (zebra_begin_read (zh))
752 resultSetAddRPN (zh, decode, stream, query,
753 zh->num_basenames, zh->basenames, setname);
760 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
761 const char *setname, Z_RecordComposition *comp,
762 oid_value input_format, int num_recs,
763 ZebraRetrievalRecord *recs)
773 zh->errString = odr_strdup (stream, setname);
779 if (zebra_begin_read (zh))
782 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
783 for (i = 0; i<num_recs; i++)
784 pos_array[i] = recs[i].position;
785 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
788 logf (LOG_DEBUG, "zebraPosSetCreate error");
790 zh->errString = nmem_strdup (stream->mem, setname);
794 for (i = 0; i<num_recs; i++)
799 recs[i].format = VAL_SUTRS;
800 recs[i].len = strlen(poset[i].term);
801 recs[i].buf = poset[i].term;
802 recs[i].base = poset[i].db;
804 else if (poset[i].sysno)
807 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
808 stream, input_format, comp,
809 &recs[i].format, &recs[i].buf,
812 recs[i].errString = NULL;
818 sprintf (num_str, "%d", pos_array[i]);
820 zh->errString = odr_strdup (stream, num_str);
824 zebraPosSetDestroy (zh, poset, num_recs);
830 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
831 oid_value attributeset,
832 int *position, int *num_entries, ZebraScanEntry **entries,
837 if (zebra_begin_read (zh))
843 rpn_scan (zh, stream, zapt, attributeset,
844 zh->num_basenames, zh->basenames, position,
845 num_entries, entries, is_partial);
849 void zebra_sort (ZebraHandle zh, ODR stream,
850 int num_input_setnames, const char **input_setnames,
851 const char *output_setname, Z_SortKeySpecList *sort_sequence,
856 if (zebra_begin_read (zh))
858 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
859 output_setname, sort_sequence, sort_status);
863 int zebra_deleleResultSet(ZebraHandle zh, int function,
864 int num_setnames, char **setnames,
870 if (zebra_begin_read(zh))
871 return Z_DeleteStatus_systemProblemAtTarget;
874 case Z_DeleteRequest_list:
875 resultSetDestroy (zh, num_setnames, setnames, statuses);
877 case Z_DeleteRequest_all:
878 resultSetDestroy (zh, -1, 0, statuses);
882 status = Z_DeleteStatus_success;
883 for (i = 0; i<num_setnames; i++)
884 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
885 status = statuses[i];
889 int zebra_errCode (ZebraHandle zh)
896 const char *zebra_errString (ZebraHandle zh)
899 return diagbib1_str (zh->errCode);
903 char *zebra_errAdd (ZebraHandle zh)
906 return zh->errString;
910 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
916 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
918 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
922 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
926 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
927 const char *record_type)
931 if (zebra_select_database(zh, database))
933 zebra_begin_trans (zh);
934 xfree (zh->admin_databaseName);
935 zh->admin_databaseName = xstrdup(database);
938 void zebra_admin_import_end (ZebraHandle zh)
942 zebra_end_trans (zh);
945 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
951 for (i = 0; i<segment->num_segmentRecords; i++)
953 Z_NamePlusRecord *npr = segment->segmentRecords[i];
954 const char *databaseName = npr->databaseName;
957 databaseName = zh->admin_databaseName;
958 printf ("--------------%d--------------------\n", i);
959 if (npr->which == Z_NamePlusRecord_intermediateFragment)
961 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
962 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
964 Odr_oct *oct = fragment->u.notExternallyTagged;
965 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
969 extract_rec_in_mem (zh, "grs.sgml",
977 0 /* match criteria */);
983 int zebra_admin_exchange_record (ZebraHandle zh,
984 const char *database,
987 const char *recid_buf, size_t recid_len,
996 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
998 memcpy (recid_z, recid_buf, recid_len);
999 recid_z[recid_len] = 0;
1001 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1004 if (action == 1) /* fail if insert */
1006 memcpy (&sysno, rinfo+1, sizeof(sysno));
1010 if (action == 2 || action == 3) /* fail if delete or update */
1013 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1014 action == 3 ? 1 : 0 /* delete flag */,
1015 0, &sysno, 1, 1, 0);
1018 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1020 else if (action == 3)
1022 dict_delete (zh->reg->matchDict, recid_z);
1027 void zebra_admin_create (ZebraHandle zh, const char *database)
1033 if (zebra_select_database (zh, database))
1035 zebra_begin_trans (zh);
1038 /* announce database */
1039 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1040 /* explainDatabase */))
1043 zh->errString = "database already exist";
1045 zebra_end_trans (zh);
1048 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1049 const char *input_str, int input_len,
1050 char *output_str, int output_len)
1055 if (!zh->reg->zebra_maps)
1057 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1058 input_str, input_len);
1061 if (wrbuf_len(wrbuf) >= output_len)
1063 if (wrbuf_len(wrbuf))
1064 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1065 output_str[wrbuf_len(wrbuf)] = '\0';
1066 return wrbuf_len(wrbuf);
1070 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1072 char state_fname[256];
1079 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1080 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1081 f = fopen (fname, "w");
1083 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1084 fprintf (f, "%c %d %ld\n", val, seqno, p);
1089 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1091 char state_fname[256];
1097 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1098 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1099 f = fopen (fname, "r");
1105 fscanf (f, "%c %d", val, seqno);
1111 int zebra_begin_read (ZebraHandle zh)
1122 if (zh->trans_no != 1)
1124 zebra_flush_reg (zh);
1128 #if HAVE_SYS_TIMES_H
1137 if (!zh->lock_normal || !zh->lock_shadow)
1143 zebra_get_state (zh, &val, &seqno);
1149 else if (seqno != zh->reg->seqno)
1151 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1152 seqno, zh->reg->seqno);
1155 else if (zh->reg->last_val != val)
1157 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1158 val, zh->reg->last_val);
1165 zebra_lock_r (zh->lock_shadow);
1167 zebra_lock_r (zh->lock_normal);
1170 zebra_register_close (zh->service, zh->reg);
1171 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1172 0, val == 'c' ? 1 : 0,
1173 zh->res, zh->path_reg);
1179 zh->reg->last_val = val;
1180 zh->reg->seqno = seqno;
1185 void zebra_end_read (ZebraHandle zh)
1190 if (zh->trans_no != 0)
1192 #if HAVE_SYS_TIMES_H
1194 logf (LOG_LOG, "user/system: %ld/%ld",
1195 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1196 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1200 zebra_unlock (zh->lock_normal);
1201 zebra_unlock (zh->lock_shadow);
1204 void zebra_begin_trans (ZebraHandle zh)
1209 const char *rval = 0;
1215 if (zh->trans_no != 1)
1221 yaz_log (LOG_LOG, "zebra_begin_trans");
1223 zh->records_inserted = 0;
1224 zh->records_updated = 0;
1225 zh->records_deleted = 0;
1226 zh->records_processed = 0;
1228 #if HAVE_SYS_TIMES_H
1233 if (zh->shadow_enable)
1234 rval = res_get (zh->res, "shadow");
1236 for (pass = 0; pass < 2; pass++)
1240 zebra_lock_r (zh->lock_normal);
1241 zebra_lock_w (zh->lock_shadow);
1245 zebra_lock_w (zh->lock_normal);
1246 zebra_lock_w (zh->lock_shadow);
1249 zebra_get_state (zh, &val, &seqno);
1252 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1253 zebra_unlock (zh->lock_shadow);
1254 zebra_unlock (zh->lock_normal);
1258 else if (val == 'd')
1262 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1264 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1265 bf_commitClean (bfs, rval);
1270 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1277 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1281 zebra_set_state (zh, 'd', seqno);
1283 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1284 1, rval ? 1 : 0, zh->res,
1287 zh->reg->seqno = seqno;
1290 void zebra_end_trans (ZebraHandle zh) {
1291 ZebraTransactionStatus dummy;
1292 zebra_end_transaction(zh, &dummy);
1295 void zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1303 status->processed = 0;
1304 status->inserted = 0;
1305 status->updated = 0;
1306 status->deleted = 0;
1311 if (zh->trans_no != 0)
1313 yaz_log (LOG_LOG, "zebra_end_trans");
1314 rval = res_get (zh->res, "shadow");
1316 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1318 zebra_flush_reg (zh);
1320 zebra_register_close (zh->service, zh->reg);
1323 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1324 zh->records_processed, zh->records_inserted,
1325 zh->records_updated, zh->records_deleted);
1327 status->processed = zh->records_processed;
1328 status->inserted = zh->records_inserted;
1329 status->updated = zh->records_updated;
1330 status->deleted = zh->records_deleted;
1332 zebra_get_state (zh, &val, &seqno);
1335 BFiles bfs = bfs_create (rval, zh->path_reg);
1336 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1337 bf_commitClean (bfs, rval);
1342 zebra_set_state (zh, 'o', seqno);
1344 zebra_unlock (zh->lock_shadow);
1345 zebra_unlock (zh->lock_normal);
1347 #if HAVE_SYS_TIMES_H
1349 logf (LOG_LOG, "user/system: %ld/%ld",
1350 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1351 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1353 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1354 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1360 void zebra_repository_update (ZebraHandle zh)
1364 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1365 repositoryUpdate (zh);
1368 void zebra_repository_delete (ZebraHandle zh)
1372 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1373 repositoryDelete (zh);
1376 void zebra_repository_show (ZebraHandle zh)
1380 repositoryShow (zh);
1383 int zebra_commit (ZebraHandle zh)
1397 rval = res_get (zh->res, "shadow");
1400 logf (LOG_WARN, "Cannot perform commit");
1401 logf (LOG_WARN, "No shadow area defined");
1405 zebra_lock_w (zh->lock_normal);
1406 zebra_lock_r (zh->lock_shadow);
1408 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1410 zebra_get_state (zh, &val, &seqno);
1413 bf_cache (bfs, rval);
1414 if (bf_commitExists (bfs))
1416 zebra_set_state (zh, 'c', seqno);
1418 logf (LOG_LOG, "commit start");
1419 bf_commitExec (bfs);
1423 logf (LOG_LOG, "commit clean");
1424 bf_commitClean (bfs, rval);
1426 zebra_set_state (zh, 'o', seqno);
1430 logf (LOG_LOG, "nothing to commit");
1434 zebra_unlock (zh->lock_shadow);
1435 zebra_unlock (zh->lock_normal);
1439 int zebra_init (ZebraHandle zh)
1451 rval = res_get (zh->res, "shadow");
1453 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1456 bf_cache (bfs, rval);
1460 zebra_set_state (zh, 'o', 0);
1464 int zebra_compact (ZebraHandle zh)
1474 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1480 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1486 zebra_begin_trans (zh);
1488 return 0; /* bad sysno */
1489 extract_rec_in_mem (zh, "grs.sgml",
1491 "Default", /* database */
1492 0 /* delete_flag */,
1497 0 /* match criteria */);
1499 zebra_end_trans (zh);
1505 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1509 memcpy (&zh->rGroup, rg, sizeof(*rg));
1512 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1515 *code = zh->errCode;
1516 *addinfo = zh->errString;
1519 void zebra_shadow_enable (ZebraHandle zh, int value)
1523 zh->shadow_enable = value;
1526 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1530 xfree (zh->record_encoding);
1536 if (zh->iconv_to_utf8 != 0)
1537 yaz_iconv_close(zh->iconv_to_utf8);
1538 if (zh->iconv_from_utf8 != 0)
1539 yaz_iconv_close(zh->iconv_from_utf8);
1541 zh->record_encoding = xstrdup (encoding);
1543 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1546 yaz_iconv_open ("UTF-8", encoding);
1547 if (zh->iconv_to_utf8 == 0)
1548 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1549 zh->iconv_from_utf8 =
1550 yaz_iconv_open (encoding, "UTF-8");
1551 if (zh->iconv_to_utf8 == 0)
1552 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1557 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1561 res_put(zh->res, name, value);
1564 const char *zebra_get_resource(ZebraHandle zh,
1565 const char *name, const char *defaultvalue)
1569 return res_get_def( zh->res, name, (char *)defaultvalue);