head     1.7;
branch   ;
access   ;
symbols  Version_2_1:1.7 C_Demo_1:1.3;
locks    ; strict;
comment  @ * @;


1.7
date     89.10.03.17.11.54;  author hirohama;  state Exp;
branches ;
next     1.6;

1.6
date     89.09.21.23.47.30;  author hirohama;  state Exp;
branches ;
next     1.5;

1.5
date     89.09.21.19.04.08;  author hirohama;  state Exp;
branches ;
next     1.4;

1.4
date     89.09.13.11.02.08;  author hirohama;  state Exp;
branches ;
next     1.3;

1.3
date     89.09.05.17.30.05;  author mao;  state C_Demo_1;
branches ;
next     1.2;

1.2
date     89.02.02.16.35.54;  author dillon;  state Stab;
branches ;
next     1.1;

1.1
date     89.01.17.05.59.02;  author cimarron;  state Exp;
branches ;
next     ;


desc
@@


1.7
log
@bug fix, floor returns double
@
text
@/*
 * xid.c --
 *	POSTGRES transaction identifier code.
 */

#include "c.h"

RcsId("$Header: RCS/xid.c,v 1.6 89/09/21 23:47:30 hirohama Exp Locker: hirohama $");

#include <math.h>	/* for floor ... */

#include "bit.h"
#include "clib.h"
#include "log.h"
#include "palloc.h"
#include "postgres.h"	/* XXX for XID, remove this when XID -> TransactionId */
#include "tim.h"

#include "xid.h"

/* ----------------------------------------------------------------
 *	transaction system constants
 * ----------------------------------------------------------------
 */

#define TransactionMultiplierPerByte	(1 << BitsPerByte)

#define TransactionsPerSecondAdjustment	TransactionMultiplierPerByte	

typedef double			TransactionIdValueData;
typedef TransactionIdValueData	*TransactionIdValue;

static TransactionIdData	NullTransactionIdData = { { 0, 0, 0, 0, 0 } };

#define NullTransactionIdValue	0.0

TransactionId NullTransactionId = &NullTransactionIdData;

static TransactionIdData TransactionIdOneData = { { 0, 0, 0, 0, 1 } };

TransactionId AmiTransactionId = &TransactionIdOneData;

/* ----------------------------------------------------------------
 * TransactionIdValueIsValid --
 *	True iff transaction identifier value is valid.
 * ----------------------------------------------------------------
 */
extern
bool
TransactionIdValueIsValid ARGS((
	TransactionIdValue	value
));

/* ----------------------------------------------------------------
 * StringGetTransactionIdValue --
 *	Returns transaction identifier value associated with a string.
 * ----------------------------------------------------------------
 */
extern
void
StringSetTransactionIdValue ARGS((
	String			representation,
	TransactionIdValue	value
));

/* ----------------------------------------------------------------
 * TransactionIdValueFormString --
 *	Returns string representation for a transaction identifier value.
 * ----------------------------------------------------------------
 */
extern
String
TransactionIdValueFormString ARGS((
	TransactionIdValue	value
));

/* ----------------------------------------------------------------
 * TransactionIdValueSetTransactionId --
 *	Sets transaction identifier to a transaction identifier value.
 * ----------------------------------------------------------------
 */
extern
void
TransactionIdValueSetTransactionId ARGS((
	TransactionIdValue	idValue,
	TransactionId		id
));

/* ----------------------------------------------------------------
 * TransactionIdSetTransactionIdValue --
 *	Sets transaction identifier value for a transaction identifier.
 * ----------------------------------------------------------------
 */
extern
void
TransactionIdSetTransactionIdValue ARGS((
	TransactionId		id,
	TransactionIdValue	idValue
));

/* ----------------------------------------------------------------
 * TransactionIdIncrement --
 *	Increments transaction identifier.
 * ----------------------------------------------------------------
 */
extern
void
TransactionIdIncrement ARGS((
	TransactionId	transactionId
));


