Example #1
0
        /// <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);
            }
        }
Example #2
0
        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)));
            }
        }