Skip to content

Commit 5f8cca1

Browse files
committed
Add ability to get path to modified collections in object notifications
1 parent 687bb98 commit 5f8cca1

24 files changed

+214
-55
lines changed

src/realm.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,13 @@ RLM_API bool realm_object_changes_is_deleted(const realm_object_changes_t*);
19461946
*/
19471947
RLM_API size_t realm_object_changes_get_num_modified_properties(const realm_object_changes_t*);
19481948

1949+
/**
1950+
* Get the number of paths to embedded collections that were modified.
1951+
*
1952+
* This function cannot fail.
1953+
*/
1954+
RLM_API size_t realm_object_changes_get_num_modified_paths(const realm_object_changes_t*);
1955+
19491956
/**
19501957
* Get the column keys for the properties that were modified in an object
19511958
* notification.
@@ -1960,6 +1967,20 @@ RLM_API size_t realm_object_changes_get_num_modified_properties(const realm_obje
19601967
RLM_API size_t realm_object_changes_get_modified_properties(const realm_object_changes_t*,
19611968
realm_property_key_t* out_modified, size_t max);
19621969

1970+
/**
1971+
* Get the column keys for the properties that were modified in an object
1972+
* notification.
1973+
*
1974+
* This function cannot fail.
1975+
*
1976+
* @param out_modified Where the paths should be written. May be NULL.
1977+
* @param max The maximum number of paths to write.
1978+
* @return The number of paths written to @a out_modified, or the number
1979+
* of modified paths if @a out_modified is NULL.
1980+
*/
1981+
RLM_API size_t realm_object_changes_get_modified_paths(const realm_object_changes_t*, realm_string_t* out_modified,
1982+
size_t max);
1983+
19631984
/**
19641985
* Get the number of various types of changes in a collection notification.
19651986
*

src/realm/collection.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class DummyParent : public CollectionParent {
3939
{
4040
return {};
4141
}
42+
void translate_path(const StablePath&, Path&) const final {}
4243
void add_index(Path&, const Index&) const noexcept final {}
4344
size_t find_index(const Index&) const noexcept final
4445
{

src/realm/collection_parent.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class CollectionParent : public std::enable_shared_from_this<CollectionParent> {
9090
// Return path from owning object
9191
virtual StablePath get_stable_path() const = 0;
9292
// Add a translation of Index to PathElement
93+
virtual void translate_path(const StablePath&, Path&) const = 0;
9394
virtual void add_index(Path& path, const Index& ndx) const = 0;
9495
// Return position of Index held by child
9596
virtual size_t find_index(const Index& ndx) const = 0;
@@ -110,9 +111,9 @@ class CollectionParent : public std::enable_shared_from_this<CollectionParent> {
110111
friend class CollectionList;
111112

112113
#ifdef REALM_DEBUG
113-
static constexpr size_t s_max_level = 4;
114+
static constexpr int s_max_level = 4;
114115
#else
115-
static constexpr size_t s_max_level = 100;
116+
static constexpr int s_max_level = 100;
116117
#endif
117118
uint8_t m_level = 0;
118119

src/realm/dictionary.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,28 @@ Dictionary::Iterator Dictionary::find(Mixed key) const noexcept
634634
return end();
635635
}
636636

637+
638+
void Dictionary::translate_path(const StablePath& stable_path, Path& path) const
639+
{
640+
auto& index = stable_path[m_level];
641+
auto ndx = find_index(index);
642+
StringData key = do_get_key(ndx).get_string();
643+
path.emplace_back(key);
644+
if (stable_path.size() > size_t(m_level) + 1) {
645+
Mixed val = do_get(ndx);
646+
if (val.is_type(type_Dictionary)) {
647+
DummyParent parent(this->get_table(), val.get_ref());
648+
Dictionary dict(parent, 0);
649+
dict.translate_path(stable_path, path);
650+
}
651+
else if (val.is_type(type_List)) {
652+
DummyParent parent(this->get_table(), val.get_ref());
653+
Lst<Mixed> list(parent, 0);
654+
list.translate_path(stable_path, path);
655+
}
656+
}
657+
}
658+
637659
void Dictionary::add_index(Path& path, const Index& index) const
638660
{
639661
auto ndx = m_values->find_key(index.get_salt());

src/realm/dictionary.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class Dictionary final : public CollectionBaseImpl<DictionaryBase>, public Colle
202202
{
203203
return Base::get_stable_path();
204204
}
205+
void translate_path(const StablePath& stable_path, Path& path) const final;
205206

206207
void add_index(Path& path, const Index& ndx) const final;
207208
size_t find_index(const Index&) const final;

src/realm/list.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,26 @@ void Lst<Mixed>::set_collection_ref(Index index, ref_type ref, CollectionType ty
763763
m_tree->set(ndx, Mixed(ref, type));
764764
}
765765

766+
void Lst<Mixed>::translate_path(const StablePath& stable_path, Path& path) const
767+
{
768+
auto& index = stable_path[m_level];
769+
auto ndx = find_index(index);
770+
path.emplace_back(ndx);
771+
if (stable_path.size() > size_t(m_level) + 1) {
772+
Mixed val = get(ndx);
773+
if (val.is_type(type_Dictionary)) {
774+
DummyParent parent(this->get_table(), val.get_ref());
775+
Dictionary dict(parent, 0);
776+
dict.translate_path(stable_path, path);
777+
}
778+
else if (val.is_type(type_List)) {
779+
DummyParent parent(this->get_table(), val.get_ref());
780+
Lst<Mixed> list(parent, 0);
781+
list.translate_path(stable_path, path);
782+
}
783+
}
784+
}
785+
766786
void Lst<Mixed>::add_index(Path& path, const Index& index) const
767787
{
768788
auto ndx = m_tree->find_key(index.get_salt());

src/realm/list.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ class Lst<Mixed> final : public CollectionBaseImpl<LstBase>, public CollectionPa
466466
{
467467
return Base::get_stable_path();
468468
}
469+
void translate_path(const StablePath& stable_path, Path& path) const final;
469470

470471
ColKey get_col_key() const noexcept override
471472
{

src/realm/obj.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,16 @@ CollectionPtr Obj::get_collection_ptr(const Path& path) const
20902090
return collection;
20912091
}
20922092

2093+
void Obj::translate_path(const StablePath& stable_path, Path& path) const
2094+
{
2095+
ColKey col_key = m_table->get_column_key(stable_path[0]);
2096+
path.emplace_back(m_table->get_column_name(col_key));
2097+
if (stable_path.size() > 1) {
2098+
CollectionBasePtr collection = get_collection_ptr(col_key);
2099+
dynamic_cast<CollectionParent*>(collection.get())->translate_path(stable_path, path);
2100+
}
2101+
}
2102+
20932103
CollectionPtr Obj::get_collection_by_stable_path(const StablePath& path) const
20942104
{
20952105
// First element in path is phony column key

src/realm/obj.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Obj {
7272
Path get_short_path() const noexcept;
7373
ColKey get_col_key() const noexcept;
7474
StablePath get_stable_path() const noexcept;
75+
void translate_path(const StablePath&, Path&) const;
7576
void add_index(Path& path, const CollectionParent::Index& ndx) const;
7677

7778
TableRef get_table() const noexcept
@@ -441,6 +442,10 @@ class ObjCollectionParent final : public Obj, public CollectionParent {
441442
{
442443
return Obj::get_stable_path();
443444
}
445+
void translate_path(const StablePath& stable_path, Path& path) const override
446+
{
447+
Obj::translate_path(stable_path, path);
448+
}
444449
void add_index(Path& path, const Index& ndx) const override
445450
{
446451
Obj::add_index(path, ndx);

src/realm/object-store/binding_context.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class BindingContext {
165165
// Populated with information about which columns were changed
166166
// May be shorter than the actual number of columns if the later columns
167167
// are not modified
168-
std::unordered_map<int64_t, ColumnInfo> changes;
168+
std::unordered_map<ColKey, ColumnInfo> changes;
169169

170170
// Simple lexographic ordering
171171
friend bool operator<(ObserverState const& lft, ObserverState const& rgt)

0 commit comments

Comments
 (0)