Beispiel #1
0
        private void LoadWriteActions(string filename, bool apply)
        {
            Log4NetHelper.Instance.LogDebugMessage(string.Format("OdbTransaction: Load write actions of {0}", filename));

            CheckFileAccess(filename);
            _fsi.SetReadPosition(0);
            _isCommited       = _fsi.ReadByte() == 1;
            _creationDateTime = _fsi.ReadLong();
            var totalNumberOfWriteActions = _fsi.ReadLong();

            Log4NetHelper.Instance.LogInfoMessage(string.Concat("OdbTransaction: ", _writeActions.Count.ToString(), " write actions in file"));

            for (var i = 0; i < totalNumberOfWriteActions; i++)
            {
                var defaultWriteAction = WriteAction.Read(_fsi);

                if (apply)
                {
                    defaultWriteAction.ApplyTo(_fsiToApplyWriteActions);
                    defaultWriteAction.Clear();
                }
                else
                {
                    AddWriteAction(defaultWriteAction, false);
                }
            }

            if (apply)
            {
                _fsiToApplyWriteActions.Flush();
            }
        }
Beispiel #2
0
        /// <summary>
        ///   Adds a write action to the transaction
        /// </summary>
        /// <param name="writeAction"> The write action to be added </param>
        /// <param name="persistWriteAction"> To indicate if write action must be persisted </param>
        private void AddWriteAction(WriteAction writeAction, bool persistWriteAction)
        {
            if (OdbConfiguration.IsLoggingEnabled())
            {
                DLogger.Info(string.Format("OdbTransaction: Adding WriteAction in Transaction of session {0}", _session.GetId()));
            }

            if (writeAction.IsEmpty())
            {
                return;
            }

            CheckRollback();
            if (!_hasBeenPersisted && persistWriteAction)
            {
                Persist();
            }

            if (persistWriteAction)
            {
                writeAction.PersistMeTo(_fsi);
            }

            // Only adds the write action to the list if the transaction keeps all in
            // memory
            if (_hasAllWriteActionsInMemory)
            {
                _writeActions.Add(writeAction);
            }

            _numberOfWriteActions++;

            if (_hasAllWriteActionsInMemory &&
                _numberOfWriteActions > StorageEngineConstant.MaxNumberOfWriteObjectPerTransaction)
            {
                _hasAllWriteActionsInMemory = false;

                foreach (var defaultWriteAction in _writeActions)
                {
                    defaultWriteAction.Clear();
                }

                _writeActions.Clear();

                if (OdbConfiguration.IsLoggingEnabled())
                {
                    var numberOfWriteActions = _numberOfWriteActions.ToString();
                    var maxNumberOfWriteObjectPerTransactionAsString =
                        StorageEngineConstant.MaxNumberOfWriteObjectPerTransaction.ToString();

                    DLogger.Info("OdbTransaction: Number of objects has exceeded the max number " + numberOfWriteActions + "/" +
                                 maxNumberOfWriteObjectPerTransactionAsString +
                                 ": switching to persistent transaction managment");
                }
            }
        }
Beispiel #3
0
        internal static WriteAction Read(IFileSystemInterface fsi)
        {
            try
            {
                var position    = fsi.ReadLong();
                var size        = fsi.ReadInt();
                var bytes       = fsi.ReadBytes(size);
                var writeAction = new WriteAction(position, bytes);

                Log4NetHelper.Instance.LogDebugMessage(string.Format("Transaction WriteAction: Loading Write Action at {0} => {1}", fsi.GetPosition(), writeAction));

                return(writeAction);
            }
            catch (OdbRuntimeException)
            {
                Log4NetHelper.Instance.LogErrorMessage(string.Format("Transaction WriteAction: error reading write action at position {0}", fsi.GetPosition()));
                throw;
            }
        }
Beispiel #4
0
 /// <summary>
 ///   Set the write position (position in main database file).
 /// </summary>
 /// <remarks>
 ///   Set the write position (position in main database file). This is used to know if the next write can be appended to the previous one (in the same current Write Action) or not.
 /// </remarks>
 /// <param name="position"> </param>
 public void SetWritePosition(long position)
 {
     if (position != _currentWritePositionInWa)
     {
         _currentWritePositionInWa = position;
         if (_currentWriteAction != null)
         {
             AddWriteAction(_currentWriteAction, true);
         }
         _currentWriteAction = new WriteAction(position);
     }
     else
     {
         if (_currentWriteAction == null)
         {
             _currentWriteAction       = new WriteAction(position);
             _currentWritePositionInWa = position;
         }
     }
 }
