Skip to content

Commit 2e6477c

Browse files
RCORE-2015 Add collection type to Clear instruction (#7590)
* Add collection type to Clear instruction * Bump sync protocol version for collections in mixed * Changes after code review --------- Co-authored-by: Jørgen Edelbo <[email protected]>
1 parent 4d815c6 commit 2e6477c

13 files changed

+48
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
### Compatibility
88
* Fileformat: Generates files with format v24. Reads and automatically upgrade from fileformat v10. If you want to upgrade from an earlier file format version you will have to use RealmCore v13.x.y or earlier.
99

10+
-----------
11+
12+
### Internals
13+
* `prior_size` field in Clear instruction is being repurposed as `collection_type` (no protocol changes required)
14+
1015
----------------------------------------------
1116

1217
# 14.5.1 Release notes

src/realm/sync/changeset.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ void Changeset::Reflector::operator()(const Instruction::AddColumn& p) const
431431
if (p.type == Instruction::Payload::Type::Link) {
432432
m_tracer.field("target_table", p.link_target_table);
433433
}
434-
if (p.collection_type == Instruction::AddColumn::CollectionType::Dictionary) {
434+
if (p.collection_type == Instruction::CollectionType::Dictionary) {
435435
m_tracer.field("key_type", p.key_type);
436436
}
437437
}
@@ -505,7 +505,7 @@ void Changeset::Printer::field(StringData n, Instruction::Payload::Type type)
505505
print_field(n, get_type_name(type));
506506
}
507507

508-
void Changeset::Printer::field(StringData n, Instruction::AddColumn::CollectionType type)
508+
void Changeset::Printer::field(StringData n, Instruction::CollectionType type)
509509
{
510510
print_field(n, get_collection_type(type));
511511
}

src/realm/sync/changeset.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ struct Changeset::Reflector {
348348
util::Optional<InternString> field, const Instruction::Path* path) = 0;
349349
virtual void field(StringData, InternString) = 0;
350350
virtual void field(StringData, Instruction::Payload::Type) = 0;
351-
virtual void field(StringData, Instruction::AddColumn::CollectionType) = 0;
351+
virtual void field(StringData, Instruction::CollectionType) = 0;
352352
virtual void field(StringData, const Instruction::PrimaryKey&) = 0;
353353
virtual void field(StringData, const Instruction::Payload&) = 0;
354354
virtual void field(StringData, const Instruction::Path&) = 0;
@@ -393,7 +393,7 @@ struct Changeset::Printer : Changeset::Reflector::Tracer {
393393
const Instruction::Path* path) final;
394394
void field(StringData, InternString) final;
395395
void field(StringData, Instruction::Payload::Type) final;
396-
void field(StringData, Instruction::AddColumn::CollectionType) final;
396+
void field(StringData, Instruction::CollectionType) final;
397397
void field(StringData, const Instruction::PrimaryKey&) final;
398398
void field(StringData, const Instruction::Payload&) final;
399399
void field(StringData, const Instruction::Path&) final;

src/realm/sync/changeset_encoder.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void ChangesetEncoder::append_value(Instruction::Payload::Type type)
121121
append_value(int64_t(type));
122122
}
123123

124-
void ChangesetEncoder::append_value(Instruction::AddColumn::CollectionType type)
124+
void ChangesetEncoder::append_value(Instruction::CollectionType type)
125125
{
126126
append_value(uint8_t(type));
127127
}
@@ -190,7 +190,7 @@ void ChangesetEncoder::operator()(const Instruction::AddInteger& instr)
190190

