1 /* $Id: zebraapi.c,v 1.120.2.7 2005-05-18 12:20:34 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 #define LOG_API LOG_APP2
48 static Res zebra_open_res (ZebraHandle zh);
49 static void zebra_close_res (ZebraHandle zh);
52 static void zebra_chdir (ZebraService zs)
56 yaz_log(LOG_API,"zebra_chdir");
57 dir = res_get (zs->global_res, "chdir");
60 logf (LOG_DEBUG, "chdir %s", dir);
68 static void zebra_flush_reg (ZebraHandle zh)
71 yaz_log(LOG_API,"zebra_flush_reg");
73 zebraExplain_flush (zh->reg->zei, zh);
75 extract_flushWriteKeys (zh,1 /* final */);
76 zebra_index_merge (zh );
79 static struct zebra_register *zebra_register_open (ZebraService zs,
81 int rw, int useshadow,
83 const char *reg_path);
84 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
86 ZebraHandle zebra_open (ZebraService zs)
89 const char *default_encoding;
95 zh = (ZebraHandle) xmalloc (sizeof(*zh));
96 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
99 zh->reg = 0; /* no register attached yet */
107 zh->reg_name = xstrdup ("");
109 zh->num_basenames = 0;
118 zh->shadow_enable = 1;
120 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
121 zh->record_encoding = xstrdup (default_encoding);
124 yaz_iconv_open ("UTF-8", default_encoding);
125 if (zh->iconv_to_utf8 == 0)
126 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
128 zh->iconv_from_utf8 =
129 yaz_iconv_open (default_encoding, "UTF-8");
130 if (zh->iconv_to_utf8 == 0)
131 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
134 zebra_mutex_cond_lock (&zs->session_lock);
136 zh->next = zs->sessions;
139 zebra_mutex_cond_unlock (&zs->session_lock);
144 ZebraService zebra_start (const char *configName)
146 return zebra_start_res(configName, 0, 0);
149 ZebraService zebra_start_res (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 xfree (zh->user_perm);
512 zh->service=0; /* more likely to trigger an assert */
513 xfree (zh->path_reg);
518 struct map_baseinfo {
524 char **new_basenames;
528 static Res zebra_open_res (ZebraHandle zh)
537 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
538 res = res_open (fname, zh->service->global_res, 0);
540 res = zh->service->global_res;
542 else if (*zh->reg_name == 0)
544 res = zh->service->global_res;
548 yaz_log (LOG_WARN, "no register root specified");
549 return 0; /* no path for register - fail! */
554 static void zebra_close_res (ZebraHandle zh)
558 if (zh->res != zh->service->global_res)
563 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
567 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
571 assert (zh->reg == 0);
572 assert (*zh->reg_name == 0);
578 resultSetInvalidate (zh);
579 zebra_register_close (zh->service, zh->reg);
584 xfree (zh->reg_name);
585 zh->reg_name = xstrdup (new_reg);
587 xfree (zh->path_reg);
589 if (zh->service->path_root)
591 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
592 strlen(zh->reg_name) + 3);
593 strcpy (zh->path_reg, zh->service->path_root);
596 strcat (zh->path_reg, "/");
597 strcat (zh->path_reg, zh->reg_name);
600 zh->res = zebra_open_res (zh);
603 zebra_lock_destroy (zh->lock_normal);
607 zebra_lock_destroy (zh->lock_shadow);
613 const char *lock_area =res_get (zh->res, "lockDir");
615 if (!lock_area && zh->path_reg)
616 res_set (zh->res, "lockDir", zh->path_reg);
617 sprintf (fname, "norm.%s.LCK", zh->reg_name);
619 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
621 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
623 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
625 if (!zh->lock_normal || !zh->lock_shadow)
629 zebra_lock_destroy(zh->lock_normal);
634 zebra_lock_destroy(zh->lock_shadow);
645 void map_basenames_func (void *vp, const char *name, const char *value)
647 struct map_baseinfo *p = (struct map_baseinfo *) vp;
649 char fromdb[128], todb[8][128];
652 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
653 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
654 todb[5], todb[6], todb[7]);
658 for (i = 0; i<p->num_bases; i++)
659 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
662 for (i = 0; i < no; i++)
664 if (p->new_num_bases == p->new_num_max)
666 p->new_basenames[(p->new_num_bases)++] =
667 nmem_strdup (p->mem, todb[i]);
673 void map_basenames (ZebraHandle zh, ODR stream,
674 int *num_bases, char ***basenames)
676 struct map_baseinfo info;
677 struct map_baseinfo *p = &info;
680 yaz_log(LOG_API,"map_basenames ");
684 info.num_bases = *num_bases;
685 info.basenames = *basenames;
686 info.new_num_max = 128;
687 info.new_num_bases = 0;
688 info.new_basenames = (char **)
689 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
690 info.mem = stream->mem;
692 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
694 for (i = 0; i<p->num_bases; i++)
695 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
697 p->new_basenames[(p->new_num_bases)++] =
698 nmem_strdup (p->mem, p->basenames[i]);
700 *num_bases = info.new_num_bases;
701 *basenames = info.new_basenames;
702 for (i = 0; i<*num_bases; i++)
703 logf (LOG_LOG, "base %s", (*basenames)[i]);
706 int zebra_select_default_database(ZebraHandle zh)
710 /* no database has been selected - so we select based on
711 resource setting (including group)
713 const char *group = res_get(zh->service->global_res, "group");
714 const char *v = res_get_prefix(zh->service->global_res,
715 "database", group, "Default");
716 return zebra_select_database(zh, v);
721 int zebra_select_database (ZebraHandle zh, const char *basename)
724 yaz_log(LOG_API,"zebra_select_database %s",basename);
726 return zebra_select_databases (zh, 1, &basename);
729 int zebra_select_databases (ZebraHandle zh, int num_bases,
730 const char **basenames)
737 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
738 num_bases,basenames[0]);
746 for (i = 0; i < zh->num_basenames; i++)
747 xfree (zh->basenames[i]);
748 xfree (zh->basenames);
750 zh->num_basenames = num_bases;
751 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
752 for (i = 0; i < zh->num_basenames; i++)
753 zh->basenames[i] = xstrdup (basenames[i]);
755 cp = strrchr(basenames[0], '/');
758 len = cp - basenames[0];
759 new_reg = xmalloc (len + 1);
760 memcpy (new_reg, basenames[0], len);
764 new_reg = xstrdup ("");
765 for (i = 1; i<num_bases; i++)
769 cp1 = strrchr (basenames[i], '/');
777 if (len != cp1 - basenames[i] ||
778 memcmp (basenames[i], new_reg, len))
793 zebra_select_register (zh, new_reg);
800 if (!zh->lock_normal || !zh->lock_shadow)
808 int zebra_search_RPN (ZebraHandle zh, ODR o,
809 Z_RPNQuery *query, const char *setname, int *hits)
812 yaz_log(LOG_API,"zebra_search_rpn");
817 if (zebra_begin_read (zh))
820 zebra_livcode_transform(zh, query);
822 resultSetAddRPN (zh, odr_extract_mem(o), query,
823 zh->num_basenames, zh->basenames, setname);
831 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
832 const char *setname, Z_RecordComposition *comp,
833 oid_value input_format, int num_recs,
834 ZebraRetrievalRecord *recs)
837 int i, *pos_array, ret = 0;
839 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
845 zh->errString = odr_strdup (stream, setname);
851 if (zebra_begin_read (zh))
854 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
855 for (i = 0; i<num_recs; i++)
856 pos_array[i] = recs[i].position;
857 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
860 logf (LOG_DEBUG, "zebraPosSetCreate error");
862 zh->errString = nmem_strdup (stream->mem, setname);
867 for (i = 0; i<num_recs; i++)
872 recs[i].format = VAL_SUTRS;
873 recs[i].len = strlen(poset[i].term);
874 recs[i].buf = poset[i].term;
875 recs[i].base = poset[i].db;
877 else if (poset[i].sysno)
880 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
881 stream, input_format, comp,
882 &recs[i].format, &recs[i].buf,
885 recs[i].errString = NULL;
891 sprintf (num_str, "%d", pos_array[i]);
893 zh->errString = odr_strdup (stream, num_str);
898 zebraPosSetDestroy (zh, poset, num_recs);
905 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
906 oid_value attributeset,
907 int *position, int *num_entries, ZebraScanEntry **entries,
911 yaz_log(LOG_API,"zebra_scan");
913 if (zebra_begin_read (zh))
919 rpn_scan (zh, stream, zapt, attributeset,
920 zh->num_basenames, zh->basenames, position,
921 num_entries, entries, is_partial, 0, 0);
926 int zebra_sort (ZebraHandle zh, ODR stream,
927 int num_input_setnames, const char **input_setnames,
928 const char *output_setname, Z_SortKeySpecList *sort_sequence,
932 yaz_log(LOG_API,"zebra_sort");
934 if (zebra_begin_read (zh))
936 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
937 output_setname, sort_sequence, sort_status);
942 int zebra_deleleResultSet(ZebraHandle zh, int function,
943 int num_setnames, char **setnames,
948 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
950 if (zebra_begin_read(zh))
951 return Z_DeleteStatus_systemProblemAtTarget;
954 case Z_DeleteResultSetRequest_list:
955 resultSetDestroy (zh, num_setnames, setnames, statuses);
957 case Z_DeleteResultSetRequest_all:
958 resultSetDestroy (zh, -1, 0, statuses);
962 status = Z_DeleteStatus_success;
963 for (i = 0; i<num_setnames; i++)
964 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
965 status = statuses[i];
969 int zebra_errCode (ZebraHandle zh)
973 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
976 yaz_log(LOG_API,"zebra_errCode: o");
980 const char *zebra_errString (ZebraHandle zh)
984 e= diagbib1_str (zh->errCode);
985 yaz_log(LOG_API,"zebra_errString: %s",e);
989 char *zebra_errAdd (ZebraHandle zh)
994 yaz_log(LOG_API,"zebra_errAdd: %s",a);
998 void zebra_clearError(ZebraHandle zh)
1007 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
1017 sprintf(u, "perm.%.30s", user ? user : "anonymous");
1018 p = res_get(zs->global_res, u);
1019 xfree (zh->user_perm);
1020 zh->user_perm = xstrdup(p ? p : "r");
1022 yaz_log(LOG_LOG, "User perm for %s: %s", u, zh->user_perm);
1024 /* users that don't require a password .. */
1025 if (zh->user_perm && strchr(zh->user_perm, 'a'))
1028 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
1033 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
1034 const char *record_type)
1037 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
1038 database, record_type);
1040 if (zebra_select_database(zh, database))
1042 if (zebra_begin_trans (zh, 1))
1047 int zebra_admin_import_end (ZebraHandle zh)
1050 yaz_log(LOG_API,"zebra_admin_import_end");
1052 zebra_end_trans (zh);
1056 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1061 yaz_log(LOG_API,"zebra_admin_import_segment");
1063 for (i = 0; i<segment->num_segmentRecords; i++)
1065 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1067 printf ("--------------%d--------------------\n", i);
1068 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1070 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1071 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1073 Odr_oct *oct = fragment->u.notExternallyTagged;
1074 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1079 zebra_update_record(zh,
1080 0, /* record Type */
1092 int zebra_admin_exchange_record (ZebraHandle zh,
1093 const char *rec_buf,
1095 const char *recid_buf, size_t recid_len,
1097 /* 1 = insert. Fail it already exists */
1098 /* 2 = replace. Fail it does not exist */
1099 /* 3 = delete. Fail if does not exist */
1100 /* 4 = update. Insert/replace */
1106 yaz_log(LOG_API,"zebra_admin_exchange_record ac=%d", action);
1109 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1111 memcpy (recid_z, recid_buf, recid_len);
1112 recid_z[recid_len] = 0;
1114 if (zebra_begin_trans(zh, 1))
1117 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1120 if (action == 1) /* fail if insert */
1122 zebra_end_trans(zh);
1126 memcpy (&sysno, rinfo+1, sizeof(sysno));
1130 if (action == 2 || action == 3) /* fail if delete or update */
1132 zebra_end_trans(zh);
1135 action = 1; /* make it an insert (if it's an update).. */
1137 buffer_extract_record (zh, rec_buf, rec_len,
1138 action == 3 ? 1 : 0 /* delete flag */,
1144 0, /* force update */
1145 1 /* allow update */
1149 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1151 else if (action == 3)
1153 dict_delete (zh->reg->matchDict, recid_z);
1155 zebra_end_trans(zh);
1159 int delete_w_handle(const char *info, void *handle)
1161 ZebraHandle zh = (ZebraHandle) handle;
1164 if (*info == sizeof(pos))
1166 memcpy (&pos, info+1, sizeof(pos));
1167 isamb_unlink(zh->reg->isamb, pos);
1172 static int delete_SU_handle(void *handle, int ord)
1174 ZebraHandle zh = (ZebraHandle) handle;
1178 ord_len = key_SU_encode (ord, ord_buf);
1179 ord_buf[ord_len] = '\0';
1181 assert (zh->reg->isamb);
1182 dict_delete_subtree(zh->reg->dict, ord_buf,
1183 zh, delete_w_handle);
1187 int zebra_drop_database (ZebraHandle zh, const char *database)
1191 yaz_log(LOG_API,"zebra_drop_database");
1194 if (zebra_select_database (zh, database))
1196 if (zebra_begin_trans (zh, 1))
1200 zebraExplain_curDatabase (zh->reg->zei, database);
1202 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1203 zebraExplain_removeDatabase(zh->reg->zei, zh);
1207 yaz_log(LOG_WARN, "drop database only supported for isam:b");
1210 zebra_end_trans (zh);
1214 int zebra_create_database (ZebraHandle zh, const char *database)
1217 yaz_log(LOG_API,"zebra_create_database");
1220 if (zebra_select_database (zh, database))
1222 if (zebra_begin_trans (zh, 1))
1225 /* announce database */
1226 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1227 /* explainDatabase */))
1229 zebra_end_trans (zh);
1231 zh->errString = "database already exist";
1234 zebra_end_trans (zh);
1238 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1239 const char *input_str, int input_len,
1240 char *output_str, int output_len)
1244 yaz_log(LOG_API,"zebra_string_norm ");
1246 if (!zh->reg->zebra_maps)
1248 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1249 input_str, input_len);
1252 if (wrbuf_len(wrbuf) >= output_len)
1254 if (wrbuf_len(wrbuf))
1255 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1256 output_str[wrbuf_len(wrbuf)] = '\0';
1257 return wrbuf_len(wrbuf);
1261 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1263 char state_fname[256];
1268 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1271 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1272 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1273 f = fopen (fname, "w");
1275 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1276 fprintf (f, "%c %d %ld\n", val, seqno, p);
1282 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1284 char state_fname[256];
1289 yaz_log(LOG_API,"zebra_get_state ");
1291 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1292 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1293 f = fopen (fname, "r");
1299 fscanf (f, "%c %d", val, seqno);
1306 int zebra_begin_read (ZebraHandle zh)
1308 return zebra_begin_trans(zh, 0);
1311 int zebra_end_read (ZebraHandle zh)
1313 return zebra_end_trans(zh);
1316 static void read_res_for_transaction(ZebraHandle zh)
1318 const char *group = res_get(zh->res, "group");
1321 zh->m_group = group;
1322 v = res_get_prefix(zh->res, "followLinks", group, "1");
1323 zh->m_follow_links = atoi(v);
1325 zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1326 zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1328 v = res_get_prefix(zh->res, "storeKeys", group, "1");
1329 zh->m_store_keys = atoi(v);
1331 v = res_get_prefix(zh->res, "storeData", group, "1");
1332 zh->m_store_data = atoi(v);
1334 v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1335 zh->m_explain_database = atoi(v);
1337 v = res_get_prefix(zh->res, "openRW", group, "1");
1338 zh->m_flag_rw = atoi(v);
1340 v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
1341 zh->m_file_verbose_limit = atoi(v);
1344 int zebra_begin_trans (ZebraHandle zh, int rw)
1346 zebra_select_default_database(zh);
1350 zh->errString = "zebra_begin_trans: no database selected";
1354 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1358 if (rw && !strchr(zh->user_perm, 'w'))
1372 const char *rval = 0;
1377 read_res_for_transaction(zh);
1380 if (zh->trans_no != 1)
1383 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1388 resultSetInvalidate (zh);
1389 zebra_register_close (zh->service, zh->reg);
1391 zh->trans_w_no = zh->trans_no;
1395 zh->records_inserted = 0;
1396 zh->records_updated = 0;
1397 zh->records_deleted = 0;
1398 zh->records_processed = 0;
1400 #if HAVE_SYS_TIMES_H
1404 if (zh->shadow_enable)
1405 rval = res_get (zh->res, "shadow");
1407 for (pass = 0; pass < 2; pass++)
1411 zebra_lock_r (zh->lock_normal);
1412 zebra_lock_w (zh->lock_shadow);
1416 zebra_lock_w (zh->lock_normal);
1417 zebra_lock_w (zh->lock_shadow);
1420 zebra_get_state (zh, &val, &seqno);
1423 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1424 zebra_unlock (zh->lock_shadow);
1425 zebra_unlock (zh->lock_normal);
1429 else if (val == 'd')
1433 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1435 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1436 bf_commitClean (bfs, rval);
1441 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1448 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1452 zebra_set_state (zh, 'd', seqno);
1454 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1455 1, rval ? 1 : 0, zh->res,
1458 zh->reg->seqno = seqno;
1461 zebra_set_state (zh, 'o', seqno);
1463 zebra_unlock (zh->lock_shadow);
1464 zebra_unlock (zh->lock_normal);
1470 zh->errString = "zebra_begin_trans: cannot open register";
1471 yaz_log(LOG_FATAL, zh->errString);
1483 if (zh->trans_no != 1)
1485 zebra_flush_reg (zh);
1489 #if HAVE_SYS_TIMES_H
1498 if (!zh->lock_normal || !zh->lock_shadow)
1504 zebra_get_state (zh, &val, &seqno);
1510 else if (seqno != zh->reg->seqno)
1512 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1513 seqno, zh->reg->seqno);
1516 else if (zh->reg->last_val != val)
1518 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1519 val, zh->reg->last_val);
1526 zebra_lock_r (zh->lock_shadow);
1528 zebra_lock_r (zh->lock_normal);
1532 resultSetInvalidate (zh);
1533 zebra_register_close (zh->service, zh->reg);
1535 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1536 0, val == 'c' ? 1 : 0,
1537 zh->res, zh->path_reg);
1540 zebra_unlock (zh->lock_normal);
1541 zebra_unlock (zh->lock_shadow);
1546 zh->reg->last_val = val;
1547 zh->reg->seqno = seqno;
1549 read_res_for_transaction(zh);
1553 int zebra_end_trans (ZebraHandle zh)
1555 ZebraTransactionStatus dummy;
1556 yaz_log(LOG_API,"zebra_end_trans");
1557 return zebra_end_transaction(zh, &dummy);
1560 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1567 yaz_log(LOG_API,"zebra_end_transaction");
1569 status->processed = 0;
1570 status->inserted = 0;
1571 status->updated = 0;
1572 status->deleted = 0;
1576 if (!zh->res || !zh->reg)
1579 zh->errString = "zebra_end_trans: no open transaction";
1582 if (zh->trans_no != zh->trans_w_no)
1585 if (zh->trans_no != 0)
1588 /* release read lock */
1590 zebra_unlock (zh->lock_normal);
1591 zebra_unlock (zh->lock_shadow);
1594 { /* release write lock */
1598 yaz_log (LOG_LOG, "zebra_end_trans");
1599 rval = res_get (zh->res, "shadow");
1601 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1603 zebra_flush_reg (zh);
1605 resultSetInvalidate (zh);
1607 zebra_register_close (zh->service, zh->reg);
1610 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1611 zh->records_processed, zh->records_inserted,
1612 zh->records_updated, zh->records_deleted);
1614 status->processed = zh->records_processed;
1615 status->inserted = zh->records_inserted;
1616 status->updated = zh->records_updated;
1617 status->deleted = zh->records_deleted;
1619 zebra_get_state (zh, &val, &seqno);
1622 BFiles bfs = bfs_create (rval, zh->path_reg);
1623 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1624 bf_commitClean (bfs, rval);
1629 zebra_set_state (zh, 'o', seqno);
1631 zebra_unlock (zh->lock_shadow);
1632 zebra_unlock (zh->lock_normal);
1635 #if HAVE_SYS_TIMES_H
1637 logf (LOG_LOG, "user/system: %ld/%ld",
1638 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1639 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1641 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1642 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1647 int zebra_repository_update (ZebraHandle zh, const char *path)
1651 logf (LOG_LOG|LOG_API, "updating %s", path);
1652 repositoryUpdate (zh, path);
1656 int zebra_repository_delete (ZebraHandle zh, const char *path)
1660 logf (LOG_LOG|LOG_API, "deleting %s", path);
1661 repositoryDelete (zh, path);
1665 int zebra_repository_show (ZebraHandle zh, const char *path)
1668 yaz_log(LOG_API,"zebra_repository_show");
1670 repositoryShow (zh, path);
1674 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1683 zebra_select_default_database(zh);
1689 rval = res_get (zh->res, "shadow");
1692 logf (LOG_WARN, "Cannot perform commit");
1693 logf (LOG_WARN, "No shadow area defined");
1697 zebra_lock_w (zh->lock_normal);
1698 zebra_lock_r (zh->lock_shadow);
1700 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1702 zebra_get_state (zh, &val, &seqno);
1705 bf_cache (bfs, rval);
1706 if (bf_commitExists (bfs))
1709 zebra_set_state (zh, 'd', seqno);
1712 zebra_set_state (zh, 'c', seqno);
1714 logf (LOG_LOG, "commit start");
1715 bf_commitExec (bfs);
1720 logf (LOG_LOG, "commit clean");
1721 bf_commitClean (bfs, rval);
1723 zebra_set_state (zh, 'o', seqno);
1727 logf (LOG_LOG, "nothing to commit");
1731 zebra_unlock (zh->lock_shadow);
1732 zebra_unlock (zh->lock_normal);
1736 int zebra_clean (ZebraHandle zh)
1738 yaz_log(LOG_API,"zebra_clean");
1739 return zebra_commit_ex(zh, 1);
1742 int zebra_commit (ZebraHandle zh)
1744 yaz_log(LOG_API,"zebra_commit");
1745 return zebra_commit_ex(zh, 0);
1748 int zebra_init (ZebraHandle zh)
1753 yaz_log(LOG_API,"zebra_init");
1756 zebra_select_default_database(zh);
1762 rval = res_get (zh->res, "shadow");
1764 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1768 bf_cache (bfs, rval);
1772 zebra_set_state (zh, 'o', 0);
1776 int zebra_compact (ZebraHandle zh)
1780 yaz_log(LOG_API,"zebra_compact");
1787 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1793 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1796 yaz_log(LOG_API,"zebra_result");
1797 *code = zh->errCode;
1798 *addinfo = zh->errString;
1802 int zebra_shadow_enable (ZebraHandle zh, int value)
1805 yaz_log(LOG_API,"zebra_shadow_enable");
1807 zh->shadow_enable = value;
1811 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1814 yaz_log(LOG_API,"zebra_record_encoding");
1816 xfree (zh->record_encoding);
1820 * Something about charset aliases. Oleg???
1823 if (zh->iconv_to_utf8 != 0)
1824 yaz_iconv_close(zh->iconv_to_utf8);
1825 if (zh->iconv_from_utf8 != 0)
1826 yaz_iconv_close(zh->iconv_from_utf8);
1828 zh->record_encoding = xstrdup (encoding);
1830 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1833 yaz_iconv_open ("UTF-8", encoding);
1834 if (zh->iconv_to_utf8 == 0)
1835 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1836 zh->iconv_from_utf8 =
1837 yaz_iconv_open (encoding, "UTF-8");
1838 if (zh->iconv_to_utf8 == 0)
1839 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1844 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1847 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1849 res_set(zh->res, name, value);
1853 const char *zebra_get_resource(ZebraHandle zh,
1854 const char *name, const char *defaultvalue)
1858 v= res_get_def( zh->res, name, (char *)defaultvalue);
1860 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1864 /* moved from zebra_api_ext.c by pop */
1865 /* FIXME: Should this really be public??? -Heikki */
1867 int zebra_trans_no (ZebraHandle zh)
1870 yaz_log(LOG_API,"zebra_trans_no");
1871 return zh->trans_no;
1874 int zebra_get_shadow_enable (ZebraHandle zh)
1876 yaz_log(LOG_API,"zebra_get_shadow_enable");
1877 return (zh->shadow_enable);
1880 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1882 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1883 zh->shadow_enable = value;
1887 /* almost the same as zebra_records_retrieve ... but how did it work?
1888 I mean for multiple records ??? CHECK ??? */
1889 void api_records_retrieve (ZebraHandle zh, ODR stream,
1890 const char *setname, Z_RecordComposition *comp,
1891 oid_value input_format, int num_recs,
1892 ZebraRetrievalRecord *recs)
1896 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1901 zh->errString = odr_strdup (stream, setname);
1907 if (zebra_begin_read (zh))
1910 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1911 for (i = 0; i<num_recs; i++)
1912 pos_array[i] = recs[i].position;
1913 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1916 logf (LOG_DEBUG, "zebraPosSetCreate error");
1918 zh->errString = nmem_strdup (stream->mem, setname);
1922 for (i = 0; i<num_recs; i++)
1926 recs[i].errCode = 0;
1927 recs[i].format = VAL_SUTRS;
1928 recs[i].len = strlen(poset[i].term);
1929 recs[i].buf = poset[i].term;
1930 recs[i].base = poset[i].db;
1934 else if (poset[i].sysno)
1936 /* changed here ??? CHECK ??? */
1939 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1940 stream, input_format, comp,
1945 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1946 memcpy(recs[i].buf, b, recs[i].len);
1947 recs[i].errString = 0; /* Hmmm !!! we should get this */
1948 recs[i].sysno = poset[i].sysno;
1949 recs[i].score = poset[i].score;
1955 sprintf (num_str, "%d", pos_array[i]);
1957 zh->errString = odr_strdup (stream, num_str);
1962 zebraPosSetDestroy (zh, poset, num_recs);
1964 zebra_end_read (zh);
1969 /* ---------------------------------------------------------------------------
1970 Record insert(=update), delete
1972 If sysno is provided, then it's used to identify the record.
1973 If not, and match_criteria is provided, then sysno is guessed
1974 If not, and a record is provided, then sysno is got from there
1975 NOTE: Now returns 0 at success and updates sysno, which is an int*
1979 int zebra_add_record(ZebraHandle zh,
1980 const char *buf, int buf_size)
1983 return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
1986 int zebra_insert_record (ZebraHandle zh,
1987 const char *recordType,
1988 int *sysno, const char *match, const char *fname,
1989 const char *buf, int buf_size, int force_update)
1992 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
1994 if (buf_size < 1) buf_size = strlen(buf);
1996 if (zebra_begin_trans(zh, 1))
1998 res = buffer_extract_record (zh, buf, buf_size,
1999 0, /* delete_flag */
2005 0); /* allow_update */
2006 zebra_end_trans(zh);
2010 int zebra_update_record (ZebraHandle zh,
2011 const char *recordType,
2012 int* sysno, const char *match, const char *fname,
2013 const char *buf, int buf_size,
2018 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
2020 if (buf_size < 1) buf_size = strlen(buf);
2022 if (zebra_begin_trans(zh, 1))
2024 res = buffer_extract_record (zh, buf, buf_size,
2025 0, /* delete_flag */
2031 1); /* allow_update */
2032 yaz_log(LOG_LOG, "zebra_update_record returned res=%d", res);
2033 zebra_end_trans(zh);
2037 int zebra_delete_record (ZebraHandle zh,
2038 const char *recordType,
2039 int *sysno, const char *match, const char *fname,
2040 const char *buf, int buf_size,
2044 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2046 if (buf_size < 1) buf_size = strlen(buf);
2048 if (zebra_begin_trans(zh, 1))
2050 res = buffer_extract_record (zh, buf, buf_size,
2051 1, /* delete_flag */
2057 1); /* allow_update */
2058 zebra_end_trans(zh);
2062 /* ---------------------------------------------------------------------------
2066 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2067 const char *setname, int *numhits)
2072 ODR odr = odr_createmem(ODR_ENCODE);
2074 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2076 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2079 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2081 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2085 yaz_log(LOG_API,"Hits: %d",hits);
2093 /* ---------------------------------------------------------------------------
2094 Sort - a simplified interface, with optional read locks.
2095 FIXME - This is a horrible name, will conflict with half the applications
2097 int zebra_sort_by_specstr (ZebraHandle zh,
2099 const char *sort_spec,
2100 const char *output_setname,
2101 const char **input_setnames)
2103 int num_input_setnames = 0;
2104 int sort_status = 0;
2105 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2106 yaz_log(LOG_API,"sort (FIXME) ");
2109 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2114 /* we can do this, since the perl typemap code for char** will
2115 put a NULL at the end of list */
2116 while (input_setnames[num_input_setnames]) num_input_setnames++;
2118 if (zebra_begin_read (zh))
2121 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2122 output_setname, sort_sequence, &sort_status);