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(); } }
/// <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"); } } }
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; } }
/// <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; } } }
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; } }
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; } }
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(); } }
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; } }
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(); }
/// <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"); } } }
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(); }