ExistenceConstraints performance improvement

Summary:
Case with existence constraints is about 8-9% slower then case without
existence constraints. Before this diff that difference was about 15-16%.

Reviewers: msantl, ipaljak

Reviewed By: ipaljak

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1917
This commit is contained in:
Vinko Kasljevic 2019-03-12 09:32:58 +01:00
parent 8a5842ef63
commit 94afbd9f56
5 changed files with 39 additions and 18 deletions

View File

@ -194,7 +194,7 @@ void GraphDbAccessor::UpdateLabelIndices(storage::Label label,
"Node couldn't be updated due to index constraint violation!");
}
if (!db_.storage().existence_constraints_.CheckIfSatisfies(vertex)) {
if (!db_.storage().existence_constraints_.CheckOnAddLabel(vertex, label)) {
throw IndexConstraintViolationException(
"Node couldn't be updated due to existence constraint violation!");
}
@ -216,7 +216,8 @@ void GraphDbAccessor::UpdatePropertyIndex(
void GraphDbAccessor::UpdateOnPropertyRemove(storage::Property property,
const Vertex *vertex,
const RecordAccessor<Vertex> &accessor) {
if (!db_.storage().existence_constraints_.CheckIfSatisfies(vertex)) {
if (!db_.storage().existence_constraints_.CheckOnRemoveProperty(vertex,
property)) {
throw IndexConstraintViolationException(
"Node couldn't be updated due to existence constraint violation!");
}
@ -270,7 +271,7 @@ bool GraphDbAccessor::ExistenceConstraintExists(
return db_.storage().existence_constraints_.Exists(rule);
}
std::list<ExistenceRule> GraphDbAccessor::ExistenceConstraintsList()
std::vector<ExistenceRule> GraphDbAccessor::ExistenceConstraintsList()
const {
return db_.storage().existence_constraints_.ListConstraints();
}

View File

@ -487,7 +487,7 @@ class GraphDbAccessor {
/**
* Returns the list of existence constraints currently active.
*/
std::list<ExistenceRule> ExistenceConstraintsList() const;
std::vector<ExistenceRule> ExistenceConstraintsList() const;
/**
* Return approximate number of all vertices in the database.

View File

@ -30,7 +30,8 @@ void ExistenceConstraints::AddConstraint(const ExistenceRule &rule) {
void ExistenceConstraints::RemoveConstraint(const ExistenceRule &rule) {
auto found = std::find(constraints_.begin(), constraints_.end(), rule);
if (found != constraints_.end()) {
constraints_.erase(found);
std::swap(*found, constraints_.back());
constraints_.pop_back();
}
}
@ -39,15 +40,29 @@ bool ExistenceConstraints::Exists(const ExistenceRule &rule) const {
return found != constraints_.end();
}
bool ExistenceConstraints::CheckIfSatisfies(const Vertex *vertex) const {
bool ExistenceConstraints::CheckOnAddLabel(const Vertex *vertex,
storage::Label label) const {
for (auto &constraint : constraints_) {
if (!CheckIfSatisfiesExistenceRule(vertex, constraint)) return false;
if (constraint.label == label &&
!CheckIfSatisfiesExistenceRule(vertex, constraint)) {
return false;
}
}
return true;
}
const std::list<ExistenceRule> &ExistenceConstraints::ListConstraints() const {
bool ExistenceConstraints::CheckOnRemoveProperty(
const Vertex *vertex, storage::Property property) const {
for (auto &constraint : constraints_) {
if (utils::Contains(constraint.properties, property) &&
!CheckIfSatisfiesExistenceRule(vertex, constraint)) {
return false;
}
}
return true;
}
const std::vector<ExistenceRule> &ExistenceConstraints::ListConstraints() const {
return constraints_;
}
} // namespace database

View File

@ -65,14 +65,19 @@ class ExistenceConstraints {
/// Checks whether given constraint is visible.
bool Exists(const ExistenceRule &rule) const;
/// Check if given vertex satisfies all visible constraints.
// TODO I could check this faster if I knew exactly what changed
bool CheckIfSatisfies(const Vertex *vertex) const;
/// Check if add label update satisfies all visible constraints.
/// @return true if all constraints are satisfied, false otherwise
bool CheckOnAddLabel(const Vertex *vertex, storage::Label label) const;
/// Check if remove property update satisfies all visible constraints.
/// @return true if all constraints are satisfied, false otherwise
bool CheckOnRemoveProperty(const Vertex *vertex,
storage::Property property) const;
/// Returns list of all constraints.
const std::list<ExistenceRule> &ListConstraints() const;
const std::vector<ExistenceRule> &ListConstraints() const;
private:
std::list<ExistenceRule> constraints_;
std::vector<ExistenceRule> constraints_;
};
}; // namespace database

View File

@ -45,7 +45,7 @@ TEST_F(ExistenceConstraintsTest, InsertTest) {
auto dba = db_.Access();
auto v = dba->InsertVertex();
v.add_label(label);
EXPECT_TRUE(constraints_.CheckIfSatisfies(v.GetNew()));
EXPECT_TRUE(constraints_.CheckOnAddLabel(v.GetNew(), label));
dba->Commit();
}
{
@ -70,11 +70,11 @@ TEST_F(ExistenceConstraintsTest, InsertTest) {
auto v1 = dba->InsertVertex();
v1.add_label(label);
EXPECT_FALSE(
constraints_.CheckIfSatisfies(v1.GetNew()));
constraints_.CheckOnAddLabel(v1.GetNew(), label));
auto v2 = dba->InsertVertex();
v2.PropsSet(prop, PropertyValue(false));
v2.add_label(label);
EXPECT_TRUE(constraints_.CheckIfSatisfies(v2.GetNew()));
EXPECT_TRUE(constraints_.CheckOnAddLabel(v2.GetNew(), label));
dba->Commit();
}
{
@ -86,7 +86,7 @@ TEST_F(ExistenceConstraintsTest, InsertTest) {
auto dba = db_.Access();
auto v = dba->InsertVertex();
v.add_label(label);
EXPECT_TRUE(constraints_.CheckIfSatisfies(v.GetNew()));
EXPECT_TRUE(constraints_.CheckOnAddLabel(v.GetNew(), label));
dba->Commit();
}
}