2 * Copyright (C) 1995-2002, Index Data
5 * $Id: zebraapi.c,v 1.50 2002-03-27 07:53:13 adam Exp $
18 #include <yaz/diagbib1.h>
22 static void zebra_chdir (ZebraService zh)
24 const char *dir = res_get (zh->res, "chdir");
27 logf (LOG_DEBUG, "chdir %s", dir);
35 static void zebra_flush_reg (ZebraHandle zh)
37 zebraExplain_flush (zh->service->zei, 1, zh);
39 extract_flushWriteKeys (zh);
40 zebra_index_merge (zh);
44 static int zebra_register_activate (ZebraHandle zh, int rw, int useshadow);
45 static int zebra_register_deactivate (ZebraHandle zh);
47 static int zebra_begin_read (ZebraHandle zh);
48 static void zebra_end_read (ZebraHandle zh);
50 ZebraHandle zebra_open (ZebraService zs)
58 zh = (ZebraHandle) xmalloc (sizeof(*zh));
59 yaz_log (LOG_LOG, "zebra_open zs=%p returns %p", zs, zh);
69 zh->lock_normal = zebra_lock_create (res_get(zs->res, "lockDir"),
71 zh->lock_shadow = zebra_lock_create (res_get(zs->res, "lockDir"),
75 zh->admin_databaseName = 0;
80 zebra_mutex_cond_lock (&zs->session_lock);
82 zh->next = zs->sessions;
85 zebra_mutex_cond_unlock (&zs->session_lock);
91 ZebraService zebra_start (const char *configName)
93 ZebraService zh = xmalloc (sizeof(*zh));
95 yaz_log (LOG_LOG, "zebra_start %s", configName);
97 zh->configName = xstrdup(configName);
102 zh->registerState = -1;
103 zh->registerChange = 0;
108 if (!(zh->res = res_open (zh->configName)))
110 logf (LOG_WARN, "Failed to read resources `%s'", zh->configName);
115 zebra_mutex_cond_init (&zh->session_lock);
116 if (!res_get (zh->res, "passwd"))
117 zh->passwd_db = NULL;
120 zh->passwd_db = passwd_db_open ();
122 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
124 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
130 static int zebra_register_activate (ZebraHandle zh, int rw, int useshadow)
132 ZebraService zs = zh->service;
133 int record_compression = REC_COMPRESS_NONE;
134 char *recordCompression = 0;
136 yaz_log (LOG_LOG, "zebra_open_register_activate rw = %d useshadow=%d",
139 zs->dh = data1_create ();
142 zs->bfs = bfs_create (res_get (zs->res, "register"));
145 data1_destroy(zs->dh);
148 bf_lockDir (zs->bfs, res_get (zs->res, "lockDir"));
150 bf_cache (zs->bfs, res_get (zs->res, "shadow"));
151 data1_set_tabpath (zs->dh, res_get(zs->res, "profilePath"));
152 zs->recTypes = recTypes_init (zs->dh);
153 recTypes_default_handlers (zs->recTypes);
155 zs->zebra_maps = zebra_maps_open (zs->res);
156 zs->rank_classes = NULL;
171 zebraRankInstall (zs, rank1_class);
173 recordCompression = res_get_def (zh->service->res,
174 "recordCompression", "none");
175 if (!strcmp (recordCompression, "none"))
176 record_compression = REC_COMPRESS_NONE;
177 if (!strcmp (recordCompression, "bzip2"))
178 record_compression = REC_COMPRESS_BZIP2;
180 if (!(zs->records = rec_open (zs->bfs, rw, record_compression)))
182 logf (LOG_WARN, "rec_open");
187 zs->matchDict = dict_open (zs->bfs, GMATCH_DICT, 20, 1, 0);
189 if (!(zs->dict = dict_open (zs->bfs, FNAME_DICT, 40, rw, 0)))
191 logf (LOG_WARN, "dict_open");
194 if (!(zs->sortIdx = sortIdx_open (zs->bfs, rw)))
196 logf (LOG_WARN, "sortIdx_open");
199 if (res_get_match (zs->res, "isam", "s", ISAM_DEFAULT))
201 struct ISAMS_M_s isams_m;
202 if (!(zs->isams = isams_open (zs->bfs, FNAME_ISAMS, rw,
203 key_isams_m(zs->res, &isams_m))))
205 logf (LOG_WARN, "isams_open");
210 else if (res_get_match (zs->res, "isam", "i", ISAM_DEFAULT))
212 if (!(zs->isam = is_open (zs->bfs, FNAME_ISAM, key_compare, rw,
213 sizeof (struct it_key), zs->res)))
215 logf (LOG_WARN, "is_open");
219 else if (res_get_match (zs->res, "isam", "c", ISAM_DEFAULT))
221 struct ISAMC_M_s isamc_m;
222 if (!(zs->isamc = isc_open (zs->bfs, FNAME_ISAMC,
223 rw, key_isamc_m(zs->res, &isamc_m))))
225 logf (LOG_WARN, "isc_open");
229 else if (res_get_match (zs->res, "isam", "d", ISAM_DEFAULT))
231 struct ISAMD_M_s isamd_m;
233 if (!(zs->isamd = isamd_open (zs->bfs, FNAME_ISAMD,
234 rw, key_isamd_m(zs->res, &isamd_m))))
236 logf (LOG_WARN, "isamd_open");
241 zs->zei = zebraExplain_open (zs->records, zs->dh,
246 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
250 yaz_log (LOG_LOG, "zebra_register_activate ok");
254 void zebra_admin_shutdown (ZebraHandle zh)
256 zebra_mutex_cond_lock (&zh->service->session_lock);
257 zh->service->stop_flag = 1;
258 if (!zh->service->sessions)
259 zebra_register_deactivate(zh);
260 zh->service->active = 0;
261 zebra_mutex_cond_unlock (&zh->service->session_lock);
264 void zebra_admin_start (ZebraHandle zh)
266 ZebraService zs = zh->service;
268 zebra_mutex_cond_lock (&zs->session_lock);
270 zh->service->active = 1;
271 zebra_mutex_cond_unlock (&zs->session_lock);
274 static int zebra_register_deactivate (ZebraHandle zh)
276 ZebraService zs = zh->service;
280 yaz_log(LOG_LOG, "zebra_register_deactivate (ignored since active=%d)",
284 yaz_log(LOG_LOG, "zebra_register_deactivate");
288 zebraExplain_close (zs->zei, 0);
289 dict_close (zs->dict);
291 dict_close (zs->matchDict);
292 sortIdx_close (zs->sortIdx);
294 isams_close (zs->isams);
299 isc_close (zs->isamc);
301 isamd_close (zs->isamd);
303 rec_close (&zs->records);
305 resultSetInvalidate (zh);
307 recTypes_destroy (zs->recTypes);
308 zebra_maps_close (zs->zebra_maps);
309 zebraRankDestroy (zs);
310 bfs_destroy (zs->bfs);
311 data1_destroy (zs->dh);
314 passwd_db_close (zs->passwd_db);
319 void zebra_stop(ZebraService zs)
323 yaz_log (LOG_LOG, "zebra_stop");
325 zebra_mutex_cond_lock (&zs->session_lock);
328 zebra_register_deactivate(zs->sessions);
329 zebra_close (zs->sessions);
332 zebra_mutex_cond_unlock (&zs->session_lock);
334 zebra_mutex_cond_destroy (&zs->session_lock);
337 xfree (zs->configName);
341 void zebra_close (ZebraHandle zh)
344 struct zebra_session **sp;
350 yaz_log (LOG_LOG, "zebra_close zh=%p", zh);
353 resultSetDestroy (zh, -1, 0, 0);
361 xfree (zh->admin_databaseName);
362 zebra_mutex_cond_lock (&zs->session_lock);
363 zebra_lock_destroy (zh->lock_normal);
364 zebra_lock_destroy (zh->lock_shadow);
376 // if (!zs->sessions && zs->stop_flag)
377 // zebra_register_deactivate(zs);
378 zebra_mutex_cond_unlock (&zs->session_lock);
382 struct map_baseinfo {
388 char **new_basenames;
392 void map_basenames_func (void *vp, const char *name, const char *value)
394 struct map_baseinfo *p = (struct map_baseinfo *) vp;
396 char fromdb[128], todb[8][128];
399 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
400 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
401 todb[5], todb[6], todb[7]);
405 for (i = 0; i<p->num_bases; i++)
406 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
409 for (i = 0; i < no; i++)
411 if (p->new_num_bases == p->new_num_max)
413 p->new_basenames[(p->new_num_bases)++] =
414 nmem_strdup (p->mem, todb[i]);
420 void map_basenames (ZebraHandle zh, ODR stream,
421 int *num_bases, char ***basenames)
423 struct map_baseinfo info;
424 struct map_baseinfo *p = &info;
428 info.num_bases = *num_bases;
429 info.basenames = *basenames;
430 info.new_num_max = 128;
431 info.new_num_bases = 0;
432 info.new_basenames = (char **)
433 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
434 info.mem = stream->mem;
436 res_trav (zh->service->res, "mapdb", &info, map_basenames_func);
438 for (i = 0; i<p->num_bases; i++)
439 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
441 p->new_basenames[(p->new_num_bases)++] =
442 nmem_strdup (p->mem, p->basenames[i]);
444 *num_bases = info.new_num_bases;
445 *basenames = info.new_basenames;
446 for (i = 0; i<*num_bases; i++)
447 logf (LOG_LOG, "base %s", (*basenames)[i]);
450 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
451 Z_RPNQuery *query, int num_bases, char **basenames,
455 if (zebra_begin_read (zh))
457 map_basenames (zh, stream, &num_bases, &basenames);
458 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
462 logf(LOG_APP,"SEARCH:%d:",zh->hits);
467 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
468 const char *setname, Z_RecordComposition *comp,
469 oid_value input_format, int num_recs,
470 ZebraRetrievalRecord *recs)
475 if (zebra_begin_read (zh))
478 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
479 for (i = 0; i<num_recs; i++)
480 pos_array[i] = recs[i].position;
481 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
484 logf (LOG_DEBUG, "zebraPosSetCreate error");
486 zh->errString = nmem_strdup (stream->mem, setname);
490 for (i = 0; i<num_recs; i++)
495 recs[i].format = VAL_SUTRS;
496 recs[i].len = strlen(poset[i].term);
497 recs[i].buf = poset[i].term;
498 recs[i].base = poset[i].db;
500 else if (poset[i].sysno)
503 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
504 stream, input_format, comp,
505 &recs[i].format, &recs[i].buf,
508 recs[i].errString = NULL;
514 sprintf (num_str, "%d", pos_array[i]);
516 zh->errString = nmem_strdup (stream->mem, num_str);
520 zebraPosSetDestroy (zh, poset, num_recs);
526 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
527 oid_value attributeset,
528 int num_bases, char **basenames,
529 int *position, int *num_entries, ZebraScanEntry **entries,
532 if (zebra_begin_read (zh))
538 map_basenames (zh, stream, &num_bases, &basenames);
539 rpn_scan (zh, stream, zapt, attributeset,
540 num_bases, basenames, position,
541 num_entries, entries, is_partial);
545 void zebra_sort (ZebraHandle zh, ODR stream,
546 int num_input_setnames, const char **input_setnames,
547 const char *output_setname, Z_SortKeySpecList *sort_sequence,
550 if (zebra_begin_read (zh))
552 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
553 output_setname, sort_sequence, sort_status);
557 int zebra_deleleResultSet(ZebraHandle zh, int function,
558 int num_setnames, char **setnames,
562 if (zebra_begin_read(zh))
563 return Z_DeleteStatus_systemProblemAtTarget;
566 case Z_DeleteRequest_list:
567 resultSetDestroy (zh, num_setnames, setnames, statuses);
569 case Z_DeleteRequest_all:
570 resultSetDestroy (zh, -1, 0, statuses);
574 status = Z_DeleteStatus_success;
575 for (i = 0; i<num_setnames; i++)
576 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
577 status = statuses[i];
581 int zebra_errCode (ZebraHandle zh)
586 const char *zebra_errString (ZebraHandle zh)
588 return diagbib1_str (zh->errCode);
591 char *zebra_errAdd (ZebraHandle zh)
593 return zh->errString;
596 int zebra_hits (ZebraHandle zh)
601 int zebra_auth (ZebraService zh, const char *user, const char *pass)
603 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
605 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
609 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
613 void zebra_admin_import_begin (ZebraHandle zh, const char *database)
615 zebra_begin_trans (zh);
616 xfree (zh->admin_databaseName);
617 zh->admin_databaseName = xstrdup(database);
620 void zebra_admin_import_end (ZebraHandle zh)
622 zebra_end_trans (zh);
625 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
629 if (zh->service->active < 2)
631 for (i = 0; i<segment->num_segmentRecords; i++)
633 Z_NamePlusRecord *npr = segment->segmentRecords[i];
634 const char *databaseName = npr->databaseName;
637 databaseName = zh->admin_databaseName;
638 printf ("--------------%d--------------------\n", i);
639 if (npr->which == Z_NamePlusRecord_intermediateFragment)
641 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
642 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
644 Odr_oct *oct = fragment->u.notExternallyTagged;
645 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
649 extract_rec_in_mem (zh, "grs.sgml",
657 0 /* match criteria */);
663 void zebra_admin_create (ZebraHandle zh, const char *database)
667 zebra_begin_trans (zh);
670 /* announce database */
671 if (zebraExplain_newDatabase (zh->service->zei, database, 0
672 /* explainDatabase */))
675 zh->errString = "Database already exist";
677 zebra_end_trans (zh);
680 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
681 const char *input_str, int input_len,
682 char *output_str, int output_len)
685 if (!zh->service->zebra_maps)
687 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, "",
688 input_str, input_len);
691 if (wrbuf_len(wrbuf) >= output_len)
693 if (wrbuf_len(wrbuf))
694 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
695 output_str[wrbuf_len(wrbuf)] = '\0';
696 return wrbuf_len(wrbuf);
700 void zebra_set_state (ZebraHandle zh, int val, int seqno)
702 char *fname = zebra_mk_fname (res_get(zh->service->res, "lockDir"),
705 FILE *f = fopen (fname, "w");
706 fprintf (f, "%c %d %ld\n", val, seqno, p);
711 void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
713 char *fname = zebra_mk_fname (res_get(zh->service->res, "lockDir"),
715 FILE *f = fopen (fname, "r");
722 fscanf (f, "%c %d", val, seqno);
728 static int zebra_begin_read (ZebraHandle zh)
737 if (zh->trans_no != 1)
739 zebra_flush_reg (zh);
743 zebra_get_state (zh, &val, &seqno);
746 if (seqno != zh->service->seqno)
748 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d", seqno, zh->service->seqno);
751 else if (zh->service->last_val != val)
753 yaz_log (LOG_LOG, "reopen last cur/old %d/%d", val, zh->service->last_val);
760 zebra_lock_r (zh->lock_shadow);
762 zebra_lock_r (zh->lock_normal);
764 zh->service->last_val = val;
765 zh->service->seqno = seqno;
767 zebra_register_deactivate (zh);
769 zebra_register_activate (zh, 0, val == 'c' ? 1 : 0);
773 static void zebra_end_read (ZebraHandle zh)
777 if (zh->trans_no != 0)
780 zebra_unlock (zh->lock_normal);
781 zebra_unlock (zh->lock_shadow);
784 void zebra_begin_trans (ZebraHandle zh)
792 if (zh->trans_no != 1)
797 yaz_log (LOG_LOG, "zebra_begin_trans");
803 rval = res_get (zh->service->res, "shadow");
805 for (pass = 0; pass < 2; pass++)
809 zebra_lock_r (zh->lock_normal);
810 zebra_lock_w (zh->lock_shadow);
814 zebra_lock_w (zh->lock_normal);
815 zebra_lock_w (zh->lock_shadow);
818 zebra_get_state (zh, &val, &seqno);
821 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
822 zebra_unlock (zh->lock_shadow);
823 zebra_unlock (zh->lock_normal);
831 BFiles bfs = bfs_create (res_get (zh->service->res, "shadow"));
832 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
833 bf_commitClean (bfs, rval);
838 yaz_log (LOG_WARN, "your previous transaction didn't finish");
845 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
849 zebra_set_state (zh, 'd', seqno);
851 zebra_register_activate (zh, 1, rval ? 1 : 0);
852 zh->service->seqno = seqno;
855 void zebra_end_trans (ZebraHandle zh)
862 if (zh->trans_no != 0)
865 yaz_log (LOG_LOG, "zebra_end_trans");
866 rval = res_get (zh->service->res, "shadow");
868 zebra_flush_reg (zh);
870 zebra_register_deactivate (zh);
872 zebra_get_state (zh, &val, &seqno);
875 BFiles bfs = bfs_create (res_get (zh->service->res, "shadow"));
876 bf_commitClean (bfs, rval);
881 zebra_set_state (zh, 'o', seqno);
883 zebra_unlock (zh->lock_shadow);
884 zebra_unlock (zh->lock_normal);
888 logf (LOG_LOG, "user/system: %ld/%ld",
889 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
890 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
895 void zebra_repository_update (ZebraHandle zh)
897 zebra_begin_trans (zh);
898 logf (LOG_LOG, "updating %s", zh->rGroup.path);
899 repositoryUpdate (zh);
900 zebra_end_trans (zh);
903 void zebra_repository_delete (ZebraHandle zh)
905 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
906 repositoryDelete (zh);
909 void zebra_repository_show (ZebraHandle zh)
914 void zebra_commit (ZebraHandle zh)
918 const char *rval = res_get (zh->service->res, "shadow");
923 logf (LOG_WARN, "Cannot perform commit");
924 logf (LOG_WARN, "No shadow area defined");
928 zebra_lock_w (zh->lock_normal);
929 zebra_lock_r (zh->lock_shadow);
931 bfs = bfs_create (res_get (zh->service->res, "register"));
933 zebra_get_state (zh, &val, &seqno);
936 bf_cache (bfs, rval);
937 if (bf_commitExists (bfs))
939 zebra_set_state (zh, 'c', seqno);
941 logf (LOG_LOG, "commit start");
946 logf (LOG_LOG, "commit clean");
947 bf_commitClean (bfs, rval);
949 zebra_set_state (zh, 'o', seqno);
953 logf (LOG_LOG, "nothing to commit");
957 zebra_unlock (zh->lock_shadow);
958 zebra_unlock (zh->lock_normal);
961 void zebra_init (ZebraHandle zh)
963 const char *rval = res_get (zh->service->res, "shadow");
966 bfs = bfs_create (res_get (zh->service->res, "register"));
968 bf_cache (bfs, rval);
972 zebra_set_state (zh, 'o', 0);
975 void zebra_compact (ZebraHandle zh)
977 BFiles bfs = bfs_create (res_get (zh->service->res, "register"));
982 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
985 zebra_begin_trans (zh);
986 extract_rec_in_mem (zh, "grs.sgml",
988 "Default", /* database */
994 0 /* match criteria */);
995 zebra_end_trans (zh);