Go to the documentation of this file.
16 #ifndef LINEAIRDB_TRANSACTION_H
17 #define LINEAIRDB_TRANSACTION_H
26 #include <string_view>
27 #include <type_traits>
77 const std::pair<const std::byte* const, const size_t>
Read(
78 const std::string_view key);
92 const std::optional<T>
Read(
const std::string_view key) {
93 static_assert(std::is_trivially_copyable<T>::value ==
true,
94 "LineairDB expects to read/write trivially copyable types.");
95 auto result =
Read(key);
96 if (result.second != 0) {
97 const T copy_constructed_result =
98 *
reinterpret_cast<const T*
>(result.first);
99 return copy_constructed_result;
113 void Write(
const std::string_view key,
const std::byte value[],
126 template <
typename T>
127 void Write(
const std::string_view key,
const T& value) {
128 static_assert(std::is_trivially_copyable<T>::value ==
true,
129 "LineairDB expects to read/write trivially copyable types.");
130 std::byte buffer[
sizeof(T)];
131 std::memcpy(buffer, &value,
sizeof(T));
132 Write(key, buffer,
sizeof(T));
161 const std::optional<size_t>
Scan(
162 const std::string_view begin,
const std::string_view end,
163 std::function<
bool(std::string_view,
164 const std::pair<const void*, const size_t>)>
179 template <
typename T>
180 const std::optional<size_t>
Scan(
181 const std::string_view begin,
const std::string_view end,
182 std::function<
bool(std::string_view, T)> operation) {
183 static_assert(std::is_trivially_copyable<T>::value ==
true,
184 "LineairDB expects to trivially copyable types.");
185 return Scan(begin, end, [&](
auto key,
auto pair) {
186 const T copy_constructed = *
reinterpret_cast<const T*
>(pair.first);
187 return operation(key, copy_constructed);
204 const std::unique_ptr<Impl> tx_pimpl_;
TxStatus
Definition: tx_status.h:25
void Write(const std::string_view key, const std::byte value[], const size_t size)
Writes a value with a given key.
const std::optional< size_t > Scan(const std::string_view begin, const std::string_view end, std::function< bool(std::string_view, const std::pair< const void *, const size_t >)> operation)
Get all data items that match the range from the "begin" key to the "end" key in the lexical order....
const std::optional< size_t > Scan(const std::string_view begin, const std::string_view end, std::function< bool(std::string_view, T)> operation)
Scan operation with user-defined template type.
Definition: transaction.h:180
bool IsRunning()
Definition: transaction.h:61
@ Running
Definition: tx_status.h:25
TxStatus GetCurrentStatus()
Get the current transaction status. For transactions such that GetCurrentStatus() returns TxStatus::A...
void Abort()
Abort this transaction manually.
Definition: database.h:31
@ Aborted
Definition: tx_status.h:25
bool IsCommitted()
Definition: transaction.h:62
const std::pair< const std::byte *const, const size_t > Read(const std::string_view key)
If the database contains a data item for "key", returns a pair (a pointer of value,...
bool IsAborted()
Definition: transaction.h:63
We adopt "the page model" [Vossen95] as the model of transaction processing. For each transaction,...
Definition: transaction.h:51
void Write(const std::string_view key, const T &value)
Writes an user-defined value with a given key.
Definition: transaction.h:127
const std::optional< T > Read(const std::string_view key)
Reads a value as user-defined type. T must be same as one on writing the value with Write().
Definition: transaction.h:92