1 /* $Id: zebraapi.c,v 1.81 2003-01-15 07:26:40 oleg 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_def(res, "profilePath",
197 DEFAULT_PROFILE_PATH));
198 data1_set_tabroot (reg->dh, reg_path);
199 reg->recTypes = recTypes_init (reg->dh);
200 recTypes_default_handlers (reg->recTypes);
202 reg->zebra_maps = zebra_maps_open (res, reg_path);
203 reg->rank_classes = NULL;
207 reg->keys.buf_max = 0;
221 reg->key_file_no = 0;
223 zebraRankInstall (reg, rank1_class);
225 recordCompression = res_get_def (res, "recordCompression", "none");
226 if (!strcmp (recordCompression, "none"))
227 record_compression = REC_COMPRESS_NONE;
228 if (!strcmp (recordCompression, "bzip2"))
229 record_compression = REC_COMPRESS_BZIP2;
231 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
233 logf (LOG_WARN, "rec_open");
238 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
240 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
242 logf (LOG_WARN, "dict_open");
245 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
247 logf (LOG_WARN, "sortIdx_open");
250 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
252 struct ISAMS_M_s isams_m;
253 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
254 key_isams_m(res, &isams_m))))
256 logf (LOG_WARN, "isams_open");
260 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
262 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
263 sizeof (struct it_key), res)))
265 logf (LOG_WARN, "is_open");
269 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
271 struct ISAMC_M_s isamc_m;
272 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
273 rw, key_isamc_m(res, &isamc_m))))
275 logf (LOG_WARN, "isc_open");
279 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
281 struct ISAMD_M_s isamd_m;
283 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
284 rw, key_isamd_m(res, &isamd_m))))
286 logf (LOG_WARN, "isamd_open");
290 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
292 struct ISAMC_M_s isamc_m;
294 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
295 rw, key_isamc_m(res, &isamc_m), 0)))
297 logf (LOG_WARN, "isamb_open");
301 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
303 struct ISAMC_M_s isamc_m;
305 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
306 rw, key_isamc_m(res, &isamc_m), 1)))
308 logf (LOG_WARN, "isamb_open");
312 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
314 struct ISAMC_M_s isamc_m;
316 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
317 rw, key_isamc_m(res, &isamc_m), -1)))
319 logf (LOG_WARN, "isamb_open");
323 reg->zei = zebraExplain_open (reg->records, reg->dh,
328 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
332 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
336 void zebra_admin_shutdown (ZebraHandle zh)
338 zebra_mutex_cond_lock (&zh->service->session_lock);
339 zh->service->stop_flag = 1;
340 zebra_mutex_cond_unlock (&zh->service->session_lock);
343 void zebra_admin_start (ZebraHandle zh)
345 ZebraService zs = zh->service;
347 zebra_mutex_cond_lock (&zs->session_lock);
348 zebra_mutex_cond_unlock (&zs->session_lock);
351 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
353 yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
358 zebraExplain_close (reg->zei);
359 dict_close (reg->dict);
361 dict_close (reg->matchDict);
362 sortIdx_close (reg->sortIdx);
364 isams_close (reg->isams);
366 is_close (reg->isam);
368 isc_close (reg->isamc);
370 isamd_close (reg->isamd);
372 isamb_close (reg->isamb);
373 rec_close (®->records);
376 recTypes_destroy (reg->recTypes);
377 zebra_maps_close (reg->zebra_maps);
378 zebraRankDestroy (reg);
379 bfs_destroy (reg->bfs);
380 data1_destroy (reg->dh);
382 xfree (reg->key_buf);
385 yaz_log(LOG_DEBUG, "zebra_register_close 2");
388 void zebra_stop(ZebraService zs)
392 yaz_log (LOG_LOG, "zebra_stop");
396 zebra_close (zs->sessions);
399 zebra_mutex_cond_destroy (&zs->session_lock);
402 passwd_db_close (zs->passwd_db);
404 res_close (zs->global_res);
405 xfree (zs->configName);
406 xfree (zs->path_root);
410 void zebra_close (ZebraHandle zh)
413 struct zebra_session **sp;
419 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
422 resultSetDestroy (zh, -1, 0, 0);
426 zebra_register_close (zh->service, zh->reg);
427 zebra_close_res (zh);
429 xfree (zh->record_encoding);
431 if (zh->iconv_to_utf8 != 0)
432 yaz_iconv_close (zh->iconv_to_utf8);
433 if (zh->iconv_from_utf8 != 0)
434 yaz_iconv_close (zh->iconv_from_utf8);
436 xfree (zh->admin_databaseName);
437 zebra_mutex_cond_lock (&zs->session_lock);
438 zebra_lock_destroy (zh->lock_normal);
439 zebra_lock_destroy (zh->lock_shadow);
451 zebra_mutex_cond_unlock (&zs->session_lock);
452 xfree (zh->reg_name);
456 struct map_baseinfo {
462 char **new_basenames;
466 static Res zebra_open_res (ZebraHandle zh)
472 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
473 res = res_open (fname, zh->service->global_res);
475 res = zh->service->global_res;
477 else if (*zh->reg_name == 0)
479 res = zh->service->global_res;
483 yaz_log (LOG_WARN, "no register root specified");
484 return 0; /* no path for register - fail! */
489 static void zebra_close_res (ZebraHandle zh)
491 if (zh->res != zh->service->global_res)
496 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
498 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
502 assert (zh->reg == 0);
503 assert (*zh->reg_name == 0);
509 resultSetInvalidate (zh);
510 zebra_register_close (zh->service, zh->reg);
515 xfree (zh->reg_name);
516 zh->reg_name = xstrdup (new_reg);
518 xfree (zh->path_reg);
520 if (zh->service->path_root)
522 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
523 strlen(zh->reg_name) + 3);
524 strcpy (zh->path_reg, zh->service->path_root);
527 strcat (zh->path_reg, "/");
528 strcat (zh->path_reg, zh->reg_name);
531 zh->res = zebra_open_res (zh);
534 zebra_lock_destroy (zh->lock_normal);
538 zebra_lock_destroy (zh->lock_shadow);
544 const char *lock_area =res_get (zh->res, "lockDir");
546 if (!lock_area && zh->path_reg)
547 res_put (zh->res, "lockDir", zh->path_reg);
548 sprintf (fname, "norm.%s.LCK", zh->reg_name);
550 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
552 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
554 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
560 void map_basenames_func (void *vp, const char *name, const char *value)
562 struct map_baseinfo *p = (struct map_baseinfo *) vp;
564 char fromdb[128], todb[8][128];
567 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
568 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
569 todb[5], todb[6], todb[7]);
573 for (i = 0; i<p->num_bases; i++)
574 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
577 for (i = 0; i < no; i++)
579 if (p->new_num_bases == p->new_num_max)
581 p->new_basenames[(p->new_num_bases)++] =
582 nmem_strdup (p->mem, todb[i]);
588 void map_basenames (ZebraHandle zh, ODR stream,
589 int *num_bases, char ***basenames)
591 struct map_baseinfo info;
592 struct map_baseinfo *p = &info;
596 info.num_bases = *num_bases;
597 info.basenames = *basenames;
598 info.new_num_max = 128;
599 info.new_num_bases = 0;
600 info.new_basenames = (char **)
601 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
602 info.mem = stream->mem;
604 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
606 for (i = 0; i<p->num_bases; i++)
607 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
609 p->new_basenames[(p->new_num_bases)++] =
610 nmem_strdup (p->mem, p->basenames[i]);
612 *num_bases = info.new_num_bases;
613 *basenames = info.new_basenames;
614 for (i = 0; i<*num_bases; i++)
615 logf (LOG_LOG, "base %s", (*basenames)[i]);
618 int zebra_select_database (ZebraHandle zh, const char *basename)
620 return zebra_select_databases (zh, 1, &basename);
623 int zebra_select_databases (ZebraHandle zh, int num_bases,
624 const char **basenames)
636 for (i = 0; i < zh->num_basenames; i++)
637 xfree (zh->basenames[i]);
638 xfree (zh->basenames);
640 zh->num_basenames = num_bases;
641 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
642 for (i = 0; i < zh->num_basenames; i++)
643 zh->basenames[i] = xstrdup (basenames[i]);
645 cp = strrchr(basenames[0], '/');
648 len = cp - basenames[0];
649 new_reg = xmalloc (len + 1);
650 memcpy (new_reg, basenames[0], len);
654 new_reg = xstrdup ("");
655 for (i = 1; i<num_bases; i++)
659 cp1 = strrchr (basenames[i], '/');
667 if (len != cp1 - basenames[i] ||
668 memcmp (basenames[i], new_reg, len))
683 zebra_select_register (zh, new_reg);
690 if (!zh->lock_normal || !zh->lock_shadow)
698 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
699 Z_RPNQuery *query, const char *setname, int *hits)
704 if (zebra_begin_read (zh))
706 resultSetAddRPN (zh, decode, stream, query,
707 zh->num_basenames, zh->basenames, setname);
714 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
715 const char *setname, Z_RecordComposition *comp,
716 oid_value input_format, int num_recs,
717 ZebraRetrievalRecord *recs)
725 zh->errString = odr_strdup (stream, setname);
731 if (zebra_begin_read (zh))
734 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
735 for (i = 0; i<num_recs; i++)
736 pos_array[i] = recs[i].position;
737 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
740 logf (LOG_DEBUG, "zebraPosSetCreate error");
742 zh->errString = nmem_strdup (stream->mem, setname);
746 for (i = 0; i<num_recs; i++)
751 recs[i].format = VAL_SUTRS;
752 recs[i].len = strlen(poset[i].term);
753 recs[i].buf = poset[i].term;
754 recs[i].base = poset[i].db;
756 else if (poset[i].sysno)
759 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
760 stream, input_format, comp,
761 &recs[i].format, &recs[i].buf,
764 recs[i].errString = NULL;
770 sprintf (num_str, "%d", pos_array[i]);
772 zh->errString = odr_strdup (stream, num_str);
776 zebraPosSetDestroy (zh, poset, num_recs);
782 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
783 oid_value attributeset,
784 int *position, int *num_entries, ZebraScanEntry **entries,
787 if (zebra_begin_read (zh))
793 rpn_scan (zh, stream, zapt, attributeset,
794 zh->num_basenames, zh->basenames, position,
795 num_entries, entries, is_partial);
799 void zebra_sort (ZebraHandle zh, ODR stream,
800 int num_input_setnames, const char **input_setnames,
801 const char *output_setname, Z_SortKeySpecList *sort_sequence,
804 if (zebra_begin_read (zh))
806 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
807 output_setname, sort_sequence, sort_status);
811 int zebra_deleleResultSet(ZebraHandle zh, int function,
812 int num_setnames, char **setnames,
816 if (zebra_begin_read(zh))
817 return Z_DeleteStatus_systemProblemAtTarget;
820 case Z_DeleteRequest_list:
821 resultSetDestroy (zh, num_setnames, setnames, statuses);
823 case Z_DeleteRequest_all:
824 resultSetDestroy (zh, -1, 0, statuses);
828 status = Z_DeleteStatus_success;
829 for (i = 0; i<num_setnames; i++)
830 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
831 status = statuses[i];
835 int zebra_errCode (ZebraHandle zh)
840 const char *zebra_errString (ZebraHandle zh)
842 return diagbib1_str (zh->errCode);
845 char *zebra_errAdd (ZebraHandle zh)
847 return zh->errString;
850 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
852 ZebraService zs = zh->service;
853 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
855 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
859 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
863 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
864 const char *record_type)
866 if (zebra_select_database(zh, database))
868 zebra_begin_trans (zh);
869 xfree (zh->admin_databaseName);
870 zh->admin_databaseName = xstrdup(database);
873 void zebra_admin_import_end (ZebraHandle zh)
875 zebra_end_trans (zh);
878 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
882 for (i = 0; i<segment->num_segmentRecords; i++)
884 Z_NamePlusRecord *npr = segment->segmentRecords[i];
885 const char *databaseName = npr->databaseName;
888 databaseName = zh->admin_databaseName;
889 printf ("--------------%d--------------------\n", i);
890 if (npr->which == Z_NamePlusRecord_intermediateFragment)
892 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
893 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
895 Odr_oct *oct = fragment->u.notExternallyTagged;
896 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
900 extract_rec_in_mem (zh, "grs.sgml",
908 0 /* match criteria */);
914 int zebra_admin_exchange_record (ZebraHandle zh,
915 const char *database,
918 const char *recid_buf, size_t recid_len,
925 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
927 memcpy (recid_z, recid_buf, recid_len);
928 recid_z[recid_len] = 0;
930 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
933 if (action == 1) /* fail if insert */
935 memcpy (&sysno, rinfo+1, sizeof(sysno));
939 if (action == 2 || action == 3) /* fail if delete or update */
942 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
943 action == 3 ? 1 : 0 /* delete flag */,
947 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
949 else if (action == 3)
951 dict_delete (zh->reg->matchDict, recid_z);
956 void zebra_admin_create (ZebraHandle zh, const char *database)
960 if (zebra_select_database (zh, database))
962 zebra_begin_trans (zh);
965 /* announce database */
966 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
967 /* explainDatabase */))
970 zh->errString = "database already exist";
972 zebra_end_trans (zh);
975 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
976 const char *input_str, int input_len,
977 char *output_str, int output_len)
980 if (!zh->reg->zebra_maps)
982 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
983 input_str, input_len);
986 if (wrbuf_len(wrbuf) >= output_len)
988 if (wrbuf_len(wrbuf))
989 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
990 output_str[wrbuf_len(wrbuf)] = '\0';
991 return wrbuf_len(wrbuf);
995 void zebra_set_state (ZebraHandle zh, int val, int seqno)
997 char state_fname[256];
1002 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1003 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1004 f = fopen (fname, "w");
1006 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1007 fprintf (f, "%c %d %ld\n", val, seqno, p);
1012 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1014 char state_fname[256];
1018 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1019 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1020 f = fopen (fname, "r");
1026 fscanf (f, "%c %d", val, seqno);
1032 int zebra_begin_read (ZebraHandle zh)
1042 if (zh->trans_no != 1)
1044 zebra_flush_reg (zh);
1047 #if HAVE_SYS_TIMES_H
1056 if (!zh->lock_normal || !zh->lock_shadow)
1062 zebra_get_state (zh, &val, &seqno);
1068 else if (seqno != zh->reg->seqno)
1070 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1071 seqno, zh->reg->seqno);
1074 else if (zh->reg->last_val != val)
1076 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1077 val, zh->reg->last_val);
1084 zebra_lock_r (zh->lock_shadow);
1086 zebra_lock_r (zh->lock_normal);
1089 zebra_register_close (zh->service, zh->reg);
1090 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1091 0, val == 'c' ? 1 : 0,
1092 zh->res, zh->path_reg);
1098 zh->reg->last_val = val;
1099 zh->reg->seqno = seqno;
1104 void zebra_end_read (ZebraHandle zh)
1108 if (zh->trans_no != 0)
1111 #if HAVE_SYS_TIMES_H
1113 logf (LOG_LOG, "user/system: %ld/%ld",
1114 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1115 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1119 zebra_unlock (zh->lock_normal);
1120 zebra_unlock (zh->lock_shadow);
1123 void zebra_begin_trans (ZebraHandle zh)
1128 const char *rval = 0;
1133 if (zh->trans_no != 1)
1138 yaz_log (LOG_LOG, "zebra_begin_trans");
1140 zh->records_inserted = 0;
1141 zh->records_updated = 0;
1142 zh->records_deleted = 0;
1143 zh->records_processed = 0;
1145 #if HAVE_SYS_TIMES_H
1150 if (zh->shadow_enable)
1151 rval = res_get (zh->res, "shadow");
1153 for (pass = 0; pass < 2; pass++)
1157 zebra_lock_r (zh->lock_normal);
1158 zebra_lock_w (zh->lock_shadow);
1162 zebra_lock_w (zh->lock_normal);
1163 zebra_lock_w (zh->lock_shadow);
1166 zebra_get_state (zh, &val, &seqno);
1169 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1170 zebra_unlock (zh->lock_shadow);
1171 zebra_unlock (zh->lock_normal);
1175 else if (val == 'd')
1179 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1181 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1182 bf_commitClean (bfs, rval);
1187 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1194 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1198 zebra_set_state (zh, 'd', seqno);
1200 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1201 1, rval ? 1 : 0, zh->res,
1204 zh->reg->seqno = seqno;
1207 void zebra_end_trans (ZebraHandle zh)
1214 if (zh->trans_no != 0)
1217 yaz_log (LOG_LOG, "zebra_end_trans");
1218 rval = res_get (zh->res, "shadow");
1220 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1222 zebra_flush_reg (zh);
1224 zebra_register_close (zh->service, zh->reg);
1227 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1228 zh->records_processed, zh->records_inserted,
1229 zh->records_updated, zh->records_deleted);
1231 zebra_get_state (zh, &val, &seqno);
1234 BFiles bfs = bfs_create (rval, zh->path_reg);
1235 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1236 bf_commitClean (bfs, rval);
1241 zebra_set_state (zh, 'o', seqno);
1243 zebra_unlock (zh->lock_shadow);
1244 zebra_unlock (zh->lock_normal);
1246 #if HAVE_SYS_TIMES_H
1248 logf (LOG_LOG, "user/system: %ld/%ld",
1249 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1250 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1255 void zebra_repository_update (ZebraHandle zh)
1257 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1258 repositoryUpdate (zh);
1261 void zebra_repository_delete (ZebraHandle zh)
1263 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1264 repositoryDelete (zh);
1267 void zebra_repository_show (ZebraHandle zh)
1269 repositoryShow (zh);
1272 int zebra_commit (ZebraHandle zh)
1284 rval = res_get (zh->res, "shadow");
1287 logf (LOG_WARN, "Cannot perform commit");
1288 logf (LOG_WARN, "No shadow area defined");
1292 zebra_lock_w (zh->lock_normal);
1293 zebra_lock_r (zh->lock_shadow);
1295 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1297 zebra_get_state (zh, &val, &seqno);
1300 bf_cache (bfs, rval);
1301 if (bf_commitExists (bfs))
1303 zebra_set_state (zh, 'c', seqno);
1305 logf (LOG_LOG, "commit start");
1306 bf_commitExec (bfs);
1310 logf (LOG_LOG, "commit clean");
1311 bf_commitClean (bfs, rval);
1313 zebra_set_state (zh, 'o', seqno);
1317 logf (LOG_LOG, "nothing to commit");
1321 zebra_unlock (zh->lock_shadow);
1322 zebra_unlock (zh->lock_normal);
1326 int zebra_init (ZebraHandle zh)
1336 rval = res_get (zh->res, "shadow");
1338 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1341 bf_cache (bfs, rval);
1345 zebra_set_state (zh, 'o', 0);
1349 int zebra_compact (ZebraHandle zh)
1357 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1363 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1366 zebra_begin_trans (zh);
1367 extract_rec_in_mem (zh, "grs.sgml",
1369 "Default", /* database */
1370 0 /* delete_flag */,
1375 0 /* match criteria */);
1376 zebra_end_trans (zh);
1380 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1382 memcpy (&zh->rGroup, rg, sizeof(*rg));
1385 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1387 *code = zh->errCode;
1388 *addinfo = zh->errString;
1391 void zebra_shadow_enable (ZebraHandle zh, int value)
1393 zh->shadow_enable = value;
1396 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1398 xfree (zh->record_encoding);
1404 if (zh->iconv_to_utf8 != 0)
1405 yaz_iconv_close(zh->iconv_to_utf8);
1406 if (zh->iconv_from_utf8 != 0)
1407 yaz_iconv_close(zh->iconv_from_utf8);
1409 zh->record_encoding = xstrdup (encoding);
1411 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1414 yaz_iconv_open ("UTF-8", encoding);
1415 if (zh->iconv_to_utf8 == 0)
1416 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1417 zh->iconv_from_utf8 =
1418 yaz_iconv_open (encoding, "UTF-8");
1419 if (zh->iconv_to_utf8 == 0)
1420 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1425 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1427 res_put(zh->res, name, value);
1430 const char *zebra_get_resource(ZebraHandle zh,
1431 const char *name, const char *defaultvalue)
1433 return res_get_def( zh->res, name, (char *)defaultvalue);