Blame view

Pods/Realm/include/core/realm/cluster_tree.hpp 6.95 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  /*************************************************************************
   *
   * Copyright 2016 Realm Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   **************************************************************************/
  
  #ifndef REALM_CLUSTER_TREE_HPP
  #define REALM_CLUSTER_TREE_HPP
  
  #include <realm/cluster.hpp>
  #include <realm/util/function_ref.hpp>
  
  namespace realm {
  
  class Cluster;
  
  class ClusterTree {
  public:
      class Iterator;
      using TraverseFunction = util::FunctionRef<bool(const Cluster*)>;
      using UpdateFunction = util::FunctionRef<void(Cluster*)>;
      using ColIterateFunction = util::FunctionRef<bool(ColKey)>;
  
      ClusterTree(Allocator& alloc);
      virtual ~ClusterTree();
  
      ClusterTree(ClusterTree&&) = default;
  
      // Disable copying, this is not allowed.
      ClusterTree& operator=(const ClusterTree&) = delete;
      ClusterTree(const ClusterTree&) = delete;
  
      bool is_attached() const
      {
          return m_root && m_root->is_attached();
      }
      Allocator& get_alloc() const
      {
          return m_alloc;
      }
  
      /// Initialize the accessor from its slot in the `ArrayParent`. If the ref
      /// in the parent slot is zero, this returns false and leaves the
      /// `ClusterTree` in an unusable state.
      bool init_from_parent();
      void update_from_parent() noexcept;
  
      size_t size() const noexcept
      {
          return m_size;
      }
  
      size_t nb_columns() const
      {
          return m_root->nb_columns();
      }
  
      static size_t size_from_ref(ref_type, Allocator& alloc);
  
      void destroy()
      {
          m_root->destroy_deep();
      }
      void nullify_links(ObjKey, CascadeState&);
      bool is_empty() const noexcept
      {
          return size() == 0;
      }
      int64_t get_last_key_value() const
      {
          return m_root->get_last_key_value();
      }
      MemRef ensure_writeable(ObjKey k)
      {
          return m_root->ensure_writeable(k);
      }
      void update_ref_in_parent(ObjKey k, ref_type ref)
      {
          m_root->update_ref_in_parent(k, ref);
      }
      Array& get_fields_accessor(Array& fallback, MemRef mem) const
      {
          if (m_root->is_leaf()) {
              return *m_root;
          }
          fallback.init_from_mem(mem);
          return fallback;
      }
  
      uint64_t bump_content_version()
      {
          return m_alloc.bump_content_version();
      }
      void bump_storage_version()
      {
          m_alloc.bump_storage_version();
      }
      uint64_t get_content_version() const
      {
          return m_alloc.get_content_version();
      }
      uint64_t get_instance_version() const
      {
          return m_alloc.get_instance_version();
      }
      uint64_t get_storage_version(uint64_t inst_ver) const
      {
          return m_alloc.get_storage_version(inst_ver);
      }
      void insert_column(ColKey col)
      {
          m_root->insert_column(col);
      }
      void remove_column(ColKey col)
      {
          m_root->remove_column(col);
      }
  
      // Insert entry for object, but do not create and return the object accessor
      void insert_fast(ObjKey k, const FieldValues& init_values, ClusterNode::State& state);
      // Create and return object
      ClusterNode::State insert(ObjKey k, const FieldValues&);
      // Delete object with given key
      void erase(ObjKey k, CascadeState& state);
      // Check if an object with given key exists
      bool is_valid(ObjKey k) const;
      // Lookup and return object
      ClusterNode::State get(ObjKey k) const;
      // Lookup and return object
      ClusterNode::State try_get(ObjKey k) const noexcept;
      // Lookup by index
      ClusterNode::State get(size_t ndx, ObjKey& k) const;
      // Get logical index of object identified by k
      size_t get_ndx(ObjKey k) const noexcept;
      // Find the leaf containing the requested object
      bool get_leaf(ObjKey key, ClusterNode::IteratorState& state) const noexcept;
      // Visit all leaves and call the supplied function. Stop when function returns true.
      // Not allowed to modify the tree
      bool traverse(TraverseFunction func) const;
      // Visit all leaves and call the supplied function. The function can modify the leaf.
      void update(UpdateFunction func);
  
      virtual void for_each_and_every_column(ColIterateFunction) const = 0;
      virtual void update_indexes(ObjKey k, const FieldValues& init_values) = 0;
      virtual void cleanup_key(ObjKey k) = 0;
      virtual void set_spec(ArrayPayload& arr, ColKey::Idx col_ndx) const = 0;
      virtual bool is_string_enum_type(ColKey::Idx col_ndx) const = 0;
      virtual const Table* get_owning_table() const noexcept = 0;
      virtual std::unique_ptr<ClusterNode> get_root_from_parent() = 0;
  
      void dump_objects()
      {
          m_root->dump_objects(0, "");
      }
      void verify() const;
  
  protected:
      friend class Obj;
      friend class Cluster;
      friend class ClusterNodeInner;
  
      Allocator& m_alloc;
  
      std::unique_ptr<ClusterNode> m_root;
      size_t m_size = 0;
  
      void clear();
      void replace_root(std::unique_ptr<ClusterNode> leaf);
  
      std::unique_ptr<ClusterNode> create_root_from_parent(ArrayParent* parent, size_t ndx_in_parent);
      std::unique_ptr<ClusterNode> get_node(ArrayParent* parent, size_t ndx_in_parent) const;
  };
  
  class ClusterTree::Iterator {
  public:
      Iterator(const ClusterTree& t, size_t ndx);
      Iterator(const Iterator& other);
  
      Iterator& operator=(const Iterator& other)
      {
          REALM_ASSERT(&m_tree == &other.m_tree);
          m_position = other.m_position;
          m_key = other.m_key;
          m_leaf_invalid = true;
  
          return *this;
      }
  
      // Set the iterator to the given absolute position in the table.
      void go(size_t abs_pos);
      bool update() const;
      // Advance the iterator to the next object in the table. This also holds if the object
      // pointed to is deleted. That is - you will get the same result of advancing no matter
      // if the previous object is deleted or not.
      Iterator& operator++();
  
      Iterator& operator+=(ptrdiff_t adj);
  
      bool operator==(const Iterator& rhs) const
      {
          return m_key == rhs.m_key;
      }
      bool operator!=(const Iterator& rhs) const
      {
          return m_key != rhs.m_key;
      }
  
  protected:
      const ClusterTree& m_tree;
      mutable uint64_t m_storage_version = uint64_t(-1);
      mutable Cluster m_leaf;
      mutable ClusterNode::IteratorState m_state;
      mutable uint64_t m_instance_version = uint64_t(-1);
      ObjKey m_key;
      mutable bool m_leaf_invalid;
      mutable size_t m_position;
      mutable size_t m_leaf_start_pos = size_t(-1);
  
      ObjKey load_leaf(ObjKey key) const;
      size_t get_position();
  };
  } // namespace realm
  
  #endif /* REALM_CLUSTER_TREE_HPP */