AST - pattern atom properties changed from map to unordered_map

Reviewers: mislav.bradac, buda, teon.banek

Reviewed By: mislav.bradac

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D844
This commit is contained in:
florijan
2017-10-02 10:34:38 +02:00
parent 30573d4fd5
commit 5d2ee8f031
4 changed files with 49 additions and 25 deletions

View File

@@ -12,6 +12,21 @@
#include "query/typed_value.hpp"
#include "utils/assert.hpp"
// Hash function for the key in pattern atom property maps.
namespace std {
template <>
struct hash<std::pair<std::string, GraphDbTypes::Property>> {
size_t operator()(
const std::pair<std::string, GraphDbTypes::Property> &pair) const {
return string_hash(pair.first) ^ property_hash(pair.second);
};
private:
std::hash<std::string> string_hash{};
std::hash<GraphDbTypes::Property> property_hash{};
};
}
namespace query {
#define CLONE_BINARY_EXPRESSION \
@@ -644,14 +659,16 @@ class MapLiteral : public BaseLiteral {
}
// maps (property_name, property) to expressions
std::map<std::pair<std::string, GraphDbTypes::Property>, Expression *>
std::unordered_map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>
elements_;
protected:
MapLiteral(int uid) : BaseLiteral(uid) {}
MapLiteral(int uid,
const std::map<std::pair<std::string, GraphDbTypes::Property>,
Expression *> &elements)
MapLiteral(
int uid,
const std::unordered_map<std::pair<std::string, GraphDbTypes::Property>,
Expression *> &elements)
: BaseLiteral(uid), elements_(elements) {}
};
@@ -954,8 +971,8 @@ class NodeAtom : public PatternAtom {
std::vector<GraphDbTypes::Label> labels_;
// maps (property_name, property) to an expression
// TODO: change to unordered_map
std::map<std::pair<std::string, GraphDbTypes::Property>, Expression *>
std::unordered_map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>
properties_;
protected:
@@ -1005,8 +1022,8 @@ class EdgeAtom : public PatternAtom {
Direction direction_ = Direction::BOTH;
std::vector<GraphDbTypes::EdgeType> edge_types_;
// maps (property_name, property) to an expression
// TODO: change to unordered_map
std::map<std::pair<std::string, GraphDbTypes::Property>, Expression *>
std::unordered_map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>
properties_;
bool has_range_ = false;
Expression *lower_bound_ = nullptr;
@@ -1584,4 +1601,4 @@ class CreateIndex : public Clause {
#undef CLONE_BINARY_EXPRESSION
#undef CLONE_UNARY_EXPRESSION
}
} // namespace query

View File

@@ -5,9 +5,9 @@
#include <codecvt>
#include <cstring>
#include <limits>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
@@ -273,11 +273,11 @@ antlrcpp::Any CypherMainVisitor::visitNodePattern(
ctx->nodeLabels()->accept(this).as<std::vector<GraphDbTypes::Label>>();
}
if (ctx->properties()) {
node->properties_ =
ctx->properties()
->accept(this)
.as<std::map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>>();
node->properties_ = ctx->properties()
->accept(this)
.as<std::unordered_map<
std::pair<std::string, GraphDbTypes::Property>,
Expression *>>();
}
return node;
}
@@ -306,7 +306,9 @@ antlrcpp::Any CypherMainVisitor::visitProperties(
antlrcpp::Any CypherMainVisitor::visitMapLiteral(
CypherParser::MapLiteralContext *ctx) {
std::map<std::pair<std::string, GraphDbTypes::Property>, Expression *> map;
std::unordered_map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>
map;
for (int i = 0; i < static_cast<int>(ctx->propertyKeyName().size()); ++i) {
std::pair<std::string, GraphDbTypes::Property> key =
ctx->propertyKeyName()[i]->accept(this);
@@ -509,8 +511,9 @@ antlrcpp::Any CypherMainVisitor::visitRelationshipPattern(
edge->properties_ =
properties[0]
->accept(this)
.as<std::map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>>();
.as<std::unordered_map<
std::pair<std::string, GraphDbTypes::Property>,
Expression *>>();
break;
}
default:
@@ -887,8 +890,9 @@ antlrcpp::Any CypherMainVisitor::visitLiteral(
return static_cast<Expression *>(storage_.Create<MapLiteral>(
ctx->mapLiteral()
->accept(this)
.as<std::map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>>()));
.as<std::unordered_map<
std::pair<std::string, GraphDbTypes::Property>,
Expression *>>()));
}
return visitChildren(ctx);
}

View File

@@ -26,6 +26,7 @@
#include <cstdlib>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
@@ -477,10 +478,10 @@ auto GetMerge(AstTreeStorage &storage, Pattern *pattern, OnMatch on_match,
#define LIST(...) \
storage.Create<query::ListLiteral>( \
std::vector<query::Expression *>{__VA_ARGS__})
#define MAP(...) \
storage.Create<query::MapLiteral>( \
std::map<std::pair<std::string, GraphDbTypes::Property>, \
query::Expression *>{__VA_ARGS__})
#define MAP(...) \
storage.Create<query::MapLiteral>( \
std::unordered_map<std::pair<std::string, GraphDbTypes::Property>, \
query::Expression *>{__VA_ARGS__})
#define PROPERTY_PAIR(property_name) \
std::make_pair(property_name, dba->Property(property_name))
#define PROPERTY_LOOKUP(...) \

View File

@@ -1,6 +1,7 @@
#include <cmath>
#include <iterator>
#include <memory>
#include <unordered_map>
#include <vector>
#include "gmock/gmock.h"
@@ -374,7 +375,8 @@ TEST(ExpressionEvaluator, MapIndexing) {
Dbms dbms;
auto dba = dbms.active();
auto *map_literal = storage.Create<MapLiteral>(
std::map<std::pair<std::string, GraphDbTypes::Property>, Expression *>{
std::unordered_map<std::pair<std::string, GraphDbTypes::Property>,
Expression *>{
{PROPERTY_PAIR("a"), storage.Create<PrimitiveLiteral>(1)},
{PROPERTY_PAIR("b"), storage.Create<PrimitiveLiteral>(2)},
{PROPERTY_PAIR("c"), storage.Create<PrimitiveLiteral>(3)}});