Class DatabaseWriter
Thread-safe database writer that ensures serialized write operations to SQLite databases. Manages one dedicated worker thread per database file to prevent write conflicts and ensure data integrity. Uses the singleton pattern to maintain one worker instance per database path.
public sealed class DatabaseWriter : IDisposable
- Inheritance
-
DatabaseWriter
- Implements
- Inherited Members
Remarks
This class implements the single-writer pattern for SQLite databases to avoid "database is locked" errors. Each database file gets its own dedicated worker thread with a blocking queue for write operations. The worker processes batches of tiles transactionally for optimal performance.
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
DisposeWriter(string)
Safely disposes the database writer for the specified path.
public static bool DisposeWriter(string databasePath)
Parameters
databasePathstringPath to the database writer to dispose.
Returns
- bool
True if a writer was found and disposed, false otherwise.
EnqueueTileBatch(string, IReadOnlyList<Tile>)
Enqueues a batch of tiles for asynchronous database writing.
public static void EnqueueTileBatch(string databasePath, IReadOnlyList<Tile> tiles)
Parameters
databasePathstringPath to the target database.
tilesIReadOnlyList<Tile>Collection of tiles to write.
Exceptions
- ArgumentException
Thrown when parameters are invalid.
- InvalidOperationException
Thrown when the writer is shutting down.
GetOrCreateWriter(string)
Gets or creates a database writer instance for the specified path.
public static DatabaseWriter GetOrCreateWriter(string databasePath)
Parameters
databasePathstringAbsolute path to the SQLite database file.
Returns
- DatabaseWriter
Database writer instance for the specified path.
Exceptions
- ArgumentException
Thrown when database path is null or empty.