query.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*************************************************************************
  2. *
  3. * Copyright 2016 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. #ifndef REALM_QUERY_HPP
  19. #define REALM_QUERY_HPP
  20. #include <cstdint>
  21. #include <cstdio>
  22. #include <climits>
  23. #include <algorithm>
  24. #include <string>
  25. #include <vector>
  26. #define REALM_MULTITHREAD_QUERY 0
  27. #if REALM_MULTITHREAD_QUERY
  28. // FIXME: Use our C++ thread abstraction API since it provides a much
  29. // higher level of encapsulation and safety.
  30. #include <pthread.h>
  31. #endif
  32. #include <realm/obj_list.hpp>
  33. #include <realm/table_ref.hpp>
  34. #include <realm/binary_data.hpp>
  35. #include <realm/timestamp.hpp>
  36. #include <realm/handover_defs.hpp>
  37. #include <realm/util/serializer.hpp>
  38. #include <realm/column_type_traits.hpp>
  39. namespace realm {
  40. // Pre-declarations
  41. class ParentNode;
  42. class Table;
  43. class TableView;
  44. class ConstTableView;
  45. class Array;
  46. class Expression;
  47. class Group;
  48. class Transaction;
  49. namespace metrics {
  50. class QueryInfo;
  51. }
  52. struct QueryGroup {
  53. enum class State {
  54. Default,
  55. OrCondition,
  56. OrConditionChildren,
  57. };
  58. QueryGroup() = default;
  59. QueryGroup(const QueryGroup&);
  60. QueryGroup& operator=(const QueryGroup&);
  61. QueryGroup(QueryGroup&&) = default;
  62. QueryGroup& operator=(QueryGroup&&) = default;
  63. std::unique_ptr<ParentNode> m_root_node;
  64. bool m_pending_not = false;
  65. State m_state = State::Default;
  66. };
  67. class Query final {
  68. public:
  69. Query(ConstTableRef table, ConstTableView* tv = nullptr);
  70. Query(ConstTableRef table, std::unique_ptr<ConstTableView>);
  71. Query(ConstTableRef table, const LnkLst& list);
  72. Query(ConstTableRef table, const LnkSet& set);
  73. Query(ConstTableRef table, LnkLstPtr&& list);
  74. Query(ConstTableRef table, LnkSetPtr&& set);
  75. Query();
  76. Query(std::unique_ptr<Expression>);
  77. ~Query() noexcept;
  78. Query(const Query& copy);
  79. Query& operator=(const Query& source);
  80. Query(Query&&);
  81. Query& operator=(Query&&);
  82. // Find links that point to a specific target row
  83. Query& links_to(ColKey column_key, ObjKey target_key);
  84. // Find links that point to specific target objects
  85. Query& links_to(ColKey column_key, const std::vector<ObjKey>& target_obj);
  86. // Conditions: null
  87. Query& equal(ColKey column_key, null);
  88. Query& not_equal(ColKey column_key, null);
  89. // Conditions: int64_t
  90. Query& equal(ColKey column_key, int64_t value);
  91. Query& not_equal(ColKey column_key, int64_t value);
  92. Query& greater(ColKey column_key, int64_t value);
  93. Query& greater_equal(ColKey column_key, int64_t value);
  94. Query& less(ColKey column_key, int64_t value);
  95. Query& less_equal(ColKey column_key, int64_t value);
  96. Query& between(ColKey column_key, int64_t from, int64_t to);
  97. // Conditions: int (we need those because conversion from '1234' is ambiguous with float/double)
  98. Query& equal(ColKey column_key, int value);
  99. Query& not_equal(ColKey column_key, int value);
  100. Query& greater(ColKey column_key, int value);
  101. Query& greater_equal(ColKey column_key, int value);
  102. Query& less(ColKey column_key, int value);
  103. Query& less_equal(ColKey column_key, int value);
  104. Query& between(ColKey column_key, int from, int to);
  105. // Conditions: float
  106. Query& equal(ColKey column_key, float value);
  107. Query& not_equal(ColKey column_key, float value);
  108. Query& greater(ColKey column_key, float value);
  109. Query& greater_equal(ColKey column_key, float value);
  110. Query& less(ColKey column_key, float value);
  111. Query& less_equal(ColKey column_key, float value);
  112. Query& between(ColKey column_key, float from, float to);
  113. // Conditions: double
  114. Query& equal(ColKey column_key, double value);
  115. Query& not_equal(ColKey column_key, double value);
  116. Query& greater(ColKey column_key, double value);
  117. Query& greater_equal(ColKey column_key, double value);
  118. Query& less(ColKey column_key, double value);
  119. Query& less_equal(ColKey column_key, double value);
  120. Query& between(ColKey column_key, double from, double to);
  121. // Conditions: timestamp
  122. Query& equal(ColKey column_key, Timestamp value);
  123. Query& not_equal(ColKey column_key, Timestamp value);
  124. Query& greater(ColKey column_key, Timestamp value);
  125. Query& greater_equal(ColKey column_key, Timestamp value);
  126. Query& less_equal(ColKey column_key, Timestamp value);
  127. Query& less(ColKey column_key, Timestamp value);
  128. // Conditions: ObjectId
  129. Query& equal(ColKey column_key, ObjectId value);
  130. Query& not_equal(ColKey column_key, ObjectId value);
  131. Query& greater(ColKey column_key, ObjectId value);
  132. Query& greater_equal(ColKey column_key, ObjectId value);
  133. Query& less_equal(ColKey column_key, ObjectId value);
  134. Query& less(ColKey column_key, ObjectId value);
  135. // Conditions: UUID
  136. Query& equal(ColKey column_key, UUID value);
  137. Query& not_equal(ColKey column_key, UUID value);
  138. Query& greater(ColKey column_key, UUID value);
  139. Query& greater_equal(ColKey column_key, UUID value);
  140. Query& less_equal(ColKey column_key, UUID value);
  141. Query& less(ColKey column_key, UUID value);
  142. // Conditions: Decimal128
  143. Query& equal(ColKey column_key, Decimal128 value);
  144. Query& not_equal(ColKey column_key, Decimal128 value);
  145. Query& greater(ColKey column_key, Decimal128 value);
  146. Query& greater_equal(ColKey column_key, Decimal128 value);
  147. Query& less_equal(ColKey column_key, Decimal128 value);
  148. Query& less(ColKey column_key, Decimal128 value);
  149. Query& between(ColKey column_key, Decimal128 from, Decimal128 to);
  150. // Conditions: Mixed
  151. Query& equal(ColKey column_key, Mixed value, bool case_sensitive = true);
  152. Query& not_equal(ColKey column_key, Mixed value, bool case_sensitive = true);
  153. Query& greater(ColKey column_key, Mixed value);
  154. Query& greater_equal(ColKey column_key, Mixed value);
  155. Query& less(ColKey column_key, Mixed value);
  156. Query& less_equal(ColKey column_key, Mixed value);
  157. Query& begins_with(ColKey column_key, Mixed value, bool case_sensitive = true);
  158. Query& ends_with(ColKey column_key, Mixed value, bool case_sensitive = true);
  159. Query& contains(ColKey column_key, Mixed value, bool case_sensitive = true);
  160. Query& like(ColKey column_key, Mixed value, bool case_sensitive = true);
  161. // Conditions: size
  162. Query& size_equal(ColKey column_key, int64_t value);
  163. Query& size_not_equal(ColKey column_key, int64_t value);
  164. Query& size_greater(ColKey column_key, int64_t value);
  165. Query& size_greater_equal(ColKey column_key, int64_t value);
  166. Query& size_less_equal(ColKey column_key, int64_t value);
  167. Query& size_less(ColKey column_key, int64_t value);
  168. Query& size_between(ColKey column_key, int64_t from, int64_t to);
  169. // Conditions: bool
  170. Query& equal(ColKey column_key, bool value);
  171. Query& not_equal(ColKey column_key, bool value);
  172. // Conditions: strings
  173. Query& equal(ColKey column_key, StringData value, bool case_sensitive = true);
  174. Query& not_equal(ColKey column_key, StringData value, bool case_sensitive = true);
  175. Query& begins_with(ColKey column_key, StringData value, bool case_sensitive = true);
  176. Query& ends_with(ColKey column_key, StringData value, bool case_sensitive = true);
  177. Query& contains(ColKey column_key, StringData value, bool case_sensitive = true);
  178. Query& like(ColKey column_key, StringData value, bool case_sensitive = true);
  179. // These are shortcuts for equal(StringData(c_str)) and
  180. // not_equal(StringData(c_str)), and are needed to avoid unwanted
  181. // implicit conversion of char* to bool.
  182. Query& equal(ColKey column_key, const char* c_str, bool case_sensitive = true);
  183. Query& not_equal(ColKey column_key, const char* c_str, bool case_sensitive = true);
  184. // Conditions: binary data
  185. Query& equal(ColKey column_key, BinaryData value, bool case_sensitive = true);
  186. Query& not_equal(ColKey column_key, BinaryData value, bool case_sensitive = true);
  187. Query& begins_with(ColKey column_key, BinaryData value, bool case_sensitive = true);
  188. Query& ends_with(ColKey column_key, BinaryData value, bool case_sensitive = true);
  189. Query& contains(ColKey column_key, BinaryData value, bool case_sensitive = true);
  190. Query& like(ColKey column_key, BinaryData b, bool case_sensitive = true);
  191. // Conditions: untyped column vs column comparison
  192. // if the column types are not comparable, an exception is thrown
  193. Query& equal(ColKey column_key1, ColKey column_key2);
  194. Query& less(ColKey column_key1, ColKey column_key2);
  195. Query& less_equal(ColKey column_key1, ColKey column_key2);
  196. Query& greater(ColKey column_key1, ColKey column_key2);
  197. Query& greater_equal(ColKey column_key1, ColKey column_key2);
  198. Query& not_equal(ColKey column_key1, ColKey column_key2);
  199. // Negation
  200. Query& Not();
  201. // Grouping
  202. Query& group();
  203. Query& end_group();
  204. Query& Or();
  205. Query& and_query(const Query& q);
  206. Query& and_query(Query&& q);
  207. Query operator||(const Query& q);
  208. Query operator&&(const Query& q);
  209. Query operator!();
  210. // Searching
  211. ObjKey find();
  212. TableView find_all(size_t start = 0, size_t end = size_t(-1), size_t limit = size_t(-1));
  213. // Aggregates
  214. size_t count() const;
  215. TableView find_all(const DescriptorOrdering& descriptor);
  216. size_t count(const DescriptorOrdering& descriptor);
  217. int64_t sum_int(ColKey column_key) const;
  218. double average_int(ColKey column_key, size_t* resultcount = nullptr) const;
  219. int64_t maximum_int(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  220. int64_t minimum_int(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  221. double sum_float(ColKey column_key) const;
  222. double average_float(ColKey column_key, size_t* resultcount = nullptr) const;
  223. float maximum_float(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  224. float minimum_float(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  225. double sum_double(ColKey column_key) const;
  226. double average_double(ColKey column_key, size_t* resultcount = nullptr) const;
  227. double maximum_double(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  228. double minimum_double(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  229. Timestamp maximum_timestamp(ColKey column_key, ObjKey* return_ndx = nullptr);
  230. Timestamp minimum_timestamp(ColKey column_key, ObjKey* return_ndx = nullptr);
  231. Decimal128 sum_decimal128(ColKey column_key) const;
  232. Decimal128 maximum_decimal128(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  233. Decimal128 minimum_decimal128(ColKey column_key, ObjKey* return_ndx = nullptr) const;
  234. Decimal128 average_decimal128(ColKey column_key, size_t* resultcount = nullptr) const;
  235. // Deletion
  236. size_t remove();
  237. #if REALM_MULTITHREAD_QUERY
  238. // Multi-threading
  239. TableView find_all_multi(size_t start = 0, size_t end = size_t(-1));
  240. ConstTableView find_all_multi(size_t start = 0, size_t end = size_t(-1)) const;
  241. int set_threads(unsigned int threadcount);
  242. #endif
  243. ConstTableRef& get_table()
  244. {
  245. return m_table;
  246. }
  247. void get_outside_versions(TableVersions&) const;
  248. // True if matching rows are guaranteed to be returned in table order.
  249. bool produces_results_in_table_order() const
  250. {
  251. return !m_view;
  252. }
  253. // Calls sync_if_needed on the restricting view, if present.
  254. // Returns the current version of the table(s) this query depends on,
  255. // or empty vector if the query is not associated with a table.
  256. TableVersions sync_view_if_needed() const;
  257. std::string validate();
  258. std::string get_description(const std::string& class_prefix = "") const;
  259. std::string get_description(util::serializer::SerialisationState& state) const;
  260. Query& set_ordering(std::unique_ptr<DescriptorOrdering> ordering);
  261. std::shared_ptr<DescriptorOrdering> get_ordering();
  262. bool eval_object(const Obj& obj) const;
  263. private:
  264. void create();
  265. void init() const;
  266. size_t find_internal(size_t start = 0, size_t end = size_t(-1)) const;
  267. void handle_pending_not();
  268. void set_table(TableRef tr);
  269. public:
  270. std::unique_ptr<Query> clone_for_handover(Transaction* tr, PayloadPolicy policy) const
  271. {
  272. return std::make_unique<Query>(this, tr, policy);
  273. }
  274. Query(const Query* source, Transaction* tr, PayloadPolicy policy);
  275. Query(const Query& source, Transaction* tr, PayloadPolicy policy)
  276. : Query(&source, tr, policy)
  277. {
  278. }
  279. private:
  280. void add_expression_node(std::unique_ptr<Expression>);
  281. template <typename TConditionFunction, class T>
  282. Query& add_condition(ColKey column_key, T value);
  283. template <typename TConditionFunction>
  284. Query& add_size_condition(ColKey column_key, int64_t value);
  285. template <typename T, typename R = typename AggregateResultType<T, act_Average>::result_type>
  286. R average(ColKey column_key, size_t* resultcount = nullptr) const;
  287. template <typename T>
  288. void aggregate(QueryStateBase& st, ColKey column_key, size_t* resultcount = nullptr,
  289. ObjKey* return_ndx = nullptr) const;
  290. size_t find_best_node(ParentNode* pn) const;
  291. void aggregate_internal(ParentNode* pn, QueryStateBase* st, size_t start, size_t end,
  292. ArrayPayload* source_column) const;
  293. void find_all(ConstTableView& tv, size_t start = 0, size_t end = size_t(-1), size_t limit = size_t(-1)) const;
  294. size_t do_count(size_t limit = size_t(-1)) const;
  295. void delete_nodes() noexcept;
  296. bool has_conditions() const
  297. {
  298. return m_groups.size() > 0 && m_groups[0].m_root_node;
  299. }
  300. ParentNode* root_node() const
  301. {
  302. REALM_ASSERT(m_groups.size());
  303. return m_groups[0].m_root_node.get();
  304. }
  305. void add_node(std::unique_ptr<ParentNode>);
  306. friend class Table;
  307. friend class ConstTableView;
  308. friend class SubQueryCount;
  309. friend class PrimitiveListCount;
  310. friend class metrics::QueryInfo;
  311. std::string error_code;
  312. std::vector<QueryGroup> m_groups;
  313. mutable std::vector<TableKey> m_table_keys;
  314. TableRef m_table;
  315. // points to the base class of the restricting view. If the restricting
  316. // view is a link view, m_source_link_list is non-zero. If it is a table view,
  317. // m_source_table_view is non-zero.
  318. ObjList* m_view = nullptr;
  319. // At most one of these can be non-zero, and if so the non-zero one indicates the restricting view.
  320. LnkLstPtr m_source_link_list; // link lists are owned by the query.
  321. LnkSetPtr m_source_link_set; // link sets are owned by the query.
  322. ConstTableView* m_source_table_view = nullptr; // table views are not refcounted, and not owned by the query.
  323. std::unique_ptr<ConstTableView> m_owned_source_table_view; // <--- except when indicated here
  324. std::shared_ptr<DescriptorOrdering> m_ordering;
  325. };
  326. // Implementation:
  327. inline Query& Query::equal(ColKey column_key, const char* c_str, bool case_sensitive)
  328. {
  329. return equal(column_key, StringData(c_str), case_sensitive);
  330. }
  331. inline Query& Query::not_equal(ColKey column_key, const char* c_str, bool case_sensitive)
  332. {
  333. return not_equal(column_key, StringData(c_str), case_sensitive);
  334. }
  335. } // namespace realm
  336. #endif // REALM_QUERY_HPP