Lightweight 0.20260303.0
Loading...
Searching...
No Matches
SqlBackup.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include "../Api.hpp"
5#include "../SqlConnectInfo.hpp"
6#include "../SqlQuery/MigrationPlan.hpp"
7#include "../SqlSchema.hpp"
8
9#include <chrono>
10#include <cstdint>
11#include <filesystem>
12#include <map>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace Lightweight::SqlBackup
18{
19
20/// Compression methods supported for ZIP entries.
21///
22/// The values correspond to the ZIP compression method IDs used by libzip.
23/// Not all methods may be available at runtime depending on how libzip was compiled.
24/// Use IsCompressionMethodSupported() to check availability.
25// NOLINTNEXTLINE(performance-enum-size) - Values must match libzip ZIP_CM_* constants
26enum class CompressionMethod : std::int32_t
27{
28 Store = 0, ///< No compression (ZIP_CM_STORE)
29 Deflate = 8, ///< Deflate compression (ZIP_CM_DEFLATE) - most compatible
30 Bzip2 = 12, ///< Bzip2 compression (ZIP_CM_BZIP2)
31 Lzma = 14, ///< LZMA compression (ZIP_CM_LZMA)
32 Zstd = 93, ///< Zstandard compression (ZIP_CM_ZSTD)
33 Xz = 95, ///< XZ compression (ZIP_CM_XZ)
34};
35
36/// Configuration for backup operations including compression and chunking.
38{
39 /// The compression method to use.
40 CompressionMethod method = CompressionMethod::Deflate;
41
42 /// The compression level (0-9).
43 /// - For Deflate: 1 = fastest, 9 = best compression, 6 = default
44 /// - For Bzip2: 1-9 (block size in 100k units)
45 /// - For Zstd: maps to zstd levels
46 /// - For Store: ignored
47 std::uint32_t level = 6;
48
49 /// The target size in bytes for each chunk before flushing.
50 /// Chunks are flushed when the buffer exceeds this size.
51 /// Default: 10 MB.
52 std::size_t chunkSizeBytes = 10 * 1024 * 1024;
53
54 /// If true, only export schema metadata without backing up table data.
55 bool schemaOnly = false;
56};
57
58/// Configuration for restore operations including memory management.
60{
61 /// Batch size for insert operations (rows per batch).
62 /// Default: 0 (auto-calculated based on available memory).
63 std::size_t batchSize = 0;
64
65 /// Maximum rows before an intermediate commit within a chunk.
66 /// Helps reduce transaction log / WAL memory accumulation.
67 /// Default: 10000. Set to 0 to disable intermediate commits.
68 std::size_t maxRowsPerCommit = 10000;
69
70 /// Database page cache size in KB (used for SQLite PRAGMA cache_size,
71 /// could be extended for other DBMS memory hints).
72 /// Default: 65536 (64MB). Set to 0 to use database default.
73 std::size_t cacheSizeKB = 65536;
74
75 /// Memory limit in bytes (0 = auto-detect from system).
76 std::size_t memoryLimitBytes = 0;
77
78 /// If true, only recreate schema without importing data.
79 bool schemaOnly = false;
80};
81
82/// Returns available system memory in bytes.
83LIGHTWEIGHT_API std::size_t GetAvailableSystemMemory() noexcept;
84
85/// Calculates optimal restore settings based on available memory.
86///
87/// @param availableMemory Available system memory in bytes.
88/// @param concurrency Number of concurrent restore workers.
89/// @return RestoreSettings optimized for the given memory constraints.
90LIGHTWEIGHT_API RestoreSettings CalculateRestoreSettings(std::size_t availableMemory, unsigned concurrency);
91
92/// Checks if a compression method is supported by the current libzip installation.
93///
94/// @param method The compression method to check.
95/// @return true if the method is available for both compression and decompression.
96LIGHTWEIGHT_API bool IsCompressionMethodSupported(CompressionMethod method) noexcept;
97
98/// Returns a list of all compression methods that are supported by the current libzip installation.
99LIGHTWEIGHT_API std::vector<CompressionMethod> GetSupportedCompressionMethods() noexcept;
100
101/// Returns the human-readable name of a compression method.
102LIGHTWEIGHT_API std::string_view CompressionMethodName(CompressionMethod method) noexcept;
103
104/// Configuration for retry behavior on transient errors during backup/restore operations.
106{
107 /// Maximum number of retry attempts for transient errors.
108 unsigned maxRetries = 3;
109
110 /// Initial delay between retry attempts.
111 std::chrono::milliseconds initialDelay { 500 };
112
113 /// Multiplier applied to delay after each failed attempt (exponential backoff).
114 double backoffMultiplier = 2.0;
115
116 /// Maximum delay between retry attempts.
117 std::chrono::milliseconds maxDelay { 30000 };
118};
119
120/// Information about a table being backed up.
122{
123 /// The list of columns in the table in SQL format.
124 std::string fields;
125
126 /// The list of columns in the table.
127 std::vector<bool> isBinaryColumn;
128
129 /// The list of columns in the table.
130 std::vector<SqlColumnDeclaration> columns;
131
132 /// The list of foreign key constraints in the table.
133 std::vector<SqlSchema::ForeignKeyConstraint> foreignKeys;
134
135 /// The indexes on the table (excluding primary key index).
136 std::vector<SqlSchema::IndexDefinition> indexes;
137
138 /// The number of rows in the table.
139 size_t rowCount = 0;
140};
141
142/// Progress information for backup/restore operations status updates.
144{
145 /// The state of an individual backup/restore operation.
146 enum class State : std::uint8_t
147 {
148 Started,
149 InProgress,
150 Finished,
151 Error,
152 Warning
153 };
154
155 /// The state of an individual backup/restore operation.
157
158 /// The name of the table being backed up / restored.
159 std::string tableName;
160
161 /// The current number of rows processed.
162 size_t currentRows {};
163
164 /// The total number of rows to be processed, if known.
165 std::optional<size_t> totalRows;
166
167 /// A message associated with the progress update.
168 std::string message;
169};
170
171/// The interface for progress updates.
173{
174 virtual ~ProgressManager() = default;
175 /// Default constructor.
176 ProgressManager() = default;
177 /// Default copy constructor.
179 /// Default copy assignment operator.
181 /// Default move constructor.
183 /// Default move assignment operator.
185
186 /// Gets called when the progress of an individual backup/restore operation changes.
187 virtual void Update(Progress const& p) = 0;
188
189 /// Gets called when all backup/restore operations are finished.
190 virtual void AllDone() = 0;
191
192 /// Sets the maximum length of a table name.
193 /// This is used to align the output of the progress manager.
194 virtual void SetMaxTableNameLength(size_t /*len*/) {}
195
196 /// Returns the number of errors encountered during the operation.
197 [[nodiscard]] virtual size_t ErrorCount() const noexcept
198 {
199 return 0;
200 }
201
202 /// Sets the total number of items to be processed (for ETA calculation).
203 /// @param totalItems Total number of items (rows) to process across all tables.
204 virtual void SetTotalItems(size_t totalItems) { (void) totalItems; }
205
206 /// Adds to the total number of items for progressive ETA calculation.
207 /// This is called as row counts become available during parallel counting.
208 /// @param additionalItems Number of additional items (rows) to add to the total.
209 virtual void AddTotalItems(size_t additionalItems) { (void) additionalItems; }
210
211 /// Called when items are processed (for rate and ETA calculation).
212 /// @param count Number of items (rows) just processed.
213 virtual void OnItemsProcessed(size_t count) { (void) count; }
214};
215
216/// Base class for progress managers that tracks errors automatically.
218{
219 public:
220 void Update(Progress const& progress) override
221 {
222 if (progress.state == Progress::State::Error)
223 ++_errorCount;
224 }
225
226 [[nodiscard]] size_t ErrorCount() const noexcept override
227 {
228 return _errorCount;
229 }
230
231 private:
232 size_t _errorCount = 0;
233};
234
235struct NullProgressManager: ErrorTrackingProgressManager
236{
237 void Update(Progress const& progress) override
238 {
240 }
241 void AllDone() override {}
242};
243
244/// Backs up the database to a file.
245///
246/// @param outputFile the output file.
247/// @param connectionString the connection string used to connect to the database.
248/// @param concurrency the number of concurrent jobs.
249/// @param progress the progress manager to use for progress updates.
250/// @param schema the database schema to backup (optional).
251/// @param tableFilter comma-separated table filter patterns (default: "*" for all tables).
252/// Supports glob wildcards (* and ?) and schema.table notation.
253/// Examples: "Users,Products", "*_log", "dbo.Users", "sales.*"
254/// @param retrySettings configuration for retry behavior on transient errors.
255/// @param backupSettings configuration for compression method, level, and chunk size.
256LIGHTWEIGHT_API void Backup(std::filesystem::path const& outputFile,
257 SqlConnectionString const& connectionString,
258 unsigned concurrency,
259 ProgressManager& progress,
260 std::string const& schema = {},
261 std::string const& tableFilter = "*",
262 RetrySettings const& retrySettings = {},
263 BackupSettings const& backupSettings = {});
264
265/// Restores the database from a file.
266///
267/// @param inputFile the input file.
268/// @param connectionString the connection string used to connect to the database.
269/// @param concurrency the number of concurrent jobs.
270/// @param progress the progress manager to use for progress updates.
271/// @param schema the database schema to restore into (optional, overrides backup metadata).
272/// @param tableFilter comma-separated table filter patterns (default: "*" for all tables).
273/// Supports glob wildcards (* and ?) and schema.table notation.
274/// Examples: "Users,Products", "*_log", "dbo.Users", "sales.*"
275/// @param retrySettings configuration for retry behavior on transient errors.
276LIGHTWEIGHT_API void Restore(std::filesystem::path const& inputFile,
277 SqlConnectionString const& connectionString,
278 unsigned concurrency,
279 ProgressManager& progress,
280 std::string const& schema = {},
281 std::string const& tableFilter = "*",
282 RetrySettings const& retrySettings = {});
283
284/// Restores the database from a file with explicit memory management settings.
285///
286/// @param inputFile the input file.
287/// @param connectionString the connection string used to connect to the database.
288/// @param concurrency the number of concurrent jobs.
289/// @param progress the progress manager to use for progress updates.
290/// @param schema the database schema to restore into (optional, overrides backup metadata).
291/// @param tableFilter comma-separated table filter patterns (default: "*" for all tables).
292/// @param retrySettings configuration for retry behavior on transient errors.
293/// @param restoreSettings configuration for memory management during restore.
294LIGHTWEIGHT_API void Restore(std::filesystem::path const& inputFile,
295 SqlConnectionString const& connectionString,
296 unsigned concurrency,
297 ProgressManager& progress,
298 std::string const& schema,
299 std::string const& tableFilter,
300 RetrySettings const& retrySettings,
301 RestoreSettings const& restoreSettings);
302
303/// Creates the metadata JSON content.
304///
305/// @param connectionString the connection string used to connect to the database.
306/// @param tables the list of tables to backup.
307/// @param schema the database schema used for these tables (optional).
308LIGHTWEIGHT_API std::string CreateMetadata(SqlConnectionString const& connectionString,
309 SqlSchema::TableList const& tables,
310 std::string const& schema = {});
311
312/// Parses the metadata JSON content and returns a map of table info.
313///
314/// @param metadataJson content of the metadata.json file.
315LIGHTWEIGHT_API std::map<std::string, TableInfo> ParseSchema(std::string_view metadataJson,
316 ProgressManager* progress = nullptr);
317
318} // namespace Lightweight::SqlBackup
Base class for progress managers that tracks errors automatically.
size_t ErrorCount() const noexcept override
Returns the number of errors encountered during the operation.
void Update(Progress const &progress) override
Gets called when the progress of an individual backup/restore operation changes.
Configuration for backup operations including compression and chunking.
Definition SqlBackup.hpp:38
CompressionMethod method
The compression method to use.
Definition SqlBackup.hpp:40
bool schemaOnly
If true, only export schema metadata without backing up table data.
Definition SqlBackup.hpp:55
The interface for progress updates.
virtual void SetTotalItems(size_t totalItems)
ProgressManager()=default
Default constructor.
virtual void Update(Progress const &p)=0
Gets called when the progress of an individual backup/restore operation changes.
virtual void SetMaxTableNameLength(size_t)
virtual size_t ErrorCount() const noexcept
Returns the number of errors encountered during the operation.
ProgressManager & operator=(ProgressManager const &)=default
Default copy assignment operator.
virtual void OnItemsProcessed(size_t count)
ProgressManager(ProgressManager &&)=default
Default move constructor.
ProgressManager & operator=(ProgressManager &&)=default
Default move assignment operator.
virtual void AddTotalItems(size_t additionalItems)
virtual void AllDone()=0
Gets called when all backup/restore operations are finished.
ProgressManager(ProgressManager const &)=default
Default copy constructor.
Progress information for backup/restore operations status updates.
size_t currentRows
The current number of rows processed.
State state
The state of an individual backup/restore operation.
std::string message
A message associated with the progress update.
std::optional< size_t > totalRows
The total number of rows to be processed, if known.
std::string tableName
The name of the table being backed up / restored.
State
The state of an individual backup/restore operation.
Configuration for restore operations including memory management.
Definition SqlBackup.hpp:60
std::size_t memoryLimitBytes
Memory limit in bytes (0 = auto-detect from system).
Definition SqlBackup.hpp:76
bool schemaOnly
If true, only recreate schema without importing data.
Definition SqlBackup.hpp:79
Configuration for retry behavior on transient errors during backup/restore operations.
Information about a table being backed up.
size_t rowCount
The number of rows in the table.
std::vector< SqlColumnDeclaration > columns
The list of columns in the table.
std::vector< SqlSchema::IndexDefinition > indexes
The indexes on the table (excluding primary key index).
std::vector< bool > isBinaryColumn
The list of columns in the table.
std::vector< SqlSchema::ForeignKeyConstraint > foreignKeys
The list of foreign key constraints in the table.
std::string fields
The list of columns in the table in SQL format.