/* ----------------------------------------------------------------
 * 	TransactionIdIsValid
 * ----------------------------------------------------------------
 */

bool
TransactionIdIsValid(transactionId)
	TransactionId	transactionId;
{
	return ((bool)(PointerIsValid(transactionId) &&
		(transactionId->data[4] != NullTransactionId->data[4] ||
			transactionId->data[3] != NullTransactionId->data[3] ||
			transactionId->data[2] != NullTransactionId->data[2] ||
			transactionId->data[1] != NullTransactionId->data[1] ||
			transactionId->data[0] != NullTransactionId->data[0])));		
}

/* ----------------------------------------------------------------
 *	StringFormTransactionId
 * ----------------------------------------------------------------
 */

TransactionId
StringFormTransactionId(representation)
	String	representation;
{
	TransactionId		transactionId;
	TransactionIdValueData	valueData;

	StringSetTransactionIdValue(representation, &valueData);
	
	transactionId = LintCast(TransactionId,
		palloc(sizeof transactionId->data));

	TransactionIdValueSetTransactionId(&valueData, transactionId);

	return (transactionId);
}

/* ----------------------------------------------------------------
 *	TransactionIdFormString
 * ----------------------------------------------------------------
 */

String
TransactionIdFormString(transactionId)
	TransactionId	transactionId;
{
	TransactionIdValueData	valueData;
	String			representation;

	TransactionIdSetTransactionIdValue(transactionId, &valueData);

	representation = TransactionIdValueFormString(&valueData);

	return (representation);
}

/* ----------------------------------------------------------------
 *	PointerStoreInvalidTransactionId
 * ----------------------------------------------------------------
 */

void
PointerStoreInvalidTransactionId(destination)
	Pointer		destination;
{
	Assert(PointerIsValid(destination));

	MemoryCopy((String)destination, (String)NullTransactionId,
		TransactionIdDataSize);
}

/* ----------------------------------------------------------------
 *	TransactionIdStore
 * ----------------------------------------------------------------
 */

void
TransactionIdStore(transactionId, destination)
	TransactionId	transactionId;
	Pointer		destination;
{
	Assert(TransactionIdIsValid(transactionId));
	Assert(PointerIsValid(destination));

	MemoryCopy((String)destination, (String)transactionId,
		TransactionIdDataSize);
}

/* ----------------------------------------------------------------
 *	TransactionIdEquals
 * ----------------------------------------------------------------
 */

bool
TransactionIdEquals(id1, id2)
	TransactionId	id1;
	TransactionId	id2;
{
	return ((bool)(id1->data[4] == id2->data[4] &&
			id1->data[3] == id2->data[3] &&
			id1->data[2] == id2->data[2] &&
			id1->data[1] == id2->data[1] &&
			id1->data[0] == id2->data[0]));
}

/* ----------------------------------------------------------------
 *	TransactionIdIsLessThan
 * ----------------------------------------------------------------
 */

bool
TransactionIdIsLessThan(id1, id2)
	TransactionId	id1;
	TransactionId	id2;
{
	TransactionIdValueData	valueData1;
	TransactionIdValueData	valueData2;

	TransactionIdSetTransactionIdValue(id1, &valueData1);
	TransactionIdSetTransactionIdValue(id2, &valueData2);

	return ((bool)((valueData2 - valueData1) > 0.7));
}

/* ----------------------------------------------------------------
 *	xidgt
 * ----------------------------------------------------------------
 */

/*
 *	xidgt		- returns 1, iff xid1 > xid2
 *				  0  else;
 */
bool
xidgt(xid1, xid2)
XID 	xid1, xid2;
{	extern	int	strcmp();

	return( (bool) (strcmp(xid1, xid2) > 0) );
}

/* ----------------------------------------------------------------
 *	xidge
 * ----------------------------------------------------------------
 */

/*
 *	xidge		- returns 1, iff xid1 >= xid2
 *				  0  else;
 */
