a724cdb112d9ee05c8c5b918e6019eb7cfc4d982.svn-base
11.3 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*************************************************************************
*
* REALM CONFIDENTIAL
* __________________
*
* [2011] - [2015] Realm Inc
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Realm Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Realm Incorporated
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Realm Incorporated.
*
**************************************************************************/
#ifndef REALM_LINK_VIEW_HPP
#define REALM_LINK_VIEW_HPP
#include <realm/util/bind_ptr.hpp>
#include <realm/column.hpp>
#include <realm/column_linklist.hpp>
#include <realm/link_view_fwd.hpp>
#include <realm/table.hpp>
namespace realm {
class LinkListColumn;
namespace _impl {
class LinkListFriend;
class TransactLogConvenientEncoder;
}
/// The effect of calling most of the link list functions on a detached accessor
/// is unspecified and may lead to general corruption, or even a crash. The
/// exceptions are is_attached() and the destructor.
///
/// FIXME: Rename this class to `LinkList`.
class LinkView : public RowIndexes {
public:
~LinkView() noexcept;
bool is_attached() const noexcept;
/// This method will return true if the LinkView is detached (no assert).
bool is_empty() const noexcept;
/// This method will return 0 if the LinkView is detached (no assert).
size_t size() const noexcept;
bool operator==(const LinkView&) const noexcept;
bool operator!=(const LinkView&) const noexcept;
// Getting links
Table::ConstRowExpr operator[](size_t link_ndx) const noexcept;
Table::RowExpr operator[](size_t link_ndx) noexcept;
Table::ConstRowExpr get(size_t link_ndx) const noexcept;
Table::RowExpr get(size_t link_ndx) noexcept;
// Modifiers
void add(size_t target_row_ndx);
void insert(size_t link_ndx, size_t target_row_ndx);
void set(size_t link_ndx, size_t target_row_ndx);
/// Move the link at \a from_ndx such that it ends up at \a to_ndx. Other
/// links are shifted as necessary in such a way that their order is
/// preserved.
///
/// Note that \a to_ndx is the desired final index of the moved link,
/// therefore, `move(1,1)` is a no-op, while `move(1,2)` moves the link at
/// index 1 by one position, such that it ends up at index 2. A side-effect
/// of that, is that the link, that was originally at index 2, is moved to
/// index 1.
void move(size_t from_ndx, size_t to_ndx);
void swap(size_t link1_ndx, size_t link2_ndx);
void remove(size_t link_ndx);
void clear();
void sort(size_t column, bool ascending = true);
void sort(std::vector<size_t> columns, std::vector<bool> ascending);
TableView get_sorted_view(std::vector<size_t> column_indexes, std::vector<bool> ascending) const;
TableView get_sorted_view(size_t column_index, bool ascending = true) const;
/// Remove the target row of the specified link from the target table. This
/// also removes the specified link from this link list, and any other link
/// pointing to that row. This is merely a shorthand for
/// `get_target_table.move_last_over(get(link_ndx))`.
void remove_target_row(size_t link_ndx);
/// Remove all target rows pointed to by links in this link list, and clear
/// this link list.
void remove_all_target_rows();
/// Search this list for a link to the specified target table row (specified
/// by its index in the target table). If found, the index of the link to
/// that row within this list is returned, otherwise `realm::not_found` is
/// returned.
size_t find(size_t target_row_ndx, size_t start=0) const noexcept;
const ColumnBase& get_column_base(size_t index) const; // FIXME: `ColumnBase` is not part of the public API, so this function must be made private.
const Table& get_origin_table() const noexcept;
Table& get_origin_table() noexcept;
size_t get_origin_row_index() const noexcept;
const Table& get_target_table() const noexcept;
Table& get_target_table() noexcept;
private:
TableRef m_origin_table;
LinkListColumn& m_origin_column;
mutable size_t m_ref_count;
using HandoverPatch = LinkViewHandoverPatch;
static void generate_patch(const ConstLinkViewRef& ref, std::unique_ptr<HandoverPatch>& patch);
static LinkViewRef create_from_and_consume_patch(std::unique_ptr<HandoverPatch>& patch, Group& group);
// constructor (protected since it can only be used by friends)
LinkView(Table* origin_table, LinkListColumn&, size_t row_ndx);
void detach();
void set_origin_row_index(size_t row_ndx) noexcept;
size_t do_set(size_t link_ndx, size_t target_row_ndx);
size_t do_remove(size_t link_ndx);
void do_clear(bool broken_reciprocal_backlinks);
void do_nullify_link(size_t old_target_row_ndx);
void do_update_link(size_t old_target_row_ndx, size_t new_target_row_ndx);
void do_swap_link(size_t target_row_ndx_1, size_t target_row_ndx_2);
void bind_ptr() const noexcept;
void unbind_ptr() const noexcept;
void refresh_accessor_tree(size_t new_row_ndx) noexcept;
void update_from_parent(size_t old_baseline) noexcept;
Replication* get_repl() noexcept;
void repl_unselect() noexcept;
friend class _impl::TransactLogConvenientEncoder;
#ifdef REALM_DEBUG
void verify(size_t row_ndx) const;
#endif
friend class _impl::LinkListFriend;
friend class LinkListColumn;
friend class util::bind_ptr<LinkView>;
friend class util::bind_ptr<const LinkView>;
friend class LangBindHelper;
friend class SharedGroup;
friend class Query;
friend class TableViewBase;
};
// Implementation
inline LinkView::LinkView(Table* origin_table, LinkListColumn& column, size_t row_ndx):
RowIndexes(IntegerColumn::unattached_root_tag(), column.get_alloc()), // Throws
m_origin_table(origin_table->get_table_ref()),
m_origin_column(column),
m_ref_count(0)
{
Array& root = *m_row_indexes.get_root_array();
root.set_parent(&column, row_ndx);
if (ref_type ref = root.get_ref_from_parent())
root.init_from_ref(ref);
}
inline LinkView::~LinkView() noexcept
{
if (is_attached()) {
repl_unselect();
m_origin_column.unregister_linkview(*this);
}
}
inline void LinkView::bind_ptr() const noexcept
{
++m_ref_count;
}
inline void LinkView::unbind_ptr() const noexcept
{
if (--m_ref_count > 0)
return;
delete this;
}
inline void LinkView::detach()
{
REALM_ASSERT(is_attached());
repl_unselect();
m_origin_table.reset();
m_row_indexes.detach();
}
inline bool LinkView::is_attached() const noexcept
{
return static_cast<bool>(m_origin_table);
}
inline bool LinkView::is_empty() const noexcept
{
if (!is_attached())
return true;
if (!m_row_indexes.is_attached())
return true;
return m_row_indexes.is_empty();
}
inline size_t LinkView::size() const noexcept
{
if (!is_attached())
return 0;
if (!m_row_indexes.is_attached())
return 0;
return m_row_indexes.size();
}
inline bool LinkView::operator==(const LinkView& link_list) const noexcept
{
Table& target_table_1 = m_origin_column.get_target_table();
Table& target_table_2 = link_list.m_origin_column.get_target_table();
if (target_table_1.get_index_in_group() != target_table_2.get_index_in_group())
return false;
if (!m_row_indexes.is_attached() || m_row_indexes.is_empty()) {
return !link_list.m_row_indexes.is_attached() ||
link_list.m_row_indexes.is_empty();
}
return link_list.m_row_indexes.is_attached() &&
m_row_indexes.compare(link_list.m_row_indexes);
}
inline bool LinkView::operator!=(const LinkView& link_list) const noexcept
{
return !(*this == link_list);
}
inline Table::ConstRowExpr LinkView::get(size_t link_ndx) const noexcept
{
return const_cast<LinkView*>(this)->get(link_ndx);
}
inline Table::RowExpr LinkView::get(size_t link_ndx) noexcept
{
REALM_ASSERT(is_attached());
REALM_ASSERT(m_row_indexes.is_attached());
REALM_ASSERT_3(link_ndx, <, m_row_indexes.size());
Table& target_table = m_origin_column.get_target_table();
size_t target_row_ndx = to_size_t(m_row_indexes.get(link_ndx));
return target_table[target_row_ndx];
}
inline Table::ConstRowExpr LinkView::operator[](size_t link_ndx) const noexcept
{
return get(link_ndx);
}
inline Table::RowExpr LinkView::operator[](size_t link_ndx) noexcept
{
return get(link_ndx);
}
inline void LinkView::add(size_t target_row_ndx)
{
REALM_ASSERT(is_attached());
size_t ins_pos = (m_row_indexes.is_attached()) ? m_row_indexes.size() : 0;
insert(ins_pos, target_row_ndx);
}
inline size_t LinkView::find(size_t target_row_ndx, size_t start) const noexcept
{
REALM_ASSERT(is_attached());
REALM_ASSERT_3(target_row_ndx, <, m_origin_column.get_target_table().size());
REALM_ASSERT_3(start, <=, size());
if (!m_row_indexes.is_attached())
return not_found;
return m_row_indexes.find_first(target_row_ndx, start);
}
inline const ColumnBase& LinkView::get_column_base(size_t index) const
{
return get_target_table().get_column_base(index);
}
inline const Table& LinkView::get_origin_table() const noexcept
{
return *m_origin_table;
}
inline Table& LinkView::get_origin_table() noexcept
{
return *m_origin_table;
}
inline size_t LinkView::get_origin_row_index() const noexcept
{
REALM_ASSERT(is_attached());
return m_row_indexes.get_root_array()->get_ndx_in_parent();
}
inline void LinkView::set_origin_row_index(size_t row_ndx) noexcept
{
REALM_ASSERT(is_attached());
m_row_indexes.get_root_array()->set_ndx_in_parent(row_ndx);
}
inline const Table& LinkView::get_target_table() const noexcept
{
return m_origin_column.get_target_table();
}
inline Table& LinkView::get_target_table() noexcept
{
return m_origin_column.get_target_table();
}
inline void LinkView::refresh_accessor_tree(size_t new_row_ndx) noexcept
{
Array& root = *m_row_indexes.get_root_array();
root.set_ndx_in_parent(new_row_ndx);
if (ref_type ref = root.get_ref_from_parent()) {
root.init_from_ref(ref);
}
else {
root.detach();
}
}
inline void LinkView::update_from_parent(size_t old_baseline) noexcept
{
if (m_row_indexes.is_attached())
m_row_indexes.update_from_parent(old_baseline);
}
inline Replication* LinkView::get_repl() noexcept
{
typedef _impl::TableFriend tf;
return tf::get_repl(*m_origin_table);
}
// The purpose of this class is to give internal access to some, but not all of
// the non-public parts of LinkView.
class _impl::LinkListFriend {
public:
static void do_set(LinkView& list, size_t link_ndx, size_t target_row_ndx)
{
list.do_set(link_ndx, target_row_ndx);
}
static void do_remove(LinkView& list, size_t link_ndx)
{
list.do_remove(link_ndx);
}
static void do_clear(LinkView& list)
{
bool broken_reciprocal_backlinks = false;
list.do_clear(broken_reciprocal_backlinks);
}
};
} // namespace realm
#endif // REALM_LINK_VIEW_HPP