diff --git a/src/storage/v2/storage.cpp b/src/storage/v2/storage.cpp index 65f927862..c34fa8953 100644 --- a/src/storage/v2/storage.cpp +++ b/src/storage/v2/storage.cpp @@ -75,6 +75,26 @@ std::optional Storage::Accessor::FindVertex(Gid gid, return VertexAccessor::Create(&*it, transaction_, view); } +Result Storage::Accessor::DeleteVertex(VertexAccessor *vertex) { + CHECK(vertex->transaction_ == transaction_) + << "VertexAccessor must be from the same transaction as the storage " + "accessor when deleting a vertex!"; + auto vertex_ptr = vertex->vertex_; + + std::lock_guard guard(vertex_ptr->lock); + + if (!PrepareForWrite(transaction_, vertex_ptr)) + return Result{Error::SERIALIZATION_ERROR}; + + if (vertex_ptr->deleted) return Result{false}; + + CreateAndLinkDelta(transaction_, vertex_ptr, Delta::RecreateObjectTag()); + + vertex_ptr->deleted = true; + + return Result{true}; +} + void Storage::Accessor::AdvanceCommand() { ++transaction_->command_id; } void Storage::Accessor::Commit() { diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp index 9f0bc014a..2bec6a135 100644 --- a/src/storage/v2/storage.hpp +++ b/src/storage/v2/storage.hpp @@ -4,6 +4,7 @@ #include "utils/skip_list.hpp" +#include "storage/v2/result.hpp" #include "storage/v2/transaction.hpp" #include "storage/v2/vertex.hpp" #include "storage/v2/vertex_accessor.hpp" @@ -39,6 +40,8 @@ class Storage final { std::optional FindVertex(Gid gid, View view); + Result DeleteVertex(VertexAccessor *vertex); + void AdvanceCommand(); void Commit(); diff --git a/src/storage/v2/vertex_accessor.cpp b/src/storage/v2/vertex_accessor.cpp index b1e35ea08..0d82aaf93 100644 --- a/src/storage/v2/vertex_accessor.cpp +++ b/src/storage/v2/vertex_accessor.cpp @@ -37,21 +37,6 @@ std::optional VertexAccessor::Create(Vertex *vertex, return VertexAccessor{vertex, transaction}; } -Result VertexAccessor::Delete() { - std::lock_guard guard(vertex_->lock); - - if (!PrepareForWrite(transaction_, vertex_)) - return Result{Error::SERIALIZATION_ERROR}; - - if (vertex_->deleted) return Result{false}; - - CreateAndLinkDelta(transaction_, vertex_, Delta::RecreateObjectTag()); - - vertex_->deleted = true; - - return Result{true}; -} - Result VertexAccessor::AddLabel(uint64_t label) { std::lock_guard guard(vertex_->lock); diff --git a/src/storage/v2/vertex_accessor.hpp b/src/storage/v2/vertex_accessor.hpp index 9d10ec00d..8b2f73698 100644 --- a/src/storage/v2/vertex_accessor.hpp +++ b/src/storage/v2/vertex_accessor.hpp @@ -10,8 +10,12 @@ namespace storage { +class Storage; + class VertexAccessor final { private: + friend class Storage; + VertexAccessor(Vertex *vertex, Transaction *transaction) : vertex_(vertex), transaction_(transaction) {} @@ -20,8 +24,6 @@ class VertexAccessor final { Transaction *transaction, View view); - Result Delete(); - Result AddLabel(uint64_t label); Result RemoveLabel(uint64_t label); diff --git a/tests/unit/storage_v2.cpp b/tests/unit/storage_v2.cpp index 40ca05cc7..d9671ec0c 100644 --- a/tests/unit/storage_v2.cpp +++ b/tests/unit/storage_v2.cpp @@ -28,7 +28,7 @@ TEST(StorageV2, Commit) { auto vertex = acc.FindVertex(gid, storage::View::NEW); ASSERT_TRUE(vertex); - auto res = vertex->Delete(); + auto res = acc.DeleteVertex(&*vertex); ASSERT_FALSE(res.IsError()); acc.Commit(); @@ -224,7 +224,7 @@ TEST(StorageV2, VertexDeleteCommit) { auto vertex = acc4.FindVertex(gid, storage::View::NEW); ASSERT_TRUE(vertex); - auto res = vertex->Delete(); + auto res = acc4.DeleteVertex(&*vertex); ASSERT_TRUE(res.IsReturn()); acc4.Commit(); @@ -279,7 +279,7 @@ TEST(StorageV2, VertexDeleteAbort) { auto vertex = acc4.FindVertex(gid, storage::View::NEW); ASSERT_TRUE(vertex); - auto res = vertex->Delete(); + auto res = acc4.DeleteVertex(&*vertex); ASSERT_TRUE(res.IsReturn()); acc4.Abort(); @@ -305,7 +305,7 @@ TEST(StorageV2, VertexDeleteAbort) { auto vertex = acc6.FindVertex(gid, storage::View::NEW); ASSERT_TRUE(vertex); - auto res = vertex->Delete(); + auto res = acc6.DeleteVertex(&*vertex); ASSERT_TRUE(res.IsReturn()); acc6.Commit(); @@ -359,13 +359,13 @@ TEST(StorageV2, VertexDeleteSerializationError) { ASSERT_TRUE(vertex); { - auto res = vertex->Delete(); + auto res = acc1.DeleteVertex(&*vertex); ASSERT_TRUE(res.IsReturn()); ASSERT_TRUE(res.GetReturn()); } { - auto res = vertex->Delete(); + auto res = acc1.DeleteVertex(&*vertex); ASSERT_TRUE(res.IsReturn()); ASSERT_FALSE(res.GetReturn()); } @@ -375,7 +375,7 @@ TEST(StorageV2, VertexDeleteSerializationError) { { auto vertex = acc2.FindVertex(gid, storage::View::OLD); ASSERT_TRUE(vertex); - auto res = vertex->Delete(); + auto res = acc2.DeleteVertex(&*vertex); ASSERT_TRUE(res.IsError()); ASSERT_EQ(res.GetError(), storage::Error::SERIALIZATION_ERROR); } @@ -409,7 +409,7 @@ TEST(StorageV2, VertexDeleteSpecialCases) { gid1 = vertex.Gid(); ASSERT_FALSE(acc.FindVertex(gid1, storage::View::OLD).has_value()); ASSERT_TRUE(acc.FindVertex(gid1, storage::View::NEW).has_value()); - auto res = vertex.Delete(); + auto res = acc.DeleteVertex(&vertex); ASSERT_TRUE(res.IsReturn()); ASSERT_TRUE(res.GetReturn()); acc.Abort(); @@ -422,7 +422,7 @@ TEST(StorageV2, VertexDeleteSpecialCases) { gid2 = vertex.Gid(); ASSERT_FALSE(acc.FindVertex(gid2, storage::View::OLD).has_value()); ASSERT_TRUE(acc.FindVertex(gid2, storage::View::NEW).has_value()); - auto res = vertex.Delete(); + auto res = acc.DeleteVertex(&vertex); ASSERT_TRUE(res.IsReturn()); ASSERT_TRUE(res.GetReturn()); acc.Commit(); @@ -481,7 +481,7 @@ TEST(StorageV2, VertexDeleteLabel) { } // Delete the vertex - ASSERT_TRUE(vertex->Delete().GetReturn()); + ASSERT_TRUE(acc.DeleteVertex(&*vertex).GetReturn()); // Check whether label 5 exists ASSERT_FALSE(vertex->HasLabel(5, storage::View::OLD).GetReturn()); @@ -551,7 +551,7 @@ TEST(StorageV2, VertexDeleteLabel) { } // Delete the vertex - ASSERT_TRUE(vertex->Delete().GetReturn()); + ASSERT_TRUE(acc.DeleteVertex(&*vertex).GetReturn()); // Check whether label 5 exists ASSERT_TRUE(vertex->HasLabel(5, storage::View::OLD).GetReturn()); @@ -644,7 +644,7 @@ TEST(StorageV2, VertexDeleteProperty) { } // Delete the vertex - ASSERT_TRUE(vertex->Delete().GetReturn()); + ASSERT_TRUE(acc.DeleteVertex(&*vertex).GetReturn()); // Check whether label 5 exists ASSERT_TRUE( @@ -719,7 +719,7 @@ TEST(StorageV2, VertexDeleteProperty) { } // Delete the vertex - ASSERT_TRUE(vertex->Delete().GetReturn()); + ASSERT_TRUE(acc.DeleteVertex(&*vertex).GetReturn()); // Check whether property 5 exists ASSERT_EQ(