From 443d0e009843365818aa321deb2921efa6bebd6b Mon Sep 17 00:00:00 2001 From: Kruno Tomola Fabro Date: Fri, 9 Sep 2016 20:56:53 +0100 Subject: [PATCH] Changed TransactionId to TransactionRead. --- CMakeLists.txt | 2 +- include/mvcc/record.hpp | 20 ++++++------ include/mvcc/version_list.hpp | 2 +- include/snapshot/snapshot_engine.hpp | 2 +- include/storage/record_accessor.hpp | 2 +- include/transactions/transaction.hpp | 8 ++--- ...ransaction_id.hpp => transaction_read.hpp} | 12 ++++--- src/snapshot/snapshot_engine.cpp | 4 +-- src/transactions/transaction.cpp | 6 ++-- src/transactions/transaction_id.cpp | 32 ------------------- src/transactions/transaction_read.cpp | 32 +++++++++++++++++++ 11 files changed, 63 insertions(+), 59 deletions(-) rename include/transactions/{transaction_id.hpp => transaction_read.hpp} (62%) delete mode 100644 src/transactions/transaction_id.cpp create mode 100644 src/transactions/transaction_read.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5993aebe8..ec725e678 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -468,7 +468,7 @@ set(memgraph_src_files ${src_dir}/storage/garbage/garbage.cpp ${src_dir}/storage/vertex_accessor.cpp ${src_dir}/transactions/transaction.cpp - ${src_dir}/transactions/transaction_id.cpp + ${src_dir}/transactions/transaction_read.cpp ${src_dir}/template_engine/engine.cpp ${src_dir}/logging/streams/stdout.cpp ${src_dir}/logging/levels.cpp diff --git a/include/mvcc/record.hpp b/include/mvcc/record.hpp index 05f79fb10..da4829826 100644 --- a/include/mvcc/record.hpp +++ b/include/mvcc/record.hpp @@ -5,7 +5,7 @@ #include "transactions/commit_log.hpp" #include "transactions/engine.hpp" -#include "transactions/transaction_id.hpp" +#include "transactions/transaction_read.hpp" #include "mvcc/cre_exp.hpp" #include "mvcc/hints.hpp" @@ -41,7 +41,7 @@ public: RecordLock lock; // check if this record is visible to the transaction t - bool visible(const tx::TransactionId &t) + bool visible(const tx::TransactionRead &t) { // TODO check if the record was created by a transaction that has been // aborted. one might implement this by checking the hints in mvcc @@ -70,34 +70,34 @@ public: )))); } - void mark_created(const tx::TransactionId &t) + void mark_created(const tx::TransactionRead &t) { tx.cre(t.id); cmd.cre(t.cid); } - void mark_deleted(const tx::TransactionId &t) + void mark_deleted(const tx::TransactionRead &t) { tx.exp(t.id); cmd.exp(t.cid); } - bool exp_committed(const Id &id, const tx::TransactionId &t) + bool exp_committed(const Id &id, const tx::TransactionRead &t) { return committed(hints.exp, id, t); } - bool exp_committed(const tx::TransactionId &t) + bool exp_committed(const tx::TransactionRead &t) { return committed(hints.exp, tx.exp(), t); } - bool cre_committed(const Id &id, const tx::TransactionId &t) + bool cre_committed(const Id &id, const tx::TransactionRead &t) { return committed(hints.cre, id, t); } - bool cre_committed(const tx::TransactionId &t) + bool cre_committed(const tx::TransactionRead &t) { return committed(hints.cre, tx.cre(), t); } @@ -110,7 +110,7 @@ public: // TODO: Test this // True if this record is visible for write. - bool is_visible_write(const tx::TransactionId &t) + bool is_visible_write(const tx::TransactionRead &t) { return (tx.cre() == t.id && // inserted by the current transaction cmd.cre() <= t.cid && // before this command, and @@ -122,7 +122,7 @@ public: protected: template - bool committed(U &hints, const Id &id, const tx::TransactionId &t) + bool committed(U &hints, const Id &id, const tx::TransactionRead &t) { // you certainly can't see the transaction with id greater than yours // as that means it started after this transaction and if it committed, diff --git a/include/mvcc/version_list.hpp b/include/mvcc/version_list.hpp index bbb1d2701..bb1c885c9 100644 --- a/include/mvcc/version_list.hpp +++ b/include/mvcc/version_list.hpp @@ -103,7 +103,7 @@ public: void vacuum() {} - T *find(const tx::TransactionId &t) const + T *find(const tx::TransactionRead &t) const { auto r = head.load(std::memory_order_seq_cst); diff --git a/include/snapshot/snapshot_engine.hpp b/include/snapshot/snapshot_engine.hpp index 45b2bb09a..c6e785265 100644 --- a/include/snapshot/snapshot_engine.hpp +++ b/include/snapshot/snapshot_engine.hpp @@ -36,7 +36,7 @@ private: // Makes snapshot. It only saves records which have changed since old_trans. void snapshot(DbTransaction const &dt, SnapshotEncoder &snap, - tx::TransactionId const &old_trans); + tx::TransactionRead const &old_trans); // Loads snapshot. True if success bool snapshot_load(DbTransaction const &dt, SnapshotDecoder &snap); diff --git a/include/storage/record_accessor.hpp b/include/storage/record_accessor.hpp index bcc2f35b8..c6c667ba7 100644 --- a/include/storage/record_accessor.hpp +++ b/include/storage/record_accessor.hpp @@ -53,7 +53,7 @@ public: // True if record visible for current transaction is visible to given // transaction id. - bool is_visble_to(tx::TransactionId const &id) + bool is_visble_to(tx::TransactionRead const &id) { return record->visible(id); } diff --git a/include/transactions/transaction.hpp b/include/transactions/transaction.hpp index b201eaceb..db3643f37 100644 --- a/include/transactions/transaction.hpp +++ b/include/transactions/transaction.hpp @@ -9,12 +9,12 @@ #include "storage/locking/record_lock.hpp" #include "transactions/lock_store.hpp" #include "transactions/snapshot.hpp" -#include "transactions/transaction_id.hpp" +#include "transactions/transaction_read.hpp" namespace tx { -class Transaction : public TransactionId +class Transaction : public TransactionRead { public: @@ -22,8 +22,8 @@ public: Transaction(const Transaction &) = delete; Transaction(Transaction &&) = delete; - // Returns copy of transaction_id - TransactionId transaction_id(); + // Returns copy of transaction_read + TransactionRead transaction_read(); // Blocks until all transactions from snapshot finish. After this method, // snapshot will be empty. diff --git a/include/transactions/transaction_id.hpp b/include/transactions/transaction_read.hpp similarity index 62% rename from include/transactions/transaction_id.hpp rename to include/transactions/transaction_read.hpp index e97add53a..e96f55ce1 100644 --- a/include/transactions/transaction_id.hpp +++ b/include/transactions/transaction_read.hpp @@ -13,16 +13,20 @@ namespace tx class Engine; -class TransactionId +// Has read only capbilities. +// TODO: Not all applicable methods in code have been changed to accept +// TransactionRead instead of a Transaction. +class TransactionRead { friend class Engine; public: - TransactionId(Engine &engine); + TransactionRead(Engine &engine); - TransactionId(const Id &&id, const Snapshot &&snapshot, Engine &engine); + TransactionRead(const Id &&id, const Snapshot &&snapshot, + Engine &engine); - TransactionId(const Id &id, const Snapshot &snapshot, Engine &engine); + TransactionRead(const Id &id, const Snapshot &snapshot, Engine &engine); // Return id of oldest transaction from snapshot. Id oldest_active(); diff --git a/src/snapshot/snapshot_engine.cpp b/src/snapshot/snapshot_engine.cpp index 3e73ff958..5f6c4dbde 100644 --- a/src/snapshot/snapshot_engine.cpp +++ b/src/snapshot/snapshot_engine.cpp @@ -38,7 +38,7 @@ bool SnapshotEngine::make_snapshot(std::time_t now, const char *type) SnapshotEncoder snap(snapshot_file); - auto old_trans = tx::TransactionId(db.tx_engine); + auto old_trans = tx::TransactionRead(db.tx_engine); snapshot(t, snap, old_trans); auto res = sys::flush_file_to_disk(snapshot_file); @@ -135,7 +135,7 @@ bool SnapshotEngine::import() } void SnapshotEngine::snapshot(DbTransaction const &dt, SnapshotEncoder &snap, - tx::TransactionId const &old_trans) + tx::TransactionRead const &old_trans) { Db &db = dt.db; DbAccessor t(db, dt.trans); diff --git a/src/transactions/transaction.cpp b/src/transactions/transaction.cpp index 3b6aa309a..69b77457a 100644 --- a/src/transactions/transaction.cpp +++ b/src/transactions/transaction.cpp @@ -11,14 +11,14 @@ namespace tx Transaction::Transaction(const Id &id, const Snapshot &snapshot, Engine &engine) - : TransactionId(id, snapshot, engine) + : TransactionRead(id, snapshot, engine) { } // Returns copy of transaction_id -TransactionId Transaction::transaction_id() +TransactionRead Transaction::transaction_read() { - TransactionId const &t = *this; + TransactionRead const &t = *this; return t; } diff --git a/src/transactions/transaction_id.cpp b/src/transactions/transaction_id.cpp deleted file mode 100644 index e3bc7cec3..000000000 --- a/src/transactions/transaction_id.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "transactions/transaction_id.hpp" - -namespace tx -{ - -TransactionId::TransactionId(Engine &engine) - : TransactionId(Id(), Snapshot(), engine) -{ -} - -TransactionId::TransactionId(const Id &&id, const Snapshot &&snapshot, - Engine &engine) - : id(id), cid(1), snapshot(std::move(snapshot)), engine(engine) -{ -} - -TransactionId::TransactionId(const Id &id, const Snapshot &snapshot, - Engine &engine) - : id(id), cid(1), snapshot(snapshot), engine(engine) -{ -} - -bool TransactionId::in_snapshot(const Id &id) const -{ - return snapshot.is_active(id); -} - -Id TransactionId::oldest_active() -{ - return snapshot.oldest_active().take_or(Id(id)); -} -} diff --git a/src/transactions/transaction_read.cpp b/src/transactions/transaction_read.cpp new file mode 100644 index 000000000..918536732 --- /dev/null +++ b/src/transactions/transaction_read.cpp @@ -0,0 +1,32 @@ +#include "transactions/transaction_read.hpp" + +namespace tx +{ + +TransactionRead::TransactionRead(Engine &engine) + : TransactionRead(Id(), Snapshot(), engine) +{ +} + +TransactionRead::TransactionRead(const Id &&id, const Snapshot &&snapshot, + Engine &engine) + : id(id), cid(1), snapshot(std::move(snapshot)), engine(engine) +{ +} + +TransactionRead::TransactionRead(const Id &id, const Snapshot &snapshot, + Engine &engine) + : id(id), cid(1), snapshot(snapshot), engine(engine) +{ +} + +bool TransactionRead::in_snapshot(const Id &id) const +{ + return snapshot.is_active(id); +} + +Id TransactionRead::oldest_active() +{ + return snapshot.oldest_active().take_or(Id(id)); +} +}