PMDK C++ bindings  1.8
This is the C++ bindings documentation for PMDK's libpmemobj.
pool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2019, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #ifndef LIBPMEMOBJ_CPP_POOL_HPP
39 #define LIBPMEMOBJ_CPP_POOL_HPP
40 
41 #include <cstddef>
42 #include <string>
43 #include <sys/stat.h>
44 
47 #include <libpmemobj++/p.hpp>
49 #include <libpmemobj/pool_base.h>
50 
51 namespace pmem
52 {
53 
54 namespace obj
55 {
56 template <typename T>
57 class persistent_ptr;
58 
67 class pool_base {
68 public:
72  pool_base() noexcept : pop(nullptr)
73  {
74  }
75 
83  explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
84  {
85  }
86 
90  pool_base(const pool_base &) noexcept = default;
91 
95  pool_base(pool_base &&) noexcept = default;
96 
100  pool_base &operator=(const pool_base &) noexcept = default;
101 
105  pool_base &operator=(pool_base &&) noexcept = default;
106 
110  virtual ~pool_base() noexcept = default;
111 
124  static pool_base
125  open(const std::string &path, const std::string &layout)
126  {
127 #ifdef _WIN32
128  pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
129 #else
130  pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
131 #endif
132  if (pop == nullptr)
133  throw pmem::pool_error("Failed opening pool")
134  .with_pmemobj_errormsg();
135 
136  return pool_base(pop);
137  }
138 
155  static pool_base
156  create(const std::string &path, const std::string &layout,
157  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
158  {
159 #ifdef _WIN32
160  pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
161  size, mode);
162 #else
163  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
164  size, mode);
165 #endif
166  if (pop == nullptr)
167  throw pmem::pool_error("Failed creating pool")
168  .with_pmemobj_errormsg();
169 
170  return pool_base(pop);
171  }
172 
183  static int
184  check(const std::string &path, const std::string &layout) noexcept
185  {
186 #ifdef _WIN32
187  return pmemobj_checkU(path.c_str(), layout.c_str());
188 #else
189  return pmemobj_check(path.c_str(), layout.c_str());
190 #endif
191  }
192 
193 #ifdef _WIN32
194 
207  static pool_base
208  open(const std::wstring &path, const std::wstring &layout)
209  {
210  pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
211  if (pop == nullptr)
212  throw pmem::pool_error("Failed opening pool")
213  .with_pmemobj_errormsg();
214 
215  return pool_base(pop);
216  }
217 
235  static pool_base
236  create(const std::wstring &path, const std::wstring &layout,
237  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
238  {
239  pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
240  size, mode);
241  if (pop == nullptr)
242  throw pmem::pool_error("Failed creating pool")
243  .with_pmemobj_errormsg();
244 
245  return pool_base(pop);
246  }
247 
259  static int
260  check(const std::wstring &path, const std::wstring &layout) noexcept
261  {
262  return pmemobj_checkW(path.c_str(), layout.c_str());
263  }
264 #endif
265 
271  void
273  {
274  if (this->pop == nullptr)
275  throw std::logic_error("Pool already closed");
276 
277  pmemobj_close(this->pop);
278  this->pop = nullptr;
279  }
280 
287  void
288  persist(const void *addr, size_t len) noexcept
289  {
290  pmemobj_persist(this->pop, addr, len);
291  }
292 
298  template <typename Y>
299  void
300  persist(const p<Y> &prop) noexcept
301  {
302  pmemobj_persist(this->pop, &prop, sizeof(Y));
303  }
304 
310  template <typename Y>
311  void
312  persist(const persistent_ptr<Y> &ptr) noexcept
313  {
314  pmemobj_persist(this->pop, &ptr, sizeof(ptr));
315  }
316 
323  void
324  flush(const void *addr, size_t len) noexcept
325  {
326  pmemobj_flush(this->pop, addr, len);
327  }
328 
334  template <typename Y>
335  void
336  flush(const p<Y> &prop) noexcept
337  {
338  pmemobj_flush(this->pop, &prop, sizeof(Y));
339  }
340 
346  template <typename Y>
347  void
348  flush(const persistent_ptr<Y> &ptr) noexcept
349  {
350  pmemobj_flush(this->pop, &ptr, sizeof(ptr));
351  }
352 
356  void
357  drain(void) noexcept
358  {
359  pmemobj_drain(this->pop);
360  }
361 
372  void *
373  memcpy_persist(void *dest, const void *src, size_t len) noexcept
374  {
375  return pmemobj_memcpy_persist(this->pop, dest, src, len);
376  }
377 
388  void *
389  memset_persist(void *dest, int c, size_t len) noexcept
390  {
391  return pmemobj_memset_persist(this->pop, dest, c, len);
392  }
393 
401  PMEMobjpool *
402  handle() noexcept
403  {
404  return this->pop;
405  }
406 
407  POBJ_CPP_DEPRECATED PMEMobjpool *
408  get_handle() noexcept
409  {
410  return pool_base::handle();
411  }
412 
413 protected:
414  /* The pool opaque handle */
415  PMEMobjpool *pop;
416 
417 #ifndef _WIN32
418  /* Default create mode */
419  static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
420 #else
421  /* Default create mode */
422  static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
423 #endif
424 };
425 
434 template <typename T>
435 class pool : public pool_base {
436 public:
440  pool() noexcept = default;
441 
445  pool(const pool &) noexcept = default;
446 
450  pool(pool &&) noexcept = default;
451 
455  pool &operator=(const pool &) noexcept = default;
456 
460  pool &operator=(pool &&) noexcept = default;
461 
465  ~pool() noexcept = default;
466 
470  explicit pool(const pool_base &pb) noexcept : pool_base(pb)
471  {
472  }
473 
477  explicit pool(pool_base &&pb) noexcept : pool_base(pb)
478  {
479  }
480 
491  template <typename M>
492  M
493  ctl_get(const std::string &name)
494  {
495  return ctl_get_detail<M>(pop, name);
496  }
497 
509  template <typename M>
510  M
511  ctl_set(const std::string &name, M arg)
512  {
513  return ctl_set_detail(pop, name, arg);
514  }
515 
527  template <typename M>
528  M
529  ctl_exec(const std::string &name, M arg)
530  {
531  return ctl_exec_detail(pop, name, arg);
532  }
533 
534 #ifdef _WIN32
535 
545  template <typename M>
546  M
547  ctl_get(const std::wstring &name)
548  {
549  return ctl_get_detail<M>(pop, name);
550  }
551 
563  template <typename M>
564  M
565  ctl_set(const std::wstring &name, M arg)
566  {
567  return ctl_set_detail(pop, name, arg);
568  }
569 
581  template <typename M>
582  M
583  ctl_exec(const std::wstring &name, M arg)
584  {
585  return ctl_exec_detail(pop, name, arg);
586  }
587 #endif
588 
596  {
597  if (pop == nullptr)
598  throw pmem::pool_error("Invalid pool handle");
599 
600  persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
601  return root;
602  }
603 
604  POBJ_CPP_DEPRECATED persistent_ptr<T>
605  get_root()
606  {
607  return pool::root();
608  }
609 
622  static pool<T>
623  open(const std::string &path, const std::string &layout)
624  {
625  return pool<T>(pool_base::open(path, layout));
626  }
627 
644  static pool<T>
645  create(const std::string &path, const std::string &layout,
646  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
647  {
648  return pool<T>(pool_base::create(path, layout, size, mode));
649  }
650 
661  static int
662  check(const std::string &path, const std::string &layout)
663  {
664  return pool_base::check(path, layout);
665  }
666 
667 #ifdef _WIN32
668 
681  static pool<T>
682  open(const std::wstring &path, const std::wstring &layout)
683  {
684  return pool<T>(pool_base::open(path, layout));
685  }
686 
704  static pool<T>
705  create(const std::wstring &path, const std::wstring &layout,
706  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
707  {
708  return pool<T>(pool_base::create(path, layout, size, mode));
709  }
710 
722  static int
723  check(const std::wstring &path, const std::wstring &layout)
724  {
725  return pool_base::check(path, layout);
726  }
727 #endif
728 };
729 
740 template <typename T>
741 T
742 ctl_get(const std::string &name)
743 {
744  return ctl_get_detail<T>(nullptr, name);
745 }
746 
758 template <typename T>
759 T
760 ctl_set(const std::string &name, T arg)
761 {
762  return ctl_set_detail(nullptr, name, arg);
763 }
764 
776 template <typename T>
777 T
778 ctl_exec(const std::string &name, T arg)
779 {
780  return ctl_exec_detail(nullptr, name, arg);
781 }
782 
783 #ifdef _WIN32
784 
794 template <typename T>
795 T
796 ctl_get(const std::wstring &name)
797 {
798  return ctl_get_detail<T>(nullptr, name);
799 }
800 
812 template <typename T>
813 T
814 ctl_set(const std::wstring &name, T arg)
815 {
816  return ctl_set_detail(nullptr, name, arg);
817 }
818 
830 template <typename T>
831 T
832 ctl_exec(const std::wstring &name, T arg)
833 {
834  return ctl_exec_detail(nullptr, name, arg);
835 }
836 #endif
837 
838 } /* namespace obj */
839 
840 } /* namespace pmem */
841 
842 #endif /* LIBPMEMOBJ_CPP_POOL_HPP */
M ctl_exec(const std::string &name, M arg)
Execute function at pool scope.
Definition: pool.hpp:529
pool() noexcept=default
Defaulted constructor.
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:184
T ctl_get(const std::string &name)
Query libpmemobj state at global scope.
Definition: pool.hpp:742
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent object.
Definition: pool.hpp:312
Persistent pointer class.
Definition: common.hpp:125
persistent_ptr< T > root()
Retrieves pool's root object.
Definition: pool.hpp:595
The non-template pool base class.
Definition: pool.hpp:67
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:477
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:336
void * memcpy_persist(void *dest, const void *src, size_t len) noexcept
Performs memcpy and persist operation on a given chunk of memory.
Definition: pool.hpp:373
Custom pool error class.
Definition: pexceptions.hpp:72
C++ ctl api.
static pool_base open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:208
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: concurrent_hash_map.hpp:65
static pool< T > open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:623
Resides on pmem property template.
PMEMobj pool class.
Definition: persistent_ptr.hpp:59
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:402
static pool_base create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:156
M ctl_get(const std::string &name)
Query libpmemobj state at pool scope.
Definition: pool.hpp:493
Commonly used functionality.
static pool_base open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:125
Custom exceptions.
T ctl_exec(const std::string &name, T arg)
Execute function at global scope.
Definition: pool.hpp:778
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:288
void * memset_persist(void *dest, int c, size_t len) noexcept
Performs memset and persist operation on a given chunk of memory.
Definition: pool.hpp:389
M ctl_set(const std::wstring &name, M arg)
Modify libpmemobj state at pool scope.
Definition: pool.hpp:565
static int check(const std::wstring &path, const std::wstring &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:723
M ctl_set(const std::string &name, M arg)
Modify libpmemobj state at pool scope.
Definition: pool.hpp:511
static pool< T > create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:645
M ctl_get(const std::wstring &name)
Query libpmemobj state at pool scope.
Definition: pool.hpp:547
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:348
static int check(const std::wstring &path, const std::wstring &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:260
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:300
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:72
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:83
T ctl_set(const std::string &name, T arg)
Modify libpmemobj state at global scope.
Definition: pool.hpp:760
static int check(const std::string &path, const std::string &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:662
static pool_base create(const std::wstring &path, const std::wstring &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:236
Resides on pmem class.
Definition: p.hpp:64
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: allocation_flag.hpp:43
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:357
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:324
static pool< T > open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:682
static pool< T > create(const std::wstring &path, const std::wstring &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:705
M ctl_exec(const std::wstring &name, M arg)
Execute function at pool scope.
Definition: pool.hpp:583
void close()
Closes the pool.
Definition: pool.hpp:272