bool
xidge(xid1, xid2)
XID 	xid1, xid2;
{	extern	int	strcmp();

	return( (bool) (strcmp(xid1, xid2) >= 0) );
}


/* ----------------------------------------------------------------
 *	xidle
 * ----------------------------------------------------------------
 */

/*
 *	xidle		- returns 1, iff xid1 <= xid2
 *				  0  else;
 */
bool
xidle(xid1, xid2)
XID 	xid1, xid2;
{	extern	int	strcmp();

	return((bool) (strcmp(xid1, xid2) <= 0) );
}


/* ----------------------------------------------------------------
 *	xideq
 * ----------------------------------------------------------------
 */

/*
 *	xideq		- returns 1, iff xid1 == xid2
 *				  0  else;
 */
bool
xideq(xid1, xid2)
XID 	xid1, xid2;
{	extern	int	strcmp();

	return( (bool) (strcmp(xid1, xid2) == 0) );
}



/* ----------------------------------------------------------------
 *	xidmi
 * ----------------------------------------------------------------
 */

/*	xidmi		- returns the distance xid1 - xid2
 *
 */
int
xidmi(xid1, xid2)
XID	xid1, xid2;
{	/* computes the 'distance' between xid1 and xid2:
	 * if there was no xidj generated between the generation of
	 * xid1 and xid2, then the distance (xid1 - xid2) has to be 1 (!!!) */
}

/* ----------------------------------------------------------------
 *	TransactionIdValueIsValid
 * ----------------------------------------------------------------
 */

bool
TransactionIdValueIsValid(value)
	TransactionIdValue	value;
{
	return ((bool)(PointerIsValid(value) &&
		*value != NullTransactionIdValue));
}

/* ----------------------------------------------------------------
 *	StringSetTransactionIdValue
 * ----------------------------------------------------------------
 */

void
StringSetTransactionIdValue(representation, value)
	String			representation;
	TransactionIdValue	value;
{
	extern double		atof();

	*value = atof(representation);
}

/* ----------------------------------------------------------------
 *	TransactionIdValueFormString
 * ----------------------------------------------------------------
 */

String
TransactionIdValueFormString(value)
	TransactionIdValue	value;
{
	String	representation;

	/* maximum 40 bit unsigned integer representation takes 12 chars */
	representation = palloc(13);

	(void)sprintf(representation, "%.0f", *value);

	return (representation);
}

/* ----------------------------------------------------------------
 *	TransactionIdValueSetTransactionId
 * ----------------------------------------------------------------
 */

void
TransactionIdValueSetTransactionId(idValue, id)
	TransactionIdValue	idValue;
	TransactionId		id;
{
	TransactionIdValueData	valueData;
	uint8			*digitPointer;
	int16			digitsLeft;

	
	valueData = *idValue;
	digitPointer = &id->data[sizeof id->data - 1];

	for (digitsLeft = sizeof id->data - 1; digitsLeft >= 0;
			digitsLeft -= 1) {

		TransactionIdValueData	tempData;

		tempData = floor(valueData / TransactionMultiplierPerByte);
		*digitPointer = valueData -
			(TransactionMultiplierPerByte * tempData);
		valueData = tempData;
		digitPointer -= 1;
	}
}

/* ----------------------------------------------------------------
 *	TransactionIdSetTransactionIdValue
 * ----------------------------------------------------------------
 */

void
TransactionIdSetTransactionIdValue(id, idValue)
	TransactionId		id;
	TransactionIdValue	idValue;
{
	TransactionIdValueData	valueData;
	uint8			*digitPointer;
	int16			digitsLeft;
#ifdef	sun
	uint16			digit;
#endif	/* defined(sun) */

	digitPointer = &id->data[0];
	valueData = 0;

	for (digitsLeft = sizeof id->data - 1; digitsLeft >= 0;
			digitsLeft -= 1) {

		valueData *= TransactionMultiplierPerByte;
#ifdef	sun
		digit = AsUint8(*digitPointer);
		valueData += digit;
#else	/* !defined(sun) */
		valueData += AsUint8(*digitPointer);
#endif	/* !defined(sun) */
		digitPointer += 1;
	}

	*idValue = valueData;
}

