Summary: This is just the first diff that tries to wire the raft protocol into memgraph. In this diff I'm introducing transaction engine reset functionality. I also introduced `RaftInterface` which should be used wherever someone wants to access Raft from Memgraph. For design decisions see the feature spec. Reviewers: ipaljak, teon.banek Reviewed By: ipaljak Subscribers: pullbot, teon.banek Differential Revision: https://phabricator.memgraph.io/D1758
86 lines
2.7 KiB
C++
86 lines
2.7 KiB
C++
#include "storage/single_node_ha/vertex_accessor.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "database/single_node_ha/graph_db_accessor.hpp"
|
|
#include "durability/single_node_ha/state_delta.hpp"
|
|
#include "utils/algorithm.hpp"
|
|
|
|
VertexAccessor::VertexAccessor(mvcc::VersionList<Vertex> *address,
|
|
database::GraphDbAccessor &db_accessor)
|
|
: RecordAccessor(address, db_accessor) {
|
|
Reconstruct();
|
|
}
|
|
|
|
size_t VertexAccessor::out_degree() const { return current().out_.size(); }
|
|
|
|
size_t VertexAccessor::in_degree() const { return current().in_.size(); }
|
|
|
|
void VertexAccessor::add_label(storage::Label label) {
|
|
auto &dba = db_accessor();
|
|
auto delta = database::StateDelta::AddLabel(dba.transaction_id(), gid(),
|
|
label, dba.LabelName(label));
|
|
Vertex &vertex = update();
|
|
// not a duplicate label, add it
|
|
if (!utils::Contains(vertex.labels_, label)) {
|
|
vertex.labels_.emplace_back(label);
|
|
dba.raft()->Emplace(delta);
|
|
dba.UpdateLabelIndices(label, *this, &vertex);
|
|
}
|
|
}
|
|
|
|
void VertexAccessor::remove_label(storage::Label label) {
|
|
auto &dba = db_accessor();
|
|
auto delta = database::StateDelta::RemoveLabel(dba.transaction_id(), gid(),
|
|
label, dba.LabelName(label));
|
|
Vertex &vertex = update();
|
|
if (utils::Contains(vertex.labels_, label)) {
|
|
auto &labels = vertex.labels_;
|
|
auto found = std::find(labels.begin(), labels.end(), delta.label);
|
|
std::swap(*found, labels.back());
|
|
labels.pop_back();
|
|
dba.raft()->Emplace(delta);
|
|
}
|
|
}
|
|
|
|
bool VertexAccessor::has_label(storage::Label label) const {
|
|
auto &labels = this->current().labels_;
|
|
return std::find(labels.begin(), labels.end(), label) != labels.end();
|
|
}
|
|
|
|
const std::vector<storage::Label> &VertexAccessor::labels() const {
|
|
return this->current().labels_;
|
|
}
|
|
|
|
void VertexAccessor::RemoveOutEdge(mvcc::VersionList<Edge> *edge) {
|
|
auto &dba = db_accessor();
|
|
|
|
SwitchNew();
|
|
if (current().is_expired_by(dba.transaction())) return;
|
|
|
|
update().out_.RemoveEdge(edge);
|
|
}
|
|
|
|
void VertexAccessor::RemoveInEdge(mvcc::VersionList<Edge> *edge) {
|
|
auto &dba = db_accessor();
|
|
|
|
SwitchNew();
|
|
if (current().is_expired_by(dba.transaction())) return;
|
|
|
|
update().in_.RemoveEdge(edge);
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &os, const VertexAccessor &va) {
|
|
os << "V(";
|
|
utils::PrintIterable(os, va.labels(), ":", [&](auto &stream, auto label) {
|
|
stream << va.db_accessor().LabelName(label);
|
|
});
|
|
os << " {";
|
|
utils::PrintIterable(os, va.Properties(), ", ",
|
|
[&](auto &stream, const auto &pair) {
|
|
stream << va.db_accessor().PropertyName(pair.first)
|
|
<< ": " << pair.second;
|
|
});
|
|
return os << "})";
|
|
}
|