Skip to content

Commit

Permalink
Store an optional Query on TableView
Browse files Browse the repository at this point in the history
This makes it so that TableView's default constructor actually doesn't allocate
any memory like the comment claimed, and is less weird than indicating "no
query" via a Query with a null table.
  • Loading branch information
tgoyne committed Sep 15, 2022
1 parent 0b51c55 commit 347fe08
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/realm/obj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ enum class UpdateStatus {
// 'Object' would have been a better name, but it clashes with a class in ObjectStore
class Obj {
public:
Obj()
constexpr Obj()
: m_table(nullptr)
, m_row_ndx(size_t(-1))
, m_storage_version(-1)
Expand Down
10 changes: 4 additions & 6 deletions src/realm/object-store/results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,19 +742,17 @@ Query Results::do_get_query() const
if (const_cast<Query&>(m_query).get_table())
return m_query;

// A TableView has an associated Query if it was produced by Query::find_all. This is indicated
// by TableView::get_query returning a Query with a non-null table.
Query query = m_table_view.get_query();
if (query.get_table()) {
return query;
// A TableView has an associated Query if it was produced by Query::find_all
if (auto& query = m_table_view.get_query()) {
return *query;
}

// The TableView has no associated query so create one with no conditions that is restricted
// to the rows in the TableView.
if (m_update_policy == UpdatePolicy::Auto) {
m_table_view.sync_if_needed();
}
return Query(m_table, std::unique_ptr<TableView>(new TableView(m_table_view)));
return Query(m_table, std::make_unique<TableView>(m_table_view));
}
case Mode::Collection:
if (auto list = dynamic_cast<ObjList*>(m_collection.get())) {
Expand Down
14 changes: 4 additions & 10 deletions src/realm/table_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class TableRef;

class ConstTableRef {
public:
ConstTableRef() noexcept {}
ConstTableRef(std::nullptr_t) noexcept {}
constexpr ConstTableRef() noexcept = default;
constexpr ConstTableRef(std::nullptr_t) noexcept {}
ConstTableRef(const TableRef& other) noexcept;

const Table* operator->() const;
Expand Down Expand Up @@ -91,14 +91,8 @@ class ConstTableRef {

class TableRef : public ConstTableRef {
public:
TableRef() noexcept
: ConstTableRef()
{
}
TableRef(std::nullptr_t) noexcept
: ConstTableRef()
{
}
constexpr TableRef() noexcept = default;
constexpr TableRef(std::nullptr_t) noexcept {}

Table* operator->() const;
Table& operator*() const;
Expand Down
31 changes: 17 additions & 14 deletions src/realm/table_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ TableView::TableView(TableView& src, Transaction* tr, PayloadPolicy policy_mode)
: m_source_column_key(src.m_source_column_key)
{
bool was_in_sync = src.is_in_sync();
m_query = Query(src.m_query, tr, policy_mode);
if (src.m_query)
m_query = Query(*src.m_query, tr, policy_mode);
m_table = tr->import_copy_of(src.m_table);

if (policy_mode == PayloadPolicy::Stay)
Expand Down Expand Up @@ -319,8 +320,8 @@ bool TableView::depends_on_deleted_object() const
if (m_source_column_key && !m_linked_obj.is_valid()) {
return true;
}
else if (m_query.m_source_table_view) {
return m_query.m_source_table_view->depends_on_deleted_object();
else if (m_query && m_query->m_source_table_view) {
return m_query->m_source_table_view->depends_on_deleted_object();
}
return false;
}
Expand All @@ -333,8 +334,8 @@ void TableView::get_dependencies(TableVersions& ret) const
ret.emplace_back(linked_table->get_key(), linked_table->get_content_version());
}
}
else if (m_query.m_table) {
m_query.get_outside_versions(ret);
else if (m_query) {
m_query->get_outside_versions(ret);
}
else {
// This TableView was created by Table::get_distinct_view() or get_sorted_view() on collections
Expand Down Expand Up @@ -362,8 +363,9 @@ void TableView::sync_if_needed() const

void TableView::update_query(const Query& q)
{
REALM_ASSERT(m_query.m_table);
REALM_ASSERT(m_query.m_table == q.m_table);
REALM_ASSERT(m_query);
REALM_ASSERT(m_query->m_table);
REALM_ASSERT(m_query->m_table == q.m_table);

m_query = q;
do_sync();
Expand Down Expand Up @@ -472,17 +474,18 @@ void TableView::do_sync()
}
// FIXME: Unimplemented for link to a column
else {
m_query.m_table.check();
REALM_ASSERT(m_query);
m_query->m_table.check();

// valid query, so clear earlier results and reexecute it.
if (m_key_values.is_attached())
m_key_values.clear();
else
m_key_values.create();

if (m_query.m_view)
m_query.m_view->sync_if_needed();
m_query.do_find_all(*const_cast<TableView*>(this), m_limit);
if (m_query->m_view)
m_query->m_view->sync_if_needed();
m_query->do_find_all(*const_cast<TableView*>(this), m_limit);
}

do_sort(m_descriptor_ordering);
Expand Down Expand Up @@ -549,11 +552,11 @@ bool TableView::is_in_table_order() const
else if (m_source_column_key) {
return false;
}
else if (!m_query.m_table) {
else if (!m_query) {
return false;
}
else {
m_query.m_table.check();
return m_query.produces_results_in_table_order() && !m_descriptor_ordering.will_apply_sort();
m_query->m_table.check();
return m_query->produces_results_in_table_order() && !m_descriptor_ordering.will_apply_sort();
}
}
7 changes: 3 additions & 4 deletions src/realm/table_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class TableView : public ObjList {
// Get the query used to create this TableView
// The query will have a null source table if this tv was not created from
// a query
const Query& get_query() const noexcept
const std::optional<Query>& get_query() const noexcept
{
return m_query;
}
Expand Down Expand Up @@ -391,9 +391,7 @@ class TableView : public ObjList {
size_t m_limit_count = 0;

// A valid query holds a reference to its table which must match our m_table.
// hence we can use a query with a null table reference to indicate that the view
// was NOT generated by a query, but follows a table directly.
Query m_query;
std::optional<Query> m_query;
// parameters for findall, needed to rerun the query
size_t m_limit = size_t(-1);

Expand Down Expand Up @@ -452,6 +450,7 @@ inline TableView::TableView(const Query& query, size_t lim)
, m_limit(lim)
{
m_key_values.create();
REALM_ASSERT(query.m_table);
}

inline TableView::TableView(ConstTableRef src_table, ColKey src_column_key, const Obj& obj)
Expand Down

0 comments on commit 347fe08

Please sign in to comment.