191191
void ChangesetEncoder::operator()(const Instruction::AddColumn& instr)
192192
{
193-
bool is_dictionary = (instr.collection_type == Instruction::AddColumn::CollectionType::Dictionary);
193+
bool is_dictionary = (instr.collection_type == Instruction::CollectionType::Dictionary);
194194
// Mixed columns are always nullable.
195195
REALM_ASSERT(instr.type != Instruction::Payload::Type::Null || instr.nullable || is_dictionary);
196196
append(Instruction::Type::AddColumn, instr.table, instr.field, instr.type, instr.nullable, instr.collection_type);
@@ -225,8 +225,7 @@ void ChangesetEncoder::operator()(const Instruction::ArrayErase& instr)
225225

226226
void ChangesetEncoder::operator()(const Instruction::Clear& instr)
227227
{
228-
uint32_t prior_size = 0; // Ignored
229-
append_path_instr(Instruction::Type::Clear, instr, prior_size);
228+
append_path_instr(Instruction::Type::Clear, instr, instr.collection_type);
230229
}
231230

232231
void ChangesetEncoder::operator()(const Instruction::SetInsert& instr)

src/realm/sync/changeset_encoder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct ChangesetEncoder {
4949
void append_value(const Instruction::Payload::Link&);
5050
void append_value(Instruction::Payload::Type);
5151
void append_value(util::Optional<Instruction::Payload::Type>);
52-
void append_value(Instruction::AddColumn::CollectionType);
52+
void append_value(Instruction::CollectionType);
5353
void append_value(const Instruction::Path&);
5454
void append_value(DataType);
5555
void append_value(bool);

src/realm/sync/changeset_parser.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct State {
5454

5555
util::Optional<Instruction::Payload::Type> read_optional_payload_type();
5656
Instruction::Payload::Type read_payload_type();
57-
Instruction::AddColumn::CollectionType read_collection_type();
57+
Instruction::CollectionType read_collection_type();
5858
Instruction::Payload read_payload();
5959
Instruction::Payload::Link read_link();
6060
Instruction::PrimaryKey read_object_key();
@@ -179,10 +179,10 @@ Instruction::Payload::Type State::read_payload_type()
179179
parser_error("Unsupported data type");
180180
}
181181

182-
Instruction::AddColumn::CollectionType State::read_collection_type()
182+
Instruction::CollectionType State::read_collection_type()
183183
{
184-
using CollectionType = Instruction::AddColumn::CollectionType;
185-
auto type = Instruction::AddColumn::CollectionType(read_int<uint8_t>());
184+
using CollectionType = Instruction::CollectionType;
185+
auto type = Instruction::CollectionType(read_int<uint8_t>());
186186
// Validate the type.
187187
switch (type) {
188188
case CollectionType::Single:
@@ -435,7 +435,7 @@ void State::parse_one()
435435
if (instr.type == Instruction::Payload::Type::Link) {
436436
instr.link_target_table = read_intern_string();
437437
}
438-
if (instr.collection_type == Instruction::AddColumn::CollectionType::Dictionary) {
438+
if (instr.collection_type == Instruction::CollectionType::Dictionary) {
439439
instr.key_type = read_payload_type();
440440
}
441441
else {
@@ -486,8 +486,8 @@ void State::parse_one()
486486
case Instruction::Type::Clear: {
487487
Instruction::Clear instr;
488488
read_path_instr(instr);
489-
uint32_t prior_size = read_int<uint32_t>();
490-
static_cast<void>(prior_size); // Ignored
489+
instr.collection_type = read_collection_type();
490+
// TODO: Add validation for collection_type once BAAS-29262 is done
491491
m_handler(instr);
492492
return;
493493
}

src/realm/sync/instruction_applier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ void InstructionApplier::operator()(const Instruction::AddInteger& instr)
550550
void InstructionApplier::operator()(const Instruction::AddColumn& instr)
551551
{
552552
using Type = Instruction::Payload::Type;
553-
using CollectionType = Instruction::AddColumn::CollectionType;
553+
using CollectionType = Instruction::CollectionType;
554554

555555
// Temporarily swap out the last object key so it doesn't get included in error messages
556556
TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);

src/realm/sync/instruction_replication.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void SyncReplication::insert_column(const Table* table, ColKey col_key, DataType
328328
Table* target_table)
329329
{
330330
Replication::insert_column(table, col_key, type, name, target_table);
331-
using CollectionType = Instruction::AddColumn::CollectionType;
331+
using CollectionType = Instruction::CollectionType;
332332

333333
if (select_table(*table)) {
334334
Instruction::AddColumn instr;
@@ -570,6 +570,7 @@ void SyncReplication::list_clear(const CollectionBase& view)
570570
if (select_collection(view)) {
571571
Instruction::Clear instr;
572572
populate_path_instr(instr, view);
573+
instr.collection_type = Instruction::CollectionType::List;
573574
emit(instr);
574575
}
575576
}
@@ -605,6 +606,7 @@ void SyncReplication::set_clear(const CollectionBase& set)
605606
if (select_collection(set)) {
606607
Instruction::Clear instr;
607608
populate_path_instr(instr, set);
609+
instr.collection_type = Instruction::CollectionType::Set;
608610
emit(instr);
609611
}
610612
}
@@ -663,6 +665,7 @@ void SyncReplication::dictionary_clear(const CollectionBase& dict)
663665
if (select_collection(dict)) {
664666
Instruction::Clear instr;
665667
populate_path_instr(instr, dict);
668+
instr.collection_type = Instruction::CollectionType::Dictionary;
666669
emit(instr);
667670
}
668671
}

src/realm/sync/instructions.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ struct Payload {
415415
}
416416
};
417417

418+
// This is backwards compatible with previous boolean type where 0
419+
// indicated simple type and 1 indicated list.
420+
enum class CollectionType : uint8_t { Single, List, Dictionary, Set };
421+
418422
/// All instructions are TableInstructions.
419423
struct TableInstruction {
420424
InternString table;
@@ -502,10 +506,6 @@ struct EraseTable : TableInstruction {
502506
struct AddColumn : TableInstruction {
503507
using TableInstruction::TableInstruction;
504508

505-
// This is backwards compatible with previous boolean type where 0
506-
// indicated simple type and 1 indicated list.
507-
enum class CollectionType : uint8_t { Single, List, Dictionary, Set };
508-
509509
InternString field;
510510

511511
// `Type::Null` for Mixed columns. Mixed columns are always nullable.
@@ -632,6 +632,7 @@ struct ArrayErase : PathInstruction {
632632

633633
struct Clear : PathInstruction {
634634
using PathInstruction::PathInstruction;
635+
CollectionType collection_type;
635636

636637
bool operator==(const Clear& rhs) const noexcept
637638
{
@@ -674,6 +675,7 @@ struct Instruction {
674675
using Payload = instr::Payload;
675676
using Path = instr::Path;
676677
using Vector = std::vector<Instruction>;
678+
using CollectionType = instr::CollectionType;
677679

678680
// CAUTION: Any change to the enum values for the instruction types is a protocol-breaking
679681
// change!
@@ -828,9 +830,9 @@ inline const char* get_type_name(Instruction::Payload::Type type)
828830
return "(unknown)";
829831
}
830832

831-
inline const char* get_collection_type(Instruction::AddColumn::CollectionType type)
833+
inline const char* get_collection_type(Instruction::CollectionType type)
832834
{
833-
using Type = Instruction::AddColumn::CollectionType;
835+
using Type = Instruction::CollectionType;
834836
switch (type) {
835837
case Type::Single:
836838
return "Single";

src/realm/sync/protocol.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ namespace sync {
5757
// Server replaces 'downloadable_bytes' (which was always zero prior this version)
5858
// with an estimated progress value (double from 0.0 to 1.0) for flx sessions
5959
//
60+
// 13 Support for syncing collections (lists and dictionaries) in Mixed columns
61+
//
6062
// XX Changes:
6163
// - TBD
6264
//
6365
constexpr int get_current_protocol_version() noexcept
6466
{
6567
// Also update the current protocol version test in flx_sync.cpp when
6668
// updating this value
67-
return 12;
69+
return 13;
6870
}
6971

7072
constexpr std::string_view get_pbs_websocket_protocol_prefix() noexcept

0 commit comments

Comments
 (0)