/// <summary> /// Called when a job completes successfully so that the header can be written. /// </summary> /// <param name="itemToLog"></param> public void LogEndSuccessfulTransaction(ILoggable itemToLog) { var endPosition = (ulong) _persistenceManager.GetFileLength(GetTransactionLogFile()); var contentLength = endPosition - _currentTransactionDataStartPosition; using (var stream = _persistenceManager.GetOutputStream(GetTransactionLogHeaderFile(), FileMode.Append)) { var transactionHeader = new TransactionInfo(itemToLog.TransactionId, TransactionStatus.CompletedOk, itemToLog.TransactionType, _currentTransactionDataStartPosition, contentLength, DateTime.UtcNow); using(var binaryWriter = new BinaryWriter(stream)) { transactionHeader.Save(binaryWriter); } } }
/// <summary> /// Return an enumeration of the most recent transactions in the transaction log in /// time order (oldest first) /// </summary> /// <param name="maxCount">The maximum number of transactions to return</param> /// <param name="ts">The maximum age of transaction to return</param> /// <returns>An enumeration of the <paramref name="maxCount"/> most recent transactions that match the age filter</returns> public List <ITransactionInfo> GetTransactionList(int maxCount, TimeSpan ts) { if (!_persistenceManager.FileExists(GetTransactionLogHeaderFile())) { return(new List <ITransactionInfo>()); } using (var stream = _persistenceManager.GetInputStream(GetTransactionLogHeaderFile())) { long firstRecordOffset = maxCount * TransactionInfo.TransactionInfoRecordSize; if (firstRecordOffset > stream.Length) { stream.Seek(0, SeekOrigin.Begin); } else { stream.Seek(firstRecordOffset, SeekOrigin.End); } var reader = new BinaryReader(stream); var oldestToReturn = DateTime.UtcNow.Subtract(ts); var ret = new List <ITransactionInfo>(maxCount); while (true) { try { var txnInfo = TransactionInfo.Load(reader); if (txnInfo.TransactionStartTime > oldestToReturn) { ret.Add(txnInfo); } } catch (EndOfStreamException) { break; } } return(ret); } }
/// <summary> /// Called when a job completes successfully so that the header can be written. /// </summary> /// <param name="itemToLog"></param> public void LogEndSuccessfulTransaction(ILoggable itemToLog) { if (!IsEnabled) { return; } var endPosition = (ulong)_persistenceManager.GetFileLength(GetTransactionLogFile()); var contentLength = endPosition - _currentTransactionDataStartPosition; using (var stream = _persistenceManager.GetOutputStream(GetTransactionLogHeaderFile(), FileMode.Append)) { var transactionHeader = new TransactionInfo(itemToLog.TransactionId, TransactionStatus.CompletedOk, itemToLog.TransactionType, _currentTransactionDataStartPosition, contentLength, DateTime.UtcNow); using (var binaryWriter = new BinaryWriter(stream)) { transactionHeader.Save(binaryWriter); } } }
public ITransactionInfo ReadTransactionInfo(int transactionNumber) { if (!_persistenceManager.FileExists(GetTransactionLogHeaderFile())) { return(null); } using (var stream = _persistenceManager.GetInputStream(GetTransactionLogHeaderFile())) { var readPosition = transactionNumber * TransactionInfo.TransactionInfoRecordSize; // check if this is a valid transaction number, return null if beyond end of file. if (readPosition > stream.Length) { return(null); } // seek to position stream.Seek(-readPosition, SeekOrigin.End); // read return(TransactionInfo.Load(new BinaryReader(stream))); } }
/// <summary> /// Loads a TransactionInfo data structure from the current position in the specified stream /// </summary> /// <param name="dataStream">The data stream to read from</param> /// <returns>The TransactionInfo data structure read.</returns> public static TransactionInfo Load(BinaryReader dataStream) { var ret = new TransactionInfo(); ret.Read(dataStream); return ret; }