Skip to content

Commit

Permalink
Simplify replication of table removal
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Apr 27, 2023
1 parent ed9acbb commit 78d4ffd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 42 deletions.
26 changes: 10 additions & 16 deletions src/realm/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,29 +833,23 @@ void Group::remove_table(size_t table_ndx, TableKey key)
// columns of other tables, however, to do that, we would have to
// automatically remove the "offending" link columns from those other
// tables. Such a behaviour is deemed too obscure, and we shall therefore
// require that a removed table does not contain foreigh origin backlink
// require that a removed table does not contain foreign origin backlink
// columns.
if (table->is_cross_table_link_target())
throw CrossTableLinkTarget(table->get_name());

// There is no easy way for Group::TransactAdvancer to handle removal of
// tables that contain foreign target table link columns, because that
// involves removal of the corresponding backlink columns. For that reason,
// we start by removing all columns, which will generate individual
// replication instructions for each column removal with sufficient
// information for Group::TransactAdvancer to handle them.
size_t n = table->get_column_count();
Replication* repl = *get_repl();
if (repl) {
// This will prevent sync instructions for column removals to be generated
repl->prepare_erase_class(key);
}
for (size_t i = n; i > 0; --i) {
ColKey col_key = table->spec_ndx2colkey(i - 1);
table->remove_column(col_key);
{
// We don't want to replicate the individual column removals along the
// way as they're covered by the table removal
Table::DisableReplication dr(*table);
for (size_t i = table->get_column_count(); i > 0; --i) {
ColKey col_key = table->spec_ndx2colkey(i - 1);
table->remove_column(col_key);
}
}

size_t prior_num_tables = m_tables.size();
Replication* repl = *get_repl();
if (repl)
repl->erase_class(key, prior_num_tables); // Throws

Expand Down
3 changes: 0 additions & 3 deletions src/realm/replication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class Replication {
virtual void add_class(TableKey table_key, StringData table_name, Table::Type table_type);
virtual void add_class_with_primary_key(TableKey, StringData table_name, DataType pk_type, StringData pk_field,
bool nullable, Table::Type table_type);
virtual void prepare_erase_class(TableKey table_key);
virtual void erase_class(TableKey table_key, size_t num_tables);
virtual void rename_class(TableKey table_key, StringData new_name);
virtual void insert_column(const Table*, ColKey col_key, DataType type, StringData name, Table* target_table);
Expand Down Expand Up @@ -494,8 +493,6 @@ inline void Replication::select_collection(const CollectionBase& list)
}
}

inline void Replication::prepare_erase_class(TableKey) {}

inline void Replication::erase_class(TableKey table_key, size_t)
{
unselect_all();
Expand Down
24 changes: 4 additions & 20 deletions src/realm/sync/instruction_replication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,6 @@ void SyncReplication::create_object_with_primary_key(const Table* table, ObjKey
}


void SyncReplication::prepare_erase_class(TableKey table_key)
{
REALM_ASSERT(!m_table_being_erased);
m_table_being_erased = table_key;
}

void SyncReplication::erase_class(TableKey table_key, size_t num_tables)
{
Replication::erase_class(table_key, num_tables);
Expand All @@ -312,15 +306,10 @@ void SyncReplication::erase_class(TableKey table_key, size_t num_tables)

bool is_class = m_transaction->table_is_public(table_key);

if (is_class) {
REALM_ASSERT(table_key == m_table_being_erased);
m_table_being_erased = TableKey();

if (!m_short_circuit) {
Instruction::EraseTable instr;
instr.table = emit_class_name(table_name);
emit(instr);
}
if (is_class && !m_short_circuit) {
Instruction::EraseTable instr;
instr.table = emit_class_name(table_name);
emit(instr);
}

m_last_table = nullptr;
Expand Down Expand Up @@ -385,11 +374,6 @@ void SyncReplication::erase_column(const Table* table, ColKey col_ndx)
Replication::erase_column(table, col_ndx);

if (select_table(*table)) {
if (table->get_key() == m_table_being_erased) {
// Ignore any EraseColumn instructions generated by Core as part of
// EraseTable.
return;
}
// Not allowed to remove PK/OID columns!
REALM_ASSERT(col_ndx != table->get_primary_key_column());
Instruction::EraseColumn instr;
Expand Down
3 changes: 0 additions & 3 deletions src/realm/sync/instruction_replication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class SyncReplication : public Replication {
void create_object(const Table*, GlobalKey) final;
void create_object_with_primary_key(const Table*, ObjKey, Mixed) final;

void prepare_erase_class(TableKey tk) final;
void erase_class(TableKey table_key, size_t num_tables) final;
void rename_class(TableKey table_key, StringData new_name) final;
void insert_column(const Table*, ColKey col_key, DataType type, StringData name, Table* target_table) final;
Expand Down Expand Up @@ -105,8 +104,6 @@ class SyncReplication : public Replication {
ChangesetEncoder m_encoder;
Transaction* m_transaction;

TableKey m_table_being_erased;

template <class T>
void emit(T instruction);

Expand Down

0 comments on commit 78d4ffd

Please sign in to comment.