Beispiel #5
0
 public void ManageWriteAction(long position, byte[] bytes)
 {
     if (_currentWritePositionInWa == position)
     {
         if (_currentWriteAction == null)
         {
             _currentWriteAction = new WriteAction(position);
         }
         _currentWriteAction.AddBytes(bytes);
         _currentWritePositionInWa += bytes.Length;
     }
     else
     {
         if (_currentWriteAction != null)
         {
             AddWriteAction(_currentWriteAction, true);
         }
         _currentWriteAction       = new WriteAction(position, bytes);
         _currentWritePositionInWa = position + bytes.Length;
     }
 }
Beispiel #6
0
        internal static WriteAction Read(IFileSystemInterface fsi)
        {
            try
            {
                var position    = fsi.ReadLong();
                var size        = fsi.ReadInt();
                var bytes       = fsi.ReadBytes(size);
                var writeAction = new WriteAction(position, bytes);

                if (OdbConfiguration.IsLoggingEnabled())
                {
                    DLogger.Debug(string.Format("Transaction WriteAction: Loading Write Action at {0} => {1}", fsi.GetPosition(), writeAction));
                }

                return(writeAction);
            }
            catch (OdbRuntimeException)
            {
                DLogger.Error(string.Format("Transaction WriteAction: error reading write action at position {0}", fsi.GetPosition()));
                throw;
            }
        }
Beispiel #7
0
        private void LoadWriteActions(string filename, bool apply)
        {
            if (OdbConfiguration.IsLoggingEnabled())
            {
                DLogger.Debug(string.Format("OdbTransaction: Load write actions of {0}", filename));
            }

            CheckFileAccess(filename);
            _fsi.SetReadPosition(0);
            _isCommited       = _fsi.ReadByte() == 1;
            _creationDateTime = _fsi.ReadLong();
            var totalNumberOfWriteActions = _fsi.ReadLong();

            if (OdbConfiguration.IsLoggingEnabled())
            {
                DLogger.Info(string.Concat("OdbTransaction: ", _writeActions.Count.ToString(), " write actions in file"));
            }

            for (var i = 0; i < totalNumberOfWriteActions; i++)
            {
                var defaultWriteAction = WriteAction.Read(_fsi);

                if (apply)
                {
                    defaultWriteAction.ApplyTo(_fsiToApplyWriteActions);
                    defaultWriteAction.Clear();
                }
                else
                {
                    AddWriteAction(defaultWriteAction, false);
                }
            }

            if (apply)
            {
                _fsiToApplyWriteActions.Flush();
            }
        }
Beispiel #8
0
        internal static WriteAction Read(IFileSystemInterface fsi)
        {
            try
            {
                var position = fsi.ReadLong();
                var size = fsi.ReadInt();
                var bytes = fsi.ReadBytes(size);
                var writeAction = new WriteAction(position, bytes);

                if (OdbConfiguration.IsLoggingEnabled())
                    DLogger.Debug(string.Format("Transaction WriteAction: Loading Write Action at {0} => {1}", fsi.GetPosition(), writeAction));

                return writeAction;
            }
            catch (OdbRuntimeException)
            {
                DLogger.Error(string.Format("Transaction WriteAction: error reading write action at position {0}", fsi.GetPosition()));
                throw;
            }
        }
Beispiel #9
0
        public void Commit()
        {
            var numberOfWriteActionsAsString       = _numberOfWriteActions.ToString();
            var hasAllWriteActionsInMemoryAsString = _hasAllWriteActionsInMemory.ToString();

            Log4NetHelper.Instance.LogInfoMessage("OdbTransaction: Commiting " + numberOfWriteActionsAsString + " write actions - In Memory : " +
                                                  hasAllWriteActionsInMemoryAsString + string.Format(" - sid={0}", _session.GetId()));

            // Check if database has been rollbacked
            CheckRollback();

            // call the commit listeners
            ManageCommitListenersBefore();

            if (_currentWriteAction != null && !_currentWriteAction.IsEmpty())
            {
                AddWriteAction(_currentWriteAction, true);
                _currentWriteAction = null;
            }

            if (_fsi == null && _numberOfWriteActions != 0)
            {
                throw new OdbRuntimeException(NDatabaseError.TransactionAlreadyCommitedOrRollbacked);
            }

            if (_numberOfWriteActions == 0)
            {
                // FIXME call commitMetaModel in realOnlyMode?
                CommitMetaModel();
                // Nothing to do
                if (_fsi != null)
                {
                    _fsi.Close();
                    _fsi = null;
                }
                if (_session != null)
                {
                    _session.GetCache().ClearOnCommit();
                }
                return;
            }

            // Marks the transaction as committed
            SetCommited();

            // Apply the write actions the main database file
            ApplyTo();

            // Commit Meta Model changes
            CommitMetaModel();

            if (_fsi != null)
            {
                _fsi.Close();
                _fsi = null;
            }

            if (_session != null)
            {
                _session.GetCache().ClearOnCommit();
            }

            ManageCommitListenersAfter();
        }
