1 /* $Id: zebraapi.c,v 1.106 2003-06-20 14:21:23 heikki 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 /* A simple log macro */
48 /* don't break with older yazen that lack log_app2 */
50 #define LOG_API LOG_APP2
52 #define LOG_API LOG_DEBUG
55 static Res zebra_open_res (ZebraHandle zh);
56 static void zebra_close_res (ZebraHandle zh);
59 static void zebra_chdir (ZebraService zs)
63 yaz_log(LOG_API,"zebra_chdir");
64 dir = res_get (zs->global_res, "chdir");
67 logf (LOG_DEBUG, "chdir %s", dir);
75 static void zebra_flush_reg (ZebraHandle zh)
78 yaz_log(LOG_API,"zebra_flush_reg");
80 zebraExplain_flush (zh->reg->zei, zh);
82 extract_flushWriteKeys (zh);
83 zebra_index_merge (zh);
86 static struct zebra_register *zebra_register_open (ZebraService zs,
88 int rw, int useshadow,
90 const char *reg_path);
91 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
93 ZebraHandle zebra_open (ZebraService zs)
96 const char *default_encoding;
98 yaz_log(LOG_API,"zebra_open");
103 zh = (ZebraHandle) xmalloc (sizeof(*zh));
104 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
107 zh->reg = 0; /* no register attached yet */
114 zh->reg_name = xstrdup ("");
116 zh->num_basenames = 0;
125 zh->admin_databaseName = 0;
127 zh->shadow_enable = 1;
129 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
130 zh->record_encoding = xstrdup (default_encoding);
133 yaz_iconv_open ("UTF-8", default_encoding);
134 if (zh->iconv_to_utf8 == 0)
135 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
137 zh->iconv_from_utf8 =
138 yaz_iconv_open (default_encoding, "UTF-8");
139 if (zh->iconv_to_utf8 == 0)
140 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
143 zebra_mutex_cond_lock (&zs->session_lock);
145 zh->next = zs->sessions;
148 zebra_mutex_cond_unlock (&zs->session_lock);
153 ZebraService zebra_start (const char *configName)
157 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
159 if ((res = res_open (configName, 0)))
161 ZebraService zh = xmalloc (sizeof(*zh));
163 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
165 zh->global_res = res;
166 zh->configName = xstrdup(configName);
171 zebra_mutex_cond_init (&zh->session_lock);
172 if (!res_get (zh->global_res, "passwd"))
173 zh->passwd_db = NULL;
176 zh->passwd_db = passwd_db_open ();
178 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
180 passwd_db_file (zh->passwd_db,
181 res_get (zh->global_res, "passwd"));
183 zh->path_root = res_get (zh->global_res, "root");
190 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
191 int rw, int useshadow, Res res,
192 const char *reg_path)
194 struct zebra_register *reg;
195 int record_compression = REC_COMPRESS_NONE;
196 char *recordCompression = 0;
200 reg = xmalloc (sizeof(*reg));
203 reg->name = xstrdup (name);
210 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
211 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
213 reg->dh = data1_createx (DATA1_FLAG_XML);
216 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
219 data1_destroy(reg->dh);
223 bf_cache (reg->bfs, res_get (res, "shadow"));
224 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
225 DEFAULT_PROFILE_PATH));
226 data1_set_tabroot (reg->dh, reg_path);
227 reg->recTypes = recTypes_init (reg->dh);
228 recTypes_default_handlers (reg->recTypes);
230 reg->zebra_maps = zebra_maps_open (res, reg_path);
231 reg->rank_classes = NULL;
235 reg->keys.buf_max = 0;
237 reg->sortKeys.buf = 0;
238 reg->sortKeys.buf_max = 0;
251 reg->key_file_no = 0;
253 zebraRankInstall (reg, rank1_class);
254 zebraRankInstall (reg, rankzv_class);
255 zebraRankInstall (reg, rankliv_class);
257 recordCompression = res_get_def (res, "recordCompression", "none");
258 if (!strcmp (recordCompression, "none"))
259 record_compression = REC_COMPRESS_NONE;
260 if (!strcmp (recordCompression, "bzip2"))
261 record_compression = REC_COMPRESS_BZIP2;
263 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
265 logf (LOG_WARN, "rec_open");
270 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
272 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
274 logf (LOG_WARN, "dict_open");
277 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
279 logf (LOG_WARN, "sortIdx_open");
282 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
284 struct ISAMS_M_s isams_m;
285 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
286 key_isams_m(res, &isams_m))))
288 logf (LOG_WARN, "isams_open");
292 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
294 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
295 sizeof (struct it_key), res)))
297 logf (LOG_WARN, "is_open");
301 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
303 struct ISAMC_M_s isamc_m;
304 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
305 rw, key_isamc_m(res, &isamc_m))))
307 logf (LOG_WARN, "isc_open");
311 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
313 struct ISAMD_M_s isamd_m;
315 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
316 rw, key_isamd_m(res, &isamd_m))))
318 logf (LOG_WARN, "isamd_open");
322 if (res_get_match (res, "isam", "b", 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), 0)))
329 logf (LOG_WARN, "isamb_open");
333 if (res_get_match (res, "isam", "bc", 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 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
346 struct ISAMC_M_s isamc_m;
348 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
349 rw, key_isamc_m(res, &isamc_m), -1)))
351 logf (LOG_WARN, "isamb_open");
355 reg->zei = zebraExplain_open (reg->records, reg->dh,
360 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
364 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
368 int zebra_admin_shutdown (ZebraHandle zh)
371 yaz_log(LOG_API,"zebra_admin_shutdown");
374 zebra_mutex_cond_lock (&zh->service->session_lock);
375 zh->service->stop_flag = 1;
376 zebra_mutex_cond_unlock (&zh->service->session_lock);
380 int zebra_admin_start (ZebraHandle zh)
384 yaz_log(LOG_API,"zebra_admin_start");
387 zebra_mutex_cond_lock (&zs->session_lock);
388 zebra_mutex_cond_unlock (&zs->session_lock);
392 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
395 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
400 zebraExplain_close (reg->zei);
401 dict_close (reg->dict);
403 dict_close (reg->matchDict);
404 sortIdx_close (reg->sortIdx);
406 isams_close (reg->isams);
408 is_close (reg->isam);
410 isc_close (reg->isamc);
412 isamd_close (reg->isamd);
414 isamb_close (reg->isamb);
415 rec_close (®->records);
418 recTypes_destroy (reg->recTypes);
419 zebra_maps_close (reg->zebra_maps);
420 zebraRankDestroy (reg);
421 bfs_destroy (reg->bfs);
422 data1_destroy (reg->dh);
424 xfree (reg->sortKeys.buf);
425 xfree (reg->keys.buf);
427 xfree (reg->key_buf);
432 int zebra_stop(ZebraService zs)
436 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
440 zebra_close (zs->sessions);
443 zebra_mutex_cond_destroy (&zs->session_lock);
446 passwd_db_close (zs->passwd_db);
448 res_close (zs->global_res);
449 xfree (zs->configName);
450 xfree (zs->path_root);
455 int zebra_close (ZebraHandle zh)
458 struct zebra_session **sp;
461 yaz_log(LOG_API,"zebra_close");
468 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
471 resultSetDestroy (zh, -1, 0, 0);
474 zebra_register_close (zh->service, zh->reg);
475 zebra_close_res (zh);
477 xfree (zh->record_encoding);
479 for (i = 0; i < zh->num_basenames; i++)
480 xfree (zh->basenames[i]);
481 xfree (zh->basenames);
483 if (zh->iconv_to_utf8 != 0)
484 yaz_iconv_close (zh->iconv_to_utf8);
485 if (zh->iconv_from_utf8 != 0)
486 yaz_iconv_close (zh->iconv_from_utf8);
488 xfree (zh->admin_databaseName);
489 zebra_mutex_cond_lock (&zs->session_lock);
490 zebra_lock_destroy (zh->lock_normal);
491 zebra_lock_destroy (zh->lock_shadow);
503 zebra_mutex_cond_unlock (&zs->session_lock);
504 xfree (zh->reg_name);
505 zh->service=0; /* more likely to trigger an assert */
510 struct map_baseinfo {
516 char **new_basenames;
520 static Res zebra_open_res (ZebraHandle zh)
529 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
530 res = res_open (fname, zh->service->global_res);
532 res = zh->service->global_res;
534 else if (*zh->reg_name == 0)
536 res = zh->service->global_res;
540 yaz_log (LOG_WARN, "no register root specified");
541 return 0; /* no path for register - fail! */
546 static void zebra_close_res (ZebraHandle zh)
550 if (zh->res != zh->service->global_res)
555 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
559 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
563 assert (zh->reg == 0);
564 assert (*zh->reg_name == 0);
570 resultSetInvalidate (zh);
571 zebra_register_close (zh->service, zh->reg);
576 xfree (zh->reg_name);
577 zh->reg_name = xstrdup (new_reg);
579 xfree (zh->path_reg);
581 if (zh->service->path_root)
583 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
584 strlen(zh->reg_name) + 3);
585 strcpy (zh->path_reg, zh->service->path_root);
588 strcat (zh->path_reg, "/");
589 strcat (zh->path_reg, zh->reg_name);
592 zh->res = zebra_open_res (zh);
595 zebra_lock_destroy (zh->lock_normal);
599 zebra_lock_destroy (zh->lock_shadow);
605 const char *lock_area =res_get (zh->res, "lockDir");
607 if (!lock_area && zh->path_reg)
608 res_put (zh->res, "lockDir", zh->path_reg);
609 sprintf (fname, "norm.%s.LCK", zh->reg_name);
611 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
613 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
615 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
621 void map_basenames_func (void *vp, const char *name, const char *value)
623 struct map_baseinfo *p = (struct map_baseinfo *) vp;
625 char fromdb[128], todb[8][128];
628 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
629 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
630 todb[5], todb[6], todb[7]);
634 for (i = 0; i<p->num_bases; i++)
635 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
638 for (i = 0; i < no; i++)
640 if (p->new_num_bases == p->new_num_max)
642 p->new_basenames[(p->new_num_bases)++] =
643 nmem_strdup (p->mem, todb[i]);
649 void map_basenames (ZebraHandle zh, ODR stream,
650 int *num_bases, char ***basenames)
652 struct map_baseinfo info;
653 struct map_baseinfo *p = &info;
656 yaz_log(LOG_API,"map_basenames ");
660 info.num_bases = *num_bases;
661 info.basenames = *basenames;
662 info.new_num_max = 128;
663 info.new_num_bases = 0;
664 info.new_basenames = (char **)
665 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
666 info.mem = stream->mem;
668 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
670 for (i = 0; i<p->num_bases; i++)
671 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
673 p->new_basenames[(p->new_num_bases)++] =
674 nmem_strdup (p->mem, p->basenames[i]);
676 *num_bases = info.new_num_bases;
677 *basenames = info.new_basenames;
678 for (i = 0; i<*num_bases; i++)
679 logf (LOG_LOG, "base %s", (*basenames)[i]);
682 int zebra_select_database (ZebraHandle zh, const char *basename)
685 yaz_log(LOG_API,"zebra_select_database %s",basename);
687 return zebra_select_databases (zh, 1, &basename);
690 int zebra_select_databases (ZebraHandle zh, int num_bases,
691 const char **basenames)
698 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
699 num_bases,basenames[0]);
707 for (i = 0; i < zh->num_basenames; i++)
708 xfree (zh->basenames[i]);
709 xfree (zh->basenames);
711 zh->num_basenames = num_bases;
712 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
713 for (i = 0; i < zh->num_basenames; i++)
714 zh->basenames[i] = xstrdup (basenames[i]);
716 cp = strrchr(basenames[0], '/');
719 len = cp - basenames[0];
720 new_reg = xmalloc (len + 1);
721 memcpy (new_reg, basenames[0], len);
725 new_reg = xstrdup ("");
726 for (i = 1; i<num_bases; i++)
730 cp1 = strrchr (basenames[i], '/');
738 if (len != cp1 - basenames[i] ||
739 memcmp (basenames[i], new_reg, len))
754 zebra_select_register (zh, new_reg);
761 if (!zh->lock_normal || !zh->lock_shadow)
769 int zebra_search_RPN (ZebraHandle zh, ODR o,
770 Z_RPNQuery *query, const char *setname, int *hits)
773 yaz_log(LOG_API,"zebra_search_rpn");
778 if (zebra_begin_read (zh))
781 zebra_livcode_transform(zh, query);
783 resultSetAddRPN (zh, odr_extract_mem(o), query,
784 zh->num_basenames, zh->basenames, setname);
792 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
793 const char *setname, Z_RecordComposition *comp,
794 oid_value input_format, int num_recs,
795 ZebraRetrievalRecord *recs)
800 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
806 zh->errString = odr_strdup (stream, setname);
812 if (zebra_begin_read (zh))
815 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
816 for (i = 0; i<num_recs; i++)
817 pos_array[i] = recs[i].position;
818 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
821 logf (LOG_DEBUG, "zebraPosSetCreate error");
823 zh->errString = nmem_strdup (stream->mem, setname);
827 for (i = 0; i<num_recs; i++)
832 recs[i].format = VAL_SUTRS;
833 recs[i].len = strlen(poset[i].term);
834 recs[i].buf = poset[i].term;
835 recs[i].base = poset[i].db;
837 else if (poset[i].sysno)
840 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
841 stream, input_format, comp,
842 &recs[i].format, &recs[i].buf,
845 recs[i].errString = NULL;
851 sprintf (num_str, "%d", pos_array[i]);
853 zh->errString = odr_strdup (stream, num_str);
857 zebraPosSetDestroy (zh, poset, num_recs);
864 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
865 oid_value attributeset,
866 int *position, int *num_entries, ZebraScanEntry **entries,
870 yaz_log(LOG_API,"zebra_scan");
872 if (zebra_begin_read (zh))
878 rpn_scan (zh, stream, zapt, attributeset,
879 zh->num_basenames, zh->basenames, position,
880 num_entries, entries, is_partial, 0, 0);
885 int zebra_sort (ZebraHandle zh, ODR stream,
886 int num_input_setnames, const char **input_setnames,
887 const char *output_setname, Z_SortKeySpecList *sort_sequence,
891 yaz_log(LOG_API,"zebra_sort");
893 if (zebra_begin_read (zh))
895 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
896 output_setname, sort_sequence, sort_status);
901 int zebra_deleleResultSet(ZebraHandle zh, int function,
902 int num_setnames, char **setnames,
907 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
909 if (zebra_begin_read(zh))
910 return Z_DeleteStatus_systemProblemAtTarget;
913 case Z_DeleteRequest_list:
914 resultSetDestroy (zh, num_setnames, setnames, statuses);
916 case Z_DeleteRequest_all:
917 resultSetDestroy (zh, -1, 0, statuses);
921 status = Z_DeleteStatus_success;
922 for (i = 0; i<num_setnames; i++)
923 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
924 status = statuses[i];
928 int zebra_errCode (ZebraHandle zh)
932 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
935 yaz_log(LOG_API,"zebra_errCode: o");
939 const char *zebra_errString (ZebraHandle zh)
943 e= diagbib1_str (zh->errCode);
944 yaz_log(LOG_API,"zebra_errString: %s",e);
948 char *zebra_errAdd (ZebraHandle zh)
953 yaz_log(LOG_API,"zebra_errAdd: %s",a);
957 void zebra_clearError(ZebraHandle zh)
966 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
970 yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
973 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
975 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
979 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
983 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
984 const char *record_type)
987 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
988 database, record_type);
990 if (zebra_select_database(zh, database))
992 zebra_begin_trans (zh, 1);
993 xfree (zh->admin_databaseName);
994 zh->admin_databaseName = xstrdup(database);
998 int zebra_admin_import_end (ZebraHandle zh)
1001 yaz_log(LOG_API,"zebra_admin_import_end");
1003 zebra_end_trans (zh);
1007 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1012 yaz_log(LOG_API,"zebra_admin_import_segment");
1014 for (i = 0; i<segment->num_segmentRecords; i++)
1016 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1017 const char *databaseName = npr->databaseName;
1020 databaseName = zh->admin_databaseName;
1021 printf ("--------------%d--------------------\n", i);
1022 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1024 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1025 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1027 Odr_oct *oct = fragment->u.notExternallyTagged;
1028 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1032 extract_rec_in_mem (zh, "grs.sgml",
1035 0 /* delete_flag */,
1040 0 /* match criteria */);
1047 int zebra_admin_exchange_record (ZebraHandle zh,
1048 const char *database,
1049 const char *rec_buf,
1051 const char *recid_buf, size_t recid_len,
1058 yaz_log(LOG_API,"zebra_admin_exchange_record db=%s ac=%d",
1062 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1064 memcpy (recid_z, recid_buf, recid_len);
1065 recid_z[recid_len] = 0;
1067 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1070 if (action == 1) /* fail if insert */
1072 memcpy (&sysno, rinfo+1, sizeof(sysno));
1076 if (action == 2 || action == 3) /* fail if delete or update */
1079 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1080 action == 3 ? 1 : 0 /* delete flag */,
1081 0, &sysno, 1, 1, 0);
1084 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1086 else if (action == 3)
1088 dict_delete (zh->reg->matchDict, recid_z);
1093 int zebra_create_database (ZebraHandle zh, const char *database)
1097 yaz_log(LOG_API,"zebra_admin_create");
1100 if (zebra_select_database (zh, database))
1102 if (zebra_begin_trans (zh, 1))
1106 /* announce database */
1107 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1108 /* explainDatabase */))
1110 zebra_end_trans (zh);
1112 zh->errString = "database already exist";
1115 zebra_end_trans (zh);
1119 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1120 const char *input_str, int input_len,
1121 char *output_str, int output_len)
1125 yaz_log(LOG_API,"zebra_string_norm ");
1127 if (!zh->reg->zebra_maps)
1129 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1130 input_str, input_len);
1133 if (wrbuf_len(wrbuf) >= output_len)
1135 if (wrbuf_len(wrbuf))
1136 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1137 output_str[wrbuf_len(wrbuf)] = '\0';
1138 return wrbuf_len(wrbuf);
1142 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1144 char state_fname[256];
1149 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1152 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1153 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1154 f = fopen (fname, "w");
1156 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1157 fprintf (f, "%c %d %ld\n", val, seqno, p);
1163 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1165 char state_fname[256];
1170 yaz_log(LOG_API,"zebra_get_state ");
1172 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1173 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1174 f = fopen (fname, "r");
1180 fscanf (f, "%c %d", val, seqno);
1187 int zebra_begin_read (ZebraHandle zh)
1189 return zebra_begin_trans(zh, 0);
1192 int zebra_end_read (ZebraHandle zh)
1194 return zebra_end_trans(zh);
1197 int zebra_begin_trans (ZebraHandle zh, int rw)
1199 yaz_log(LOG_LOG, "zebra_begin_trans rw=%d trans=%d", rw, zh->trans_no);
1203 zh->errString = "zebra_begin_trans: no database selected";
1207 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1214 const char *rval = 0;
1219 if (zh->trans_no != 1)
1222 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1227 resultSetInvalidate (zh);
1228 zebra_register_close (zh->service, zh->reg);
1230 zh->trans_w_no = zh->trans_no;
1234 zh->records_inserted = 0;
1235 zh->records_updated = 0;
1236 zh->records_deleted = 0;
1237 zh->records_processed = 0;
1239 #if HAVE_SYS_TIMES_H
1243 if (zh->shadow_enable)
1244 rval = res_get (zh->res, "shadow");
1246 for (pass = 0; pass < 2; pass++)
1250 zebra_lock_r (zh->lock_normal);
1251 zebra_lock_w (zh->lock_shadow);
1255 zebra_lock_w (zh->lock_normal);
1256 zebra_lock_w (zh->lock_shadow);
1259 zebra_get_state (zh, &val, &seqno);
1262 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1263 zebra_unlock (zh->lock_shadow);
1264 zebra_unlock (zh->lock_normal);
1268 else if (val == 'd')
1272 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1274 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1275 bf_commitClean (bfs, rval);
1280 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1287 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1291 zebra_set_state (zh, 'd', seqno);
1293 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1294 1, rval ? 1 : 0, zh->res,
1297 zh->reg->seqno = seqno;
1300 zebra_set_state (zh, 'o', seqno);
1302 zebra_unlock (zh->lock_shadow);
1303 zebra_unlock (zh->lock_normal);
1309 zh->errString = "zebra_begin_trans: cannot open register";
1310 yaz_log(LOG_FATAL, zh->errString);
1322 if (zh->trans_no != 1)
1324 zebra_flush_reg (zh);
1328 #if HAVE_SYS_TIMES_H
1337 if (!zh->lock_normal || !zh->lock_shadow)
1343 zebra_get_state (zh, &val, &seqno);
1349 else if (seqno != zh->reg->seqno)
1351 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1352 seqno, zh->reg->seqno);
1355 else if (zh->reg->last_val != val)
1357 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1358 val, zh->reg->last_val);
1365 zebra_lock_r (zh->lock_shadow);
1367 zebra_lock_r (zh->lock_normal);
1370 zebra_register_close (zh->service, zh->reg);
1371 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1372 0, val == 'c' ? 1 : 0,
1373 zh->res, zh->path_reg);
1376 zebra_unlock (zh->lock_normal);
1377 zebra_unlock (zh->lock_shadow);
1382 zh->reg->last_val = val;
1383 zh->reg->seqno = seqno;
1388 int zebra_end_trans (ZebraHandle zh)
1390 ZebraTransactionStatus dummy;
1391 yaz_log(LOG_API,"zebra_end_trans");
1392 return zebra_end_transaction(zh, &dummy);
1395 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1402 yaz_log(LOG_API,"zebra_end_transaction");
1404 status->processed = 0;
1405 status->inserted = 0;
1406 status->updated = 0;
1407 status->deleted = 0;
1411 if (!zh->res || !zh->reg)
1414 zh->errString = "zebra_end_trans: no open transaction";
1417 if (zh->trans_no != zh->trans_w_no)
1420 if (zh->trans_no != 0)
1423 /* release read lock */
1425 zebra_unlock (zh->lock_normal);
1426 zebra_unlock (zh->lock_shadow);
1429 { /* release write lock */
1433 yaz_log (LOG_LOG, "zebra_end_trans");
1434 rval = res_get (zh->res, "shadow");
1436 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1438 zebra_flush_reg (zh);
1440 resultSetInvalidate (zh);
1442 zebra_register_close (zh->service, zh->reg);
1445 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1446 zh->records_processed, zh->records_inserted,
1447 zh->records_updated, zh->records_deleted);
1449 status->processed = zh->records_processed;
1450 status->inserted = zh->records_inserted;
1451 status->updated = zh->records_updated;
1452 status->deleted = zh->records_deleted;
1454 zebra_get_state (zh, &val, &seqno);
1457 BFiles bfs = bfs_create (rval, zh->path_reg);
1458 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1459 bf_commitClean (bfs, rval);
1464 zebra_set_state (zh, 'o', seqno);
1466 zebra_unlock (zh->lock_shadow);
1467 zebra_unlock (zh->lock_normal);
1470 #if HAVE_SYS_TIMES_H
1472 logf (LOG_LOG, "user/system: %ld/%ld",
1473 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1474 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1476 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1477 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1482 int zebra_repository_update (ZebraHandle zh)
1486 logf (LOG_LOG|LOG_API, "updating %s", zh->rGroup.path);
1487 repositoryUpdate (zh);
1491 int zebra_repository_delete (ZebraHandle zh)
1495 logf (LOG_LOG|LOG_API, "deleting %s", zh->rGroup.path);
1496 repositoryDelete (zh);
1500 int zebra_repository_show (ZebraHandle zh)
1503 yaz_log(LOG_API,"zebra_repository_show");
1505 repositoryShow (zh);
1509 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1523 rval = res_get (zh->res, "shadow");
1526 logf (LOG_WARN, "Cannot perform commit");
1527 logf (LOG_WARN, "No shadow area defined");
1531 zebra_lock_w (zh->lock_normal);
1532 zebra_lock_r (zh->lock_shadow);
1534 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1536 zebra_get_state (zh, &val, &seqno);
1539 bf_cache (bfs, rval);
1540 if (bf_commitExists (bfs))
1543 zebra_set_state (zh, 'd', seqno);
1546 zebra_set_state (zh, 'c', seqno);
1548 logf (LOG_LOG, "commit start");
1549 bf_commitExec (bfs);
1554 logf (LOG_LOG, "commit clean");
1555 bf_commitClean (bfs, rval);
1557 zebra_set_state (zh, 'o', seqno);
1561 logf (LOG_LOG, "nothing to commit");
1565 zebra_unlock (zh->lock_shadow);
1566 zebra_unlock (zh->lock_normal);
1570 int zebra_clean (ZebraHandle zh)
1572 yaz_log(LOG_API,"zebra_clean");
1573 return zebra_commit_ex(zh, 1);
1576 int zebra_commit (ZebraHandle zh)
1578 yaz_log(LOG_API,"zebra_commit");
1579 return zebra_commit_ex(zh, 0);
1582 int zebra_init (ZebraHandle zh)
1587 yaz_log(LOG_API,"zebra_init");
1595 rval = res_get (zh->res, "shadow");
1597 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1600 bf_cache (bfs, rval);
1604 zebra_set_state (zh, 'o', 0);
1608 int zebra_compact (ZebraHandle zh)
1612 yaz_log(LOG_API,"zebra_compact");
1619 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1625 int zebra_record_insert (ZebraHandle zh, const char *buf, int len, int *sysno)
1629 yaz_log(LOG_API,"zebra_record_insert");
1633 if (zebra_begin_trans (zh, 1))
1635 extract_rec_in_mem (zh, "grs.sgml",
1637 "Default", /* database */
1638 0 /* delete_flag */,
1643 0 /* match criteria */);
1644 if (zebra_end_trans (zh))
1651 int zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1654 yaz_log(LOG_API,"zebra_set_group");
1656 memcpy (&zh->rGroup, rg, sizeof(*rg));
1660 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1663 yaz_log(LOG_API,"zebra_result");
1664 *code = zh->errCode;
1665 *addinfo = zh->errString;
1669 int zebra_shadow_enable (ZebraHandle zh, int value)
1672 yaz_log(LOG_API,"zebra_shadow_enable");
1674 zh->shadow_enable = value;
1678 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1681 yaz_log(LOG_API,"zebra_record_encoding");
1683 xfree (zh->record_encoding);
1687 * Something about charset aliases. Oleg???
1690 if (zh->iconv_to_utf8 != 0)
1691 yaz_iconv_close(zh->iconv_to_utf8);
1692 if (zh->iconv_from_utf8 != 0)
1693 yaz_iconv_close(zh->iconv_from_utf8);
1695 zh->record_encoding = xstrdup (encoding);
1697 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1700 yaz_iconv_open ("UTF-8", encoding);
1701 if (zh->iconv_to_utf8 == 0)
1702 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1703 zh->iconv_from_utf8 =
1704 yaz_iconv_open (encoding, "UTF-8");
1705 if (zh->iconv_to_utf8 == 0)
1706 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1711 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1714 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1716 res_put(zh->res, name, value);
1720 const char *zebra_get_resource(ZebraHandle zh,
1721 const char *name, const char *defaultvalue)
1725 v= res_get_def( zh->res, name, (char *)defaultvalue);
1727 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1731 /* moved from zebra_api_ext.c by pop */
1732 /* FIXME: Should this really be public??? -Heikki */
1734 int zebra_trans_no (ZebraHandle zh) {
1736 yaz_log(LOG_API,"zebra_trans_no");
1737 return (zh->trans_no);
1742 int zebra_get_shadow_enable (ZebraHandle zh) {
1743 yaz_log(LOG_API,"zebra_get_shadow_enable");
1744 return (zh->shadow_enable);
1747 int zebra_set_shadow_enable (ZebraHandle zh, int value) {
1748 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1749 zh->shadow_enable = value;
1753 int init_recordGroup (struct recordGroup *rg) {
1755 yaz_log(LOG_API,"init_recordGroup");
1756 rg->groupName = NULL;
1757 rg->databaseName = NULL;
1759 rg->recordId = NULL;
1760 rg->recordType = NULL;
1761 rg->flagStoreData = -1;
1762 rg->flagStoreKeys = -1;
1764 rg->databaseNamePath = 0;
1765 rg->explainDatabase = 0;
1766 rg->fileVerboseLimit = 100000;
1767 rg->followLinks = -1;
1772 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1773 called... and in general... Should be moved to somewhere else */
1774 void res_get_recordGroup (ZebraHandle zh,
1775 struct recordGroup *rGroup,
1780 yaz_log(LOG_API,"res_get_recordGroup e=%s",ext);
1781 if (!rGroup->groupName || !*rGroup->groupName)
1784 sprintf (gprefix, "%s.", rGroup->groupName);
1786 /* determine file type - depending on extension */
1787 if (!rGroup->recordType) {
1788 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1789 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1790 sprintf (ext_res, "%srecordType", gprefix);
1791 rGroup->recordType = res_get (zh->res, ext_res);
1794 /* determine match criteria */
1795 if (!rGroup->recordId) {
1796 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1797 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1798 sprintf (ext_res, "%srecordId", gprefix);
1799 rGroup->recordId = res_get (zh->res, ext_res);
1803 /* determine database name */
1804 if (!rGroup->databaseName) {
1805 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1806 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1807 sprintf (ext_res, "%sdatabase", gprefix);
1808 rGroup->databaseName = res_get (zh->res, ext_res);
1811 if (!rGroup->databaseName)
1812 rGroup->databaseName = "Default";
1814 /* determine if explain database */
1815 sprintf (ext_res, "%sexplainDatabase", gprefix);
1816 rGroup->explainDatabase =
1817 atoi (res_get_def (zh->res, ext_res, "0"));
1820 if (rGroup->flagStoreData == -1) {
1822 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1823 if (!(sval = res_get (zh->res, ext_res))) {
1824 sprintf (ext_res, "%sstoreData", gprefix);
1825 sval = res_get (zh->res, ext_res);
1828 rGroup->flagStoreData = atoi (sval);
1830 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
1833 if (rGroup->flagStoreKeys == -1) {
1836 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1837 sval = res_get (zh->res, ext_res);
1839 sprintf (ext_res, "%sstoreKeys", gprefix);
1840 sval = res_get (zh->res, ext_res);
1842 if (!sval) sval = res_get (zh->res, "storeKeys");
1843 if (sval) rGroup->flagStoreKeys = atoi (sval);
1845 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
1850 /* almost the same as zebra_records_retrieve ... but how did it work?
1851 I mean for multiple records ??? CHECK ??? */
1852 void api_records_retrieve (ZebraHandle zh, ODR stream,
1853 const char *setname, Z_RecordComposition *comp,
1854 oid_value input_format, int num_recs,
1855 ZebraRetrievalRecord *recs)
1859 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1864 zh->errString = odr_strdup (stream, setname);
1870 if (zebra_begin_read (zh))
1873 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1874 for (i = 0; i<num_recs; i++)
1875 pos_array[i] = recs[i].position;
1876 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1879 logf (LOG_DEBUG, "zebraPosSetCreate error");
1881 zh->errString = nmem_strdup (stream->mem, setname);
1885 for (i = 0; i<num_recs; i++)
1889 recs[i].errCode = 0;
1890 recs[i].format = VAL_SUTRS;
1891 recs[i].len = strlen(poset[i].term);
1892 recs[i].buf = poset[i].term;
1893 recs[i].base = poset[i].db;
1897 else if (poset[i].sysno)
1899 /* changed here ??? CHECK ??? */
1902 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1903 stream, input_format, comp,
1908 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1909 memcpy(recs[i].buf, b, recs[i].len);
1910 recs[i].errString = 0; /* Hmmm !!! we should get this */
1911 recs[i].sysno = poset[i].sysno;
1912 recs[i].score = poset[i].score;
1918 sprintf (num_str, "%d", pos_array[i]);
1920 zh->errString = odr_strdup (stream, num_str);
1925 zebraPosSetDestroy (zh, poset, num_recs);
1927 zebra_end_read (zh);
1932 /* ---------------------------------------------------------------------------
1933 Record insert(=update), delete
1935 If sysno is provided, then it's used to identify the reocord.
1936 If not, and match_criteria is provided, then sysno is guessed
1937 If not, and a record is provided, then sysno is got from there
1938 NOTE: Now returns 0 at success and updates sysno, which is an int*
1942 int zebra_insert_record (ZebraHandle zh,
1943 struct recordGroup *rGroup,
1944 const char *recordType,
1945 int *sysno, const char *match, const char *fname,
1946 const char *buf, int buf_size,
1947 int force_update) /* This one is ignored */
1951 yaz_log(LOG_API,"zebra_insert_record sysno=%d",sysno);
1953 if (buf_size < 1) buf_size = strlen(buf);
1955 zebra_begin_trans(zh, 1);
1956 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1957 0, /* delete_flag */
1963 0); /* allow_update */
1964 zebra_end_trans(zh);
1965 if (res < 0) return (res);
1969 int zebra_update_record (ZebraHandle zh,
1970 struct recordGroup *rGroup,
1971 const char *recordType,
1972 int* sysno, const char *match, const char *fname,
1973 const char *buf, int buf_size,
1978 yaz_log(LOG_API,"zebra_update_record sysno=%d",sysno);
1980 if (buf_size < 1) buf_size = strlen(buf);
1982 zebra_begin_trans(zh, 1);
1983 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1984 0, /* delete_flag */
1990 1); /* allow_update */
1991 zebra_end_trans(zh);
1997 int zebra_delete_record (ZebraHandle zh,
1998 struct recordGroup *rGroup,
1999 const char *recordType,
2000 int *sysno, const char *match, const char *fname,
2001 const char *buf, int buf_size,
2005 yaz_log(LOG_API,"zebra_delete_record sysno=%d",sysno);
2007 if (buf_size < 1) buf_size = strlen(buf);
2009 zebra_begin_trans(zh, 1);
2010 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2011 1, /* delete_flag */
2017 1); /* allow_update */
2018 zebra_end_trans(zh);
2022 /* ---------------------------------------------------------------------------
2026 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2027 const char *setname, int *numhits)
2032 ODR odr = odr_createmem(ODR_ENCODE);
2034 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2036 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2039 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2041 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2050 /* ---------------------------------------------------------------------------
2051 Sort - a simplified interface, with optional read locks.
2052 FIXME - This is a horrible name, will conflict with half the applications
2054 int zebra_sort_2 (ZebraHandle zh,
2056 const char *sort_spec,
2057 const char *output_setname,
2058 const char **input_setnames
2061 int num_input_setnames = 0;
2062 int sort_status = 0;
2063 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2064 yaz_log(LOG_API,"sort (FIXME) ");
2065 if (!sort_sequence) {
2066 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2071 /* we can do this, since the perl typemap code for char** will
2072 put a NULL at the end of list */
2073 while (input_setnames[num_input_setnames]) num_input_setnames++;
2075 if (zebra_begin_read (zh))
2078 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2079 output_setname, sort_sequence, &sort_status);
2082 return (sort_status);