Clean-up utils/hashing

Reviewers: teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2645
This commit is contained in:
Matej Ferencevic 2020-01-27 17:03:17 +01:00
parent de48548164
commit 015573b5af
12 changed files with 28 additions and 114 deletions

View File

@ -14,7 +14,7 @@
#include "query/frontend/opencypher/generated/MemgraphCypherLexer.h"
#include "query/frontend/parsing.hpp"
#include "query/frontend/stripped_lexer_constants.hpp"
#include "utils/hashing/fnv.hpp"
#include "utils/fnv.hpp"
#include "utils/string.hpp"
namespace query::frontend {
@ -130,7 +130,7 @@ StrippedQuery::StrippedQuery(const std::string &query) : original_(query) {
}
query_ = utils::Join(token_strings, " ");
hash_ = fnv(query_);
hash_ = utils::Fnv(query_);
auto it = tokens.begin();
while (it != tokens.end()) {

View File

@ -4,7 +4,7 @@
#include <unordered_map>
#include "query/parameters.hpp"
#include "utils/hashing/fnv.hpp"
#include "utils/fnv.hpp"
namespace query::frontend {
@ -50,7 +50,7 @@ class StrippedQuery {
const auto &literals() const { return literals_; }
const auto &named_expressions() const { return named_exprs_; }
const auto &parameters() const { return parameters_; }
HashType hash() const { return hash_; }
uint64_t hash() const { return hash_; }
private:
// Return len of matched keyword if something is matched, otherwise 0.
@ -86,7 +86,7 @@ class StrippedQuery {
std::unordered_map<int, std::string> named_exprs_;
// Hash based on the stripped query.
HashType hash_;
uint64_t hash_;
};
} // namespace query::frontend

View File

@ -422,7 +422,7 @@ std::unique_ptr<LogicalPlan> MakeLogicalPlan(AstStorage ast_storage,
* cache a fresh one if it doesn't yet exist.
*/
std::shared_ptr<CachedPlan> CypherQueryToPlan(
HashType hash, AstStorage ast_storage, CypherQuery *query,
uint64_t hash, AstStorage ast_storage, CypherQuery *query,
const Parameters &parameters, utils::SkipList<PlanCacheEntry> *plan_cache,
DbAccessor *db_accessor) {
auto plan_cache_access = plan_cache->access();

View File

@ -151,10 +151,10 @@ struct QueryCacheEntry {
bool operator<(const QueryCacheEntry &other) const {
return first < other.first;
}
bool operator==(const HashType &other) const { return first == other; }
bool operator<(const HashType &other) const { return first < other; }
bool operator==(const uint64_t &other) const { return first == other; }
bool operator<(const uint64_t &other) const { return first < other; }
HashType first;
uint64_t first;
// TODO: Maybe store the query string here and use it as a key with the hash
// so that we eliminate the risk of hash collisions.
CachedQuery second;
@ -167,10 +167,10 @@ struct PlanCacheEntry {
bool operator<(const PlanCacheEntry &other) const {
return first < other.first;
}
bool operator==(const HashType &other) const { return first == other; }
bool operator<(const HashType &other) const { return first < other; }
bool operator==(const uint64_t &other) const { return first == other; }
bool operator<(const uint64_t &other) const { return first < other; }
HashType first;
uint64_t first;
// TODO: Maybe store the query string here and use it as a key with the hash
// so that we eliminate the risk of hash collisions.
std::shared_ptr<CachedPlan> second;

View File

@ -27,7 +27,7 @@
#include "query/procedure/module.hpp"
#include "utils/algorithm.hpp"
#include "utils/exceptions.hpp"
#include "utils/hashing/fnv.hpp"
#include "utils/fnv.hpp"
#include "utils/pmr/unordered_map.hpp"
#include "utils/pmr/unordered_set.hpp"
#include "utils/pmr/vector.hpp"

View File

@ -16,7 +16,7 @@
#include "query/typed_value.hpp"
#include "storage/v2/id_types.hpp"
#include "utils/bound.hpp"
#include "utils/hashing/fnv.hpp"
#include "utils/fnv.hpp"
#include "utils/memory.hpp"
#include "utils/visitor.hpp"
cpp<#

View File

@ -7,7 +7,7 @@
#include "storage/v2/id_types.hpp"
#include "storage/v2/property_value.hpp"
#include "utils/bound.hpp"
#include "utils/hashing/fnv.hpp"
#include "utils/fnv.hpp"
namespace query::plan {

View File

@ -10,7 +10,7 @@
#include "glog/logging.h"
#include "utils/exceptions.hpp"
#include "utils/hashing/fnv.hpp"
#include "utils/fnv.hpp"
namespace query {

View File

@ -1,39 +1,21 @@
#pragma once
#include <cstdlib>
#include <string>
#include "utils/platform.hpp"
#include "fnv32.hpp"
#include "fnv64.hpp"
// fnv1a is recommended so use it as a default implementation. also use the
// platform specific version of the function
namespace {
#ifdef MEMGRAPH64
__attribute__((unused)) uint64_t fnv(const std::string &s) {
return fnv1a64(s);
}
using HashType = uint64_t;
#else
__attribute__((unused)) uint32_t fnv(const std::string &s) {
return fnv1a32(s);
}
using HashType = uint32_t;
#endif
}
#include <string_view>
namespace utils {
inline uint64_t Fnv(const std::string_view &s) {
// fnv1a is recommended so use it as the default implementation.
uint64_t hash = 14695981039346656037UL;
for (const auto &ch : s) {
hash = (hash ^ (uint64_t)ch) * 1099511628211UL;
}
return hash;
}
/**
* Does FNV-like hashing on a collection. Not truly FNV
* because it operates on 8-bit elements, while this

View File

@ -1,28 +0,0 @@
#pragma once
#include <cstdlib>
#include <string>
namespace {
#define OFFSET_BASIS32 2166136261u
#define FNV_PRIME32 16777619u
__attribute__((unused)) uint32_t fnv32(const std::string &s) {
uint32_t hash = OFFSET_BASIS32;
for (size_t i = 0; i < s.size(); ++i)
hash = (hash * FNV_PRIME32) xor (uint32_t) s[i];
return hash;
}
__attribute__((unused)) uint32_t fnv1a32(const std::string &s) {
uint32_t hash = OFFSET_BASIS32;
for (size_t i = 0; i < s.size(); ++i)
hash = (hash xor (uint32_t) s[i]) * FNV_PRIME32;
return hash;
}
}

View File

@ -1,28 +0,0 @@
#pragma once
#include <cstdlib>
#include <string>
namespace {
#define OFFSET_BASIS64 14695981039346656037u
#define FNV_PRIME64 1099511628211u
__attribute__((unused)) uint64_t fnv64(const std::string &s) {
uint64_t hash = OFFSET_BASIS64;
for (size_t i = 0; i < s.size(); ++i)
hash = (hash * FNV_PRIME64) xor (uint64_t) s[i];
return hash;
}
__attribute__((unused)) uint64_t fnv1a64(const std::string &s) {
uint64_t hash = OFFSET_BASIS64;
for (size_t i = 0; i < s.size(); ++i)
hash = (hash xor (uint64_t) s[i]) * FNV_PRIME64;
return hash;
}
}

View File

@ -1,12 +0,0 @@
#pragma once
#include <cstdint>
// is there a better way?
#if UINTPTR_MAX == 0xffffffff
#define MEMGRAPH32
#elif UINTPTR_MAX == 0xffffffffffffffff
#define MEMGRAPH64
#else
#error Unrecognized platform (neither 32 or 64)
#endif