Skip to content

Commit 83c46e8

Browse files
committed
Implement Service Manager API to set replica mode (#52)
1 parent a73eda3 commit 83c46e8

7 files changed

Lines changed: 366 additions & 0 deletions

File tree

src/fb-cpp/BackupManager.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ void BackupManager::restore(const RestoreOptions& options)
8484
if (const auto parallelWorkers = options.getParallelWorkers())
8585
builder->insertInt(&statusWrapper, isc_spb_res_parallel_workers, static_cast<int>(*parallelWorkers));
8686

87+
if (const auto replicaMode = options.getReplicaMode())
88+
{
89+
std::uint8_t modeVal = 0;
90+
switch (*replicaMode)
91+
{
92+
case ReplicaMode::NONE:
93+
modeVal = isc_spb_res_rm_none;
94+
break;
95+
case ReplicaMode::READ_ONLY:
96+
modeVal = isc_spb_res_rm_readonly;
97+
break;
98+
case ReplicaMode::READ_WRITE:
99+
modeVal = isc_spb_res_rm_readwrite;
100+
break;
101+
}
102+
builder->insertBytes(&statusWrapper, isc_spb_res_replica_mode, &modeVal, 1u);
103+
}
104+
87105
const auto buffer = builder->getBuffer(&statusWrapper);
88106
const auto length = builder->getBufferLength(&statusWrapper);
89107

src/fb-cpp/BackupManager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,30 @@ namespace fbcpp
296296
return *this;
297297
}
298298

299+
///
300+
/// Returns the replica mode.
301+
///
302+
const std::optional<ReplicaMode>& getReplicaMode() const
303+
{
304+
return replicaMode;
305+
}
306+
307+
///
308+
/// Sets the replica mode.
309+
///
310+
RestoreOptions& setReplicaMode(ReplicaMode value)
311+
{
312+
replicaMode = value;
313+
return *this;
314+
}
315+
299316
private:
300317
std::vector<DatabaseFileSpec> databaseFiles;
301318
std::vector<std::string> backupFiles;
302319
bool replace = false;
303320
ServiceManager::VerboseOutput verboseOutput;
304321
std::optional<std::uint32_t> parallelWorkers;
322+
std::optional<ReplicaMode> replicaMode;
305323
};
306324

307325
///

src/fb-cpp/ConfigManager.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2026 Popa Adrian Marius
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "ConfigManager.h"
26+
#include "Client.h"
27+
28+
using namespace fbcpp;
29+
using namespace fbcpp::impl;
30+
31+
32+
void ConfigManager::configure(const ConfigOptions& options)
33+
{
34+
StatusWrapper statusWrapper{getClient()};
35+
auto builder =
36+
fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
37+
builder->insertTag(&statusWrapper, isc_action_svc_properties);
38+
builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
39+
40+
if (const auto replicaMode = options.getReplicaMode())
41+
{
42+
std::uint8_t modeVal = 0;
43+
switch (*replicaMode)
44+
{
45+
case ReplicaMode::NONE:
46+
modeVal = isc_spb_prp_rm_none;
47+
break;
48+
case ReplicaMode::READ_ONLY:
49+
modeVal = isc_spb_prp_rm_readonly;
50+
break;
51+
case ReplicaMode::READ_WRITE:
52+
modeVal = isc_spb_prp_rm_readwrite;
53+
break;
54+
}
55+
builder->insertBytes(&statusWrapper, isc_spb_prp_replica_mode, &modeVal, 1u);
56+
}
57+
58+
const auto buffer = builder->getBuffer(&statusWrapper);
59+
const auto length = builder->getBufferLength(&statusWrapper);
60+
61+
startAction(std::vector<std::uint8_t>(buffer, buffer + length));
62+
waitForCompletion();
63+
}

src/fb-cpp/ConfigManager.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2026 Popa Adrian Marius
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#ifndef FBCPP_CONFIG_MANAGER_H
26+
#define FBCPP_CONFIG_MANAGER_H
27+
28+
#include "ServiceManager.h"
29+
#include <optional>
30+
#include <string>
31+
32+
33+
///
34+
/// fb-cpp namespace.
35+
///
36+
namespace fbcpp
37+
{
38+
///
39+
/// Represents options used to configure database properties through the service manager.
40+
///
41+
class ConfigOptions final
42+
{
43+
public:
44+
///
45+
/// Returns the database path to be configured.
46+
///
47+
const std::string& getDatabase() const
48+
{
49+
return database;
50+
}
51+
52+
///
53+
/// Sets the database path to be configured.
54+
///
55+
ConfigOptions& setDatabase(const std::string& value)
56+
{
57+
database = value;
58+
return *this;
59+
}
60+
61+
///
62+
/// Returns the replica mode.
63+
///
64+
const std::optional<ReplicaMode>& getReplicaMode() const
65+
{
66+
return replicaMode;
67+
}
68+
69+
///
70+
/// Sets the replica mode.
71+
///
72+
ConfigOptions& setReplicaMode(ReplicaMode value)
73+
{
74+
replicaMode = value;
75+
return *this;
76+
}
77+
78+
private:
79+
std::string database;
80+
std::optional<ReplicaMode> replicaMode;
81+
};
82+
83+
///
84+
/// Executes configuration and maintenance operations through the Firebird service manager.
85+
///
86+
class ConfigManager final : public ServiceManager
87+
{
88+
public:
89+
using ServiceManager::ServiceManager;
90+
91+
public:
92+
///
93+
/// Configures database properties using the provided options.
94+
///
95+
void configure(const ConfigOptions& options);
96+
};
97+
} // namespace fbcpp
98+
99+
100+
#endif // FBCPP_CONFIG_MANAGER_H

src/fb-cpp/ServiceManager.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@
4242
///
4343
namespace fbcpp
4444
{
45+
///
46+
/// Replica mode.
47+
///
48+
enum class ReplicaMode
49+
{
50+
///
51+
/// The database is not a replica (operates as primary).
52+
///
53+
NONE,
54+
55+
///
56+
/// Read-only replica.
57+
///
58+
READ_ONLY,
59+
60+
///
61+
/// Read-write replica.
62+
///
63+
READ_WRITE
64+
};
65+
4566
class Client;
4667

4768
///

src/fb-cpp/fb-cpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
#include "EventListener.h"
3737
#include "ServiceManager.h"
3838
#include "BackupManager.h"
39+
#include "ConfigManager.h"
3940

4041
#endif // FBCPP_H

0 commit comments

Comments
 (0)