1 /* $Id: zebraapi.c,v 1.109 2003-06-30 19:37:12 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
35 #include <yaz/diagbib1.h>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
42 /* simple asserts to validate the most essential input args */
43 #define ASSERTZH assert(zh && zh->service)
44 #define ASSERTZHRES assert(zh && zh->service && zh->res)
45 #define ASSERTZS assert(zs)
47 /* A simple log macro */
48 /* don't break with older yazen that lack log_app2 */
50 #define LOG_API LOG_APP2
52 #define LOG_API LOG_DEBUG
55 static Res zebra_open_res (ZebraHandle zh);
56 static void zebra_close_res (ZebraHandle zh);
59 static void zebra_chdir (ZebraService zs)
63 yaz_log(LOG_API,"zebra_chdir");
64 dir = res_get (zs->global_res, "chdir");
67 logf (LOG_DEBUG, "chdir %s", dir);
75 static void zebra_flush_reg (ZebraHandle zh)
78 yaz_log(LOG_API,"zebra_flush_reg");
80 zebraExplain_flush (zh->reg->zei, zh);
82 extract_flushWriteKeys (zh);
83 zebra_index_merge (zh);
86 static struct zebra_register *zebra_register_open (ZebraService zs,
88 int rw, int useshadow,
90 const char *reg_path);
91 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
93 ZebraHandle zebra_open (ZebraService zs)
96 const char *default_encoding;
98 yaz_log(LOG_API,"zebra_open");
103 zh = (ZebraHandle) xmalloc (sizeof(*zh));
104 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
107 zh->reg = 0; /* no register attached yet */
114 zh->reg_name = xstrdup ("");
116 zh->num_basenames = 0;
125 zh->admin_databaseName = 0;
127 zh->shadow_enable = 1;
129 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
130 zh->record_encoding = xstrdup (default_encoding);
133 yaz_iconv_open ("UTF-8", default_encoding);
134 if (zh->iconv_to_utf8 == 0)
135 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
137 zh->iconv_from_utf8 =
138 yaz_iconv_open (default_encoding, "UTF-8");
139 if (zh->iconv_to_utf8 == 0)
140 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
143 zebra_mutex_cond_lock (&zs->session_lock);
145 zh->next = zs->sessions;
148 zebra_mutex_cond_unlock (&zs->session_lock);
153 ZebraService zebra_start (const char *configName)
157 yaz_log(LOG_API|LOG_LOG,"zebra_start %s",configName);
159 if ((res = res_open (configName, 0)))
161 ZebraService zh = xmalloc (sizeof(*zh));
163 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
165 zh->global_res = res;
166 zh->configName = xstrdup(configName);
171 zebra_mutex_cond_init (&zh->session_lock);
172 if (!res_get (zh->global_res, "passwd"))
173 zh->passwd_db = NULL;
176 zh->passwd_db = passwd_db_open ();
178 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
180 passwd_db_file (zh->passwd_db,
181 res_get (zh->global_res, "passwd"));
183 zh->path_root = res_get (zh->global_res, "root");
190 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
191 int rw, int useshadow, Res res,
192 const char *reg_path)
194 struct zebra_register *reg;
195 int record_compression = REC_COMPRESS_NONE;
196 char *recordCompression = 0;
200 reg = xmalloc (sizeof(*reg));
203 reg->name = xstrdup (name);
210 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
211 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
213 reg->dh = data1_createx (DATA1_FLAG_XML);
216 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
219 data1_destroy(reg->dh);
223 bf_cache (reg->bfs, res_get (res, "shadow"));
224 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
225 DEFAULT_PROFILE_PATH));
226 data1_set_tabroot (reg->dh, reg_path);
227 reg->recTypes = recTypes_init (reg->dh);
228 recTypes_default_handlers (reg->recTypes);
230 reg->zebra_maps = zebra_maps_open (res, reg_path);
231 reg->rank_classes = NULL;
235 reg->keys.buf_max = 0;
237 reg->sortKeys.buf = 0;
238 reg->sortKeys.buf_max = 0;
251 reg->key_file_no = 0;
253 zebraRankInstall (reg, rank1_class);
254 zebraRankInstall (reg, rankzv_class);
255 zebraRankInstall (reg, rankliv_class);
257 recordCompression = res_get_def (res, "recordCompression", "none");
258 if (!strcmp (recordCompression, "none"))
259 record_compression = REC_COMPRESS_NONE;
260 if (!strcmp (recordCompression, "bzip2"))
261 record_compression = REC_COMPRESS_BZIP2;
263 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
265 logf (LOG_WARN, "rec_open");
270 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
272 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
274 logf (LOG_WARN, "dict_open");
277 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
279 logf (LOG_WARN, "sortIdx_open");
282 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
284 struct ISAMS_M_s isams_m;
285 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
286 key_isams_m(res, &isams_m))))
288 logf (LOG_WARN, "isams_open");
292 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
294 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
295 sizeof (struct it_key), res)))
297 logf (LOG_WARN, "is_open");
301 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
303 struct ISAMC_M_s isamc_m;
304 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
305 rw, key_isamc_m(res, &isamc_m))))
307 logf (LOG_WARN, "isc_open");
311 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
313 struct ISAMD_M_s isamd_m;
315 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
316 rw, key_isamd_m(res, &isamd_m))))
318 logf (LOG_WARN, "isamd_open");
322 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
324 struct ISAMC_M_s isamc_m;
326 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
327 rw, key_isamc_m(res, &isamc_m), 0)))
329 logf (LOG_WARN, "isamb_open");
333 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
335 struct ISAMC_M_s isamc_m;
337 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
338 rw, key_isamc_m(res, &isamc_m), 1)))
340 logf (LOG_WARN, "isamb_open");
344 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
346 struct ISAMC_M_s isamc_m;
348 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
349 rw, key_isamc_m(res, &isamc_m), -1)))
351 logf (LOG_WARN, "isamb_open");
355 reg->zei = zebraExplain_open (reg->records, reg->dh,
360 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
364 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
368 int zebra_admin_shutdown (ZebraHandle zh)
371 yaz_log(LOG_API,"zebra_admin_shutdown");
374 zebra_mutex_cond_lock (&zh->service->session_lock);
375 zh->service->stop_flag = 1;
376 zebra_mutex_cond_unlock (&zh->service->session_lock);
380 int zebra_admin_start (ZebraHandle zh)
384 yaz_log(LOG_API,"zebra_admin_start");
387 zebra_mutex_cond_lock (&zs->session_lock);
388 zebra_mutex_cond_unlock (&zs->session_lock);
392 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
395 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
400 zebraExplain_close (reg->zei);
401 dict_close (reg->dict);
403 dict_close (reg->matchDict);
404 sortIdx_close (reg->sortIdx);
406 isams_close (reg->isams);
408 is_close (reg->isam);
410 isc_close (reg->isamc);
412 isamd_close (reg->isamd);
414 isamb_close (reg->isamb);
415 rec_close (®->records);
418 recTypes_destroy (reg->recTypes);
419 zebra_maps_close (reg->zebra_maps);
420 zebraRankDestroy (reg);
421 bfs_destroy (reg->bfs);
422 data1_destroy (reg->dh);
424 xfree (reg->sortKeys.buf);
425 xfree (reg->keys.buf);
427 xfree (reg->key_buf);
432 int zebra_stop(ZebraService zs)
436 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
440 zebra_close (zs->sessions);
443 zebra_mutex_cond_destroy (&zs->session_lock);
446 passwd_db_close (zs->passwd_db);
448 res_close (zs->global_res);
449 xfree (zs->configName);
450 xfree (zs->path_root);
455 int zebra_close (ZebraHandle zh)
458 struct zebra_session **sp;
461 yaz_log(LOG_API,"zebra_close");
468 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
469 resultSetDestroy (zh, -1, 0, 0);
472 zebra_register_close (zh->service, zh->reg);
473 zebra_close_res (zh);
475 xfree (zh->record_encoding);
477 for (i = 0; i < zh->num_basenames; i++)
478 xfree (zh->basenames[i]);
479 xfree (zh->basenames);
481 if (zh->iconv_to_utf8 != 0)
482 yaz_iconv_close (zh->iconv_to_utf8);
483 if (zh->iconv_from_utf8 != 0)
484 yaz_iconv_close (zh->iconv_from_utf8);
486 xfree (zh->admin_databaseName);
487 zebra_mutex_cond_lock (&zs->session_lock);
488 zebra_lock_destroy (zh->lock_normal);
489 zebra_lock_destroy (zh->lock_shadow);
501 zebra_mutex_cond_unlock (&zs->session_lock);
502 xfree (zh->reg_name);
503 zh->service=0; /* more likely to trigger an assert */
508 struct map_baseinfo {
514 char **new_basenames;
518 static Res zebra_open_res (ZebraHandle zh)
527 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
528 res = res_open (fname, zh->service->global_res);
530 res = zh->service->global_res;
532 else if (*zh->reg_name == 0)
534 res = zh->service->global_res;
538 yaz_log (LOG_WARN, "no register root specified");
539 return 0; /* no path for register - fail! */
544 static void zebra_close_res (ZebraHandle zh)
548 if (zh->res != zh->service->global_res)
553 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
557 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
561 assert (zh->reg == 0);
562 assert (*zh->reg_name == 0);
568 resultSetInvalidate (zh);
569 zebra_register_close (zh->service, zh->reg);
574 xfree (zh->reg_name);
575 zh->reg_name = xstrdup (new_reg);
577 xfree (zh->path_reg);
579 if (zh->service->path_root)
581 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
582 strlen(zh->reg_name) + 3);
583 strcpy (zh->path_reg, zh->service->path_root);
586 strcat (zh->path_reg, "/");
587 strcat (zh->path_reg, zh->reg_name);
590 zh->res = zebra_open_res (zh);
593 zebra_lock_destroy (zh->lock_normal);
597 zebra_lock_destroy (zh->lock_shadow);
603 const char *lock_area =res_get (zh->res, "lockDir");
605 if (!lock_area && zh->path_reg)
606 res_put (zh->res, "lockDir", zh->path_reg);
607 sprintf (fname, "norm.%s.LCK", zh->reg_name);
609 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
611 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
613 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
619 void map_basenames_func (void *vp, const char *name, const char *value)
621 struct map_baseinfo *p = (struct map_baseinfo *) vp;
623 char fromdb[128], todb[8][128];
626 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
627 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
628 todb[5], todb[6], todb[7]);
632 for (i = 0; i<p->num_bases; i++)
633 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
636 for (i = 0; i < no; i++)
638 if (p->new_num_bases == p->new_num_max)
640 p->new_basenames[(p->new_num_bases)++] =
641 nmem_strdup (p->mem, todb[i]);
647 void map_basenames (ZebraHandle zh, ODR stream,
648 int *num_bases, char ***basenames)
650 struct map_baseinfo info;
651 struct map_baseinfo *p = &info;
654 yaz_log(LOG_API,"map_basenames ");
658 info.num_bases = *num_bases;
659 info.basenames = *basenames;
660 info.new_num_max = 128;
661 info.new_num_bases = 0;
662 info.new_basenames = (char **)
663 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
664 info.mem = stream->mem;
666 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
668 for (i = 0; i<p->num_bases; i++)
669 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
671 p->new_basenames[(p->new_num_bases)++] =
672 nmem_strdup (p->mem, p->basenames[i]);
674 *num_bases = info.new_num_bases;
675 *basenames = info.new_basenames;
676 for (i = 0; i<*num_bases; i++)
677 logf (LOG_LOG, "base %s", (*basenames)[i]);
680 int zebra_select_database (ZebraHandle zh, const char *basename)
683 yaz_log(LOG_API,"zebra_select_database %s",basename);
685 return zebra_select_databases (zh, 1, &basename);
688 int zebra_select_databases (ZebraHandle zh, int num_bases,
689 const char **basenames)
696 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
697 num_bases,basenames[0]);
705 for (i = 0; i < zh->num_basenames; i++)
706 xfree (zh->basenames[i]);
707 xfree (zh->basenames);
709 zh->num_basenames = num_bases;
710 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
711 for (i = 0; i < zh->num_basenames; i++)
712 zh->basenames[i] = xstrdup (basenames[i]);
714 cp = strrchr(basenames[0], '/');
717 len = cp - basenames[0];
718 new_reg = xmalloc (len + 1);
719 memcpy (new_reg, basenames[0], len);
723 new_reg = xstrdup ("");
724 for (i = 1; i<num_bases; i++)
728 cp1 = strrchr (basenames[i], '/');
736 if (len != cp1 - basenames[i] ||
737 memcmp (basenames[i], new_reg, len))
752 zebra_select_register (zh, new_reg);
759 if (!zh->lock_normal || !zh->lock_shadow)
767 int zebra_search_RPN (ZebraHandle zh, ODR o,
768 Z_RPNQuery *query, const char *setname, int *hits)
771 yaz_log(LOG_API,"zebra_search_rpn");
776 if (zebra_begin_read (zh))
779 zebra_livcode_transform(zh, query);
781 resultSetAddRPN (zh, odr_extract_mem(o), query,
782 zh->num_basenames, zh->basenames, setname);
790 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
791 const char *setname, Z_RecordComposition *comp,
792 oid_value input_format, int num_recs,
793 ZebraRetrievalRecord *recs)
798 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
804 zh->errString = odr_strdup (stream, setname);
810 if (zebra_begin_read (zh))
813 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
814 for (i = 0; i<num_recs; i++)
815 pos_array[i] = recs[i].position;
816 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
819 logf (LOG_DEBUG, "zebraPosSetCreate error");
821 zh->errString = nmem_strdup (stream->mem, setname);
825 for (i = 0; i<num_recs; i++)
830 recs[i].format = VAL_SUTRS;
831 recs[i].len = strlen(poset[i].term);
832 recs[i].buf = poset[i].term;
833 recs[i].base = poset[i].db;
835 else if (poset[i].sysno)
838 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
839 stream, input_format, comp,
840 &recs[i].format, &recs[i].buf,
843 recs[i].errString = NULL;
849 sprintf (num_str, "%d", pos_array[i]);
851 zh->errString = odr_strdup (stream, num_str);
855 zebraPosSetDestroy (zh, poset, num_recs);
862 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
863 oid_value attributeset,
864 int *position, int *num_entries, ZebraScanEntry **entries,
868 yaz_log(LOG_API,"zebra_scan");
870 if (zebra_begin_read (zh))
876 rpn_scan (zh, stream, zapt, attributeset,
877 zh->num_basenames, zh->basenames, position,
878 num_entries, entries, is_partial, 0, 0);
883 int zebra_sort (ZebraHandle zh, ODR stream,
884 int num_input_setnames, const char **input_setnames,
885 const char *output_setname, Z_SortKeySpecList *sort_sequence,
889 yaz_log(LOG_API,"zebra_sort");
891 if (zebra_begin_read (zh))
893 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
894 output_setname, sort_sequence, sort_status);
899 int zebra_deleleResultSet(ZebraHandle zh, int function,
900 int num_setnames, char **setnames,
905 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
907 if (zebra_begin_read(zh))
908 return Z_DeleteStatus_systemProblemAtTarget;
911 case Z_DeleteRequest_list:
912 resultSetDestroy (zh, num_setnames, setnames, statuses);
914 case Z_DeleteRequest_all:
915 resultSetDestroy (zh, -1, 0, statuses);
919 status = Z_DeleteStatus_success;
920 for (i = 0; i<num_setnames; i++)
921 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
922 status = statuses[i];
926 int zebra_errCode (ZebraHandle zh)
930 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
933 yaz_log(LOG_API,"zebra_errCode: o");
937 const char *zebra_errString (ZebraHandle zh)
941 e= diagbib1_str (zh->errCode);
942 yaz_log(LOG_API,"zebra_errString: %s",e);
946 char *zebra_errAdd (ZebraHandle zh)
951 yaz_log(LOG_API,"zebra_errAdd: %s",a);
955 void zebra_clearError(ZebraHandle zh)
964 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
968 yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
971 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
973 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
977 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
981 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
982 const char *record_type)
985 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
986 database, record_type);
988 if (zebra_select_database(zh, database))
990 zebra_begin_trans (zh, 1);
991 xfree (zh->admin_databaseName);
992 zh->admin_databaseName = xstrdup(database);
996 int zebra_admin_import_end (ZebraHandle zh)
999 yaz_log(LOG_API,"zebra_admin_import_end");
1001 zebra_end_trans (zh);
1005 int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
1010 yaz_log(LOG_API,"zebra_admin_import_segment");
1012 for (i = 0; i<segment->num_segmentRecords; i++)
1014 Z_NamePlusRecord *npr = segment->segmentRecords[i];
1015 const char *databaseName = npr->databaseName;
1018 databaseName = zh->admin_databaseName;
1019 printf ("--------------%d--------------------\n", i);
1020 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1022 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1023 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1025 Odr_oct *oct = fragment->u.notExternallyTagged;
1026 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1030 extract_rec_in_mem (zh, "grs.sgml",
1033 0 /* delete_flag */,
1038 0 /* match criteria */);
1045 int zebra_admin_exchange_record (ZebraHandle zh,
1046 const char *database,
1047 const char *rec_buf,
1049 const char *recid_buf, size_t recid_len,
1056 yaz_log(LOG_API,"zebra_admin_exchange_record db=%s ac=%d",
1060 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1062 memcpy (recid_z, recid_buf, recid_len);
1063 recid_z[recid_len] = 0;
1065 zebra_begin_trans(zh,1);
1067 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1070 if (action == 1) /* fail if insert */
1072 zebra_end_trans(zh);
1076 memcpy (&sysno, rinfo+1, sizeof(sysno));
1080 if (action == 2 || action == 3) /* fail if delete or update */
1082 zebra_end_trans(zh);
1086 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1087 action == 3 ? 1 : 0 /* delete flag */,
1088 0, &sysno, 1, 1, 0);
1091 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1093 else if (action == 3)
1095 dict_delete (zh->reg->matchDict, recid_z);
1097 zebra_end_trans(zh);
1101 int delete_w_handle(const char *info, void *handle)
1103 ZebraHandle zh = (ZebraHandle) handle;
1106 if (*info == sizeof(pos))
1108 memcpy (&pos, info+1, sizeof(pos));
1109 isamb_unlink(zh->reg->isamb, pos);
1114 static int delete_SU_handle(void *handle, int ord)
1116 ZebraHandle zh = (ZebraHandle) handle;
1120 ord_len = key_SU_encode (ord, ord_buf);
1121 ord_buf[ord_len] = '\0';
1123 assert (zh->reg->isamb);
1124 dict_delete_subtree(zh->reg->dict, ord_buf,
1125 zh, delete_w_handle);
1129 int zebra_drop_database (ZebraHandle zh, const char *database)
1132 yaz_log(LOG_API,"zebra_drop_database");
1135 if (zebra_select_database (zh, database))
1137 if (zebra_begin_trans (zh, 1))
1141 zebraExplain_curDatabase (zh->reg->zei, database);
1143 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1144 zebraExplain_removeDatabase(zh->reg->zei, zh);
1146 zebra_end_trans (zh);
1150 int zebra_create_database (ZebraHandle zh, const char *database)
1153 yaz_log(LOG_API,"zebra_create_database");
1156 if (zebra_select_database (zh, database))
1158 if (zebra_begin_trans (zh, 1))
1161 /* announce database */
1162 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1163 /* explainDatabase */))
1165 zebra_end_trans (zh);
1167 zh->errString = "database already exist";
1170 zebra_end_trans (zh);
1174 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1175 const char *input_str, int input_len,
1176 char *output_str, int output_len)
1180 yaz_log(LOG_API,"zebra_string_norm ");
1182 if (!zh->reg->zebra_maps)
1184 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1185 input_str, input_len);
1188 if (wrbuf_len(wrbuf) >= output_len)
1190 if (wrbuf_len(wrbuf))
1191 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1192 output_str[wrbuf_len(wrbuf)] = '\0';
1193 return wrbuf_len(wrbuf);
1197 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1199 char state_fname[256];
1204 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1207 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1208 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1209 f = fopen (fname, "w");
1211 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1212 fprintf (f, "%c %d %ld\n", val, seqno, p);
1218 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1220 char state_fname[256];
1225 yaz_log(LOG_API,"zebra_get_state ");
1227 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1228 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1229 f = fopen (fname, "r");
1235 fscanf (f, "%c %d", val, seqno);
1242 int zebra_begin_read (ZebraHandle zh)
1244 return zebra_begin_trans(zh, 0);
1247 int zebra_end_read (ZebraHandle zh)
1249 return zebra_end_trans(zh);
1252 int zebra_begin_trans (ZebraHandle zh, int rw)
1254 yaz_log(LOG_LOG, "zebra_begin_trans rw=%d trans=%d", rw, zh->trans_no);
1258 zh->errString = "zebra_begin_trans: no database selected";
1262 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1269 const char *rval = 0;
1274 if (zh->trans_no != 1)
1277 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1282 resultSetInvalidate (zh);
1283 zebra_register_close (zh->service, zh->reg);
1285 zh->trans_w_no = zh->trans_no;
1289 zh->records_inserted = 0;
1290 zh->records_updated = 0;
1291 zh->records_deleted = 0;
1292 zh->records_processed = 0;
1294 #if HAVE_SYS_TIMES_H
1298 if (zh->shadow_enable)
1299 rval = res_get (zh->res, "shadow");
1301 for (pass = 0; pass < 2; pass++)
1305 zebra_lock_r (zh->lock_normal);
1306 zebra_lock_w (zh->lock_shadow);
1310 zebra_lock_w (zh->lock_normal);
1311 zebra_lock_w (zh->lock_shadow);
1314 zebra_get_state (zh, &val, &seqno);
1317 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1318 zebra_unlock (zh->lock_shadow);
1319 zebra_unlock (zh->lock_normal);
1323 else if (val == 'd')
1327 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1329 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1330 bf_commitClean (bfs, rval);
1335 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1342 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1346 zebra_set_state (zh, 'd', seqno);
1348 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1349 1, rval ? 1 : 0, zh->res,
1352 zh->reg->seqno = seqno;
1355 zebra_set_state (zh, 'o', seqno);
1357 zebra_unlock (zh->lock_shadow);
1358 zebra_unlock (zh->lock_normal);
1364 zh->errString = "zebra_begin_trans: cannot open register";
1365 yaz_log(LOG_FATAL, zh->errString);
1377 if (zh->trans_no != 1)
1379 zebra_flush_reg (zh);
1383 #if HAVE_SYS_TIMES_H
1392 if (!zh->lock_normal || !zh->lock_shadow)
1398 zebra_get_state (zh, &val, &seqno);
1404 else if (seqno != zh->reg->seqno)
1406 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1407 seqno, zh->reg->seqno);
1410 else if (zh->reg->last_val != val)
1412 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1413 val, zh->reg->last_val);
1420 zebra_lock_r (zh->lock_shadow);
1422 zebra_lock_r (zh->lock_normal);
1425 zebra_register_close (zh->service, zh->reg);
1426 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1427 0, val == 'c' ? 1 : 0,
1428 zh->res, zh->path_reg);
1431 zebra_unlock (zh->lock_normal);
1432 zebra_unlock (zh->lock_shadow);
1437 zh->reg->last_val = val;
1438 zh->reg->seqno = seqno;
1443 int zebra_end_trans (ZebraHandle zh)
1445 ZebraTransactionStatus dummy;
1446 yaz_log(LOG_API,"zebra_end_trans");
1447 return zebra_end_transaction(zh, &dummy);
1450 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1457 yaz_log(LOG_API,"zebra_end_transaction");
1459 status->processed = 0;
1460 status->inserted = 0;
1461 status->updated = 0;
1462 status->deleted = 0;
1466 if (!zh->res || !zh->reg)
1469 zh->errString = "zebra_end_trans: no open transaction";
1472 if (zh->trans_no != zh->trans_w_no)
1475 if (zh->trans_no != 0)
1478 /* release read lock */
1480 zebra_unlock (zh->lock_normal);
1481 zebra_unlock (zh->lock_shadow);
1484 { /* release write lock */
1488 yaz_log (LOG_LOG, "zebra_end_trans");
1489 rval = res_get (zh->res, "shadow");
1491 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1493 zebra_flush_reg (zh);
1495 resultSetInvalidate (zh);
1497 zebra_register_close (zh->service, zh->reg);
1500 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1501 zh->records_processed, zh->records_inserted,
1502 zh->records_updated, zh->records_deleted);
1504 status->processed = zh->records_processed;
1505 status->inserted = zh->records_inserted;
1506 status->updated = zh->records_updated;
1507 status->deleted = zh->records_deleted;
1509 zebra_get_state (zh, &val, &seqno);
1512 BFiles bfs = bfs_create (rval, zh->path_reg);
1513 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1514 bf_commitClean (bfs, rval);
1519 zebra_set_state (zh, 'o', seqno);
1521 zebra_unlock (zh->lock_shadow);
1522 zebra_unlock (zh->lock_normal);
1525 #if HAVE_SYS_TIMES_H
1527 logf (LOG_LOG, "user/system: %ld/%ld",
1528 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1529 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1531 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1532 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1537 int zebra_repository_update (ZebraHandle zh)
1541 logf (LOG_LOG|LOG_API, "updating %s", zh->rGroup.path);
1542 repositoryUpdate (zh);
1546 int zebra_repository_delete (ZebraHandle zh)
1550 logf (LOG_LOG|LOG_API, "deleting %s", zh->rGroup.path);
1551 repositoryDelete (zh);
1555 int zebra_repository_show (ZebraHandle zh)
1558 yaz_log(LOG_API,"zebra_repository_show");
1560 repositoryShow (zh);
1564 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1578 rval = res_get (zh->res, "shadow");
1581 logf (LOG_WARN, "Cannot perform commit");
1582 logf (LOG_WARN, "No shadow area defined");
1586 zebra_lock_w (zh->lock_normal);
1587 zebra_lock_r (zh->lock_shadow);
1589 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1591 zebra_get_state (zh, &val, &seqno);
1594 bf_cache (bfs, rval);
1595 if (bf_commitExists (bfs))
1598 zebra_set_state (zh, 'd', seqno);
1601 zebra_set_state (zh, 'c', seqno);
1603 logf (LOG_LOG, "commit start");
1604 bf_commitExec (bfs);
1609 logf (LOG_LOG, "commit clean");
1610 bf_commitClean (bfs, rval);
1612 zebra_set_state (zh, 'o', seqno);
1616 logf (LOG_LOG, "nothing to commit");
1620 zebra_unlock (zh->lock_shadow);
1621 zebra_unlock (zh->lock_normal);
1625 int zebra_clean (ZebraHandle zh)
1627 yaz_log(LOG_API,"zebra_clean");
1628 return zebra_commit_ex(zh, 1);
1631 int zebra_commit (ZebraHandle zh)
1633 yaz_log(LOG_API,"zebra_commit");
1634 return zebra_commit_ex(zh, 0);
1637 int zebra_init (ZebraHandle zh)
1642 yaz_log(LOG_API,"zebra_init");
1650 rval = res_get (zh->res, "shadow");
1652 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1655 bf_cache (bfs, rval);
1659 zebra_set_state (zh, 'o', 0);
1663 int zebra_compact (ZebraHandle zh)
1667 yaz_log(LOG_API,"zebra_compact");
1674 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1680 int zebra_record_insert (ZebraHandle zh, const char *buf, int len, int *sysno)
1684 yaz_log(LOG_API,"zebra_record_insert");
1688 if (zebra_begin_trans (zh, 1))
1690 extract_rec_in_mem (zh, "grs.sgml",
1692 "Default", /* database */
1693 0 /* delete_flag */,
1698 0 /* match criteria */);
1699 if (zebra_end_trans (zh))
1706 int zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1709 yaz_log(LOG_API,"zebra_set_group");
1711 memcpy (&zh->rGroup, rg, sizeof(*rg));
1715 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1718 yaz_log(LOG_API,"zebra_result");
1719 *code = zh->errCode;
1720 *addinfo = zh->errString;
1724 int zebra_shadow_enable (ZebraHandle zh, int value)
1727 yaz_log(LOG_API,"zebra_shadow_enable");
1729 zh->shadow_enable = value;
1733 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1736 yaz_log(LOG_API,"zebra_record_encoding");
1738 xfree (zh->record_encoding);
1742 * Something about charset aliases. Oleg???
1745 if (zh->iconv_to_utf8 != 0)
1746 yaz_iconv_close(zh->iconv_to_utf8);
1747 if (zh->iconv_from_utf8 != 0)
1748 yaz_iconv_close(zh->iconv_from_utf8);
1750 zh->record_encoding = xstrdup (encoding);
1752 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1755 yaz_iconv_open ("UTF-8", encoding);
1756 if (zh->iconv_to_utf8 == 0)
1757 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1758 zh->iconv_from_utf8 =
1759 yaz_iconv_open (encoding, "UTF-8");
1760 if (zh->iconv_to_utf8 == 0)
1761 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1766 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1769 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1771 res_put(zh->res, name, value);
1775 const char *zebra_get_resource(ZebraHandle zh,
1776 const char *name, const char *defaultvalue)
1780 v= res_get_def( zh->res, name, (char *)defaultvalue);
1782 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1786 /* moved from zebra_api_ext.c by pop */
1787 /* FIXME: Should this really be public??? -Heikki */
1789 int zebra_trans_no (ZebraHandle zh)
1792 yaz_log(LOG_API,"zebra_trans_no");
1793 return zh->trans_no;
1796 int zebra_get_shadow_enable (ZebraHandle zh)
1798 yaz_log(LOG_API,"zebra_get_shadow_enable");
1799 return (zh->shadow_enable);
1802 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1804 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1805 zh->shadow_enable = value;
1809 int init_recordGroup (struct recordGroup *rg)
1812 yaz_log(LOG_API,"init_recordGroup");
1813 rg->groupName = NULL;
1814 rg->databaseName = NULL;
1816 rg->recordId = NULL;
1817 rg->recordType = NULL;
1818 rg->flagStoreData = -1;
1819 rg->flagStoreKeys = -1;
1821 rg->databaseNamePath = 0;
1822 rg->explainDatabase = 0;
1823 rg->fileVerboseLimit = 100000;
1824 rg->followLinks = -1;
1829 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1830 called... and in general... Should be moved to somewhere else */
1831 void res_get_recordGroup (ZebraHandle zh,
1832 struct recordGroup *rGroup,
1838 yaz_log(LOG_API,"res_get_recordGroup e=%s",ext);
1839 if (!rGroup->groupName || !*rGroup->groupName)
1842 sprintf (gprefix, "%s.", rGroup->groupName);
1844 /* determine file type - depending on extension */
1845 if (!rGroup->recordType) {
1846 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1847 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1848 sprintf (ext_res, "%srecordType", gprefix);
1849 rGroup->recordType = res_get (zh->res, ext_res);
1852 /* determine match criteria */
1853 if (!rGroup->recordId) {
1854 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1855 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1856 sprintf (ext_res, "%srecordId", gprefix);
1857 rGroup->recordId = res_get (zh->res, ext_res);
1861 /* determine database name */
1862 if (!rGroup->databaseName) {
1863 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1864 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1865 sprintf (ext_res, "%sdatabase", gprefix);
1866 rGroup->databaseName = res_get (zh->res, ext_res);
1869 if (!rGroup->databaseName)
1870 rGroup->databaseName = "Default";
1872 /* determine if explain database */
1873 sprintf (ext_res, "%sexplainDatabase", gprefix);
1874 rGroup->explainDatabase =
1875 atoi (res_get_def (zh->res, ext_res, "0"));
1878 if (rGroup->flagStoreData == -1)
1881 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1882 if (!(sval = res_get (zh->res, ext_res)))
1884 sprintf (ext_res, "%sstoreData", gprefix);
1885 sval = res_get (zh->res, ext_res);
1888 rGroup->flagStoreData = atoi (sval);
1890 if (rGroup->flagStoreData == -1)
1891 rGroup->flagStoreData = 0;
1894 if (rGroup->flagStoreKeys == -1)
1898 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1899 sval = res_get (zh->res, ext_res);
1902 sprintf (ext_res, "%sstoreKeys", gprefix);
1903 sval = res_get (zh->res, ext_res);
1906 sval = res_get (zh->res, "storeKeys");
1908 rGroup->flagStoreKeys = atoi (sval);
1910 if (rGroup->flagStoreKeys == -1)
1911 rGroup->flagStoreKeys = 0;
1915 /* almost the same as zebra_records_retrieve ... but how did it work?
1916 I mean for multiple records ??? CHECK ??? */
1917 void api_records_retrieve (ZebraHandle zh, ODR stream,
1918 const char *setname, Z_RecordComposition *comp,
1919 oid_value input_format, int num_recs,
1920 ZebraRetrievalRecord *recs)
1924 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1929 zh->errString = odr_strdup (stream, setname);
1935 if (zebra_begin_read (zh))
1938 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1939 for (i = 0; i<num_recs; i++)
1940 pos_array[i] = recs[i].position;
1941 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1944 logf (LOG_DEBUG, "zebraPosSetCreate error");
1946 zh->errString = nmem_strdup (stream->mem, setname);
1950 for (i = 0; i<num_recs; i++)
1954 recs[i].errCode = 0;
1955 recs[i].format = VAL_SUTRS;
1956 recs[i].len = strlen(poset[i].term);
1957 recs[i].buf = poset[i].term;
1958 recs[i].base = poset[i].db;
1962 else if (poset[i].sysno)
1964 /* changed here ??? CHECK ??? */
1967 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1968 stream, input_format, comp,
1973 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1974 memcpy(recs[i].buf, b, recs[i].len);
1975 recs[i].errString = 0; /* Hmmm !!! we should get this */
1976 recs[i].sysno = poset[i].sysno;
1977 recs[i].score = poset[i].score;
1983 sprintf (num_str, "%d", pos_array[i]);
1985 zh->errString = odr_strdup (stream, num_str);
1990 zebraPosSetDestroy (zh, poset, num_recs);
1992 zebra_end_read (zh);
1997 /* ---------------------------------------------------------------------------
1998 Record insert(=update), delete
2000 If sysno is provided, then it's used to identify the reocord.
2001 If not, and match_criteria is provided, then sysno is guessed
2002 If not, and a record is provided, then sysno is got from there
2003 NOTE: Now returns 0 at success and updates sysno, which is an int*
2007 int zebra_insert_record (ZebraHandle zh,
2008 struct recordGroup *rGroup,
2009 const char *recordType,
2010 int *sysno, const char *match, const char *fname,
2011 const char *buf, int buf_size,
2012 int force_update) /* This one is ignored */
2016 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
2018 if (buf_size < 1) buf_size = strlen(buf);
2020 zebra_begin_trans(zh, 1);
2021 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2022 0, /* delete_flag */
2028 0); /* allow_update */
2029 zebra_end_trans(zh);
2030 if (res < 0) return (res);
2034 int zebra_update_record (ZebraHandle zh,
2035 struct recordGroup *rGroup,
2036 const char *recordType,
2037 int* sysno, const char *match, const char *fname,
2038 const char *buf, int buf_size,
2043 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
2045 if (buf_size < 1) buf_size = strlen(buf);
2047 zebra_begin_trans(zh, 1);
2048 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2049 0, /* delete_flag */
2055 1); /* allow_update */
2056 zebra_end_trans(zh);
2060 int zebra_delete_record (ZebraHandle zh,
2061 struct recordGroup *rGroup,
2062 const char *recordType,
2063 int *sysno, const char *match, const char *fname,
2064 const char *buf, int buf_size,
2068 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2070 if (buf_size < 1) buf_size = strlen(buf);
2072 zebra_begin_trans(zh, 1);
2073 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2074 1, /* delete_flag */
2080 1); /* allow_update */
2081 zebra_end_trans(zh);
2085 /* ---------------------------------------------------------------------------
2089 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2090 const char *setname, int *numhits)
2095 ODR odr = odr_createmem(ODR_ENCODE);
2097 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2099 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2102 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2104 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2114 /* ---------------------------------------------------------------------------
2115 Sort - a simplified interface, with optional read locks.
2116 FIXME - This is a horrible name, will conflict with half the applications
2118 int zebra_sort_2 (ZebraHandle zh,
2120 const char *sort_spec,
2121 const char *output_setname,
2122 const char **input_setnames)
2124 int num_input_setnames = 0;
2125 int sort_status = 0;
2126 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2127 yaz_log(LOG_API,"sort (FIXME) ");
2130 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2135 /* we can do this, since the perl typemap code for char** will
2136 put a NULL at the end of list */
2137 while (input_setnames[num_input_setnames]) num_input_setnames++;
2139 if (zebra_begin_read (zh))
2142 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2143 output_setname, sort_sequence, &sort_status);