/* ----------------------------------------------------------------
 *	TransactionIdIncrement
 * ----------------------------------------------------------------
 */

void
TransactionIdIncrement(transactionId)
	TransactionId	transactionId;
{
	uint8	*digitPointer;
	int16	digitsLeft;

	digitPointer = &transactionId->data[sizeof transactionId->data - 1];

	for (digitsLeft = sizeof transactionId->data - 1; digitsLeft >= 0;
			digitsLeft -= 1) {

		*digitPointer = AsUint8(1 + AsUint8(*digitPointer));

		if (!(AsUint8(*digitPointer) == 0)) {
			return;
		}
		digitPointer -= 1;
	}
	elog(FATAL, "TransactionIdIncrement: exhausted XID's");
}

/* ----------------------------------------------------------------
 *	TransactionIdGetApproximateTime
 * ----------------------------------------------------------------
 */

Time
TransactionIdGetApproximateTime(transactionId)
	TransactionId	transactionId;
{
	TransactionIdValueData	valueData;

	TransactionIdSetTransactionIdValue(transactionId, &valueData);

	valueData /= TransactionsPerSecondAdjustment;
	return((Time)valueData);
}
@


1.6
log
@readded "postgres.h" for now
@
text
@d8 3
a10 1
RcsId("$Header: RCS/xid.c,v 1.5 89/09/21 19:04:08 hirohama Exp Locker: hirohama $");
@


1.5
log
@<math.h>, "postgres.h" -> "palloc.h"
@
text
@d8 1
a8 1
RcsId("$Header: RCS/xid.c,v 1.4 89/09/13 11:02:08 hirohama Exp Locker: hirohama $");
d14 1
@


1.4
log
@TransactionIdEquals speedup!!!
@
text
@d1 1
a1 1
/* ----------------------------------------------------------------
a3 1
 * ----------------------------------------------------------------
a5 2
#include <math.h>	/* XXX */

a6 1
#include "clib.h"
d8 1
a8 2
#include "postgres.h"
#include "log.h"
d11 3
a16 2

RcsId("$Header: RCS/xid.c,v 1.3 89/09/05 17:30:05 mao C_Demo_1 Locker: hirohama $");
@


1.3
log
@Working version of C-only demo
@
text
@d20 1
a20 1
RcsId("$Header: /usr6/postgres/mao/postgres/src/utils/xact/RCS/xid.c,v 1.2 89/02/02 16:35:54 dillon Stab $");
d214 5
a218 7
	TransactionIdValueData	valueData1;
	TransactionIdValueData	valueData2;

	TransactionIdSetTransactionIdValue(id1, &valueData1);
	TransactionIdSetTransactionIdValue(id2, &valueData2);

	return ((bool)(fabs(valueData2 - valueData1) < 0.3));
@


1.2
log
@Txfer from old tree
@
text
@d20 1
a20 1
RcsId("$Header: xid.c,v 1.6 88/07/05 01:02:17 dillon Locked $");
@


1.1
log
@Initial revision
@
text
@a0 27

/*
 * 
 * POSTGRES Data Base Management System
 * 
 * Copyright (c) 1988 Regents of the University of California
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for educational, research, and non-profit purposes and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of the University of California not be used in advertising
 * or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Permission to incorporate this
 * software into commercial products can be obtained from the Campus
 * Software Office, 295 Evans Hall, University of California, Berkeley,
 * Ca., 94720 provided only that the the requestor give the University
 * of California a free licence to any derived software for educational
 * and research purposes.  The University of California makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 * 
 */



d20 1
a20 1
RcsId("$Header: xid.c,v 1.1 88/11/11 16:35:38 postgres Exp $");
@
