123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- #ifndef REALM_TABLE_VIEW_HPP
- #define REALM_TABLE_VIEW_HPP
- #include <realm/sort_descriptor.hpp>
- #include <realm/table.hpp>
- #include <realm/util/features.h>
- #include <realm/obj_list.hpp>
- #include <realm/list.hpp>
- #include <realm/set.hpp>
- namespace realm {
- class TableView : public ObjList {
- public:
-
- TableView() {}
-
- explicit TableView(ConstTableRef parent);
- TableView(const Query& query, size_t limit);
- TableView(ConstTableRef parent, ColKey column, const Obj& obj);
- TableView(LinkCollectionPtr&& collection);
-
- TableView(const TableView&);
-
- TableView(TableView&&) noexcept;
- TableView& operator=(const TableView&);
- TableView& operator=(TableView&&) noexcept;
- TableView(TableView& source, Transaction* tr, PayloadPolicy mode);
- ~TableView() {}
- TableRef get_parent() const noexcept
- {
- return m_table.cast_away_const();
- }
- TableRef get_target_table() const final
- {
- return m_table.cast_away_const();
- }
- size_t size() const final
- {
- return m_key_values.size();
- }
- bool is_empty() const noexcept
- {
- return m_key_values.size() == 0;
- }
-
- bool is_attached() const noexcept
- {
- return bool(m_table);
- }
- ObjKey get_key(size_t ndx) const final
- {
- return m_key_values.get(ndx);
- }
- bool is_obj_valid(size_t ndx) const noexcept
- {
- return m_table->is_valid(get_key(ndx));
- }
- Obj get_object(size_t ndx) const noexcept final
- {
- REALM_ASSERT(ndx < size());
- ObjKey key(m_key_values.get(ndx));
- return m_table->try_get_object(key);
- }
-
-
-
- const std::optional<Query>& get_query() const noexcept
- {
- return m_query;
- }
- void clear();
-
-
-
- void update_query(const Query& q);
- std::unique_ptr<TableView> clone() const
- {
- return std::unique_ptr<TableView>(new TableView(*this));
- }
- LinkCollectionPtr clone_obj_list() const final
- {
- return std::unique_ptr<TableView>(new TableView(*this));
- }
-
-
-
- std::unique_ptr<TableView> clone_for_handover(Transaction* tr, PayloadPolicy mode)
- {
- std::unique_ptr<TableView> retval(new TableView(*this, tr, mode));
- return retval;
- }
- template <Action action, typename T>
- Mixed aggregate(ColKey column_key, size_t* result_count = nullptr, ObjKey* return_key = nullptr) const;
- template <typename T>
- size_t aggregate_count(ColKey column_key, T count_target) const;
- size_t count_int(ColKey column_key, int64_t target) const;
- size_t count_float(ColKey column_key, float target) const;
- size_t count_double(ColKey column_key, double target) const;
- size_t count_timestamp(ColKey column_key, Timestamp target) const;
- size_t count_decimal(ColKey column_key, Decimal128 target) const;
- size_t count_mixed(ColKey column_key, Mixed target) const;
-
-
- util::Optional<Mixed> min(ColKey column_key, ObjKey* return_key = nullptr) const;
-
-
- util::Optional<Mixed> max(ColKey column_key, ObjKey* return_key = nullptr) const;
-
-
- util::Optional<Mixed> sum(ColKey column_key) const;
-
-
- util::Optional<Mixed> avg(ColKey column_key, size_t* value_count = nullptr) const;
-
-
- size_t find_by_source_ndx(ObjKey key) const noexcept
- {
- return m_key_values.find_first(key);
- }
-
- void to_json(std::ostream&, size_t link_depth = 0, const std::map<std::string, std::string>& renames = {},
- JSONOutputMode mode = output_mode_json) const;
-
-
-
-
-
-
-
- bool is_in_sync() const final;
-
-
- bool is_frozen()
- {
- return m_table->is_frozen() && is_in_sync();
- }
-
- bool depends_on_deleted_object() const;
-
-
-
-
-
-
-
-
- void sync_if_needed() const final;
-
- TableVersions get_dependency_versions() const
- {
- TableVersions ret;
- get_dependencies(ret);
- return ret;
- }
- bool has_changed() const
- {
- return m_last_seen_versions != get_dependency_versions();
- }
-
- void sort(ColKey column, bool ascending = true);
-
- void sort(SortDescriptor order);
-
-
- size_t get_num_results_excluded_by_limit() const noexcept
- {
- return m_limit_count;
- }
-
-
-
-
-
- void distinct(ColKey column);
- void distinct(DistinctDescriptor columns);
- void limit(LimitDescriptor limit);
-
-
- void apply_descriptor_ordering(const DescriptorOrdering& new_ordering);
-
-
- std::string get_descriptor_ordering_description() const;
-
-
-
-
- bool is_in_table_order() const;
- bool is_backlink_view() const
- {
- return m_source_column_key != ColKey();
- }
- protected:
-
-
-
-
-
- void get_dependencies(TableVersions&) const final;
- void do_sync();
- void do_sort(const DescriptorOrdering&);
- mutable ConstTableRef m_table;
-
- ColKey m_source_column_key;
-
- Obj m_linked_obj;
-
- mutable LinkCollectionPtr m_collection_source;
-
- DescriptorOrdering m_descriptor_ordering;
- size_t m_limit_count = 0;
-
- std::optional<Query> m_query;
-
- size_t m_limit = size_t(-1);
-
-
-
- class KeyValues : public KeyColumn {
- public:
- KeyValues()
- : KeyColumn(Allocator::get_default())
- {
- }
- KeyValues(const KeyValues&) = delete;
- ~KeyValues()
- {
- destroy();
- }
- void move_from(KeyValues&);
- void copy_from(const KeyValues&);
- };
- mutable TableVersions m_last_seen_versions;
- KeyValues m_key_values;
- private:
- ObjKey find_first_integer(ColKey column_key, int64_t value) const;
- template <Action action>
- std::optional<Mixed> aggregate(ColKey column_key, size_t* count, ObjKey* return_key) const;
- util::RaceDetector m_race_detector;
- friend class Table;
- friend class Obj;
- friend class Query;
- friend class DB;
- friend class ObjList;
- friend class LnkLst;
- };
- // ================================================================================================
- // TableView Implementation:
- inline TableView::TableView(ConstTableRef parent)
- : m_table(parent) // Throws
- {
- m_key_values.create();
- if (m_table) {
- m_last_seen_versions.emplace_back(m_table->get_key(), m_table->get_content_version());
- }
- }
- inline TableView::TableView(const Query& query, size_t lim)
- : m_table(query.get_table())
- , m_query(query)
- , 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)
- : m_table(src_table)
- , m_source_column_key(src_column_key)
- , m_linked_obj(obj)
- {
- m_key_values.create();
- if (m_table) {
- m_last_seen_versions.emplace_back(m_table->get_key(), m_table->get_content_version());
- m_last_seen_versions.emplace_back(obj.get_table()->get_key(), obj.get_table()->get_content_version());
- }
- }
- inline TableView::TableView(LinkCollectionPtr&& collection)
- : m_table(collection->get_target_table())
- , m_collection_source(std::move(collection))
- {
- REALM_ASSERT(m_collection_source);
- m_key_values.create();
- if (m_table) {
- m_last_seen_versions.emplace_back(m_table->get_key(), m_table->get_content_version());
- }
- }
- inline TableView::TableView(const TableView& tv)
- : m_table(tv.m_table)
- , m_source_column_key(tv.m_source_column_key)
- , m_linked_obj(tv.m_linked_obj)
- , m_collection_source(tv.m_collection_source ? tv.m_collection_source->clone_obj_list() : LinkCollectionPtr{})
- , m_descriptor_ordering(tv.m_descriptor_ordering)
- , m_query(tv.m_query)
- , m_limit(tv.m_limit)
- , m_last_seen_versions(tv.m_last_seen_versions)
- {
- m_key_values.copy_from(tv.m_key_values);
- m_limit_count = tv.m_limit_count;
- }
- inline TableView::TableView(TableView&& tv) noexcept
- : m_table(tv.m_table)
- , m_source_column_key(tv.m_source_column_key)
- , m_linked_obj(tv.m_linked_obj)
- , m_collection_source(std::move(tv.m_collection_source))
- , m_descriptor_ordering(std::move(tv.m_descriptor_ordering))
- , m_query(std::move(tv.m_query))
- , m_limit(tv.m_limit)
-
-
- , m_last_seen_versions(std::move(tv.m_last_seen_versions))
- {
- m_key_values.move_from(tv.m_key_values);
- m_limit_count = tv.m_limit_count;
- }
- inline TableView& TableView::operator=(TableView&& tv) noexcept
- {
- m_table = std::move(tv.m_table);
- m_key_values.move_from(tv.m_key_values);
- m_query = std::move(tv.m_query);
- m_last_seen_versions = tv.m_last_seen_versions;
- m_limit = tv.m_limit;
- m_limit_count = tv.m_limit_count;
- m_source_column_key = tv.m_source_column_key;
- m_linked_obj = tv.m_linked_obj;
- m_collection_source = std::move(tv.m_collection_source);
- m_descriptor_ordering = std::move(tv.m_descriptor_ordering);
- return *this;
- }
- inline TableView& TableView::operator=(const TableView& tv)
- {
- if (this == &tv)
- return *this;
- m_key_values.copy_from(tv.m_key_values);
- m_query = tv.m_query;
- m_last_seen_versions = tv.m_last_seen_versions;
- m_limit = tv.m_limit;
- m_limit_count = tv.m_limit_count;
- m_source_column_key = tv.m_source_column_key;
- m_linked_obj = tv.m_linked_obj;
- m_collection_source = tv.m_collection_source ? tv.m_collection_source->clone_obj_list() : LinkCollectionPtr{};
- m_descriptor_ordering = tv.m_descriptor_ordering;
- return *this;
- }
- }
- #endif
|