Skip to content

Commit 33808b1

Browse files
committed
Client file id again stored in sync history
1 parent b394e92 commit 33808b1

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

src/realm/sync/noinst/client_history_impl.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ void ClientHistory::set_client_file_ident_in_wt(version_type current_version, Sa
4444

4545
Array& root = m_arrays->root;
4646
m_group->set_sync_file_id(client_file_ident.ident); // Throws
47+
root.set(s_client_file_ident_iip,
48+
RefOrTagged::make_tagged(client_file_ident.ident)); // Throws
4749
root.set(s_client_file_ident_salt_iip,
4850
RefOrTagged::make_tagged(client_file_ident.salt)); // Throws
51+
52+
m_client_file_ident = client_file_ident.ident;
4953
}
5054

5155

@@ -61,6 +65,8 @@ void ClientHistory::set_client_reset_adjustments(version_type current_version, S
6165
UploadCursor upload_progress = {0, 0};
6266
Array& root = m_arrays->root;
6367
m_group->set_sync_file_id(client_file_ident.ident); // Throws
68+
root.set(s_client_file_ident_iip,
69+
RefOrTagged::make_tagged(client_file_ident.ident)); // Throws
6470
root.set(s_client_file_ident_salt_iip,
6571
RefOrTagged::make_tagged(client_file_ident.salt)); // Throws
6672
root.set(s_progress_download_server_version_iip,
@@ -84,6 +90,8 @@ void ClientHistory::set_client_reset_adjustments(version_type current_version, S
8490
root.set(s_progress_uploadable_bytes_iip,
8591
RefOrTagged::make_tagged(0)); // Throws
8692

93+
m_client_file_ident = client_file_ident.ident;
94+
8795
// Discard existing synchronization history
8896
do_trim_sync_history(sync_history_size()); // Throws
8997

@@ -152,7 +160,7 @@ int ClientReplication::get_history_schema_version() const noexcept
152160
// Overriding member function in realm::Replication
153161
bool ClientReplication::is_upgradable_history_schema(int stored_schema_version) const noexcept
154162
{
155-
if (stored_schema_version == 11) {
163+
if (stored_schema_version == 11 || stored_schema_version == 12) {
156164
return true;
157165
}
158166
return false;
@@ -176,6 +184,11 @@ void ClientReplication::upgrade_history_schema(int stored_schema_version)
176184
schema_version = 12;
177185
}
178186

187+
if (schema_version < 13) {
188+
m_history.update_client_file_id();
189+
schema_version = 13;
190+
}
191+
179192
// NOTE: Future migration steps go here.
180193

181194
REALM_ASSERT(schema_version == get_client_history_schema_version());
@@ -184,6 +197,19 @@ void ClientReplication::upgrade_history_schema(int stored_schema_version)
184197
m_history.record_current_schema_version(); // Throws
185198
}
186199

200+
void ClientHistory::update_client_file_id()
201+
{
202+
using gf = _impl::GroupFriend;
203+
Allocator& alloc = gf::get_alloc(*m_group);
204+
auto ref = gf::get_history_ref(*m_group);
205+
Array root{alloc};
206+
root.init_from_ref(ref);
207+
gf::set_history_parent(*m_group, root);
208+
209+
auto file_id = m_group->get_sync_file_id();
210+
root.set(s_client_file_ident_iip, RefOrTagged::make_tagged(file_id));
211+
}
212+
187213
void ClientHistory::compress_stored_changesets()
188214
{
189215
using gf = _impl::GroupFriend;
@@ -235,12 +261,13 @@ void ClientHistory::get_status(version_type& current_client_version, SaltedFileI
235261
TransactionRef rt = m_db->start_read(); // Throws
236262
version_type current_client_version_2 = rt->get_version();
237263

238-
SaltedFileIdent client_file_ident_2{rt->get_sync_file_id(), 0};
264+
SaltedFileIdent client_file_ident_2{m_client_file_ident, 0};
239265
SyncProgress progress_2;
240266
using gf = _impl::GroupFriend;
241267
if (ref_type ref = gf::get_history_ref(*rt)) {
242268
Array root(m_db->get_alloc());
243269
root.init_from_ref(ref);
270+
client_file_ident_2.ident = file_ident_type(root.get_as_ref_or_tagged(s_client_file_ident_iip).get_as_int());
244271
client_file_ident_2.salt = salt_type(root.get_as_ref_or_tagged(s_client_file_ident_salt_iip).get_as_int());
245272
progress_2.latest_server_version.version =
246273
version_type(root.get_as_ref_or_tagged(s_progress_latest_server_version_iip).get_as_int());
@@ -280,13 +307,17 @@ void ClientHistory::set_client_file_ident(SaltedFileIdent client_file_ident)
280307
prepare_for_write(); // Throws
281308

282309
Array& root = m_arrays->root;
283-
REALM_ASSERT(wt->get_sync_file_id() == 0);
310+
REALM_ASSERT(m_client_file_ident == 0);
284311
wt->set_sync_file_id(client_file_ident.ident);
312+
root.set(s_client_file_ident_iip,
313+
RefOrTagged::make_tagged(client_file_ident.ident)); // Throws
285314
root.set(s_client_file_ident_salt_iip,
286315
RefOrTagged::make_tagged(client_file_ident.salt)); // Throws
287316
root.set(s_progress_download_client_version_iip, RefOrTagged::make_tagged(0));
288317
root.set(s_progress_upload_client_version_iip, RefOrTagged::make_tagged(0));
289318

319+
m_client_file_ident = client_file_ident.ident;
320+
290321
// Note: This transaction produces an empty changeset. Empty changesets are
291322
// not uploaded to the server.
292323
wt->commit(); // Throws
@@ -420,8 +451,7 @@ void ClientHistory::integrate_server_changesets(
420451
}
421452
VersionID old_version = transact->get_version_of_current_transaction();
422453
version_type local_version = old_version.version;
423-
auto sync_file_id = transact->get_sync_file_id();
424-
REALM_ASSERT(sync_file_id != 0);
454+
REALM_ASSERT(m_client_file_ident != 0);
425455

426456
ensure_updated(local_version); // Throws
427457
prepare_for_write(); // Throws
@@ -520,12 +550,11 @@ size_t ClientHistory::transform_and_apply_server_changesets(util::Span<Changeset
520550
}
521551

522552
version_type local_version = transact->get_version_of_current_transaction().version;
523-
auto sync_file_id = transact->get_sync_file_id();
524553

525554
try {
526555
for (auto& changeset : changesets_to_integrate) {
527556
REALM_ASSERT(changeset.last_integrated_remote_version <= local_version);
528-
REALM_ASSERT(changeset.origin_file_ident > 0 && changeset.origin_file_ident != sync_file_id);
557+
REALM_ASSERT(changeset.origin_file_ident > 0 && changeset.origin_file_ident != m_client_file_ident);
529558

530559
// It is possible that the synchronization history has been trimmed
531560
// to a point where a prefix of the merge window is no longer
@@ -555,9 +584,9 @@ size_t ClientHistory::transform_and_apply_server_changesets(util::Span<Changeset
555584
return !(m_db->other_writers_waiting_for_lock() &&
556585
transact->get_commit_size() >= commit_byte_size_limit && allow_lock_release);
557586
};
558-
auto changesets_transformed_count =
559-
transformer.transform_remote_changesets(*this, sync_file_id, local_version, changesets_to_integrate,
560-
std::move(changeset_applier), logger); // Throws
587+
auto changesets_transformed_count = transformer.transform_remote_changesets(
588+
*this, m_client_file_ident, local_version, changesets_to_integrate, std::move(changeset_applier),
589+
logger); // Throws
561590
return changesets_transformed_count;
562591
}
563592
catch (const BadChangesetError& e) {
@@ -1095,6 +1124,7 @@ void ClientHistory::update_from_ref_and_version(ref_type ref, version_type versi
10951124
// No history
10961125
m_ct_history_base_version = version;
10971126
m_sync_history_base_version = version;
1127+
m_client_file_ident = 0;
10981128
m_arrays.reset();
10991129
m_progress_download = {0, 0};
11001130
return;
@@ -1115,6 +1145,7 @@ void ClientHistory::update_from_ref_and_version(ref_type ref, version_type versi
11151145
REALM_ASSERT(m_arrays->origin_timestamps.size() == sync_history_size());
11161146

11171147
const Array& root = m_arrays->root;
1148+
m_client_file_ident = file_ident_type(root.get_as_ref_or_tagged(s_client_file_ident_iip).get_as_int());
11181149
m_progress_download.server_version =
11191150
version_type(root.get_as_ref_or_tagged(s_progress_download_server_version_iip).get_as_int());
11201151
m_progress_download.last_integrated_client_version =
@@ -1167,6 +1198,7 @@ void ClientHistory::verify() const
11671198
if (!m_arrays) {
11681199
REALM_ASSERT(m_progress_download.server_version == 0);
11691200
REALM_ASSERT(m_progress_download.last_integrated_client_version == 0);
1201+
REALM_ASSERT(m_client_file_ident == 0);
11701202
return;
11711203
}
11721204
m_arrays->verify();

src/realm/sync/noinst/client_history_impl.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ class ClientReplication;
6161
// Cooked history was removed, except to verify that there is no cooked history.
6262
//
6363
// 12 History entries are compressed.
64+
//
65+
// 13 Client file id back in sync history
6466

6567
constexpr int get_client_history_schema_version() noexcept
6668
{
67-
return 12;
69+
return 13;
6870
}
6971

7072
class IntegrationException : public RuntimeError {
@@ -302,6 +304,10 @@ class ClientHistory final : public _impl::History, public TransformHistory {
302304
/// add_sync_history_entry() is called, it is equal to that plus one.
303305
mutable version_type m_sync_history_base_version = 0;
304306

307+
/// The identifier assigned to this file by the server. A cache of the
308+
/// `s_client_file_ident_iip` slot in `m_top`.
309+
mutable file_ident_type m_client_file_ident = 0;
310+
305311
using IntegerBpTree = BPlusTree<int64_t>;
306312
struct Arrays {
307313
// Create the client history arrays in the target group
@@ -426,6 +432,7 @@ class ClientHistory final : public _impl::History, public TransformHistory {
426432
void record_current_schema_version();
427433
static void record_current_schema_version(Array& schema_versions, version_type snapshot_version);
428434
void compress_stored_changesets();
435+
void update_client_file_id();
429436

430437
size_t sync_history_size() const noexcept
431438
{

src/realm/sync/noinst/server/server_history.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,13 +1700,7 @@ void ServerHistory::update_from_ref_and_version(ref_type ref, version_type realm
17001700
dag.release();
17011701
}
17021702

1703-
if (m_acc->upstream_status.is_attached()) {
1704-
file_ident_type file_ident = m_group->get_sync_file_id();
1705-
m_local_file_ident = (file_ident == 0 ? g_root_node_file_ident : file_ident);
1706-
}
1707-
else {
1708-
m_local_file_ident = g_root_node_file_ident;
1709-
}
1703+
m_local_file_ident = g_root_node_file_ident;
17101704

17111705
m_num_client_files = m_acc->cf_ident_salts.size();
17121706
REALM_ASSERT(m_acc->cf_client_versions.size() == m_num_client_files);

src/realm/table.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,12 +1462,6 @@ Group* Table::get_parent_group() const noexcept
14621462
return static_cast<Group*>(parent);
14631463
}
14641464

1465-
inline uint64_t Table::get_sync_file_id() const noexcept
1466-
{
1467-
Group* g = get_parent_group();
1468-
return g ? g->get_sync_file_id() : 0;
1469-
}
1470-
14711465
size_t Table::get_index_in_group() const noexcept
14721466
{
14731467
if (!m_top.is_attached())

src/realm/table.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,6 @@ class Table {
790790

791791
ColumnType get_real_column_type(ColKey col_key) const noexcept;
792792

793-
uint64_t get_sync_file_id() const noexcept;
794-
795793
/// Create an empty table with independent spec and return just
796794
/// the reference to the underlying memory.
797795
static ref_type create_empty_table(Allocator&, TableKey = TableKey());

0 commit comments

Comments
 (0)