2 * Copyright (C) 1995-2002, Index Data
5 * $Id: zebraapi.c,v 1.46 2002-02-20 23:07:54 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);
71 zh->lock_normal = zebra_lock_create ("norm.LCK", 0);
72 zh->lock_shadow = zebra_lock_create ("shadow.LCK", 0);
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;
105 if (!(zh->res = res_open (zh->configName)))
107 logf (LOG_WARN, "Failed to read resources `%s'", zh->configName);
112 zebra_mutex_cond_init (&zh->session_lock);
113 if (!res_get (zh->res, "passwd"))
114 zh->passwd_db = NULL;
117 zh->passwd_db = passwd_db_open ();
119 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
121 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
127 static int zebra_register_activate (ZebraHandle zh, int rw, int useshadow)
129 ZebraService zs = zh->service;
130 int record_compression = REC_COMPRESS_NONE;
131 char *recordCompression = 0;
133 yaz_log (LOG_LOG, "zebra_open_register_activate rw = %d useshadow=%d",
136 zs->dh = data1_create ();
139 zs->bfs = bfs_create (res_get (zs->res, "register"));
142 data1_destroy(zs->dh);
145 bf_lockDir (zs->bfs, res_get (zs->res, "lockDir"));
147 bf_cache (zs->bfs, res_get (zs->res, "shadow"));
148 data1_set_tabpath (zs->dh, res_get(zs->res, "profilePath"));
149 zs->recTypes = recTypes_init (zs->dh);
150 recTypes_default_handlers (zs->recTypes);
152 zs->zebra_maps = zebra_maps_open (zs->res);
153 zs->rank_classes = NULL;
168 zebraRankInstall (zs, rank1_class);
170 recordCompression = res_get_def (zh->service->res,
171 "recordCompression", "none");
172 if (!strcmp (recordCompression, "none"))
173 record_compression = REC_COMPRESS_NONE;
174 if (!strcmp (recordCompression, "bzip2"))
175 record_compression = REC_COMPRESS_BZIP2;
177 if (!(zs->records = rec_open (zs->bfs, rw, record_compression)))
179 logf (LOG_WARN, "rec_open");
184 zs->matchDict = dict_open (zs->bfs, GMATCH_DICT, 50, 1, 0);
186 if (!(zs->dict = dict_open (zs->bfs, FNAME_DICT, 80, rw, 0)))
188 logf (LOG_WARN, "dict_open");
191 if (!(zs->sortIdx = sortIdx_open (zs->bfs, rw)))
193 logf (LOG_WARN, "sortIdx_open");
196 if (res_get_match (zs->res, "isam", "s", ISAM_DEFAULT))
198 struct ISAMS_M_s isams_m;
199 if (!(zs->isams = isams_open (zs->bfs, FNAME_ISAMS, rw,
200 key_isams_m(zs->res, &isams_m))))
202 logf (LOG_WARN, "isams_open");
207 else if (res_get_match (zs->res, "isam", "i", ISAM_DEFAULT))
209 if (!(zs->isam = is_open (zs->bfs, FNAME_ISAM, key_compare, rw,
210 sizeof (struct it_key), zs->res)))
212 logf (LOG_WARN, "is_open");
216 else if (res_get_match (zs->res, "isam", "c", ISAM_DEFAULT))
218 struct ISAMC_M_s isamc_m;
219 if (!(zs->isamc = isc_open (zs->bfs, FNAME_ISAMC,
220 rw, key_isamc_m(zs->res, &isamc_m))))
222 logf (LOG_WARN, "isc_open");
226 else if (res_get_match (zs->res, "isam", "d", ISAM_DEFAULT))
228 struct ISAMD_M_s isamd_m;
230 if (!(zs->isamd = isamd_open (zs->bfs, FNAME_ISAMD,
231 rw, key_isamd_m(zs->res, &isamd_m))))
233 logf (LOG_WARN, "isamd_open");
238 zs->zei = zebraExplain_open (zs->records, zs->dh,
243 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
247 yaz_log (LOG_LOG, "zebra_register_activate ok");
251 void zebra_admin_shutdown (ZebraHandle zh)
253 zebra_mutex_cond_lock (&zh->service->session_lock);
254 zh->service->stop_flag = 1;
255 if (!zh->service->sessions)
256 zebra_register_deactivate(zh);
257 zh->service->active = 0;
258 zebra_mutex_cond_unlock (&zh->service->session_lock);
261 void zebra_admin_start (ZebraHandle zh)
263 ZebraService zs = zh->service;
265 zebra_mutex_cond_lock (&zs->session_lock);
267 zh->service->active = 1;
268 zebra_mutex_cond_unlock (&zs->session_lock);
271 static int zebra_register_deactivate (ZebraHandle zh)
273 ZebraService zs = zh->service;
277 yaz_log(LOG_LOG, "zebra_register_deactivate (ignored since active=%d)",
281 yaz_log(LOG_LOG, "zebra_register_deactivate");
285 zebraExplain_close (zs->zei, 0);
286 dict_close (zs->dict);
288 dict_close (zs->matchDict);
289 sortIdx_close (zs->sortIdx);
291 isams_close (zs->isams);
296 isc_close (zs->isamc);
298 isamd_close (zs->isamd);
300 rec_close (&zs->records);
302 resultSetInvalidate (zh);
304 recTypes_destroy (zs->recTypes);
305 zebra_maps_close (zs->zebra_maps);
306 zebraRankDestroy (zs);
307 bfs_destroy (zs->bfs);
308 data1_destroy (zs->dh);
311 passwd_db_close (zs->passwd_db);
316 void zebra_stop(ZebraService zs)
320 yaz_log (LOG_LOG, "zebra_stop");
322 zebra_mutex_cond_lock (&zs->session_lock);
325 zebra_register_deactivate(zs->sessions);
326 zebra_close (zs->sessions);
329 zebra_mutex_cond_unlock (&zs->session_lock);
331 zebra_mutex_cond_destroy (&zs->session_lock);
334 xfree (zs->configName);
338 void zebra_close (ZebraHandle zh)
341 struct zebra_session **sp;
347 yaz_log (LOG_LOG, "zebra_close zh=%p", zh);
350 resultSetDestroy (zh, -1, 0, 0);
358 xfree (zh->admin_databaseName);
359 zebra_mutex_cond_lock (&zs->session_lock);
360 zebra_lock_destroy (zh->lock_normal);
361 zebra_lock_destroy (zh->lock_shadow);
373 // if (!zs->sessions && zs->stop_flag)
374 // zebra_register_deactivate(zs);
375 zebra_mutex_cond_unlock (&zs->session_lock);
379 struct map_baseinfo {
385 char **new_basenames;
389 void map_basenames_func (void *vp, const char *name, const char *value)
391 struct map_baseinfo *p = (struct map_baseinfo *) vp;
393 char fromdb[128], todb[8][128];
396 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
397 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
398 todb[5], todb[6], todb[7]);
402 for (i = 0; i<p->num_bases; i++)
403 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
406 for (i = 0; i < no; i++)
408 if (p->new_num_bases == p->new_num_max)
410 p->new_basenames[(p->new_num_bases)++] =
411 nmem_strdup (p->mem, todb[i]);
417 void map_basenames (ZebraHandle zh, ODR stream,
418 int *num_bases, char ***basenames)
420 struct map_baseinfo info;
421 struct map_baseinfo *p = &info;
425 info.num_bases = *num_bases;
426 info.basenames = *basenames;
427 info.new_num_max = 128;
428 info.new_num_bases = 0;
429 info.new_basenames = (char **)
430 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
431 info.mem = stream->mem;
433 res_trav (zh->service->res, "mapdb", &info, map_basenames_func);
435 for (i = 0; i<p->num_bases; i++)
436 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
438 p->new_basenames[(p->new_num_bases)++] =
439 nmem_strdup (p->mem, p->basenames[i]);
441 *num_bases = info.new_num_bases;
442 *basenames = info.new_basenames;
443 for (i = 0; i<*num_bases; i++)
444 logf (LOG_LOG, "base %s", (*basenames)[i]);
447 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
448 Z_RPNQuery *query, int num_bases, char **basenames,
452 if (zebra_begin_read (zh))
454 map_basenames (zh, stream, &num_bases, &basenames);
455 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
459 logf(LOG_APP,"SEARCH:%d:",zh->hits);
462 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
463 const char *setname, Z_RecordComposition *comp,
464 oid_value input_format, int num_recs,
465 ZebraRetrievalRecord *recs)
470 if (zebra_begin_read (zh))
473 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
474 for (i = 0; i<num_recs; i++)
475 pos_array[i] = recs[i].position;
476 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
479 logf (LOG_DEBUG, "zebraPosSetCreate error");
481 zh->errString = nmem_strdup (stream->mem, setname);
485 for (i = 0; i<num_recs; i++)
490 recs[i].format = VAL_SUTRS;
491 recs[i].len = strlen(poset[i].term);
492 recs[i].buf = poset[i].term;
493 recs[i].base = poset[i].db;
495 else if (poset[i].sysno)
498 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
499 stream, input_format, comp,
500 &recs[i].format, &recs[i].buf,
503 recs[i].errString = NULL;
509 sprintf (num_str, "%d", pos_array[i]);
511 zh->errString = nmem_strdup (stream->mem, num_str);
515 zebraPosSetDestroy (zh, poset, num_recs);
521 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
522 oid_value attributeset,
523 int num_bases, char **basenames,
524 int *position, int *num_entries, ZebraScanEntry **entries,
527 if (zebra_begin_read (zh))
533 map_basenames (zh, stream, &num_bases, &basenames);
534 rpn_scan (zh, stream, zapt, attributeset,
535 num_bases, basenames, position,
536 num_entries, entries, is_partial);
540 void zebra_sort (ZebraHandle zh, ODR stream,
541 int num_input_setnames, const char **input_setnames,
542 const char *output_setname, Z_SortKeySpecList *sort_sequence,
545 if (zebra_begin_read (zh))
547 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
548 output_setname, sort_sequence, sort_status);
552 int zebra_deleleResultSet(ZebraHandle zh, int function,
553 int num_setnames, char **setnames,
557 if (zebra_begin_read(zh))
558 return Z_DeleteStatus_systemProblemAtTarget;
561 case Z_DeleteRequest_list:
562 resultSetDestroy (zh, num_setnames, setnames, statuses);
564 case Z_DeleteRequest_all:
565 resultSetDestroy (zh, -1, 0, statuses);
569 status = Z_DeleteStatus_success;
570 for (i = 0; i<num_setnames; i++)
571 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
572 status = statuses[i];
576 int zebra_errCode (ZebraHandle zh)
581 const char *zebra_errString (ZebraHandle zh)
583 return diagbib1_str (zh->errCode);
586 char *zebra_errAdd (ZebraHandle zh)
588 return zh->errString;
591 int zebra_hits (ZebraHandle zh)
596 int zebra_auth (ZebraService zh, const char *user, const char *pass)
598 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
600 logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS");
604 logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS");
608 void zebra_admin_import_begin (ZebraHandle zh, const char *database)
610 zebra_begin_trans (zh);
611 xfree (zh->admin_databaseName);
612 zh->admin_databaseName = xstrdup(database);
615 void zebra_admin_import_end (ZebraHandle zh)
617 zebra_end_trans (zh);
620 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
624 if (zh->service->active < 2)
626 for (i = 0; i<segment->num_segmentRecords; i++)
628 Z_NamePlusRecord *npr = segment->segmentRecords[i];
629 const char *databaseName = npr->databaseName;
632 databaseName = zh->admin_databaseName;
633 printf ("--------------%d--------------------\n", i);
634 if (npr->which == Z_NamePlusRecord_intermediateFragment)
636 Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
637 if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
639 Odr_oct *oct = fragment->u.notExternallyTagged;
640 printf ("%.*s", (oct->len > 100 ? 100 : oct->len) ,
644 extract_rec_in_mem (zh, "grs.sgml",
652 0 /* match criteria */);
658 void zebra_admin_create (ZebraHandle zh, const char *database)
662 zebra_begin_trans (zh);
665 /* announce database */
666 if (zebraExplain_newDatabase (zh->service->zei, database, 0
667 /* explainDatabase */))
670 zh->errString = "Database already exist";
672 zebra_end_trans (zh);
675 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
676 const char *input_str, int input_len,
677 char *output_str, int output_len)
680 if (!zh->service->zebra_maps)
682 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, "",
683 input_str, input_len);
686 if (wrbuf_len(wrbuf) >= output_len)
688 if (wrbuf_len(wrbuf))
689 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
690 output_str[wrbuf_len(wrbuf)] = '\0';
691 return wrbuf_len(wrbuf);
695 void zebra_set_state (int val, int seqno)
698 FILE *f = fopen ("state.LCK", "w");
699 fprintf (f, "%c %d %ld\n", val, seqno, p);
703 void zebra_get_state (char *val, int *seqno)
705 FILE *f = fopen ("state.LCK", "r");
712 fscanf (f, "%c %d", val, seqno);
717 static int zebra_begin_read (ZebraHandle zh)
726 if (zh->trans_no != 1)
728 zebra_flush_reg (zh);
732 zebra_get_state (&val, &seqno);
735 if (seqno != zh->seqno)
737 yaz_log (LOG_LOG, "reopen seqno cur/old %d/%d", seqno, zh->seqno);
740 else if (zh->last_val != val)
742 yaz_log (LOG_LOG, "reopen last cur/old %d/%d", val, zh->last_val);
749 zebra_lock_r (zh->lock_shadow);
751 zebra_lock_r (zh->lock_normal);
756 zebra_register_deactivate (zh);
758 zebra_register_activate (zh, 0, val == 'c' ? 1 : 0);
762 static void zebra_end_read (ZebraHandle zh)
766 if (zh->trans_no != 0)
769 zebra_unlock (zh->lock_normal);
770 zebra_unlock (zh->lock_shadow);
773 void zebra_begin_trans (ZebraHandle zh)
781 if (zh->trans_no != 1)
786 yaz_log (LOG_LOG, "zebra_begin_trans");
792 rval = res_get (zh->service->res, "shadow");
794 for (pass = 0; pass < 2; pass++)
798 zebra_lock_r (zh->lock_normal);
799 zebra_lock_w (zh->lock_shadow);
803 zebra_lock_w (zh->lock_normal);
804 zebra_lock_w (zh->lock_shadow);
807 zebra_get_state (&val, &seqno);
810 yaz_log (LOG_LOG, "previous transaction didn't finish commit");
811 zebra_unlock (zh->lock_shadow);
812 zebra_unlock (zh->lock_normal);
820 BFiles bfs = bfs_create (res_get (zh->service->res, "shadow"));
821 yaz_log (LOG_LOG, "previous transaction didn't reach commit");
822 bf_commitClean (bfs, rval);
827 yaz_log (LOG_WARN, "your previous transaction didn't finish");
834 yaz_log (LOG_FATAL, "zebra_begin_trans couldn't finish commit");
838 zebra_set_state ('d', seqno);
840 zebra_register_activate (zh, 1, rval ? 1 : 0);
844 void zebra_end_trans (ZebraHandle zh)
851 if (zh->trans_no != 0)
854 yaz_log (LOG_LOG, "zebra_end_trans");
855 rval = res_get (zh->service->res, "shadow");
857 zebra_flush_reg (zh);
859 zebra_register_deactivate (zh);
861 zebra_get_state (&val, &seqno);
864 BFiles bfs = bfs_create (res_get (zh->service->res, "shadow"));
865 bf_commitClean (bfs, rval);
870 zebra_set_state ('o', seqno);
872 zebra_unlock (zh->lock_shadow);
873 zebra_unlock (zh->lock_normal);
877 logf (LOG_LOG, "user/system: %ld/%ld",
878 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
879 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
884 void zebra_repository_update (ZebraHandle zh)
886 zebra_begin_trans (zh);
887 logf (LOG_LOG, "updating %s", zh->rGroup.path);
888 repositoryUpdate (zh);
889 zebra_end_trans (zh);
892 void zebra_repository_delete (ZebraHandle zh)
894 logf (LOG_LOG, "deleting %s", zh->rGroup.path);
895 repositoryDelete (zh);
898 void zebra_repository_show (ZebraHandle zh)
903 void zebra_commit (ZebraHandle zh)
907 const char *rval = res_get (zh->service->res, "shadow");
912 logf (LOG_WARN, "Cannot perform commit");
913 logf (LOG_WARN, "No shadow area defined");
917 zebra_lock_w (zh->lock_normal);
918 zebra_lock_r (zh->lock_shadow);
920 bfs = bfs_create (res_get (zh->service->res, "register"));
922 zebra_get_state (&val, &seqno);
925 bf_cache (bfs, rval);
926 if (bf_commitExists (bfs))
928 zebra_set_state ('c', seqno);
930 logf (LOG_LOG, "commit start");
935 logf (LOG_LOG, "commit clean");
936 bf_commitClean (bfs, rval);
938 zebra_set_state ('o', seqno);
942 logf (LOG_LOG, "nothing to commit");
946 zebra_unlock (zh->lock_shadow);
947 zebra_unlock (zh->lock_normal);
950 void zebra_init (ZebraHandle zh)
952 const char *rval = res_get (zh->service->res, "shadow");
955 bfs = bfs_create (res_get (zh->service->res, "register"));
957 bf_cache (bfs, rval);
961 zebra_set_state ('o', 0);
964 void zebra_compact (ZebraHandle zh)
966 BFiles bfs = bfs_create (res_get (zh->service->res, "register"));
971 int zebra_record_insert (ZebraHandle zh, const char *buf, int len)
974 zebra_begin_trans (zh);
975 extract_rec_in_mem (zh, "grs.sgml",
977 "Default", /* database */
983 0 /* match criteria */);
984 zebra_end_trans (zh);