Package fr.dyade.aaa.util
Class AbstractTransaction
- java.lang.Object
-
- fr.dyade.aaa.util.BaseTransaction
-
- fr.dyade.aaa.util.AbstractTransaction
-
- All Implemented Interfaces:
Transaction
- Direct Known Subclasses:
ATransaction,DBTransaction,NGTransaction,NTransaction
public abstract class AbstractTransaction extends BaseTransaction
The AbstractTransaction class implements the common part of the Transaction Transaction interface. A transaction implementation only needs to define several methods: saveInLog, loadByteArray, delete and commit.- See Also:
Transaction
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classAbstractTransaction.Context
-
Field Summary
Fields Modifier and Type Field Description protected ExponentialMovingAverageaverageprotected Filedirprivate static byteENCODING_TAGprivate static byteJAVA_SERIALIZATION_TAGprotected static Loggerlogmonprotected static booleanMONITORprivate booleanonlyUseJavaSerializationprotected static byte[]OOS_STREAM_HEADERThe OOS_STREAM_HEADER allows to reset an ObjectOutputStream built on top of a ByteArrayOutputStream.protected static intPERIODSprotected ThreadLocal<AbstractTransaction.Context>perThreadContextThreadLocal variable used to get the log to associate state with each thread.protected intphaseprotected longstartTime-
Fields inherited from class fr.dyade.aaa.util.BaseTransaction
DEBUG
-
-
Constructor Summary
Constructors Constructor Description AbstractTransaction()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidbegin()Start a transaction validation, the validation phase needs 3 phases: begin, commit and release.booleancontainsOperations()Indicates whether some operations have been done in this transaction.voidcreate(Serializable obj, String name)Register the state of a newly created object in the current transaction.voidcreate(Serializable obj, String dirName, String name)Register the state of a newly created object in the current transaction.voidcreateByteArray(byte[] buf, String name)Register a new byte array in the current transaction.voidcreateByteArray(byte[] buf, String dirName, String name)Register a new byte array in the current transaction.private Objectdecode(byte[] buf, int offset)voiddelete(String name)Deletes the specified object.private Objectdeserialize(byte[] buf, int offset)longgetAverageCommitTime()intgetOperationCount()Returns the number of operations to be committed.intgetPhase()Returns the transaction state.StringgetPhaseInfo()Returns a string representation of the transaction state.longgetStartTime()Returns the starting time.voidinit(String path)Initializes the atomic storage component.abstract voidinitRepository()private EncodableisEncodable(Object object)booleanisPersistent()Tests if the Transaction component is persistent.Objectload(String name)Load the specified object.Objectload(String dirName, String name)Load the specified object.voidloadAll(String prefix, Map map)Fills the map with all objects of the component whose name begins with the prefix.byte[]loadByteArray(String name)Load the specified byte array.protected ObjectloadFromByteArray(byte[] buf)voidrelease()Release the mutual exclusion.voidsave(Serializable obj, String name)Register the modified state of an object in the current transaction.voidsave(Serializable obj, String dirName, String name)Register the modified state of an object in the current transaction.voidsave(Serializable obj, String dirName, String name, boolean first)Register the state of an object in the current transaction.voidsaveByteArray(byte[] buf, String name)Register a modified byte array in the current transaction.voidsaveByteArray(byte[] buf, String dirName, String name)Register a modified byte array in the current transaction.voidsaveByteArray(byte[] buf, String dirName, String name, boolean copy, boolean first)Register a modified byte array in the current transaction.protected abstract voidsaveInLog(byte[] buf, String dirName, String name, Hashtable log, boolean copy, boolean first)Register the specified object in transaction log.private byte[]serialize(Serializable obj, AbstractTransaction.Context ctx)protected abstract voidsetPhase(int newPhase)Changes the transaction state.booleanuseLoadAll()Returns true if this Transaction implementation implements an optimized loadAll method.-
Methods inherited from class fr.dyade.aaa.util.BaseTransaction
dumpObjectList, freeze, getBoolean, getInteger, getInteger, getObject, getObjectList, getProperty, getProperty, loadProperties, saveProperties
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface fr.dyade.aaa.util.Transaction
backup, close, commit, delete, getCommitCount, getList, loadByteArray, stop
-
-
-
-
Field Detail
-
logmon
protected static final Logger logmon
-
MONITOR
protected static final boolean MONITOR
-
PERIODS
protected static final int PERIODS
-
average
protected final ExponentialMovingAverage average
-
JAVA_SERIALIZATION_TAG
private static final byte JAVA_SERIALIZATION_TAG
- See Also:
- Constant Field Values
-
ENCODING_TAG
private static final byte ENCODING_TAG
- See Also:
- Constant Field Values
-
onlyUseJavaSerialization
private boolean onlyUseJavaSerialization
-
startTime
protected long startTime
-
phase
protected int phase
-
dir
protected File dir
-
perThreadContext
protected ThreadLocal<AbstractTransaction.Context> perThreadContext
ThreadLocal variable used to get the log to associate state with each thread. The log contains all operations do by the current thread since the lastcommit. On commit, its content is added to current log (clog, memory + disk), then it is freed.
-
OOS_STREAM_HEADER
protected static final byte[] OOS_STREAM_HEADER
The OOS_STREAM_HEADER allows to reset an ObjectOutputStream built on top of a ByteArrayOutputStream.
-
-
Method Detail
-
getAverageCommitTime
public long getAverageCommitTime()
-
getStartTime
public long getStartTime()
Returns the starting time.- Returns:
- The starting time.
-
isPersistent
public final boolean isPersistent()
Tests if the Transaction component is persistent.- Returns:
- true.
-
getPhase
public final int getPhase()
Returns the transaction state.- Returns:
- the transaction state.
- See Also:
Transaction.getPhase()
-
getPhaseInfo
public final String getPhaseInfo()
Returns a string representation of the transaction state.- Returns:
- the string representation of the transaction state.
- See Also:
Transaction.getPhaseInfo()
-
setPhase
protected abstract void setPhase(int newPhase) throws IOExceptionChanges the transaction state.- Parameters:
newPhase- the new transaction state.- Throws:
IOException- an error occurs.
-
initRepository
public abstract void initRepository() throws IOException- Throws:
IOException
-
init
public final void init(String path) throws IOException
Description copied from interface:TransactionInitializes the atomic storage component.- Parameters:
path- the path of storage directory.- Throws:
IOException- an error occurs.
-
begin
public final void begin() throws IOExceptionStart a transaction validation, the validation phase needs 3 phases: begin, commit and release. The begin ensure the mutual exclusion of the current transaction.- Throws:
IOException- an error occurs.- See Also:
Transaction.begin()
-
create
public final void create(Serializable obj, String name) throws IOException
Register the state of a newly created object in the current transaction.- Parameters:
obj- the object to store.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.create(java.io.Serializable, java.lang.String)
-
create
public final void create(Serializable obj, String dirName, String name) throws IOException
Register the state of a newly created object in the current transaction.- Parameters:
obj- the object to store.dirName- the directory name of the object.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.create(java.io.Serializable, java.lang.String, java.lang.String)
-
save
public final void save(Serializable obj, String name) throws IOException
Register the modified state of an object in the current transaction.- Parameters:
obj- the object to store.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.save(java.io.Serializable, java.lang.String)
-
save
public final void save(Serializable obj, String dirName, String name) throws IOException
Register the modified state of an object in the current transaction.- Parameters:
obj- the object to store.dirName- the directory name of the object.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.save(java.io.Serializable, java.lang.String, java.lang.String)
-
serialize
private byte[] serialize(Serializable obj, AbstractTransaction.Context ctx) throws IOException
- Throws:
IOException
-
save
public final void save(Serializable obj, String dirName, String name, boolean first) throws IOException
Register the state of an object in the current transaction.- Parameters:
obj- the object to store.dirName- the directory name of the object.name- the name of the object.first- the object is a new one.- Throws:
IOException- an error occurs.- See Also:
Transaction.save(java.io.Serializable, java.lang.String, java.lang.String, boolean)
-
createByteArray
public final void createByteArray(byte[] buf, String name) throws IOExceptionRegister a new byte array in the current transaction.- Parameters:
buf- the byte array to store.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.createByteArray(byte[], java.lang.String)
-
createByteArray
public final void createByteArray(byte[] buf, String dirName, String name) throws IOExceptionRegister a new byte array in the current transaction.- Parameters:
buf- the byte array to store.dirName- the directory name of the object.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.createByteArray(byte[], java.lang.String, java.lang.String)
-
saveByteArray
public final void saveByteArray(byte[] buf, String name) throws IOExceptionRegister a modified byte array in the current transaction.- Parameters:
buf- the byte array to store.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.saveByteArray(byte[], java.lang.String)
-
saveByteArray
public final void saveByteArray(byte[] buf, String dirName, String name) throws IOExceptionRegister a modified byte array in the current transaction.- Parameters:
buf- the byte array to store.dirName- the directory name of the object.name- the name of the object.- Throws:
IOException- an error occurs.- See Also:
Transaction.saveByteArray(byte[], java.lang.String, java.lang.String)
-
saveByteArray
public final void saveByteArray(byte[] buf, String dirName, String name, boolean copy, boolean first) throws IOExceptionRegister a modified byte array in the current transaction.- Parameters:
buf- the byte array to store.dirName- the directory name of the object.name- the name of the object.copy- the byte array can be modified, copy it.first- the object is a new one.- Throws:
IOException- an error occurs.- See Also:
Transaction.saveByteArray(byte[], java.lang.String, java.lang.String, boolean, boolean)
-
saveInLog
protected abstract void saveInLog(byte[] buf, String dirName, String name, Hashtable log, boolean copy, boolean first) throws IOExceptionRegister the specified object in transaction log.- Parameters:
buf- the byte array containing the state of the object.dirName- the directory name of the object.name- the name of the object.log- the log to use.copy- the byte array can be modified, copy it.first- the object is a new one.- Throws:
IOException- an error occurs.
-
load
public final Object load(String name) throws IOException, ClassNotFoundException
Load the specified object.- Parameters:
name- the name of the object.- Returns:
- the loaded object.
- Throws:
IOException- an error occurs.ClassNotFoundException- an error occurs.- See Also:
Transaction.load(java.lang.String)
-
load
public final Object load(String dirName, String name) throws IOException, ClassNotFoundException
Load the specified object.- Parameters:
dirName- the directory name of the object.name- the name of the object.- Returns:
- the loaded object.
- Throws:
IOException- an error occurs.ClassNotFoundException- an error occurs.- See Also:
Transaction.load(java.lang.String, java.lang.String)
-
useLoadAll
public boolean useLoadAll()
Returns true if this Transaction implementation implements an optimized loadAll method.- Returns:
- false.
-
loadAll
public void loadAll(String prefix, Map map)
Fills the map with all objects of the component whose name begins with the prefix.- Parameters:
prefix- The prefix of searched objects.map- The map of corresponding objects.
-
loadFromByteArray
protected final Object loadFromByteArray(byte[] buf) throws IOException, ClassNotFoundException
- Throws:
IOExceptionClassNotFoundException
-
deserialize
private Object deserialize(byte[] buf, int offset) throws IOException, ClassNotFoundException
- Throws:
IOExceptionClassNotFoundException
-
decode
private Object decode(byte[] buf, int offset) throws IOException
- Throws:
IOException
-
loadByteArray
public final byte[] loadByteArray(String name) throws IOException
Load the specified byte array.- Parameters:
name- the name of the object.- Returns:
- the loaded byte array.
- Throws:
IOException- an error occurs.- See Also:
Transaction.loadByteArray(java.lang.String)
-
delete
public final void delete(String name)
Deletes the specified object.- Parameters:
name- the name of the object.- See Also:
Transaction.delete(java.lang.String)
-
release
public void release() throws IOExceptionRelease the mutual exclusion.- Throws:
IOException- an error occurs.- See Also:
Transaction.release()
-
containsOperations
public boolean containsOperations()
Indicates whether some operations have been done in this transaction.- Returns:
- true if some operations are waiting for commit in this transaction.
-
getOperationCount
public int getOperationCount()
Returns the number of operations to be committed.- Returns:
- the number of operations to be committed.
-
-