Skip to content

Commit 928d3b2

Browse files
authored
Merge pull request #54 from mariuz/main
Implement database maintenance and repair features
2 parents 8aed122 + 8bea51a commit 928d3b2

3 files changed

Lines changed: 326 additions & 7 deletions

File tree

src/fb-cpp/DatabaseManager.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using namespace fbcpp;
3030
using namespace fbcpp::impl;
3131

3232

33-
void DatabaseManager::execute(const DatabaseManagerOptions& options)
33+
void DatabaseManager::setProperties(const DatabasePropertiesOptions& options)
3434
{
3535
StatusWrapper statusWrapper{getClient()};
3636
auto builder =
@@ -65,3 +65,46 @@ void DatabaseManager::execute(const DatabaseManagerOptions& options)
6565
startAction(std::vector<std::uint8_t>(buffer, buffer + length));
6666
waitForCompletion();
6767
}
68+
69+
70+
void DatabaseManager::repair(const DatabaseRepairOptions& options)
71+
{
72+
StatusWrapper statusWrapper{getClient()};
73+
auto builder =
74+
fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
75+
76+
builder->insertTag(&statusWrapper, isc_action_svc_repair);
77+
builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
78+
79+
int optionsVal = 0;
80+
if (options.getSweep())
81+
optionsVal |= isc_spb_rpr_sweep_db;
82+
if (options.getValidate())
83+
optionsVal |= isc_spb_rpr_validate_db;
84+
if (options.getMend())
85+
optionsVal |= isc_spb_rpr_mend_db;
86+
if (options.getIgnoreChecksum())
87+
optionsVal |= isc_spb_rpr_ignore_checksum;
88+
if (options.getKillShadows())
89+
optionsVal |= isc_spb_rpr_kill_shadows;
90+
if (options.getFull())
91+
optionsVal |= isc_spb_rpr_full;
92+
if (options.getCheckDb())
93+
optionsVal |= isc_spb_rpr_check_db;
94+
if (options.getIcu())
95+
optionsVal |= isc_spb_rpr_icu;
96+
if (options.getUpgradeDb())
97+
optionsVal |= isc_spb_rpr_upgrade_db;
98+
99+
if (optionsVal != 0)
100+
builder->insertInt(&statusWrapper, isc_spb_options, optionsVal);
101+
102+
if (const auto parallelWorkers = options.getParallelWorkers())
103+
builder->insertInt(&statusWrapper, isc_spb_rpr_par_workers, static_cast<int>(*parallelWorkers));
104+
105+
const auto buffer = builder->getBuffer(&statusWrapper);
106+
const auto length = builder->getBufferLength(&statusWrapper);
107+
108+
startAction(std::vector<std::uint8_t>(buffer, buffer + length));
109+
waitForCompletion(options.getVerboseOutput());
110+
}

src/fb-cpp/DatabaseManager.h

Lines changed: 235 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define FBCPP_DATABASE_MANAGER_H
2727

2828
#include "ServiceManager.h"
29+
#include <cstdint>
2930
#include <optional>
3031
#include <string>
3132

