LineairDB  0.1.0
transaction.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Nippon Telegraph and Telephone Corporation.
3 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7 
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef LINEAIRDB_TRANSACTION_H
17 #define LINEAIRDB_TRANSACTION_H
18 
19 #include <lineairdb/tx_status.h>
20 
21 #include <cstddef>
22 #include <cstring>
23 #include <functional>
24 #include <memory>
25 #include <optional>
26 #include <string_view>
27 #include <type_traits>
28 
29 namespace LineairDB {
30 
51 class Transaction {
52  public:
64 
77  const std::pair<const std::byte* const, const size_t> Read(
78  const std::string_view key);
79 
91  template <typename T>
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;
100  } else {
101  return std::nullopt;
102  }
103  }
104 
113  void Write(const std::string_view key, const std::byte value[],
114  const size_t size);
115 
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));
133  };
134 
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>)>
165  operation);
166 
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);
188  });
189  }
190 
195  void Abort();
196 
197  private:
198  Transaction(void*) noexcept;
199  ~Transaction() noexcept;
200  bool Precommit();
201 
202  private:
203  class Impl;
204  const std::unique_ptr<Impl> tx_pimpl_;
205  friend class Database;
206 };
207 
208 } // namespace LineairDB
209 #endif
LineairDB::TxStatus
TxStatus
Definition: tx_status.h:25
LineairDB::Transaction::Write
void Write(const std::string_view key, const std::byte value[], const size_t size)
Writes a value with a given key.
LineairDB::Transaction::Scan
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....
LineairDB::Transaction::Scan
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
LineairDB::Transaction::IsRunning
bool IsRunning()
Definition: transaction.h:61
LineairDB::Running
@ Running
Definition: tx_status.h:25
LineairDB::Transaction::GetCurrentStatus
TxStatus GetCurrentStatus()
Get the current transaction status. For transactions such that GetCurrentStatus() returns TxStatus::A...
LineairDB::Transaction::Abort
void Abort()
Abort this transaction manually.
LineairDB::Database
Definition: database.h:31
LineairDB::Aborted
@ Aborted
Definition: tx_status.h:25
LineairDB::Transaction::IsCommitted
bool IsCommitted()
Definition: transaction.h:62
LineairDB::Transaction::Read
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,...
LineairDB::Transaction::IsAborted
bool IsAborted()
Definition: transaction.h:63
LineairDB::Transaction
We adopt "the page model" [Vossen95] as the model of transaction processing. For each transaction,...
Definition: transaction.h:51
LineairDB
Definition: config.h:24
LineairDB::Transaction::Write
void Write(const std::string_view key, const T &value)
Writes an user-defined value with a given key.
Definition: transaction.h:127
LineairDB::Transaction::Read
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
tx_status.h