/**********************************************************************
 * luc_trn_info.c -- return info about lucy transaction
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_trn_info.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_trn_info.c,v 1.2 91/08/01 14:33:13 brlewis Exp Locker: brlewis $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid_luc_trn_info_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_trn_info.c,v 1.2 91/08/01 14:33:13 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <sys/errno.h>
#include "lucy/lucy.h"
#include "lucy/memory.h"

/**********************************************************************
 * luc_trn_info(lmeeting *mp, int n, ltrn **tp)
 *	caller sets mp (usually luv_curmtgp) to specify meeting
 *	caller sets n to specify transaction number
 *
 * - points *tp at ltrn structure.
 * - returns error code
 **********************************************************************/

long
luc_trn_info(mp, n, tp)
     LMEETING *mp;
     int n;
     ltrn **tp;
{
  long code;
  int hi, lo=0, med;		/* for binary search */

  /* If no ltrns have been cached yet, allocate array */
  if (!mp->trn) {
    mp->trn = NewArray(ltrn *, 1);
    if (!mp->trn) {
      *luv_context = '\0';	/* luv_context = "" */
      return((long) errno);
    }
  }

  /* Do binary search for correct trn */
  hi = mp->ntrn;
  while (hi-lo > 1) {
    med = (lo+hi)/2;
    if (mp->trn[med]->current == n) {
      /* found it! */
      *tp = mp->trn[med];
      return(0L);
    }
    if (mp->trn[med]->current < n) lo = med;
    else hi = med;
  }

  /* Binary search failed.  Enlarge array */
  mp->trn = BiggerArray(ltrn *, mp->trn, ++(mp->ntrn));
  if (!mp->trn) {
      *luv_context = '\0';	/* luv_context = "" */
    return((long) errno);
  }
  /* shift everything over to place new info in order */
  if (mp->ntrn - hi > 1) bcopy((char *) &(mp->trn[hi]),
			       (char *) &(mp->trn[hi+1]),
			       (mp->ntrn - hi - 1) * (int) sizeof(ltrn *));

  /* Allocate space and retrieve info */
  mp->trn[hi] = New(ltrn);
  if (!mp->trn[hi]) code = (long) errno;
  else dsc_get_trn_info3(&(mp->nb), n, mp->trn[hi], &code);

  /* Upon failure, close the hole in cached array */
  if (code) {
    /* shift everything back */
    if (mp->ntrn - hi > 1) bcopy((char *) &(mp->trn[hi+1]),
				 (char *) &(mp->trn[hi]),
				 (mp->ntrn - hi - 1) * (int) sizeof(ltrn *));
    mp->ntrn--;
    sprintf(luv_context, "%d", n);	/* error context */
    return(code);
  }

  /* Successfully found new trn info */
  *tp = mp->trn[hi];
  return(0L);
}
