Example #1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="syncLock">The thread synchronization object.</param>
 /// <param name="mode">The log's current <see cref="OperationLogMode" />.</param>
 internal MemoryOperationLog(object syncLock, OperationLogMode mode)
 {
     this.syncLock   = syncLock;
     this.isOpen     = true;
     this.mode       = mode;
     this.operations = new List <IOperation>();
 }
Example #2
0
        private OperationLogMode mode;                          // The current mode

        /// <summary>
        /// Opens or creates a file operation log file.
        /// </summary>
        /// <param name="path">The path to the log file.</param>
        /// <param name="transactionID">The log's transaction <see cref="Guid" />.</param>
        /// <remarks>
        /// New logs will be created in <see cref="OperationLogMode.Undo" /> mode.
        /// </remarks>
        public FileOperationLog(string path, Guid transactionID)
        {
            this.path = Path.GetFullPath(path);
            this.file = new EnhancedFileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            if (file.Length == 0)
            {
                // Initialize a new file

                file.WriteInt32(Magic);
                file.WriteInt32(0);
                file.WriteInt32(0);
                file.WriteInt32((int)OperationLogMode.Undo);
                file.WriteBytesNoLen(transactionID.ToByteArray());
                file.Flush();
            }
            else
            {
                // Open an existing file.

                try
                {
                    if (file.ReadInt32() != Magic || // Magic number
                        file.ReadInt32() != 0)
                    {                                // Format Versopn
                        throw new Exception();
                    }

                    file.ReadInt32();                   // Reserved

                    switch (file.ReadInt32())           // Mode
                    {
                    case (int)OperationLogMode.Undo:

                        mode = OperationLogMode.Undo;
                        break;

                    case (int)OperationLogMode.Redo:

                        mode = OperationLogMode.Redo;
                        break;

                    default:

                        throw new Exception();
                    }

                    this.transactionID = new Guid(file.ReadBytes(16));
                    if (transactionID != this.transactionID)
                    {
                        throw new Exception();
                    }
                }
                catch
                {
                    throw new TransactionException(CorruptMsg);
                }
            }
        }