Skip to content

Commit 2face88

Browse files
committed
Merge MaintenanceOptions into DatabaseManagerOptions and keep single execute method
1 parent d79dac5 commit 2face88

3 files changed

Lines changed: 82 additions & 112 deletions

File tree

src/fb-cpp/DatabaseManager.cpp

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,76 +35,77 @@ void DatabaseManager::execute(const DatabaseManagerOptions& options)
3535
StatusWrapper statusWrapper{getClient()};
3636
auto builder =
3737
fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
38-
builder->insertTag(&statusWrapper, isc_action_svc_properties);
39-
builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
40-
41-
if (const auto replicaMode = options.getReplicaMode())
42-
{
43-
std::uint8_t modeVal = 0;
44-
switch (*replicaMode)
45-
{
46-
case ReplicaMode::NONE:
47-
modeVal = isc_spb_prp_rm_none;
48-
break;
49-
case ReplicaMode::READ_ONLY:
50-
modeVal = isc_spb_prp_rm_readonly;
51-
break;
52-
case ReplicaMode::READ_WRITE:
53-
modeVal = isc_spb_prp_rm_readwrite;
54-
break;
55-
default:
56-
assert(false);
57-
break;
58-
}
59-
builder->insertBytes(&statusWrapper, isc_spb_prp_replica_mode, &modeVal, 1u);
60-
}
6138

62-
const auto buffer = builder->getBuffer(&statusWrapper);
63-
const auto length = builder->getBufferLength(&statusWrapper);
64-
65-
startAction(std::vector<std::uint8_t>(buffer, buffer + length));
66-
waitForCompletion();
67-
}
39+
const bool isRepair = options.getSweep() || options.getValidate() || options.getMend() ||
40+
options.getIgnoreChecksum() || options.getKillShadows() || options.getFull() || options.getCheckDb() ||
41+
options.getIcu() || options.getUpgradeDb() || options.getParallelWorkers().has_value();
6842

43+
if (isRepair)
44+
builder->insertTag(&statusWrapper, isc_action_svc_repair);
45+
else
46+
builder->insertTag(&statusWrapper, isc_action_svc_properties);
6947

70-
void DatabaseManager::execute(const MaintenanceOptions& 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);
7748
builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
7849

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;
50+
if (isRepair)
51+
{
52+
int optionsVal = 0;
53+
if (options.getSweep())
54+
optionsVal |= isc_spb_rpr_sweep_db;
55+
if (options.getValidate())
56+
optionsVal |= isc_spb_rpr_validate_db;
57+
if (options.getMend())
58+
optionsVal |= isc_spb_rpr_mend_db;
59+
if (options.getIgnoreChecksum())
60+
optionsVal |= isc_spb_rpr_ignore_checksum;
61+
if (options.getKillShadows())
62+
optionsVal |= isc_spb_rpr_kill_shadows;
63+
if (options.getFull())
64+
optionsVal |= isc_spb_rpr_full;
65+
if (options.getCheckDb())
66+
optionsVal |= isc_spb_rpr_check_db;
67+
if (options.getIcu())
68+
optionsVal |= isc_spb_rpr_icu;
69+
if (options.getUpgradeDb())
70+
optionsVal |= isc_spb_rpr_upgrade_db;
9871

99-
if (optionsVal != 0)
100-
builder->insertInt(&statusWrapper, isc_spb_options, optionsVal);
72+
if (optionsVal != 0)
73+
builder->insertInt(&statusWrapper, isc_spb_options, optionsVal);
10174

102-
if (const auto parallelWorkers = options.getParallelWorkers())
103-
builder->insertInt(&statusWrapper, isc_spb_rpr_par_workers, static_cast<int>(*parallelWorkers));
75+
if (const auto parallelWorkers = options.getParallelWorkers())
76+
builder->insertInt(&statusWrapper, isc_spb_rpr_par_workers, static_cast<int>(*parallelWorkers));
77+
}
78+
else
79+
{
80+
if (const auto replicaMode = options.getReplicaMode())
81+
{
82+
std::uint8_t modeVal = 0;
83+
switch (*replicaMode)
84+
{
85+
case ReplicaMode::NONE:
86+
modeVal = isc_spb_prp_rm_none;
87+
break;
88+
case ReplicaMode::READ_ONLY:
89+
modeVal = isc_spb_prp_rm_readonly;
90+
break;
91+
case ReplicaMode::READ_WRITE:
92+
modeVal = isc_spb_prp_rm_readwrite;
93+
break;
94+
default:
95+
assert(false);
96+
break;
97+
}
98+
builder->insertBytes(&statusWrapper, isc_spb_prp_replica_mode, &modeVal, 1u);
99+
}
100+
}
104101

