1 /* $Id: zebraapi.c,v 1.98 2003-04-24 19:46:59 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
42 /* simple asserts to validate the most essential input args */
43 #define ASSERTZH assert(zh && zh->service)
44 #define ASSERTZHRES assert(zh && zh->service && zh->res)
45 #define ASSERTZS assert(zs)
47 static Res zebra_open_res (ZebraHandle zh);
48 static void zebra_close_res (ZebraHandle zh);
51 static void zebra_chdir (ZebraService zs)
55 dir = res_get (zs->global_res, "chdir");
58 logf (LOG_DEBUG, "chdir %s", dir);
66 static void zebra_flush_reg (ZebraHandle zh)
70 zebraExplain_flush (zh->reg->zei, zh);
72 extract_flushWriteKeys (zh);
73 zebra_index_merge (zh);
76 static struct zebra_register *zebra_register_open (ZebraService zs,
78 int rw, int useshadow,
80 const char *reg_path);
81 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
83 ZebraHandle zebra_open (ZebraService zs)
86 const char *default_encoding;
92 zh = (ZebraHandle) xmalloc (sizeof(*zh));
93 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
96 zh->reg = 0; /* no register attached yet */
103 zh->reg_name = xstrdup ("");
105 zh->num_basenames = 0;
114 zh->admin_databaseName = 0;
116 zh->shadow_enable = 1;
118 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
119 zh->record_encoding = xstrdup (default_encoding);
122 yaz_iconv_open ("UTF-8", default_encoding);
123 if (zh->iconv_to_utf8 == 0)
124 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
126 zh->iconv_from_utf8 =
127 yaz_iconv_open (default_encoding, "UTF-8");
128 if (zh->iconv_to_utf8 == 0)
129 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
132 zebra_mutex_cond_lock (&zs->session_lock);
134 zh->next = zs->sessions;
137 zebra_mutex_cond_unlock (&zs->session_lock);
142 ZebraService zebra_start (const char *configName)
146 yaz_log (LOG_LOG, "zebra_start %s", configName);
148 if ((res = res_open (configName, 0)))
150 ZebraService zh = xmalloc (sizeof(*zh));
152 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
154 zh->global_res = res;
155 zh->configName = xstrdup(configName);
160 zebra_mutex_cond_init (&zh->session_lock);
161 if (!res_get (zh->global_res, "passwd"))
162 zh->passwd_db = NULL;
165 zh->passwd_db = passwd_db_open ();
167 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
169 passwd_db_file (zh->passwd_db,
170 res_get (zh->global_res, "passwd"));
172 zh->path_root = res_get (zh->global_res, "root");
179 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
180 int rw, int useshadow, Res res,
181 const char *reg_path)
183 struct zebra_register *reg;
184 int record_compression = REC_COMPRESS_NONE;
185 char *recordCompression = 0;
189 reg = xmalloc (sizeof(*reg));
192 reg->name = xstrdup (name);
199 yaz_log (LOG_DEBUG, "zebra_register_open rw = %d useshadow=%d p=%p",
202 reg->dh = data1_createx (DATA1_FLAG_XML);
205 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
208 data1_destroy(reg->dh);
212 bf_cache (reg->bfs, res_get (res, "shadow"));
213 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
214 DEFAULT_PROFILE_PATH));
215 data1_set_tabroot (reg->dh, reg_path);
216 reg->recTypes = recTypes_init (reg->dh);
217 recTypes_default_handlers (reg->recTypes);
219 reg->zebra_maps = zebra_maps_open (res, reg_path);
220 reg->rank_classes = NULL;
224 reg->keys.buf_max = 0;
226 reg->sortKeys.buf = 0;
227 reg->sortKeys.buf_max = 0;
240 reg->key_file_no = 0;
242 zebraRankInstall (reg, rank1_class);
243 zebraRankInstall (reg, rankzv_class);
244 zebraRankInstall (reg, rankliv_class);
246 recordCompression = res_get_def (res, "recordCompression", "none");
247 if (!strcmp (recordCompression, "none"))
248 record_compression = REC_COMPRESS_NONE;
249 if (!strcmp (recordCompression, "bzip2"))
250 record_compression = REC_COMPRESS_BZIP2;
252 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
254 logf (LOG_WARN, "rec_open");
259 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
261 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
263 logf (LOG_WARN, "dict_open");
266 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
268 logf (LOG_WARN, "sortIdx_open");
271 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
273 struct ISAMS_M_s isams_m;
274 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
275 key_isams_m(res, &isams_m))))
277 logf (LOG_WARN, "isams_open");
281 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
283 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
284 sizeof (struct it_key), res)))
286 logf (LOG_WARN, "is_open");
290 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
292 struct ISAMC_M_s isamc_m;
293 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
294 rw, key_isamc_m(res, &isamc_m))))
296 logf (LOG_WARN, "isc_open");
300 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
302 struct ISAMD_M_s isamd_m;
304 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
305 rw, key_isamd_m(res, &isamd_m))))
307 logf (LOG_WARN, "isamd_open");
311 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
313 struct ISAMC_M_s isamc_m;
315 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
316 rw, key_isamc_m(res, &isamc_m), 0)))
318 logf (LOG_WARN, "isamb_open");
322 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
324 struct ISAMC_M_s isamc_m;
326 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
327 rw, key_isamc_m(res, &isamc_m), 1)))
329 logf (LOG_WARN, "isamb_open");
333 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
335 struct ISAMC_M_s isamc_m;
337 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
338 rw, key_isamc_m(res, &isamc_m), -1)))
340 logf (LOG_WARN, "isamb_open");
344 reg->zei = zebraExplain_open (reg->records, reg->dh,
349 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
353 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
357 void zebra_admin_shutdown (ZebraHandle zh)
362 zebra_mutex_cond_lock (&zh->service->session_lock);
363 zh->service->stop_flag = 1;
364 zebra_mutex_cond_unlock (&zh->service->session_lock);
367 void zebra_admin_start (ZebraHandle zh)
373 zebra_mutex_cond_lock (&zs->session_lock);
374 zebra_mutex_cond_unlock (&zs->session_lock);
377 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
380 yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
385 zebraExplain_close (reg->zei);
386 dict_close (reg->dict);
388 dict_close (reg->matchDict);
389 sortIdx_close (reg->sortIdx);
391 isams_close (reg->isams);
393 is_close (reg->isam);
395 isc_close (reg->isamc);
397 isamd_close (reg->isamd);
399 isamb_close (reg->isamb);
400 rec_close (®->records);
403 recTypes_destroy (reg->recTypes);
404 zebra_maps_close (reg->zebra_maps);
405 zebraRankDestroy (reg);
406 bfs_destroy (reg->bfs);
407 data1_destroy (reg->dh);
409 xfree (reg->sortKeys.buf);
410 xfree (reg->keys.buf);
412 xfree (reg->key_buf);
417 void zebra_stop(ZebraService zs)
421 yaz_log (LOG_LOG, "zebra_stop");
425 zebra_close (zs->sessions);
428 zebra_mutex_cond_destroy (&zs->session_lock);
431 passwd_db_close (zs->passwd_db);
433 res_close (zs->global_res);
434 xfree (zs->configName);
435 xfree (zs->path_root);
439 void zebra_close (ZebraHandle zh)
442 struct zebra_session **sp;
451 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
454 resultSetDestroy (zh, -1, 0, 0);
457 zebra_register_close (zh->service, zh->reg);
458 zebra_close_res (zh);
460 xfree (zh->record_encoding);
462 for (i = 0; i < zh->num_basenames; i++)
463 xfree (zh->basenames[i]);
464 xfree (zh->basenames);
466 if (zh->iconv_to_utf8 != 0)
467 yaz_iconv_close (zh->iconv_to_utf8);
468 if (zh->iconv_from_utf8 != 0)
469 yaz_iconv_close (zh->iconv_from_utf8);
471 xfree (zh->admin_databaseName);
472 zebra_mutex_cond_lock (&zs->session_lock);
473 zebra_lock_destroy (zh->lock_normal);
474 zebra_lock_destroy (zh->lock_shadow);
486 zebra_mutex_cond_unlock (&zs->session_lock);
487 xfree (zh->reg_name);
488 zh->service=0; /* more likely to trigger an assert */
492 struct map_baseinfo {
498 char **new_basenames;
502 static Res zebra_open_res (ZebraHandle zh)
511 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
512 res = res_open (fname, zh->service->global_res);
514 res = zh->service->global_res;
516 else if (*zh->reg_name == 0)
518 res = zh->service->global_res;
522 yaz_log (LOG_WARN, "no register root specified");
523 return 0; /* no path for register - fail! */
528 static void zebra_close_res (ZebraHandle zh)
532 if (zh->res != zh->service->global_res)
537 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
541 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
545 assert (zh->reg == 0);
546 assert (*zh->reg_name == 0);
552 resultSetInvalidate (zh);
553 zebra_register_close (zh->service, zh->reg);
558 xfree (zh->reg_name);
559 zh->reg_name = xstrdup (new_reg);
561 xfree (zh->path_reg);
563 if (zh->service->path_root)
565 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
566 strlen(zh->reg_name) + 3);
567 strcpy (zh->path_reg, zh->service->path_root);
570 strcat (zh->path_reg, "/");
571 strcat (zh->path_reg, zh->reg_name);
574 zh->res = zebra_open_res (zh);
577 zebra_lock_destroy (zh->lock_normal);
581 zebra_lock_destroy (zh->lock_shadow);
587 const char *lock_area =res_get (zh->res, "lockDir");
589 if (!lock_area && zh->path_reg)
590 res_put (zh->res, "lockDir", zh->path_reg);
591 sprintf (fname, "norm.%s.LCK", zh->reg_name);
593 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
595 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
597 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
603 void map_basenames_func (void *vp, const char *name, const char *value)
605 struct map_baseinfo *p = (struct map_baseinfo *) vp;
607 char fromdb[128], todb[8][128];
610 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
611 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
612 todb[5], todb[6], todb[7]);
616 for (i = 0; i<p->num_bases; i++)
617 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
620 for (i = 0; i < no; i++)
622 if (p->new_num_bases == p->new_num_max)
624 p->new_basenames[(p->new_num_bases)++] =
625 nmem_strdup (p->mem, todb[i]);
631 void map_basenames (ZebraHandle zh, ODR stream,
632 int *num_bases, char ***basenames)
634 struct map_baseinfo info;
635 struct map_baseinfo *p = &info;
641 info.num_bases = *num_bases;
642 info.basenames = *basenames;
643 info.new_num_max = 128;
644 info.new_num_bases = 0;
645 info.new_basenames = (char **)
646 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
647 info.mem = stream->mem;
649 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
651 for (i = 0; i<p->num_bases; i++)
652 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
654 p->new_basenames[(p->new_num_bases)++] =
655 nmem_strdup (p->mem, p->basenames[i]);
657 *num_bases = info.new_num_bases;
658 *basenames = info.new_basenames;
659 for (i = 0; i<*num_bases; i++)
660 logf (LOG_LOG, "base %s", (*basenames)[i]);
663 int zebra_select_database (ZebraHandle zh, const char *basename)
667 return zebra_select_databases (zh, 1, &basename);
670 int zebra_select_databases (ZebraHandle zh, int num_bases,
671 const char **basenames)
685 for (i = 0; i < zh->num_basenames; i++)
686 xfree (zh->basenames[i]);
687 xfree (zh->basenames);
689 zh->num_basenames = num_bases;
690 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
691 for (i = 0; i < zh->num_basenames; i++)
692 zh->basenames[i] = xstrdup (basenames[i]);
694 cp = strrchr(basenames[0], '/');
697 len = cp - basenames[0];
698 new_reg = xmalloc (len + 1);
699 memcpy (new_reg, basenames[0], len);
703 new_reg = xstrdup ("");
704 for (i = 1; i<num_bases; i++)
708 cp1 = strrchr (basenames[i], '/');
716 if (len != cp1 - basenames[i] ||
717 memcmp (basenames[i], new_reg, len))
732 zebra_select_register (zh, new_reg);
739 if (!zh->lock_normal || !zh->lock_shadow)
747 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
748 Z_RPNQuery *query, const char *setname, int *hits)
755 if (zebra_begin_read (zh))
758 zebra_livcode_transform(zh, query);
760 resultSetAddRPN (zh, decode, stream, query,
761 zh->num_basenames, zh->basenames, setname);
768 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
769 const char *setname, Z_RecordComposition *comp,
770 oid_value input_format, int num_recs,
771 ZebraRetrievalRecord *recs)
781 zh->errString = odr_strdup (stream, setname);
787 if (zebra_begin_read (zh))
790 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
791 for (i = 0; i<num_recs; i++)
792 pos_array[i] = recs[i].position;
793 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
796 logf (LOG_DEBUG, "zebraPosSetCreate error");
798 zh->errString = nmem_strdup (stream->mem, setname);
802 for (i = 0; i<num_recs; i++)
807 recs[i].format = VAL_SUTRS;
808 recs[i].len = strlen(poset[i].term);
809 recs[i].buf = poset[i].term;
810 recs[i].base = poset[i].db;
812 else if (poset[i].sysno)
815 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
816 stream, input_format, comp,
817 &recs[i].format, &recs[i].buf,
820 recs[i].errString = NULL;
826 sprintf (num_str, "%d", pos_array[i]);
828 zh->errString = odr_strdup (stream, num_str);
832 zebraPosSetDestroy (zh, poset, num_recs);
838 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
839 oid_value attributeset,
840 int *position, int *num_entries, ZebraScanEntry **entries,
845 if (zebra_begin_read (zh))
851 rpn_scan (zh, stream, zapt, attributeset,
852 zh->num_basenames, zh->basenames, position,
853 num_entries, entries, is_partial, 0, 0);
857 void zebra_sort (ZebraHandle zh, ODR stream,
858 int num_input_setnames, const char **input_setnames,
859 const char *output_setname, Z_SortKeySpecList *sort_sequence,
864 if (zebra_begin_read (zh))
866 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
867 output_setname, sort_sequence, sort_status);
871 int zebra_deleleResultSet(ZebraHandle zh, int function,
872 int num_setnames, char **setnames,
878 if (zebra_begin_read(zh))
879 return Z_DeleteStatus_systemProblemAtTarget;
882 case Z_DeleteRequest_list:
883 resultSetDestroy (zh, num_setnames, setnames, statuses);
885 case Z_DeleteRequest_all:
886 resultSetDestroy (zh, -1, 0, statuses);
890 status = Z_DeleteStatus_success;
891 for (i = 0; i<num_setnames; i++)
892 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
893 status = statuses[i];
897 int zebra_errCode (ZebraHandle zh)
904 const char *zebra_errString (ZebraHandle zh)
907 return diagbib1_str (zh->errCode);
911 char *zebra_errAdd (ZebraHandle zh)
914 return zh->errString;
918 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
924 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
926 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
930 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
934 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
935 const char *record_type)
939 if (zebra_select_database(zh, database))
941 zebra_begin_trans (zh, 1);
942 xfree (zh->admin_databaseName);
943 zh->admin_databaseName = xstrdup(database);
946 void zebra_admin_import_end (ZebraHandle zh)
950 zebra_end_trans (zh);
953 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
959 for (i = 0; i<segment->num_segmentRecords; i++)
961 Z_NamePlusRecord *npr = segment->segmentRecords[i];
962 const char *databaseName = npr->databaseName;
965 databaseName = zh->admin_databaseName;
966 printf ("--------------%d--------------------\n", i);
967 if (npr->which == Z_NamePlusRecord_intermediateFragment)
969 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
970 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
972 Odr_oct *oct = fragment->u.notExternallyTagged;
973 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
977 extract_rec_in_mem (zh, "grs.sgml",
985 0 /* match criteria */);
991 int zebra_admin_exchange_record (ZebraHandle zh,
992 const char *database,
995 const char *recid_buf, size_t recid_len,
1004 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1006 memcpy (recid_z, recid_buf, recid_len);
1007 recid_z[recid_len] = 0;
1009 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1012 if (action == 1) /* fail if insert */
1014 memcpy (&sysno, rinfo+1, sizeof(sysno));
1018 if (action == 2 || action == 3) /* fail if delete or update */
1021 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1022 action == 3 ? 1 : 0 /* delete flag */,
1023 0, &sysno, 1, 1, 0);
1026 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1028 else if (action == 3)
1030 dict_delete (zh->reg->matchDict, recid_z);
1035 void zebra_admin_create (ZebraHandle zh, const char *database)
1041 if (zebra_select_database (zh, database))
1043 zebra_begin_trans (zh, 1);
1046 /* announce database */
1047 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1048 /* explainDatabase */))
1051 zh->errString = "database already exist";
1053 zebra_end_trans (zh);
1056 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1057 const char *input_str, int input_len,
1058 char *output_str, int output_len)
1063 if (!zh->reg->zebra_maps)
1065 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1066 input_str, input_len);
1069 if (wrbuf_len(wrbuf) >= output_len)
1071 if (wrbuf_len(wrbuf))
1072 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1073 output_str[wrbuf_len(wrbuf)] = '\0';
1074 return wrbuf_len(wrbuf);
1078 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1080 char state_fname[256];
1087 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1088 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1089 f = fopen (fname, "w");
1091 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1092 fprintf (f, "%c %d %ld\n", val, seqno, p);
1097 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1099 char state_fname[256];
1105 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1106 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1107 f = fopen (fname, "r");
1113 fscanf (f, "%c %d", val, seqno);
1119 int zebra_begin_read (ZebraHandle zh)
1121 return zebra_begin_trans(zh, 0);
1124 void zebra_end_read (ZebraHandle zh)
1126 zebra_end_trans(zh);
1129 int zebra_begin_trans (ZebraHandle zh, int rw)
1134 zh->errString = "zebra_begin_trans: no database selected";
1144 const char *rval = 0;
1149 if (zh->trans_no != 1)
1152 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1155 zh->trans_w_no = zh->trans_no;
1159 yaz_log (LOG_LOG, "zebra_begin_trans");
1161 zh->records_inserted = 0;
1162 zh->records_updated = 0;
1163 zh->records_deleted = 0;
1164 zh->records_processed = 0;
1166 #if HAVE_SYS_TIMES_H
1170 if (zh->shadow_enable)
1171 rval = res_get (zh->res, "shadow");
1173 for (pass = 0; pass < 2; pass++)
1177 zebra_lock_r (zh->lock_normal);
1178 zebra_lock_w (zh->lock_shadow);
1182 zebra_lock_w (zh->lock_normal);
1183 zebra_lock_w (zh->lock_shadow);
1186 zebra_get_state (zh, &val, &seqno);
1189 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1190 zebra_unlock (zh->lock_shadow);
1191 zebra_unlock (zh->lock_normal);
1195 else if (val == 'd')
1199 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1201 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1202 bf_commitClean (bfs, rval);
1207 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1214 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1218 zebra_set_state (zh, 'd', seqno);
1220 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1221 1, rval ? 1 : 0, zh->res,
1224 zh->reg->seqno = seqno;
1234 if (zh->trans_no != 1)
1236 zebra_flush_reg (zh);
1240 #if HAVE_SYS_TIMES_H
1249 if (!zh->lock_normal || !zh->lock_shadow)
1255 zebra_get_state (zh, &val, &seqno);
1261 else if (seqno != zh->reg->seqno)
1263 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1264 seqno, zh->reg->seqno);
1267 else if (zh->reg->last_val != val)
1269 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1270 val, zh->reg->last_val);
1277 zebra_lock_r (zh->lock_shadow);
1279 zebra_lock_r (zh->lock_normal);
1282 zebra_register_close (zh->service, zh->reg);
1283 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1284 0, val == 'c' ? 1 : 0,
1285 zh->res, zh->path_reg);
1291 zh->reg->last_val = val;
1292 zh->reg->seqno = seqno;
1297 int zebra_end_trans (ZebraHandle zh)
1299 ZebraTransactionStatus dummy;
1300 return zebra_end_transaction(zh, &dummy);
1303 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1311 status->processed = 0;
1312 status->inserted = 0;
1313 status->updated = 0;
1314 status->deleted = 0;
1318 if (!zh->res || !zh->reg)
1321 zh->errString = "zebra_end_trans: no open transaction";
1324 if (zh->trans_no != zh->trans_w_no)
1327 if (zh->trans_no != 0)
1330 /* release read lock */
1332 zebra_unlock (zh->lock_normal);
1333 zebra_unlock (zh->lock_shadow);
1336 { /* release write lock */
1340 yaz_log (LOG_LOG, "zebra_end_trans");
1341 rval = res_get (zh->res, "shadow");
1343 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1345 zebra_flush_reg (zh);
1347 zebra_register_close (zh->service, zh->reg);
1350 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1351 zh->records_processed, zh->records_inserted,
1352 zh->records_updated, zh->records_deleted);
1354 status->processed = zh->records_processed;
1355 status->inserted = zh->records_inserted;
1356 status->updated = zh->records_updated;
1357 status->deleted = zh->records_deleted;
1359 zebra_get_state (zh, &val, &seqno);
1362 BFiles bfs = bfs_create (rval, zh->path_reg);
1363 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1364 bf_commitClean (bfs, rval);
1369 zebra_set_state (zh, 'o', seqno);
1371 zebra_unlock (zh->lock_shadow);
1372 zebra_unlock (zh->lock_normal);
1375 #if HAVE_SYS_TIMES_H
1377 logf (LOG_LOG, "user/system: %ld/%ld",
1378 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1379 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1381 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1382 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1387 void zebra_repository_update (ZebraHandle zh)
1391 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1392 repositoryUpdate (zh);
1395 void zebra_repository_delete (ZebraHandle zh)
1399 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1400 repositoryDelete (zh);
1403 void zebra_repository_show (ZebraHandle zh)
1407 repositoryShow (zh);
1410 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1424 rval = res_get (zh->res, "shadow");
1427 logf (LOG_WARN, "Cannot perform commit");
1428 logf (LOG_WARN, "No shadow area defined");
1432 zebra_lock_w (zh->lock_normal);
1433 zebra_lock_r (zh->lock_shadow);
1435 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1437 zebra_get_state (zh, &val, &seqno);
1440 bf_cache (bfs, rval);
1441 if (bf_commitExists (bfs))
1444 zebra_set_state (zh, 'd', seqno);
1447 zebra_set_state (zh, 'c', seqno);
1449 logf (LOG_LOG, "commit start");
1450 bf_commitExec (bfs);
1455 logf (LOG_LOG, "commit clean");
1456 bf_commitClean (bfs, rval);
1458 zebra_set_state (zh, 'o', seqno);
1462 logf (LOG_LOG, "nothing to commit");
1466 zebra_unlock (zh->lock_shadow);
1467 zebra_unlock (zh->lock_normal);
1471 int zebra_clean (ZebraHandle zh)
1473 return zebra_commit_ex(zh, 1);
1476 int zebra_commit (ZebraHandle zh)
1478 return zebra_commit_ex(zh, 0);
1481 int zebra_init (ZebraHandle zh)
1493 rval = res_get (zh->res, "shadow");
1495 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1498 bf_cache (bfs, rval);
1502 zebra_set_state (zh, 'o', 0);
1506 int zebra_compact (ZebraHandle zh)
1516 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1522 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1527 if (zebra_begin_trans (zh, 1))
1529 extract_rec_in_mem (zh, "grs.sgml",
1531 "Default", /* database */
1532 0 /* delete_flag */,
1537 0 /* match criteria */);
1538 if (zebra_end_trans (zh))
1543 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1547 memcpy (&zh->rGroup, rg, sizeof(*rg));
1550 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1553 *code = zh->errCode;
1554 *addinfo = zh->errString;
1557 void zebra_shadow_enable (ZebraHandle zh, int value)
1561 zh->shadow_enable = value;
1564 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1568 xfree (zh->record_encoding);
1574 if (zh->iconv_to_utf8 != 0)
1575 yaz_iconv_close(zh->iconv_to_utf8);
1576 if (zh->iconv_from_utf8 != 0)
1577 yaz_iconv_close(zh->iconv_from_utf8);
1579 zh->record_encoding = xstrdup (encoding);
1581 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1584 yaz_iconv_open ("UTF-8", encoding);
1585 if (zh->iconv_to_utf8 == 0)
1586 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1587 zh->iconv_from_utf8 =
1588 yaz_iconv_open (encoding, "UTF-8");
1589 if (zh->iconv_to_utf8 == 0)
1590 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1595 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1599 res_put(zh->res, name, value);
1602 const char *zebra_get_resource(ZebraHandle zh,
1603 const char *name, const char *defaultvalue)
1607 return res_get_def( zh->res, name, (char *)defaultvalue);
1610 /* moved from zebra_api_ext.c by pop */
1612 int zebra_trans_no (ZebraHandle zh) {
1613 return (zh->trans_no);
1618 int zebra_get_shadow_enable (ZebraHandle zh) {
1619 return (zh->shadow_enable);
1622 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
1623 zh->shadow_enable = value;
1626 void init_recordGroup (struct recordGroup *rg) {
1627 rg->groupName = NULL;
1628 rg->databaseName = NULL;
1630 rg->recordId = NULL;
1631 rg->recordType = NULL;
1632 rg->flagStoreData = -1;
1633 rg->flagStoreKeys = -1;
1635 rg->databaseNamePath = 0;
1636 rg->explainDatabase = 0;
1637 rg->fileVerboseLimit = 100000;
1638 rg->followLinks = -1;
1642 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1643 called... and in general... Should be moved to somewhere else */
1644 void res_get_recordGroup (ZebraHandle zh,
1645 struct recordGroup *rGroup,
1650 if (!rGroup->groupName || !*rGroup->groupName)
1653 sprintf (gprefix, "%s.", rGroup->groupName);
1655 /* determine file type - depending on extension */
1656 if (!rGroup->recordType) {
1657 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1658 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1659 sprintf (ext_res, "%srecordType", gprefix);
1660 rGroup->recordType = res_get (zh->res, ext_res);
1663 /* determine match criteria */
1664 if (!rGroup->recordId) {
1665 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1666 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1667 sprintf (ext_res, "%srecordId", gprefix);
1668 rGroup->recordId = res_get (zh->res, ext_res);
1672 /* determine database name */
1673 if (!rGroup->databaseName) {
1674 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1675 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1676 sprintf (ext_res, "%sdatabase", gprefix);
1677 rGroup->databaseName = res_get (zh->res, ext_res);
1680 if (!rGroup->databaseName)
1681 rGroup->databaseName = "Default";
1683 /* determine if explain database */
1684 sprintf (ext_res, "%sexplainDatabase", gprefix);
1685 rGroup->explainDatabase =
1686 atoi (res_get_def (zh->res, ext_res, "0"));
1689 if (rGroup->flagStoreData == -1) {
1691 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1692 if (!(sval = res_get (zh->res, ext_res))) {
1693 sprintf (ext_res, "%sstoreData", gprefix);
1694 sval = res_get (zh->res, ext_res);
1697 rGroup->flagStoreData = atoi (sval);
1699 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
1702 if (rGroup->flagStoreKeys == -1) {
1705 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1706 sval = res_get (zh->res, ext_res);
1708 sprintf (ext_res, "%sstoreKeys", gprefix);
1709 sval = res_get (zh->res, ext_res);
1711 if (!sval) sval = res_get (zh->res, "storeKeys");
1712 if (sval) rGroup->flagStoreKeys = atoi (sval);
1714 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
1719 /* almost the same as zebra_records_retrieve ... but how did it work?
1720 I mean for multiple records ??? CHECK ??? */
1721 void api_records_retrieve (ZebraHandle zh, ODR stream,
1722 const char *setname, Z_RecordComposition *comp,
1723 oid_value input_format, int num_recs,
1724 ZebraRetrievalRecord *recs)
1732 zh->errString = odr_strdup (stream, setname);
1738 if (zebra_begin_read (zh))
1741 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1742 for (i = 0; i<num_recs; i++)
1743 pos_array[i] = recs[i].position;
1744 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1747 logf (LOG_DEBUG, "zebraPosSetCreate error");
1749 zh->errString = nmem_strdup (stream->mem, setname);
1753 for (i = 0; i<num_recs; i++)
1757 recs[i].errCode = 0;
1758 recs[i].format = VAL_SUTRS;
1759 recs[i].len = strlen(poset[i].term);
1760 recs[i].buf = poset[i].term;
1761 recs[i].base = poset[i].db;
1765 else if (poset[i].sysno)
1767 /* changed here ??? CHECK ??? */
1770 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1771 stream, input_format, comp,
1776 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1777 memcpy(recs[i].buf, b, recs[i].len);
1778 recs[i].errString = 0; /* Hmmm !!! we should get this */
1779 recs[i].sysno = poset[i].sysno;
1780 recs[i].score = poset[i].score;
1786 sprintf (num_str, "%d", pos_array[i]);
1788 zh->errString = odr_strdup (stream, num_str);
1793 zebraPosSetDestroy (zh, poset, num_recs);
1795 zebra_end_read (zh);
1800 /* ---------------------------------------------------------------------------
1801 Record insert(=update), delete
1803 If sysno is provided, then it's used to identify the reocord.
1804 If not, and match_criteria is provided, then sysno is guessed
1805 If not, and a record is provided, then sysno is got from there
1808 int zebra_insert_record (ZebraHandle zh,
1809 struct recordGroup *rGroup,
1810 const char *recordType,
1811 int sysno, const char *match, const char *fname,
1812 const char *buf, int buf_size,
1813 int force_update) /* This one is ignored */
1818 if (buf_size < 1) buf_size = strlen(buf);
1820 zebra_begin_trans(zh, 1);
1821 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1822 0, /* delete_flag */
1828 0); /* allow_update */
1829 zebra_end_trans(zh);
1830 if (res < 0) return (res);
1834 int zebra_update_record (ZebraHandle zh,
1835 struct recordGroup *rGroup,
1836 const char *recordType,
1837 int sysno, const char *match, const char *fname,
1838 const char *buf, int buf_size,
1844 if (buf_size < 1) buf_size = strlen(buf);
1846 zebra_begin_trans(zh, 1);
1847 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1848 0, /* delete_flag */
1854 1); /* allow_update */
1855 zebra_end_trans(zh);
1861 int zebra_delete_record (ZebraHandle zh,
1862 struct recordGroup *rGroup,
1863 const char *recordType,
1864 int sysno, const char *match, const char *fname,
1865 const char *buf, int buf_size,
1870 if (buf_size < 1) buf_size = strlen(buf);
1872 zebra_begin_trans(zh, 1);
1873 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1874 1, /* delete_flag */
1880 1); /* allow_update */
1881 zebra_end_trans(zh);
1885 /* ---------------------------------------------------------------------------
1888 zebra_search_RPN is the same as zebra_search_rpn, except that read locking
1889 is not mandatory. (it's repeatable now, also in zebraapi.c)
1892 void zebra_search_RPN (ZebraHandle zh, ODR decode, ODR stream,
1893 Z_RPNQuery *query, const char *setname, int *hits)
1898 if (zebra_begin_read (zh))
1900 resultSetAddRPN (zh, decode, stream, query,
1901 zh->num_basenames, zh->basenames, setname);
1903 zebra_end_read (zh);
1908 int zebra_search_PQF (ZebraHandle zh,
1909 ODR odr_input, ODR odr_output,
1910 const char *pqf_query,
1911 const char *setname)
1916 query = p_query_rpn (odr_input, PROTO_Z3950, pqf_query);
1919 logf (LOG_WARN, "bad query %s\n", pqf_query);
1920 odr_reset (odr_input);
1923 zebra_search_RPN (zh, odr_input, odr_output, query, setname, &hits);
1925 odr_reset (odr_input);
1926 odr_reset (odr_output);
1931 /* ---------------------------------------------------------------------------
1932 Sort - a simplified interface, with optional read locks.
1934 int sort (ZebraHandle zh,
1936 const char *sort_spec,
1937 const char *output_setname,
1938 const char **input_setnames
1941 int num_input_setnames = 0;
1942 int sort_status = 0;
1943 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
1944 if (!sort_sequence) {
1945 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
1950 /* we can do this, since the perl typemap code for char** will
1951 put a NULL at the end of list */
1952 while (input_setnames[num_input_setnames]) num_input_setnames++;
1954 if (zebra_begin_read (zh))
1957 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
1958 output_setname, sort_sequence, &sort_status);
1961 return (sort_status);