Beispiel #10
0
        /// <summary>
        ///   Adds a write action to the transaction
        /// </summary>
        /// <param name="writeAction"> The write action to be added </param>
        /// <param name="persistWriteAction"> To indicate if write action must be persisted </param>
        private void AddWriteAction(WriteAction writeAction, bool persistWriteAction)
        {
            if (OdbConfiguration.IsLoggingEnabled())
                DLogger.Info(string.Format("OdbTransaction: Adding WriteAction in Transaction of session {0}", _session.GetId()));

            if (writeAction.IsEmpty())
                return;

            CheckRollback();
            if (!_hasBeenPersisted && persistWriteAction)
                Persist();

            if (persistWriteAction)
                writeAction.PersistMeTo(_fsi);

            // Only adds the write action to the list if the transaction keeps all in
            // memory
            if (_hasAllWriteActionsInMemory)
                _writeActions.Add(writeAction);

            _numberOfWriteActions++;

            if (_hasAllWriteActionsInMemory &&
                _numberOfWriteActions > StorageEngineConstant.MaxNumberOfWriteObjectPerTransaction)
            {
                _hasAllWriteActionsInMemory = false;

                foreach (var defaultWriteAction in _writeActions)
                    defaultWriteAction.Clear();

                _writeActions.Clear();

                if (OdbConfiguration.IsLoggingEnabled())
                {
                    var numberOfWriteActions = _numberOfWriteActions.ToString();
                    var maxNumberOfWriteObjectPerTransactionAsString =
                        StorageEngineConstant.MaxNumberOfWriteObjectPerTransaction.ToString();

                    DLogger.Info("OdbTransaction: Number of objects has exceeded the max number " + numberOfWriteActions + "/" +
                                 maxNumberOfWriteObjectPerTransactionAsString +
                                 ": switching to persistent transaction managment");
                }
            }
        }
Beispiel #11
0
 public void ManageWriteAction(long position, byte[] bytes)
 {
     if (_currentWritePositionInWa == position)
     {
         if (_currentWriteAction == null)
         {
             _currentWriteAction = new WriteAction(position);
         }
         _currentWriteAction.AddBytes(bytes);
         _currentWritePositionInWa += bytes.Length;
     }
     else
     {
         if (_currentWriteAction != null)
         {
             AddWriteAction(_currentWriteAction, true);
         }
         _currentWriteAction = new WriteAction(position, bytes);
         _currentWritePositionInWa = position + bytes.Length;
     }
 }
Beispiel #12
0
 /// <summary>
 ///   Set the write position (position in main database file).
 /// </summary>
 /// <remarks>
 ///   Set the write position (position in main database file). This is used to know if the next write can be appended to the previous one (in the same current Write Action) or not.
 /// </remarks>
 /// <param name="position"> </param>
 public void SetWritePosition(long position)
 {
     if (position != _currentWritePositionInWa)
     {
         _currentWritePositionInWa = position;
         if (_currentWriteAction != null)
         {
             AddWriteAction(_currentWriteAction, true);
         }
         _currentWriteAction = new WriteAction(position);
     }
     else
     {
         if (_currentWriteAction == null)
         {
             _currentWriteAction = new WriteAction(position);
             _currentWritePositionInWa = position;
         }
     }
 }
Beispiel #13
0
        public void Commit()
        {
            if (OdbConfiguration.IsLoggingEnabled())
            {
                var numberOfWriteActionsAsString = _numberOfWriteActions.ToString();
                var hasAllWriteActionsInMemoryAsString = _hasAllWriteActionsInMemory.ToString();

                DLogger.Info("OdbTransaction: Commiting " + numberOfWriteActionsAsString + " write actions - In Memory : " +
                             hasAllWriteActionsInMemoryAsString + string.Format(" - sid={0}", _session.GetId()));
            }

            // Check if database has been rollbacked
            CheckRollback();

            // call the commit listeners
            ManageCommitListenersBefore();

            if (_currentWriteAction != null && !_currentWriteAction.IsEmpty())
            {
                AddWriteAction(_currentWriteAction, true);
                _currentWriteAction = null;
            }

            if (_fsi == null && _numberOfWriteActions != 0)
                throw new OdbRuntimeException(NDatabaseError.TransactionAlreadyCommitedOrRollbacked);

            if (_numberOfWriteActions == 0)
            {
                // FIXME call commitMetaModel in realOnlyMode?
                CommitMetaModel();
                // Nothing to do
                if (_fsi != null)
                {
                    _fsi.Close();
                    _fsi = null;
                }
                if (_session != null)
                    _session.GetCache().ClearOnCommit();
                return;
            }

            // Marks the transaction as committed
            SetCommited();

            // Apply the write actions the main database file
            ApplyTo();

            // Commit Meta Model changes
            CommitMetaModel();

            if (_fsi != null)
            {
                _fsi.Close();
                _fsi = null;
            }

            if (_session != null)
                _session.GetCache().ClearOnCommit();

            ManageCommitListenersAfter();
        }