Skip to content

Commit 8aed122

Browse files
authored
Implement Service Manager API to set replica mode (#52) (#53)
1 parent a73eda3 commit 8aed122

7 files changed

Lines changed: 381 additions & 0 deletions

File tree

src/fb-cpp/BackupManager.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "BackupManager.h"
2626
#include "Client.h"
27+
#include <cassert>
2728

2829
using namespace fbcpp;
2930
using namespace fbcpp::impl;
@@ -84,6 +85,27 @@ void BackupManager::restore(const RestoreOptions& options)
8485
if (const auto parallelWorkers = options.getParallelWorkers())
8586
builder->insertInt(&statusWrapper, isc_spb_res_parallel_workers, static_cast<int>(*parallelWorkers));
8687

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

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/DatabaseManager.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 "DatabaseManager.h"
26+
#include "Client.h"
27+
#include <cassert>
28+
29+
using namespace fbcpp;
30+
using namespace fbcpp::impl;
31+
32+
33+
void DatabaseManager::execute(const DatabaseManagerOptions& options)
34+
{
35+
StatusWrapper statusWrapper{getClient()};
36+
auto builder =
37+
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+
}
61+
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+
}

src/fb-cpp/DatabaseManager.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_DATABASE_MANAGER_H
26+
#define FBCPP_DATABASE_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 DatabaseManagerOptions 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+
DatabaseManagerOptions& 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+
DatabaseManagerOptions& 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 DatabaseManager 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 execute(const DatabaseManagerOptions& options);
96+
};
97+
} // namespace fbcpp
98+
99+
100+
#endif // FBCPP_DATABASE_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 "DatabaseManager.h"
3940

4041
#endif // FBCPP_H

0 commit comments

Comments
 (0)