Metall  v0.27
A persistent memory allocator for data-centric analytics
object_attribute_accessor.hpp
Go to the documentation of this file.
1 // Copyright 2020 Lawrence Livermore National Security, LLC and other Metall
2 // Project Developers. See the top-level COPYRIGHT file for details.
3 //
4 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
5 
6 #ifndef METALL_KERNEL_OBJECT_ATTRIBUTE_ACCESSOR_HPP
7 #define METALL_KERNEL_OBJECT_ATTRIBUTE_ACCESSOR_HPP
8 
9 #include <type_traits>
10 #include <memory>
11 
12 #include <metall/tags.hpp>
13 #include <metall/kernel/attributed_object_directory.hpp>
14 
15 namespace metall {
16 
19 namespace attraccs_detail {
20 
24 template <typename _offset_type, typename _size_type>
26  private:
27  using object_directory_type =
28  kernel::attributed_object_directory<_offset_type, _size_type>;
29 
30  struct core_data {
31  object_directory_type object_directory{};
32  std::string object_attribute_file_path{};
33  };
34 
35  public:
36  // -------------------- //
37  // Public types and static values
38  // -------------------- //
39  using size_type = typename object_directory_type::size_type;
40  using name_type = typename object_directory_type::name_type;
41  using offset_type = typename object_directory_type::offset_type;
42  using length_type = typename object_directory_type::length_type;
43  using description_type = typename object_directory_type::description_type;
44 
45  using const_iterator = typename object_directory_type::const_iterator;
46 
47  general_named_object_attr_accessor() noexcept = default;
48 
50  const std::string &object_attribute_file_path) noexcept {
51  priv_alloc_core_data();
52  try {
53  m_core_data->object_attribute_file_path = object_attribute_file_path;
54  const bool succeeded = m_core_data->object_directory.deserialize(
55  m_core_data->object_attribute_file_path.c_str());
56  if (!succeeded) {
57  m_core_data.reset(nullptr);
58  }
59  } catch (...) {
60  logger::out(logger::level::error, __FILE__, __LINE__,
61  "Filed to initialize the core data");
62  m_core_data.reset(nullptr);
63  }
64  }
65 
67  const general_named_object_attr_accessor &other) noexcept {
68  if (priv_alloc_core_data()) {
69  priv_copy_core_data(other);
70  }
71  }
72 
74  general_named_object_attr_accessor &&) noexcept = default;
75 
77  const general_named_object_attr_accessor &other) noexcept {
78  priv_copy_core_data(other);
79  return *this;
80  }
81 
83  general_named_object_attr_accessor &&) noexcept = default;
84 
87  bool good() const noexcept { return !!m_core_data; }
88 
91  size_type num_objects() const noexcept {
92  if (!m_core_data) return 0;
93  return m_core_data->object_directory.size();
94  }
95 
100  size_type count(const char *name) const noexcept {
101  if (!m_core_data) return 0;
102  return m_core_data->object_directory.count(name);
103  }
104 
109  const_iterator find(const char *name) const noexcept {
110  if (!m_core_data) return const_iterator();
111 
112  return m_core_data->object_directory.find(name);
113  }
114 
118  const_iterator begin() const noexcept {
119  if (!m_core_data) return const_iterator();
120 
121  return m_core_data->object_directory.begin();
122  }
123 
127  const_iterator end() const noexcept {
128  if (!m_core_data) return const_iterator();
129 
130  return m_core_data->object_directory.end();
131  }
132 
140  const description_type &description) noexcept {
141  if (!m_core_data) return false;
142 
143  if (position == end()) return false;
144 
145  if (!m_core_data->object_directory.set_description(position, description) ||
146  !m_core_data->object_directory.serialize(
147  m_core_data->object_attribute_file_path.c_str())) {
148  logger::out(logger::level::error, __FILE__, __LINE__,
149  "Filed to set description");
150  return false;
151  }
152 
153  return true;
154  }
155 
162  bool set_description(const char *name,
163  const description_type &description) noexcept {
164  return set_description(find(name), description);
165  }
166 
167  private:
168  bool priv_alloc_core_data() noexcept {
169  try {
170  m_core_data = std::make_unique<core_data>();
171  } catch (...) {
172  m_core_data.reset(nullptr);
173  return false;
174  }
175  return true;
176  }
177 
178  bool priv_copy_core_data(
179  const general_named_object_attr_accessor &other) noexcept {
180  try {
181  m_core_data = other.m_core_data;
182  } catch (...) {
183  logger::out(logger::level::error, __FILE__, __LINE__,
184  "Filed to copy the core data");
185  m_core_data.reset(nullptr);
186  return false;
187  }
188  return true;
189  }
190 
191  std::unique_ptr<core_data> m_core_data{nullptr};
192 };
193 } // namespace attraccs_detail
194 
198 template <typename _offset_type, typename _size_type>
201  _size_type> {
202  private:
203  using base_type =
205  _size_type>;
206 
207  public:
208  using size_type = typename base_type::size_type;
209  using name_type = typename base_type::name_type;
214 
215  named_object_attr_accessor() noexcept = default;
216 
218  const std::string &object_attribute_file_path) noexcept
219  : base_type(object_attribute_file_path) {}
220 };
221 
225 template <typename _offset_type, typename _size_type>
228  _size_type> {
229  private:
230  using base_type =
232  _size_type>;
233 
234  public:
235  using size_type = typename base_type::size_type;
236  using name_type = typename base_type::name_type;
241 
242  unique_object_attr_accessor() noexcept = default;
243 
245  const std::string &object_attribute_file_path) noexcept
246  : base_type(object_attribute_file_path) {}
247 
252  size_type count(const char *name) const noexcept {
253  return base_type::count(name);
254  }
255 
259  template <typename T>
260  size_type count(const decltype(unique_instance) &) const noexcept {
261  return base_type::count(typeid(T).name());
262  }
263 
268  const_iterator find(const char *name) const noexcept {
269  return base_type::find(name);
270  }
271 
276  template <typename T>
277  const_iterator find(const decltype(unique_instance) &) const noexcept {
278  return base_type::find(typeid(T).name());
279  }
280 
288  const description_type &description) noexcept {
289  return base_type::set_description(position, description);
290  }
291 
298  bool set_description(const char *name,
299  const description_type &description) noexcept {
300  return base_type::set_description(name, description);
301  }
302 
309  template <typename T>
310  bool set_description(const decltype(unique_instance) &,
311  const description_type &description) noexcept {
312  return base_type::set_description(typeid(T).name(), description);
313  }
314 };
315 
319 template <typename _offset_type, typename _size_type>
321  private:
322  using object_directory_type =
323  kernel::attributed_object_directory<_offset_type, _size_type>;
324 
325  struct core_data {
326  object_directory_type object_directory{};
327  std::string object_attribute_file_path{};
328  };
329 
330  public:
331  // -------------------- //
332  // Public types and static values
333  // -------------------- //
334  using size_type = typename object_directory_type::size_type;
335  using name_type = typename object_directory_type::name_type;
336  using offset_type = typename object_directory_type::offset_type;
337  using length_type = typename object_directory_type::length_type;
338  using description_type = typename object_directory_type::description_type;
339 
340  using const_iterator = typename object_directory_type::const_iterator;
341 
342  anonymous_object_attr_accessor() noexcept = default;
343 
345  const std::string &object_attribute_file_path) noexcept {
346  priv_alloc_core_data();
347  try {
348  m_core_data->object_attribute_file_path = object_attribute_file_path;
349  const bool succeeded = m_core_data->object_directory.deserialize(
350  m_core_data->object_attribute_file_path.c_str());
351  if (!succeeded) {
352  m_core_data.reset(nullptr);
353  }
354  } catch (...) {
355  logger::out(logger::level::error, __FILE__, __LINE__,
356  "Filed to initialize the core data");
357  m_core_data.reset(nullptr);
358  }
359  }
360 
362  const anonymous_object_attr_accessor &other) noexcept {
363  if (priv_alloc_core_data()) {
364  priv_copy_core_data(other);
365  }
366  }
367 
369  default;
370 
372  const anonymous_object_attr_accessor &other) noexcept {
373  priv_copy_core_data(other);
374  return *this;
375  }
376 
378  anonymous_object_attr_accessor &&) noexcept = default;
379 
382  bool good() const noexcept { return !!m_core_data; }
383 
386  size_type num_objects() const noexcept {
387  if (!m_core_data) return 0;
388  return m_core_data->object_directory.size();
389  }
390 
394  const_iterator begin() const noexcept {
395  if (!m_core_data) return const_iterator();
396  return m_core_data->object_directory.begin();
397  }
398 
402  const_iterator end() const noexcept {
403  if (!m_core_data) return const_iterator();
404  return m_core_data->object_directory.end();
405  }
406 
414  const description_type &description) noexcept {
415  if (!m_core_data) return false;
416 
417  if (position == end()) return false;
418 
419  if (!m_core_data->object_directory.set_description(position, description) ||
420  !m_core_data->object_directory.serialize(
421  m_core_data->object_attribute_file_path.c_str())) {
422  logger::out(logger::level::error, __FILE__, __LINE__,
423  "Filed to set description");
424  return false;
425  }
426 
427  return true;
428  }
429 
430  private:
431  bool priv_alloc_core_data() noexcept {
432  try {
433  m_core_data = std::make_unique<core_data>();
434  } catch (...) {
435  m_core_data.reset(nullptr);
436  return false;
437  }
438  return true;
439  }
440 
441  bool priv_copy_core_data(
442  const anonymous_object_attr_accessor &other) noexcept {
443  try {
444  m_core_data = other.m_core_data;
445  } catch (...) {
446  logger::out(logger::level::error, __FILE__, __LINE__,
447  "Filed to copy the core data");
448  m_core_data.reset(nullptr);
449  return false;
450  }
451  return true;
452  }
453 
454  std::unique_ptr<core_data> m_core_data{nullptr};
455 };
456 } // namespace metall
457 
458 #endif // METALL_KERNEL_OBJECT_ATTRIBUTE_ACCESSOR_HPP
Definition: object_attribute_accessor.hpp:320
anonymous_object_attr_accessor(anonymous_object_attr_accessor &&) noexcept=default
typename object_directory_type::size_type size_type
Definition: object_attribute_accessor.hpp:334
const_iterator begin() const noexcept
Returns a const iterator that points the beginning of stored object attribute.
Definition: object_attribute_accessor.hpp:394
size_type num_objects() const noexcept
Returns the number of objects in the directory.
Definition: object_attribute_accessor.hpp:386
bool set_description(const_iterator position, const description_type &description) noexcept
Sets an description. An existing one is overwrite.
Definition: object_attribute_accessor.hpp:413
bool good() const noexcept
Returns if the internal state is good.
Definition: object_attribute_accessor.hpp:382
anonymous_object_attr_accessor() noexcept=default
typename object_directory_type::description_type description_type
Definition: object_attribute_accessor.hpp:338
typename object_directory_type::offset_type offset_type
Definition: object_attribute_accessor.hpp:336
typename object_directory_type::const_iterator const_iterator
Definition: object_attribute_accessor.hpp:340
anonymous_object_attr_accessor(const anonymous_object_attr_accessor &other) noexcept
Definition: object_attribute_accessor.hpp:361
typename object_directory_type::length_type length_type
Definition: object_attribute_accessor.hpp:337
typename object_directory_type::name_type name_type
Definition: object_attribute_accessor.hpp:335
anonymous_object_attr_accessor & operator=(anonymous_object_attr_accessor &&) noexcept=default
const_iterator end() const noexcept
Returns a const iterator that points the end of stored object attribute.
Definition: object_attribute_accessor.hpp:402
Definition: object_attribute_accessor.hpp:25
typename object_directory_type::const_iterator const_iterator
Definition: object_attribute_accessor.hpp:45
general_named_object_attr_accessor & operator=(general_named_object_attr_accessor &&) noexcept=default
const_iterator find(const char *name) const noexcept
Finds the position of the object attribute with 'name'.
Definition: object_attribute_accessor.hpp:109
typename object_directory_type::offset_type offset_type
Definition: object_attribute_accessor.hpp:41
const_iterator end() const noexcept
Returns a const iterator that points the end of stored object attribute.
Definition: object_attribute_accessor.hpp:127
typename object_directory_type::length_type length_type
Definition: object_attribute_accessor.hpp:42
bool set_description(const_iterator position, const description_type &description) noexcept
Sets an description. An existing one is overwrite.
Definition: object_attribute_accessor.hpp:139
bool good() const noexcept
Returns if the internal state is good.
Definition: object_attribute_accessor.hpp:87
size_type num_objects() const noexcept
Returns the number of objects in the directory.
Definition: object_attribute_accessor.hpp:91
general_named_object_attr_accessor(general_named_object_attr_accessor &&) noexcept=default
size_type count(const char *name) const noexcept
Counts the number of objects with the name. As object name must be unique, only 1 or 0 is returned.
Definition: object_attribute_accessor.hpp:100
typename object_directory_type::description_type description_type
Definition: object_attribute_accessor.hpp:43
const_iterator begin() const noexcept
Returns a const iterator that points the beginning of stored object attribute.
Definition: object_attribute_accessor.hpp:118
typename object_directory_type::size_type size_type
Definition: object_attribute_accessor.hpp:39
typename object_directory_type::name_type name_type
Definition: object_attribute_accessor.hpp:40
general_named_object_attr_accessor(const general_named_object_attr_accessor &other) noexcept
Definition: object_attribute_accessor.hpp:66
bool set_description(const char *name, const description_type &description) noexcept
Sets an description. An existing one is overwrite.
Definition: object_attribute_accessor.hpp:162
static void out(const level lvl, const char *const file_name, const int line_no, const char *const message) noexcept
Log a message to std::cerr if the specified logger level is equal to or higher than the pre-set logge...
Definition: logger.hpp:48
@ error
Error logger message.
Objet attribute accessor for named object.
Definition: object_attribute_accessor.hpp:201
named_object_attr_accessor() noexcept=default
Objet attribute accessor for unique object.
Definition: object_attribute_accessor.hpp:228
bool set_description(const char *name, const description_type &description) noexcept
Sets an description. An existing one is overwrite.
Definition: object_attribute_accessor.hpp:298
size_type count(const char *name) const noexcept
Counts the number of objects with the name. As object name must be unique, only 1 or 0 is returned.
Definition: object_attribute_accessor.hpp:252
const_iterator find(const decltype(unique_instance) &) const noexcept
Finds the position of the attribute of the unique object of type T.
Definition: object_attribute_accessor.hpp:277
bool set_description(const_iterator position, const description_type &description) noexcept
Sets an description. An existing one is overwrite.
Definition: object_attribute_accessor.hpp:287
unique_object_attr_accessor() noexcept=default
const_iterator find(const char *name) const noexcept
Finds the position of the object attribute with 'name'.
Definition: object_attribute_accessor.hpp:268
size_type count(const decltype(unique_instance) &) const noexcept
Counts the number of the unique object of type T with the name, i.e., 1 or 0 is returned.
Definition: object_attribute_accessor.hpp:260
bool set_description(const decltype(unique_instance) &, const description_type &description) noexcept
Sets an description to the unique object of type T. An existing one is overwrite.
Definition: object_attribute_accessor.hpp:310
basic_string< char > string
A string container that uses char as its character type and Metall as its default allocator.
Definition: string.hpp:23
The top level of namespace of Metall.
Definition: basic_manager.hpp:18