TransactionFileStream
Represents a transactional file stream that writes data to a temporary file. Changes are only committed to the target file when Commit is called. If disposed without commit, changes are rolled back (temporary file is deleted).
Inherits: Stream
Implements: ICommitable, IAsyncCommitable
Constructors
public TransactionFileStream(string name, FileMode mode)
transactionFileStream = TransactionFileStream(name, mode)
Initializes a new instance of the TransactionFileStream class.
- name
- The name of the target file.
- mode
- The file mode that specifies the type of operations to be performed on the file.
public TransactionFileStream(IFileSystem fileSystem, string name, FileMode mode)
transactionFileStream = TransactionFileStream(fileSystem, name, mode)
Initializes a new instance of the TransactionFileStream class.
- fileSystem
- IFileSystem
- name
- The name of the target file.
- mode
- The file mode that specifies the type of operations to be performed on the file.
Properties
public override bool CanRead { get; }
value = transactionFileStream.CanRead
Gets a value indicating whether the temporary file stream supports reading. Always returns false.
public override bool CanSeek { get; }
value = transactionFileStream.CanSeek
Gets a value indicating whether the stream supports seeking. Always returns false - this is an append-only stream.
public override bool CanWrite { get; }
value = transactionFileStream.CanWrite
Gets a value indicating whether the underlying temporary file stream supports writing.
public bool IsCommitted { get; }
value = transactionFileStream.IsCommitted
Gets a value indicating whether the transaction has ever been committed.
public override long Length { get; }
value = transactionFileStream.Length
Gets the length in bytes of the underlying temporary file stream.
public override long Position { get; set; }
value = transactionFileStream.Position
transactionFileStream.Position = value
Gets the current position within the stream. Setting position is not supported - this is an append-only stream.
Methods
public void Commit()
transactionFileStream.Commit()
Commits the transaction, moving the temporary file to the target location. Can be called multiple times. After commit, stream continues appending.
public ValueTask CommitAsync(CancellationToken cancellationToken)
result = transactionFileStream.CommitAsync(cancellationToken)
Asynchronously commits the transaction, moving the temporary file to the target location. Can be called multiple times. After commit, stream continues appending.
- cancellationToken
- CancellationToken
Returns: ValueTask
protected override void Dispose(bool disposing)
transactionFileStream.Dispose(disposing)
Releases the unmanaged resources used by the TransactionFileStream and optionally releases the managed resources. If Commit was never called, performs rollback by deleting the temporary file. If Commit() failed, preserves .tmp file for manual recovery.
- disposing
- true to release both managed and unmanaged resources; false to release only unmanaged resources.
public override void Flush()
transactionFileStream.Flush()
Clears all buffers for the current stream and causes any buffered data to be written to the underlying file.
public override int Read(byte[] buffer, int offset, int count)
result = transactionFileStream.Read(buffer, offset, count)
Reading is not supported by the TransactionFileStream.
- buffer
- The buffer to read data into.
- offset
- The byte offset in buffer at which to begin storing the data read from the underlying stream.
- count
- The maximum number of bytes to be read from the underlying stream.
Returns: This method always throws a NotSupportedException.
public override long Seek(long offset, SeekOrigin origin)
result = transactionFileStream.Seek(offset, origin)
Seeking is not supported. This is an append-only stream.
public override void SetLength(long value)
transactionFileStream.SetLength(value)
Setting length is not supported. This is an append-only stream.
public override void Write(byte[] buffer, int offset, int count)
transactionFileStream.Write(buffer, offset, count)
Writes a sequence of bytes to the temporary file stream.
- buffer
- An array of bytes. This is the buffer that contains the data to write to the temporary file.
- offset
- The zero-based byte offset in buffer at which to begin copying bytes to the temporary file.
- count
- The number of bytes to be written to the temporary file.