19 #ifndef PSTORE_CORE_TRANSACTION_HPP 20 #define PSTORE_CORE_TRANSACTION_HPP 23 #include <type_traits> 27 #include "pstore/core/time.hpp" 53 database & db () noexcept {
return db_; }
54 database const & db ()
const noexcept {
return db_; }
58 bool is_open () const noexcept {
return first_ != address::null (); }
69 std::shared_ptr<void const> getro (
address const addr, std::size_t
const size) {
70 PSTORE_ASSERT (addr >= first_ && addr + size <= first_ + size_);
71 return db ().
getro (addr, size);
75 typename =
typename std::enable_if<std::is_standard_layout<T>::value>::type>
76 std::shared_ptr<T const> getro (
extent<T> const & ex) {
77 return this->getro (ex.
addr, ex.
size);
81 typename =
typename std::enable_if<std::is_standard_layout<T>::value>::type>
83 std::size_t
const elements = 1) {
84 PSTORE_ASSERT (addr.to_address () >= first_ &&
85 (addr.to_address () + elements *
sizeof (T)) <= first_ + size_);
86 return db_.
getro (addr, elements);
92 std::shared_ptr<void> getrw (
address const addr, std::size_t
const size) {
93 PSTORE_ASSERT (addr >= first_ && addr + size <= first_ + size_);
94 return db_.
getrw (addr, size);
98 typename =
typename std::enable_if<std::is_standard_layout<T>::value>::type>
99 std::shared_ptr<T> getrw (
extent<T> const & ex) {
100 return db_.
getrw (ex);
103 template <
typename T,
104 typename =
typename std::enable_if<std::is_standard_layout<T>::value>::type>
105 std::shared_ptr<T> getrw (
typed_address<T> const addr, std::size_t
const elements = 1) {
106 PSTORE_ASSERT (addr.to_address () >= first_ &&
107 (addr.to_address () + elements *
sizeof (T)) <= first_ + size_);
108 return db_.
getrw (addr, elements);
126 template <
typename Ty>
128 return this->
allocate (
sizeof (Ty),
alignof (Ty));
145 std::pair<std::shared_ptr<void>,
address>
alloc_rw (std::size_t size,
unsigned align);
156 template <
typename Ty,
157 typename =
typename std::enable_if<std::is_standard_layout<Ty>::value>::type>
160 auto result = this->
alloc_rw (
sizeof (Ty) * num,
alignof (Ty));
161 return {std::static_pointer_cast<Ty> (result.first),
typed_address<Ty> (result.second)};
166 std::uint64_t
size () const noexcept {
return size_; }
174 std::uint64_t size_ = 0;
177 std::uint64_t dbsize_ = 0;
181 address first_ = address::null ();
190 template <
typename LockGuard>
193 using lock_type = LockGuard;
210 template <
typename LockGuard>
213 , lock_{std::move (lock)} {
215 PSTORE_ASSERT (!this->
is_open ());
220 template <
typename LockGuard>
222 static_assert (noexcept (
rollback ()),
"rollback must be noexcept");
235 template <
typename MutexType>
239 : mut_{std::move (mut)} {
245 : mut_{std::move (rhs.mut_)}
246 , owned_{rhs.owned_} {
260 mut_ = std::move (rhs.mut_);
286 sizeof (lock_block::transaction_lock),
296 void lock () { rl_.lock (); }
297 void unlock () { rl_.unlock (); }
312 return {db, std::move (lock)};
315 return {db, std::move (lock)};
323 #endif // PSTORE_CORE_TRANSACTION_HPP bool is_open() const noexcept
Returns true if data has been added to this transaction, but not yet committed.
Definition: transaction.hpp:58
Specifies a write (or exclusive) lock.
lock_guard fills a similar role as a type such as std::scoped_lock<> in that it provides convenient R...
Definition: transaction.hpp:236
std::shared_ptr< void const > getro(address const addr, std::size_t const size) const
Definition: database.hpp:128
The lock-block is a small struct placed immediately after the file header which is used by the transa...
Definition: file_header.hpp:162
typed_address< T > addr
The address of the data associated with this extent.
Definition: address.hpp:456
An extent is a contiguous area of storage reserved for a data BLOB, represented as a range...
Definition: address.hpp:441
auto alloc_rw(std::size_t const num=1) -> std::pair< std::shared_ptr< Ty >, typed_address< Ty >>
Allocates sufficient space in the transaction for one or more new instances of type 'Ty' and returns ...
Definition: transaction.hpp:158
Definition: address.hpp:81
file::file_base const * file() const
Returns the file in which this database is contained.
Definition: database.hpp:87
transaction< transaction_lock > begin(database &db, transaction_lock &lock)
Creates a new transaction. Every operation performed on a transaction instance can be potentially und...
Definition: transaction.hpp:311
std::pair< std::shared_ptr< void >, address > alloc_rw(std::size_t size, unsigned align)
Allocates sufficient space in the transaction for 'size' bytes at an alignment given by 'align' and r...
Definition: transaction.cpp:75
Definition: transaction.hpp:191
Definition: address.hpp:231
std::shared_ptr< void > getrw(address const addr, std::size_t const size)
Load a block of data starting at address addr and of size bytes.
Definition: database.hpp:189
A synchronization object that can be used to protect data in a file from being simultaneously accesse...
Definition: file.hpp:382
std::uint64_t size
The size of the data associated with this extent.
Definition: address.hpp:461
address allocate()
Extend the database store ensuring that there's enough room for an instance of the template type...
Definition: transaction.hpp:127
transaction_base & commit()
Commits all modifications made to the data store as part of this transaction.
Definition: transaction.cpp:89
transaction_base & rollback() noexcept
Discards all modifications made to the data store as part of this transaction.
Definition: transaction.cpp:147
Definition: nonpod2.cpp:40
std::uint64_t size() const noexcept
Returns the number of bytes allocated in this transaction.
Definition: transaction.hpp:166
Definition: database.hpp:40
A mutex which is used to protect a pstore file from being simultaneously written by multiple threads ...
Definition: transaction.hpp:280
The database transaction class.
Definition: transaction.hpp:43