1 /* $Id: zebraapi.c,v 1.117 2004-01-22 15:40:25 heikki 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
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,1 /* final */);
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->shadow_enable = 1;
125 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
126 zh->record_encoding = xstrdup (default_encoding);
129 yaz_iconv_open ("UTF-8", default_encoding);
130 if (zh->iconv_to_utf8 == 0)
131 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
133 zh->iconv_from_utf8 =
134 yaz_iconv_open (default_encoding, "UTF-8");
135 if (zh->iconv_to_utf8 == 0)
136 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
139 zebra_mutex_cond_lock (&zs->session_lock);
141 zh->next = zs->sessions;
144 zebra_mutex_cond_unlock (&zs->session_lock);
149 ZebraService zebra_start (const char *configName, Res def_res, Res over_res)
153 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
155 if ((res = res_open (configName, def_res, over_res)))
157 ZebraService zh = xmalloc (sizeof(*zh));
159 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
161 zh->global_res = res;
162 zh->configName = xstrdup(configName);
167 zebra_mutex_cond_init (&zh->session_lock);
168 if (!res_get (zh->global_res, "passwd"))
169 zh->passwd_db = NULL;
172 zh->passwd_db = passwd_db_open ();
174 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
176 passwd_db_file (zh->passwd_db,
177 res_get (zh->global_res, "passwd"));
179 zh->path_root = res_get (zh->global_res, "root");
186 void zebra_pidfname(ZebraService zs, char *path)
188 zebra_lock_prefix (zs->global_res, path);
189 strcat(path, "zebrasrv.pid");
193 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
194 int rw, int useshadow, Res res,
195 const char *reg_path)
197 struct zebra_register *reg;
198 int record_compression = REC_COMPRESS_NONE;
199 const char *recordCompression = 0;
200 const char *profilePath;
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"));
230 getcwd(cwd, sizeof(cwd)-1);
231 profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH);
232 yaz_log(LOG_LOG, "profilePath=%s cwd=%s", profilePath, cwd);
234 data1_set_tabpath (reg->dh, profilePath);
235 data1_set_tabroot (reg->dh, reg_path);
236 reg->recTypes = recTypes_init (reg->dh);
237 recTypes_default_handlers (reg->recTypes);
239 reg->zebra_maps = zebra_maps_open (res, reg_path);
240 reg->rank_classes = NULL;
244 reg->keys.buf_max = 0;
246 reg->sortKeys.buf = 0;
247 reg->sortKeys.buf_max = 0;
260 reg->key_file_no = 0;
263 zebraRankInstall (reg, rank1_class);
264 zebraRankInstall (reg, rankzv_class);
265 zebraRankInstall (reg, rankliv_class);
267 recordCompression = res_get_def (res, "recordCompression", "none");
268 if (!strcmp (recordCompression, "none"))
269 record_compression = REC_COMPRESS_NONE;
270 if (!strcmp (recordCompression, "bzip2"))
271 record_compression = REC_COMPRESS_BZIP2;
273 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
275 logf (LOG_WARN, "rec_open");
280 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
282 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
284 logf (LOG_WARN, "dict_open");
287 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
289 logf (LOG_WARN, "sortIdx_open");
292 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
294 struct ISAMS_M_s isams_m;
295 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
296 key_isams_m(res, &isams_m))))
298 logf (LOG_WARN, "isams_open");
302 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
304 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
305 sizeof (struct it_key), res)))
307 logf (LOG_WARN, "is_open");
311 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
313 struct ISAMC_M_s isamc_m;
314 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
315 rw, key_isamc_m(res, &isamc_m))))
317 logf (LOG_WARN, "isc_open");
321 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
323 struct ISAMD_M_s isamd_m;
325 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
326 rw, key_isamd_m(res, &isamd_m))))
328 logf (LOG_WARN, "isamd_open");
332 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
334 struct ISAMC_M_s isamc_m;
336 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
337 rw, key_isamc_m(res, &isamc_m), 0)))
339 logf (LOG_WARN, "isamb_open");
343 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
345 struct ISAMC_M_s isamc_m;
347 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
348 rw, key_isamc_m(res, &isamc_m), 1)))
350 logf (LOG_WARN, "isamb_open");
354 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
356 struct ISAMC_M_s isamc_m;
358 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
359 rw, key_isamc_m(res, &isamc_m), -1)))
361 logf (LOG_WARN, "isamb_open");
365 reg->zei = zebraExplain_open (reg->records, reg->dh,
370 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
374 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
378 int zebra_admin_shutdown (ZebraHandle zh)
381 yaz_log(LOG_API,"zebra_admin_shutdown");
384 zebra_mutex_cond_lock (&zh->service->session_lock);
385 zh->service->stop_flag = 1;
386 zebra_mutex_cond_unlock (&zh->service->session_lock);
390 int zebra_admin_start (ZebraHandle zh)
394 yaz_log(LOG_API,"zebra_admin_start");
397 zebra_mutex_cond_lock (&zs->session_lock);
398 zebra_mutex_cond_unlock (&zs->session_lock);
402 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
405 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
410 zebraExplain_close (reg->zei);
411 dict_close (reg->dict);
413 dict_close (reg->matchDict);
414 sortIdx_close (reg->sortIdx);
416 isams_close (reg->isams);
418 is_close (reg->isam);
420 isc_close (reg->isamc);
422 isamd_close (reg->isamd);
424 isamb_close (reg->isamb);
425 rec_close (®->records);
428 recTypes_destroy (reg->recTypes);
429 zebra_maps_close (reg->zebra_maps);
430 zebraRankDestroy (reg);
431 bfs_destroy (reg->bfs);
432 data1_destroy (reg->dh);
434 xfree (reg->sortKeys.buf);
435 xfree (reg->keys.buf);
437 xfree (reg->key_buf);
442 int zebra_stop(ZebraService zs)
446 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
450 zebra_close (zs->sessions);
453 zebra_mutex_cond_destroy (&zs->session_lock);
456 passwd_db_close (zs->passwd_db);
458 res_close (zs->global_res);
459 xfree (zs->configName);
464 int zebra_close (ZebraHandle zh)
467 struct zebra_session **sp;
470 yaz_log(LOG_API,"zebra_close");
477 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
478 resultSetDestroy (zh, -1, 0, 0);
481 zebra_register_close (zh->service, zh->reg);
482 zebra_close_res (zh);
484 xfree (zh->record_encoding);
486 for (i = 0; i < zh->num_basenames; i++)
487 xfree (zh->basenames[i]);
488 xfree (zh->basenames);
490 if (zh->iconv_to_utf8 != 0)
491 yaz_iconv_close (zh->iconv_to_utf8);
492 if (zh->iconv_from_utf8 != 0)
493 yaz_iconv_close (zh->iconv_from_utf8);
495 zebra_mutex_cond_lock (&zs->session_lock);
496 zebra_lock_destroy (zh->lock_normal);
497 zebra_lock_destroy (zh->lock_shadow);
509 zebra_mutex_cond_unlock (&zs->session_lock);
510 xfree (zh->reg_name);
511 zh->service=0; /* more likely to trigger an assert */
512 xfree (zh->path_reg);
517 struct map_baseinfo {
523 char **new_basenames;
527 static Res zebra_open_res (ZebraHandle zh)
536 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
537 res = res_open (fname, zh->service->global_res, 0);
539 res = zh->service->global_res;
541 else if (*zh->reg_name == 0)
543 res = zh->service->global_res;
547 yaz_log (LOG_WARN, "no register root specified");
548 return 0; /* no path for register - fail! */
553 static void zebra_close_res (ZebraHandle zh)
557 if (zh->res != zh->service->global_res)
562 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
566 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
570 assert (zh->reg == 0);
571 assert (*zh->reg_name == 0);
577 resultSetInvalidate (zh);
578 zebra_register_close (zh->service, zh->reg);
583 xfree (zh->reg_name);
584 zh->reg_name = xstrdup (new_reg);
586 xfree (zh->path_reg);
588 if (zh->service->path_root)
590 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
591 strlen(zh->reg_name) + 3);
592 strcpy (zh->path_reg, zh->service->path_root);
595 strcat (zh->path_reg, "/");
596 strcat (zh->path_reg, zh->reg_name);
599 zh->res = zebra_open_res (zh);
602 zebra_lock_destroy (zh->lock_normal);
606 zebra_lock_destroy (zh->lock_shadow);
612 const char *lock_area =res_get (zh->res, "lockDir");
614 if (!lock_area && zh->path_reg)
615 res_set (zh->res, "lockDir", zh->path_reg);
616 sprintf (fname, "norm.%s.LCK", zh->reg_name);
618 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
620 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
622 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
628 void map_basenames_func (void *vp, const char *name, const char *value)
630 struct map_baseinfo *p = (struct map_baseinfo *) vp;
632 char fromdb[128], todb[8][128];
635 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
636 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
637 todb[5], todb[6], todb[7]);
641 for (i = 0; i<p->num_bases; i++)
642 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
645 for (i = 0; i < no; i++)
647 if (p->new_num_bases == p->new_num_max)
649 p->new_basenames[(p->new_num_bases)++] =
650 nmem_strdup (p->mem, todb[i]);
656 void map_basenames (ZebraHandle zh, ODR stream,
657 int *num_bases, char ***basenames)
659 struct map_baseinfo info;
660 struct map_baseinfo *p = &info;
663 yaz_log(LOG_API,"map_basenames ");
667 info.num_bases = *num_bases;
668 info.basenames = *basenames;
669 info.new_num_max = 128;
670 info.new_num_bases = 0;
671 info.new_basenames = (char **)
672 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
673 info.mem = stream->mem;
675 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
677 for (i = 0; i<p->num_bases; i++)
678 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
680 p->new_basenames[(p->new_num_bases)++] =
681 nmem_strdup (p->mem, p->basenames[i]);
683 *num_bases = info.new_num_bases;
684 *basenames = info.new_basenames;
685 for (i = 0; i<*num_bases; i++)
686 logf (LOG_LOG, "base %s", (*basenames)[i]);
689 int zebra_select_database (ZebraHandle zh, const char *basename)
692 yaz_log(LOG_API,"zebra_select_database %s",basename);
694 return zebra_select_databases (zh, 1, &basename);
697 int zebra_select_databases (ZebraHandle zh, int num_bases,
698 const char **basenames)
705 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
706 num_bases,basenames[0]);
714 for (i = 0; i < zh->num_basenames; i++)
715 xfree (zh->basenames[i]);
716 xfree (zh->basenames);
718 zh->num_basenames = num_bases;
719 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
720 for (i = 0; i < zh->num_basenames; i++)
721 zh->basenames[i] = xstrdup (basenames[i]);
723 cp = strrchr(basenames[0], '/');
726 len = cp - basenames[0];
727 new_reg = xmalloc (len + 1);
728 memcpy (new_reg, basenames[0], len);
732 new_reg = xstrdup ("");
733 for (i = 1; i<num_bases; i++)
737 cp1 = strrchr (basenames[i], '/');
745 if (len != cp1 - basenames[i] ||
746 memcmp (basenames[i], new_reg, len))
761 zebra_select_register (zh, new_reg);
768 if (!zh->lock_normal || !zh->lock_shadow)
776 int zebra_search_RPN (ZebraHandle zh, ODR o,
777 Z_RPNQuery *query, const char *setname, int *hits)
780 yaz_log(LOG_API,"zebra_search_rpn");
785 if (zebra_begin_read (zh))
788 zebra_livcode_transform(zh, query);
790 resultSetAddRPN (zh, odr_extract_mem(o), query,
791 zh->num_basenames, zh->basenames, setname);
799 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
800 const char *setname, Z_RecordComposition *comp,
801 oid_value input_format, int num_recs,
802 ZebraRetrievalRecord *recs)
805 int i, *pos_array, ret = 0;
807 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
813 zh->errString = odr_strdup (stream, setname);
819 if (zebra_begin_read (zh))
822 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
823 for (i = 0; i<num_recs; i++)
824 pos_array[i] = recs[i].position;
825 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
828 logf (LOG_DEBUG, "zebraPosSetCreate error");
830 zh->errString = nmem_strdup (stream->mem, setname);
835 for (i = 0; i<num_recs; i++)
840 recs[i].format = VAL_SUTRS;
841 recs[i].len = strlen(poset[i].term);
842 recs[i].buf = poset[i].term;
843 recs[i].base = poset[i].db;
845 else if (poset[i].sysno)
848 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
849 stream, input_format, comp,
850 &recs[i].format, &recs[i].buf,
853 recs[i].errString = NULL;
859 sprintf (num_str, "%d", pos_array[i]);
861 zh->errString = odr_strdup (stream, num_str);
866 zebraPosSetDestroy (zh, poset, num_recs);
873 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
874 oid_value attributeset,
875 int *position, int *num_entries, ZebraScanEntry **entries,
879 yaz_log(LOG_API,"zebra_scan");
881 if (zebra_begin_read (zh))
887 rpn_scan (zh, stream, zapt, attributeset,
888 zh->num_basenames, zh->basenames, position,
889 num_entries, entries, is_partial, 0, 0);
894 int zebra_sort (ZebraHandle zh, ODR stream,
895 int num_input_setnames, const char **input_setnames,
896 const char *output_setname, Z_SortKeySpecList *sort_sequence,
900 yaz_log(LOG_API,"zebra_sort");
902 if (zebra_begin_read (zh))
904 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
905 output_setname, sort_sequence, sort_status);
910 int zebra_deleleResultSet(ZebraHandle zh, int function,
911 int num_setnames, char **setnames,
916 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
918 if (zebra_begin_read(zh))
919 return Z_DeleteStatus_systemProblemAtTarget;
922 case Z_DeleteRequest_list:
923 resultSetDestroy (zh, num_setnames, setnames, statuses);
925 case Z_DeleteRequest_all:
926 resultSetDestroy (zh, -1, 0, statuses);
930 status = Z_DeleteStatus_success;
931 for (i = 0; i<num_setnames; i++)
932 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
933 status = statuses[i];
937 int zebra_errCode (ZebraHandle zh)
941 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
944 yaz_log(LOG_API,"zebra_errCode: o");
948 const char *zebra_errString (ZebraHandle zh)
952 e= diagbib1_str (zh->errCode);
953 yaz_log(LOG_API,"zebra_errString: %s",e);
957 char *zebra_errAdd (ZebraHandle zh)
962 yaz_log(LOG_API,"zebra_errAdd: %s",a);
966 void zebra_clearError(ZebraHandle zh)
975 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
979 yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
982 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
984 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
988 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
992 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
993 const char *record_type)
996 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
997 database, record_type);
999 if (zebra_select_database(zh, database))
1001 zebra_begin_trans (zh, 1);
1005 int zebra_admin_import_end (ZebraHandle zh)
1008 yaz_log(LOG_API,"zebra_admin_import_end");
1010 zebra_end_trans (zh);
1014 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1019 yaz_log(LOG_API,"zebra_admin_import_segment");
1021 for (i = 0; i<segment->num_segmentRecords; i++)
1023 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1025 printf ("--------------%d--------------------\n", i);
1026 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1028 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1029 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1031 Odr_oct *oct = fragment->u.notExternallyTagged;
1032 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1037 zebra_update_record(zh,
1038 0, /* record Type */
1050 int zebra_admin_exchange_record (ZebraHandle zh,
1051 const char *rec_buf,
1053 const char *recid_buf, size_t recid_len,
1055 /* 1 = insert. Fail it already exists */
1056 /* 2 = replace. Fail it does not exist */
1057 /* 3 = delete. Fail if does not exist */
1058 /* 4 = update. Insert/replace */
1064 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
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);
1092 action = 1; /* make it an insert (if it's an update).. */
1094 buffer_extract_record (zh, rec_buf, rec_len,
1095 action == 3 ? 1 : 0 /* delete flag */,
1101 0, /* force update */
1102 1 /* allow update */
1106 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1108 else if (action == 3)
1110 dict_delete (zh->reg->matchDict, recid_z);
1112 zebra_end_trans(zh);
1116 int delete_w_handle(const char *info, void *handle)
1118 ZebraHandle zh = (ZebraHandle) handle;
1121 if (*info == sizeof(pos))
1123 memcpy (&pos, info+1, sizeof(pos));
1124 isamb_unlink(zh->reg->isamb, pos);
1129 static int delete_SU_handle(void *handle, int ord)
1131 ZebraHandle zh = (ZebraHandle) handle;
1135 ord_len = key_SU_encode (ord, ord_buf);
1136 ord_buf[ord_len] = '\0';
1138 assert (zh->reg->isamb);
1139 dict_delete_subtree(zh->reg->dict, ord_buf,
1140 zh, delete_w_handle);
1144 int zebra_drop_database (ZebraHandle zh, const char *database)
1148 yaz_log(LOG_API,"zebra_drop_database");
1151 if (zebra_select_database (zh, database))
1153 if (zebra_begin_trans (zh, 1))
1157 zebraExplain_curDatabase (zh->reg->zei, database);
1159 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1160 zebraExplain_removeDatabase(zh->reg->zei, zh);
1164 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1167 zebra_end_trans (zh);
1171 int zebra_create_database (ZebraHandle zh, const char *database)
1174 yaz_log(LOG_API,"zebra_create_database");
1177 if (zebra_select_database (zh, database))
1179 if (zebra_begin_trans (zh, 1))
1182 /* announce database */
1183 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1184 /* explainDatabase */))
1186 zebra_end_trans (zh);
1188 zh->errString = "database already exist";
1191 zebra_end_trans (zh);
1195 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1196 const char *input_str, int input_len,
1197 char *output_str, int output_len)
1201 yaz_log(LOG_API,"zebra_string_norm ");
1203 if (!zh->reg->zebra_maps)
1205 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1206 input_str, input_len);
1209 if (wrbuf_len(wrbuf) >= output_len)
1211 if (wrbuf_len(wrbuf))
1212 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1213 output_str[wrbuf_len(wrbuf)] = '\0';
1214 return wrbuf_len(wrbuf);
1218 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1220 char state_fname[256];
1225 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1228 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1229 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1230 f = fopen (fname, "w");
1232 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1233 fprintf (f, "%c %d %ld\n", val, seqno, p);
1239 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1241 char state_fname[256];
1246 yaz_log(LOG_API,"zebra_get_state ");
1248 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1249 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1250 f = fopen (fname, "r");
1256 fscanf (f, "%c %d", val, seqno);
1263 int zebra_begin_read (ZebraHandle zh)
1265 return zebra_begin_trans(zh, 0);
1268 int zebra_end_read (ZebraHandle zh)
1270 return zebra_end_trans(zh);
1273 static void read_res_for_transaction(ZebraHandle zh)
1275 const char *group = res_get(zh->res, "group");
1278 zh->m_group = group;
1279 v = res_get_prefix(zh->res, "followLinks", group, "1");
1280 zh->m_follow_links = atoi(v);
1282 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1283 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1285 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1286 zh->m_store_keys = atoi(v);
1288 v = res_get_prefix(zh->res, "storeData", group, "1");
1289 zh->m_store_data = atoi(v);
1291 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1292 zh->m_explain_database = atoi(v);
1294 v = res_get_prefix(zh->res, "openRW", group, "1");
1295 zh->m_flag_rw = atoi(v);
1297 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1298 zh->m_file_verbose_limit = atoi(v);
1301 int zebra_begin_trans (ZebraHandle zh, int rw)
1306 zh->errString = "zebra_begin_trans: no database selected";
1310 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1317 const char *rval = 0;
1322 if (zh->trans_no != 1)
1325 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1330 resultSetInvalidate (zh);
1331 zebra_register_close (zh->service, zh->reg);
1333 zh->trans_w_no = zh->trans_no;
1337 zh->records_inserted = 0;
1338 zh->records_updated = 0;
1339 zh->records_deleted = 0;
1340 zh->records_processed = 0;
1342 #if HAVE_SYS_TIMES_H
1346 if (zh->shadow_enable)
1347 rval = res_get (zh->res, "shadow");
1349 for (pass = 0; pass < 2; pass++)
1353 zebra_lock_r (zh->lock_normal);
1354 zebra_lock_w (zh->lock_shadow);
1358 zebra_lock_w (zh->lock_normal);
1359 zebra_lock_w (zh->lock_shadow);
1362 zebra_get_state (zh, &val, &seqno);
1365 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1366 zebra_unlock (zh->lock_shadow);
1367 zebra_unlock (zh->lock_normal);
1371 else if (val == 'd')
1375 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1377 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1378 bf_commitClean (bfs, rval);
1383 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1390 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1394 zebra_set_state (zh, 'd', seqno);
1396 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1397 1, rval ? 1 : 0, zh->res,
1400 zh->reg->seqno = seqno;
1403 zebra_set_state (zh, 'o', seqno);
1405 zebra_unlock (zh->lock_shadow);
1406 zebra_unlock (zh->lock_normal);
1412 zh->errString = "zebra_begin_trans: cannot open register";
1413 yaz_log(LOG_FATAL, zh->errString);
1425 if (zh->trans_no != 1)
1427 zebra_flush_reg (zh);
1431 #if HAVE_SYS_TIMES_H
1440 if (!zh->lock_normal || !zh->lock_shadow)
1446 zebra_get_state (zh, &val, &seqno);
1452 else if (seqno != zh->reg->seqno)
1454 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1455 seqno, zh->reg->seqno);
1458 else if (zh->reg->last_val != val)
1460 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1461 val, zh->reg->last_val);
1468 zebra_lock_r (zh->lock_shadow);
1470 zebra_lock_r (zh->lock_normal);
1474 resultSetInvalidate (zh);
1475 zebra_register_close (zh->service, zh->reg);
1477 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1478 0, val == 'c' ? 1 : 0,
1479 zh->res, zh->path_reg);
1482 zebra_unlock (zh->lock_normal);
1483 zebra_unlock (zh->lock_shadow);
1488 zh->reg->last_val = val;
1489 zh->reg->seqno = seqno;
1491 read_res_for_transaction(zh);
1495 int zebra_end_trans (ZebraHandle zh)
1497 ZebraTransactionStatus dummy;
1498 yaz_log(LOG_API,"zebra_end_trans");
1499 return zebra_end_transaction(zh, &dummy);
1502 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1509 yaz_log(LOG_API,"zebra_end_transaction");
1511 status->processed = 0;
1512 status->inserted = 0;
1513 status->updated = 0;
1514 status->deleted = 0;
1518 if (!zh->res || !zh->reg)
1521 zh->errString = "zebra_end_trans: no open transaction";
1524 if (zh->trans_no != zh->trans_w_no)
1527 if (zh->trans_no != 0)
1530 /* release read lock */
1532 zebra_unlock (zh->lock_normal);
1533 zebra_unlock (zh->lock_shadow);
1536 { /* release write lock */
1540 yaz_log (LOG_LOG, "zebra_end_trans");
1541 rval = res_get (zh->res, "shadow");
1543 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1545 zebra_flush_reg (zh);
1547 resultSetInvalidate (zh);
1549 zebra_register_close (zh->service, zh->reg);
1552 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1553 zh->records_processed, zh->records_inserted,
1554 zh->records_updated, zh->records_deleted);
1556 status->processed = zh->records_processed;
1557 status->inserted = zh->records_inserted;
1558 status->updated = zh->records_updated;
1559 status->deleted = zh->records_deleted;
1561 zebra_get_state (zh, &val, &seqno);
1564 BFiles bfs = bfs_create (rval, zh->path_reg);
1565 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1566 bf_commitClean (bfs, rval);
1571 zebra_set_state (zh, 'o', seqno);
1573 zebra_unlock (zh->lock_shadow);
1574 zebra_unlock (zh->lock_normal);
1577 #if HAVE_SYS_TIMES_H
1579 logf (LOG_LOG, "user/system: %ld/%ld",
1580 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1581 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1583 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1584 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1589 int zebra_repository_update (ZebraHandle zh, const char *path)
1593 logf (LOG_LOG|LOG_API, "updating %s", path);
1594 repositoryUpdate (zh, path);
1598 int zebra_repository_delete (ZebraHandle zh, const char *path)
1602 logf (LOG_LOG|LOG_API, "deleting %s", path);
1603 repositoryDelete (zh, path);
1607 int zebra_repository_show (ZebraHandle zh, const char *path)
1610 yaz_log(LOG_API,"zebra_repository_show");
1612 repositoryShow (zh, path);
1616 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1630 rval = res_get (zh->res, "shadow");
1633 logf (LOG_WARN, "Cannot perform commit");
1634 logf (LOG_WARN, "No shadow area defined");
1638 zebra_lock_w (zh->lock_normal);
1639 zebra_lock_r (zh->lock_shadow);
1641 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1643 zebra_get_state (zh, &val, &seqno);
1646 bf_cache (bfs, rval);
1647 if (bf_commitExists (bfs))
1650 zebra_set_state (zh, 'd', seqno);
1653 zebra_set_state (zh, 'c', seqno);
1655 logf (LOG_LOG, "commit start");
1656 bf_commitExec (bfs);
1661 logf (LOG_LOG, "commit clean");
1662 bf_commitClean (bfs, rval);
1664 zebra_set_state (zh, 'o', seqno);
1668 logf (LOG_LOG, "nothing to commit");
1672 zebra_unlock (zh->lock_shadow);
1673 zebra_unlock (zh->lock_normal);
1677 int zebra_clean (ZebraHandle zh)
1679 yaz_log(LOG_API,"zebra_clean");
1680 return zebra_commit_ex(zh, 1);
1683 int zebra_commit (ZebraHandle zh)
1685 yaz_log(LOG_API,"zebra_commit");
1686 return zebra_commit_ex(zh, 0);
1689 int zebra_init (ZebraHandle zh)
1694 yaz_log(LOG_API,"zebra_init");
1702 rval = res_get (zh->res, "shadow");
1704 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1707 bf_cache (bfs, rval);
1711 zebra_set_state (zh, 'o', 0);
1715 int zebra_compact (ZebraHandle zh)
1719 yaz_log(LOG_API,"zebra_compact");
1726 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1732 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1735 yaz_log(LOG_API,"zebra_result");
1736 *code = zh->errCode;
1737 *addinfo = zh->errString;
1741 int zebra_shadow_enable (ZebraHandle zh, int value)
1744 yaz_log(LOG_API,"zebra_shadow_enable");
1746 zh->shadow_enable = value;
1750 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1753 yaz_log(LOG_API,"zebra_record_encoding");
1755 xfree (zh->record_encoding);
1759 * Something about charset aliases. Oleg???
1762 if (zh->iconv_to_utf8 != 0)
1763 yaz_iconv_close(zh->iconv_to_utf8);
1764 if (zh->iconv_from_utf8 != 0)
1765 yaz_iconv_close(zh->iconv_from_utf8);
1767 zh->record_encoding = xstrdup (encoding);
1769 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1772 yaz_iconv_open ("UTF-8", encoding);
1773 if (zh->iconv_to_utf8 == 0)
1774 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1775 zh->iconv_from_utf8 =
1776 yaz_iconv_open (encoding, "UTF-8");
1777 if (zh->iconv_to_utf8 == 0)
1778 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1783 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1786 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1788 res_set(zh->res, name, value);
1792 const char *zebra_get_resource(ZebraHandle zh,
1793 const char *name, const char *defaultvalue)
1797 v= res_get_def( zh->res, name, (char *)defaultvalue);
1799 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1803 /* moved from zebra_api_ext.c by pop */
1804 /* FIXME: Should this really be public??? -Heikki */
1806 int zebra_trans_no (ZebraHandle zh)
1809 yaz_log(LOG_API,"zebra_trans_no");
1810 return zh->trans_no;
1813 int zebra_get_shadow_enable (ZebraHandle zh)
1815 yaz_log(LOG_API,"zebra_get_shadow_enable");
1816 return (zh->shadow_enable);
1819 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1821 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1822 zh->shadow_enable = value;
1826 /* almost the same as zebra_records_retrieve ... but how did it work?
1827 I mean for multiple records ??? CHECK ??? */
1828 void api_records_retrieve (ZebraHandle zh, ODR stream,
1829 const char *setname, Z_RecordComposition *comp,
1830 oid_value input_format, int num_recs,
1831 ZebraRetrievalRecord *recs)
1835 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1840 zh->errString = odr_strdup (stream, setname);
1846 if (zebra_begin_read (zh))
1849 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1850 for (i = 0; i<num_recs; i++)
1851 pos_array[i] = recs[i].position;
1852 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1855 logf (LOG_DEBUG, "zebraPosSetCreate error");
1857 zh->errString = nmem_strdup (stream->mem, setname);
1861 for (i = 0; i<num_recs; i++)
1865 recs[i].errCode = 0;
1866 recs[i].format = VAL_SUTRS;
1867 recs[i].len = strlen(poset[i].term);
1868 recs[i].buf = poset[i].term;
1869 recs[i].base = poset[i].db;
1873 else if (poset[i].sysno)
1875 /* changed here ??? CHECK ??? */
1878 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1879 stream, input_format, comp,
1884 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1885 memcpy(recs[i].buf, b, recs[i].len);
1886 recs[i].errString = 0; /* Hmmm !!! we should get this */
1887 recs[i].sysno = poset[i].sysno;
1888 recs[i].score = poset[i].score;
1894 sprintf (num_str, "%d", pos_array[i]);
1896 zh->errString = odr_strdup (stream, num_str);
1901 zebraPosSetDestroy (zh, poset, num_recs);
1903 zebra_end_read (zh);
1908 /* ---------------------------------------------------------------------------
1909 Record insert(=update), delete
1911 If sysno is provided, then it's used to identify the record.
1912 If not, and match_criteria is provided, then sysno is guessed
1913 If not, and a record is provided, then sysno is got from there
1914 NOTE: Now returns 0 at success and updates sysno, which is an int*
1918 int zebra_add_record(ZebraHandle zh,
1919 const char *buf, int buf_size)
1922 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1925 int zebra_insert_record (ZebraHandle zh,
1926 const char *recordType,
1927 int *sysno, const char *match, const char *fname,
1928 const char *buf, int buf_size)
1931 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
1933 if (buf_size < 1) buf_size = strlen(buf);
1935 zebra_begin_trans(zh, 1);
1936 res = buffer_extract_record (zh, buf, buf_size,
1937 0, /* delete_flag */
1943 0); /* allow_update */
1944 zebra_end_trans(zh);
1948 int zebra_update_record (ZebraHandle zh,
1949 const char *recordType,
1950 int* sysno, const char *match, const char *fname,
1951 const char *buf, int buf_size,
1956 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
1958 if (buf_size < 1) buf_size = strlen(buf);
1960 zebra_begin_trans(zh, 1);
1961 res = buffer_extract_record (zh, buf, buf_size,
1962 0, /* delete_flag */
1968 1); /* allow_update */
1969 zebra_end_trans(zh);
1973 int zebra_delete_record (ZebraHandle zh,
1974 const char *recordType,
1975 int *sysno, const char *match, const char *fname,
1976 const char *buf, int buf_size,
1980 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
1982 if (buf_size < 1) buf_size = strlen(buf);
1984 zebra_begin_trans(zh, 1);
1985 res = buffer_extract_record (zh, buf, buf_size,
1986 1, /* delete_flag */
1992 1); /* allow_update */
1993 zebra_end_trans(zh);
1997 /* ---------------------------------------------------------------------------
2001 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2002 const char *setname, int *numhits)
2007 ODR odr = odr_createmem(ODR_ENCODE);
2009 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2011 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2014 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2016 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2020 yaz_log(LOG_API,"Hits: %d",hits);
2028 /* ---------------------------------------------------------------------------
2029 Sort - a simplified interface, with optional read locks.
2030 FIXME - This is a horrible name, will conflict with half the applications
2032 int zebra_sort_by_specstr (ZebraHandle zh,
2034 const char *sort_spec,
2035 const char *output_setname,
2036 const char **input_setnames)
2038 int num_input_setnames = 0;
2039 int sort_status = 0;
2040 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2041 yaz_log(LOG_API,"sort (FIXME) ");
2044 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2049 /* we can do this, since the perl typemap code for char** will
2050 put a NULL at the end of list */
2051 while (input_setnames[num_input_setnames]) num_input_setnames++;
2053 if (zebra_begin_read (zh))
2056 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2057 output_setname, sort_sequence, &sort_status);