1 /* $Id: zebraapi.c,v 1.103 2003-05-22 16:16:22 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);
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 void 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);
379 void zebra_admin_start (ZebraHandle zh)
383 yaz_log(LOG_API,"zebra_admin_start");
386 zebra_mutex_cond_lock (&zs->session_lock);
387 zebra_mutex_cond_unlock (&zs->session_lock);
390 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
393 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
398 zebraExplain_close (reg->zei);
399 dict_close (reg->dict);
401 dict_close (reg->matchDict);
402 sortIdx_close (reg->sortIdx);
404 isams_close (reg->isams);
406 is_close (reg->isam);
408 isc_close (reg->isamc);
410 isamd_close (reg->isamd);
412 isamb_close (reg->isamb);
413 rec_close (®->records);
416 recTypes_destroy (reg->recTypes);
417 zebra_maps_close (reg->zebra_maps);
418 zebraRankDestroy (reg);
419 bfs_destroy (reg->bfs);
420 data1_destroy (reg->dh);
422 xfree (reg->sortKeys.buf);
423 xfree (reg->keys.buf);
425 xfree (reg->key_buf);
430 void zebra_stop(ZebraService zs)
434 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
438 zebra_close (zs->sessions);
441 zebra_mutex_cond_destroy (&zs->session_lock);
444 passwd_db_close (zs->passwd_db);
446 res_close (zs->global_res);
447 xfree (zs->configName);
448 xfree (zs->path_root);
452 void zebra_close (ZebraHandle zh)
455 struct zebra_session **sp;
458 yaz_log(LOG_API,"zebra_close");
465 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
468 resultSetDestroy (zh, -1, 0, 0);
471 zebra_register_close (zh->service, zh->reg);
472 zebra_close_res (zh);
474 xfree (zh->record_encoding);
476 for (i = 0; i < zh->num_basenames; i++)
477 xfree (zh->basenames[i]);
478 xfree (zh->basenames);
480 if (zh->iconv_to_utf8 != 0)
481 yaz_iconv_close (zh->iconv_to_utf8);
482 if (zh->iconv_from_utf8 != 0)
483 yaz_iconv_close (zh->iconv_from_utf8);
485 xfree (zh->admin_databaseName);
486 zebra_mutex_cond_lock (&zs->session_lock);
487 zebra_lock_destroy (zh->lock_normal);
488 zebra_lock_destroy (zh->lock_shadow);
500 zebra_mutex_cond_unlock (&zs->session_lock);
501 xfree (zh->reg_name);
502 zh->service=0; /* more likely to trigger an assert */
506 struct map_baseinfo {
512 char **new_basenames;
516 static Res zebra_open_res (ZebraHandle zh)
525 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
526 res = res_open (fname, zh->service->global_res);
528 res = zh->service->global_res;
530 else if (*zh->reg_name == 0)
532 res = zh->service->global_res;
536 yaz_log (LOG_WARN, "no register root specified");
537 return 0; /* no path for register - fail! */
542 static void zebra_close_res (ZebraHandle zh)
546 if (zh->res != zh->service->global_res)
551 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
555 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
559 assert (zh->reg == 0);
560 assert (*zh->reg_name == 0);
566 resultSetInvalidate (zh);
567 zebra_register_close (zh->service, zh->reg);
572 xfree (zh->reg_name);
573 zh->reg_name = xstrdup (new_reg);
575 xfree (zh->path_reg);
577 if (zh->service->path_root)
579 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
580 strlen(zh->reg_name) + 3);
581 strcpy (zh->path_reg, zh->service->path_root);
584 strcat (zh->path_reg, "/");
585 strcat (zh->path_reg, zh->reg_name);
588 zh->res = zebra_open_res (zh);
591 zebra_lock_destroy (zh->lock_normal);
595 zebra_lock_destroy (zh->lock_shadow);
601 const char *lock_area =res_get (zh->res, "lockDir");
603 if (!lock_area && zh->path_reg)
604 res_put (zh->res, "lockDir", zh->path_reg);
605 sprintf (fname, "norm.%s.LCK", zh->reg_name);
607 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
609 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
611 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
617 void map_basenames_func (void *vp, const char *name, const char *value)
619 struct map_baseinfo *p = (struct map_baseinfo *) vp;
621 char fromdb[128], todb[8][128];
624 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
625 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
626 todb[5], todb[6], todb[7]);
630 for (i = 0; i<p->num_bases; i++)
631 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
634 for (i = 0; i < no; i++)
636 if (p->new_num_bases == p->new_num_max)
638 p->new_basenames[(p->new_num_bases)++] =
639 nmem_strdup (p->mem, todb[i]);
645 void map_basenames (ZebraHandle zh, ODR stream,
646 int *num_bases, char ***basenames)
648 struct map_baseinfo info;
649 struct map_baseinfo *p = &info;
652 yaz_log(LOG_API,"map_basenames ");
656 info.num_bases = *num_bases;
657 info.basenames = *basenames;
658 info.new_num_max = 128;
659 info.new_num_bases = 0;
660 info.new_basenames = (char **)
661 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
662 info.mem = stream->mem;
664 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
666 for (i = 0; i<p->num_bases; i++)
667 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
669 p->new_basenames[(p->new_num_bases)++] =
670 nmem_strdup (p->mem, p->basenames[i]);
672 *num_bases = info.new_num_bases;
673 *basenames = info.new_basenames;
674 for (i = 0; i<*num_bases; i++)
675 logf (LOG_LOG, "base %s", (*basenames)[i]);
678 int zebra_select_database (ZebraHandle zh, const char *basename)
681 yaz_log(LOG_API,"zebra_select_database %s",basename);
683 return zebra_select_databases (zh, 1, &basename);
686 int zebra_select_databases (ZebraHandle zh, int num_bases,
687 const char **basenames)
694 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
695 num_bases,basenames[0]);
703 for (i = 0; i < zh->num_basenames; i++)
704 xfree (zh->basenames[i]);
705 xfree (zh->basenames);
707 zh->num_basenames = num_bases;
708 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
709 for (i = 0; i < zh->num_basenames; i++)
710 zh->basenames[i] = xstrdup (basenames[i]);
712 cp = strrchr(basenames[0], '/');
715 len = cp - basenames[0];
716 new_reg = xmalloc (len + 1);
717 memcpy (new_reg, basenames[0], len);
721 new_reg = xstrdup ("");
722 for (i = 1; i<num_bases; i++)
726 cp1 = strrchr (basenames[i], '/');
734 if (len != cp1 - basenames[i] ||
735 memcmp (basenames[i], new_reg, len))
750 zebra_select_register (zh, new_reg);
757 if (!zh->lock_normal || !zh->lock_shadow)
765 void zebra_search_RPN (ZebraHandle zh,
766 Z_RPNQuery *query, const char *setname, int *hits)
769 yaz_log(LOG_API,"zebra_search_rpn");
774 if (zebra_begin_read (zh))
777 zebra_livcode_transform(zh, query);
779 resultSetAddRPN (zh, query,
780 zh->num_basenames, zh->basenames, setname);
787 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
788 const char *setname, Z_RecordComposition *comp,
789 oid_value input_format, int num_recs,
790 ZebraRetrievalRecord *recs)
795 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
801 zh->errString = odr_strdup (stream, setname);
807 if (zebra_begin_read (zh))
810 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
811 for (i = 0; i<num_recs; i++)
812 pos_array[i] = recs[i].position;
813 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
816 logf (LOG_DEBUG, "zebraPosSetCreate error");
818 zh->errString = nmem_strdup (stream->mem, setname);
822 for (i = 0; i<num_recs; i++)
827 recs[i].format = VAL_SUTRS;
828 recs[i].len = strlen(poset[i].term);
829 recs[i].buf = poset[i].term;
830 recs[i].base = poset[i].db;
832 else if (poset[i].sysno)
835 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
836 stream, input_format, comp,
837 &recs[i].format, &recs[i].buf,
840 recs[i].errString = NULL;
846 sprintf (num_str, "%d", pos_array[i]);
848 zh->errString = odr_strdup (stream, num_str);
852 zebraPosSetDestroy (zh, poset, num_recs);
858 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
859 oid_value attributeset,
860 int *position, int *num_entries, ZebraScanEntry **entries,
864 yaz_log(LOG_API,"zebra_scan");
866 if (zebra_begin_read (zh))
872 rpn_scan (zh, stream, zapt, attributeset,
873 zh->num_basenames, zh->basenames, position,
874 num_entries, entries, is_partial, 0, 0);
878 void zebra_sort (ZebraHandle zh, ODR stream,
879 int num_input_setnames, const char **input_setnames,
880 const char *output_setname, Z_SortKeySpecList *sort_sequence,
884 yaz_log(LOG_API,"zebra_sort");
886 if (zebra_begin_read (zh))
888 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
889 output_setname, sort_sequence, sort_status);
893 int zebra_deleleResultSet(ZebraHandle zh, int function,
894 int num_setnames, char **setnames,
899 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
901 if (zebra_begin_read(zh))
902 return Z_DeleteStatus_systemProblemAtTarget;
905 case Z_DeleteRequest_list:
906 resultSetDestroy (zh, num_setnames, setnames, statuses);
908 case Z_DeleteRequest_all:
909 resultSetDestroy (zh, -1, 0, statuses);
913 status = Z_DeleteStatus_success;
914 for (i = 0; i<num_setnames; i++)
915 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
916 status = statuses[i];
920 int zebra_errCode (ZebraHandle zh)
924 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
927 yaz_log(LOG_API,"zebra_errCode: o");
931 const char *zebra_errString (ZebraHandle zh)
935 e= diagbib1_str (zh->errCode);
936 yaz_log(LOG_API,"zebra_errString: %s",e);
940 char *zebra_errAdd (ZebraHandle zh)
945 yaz_log(LOG_API,"zebra_errAdd: %s",a);
949 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
953 yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
956 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
958 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
962 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
966 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
967 const char *record_type)
970 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
971 database, record_type);
973 if (zebra_select_database(zh, database))
975 zebra_begin_trans (zh, 1);
976 xfree (zh->admin_databaseName);
977 zh->admin_databaseName = xstrdup(database);
980 void zebra_admin_import_end (ZebraHandle zh)
983 yaz_log(LOG_API,"zebra_admin_import_end");
985 zebra_end_trans (zh);
988 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
993 yaz_log(LOG_API,"zebra_admin_import_segment");
995 for (i = 0; i<segment->num_segmentRecords; i++)
997 Z_NamePlusRecord *npr = segment->segmentRecords[i];
998 const char *databaseName = npr->databaseName;
1001 databaseName = zh->admin_databaseName;
1002 printf ("--------------%d--------------------\n", i);
1003 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1005 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1006 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1008 Odr_oct *oct = fragment->u.notExternallyTagged;
1009 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1013 extract_rec_in_mem (zh, "grs.sgml",
1016 0 /* delete_flag */,
1021 0 /* match criteria */);
1027 int zebra_admin_exchange_record (ZebraHandle zh,
1028 const char *database,
1029 const char *rec_buf,
1031 const char *recid_buf, size_t recid_len,
1038 yaz_log(LOG_API,"zebra_admin_exchange_record db=%s ac=%d",
1042 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1044 memcpy (recid_z, recid_buf, recid_len);
1045 recid_z[recid_len] = 0;
1047 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1050 if (action == 1) /* fail if insert */
1052 memcpy (&sysno, rinfo+1, sizeof(sysno));
1056 if (action == 2 || action == 3) /* fail if delete or update */
1059 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1060 action == 3 ? 1 : 0 /* delete flag */,
1061 0, &sysno, 1, 1, 0);
1064 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1066 else if (action == 3)
1068 dict_delete (zh->reg->matchDict, recid_z);
1073 void zebra_create_database (ZebraHandle zh, const char *database)
1077 yaz_log(LOG_API,"zebra_admin_create");
1080 if (zebra_select_database (zh, database))
1082 if (zebra_begin_trans (zh, 1))
1086 /* announce database */
1087 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1088 /* explainDatabase */))
1091 zh->errString = "database already exist";
1093 zebra_end_trans (zh);
1096 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1097 const char *input_str, int input_len,
1098 char *output_str, int output_len)
1102 yaz_log(LOG_API,"zebra_string_norm ");
1104 if (!zh->reg->zebra_maps)
1106 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1107 input_str, input_len);
1110 if (wrbuf_len(wrbuf) >= output_len)
1112 if (wrbuf_len(wrbuf))
1113 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1114 output_str[wrbuf_len(wrbuf)] = '\0';
1115 return wrbuf_len(wrbuf);
1119 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1121 char state_fname[256];
1126 yaz_log(LOG_API,"zebra_set_state v=%d seq=%s", val, seqno);
1129 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1130 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1131 f = fopen (fname, "w");
1133 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1134 fprintf (f, "%c %d %ld\n", val, seqno, p);
1139 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1141 char state_fname[256];
1146 yaz_log(LOG_API,"zebra_get_state ");
1148 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1149 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1150 f = fopen (fname, "r");
1156 fscanf (f, "%c %d", val, seqno);
1162 int zebra_begin_read (ZebraHandle zh)
1164 return zebra_begin_trans(zh, 0);
1167 void zebra_end_read (ZebraHandle zh)
1169 zebra_end_trans(zh);
1172 int zebra_begin_trans (ZebraHandle zh, int rw)
1174 yaz_log(LOG_LOG, "zebra_begin_trans rw=%d trans=%d", rw, zh->trans_no);
1178 zh->errString = "zebra_begin_trans: no database selected";
1182 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1189 const char *rval = 0;
1194 if (zh->trans_no != 1)
1197 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1201 zebra_register_close (zh->service, zh->reg);
1203 zh->trans_w_no = zh->trans_no;
1207 zh->records_inserted = 0;
1208 zh->records_updated = 0;
1209 zh->records_deleted = 0;
1210 zh->records_processed = 0;
1212 #if HAVE_SYS_TIMES_H
1216 if (zh->shadow_enable)
1217 rval = res_get (zh->res, "shadow");
1219 for (pass = 0; pass < 2; pass++)
1223 zebra_lock_r (zh->lock_normal);
1224 zebra_lock_w (zh->lock_shadow);
1228 zebra_lock_w (zh->lock_normal);
1229 zebra_lock_w (zh->lock_shadow);
1232 zebra_get_state (zh, &val, &seqno);
1235 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1236 zebra_unlock (zh->lock_shadow);
1237 zebra_unlock (zh->lock_normal);
1241 else if (val == 'd')
1245 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1247 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1248 bf_commitClean (bfs, rval);
1253 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1260 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1264 zebra_set_state (zh, 'd', seqno);
1266 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1267 1, rval ? 1 : 0, zh->res,
1270 zh->reg->seqno = seqno;
1273 zebra_set_state (zh, 'o', seqno);
1275 zebra_unlock (zh->lock_shadow);
1276 zebra_unlock (zh->lock_normal);
1282 zh->errString = "zebra_begin_trans: cannot open register";
1283 yaz_log(LOG_FATAL, zh->errString);
1295 if (zh->trans_no != 1)
1297 zebra_flush_reg (zh);
1301 #if HAVE_SYS_TIMES_H
1310 if (!zh->lock_normal || !zh->lock_shadow)
1316 zebra_get_state (zh, &val, &seqno);
1322 else if (seqno != zh->reg->seqno)
1324 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1325 seqno, zh->reg->seqno);
1328 else if (zh->reg->last_val != val)
1330 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1331 val, zh->reg->last_val);
1338 zebra_lock_r (zh->lock_shadow);
1340 zebra_lock_r (zh->lock_normal);
1343 zebra_register_close (zh->service, zh->reg);
1344 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1345 0, val == 'c' ? 1 : 0,
1346 zh->res, zh->path_reg);
1349 zebra_unlock (zh->lock_normal);
1350 zebra_unlock (zh->lock_shadow);
1355 zh->reg->last_val = val;
1356 zh->reg->seqno = seqno;
1361 int zebra_end_trans (ZebraHandle zh)
1363 ZebraTransactionStatus dummy;
1364 yaz_log(LOG_API,"zebra_end_trans");
1365 return zebra_end_transaction(zh, &dummy);
1368 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1375 yaz_log(LOG_API,"zebra_end_transaction");
1377 status->processed = 0;
1378 status->inserted = 0;
1379 status->updated = 0;
1380 status->deleted = 0;
1384 if (!zh->res || !zh->reg)
1387 zh->errString = "zebra_end_trans: no open transaction";
1390 if (zh->trans_no != zh->trans_w_no)
1393 if (zh->trans_no != 0)
1396 /* release read lock */
1398 zebra_unlock (zh->lock_normal);
1399 zebra_unlock (zh->lock_shadow);
1402 { /* release write lock */
1406 yaz_log (LOG_LOG, "zebra_end_trans");
1407 rval = res_get (zh->res, "shadow");
1409 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1411 zebra_flush_reg (zh);
1413 resultSetInvalidate (zh);
1415 zebra_register_close (zh->service, zh->reg);
1418 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1419 zh->records_processed, zh->records_inserted,
1420 zh->records_updated, zh->records_deleted);
1422 status->processed = zh->records_processed;
1423 status->inserted = zh->records_inserted;
1424 status->updated = zh->records_updated;
1425 status->deleted = zh->records_deleted;
1427 zebra_get_state (zh, &val, &seqno);
1430 BFiles bfs = bfs_create (rval, zh->path_reg);
1431 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1432 bf_commitClean (bfs, rval);
1437 zebra_set_state (zh, 'o', seqno);
1439 zebra_unlock (zh->lock_shadow);
1440 zebra_unlock (zh->lock_normal);
1443 #if HAVE_SYS_TIMES_H
1445 logf (LOG_LOG, "user/system: %ld/%ld",
1446 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1447 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1449 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1450 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1455 void zebra_repository_update (ZebraHandle zh)
1459 logf (LOG_LOG|LOG_API, "updating %s", zh->rGroup.path);
1460 repositoryUpdate (zh);
1463 void zebra_repository_delete (ZebraHandle zh)
1467 logf (LOG_LOG|LOG_API, "deleting %s", zh->rGroup.path);
1468 repositoryDelete (zh);
1471 void zebra_repository_show (ZebraHandle zh)
1474 yaz_log(LOG_API,"zebra_repository_show");
1476 repositoryShow (zh);
1479 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1493 rval = res_get (zh->res, "shadow");
1496 logf (LOG_WARN, "Cannot perform commit");
1497 logf (LOG_WARN, "No shadow area defined");
1501 zebra_lock_w (zh->lock_normal);
1502 zebra_lock_r (zh->lock_shadow);
1504 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1506 zebra_get_state (zh, &val, &seqno);
1509 bf_cache (bfs, rval);
1510 if (bf_commitExists (bfs))
1513 zebra_set_state (zh, 'd', seqno);
1516 zebra_set_state (zh, 'c', seqno);
1518 logf (LOG_LOG, "commit start");
1519 bf_commitExec (bfs);
1524 logf (LOG_LOG, "commit clean");
1525 bf_commitClean (bfs, rval);
1527 zebra_set_state (zh, 'o', seqno);
1531 logf (LOG_LOG, "nothing to commit");
1535 zebra_unlock (zh->lock_shadow);
1536 zebra_unlock (zh->lock_normal);
1540 int zebra_clean (ZebraHandle zh)
1542 yaz_log(LOG_API,"zebra_clean");
1543 return zebra_commit_ex(zh, 1);
1546 int zebra_commit (ZebraHandle zh)
1548 yaz_log(LOG_API,"zebra_commit");
1549 return zebra_commit_ex(zh, 0);
1552 int zebra_init (ZebraHandle zh)
1557 yaz_log(LOG_API,"zebra_init");
1565 rval = res_get (zh->res, "shadow");
1567 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1570 bf_cache (bfs, rval);
1574 zebra_set_state (zh, 'o', 0);
1578 int zebra_compact (ZebraHandle zh)
1582 yaz_log(LOG_API,"zebra_compact");
1589 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1595 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1599 yaz_log(LOG_API,"zebra_record_insert");
1601 if (zebra_begin_trans (zh, 1))
1603 extract_rec_in_mem (zh, "grs.sgml",
1605 "Default", /* database */
1606 0 /* delete_flag */,
1611 0 /* match criteria */);
1612 if (zebra_end_trans (zh))
1617 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1620 yaz_log(LOG_API,"zebra_set_group");
1622 memcpy (&zh->rGroup, rg, sizeof(*rg));
1625 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1628 yaz_log(LOG_API,"zebra_result");
1629 *code = zh->errCode;
1630 *addinfo = zh->errString;
1633 void zebra_shadow_enable (ZebraHandle zh, int value)
1636 yaz_log(LOG_API,"zebra_shadow_enable");
1638 zh->shadow_enable = value;
1641 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1644 yaz_log(LOG_API,"zebra_record_encoding");
1646 xfree (zh->record_encoding);
1652 if (zh->iconv_to_utf8 != 0)
1653 yaz_iconv_close(zh->iconv_to_utf8);
1654 if (zh->iconv_from_utf8 != 0)
1655 yaz_iconv_close(zh->iconv_from_utf8);
1657 zh->record_encoding = xstrdup (encoding);
1659 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1662 yaz_iconv_open ("UTF-8", encoding);
1663 if (zh->iconv_to_utf8 == 0)
1664 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1665 zh->iconv_from_utf8 =
1666 yaz_iconv_open (encoding, "UTF-8");
1667 if (zh->iconv_to_utf8 == 0)
1668 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1673 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1676 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1678 res_put(zh->res, name, value);
1681 const char *zebra_get_resource(ZebraHandle zh,
1682 const char *name, const char *defaultvalue)
1686 v= res_get_def( zh->res, name, (char *)defaultvalue);
1688 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1692 /* moved from zebra_api_ext.c by pop */
1694 int zebra_trans_no (ZebraHandle zh) {
1696 yaz_log(LOG_API,"zebra_trans_no");
1697 return (zh->trans_no);
1702 int zebra_get_shadow_enable (ZebraHandle zh) {
1703 yaz_log(LOG_API,"zebra_get_shadow_enable");
1704 return (zh->shadow_enable);
1707 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
1708 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1709 zh->shadow_enable = value;
1712 void init_recordGroup (struct recordGroup *rg) {
1714 yaz_log(LOG_API,"init_recordGroup");
1715 rg->groupName = NULL;
1716 rg->databaseName = NULL;
1718 rg->recordId = NULL;
1719 rg->recordType = NULL;
1720 rg->flagStoreData = -1;
1721 rg->flagStoreKeys = -1;
1723 rg->databaseNamePath = 0;
1724 rg->explainDatabase = 0;
1725 rg->fileVerboseLimit = 100000;
1726 rg->followLinks = -1;
1730 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1731 called... and in general... Should be moved to somewhere else */
1732 void res_get_recordGroup (ZebraHandle zh,
1733 struct recordGroup *rGroup,
1738 yaz_log(LOG_API,"res_get_recordGroup e=%s",ext);
1739 if (!rGroup->groupName || !*rGroup->groupName)
1742 sprintf (gprefix, "%s.", rGroup->groupName);
1744 /* determine file type - depending on extension */
1745 if (!rGroup->recordType) {
1746 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1747 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1748 sprintf (ext_res, "%srecordType", gprefix);
1749 rGroup->recordType = res_get (zh->res, ext_res);
1752 /* determine match criteria */
1753 if (!rGroup->recordId) {
1754 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1755 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1756 sprintf (ext_res, "%srecordId", gprefix);
1757 rGroup->recordId = res_get (zh->res, ext_res);
1761 /* determine database name */
1762 if (!rGroup->databaseName) {
1763 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1764 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1765 sprintf (ext_res, "%sdatabase", gprefix);
1766 rGroup->databaseName = res_get (zh->res, ext_res);
1769 if (!rGroup->databaseName)
1770 rGroup->databaseName = "Default";
1772 /* determine if explain database */
1773 sprintf (ext_res, "%sexplainDatabase", gprefix);
1774 rGroup->explainDatabase =
1775 atoi (res_get_def (zh->res, ext_res, "0"));
1778 if (rGroup->flagStoreData == -1) {
1780 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1781 if (!(sval = res_get (zh->res, ext_res))) {
1782 sprintf (ext_res, "%sstoreData", gprefix);
1783 sval = res_get (zh->res, ext_res);
1786 rGroup->flagStoreData = atoi (sval);
1788 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
1791 if (rGroup->flagStoreKeys == -1) {
1794 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1795 sval = res_get (zh->res, ext_res);
1797 sprintf (ext_res, "%sstoreKeys", gprefix);
1798 sval = res_get (zh->res, ext_res);
1800 if (!sval) sval = res_get (zh->res, "storeKeys");
1801 if (sval) rGroup->flagStoreKeys = atoi (sval);
1803 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
1808 /* almost the same as zebra_records_retrieve ... but how did it work?
1809 I mean for multiple records ??? CHECK ??? */
1810 void api_records_retrieve (ZebraHandle zh, ODR stream,
1811 const char *setname, Z_RecordComposition *comp,
1812 oid_value input_format, int num_recs,
1813 ZebraRetrievalRecord *recs)
1817 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1822 zh->errString = odr_strdup (stream, setname);
1828 if (zebra_begin_read (zh))
1831 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1832 for (i = 0; i<num_recs; i++)
1833 pos_array[i] = recs[i].position;
1834 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1837 logf (LOG_DEBUG, "zebraPosSetCreate error");
1839 zh->errString = nmem_strdup (stream->mem, setname);
1843 for (i = 0; i<num_recs; i++)
1847 recs[i].errCode = 0;
1848 recs[i].format = VAL_SUTRS;
1849 recs[i].len = strlen(poset[i].term);
1850 recs[i].buf = poset[i].term;
1851 recs[i].base = poset[i].db;
1855 else if (poset[i].sysno)
1857 /* changed here ??? CHECK ??? */
1860 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1861 stream, input_format, comp,
1866 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1867 memcpy(recs[i].buf, b, recs[i].len);
1868 recs[i].errString = 0; /* Hmmm !!! we should get this */
1869 recs[i].sysno = poset[i].sysno;
1870 recs[i].score = poset[i].score;
1876 sprintf (num_str, "%d", pos_array[i]);
1878 zh->errString = odr_strdup (stream, num_str);
1883 zebraPosSetDestroy (zh, poset, num_recs);
1885 zebra_end_read (zh);
1890 /* ---------------------------------------------------------------------------
1891 Record insert(=update), delete
1893 If sysno is provided, then it's used to identify the reocord.
1894 If not, and match_criteria is provided, then sysno is guessed
1895 If not, and a record is provided, then sysno is got from there
1898 int zebra_insert_record (ZebraHandle zh,
1899 struct recordGroup *rGroup,
1900 const char *recordType,
1901 int sysno, const char *match, const char *fname,
1902 const char *buf, int buf_size,
1903 int force_update) /* This one is ignored */
1907 yaz_log(LOG_API,"zebra_insert_record sysno=%d",sysno);
1909 if (buf_size < 1) buf_size = strlen(buf);
1911 zebra_begin_trans(zh, 1);
1912 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1913 0, /* delete_flag */
1919 0); /* allow_update */
1920 zebra_end_trans(zh);
1921 if (res < 0) return (res);
1925 int zebra_update_record (ZebraHandle zh,
1926 struct recordGroup *rGroup,
1927 const char *recordType,
1928 int sysno, const char *match, const char *fname,
1929 const char *buf, int buf_size,
1934 yaz_log(LOG_API,"zebra_update_record sysno=%d",sysno);
1936 if (buf_size < 1) buf_size = strlen(buf);
1938 zebra_begin_trans(zh, 1);
1939 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1940 0, /* delete_flag */
1946 1); /* allow_update */
1947 zebra_end_trans(zh);
1953 int zebra_delete_record (ZebraHandle zh,
1954 struct recordGroup *rGroup,
1955 const char *recordType,
1956 int sysno, const char *match, const char *fname,
1957 const char *buf, int buf_size,
1961 yaz_log(LOG_API,"zebra_delete_record sysno=%d",sysno);
1963 if (buf_size < 1) buf_size = strlen(buf);
1965 zebra_begin_trans(zh, 1);
1966 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1967 1, /* delete_flag */
1973 1); /* allow_update */
1974 zebra_end_trans(zh);
1978 /* ---------------------------------------------------------------------------
1982 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
1983 const char *setname)
1987 ODR odr = odr_createmem(ODR_ENCODE);
1988 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
1990 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
1993 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
1995 zebra_search_RPN (zh, query, setname, &hits);
2002 /* ---------------------------------------------------------------------------
2003 Sort - a simplified interface, with optional read locks.
2004 FIXME - This is a horrible name, will conflict with half the applications
2006 int sort (ZebraHandle zh,
2008 const char *sort_spec,
2009 const char *output_setname,
2010 const char **input_setnames
2013 int num_input_setnames = 0;
2014 int sort_status = 0;
2015 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2016 yaz_log(LOG_API,"sort (FIXME) ");
2017 if (!sort_sequence) {
2018 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2023 /* we can do this, since the perl typemap code for char** will
2024 put a NULL at the end of list */
2025 while (input_setnames[num_input_setnames]) num_input_setnames++;
2027 if (zebra_begin_read (zh))
2030 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2031 output_setname, sort_sequence, &sort_status);
2034 return (sort_status);