1 /* $Id: zebraapi.c,v 1.120.2.5 2005-05-06 18:59:23 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
34 #include <yaz/diagbib1.h>
35 #include <yaz/pquery.h>
36 #include <yaz/sortspec.h>
41 /* simple asserts to validate the most essential input args */
42 #define ASSERTZH assert(zh && zh->service)
43 #define ASSERTZHRES assert(zh && zh->service && zh->res)
44 #define ASSERTZS assert(zs)
46 /* A simple log macro */
47 /* don't break with older yazen that lack log_app2 */
49 #define LOG_API LOG_APP2
51 #define LOG_API LOG_DEBUG
54 static Res zebra_open_res (ZebraHandle zh);
55 static void zebra_close_res (ZebraHandle zh);
58 static void zebra_chdir (ZebraService zs)
62 yaz_log(LOG_API,"zebra_chdir");
63 dir = res_get (zs->global_res, "chdir");
66 logf (LOG_DEBUG, "chdir %s", dir);
74 static void zebra_flush_reg (ZebraHandle zh)
77 yaz_log(LOG_API,"zebra_flush_reg");
79 zebraExplain_flush (zh->reg->zei, zh);
81 extract_flushWriteKeys (zh,1 /* final */);
82 zebra_index_merge (zh );
85 static struct zebra_register *zebra_register_open (ZebraService zs,
87 int rw, int useshadow,
89 const char *reg_path);
90 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
92 ZebraHandle zebra_open (ZebraService zs)
95 const char *default_encoding;
97 yaz_log(LOG_API,"zebra_open");
102 zh = (ZebraHandle) xmalloc (sizeof(*zh));
103 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
106 zh->reg = 0; /* no register attached yet */
114 zh->reg_name = xstrdup ("");
116 zh->num_basenames = 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)
153 return zebra_start_res(configName, 0, 0);
156 ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res)
160 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
162 if ((res = res_open (configName, def_res, over_res)))
164 ZebraService zh = xmalloc (sizeof(*zh));
166 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
168 zh->global_res = res;
169 zh->configName = xstrdup(configName);
174 zebra_mutex_cond_init (&zh->session_lock);
175 if (!res_get (zh->global_res, "passwd"))
176 zh->passwd_db = NULL;
179 zh->passwd_db = passwd_db_open ();
181 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
183 passwd_db_file (zh->passwd_db,
184 res_get (zh->global_res, "passwd"));
186 zh->path_root = res_get (zh->global_res, "root");
193 void zebra_pidfname(ZebraService zs, char *path)
195 zebra_lock_prefix (zs->global_res, path);
196 strcat(path, "zebrasrv.pid");
200 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
201 int rw, int useshadow, Res res,
202 const char *reg_path)
204 struct zebra_register *reg;
205 int record_compression = REC_COMPRESS_NONE;
206 const char *recordCompression = 0;
207 const char *profilePath;
212 reg = xmalloc (sizeof(*reg));
215 reg->name = xstrdup (name);
222 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
223 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
225 reg->dh = data1_createx (DATA1_FLAG_XML);
228 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
231 data1_destroy(reg->dh);
235 bf_cache (reg->bfs, res_get (res, "shadow"));
237 getcwd(cwd, sizeof(cwd)-1);
238 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
239 yaz_log(LOG_LOG, "profilePath=%s cwd=%s", profilePath, cwd);
241 data1_set_tabpath (reg->dh, profilePath);
242 data1_set_tabroot (reg->dh, reg_path);
243 reg->recTypes = recTypes_init (reg->dh);
244 recTypes_default_handlers (reg->recTypes);
246 reg->zebra_maps = zebra_maps_open (res, reg_path);
247 reg->rank_classes = NULL;
251 reg->keys.buf_max = 0;
253 reg->sortKeys.buf = 0;
254 reg->sortKeys.buf_max = 0;
267 reg->key_file_no = 0;
270 zebraRankInstall (reg, rank1_class);
271 zebraRankInstall (reg, rankzv_class);
272 zebraRankInstall (reg, rankliv_class);
274 recordCompression = res_get_def (res, "recordCompression", "none");
275 if (!strcmp (recordCompression, "none"))
276 record_compression = REC_COMPRESS_NONE;
277 if (!strcmp (recordCompression, "bzip2"))
278 record_compression = REC_COMPRESS_BZIP2;
280 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
282 logf (LOG_WARN, "rec_open");
287 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
289 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
291 logf (LOG_WARN, "dict_open");
294 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
296 logf (LOG_WARN, "sortIdx_open");
299 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
301 struct ISAMS_M_s isams_m;
302 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
303 key_isams_m(res, &isams_m))))
305 logf (LOG_WARN, "isams_open");
309 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
311 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
312 sizeof (struct it_key), res)))
314 logf (LOG_WARN, "is_open");
318 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
320 struct ISAMC_M_s isamc_m;
321 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
322 rw, key_isamc_m(res, &isamc_m))))
324 logf (LOG_WARN, "isc_open");
328 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
330 struct ISAMD_M_s isamd_m;
332 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
333 rw, key_isamd_m(res, &isamd_m))))
335 logf (LOG_WARN, "isamd_open");
339 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
341 struct ISAMC_M_s isamc_m;
343 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
344 rw, key_isamc_m(res, &isamc_m), 0)))
346 logf (LOG_WARN, "isamb_open");
350 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
352 struct ISAMC_M_s isamc_m;
354 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
355 rw, key_isamc_m(res, &isamc_m), 1)))
357 logf (LOG_WARN, "isamb_open");
361 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
363 struct ISAMC_M_s isamc_m;
365 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
366 rw, key_isamc_m(res, &isamc_m), -1)))
368 logf (LOG_WARN, "isamb_open");
372 reg->zei = zebraExplain_open (reg->records, reg->dh,
377 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
381 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
385 int zebra_admin_shutdown (ZebraHandle zh)
388 yaz_log(LOG_API,"zebra_admin_shutdown");
391 zebra_mutex_cond_lock (&zh->service->session_lock);
392 zh->service->stop_flag = 1;
393 zebra_mutex_cond_unlock (&zh->service->session_lock);
397 int zebra_admin_start (ZebraHandle zh)
401 yaz_log(LOG_API,"zebra_admin_start");
404 zebra_mutex_cond_lock (&zs->session_lock);
405 zebra_mutex_cond_unlock (&zs->session_lock);
409 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
412 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
417 zebraExplain_close (reg->zei);
418 dict_close (reg->dict);
420 dict_close (reg->matchDict);
421 sortIdx_close (reg->sortIdx);
423 isams_close (reg->isams);
425 is_close (reg->isam);
427 isc_close (reg->isamc);
429 isamd_close (reg->isamd);
431 isamb_close (reg->isamb);
432 rec_close (®->records);
435 recTypes_destroy (reg->recTypes);
436 zebra_maps_close (reg->zebra_maps);
437 zebraRankDestroy (reg);
438 bfs_destroy (reg->bfs);
439 data1_destroy (reg->dh);
441 xfree (reg->sortKeys.buf);
442 xfree (reg->keys.buf);
444 xfree (reg->key_buf);
449 int zebra_stop(ZebraService zs)
453 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
457 zebra_close (zs->sessions);
460 zebra_mutex_cond_destroy (&zs->session_lock);
463 passwd_db_close (zs->passwd_db);
465 res_close (zs->global_res);
466 xfree (zs->configName);
471 int zebra_close (ZebraHandle zh)
474 struct zebra_session **sp;
477 yaz_log(LOG_API,"zebra_close");
484 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
485 resultSetDestroy (zh, -1, 0, 0);
488 zebra_register_close (zh->service, zh->reg);
489 zebra_close_res (zh);
491 xfree (zh->record_encoding);
493 for (i = 0; i < zh->num_basenames; i++)
494 xfree (zh->basenames[i]);
495 xfree (zh->basenames);
497 if (zh->iconv_to_utf8 != 0)
498 yaz_iconv_close (zh->iconv_to_utf8);
499 if (zh->iconv_from_utf8 != 0)
500 yaz_iconv_close (zh->iconv_from_utf8);
502 zebra_mutex_cond_lock (&zs->session_lock);
503 zebra_lock_destroy (zh->lock_normal);
504 zebra_lock_destroy (zh->lock_shadow);
516 zebra_mutex_cond_unlock (&zs->session_lock);
517 xfree (zh->reg_name);
518 xfree (zh->user_perm);
519 zh->service=0; /* more likely to trigger an assert */
520 xfree (zh->path_reg);
525 struct map_baseinfo {
531 char **new_basenames;
535 static Res zebra_open_res (ZebraHandle zh)
544 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
545 res = res_open (fname, zh->service->global_res, 0);
547 res = zh->service->global_res;
549 else if (*zh->reg_name == 0)
551 res = zh->service->global_res;
555 yaz_log (LOG_WARN, "no register root specified");
556 return 0; /* no path for register - fail! */
561 static void zebra_close_res (ZebraHandle zh)
565 if (zh->res != zh->service->global_res)
570 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
574 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
578 assert (zh->reg == 0);
579 assert (*zh->reg_name == 0);
585 resultSetInvalidate (zh);
586 zebra_register_close (zh->service, zh->reg);
591 xfree (zh->reg_name);
592 zh->reg_name = xstrdup (new_reg);
594 xfree (zh->path_reg);
596 if (zh->service->path_root)
598 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
599 strlen(zh->reg_name) + 3);
600 strcpy (zh->path_reg, zh->service->path_root);
603 strcat (zh->path_reg, "/");
604 strcat (zh->path_reg, zh->reg_name);
607 zh->res = zebra_open_res (zh);
610 zebra_lock_destroy (zh->lock_normal);
614 zebra_lock_destroy (zh->lock_shadow);
620 const char *lock_area =res_get (zh->res, "lockDir");
622 if (!lock_area && zh->path_reg)
623 res_set (zh->res, "lockDir", zh->path_reg);
624 sprintf (fname, "norm.%s.LCK", zh->reg_name);
626 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
628 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
630 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
632 if (!zh->lock_normal || !zh->lock_shadow)
636 zebra_lock_destroy(zh->lock_normal);
641 zebra_lock_destroy(zh->lock_shadow);
652 void map_basenames_func (void *vp, const char *name, const char *value)
654 struct map_baseinfo *p = (struct map_baseinfo *) vp;
656 char fromdb[128], todb[8][128];
659 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
660 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
661 todb[5], todb[6], todb[7]);
665 for (i = 0; i<p->num_bases; i++)
666 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
669 for (i = 0; i < no; i++)
671 if (p->new_num_bases == p->new_num_max)
673 p->new_basenames[(p->new_num_bases)++] =
674 nmem_strdup (p->mem, todb[i]);
680 void map_basenames (ZebraHandle zh, ODR stream,
681 int *num_bases, char ***basenames)
683 struct map_baseinfo info;
684 struct map_baseinfo *p = &info;
687 yaz_log(LOG_API,"map_basenames ");
691 info.num_bases = *num_bases;
692 info.basenames = *basenames;
693 info.new_num_max = 128;
694 info.new_num_bases = 0;
695 info.new_basenames = (char **)
696 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
697 info.mem = stream->mem;
699 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
701 for (i = 0; i<p->num_bases; i++)
702 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
704 p->new_basenames[(p->new_num_bases)++] =
705 nmem_strdup (p->mem, p->basenames[i]);
707 *num_bases = info.new_num_bases;
708 *basenames = info.new_basenames;
709 for (i = 0; i<*num_bases; i++)
710 logf (LOG_LOG, "base %s", (*basenames)[i]);
713 int zebra_select_default_database(ZebraHandle zh)
717 /* no database has been selected - so we select based on
718 resource setting (including group)
720 const char *group = res_get(zh->service->global_res, "group");
721 const char *v = res_get_prefix(zh->service->global_res,
722 "database", group, "Default");
723 return zebra_select_database(zh, v);
728 int zebra_select_database (ZebraHandle zh, const char *basename)
731 yaz_log(LOG_API,"zebra_select_database %s",basename);
733 return zebra_select_databases (zh, 1, &basename);
736 int zebra_select_databases (ZebraHandle zh, int num_bases,
737 const char **basenames)
744 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
745 num_bases,basenames[0]);
753 for (i = 0; i < zh->num_basenames; i++)
754 xfree (zh->basenames[i]);
755 xfree (zh->basenames);
757 zh->num_basenames = num_bases;
758 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
759 for (i = 0; i < zh->num_basenames; i++)
760 zh->basenames[i] = xstrdup (basenames[i]);
762 cp = strrchr(basenames[0], '/');
765 len = cp - basenames[0];
766 new_reg = xmalloc (len + 1);
767 memcpy (new_reg, basenames[0], len);
771 new_reg = xstrdup ("");
772 for (i = 1; i<num_bases; i++)
776 cp1 = strrchr (basenames[i], '/');
784 if (len != cp1 - basenames[i] ||
785 memcmp (basenames[i], new_reg, len))
800 zebra_select_register (zh, new_reg);
807 if (!zh->lock_normal || !zh->lock_shadow)
815 int zebra_search_RPN (ZebraHandle zh, ODR o,
816 Z_RPNQuery *query, const char *setname, int *hits)
819 yaz_log(LOG_API,"zebra_search_rpn");
824 if (zebra_begin_read (zh))
827 zebra_livcode_transform(zh, query);
829 resultSetAddRPN (zh, odr_extract_mem(o), query,
830 zh->num_basenames, zh->basenames, setname);
838 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
839 const char *setname, Z_RecordComposition *comp,
840 oid_value input_format, int num_recs,
841 ZebraRetrievalRecord *recs)
844 int i, *pos_array, ret = 0;
846 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
852 zh->errString = odr_strdup (stream, setname);
858 if (zebra_begin_read (zh))
861 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
862 for (i = 0; i<num_recs; i++)
863 pos_array[i] = recs[i].position;
864 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
867 logf (LOG_DEBUG, "zebraPosSetCreate error");
869 zh->errString = nmem_strdup (stream->mem, setname);
874 for (i = 0; i<num_recs; i++)
879 recs[i].format = VAL_SUTRS;
880 recs[i].len = strlen(poset[i].term);
881 recs[i].buf = poset[i].term;
882 recs[i].base = poset[i].db;
884 else if (poset[i].sysno)
887 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
888 stream, input_format, comp,
889 &recs[i].format, &recs[i].buf,
892 recs[i].errString = NULL;
898 sprintf (num_str, "%d", pos_array[i]);
900 zh->errString = odr_strdup (stream, num_str);
905 zebraPosSetDestroy (zh, poset, num_recs);
912 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
913 oid_value attributeset,
914 int *position, int *num_entries, ZebraScanEntry **entries,
918 yaz_log(LOG_API,"zebra_scan");
920 if (zebra_begin_read (zh))
926 rpn_scan (zh, stream, zapt, attributeset,
927 zh->num_basenames, zh->basenames, position,
928 num_entries, entries, is_partial, 0, 0);
933 int zebra_sort (ZebraHandle zh, ODR stream,
934 int num_input_setnames, const char **input_setnames,
935 const char *output_setname, Z_SortKeySpecList *sort_sequence,
939 yaz_log(LOG_API,"zebra_sort");
941 if (zebra_begin_read (zh))
943 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
944 output_setname, sort_sequence, sort_status);
949 int zebra_deleleResultSet(ZebraHandle zh, int function,
950 int num_setnames, char **setnames,
955 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
957 if (zebra_begin_read(zh))
958 return Z_DeleteStatus_systemProblemAtTarget;
961 case Z_DeleteResultSetRequest_list:
962 resultSetDestroy (zh, num_setnames, setnames, statuses);
964 case Z_DeleteResultSetRequest_all:
965 resultSetDestroy (zh, -1, 0, statuses);
969 status = Z_DeleteStatus_success;
970 for (i = 0; i<num_setnames; i++)
971 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
972 status = statuses[i];
976 int zebra_errCode (ZebraHandle zh)
980 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
983 yaz_log(LOG_API,"zebra_errCode: o");
987 const char *zebra_errString (ZebraHandle zh)
991 e= diagbib1_str (zh->errCode);
992 yaz_log(LOG_API,"zebra_errString: %s",e);
996 char *zebra_errAdd (ZebraHandle zh)
1001 yaz_log(LOG_API,"zebra_errAdd: %s",a);
1005 void zebra_clearError(ZebraHandle zh)
1014 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1024 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1025 p = res_get(zs->global_res, u);
1026 xfree (zh->user_perm);
1027 zh->user_perm = xstrdup(p ? p : "r");
1029 /* users that don't require a password .. */
1030 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1033 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1038 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
1039 const char *record_type)
1042 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
1043 database, record_type);
1045 if (zebra_select_database(zh, database))
1047 if (zebra_begin_trans (zh, 1))
1052 int zebra_admin_import_end (ZebraHandle zh)
1055 yaz_log(LOG_API,"zebra_admin_import_end");
1057 zebra_end_trans (zh);
1061 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1066 yaz_log(LOG_API,"zebra_admin_import_segment");
1068 for (i = 0; i<segment->num_segmentRecords; i++)
1070 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1072 printf ("--------------%d--------------------\n", i);
1073 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1075 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1076 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1078 Odr_oct *oct = fragment->u.notExternallyTagged;
1079 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1084 zebra_update_record(zh,
1085 0, /* record Type */
1097 int zebra_admin_exchange_record (ZebraHandle zh,
1098 const char *rec_buf,
1100 const char *recid_buf, size_t recid_len,
1102 /* 1 = insert. Fail it already exists */
1103 /* 2 = replace. Fail it does not exist */
1104 /* 3 = delete. Fail if does not exist */
1105 /* 4 = update. Insert/replace */
1111 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1114 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1116 memcpy (recid_z, recid_buf, recid_len);
1117 recid_z[recid_len] = 0;
1119 if (zebra_begin_trans(zh, 1))
1122 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1125 if (action == 1) /* fail if insert */
1127 zebra_end_trans(zh);
1131 memcpy (&sysno, rinfo+1, sizeof(sysno));
1135 if (action == 2 || action == 3) /* fail if delete or update */
1137 zebra_end_trans(zh);
1140 action = 1; /* make it an insert (if it's an update).. */
1142 buffer_extract_record (zh, rec_buf, rec_len,
1143 action == 3 ? 1 : 0 /* delete flag */,
1149 0, /* force update */
1150 1 /* allow update */
1154 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1156 else if (action == 3)
1158 dict_delete (zh->reg->matchDict, recid_z);
1160 zebra_end_trans(zh);
1164 int delete_w_handle(const char *info, void *handle)
1166 ZebraHandle zh = (ZebraHandle) handle;
1169 if (*info == sizeof(pos))
1171 memcpy (&pos, info+1, sizeof(pos));
1172 isamb_unlink(zh->reg->isamb, pos);
1177 static int delete_SU_handle(void *handle, int ord)
1179 ZebraHandle zh = (ZebraHandle) handle;
1183 ord_len = key_SU_encode (ord, ord_buf);
1184 ord_buf[ord_len] = '\0';
1186 assert (zh->reg->isamb);
1187 dict_delete_subtree(zh->reg->dict, ord_buf,
1188 zh, delete_w_handle);
1192 int zebra_drop_database (ZebraHandle zh, const char *database)
1196 yaz_log(LOG_API,"zebra_drop_database");
1199 if (zebra_select_database (zh, database))
1201 if (zebra_begin_trans (zh, 1))
1205 zebraExplain_curDatabase (zh->reg->zei, database);
1207 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1208 zebraExplain_removeDatabase(zh->reg->zei, zh);
1212 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1215 zebra_end_trans (zh);
1219 int zebra_create_database (ZebraHandle zh, const char *database)
1222 yaz_log(LOG_API,"zebra_create_database");
1225 if (zebra_select_database (zh, database))
1227 if (zebra_begin_trans (zh, 1))
1230 /* announce database */
1231 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1232 /* explainDatabase */))
1234 zebra_end_trans (zh);
1236 zh->errString = "database already exist";
1239 zebra_end_trans (zh);
1243 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1244 const char *input_str, int input_len,
1245 char *output_str, int output_len)
1249 yaz_log(LOG_API,"zebra_string_norm ");
1251 if (!zh->reg->zebra_maps)
1253 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1254 input_str, input_len);
1257 if (wrbuf_len(wrbuf) >= output_len)
1259 if (wrbuf_len(wrbuf))
1260 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1261 output_str[wrbuf_len(wrbuf)] = '\0';
1262 return wrbuf_len(wrbuf);
1266 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1268 char state_fname[256];
1273 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1276 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1277 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1278 f = fopen (fname, "w");
1280 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1281 fprintf (f, "%c %d %ld\n", val, seqno, p);
1287 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1289 char state_fname[256];
1294 yaz_log(LOG_API,"zebra_get_state ");
1296 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1297 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1298 f = fopen (fname, "r");
1304 fscanf (f, "%c %d", val, seqno);
1311 int zebra_begin_read (ZebraHandle zh)
1313 return zebra_begin_trans(zh, 0);
1316 int zebra_end_read (ZebraHandle zh)
1318 return zebra_end_trans(zh);
1321 static void read_res_for_transaction(ZebraHandle zh)
1323 const char *group = res_get(zh->res, "group");
1326 zh->m_group = group;
1327 v = res_get_prefix(zh->res, "followLinks", group, "1");
1328 zh->m_follow_links = atoi(v);
1330 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1331 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1333 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1334 zh->m_store_keys = atoi(v);
1336 v = res_get_prefix(zh->res, "storeData", group, "1");
1337 zh->m_store_data = atoi(v);
1339 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1340 zh->m_explain_database = atoi(v);
1342 v = res_get_prefix(zh->res, "openRW", group, "1");
1343 zh->m_flag_rw = atoi(v);
1345 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1346 zh->m_file_verbose_limit = atoi(v);
1349 int zebra_begin_trans (ZebraHandle zh, int rw)
1351 zebra_select_default_database(zh);
1355 zh->errString = "zebra_begin_trans: no database selected";
1359 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1363 if (rw && !strchr(zh->user_perm, 'w'))
1377 const char *rval = 0;
1382 read_res_for_transaction(zh);
1385 if (zh->trans_no != 1)
1388 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1393 resultSetInvalidate (zh);
1394 zebra_register_close (zh->service, zh->reg);
1396 zh->trans_w_no = zh->trans_no;
1400 zh->records_inserted = 0;
1401 zh->records_updated = 0;
1402 zh->records_deleted = 0;
1403 zh->records_processed = 0;
1405 #if HAVE_SYS_TIMES_H
1409 if (zh->shadow_enable)
1410 rval = res_get (zh->res, "shadow");
1412 for (pass = 0; pass < 2; pass++)
1416 zebra_lock_r (zh->lock_normal);
1417 zebra_lock_w (zh->lock_shadow);
1421 zebra_lock_w (zh->lock_normal);
1422 zebra_lock_w (zh->lock_shadow);
1425 zebra_get_state (zh, &val, &seqno);
1428 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1429 zebra_unlock (zh->lock_shadow);
1430 zebra_unlock (zh->lock_normal);
1434 else if (val == 'd')
1438 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1440 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1441 bf_commitClean (bfs, rval);
1446 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1453 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1457 zebra_set_state (zh, 'd', seqno);
1459 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1460 1, rval ? 1 : 0, zh->res,
1463 zh->reg->seqno = seqno;
1466 zebra_set_state (zh, 'o', seqno);
1468 zebra_unlock (zh->lock_shadow);
1469 zebra_unlock (zh->lock_normal);
1475 zh->errString = "zebra_begin_trans: cannot open register";
1476 yaz_log(LOG_FATAL, zh->errString);
1488 if (zh->trans_no != 1)
1490 zebra_flush_reg (zh);
1494 #if HAVE_SYS_TIMES_H
1503 if (!zh->lock_normal || !zh->lock_shadow)
1509 zebra_get_state (zh, &val, &seqno);
1515 else if (seqno != zh->reg->seqno)
1517 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1518 seqno, zh->reg->seqno);
1521 else if (zh->reg->last_val != val)
1523 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1524 val, zh->reg->last_val);
1531 zebra_lock_r (zh->lock_shadow);
1533 zebra_lock_r (zh->lock_normal);
1537 resultSetInvalidate (zh);
1538 zebra_register_close (zh->service, zh->reg);
1540 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1541 0, val == 'c' ? 1 : 0,
1542 zh->res, zh->path_reg);
1545 zebra_unlock (zh->lock_normal);
1546 zebra_unlock (zh->lock_shadow);
1551 zh->reg->last_val = val;
1552 zh->reg->seqno = seqno;
1554 read_res_for_transaction(zh);
1558 int zebra_end_trans (ZebraHandle zh)
1560 ZebraTransactionStatus dummy;
1561 yaz_log(LOG_API,"zebra_end_trans");
1562 return zebra_end_transaction(zh, &dummy);
1565 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1572 yaz_log(LOG_API,"zebra_end_transaction");
1574 status->processed = 0;
1575 status->inserted = 0;
1576 status->updated = 0;
1577 status->deleted = 0;
1581 if (!zh->res || !zh->reg)
1584 zh->errString = "zebra_end_trans: no open transaction";
1587 if (zh->trans_no != zh->trans_w_no)
1590 if (zh->trans_no != 0)
1593 /* release read lock */
1595 zebra_unlock (zh->lock_normal);
1596 zebra_unlock (zh->lock_shadow);
1599 { /* release write lock */
1603 yaz_log (LOG_LOG, "zebra_end_trans");
1604 rval = res_get (zh->res, "shadow");
1606 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1608 zebra_flush_reg (zh);
1610 resultSetInvalidate (zh);
1612 zebra_register_close (zh->service, zh->reg);
1615 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1616 zh->records_processed, zh->records_inserted,
1617 zh->records_updated, zh->records_deleted);
1619 status->processed = zh->records_processed;
1620 status->inserted = zh->records_inserted;
1621 status->updated = zh->records_updated;
1622 status->deleted = zh->records_deleted;
1624 zebra_get_state (zh, &val, &seqno);
1627 BFiles bfs = bfs_create (rval, zh->path_reg);
1628 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1629 bf_commitClean (bfs, rval);
1634 zebra_set_state (zh, 'o', seqno);
1636 zebra_unlock (zh->lock_shadow);
1637 zebra_unlock (zh->lock_normal);
1640 #if HAVE_SYS_TIMES_H
1642 logf (LOG_LOG, "user/system: %ld/%ld",
1643 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1644 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1646 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1647 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1652 int zebra_repository_update (ZebraHandle zh, const char *path)
1656 logf (LOG_LOG|LOG_API, "updating %s", path);
1657 repositoryUpdate (zh, path);
1661 int zebra_repository_delete (ZebraHandle zh, const char *path)
1665 logf (LOG_LOG|LOG_API, "deleting %s", path);
1666 repositoryDelete (zh, path);
1670 int zebra_repository_show (ZebraHandle zh, const char *path)
1673 yaz_log(LOG_API,"zebra_repository_show");
1675 repositoryShow (zh, path);
1679 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1688 zebra_select_default_database(zh);
1694 rval = res_get (zh->res, "shadow");
1697 logf (LOG_WARN, "Cannot perform commit");
1698 logf (LOG_WARN, "No shadow area defined");
1702 zebra_lock_w (zh->lock_normal);
1703 zebra_lock_r (zh->lock_shadow);
1705 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1707 zebra_get_state (zh, &val, &seqno);
1710 bf_cache (bfs, rval);
1711 if (bf_commitExists (bfs))
1714 zebra_set_state (zh, 'd', seqno);
1717 zebra_set_state (zh, 'c', seqno);
1719 logf (LOG_LOG, "commit start");
1720 bf_commitExec (bfs);
1725 logf (LOG_LOG, "commit clean");
1726 bf_commitClean (bfs, rval);
1728 zebra_set_state (zh, 'o', seqno);
1732 logf (LOG_LOG, "nothing to commit");
1736 zebra_unlock (zh->lock_shadow);
1737 zebra_unlock (zh->lock_normal);
1741 int zebra_clean (ZebraHandle zh)
1743 yaz_log(LOG_API,"zebra_clean");
1744 return zebra_commit_ex(zh, 1);
1747 int zebra_commit (ZebraHandle zh)
1749 yaz_log(LOG_API,"zebra_commit");
1750 return zebra_commit_ex(zh, 0);
1753 int zebra_init (ZebraHandle zh)
1758 yaz_log(LOG_API,"zebra_init");
1761 zebra_select_default_database(zh);
1767 rval = res_get (zh->res, "shadow");
1769 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1773 bf_cache (bfs, rval);
1777 zebra_set_state (zh, 'o', 0);
1781 int zebra_compact (ZebraHandle zh)
1785 yaz_log(LOG_API,"zebra_compact");
1792 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1798 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1801 yaz_log(LOG_API,"zebra_result");
1802 *code = zh->errCode;
1803 *addinfo = zh->errString;
1807 int zebra_shadow_enable (ZebraHandle zh, int value)
1810 yaz_log(LOG_API,"zebra_shadow_enable");
1812 zh->shadow_enable = value;
1816 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1819 yaz_log(LOG_API,"zebra_record_encoding");
1821 xfree (zh->record_encoding);
1825 * Something about charset aliases. Oleg???
1828 if (zh->iconv_to_utf8 != 0)
1829 yaz_iconv_close(zh->iconv_to_utf8);
1830 if (zh->iconv_from_utf8 != 0)
1831 yaz_iconv_close(zh->iconv_from_utf8);
1833 zh->record_encoding = xstrdup (encoding);
1835 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1838 yaz_iconv_open ("UTF-8", encoding);
1839 if (zh->iconv_to_utf8 == 0)
1840 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1841 zh->iconv_from_utf8 =
1842 yaz_iconv_open (encoding, "UTF-8");
1843 if (zh->iconv_to_utf8 == 0)
1844 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1849 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1852 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1854 res_set(zh->res, name, value);
1858 const char *zebra_get_resource(ZebraHandle zh,
1859 const char *name, const char *defaultvalue)
1863 v= res_get_def( zh->res, name, (char *)defaultvalue);
1865 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1869 /* moved from zebra_api_ext.c by pop */
1870 /* FIXME: Should this really be public??? -Heikki */
1872 int zebra_trans_no (ZebraHandle zh)
1875 yaz_log(LOG_API,"zebra_trans_no");
1876 return zh->trans_no;
1879 int zebra_get_shadow_enable (ZebraHandle zh)
1881 yaz_log(LOG_API,"zebra_get_shadow_enable");
1882 return (zh->shadow_enable);
1885 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1887 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1888 zh->shadow_enable = value;
1892 /* almost the same as zebra_records_retrieve ... but how did it work?
1893 I mean for multiple records ??? CHECK ??? */
1894 void api_records_retrieve (ZebraHandle zh, ODR stream,
1895 const char *setname, Z_RecordComposition *comp,
1896 oid_value input_format, int num_recs,
1897 ZebraRetrievalRecord *recs)
1901 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1906 zh->errString = odr_strdup (stream, setname);
1912 if (zebra_begin_read (zh))
1915 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1916 for (i = 0; i<num_recs; i++)
1917 pos_array[i] = recs[i].position;
1918 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1921 logf (LOG_DEBUG, "zebraPosSetCreate error");
1923 zh->errString = nmem_strdup (stream->mem, setname);
1927 for (i = 0; i<num_recs; i++)
1931 recs[i].errCode = 0;
1932 recs[i].format = VAL_SUTRS;
1933 recs[i].len = strlen(poset[i].term);
1934 recs[i].buf = poset[i].term;
1935 recs[i].base = poset[i].db;
1939 else if (poset[i].sysno)
1941 /* changed here ??? CHECK ??? */
1944 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1945 stream, input_format, comp,
1950 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1951 memcpy(recs[i].buf, b, recs[i].len);
1952 recs[i].errString = 0; /* Hmmm !!! we should get this */
1953 recs[i].sysno = poset[i].sysno;
1954 recs[i].score = poset[i].score;
1960 sprintf (num_str, "%d", pos_array[i]);
1962 zh->errString = odr_strdup (stream, num_str);
1967 zebraPosSetDestroy (zh, poset, num_recs);
1969 zebra_end_read (zh);
1974 /* ---------------------------------------------------------------------------
1975 Record insert(=update), delete
1977 If sysno is provided, then it's used to identify the record.
1978 If not, and match_criteria is provided, then sysno is guessed
1979 If not, and a record is provided, then sysno is got from there
1980 NOTE: Now returns 0 at success and updates sysno, which is an int*
1984 int zebra_add_record(ZebraHandle zh,
1985 const char *buf, int buf_size)
1988 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1991 int zebra_insert_record (ZebraHandle zh,
1992 const char *recordType,
1993 int *sysno, const char *match, const char *fname,
1994 const char *buf, int buf_size, int force_update)
1997 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
1999 if (buf_size < 1) buf_size = strlen(buf);
2001 if (zebra_begin_trans(zh, 1))
2003 res = buffer_extract_record (zh, buf, buf_size,
2004 0, /* delete_flag */
2010 0); /* allow_update */
2011 zebra_end_trans(zh);
2015 int zebra_update_record (ZebraHandle zh,
2016 const char *recordType,
2017 int* sysno, const char *match, const char *fname,
2018 const char *buf, int buf_size,
2023 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
2025 if (buf_size < 1) buf_size = strlen(buf);
2027 if (zebra_begin_trans(zh, 1))
2029 res = buffer_extract_record (zh, buf, buf_size,
2030 0, /* delete_flag */
2036 1); /* allow_update */
2037 yaz_log(LOG_LOG, "zebra_update_record returned res=%d", res);
2038 zebra_end_trans(zh);
2042 int zebra_delete_record (ZebraHandle zh,
2043 const char *recordType,
2044 int *sysno, const char *match, const char *fname,
2045 const char *buf, int buf_size,
2049 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2051 if (buf_size < 1) buf_size = strlen(buf);
2053 if (zebra_begin_trans(zh, 1))
2055 res = buffer_extract_record (zh, buf, buf_size,
2056 1, /* delete_flag */
2062 1); /* allow_update */
2063 zebra_end_trans(zh);
2067 /* ---------------------------------------------------------------------------
2071 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2072 const char *setname, int *numhits)
2077 ODR odr = odr_createmem(ODR_ENCODE);
2079 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2081 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2084 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2086 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2090 yaz_log(LOG_API,"Hits: %d",hits);
2098 /* ---------------------------------------------------------------------------
2099 Sort - a simplified interface, with optional read locks.
2100 FIXME - This is a horrible name, will conflict with half the applications
2102 int zebra_sort_by_specstr (ZebraHandle zh,
2104 const char *sort_spec,
2105 const char *output_setname,
2106 const char **input_setnames)
2108 int num_input_setnames = 0;
2109 int sort_status = 0;
2110 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2111 yaz_log(LOG_API,"sort (FIXME) ");
2114 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2119 /* we can do this, since the perl typemap code for char** will
2120 put a NULL at the end of list */
2121 while (input_setnames[num_input_setnames]) num_input_setnames++;
2123 if (zebra_begin_read (zh))
2126 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2127 output_setname, sort_sequence, &sort_status);