Blame view

Pods/Realm/include/core/realm/keys.hpp 8.28 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
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
  /*************************************************************************
   *
   * 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_KEYS_HPP
  #define REALM_KEYS_HPP
  
  #include <realm/util/to_string.hpp>
  #include <realm/column_type.hpp>
  #include <ostream>
  #include <vector>
  
  namespace realm {
  
  class Obj;
  
  struct TableKey {
      static constexpr uint32_t null_value = uint32_t(-1) >> 1; // free top bit
  
      constexpr TableKey() noexcept
          : value(null_value)
      {
      }
      constexpr explicit TableKey(uint32_t val) noexcept
          : value(val)
      {
      }
      constexpr bool operator==(const TableKey& rhs) const noexcept
      {
          return value == rhs.value;
      }
      constexpr bool operator!=(const TableKey& rhs) const noexcept
      {
          return value != rhs.value;
      }
      constexpr bool operator<(const TableKey& rhs) const noexcept
      {
          return value < rhs.value;
      }
      constexpr bool operator>(const TableKey& rhs) const noexcept
      {
          return value > rhs.value;
      }
  
      constexpr explicit operator bool() const noexcept
      {
          return value != null_value;
      }
      uint32_t value;
  };
  
  
  inline std::ostream& operator<<(std::ostream& os, TableKey tk)
  {
      os << "TableKey(" << tk.value << ")";
      return os;
  }
  
  namespace util {
  
  inline std::string to_string(TableKey tk)
  {
      return to_string(tk.value);
  }
  } // namespace util
  
  class TableVersions : public std::vector<std::pair<TableKey, uint64_t>> {
  public:
      TableVersions() {}
      TableVersions(TableKey key, uint64_t version)
      {
          emplace_back(key, version);
      }
      bool operator==(const TableVersions& other) const;
  };
  
  struct ColKey {
      static constexpr int64_t null_value = int64_t(uint64_t(-1) >> 1); // free top bit
  
      struct Idx {
          unsigned val;
      };
  
      constexpr ColKey() noexcept
          : value(null_value)
      {
      }
      constexpr explicit ColKey(int64_t val) noexcept
          : value(val)
      {
      }
      constexpr ColKey(Idx index, ColumnType type, ColumnAttrMask attrs, uint64_t tag) noexcept
          : ColKey((index.val & 0xFFFFUL) | ((int(type) & 0x3FUL) << 16) | ((attrs.m_value & 0xFFUL) << 22) |
                   ((tag & 0xFFFFFFFFUL) << 30))
      {
      }
      bool is_nullable() const
      {
          return get_attrs().test(col_attr_Nullable);
      }
      bool is_list() const
      {
          return get_attrs().test(col_attr_List);
      }
      bool is_set() const
      {
          return get_attrs().test(col_attr_Set);
      }
      bool is_dictionary() const
      {
          return get_attrs().test(col_attr_Dictionary);
      }
      bool is_collection() const
      {
          return get_attrs().test(col_attr_Collection);
      }
      bool operator==(const ColKey& rhs) const noexcept
      {
          return value == rhs.value;
      }
      bool operator!=(const ColKey& rhs) const noexcept
      {
          return value != rhs.value;
      }
      bool operator<(const ColKey& rhs) const noexcept
      {
          return value < rhs.value;
      }
      bool operator>(const ColKey& rhs) const noexcept
      {
          return value > rhs.value;
      }
      explicit operator bool() const noexcept
      {
          return value != null_value;
      }
      Idx get_index() const noexcept
      {
          return Idx{static_cast<unsigned>(value) & 0xFFFFU};
      }
      ColumnType get_type() const noexcept
      {
          return ColumnType(ColumnType::Type((static_cast<unsigned>(value) >> 16) & 0x3F));
      }
      ColumnAttrMask get_attrs() const noexcept
      {
          return ColumnAttrMask((static_cast<unsigned>(value) >> 22) & 0xFF);
      }
      unsigned get_tag() const noexcept
      {
          return (value >> 30) & 0xFFFFFFFFUL;
      }
      int64_t value;
  };
  
  static_assert(ColKey::null_value == 0x7fffffffffffffff, "Fix this");
  
  inline std::ostream& operator<<(std::ostream& os, ColKey ck)
  {
      os << "ColKey(" << ck.value << ")";
      return os;
  }
  
  struct ObjKey {
      constexpr ObjKey() noexcept
          : value(-1)
      {
      }
      explicit constexpr ObjKey(int64_t val) noexcept
          : value(val)
      {
      }
      bool is_unresolved() const
      {
          return value <= -2;
      }
      ObjKey get_unresolved() const
      {
          return ObjKey(-2 - value);
      }
      bool operator==(const ObjKey& rhs) const noexcept
      {
          return value == rhs.value;
      }
      bool operator!=(const ObjKey& rhs) const noexcept
      {
          return value != rhs.value;
      }
      bool operator<(const ObjKey& rhs) const noexcept
      {
          return value < rhs.value;
      }
      bool operator<=(const ObjKey& rhs) const noexcept
      {
          return value <= rhs.value;
      }
      bool operator>(const ObjKey& rhs) const noexcept
      {
          return value > rhs.value;
      }
      bool operator>=(const ObjKey& rhs) const noexcept
      {
          return value >= rhs.value;
      }
      explicit operator bool() const noexcept
      {
          return value != -1;
      }
      int64_t value;
  
  private:
      // operator bool will enable casting to integer. Prevent this.
      operator int64_t() const = delete;
  };
  
  inline std::ostream& operator<<(std::ostream& ostr, ObjKey key)
  {
      ostr << "ObjKey(" << key.value << ")";
      return ostr;
  }
  
  class ObjKeys : public std::vector<ObjKey> {
  public:
      explicit ObjKeys(const std::vector<int64_t>& init)
      {
          reserve(init.size());
          for (auto i : init) {
              emplace_back(i);
          }
      }
      ObjKeys(const std::initializer_list<int64_t>& list)
      {
          reserve(list.size());
          for (auto i : list) {
              emplace_back(i);
          }
      }
      ObjKeys() {}
  };
  
  struct ObjLink {
  public:
      ObjLink() {}
      ObjLink(TableKey table_key, ObjKey obj_key)
          : m_obj_key(obj_key)
          , m_table_key(table_key)
      {
      }
      explicit operator bool() const
      {
          return bool(m_table_key) && bool(m_obj_key);
      }
      bool is_null() const
      {
          return !bool(*this);
      }
      bool is_unresolved() const
      {
          return m_obj_key.is_unresolved();
      }
      bool operator==(const ObjLink& other) const
      {
          return m_obj_key == other.m_obj_key && m_table_key == other.m_table_key;
      }
      bool operator!=(const ObjLink& other) const
      {
          return m_obj_key != other.m_obj_key || m_table_key != other.m_table_key;
      }
      bool operator<(const ObjLink& rhs) const
      {
          if (m_table_key < rhs.m_table_key) {
              return true;
          }
          else if (m_table_key == rhs.m_table_key) {
              return m_obj_key < rhs.m_obj_key;
          }
          else {
              // m_table_key >= rhs.m_table_key
              return false;
          }
      }
      bool operator>(const ObjLink& rhs) const
      {
          return (*this != rhs) && !(*this < rhs);
      }
      TableKey get_table_key() const
      {
          return m_table_key;
      }
      ObjKey get_obj_key() const
      {
          return m_obj_key;
      }
  
  private:
      // Having ObjKey first ensures that there will be no uninitialized space
      // in the first 12 bytes. This is important when generating a hash
      ObjKey m_obj_key;
      TableKey m_table_key;
  };
  
  inline std::ostream& operator<<(std::ostream& os, ObjLink link)
  {
      os << '{' << link.get_table_key() << ',' << link.get_obj_key() << '}';
      return os;
  }
  constexpr ObjKey null_key;
  
  namespace util {
  
  inline std::string to_string(ColKey ck)
  {
      return to_string(ck.value);
  }
  
  } // namespace util
  
  } // namespace realm
  
  
  namespace std {
  
  template <>
  struct hash<realm::ObjKey> {
      size_t operator()(realm::ObjKey key) const
      {
          return std::hash<uint64_t>{}(key.value);
      }
  };
  template <>
  struct hash<realm::ColKey> {
      size_t operator()(realm::ColKey key) const
      {
          return std::hash<int64_t>{}(key.value);
      }
  };
  template <>
  struct hash<realm::TableKey> {
      size_t operator()(realm::TableKey key) const
      {
          return std::hash<uint32_t>{}(key.value);
      }
  };
  
  } // namespace std
  
  
  #endif