105102
const auto buffer = builder->getBuffer(&statusWrapper);
106103
const auto length = builder->getBufferLength(&statusWrapper);
107104

108105
startAction(std::vector<std::uint8_t>(buffer, buffer + length));
109-
waitForCompletion(options.getVerboseOutput());
106+
107+
if (isRepair)
108+
waitForCompletion(options.getVerboseOutput());
109+
else
110+
waitForCompletion();
110111
}

src/fb-cpp/DatabaseManager.h

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,22 @@
3737
namespace fbcpp
3838
{
3939
///
40-
/// Represents options used to configure database properties through the service manager.
40+
/// Represents options used to configure database properties or run maintenance operations through the service
41+
/// manager.
4142
///
4243
class DatabaseManagerOptions final
4344
{
4445
public:
4546
///
46-
/// Returns the database path to be configured.
47+
/// Returns the database path to be configured or maintained.
4748
///
4849
const std::string& getDatabase() const
4950
{
5051
return database;
5152
}
5253

5354
///
54-
/// Sets the database path to be configured.
55+
/// Sets the database path to be configured or maintained.
5556
///
5657
DatabaseManagerOptions& setDatabase(const std::string& value)
5758
{
@@ -76,34 +77,6 @@ namespace fbcpp
7677
return *this;
7778
}
7879

79-
private:
80-
std::string database;
81-
std::optional<ReplicaMode> replicaMode;
82-
};
83-
84-
///
85-
/// Represents options used to run a database maintenance operation through the service manager.
86-
///
87-
class MaintenanceOptions 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-
MaintenanceOptions& setDatabase(const std::string& value)
102-
{
103-
database = value;
104-
return *this;
105-
}
106-
10780
///
10881
/// Returns the verbose output callback.
10982
///
@@ -115,7 +88,7 @@ namespace fbcpp
11588
///
11689
/// Sets the verbose output callback.
11790
///
118-
MaintenanceOptions& setVerboseOutput(ServiceManager::VerboseOutput value)
91+
DatabaseManagerOptions& setVerboseOutput(ServiceManager::VerboseOutput value)
11992
{
12093
verboseOutput = std::move(value);
12194
return *this;
@@ -132,7 +105,7 @@ namespace fbcpp
132105
///
133106
/// Sets the requested number of parallel workers.
134107
///
135-
MaintenanceOptions& setParallelWorkers(std::uint32_t value)
108+
DatabaseManagerOptions& setParallelWorkers(std::uint32_t value)
136109
{
137110
parallelWorkers = value;
138111
return *this;
@@ -149,7 +122,7 @@ namespace fbcpp
149122
///
150123
/// Sets whether database sweep is enabled.
151124
///
152-
MaintenanceOptions& setSweep(bool value)
125+
DatabaseManagerOptions& setSweep(bool value)
153126
{
154127
sweep = value;
155128
return *this;
@@ -166,7 +139,7 @@ namespace fbcpp
166139
///
167140
/// Sets whether database validation is enabled.
168141
///
169-
MaintenanceOptions& setValidate(bool value)
142+
DatabaseManagerOptions& setValidate(bool value)
170143
{
171144
validate = value;
172145
return *this;
@@ -183,7 +156,7 @@ namespace fbcpp
183156
///
184157
/// Sets whether database mending is enabled.
185158
///
186-
MaintenanceOptions& setMend(bool value)
159+
DatabaseManagerOptions& setMend(bool value)
187160
{
188161
mend = value;
189162
return *this;
@@ -200,7 +173,7 @@ namespace fbcpp
200173
///
201174
/// Sets whether checksum verification is ignored.
202175
///
203-
MaintenanceOptions& setIgnoreChecksum(bool value)
176+
DatabaseManagerOptions& setIgnoreChecksum(bool value)
204177
{
205178
ignoreChecksum = value;
206179
return *this;
@@ -217,7 +190,7 @@ namespace fbcpp
217190
///
218191
/// Sets whether killing database shadows is enabled.
219192
///
220-
MaintenanceOptions& setKillShadows(bool value)
193+
DatabaseManagerOptions& setKillShadows(bool value)
221194
{
222195
killShadows = value;
223196
return *this;
@@ -234,7 +207,7 @@ namespace fbcpp
234207
///
235208
/// Sets whether full validation is enabled.
236209
///
237-
MaintenanceOptions& setFull(bool value)
210+
DatabaseManagerOptions& setFull(bool value)
238211
{
239212
full = value;
240213
return *this;
@@ -251,7 +224,7 @@ namespace fbcpp
251224
///
252225
/// Sets whether checking only metadata/structure is enabled.
253226
///
254-
MaintenanceOptions& setCheckDb(bool value)
227+
DatabaseManagerOptions& setCheckDb(bool value)
255228
{
256229
checkDb = value;
257230
return *this;
@@ -268,7 +241,7 @@ namespace fbcpp
268241
///
269242
/// Sets whether recreating ICU indexes is enabled.
270243
///
271-
MaintenanceOptions& setIcu(bool value)
244+
DatabaseManagerOptions& setIcu(bool value)
272245
{
273246
icu = value;
274247
return *this;
@@ -285,14 +258,15 @@ namespace fbcpp
285258
///
286259
/// Sets whether database upgrade is enabled.
287260
///
288-
MaintenanceOptions& setUpgradeDb(bool value)
261+
DatabaseManagerOptions& setUpgradeDb(bool value)
289262
{
290263
upgradeDb = value;
291264
return *this;
292265
}
293266

294267
private:
295268
std::string database;
269+
std::optional<ReplicaMode> replicaMode;
296270
ServiceManager::VerboseOutput verboseOutput;
297271
std::optional<std::uint32_t> parallelWorkers;
298272
bool sweep = false;
@@ -316,14 +290,9 @@ namespace fbcpp
316290

317291
public:
318292
///
319-
/// Configures database properties using the provided options.
293+
/// Configures or maintains database using the provided options.
320294
///
321295
void execute(const DatabaseManagerOptions& options);
322-
323-
///
324-
/// Runs a maintenance or repair operation using the provided options.
325-
///
326-
void execute(const MaintenanceOptions& options);
327296
};
328297
} // namespace fbcpp
329298

src/test/DatabaseManager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,18 @@ BOOST_AUTO_TEST_CASE(databaseSweepAndValidate)
177177
DatabaseManager manager{CLIENT, makeServiceManagerOptions()};
178178

179179
// 1. Run database sweep
180-
BOOST_CHECK_NO_THROW(manager.execute(MaintenanceOptions().setDatabase(databasePath).setSweep(true)));
180+
BOOST_CHECK_NO_THROW(manager.execute(DatabaseManagerOptions().setDatabase(databasePath).setSweep(true)));
181181

182182
// 2. Run multi-threaded database sweep
183183
BOOST_CHECK_NO_THROW(
184-
manager.execute(MaintenanceOptions().setDatabase(databasePath).setSweep(true).setParallelWorkers(4)));
184+
manager.execute(DatabaseManagerOptions().setDatabase(databasePath).setSweep(true).setParallelWorkers(4)));
185185

186186
// 3. Run database validation
187187
BOOST_CHECK_NO_THROW(
188-
manager.execute(MaintenanceOptions().setDatabase(databasePath).setValidate(true).setFull(true)));
188+
manager.execute(DatabaseManagerOptions().setDatabase(databasePath).setValidate(true).setFull(true)));
189189

190190
// 4. Run database upgrade (minor ODS upgrade)
191-
BOOST_CHECK_NO_THROW(manager.execute(MaintenanceOptions().setDatabase(databasePath).setUpgradeDb(true)));
191+
BOOST_CHECK_NO_THROW(manager.execute(DatabaseManagerOptions().setDatabase(databasePath).setUpgradeDb(true)));
192192

193193
Attachment cleanup{CLIENT, databaseUri, attachmentOptions};
194194
cleanup.dropDatabase();

0 commit comments

Comments
 (0)