Skip to content

EntropyEngine::Core::IO::WriteBatch

WriteBatch - Collects multiple write operations and applies them atomically. More…

#include <WriteBatch.h>

Name
~WriteBatch() =default
WriteBatch &writeLines(const std::map< size_t, std::string > & lines)
Overwrite multiple specific lines in one call.
WriteBatch &writeLine(size_t lineNumber, std::string_view content)
Overwrite a line at index (0-based).
voidreset()
Clears all pending operations without writing.
WriteBatch &replaceAll(std::string_view content)
Replace entire file content with the provided text.
FileOperationHandlepreview() const
Build the resulting content without writing it (debugging aid).
size_tpendingOperations() const
Number of pending operations in the batch.
WriteBatch &insertLines(size_t startLine, const std::vector< std::string > & lines)
Insert multiple lines starting at position.
WriteBatch &insertLine(size_t lineNumber, std::string_view content)
Insert a line at index (shifts existing lines down).
const std::string &getPath() const
Target file path for this batch.
boolempty() const
Returns true if no operations are pending.
WriteBatch &deleteRange(size_t startLine, size_t endLine)
Delete a range of lines [startLine, endLine).
WriteBatch &deleteLine(size_t lineNumber)
Delete a line at index (shifts remaining lines up).
FileOperationHandlecommit()
Apply all pending operations atomically.
FileOperationHandlecommit(const WriteOptions & opts)
Apply all pending operations atomically with per-commit options.
WriteBatch &clear()
Clear the file (equivalent to truncate to zero).
WriteBatch &appendLine(std::string_view content)
Append a line to the end of the file.
WriteBatch(VirtualFileSystem * vfs, std::string path)
class EntropyEngine::Core::IO::WriteBatch;

WriteBatch - Collects multiple write operations and applies them atomically.

This allows for efficient batch processing of file modifications without repeatedly reading and writing the entire file. All operations are collected in memory and then applied in a single atomic operation.

Usage: auto batch = vfs->createWriteBatch(“myfile.txt”); batch->writeLine(0, “First line”); batch->writeLine(5, “Sixth line”); batch->insertLine(2, “New third line”); batch->deleteLine(10); auto handle = batch->commit(); // Applies all changes atomically handle.wait();

~WriteBatch() =default
WriteBatch & writeLines(
const std::map< size_t, std::string > & lines
)

Overwrite multiple specific lines in one call.

Parameters:

  • lines Map of line index -> content; sparse indices allowed

Return: Reference to this batch for chaining

WriteBatch & writeLine(
size_t lineNumber,
std::string_view content
)

Overwrite a line at index (0-based).

Parameters:

  • lineNumber Line index to write
  • content New content (no newline)

Return: Reference to this batch for chaining

void reset()

Clears all pending operations without writing.

WriteBatch & replaceAll(
std::string_view content
)

Replace entire file content with the provided text.

Parameters:

  • content New content (may contain multiple lines)

Return: Reference to this batch for chaining

FileOperationHandle preview() const

Build the resulting content without writing it (debugging aid).

Return: Handle whose contentsText() contains the preview after wait()

inline size_t pendingOperations() const

Number of pending operations in the batch.

WriteBatch & insertLines(
size_t startLine,
const std::vector< std::string > & lines
)

Insert multiple lines starting at position.

Parameters:

  • startLine First index at which to insert
  • lines Lines to insert

Return: Reference to this batch for chaining

WriteBatch & insertLine(
size_t lineNumber,
std::string_view content
)

Insert a line at index (shifts existing lines down).

Parameters:

  • lineNumber Insert position
  • content Line content

Return: Reference to this batch for chaining

inline const std::string & getPath() const

Target file path for this batch.

inline bool empty() const

Returns true if no operations are pending.

WriteBatch & deleteRange(
size_t startLine,
size_t endLine
)

Delete a range of lines [startLine, endLine).

Parameters:

  • startLine First line to delete (inclusive)
  • endLine One past the last line to delete (exclusive)

Return: Reference to this batch for chaining

WriteBatch & deleteLine(
size_t lineNumber
)

Delete a line at index (shifts remaining lines up).

Parameters:

  • lineNumber Line to delete

Return: Reference to this batch for chaining

FileOperationHandle commit()

Apply all pending operations atomically.

Return: Handle for the asynchronous commit

auto batch = vfs.createWriteBatch("file.txt");
batch->writeLine(0, "Hello").appendLine("World");
auto h = batch->commit(); h.wait();

Uses VFS defaults for parent directory creation and preserves the original file’s line-ending style and trailing-newline policy when possible.

FileOperationHandle commit(
const WriteOptions & opts
)

Apply all pending operations atomically with per-commit options.

Parameters:

  • opts WriteOptions controlling behavior:

  • createParentDirs: per-commit override for creating parent directories

  • ensureFinalNewline: force presence/absence of a final newline on whole-file rewrites

Return: Handle for the asynchronous commit

Note: Line endings are detected from the original file and preserved (LF vs CRLF), avoiding mixed endings. See VirtualFileSystemExample.cpp for usage.

WriteBatch & clear()

Clear the file (equivalent to truncate to zero).

Return: Reference to this batch for chaining

WriteBatch & appendLine(
std::string_view content
)

Append a line to the end of the file.

Parameters:

  • content Line content (no newline)

Return: Reference to this batch for chaining

WriteBatch(
VirtualFileSystem * vfs,
std::string path
)

Updated on 2026-01-26 at 16:50:32 -0500