Lightweight 0.20251202.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 ProgressManager() = default;
176 ProgressManager(ProgressManager const&) = default;
177 ProgressManager& operator=(ProgressManager const&) = default;
178 ProgressManager(ProgressManager&&) = default;
179 ProgressManager& operator=(ProgressManager&&) = default;
180
181 /// Gets called when the progress of an individual backup/restore operation changes.
182 virtual void Update(Progress const& p) = 0;
183
184 /// Gets called when all backup/restore operations are finished.
185 virtual void AllDone() = 0;
186
187 /// Sets the maximum length of a table name.
188 /// This is used to align the output of the progress manager.
189 virtual void SetMaxTableNameLength(size_t /*len*/) {}
190
191 /// Returns the number of errors encountered during the operation.
192 [[nodiscard]] virtual size_t ErrorCount() const noexcept
193 {
194 return 0;
195 }
196
197 /// Sets the total number of items to be processed (for ETA calculation).
198 /// @param totalItems Total number of items (rows) to process across all tables.
199 virtual void SetTotalItems(size_t /*totalItems*/) {}
200
201 /// Adds to the total number of items for progressive ETA calculation.
202 /// This is called as row counts become available during parallel counting.
203 /// @param additionalItems Number of additional items (rows) to add to the total.
204 virtual void AddTotalItems(size_t /*additionalItems*/) {}
205
206 /// Called when items are processed (for rate and ETA calculation).
207 /// @param count Number of items (rows) just processed.
208 virtual void OnItemsProcessed(size_t /*count*/) {}
209};
210
211/// Base class for progress managers that tracks errors automatically.
213{
214 public:
215 void Update(Progress const& progress) override
216 {
217 if (progress.state == Progress::State::Error)
218 ++_errorCount;
219 }
220
221 [[nodiscard]] size_t ErrorCount() const noexcept override
222 {
223 return _errorCount;
224 }
225
226 private:
227 size_t _errorCount = 0;
228};
229
230struct NullProgressManager: ErrorTrackingProgressManager
231{
232 void Update(Progress const& progress) override
233 {
235 }
236 void AllDone() override {}
237};
238
239/// Backs up the database to a file.
240///
241/// @param outputFile the output file.
242/// @param connectionString the connection string used to connect to the database.
243/// @param concurrency the number of concurrent jobs.
244/// @param progress the progress manager to use for progress updates.
245/// @param schema the database schema to backup (optional).
246/// @param tableFilter comma-separated table filter patterns (default: "*" for all tables).
247/// Supports glob wildcards (* and ?) and schema.table notation.
248/// Examples: "Users,Products", "*_log", "dbo.Users", "sales.*"
249/// @param retrySettings configuration for retry behavior on transient errors.
250/// @param backupSettings configuration for compression method, level, and chunk size.
251LIGHTWEIGHT_API void Backup(std::filesystem::path const& outputFile,
252 SqlConnectionString const& connectionString,
253 unsigned concurrency,
254 ProgressManager& progress,
255 std::string const& schema = {},
256 std::string const& tableFilter = "*",
257 RetrySettings const& retrySettings = {},
258 BackupSettings const& backupSettings = {});
259
260/// Restores the database from a file.
261///
262/// @param inputFile the input file.
263/// @param connectionString the connection string used to connect to the database.
264/// @param concurrency the number of concurrent jobs.
265/// @param progress the progress manager to use for progress updates.
266/// @param schema the database schema to restore into (optional, overrides backup metadata).
267/// @param tableFilter comma-separated table filter patterns (default: "*" for all tables).
268/// Supports glob wildcards (* and ?) and schema.table notation.
269/// Examples: "Users,Products", "*_log", "dbo.Users", "sales.*"
270/// @param retrySettings configuration for retry behavior on transient errors.
271LIGHTWEIGHT_API void Restore(std::filesystem::path const& inputFile,
272 SqlConnectionString const& connectionString,
273 unsigned concurrency,
274 ProgressManager& progress,
275 std::string const& schema = {},
276 std::string const& tableFilter = "*",
277 RetrySettings const& retrySettings = {});
278
279/// Restores the database from a file with explicit memory management settings.
280///
281/// @param inputFile the input file.
282/// @param connectionString the connection string used to connect to the database.
283/// @param concurrency the number of concurrent jobs.
284/// @param progress the progress manager to use for progress updates.
285/// @param schema the database schema to restore into (optional, overrides backup metadata).
286/// @param tableFilter comma-separated table filter patterns (default: "*" for all tables).
287/// @param retrySettings configuration for retry behavior on transient errors.
288/// @param restoreSettings configuration for memory management during restore.
289LIGHTWEIGHT_API void Restore(std::filesystem::path const& inputFile,
290 SqlConnectionString const& connectionString,
291 unsigned concurrency,
292 ProgressManager& progress,
293 std::string const& schema,
294 std::string const& tableFilter,
295 RetrySettings const& retrySettings,
296 RestoreSettings const& restoreSettings);
297
298/// Creates the metadata JSON content.
299///
300/// @param connectionString the connection string used to connect to the database.
301/// @param tables the list of tables to backup.
302/// @param schema the database schema used for these tables (optional).
303LIGHTWEIGHT_API std::string CreateMetadata(SqlConnectionString const& connectionString,
304 SqlSchema::TableList const& tables,
305 std::string const& schema = {});
306
307/// Parses the metadata JSON content and returns a map of table info.
308///
309/// @param metadataJson content of the metadata.json file.
310LIGHTWEIGHT_API std::map<std::string, TableInfo> ParseSchema(std::string_view metadataJson,
311 ProgressManager* progress = nullptr);
312
313} // 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 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.
virtual void AllDone()=0
Gets called when all backup/restore operations are finished.
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.