storage/v2: Add (In|Out)Degree to VertexAccessor

Reviewers: mtomic, mferencevic

Reviewed By: mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2309
This commit is contained in:
Teon Banek 2019-07-23 13:03:45 +02:00
parent 1ab0b8e0ff
commit 4f93632fb8
4 changed files with 505 additions and 0 deletions

View File

@ -449,4 +449,78 @@ Result<std::vector<EdgeAccessor>> VertexAccessor::OutEdges(
return std::move(ret);
}
Result<size_t> VertexAccessor::InDegree(View view) const {
size_t degree = 0;
bool deleted = false;
Delta *delta = nullptr;
{
std::lock_guard<utils::SpinLock> guard(vertex_->lock);
deleted = vertex_->deleted;
degree = vertex_->in_edges.size();
delta = vertex_->delta;
}
ApplyDeltasForRead(transaction_, delta, view,
[&deleted, &degree](const Delta &delta) {
switch (delta.action) {
case Delta::Action::ADD_IN_EDGE:
++degree;
break;
case Delta::Action::REMOVE_IN_EDGE:
--degree;
break;
case Delta::Action::DELETE_OBJECT:
LOG(FATAL) << "Invalid accessor!";
break;
case Delta::Action::RECREATE_OBJECT:
deleted = false;
break;
case Delta::Action::ADD_LABEL:
case Delta::Action::REMOVE_LABEL:
case Delta::Action::SET_PROPERTY:
case Delta::Action::ADD_OUT_EDGE:
case Delta::Action::REMOVE_OUT_EDGE:
break;
}
});
if (deleted) return Error::DELETED_OBJECT;
return degree;
}
Result<size_t> VertexAccessor::OutDegree(View view) const {
size_t degree = 0;
bool deleted = false;
Delta *delta = nullptr;
{
std::lock_guard<utils::SpinLock> guard(vertex_->lock);
deleted = vertex_->deleted;
degree = vertex_->out_edges.size();
delta = vertex_->delta;
}
ApplyDeltasForRead(transaction_, delta, view,
[&deleted, &degree](const Delta &delta) {
switch (delta.action) {
case Delta::Action::ADD_OUT_EDGE:
++degree;
break;
case Delta::Action::REMOVE_OUT_EDGE:
--degree;
break;
case Delta::Action::DELETE_OBJECT:
LOG(FATAL) << "Invalid accessor!";
break;
case Delta::Action::RECREATE_OBJECT:
deleted = false;
break;
case Delta::Action::ADD_LABEL:
case Delta::Action::REMOVE_LABEL:
case Delta::Action::SET_PROPERTY:
case Delta::Action::ADD_IN_EDGE:
case Delta::Action::REMOVE_IN_EDGE:
break;
}
});
if (deleted) return Error::DELETED_OBJECT;
return degree;
}
} // namespace storage

View File

@ -49,6 +49,10 @@ class VertexAccessor final {
Result<std::vector<EdgeAccessor>> OutEdges(
const std::vector<EdgeTypeId> &edge_types, View view) const;
Result<size_t> InDegree(View view) const;
Result<size_t> OutDegree(View view) const;
Gid Gid() const { return vertex_->gid; }
bool operator==(const VertexAccessor &other) const {

File diff suppressed because it is too large Load Diff

View File

@ -132,6 +132,7 @@ TEST(StorageV2Gc, Sanity) {
auto out_edges = vertex->OutEdges({}, storage::View::NEW);
if (i % 5 != 4 && i % 3 != 2) {
EXPECT_EQ(out_edges.GetValue().size(), 1);
EXPECT_EQ(*vertex->OutDegree(storage::View::NEW), 1);
EXPECT_EQ(out_edges.GetValue().at(0).EdgeType().AsUint(), i);
} else {
EXPECT_TRUE(out_edges->empty());
@ -140,6 +141,7 @@ TEST(StorageV2Gc, Sanity) {
auto in_edges = vertex->InEdges({}, storage::View::NEW);
if (i % 5 != 1 && i % 3 != 1) {
EXPECT_EQ(in_edges.GetValue().size(), 1);
EXPECT_EQ(*vertex->InDegree(storage::View::NEW), 1);
EXPECT_EQ(in_edges.GetValue().at(0).EdgeType().AsUint(),
(i + 999) % 1000);
} else {