1 /* $Id: zebraapi.c,v 1.125 2004-08-10 08:54:39 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
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;
254 reg->keys.codec_handle = iscz1_start();
257 reg->sortKeys.buf = 0;
258 reg->sortKeys.buf_max = 0;
269 reg->key_file_no = 0;
272 zebraRankInstall (reg, rank1_class);
273 zebraRankInstall (reg, rankzv_class);
274 zebraRankInstall (reg, rankliv_class);
276 recordCompression = res_get_def (res, "recordCompression", "none");
277 if (!strcmp (recordCompression, "none"))
278 record_compression = REC_COMPRESS_NONE;
279 if (!strcmp (recordCompression, "bzip2"))
280 record_compression = REC_COMPRESS_BZIP2;
282 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
284 logf (LOG_WARN, "rec_open");
289 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
291 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
293 logf (LOG_WARN, "dict_open");
296 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
298 logf (LOG_WARN, "sortIdx_open");
301 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
303 struct ISAMS_M_s isams_m;
304 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
305 key_isams_m(res, &isams_m))))
307 logf (LOG_WARN, "isams_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", "b", ISAM_DEFAULT))
323 struct ISAMC_M_s isamc_m;
325 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
326 rw, key_isamc_m(res, &isamc_m), 0)))
328 logf (LOG_WARN, "isamb_open");
332 if (res_get_match (res, "isam", "bc", 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), 1)))
339 logf (LOG_WARN, "isamb_open");
343 if (res_get_match (res, "isam", "null", 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 reg->zei = zebraExplain_open (reg->records, reg->dh,
359 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
363 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
367 int zebra_admin_shutdown (ZebraHandle zh)
370 yaz_log(LOG_API,"zebra_admin_shutdown");
373 zebra_mutex_cond_lock (&zh->service->session_lock);
374 zh->service->stop_flag = 1;
375 zebra_mutex_cond_unlock (&zh->service->session_lock);
379 int zebra_admin_start (ZebraHandle zh)
383 yaz_log(LOG_API,"zebra_admin_start");
386 zebra_mutex_cond_lock (&zs->session_lock);
387 zebra_mutex_cond_unlock (&zs->session_lock);
391 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
394 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
399 zebraExplain_close (reg->zei);
400 dict_close (reg->dict);
402 dict_close (reg->matchDict);
403 sortIdx_close (reg->sortIdx);
405 isams_close (reg->isams);
407 isc_close (reg->isamc);
409 isamb_close (reg->isamb);
410 rec_close (®->records);
413 recTypes_destroy (reg->recTypes);
414 zebra_maps_close (reg->zebra_maps);
415 zebraRankDestroy (reg);
416 bfs_destroy (reg->bfs);
417 data1_destroy (reg->dh);
419 xfree (reg->sortKeys.buf);
420 xfree (reg->keys.buf);
422 if (reg->keys.codec_handle)
423 iscz1_stop(reg->keys.codec_handle);
426 xfree (reg->key_buf);
431 int zebra_stop(ZebraService zs)
435 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
439 zebra_close (zs->sessions);
442 zebra_mutex_cond_destroy (&zs->session_lock);
445 passwd_db_close (zs->passwd_db);
447 res_close (zs->global_res);
448 xfree (zs->configName);
453 int zebra_close (ZebraHandle zh)
456 struct zebra_session **sp;
459 yaz_log(LOG_API,"zebra_close");
466 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
467 resultSetDestroy (zh, -1, 0, 0);
470 zebra_register_close (zh->service, zh->reg);
471 zebra_close_res (zh);
473 xfree (zh->record_encoding);
475 for (i = 0; i < zh->num_basenames; i++)
476 xfree (zh->basenames[i]);
477 xfree (zh->basenames);
479 if (zh->iconv_to_utf8 != 0)
480 yaz_iconv_close (zh->iconv_to_utf8);
481 if (zh->iconv_from_utf8 != 0)
482 yaz_iconv_close (zh->iconv_from_utf8);
484 zebra_mutex_cond_lock (&zs->session_lock);
485 zebra_lock_destroy (zh->lock_normal);
486 zebra_lock_destroy (zh->lock_shadow);
498 zebra_mutex_cond_unlock (&zs->session_lock);
499 xfree (zh->reg_name);
500 xfree (zh->user_perm);
501 zh->service=0; /* more likely to trigger an assert */
502 xfree (zh->path_reg);
507 struct map_baseinfo {
513 char **new_basenames;
517 static Res zebra_open_res (ZebraHandle zh)
526 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
527 res = res_open (fname, zh->service->global_res, 0);
529 res = zh->service->global_res;
531 else if (*zh->reg_name == 0)
533 res = zh->service->global_res;
537 yaz_log (LOG_WARN, "no register root specified");
538 return 0; /* no path for register - fail! */
543 static void zebra_close_res (ZebraHandle zh)
547 if (zh->res != zh->service->global_res)
552 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
556 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
560 assert (zh->reg == 0);
561 assert (*zh->reg_name == 0);
567 resultSetInvalidate (zh);
568 zebra_register_close (zh->service, zh->reg);
573 xfree (zh->reg_name);
574 zh->reg_name = xstrdup (new_reg);
576 xfree (zh->path_reg);
578 if (zh->service->path_root)
580 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
581 strlen(zh->reg_name) + 3);
582 strcpy (zh->path_reg, zh->service->path_root);
585 strcat (zh->path_reg, "/");
586 strcat (zh->path_reg, zh->reg_name);
589 zh->res = zebra_open_res (zh);
592 zebra_lock_destroy (zh->lock_normal);
596 zebra_lock_destroy (zh->lock_shadow);
602 const char *lock_area =res_get (zh->res, "lockDir");
604 if (!lock_area && zh->path_reg)
605 res_set (zh->res, "lockDir", zh->path_reg);
606 sprintf (fname, "norm.%s.LCK", zh->reg_name);
608 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
610 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
612 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
618 void map_basenames_func (void *vp, const char *name, const char *value)
620 struct map_baseinfo *p = (struct map_baseinfo *) vp;
622 char fromdb[128], todb[8][128];
625 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
626 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
627 todb[5], todb[6], todb[7]);
631 for (i = 0; i<p->num_bases; i++)
632 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
635 for (i = 0; i < no; i++)
637 if (p->new_num_bases == p->new_num_max)
639 p->new_basenames[(p->new_num_bases)++] =
640 nmem_strdup (p->mem, todb[i]);
646 void map_basenames (ZebraHandle zh, ODR stream,
647 int *num_bases, char ***basenames)
649 struct map_baseinfo info;
650 struct map_baseinfo *p = &info;
653 yaz_log(LOG_API,"map_basenames ");
657 info.num_bases = *num_bases;
658 info.basenames = *basenames;
659 info.new_num_max = 128;
660 info.new_num_bases = 0;
661 info.new_basenames = (char **)
662 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
663 info.mem = stream->mem;
665 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
667 for (i = 0; i<p->num_bases; i++)
668 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
670 p->new_basenames[(p->new_num_bases)++] =
671 nmem_strdup (p->mem, p->basenames[i]);
673 *num_bases = info.new_num_bases;
674 *basenames = info.new_basenames;
675 for (i = 0; i<*num_bases; i++)
676 logf (LOG_LOG, "base %s", (*basenames)[i]);
679 int zebra_select_database (ZebraHandle zh, const char *basename)
682 yaz_log(LOG_API,"zebra_select_database %s",basename);
684 return zebra_select_databases (zh, 1, &basename);
687 int zebra_select_databases (ZebraHandle zh, int num_bases,
688 const char **basenames)
695 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
696 num_bases,basenames[0]);
704 for (i = 0; i < zh->num_basenames; i++)
705 xfree (zh->basenames[i]);
706 xfree (zh->basenames);
708 zh->num_basenames = num_bases;
709 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
710 for (i = 0; i < zh->num_basenames; i++)
711 zh->basenames[i] = xstrdup (basenames[i]);
713 cp = strrchr(basenames[0], '/');
716 len = cp - basenames[0];
717 new_reg = xmalloc (len + 1);
718 memcpy (new_reg, basenames[0], len);
722 new_reg = xstrdup ("");
723 for (i = 1; i<num_bases; i++)
727 cp1 = strrchr (basenames[i], '/');
735 if (len != cp1 - basenames[i] ||
736 memcmp (basenames[i], new_reg, len))
751 zebra_select_register (zh, new_reg);
758 if (!zh->lock_normal || !zh->lock_shadow)
766 int zebra_search_RPN (ZebraHandle zh, ODR o,
767 Z_RPNQuery *query, const char *setname, int *hits)
770 yaz_log(LOG_API,"zebra_search_rpn");
775 if (zebra_begin_read (zh))
778 zebra_livcode_transform(zh, query);
780 resultSetAddRPN (zh, odr_extract_mem(o), query,
781 zh->num_basenames, zh->basenames, setname);
784 if (zh->hits > INT_MAX)
791 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
792 const char *setname, Z_RecordComposition *comp,
793 oid_value input_format, int num_recs,
794 ZebraRetrievalRecord *recs)
797 int i, *pos_array, ret = 0;
799 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
805 zh->errString = odr_strdup (stream, setname);
811 if (zebra_begin_read (zh))
814 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
815 for (i = 0; i<num_recs; i++)
816 pos_array[i] = recs[i].position;
817 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
820 logf (LOG_DEBUG, "zebraPosSetCreate error");
822 zh->errString = nmem_strdup (stream->mem, setname);
827 for (i = 0; i<num_recs; i++)
832 recs[i].format = VAL_SUTRS;
833 recs[i].len = strlen(poset[i].term);
834 recs[i].buf = poset[i].term;
835 recs[i].base = poset[i].db;
837 else if (poset[i].sysno)
840 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
841 stream, input_format, comp,
842 &recs[i].format, &recs[i].buf,
845 recs[i].errString = NULL;
851 sprintf (num_str, "%d", pos_array[i]);
853 zh->errString = odr_strdup (stream, num_str);
858 zebraPosSetDestroy (zh, poset, num_recs);
865 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
866 oid_value attributeset,
867 int *position, int *num_entries, ZebraScanEntry **entries,
871 yaz_log(LOG_API,"zebra_scan");
873 if (zebra_begin_read (zh))
879 rpn_scan (zh, stream, zapt, attributeset,
880 zh->num_basenames, zh->basenames, position,
881 num_entries, entries, is_partial, 0, 0);
886 int zebra_sort (ZebraHandle zh, ODR stream,
887 int num_input_setnames, const char **input_setnames,
888 const char *output_setname, Z_SortKeySpecList *sort_sequence,
892 yaz_log(LOG_API,"zebra_sort");
894 if (zebra_begin_read (zh))
896 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
897 output_setname, sort_sequence, sort_status);
902 int zebra_deleleResultSet(ZebraHandle zh, int function,
903 int num_setnames, char **setnames,
908 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
910 if (zebra_begin_read(zh))
911 return Z_DeleteStatus_systemProblemAtTarget;
914 case Z_DeleteResultSetRequest_list:
915 resultSetDestroy (zh, num_setnames, setnames, statuses);
917 case Z_DeleteResultSetRequest_all:
918 resultSetDestroy (zh, -1, 0, statuses);
922 status = Z_DeleteStatus_success;
923 for (i = 0; i<num_setnames; i++)
924 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
925 status = statuses[i];
929 int zebra_errCode (ZebraHandle zh)
933 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
936 yaz_log(LOG_API,"zebra_errCode: o");
940 const char *zebra_errString (ZebraHandle zh)
944 e= diagbib1_str (zh->errCode);
945 yaz_log(LOG_API,"zebra_errString: %s",e);
949 char *zebra_errAdd (ZebraHandle zh)
954 yaz_log(LOG_API,"zebra_errAdd: %s",a);
958 void zebra_clearError(ZebraHandle zh)
967 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
977 sprintf(u, "perm.%.30s", user ? user : "anonymous");
978 p = res_get(zs->global_res, u);
979 xfree (zh->user_perm);
980 zh->user_perm = xstrdup(p ? p : "r");
982 /* users that don't require a password .. */
983 if (zh->user_perm && strchr(zh->user_perm, 'a'))
986 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
991 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
992 const char *record_type)
995 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
996 database, record_type);
998 if (zebra_select_database(zh, database))
1000 if (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 if (zebra_begin_trans(zh, 1))
1075 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1078 if (action == 1) /* fail if insert */
1080 zebra_end_trans(zh);
1084 memcpy (&sysno, rinfo+1, sizeof(sysno));
1088 if (action == 2 || action == 3) /* fail if delete or update */
1090 zebra_end_trans(zh);
1093 action = 1; /* make it an insert (if it's an update).. */
1095 buffer_extract_record (zh, rec_buf, rec_len,
1096 action == 3 ? 1 : 0 /* delete flag */,
1102 0, /* force update */
1103 1 /* allow update */
1107 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1109 else if (action == 3)
1111 dict_delete (zh->reg->matchDict, recid_z);
1113 zebra_end_trans(zh);
1117 int delete_w_handle(const char *info, void *handle)
1119 ZebraHandle zh = (ZebraHandle) handle;
1122 if (*info == sizeof(pos))
1124 memcpy (&pos, info+1, sizeof(pos));
1125 isamb_unlink(zh->reg->isamb, pos);
1130 static int delete_SU_handle(void *handle, int ord)
1132 ZebraHandle zh = (ZebraHandle) handle;
1136 ord_len = key_SU_encode (ord, ord_buf);
1137 ord_buf[ord_len] = '\0';
1139 assert (zh->reg->isamb);
1140 dict_delete_subtree(zh->reg->dict, ord_buf,
1141 zh, delete_w_handle);
1145 int zebra_drop_database (ZebraHandle zh, const char *database)
1149 yaz_log(LOG_API,"zebra_drop_database");
1152 if (zebra_select_database (zh, database))
1154 if (zebra_begin_trans (zh, 1))
1158 zebraExplain_curDatabase (zh->reg->zei, database);
1160 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1161 zebraExplain_removeDatabase(zh->reg->zei, zh);
1165 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1168 zebra_end_trans (zh);
1172 int zebra_create_database (ZebraHandle zh, const char *database)
1175 yaz_log(LOG_API,"zebra_create_database");
1178 if (zebra_select_database (zh, database))
1180 if (zebra_begin_trans (zh, 1))
1183 /* announce database */
1184 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1185 /* explainDatabase */))
1187 zebra_end_trans (zh);
1189 zh->errString = "database already exist";
1192 zebra_end_trans (zh);
1196 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1197 const char *input_str, int input_len,
1198 char *output_str, int output_len)
1202 yaz_log(LOG_API,"zebra_string_norm ");
1204 if (!zh->reg->zebra_maps)
1206 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1207 input_str, input_len);
1210 if (wrbuf_len(wrbuf) >= output_len)
1212 if (wrbuf_len(wrbuf))
1213 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1214 output_str[wrbuf_len(wrbuf)] = '\0';
1215 return wrbuf_len(wrbuf);
1219 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1221 char state_fname[256];
1226 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1229 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1230 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1231 f = fopen (fname, "w");
1233 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1234 fprintf (f, "%c %d %ld\n", val, seqno, p);
1240 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1242 char state_fname[256];
1247 yaz_log(LOG_API,"zebra_get_state ");
1249 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1250 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1251 f = fopen (fname, "r");
1257 fscanf (f, "%c %d", val, seqno);
1264 int zebra_begin_read (ZebraHandle zh)
1266 return zebra_begin_trans(zh, 0);
1269 int zebra_end_read (ZebraHandle zh)
1271 return zebra_end_trans(zh);
1274 static void read_res_for_transaction(ZebraHandle zh)
1276 const char *group = res_get(zh->res, "group");
1279 zh->m_group = group;
1280 v = res_get_prefix(zh->res, "followLinks", group, "1");
1281 zh->m_follow_links = atoi(v);
1283 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1284 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1286 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1287 zh->m_store_keys = atoi(v);
1289 v = res_get_prefix(zh->res, "storeData", group, "1");
1290 zh->m_store_data = atoi(v);
1292 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1293 zh->m_explain_database = atoi(v);
1295 v = res_get_prefix(zh->res, "openRW", group, "1");
1296 zh->m_flag_rw = atoi(v);
1298 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1299 zh->m_file_verbose_limit = atoi(v);
1302 int zebra_begin_trans (ZebraHandle zh, int rw)
1307 zh->errString = "zebra_begin_trans: no database selected";
1311 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1315 if (rw && !strchr(zh->user_perm, 'w'))
1329 const char *rval = 0;
1334 read_res_for_transaction(zh);
1337 if (zh->trans_no != 1)
1340 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1345 resultSetInvalidate (zh);
1346 zebra_register_close (zh->service, zh->reg);
1348 zh->trans_w_no = zh->trans_no;
1352 zh->records_inserted = 0;
1353 zh->records_updated = 0;
1354 zh->records_deleted = 0;
1355 zh->records_processed = 0;
1357 #if HAVE_SYS_TIMES_H
1361 if (zh->shadow_enable)
1362 rval = res_get (zh->res, "shadow");
1364 for (pass = 0; pass < 2; pass++)
1368 zebra_lock_r (zh->lock_normal);
1369 zebra_lock_w (zh->lock_shadow);
1373 zebra_lock_w (zh->lock_normal);
1374 zebra_lock_w (zh->lock_shadow);
1377 zebra_get_state (zh, &val, &seqno);
1380 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1381 zebra_unlock (zh->lock_shadow);
1382 zebra_unlock (zh->lock_normal);
1386 else if (val == 'd')
1390 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1392 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1393 bf_commitClean (bfs, rval);
1398 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1405 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1409 zebra_set_state (zh, 'd', seqno);
1411 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1412 1, rval ? 1 : 0, zh->res,
1415 zh->reg->seqno = seqno;
1418 zebra_set_state (zh, 'o', seqno);
1420 zebra_unlock (zh->lock_shadow);
1421 zebra_unlock (zh->lock_normal);
1427 zh->errString = "zebra_begin_trans: cannot open register";
1428 yaz_log(LOG_FATAL, zh->errString);
1440 if (zh->trans_no != 1)
1442 zebra_flush_reg (zh);
1446 #if HAVE_SYS_TIMES_H
1455 if (!zh->lock_normal || !zh->lock_shadow)
1461 zebra_get_state (zh, &val, &seqno);
1467 else if (seqno != zh->reg->seqno)
1469 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1470 seqno, zh->reg->seqno);
1473 else if (zh->reg->last_val != val)
1475 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1476 val, zh->reg->last_val);
1483 zebra_lock_r (zh->lock_shadow);
1485 zebra_lock_r (zh->lock_normal);
1489 resultSetInvalidate (zh);
1490 zebra_register_close (zh->service, zh->reg);
1492 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1493 0, val == 'c' ? 1 : 0,
1494 zh->res, zh->path_reg);
1497 zebra_unlock (zh->lock_normal);
1498 zebra_unlock (zh->lock_shadow);
1503 zh->reg->last_val = val;
1504 zh->reg->seqno = seqno;
1506 read_res_for_transaction(zh);
1510 int zebra_end_trans (ZebraHandle zh)
1512 ZebraTransactionStatus dummy;
1513 yaz_log(LOG_API,"zebra_end_trans");
1514 return zebra_end_transaction(zh, &dummy);
1517 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1524 yaz_log(LOG_API,"zebra_end_transaction");
1526 status->processed = 0;
1527 status->inserted = 0;
1528 status->updated = 0;
1529 status->deleted = 0;
1533 if (!zh->res || !zh->reg)
1536 zh->errString = "zebra_end_trans: no open transaction";
1539 if (zh->trans_no != zh->trans_w_no)
1542 if (zh->trans_no != 0)
1545 /* release read lock */
1547 zebra_unlock (zh->lock_normal);
1548 zebra_unlock (zh->lock_shadow);
1551 { /* release write lock */
1555 yaz_log (LOG_LOG, "zebra_end_trans");
1556 rval = res_get (zh->res, "shadow");
1558 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1560 zebra_flush_reg (zh);
1562 resultSetInvalidate (zh);
1564 zebra_register_close (zh->service, zh->reg);
1567 yaz_log (LOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1568 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
1569 zh->records_processed, zh->records_inserted,
1570 zh->records_updated, zh->records_deleted);
1572 status->processed = zh->records_processed;
1573 status->inserted = zh->records_inserted;
1574 status->updated = zh->records_updated;
1575 status->deleted = zh->records_deleted;
1577 zebra_get_state (zh, &val, &seqno);
1580 BFiles bfs = bfs_create (rval, zh->path_reg);
1581 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1582 bf_commitClean (bfs, rval);
1587 zebra_set_state (zh, 'o', seqno);
1589 zebra_unlock (zh->lock_shadow);
1590 zebra_unlock (zh->lock_normal);
1593 #if HAVE_SYS_TIMES_H
1595 logf (LOG_LOG, "user/system: %ld/%ld",
1596 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1597 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1599 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1600 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1605 int zebra_repository_update (ZebraHandle zh, const char *path)
1609 logf (LOG_LOG|LOG_API, "updating %s", path);
1610 repositoryUpdate (zh, path);
1614 int zebra_repository_delete (ZebraHandle zh, const char *path)
1618 logf (LOG_LOG|LOG_API, "deleting %s", path);
1619 repositoryDelete (zh, path);
1623 int zebra_repository_show (ZebraHandle zh, const char *path)
1626 yaz_log(LOG_API,"zebra_repository_show");
1628 repositoryShow (zh, path);
1632 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1646 rval = res_get (zh->res, "shadow");
1649 logf (LOG_WARN, "Cannot perform commit");
1650 logf (LOG_WARN, "No shadow area defined");
1654 zebra_lock_w (zh->lock_normal);
1655 zebra_lock_r (zh->lock_shadow);
1657 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1659 zebra_get_state (zh, &val, &seqno);
1662 bf_cache (bfs, rval);
1663 if (bf_commitExists (bfs))
1666 zebra_set_state (zh, 'd', seqno);
1669 zebra_set_state (zh, 'c', seqno);
1671 logf (LOG_LOG, "commit start");
1672 bf_commitExec (bfs);
1677 logf (LOG_LOG, "commit clean");
1678 bf_commitClean (bfs, rval);
1680 zebra_set_state (zh, 'o', seqno);
1684 logf (LOG_LOG, "nothing to commit");
1688 zebra_unlock (zh->lock_shadow);
1689 zebra_unlock (zh->lock_normal);
1693 int zebra_clean (ZebraHandle zh)
1695 yaz_log(LOG_API,"zebra_clean");
1696 return zebra_commit_ex(zh, 1);
1699 int zebra_commit (ZebraHandle zh)
1701 yaz_log(LOG_API,"zebra_commit");
1702 return zebra_commit_ex(zh, 0);
1705 int zebra_init (ZebraHandle zh)
1710 yaz_log(LOG_API,"zebra_init");
1718 rval = res_get (zh->res, "shadow");
1720 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1723 bf_cache (bfs, rval);
1727 zebra_set_state (zh, 'o', 0);
1731 int zebra_compact (ZebraHandle zh)
1735 yaz_log(LOG_API,"zebra_compact");
1742 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1748 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1751 yaz_log(LOG_API,"zebra_result");
1752 *code = zh->errCode;
1753 *addinfo = zh->errString;
1757 int zebra_shadow_enable (ZebraHandle zh, int value)
1760 yaz_log(LOG_API,"zebra_shadow_enable");
1762 zh->shadow_enable = value;
1766 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1769 yaz_log(LOG_API,"zebra_record_encoding");
1771 xfree (zh->record_encoding);
1775 * Something about charset aliases. Oleg???
1778 if (zh->iconv_to_utf8 != 0)
1779 yaz_iconv_close(zh->iconv_to_utf8);
1780 if (zh->iconv_from_utf8 != 0)
1781 yaz_iconv_close(zh->iconv_from_utf8);
1783 zh->record_encoding = xstrdup (encoding);
1785 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1788 yaz_iconv_open ("UTF-8", encoding);
1789 if (zh->iconv_to_utf8 == 0)
1790 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1791 zh->iconv_from_utf8 =
1792 yaz_iconv_open (encoding, "UTF-8");
1793 if (zh->iconv_to_utf8 == 0)
1794 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1799 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1802 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1804 res_set(zh->res, name, value);
1808 const char *zebra_get_resource(ZebraHandle zh,
1809 const char *name, const char *defaultvalue)
1813 v= res_get_def( zh->res, name, (char *)defaultvalue);
1815 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1819 /* moved from zebra_api_ext.c by pop */
1820 /* FIXME: Should this really be public??? -Heikki */
1822 int zebra_trans_no (ZebraHandle zh)
1825 yaz_log(LOG_API,"zebra_trans_no");
1826 return zh->trans_no;
1829 int zebra_get_shadow_enable (ZebraHandle zh)
1831 yaz_log(LOG_API,"zebra_get_shadow_enable");
1832 return (zh->shadow_enable);
1835 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1837 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1838 zh->shadow_enable = value;
1842 /* almost the same as zebra_records_retrieve ... but how did it work?
1843 I mean for multiple records ??? CHECK ??? */
1844 void api_records_retrieve (ZebraHandle zh, ODR stream,
1845 const char *setname, Z_RecordComposition *comp,
1846 oid_value input_format, int num_recs,
1847 ZebraRetrievalRecord *recs)
1851 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1856 zh->errString = odr_strdup (stream, setname);
1862 if (zebra_begin_read (zh))
1865 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1866 for (i = 0; i<num_recs; i++)
1867 pos_array[i] = recs[i].position;
1868 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1871 logf (LOG_DEBUG, "zebraPosSetCreate error");
1873 zh->errString = nmem_strdup (stream->mem, setname);
1877 for (i = 0; i<num_recs; i++)
1881 recs[i].errCode = 0;
1882 recs[i].format = VAL_SUTRS;
1883 recs[i].len = strlen(poset[i].term);
1884 recs[i].buf = poset[i].term;
1885 recs[i].base = poset[i].db;
1889 else if (poset[i].sysno)
1891 /* changed here ??? CHECK ??? */
1894 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1895 stream, input_format, comp,
1900 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1901 memcpy(recs[i].buf, b, recs[i].len);
1902 recs[i].errString = 0; /* Hmmm !!! we should get this */
1903 recs[i].sysno = poset[i].sysno;
1904 recs[i].score = poset[i].score;
1910 sprintf (num_str, "%d", pos_array[i]);
1912 zh->errString = odr_strdup (stream, num_str);
1917 zebraPosSetDestroy (zh, poset, num_recs);
1919 zebra_end_read (zh);
1924 /* ---------------------------------------------------------------------------
1925 Record insert(=update), delete
1927 If sysno is provided, then it's used to identify the record.
1928 If not, and match_criteria is provided, then sysno is guessed
1929 If not, and a record is provided, then sysno is got from there
1930 NOTE: Now returns 0 at success and updates sysno, which is an int*
1934 int zebra_add_record(ZebraHandle zh,
1935 const char *buf, int buf_size)
1938 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1941 int zebra_insert_record (ZebraHandle zh,
1942 const char *recordType,
1943 SYSNO *sysno, const char *match, const char *fname,
1944 const char *buf, int buf_size, int force_update)
1947 yaz_log(LOG_API,"zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
1949 if (buf_size < 1) buf_size = strlen(buf);
1951 if (zebra_begin_trans(zh, 1))
1953 res = buffer_extract_record (zh, buf, buf_size,
1954 0, /* delete_flag */
1960 0); /* allow_update */
1961 zebra_end_trans(zh);
1965 int zebra_update_record (ZebraHandle zh,
1966 const char *recordType,
1967 SYSNO* sysno, const char *match, const char *fname,
1968 const char *buf, int buf_size,
1973 yaz_log(LOG_API,"zebra_update_record sysno=" ZINT_FORMAT, *sysno);
1975 if (buf_size < 1) buf_size = strlen(buf);
1977 if (zebra_begin_trans(zh, 1))
1979 res = buffer_extract_record (zh, buf, buf_size,
1980 0, /* delete_flag */
1986 1); /* allow_update */
1987 zebra_end_trans(zh);
1991 int zebra_delete_record (ZebraHandle zh,
1992 const char *recordType,
1993 SYSNO *sysno, const char *match, const char *fname,
1994 const char *buf, int buf_size,
1998 yaz_log(LOG_API,"zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
2000 if (buf_size < 1) buf_size = strlen(buf);
2002 if (zebra_begin_trans(zh, 1))
2004 res = buffer_extract_record (zh, buf, buf_size,
2005 1, /* delete_flag */
2011 1); /* allow_update */
2012 zebra_end_trans(zh);
2016 /* ---------------------------------------------------------------------------
2020 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2021 const char *setname, int *numhits)
2026 ODR odr = odr_createmem(ODR_ENCODE);
2028 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2030 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2033 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2035 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2039 yaz_log(LOG_API,"Hits: %d",hits);
2047 /* ---------------------------------------------------------------------------
2048 Sort - a simplified interface, with optional read locks.
2049 FIXME - This is a horrible name, will conflict with half the applications
2051 int zebra_sort_by_specstr (ZebraHandle zh,
2053 const char *sort_spec,
2054 const char *output_setname,
2055 const char **input_setnames)
2057 int num_input_setnames = 0;
2058 int sort_status = 0;
2059 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2060 yaz_log(LOG_API,"sort (FIXME) ");
2063 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2068 /* we can do this, since the perl typemap code for char** will
2069 put a NULL at the end of list */
2070 while (input_setnames[num_input_setnames]) num_input_setnames++;
2072 if (zebra_begin_read (zh))
2075 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2076 output_setname, sort_sequence, &sort_status);