@@ -38,7 +39,7 @@ namespace fbcpp
3839
///
3940
/// Represents options used to configure database properties through the service manager.
4041
///
41-
class DatabaseManagerOptions final
42+
class DatabasePropertiesOptions final
4243
{
4344
public:
4445
///
@@ -52,7 +53,7 @@ namespace fbcpp
5253
///
5354
/// Sets the database path to be configured.
5455
///
55-
DatabaseManagerOptions& setDatabase(const std::string& value)
56+
DatabasePropertiesOptions& setDatabase(const std::string& value)
5657
{
5758
database = value;
5859
return *this;
@@ -69,7 +70,7 @@ namespace fbcpp
6970
///
7071
/// Sets the replica mode.
7172
///
72-
DatabaseManagerOptions& setReplicaMode(ReplicaMode value)
73+
DatabasePropertiesOptions& setReplicaMode(ReplicaMode value)
7374
{
7475
replicaMode = value;
7576
return *this;
@@ -80,6 +81,231 @@ namespace fbcpp
8081
std::optional<ReplicaMode> replicaMode;
8182
};
8283

84+
///
85+
/// Represents options used to run a database maintenance operation through the service manager.
86+
///
87+
class DatabaseRepairOptions final
88+
{
89+
public:
90+
///
91+
/// Returns the database path to be maintained.
92+
///
93+
const std::string& getDatabase() const
94+
{
95+
return database;
96+
}
97+
98+
///
99+
/// Sets the database path to be maintained.
100+
///
101+
DatabaseRepairOptions& setDatabase(const std::string& value)
102+
{
103+
database = value;
104+
return *this;
105+
}
106+
107+
///
108+
/// Returns the verbose output callback.
109+
///
110+
const ServiceManager::VerboseOutput& getVerboseOutput() const
111+
{
112+
return verboseOutput;
113+
}
114+
115+
///
116+
/// Sets the verbose output callback.
117+
///
118+
DatabaseRepairOptions& setVerboseOutput(ServiceManager::VerboseOutput value)
119+
{
120+
verboseOutput = std::move(value);
121+
return *this;
122+
}
123+
124+
///
125+
/// Returns the requested number of parallel workers.
126+
///
127+
const std::optional<std::uint32_t>& getParallelWorkers() const
128+
{
129+
return parallelWorkers;
130+
}
131+
132+
///
133+
/// Sets the requested number of parallel workers.
134+
///
135+
DatabaseRepairOptions& setParallelWorkers(std::uint32_t value)
136+
{
137+
parallelWorkers = value;
138+
return *this;
139+
}
140+
141+
///
142+
/// Returns whether database sweep is configured to be run.
143+
///
144+
bool getSweep() const
145+
{
146+
return sweep;
147+
}
148+
149+
///
150+
/// Sets whether database sweep should be run.
151+
///
152+
DatabaseRepairOptions& setSweep(bool value)
153+
{
154+
sweep = value;
155+
return *this;
156+
}
157+
158+
///
159+
/// Returns whether database validation is configured to be run.
160+
///
161+
bool getValidate() const
162+
{
163+
return validate;
164+
}
165+
166+
///
167+
/// Sets whether database validation should be run.
168+
///
169+
DatabaseRepairOptions& setValidate(bool value)
170+
{
171+
validate = value;
172+
return *this;
173+
}
174+
175+
///
176+
/// Returns whether database mending is configured to be run.
177+
///
178+
bool getMend() const
179+
{
180+
return mend;
181+
}
182+
183+
///
184+
/// Sets whether database mending should be run.
185+
///
186+
DatabaseRepairOptions& setMend(bool value)
187+
{
188+
mend = value;
189+
return *this;
190+
}
191+
192+
///
193+
/// Returns whether checksum verification is configured to be ignored.
194+
///
195+
bool getIgnoreChecksum() const
196+
{
197+
return ignoreChecksum;
198+
}
199+
200+
///
201+
/// Sets whether checksum verification should be ignored.
202+
///
203+
DatabaseRepairOptions& setIgnoreChecksum(bool value)
204+
{
205+
ignoreChecksum = value;
206+
return *this;
207+
}
208+
209+
///
210+
/// Returns whether killing database shadows is configured to be run.
211+
///
212+
bool getKillShadows() const
213+
{
214+
return killShadows;
215+
}
216+
217+
///
218+
/// Sets whether killing database shadows should be run.
219+
///
220+
DatabaseRepairOptions& setKillShadows(bool value)
221+
{
222+
killShadows = value;
223+
return *this;
224+
}
225+
226+
///
227+
/// Returns whether full validation is configured to be run.
228+
///
229+
bool getFull() const
230+
{
231+
return full;
232+
}
233+
234+
///
235+
/// Sets whether full validation should be run.
236+
///
237+
DatabaseRepairOptions& setFull(bool value)
238+
{
239+
full = value;
240+
return *this;
241+
}
242+
243+
///
244+
/// Returns whether checking only metadata/structure is configured to be run.
245+
///
246+
bool getCheckDb() const
247+
{
248+
return checkDb;
249+
}
250+
251+
///
252+
/// Sets whether checking only metadata/structure should be run.
253+
///
254+
DatabaseRepairOptions& setCheckDb(bool value)
255+
{
256+
checkDb = value;
257+
return *this;
258+
}
259+
260+
///
261+
/// Returns whether recreating ICU indexes is configured to be run.
262+
///
263+
bool getIcu() const
264+
{
265+
return icu;
266+
}
267+
268+
///
269+
/// Sets whether recreating ICU indexes should be run.
270+
///
271+
DatabaseRepairOptions& setIcu(bool value)
272+
{
273+
icu = value;
274+
return *this;
275+
}
276+
277+
///
278+
/// Returns whether database upgrade is configured to be run.
279+
///
280+
bool getUpgradeDb() const
281+
{
282+
return upgradeDb;
283+
}
284+
285+
///
286+
/// Sets whether database upgrade should be run.
287+
///
288+
DatabaseRepairOptions& setUpgradeDb(bool value)
289+
{
290+
upgradeDb = value;
291+
return *this;
292+
}
293+
294+
private:
295+
std::string database;
296+
ServiceManager::VerboseOutput verboseOutput;
297+
std::optional<std::uint32_t> parallelWorkers;
298+
bool sweep = false;
299+
bool validate = false;
300+
bool mend = false;
301+
bool ignoreChecksum = false;
302+
bool killShadows = false;
303+
bool full = false;
304+
bool checkDb = false;
305+
bool icu = false;
306+
bool upgradeDb = false;
307+
};
308+
83309
///
84310
/// Executes configuration and maintenance operations through the Firebird service manager.
85311
///
@@ -92,7 +318,12 @@ namespace fbcpp
92318
///
93319
/// Configures database properties using the provided options.
94320
///
95-
void execute(const DatabaseManagerOptions& options);
321+
void setProperties(const DatabasePropertiesOptions& options);
322+
323+
///
324+
/// Runs a repair operation using the provided options.
325+
///
326+
void repair(const DatabaseRepairOptions& options);
96327
};
97328
} // namespace fbcpp
98329

0 commit comments

Comments
 (0)