Skip to content

Commit

Permalink
Replication shouldn't know about DB file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeard0531 committed Oct 19, 2021
1 parent f0db95e commit 053c22a
Show file tree
Hide file tree
Showing 60 changed files with 772 additions and 837 deletions.
11 changes: 5 additions & 6 deletions src/realm/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ void DB::open(const std::string& path, bool no_create_file, const DBOptions opti
do_open(path, no_create_file, is_backend, options); // Throws
}

void DB::open(Replication& repl, const DBOptions options)
void DB::open(Replication& repl, const std::string& file, const DBOptions options)
{
// Exception safety: Since open() is called from constructors, if it throws,
// it must leave the file closed.
Expand All @@ -1357,7 +1357,6 @@ void DB::open(Replication& repl, const DBOptions options)
// on the DB. This will prevent the DB from calling the replication object when it closes.
repl.register_db(this);

std::string file = repl.get_database_path();
bool no_create = false;
bool is_backend = false;
do_open(file, no_create, is_backend, options); // Throws
Expand Down Expand Up @@ -2831,19 +2830,19 @@ DBRef DB::create(const std::string& file, bool no_create, const DBOptions option
return retval;
}

DBRef DB::create(Replication& repl, const DBOptions options)
DBRef DB::create(Replication& repl, const std::string& file, const DBOptions options)
{
DBRef retval = std::make_shared<DBInit>(options);
retval->open(repl, options);
retval->open(repl, file, options);
return retval;
}

DBRef DB::create(std::unique_ptr<Replication> repl, const DBOptions options)
DBRef DB::create(std::unique_ptr<Replication> repl, const std::string& file, const DBOptions options)
{
REALM_ASSERT(repl);
DBRef retval = std::make_shared<DBInit>(options);
retval->m_history = std::move(repl);
retval->open(*retval->m_history, options);
retval->open(*retval->m_history, file, options);
return retval;
}

Expand Down
7 changes: 4 additions & 3 deletions src/realm/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ class DB : public std::enable_shared_from_this<DB> {
// calling DB::close(), but after that no new association can be established. To reopen the
// file (or another file), a new DB object is needed.
static DBRef create(const std::string& file, bool no_create = false, const DBOptions options = DBOptions());
static DBRef create(Replication& repl, const DBOptions options = DBOptions());
static DBRef create(std::unique_ptr<Replication> repl, const DBOptions options = DBOptions());
static DBRef create(Replication& repl, const std::string& file, const DBOptions options = DBOptions());
static DBRef create(std::unique_ptr<Replication> repl, const std::string& file,
const DBOptions options = DBOptions());
static DBRef create(BinaryData, bool take_ownership = true);

~DB() noexcept;
Expand Down Expand Up @@ -496,7 +497,7 @@ class DB : public std::enable_shared_from_this<DB> {

/// Open this group in replication mode. The specified Replication instance
/// must remain in existence for as long as the DB.
void open(Replication&, const DBOptions options = DBOptions());
void open(Replication&, const std::string& file, const DBOptions options = DBOptions());


void do_open(const std::string& file, bool no_create, bool is_backend, const DBOptions options);
Expand Down
4 changes: 2 additions & 2 deletions src/realm/exec/realm2json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ int main(int argc, char const* argv[])
catch (const realm::FileFormatUpgradeRequired&) {
// In realm history
// Last chance - this one must succeed
auto hist = realm::make_in_realm_history(path);
auto hist = realm::make_in_realm_history();
realm::DBOptions options;
options.allow_file_format_upgrade = true;

auto db = realm::DB::create(*hist, options);
auto db = realm::DB::create(*hist, path, options);

std::cerr << "File upgraded to latest version: " << path << std::endl;

Expand Down
9 changes: 2 additions & 7 deletions src/realm/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ class InRealmHistoryImpl : public Replication {
public:
using version_type = Replication::version_type;

InRealmHistoryImpl(std::string realm_path)
: Replication(realm_path)
{
}

void initialize(DB& db) override
{
Replication::initialize(db); // Throws
Expand Down Expand Up @@ -275,9 +270,9 @@ class InRealmHistoryImpl : public Replication {

namespace realm {

std::unique_ptr<Replication> make_in_realm_history(const std::string& realm_path)
std::unique_ptr<Replication> make_in_realm_history()
{
return std::unique_ptr<InRealmHistoryImpl>(new InRealmHistoryImpl(realm_path)); // Throws
return std::unique_ptr<InRealmHistoryImpl>(new InRealmHistoryImpl()); // Throws
}

} // namespace realm
2 changes: 1 addition & 1 deletion src/realm/history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace realm {

std::unique_ptr<Replication> make_in_realm_history(const std::string& realm_path);
std::unique_ptr<Replication> make_in_realm_history();

} // namespace realm

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ using namespace realm::_impl;

ExternalCommitHelper::ExternalCommitHelper(RealmCoordinator& parent)
: m_parent(parent)
, m_history(realm::make_in_realm_history(parent.get_path()))
, m_sg(DB::create(*m_history,
, m_history(realm::make_in_realm_history())
, m_sg(DB::create(*m_history, parent.get_path(),
DBOptions(parent.is_in_memory() ? DBOptions::Durability::MemOnly : DBOptions::Durability::Full,
parent.get_encryption_key().data())))
, m_thread(std::async(std::launch::async, [=] {
Expand Down
6 changes: 3 additions & 3 deletions src/realm/object-store/impl/realm_coordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,13 @@ void RealmCoordinator::open_db()
std::unique_ptr<Replication> history;
if (server_synchronization_mode) {
#if REALM_ENABLE_SYNC
history = sync::make_client_replication(m_config.path);
history = sync::make_client_replication();
#else
REALM_TERMINATE("Realm was not built with sync enabled");
#endif
}
else if (!m_config.immutable()) {
history = make_in_realm_history(m_config.path);
history = make_in_realm_history();
}

DBOptions options;
Expand All @@ -505,7 +505,7 @@ void RealmCoordinator::open_db()
!m_config.disable_format_upgrade && m_config.schema_mode != SchemaMode::ResetFile;
if (history) {
options.backup_at_file_format_change = m_config.backup_at_file_format_change;
m_db = DB::create(std::move(history), options);
m_db = DB::create(std::move(history), m_config.path, options);
}
else {
m_db = DB::create(m_config.path, true, options);
Expand Down
5 changes: 0 additions & 5 deletions src/realm/replication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ class InputStreamImpl : public _impl::NoCopyInputStream {

} // anonymous namespace

std::string Replication::get_database_path() const
{
return m_database_file;
}

void Replication::initialize(DB&)
{
// Nothing needs to be done here
Expand Down
14 changes: 3 additions & 11 deletions src/realm/replication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class Replication {
}

protected:
Replication(const std::string& database_file);
Replication() = default;


//@{
Expand Down Expand Up @@ -397,7 +397,8 @@ class Replication {

DB* m_db = nullptr;

_impl::TransactLogEncoder m_encoder;
_impl::TransactLogBufferStream m_stream;
_impl::TransactLogEncoder m_encoder{m_stream};
mutable const Table* m_selected_table = nullptr;
mutable CollectionId m_selected_list;

Expand All @@ -410,9 +411,6 @@ class Replication {

void do_set(const Table*, ColKey col_key, ObjKey key, _impl::Instruction variant = _impl::instr_Set);

const std::string m_database_file;
_impl::TransactLogBufferStream m_stream;

size_t transact_log_size();
};

Expand All @@ -427,12 +425,6 @@ class Replication::Interrupted : public std::exception {

// Implementation:

inline Replication::Replication(const std::string& database_file)
: m_encoder(m_stream)
, m_database_file(database_file)
{
}

inline void Replication::initiate_transact(Group& group, version_type current_version, bool history_updated)
{
if (auto hist = _get_history_write()) {
Expand Down
4 changes: 2 additions & 2 deletions src/realm/sync/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace realm {
namespace sync {

std::unique_ptr<ClientReplication> make_client_replication(const std::string& realm_path)
std::unique_ptr<ClientReplication> make_client_replication()
{
return std::make_unique<ClientReplication>(realm_path); // Throws
return std::make_unique<ClientReplication>(); // Throws
}

} // namespace sync
Expand Down
5 changes: 0 additions & 5 deletions src/realm/sync/instruction_replication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
namespace realm {
namespace sync {

SyncReplication::SyncReplication(const std::string& realm_path)
: Replication(realm_path)
{
}

void SyncReplication::reset()
{
m_encoder.reset();
Expand Down
1 change: 0 additions & 1 deletion src/realm/sync/instruction_replication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace sync {

class SyncReplication : public Replication {
public:
explicit SyncReplication(const std::string& realm_path);
void set_short_circuit(bool) noexcept;
bool is_short_circuited() const noexcept;

Expand Down
2 changes: 1 addition & 1 deletion src/realm/sync/noinst/client_history_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ _impl::History* ClientReplication::_get_history_write()
// Overriding member function in realm::Replication
std::unique_ptr<_impl::History> ClientReplication::_create_history_read()
{
auto hist_impl = std::make_unique<ClientReplication>(get_database_path());
auto hist_impl = std::make_unique<ClientReplication>();
hist_impl->initialize(*m_db); // Throws
// Transfer ownership with pointer to private base class
return std::unique_ptr<_impl::History>{hist_impl.release()};
Expand Down
9 changes: 1 addition & 8 deletions src/realm/sync/noinst/client_history_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class ClientReplication final : public SyncReplication, private _impl::History,

enum class IntegrationError { bad_origin_file_ident, bad_changeset };

ClientReplication(const std::string& realm_path);

/// set_client_reset_adjustments() is used by client reset to adjust the
/// content of the history compartment. The shared group associated with
/// this history object must be in a write transaction when this function
Expand Down Expand Up @@ -404,11 +402,6 @@ class ClientReplication final : public SyncReplication, private _impl::History,

// Implementation

inline ClientReplication::ClientReplication(const std::string& realm_path)
: SyncReplication{realm_path}
{
}

// Clamp the beginning of the specified upload skippable version range to the
// beginning of the synchronization history.
//
Expand Down Expand Up @@ -455,7 +448,7 @@ inline auto ClientReplication::get_transformer() -> Transformer&
///
/// The intended role for such an object is as a plugin for new
/// realm::DB objects.
std::unique_ptr<ClientReplication> make_client_replication(const std::string& realm_path);
std::unique_ptr<ClientReplication> make_client_replication();

} // namespace sync
} // namespace realm
Expand Down
12 changes: 6 additions & 6 deletions src/realm/sync/noinst/server/encryption_transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,25 @@ void do_transform(const std::string& file_name, const char* read_key, const char
break;
}
case Replication::hist_InRealm: {
std::unique_ptr<Replication> hist(make_in_realm_history(file_name));
auto sg = DB::create(*hist, DBOptions(read_key));
std::unique_ptr<Replication> hist(make_in_realm_history());
auto sg = DB::create(*hist, file_name, DBOptions(read_key));
success = sg->compact(bump_version_number, write_key);
break;
}
case Replication::hist_OutOfRealm:
throw std::runtime_error("Could not transform Realm file with history type 'OutOfRealm' for " +
file_name);
case Replication::hist_SyncClient: {
std::unique_ptr<Replication> reference_history = realm::sync::make_client_replication(file_name);
auto sg = DB::create(*reference_history, DBOptions(read_key));
std::unique_ptr<Replication> reference_history = realm::sync::make_client_replication();
auto sg = DB::create(*reference_history, file_name, DBOptions(read_key));
success = sg->compact(bump_version_number, write_key);
break;
}
case Replication::hist_SyncServer: {
ServerHistoryContextImpl context;
_impl::ServerHistory::DummyCompactionControl compaction_control;
_impl::ServerHistory history{file_name, context, compaction_control};
auto sg = DB::create(history, DBOptions(read_key));
_impl::ServerHistory history{context, compaction_control};
auto sg = DB::create(history, file_name, DBOptions(read_key));
success = sg->compact(bump_version_number, write_key);
break;
}
Expand Down
8 changes: 4 additions & 4 deletions src/realm/sync/noinst/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,9 +1115,9 @@ class ServerImpl : public ServerImplBase, public ServerHistory::Context {
return file;
}

std::unique_ptr<ServerHistory> make_history_for_path(std::string path, CompactionControl& cc)
std::unique_ptr<ServerHistory> make_history_for_path(CompactionControl& cc)
{
return std::make_unique<ServerHistory>(path, *this, cc);
return std::make_unique<ServerHistory>(*this, cc);
}

util::bind_ptr<ServerFile> get_file(const std::string& virt_path) noexcept
Expand Down Expand Up @@ -4498,9 +4498,9 @@ ServerHistory& ServerFile::get_client_file_history(WorkerState& state, std::uniq
if (state.use_file_cache)
return worker_access().history; // Throws
const std::string& path = m_worker_file.realm_path;
hist_ptr = m_server.make_history_for_path(path, *this); // Throws
hist_ptr = m_server.make_history_for_path(*this); // Throws
DBOptions options = m_worker_file.make_shared_group_options(); // Throws
sg_ptr = DB::create(*hist_ptr, options); // Throws
sg_ptr = DB::create(*hist_ptr, path, options); // Throws
sg_ptr->claim_sync_agent(); // Throws
return *hist_ptr; // Throws
}
Expand Down
4 changes: 2 additions & 2 deletions src/realm/sync/noinst/server/server_file_access_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ inline void ServerFileAccessCache::Slot::do_close() noexcept
}

inline ServerFileAccessCache::File::File(const Slot& slot)
: history{slot.realm_path, slot.m_cache.m_history_context, slot.m_compaction_control} // Throws
, shared_group{DB::create(history, slot.make_shared_group_options())} // Throws
: history{slot.m_cache.m_history_context, slot.m_compaction_control} // Throws
, shared_group{DB::create(history, slot.realm_path, slot.make_shared_group_options())} // Throws
{
if (slot.m_claim_sync_agent) {
shared_group->claim_sync_agent();
Expand Down
3 changes: 1 addition & 2 deletions src/realm/sync/noinst/server/server_history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1785,10 +1785,9 @@ _impl::History* ServerHistory::_get_history_write()
std::unique_ptr<_impl::History> ServerHistory::_create_history_read()
{
_impl::ServerHistory::DummyCompactionControl compaction_control;
auto server_hist = std::make_unique<ServerHistory>(get_database_path(), m_context, compaction_control); // Throws
auto server_hist = std::make_unique<ServerHistory>(m_context, compaction_control); // Throws
server_hist->initialize(*m_db); // Throws
return std::unique_ptr<_impl::History>(server_hist.release());
;
}


Expand Down
8 changes: 3 additions & 5 deletions src/realm/sync/noinst/server/server_history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class ServerHistory : public sync::SyncReplication,

static constexpr bool is_direct_client(ClientType) noexcept;

ServerHistory(const std::string& realm_path, Context&, CompactionControl&);
ServerHistory(Context&, CompactionControl&);

/// Get the current Realm version and server version.
///
Expand Down Expand Up @@ -873,10 +873,8 @@ inline ServerHistory::IntegratableChangeset::IntegratableChangeset(file_ident_ty
{
}

inline ServerHistory::ServerHistory(const std::string& realm_path, Context& context,
CompactionControl& compaction_control)
: sync::SyncReplication{realm_path} // Throws
, m_context{context}
inline ServerHistory::ServerHistory(Context& context, CompactionControl& compaction_control)
: m_context{context}
, m_compaction_control{compaction_control}
{
m_enable_compaction =
Expand Down
8 changes: 4 additions & 4 deletions src/realm/sync/noinst/server/server_legacy_migration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ bool try_migrate_file(const std::string original_path, const std::string& new_pa
_impl::ServerHistory::DummyCompactionControl compaction_control;
Group legacy_group{original_path}; // Throws
if (check_legacy_format_1(legacy_group)) {
_impl::ServerHistory new_history{new_path, context, compaction_control}; // Throws
DBRef new_shared_group = DB::create(new_history); // Throws
_impl::ServerHistory new_history{context, compaction_control}; // Throws
DBRef new_shared_group = DB::create(new_history, new_path); // Throws
return true;
}
else {
// Assume that this failure is because the Realm file was already
// migrated, but verify the assumption by opening it with the right
// history type.
_impl::ServerHistory history{original_path, context, compaction_control}; // Throws
DBRef db = DB::create(history); // Throws
_impl::ServerHistory history{context, compaction_control}; // Throws
DBRef db = DB::create(history, original_path); // Throws
return false;
}
}
Expand Down
Loading

0 comments on commit 053c22a

Please sign in to comment.