1 /* $Id: zebraapi.c,v 1.113 2003-08-21 10:30:04 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 void zebra_pidfname(ZebraService zs, char *path)
192 zebra_lock_prefix (zs->global_res, path);
193 strcat(path, "zebrasrv.pid");
197 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
198 int rw, int useshadow, Res res,
199 const char *reg_path)
201 struct zebra_register *reg;
202 int record_compression = REC_COMPRESS_NONE;
203 char *recordCompression = 0;
207 reg = xmalloc (sizeof(*reg));
210 reg->name = xstrdup (name);
217 yaz_log (LOG_LOG|LOG_API, "zebra_register_open rw = %d useshadow=%d p=%p,n=%s,rp=%s",
218 rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
220 reg->dh = data1_createx (DATA1_FLAG_XML);
223 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
226 data1_destroy(reg->dh);
230 bf_cache (reg->bfs, res_get (res, "shadow"));
231 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
232 DEFAULT_PROFILE_PATH));
233 data1_set_tabroot (reg->dh, reg_path);
234 reg->recTypes = recTypes_init (reg->dh);
235 recTypes_default_handlers (reg->recTypes);
237 reg->zebra_maps = zebra_maps_open (res, reg_path);
238 reg->rank_classes = NULL;
242 reg->keys.buf_max = 0;
244 reg->sortKeys.buf = 0;
245 reg->sortKeys.buf_max = 0;
258 reg->key_file_no = 0;
260 zebraRankInstall (reg, rank1_class);
261 zebraRankInstall (reg, rankzv_class);
262 zebraRankInstall (reg, rankliv_class);
264 recordCompression = res_get_def (res, "recordCompression", "none");
265 if (!strcmp (recordCompression, "none"))
266 record_compression = REC_COMPRESS_NONE;
267 if (!strcmp (recordCompression, "bzip2"))
268 record_compression = REC_COMPRESS_BZIP2;
270 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
272 logf (LOG_WARN, "rec_open");
277 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
279 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
281 logf (LOG_WARN, "dict_open");
284 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
286 logf (LOG_WARN, "sortIdx_open");
289 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
291 struct ISAMS_M_s isams_m;
292 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
293 key_isams_m(res, &isams_m))))
295 logf (LOG_WARN, "isams_open");
299 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
301 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
302 sizeof (struct it_key), res)))
304 logf (LOG_WARN, "is_open");
308 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
310 struct ISAMC_M_s isamc_m;
311 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
312 rw, key_isamc_m(res, &isamc_m))))
314 logf (LOG_WARN, "isc_open");
318 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
320 struct ISAMD_M_s isamd_m;
322 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
323 rw, key_isamd_m(res, &isamd_m))))
325 logf (LOG_WARN, "isamd_open");
329 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
331 struct ISAMC_M_s isamc_m;
333 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
334 rw, key_isamc_m(res, &isamc_m), 0)))
336 logf (LOG_WARN, "isamb_open");
340 if (res_get_match (res, "isam", "bc", ISAM_DEFAULT))
342 struct ISAMC_M_s isamc_m;
344 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
345 rw, key_isamc_m(res, &isamc_m), 1)))
347 logf (LOG_WARN, "isamb_open");
351 if (res_get_match (res, "isam", "null", ISAM_DEFAULT))
353 struct ISAMC_M_s isamc_m;
355 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
356 rw, key_isamc_m(res, &isamc_m), -1)))
358 logf (LOG_WARN, "isamb_open");
362 reg->zei = zebraExplain_open (reg->records, reg->dh,
367 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
371 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
375 int zebra_admin_shutdown (ZebraHandle zh)
378 yaz_log(LOG_API,"zebra_admin_shutdown");
381 zebra_mutex_cond_lock (&zh->service->session_lock);
382 zh->service->stop_flag = 1;
383 zebra_mutex_cond_unlock (&zh->service->session_lock);
387 int zebra_admin_start (ZebraHandle zh)
391 yaz_log(LOG_API,"zebra_admin_start");
394 zebra_mutex_cond_lock (&zs->session_lock);
395 zebra_mutex_cond_unlock (&zs->session_lock);
399 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
402 yaz_log(LOG_LOG|LOG_API, "zebra_register_close p=%p", reg);
407 zebraExplain_close (reg->zei);
408 dict_close (reg->dict);
410 dict_close (reg->matchDict);
411 sortIdx_close (reg->sortIdx);
413 isams_close (reg->isams);
415 is_close (reg->isam);
417 isc_close (reg->isamc);
419 isamd_close (reg->isamd);
421 isamb_close (reg->isamb);
422 rec_close (®->records);
425 recTypes_destroy (reg->recTypes);
426 zebra_maps_close (reg->zebra_maps);
427 zebraRankDestroy (reg);
428 bfs_destroy (reg->bfs);
429 data1_destroy (reg->dh);
431 xfree (reg->sortKeys.buf);
432 xfree (reg->keys.buf);
434 xfree (reg->key_buf);
439 int zebra_stop(ZebraService zs)
443 yaz_log (LOG_LOG|LOG_API, "zebra_stop");
447 zebra_close (zs->sessions);
450 zebra_mutex_cond_destroy (&zs->session_lock);
453 passwd_db_close (zs->passwd_db);
455 res_close (zs->global_res);
456 xfree (zs->configName);
457 xfree (zs->path_root);
462 int zebra_close (ZebraHandle zh)
465 struct zebra_session **sp;
468 yaz_log(LOG_API,"zebra_close");
475 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
476 resultSetDestroy (zh, -1, 0, 0);
479 zebra_register_close (zh->service, zh->reg);
480 zebra_close_res (zh);
482 xfree (zh->record_encoding);
484 for (i = 0; i < zh->num_basenames; i++)
485 xfree (zh->basenames[i]);
486 xfree (zh->basenames);
488 if (zh->iconv_to_utf8 != 0)
489 yaz_iconv_close (zh->iconv_to_utf8);
490 if (zh->iconv_from_utf8 != 0)
491 yaz_iconv_close (zh->iconv_from_utf8);
493 xfree (zh->admin_databaseName);
494 zebra_mutex_cond_lock (&zs->session_lock);
495 zebra_lock_destroy (zh->lock_normal);
496 zebra_lock_destroy (zh->lock_shadow);
508 zebra_mutex_cond_unlock (&zs->session_lock);
509 xfree (zh->reg_name);
510 zh->service=0; /* more likely to trigger an assert */
515 struct map_baseinfo {
521 char **new_basenames;
525 static Res zebra_open_res (ZebraHandle zh)
534 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
535 res = res_open (fname, zh->service->global_res);
537 res = zh->service->global_res;
539 else if (*zh->reg_name == 0)
541 res = zh->service->global_res;
545 yaz_log (LOG_WARN, "no register root specified");
546 return 0; /* no path for register - fail! */
551 static void zebra_close_res (ZebraHandle zh)
555 if (zh->res != zh->service->global_res)
560 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
564 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
568 assert (zh->reg == 0);
569 assert (*zh->reg_name == 0);
575 resultSetInvalidate (zh);
576 zebra_register_close (zh->service, zh->reg);
581 xfree (zh->reg_name);
582 zh->reg_name = xstrdup (new_reg);
584 xfree (zh->path_reg);
586 if (zh->service->path_root)
588 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
589 strlen(zh->reg_name) + 3);
590 strcpy (zh->path_reg, zh->service->path_root);
593 strcat (zh->path_reg, "/");
594 strcat (zh->path_reg, zh->reg_name);
597 zh->res = zebra_open_res (zh);
600 zebra_lock_destroy (zh->lock_normal);
604 zebra_lock_destroy (zh->lock_shadow);
610 const char *lock_area =res_get (zh->res, "lockDir");
612 if (!lock_area && zh->path_reg)
613 res_put (zh->res, "lockDir", zh->path_reg);
614 sprintf (fname, "norm.%s.LCK", zh->reg_name);
616 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
618 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
620 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
626 void map_basenames_func (void *vp, const char *name, const char *value)
628 struct map_baseinfo *p = (struct map_baseinfo *) vp;
630 char fromdb[128], todb[8][128];
633 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
634 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
635 todb[5], todb[6], todb[7]);
639 for (i = 0; i<p->num_bases; i++)
640 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
643 for (i = 0; i < no; i++)
645 if (p->new_num_bases == p->new_num_max)
647 p->new_basenames[(p->new_num_bases)++] =
648 nmem_strdup (p->mem, todb[i]);
654 void map_basenames (ZebraHandle zh, ODR stream,
655 int *num_bases, char ***basenames)
657 struct map_baseinfo info;
658 struct map_baseinfo *p = &info;
661 yaz_log(LOG_API,"map_basenames ");
665 info.num_bases = *num_bases;
666 info.basenames = *basenames;
667 info.new_num_max = 128;
668 info.new_num_bases = 0;
669 info.new_basenames = (char **)
670 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
671 info.mem = stream->mem;
673 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
675 for (i = 0; i<p->num_bases; i++)
676 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
678 p->new_basenames[(p->new_num_bases)++] =
679 nmem_strdup (p->mem, p->basenames[i]);
681 *num_bases = info.new_num_bases;
682 *basenames = info.new_basenames;
683 for (i = 0; i<*num_bases; i++)
684 logf (LOG_LOG, "base %s", (*basenames)[i]);
687 int zebra_select_database (ZebraHandle zh, const char *basename)
690 yaz_log(LOG_API,"zebra_select_database %s",basename);
692 return zebra_select_databases (zh, 1, &basename);
695 int zebra_select_databases (ZebraHandle zh, int num_bases,
696 const char **basenames)
703 yaz_log(LOG_API,"zebra_select_databases n=%d [0]=%s",
704 num_bases,basenames[0]);
712 for (i = 0; i < zh->num_basenames; i++)
713 xfree (zh->basenames[i]);
714 xfree (zh->basenames);
716 zh->num_basenames = num_bases;
717 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
718 for (i = 0; i < zh->num_basenames; i++)
719 zh->basenames[i] = xstrdup (basenames[i]);
721 cp = strrchr(basenames[0], '/');
724 len = cp - basenames[0];
725 new_reg = xmalloc (len + 1);
726 memcpy (new_reg, basenames[0], len);
730 new_reg = xstrdup ("");
731 for (i = 1; i<num_bases; i++)
735 cp1 = strrchr (basenames[i], '/');
743 if (len != cp1 - basenames[i] ||
744 memcmp (basenames[i], new_reg, len))
759 zebra_select_register (zh, new_reg);
766 if (!zh->lock_normal || !zh->lock_shadow)
774 int zebra_search_RPN (ZebraHandle zh, ODR o,
775 Z_RPNQuery *query, const char *setname, int *hits)
778 yaz_log(LOG_API,"zebra_search_rpn");
783 if (zebra_begin_read (zh))
786 zebra_livcode_transform(zh, query);
788 resultSetAddRPN (zh, odr_extract_mem(o), query,
789 zh->num_basenames, zh->basenames, setname);
797 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
798 const char *setname, Z_RecordComposition *comp,
799 oid_value input_format, int num_recs,
800 ZebraRetrievalRecord *recs)
803 int i, *pos_array, ret = 0;
805 yaz_log(LOG_API,"zebra_records_retrieve n=%d",num_recs);
811 zh->errString = odr_strdup (stream, setname);
817 if (zebra_begin_read (zh))
820 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
821 for (i = 0; i<num_recs; i++)
822 pos_array[i] = recs[i].position;
823 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
826 logf (LOG_DEBUG, "zebraPosSetCreate error");
828 zh->errString = nmem_strdup (stream->mem, setname);
833 for (i = 0; i<num_recs; i++)
838 recs[i].format = VAL_SUTRS;
839 recs[i].len = strlen(poset[i].term);
840 recs[i].buf = poset[i].term;
841 recs[i].base = poset[i].db;
843 else if (poset[i].sysno)
846 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
847 stream, input_format, comp,
848 &recs[i].format, &recs[i].buf,
851 recs[i].errString = NULL;
857 sprintf (num_str, "%d", pos_array[i]);
859 zh->errString = odr_strdup (stream, num_str);
864 zebraPosSetDestroy (zh, poset, num_recs);
871 int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
872 oid_value attributeset,
873 int *position, int *num_entries, ZebraScanEntry **entries,
877 yaz_log(LOG_API,"zebra_scan");
879 if (zebra_begin_read (zh))
885 rpn_scan (zh, stream, zapt, attributeset,
886 zh->num_basenames, zh->basenames, position,
887 num_entries, entries, is_partial, 0, 0);
892 int zebra_sort (ZebraHandle zh, ODR stream,
893 int num_input_setnames, const char **input_setnames,
894 const char *output_setname, Z_SortKeySpecList *sort_sequence,
898 yaz_log(LOG_API,"zebra_sort");
900 if (zebra_begin_read (zh))
902 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
903 output_setname, sort_sequence, sort_status);
908 int zebra_deleleResultSet(ZebraHandle zh, int function,
909 int num_setnames, char **setnames,
914 yaz_log(LOG_API,"zebra_deleleResultSet n=%d",num_setnames);
916 if (zebra_begin_read(zh))
917 return Z_DeleteStatus_systemProblemAtTarget;
920 case Z_DeleteRequest_list:
921 resultSetDestroy (zh, num_setnames, setnames, statuses);
923 case Z_DeleteRequest_all:
924 resultSetDestroy (zh, -1, 0, statuses);
928 status = Z_DeleteStatus_success;
929 for (i = 0; i<num_setnames; i++)
930 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
931 status = statuses[i];
935 int zebra_errCode (ZebraHandle zh)
939 yaz_log(LOG_API,"zebra_errCode: %d",zh->errCode);
942 yaz_log(LOG_API,"zebra_errCode: o");
946 const char *zebra_errString (ZebraHandle zh)
950 e= diagbib1_str (zh->errCode);
951 yaz_log(LOG_API,"zebra_errString: %s",e);
955 char *zebra_errAdd (ZebraHandle zh)
960 yaz_log(LOG_API,"zebra_errAdd: %s",a);
964 void zebra_clearError(ZebraHandle zh)
973 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
977 yaz_log(LOG_API,"zebra_auth u=%s p=%s",user,pass);
980 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
982 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
986 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
990 int zebra_admin_import_begin (ZebraHandle zh, const char *database,
991 const char *record_type)
994 yaz_log(LOG_API,"zebra_admin_import_begin db=%s rt=%s",
995 database, record_type);
997 if (zebra_select_database(zh, database))
999 zebra_begin_trans (zh, 1);
1000 xfree (zh->admin_databaseName);
1001 zh->admin_databaseName = xstrdup(database);
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];
1024 const char *databaseName = npr->databaseName;
1027 databaseName = zh->admin_databaseName;
1028 printf ("--------------%d--------------------\n", i);
1029 if (npr->which == Z_NamePlusRecord_intermediateFragment)
1031 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1032 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1034 Odr_oct *oct = fragment->u.notExternallyTagged;
1035 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
1039 extract_rec_in_mem (zh, "grs.sgml",
1042 0 /* delete_flag */,
1047 0 /* match criteria */);
1054 int zebra_admin_exchange_record (ZebraHandle zh,
1055 const char *database,
1056 const char *rec_buf,
1058 const char *recid_buf, size_t recid_len,
1065 yaz_log(LOG_API,"zebra_admin_exchange_record db=%s ac=%d",
1069 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1071 memcpy (recid_z, recid_buf, recid_len);
1072 recid_z[recid_len] = 0;
1074 zebra_begin_trans(zh,1);
1076 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1079 if (action == 1) /* fail if insert */
1081 zebra_end_trans(zh);
1085 memcpy (&sysno, rinfo+1, sizeof(sysno));
1089 if (action == 2 || action == 3) /* fail if delete or update */
1091 zebra_end_trans(zh);
1095 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1096 action == 3 ? 1 : 0 /* delete flag */,
1097 0, &sysno, 1, 1, 0);
1100 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1102 else if (action == 3)
1104 dict_delete (zh->reg->matchDict, recid_z);
1106 zebra_end_trans(zh);
1110 int delete_w_handle(const char *info, void *handle)
1112 ZebraHandle zh = (ZebraHandle) handle;
1115 if (*info == sizeof(pos))
1117 memcpy (&pos, info+1, sizeof(pos));
1118 isamb_unlink(zh->reg->isamb, pos);
1123 static int delete_SU_handle(void *handle, int ord)
1125 ZebraHandle zh = (ZebraHandle) handle;
1129 ord_len = key_SU_encode (ord, ord_buf);
1130 ord_buf[ord_len] = '\0';
1132 assert (zh->reg->isamb);
1133 dict_delete_subtree(zh->reg->dict, ord_buf,
1134 zh, delete_w_handle);
1138 int zebra_drop_database (ZebraHandle zh, const char *database)
1141 yaz_log(LOG_API,"zebra_drop_database");
1144 if (zebra_select_database (zh, database))
1146 if (zebra_begin_trans (zh, 1))
1150 zebraExplain_curDatabase (zh->reg->zei, database);
1152 zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1153 zebraExplain_removeDatabase(zh->reg->zei, zh);
1155 zebra_end_trans (zh);
1159 int zebra_create_database (ZebraHandle zh, const char *database)
1162 yaz_log(LOG_API,"zebra_create_database");
1165 if (zebra_select_database (zh, database))
1167 if (zebra_begin_trans (zh, 1))
1170 /* announce database */
1171 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1172 /* explainDatabase */))
1174 zebra_end_trans (zh);
1176 zh->errString = "database already exist";
1179 zebra_end_trans (zh);
1183 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1184 const char *input_str, int input_len,
1185 char *output_str, int output_len)
1189 yaz_log(LOG_API,"zebra_string_norm ");
1191 if (!zh->reg->zebra_maps)
1193 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1194 input_str, input_len);
1197 if (wrbuf_len(wrbuf) >= output_len)
1199 if (wrbuf_len(wrbuf))
1200 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1201 output_str[wrbuf_len(wrbuf)] = '\0';
1202 return wrbuf_len(wrbuf);
1206 int zebra_set_state (ZebraHandle zh, int val, int seqno)
1208 char state_fname[256];
1213 yaz_log(LOG_API,"zebra_set_state v=%d seq=%d", val, seqno);
1216 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1217 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1218 f = fopen (fname, "w");
1220 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1221 fprintf (f, "%c %d %ld\n", val, seqno, p);
1227 int zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1229 char state_fname[256];
1234 yaz_log(LOG_API,"zebra_get_state ");
1236 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1237 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1238 f = fopen (fname, "r");
1244 fscanf (f, "%c %d", val, seqno);
1251 int zebra_begin_read (ZebraHandle zh)
1253 return zebra_begin_trans(zh, 0);
1256 int zebra_end_read (ZebraHandle zh)
1258 return zebra_end_trans(zh);
1261 int zebra_begin_trans (ZebraHandle zh, int rw)
1263 yaz_log(LOG_LOG, "zebra_begin_trans rw=%d trans=%d", rw, zh->trans_no);
1267 zh->errString = "zebra_begin_trans: no database selected";
1271 yaz_log(LOG_API,"zebra_begin_trans rw=%d",rw);
1278 const char *rval = 0;
1283 if (zh->trans_no != 1)
1286 zh->errString = "zebra_begin_trans: write trans not allowed within read trans";
1291 resultSetInvalidate (zh);
1292 zebra_register_close (zh->service, zh->reg);
1294 zh->trans_w_no = zh->trans_no;
1298 zh->records_inserted = 0;
1299 zh->records_updated = 0;
1300 zh->records_deleted = 0;
1301 zh->records_processed = 0;
1303 #if HAVE_SYS_TIMES_H
1307 if (zh->shadow_enable)
1308 rval = res_get (zh->res, "shadow");
1310 for (pass = 0; pass < 2; pass++)
1314 zebra_lock_r (zh->lock_normal);
1315 zebra_lock_w (zh->lock_shadow);
1319 zebra_lock_w (zh->lock_normal);
1320 zebra_lock_w (zh->lock_shadow);
1323 zebra_get_state (zh, &val, &seqno);
1326 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1327 zebra_unlock (zh->lock_shadow);
1328 zebra_unlock (zh->lock_normal);
1332 else if (val == 'd')
1336 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1338 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1339 bf_commitClean (bfs, rval);
1344 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1351 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1355 zebra_set_state (zh, 'd', seqno);
1357 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1358 1, rval ? 1 : 0, zh->res,
1361 zh->reg->seqno = seqno;
1364 zebra_set_state (zh, 'o', seqno);
1366 zebra_unlock (zh->lock_shadow);
1367 zebra_unlock (zh->lock_normal);
1373 zh->errString = "zebra_begin_trans: cannot open register";
1374 yaz_log(LOG_FATAL, zh->errString);
1386 if (zh->trans_no != 1)
1388 zebra_flush_reg (zh);
1392 #if HAVE_SYS_TIMES_H
1401 if (!zh->lock_normal || !zh->lock_shadow)
1407 zebra_get_state (zh, &val, &seqno);
1413 else if (seqno != zh->reg->seqno)
1415 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1416 seqno, zh->reg->seqno);
1419 else if (zh->reg->last_val != val)
1421 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1422 val, zh->reg->last_val);
1429 zebra_lock_r (zh->lock_shadow);
1431 zebra_lock_r (zh->lock_normal);
1434 zebra_register_close (zh->service, zh->reg);
1435 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1436 0, val == 'c' ? 1 : 0,
1437 zh->res, zh->path_reg);
1440 zebra_unlock (zh->lock_normal);
1441 zebra_unlock (zh->lock_shadow);
1446 zh->reg->last_val = val;
1447 zh->reg->seqno = seqno;
1452 int zebra_end_trans (ZebraHandle zh)
1454 ZebraTransactionStatus dummy;
1455 yaz_log(LOG_API,"zebra_end_trans");
1456 return zebra_end_transaction(zh, &dummy);
1459 int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1466 yaz_log(LOG_API,"zebra_end_transaction");
1468 status->processed = 0;
1469 status->inserted = 0;
1470 status->updated = 0;
1471 status->deleted = 0;
1475 if (!zh->res || !zh->reg)
1478 zh->errString = "zebra_end_trans: no open transaction";
1481 if (zh->trans_no != zh->trans_w_no)
1484 if (zh->trans_no != 0)
1487 /* release read lock */
1489 zebra_unlock (zh->lock_normal);
1490 zebra_unlock (zh->lock_shadow);
1493 { /* release write lock */
1497 yaz_log (LOG_LOG, "zebra_end_trans");
1498 rval = res_get (zh->res, "shadow");
1500 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1502 zebra_flush_reg (zh);
1504 resultSetInvalidate (zh);
1506 zebra_register_close (zh->service, zh->reg);
1509 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1510 zh->records_processed, zh->records_inserted,
1511 zh->records_updated, zh->records_deleted);
1513 status->processed = zh->records_processed;
1514 status->inserted = zh->records_inserted;
1515 status->updated = zh->records_updated;
1516 status->deleted = zh->records_deleted;
1518 zebra_get_state (zh, &val, &seqno);
1521 BFiles bfs = bfs_create (rval, zh->path_reg);
1522 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1523 bf_commitClean (bfs, rval);
1528 zebra_set_state (zh, 'o', seqno);
1530 zebra_unlock (zh->lock_shadow);
1531 zebra_unlock (zh->lock_normal);
1534 #if HAVE_SYS_TIMES_H
1536 logf (LOG_LOG, "user/system: %ld/%ld",
1537 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1538 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1540 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1541 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1546 int zebra_repository_update (ZebraHandle zh)
1550 logf (LOG_LOG|LOG_API, "updating %s", zh->rGroup.path);
1551 repositoryUpdate (zh);
1555 int zebra_repository_delete (ZebraHandle zh)
1559 logf (LOG_LOG|LOG_API, "deleting %s", zh->rGroup.path);
1560 repositoryDelete (zh);
1564 int zebra_repository_show (ZebraHandle zh)
1567 yaz_log(LOG_API,"zebra_repository_show");
1569 repositoryShow (zh);
1573 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1587 rval = res_get (zh->res, "shadow");
1590 logf (LOG_WARN, "Cannot perform commit");
1591 logf (LOG_WARN, "No shadow area defined");
1595 zebra_lock_w (zh->lock_normal);
1596 zebra_lock_r (zh->lock_shadow);
1598 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1600 zebra_get_state (zh, &val, &seqno);
1603 bf_cache (bfs, rval);
1604 if (bf_commitExists (bfs))
1607 zebra_set_state (zh, 'd', seqno);
1610 zebra_set_state (zh, 'c', seqno);
1612 logf (LOG_LOG, "commit start");
1613 bf_commitExec (bfs);
1618 logf (LOG_LOG, "commit clean");
1619 bf_commitClean (bfs, rval);
1621 zebra_set_state (zh, 'o', seqno);
1625 logf (LOG_LOG, "nothing to commit");
1629 zebra_unlock (zh->lock_shadow);
1630 zebra_unlock (zh->lock_normal);
1634 int zebra_clean (ZebraHandle zh)
1636 yaz_log(LOG_API,"zebra_clean");
1637 return zebra_commit_ex(zh, 1);
1640 int zebra_commit (ZebraHandle zh)
1642 yaz_log(LOG_API,"zebra_commit");
1643 return zebra_commit_ex(zh, 0);
1646 int zebra_init (ZebraHandle zh)
1651 yaz_log(LOG_API,"zebra_init");
1659 rval = res_get (zh->res, "shadow");
1661 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1664 bf_cache (bfs, rval);
1668 zebra_set_state (zh, 'o', 0);
1672 int zebra_compact (ZebraHandle zh)
1676 yaz_log(LOG_API,"zebra_compact");
1683 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1689 int zebra_record_insert (ZebraHandle zh, const char *buf, int len, int *sysno)
1693 yaz_log(LOG_API,"zebra_record_insert");
1697 if (zebra_begin_trans (zh, 1))
1699 extract_rec_in_mem (zh, "grs.sgml",
1701 "Default", /* database */
1702 0 /* delete_flag */,
1707 0 /* match criteria */);
1708 if (zebra_end_trans (zh))
1715 int zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1718 yaz_log(LOG_API,"zebra_set_group");
1720 memcpy (&zh->rGroup, rg, sizeof(*rg));
1724 int zebra_result (ZebraHandle zh, int *code, char **addinfo)
1727 yaz_log(LOG_API,"zebra_result");
1728 *code = zh->errCode;
1729 *addinfo = zh->errString;
1733 int zebra_shadow_enable (ZebraHandle zh, int value)
1736 yaz_log(LOG_API,"zebra_shadow_enable");
1738 zh->shadow_enable = value;
1742 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1745 yaz_log(LOG_API,"zebra_record_encoding");
1747 xfree (zh->record_encoding);
1751 * Something about charset aliases. Oleg???
1754 if (zh->iconv_to_utf8 != 0)
1755 yaz_iconv_close(zh->iconv_to_utf8);
1756 if (zh->iconv_from_utf8 != 0)
1757 yaz_iconv_close(zh->iconv_from_utf8);
1759 zh->record_encoding = xstrdup (encoding);
1761 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1764 yaz_iconv_open ("UTF-8", encoding);
1765 if (zh->iconv_to_utf8 == 0)
1766 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1767 zh->iconv_from_utf8 =
1768 yaz_iconv_open (encoding, "UTF-8");
1769 if (zh->iconv_to_utf8 == 0)
1770 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1775 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1778 yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value);
1780 res_put(zh->res, name, value);
1784 const char *zebra_get_resource(ZebraHandle zh,
1785 const char *name, const char *defaultvalue)
1789 v= res_get_def( zh->res, name, (char *)defaultvalue);
1791 yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
1795 /* moved from zebra_api_ext.c by pop */
1796 /* FIXME: Should this really be public??? -Heikki */
1798 int zebra_trans_no (ZebraHandle zh)
1801 yaz_log(LOG_API,"zebra_trans_no");
1802 return zh->trans_no;
1805 int zebra_get_shadow_enable (ZebraHandle zh)
1807 yaz_log(LOG_API,"zebra_get_shadow_enable");
1808 return (zh->shadow_enable);
1811 int zebra_set_shadow_enable (ZebraHandle zh, int value)
1813 yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
1814 zh->shadow_enable = value;
1818 int init_recordGroup (struct recordGroup *rg)
1821 yaz_log(LOG_API,"init_recordGroup");
1822 rg->groupName = NULL;
1823 rg->databaseName = NULL;
1825 rg->recordId = NULL;
1826 rg->recordType = NULL;
1827 rg->flagStoreData = -1;
1828 rg->flagStoreKeys = -1;
1830 rg->databaseNamePath = 0;
1831 rg->explainDatabase = 0;
1832 rg->fileVerboseLimit = 100000;
1833 rg->followLinks = -1;
1838 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1839 called... and in general... Should be moved to somewhere else */
1840 void res_get_recordGroup (ZebraHandle zh,
1841 struct recordGroup *rGroup,
1847 yaz_log(LOG_API,"res_get_recordGroup e=%s",ext);
1848 if (!rGroup->groupName || !*rGroup->groupName)
1851 sprintf (gprefix, "%s.", rGroup->groupName);
1853 /* determine file type - depending on extension */
1854 if (!rGroup->recordType) {
1855 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1856 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1857 sprintf (ext_res, "%srecordType", gprefix);
1858 rGroup->recordType = res_get (zh->res, ext_res);
1861 /* determine match criteria */
1862 if (!rGroup->recordId) {
1863 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1864 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1865 sprintf (ext_res, "%srecordId", gprefix);
1866 rGroup->recordId = res_get (zh->res, ext_res);
1870 /* determine database name */
1871 if (!rGroup->databaseName) {
1872 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1873 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1874 sprintf (ext_res, "%sdatabase", gprefix);
1875 rGroup->databaseName = res_get (zh->res, ext_res);
1878 if (!rGroup->databaseName)
1879 rGroup->databaseName = "Default";
1881 /* determine if explain database */
1882 sprintf (ext_res, "%sexplainDatabase", gprefix);
1883 rGroup->explainDatabase =
1884 atoi (res_get_def (zh->res, ext_res, "0"));
1887 if (rGroup->flagStoreData == -1)
1890 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1891 if (!(sval = res_get (zh->res, ext_res)))
1893 sprintf (ext_res, "%sstoreData", gprefix);
1894 sval = res_get (zh->res, ext_res);
1897 rGroup->flagStoreData = atoi (sval);
1899 if (rGroup->flagStoreData == -1)
1900 rGroup->flagStoreData = 0;
1903 if (rGroup->flagStoreKeys == -1)
1907 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1908 sval = res_get (zh->res, ext_res);
1911 sprintf (ext_res, "%sstoreKeys", gprefix);
1912 sval = res_get (zh->res, ext_res);
1915 sval = res_get (zh->res, "storeKeys");
1917 rGroup->flagStoreKeys = atoi (sval);
1919 if (rGroup->flagStoreKeys == -1)
1920 rGroup->flagStoreKeys = 0;
1924 /* almost the same as zebra_records_retrieve ... but how did it work?
1925 I mean for multiple records ??? CHECK ??? */
1926 void api_records_retrieve (ZebraHandle zh, ODR stream,
1927 const char *setname, Z_RecordComposition *comp,
1928 oid_value input_format, int num_recs,
1929 ZebraRetrievalRecord *recs)
1933 yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
1938 zh->errString = odr_strdup (stream, setname);
1944 if (zebra_begin_read (zh))
1947 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1948 for (i = 0; i<num_recs; i++)
1949 pos_array[i] = recs[i].position;
1950 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1953 logf (LOG_DEBUG, "zebraPosSetCreate error");
1955 zh->errString = nmem_strdup (stream->mem, setname);
1959 for (i = 0; i<num_recs; i++)
1963 recs[i].errCode = 0;
1964 recs[i].format = VAL_SUTRS;
1965 recs[i].len = strlen(poset[i].term);
1966 recs[i].buf = poset[i].term;
1967 recs[i].base = poset[i].db;
1971 else if (poset[i].sysno)
1973 /* changed here ??? CHECK ??? */
1976 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1977 stream, input_format, comp,
1982 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1983 memcpy(recs[i].buf, b, recs[i].len);
1984 recs[i].errString = 0; /* Hmmm !!! we should get this */
1985 recs[i].sysno = poset[i].sysno;
1986 recs[i].score = poset[i].score;
1992 sprintf (num_str, "%d", pos_array[i]);
1994 zh->errString = odr_strdup (stream, num_str);
1999 zebraPosSetDestroy (zh, poset, num_recs);
2001 zebra_end_read (zh);
2006 /* ---------------------------------------------------------------------------
2007 Record insert(=update), delete
2009 If sysno is provided, then it's used to identify the reocord.
2010 If not, and match_criteria is provided, then sysno is guessed
2011 If not, and a record is provided, then sysno is got from there
2012 NOTE: Now returns 0 at success and updates sysno, which is an int*
2016 int zebra_insert_record (ZebraHandle zh,
2017 struct recordGroup *rGroup,
2018 const char *recordType,
2019 int *sysno, const char *match, const char *fname,
2020 const char *buf, int buf_size,
2021 int force_update) /* This one is ignored */
2025 yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
2027 if (buf_size < 1) buf_size = strlen(buf);
2029 zebra_begin_trans(zh, 1);
2030 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2031 0, /* delete_flag */
2037 0); /* allow_update */
2038 zebra_end_trans(zh);
2039 if (res < 0) return (res);
2043 int zebra_update_record (ZebraHandle zh,
2044 struct recordGroup *rGroup,
2045 const char *recordType,
2046 int* sysno, const char *match, const char *fname,
2047 const char *buf, int buf_size,
2052 yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
2054 if (buf_size < 1) buf_size = strlen(buf);
2056 zebra_begin_trans(zh, 1);
2057 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2058 0, /* delete_flag */
2064 1); /* allow_update */
2065 zebra_end_trans(zh);
2069 int zebra_delete_record (ZebraHandle zh,
2070 struct recordGroup *rGroup,
2071 const char *recordType,
2072 int *sysno, const char *match, const char *fname,
2073 const char *buf, int buf_size,
2077 yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
2079 if (buf_size < 1) buf_size = strlen(buf);
2081 zebra_begin_trans(zh, 1);
2082 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
2083 1, /* delete_flag */
2089 1); /* allow_update */
2090 zebra_end_trans(zh);
2094 /* ---------------------------------------------------------------------------
2098 int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
2099 const char *setname, int *numhits)
2104 ODR odr = odr_createmem(ODR_ENCODE);
2106 yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
2108 query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
2111 yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
2113 res=zebra_search_RPN (zh, odr, query, setname, &hits);
2117 yaz_log(LOG_API,"Hits: %d",hits);
2125 /* ---------------------------------------------------------------------------
2126 Sort - a simplified interface, with optional read locks.
2127 FIXME - This is a horrible name, will conflict with half the applications
2129 int zebra_sort_by_specstr (ZebraHandle zh,
2131 const char *sort_spec,
2132 const char *output_setname,
2133 const char **input_setnames)
2135 int num_input_setnames = 0;
2136 int sort_status = 0;
2137 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
2138 yaz_log(LOG_API,"sort (FIXME) ");
2141 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
2146 /* we can do this, since the perl typemap code for char** will
2147 put a NULL at the end of list */
2148 while (input_setnames[num_input_setnames]) num_input_setnames++;
2150 if (zebra_begin_read (zh))
2153 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
2154 output_setname, sort_sequence, &sort_status);