Skip to content

Commit 8948e83

Browse files
authored
Initial ObjectStore Class class (#6521)
1 parent 903e278 commit 8948e83

File tree

17 files changed

+193
-21
lines changed

17 files changed

+193
-21
lines changed

src/realm/obj.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ ObjKey Obj::get_backlink(const Table& origin, ColKey origin_col_key, size_t back
684684
return get_backlink(backlink_col_key, backlink_ndx);
685685
}
686686

687-
TableView Obj::get_backlink_view(TableRef src_table, ColKey src_col_key)
687+
TableView Obj::get_backlink_view(TableRef src_table, ColKey src_col_key) const
688688
{
689689
TableView tv(src_table, src_col_key, *this);
690690
tv.do_sync();

src/realm/obj.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Obj {
168168
size_t get_backlink_count() const;
169169
size_t get_backlink_count(const Table& origin, ColKey origin_col_key) const;
170170
ObjKey get_backlink(const Table& origin, ColKey origin_col_key, size_t backlink_ndx) const;
171-
TableView get_backlink_view(TableRef src_table, ColKey src_col_key);
171+
TableView get_backlink_view(TableRef src_table, ColKey src_col_key) const;
172172

173173
// To be used by the query system when a single object should
174174
// be tested. Will allow a function to be called in the context

src/realm/object-store/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(SOURCES
66
dictionary.cpp
77
index_set.cpp
88
list.cpp
9+
class.cpp
910
object.cpp
1011
object_changeset.cpp
1112
object_schema.cpp
@@ -45,6 +46,7 @@ set(HEADERS
4546
index_set.hpp
4647
keypath_helpers.hpp
4748
list.hpp
49+
class.hpp
4850
object.hpp
4951
object_accessor.hpp
5052
object_changeset.hpp

src/realm/object-store/c_api/query.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,8 @@ RLM_API realm_results_t* realm_get_backlinks(realm_object_t* object, realm_class
330330
return wrap_err([&]() {
331331
object->verify_attached();
332332
auto realm = object->realm();
333-
auto source_table = realm->read_group().get_table(TableKey{source_table_key});
334-
auto backlink_view = object->get_obj().get_backlink_view(source_table, ColKey{property_key});
335-
return new realm_results_t{Results{realm, backlink_view}};
333+
return new realm_results_t{
334+
Results{realm, object->get_obj(), TableKey{source_table_key}, ColKey{property_key}}};
336335
});
337336
}
338337

src/realm/object-store/class.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright 2023 Realm Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
////////////////////////////////////////////////////////////////////////////
18+
19+
20+
#include <realm/object-store/class.hpp>
21+
#include <realm/object-store/shared_realm.hpp>
22+
23+
namespace realm {
24+
25+
Class::Class(std::shared_ptr<Realm> r, const ObjectSchema* object_schema)
26+
: m_realm(std::move(r))
27+
, m_object_schema(object_schema)
28+
, m_table(m_realm->read_group().get_table(m_object_schema->table_key))
29+
{
30+
}
31+
32+
std::pair<Obj, bool> Class::create_object(Mixed pk)
33+
{
34+
bool did_create;
35+
auto obj = m_table->create_object_with_primary_key(pk, &did_create);
36+
return {obj, did_create};
37+
}
38+
39+
Obj Class::create_object()
40+
{
41+
return m_table->create_object();
42+
}
43+
44+
Obj Class::get_object(Mixed pk)
45+
{
46+
return m_table->get_object_with_primary_key(pk);
47+
}
48+
49+
} // namespace realm

src/realm/object-store/class.hpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Copyright 2023 Realm Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
////////////////////////////////////////////////////////////////////////////
18+
19+
#ifndef REALM_OS_CLASS_HPP
20+
#define REALM_OS_CLASS_HPP
21+
22+
#include <realm/obj.hpp>
23+
#include "realm/parser/keypath_mapping.hpp"
24+
#include "realm/parser/query_parser.hpp"
25+
#include <memory>
26+
27+
namespace realm {
28+
29+
class ObjectSchema;
30+
class Realm;
31+
class Query;
32+
33+
class Class {
34+
public:
35+
Class(std::shared_ptr<Realm> r, const ObjectSchema* object_schema);
36+
37+
size_t num_objects() const
38+
{
39+
return m_table->size();
40+
}
41+
bool is_embedded()
42+
{
43+
return m_table->is_embedded();
44+
}
45+
TableKey get_key() const
46+
{
47+
return m_table->get_key();
48+
}
49+
ColKey get_column_key(StringData name) const
50+
{
51+
return m_table->get_column_key(name);
52+
}
53+
std::shared_ptr<Realm> get_realm() const noexcept
54+
{
55+
return m_realm;
56+
}
57+
TableRef get_table() const noexcept
58+
{
59+
return m_table;
60+
}
61+
const ObjectSchema& get_schema() const noexcept
62+
{
63+
return *m_object_schema;
64+
}
65+
66+
Query get_query(const std::string& query_string, query_parser::Arguments& args,
67+
const query_parser::KeyPathMapping& mapping) const
68+
{
69+
return m_table->query(query_string, args, mapping);
70+
}
71+
72+
73+
std::pair<Obj, bool> create_object(Mixed primary_value);
74+
Obj create_object();
75+
Obj get_object(Mixed primary_value);
76+
77+
private:
78+
std::shared_ptr<Realm> m_realm;
79+
const ObjectSchema* m_object_schema;
80+
TableRef m_table;
81+
};
82+
83+
} // namespace realm
84+
85+
#endif // REALM_OS_CLASS_HPP

src/realm/object-store/impl/object_notifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
using namespace realm;
2424
using namespace realm::_impl;
2525

26-
ObjectNotifier::ObjectNotifier(std::shared_ptr<Realm> realm, TableKey table_key, ObjKey obj_key)
26+
ObjectNotifier::ObjectNotifier(std::shared_ptr<Realm> realm, const Obj& obj)
2727
: CollectionNotifier(std::move(realm))
28-
, m_table(transaction().get_table(table_key))
29-
, m_obj_key(obj_key)
28+
, m_table(obj.get_table())
29+
, m_obj_key(obj.get_key())
3030
{
3131
}
3232

src/realm/object-store/impl/object_notifier.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
namespace realm::_impl {
2828
class ObjectNotifier : public CollectionNotifier {
2929
public:
30-
ObjectNotifier(std::shared_ptr<Realm> realm, TableKey table_key, ObjKey obj_key);
30+
ObjectNotifier(std::shared_ptr<Realm> realm, const Obj&);
3131

3232
private:
3333
TableRef m_table;

src/realm/object-store/object.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ NotificationToken Object::add_notification_callback(CollectionChangeCallback cal
166166
verify_attached();
167167
m_realm->verify_notifications_available();
168168
if (!m_notifier) {
169-
m_notifier = std::make_shared<_impl::ObjectNotifier>(m_realm, m_obj.get_table()->get_key(), m_obj.get_key());
169+
m_notifier = std::make_shared<_impl::ObjectNotifier>(m_realm, m_obj);
170170
_impl::RealmCoordinator::register_notifier(m_notifier);
171171
}
172172
return {m_notifier, m_notifier->add_callback(std::move(callback), std::move(key_path_array))};

src/realm/object-store/object_accessor.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ ValueType Object::get_property_value_impl(ContextType& ctx, const Property& prop
224224
case PropertyType::LinkingObjects: {
225225
auto target_object_schema = m_realm->schema().find(property.object_type);
226226
auto link_property = target_object_schema->property_for_name(property.link_origin_property_name);
227-
auto table = m_realm->read_group().get_table(target_object_schema->table_key);
228-
auto tv = const_cast<Obj&>(m_obj).get_backlink_view(table, ColKey(link_property->column_key));
229-
return ctx.box(Results(m_realm, std::move(tv)));
227+
return ctx.box(Results(m_realm, m_obj, target_object_schema->table_key, link_property->column_key));
230228
}
231229
default:
232230
REALM_UNREACHABLE();

0 commit comments

Comments
 (0)