1 /* $Id: zebraapi.c,v 1.97 2003-04-02 18:46:13 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 static Res zebra_open_res (ZebraHandle zh);
48 static void zebra_close_res (ZebraHandle zh);
51 static void zebra_chdir (ZebraService zs)
55 dir = res_get (zs->global_res, "chdir");
58 logf (LOG_DEBUG, "chdir %s", dir);
66 static void zebra_flush_reg (ZebraHandle zh)
70 zebraExplain_flush (zh->reg->zei, zh);
72 extract_flushWriteKeys (zh);
73 zebra_index_merge (zh);
76 static struct zebra_register *zebra_register_open (ZebraService zs,
78 int rw, int useshadow,
80 const char *reg_path);
81 static void zebra_register_close (ZebraService zs, struct zebra_register *reg);
83 ZebraHandle zebra_open (ZebraService zs)
86 const char *default_encoding;
92 zh = (ZebraHandle) xmalloc (sizeof(*zh));
93 yaz_log (LOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
96 zh->reg = 0; /* no register attached yet */
103 zh->reg_name = xstrdup ("");
105 zh->num_basenames = 0;
114 zh->admin_databaseName = 0;
116 zh->shadow_enable = 1;
118 default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
119 zh->record_encoding = xstrdup (default_encoding);
122 yaz_iconv_open ("UTF-8", default_encoding);
123 if (zh->iconv_to_utf8 == 0)
124 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported",
126 zh->iconv_from_utf8 =
127 yaz_iconv_open (default_encoding, "UTF-8");
128 if (zh->iconv_to_utf8 == 0)
129 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported",
132 zebra_mutex_cond_lock (&zs->session_lock);
134 zh->next = zs->sessions;
137 zebra_mutex_cond_unlock (&zs->session_lock);
142 ZebraService zebra_start (const char *configName)
146 yaz_log (LOG_LOG, "zebra_start %s", configName);
148 if ((res = res_open (configName, 0)))
150 ZebraService zh = xmalloc (sizeof(*zh));
152 yaz_log (LOG_DEBUG, "Read resources `%s'", configName);
154 zh->global_res = res;
155 zh->configName = xstrdup(configName);
160 zebra_mutex_cond_init (&zh->session_lock);
161 if (!res_get (zh->global_res, "passwd"))
162 zh->passwd_db = NULL;
165 zh->passwd_db = passwd_db_open ();
167 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
169 passwd_db_file (zh->passwd_db,
170 res_get (zh->global_res, "passwd"));
172 zh->path_root = res_get (zh->global_res, "root");
179 struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
180 int rw, int useshadow, Res res,
181 const char *reg_path)
183 struct zebra_register *reg;
184 int record_compression = REC_COMPRESS_NONE;
185 char *recordCompression = 0;
189 reg = xmalloc (sizeof(*reg));
192 reg->name = xstrdup (name);
199 yaz_log (LOG_DEBUG, "zebra_register_open rw = %d useshadow=%d p=%p",
202 reg->dh = data1_createx (DATA1_FLAG_XML);
205 reg->bfs = bfs_create (res_get (res, "register"), reg_path);
208 data1_destroy(reg->dh);
212 bf_cache (reg->bfs, res_get (res, "shadow"));
213 data1_set_tabpath (reg->dh, res_get_def(res, "profilePath",
214 DEFAULT_PROFILE_PATH));
215 data1_set_tabroot (reg->dh, reg_path);
216 reg->recTypes = recTypes_init (reg->dh);
217 recTypes_default_handlers (reg->recTypes);
219 reg->zebra_maps = zebra_maps_open (res, reg_path);
220 reg->rank_classes = NULL;
224 reg->keys.buf_max = 0;
226 reg->sortKeys.buf = 0;
227 reg->sortKeys.buf_max = 0;
240 reg->key_file_no = 0;
242 zebraRankInstall (reg, rank1_class);
243 zebraRankInstall (reg, rankzv_class);
244 zebraRankInstall (reg, rankliv_class);
246 recordCompression = res_get_def (res, "recordCompression", "none");
247 if (!strcmp (recordCompression, "none"))
248 record_compression = REC_COMPRESS_NONE;
249 if (!strcmp (recordCompression, "bzip2"))
250 record_compression = REC_COMPRESS_BZIP2;
252 if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
254 logf (LOG_WARN, "rec_open");
259 reg->matchDict = dict_open (reg->bfs, GMATCH_DICT, 20, 1, 0);
261 if (!(reg->dict = dict_open (reg->bfs, FNAME_DICT, 40, rw, 0)))
263 logf (LOG_WARN, "dict_open");
266 if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
268 logf (LOG_WARN, "sortIdx_open");
271 if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
273 struct ISAMS_M_s isams_m;
274 if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
275 key_isams_m(res, &isams_m))))
277 logf (LOG_WARN, "isams_open");
281 if (res_get_match (res, "isam", "i", ISAM_DEFAULT))
283 if (!(reg->isam = is_open (reg->bfs, FNAME_ISAM, key_compare, rw,
284 sizeof (struct it_key), res)))
286 logf (LOG_WARN, "is_open");
290 if (res_get_match (res, "isam", "c", ISAM_DEFAULT))
292 struct ISAMC_M_s isamc_m;
293 if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
294 rw, key_isamc_m(res, &isamc_m))))
296 logf (LOG_WARN, "isc_open");
300 if (res_get_match (res, "isam", "d", ISAM_DEFAULT))
302 struct ISAMD_M_s isamd_m;
304 if (!(reg->isamd = isamd_open (reg->bfs, FNAME_ISAMD,
305 rw, key_isamd_m(res, &isamd_m))))
307 logf (LOG_WARN, "isamd_open");
311 if (res_get_match (res, "isam", "b", ISAM_DEFAULT))
313 struct ISAMC_M_s isamc_m;
315 if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
316 rw, key_isamc_m(res, &isamc_m), 0)))
318 logf (LOG_WARN, "isamb_open");
322 if (res_get_match (res, "isam", "bc", 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), 1)))
329 logf (LOG_WARN, "isamb_open");
333 if (res_get_match (res, "isam", "null", 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 reg->zei = zebraExplain_open (reg->records, reg->dh,
349 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
353 yaz_log (LOG_DEBUG, "zebra_register_open ok p=%p", reg);
357 void zebra_admin_shutdown (ZebraHandle zh)
362 zebra_mutex_cond_lock (&zh->service->session_lock);
363 zh->service->stop_flag = 1;
364 zebra_mutex_cond_unlock (&zh->service->session_lock);
367 void zebra_admin_start (ZebraHandle zh)
373 zebra_mutex_cond_lock (&zs->session_lock);
374 zebra_mutex_cond_unlock (&zs->session_lock);
377 static void zebra_register_close (ZebraService zs, struct zebra_register *reg)
380 yaz_log(LOG_DEBUG, "zebra_register_close p=%p", reg);
385 zebraExplain_close (reg->zei);
386 dict_close (reg->dict);
388 dict_close (reg->matchDict);
389 sortIdx_close (reg->sortIdx);
391 isams_close (reg->isams);
393 is_close (reg->isam);
395 isc_close (reg->isamc);
397 isamd_close (reg->isamd);
399 isamb_close (reg->isamb);
400 rec_close (®->records);
403 recTypes_destroy (reg->recTypes);
404 zebra_maps_close (reg->zebra_maps);
405 zebraRankDestroy (reg);
406 bfs_destroy (reg->bfs);
407 data1_destroy (reg->dh);
409 xfree (reg->sortKeys.buf);
410 xfree (reg->keys.buf);
412 xfree (reg->key_buf);
417 void zebra_stop(ZebraService zs)
421 yaz_log (LOG_LOG, "zebra_stop");
425 zebra_close (zs->sessions);
428 zebra_mutex_cond_destroy (&zs->session_lock);
431 passwd_db_close (zs->passwd_db);
433 res_close (zs->global_res);
434 xfree (zs->configName);
435 xfree (zs->path_root);
439 void zebra_close (ZebraHandle zh)
442 struct zebra_session **sp;
451 yaz_log (LOG_DEBUG, "zebra_close zh=%p", zh);
454 resultSetDestroy (zh, -1, 0, 0);
457 zebra_register_close (zh->service, zh->reg);
458 zebra_close_res (zh);
460 xfree (zh->record_encoding);
462 for (i = 0; i < zh->num_basenames; i++)
463 xfree (zh->basenames[i]);
464 xfree (zh->basenames);
466 if (zh->iconv_to_utf8 != 0)
467 yaz_iconv_close (zh->iconv_to_utf8);
468 if (zh->iconv_from_utf8 != 0)
469 yaz_iconv_close (zh->iconv_from_utf8);
471 xfree (zh->admin_databaseName);
472 zebra_mutex_cond_lock (&zs->session_lock);
473 zebra_lock_destroy (zh->lock_normal);
474 zebra_lock_destroy (zh->lock_shadow);
486 zebra_mutex_cond_unlock (&zs->session_lock);
487 xfree (zh->reg_name);
488 zh->service=0; /* more likely to trigger an assert */
492 struct map_baseinfo {
498 char **new_basenames;
502 static Res zebra_open_res (ZebraHandle zh)
511 sprintf (fname, "%.200s/zebra.cfg", zh->path_reg);
512 res = res_open (fname, zh->service->global_res);
514 res = zh->service->global_res;
516 else if (*zh->reg_name == 0)
518 res = zh->service->global_res;
522 yaz_log (LOG_WARN, "no register root specified");
523 return 0; /* no path for register - fail! */
528 static void zebra_close_res (ZebraHandle zh)
532 if (zh->res != zh->service->global_res)
537 static int zebra_select_register (ZebraHandle zh, const char *new_reg)
541 if (zh->res && strcmp (zh->reg_name, new_reg) == 0)
545 assert (zh->reg == 0);
546 assert (*zh->reg_name == 0);
552 resultSetInvalidate (zh);
553 zebra_register_close (zh->service, zh->reg);
558 xfree (zh->reg_name);
559 zh->reg_name = xstrdup (new_reg);
561 xfree (zh->path_reg);
563 if (zh->service->path_root)
565 zh->path_reg = xmalloc (strlen(zh->service->path_root) +
566 strlen(zh->reg_name) + 3);
567 strcpy (zh->path_reg, zh->service->path_root);
570 strcat (zh->path_reg, "/");
571 strcat (zh->path_reg, zh->reg_name);
574 zh->res = zebra_open_res (zh);
577 zebra_lock_destroy (zh->lock_normal);
581 zebra_lock_destroy (zh->lock_shadow);
587 const char *lock_area =res_get (zh->res, "lockDir");
589 if (!lock_area && zh->path_reg)
590 res_put (zh->res, "lockDir", zh->path_reg);
591 sprintf (fname, "norm.%s.LCK", zh->reg_name);
593 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
595 sprintf (fname, "shadow.%s.LCK", zh->reg_name);
597 zebra_lock_create (res_get(zh->res, "lockDir"), fname, 0);
603 void map_basenames_func (void *vp, const char *name, const char *value)
605 struct map_baseinfo *p = (struct map_baseinfo *) vp;
607 char fromdb[128], todb[8][128];
610 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
611 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
612 todb[5], todb[6], todb[7]);
616 for (i = 0; i<p->num_bases; i++)
617 if (p->basenames[i] && !STRCASECMP (p->basenames[i], fromdb))
620 for (i = 0; i < no; i++)
622 if (p->new_num_bases == p->new_num_max)
624 p->new_basenames[(p->new_num_bases)++] =
625 nmem_strdup (p->mem, todb[i]);
631 void map_basenames (ZebraHandle zh, ODR stream,
632 int *num_bases, char ***basenames)
634 struct map_baseinfo info;
635 struct map_baseinfo *p = &info;
641 info.num_bases = *num_bases;
642 info.basenames = *basenames;
643 info.new_num_max = 128;
644 info.new_num_bases = 0;
645 info.new_basenames = (char **)
646 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
647 info.mem = stream->mem;
649 res_trav (zh->service->global_res, "mapdb", &info, map_basenames_func);
651 for (i = 0; i<p->num_bases; i++)
652 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
654 p->new_basenames[(p->new_num_bases)++] =
655 nmem_strdup (p->mem, p->basenames[i]);
657 *num_bases = info.new_num_bases;
658 *basenames = info.new_basenames;
659 for (i = 0; i<*num_bases; i++)
660 logf (LOG_LOG, "base %s", (*basenames)[i]);
663 int zebra_select_database (ZebraHandle zh, const char *basename)
667 return zebra_select_databases (zh, 1, &basename);
670 int zebra_select_databases (ZebraHandle zh, int num_bases,
671 const char **basenames)
685 for (i = 0; i < zh->num_basenames; i++)
686 xfree (zh->basenames[i]);
687 xfree (zh->basenames);
689 zh->num_basenames = num_bases;
690 zh->basenames = xmalloc (zh->num_basenames * sizeof(*zh->basenames));
691 for (i = 0; i < zh->num_basenames; i++)
692 zh->basenames[i] = xstrdup (basenames[i]);
694 cp = strrchr(basenames[0], '/');
697 len = cp - basenames[0];
698 new_reg = xmalloc (len + 1);
699 memcpy (new_reg, basenames[0], len);
703 new_reg = xstrdup ("");
704 for (i = 1; i<num_bases; i++)
708 cp1 = strrchr (basenames[i], '/');
716 if (len != cp1 - basenames[i] ||
717 memcmp (basenames[i], new_reg, len))
732 zebra_select_register (zh, new_reg);
739 if (!zh->lock_normal || !zh->lock_shadow)
747 void zebra_search_rpn (ZebraHandle zh, ODR decode, ODR stream,
748 Z_RPNQuery *query, const char *setname, int *hits)
755 if (zebra_begin_read (zh))
758 zebra_livcode_transform(zh, query);
760 resultSetAddRPN (zh, decode, stream, query,
761 zh->num_basenames, zh->basenames, setname);
768 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
769 const char *setname, Z_RecordComposition *comp,
770 oid_value input_format, int num_recs,
771 ZebraRetrievalRecord *recs)
781 zh->errString = odr_strdup (stream, setname);
787 if (zebra_begin_read (zh))
790 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
791 for (i = 0; i<num_recs; i++)
792 pos_array[i] = recs[i].position;
793 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
796 logf (LOG_DEBUG, "zebraPosSetCreate error");
798 zh->errString = nmem_strdup (stream->mem, setname);
802 for (i = 0; i<num_recs; i++)
807 recs[i].format = VAL_SUTRS;
808 recs[i].len = strlen(poset[i].term);
809 recs[i].buf = poset[i].term;
810 recs[i].base = poset[i].db;
812 else if (poset[i].sysno)
815 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
816 stream, input_format, comp,
817 &recs[i].format, &recs[i].buf,
820 recs[i].errString = NULL;
826 sprintf (num_str, "%d", pos_array[i]);
828 zh->errString = odr_strdup (stream, num_str);
832 zebraPosSetDestroy (zh, poset, num_recs);
838 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
839 oid_value attributeset,
840 int *position, int *num_entries, ZebraScanEntry **entries,
845 if (zebra_begin_read (zh))
851 rpn_scan (zh, stream, zapt, attributeset,
852 zh->num_basenames, zh->basenames, position,
853 num_entries, entries, is_partial, 0, 0);
857 void zebra_sort (ZebraHandle zh, ODR stream,
858 int num_input_setnames, const char **input_setnames,
859 const char *output_setname, Z_SortKeySpecList *sort_sequence,
864 if (zebra_begin_read (zh))
866 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
867 output_setname, sort_sequence, sort_status);
871 int zebra_deleleResultSet(ZebraHandle zh, int function,
872 int num_setnames, char **setnames,
878 if (zebra_begin_read(zh))
879 return Z_DeleteStatus_systemProblemAtTarget;
882 case Z_DeleteRequest_list:
883 resultSetDestroy (zh, num_setnames, setnames, statuses);
885 case Z_DeleteRequest_all:
886 resultSetDestroy (zh, -1, 0, statuses);
890 status = Z_DeleteStatus_success;
891 for (i = 0; i<num_setnames; i++)
892 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
893 status = statuses[i];
897 int zebra_errCode (ZebraHandle zh)
904 const char *zebra_errString (ZebraHandle zh)
907 return diagbib1_str (zh->errCode);
911 char *zebra_errAdd (ZebraHandle zh)
914 return zh->errString;
918 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
924 if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
926 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
930 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
934 void zebra_admin_import_begin (ZebraHandle zh, const char *database,
935 const char *record_type)
939 if (zebra_select_database(zh, database))
941 zebra_begin_trans (zh, 1);
942 xfree (zh->admin_databaseName);
943 zh->admin_databaseName = xstrdup(database);
946 void zebra_admin_import_end (ZebraHandle zh)
950 zebra_end_trans (zh);
953 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
959 for (i = 0; i<segment->num_segmentRecords; i++)
961 Z_NamePlusRecord *npr = segment->segmentRecords[i];
962 const char *databaseName = npr->databaseName;
965 databaseName = zh->admin_databaseName;
966 printf ("--------------%d--------------------\n", i);
967 if (npr->which == Z_NamePlusRecord_intermediateFragment)
969 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
970 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
972 Odr_oct *oct = fragment->u.notExternallyTagged;
973 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
977 extract_rec_in_mem (zh, "grs.sgml",
985 0 /* match criteria */);
991 int zebra_admin_exchange_record (ZebraHandle zh,
992 const char *database,
995 const char *recid_buf, size_t recid_len,
1004 if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
1006 memcpy (recid_z, recid_buf, recid_len);
1007 recid_z[recid_len] = 0;
1009 rinfo = dict_lookup (zh->reg->matchDict, recid_z);
1012 if (action == 1) /* fail if insert */
1014 memcpy (&sysno, rinfo+1, sizeof(sysno));
1018 if (action == 2 || action == 3) /* fail if delete or update */
1021 extract_rec_in_mem (zh, "grs.sgml", rec_buf, rec_len, database,
1022 action == 3 ? 1 : 0 /* delete flag */,
1023 0, &sysno, 1, 1, 0);
1026 dict_insert (zh->reg->matchDict, recid_z, sizeof(sysno), &sysno);
1028 else if (action == 3)
1030 dict_delete (zh->reg->matchDict, recid_z);
1035 void zebra_admin_create (ZebraHandle zh, const char *database)
1041 if (zebra_select_database (zh, database))
1043 zebra_begin_trans (zh, 1);
1046 /* announce database */
1047 if (zebraExplain_newDatabase (zh->reg->zei, database, 0
1048 /* explainDatabase */))
1051 zh->errString = "database already exist";
1053 zebra_end_trans (zh);
1056 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
1057 const char *input_str, int input_len,
1058 char *output_str, int output_len)
1063 if (!zh->reg->zebra_maps)
1065 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
1066 input_str, input_len);
1069 if (wrbuf_len(wrbuf) >= output_len)
1071 if (wrbuf_len(wrbuf))
1072 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1073 output_str[wrbuf_len(wrbuf)] = '\0';
1074 return wrbuf_len(wrbuf);
1078 void zebra_set_state (ZebraHandle zh, int val, int seqno)
1080 char state_fname[256];
1087 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1088 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1089 f = fopen (fname, "w");
1091 yaz_log (LOG_DEBUG, "%c %d %ld", val, seqno, p);
1092 fprintf (f, "%c %d %ld\n", val, seqno, p);
1097 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
1099 char state_fname[256];
1105 sprintf (state_fname, "state.%s.LCK", zh->reg_name);
1106 fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
1107 f = fopen (fname, "r");
1113 fscanf (f, "%c %d", val, seqno);
1119 int zebra_begin_read (ZebraHandle zh)
1121 return zebra_begin_trans(zh, 0);
1124 void zebra_end_read (ZebraHandle zh)
1126 zebra_end_trans(zh);
1129 int zebra_begin_trans (ZebraHandle zh, int rw)
1138 const char *rval = 0;
1143 if (zh->trans_no != 1)
1146 zh->errString = "write trans not allowed within read trans";
1149 zh->trans_w_no = zh->trans_no;
1153 yaz_log (LOG_LOG, "zebra_begin_trans");
1155 zh->records_inserted = 0;
1156 zh->records_updated = 0;
1157 zh->records_deleted = 0;
1158 zh->records_processed = 0;
1160 #if HAVE_SYS_TIMES_H
1164 if (zh->shadow_enable)
1165 rval = res_get (zh->res, "shadow");
1167 for (pass = 0; pass < 2; pass++)
1171 zebra_lock_r (zh->lock_normal);
1172 zebra_lock_w (zh->lock_shadow);
1176 zebra_lock_w (zh->lock_normal);
1177 zebra_lock_w (zh->lock_shadow);
1180 zebra_get_state (zh, &val, &seqno);
1183 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
1184 zebra_unlock (zh->lock_shadow);
1185 zebra_unlock (zh->lock_normal);
1189 else if (val == 'd')
1193 BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
1195 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
1196 bf_commitClean (bfs, rval);
1201 yaz_log (LOG_WARN, "your previous transaction didn't finish");
1208 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
1212 zebra_set_state (zh, 'd', seqno);
1214 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1215 1, rval ? 1 : 0, zh->res,
1218 zh->reg->seqno = seqno;
1228 if (zh->trans_no != 1)
1230 zebra_flush_reg (zh);
1234 #if HAVE_SYS_TIMES_H
1243 if (!zh->lock_normal || !zh->lock_shadow)
1249 zebra_get_state (zh, &val, &seqno);
1255 else if (seqno != zh->reg->seqno)
1257 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d",
1258 seqno, zh->reg->seqno);
1261 else if (zh->reg->last_val != val)
1263 yaz_log (LOG_LOG, "reopen last cur/old %d/%d",
1264 val, zh->reg->last_val);
1271 zebra_lock_r (zh->lock_shadow);
1273 zebra_lock_r (zh->lock_normal);
1276 zebra_register_close (zh->service, zh->reg);
1277 zh->reg = zebra_register_open (zh->service, zh->reg_name,
1278 0, val == 'c' ? 1 : 0,
1279 zh->res, zh->path_reg);
1285 zh->reg->last_val = val;
1286 zh->reg->seqno = seqno;
1291 void zebra_end_trans (ZebraHandle zh) {
1292 ZebraTransactionStatus dummy;
1293 zebra_end_transaction(zh, &dummy);
1296 void zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
1304 status->processed = 0;
1305 status->inserted = 0;
1306 status->updated = 0;
1307 status->deleted = 0;
1311 if (zh->trans_no != zh->trans_w_no)
1314 if (zh->trans_no != 0)
1317 /* release read lock */
1319 zebra_unlock (zh->lock_normal);
1320 zebra_unlock (zh->lock_shadow);
1323 { /* release write lock */
1327 yaz_log (LOG_LOG, "zebra_end_trans");
1328 rval = res_get (zh->res, "shadow");
1330 zebraExplain_runNumberIncrement (zh->reg->zei, 1);
1332 zebra_flush_reg (zh);
1334 zebra_register_close (zh->service, zh->reg);
1337 yaz_log (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
1338 zh->records_processed, zh->records_inserted,
1339 zh->records_updated, zh->records_deleted);
1341 status->processed = zh->records_processed;
1342 status->inserted = zh->records_inserted;
1343 status->updated = zh->records_updated;
1344 status->deleted = zh->records_deleted;
1346 zebra_get_state (zh, &val, &seqno);
1349 BFiles bfs = bfs_create (rval, zh->path_reg);
1350 yaz_log (LOG_LOG, "deleting shadow stuff val=%c", val);
1351 bf_commitClean (bfs, rval);
1356 zebra_set_state (zh, 'o', seqno);
1358 zebra_unlock (zh->lock_shadow);
1359 zebra_unlock (zh->lock_normal);
1362 #if HAVE_SYS_TIMES_H
1364 logf (LOG_LOG, "user/system: %ld/%ld",
1365 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1366 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1368 status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1369 status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1373 void zebra_repository_update (ZebraHandle zh)
1377 logf (LOG_LOG, "updating %s", zh->rGroup.path);
1378 repositoryUpdate (zh);
1381 void zebra_repository_delete (ZebraHandle zh)
1385 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
1386 repositoryDelete (zh);
1389 void zebra_repository_show (ZebraHandle zh)
1393 repositoryShow (zh);
1396 static int zebra_commit_ex (ZebraHandle zh, int clean_only)
1410 rval = res_get (zh->res, "shadow");
1413 logf (LOG_WARN, "Cannot perform commit");
1414 logf (LOG_WARN, "No shadow area defined");
1418 zebra_lock_w (zh->lock_normal);
1419 zebra_lock_r (zh->lock_shadow);
1421 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1423 zebra_get_state (zh, &val, &seqno);
1426 bf_cache (bfs, rval);
1427 if (bf_commitExists (bfs))
1430 zebra_set_state (zh, 'd', seqno);
1433 zebra_set_state (zh, 'c', seqno);
1435 logf (LOG_LOG, "commit start");
1436 bf_commitExec (bfs);
1441 logf (LOG_LOG, "commit clean");
1442 bf_commitClean (bfs, rval);
1444 zebra_set_state (zh, 'o', seqno);
1448 logf (LOG_LOG, "nothing to commit");
1452 zebra_unlock (zh->lock_shadow);
1453 zebra_unlock (zh->lock_normal);
1457 int zebra_clean (ZebraHandle zh)
1459 return zebra_commit_ex(zh, 1);
1462 int zebra_commit (ZebraHandle zh)
1464 return zebra_commit_ex(zh, 0);
1467 int zebra_init (ZebraHandle zh)
1479 rval = res_get (zh->res, "shadow");
1481 bfs = bfs_create (res_get (zh->service->global_res, "register"),
1484 bf_cache (bfs, rval);
1488 zebra_set_state (zh, 'o', 0);
1492 int zebra_compact (ZebraHandle zh)
1502 bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
1508 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
1514 zebra_begin_trans (zh, 1);
1516 return 0; /* bad sysno */
1517 extract_rec_in_mem (zh, "grs.sgml",
1519 "Default", /* database */
1520 0 /* delete_flag */,
1525 0 /* match criteria */);
1527 zebra_end_trans (zh);
1533 void zebra_set_group (ZebraHandle zh, struct recordGroup *rg)
1537 memcpy (&zh->rGroup, rg, sizeof(*rg));
1540 void zebra_result (ZebraHandle zh, int *code, char **addinfo)
1543 *code = zh->errCode;
1544 *addinfo = zh->errString;
1547 void zebra_shadow_enable (ZebraHandle zh, int value)
1551 zh->shadow_enable = value;
1554 int zebra_record_encoding (ZebraHandle zh, const char *encoding)
1558 xfree (zh->record_encoding);
1564 if (zh->iconv_to_utf8 != 0)
1565 yaz_iconv_close(zh->iconv_to_utf8);
1566 if (zh->iconv_from_utf8 != 0)
1567 yaz_iconv_close(zh->iconv_from_utf8);
1569 zh->record_encoding = xstrdup (encoding);
1571 logf(LOG_DEBUG, "Reset record encoding: %s", encoding);
1574 yaz_iconv_open ("UTF-8", encoding);
1575 if (zh->iconv_to_utf8 == 0)
1576 yaz_log (LOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
1577 zh->iconv_from_utf8 =
1578 yaz_iconv_open (encoding, "UTF-8");
1579 if (zh->iconv_to_utf8 == 0)
1580 yaz_log (LOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
1585 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
1589 res_put(zh->res, name, value);
1592 const char *zebra_get_resource(ZebraHandle zh,
1593 const char *name, const char *defaultvalue)
1597 return res_get_def( zh->res, name, (char *)defaultvalue);
1600 /* moved from zebra_api_ext.c by pop */
1602 int zebra_trans_no (ZebraHandle zh) {
1603 return (zh->trans_no);
1608 int zebra_get_shadow_enable (ZebraHandle zh) {
1609 return (zh->shadow_enable);
1612 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
1613 zh->shadow_enable = value;
1616 void init_recordGroup (struct recordGroup *rg) {
1617 rg->groupName = NULL;
1618 rg->databaseName = NULL;
1620 rg->recordId = NULL;
1621 rg->recordType = NULL;
1622 rg->flagStoreData = -1;
1623 rg->flagStoreKeys = -1;
1625 rg->databaseNamePath = 0;
1626 rg->explainDatabase = 0;
1627 rg->fileVerboseLimit = 100000;
1628 rg->followLinks = -1;
1632 /* This is from extract.c... it seems useful, when extract_rec_in mem is
1633 called... and in general... Should be moved to somewhere else */
1634 void res_get_recordGroup (ZebraHandle zh,
1635 struct recordGroup *rGroup,
1640 if (!rGroup->groupName || !*rGroup->groupName)
1643 sprintf (gprefix, "%s.", rGroup->groupName);
1645 /* determine file type - depending on extension */
1646 if (!rGroup->recordType) {
1647 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1648 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
1649 sprintf (ext_res, "%srecordType", gprefix);
1650 rGroup->recordType = res_get (zh->res, ext_res);
1653 /* determine match criteria */
1654 if (!rGroup->recordId) {
1655 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1656 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
1657 sprintf (ext_res, "%srecordId", gprefix);
1658 rGroup->recordId = res_get (zh->res, ext_res);
1662 /* determine database name */
1663 if (!rGroup->databaseName) {
1664 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1665 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
1666 sprintf (ext_res, "%sdatabase", gprefix);
1667 rGroup->databaseName = res_get (zh->res, ext_res);
1670 if (!rGroup->databaseName)
1671 rGroup->databaseName = "Default";
1673 /* determine if explain database */
1674 sprintf (ext_res, "%sexplainDatabase", gprefix);
1675 rGroup->explainDatabase =
1676 atoi (res_get_def (zh->res, ext_res, "0"));
1679 if (rGroup->flagStoreData == -1) {
1681 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1682 if (!(sval = res_get (zh->res, ext_res))) {
1683 sprintf (ext_res, "%sstoreData", gprefix);
1684 sval = res_get (zh->res, ext_res);
1687 rGroup->flagStoreData = atoi (sval);
1689 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
1692 if (rGroup->flagStoreKeys == -1) {
1695 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1696 sval = res_get (zh->res, ext_res);
1698 sprintf (ext_res, "%sstoreKeys", gprefix);
1699 sval = res_get (zh->res, ext_res);
1701 if (!sval) sval = res_get (zh->res, "storeKeys");
1702 if (sval) rGroup->flagStoreKeys = atoi (sval);
1704 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
1709 /* almost the same as zebra_records_retrieve ... but how did it work?
1710 I mean for multiple records ??? CHECK ??? */
1711 void api_records_retrieve (ZebraHandle zh, ODR stream,
1712 const char *setname, Z_RecordComposition *comp,
1713 oid_value input_format, int num_recs,
1714 ZebraRetrievalRecord *recs)
1722 zh->errString = odr_strdup (stream, setname);
1728 if (zebra_begin_read (zh))
1731 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
1732 for (i = 0; i<num_recs; i++)
1733 pos_array[i] = recs[i].position;
1734 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
1737 logf (LOG_DEBUG, "zebraPosSetCreate error");
1739 zh->errString = nmem_strdup (stream->mem, setname);
1743 for (i = 0; i<num_recs; i++)
1747 recs[i].errCode = 0;
1748 recs[i].format = VAL_SUTRS;
1749 recs[i].len = strlen(poset[i].term);
1750 recs[i].buf = poset[i].term;
1751 recs[i].base = poset[i].db;
1755 else if (poset[i].sysno)
1757 /* changed here ??? CHECK ??? */
1760 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
1761 stream, input_format, comp,
1766 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
1767 memcpy(recs[i].buf, b, recs[i].len);
1768 recs[i].errString = 0; /* Hmmm !!! we should get this */
1769 recs[i].sysno = poset[i].sysno;
1770 recs[i].score = poset[i].score;
1776 sprintf (num_str, "%d", pos_array[i]);
1778 zh->errString = odr_strdup (stream, num_str);
1783 zebraPosSetDestroy (zh, poset, num_recs);
1785 zebra_end_read (zh);
1790 /* ---------------------------------------------------------------------------
1791 Record insert(=update), delete
1793 If sysno is provided, then it's used to identify the reocord.
1794 If not, and match_criteria is provided, then sysno is guessed
1795 If not, and a record is provided, then sysno is got from there
1798 int zebra_insert_record (ZebraHandle zh,
1799 struct recordGroup *rGroup,
1800 const char *recordType,
1801 int sysno, const char *match, const char *fname,
1802 const char *buf, int buf_size,
1803 int force_update) /* This one is ignored */
1808 if (buf_size < 1) buf_size = strlen(buf);
1810 zebra_begin_trans(zh, 1);
1811 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1812 0, /* delete_flag */
1818 0); /* allow_update */
1819 zebra_end_trans(zh);
1820 if (res < 0) return (res);
1824 int zebra_update_record (ZebraHandle zh,
1825 struct recordGroup *rGroup,
1826 const char *recordType,
1827 int sysno, const char *match, const char *fname,
1828 const char *buf, int buf_size,
1834 if (buf_size < 1) buf_size = strlen(buf);
1836 zebra_begin_trans(zh, 1);
1837 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1838 0, /* delete_flag */
1844 1); /* allow_update */
1845 zebra_end_trans(zh);
1851 int zebra_delete_record (ZebraHandle zh,
1852 struct recordGroup *rGroup,
1853 const char *recordType,
1854 int sysno, const char *match, const char *fname,
1855 const char *buf, int buf_size,
1860 if (buf_size < 1) buf_size = strlen(buf);
1862 zebra_begin_trans(zh, 1);
1863 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
1864 1, /* delete_flag */
1870 1); /* allow_update */
1871 zebra_end_trans(zh);
1875 /* ---------------------------------------------------------------------------
1878 zebra_search_RPN is the same as zebra_search_rpn, except that read locking
1879 is not mandatory. (it's repeatable now, also in zebraapi.c)
1882 void zebra_search_RPN (ZebraHandle zh, ODR decode, ODR stream,
1883 Z_RPNQuery *query, const char *setname, int *hits)
1888 if (zebra_begin_read (zh))
1890 resultSetAddRPN (zh, decode, stream, query,
1891 zh->num_basenames, zh->basenames, setname);
1893 zebra_end_read (zh);
1898 int zebra_search_PQF (ZebraHandle zh,
1899 ODR odr_input, ODR odr_output,
1900 const char *pqf_query,
1901 const char *setname)
1906 query = p_query_rpn (odr_input, PROTO_Z3950, pqf_query);
1909 logf (LOG_WARN, "bad query %s\n", pqf_query);
1910 odr_reset (odr_input);
1913 zebra_search_RPN (zh, odr_input, odr_output, query, setname, &hits);
1915 odr_reset (odr_input);
1916 odr_reset (odr_output);
1921 /* ---------------------------------------------------------------------------
1922 Sort - a simplified interface, with optional read locks.
1924 int sort (ZebraHandle zh,
1926 const char *sort_spec,
1927 const char *output_setname,
1928 const char **input_setnames
1931 int num_input_setnames = 0;
1932 int sort_status = 0;
1933 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
1934 if (!sort_sequence) {
1935 logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
1940 /* we can do this, since the perl typemap code for char** will
1941 put a NULL at the end of list */
1942 while (input_setnames[num_input_setnames]) num_input_setnames++;
1944 if (zebra_begin_read (zh))
1947 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
1948 output_setname, sort_sequence, &sort_status);
1951 return (sort_status);