1 /* $Id: zebraapi.c,v 1.115 2003-11-28 14:47:45 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 #include <yaz/diagbib1.h>
34 #include <yaz/pquery.h>
35 #include <yaz/sortspec.h>
40 /* simple asserts to validate the most essential input args */
41 #define ASSERTZH assert(zh && zh->service)
42 #define ASSERTZHRES assert(zh && zh->service && zh->res)
43 #define ASSERTZS assert(zs)
45 /* A simple log macro */
46 /* don't break with older yazen that lack log_app2 */
48 #define LOG_API LOG_APP2
50 #define LOG_API LOG_DEBUG
53 static Res zebra_open_res (ZebraHandle zh);
54 static void zebra_close_res (ZebraHandle zh);
57 static void zebra_chdir (ZebraService zs)
61 yaz_log(LOG_API,"zebra_chdir");
62 dir = res_get (zs->global_res, "chdir");
65 logf (LOG_DEBUG, "chdir %s", dir);
73 static void zebra_flush_reg (ZebraHandle zh)
76 yaz_log(LOG_API,"zebra_flush_reg");
78 zebraExplain_flush (zh->reg->zei, zh);
80 extract_flushWriteKeys (zh);
81 zebra_index_merge (zh);
84 static struct zebra_register *zebra_register_open (ZebraService zs,
86 int rw, int useshadow,
88 const char *reg_path);
89 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
91 ZebraHandle zebra_open (ZebraService zs)
94 const char *default_encoding;
96 yaz_log(LOG_API,"zebra_open");
101 zh = (ZebraHandle) xmalloc (sizeof(*zh));
102 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
105 zh->reg = 0; /* no register attached yet */
112 zh->reg_name = xstrdup ("");
114 zh->num_basenames = 0;
123 zh->admin_databaseName = 0;
125 zh->shadow_enable = 1;
127 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
128 zh->record_encoding = xstrdup (default_encoding);
131 yaz_iconv_open ("UTF-8", default_encoding);
132 if (zh->iconv_to_utf8 == 0)
133 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
135 zh->iconv_from_utf8 =
136 yaz_iconv_open (default_encoding, "UTF-8");
137 if (zh->iconv_to_utf8 == 0)
138 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
141 zebra_mutex_cond_lock (&zs->session_lock);
143 zh->next = zs->sessions;
146 zebra_mutex_cond_unlock (&zs->session_lock);
151 ZebraService zebra_start (const char *configName)
155 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
157 if ((res = res_open (configName, 0)))
159 ZebraService zh = xmalloc (sizeof(*zh));
161 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
163 zh->global_res = res;
164 zh->configName = xstrdup(configName);
169 zebra_mutex_cond_init (&zh->session_lock);
170 if (!res_get (zh->global_res, "passwd"))
171 zh->passwd_db = NULL;
174 zh->passwd_db = passwd_db_open ();
176 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
178 passwd_db_file (zh->passwd_db,
179 res_get (zh->global_res, "passwd"));
181 zh->path_root = res_get (zh->global_res, "root");
188 void zebra_pidfname(ZebraService zs, char *path)
190 zebra_lock_prefix (zs->global_res, path);
191 strcat(path, "zebrasrv.pid");
195 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
196 int rw, int useshadow, Res res,
197 const char *reg_path)
199 struct zebra_register *reg;
200 int record_compression = REC_COMPRESS_NONE;
201 char *recordCompression = 0;
205 reg = xmalloc (sizeof(*reg));
208 reg->name = xstrdup (name);
215 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
216 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
218 reg->dh = data1_createx (DATA1_FLAG_XML);
221 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
224 data1_destroy(reg->dh);
228 bf_cache (reg->bfs, res_get (res, "shadow"));
229 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
230 DEFAULT_PROFILE_PATH));
231 data1_set_tabroot (reg->dh, reg_path);
232 reg->recTypes = recTypes_init (reg->dh);
233 recTypes_default_handlers (reg->recTypes);
235 reg->zebra_maps = zebra_maps_open (res, reg_path);
236 reg->rank_classes = NULL;
240 reg->keys.buf_max = 0;
242 reg->sortKeys.buf = 0;
243 reg->sortKeys.buf_max = 0;
256 reg->key_file_no = 0;
258 zebraRankInstall (reg, rank1_class);
259 zebraRankInstall (reg, rankzv_class);
260 zebraRankInstall (reg, rankliv_class);
262 recordCompression = res_get_def (res, "recordCompression", "none");
263 if (!strcmp (recordCompression, "none"))
264 record_compression = REC_COMPRESS_NONE;
265 if (!strcmp (recordCompression, "bzip2"))
266 record_compression = REC_COMPRESS_BZIP2;
268 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
270 logf (LOG_WARN, "rec_open");
275 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
277 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
279 logf (LOG_WARN, "dict_open");
282 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
284 logf (LOG_WARN, "sortIdx_open");
287 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
289 struct ISAMS_M_s isams_m;
290 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
291 key_isams_m(res, &isams_m))))
293 logf (LOG_WARN, "isams_open");
297 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
299 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
300 sizeof (struct it_key), res)))
302 logf (LOG_WARN, "is_open");
306 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
308 struct ISAMC_M_s isamc_m;
309 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
310 rw, key_isamc_m(res, &isamc_m))))
312 logf (LOG_WARN, "isc_open");
316 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
318 struct ISAMD_M_s isamd_m;
320 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
321 rw, key_isamd_m(res, &isamd_m))))
323 logf (LOG_WARN, "isamd_open");
327 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
329 struct ISAMC_M_s isamc_m;
331 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
332 rw, key_isamc_m(res, &isamc_m), 0)))
334 logf (LOG_WARN, "isamb_open");
338 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
340 struct ISAMC_M_s isamc_m;
342 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
343 rw, key_isamc_m(res, &isamc_m), 1)))
345 logf (LOG_WARN, "isamb_open");
349 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
351 struct ISAMC_M_s isamc_m;
353 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
354 rw, key_isamc_m(res, &isamc_m), -1)))
356 logf (LOG_WARN, "isamb_open");
360 reg->zei = zebraExplain_open (reg->records, reg->dh,
365 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
369 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
373 int zebra_admin_shutdown (ZebraHandle zh)
376 yaz_log(LOG_API,"zebra_admin_shutdown");
379 zebra_mutex_cond_lock (&zh->service->session_lock);
380 zh->service->stop_flag = 1;
381 zebra_mutex_cond_unlock (&zh->service->session_lock);
385 int zebra_admin_start (ZebraHandle zh)
389 yaz_log(LOG_API,"zebra_admin_start");
392 zebra_mutex_cond_lock (&zs->session_lock);
393 zebra_mutex_cond_unlock (&zs->session_lock);
397 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
400 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
405 zebraExplain_close (reg->zei);
406 dict_close (reg->dict);
408 dict_close (reg->matchDict);
409 sortIdx_close (reg->sortIdx);
411 isams_close (reg->isams);
413 is_close (reg->isam);
415 isc_close (reg->isamc);
417 isamd_close (reg->isamd);
419 isamb_close (reg->isamb);
420 rec_close (®->records);
423 recTypes_destroy (reg->recTypes);
424 zebra_maps_close (reg->zebra_maps);
425 zebraRankDestroy (reg);
426 bfs_destroy (reg->bfs);
427 data1_destroy (reg->dh);
429 xfree (reg->sortKeys.buf);
430 xfree (reg->keys.buf);
432 xfree (reg->key_buf);
437 int zebra_stop(ZebraService zs)
441 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
445 zebra_close (zs->sessions);
448 zebra_mutex_cond_destroy (&zs->session_lock);
451 passwd_db_close (zs->passwd_db);
453 res_close (zs->global_res);
454 xfree (zs->configName);
455 xfree (zs->path_root);
460 int zebra_close (ZebraHandle zh)
463 struct zebra_session **sp;
466 yaz_log(LOG_API,"zebra_close");
473 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
474 resultSetDestroy (zh, -1, 0, 0);
477 zebra_register_close (zh->service, zh->reg);
478 zebra_close_res (zh);
480 xfree (zh->record_encoding);
482 for (i = 0; i < zh->num_basenames; i++)
483 xfree (zh->basenames[i]);
484 xfree (zh->basenames);
486 if (zh->iconv_to_utf8 != 0)
487 yaz_iconv_close (zh->iconv_to_utf8);
488 if (zh->iconv_from_utf8 != 0)
489 yaz_iconv_close (zh->iconv_from_utf8);
491 xfree (zh->admin_databaseName);
492 zebra_mutex_cond_lock (&zs->session_lock);
493 zebra_lock_destroy (zh->lock_normal);
494 zebra_lock_destroy (zh->lock_shadow);
506 zebra_mutex_cond_unlock (&zs->session_lock);
507 xfree (zh->reg_name);
508 zh->service=0; /* more likely to trigger an assert */
513 struct map_baseinfo {
519 char **new_basenames;
523 static Res zebra_open_res (ZebraHandle zh)
532 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
533 res = res_open (fname, zh->service->global_res);
535 res = zh->service->global_res;
537 else if (*zh->reg_name == 0)
539 res = zh->service->global_res;
543 yaz_log (LOG_WARN, "no register root specified");
544 return 0; /* no path for register - fail! */
549 static void zebra_close_res (ZebraHandle zh)
553 if (zh->res != zh->service->global_res)
558 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
562 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
566 assert (zh->reg == 0);
567 assert (*zh->reg_name == 0);
573 resultSetInvalidate (zh);
574 zebra_register_close (zh->service, zh->reg);
579 xfree (zh->reg_name);
580 zh->reg_name = xstrdup (new_reg);
582 xfree (zh->path_reg);
584 if (zh->service->path_root)
586 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
587 strlen(zh->reg_name) + 3);
588 strcpy (zh->path_reg, zh->service->path_root);
591 strcat (zh->path_reg, "/");
592 strcat (zh->path_reg, zh->reg_name);
595 zh->res = zebra_open_res (zh);
598 zebra_lock_destroy (zh->lock_normal);
602 zebra_lock_destroy (zh->lock_shadow);
608 const char *lock_area =res_get (zh->res, "lockDir");
610 if (!lock_area && zh->path_reg)
611 res_put (zh->res, "lockDir", zh->path_reg);
612 sprintf (fname, "norm.%s.LCK", zh->reg_name);
614 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
616 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
618 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
624 void map_basenames_func (void *vp, const char *name, const char *value)
626 struct map_baseinfo *p = (struct map_baseinfo *) vp;
628 char fromdb[128], todb[8][128];
631 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
632 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
633 todb[5], todb[6], todb[7]);
637 for (i = 0; i<p->num_bases; i++)
638 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
641 for (i = 0; i < no; i++)
643 if (p->new_num_bases == p->new_num_max)
645 p->new_basenames[(p->new_num_bases)++] =
646 nmem_strdup (p->mem, todb[i]);
652 void map_basenames (ZebraHandle zh, ODR stream,
653 int *num_bases, char ***basenames)
655 struct map_baseinfo info;
656 struct map_baseinfo *p = &info;
659 yaz_log(LOG_API,"map_basenames ");
663 info.num_bases = *num_bases;
664 info.basenames = *basenames;
665 info.new_num_max = 128;
666 info.new_num_bases = 0;
667 info.new_basenames = (char **)
668 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
669 info.mem = stream->mem;
671 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
673 for (i = 0; i<p->num_bases; i++)
674 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
676 p->new_basenames[(p->new_num_bases)++] =
677 nmem_strdup (p->mem, p->basenames[i]);
679 *num_bases = info.new_num_bases;
680 *basenames = info.new_basenames;
681 for (i = 0; i<*num_bases; i++)
682 logf (LOG_LOG, "base %s", (*basenames)[i]);
685 int zebra_select_database (ZebraHandle zh, const char *basename)
688 yaz_log(LOG_API,"zebra_select_database %s",basename);
690 return zebra_select_databases (zh, 1, &basename);
693 int zebra_select_databases (ZebraHandle zh, int num_bases,
694 const char **basenames)
701 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
702 num_bases,basenames[0]);
710 for (i = 0; i < zh->num_basenames; i++)
711 xfree (zh->basenames[i]);
712 xfree (zh->basenames);
714 zh->num_basenames = num_bases;
715 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
716 for (i = 0; i < zh->num_basenames; i++)
717 zh->basenames[i] = xstrdup (basenames[i]);
719 cp = strrchr(basenames[0], '/');
722 len = cp - basenames[0];
723 new_reg = xmalloc (len + 1);
724 memcpy (new_reg, basenames[0], len);
728 new_reg = xstrdup ("");
729 for (i = 1; i<num_bases; i++)
733 cp1 = strrchr (basenames[i], '/');
741 if (len != cp1 - basenames[i] ||
742 memcmp (basenames[i], new_reg, len))
757 zebra_select_register (zh, new_reg);
764 if (!zh->lock_normal || !zh->lock_shadow)
772 int zebra_search_RPN (ZebraHandle zh, ODR o,
773 Z_RPNQuery *query, const char *setname, int *hits)
776 yaz_log(LOG_API,"zebra_search_rpn");
781 if (zebra_begin_read (zh))
784 zebra_livcode_transform(zh, query);
786 resultSetAddRPN (zh, odr_extract_mem(o), query,
787 zh->num_basenames, zh->basenames, setname);
795 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
796 const char *setname, Z_RecordComposition *comp,
797 oid_value input_format, int num_recs,
798 ZebraRetrievalRecord *recs)
801 int i, *pos_array, ret = 0;
803 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
809 zh->errString = odr_strdup (stream, setname);
815 if (zebra_begin_read (zh))
818 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
819 for (i = 0; i<num_recs; i++)
820 pos_array[i] = recs[i].position;
821 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
824 logf (LOG_DEBUG, "zebraPosSetCreate error");
826 zh->errString = nmem_strdup (stream->mem, setname);
831 for (i = 0; i<num_recs; i++)
836 recs[i].format = VAL_SUTRS;
837 recs[i].len = strlen(poset[i].term);
838 recs[i].buf = poset[i].term;
839 recs[i].base = poset[i].db;
841 else if (poset[i].sysno)
844 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
845 stream, input_format, comp,
846 &recs[i].format, &recs[i].buf,
849 recs[i].errString = NULL;
855 sprintf (num_str, "%d", pos_array[i]);
857 zh->errString = odr_strdup (stream, num_str);
862 zebraPosSetDestroy (zh, poset, num_recs);
869 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
870 oid_value attributeset,
871 int *position, int *num_entries, ZebraScanEntry **entries,
875 yaz_log(LOG_API,"zebra_scan");
877 if (zebra_begin_read (zh))
883 rpn_scan (zh, stream, zapt, attributeset,
884 zh->num_basenames, zh->basenames, position,
885 num_entries, entries, is_partial, 0, 0);
890 int zebra_sort (ZebraHandle zh, ODR stream,
891 int num_input_setnames, const char **input_setnames,
892 const char *output_setname, Z_SortKeySpecList *sort_sequence,
896 yaz_log(LOG_API,"zebra_sort");
898 if (zebra_begin_read (zh))
900 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
901 output_setname, sort_sequence, sort_status);
906 int zebra_deleleResultSet(ZebraHandle zh, int function,
907 int num_setnames, char **setnames,
912 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
914 if (zebra_begin_read(zh))
915 return Z_DeleteStatus_systemProblemAtTarget;
918 case Z_DeleteRequest_list:
919 resultSetDestroy (zh, num_setnames, setnames, statuses);
921 case Z_DeleteRequest_all:
922 resultSetDestroy (zh, -1, 0, statuses);
926 status = Z_DeleteStatus_success;
927 for (i = 0; i<num_setnames; i++)
928 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
929 status = statuses[i];
933 int zebra_errCode (ZebraHandle zh)
937 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
940 yaz_log(LOG_API,"zebra_errCode: o");
944 const char *zebra_errString (ZebraHandle zh)
948 e= diagbib1_str (zh->errCode);
949 yaz_log(LOG_API,"zebra_errString: %s",e);
953 char *zebra_errAdd (ZebraHandle zh)
958 yaz_log(LOG_API,"zebra_errAdd: %s",a);
962 void zebra_clearError(ZebraHandle zh)
971 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
975 yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
978 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
980 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
984 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
988 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
989 const char *record_type)
992 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
993 database, record_type);
995 if (zebra_select_database(zh, database))
997 zebra_begin_trans (zh, 1);
998 xfree (zh->admin_databaseName);
999 zh->admin_databaseName = xstrdup(database);
1003 int zebra_admin_import_end (ZebraHandle zh)
1006 yaz_log(LOG_API,"zebra_admin_import_end");
1008 zebra_end_trans (zh);
1012 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1017 yaz_log(LOG_API,"zebra_admin_import_segment");
1019 for (i = 0; i<segment->num_segmentRecords; i++)
1021 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1022 const char *databaseName = npr->databaseName;
1025 databaseName = zh->admin_databaseName;
1026 printf ("--------------%d--------------------\n", i);
1027 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1029 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1030 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1032 Odr_oct *oct = fragment->u.notExternallyTagged;
1033 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1037 extract_rec_in_mem (zh, "grs.sgml",
1040 0 /* delete_flag */,
1045 0 /* match criteria */);
1052 int zebra_admin_exchange_record (ZebraHandle zh,
1053 const char *database,
1054 const char *rec_buf,
1056 const char *recid_buf, size_t recid_len,
1063 yaz_log(LOG_API,"zebra_admin_exchange_record db=%s ac=%d",
1067 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1069 memcpy (recid_z, recid_buf, recid_len);
1070 recid_z[recid_len] = 0;
1072 zebra_begin_trans(zh,1);
1074 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1077 if (action == 1) /* fail if insert */
1079 zebra_end_trans(zh);
1083 memcpy (&sysno, rinfo+1, sizeof(sysno));
1087 if (action == 2 || action == 3) /* fail if delete or update */
1089 zebra_end_trans(zh);
1093 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1094 action == 3 ? 1 : 0 /* delete flag */,
1095 0, &sysno, 1, 1, 0);
1098 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1100 else if (action == 3)
1102 dict_delete (zh->reg->matchDict, recid_z);
1104 zebra_end_trans(zh);
1108 int delete_w_handle(const char *info, void *handle)
1110 ZebraHandle zh = (ZebraHandle) handle;
1113 if (*info == sizeof(pos))
1115 memcpy (&pos, info+1, sizeof(pos));
1116 isamb_unlink(zh->reg->isamb, pos);
1121 static int delete_SU_handle(void *handle, int ord)
1123 ZebraHandle zh = (ZebraHandle) handle;
1127 ord_len = key_SU_encode (ord, ord_buf);
1128 ord_buf[ord_len] = '\0';
1130 assert (zh->reg->isamb);
1131 dict_delete_subtree(zh->reg->dict, ord_buf,
1132 zh, delete_w_handle);
1136 int zebra_drop_database (ZebraHandle zh, const char *database)
1140 yaz_log(LOG_API,"zebra_drop_database");
1143 if (zebra_select_database (zh, database))
1145 if (zebra_begin_trans (zh, 1))
1149 zebraExplain_curDatabase (zh->reg->zei, database);
1151 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1152 zebraExplain_removeDatabase(zh->reg->zei, zh);
1156 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1159 zebra_end_trans (zh);
1163 int zebra_create_database (ZebraHandle zh, const char *database)
1166 yaz_log(LOG_API,"zebra_create_database");
1169 if (zebra_select_database (zh, database))
1171 if (zebra_begin_trans (zh, 1))
1174 /* announce database */
1175 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1176 /* explainDatabase */))
1178 zebra_end_trans (zh);
1180 zh->errString = "database already exist";
1183 zebra_end_trans (zh);
1187 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1188 const char *input_str, int input_len,
1189 char *output_str, int output_len)
1193 yaz_log(LOG_API,"zebra_string_norm ");
1195 if (!zh->reg->zebra_maps)
1197 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1198 input_str, input_len);
1201 if (wrbuf_len(wrbuf) >= output_len)
1203 if (wrbuf_len(wrbuf))
1204 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1205 output_str[wrbuf_len(wrbuf)] = '\0';
1206 return wrbuf_len(wrbuf);
1210 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1212 char state_fname[256];
1217 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1220 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1221 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1222 f = fopen (fname, "w");
1224 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1225 fprintf (f, "%c %d %ld\n", val, seqno, p);
1231 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1233 char state_fname[256];
1238 yaz_log(LOG_API,"zebra_get_state ");
1240 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1241 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1242 f = fopen (fname, "r");
1248 fscanf (f, "%c %d", val, seqno);
1255 int zebra_begin_read (ZebraHandle zh)
1257 return zebra_begin_trans(zh, 0);
1260 int zebra_end_read (ZebraHandle zh)
1262 return zebra_end_trans(zh);
1265 int zebra_begin_trans (ZebraHandle zh, int rw)
1270 zh->errString = "zebra_begin_trans: no database selected";
1274 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1281 const char *rval = 0;
1286 if (zh->trans_no != 1)
1289 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1294 resultSetInvalidate (zh);
1295 zebra_register_close (zh->service, zh->reg);
1297 zh->trans_w_no = zh->trans_no;
1301 zh->records_inserted = 0;
1302 zh->records_updated = 0;
1303 zh->records_deleted = 0;
1304 zh->records_processed = 0;
1306 #if HAVE_SYS_TIMES_H
1310 if (zh->shadow_enable)
1311 rval = res_get (zh->res, "shadow");
1313 for (pass = 0; pass < 2; pass++)
1317 zebra_lock_r (zh->lock_normal);
1318 zebra_lock_w (zh->lock_shadow);
1322 zebra_lock_w (zh->lock_normal);
1323 zebra_lock_w (zh->lock_shadow);
1326 zebra_get_state (zh, &val, &seqno);
1329 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1330 zebra_unlock (zh->lock_shadow);
1331 zebra_unlock (zh->lock_normal);
1335 else if (val == 'd')
1339 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1341 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1342 bf_commitClean (bfs, rval);
1347 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1354 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1358 zebra_set_state (zh, 'd', seqno);
1360 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1361 1, rval ? 1 : 0, zh->res,
1364 zh->reg->seqno = seqno;
1367 zebra_set_state (zh, 'o', seqno);
1369 zebra_unlock (zh->lock_shadow);
1370 zebra_unlock (zh->lock_normal);
1376 zh->errString = "zebra_begin_trans: cannot open register";
1377 yaz_log(LOG_FATAL, zh->errString);
1389 if (zh->trans_no != 1)
1391 zebra_flush_reg (zh);
1395 #if HAVE_SYS_TIMES_H
1404 if (!zh->lock_normal || !zh->lock_shadow)
1410 zebra_get_state (zh, &val, &seqno);
1416 else if (seqno != zh->reg->seqno)
1418 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1419 seqno, zh->reg->seqno);
1422 else if (zh->reg->last_val != val)
1424 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1425 val, zh->reg->last_val);
1432 zebra_lock_r (zh->lock_shadow);
1434 zebra_lock_r (zh->lock_normal);
1438 resultSetInvalidate (zh);
1439 zebra_register_close (zh->service, zh->reg);
1441 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1442 0, val == 'c' ? 1 : 0,
1443 zh->res, zh->path_reg);
1446 zebra_unlock (zh->lock_normal);
1447 zebra_unlock (zh->lock_shadow);
1452 zh->reg->last_val = val;
1453 zh->reg->seqno = seqno;
1458 int zebra_end_trans (ZebraHandle zh)
1460 ZebraTransactionStatus dummy;
1461 yaz_log(LOG_API,"zebra_end_trans");
1462 return zebra_end_transaction(zh, &dummy);
1465 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1472 yaz_log(LOG_API,"zebra_end_transaction");
1474 status->processed = 0;
1475 status->inserted = 0;
1476 status->updated = 0;
1477 status->deleted = 0;
1481 if (!zh->res || !zh->reg)
1484 zh->errString = "zebra_end_trans: no open transaction";
1487 if (zh->trans_no != zh->trans_w_no)
1490 if (zh->trans_no != 0)
1493 /* release read lock */
1495 zebra_unlock (zh->lock_normal);
1496 zebra_unlock (zh->lock_shadow);
1499 { /* release write lock */
1503 yaz_log (LOG_LOG, "zebra_end_trans");
1504 rval = res_get (zh->res, "shadow");
1506 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1508 zebra_flush_reg (zh);
1510 resultSetInvalidate (zh);
1512 zebra_register_close (zh->service, zh->reg);
1515 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1516 zh->records_processed, zh->records_inserted,
1517 zh->records_updated, zh->records_deleted);
1519 status->processed = zh->records_processed;
1520 status->inserted = zh->records_inserted;
1521 status->updated = zh->records_updated;
1522 status->deleted = zh->records_deleted;
1524 zebra_get_state (zh, &val, &seqno);
1527 BFiles bfs = bfs_create (rval, zh->path_reg);
1528 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1529 bf_commitClean (bfs, rval);
1534 zebra_set_state (zh, 'o', seqno);
1536 zebra_unlock (zh->lock_shadow);
1537 zebra_unlock (zh->lock_normal);
1540 #if HAVE_SYS_TIMES_H
1542 logf (LOG_LOG, "user/system: %ld/%ld",
1543 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1544 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1546 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1547 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1552 int zebra_repository_update (ZebraHandle zh)
1556 logf (LOG_LOG|LOG_API, "updating %s", zh->rGroup.path);
1557 repositoryUpdate (zh);
1561 int zebra_repository_delete (ZebraHandle zh)
1565 logf (LOG_LOG|LOG_API, "deleting %s", zh->rGroup.path);
1566 repositoryDelete (zh);
1570 int zebra_repository_show (ZebraHandle zh)
1573 yaz_log(LOG_API,"zebra_repository_show");
1575 repositoryShow (zh);
1579 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1593 rval = res_get (zh->res, "shadow");
1596 logf (LOG_WARN, "Cannot perform commit");
1597 logf (LOG_WARN, "No shadow area defined");
1601 zebra_lock_w (zh->lock_normal);
1602 zebra_lock_r (zh->lock_shadow);
1604 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1606 zebra_get_state (zh, &val, &seqno);
1609 bf_cache (bfs, rval);
1610 if (bf_commitExists (bfs))
1613 zebra_set_state (zh, 'd', seqno);
1616 zebra_set_state (zh, 'c', seqno);
1618 logf (LOG_LOG, "commit start");
1619 bf_commitExec (bfs);
1624 logf (LOG_LOG, "commit clean");
1625 bf_commitClean (bfs, rval);
1627 zebra_set_state (zh, 'o', seqno);
1631 logf (LOG_LOG, "nothing to commit");
1635 zebra_unlock (zh->lock_shadow);
1636 zebra_unlock (zh->lock_normal);
1640 int zebra_clean (ZebraHandle zh)
1642 yaz_log(LOG_API,"zebra_clean");
1643 return zebra_commit_ex(zh, 1);
1646 int zebra_commit (ZebraHandle zh)
1648 yaz_log(LOG_API,"zebra_commit");
1649 return zebra_commit_ex(zh, 0);
1652 int zebra_init (ZebraHandle zh)
1657 yaz_log(LOG_API,"zebra_init");
1665 rval = res_get (zh->res, "shadow");
1667 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1670 bf_cache (bfs, rval);
1674 zebra_set_state (zh, 'o', 0);
1678 int zebra_compact (ZebraHandle zh)
1682 yaz_log(LOG_API,"zebra_compact");
1689 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1695 int zebra_record_insert (ZebraHandle zh, const char *buf, int len, int *sysno)
1699 yaz_log(LOG_API,"zebra_record_insert");
1703 if (zebra_begin_trans (zh, 1))
1705 extract_rec_in_mem (zh, "grs.sgml",
1707 "Default", /* database */
1708 0 /* delete_flag */,
1713 0 /* match criteria */);
1714 if (zebra_end_trans (zh))
1721 int zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1724 yaz_log(LOG_API,"zebra_set_group");
1726 memcpy (&zh->rGroup, rg, sizeof(*rg));
1730 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1733 yaz_log(LOG_API,"zebra_result");
1734 *code = zh->errCode;
1735 *addinfo = zh->errString;
1739 int zebra_shadow_enable (ZebraHandle zh, int value)
1742 yaz_log(LOG_API,"zebra_shadow_enable");
1744 zh->shadow_enable = value;
1748 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1751 yaz_log(LOG_API,"zebra_record_encoding");
1753 xfree (zh->record_encoding);
1757 * Something about charset aliases. Oleg???
1760 if (zh->iconv_to_utf8 != 0)
1761 yaz_iconv_close(zh->iconv_to_utf8);
1762 if (zh->iconv_from_utf8 != 0)
1763 yaz_iconv_close(zh->iconv_from_utf8);
1765 zh->record_encoding = xstrdup (encoding);
1767 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1770 yaz_iconv_open ("UTF-8", encoding);
1771 if (zh->iconv_to_utf8 == 0)
1772 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1773 zh->iconv_from_utf8 =
1774 yaz_iconv_open (encoding, "UTF-8");
1775 if (zh->iconv_to_utf8 == 0)
1776 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1781 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1784 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1786 res_put(zh->res, name, value);
1790 const char *zebra_get_resource(ZebraHandle zh,
1791 const char *name, const char *defaultvalue)
1795 v= res_get_def( zh->res, name, (char *)defaultvalue);
1797 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1801 /* moved from zebra_api_ext.c by pop */
1802 /* FIXME: Should this really be public??? -Heikki */
1804 int zebra_trans_no (ZebraHandle zh)
1807 yaz_log(LOG_API,"zebra_trans_no");
1808 return zh->trans_no;
1811 int zebra_get_shadow_enable (ZebraHandle zh)
1813 yaz_log(LOG_API,"zebra_get_shadow_enable");
1814 return (zh->shadow_enable);
1817 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1819 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1820 zh->shadow_enable = value;
1824 int init_recordGroup (struct recordGroup *rg)
1827 yaz_log(LOG_API,"init_recordGroup");
1828 rg->groupName = NULL;
1829 rg->databaseName = NULL;
1831 rg->recordId = NULL;
1832 rg->recordType = NULL;
1833 rg->flagStoreData = -1;
1834 rg->flagStoreKeys = -1;
1836 rg->databaseNamePath = 0;
1837 rg->explainDatabase = 0;
1838 rg->fileVerboseLimit = 100000;
1839 rg->followLinks = -1;
1844 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1845 called... and in general... Should be moved to somewhere else */
1846 void res_get_recordGroup (ZebraHandle zh,
1847 struct recordGroup *rGroup,
1853 yaz_log(LOG_API,"res_get_recordGroup e=%s",ext);
1854 if (!rGroup->groupName || !*rGroup->groupName)
1857 sprintf (gprefix, "%s.", rGroup->groupName);
1859 /* determine file type - depending on extension */
1860 if (!rGroup->recordType) {
1861 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1862 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1863 sprintf (ext_res, "%srecordType", gprefix);
1864 rGroup->recordType = res_get (zh->res, ext_res);
1867 /* determine match criteria */
1868 if (!rGroup->recordId) {
1869 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1870 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1871 sprintf (ext_res, "%srecordId", gprefix);
1872 rGroup->recordId = res_get (zh->res, ext_res);
1876 /* determine database name */
1877 if (!rGroup->databaseName) {
1878 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1879 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1880 sprintf (ext_res, "%sdatabase", gprefix);
1881 rGroup->databaseName = res_get (zh->res, ext_res);
1884 if (!rGroup->databaseName)
1885 rGroup->databaseName = "Default";
1887 /* determine if explain database */
1888 sprintf (ext_res, "%sexplainDatabase", gprefix);
1889 rGroup->explainDatabase =
1890 atoi (res_get_def (zh->res, ext_res, "0"));
1893 if (rGroup->flagStoreData == -1)
1896 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1897 if (!(sval = res_get (zh->res, ext_res)))
1899 sprintf (ext_res, "%sstoreData", gprefix);
1900 sval = res_get (zh->res, ext_res);
1903 rGroup->flagStoreData = atoi (sval);
1905 if (rGroup->flagStoreData == -1)
1906 rGroup->flagStoreData = 0;
1909 if (rGroup->flagStoreKeys == -1)
1913 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1914 sval = res_get (zh->res, ext_res);
1917 sprintf (ext_res, "%sstoreKeys", gprefix);
1918 sval = res_get (zh->res, ext_res);
1921 sval = res_get (zh->res, "storeKeys");
1923 rGroup->flagStoreKeys = atoi (sval);
1925 if (rGroup->flagStoreKeys == -1)
1926 rGroup->flagStoreKeys = 0;
1930 /* almost the same as zebra_records_retrieve ... but how did it work?
1931 I mean for multiple records ??? CHECK ??? */
1932 void api_records_retrieve (ZebraHandle zh, ODR stream,
1933 const char *setname, Z_RecordComposition *comp,
1934 oid_value input_format, int num_recs,
1935 ZebraRetrievalRecord *recs)
1939 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1944 zh->errString = odr_strdup (stream, setname);
1950 if (zebra_begin_read (zh))
1953 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1954 for (i = 0; i<num_recs; i++)
1955 pos_array[i] = recs[i].position;
1956 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1959 logf (LOG_DEBUG, "zebraPosSetCreate error");
1961 zh->errString = nmem_strdup (stream->mem, setname);
1965 for (i = 0; i<num_recs; i++)
1969 recs[i].errCode = 0;
1970 recs[i].format = VAL_SUTRS;
1971 recs[i].len = strlen(poset[i].term);
1972 recs[i].buf = poset[i].term;
1973 recs[i].base = poset[i].db;
1977 else if (poset[i].sysno)
1979 /* changed here ??? CHECK ??? */
1982 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1983 stream, input_format, comp,
1988 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1989 memcpy(recs[i].buf, b, recs[i].len);
1990 recs[i].errString = 0; /* Hmmm !!! we should get this */
1991 recs[i].sysno = poset[i].sysno;
1992 recs[i].score = poset[i].score;
1998 sprintf (num_str, "%d", pos_array[i]);
2000 zh->errString = odr_strdup (stream, num_str);
2005 zebraPosSetDestroy (zh, poset, num_recs);
2007 zebra_end_read (zh);
2012 /* ---------------------------------------------------------------------------
2013 Record insert(=update), delete
2015 If sysno is provided, then it's used to identify the reocord.
2016 If not, and match_criteria is provided, then sysno is guessed
2017 If not, and a record is provided, then sysno is got from there
2018 NOTE: Now returns 0 at success and updates sysno, which is an int*
2022 int zebra_insert_record (ZebraHandle zh,
2023 struct recordGroup *rGroup,
2024 const char *recordType,
2025 int *sysno, const char *match, const char *fname,
2026 const char *buf, int buf_size,
2027 int force_update) /* This one is ignored */
2031 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
2033 if (buf_size < 1) buf_size = strlen(buf);
2035 zebra_begin_trans(zh, 1);
2036 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2037 0, /* delete_flag */
2043 0); /* allow_update */
2044 zebra_end_trans(zh);
2045 if (res < 0) return (res);
2049 int zebra_update_record (ZebraHandle zh,
2050 struct recordGroup *rGroup,
2051 const char *recordType,
2052 int* sysno, const char *match, const char *fname,
2053 const char *buf, int buf_size,
2058 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
2060 if (buf_size < 1) buf_size = strlen(buf);
2062 zebra_begin_trans(zh, 1);
2063 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2064 0, /* delete_flag */
2070 1); /* allow_update */
2071 zebra_end_trans(zh);
2075 int zebra_delete_record (ZebraHandle zh,
2076 struct recordGroup *rGroup,
2077 const char *recordType,
2078 int *sysno, const char *match, const char *fname,
2079 const char *buf, int buf_size,
2083 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2085 if (buf_size < 1) buf_size = strlen(buf);
2087 zebra_begin_trans(zh, 1);
2088 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2089 1, /* delete_flag */
2095 1); /* allow_update */
2096 zebra_end_trans(zh);
2100 /* ---------------------------------------------------------------------------
2104 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2105 const char *setname, int *numhits)
2110 ODR odr = odr_createmem(ODR_ENCODE);
2112 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2114 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2117 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2119 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2123 yaz_log(LOG_API,"Hits: %d",hits);
2131 /* ---------------------------------------------------------------------------
2132 Sort - a simplified interface, with optional read locks.
2133 FIXME - This is a horrible name, will conflict with half the applications
2135 int zebra_sort_by_specstr (ZebraHandle zh,
2137 const char *sort_spec,
2138 const char *output_setname,
2139 const char **input_setnames)
2141 int num_input_setnames = 0;
2142 int sort_status = 0;
2143 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2144 yaz_log(LOG_API,"sort (FIXME) ");
2147 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2152 /* we can do this, since the perl typemap code for char** will
2153 put a NULL at the end of list */
2154 while (input_setnames[num_input_setnames]) num_input_setnames++;
2156 if (zebra_begin_read (zh))
2159 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2160 output_setname, sort_sequence, &sort_status);