1 /* $Id: zebraapi.c,v 1.120.2.4 2005-01-23 14:59:42 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);
636 void map_basenames_func (void *vp, const char *name, const char *value)
638 struct map_baseinfo *p = (struct map_baseinfo *) vp;
640 char fromdb[128], todb[8][128];
643 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
644 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
645 todb[5], todb[6], todb[7]);
649 for (i = 0; i<p->num_bases; i++)
650 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
653 for (i = 0; i < no; i++)
655 if (p->new_num_bases == p->new_num_max)
657 p->new_basenames[(p->new_num_bases)++] =
658 nmem_strdup (p->mem, todb[i]);
664 void map_basenames (ZebraHandle zh, ODR stream,
665 int *num_bases, char ***basenames)
667 struct map_baseinfo info;
668 struct map_baseinfo *p = &info;
671 yaz_log(LOG_API,"map_basenames ");
675 info.num_bases = *num_bases;
676 info.basenames = *basenames;
677 info.new_num_max = 128;
678 info.new_num_bases = 0;
679 info.new_basenames = (char **)
680 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
681 info.mem = stream->mem;
683 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
685 for (i = 0; i<p->num_bases; i++)
686 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
688 p->new_basenames[(p->new_num_bases)++] =
689 nmem_strdup (p->mem, p->basenames[i]);
691 *num_bases = info.new_num_bases;
692 *basenames = info.new_basenames;
693 for (i = 0; i<*num_bases; i++)
694 logf (LOG_LOG, "base %s", (*basenames)[i]);
697 int zebra_select_default_database(ZebraHandle zh)
701 /* no database has been selected - so we select based on
702 resource setting (including group)
704 const char *group = res_get(zh->service->global_res, "group");
705 const char *v = res_get_prefix(zh->service->global_res,
706 "database", group, "Default");
707 zebra_select_database(zh, v);
712 int zebra_select_database (ZebraHandle zh, const char *basename)
715 yaz_log(LOG_API,"zebra_select_database %s",basename);
717 return zebra_select_databases (zh, 1, &basename);
720 int zebra_select_databases (ZebraHandle zh, int num_bases,
721 const char **basenames)
728 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
729 num_bases,basenames[0]);
737 for (i = 0; i < zh->num_basenames; i++)
738 xfree (zh->basenames[i]);
739 xfree (zh->basenames);
741 zh->num_basenames = num_bases;
742 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
743 for (i = 0; i < zh->num_basenames; i++)
744 zh->basenames[i] = xstrdup (basenames[i]);
746 cp = strrchr(basenames[0], '/');
749 len = cp - basenames[0];
750 new_reg = xmalloc (len + 1);
751 memcpy (new_reg, basenames[0], len);
755 new_reg = xstrdup ("");
756 for (i = 1; i<num_bases; i++)
760 cp1 = strrchr (basenames[i], '/');
768 if (len != cp1 - basenames[i] ||
769 memcmp (basenames[i], new_reg, len))
784 zebra_select_register (zh, new_reg);
791 if (!zh->lock_normal || !zh->lock_shadow)
799 int zebra_search_RPN (ZebraHandle zh, ODR o,
800 Z_RPNQuery *query, const char *setname, int *hits)
803 yaz_log(LOG_API,"zebra_search_rpn");
808 if (zebra_begin_read (zh))
811 zebra_livcode_transform(zh, query);
813 resultSetAddRPN (zh, odr_extract_mem(o), query,
814 zh->num_basenames, zh->basenames, setname);
822 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
823 const char *setname, Z_RecordComposition *comp,
824 oid_value input_format, int num_recs,
825 ZebraRetrievalRecord *recs)
828 int i, *pos_array, ret = 0;
830 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
836 zh->errString = odr_strdup (stream, setname);
842 if (zebra_begin_read (zh))
845 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
846 for (i = 0; i<num_recs; i++)
847 pos_array[i] = recs[i].position;
848 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
851 logf (LOG_DEBUG, "zebraPosSetCreate error");
853 zh->errString = nmem_strdup (stream->mem, setname);
858 for (i = 0; i<num_recs; i++)
863 recs[i].format = VAL_SUTRS;
864 recs[i].len = strlen(poset[i].term);
865 recs[i].buf = poset[i].term;
866 recs[i].base = poset[i].db;
868 else if (poset[i].sysno)
871 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
872 stream, input_format, comp,
873 &recs[i].format, &recs[i].buf,
876 recs[i].errString = NULL;
882 sprintf (num_str, "%d", pos_array[i]);
884 zh->errString = odr_strdup (stream, num_str);
889 zebraPosSetDestroy (zh, poset, num_recs);
896 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
897 oid_value attributeset,
898 int *position, int *num_entries, ZebraScanEntry **entries,
902 yaz_log(LOG_API,"zebra_scan");
904 if (zebra_begin_read (zh))
910 rpn_scan (zh, stream, zapt, attributeset,
911 zh->num_basenames, zh->basenames, position,
912 num_entries, entries, is_partial, 0, 0);
917 int zebra_sort (ZebraHandle zh, ODR stream,
918 int num_input_setnames, const char **input_setnames,
919 const char *output_setname, Z_SortKeySpecList *sort_sequence,
923 yaz_log(LOG_API,"zebra_sort");
925 if (zebra_begin_read (zh))
927 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
928 output_setname, sort_sequence, sort_status);
933 int zebra_deleleResultSet(ZebraHandle zh, int function,
934 int num_setnames, char **setnames,
939 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
941 if (zebra_begin_read(zh))
942 return Z_DeleteStatus_systemProblemAtTarget;
945 case Z_DeleteResultSetRequest_list:
946 resultSetDestroy (zh, num_setnames, setnames, statuses);
948 case Z_DeleteResultSetRequest_all:
949 resultSetDestroy (zh, -1, 0, statuses);
953 status = Z_DeleteStatus_success;
954 for (i = 0; i<num_setnames; i++)
955 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
956 status = statuses[i];
960 int zebra_errCode (ZebraHandle zh)
964 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
967 yaz_log(LOG_API,"zebra_errCode: o");
971 const char *zebra_errString (ZebraHandle zh)
975 e= diagbib1_str (zh->errCode);
976 yaz_log(LOG_API,"zebra_errString: %s",e);
980 char *zebra_errAdd (ZebraHandle zh)
985 yaz_log(LOG_API,"zebra_errAdd: %s",a);
989 void zebra_clearError(ZebraHandle zh)
998 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1008 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1009 p = res_get(zs->global_res, u);
1010 xfree (zh->user_perm);
1011 zh->user_perm = xstrdup(p ? p : "r");
1013 /* users that don't require a password .. */
1014 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1017 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1022 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
1023 const char *record_type)
1026 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
1027 database, record_type);
1029 if (zebra_select_database(zh, database))
1031 if (zebra_begin_trans (zh, 1))
1036 int zebra_admin_import_end (ZebraHandle zh)
1039 yaz_log(LOG_API,"zebra_admin_import_end");
1041 zebra_end_trans (zh);
1045 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1050 yaz_log(LOG_API,"zebra_admin_import_segment");
1052 for (i = 0; i<segment->num_segmentRecords; i++)
1054 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1056 printf ("--------------%d--------------------\n", i);
1057 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1059 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1060 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1062 Odr_oct *oct = fragment->u.notExternallyTagged;
1063 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1068 zebra_update_record(zh,
1069 0, /* record Type */
1081 int zebra_admin_exchange_record (ZebraHandle zh,
1082 const char *rec_buf,
1084 const char *recid_buf, size_t recid_len,
1086 /* 1 = insert. Fail it already exists */
1087 /* 2 = replace. Fail it does not exist */
1088 /* 3 = delete. Fail if does not exist */
1089 /* 4 = update. Insert/replace */
1095 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1098 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1100 memcpy (recid_z, recid_buf, recid_len);
1101 recid_z[recid_len] = 0;
1103 if (zebra_begin_trans(zh, 1))
1106 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1109 if (action == 1) /* fail if insert */
1111 zebra_end_trans(zh);
1115 memcpy (&sysno, rinfo+1, sizeof(sysno));
1119 if (action == 2 || action == 3) /* fail if delete or update */
1121 zebra_end_trans(zh);
1124 action = 1; /* make it an insert (if it's an update).. */
1126 buffer_extract_record (zh, rec_buf, rec_len,
1127 action == 3 ? 1 : 0 /* delete flag */,
1133 0, /* force update */
1134 1 /* allow update */
1138 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1140 else if (action == 3)
1142 dict_delete (zh->reg->matchDict, recid_z);
1144 zebra_end_trans(zh);
1148 int delete_w_handle(const char *info, void *handle)
1150 ZebraHandle zh = (ZebraHandle) handle;
1153 if (*info == sizeof(pos))
1155 memcpy (&pos, info+1, sizeof(pos));
1156 isamb_unlink(zh->reg->isamb, pos);
1161 static int delete_SU_handle(void *handle, int ord)
1163 ZebraHandle zh = (ZebraHandle) handle;
1167 ord_len = key_SU_encode (ord, ord_buf);
1168 ord_buf[ord_len] = '\0';
1170 assert (zh->reg->isamb);
1171 dict_delete_subtree(zh->reg->dict, ord_buf,
1172 zh, delete_w_handle);
1176 int zebra_drop_database (ZebraHandle zh, const char *database)
1180 yaz_log(LOG_API,"zebra_drop_database");
1183 if (zebra_select_database (zh, database))
1185 if (zebra_begin_trans (zh, 1))
1189 zebraExplain_curDatabase (zh->reg->zei, database);
1191 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1192 zebraExplain_removeDatabase(zh->reg->zei, zh);
1196 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1199 zebra_end_trans (zh);
1203 int zebra_create_database (ZebraHandle zh, const char *database)
1206 yaz_log(LOG_API,"zebra_create_database");
1209 if (zebra_select_database (zh, database))
1211 if (zebra_begin_trans (zh, 1))
1214 /* announce database */
1215 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1216 /* explainDatabase */))
1218 zebra_end_trans (zh);
1220 zh->errString = "database already exist";
1223 zebra_end_trans (zh);
1227 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1228 const char *input_str, int input_len,
1229 char *output_str, int output_len)
1233 yaz_log(LOG_API,"zebra_string_norm ");
1235 if (!zh->reg->zebra_maps)
1237 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1238 input_str, input_len);
1241 if (wrbuf_len(wrbuf) >= output_len)
1243 if (wrbuf_len(wrbuf))
1244 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1245 output_str[wrbuf_len(wrbuf)] = '\0';
1246 return wrbuf_len(wrbuf);
1250 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1252 char state_fname[256];
1257 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1260 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1261 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1262 f = fopen (fname, "w");
1264 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1265 fprintf (f, "%c %d %ld\n", val, seqno, p);
1271 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1273 char state_fname[256];
1278 yaz_log(LOG_API,"zebra_get_state ");
1280 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1281 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1282 f = fopen (fname, "r");
1288 fscanf (f, "%c %d", val, seqno);
1295 int zebra_begin_read (ZebraHandle zh)
1297 return zebra_begin_trans(zh, 0);
1300 int zebra_end_read (ZebraHandle zh)
1302 return zebra_end_trans(zh);
1305 static void read_res_for_transaction(ZebraHandle zh)
1307 const char *group = res_get(zh->res, "group");
1310 zh->m_group = group;
1311 v = res_get_prefix(zh->res, "followLinks", group, "1");
1312 zh->m_follow_links = atoi(v);
1314 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1315 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1317 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1318 zh->m_store_keys = atoi(v);
1320 v = res_get_prefix(zh->res, "storeData", group, "1");
1321 zh->m_store_data = atoi(v);
1323 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1324 zh->m_explain_database = atoi(v);
1326 v = res_get_prefix(zh->res, "openRW", group, "1");
1327 zh->m_flag_rw = atoi(v);
1329 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1330 zh->m_file_verbose_limit = atoi(v);
1333 int zebra_begin_trans (ZebraHandle zh, int rw)
1335 zebra_select_default_database(zh);
1339 zh->errString = "zebra_begin_trans: no database selected";
1343 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1347 if (rw && !strchr(zh->user_perm, 'w'))
1361 const char *rval = 0;
1366 read_res_for_transaction(zh);
1369 if (zh->trans_no != 1)
1372 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1377 resultSetInvalidate (zh);
1378 zebra_register_close (zh->service, zh->reg);
1380 zh->trans_w_no = zh->trans_no;
1384 zh->records_inserted = 0;
1385 zh->records_updated = 0;
1386 zh->records_deleted = 0;
1387 zh->records_processed = 0;
1389 #if HAVE_SYS_TIMES_H
1393 if (zh->shadow_enable)
1394 rval = res_get (zh->res, "shadow");
1396 for (pass = 0; pass < 2; pass++)
1400 zebra_lock_r (zh->lock_normal);
1401 zebra_lock_w (zh->lock_shadow);
1405 zebra_lock_w (zh->lock_normal);
1406 zebra_lock_w (zh->lock_shadow);
1409 zebra_get_state (zh, &val, &seqno);
1412 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1413 zebra_unlock (zh->lock_shadow);
1414 zebra_unlock (zh->lock_normal);
1418 else if (val == 'd')
1422 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1424 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1425 bf_commitClean (bfs, rval);
1430 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1437 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1441 zebra_set_state (zh, 'd', seqno);
1443 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1444 1, rval ? 1 : 0, zh->res,
1447 zh->reg->seqno = seqno;
1450 zebra_set_state (zh, 'o', seqno);
1452 zebra_unlock (zh->lock_shadow);
1453 zebra_unlock (zh->lock_normal);
1459 zh->errString = "zebra_begin_trans: cannot open register";
1460 yaz_log(LOG_FATAL, zh->errString);
1472 if (zh->trans_no != 1)
1474 zebra_flush_reg (zh);
1478 #if HAVE_SYS_TIMES_H
1487 if (!zh->lock_normal || !zh->lock_shadow)
1493 zebra_get_state (zh, &val, &seqno);
1499 else if (seqno != zh->reg->seqno)
1501 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1502 seqno, zh->reg->seqno);
1505 else if (zh->reg->last_val != val)
1507 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1508 val, zh->reg->last_val);
1515 zebra_lock_r (zh->lock_shadow);
1517 zebra_lock_r (zh->lock_normal);
1521 resultSetInvalidate (zh);
1522 zebra_register_close (zh->service, zh->reg);
1524 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1525 0, val == 'c' ? 1 : 0,
1526 zh->res, zh->path_reg);
1529 zebra_unlock (zh->lock_normal);
1530 zebra_unlock (zh->lock_shadow);
1535 zh->reg->last_val = val;
1536 zh->reg->seqno = seqno;
1538 read_res_for_transaction(zh);
1542 int zebra_end_trans (ZebraHandle zh)
1544 ZebraTransactionStatus dummy;
1545 yaz_log(LOG_API,"zebra_end_trans");
1546 return zebra_end_transaction(zh, &dummy);
1549 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1556 yaz_log(LOG_API,"zebra_end_transaction");
1558 status->processed = 0;
1559 status->inserted = 0;
1560 status->updated = 0;
1561 status->deleted = 0;
1565 if (!zh->res || !zh->reg)
1568 zh->errString = "zebra_end_trans: no open transaction";
1571 if (zh->trans_no != zh->trans_w_no)
1574 if (zh->trans_no != 0)
1577 /* release read lock */
1579 zebra_unlock (zh->lock_normal);
1580 zebra_unlock (zh->lock_shadow);
1583 { /* release write lock */
1587 yaz_log (LOG_LOG, "zebra_end_trans");
1588 rval = res_get (zh->res, "shadow");
1590 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1592 zebra_flush_reg (zh);
1594 resultSetInvalidate (zh);
1596 zebra_register_close (zh->service, zh->reg);
1599 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1600 zh->records_processed, zh->records_inserted,
1601 zh->records_updated, zh->records_deleted);
1603 status->processed = zh->records_processed;
1604 status->inserted = zh->records_inserted;
1605 status->updated = zh->records_updated;
1606 status->deleted = zh->records_deleted;
1608 zebra_get_state (zh, &val, &seqno);
1611 BFiles bfs = bfs_create (rval, zh->path_reg);
1612 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1613 bf_commitClean (bfs, rval);
1618 zebra_set_state (zh, 'o', seqno);
1620 zebra_unlock (zh->lock_shadow);
1621 zebra_unlock (zh->lock_normal);
1624 #if HAVE_SYS_TIMES_H
1626 logf (LOG_LOG, "user/system: %ld/%ld",
1627 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1628 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1630 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1631 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1636 int zebra_repository_update (ZebraHandle zh, const char *path)
1640 logf (LOG_LOG|LOG_API, "updating %s", path);
1641 repositoryUpdate (zh, path);
1645 int zebra_repository_delete (ZebraHandle zh, const char *path)
1649 logf (LOG_LOG|LOG_API, "deleting %s", path);
1650 repositoryDelete (zh, path);
1654 int zebra_repository_show (ZebraHandle zh, const char *path)
1657 yaz_log(LOG_API,"zebra_repository_show");
1659 repositoryShow (zh, path);
1663 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1672 zebra_select_default_database(zh);
1678 rval = res_get (zh->res, "shadow");
1681 logf (LOG_WARN, "Cannot perform commit");
1682 logf (LOG_WARN, "No shadow area defined");
1686 zebra_lock_w (zh->lock_normal);
1687 zebra_lock_r (zh->lock_shadow);
1689 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1691 zebra_get_state (zh, &val, &seqno);
1694 bf_cache (bfs, rval);
1695 if (bf_commitExists (bfs))
1698 zebra_set_state (zh, 'd', seqno);
1701 zebra_set_state (zh, 'c', seqno);
1703 logf (LOG_LOG, "commit start");
1704 bf_commitExec (bfs);
1709 logf (LOG_LOG, "commit clean");
1710 bf_commitClean (bfs, rval);
1712 zebra_set_state (zh, 'o', seqno);
1716 logf (LOG_LOG, "nothing to commit");
1720 zebra_unlock (zh->lock_shadow);
1721 zebra_unlock (zh->lock_normal);
1725 int zebra_clean (ZebraHandle zh)
1727 yaz_log(LOG_API,"zebra_clean");
1728 return zebra_commit_ex(zh, 1);
1731 int zebra_commit (ZebraHandle zh)
1733 yaz_log(LOG_API,"zebra_commit");
1734 return zebra_commit_ex(zh, 0);
1737 int zebra_init (ZebraHandle zh)
1742 yaz_log(LOG_API,"zebra_init");
1745 zebra_select_default_database(zh);
1751 rval = res_get (zh->res, "shadow");
1753 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1757 bf_cache (bfs, rval);
1761 zebra_set_state (zh, 'o', 0);
1765 int zebra_compact (ZebraHandle zh)
1769 yaz_log(LOG_API,"zebra_compact");
1776 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1782 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1785 yaz_log(LOG_API,"zebra_result");
1786 *code = zh->errCode;
1787 *addinfo = zh->errString;
1791 int zebra_shadow_enable (ZebraHandle zh, int value)
1794 yaz_log(LOG_API,"zebra_shadow_enable");
1796 zh->shadow_enable = value;
1800 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1803 yaz_log(LOG_API,"zebra_record_encoding");
1805 xfree (zh->record_encoding);
1809 * Something about charset aliases. Oleg???
1812 if (zh->iconv_to_utf8 != 0)
1813 yaz_iconv_close(zh->iconv_to_utf8);
1814 if (zh->iconv_from_utf8 != 0)
1815 yaz_iconv_close(zh->iconv_from_utf8);
1817 zh->record_encoding = xstrdup (encoding);
1819 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1822 yaz_iconv_open ("UTF-8", encoding);
1823 if (zh->iconv_to_utf8 == 0)
1824 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1825 zh->iconv_from_utf8 =
1826 yaz_iconv_open (encoding, "UTF-8");
1827 if (zh->iconv_to_utf8 == 0)
1828 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1833 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1836 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1838 res_set(zh->res, name, value);
1842 const char *zebra_get_resource(ZebraHandle zh,
1843 const char *name, const char *defaultvalue)
1847 v= res_get_def( zh->res, name, (char *)defaultvalue);
1849 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1853 /* moved from zebra_api_ext.c by pop */
1854 /* FIXME: Should this really be public??? -Heikki */
1856 int zebra_trans_no (ZebraHandle zh)
1859 yaz_log(LOG_API,"zebra_trans_no");
1860 return zh->trans_no;
1863 int zebra_get_shadow_enable (ZebraHandle zh)
1865 yaz_log(LOG_API,"zebra_get_shadow_enable");
1866 return (zh->shadow_enable);
1869 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1871 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1872 zh->shadow_enable = value;
1876 /* almost the same as zebra_records_retrieve ... but how did it work?
1877 I mean for multiple records ??? CHECK ??? */
1878 void api_records_retrieve (ZebraHandle zh, ODR stream,
1879 const char *setname, Z_RecordComposition *comp,
1880 oid_value input_format, int num_recs,
1881 ZebraRetrievalRecord *recs)
1885 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1890 zh->errString = odr_strdup (stream, setname);
1896 if (zebra_begin_read (zh))
1899 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1900 for (i = 0; i<num_recs; i++)
1901 pos_array[i] = recs[i].position;
1902 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1905 logf (LOG_DEBUG, "zebraPosSetCreate error");
1907 zh->errString = nmem_strdup (stream->mem, setname);
1911 for (i = 0; i<num_recs; i++)
1915 recs[i].errCode = 0;
1916 recs[i].format = VAL_SUTRS;
1917 recs[i].len = strlen(poset[i].term);
1918 recs[i].buf = poset[i].term;
1919 recs[i].base = poset[i].db;
1923 else if (poset[i].sysno)
1925 /* changed here ??? CHECK ??? */
1928 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1929 stream, input_format, comp,
1934 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1935 memcpy(recs[i].buf, b, recs[i].len);
1936 recs[i].errString = 0; /* Hmmm !!! we should get this */
1937 recs[i].sysno = poset[i].sysno;
1938 recs[i].score = poset[i].score;
1944 sprintf (num_str, "%d", pos_array[i]);
1946 zh->errString = odr_strdup (stream, num_str);
1951 zebraPosSetDestroy (zh, poset, num_recs);
1953 zebra_end_read (zh);
1958 /* ---------------------------------------------------------------------------
1959 Record insert(=update), delete
1961 If sysno is provided, then it's used to identify the record.
1962 If not, and match_criteria is provided, then sysno is guessed
1963 If not, and a record is provided, then sysno is got from there
1964 NOTE: Now returns 0 at success and updates sysno, which is an int*
1968 int zebra_add_record(ZebraHandle zh,
1969 const char *buf, int buf_size)
1972 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1975 int zebra_insert_record (ZebraHandle zh,
1976 const char *recordType,
1977 int *sysno, const char *match, const char *fname,
1978 const char *buf, int buf_size, int force_update)
1981 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
1983 if (buf_size < 1) buf_size = strlen(buf);
1985 if (zebra_begin_trans(zh, 1))
1987 res = buffer_extract_record (zh, buf, buf_size,
1988 0, /* delete_flag */
1994 0); /* allow_update */
1995 zebra_end_trans(zh);
1999 int zebra_update_record (ZebraHandle zh,
2000 const char *recordType,
2001 int* sysno, const char *match, const char *fname,
2002 const char *buf, int buf_size,
2007 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
2009 if (buf_size < 1) buf_size = strlen(buf);
2011 if (zebra_begin_trans(zh, 1))
2013 res = buffer_extract_record (zh, buf, buf_size,
2014 0, /* delete_flag */
2020 1); /* allow_update */
2021 yaz_log(LOG_LOG, "zebra_update_record returned res=%d", res);
2022 zebra_end_trans(zh);
2026 int zebra_delete_record (ZebraHandle zh,
2027 const char *recordType,
2028 int *sysno, const char *match, const char *fname,
2029 const char *buf, int buf_size,
2033 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2035 if (buf_size < 1) buf_size = strlen(buf);
2037 if (zebra_begin_trans(zh, 1))
2039 res = buffer_extract_record (zh, buf, buf_size,
2040 1, /* delete_flag */
2046 1); /* allow_update */
2047 zebra_end_trans(zh);
2051 /* ---------------------------------------------------------------------------
2055 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2056 const char *setname, int *numhits)
2061 ODR odr = odr_createmem(ODR_ENCODE);
2063 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2065 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2068 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2070 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2074 yaz_log(LOG_API,"Hits: %d",hits);
2082 /* ---------------------------------------------------------------------------
2083 Sort - a simplified interface, with optional read locks.
2084 FIXME - This is a horrible name, will conflict with half the applications
2086 int zebra_sort_by_specstr (ZebraHandle zh,
2088 const char *sort_spec,
2089 const char *output_setname,
2090 const char **input_setnames)
2092 int num_input_setnames = 0;
2093 int sort_status = 0;
2094 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2095 yaz_log(LOG_API,"sort (FIXME) ");
2098 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2103 /* we can do this, since the perl typemap code for char** will
2104 put a NULL at the end of list */
2105 while (input_setnames[num_input_setnames]) num_input_setnames++;
2107 if (zebra_begin_read (zh))
2110 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2111 output_setname, sort_sequence, &sort_status);