1 /* $Id: zebraapi.c,v 1.88 2003-03-04 23:30:20 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 #include <yaz/diagbib1.h>
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);
245 recordCompression = res_get_def (res, "recordCompression", "none");
246 if (!strcmp (recordCompression, "none"))
247 record_compression = REC_COMPRESS_NONE;
248 if (!strcmp (recordCompression, "bzip2"))
249 record_compression = REC_COMPRESS_BZIP2;
251 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
253 logf (LOG_WARN, "rec_open");
258 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
260 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
262 logf (LOG_WARN, "dict_open");
265 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
267 logf (LOG_WARN, "sortIdx_open");
270 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
272 struct ISAMS_M_s isams_m;
273 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
274 key_isams_m(res, &isams_m))))
276 logf (LOG_WARN, "isams_open");
280 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
282 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
283 sizeof (struct it_key), res)))
285 logf (LOG_WARN, "is_open");
289 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
291 struct ISAMC_M_s isamc_m;
292 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
293 rw, key_isamc_m(res, &isamc_m))))
295 logf (LOG_WARN, "isc_open");
299 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
301 struct ISAMD_M_s isamd_m;
303 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
304 rw, key_isamd_m(res, &isamd_m))))
306 logf (LOG_WARN, "isamd_open");
310 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
312 struct ISAMC_M_s isamc_m;
314 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
315 rw, key_isamc_m(res, &isamc_m), 0)))
317 logf (LOG_WARN, "isamb_open");
321 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
323 struct ISAMC_M_s isamc_m;
325 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
326 rw, key_isamc_m(res, &isamc_m), 1)))
328 logf (LOG_WARN, "isamb_open");
332 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
334 struct ISAMC_M_s isamc_m;
336 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
337 rw, key_isamc_m(res, &isamc_m), -1)))
339 logf (LOG_WARN, "isamb_open");
343 reg->zei = zebraExplain_open (reg->records, reg->dh,
348 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
352 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
356 void zebra_admin_shutdown (ZebraHandle zh)
361 zebra_mutex_cond_lock (&zh->service->session_lock);
362 zh->service->stop_flag = 1;
363 zebra_mutex_cond_unlock (&zh->service->session_lock);
366 void zebra_admin_start (ZebraHandle zh)
372 zebra_mutex_cond_lock (&zs->session_lock);
373 zebra_mutex_cond_unlock (&zs->session_lock);
376 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
379 yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
384 zebraExplain_close (reg->zei);
385 dict_close (reg->dict);
387 dict_close (reg->matchDict);
388 sortIdx_close (reg->sortIdx);
390 isams_close (reg->isams);
392 is_close (reg->isam);
394 isc_close (reg->isamc);
396 isamd_close (reg->isamd);
398 isamb_close (reg->isamb);
399 rec_close (®->records);
402 recTypes_destroy (reg->recTypes);
403 zebra_maps_close (reg->zebra_maps);
404 zebraRankDestroy (reg);
405 bfs_destroy (reg->bfs);
406 data1_destroy (reg->dh);
408 xfree (reg->sortKeys.buf);
409 xfree (reg->keys.buf);
411 xfree (reg->key_buf);
416 void zebra_stop(ZebraService zs)
420 yaz_log (LOG_LOG, "zebra_stop");
424 zebra_close (zs->sessions);
427 zebra_mutex_cond_destroy (&zs->session_lock);
430 passwd_db_close (zs->passwd_db);
432 res_close (zs->global_res);
433 xfree (zs->configName);
434 xfree (zs->path_root);
438 void zebra_close (ZebraHandle zh)
441 struct zebra_session **sp;
450 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
453 resultSetDestroy (zh, -1, 0, 0);
456 zebra_register_close (zh->service, zh->reg);
457 zebra_close_res (zh);
459 xfree (zh->record_encoding);
461 for (i = 0; i < zh->num_basenames; i++)
462 xfree (zh->basenames[i]);
463 xfree (zh->basenames);
465 if (zh->iconv_to_utf8 != 0)
466 yaz_iconv_close (zh->iconv_to_utf8);
467 if (zh->iconv_from_utf8 != 0)
468 yaz_iconv_close (zh->iconv_from_utf8);
470 xfree (zh->admin_databaseName);
471 zebra_mutex_cond_lock (&zs->session_lock);
472 zebra_lock_destroy (zh->lock_normal);
473 zebra_lock_destroy (zh->lock_shadow);
485 zebra_mutex_cond_unlock (&zs->session_lock);
486 xfree (zh->reg_name);
487 zh->service=0; /* more likely to trigger an assert */
491 struct map_baseinfo {
497 char **new_basenames;
501 static Res zebra_open_res (ZebraHandle zh)
510 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
511 res = res_open (fname, zh->service->global_res);
513 res = zh->service->global_res;
515 else if (*zh->reg_name == 0)
517 res = zh->service->global_res;
521 yaz_log (LOG_WARN, "no register root specified");
522 return 0; /* no path for register - fail! */
527 static void zebra_close_res (ZebraHandle zh)
531 if (zh->res != zh->service->global_res)
536 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
540 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
544 assert (zh->reg == 0);
545 assert (*zh->reg_name == 0);
551 resultSetInvalidate (zh);
552 zebra_register_close (zh->service, zh->reg);
557 xfree (zh->reg_name);
558 zh->reg_name = xstrdup (new_reg);
560 xfree (zh->path_reg);
562 if (zh->service->path_root)
564 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
565 strlen(zh->reg_name) + 3);
566 strcpy (zh->path_reg, zh->service->path_root);
569 strcat (zh->path_reg, "/");
570 strcat (zh->path_reg, zh->reg_name);
573 zh->res = zebra_open_res (zh);
576 zebra_lock_destroy (zh->lock_normal);
580 zebra_lock_destroy (zh->lock_shadow);
586 const char *lock_area =res_get (zh->res, "lockDir");
588 if (!lock_area && zh->path_reg)
589 res_put (zh->res, "lockDir", zh->path_reg);
590 sprintf (fname, "norm.%s.LCK", zh->reg_name);
592 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
594 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
596 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
602 void map_basenames_func (void *vp, const char *name, const char *value)
604 struct map_baseinfo *p = (struct map_baseinfo *) vp;
606 char fromdb[128], todb[8][128];
609 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
610 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
611 todb[5], todb[6], todb[7]);
615 for (i = 0; i<p->num_bases; i++)
616 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
619 for (i = 0; i < no; i++)
621 if (p->new_num_bases == p->new_num_max)
623 p->new_basenames[(p->new_num_bases)++] =
624 nmem_strdup (p->mem, todb[i]);
630 void map_basenames (ZebraHandle zh, ODR stream,
631 int *num_bases, char ***basenames)
633 struct map_baseinfo info;
634 struct map_baseinfo *p = &info;
640 info.num_bases = *num_bases;
641 info.basenames = *basenames;
642 info.new_num_max = 128;
643 info.new_num_bases = 0;
644 info.new_basenames = (char **)
645 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
646 info.mem = stream->mem;
648 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
650 for (i = 0; i<p->num_bases; i++)
651 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
653 p->new_basenames[(p->new_num_bases)++] =
654 nmem_strdup (p->mem, p->basenames[i]);
656 *num_bases = info.new_num_bases;
657 *basenames = info.new_basenames;
658 for (i = 0; i<*num_bases; i++)
659 logf (LOG_LOG, "base %s", (*basenames)[i]);
662 int zebra_select_database (ZebraHandle zh, const char *basename)
666 return zebra_select_databases (zh, 1, &basename);
669 int zebra_select_databases (ZebraHandle zh, int num_bases,
670 const char **basenames)
684 for (i = 0; i < zh->num_basenames; i++)
685 xfree (zh->basenames[i]);
686 xfree (zh->basenames);
688 zh->num_basenames = num_bases;
689 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
690 for (i = 0; i < zh->num_basenames; i++)
691 zh->basenames[i] = xstrdup (basenames[i]);
693 cp = strrchr(basenames[0], '/');
696 len = cp - basenames[0];
697 new_reg = xmalloc (len + 1);
698 memcpy (new_reg, basenames[0], len);
702 new_reg = xstrdup ("");
703 for (i = 1; i<num_bases; i++)
707 cp1 = strrchr (basenames[i], '/');
715 if (len != cp1 - basenames[i] ||
716 memcmp (basenames[i], new_reg, len))
731 zebra_select_register (zh, new_reg);
738 if (!zh->lock_normal || !zh->lock_shadow)
746 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
747 Z_RPNQuery *query, const char *setname, int *hits)
754 if (zebra_begin_read (zh))
756 resultSetAddRPN (zh, decode, stream, query,
757 zh->num_basenames, zh->basenames, setname);
764 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
765 const char *setname, Z_RecordComposition *comp,
766 oid_value input_format, int num_recs,
767 ZebraRetrievalRecord *recs)
777 zh->errString = odr_strdup (stream, setname);
783 if (zebra_begin_read (zh))
786 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
787 for (i = 0; i<num_recs; i++)
788 pos_array[i] = recs[i].position;
789 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
792 logf (LOG_DEBUG, "zebraPosSetCreate error");
794 zh->errString = nmem_strdup (stream->mem, setname);
798 for (i = 0; i<num_recs; i++)
803 recs[i].format = VAL_SUTRS;
804 recs[i].len = strlen(poset[i].term);
805 recs[i].buf = poset[i].term;
806 recs[i].base = poset[i].db;
808 else if (poset[i].sysno)
811 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
812 stream, input_format, comp,
813 &recs[i].format, &recs[i].buf,
816 recs[i].errString = NULL;
822 sprintf (num_str, "%d", pos_array[i]);
824 zh->errString = odr_strdup (stream, num_str);
828 zebraPosSetDestroy (zh, poset, num_recs);
834 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
835 oid_value attributeset,
836 int *position, int *num_entries, ZebraScanEntry **entries,
841 if (zebra_begin_read (zh))
847 rpn_scan (zh, stream, zapt, attributeset,
848 zh->num_basenames, zh->basenames, position,
849 num_entries, entries, is_partial);
853 void zebra_sort (ZebraHandle zh, ODR stream,
854 int num_input_setnames, const char **input_setnames,
855 const char *output_setname, Z_SortKeySpecList *sort_sequence,
860 if (zebra_begin_read (zh))
862 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
863 output_setname, sort_sequence, sort_status);
867 int zebra_deleleResultSet(ZebraHandle zh, int function,
868 int num_setnames, char **setnames,
874 if (zebra_begin_read(zh))
875 return Z_DeleteStatus_systemProblemAtTarget;
878 case Z_DeleteRequest_list:
879 resultSetDestroy (zh, num_setnames, setnames, statuses);
881 case Z_DeleteRequest_all:
882 resultSetDestroy (zh, -1, 0, statuses);
886 status = Z_DeleteStatus_success;
887 for (i = 0; i<num_setnames; i++)
888 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
889 status = statuses[i];
893 int zebra_errCode (ZebraHandle zh)
900 const char *zebra_errString (ZebraHandle zh)
903 return diagbib1_str (zh->errCode);
907 char *zebra_errAdd (ZebraHandle zh)
910 return zh->errString;
914 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
920 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
922 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
926 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
930 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
931 const char *record_type)
935 if (zebra_select_database(zh, database))
937 zebra_begin_trans (zh, 1);
938 xfree (zh->admin_databaseName);
939 zh->admin_databaseName = xstrdup(database);
942 void zebra_admin_import_end (ZebraHandle zh)
946 zebra_end_trans (zh);
949 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
955 for (i = 0; i<segment->num_segmentRecords; i++)
957 Z_NamePlusRecord *npr = segment->segmentRecords[i];
958 const char *databaseName = npr->databaseName;
961 databaseName = zh->admin_databaseName;
962 printf ("--------------%d--------------------\n", i);
963 if (npr->which == Z_NamePlusRecord_intermediateFragment)
965 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
966 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
968 Odr_oct *oct = fragment->u.notExternallyTagged;
969 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
973 extract_rec_in_mem (zh, "grs.sgml",
981 0 /* match criteria */);
987 int zebra_admin_exchange_record (ZebraHandle zh,
988 const char *database,
991 const char *recid_buf, size_t recid_len,
1000 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1002 memcpy (recid_z, recid_buf, recid_len);
1003 recid_z[recid_len] = 0;
1005 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1008 if (action == 1) /* fail if insert */
1010 memcpy (&sysno, rinfo+1, sizeof(sysno));
1014 if (action == 2 || action == 3) /* fail if delete or update */
1017 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1018 action == 3 ? 1 : 0 /* delete flag */,
1019 0, &sysno, 1, 1, 0);
1022 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1024 else if (action == 3)
1026 dict_delete (zh->reg->matchDict, recid_z);
1031 void zebra_admin_create (ZebraHandle zh, const char *database)
1037 if (zebra_select_database (zh, database))
1039 zebra_begin_trans (zh, 1);
1042 /* announce database */
1043 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1044 /* explainDatabase */))
1047 zh->errString = "database already exist";
1049 zebra_end_trans (zh);
1052 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1053 const char *input_str, int input_len,
1054 char *output_str, int output_len)
1059 if (!zh->reg->zebra_maps)
1061 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1062 input_str, input_len);
1065 if (wrbuf_len(wrbuf) >= output_len)
1067 if (wrbuf_len(wrbuf))
1068 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1069 output_str[wrbuf_len(wrbuf)] = '\0';
1070 return wrbuf_len(wrbuf);
1074 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1076 char state_fname[256];
1083 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1084 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1085 f = fopen (fname, "w");
1087 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1088 fprintf (f, "%c %d %ld\n", val, seqno, p);
1093 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1095 char state_fname[256];
1101 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1102 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1103 f = fopen (fname, "r");
1109 fscanf (f, "%c %d", val, seqno);
1115 int zebra_begin_read (ZebraHandle zh)
1117 return zebra_begin_trans(zh, 0);
1120 void zebra_end_read (ZebraHandle zh)
1122 zebra_end_trans(zh);
1125 int zebra_begin_trans (ZebraHandle zh, int rw)
1134 const char *rval = 0;
1139 zh->trans_w_no = zh->trans_no;
1143 yaz_log (LOG_LOG, "zebra_begin_trans");
1145 zh->records_inserted = 0;
1146 zh->records_updated = 0;
1147 zh->records_deleted = 0;
1148 zh->records_processed = 0;
1150 #if HAVE_SYS_TIMES_H
1154 if (zh->shadow_enable)
1155 rval = res_get (zh->res, "shadow");
1157 for (pass = 0; pass < 2; pass++)
1161 zebra_lock_r (zh->lock_normal);
1162 zebra_lock_w (zh->lock_shadow);
1166 zebra_lock_w (zh->lock_normal);
1167 zebra_lock_w (zh->lock_shadow);
1170 zebra_get_state (zh, &val, &seqno);
1173 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1174 zebra_unlock (zh->lock_shadow);
1175 zebra_unlock (zh->lock_normal);
1179 else if (val == 'd')
1183 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1185 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1186 bf_commitClean (bfs, rval);
1191 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1198 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1202 zebra_set_state (zh, 'd', seqno);
1204 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1205 1, rval ? 1 : 0, zh->res,
1208 zh->reg->seqno = seqno;
1218 if (zh->trans_no != 1)
1220 zebra_flush_reg (zh);
1224 #if HAVE_SYS_TIMES_H
1233 if (!zh->lock_normal || !zh->lock_shadow)
1239 zebra_get_state (zh, &val, &seqno);
1245 else if (seqno != zh->reg->seqno)
1247 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1248 seqno, zh->reg->seqno);
1251 else if (zh->reg->last_val != val)
1253 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1254 val, zh->reg->last_val);
1261 zebra_lock_r (zh->lock_shadow);
1263 zebra_lock_r (zh->lock_normal);
1266 zebra_register_close (zh->service, zh->reg);
1267 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1268 0, val == 'c' ? 1 : 0,
1269 zh->res, zh->path_reg);
1275 zh->reg->last_val = val;
1276 zh->reg->seqno = seqno;
1281 void zebra_end_trans (ZebraHandle zh) {
1282 ZebraTransactionStatus dummy;
1283 zebra_end_transaction(zh, &dummy);
1286 void zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1294 status->processed = 0;
1295 status->inserted = 0;
1296 status->updated = 0;
1297 status->deleted = 0;
1301 if (zh->trans_no != zh->trans_w_no)
1304 if (zh->trans_no != 0)
1307 /* release read lock */
1309 zebra_unlock (zh->lock_normal);
1310 zebra_unlock (zh->lock_shadow);
1313 { /* release write lock */
1317 yaz_log (LOG_LOG, "zebra_end_trans");
1318 rval = res_get (zh->res, "shadow");
1320 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1322 zebra_flush_reg (zh);
1324 zebra_register_close (zh->service, zh->reg);
1327 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1328 zh->records_processed, zh->records_inserted,
1329 zh->records_updated, zh->records_deleted);
1331 status->processed = zh->records_processed;
1332 status->inserted = zh->records_inserted;
1333 status->updated = zh->records_updated;
1334 status->deleted = zh->records_deleted;
1336 zebra_get_state (zh, &val, &seqno);
1339 BFiles bfs = bfs_create (rval, zh->path_reg);
1340 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1341 bf_commitClean (bfs, rval);
1346 zebra_set_state (zh, 'o', seqno);
1348 zebra_unlock (zh->lock_shadow);
1349 zebra_unlock (zh->lock_normal);
1352 #if HAVE_SYS_TIMES_H
1354 logf (LOG_LOG, "user/system: %ld/%ld",
1355 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1356 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1358 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1359 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1363 void zebra_repository_update (ZebraHandle zh)
1367 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1368 repositoryUpdate (zh);
1371 void zebra_repository_delete (ZebraHandle zh)
1375 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1376 repositoryDelete (zh);
1379 void zebra_repository_show (ZebraHandle zh)
1383 repositoryShow (zh);
1386 int zebra_commit (ZebraHandle zh)
1400 rval = res_get (zh->res, "shadow");
1403 logf (LOG_WARN, "Cannot perform commit");
1404 logf (LOG_WARN, "No shadow area defined");
1408 zebra_lock_w (zh->lock_normal);
1409 zebra_lock_r (zh->lock_shadow);
1411 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1413 zebra_get_state (zh, &val, &seqno);
1416 bf_cache (bfs, rval);
1417 if (bf_commitExists (bfs))
1419 zebra_set_state (zh, 'c', seqno);
1421 logf (LOG_LOG, "commit start");
1422 bf_commitExec (bfs);
1426 logf (LOG_LOG, "commit clean");
1427 bf_commitClean (bfs, rval);
1429 zebra_set_state (zh, 'o', seqno);
1433 logf (LOG_LOG, "nothing to commit");
1437 zebra_unlock (zh->lock_shadow);
1438 zebra_unlock (zh->lock_normal);
1442 int zebra_init (ZebraHandle zh)
1454 rval = res_get (zh->res, "shadow");
1456 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1459 bf_cache (bfs, rval);
1463 zebra_set_state (zh, 'o', 0);
1467 int zebra_compact (ZebraHandle zh)
1477 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1483 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1489 zebra_begin_trans (zh, 1);
1491 return 0; /* bad sysno */
1492 extract_rec_in_mem (zh, "grs.sgml",
1494 "Default", /* database */
1495 0 /* delete_flag */,
1500 0 /* match criteria */);
1502 zebra_end_trans (zh);
1508 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1512 memcpy (&zh->rGroup, rg, sizeof(*rg));
1515 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1518 *code = zh->errCode;
1519 *addinfo = zh->errString;
1522 void zebra_shadow_enable (ZebraHandle zh, int value)
1526 zh->shadow_enable = value;
1529 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1533 xfree (zh->record_encoding);
1539 if (zh->iconv_to_utf8 != 0)
1540 yaz_iconv_close(zh->iconv_to_utf8);
1541 if (zh->iconv_from_utf8 != 0)
1542 yaz_iconv_close(zh->iconv_from_utf8);
1544 zh->record_encoding = xstrdup (encoding);
1546 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1549 yaz_iconv_open ("UTF-8", encoding);
1550 if (zh->iconv_to_utf8 == 0)
1551 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1552 zh->iconv_from_utf8 =
1553 yaz_iconv_open (encoding, "UTF-8");
1554 if (zh->iconv_to_utf8 == 0)
1555 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1560 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1564 res_put(zh->res, name, value);
1567 const char *zebra_get_resource(ZebraHandle zh,
1568 const char *name, const char *defaultvalue)
1572 return res_get_def( zh->res, name, (char *)defaultvalue);
1575 /* moved from zebra_api_ext.c by pop */
1577 int zebra_trans_no (ZebraHandle zh) {
1578 return (zh->trans_no);
1583 int zebra_get_shadow_enable (ZebraHandle zh) {
1584 return (zh->shadow_enable);
1587 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
1588 zh->shadow_enable = value;
1591 void init_recordGroup (struct recordGroup *rg) {
1592 rg->groupName = NULL;
1593 rg->databaseName = NULL;
1595 rg->recordId = NULL;
1596 rg->recordType = NULL;
1597 rg->flagStoreData = -1;
1598 rg->flagStoreKeys = -1;
1600 rg->databaseNamePath = 0;
1601 rg->explainDatabase = 0;
1602 rg->fileVerboseLimit = 100000;
1603 rg->followLinks = -1;
1607 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1608 called... and in general... Should be moved to somewhere else */
1609 void res_get_recordGroup (ZebraHandle zh,
1610 struct recordGroup *rGroup,
1615 if (!rGroup->groupName || !*rGroup->groupName)
1618 sprintf (gprefix, "%s.", rGroup->groupName);
1620 /* determine file type - depending on extension */
1621 if (!rGroup->recordType) {
1622 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1623 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1624 sprintf (ext_res, "%srecordType", gprefix);
1625 rGroup->recordType = res_get (zh->res, ext_res);
1628 /* determine match criteria */
1629 if (!rGroup->recordId) {
1630 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1631 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1632 sprintf (ext_res, "%srecordId", gprefix);
1633 rGroup->recordId = res_get (zh->res, ext_res);
1637 /* determine database name */
1638 if (!rGroup->databaseName) {
1639 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1640 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1641 sprintf (ext_res, "%sdatabase", gprefix);
1642 rGroup->databaseName = res_get (zh->res, ext_res);
1645 if (!rGroup->databaseName)
1646 rGroup->databaseName = "Default";
1648 /* determine if explain database */
1649 sprintf (ext_res, "%sexplainDatabase", gprefix);
1650 rGroup->explainDatabase =
1651 atoi (res_get_def (zh->res, ext_res, "0"));
1654 if (rGroup->flagStoreData == -1) {
1656 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1657 if (!(sval = res_get (zh->res, ext_res))) {
1658 sprintf (ext_res, "%sstoreData", gprefix);
1659 sval = res_get (zh->res, ext_res);
1662 rGroup->flagStoreData = atoi (sval);
1664 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
1667 if (rGroup->flagStoreKeys == -1) {
1670 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1671 sval = res_get (zh->res, ext_res);
1673 sprintf (ext_res, "%sstoreKeys", gprefix);
1674 sval = res_get (zh->res, ext_res);
1676 if (!sval) sval = res_get (zh->res, "storeKeys");
1677 if (sval) rGroup->flagStoreKeys = atoi (sval);
1679 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
1684 /* almost the same as zebra_records_retrieve ... but how did it work?
1685 I mean for multiple records ??? CHECK ??? */
1686 void api_records_retrieve (ZebraHandle zh, ODR stream,
1687 const char *setname, Z_RecordComposition *comp,
1688 oid_value input_format, int num_recs,
1689 ZebraRetrievalRecord *recs)
1697 zh->errString = odr_strdup (stream, setname);
1703 if (zebra_begin_read (zh))
1706 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1707 for (i = 0; i<num_recs; i++)
1708 pos_array[i] = recs[i].position;
1709 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1712 logf (LOG_DEBUG, "zebraPosSetCreate error");
1714 zh->errString = nmem_strdup (stream->mem, setname);
1718 for (i = 0; i<num_recs; i++)
1722 recs[i].errCode = 0;
1723 recs[i].format = VAL_SUTRS;
1724 recs[i].len = strlen(poset[i].term);
1725 recs[i].buf = poset[i].term;
1726 recs[i].base = poset[i].db;
1730 else if (poset[i].sysno)
1732 /* changed here ??? CHECK ??? */
1735 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1736 stream, input_format, comp,
1741 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1742 memcpy(recs[i].buf, b, recs[i].len);
1743 recs[i].errString = 0; /* Hmmm !!! we should get this */
1744 recs[i].sysno = poset[i].sysno;
1745 recs[i].score = poset[i].score;
1751 sprintf (num_str, "%d", pos_array[i]);
1753 zh->errString = odr_strdup (stream, num_str);
1758 zebraPosSetDestroy (zh, poset, num_recs);
1760 zebra_end_read (zh);
1765 /* ---------------------------------------------------------------------------
1766 Record insert(=update), delete
1768 If sysno is provided, then it's used to identify the reocord.
1769 If not, and match_criteria is provided, then sysno is guessed
1770 If not, and a record is provided, then sysno is got from there
1773 int zebra_update_record (ZebraHandle zh,
1774 struct recordGroup *rGroup,
1775 const char *recordType,
1776 int sysno, const char *match, const char *fname,
1777 const char *buf, int buf_size)
1782 if (buf_size < 1) buf_size = strlen(buf);
1784 zebra_begin_trans(zh, 1);
1785 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1791 zebra_end_trans(zh);
1795 int zebra_delete_record (ZebraHandle zh,
1796 struct recordGroup *rGroup,
1797 const char *recordType,
1798 int sysno, const char *match, const char *fname,
1799 const char *buf, int buf_size)
1803 if (buf_size < 1) buf_size = strlen(buf);
1805 zebra_begin_trans(zh, 1);
1806 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1812 zebra_end_trans(zh);
1816 /* ---------------------------------------------------------------------------
1819 zebra_search_RPN is the same as zebra_search_rpn, except that read locking
1820 is not mandatory. (it's repeatable now, also in zebraapi.c)
1823 void zebra_search_RPN (ZebraHandle zh, ODR decode, ODR stream,
1824 Z_RPNQuery *query, const char *setname, int *hits)
1829 if (zebra_begin_read (zh))
1831 resultSetAddRPN (zh, decode, stream, query,
1832 zh->num_basenames, zh->basenames, setname);
1834 zebra_end_read (zh);
1839 int zebra_search_PQF (ZebraHandle zh,
1840 ODR odr_input, ODR odr_output,
1841 const char *pqf_query,
1842 const char *setname)
1847 query = p_query_rpn (odr_input, PROTO_Z3950, pqf_query);
1850 logf (LOG_WARN, "bad query %s\n", pqf_query);
1851 odr_reset (odr_input);
1854 zebra_search_RPN (zh, odr_input, odr_output, query, setname, &hits);
1856 odr_reset (odr_input);
1857 odr_reset (odr_output);
1862 /* ---------------------------------------------------------------------------
1863 Sort - a simplified interface, with optional read locks.
1865 int sort (ZebraHandle zh,
1867 const char *sort_spec,
1868 const char *output_setname,
1869 const char **input_setnames
1872 int num_input_setnames = 0;
1873 int sort_status = 0;
1874 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
1875 if (!sort_sequence) {
1876 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
1881 /* we can do this, since the perl typemap code for char** will
1882 put a NULL at the end of list */
1883 while (input_setnames[num_input_setnames]) num_input_setnames++;
1885 if (zebra_begin_read (zh))
1888 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
1889 output_setname, sort_sequence, &sort_status);
1892 return (sort_status);