Skip to content

Commit f92baa3

Browse files
committed
Add ability to get path to modified collections in object notifications
1 parent 291bf9a commit f92baa3

24 files changed

+209
-53
lines changed

src/realm.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,13 @@ RLM_API bool realm_object_changes_is_deleted(const realm_object_changes_t*);
19531953
*/
19541954
RLM_API size_t realm_object_changes_get_num_modified_properties(const realm_object_changes_t*);
19551955

1956+
/**
1957+
* Get the number of paths to embedded collections that were modified.
1958+
*
1959+
* This function cannot fail.
1960+
*/
1961+
RLM_API size_t realm_object_changes_get_num_modified_paths(const realm_object_changes_t*);
1962+
19561963
/**
19571964
* Get the column keys for the properties that were modified in an object
19581965
* notification.
@@ -1967,6 +1974,20 @@ RLM_API size_t realm_object_changes_get_num_modified_properties(const realm_obje
19671974
RLM_API size_t realm_object_changes_get_modified_properties(const realm_object_changes_t*,
19681975
realm_property_key_t* out_modified, size_t max);
19691976

1977+
/**
1978+
* Get the column keys for the properties that were modified in an object
1979+
* notification.
1980+
*
1981+
* This function cannot fail.
1982+
*
1983+
* @param out_modified Where the paths should be written. May be NULL.
1984+
* @param max The maximum number of paths to write.
1985+
* @return The number of paths written to @a out_modified, or the number
1986+
* of modified paths if @a out_modified is NULL.
1987+
*/
1988+
RLM_API size_t realm_object_changes_get_modified_paths(const realm_object_changes_t*, realm_string_t* out_modified,
1989+
size_t max);
1990+
19701991
/**
19711992
* Get the number of various types of changes in a collection notification.
19721993
*

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class CollectionParent : public std::enable_shared_from_this<CollectionParent> {
9595
/// Get table of owning object
9696
virtual TableRef get_table() const noexcept = 0;
9797

98+
virtual void translate_path(const StablePath&, Path&) const = 0;
99+
98100
protected:
99101
friend class Collection;
100102
template <class>

src/realm/dictionary.cpp

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

641+
642+
void Dictionary::translate_path(const StablePath& stable_path, Path& path) const
643+
{
644+
auto& index = stable_path[m_level];
645+
auto ndx = find_index(index);
646+
StringData key = do_get_key(ndx).get_string();
647+
path.emplace_back(key);
648+
if (stable_path.size() > m_level + 1) {
649+
Mixed val = do_get(ndx);
650+
if (val.is_type(type_Dictionary)) {
651+
DummyParent parent(this->get_table(), val.get_ref());
652+
Dictionary dict(parent, 0);
653+
dict.translate_path(stable_path, path);
654+
}
655+
else if (val.is_type(type_List)) {
656+
DummyParent parent(this->get_table(), val.get_ref());
657+
Lst<Mixed> list(parent, 0);
658+
list.translate_path(stable_path, path);
659+
}
660+
}
661+
}
662+
641663
void Dictionary::add_index(Path& path, const Index& index) const
642664
{
643665
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
@@ -205,6 +205,7 @@ class Dictionary final : public CollectionBaseImpl<DictionaryBase>, public Colle
205205
{
206206
return Base::get_stable_path();
207207
}
208+
void translate_path(const StablePath& stable_path, Path& path) const final;
208209

209210
void add_index(Path& path, const Index& ndx) const final;
210211
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
@@ -790,6 +790,26 @@ void Lst<Mixed>::set_collection_ref(Index index, ref_type ref, CollectionType ty
790790
m_tree->set(ndx, Mixed(ref, type));
791791
}
792792

793+
void Lst<Mixed>::translate_path(const StablePath& stable_path, Path& path) const
794+
{
795+
auto& index = stable_path[m_level];
796+
auto ndx = find_index(index);
797+
path.emplace_back(ndx);
798+
if (stable_path.size() > m_level + 1) {
799+
Mixed val = get(ndx);
800+
if (val.is_type(type_Dictionary)) {
801+
DummyParent parent(this->get_table(), val.get_ref());
802+
Dictionary dict(parent, 0);
803+
dict.translate_path(stable_path, path);
804+
}
805+
else if (val.is_type(type_List)) {
806+
DummyParent parent(this->get_table(), val.get_ref());
807+
Lst<Mixed> list(parent, 0);
808+
list.translate_path(stable_path, path);
809+
}
810+
}
811+
}
812+
793813
void Lst<Mixed>::add_index(Path& path, const Index& index) const
794814
{
795815
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
@@ -484,6 +484,7 @@ class Lst<Mixed> final : public CollectionBaseImpl<LstBase>, public CollectionPa
484484
{
485485
return Base::get_stable_path();
486486
}
487+
void translate_path(const StablePath& stable_path, Path& path) const final;
487488

488489
ColKey get_col_key() const noexcept override
489490
{

src/realm/obj.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,16 @@ CollectionPtr Obj::get_collection_ptr(const Path& path) const
20792079
return collection;
20802080
}
20812081

2082+
void Obj::translate_path(const StablePath& stable_path, Path& path) const
2083+
{
2084+
ColKey col_key = m_table->get_column_key(stable_path[0]);
2085+
path.emplace_back(m_table->get_column_name(col_key));
2086+
if (stable_path.size() > 1) {
2087+
CollectionBasePtr collection = get_collection_ptr(col_key);
2088+
dynamic_cast<CollectionParent*>(collection.get())->translate_path(stable_path, path);
2089+
}
2090+
}
2091+
20822092
CollectionPtr Obj::get_collection_by_stable_path(const StablePath& path) const
20832093
{
20842094
// First element in path is phony column key

src/realm/obj.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Obj : public CollectionParent {
7878
Path get_short_path() const noexcept final;
7979
ColKey get_col_key() const noexcept final;
8080
StablePath get_stable_path() const noexcept final;
81+
void translate_path(const StablePath&, Path&) const final;
8182
void add_index(Path& path, const Index& ndx) const final;
8283
size_t find_index(const Index&) const final
8384
{

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)