1 /* $Id: zebraapi.c,v 1.87 2003-03-04 23:05:30 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>
40 /* simple asserts to validate the most essential input args */
41 #define ASSERTZH assert(zh && zh->service)
42 #define ASSERTZHRES assert(zh && zh->service && zh->res)
43 #define ASSERTZS assert(zs)
45 static Res zebra_open_res (ZebraHandle zh);
46 static void zebra_close_res (ZebraHandle zh);
49 static void zebra_chdir (ZebraService zs)
53 dir = res_get (zs->global_res, "chdir");
56 logf (LOG_DEBUG, "chdir %s", dir);
64 static void zebra_flush_reg (ZebraHandle zh)
68 zebraExplain_flush (zh->reg->zei, zh);
70 extract_flushWriteKeys (zh);
71 zebra_index_merge (zh);
74 static struct zebra_register *zebra_register_open (ZebraService zs,
76 int rw, int useshadow,
78 const char *reg_path);
79 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
81 ZebraHandle zebra_open (ZebraService zs)
84 const char *default_encoding;
90 zh = (ZebraHandle) xmalloc (sizeof(*zh));
91 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
94 zh->reg = 0; /* no register attached yet */
101 zh->reg_name = xstrdup ("");
103 zh->num_basenames = 0;
111 zh->admin_databaseName = 0;
113 zh->shadow_enable = 1;
115 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
116 zh->record_encoding = xstrdup (default_encoding);
119 yaz_iconv_open ("UTF-8", default_encoding);
120 if (zh->iconv_to_utf8 == 0)
121 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
123 zh->iconv_from_utf8 =
124 yaz_iconv_open (default_encoding, "UTF-8");
125 if (zh->iconv_to_utf8 == 0)
126 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
129 zebra_mutex_cond_lock (&zs->session_lock);
131 zh->next = zs->sessions;
134 zebra_mutex_cond_unlock (&zs->session_lock);
139 ZebraService zebra_start (const char *configName)
143 yaz_log (LOG_LOG, "zebra_start %s", configName);
145 if ((res = res_open (configName, 0)))
147 ZebraService zh = xmalloc (sizeof(*zh));
149 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
151 zh->global_res = res;
152 zh->configName = xstrdup(configName);
157 zebra_mutex_cond_init (&zh->session_lock);
158 if (!res_get (zh->global_res, "passwd"))
159 zh->passwd_db = NULL;
162 zh->passwd_db = passwd_db_open ();
164 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
166 passwd_db_file (zh->passwd_db,
167 res_get (zh->global_res, "passwd"));
169 zh->path_root = res_get (zh->global_res, "root");
176 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
177 int rw, int useshadow, Res res,
178 const char *reg_path)
180 struct zebra_register *reg;
181 int record_compression = REC_COMPRESS_NONE;
182 char *recordCompression = 0;
186 reg = xmalloc (sizeof(*reg));
189 reg->name = xstrdup (name);
196 yaz_log (LOG_DEBUG, "zebra_register_open rw = %d useshadow=%d p=%p",
199 reg->dh = data1_createx (DATA1_FLAG_XML);
202 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
205 data1_destroy(reg->dh);
209 bf_cache (reg->bfs, res_get (res, "shadow"));
210 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
211 DEFAULT_PROFILE_PATH));
212 data1_set_tabroot (reg->dh, reg_path);
213 reg->recTypes = recTypes_init (reg->dh);
214 recTypes_default_handlers (reg->recTypes);
216 reg->zebra_maps = zebra_maps_open (res, reg_path);
217 reg->rank_classes = NULL;
221 reg->keys.buf_max = 0;
223 reg->sortKeys.buf = 0;
224 reg->sortKeys.buf_max = 0;
237 reg->key_file_no = 0;
239 zebraRankInstall (reg, rank1_class);
240 zebraRankInstall (reg, rankzv_class);
242 recordCompression = res_get_def (res, "recordCompression", "none");
243 if (!strcmp (recordCompression, "none"))
244 record_compression = REC_COMPRESS_NONE;
245 if (!strcmp (recordCompression, "bzip2"))
246 record_compression = REC_COMPRESS_BZIP2;
248 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
250 logf (LOG_WARN, "rec_open");
255 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
257 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
259 logf (LOG_WARN, "dict_open");
262 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
264 logf (LOG_WARN, "sortIdx_open");
267 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
269 struct ISAMS_M_s isams_m;
270 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
271 key_isams_m(res, &isams_m))))
273 logf (LOG_WARN, "isams_open");
277 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
279 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
280 sizeof (struct it_key), res)))
282 logf (LOG_WARN, "is_open");
286 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
288 struct ISAMC_M_s isamc_m;
289 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
290 rw, key_isamc_m(res, &isamc_m))))
292 logf (LOG_WARN, "isc_open");
296 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
298 struct ISAMD_M_s isamd_m;
300 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
301 rw, key_isamd_m(res, &isamd_m))))
303 logf (LOG_WARN, "isamd_open");
307 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
309 struct ISAMC_M_s isamc_m;
311 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
312 rw, key_isamc_m(res, &isamc_m), 0)))
314 logf (LOG_WARN, "isamb_open");
318 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
320 struct ISAMC_M_s isamc_m;
322 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
323 rw, key_isamc_m(res, &isamc_m), 1)))
325 logf (LOG_WARN, "isamb_open");
329 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
331 struct ISAMC_M_s isamc_m;
333 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
334 rw, key_isamc_m(res, &isamc_m), -1)))
336 logf (LOG_WARN, "isamb_open");
340 reg->zei = zebraExplain_open (reg->records, reg->dh,
345 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
349 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
353 void zebra_admin_shutdown (ZebraHandle zh)
358 zebra_mutex_cond_lock (&zh->service->session_lock);
359 zh->service->stop_flag = 1;
360 zebra_mutex_cond_unlock (&zh->service->session_lock);
363 void zebra_admin_start (ZebraHandle zh)
369 zebra_mutex_cond_lock (&zs->session_lock);
370 zebra_mutex_cond_unlock (&zs->session_lock);
373 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
376 yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
381 zebraExplain_close (reg->zei);
382 dict_close (reg->dict);
384 dict_close (reg->matchDict);
385 sortIdx_close (reg->sortIdx);
387 isams_close (reg->isams);
389 is_close (reg->isam);
391 isc_close (reg->isamc);
393 isamd_close (reg->isamd);
395 isamb_close (reg->isamb);
396 rec_close (®->records);
399 recTypes_destroy (reg->recTypes);
400 zebra_maps_close (reg->zebra_maps);
401 zebraRankDestroy (reg);
402 bfs_destroy (reg->bfs);
403 data1_destroy (reg->dh);
405 xfree (reg->sortKeys.buf);
406 xfree (reg->keys.buf);
408 xfree (reg->key_buf);
413 void zebra_stop(ZebraService zs)
417 yaz_log (LOG_LOG, "zebra_stop");
421 zebra_close (zs->sessions);
424 zebra_mutex_cond_destroy (&zs->session_lock);
427 passwd_db_close (zs->passwd_db);
429 res_close (zs->global_res);
430 xfree (zs->configName);
431 xfree (zs->path_root);
435 void zebra_close (ZebraHandle zh)
438 struct zebra_session **sp;
447 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
450 resultSetDestroy (zh, -1, 0, 0);
453 zebra_register_close (zh->service, zh->reg);
454 zebra_close_res (zh);
456 xfree (zh->record_encoding);
458 for (i = 0; i < zh->num_basenames; i++)
459 xfree (zh->basenames[i]);
460 xfree (zh->basenames);
462 if (zh->iconv_to_utf8 != 0)
463 yaz_iconv_close (zh->iconv_to_utf8);
464 if (zh->iconv_from_utf8 != 0)
465 yaz_iconv_close (zh->iconv_from_utf8);
467 xfree (zh->admin_databaseName);
468 zebra_mutex_cond_lock (&zs->session_lock);
469 zebra_lock_destroy (zh->lock_normal);
470 zebra_lock_destroy (zh->lock_shadow);
482 zebra_mutex_cond_unlock (&zs->session_lock);
483 xfree (zh->reg_name);
484 zh->service=0; /* more likely to trigger an assert */
488 struct map_baseinfo {
494 char **new_basenames;
498 static Res zebra_open_res (ZebraHandle zh)
507 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
508 res = res_open (fname, zh->service->global_res);
510 res = zh->service->global_res;
512 else if (*zh->reg_name == 0)
514 res = zh->service->global_res;
518 yaz_log (LOG_WARN, "no register root specified");
519 return 0; /* no path for register - fail! */
524 static void zebra_close_res (ZebraHandle zh)
528 if (zh->res != zh->service->global_res)
533 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
537 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
541 assert (zh->reg == 0);
542 assert (*zh->reg_name == 0);
548 resultSetInvalidate (zh);
549 zebra_register_close (zh->service, zh->reg);
554 xfree (zh->reg_name);
555 zh->reg_name = xstrdup (new_reg);
557 xfree (zh->path_reg);
559 if (zh->service->path_root)
561 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
562 strlen(zh->reg_name) + 3);
563 strcpy (zh->path_reg, zh->service->path_root);
566 strcat (zh->path_reg, "/");
567 strcat (zh->path_reg, zh->reg_name);
570 zh->res = zebra_open_res (zh);
573 zebra_lock_destroy (zh->lock_normal);
577 zebra_lock_destroy (zh->lock_shadow);
583 const char *lock_area =res_get (zh->res, "lockDir");
585 if (!lock_area && zh->path_reg)
586 res_put (zh->res, "lockDir", zh->path_reg);
587 sprintf (fname, "norm.%s.LCK", zh->reg_name);
589 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
591 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
593 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
599 void map_basenames_func (void *vp, const char *name, const char *value)
601 struct map_baseinfo *p = (struct map_baseinfo *) vp;
603 char fromdb[128], todb[8][128];
606 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
607 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
608 todb[5], todb[6], todb[7]);
612 for (i = 0; i<p->num_bases; i++)
613 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
616 for (i = 0; i < no; i++)
618 if (p->new_num_bases == p->new_num_max)
620 p->new_basenames[(p->new_num_bases)++] =
621 nmem_strdup (p->mem, todb[i]);
627 void map_basenames (ZebraHandle zh, ODR stream,
628 int *num_bases, char ***basenames)
630 struct map_baseinfo info;
631 struct map_baseinfo *p = &info;
637 info.num_bases = *num_bases;
638 info.basenames = *basenames;
639 info.new_num_max = 128;
640 info.new_num_bases = 0;
641 info.new_basenames = (char **)
642 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
643 info.mem = stream->mem;
645 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
647 for (i = 0; i<p->num_bases; i++)
648 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
650 p->new_basenames[(p->new_num_bases)++] =
651 nmem_strdup (p->mem, p->basenames[i]);
653 *num_bases = info.new_num_bases;
654 *basenames = info.new_basenames;
655 for (i = 0; i<*num_bases; i++)
656 logf (LOG_LOG, "base %s", (*basenames)[i]);
659 int zebra_select_database (ZebraHandle zh, const char *basename)
663 return zebra_select_databases (zh, 1, &basename);
666 int zebra_select_databases (ZebraHandle zh, int num_bases,
667 const char **basenames)
681 for (i = 0; i < zh->num_basenames; i++)
682 xfree (zh->basenames[i]);
683 xfree (zh->basenames);
685 zh->num_basenames = num_bases;
686 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
687 for (i = 0; i < zh->num_basenames; i++)
688 zh->basenames[i] = xstrdup (basenames[i]);
690 cp = strrchr(basenames[0], '/');
693 len = cp - basenames[0];
694 new_reg = xmalloc (len + 1);
695 memcpy (new_reg, basenames[0], len);
699 new_reg = xstrdup ("");
700 for (i = 1; i<num_bases; i++)
704 cp1 = strrchr (basenames[i], '/');
712 if (len != cp1 - basenames[i] ||
713 memcmp (basenames[i], new_reg, len))
728 zebra_select_register (zh, new_reg);
735 if (!zh->lock_normal || !zh->lock_shadow)
743 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
744 Z_RPNQuery *query, const char *setname, int *hits)
751 if (zebra_begin_read (zh))
753 resultSetAddRPN (zh, decode, stream, query,
754 zh->num_basenames, zh->basenames, setname);
761 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
762 const char *setname, Z_RecordComposition *comp,
763 oid_value input_format, int num_recs,
764 ZebraRetrievalRecord *recs)
774 zh->errString = odr_strdup (stream, setname);
780 if (zebra_begin_read (zh))
783 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
784 for (i = 0; i<num_recs; i++)
785 pos_array[i] = recs[i].position;
786 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
789 logf (LOG_DEBUG, "zebraPosSetCreate error");
791 zh->errString = nmem_strdup (stream->mem, setname);
795 for (i = 0; i<num_recs; i++)
800 recs[i].format = VAL_SUTRS;
801 recs[i].len = strlen(poset[i].term);
802 recs[i].buf = poset[i].term;
803 recs[i].base = poset[i].db;
805 else if (poset[i].sysno)
808 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
809 stream, input_format, comp,
810 &recs[i].format, &recs[i].buf,
813 recs[i].errString = NULL;
819 sprintf (num_str, "%d", pos_array[i]);
821 zh->errString = odr_strdup (stream, num_str);
825 zebraPosSetDestroy (zh, poset, num_recs);
831 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
832 oid_value attributeset,
833 int *position, int *num_entries, ZebraScanEntry **entries,
838 if (zebra_begin_read (zh))
844 rpn_scan (zh, stream, zapt, attributeset,
845 zh->num_basenames, zh->basenames, position,
846 num_entries, entries, is_partial);
850 void zebra_sort (ZebraHandle zh, ODR stream,
851 int num_input_setnames, const char **input_setnames,
852 const char *output_setname, Z_SortKeySpecList *sort_sequence,
857 if (zebra_begin_read (zh))
859 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
860 output_setname, sort_sequence, sort_status);
864 int zebra_deleleResultSet(ZebraHandle zh, int function,
865 int num_setnames, char **setnames,
871 if (zebra_begin_read(zh))
872 return Z_DeleteStatus_systemProblemAtTarget;
875 case Z_DeleteRequest_list:
876 resultSetDestroy (zh, num_setnames, setnames, statuses);
878 case Z_DeleteRequest_all:
879 resultSetDestroy (zh, -1, 0, statuses);
883 status = Z_DeleteStatus_success;
884 for (i = 0; i<num_setnames; i++)
885 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
886 status = statuses[i];
890 int zebra_errCode (ZebraHandle zh)
897 const char *zebra_errString (ZebraHandle zh)
900 return diagbib1_str (zh->errCode);
904 char *zebra_errAdd (ZebraHandle zh)
907 return zh->errString;
911 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
917 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
919 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
923 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
927 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
928 const char *record_type)
932 if (zebra_select_database(zh, database))
934 zebra_begin_trans (zh);
935 xfree (zh->admin_databaseName);
936 zh->admin_databaseName = xstrdup(database);
939 void zebra_admin_import_end (ZebraHandle zh)
943 zebra_end_trans (zh);
946 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
952 for (i = 0; i<segment->num_segmentRecords; i++)
954 Z_NamePlusRecord *npr = segment->segmentRecords[i];
955 const char *databaseName = npr->databaseName;
958 databaseName = zh->admin_databaseName;
959 printf ("--------------%d--------------------\n", i);
960 if (npr->which == Z_NamePlusRecord_intermediateFragment)
962 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
963 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
965 Odr_oct *oct = fragment->u.notExternallyTagged;
966 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
970 extract_rec_in_mem (zh, "grs.sgml",
978 0 /* match criteria */);
984 int zebra_admin_exchange_record (ZebraHandle zh,
985 const char *database,
988 const char *recid_buf, size_t recid_len,
997 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
999 memcpy (recid_z, recid_buf, recid_len);
1000 recid_z[recid_len] = 0;
1002 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1005 if (action == 1) /* fail if insert */
1007 memcpy (&sysno, rinfo+1, sizeof(sysno));
1011 if (action == 2 || action == 3) /* fail if delete or update */
1014 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1015 action == 3 ? 1 : 0 /* delete flag */,
1016 0, &sysno, 1, 1, 0);
1019 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1021 else if (action == 3)
1023 dict_delete (zh->reg->matchDict, recid_z);
1028 void zebra_admin_create (ZebraHandle zh, const char *database)
1034 if (zebra_select_database (zh, database))
1036 zebra_begin_trans (zh);
1039 /* announce database */
1040 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1041 /* explainDatabase */))
1044 zh->errString = "database already exist";
1046 zebra_end_trans (zh);
1049 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1050 const char *input_str, int input_len,
1051 char *output_str, int output_len)
1056 if (!zh->reg->zebra_maps)
1058 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1059 input_str, input_len);
1062 if (wrbuf_len(wrbuf) >= output_len)
1064 if (wrbuf_len(wrbuf))
1065 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1066 output_str[wrbuf_len(wrbuf)] = '\0';
1067 return wrbuf_len(wrbuf);
1071 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1073 char state_fname[256];
1080 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1081 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1082 f = fopen (fname, "w");
1084 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1085 fprintf (f, "%c %d %ld\n", val, seqno, p);
1090 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1092 char state_fname[256];
1098 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1099 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1100 f = fopen (fname, "r");
1106 fscanf (f, "%c %d", val, seqno);
1112 int zebra_begin_read (ZebraHandle zh)
1123 if (zh->trans_no != 1)
1125 zebra_flush_reg (zh);
1129 #if HAVE_SYS_TIMES_H
1138 if (!zh->lock_normal || !zh->lock_shadow)
1144 zebra_get_state (zh, &val, &seqno);
1150 else if (seqno != zh->reg->seqno)
1152 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1153 seqno, zh->reg->seqno);
1156 else if (zh->reg->last_val != val)
1158 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1159 val, zh->reg->last_val);
1166 zebra_lock_r (zh->lock_shadow);
1168 zebra_lock_r (zh->lock_normal);
1171 zebra_register_close (zh->service, zh->reg);
1172 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1173 0, val == 'c' ? 1 : 0,
1174 zh->res, zh->path_reg);
1180 zh->reg->last_val = val;
1181 zh->reg->seqno = seqno;
1186 void zebra_end_read (ZebraHandle zh)
1191 if (zh->trans_no != 0)
1193 #if HAVE_SYS_TIMES_H
1195 logf (LOG_LOG, "user/system: %ld/%ld",
1196 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1197 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1201 zebra_unlock (zh->lock_normal);
1202 zebra_unlock (zh->lock_shadow);
1205 void zebra_begin_trans (ZebraHandle zh)
1210 const char *rval = 0;
1216 if (zh->trans_no != 1)
1222 yaz_log (LOG_LOG, "zebra_begin_trans");
1224 zh->records_inserted = 0;
1225 zh->records_updated = 0;
1226 zh->records_deleted = 0;
1227 zh->records_processed = 0;
1229 #if HAVE_SYS_TIMES_H
1234 if (zh->shadow_enable)
1235 rval = res_get (zh->res, "shadow");
1237 for (pass = 0; pass < 2; pass++)
1241 zebra_lock_r (zh->lock_normal);
1242 zebra_lock_w (zh->lock_shadow);
1246 zebra_lock_w (zh->lock_normal);
1247 zebra_lock_w (zh->lock_shadow);
1250 zebra_get_state (zh, &val, &seqno);
1253 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1254 zebra_unlock (zh->lock_shadow);
1255 zebra_unlock (zh->lock_normal);
1259 else if (val == 'd')
1263 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1265 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1266 bf_commitClean (bfs, rval);
1271 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1278 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1282 zebra_set_state (zh, 'd', seqno);
1284 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1285 1, rval ? 1 : 0, zh->res,
1288 zh->reg->seqno = seqno;
1291 void zebra_end_trans (ZebraHandle zh) {
1292 ZebraTransactionStatus dummy;
1293 zebra_end_transaction(zh, &dummy);
1296 void zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1304 status->processed = 0;
1305 status->inserted = 0;
1306 status->updated = 0;
1307 status->deleted = 0;
1312 if (zh->trans_no != 0)
1314 yaz_log (LOG_LOG, "zebra_end_trans");
1315 rval = res_get (zh->res, "shadow");
1317 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1319 zebra_flush_reg (zh);
1321 zebra_register_close (zh->service, zh->reg);
1324 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1325 zh->records_processed, zh->records_inserted,
1326 zh->records_updated, zh->records_deleted);
1328 status->processed = zh->records_processed;
1329 status->inserted = zh->records_inserted;
1330 status->updated = zh->records_updated;
1331 status->deleted = zh->records_deleted;
1333 zebra_get_state (zh, &val, &seqno);
1336 BFiles bfs = bfs_create (rval, zh->path_reg);
1337 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1338 bf_commitClean (bfs, rval);
1343 zebra_set_state (zh, 'o', seqno);
1345 zebra_unlock (zh->lock_shadow);
1346 zebra_unlock (zh->lock_normal);
1348 #if HAVE_SYS_TIMES_H
1350 logf (LOG_LOG, "user/system: %ld/%ld",
1351 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1352 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1354 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1355 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1361 void zebra_repository_update (ZebraHandle zh)
1365 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1366 repositoryUpdate (zh);
1369 void zebra_repository_delete (ZebraHandle zh)
1373 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1374 repositoryDelete (zh);
1377 void zebra_repository_show (ZebraHandle zh)
1381 repositoryShow (zh);
1384 int zebra_commit (ZebraHandle zh)
1398 rval = res_get (zh->res, "shadow");
1401 logf (LOG_WARN, "Cannot perform commit");
1402 logf (LOG_WARN, "No shadow area defined");
1406 zebra_lock_w (zh->lock_normal);
1407 zebra_lock_r (zh->lock_shadow);
1409 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1411 zebra_get_state (zh, &val, &seqno);
1414 bf_cache (bfs, rval);
1415 if (bf_commitExists (bfs))
1417 zebra_set_state (zh, 'c', seqno);
1419 logf (LOG_LOG, "commit start");
1420 bf_commitExec (bfs);
1424 logf (LOG_LOG, "commit clean");
1425 bf_commitClean (bfs, rval);
1427 zebra_set_state (zh, 'o', seqno);
1431 logf (LOG_LOG, "nothing to commit");
1435 zebra_unlock (zh->lock_shadow);
1436 zebra_unlock (zh->lock_normal);
1440 int zebra_init (ZebraHandle zh)
1452 rval = res_get (zh->res, "shadow");
1454 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1457 bf_cache (bfs, rval);
1461 zebra_set_state (zh, 'o', 0);
1465 int zebra_compact (ZebraHandle zh)
1475 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1481 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1487 zebra_begin_trans (zh);
1489 return 0; /* bad sysno */
1490 extract_rec_in_mem (zh, "grs.sgml",
1492 "Default", /* database */
1493 0 /* delete_flag */,
1498 0 /* match criteria */);
1500 zebra_end_trans (zh);
1506 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1510 memcpy (&zh->rGroup, rg, sizeof(*rg));
1513 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1516 *code = zh->errCode;
1517 *addinfo = zh->errString;
1520 void zebra_shadow_enable (ZebraHandle zh, int value)
1524 zh->shadow_enable = value;
1527 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1531 xfree (zh->record_encoding);
1537 if (zh->iconv_to_utf8 != 0)
1538 yaz_iconv_close(zh->iconv_to_utf8);
1539 if (zh->iconv_from_utf8 != 0)
1540 yaz_iconv_close(zh->iconv_from_utf8);
1542 zh->record_encoding = xstrdup (encoding);
1544 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1547 yaz_iconv_open ("UTF-8", encoding);
1548 if (zh->iconv_to_utf8 == 0)
1549 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1550 zh->iconv_from_utf8 =
1551 yaz_iconv_open (encoding, "UTF-8");
1552 if (zh->iconv_to_utf8 == 0)
1553 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1558 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1562 res_put(zh->res, name, value);
1565 const char *zebra_get_resource(ZebraHandle zh,
1566 const char *name, const char *defaultvalue)
1570 return res_get_def( zh->res, name, (char *)defaultvalue);
1573 /* moved from zebra_api_ext.c by pop */
1575 int zebra_trans_no (ZebraHandle zh) {
1576 return (zh->trans_no);
1581 int zebra_get_shadow_enable (ZebraHandle zh) {
1582 return (zh->shadow_enable);
1585 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
1586 zh->shadow_enable = value;
1589 void init_recordGroup (struct recordGroup *rg) {
1590 rg->groupName = NULL;
1591 rg->databaseName = NULL;
1593 rg->recordId = NULL;
1594 rg->recordType = NULL;
1595 rg->flagStoreData = -1;
1596 rg->flagStoreKeys = -1;
1598 rg->databaseNamePath = 0;
1599 rg->explainDatabase = 0;
1600 rg->fileVerboseLimit = 100000;
1601 rg->followLinks = -1;
1605 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1606 called... and in general... Should be moved to somewhere else */
1607 void res_get_recordGroup (ZebraHandle zh,
1608 struct recordGroup *rGroup,
1613 if (!rGroup->groupName || !*rGroup->groupName)
1616 sprintf (gprefix, "%s.", rGroup->groupName);
1618 /* determine file type - depending on extension */
1619 if (!rGroup->recordType) {
1620 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1621 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1622 sprintf (ext_res, "%srecordType", gprefix);
1623 rGroup->recordType = res_get (zh->res, ext_res);
1626 /* determine match criteria */
1627 if (!rGroup->recordId) {
1628 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1629 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1630 sprintf (ext_res, "%srecordId", gprefix);
1631 rGroup->recordId = res_get (zh->res, ext_res);
1635 /* determine database name */
1636 if (!rGroup->databaseName) {
1637 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1638 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1639 sprintf (ext_res, "%sdatabase", gprefix);
1640 rGroup->databaseName = res_get (zh->res, ext_res);
1643 if (!rGroup->databaseName)
1644 rGroup->databaseName = "Default";
1646 /* determine if explain database */
1647 sprintf (ext_res, "%sexplainDatabase", gprefix);
1648 rGroup->explainDatabase =
1649 atoi (res_get_def (zh->res, ext_res, "0"));
1652 if (rGroup->flagStoreData == -1) {
1654 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1655 if (!(sval = res_get (zh->res, ext_res))) {
1656 sprintf (ext_res, "%sstoreData", gprefix);
1657 sval = res_get (zh->res, ext_res);
1660 rGroup->flagStoreData = atoi (sval);
1662 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
1665 if (rGroup->flagStoreKeys == -1) {
1668 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1669 sval = res_get (zh->res, ext_res);
1671 sprintf (ext_res, "%sstoreKeys", gprefix);
1672 sval = res_get (zh->res, ext_res);
1674 if (!sval) sval = res_get (zh->res, "storeKeys");
1675 if (sval) rGroup->flagStoreKeys = atoi (sval);
1677 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
1682 /* almost the same as zebra_records_retrieve ... but how did it work?
1683 I mean for multiple records ??? CHECK ??? */
1684 void api_records_retrieve (ZebraHandle zh, ODR stream,
1685 const char *setname, Z_RecordComposition *comp,
1686 oid_value input_format, int num_recs,
1687 ZebraRetrievalRecord *recs)
1695 zh->errString = odr_strdup (stream, setname);
1701 if (zebra_begin_read (zh))
1704 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1705 for (i = 0; i<num_recs; i++)
1706 pos_array[i] = recs[i].position;
1707 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1710 logf (LOG_DEBUG, "zebraPosSetCreate error");
1712 zh->errString = nmem_strdup (stream->mem, setname);
1716 for (i = 0; i<num_recs; i++)
1720 recs[i].errCode = 0;
1721 recs[i].format = VAL_SUTRS;
1722 recs[i].len = strlen(poset[i].term);
1723 recs[i].buf = poset[i].term;
1724 recs[i].base = poset[i].db;
1728 else if (poset[i].sysno)
1730 /* changed here ??? CHECK ??? */
1733 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1734 stream, input_format, comp,
1739 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1740 memcpy(recs[i].buf, b, recs[i].len);
1741 recs[i].errString = 0; /* Hmmm !!! we should get this */
1742 recs[i].sysno = poset[i].sysno;
1743 recs[i].score = poset[i].score;
1749 sprintf (num_str, "%d", pos_array[i]);
1751 zh->errString = odr_strdup (stream, num_str);
1756 zebraPosSetDestroy (zh, poset, num_recs);
1758 zebra_end_read (zh);
1763 /* ---------------------------------------------------------------------------
1764 Record insert(=update), delete
1766 If sysno is provided, then it's used to identify the reocord.
1767 If not, and match_criteria is provided, then sysno is guessed
1768 If not, and a record is provided, then sysno is got from there
1771 int zebra_update_record (ZebraHandle zh,
1772 struct recordGroup *rGroup,
1773 const char *recordType,
1774 int sysno, const char *match, const char *fname,
1775 const char *buf, int buf_size)
1780 if (buf_size < 1) buf_size = strlen(buf);
1782 zebra_begin_trans(zh);
1783 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1789 zebra_end_trans(zh);
1793 int zebra_delete_record (ZebraHandle zh,
1794 struct recordGroup *rGroup,
1795 const char *recordType,
1796 int sysno, const char *match, const char *fname,
1797 const char *buf, int buf_size)
1801 if (buf_size < 1) buf_size = strlen(buf);
1803 zebra_begin_trans(zh);
1804 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1810 zebra_end_trans(zh);
1814 /* ---------------------------------------------------------------------------
1817 zebra_search_RPN is the same as zebra_search_rpn, except that read locking
1818 is not mandatory. (it's repeatable now, also in zebraapi.c)
1821 void zebra_search_RPN (ZebraHandle zh, ODR decode, ODR stream,
1822 Z_RPNQuery *query, const char *setname, int *hits)
1827 if (zebra_begin_read (zh))
1829 resultSetAddRPN (zh, decode, stream, query,
1830 zh->num_basenames, zh->basenames, setname);
1832 zebra_end_read (zh);
1837 int zebra_search_PQF (ZebraHandle zh,
1838 ODR odr_input, ODR odr_output,
1839 const char *pqf_query,
1840 const char *setname)
1845 query = p_query_rpn (odr_input, PROTO_Z3950, pqf_query);
1848 logf (LOG_WARN, "bad query %s\n", pqf_query);
1849 odr_reset (odr_input);
1852 zebra_search_RPN (zh, odr_input, odr_output, query, setname, &hits);
1854 odr_reset (odr_input);
1855 odr_reset (odr_output);
1860 /* ---------------------------------------------------------------------------
1861 Sort - a simplified interface, with optional read locks.
1863 int sort (ZebraHandle zh,
1865 const char *sort_spec,
1866 const char *output_setname,
1867 const char **input_setnames
1870 int num_input_setnames = 0;
1871 int sort_status = 0;
1872 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
1873 if (!sort_sequence) {
1874 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
1879 /* we can do this, since the perl typemap code for char** will
1880 put a NULL at the end of list */
1881 while (input_setnames[num_input_setnames]) num_input_setnames++;
1883 if (zebra_begin_read (zh))
1886 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
1887 output_setname, sort_sequence, &sort_status);
1890 return (sort_status);