int -> int64_t in in Typed and PropertyValue

Reviewers: buda, mferencevic

Reviewed By: buda, mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D106
This commit is contained in:
Mislav Bradac 2017-03-09 11:44:02 +01:00
parent 09dbe2e722
commit 725ebd1a0e
12 changed files with 91 additions and 86 deletions

View File

@ -119,7 +119,7 @@ class BoltSerializer {
encoder.write_string(value.Value<std::string>());
return;
case PropertyValue::Type::Int:
encoder.write_integer(value.Value<int>());
encoder.write_integer(value.Value<int64_t>());
return;
case PropertyValue::Type::Double:
encoder.write_double(value.Value<double>());

View File

@ -14,18 +14,18 @@ namespace bolt {
*
* @tparam TChunkedEncoder Type of chunked encoder used.
*/
// TODO templatisation on TChunkedEncoder might not be desired
// TODO templatisation on TChunkedEncoder might not be desired
// but makes the code a bit easer to understand because we know
// that this class uses a BoltEncoder (and not some arbitrary template)
// it helps the programmer, the compiler and the IDE
template <typename TChunkedEncoder>
class RecordStream {
public:
// TODO add logging to this class
// TODO add logging to this class
RecordStream(BoltEncoder<TChunkedEncoder> &bolt_encoder)
: bolt_encoder_(bolt_encoder), serializer_(bolt_encoder) {}
RecordStream(BoltEncoder<TChunkedEncoder> &bolt_encoder) :
bolt_encoder_(bolt_encoder), serializer_(bolt_encoder) {}
void Header(const std::vector<std::string> &fields) {
bolt_encoder_.message_success();
bolt_encoder_.write_map_header(1);
@ -65,7 +65,7 @@ class RecordStream {
Chunk();
}
private:
private:
BoltEncoder<TChunkedEncoder> bolt_encoder_;
BoltSerializer<BoltEncoder<TChunkedEncoder>> serializer_;
@ -82,7 +82,7 @@ private:
bolt_encoder_.write(value.Value<bool>());
break;
case TypedValue::Type::Int:
bolt_encoder_.write(value.Value<int>());
bolt_encoder_.write(value.Value<int64_t>());
break;
case TypedValue::Type::Double:
bolt_encoder_.write(value.Value<double>());
@ -103,14 +103,14 @@ private:
serializer_.write(value.Value<EdgeAccessor>());
break;
default:
throw std::runtime_error("Serialization not implemented for given type");
throw std::runtime_error(
"Serialization not implemented for given type");
}
}
void Write(const std::vector<TypedValue> &values) {
bolt_encoder_.write_list_header(values.size());
for (const auto &value : values)
Write(value);
for (const auto &value : values) Write(value);
}
void Write(const std::map<std::string, TypedValue> &values) {

View File

@ -18,7 +18,7 @@ TypedValue::TypedValue(const PropertyValue& value) {
return;
case PropertyValue::Type::Int:
type_ = Type::Int;
int_v = value.Value<int>();
int_v = value.Value<int64_t>();
return;
case PropertyValue::Type::Double:
type_ = Type::Double;
@ -70,7 +70,7 @@ bool TypedValue::Value<bool>() const {
}
template <>
int TypedValue::Value<int>() const {
int64_t TypedValue::Value<int64_t>() const {
if (type_ != TypedValue::Type::Int) {
throw TypedValueException("Incompatible template param and type");
}
@ -201,7 +201,7 @@ std::ostream& operator<<(std::ostream& os, const TypedValue& value) {
case TypedValue::Type::Bool:
return os << (value.Value<bool>() ? "true" : "false");
case TypedValue::Type::Int:
return os << value.Value<int>();
return os << value.Value<int64_t>();
case TypedValue::Type::Double:
return os << value.Value<double>();
case TypedValue::Type::String:
@ -318,7 +318,7 @@ TypedValue::~TypedValue() {
double ToDouble(const TypedValue& value) {
switch (value.type()) {
case TypedValue::Type::Int:
return (double)value.Value<int>();
return (double)value.Value<int64_t>();
case TypedValue::Type::Double:
return value.Value<double>();
default:
@ -350,7 +350,7 @@ TypedValue operator<(const TypedValue& a, const TypedValue& b) {
b.type() == TypedValue::Type::Double) {
return ToDouble(a) < ToDouble(b);
} else {
return a.Value<int>() < b.Value<int>();
return a.Value<int64_t>() < b.Value<int64_t>();
}
}
@ -413,7 +413,7 @@ TypedValue operator==(const TypedValue& a, const TypedValue& b) {
b.type() == TypedValue::Type::Double) {
return ToDouble(a) == ToDouble(b);
} else {
return a.Value<int>() == b.Value<int>();
return a.Value<int64_t>() == b.Value<int64_t>();
}
}
@ -440,7 +440,7 @@ std::string ValueToString(const TypedValue& value) {
case TypedValue::Type::String:
return value.Value<std::string>();
case TypedValue::Type::Int:
return std::to_string(value.Value<int>());
return std::to_string(value.Value<int64_t>());
case TypedValue::Type::Double:
return fmt::format("{}", value.Value<double>());
// unsupported situations
@ -455,7 +455,7 @@ TypedValue operator-(const TypedValue& a) {
case TypedValue::Type::Null:
return TypedValue::Null;
case TypedValue::Type::Int:
return -a.Value<int>();
return -a.Value<int64_t>();
case TypedValue::Type::Double:
return -a.Value<double>();
default:
@ -521,7 +521,7 @@ TypedValue operator+(const TypedValue& a, const TypedValue& b) {
b.type() == TypedValue::Type::Double) {
return ToDouble(a) + ToDouble(b);
} else {
return a.Value<int>() + b.Value<int>();
return a.Value<int64_t>() + b.Value<int64_t>();
}
}
@ -536,7 +536,7 @@ TypedValue operator-(const TypedValue& a, const TypedValue& b) {
b.type() == TypedValue::Type::Double) {
return ToDouble(a) - ToDouble(b);
} else {
return a.Value<int>() - b.Value<int>();
return a.Value<int64_t>() - b.Value<int64_t>();
}
}
@ -551,7 +551,7 @@ TypedValue operator/(const TypedValue& a, const TypedValue& b) {
b.type() == TypedValue::Type::Double) {
return ToDouble(a) / ToDouble(b);
} else {
return a.Value<int>() / b.Value<int>();
return a.Value<int64_t>() / b.Value<int64_t>();
}
}
@ -566,7 +566,7 @@ TypedValue operator*(const TypedValue& a, const TypedValue& b) {
b.type() == TypedValue::Type::Double) {
return ToDouble(a) * ToDouble(b);
} else {
return a.Value<int>() * b.Value<int>();
return a.Value<int64_t>() * b.Value<int64_t>();
}
}
@ -581,7 +581,7 @@ TypedValue operator%(const TypedValue& a, const TypedValue& b) {
b.type() == TypedValue::Type::Double) {
return (double)fmod(ToDouble(a), ToDouble(b));
} else {
return a.Value<int>() % b.Value<int>();
return a.Value<int64_t>() % b.Value<int64_t>();
}
}

View File

@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <map>
#include <cstdint>
#include "utils/exceptions/stacktrace_exception.hpp"
#include "utils/total_ordering.hpp"
@ -50,6 +51,7 @@ class TypedValue : public TotalOrdering<TypedValue, TypedValue, TypedValue> {
// constructors for primitive types
TypedValue(bool value) : type_(Type::Bool) { bool_v = value; }
TypedValue(int value) : type_(Type::Int) { int_v = value; }
TypedValue(int64_t value) : type_(Type::Int) { int_v = value; }
TypedValue(double value) : type_(Type::Double) { double_v = value; }
// conversion function to PropertyValue
@ -102,7 +104,7 @@ class TypedValue : public TotalOrdering<TypedValue, TypedValue, TypedValue> {
// storage for the value of the property
union {
bool bool_v;
int int_v;
int64_t int_v;
double double_v;
// Since this is used in query runtime, size of union is not critical so
// string and vector are used instead of pointers. It requires copy of data,

View File

@ -3,5 +3,5 @@
#include "query/stripped.hpp"
namespace query {
StrippedQuery Strip(const std::string &query);
StrippedQuery Strip(const std::string &query);
};

View File

@ -25,7 +25,7 @@ std::string PropertyValue::Value<std::string>() const {
}
template <>
int PropertyValue::Value<int>() const {
int64_t PropertyValue::Value<int64_t>() const {
if (type_ != PropertyValue::Type::Int) {
throw PropertyValueException("Incompatible template param and type");
}
@ -105,7 +105,7 @@ std::ostream& operator<<(std::ostream& os, const PropertyValue& value) {
case PropertyValue::Type::String:
return os << value.Value<std::string>();
case PropertyValue::Type::Int:
return os << value.Value<int>();
return os << value.Value<int64_t>();
case PropertyValue::Type::Double:
return os << value.Value<double>();
case PropertyValue::Type::List:

View File

@ -32,6 +32,7 @@ class PropertyValue {
// constructors for primitive types
PropertyValue(bool value) : type_(Type::Bool) { bool_v = value; }
PropertyValue(int value) : type_(Type::Int) { int_v = value; }
PropertyValue(int64_t value) : type_(Type::Int) { int_v = value; }
PropertyValue(double value) : type_(Type::Double) { double_v = value; }
/// constructors for non-primitive types (shared pointers)
@ -71,7 +72,7 @@ class PropertyValue {
// storage for the value of the property
union {
bool bool_v;
int int_v;
int64_t int_v;
double double_v;
std::shared_ptr<std::string> string_v;
// We support lists of values of different types, neo4j supports lists of

View File

@ -16,19 +16,19 @@
* @param ostream The stream to write to.
*/
void PropertyValuesToJson(const PropertyValueStore& store,
std::ostream& ostream = std::cout) {
std::ostream& ostream = std::cout) {
bool first = true;
auto write_key = [&ostream,
&first](const PropertyValueStore::TKey& key) -> std::ostream& {
if (first) {
ostream << '{';
first = false;
} else
ostream << ',';
auto write_key =
[&ostream, &first](const PropertyValueStore::TKey& key) -> std::ostream& {
if (first) {
ostream << '{';
first = false;
} else
ostream << ',';
return ostream << '"' << key << "\":";
};
return ostream << '"' << key << "\":";
};
auto handler = [&ostream, &write_key](const PropertyValueStore::TKey& key,
const PropertyValue& value) {
@ -42,7 +42,7 @@ void PropertyValuesToJson(const PropertyValueStore& store,
write_key(key) << '"' << value.Value<std::string>() << '"';
break;
case PropertyValue::Type::Int:
write_key(key) << value.Value<int>();
write_key(key) << value.Value<int64_t>();
break;
case PropertyValue::Type::Float:
write_key(key) << value.Value<float>();

View File

@ -28,9 +28,8 @@ using std::endl;
enum CliqueQuery { SCORE_AND_LIMIT, FIND_ALL };
bool run_general_query(GraphDbAccessor &db_accessor,
const Parameters &args, Stream &stream,
enum CliqueQuery query_type) {
bool run_general_query(GraphDbAccessor &db_accessor, const Parameters &args,
Stream &stream, enum CliqueQuery query_type) {
if (query_type == CliqueQuery::FIND_ALL)
stream.write_fields(
{"a.garment_id", "b.garment_id", "c.garment_id", "d.garment_id"});
@ -72,8 +71,8 @@ bool run_general_query(GraphDbAccessor &db_accessor,
edges_indexed.push_back(&edges[i]);
}
const int n = vertices_indexed.size();
auto cmp_vertex = [](const VertexAccessor *a,
const VertexAccessor *b) -> bool { return *a < *b; };
auto cmp_vertex = [](const VertexAccessor *a, const VertexAccessor *b)
-> bool { return *a < *b; };
auto cmp_edge = [](const EdgeAccessor *a, const EdgeAccessor *b) -> bool {
if (a->from() != b->from()) return a->from() < b->from();
return a->to() < b->to();
@ -83,15 +82,15 @@ bool run_general_query(GraphDbAccessor &db_accessor,
* @param v VertexAccessor to a vertex.
* @return position of vertex or -1 if it doesn't exist.
*/
auto query = [&vertices_indexed,
&cmp_vertex](const VertexAccessor &v) -> int {
int pos = lower_bound(vertices_indexed.begin(), vertices_indexed.end(), &v,
cmp_vertex) -
vertices_indexed.begin();
if (pos == (int)vertices_indexed.size() || *vertices_indexed[pos] != v)
return -1;
return pos;
};
auto query =
[&vertices_indexed, &cmp_vertex](const VertexAccessor &v) -> int {
int pos = lower_bound(vertices_indexed.begin(), vertices_indexed.end(),
&v, cmp_vertex) -
vertices_indexed.begin();
if (pos == (int)vertices_indexed.size() || *vertices_indexed[pos] != v)
return -1;
return pos;
};
/**
* Update bitset of neighbours. Set bit to 1 for index of every vertex
* endpoint of edges with type default_outfit.
@ -148,15 +147,16 @@ bool run_general_query(GraphDbAccessor &db_accessor,
* @return EdgeAccessor* if it exists, nullptr otherwise.
*/
auto get_edge = [&edges_indexed](
const VertexAccessor &first,
const VertexAccessor &second) -> EdgeAccessor * {
auto cmp_edge_to_pair = [](
const EdgeAccessor *edge,
const pair<const VertexAccessor *, const VertexAccessor *> &e) -> bool {
if (edge->from() != *e.first) return edge->from() < *e.first;
if (edge->to() != *e.second) return edge->to() < *e.second;
return false;
};
const VertexAccessor &first,
const VertexAccessor &second) -> EdgeAccessor *{
auto cmp_edge_to_pair =
[](const EdgeAccessor *edge,
const pair<const VertexAccessor *, const VertexAccessor *> &e)
-> bool {
if (edge->from() != *e.first) return edge->from() < *e.first;
if (edge->to() != *e.second) return edge->to() < *e.second;
return false;
};
auto pos = lower_bound(edges_indexed.begin(), edges_indexed.end(),
std::make_pair(&first, &second), cmp_edge_to_pair) -
edges_indexed.begin();
@ -180,18 +180,20 @@ bool run_general_query(GraphDbAccessor &db_accessor,
* @param V index of clique vertices in vertices_indexed.
* @return score if profile_index exists, else 0.
*/
auto calc_score = [&db_accessor, &vertices, &profile_index, &vertices_indexed,
&get_edge](const std::vector<int> &V) -> int {
int res = 0;
if (profile_index == -1) return 0;
for (auto x : V) {
auto edge = get_edge(vertices[profile_index], *vertices_indexed[x]);
if (edge == nullptr) continue;
auto prop = TypedValue(edge->PropsAt(db_accessor.property("score")));
if (prop.type() == TypedValue::Type::Int) res += prop.Value<int>();
}
return res;
};
auto calc_score =
[&db_accessor, &vertices, &profile_index, &vertices_indexed, &get_edge](
const std::vector<int> &V) -> int {
int res = 0;
if (profile_index == -1) return 0;
for (auto x : V) {
auto edge = get_edge(vertices[profile_index], *vertices_indexed[x]);
if (edge == nullptr) continue;
auto prop = TypedValue(edge->PropsAt(db_accessor.property("score")));
if (prop.type() == TypedValue::Type::Int)
res += prop.Value<int64_t>();
}
return res;
};
if (query_type == CliqueQuery::SCORE_AND_LIMIT) {
auto cmp_results = [&calc_score](const std::vector<int> &first,
const std::vector<int> &second) {
@ -201,7 +203,7 @@ bool run_general_query(GraphDbAccessor &db_accessor,
reverse(results.begin(), results.end());
}
const int limit = query_type == CliqueQuery::SCORE_AND_LIMIT
? args.At((int)args.Size() - 1).Value<int>()
? args.at((int)args.size() - 1).Value<int64_t>()
: (int)results.size();
for (int i = 0; i < std::min(limit, (int)results.size()); ++i) {
stream.write_record();
@ -210,7 +212,7 @@ bool run_general_query(GraphDbAccessor &db_accessor,
for (auto x : results[i]) {
stream.write(vertices_indexed[x]
->PropsAt(db_accessor.property("garment_id"))
.Value<int>());
.Value<int64_t>());
}
if (query_type == CliqueQuery::SCORE_AND_LIMIT)
stream.write(calc_score(results[i]));

View File

@ -19,7 +19,7 @@ TEST(PropertyValueStore, At) {
props.set(0, some_string);
EXPECT_EQ(PropertyValue(props.at(0)).Value<string>(), some_string);
props.set(120, 42);
EXPECT_EQ(PropertyValue(props.at(120)).Value<int>(), 42);
EXPECT_EQ(PropertyValue(props.at(120)).Value<int64_t>(), 42);
}
TEST(PropertyValueStore, AtNull) {
@ -56,7 +56,7 @@ TEST(PropertyValueStore, Remove) {
TEST(PropertyValueStore, Replace) {
PropertyValueStore<> props;
props.set(10, 42);
EXPECT_EQ(props.at(10).Value<int>(), 42);
EXPECT_EQ(props.at(10).Value<int64_t>(), 42);
props.set(10, 0.25f);
EXPECT_EQ(props.at(10).type(), PropertyValue::Type::Double);
EXPECT_FLOAT_EQ(props.at(10).Value<double>(), 0.25);
@ -119,7 +119,7 @@ TEST(PropertyValueStore, InsertRetrieveList) {
auto l = p.Value<std::vector<PropertyValue>>();
EXPECT_EQ(l.size(), 5);
EXPECT_EQ(l[0].type(), PropertyValue::Type::Int);
EXPECT_EQ(l[0].Value<int>(), 1);
EXPECT_EQ(l[0].Value<int64_t>(), 1);
EXPECT_EQ(l[1].type(), PropertyValue::Type::Bool);
EXPECT_EQ(l[1].Value<bool>(), true);
EXPECT_EQ(l[2].type(), PropertyValue::Type::Double);

View File

@ -22,8 +22,8 @@ TEST(RecordAccessor, Properties) {
EXPECT_EQ(vertex.PropsAt(property).type(), PropertyValue::Type::Null);
vertex.PropsSet(property, 42);
EXPECT_EQ(vertex.PropsAt(property).Value<int>(), 42);
EXPECT_EQ(properties.at(property).Value<int>(), 42);
EXPECT_EQ(vertex.PropsAt(property).Value<int64_t>(), 42);
EXPECT_EQ(properties.at(property).Value<int64_t>(), 42);
EXPECT_EQ(vertex.PropsAt(property_other).type(), PropertyValue::Type::Null);
EXPECT_EQ(properties.at(property_other).type(), PropertyValue::Type::Null);

View File

@ -61,7 +61,7 @@ TEST(TypedValue, CreationValues) {
EXPECT_EQ(TypedValue(std::string("bla")).Value<std::string>(), "bla");
EXPECT_EQ(TypedValue("bla2").Value<std::string>(), "bla2");
EXPECT_EQ(TypedValue(55).Value<int>(), 55);
EXPECT_EQ(TypedValue(55).Value<int64_t>(), 55);
EXPECT_FLOAT_EQ(TypedValue(66.6).Value<double>(), 66.6);
}
@ -139,7 +139,7 @@ TEST(TypedValue, LogicalNot) {
TEST(TypedValue, UnaryMinus) {
EXPECT_TRUE((-TypedValue::Null).type() == TypedValue::Type::Null);
EXPECT_PROP_EQ((-TypedValue(2).Value<int>()), -2);
EXPECT_PROP_EQ((-TypedValue(2).Value<int64_t>()), -2);
EXPECT_FLOAT_EQ((-TypedValue(2.0).Value<double>()), -2.0);
EXPECT_THROW(-TypedValue(true), TypedValueException);
@ -192,7 +192,7 @@ TEST(TypedValue, Sum) {
true, [](const TypedValue& a, const TypedValue& b) { return a + b; });
// sum of props of the same type
EXPECT_EQ((TypedValue(2) + TypedValue(3)).Value<int>(), 5);
EXPECT_EQ((TypedValue(2) + TypedValue(3)).Value<int64_t>(), 5);
EXPECT_FLOAT_EQ((TypedValue(2.5) + TypedValue(1.25)).Value<double>(), 3.75);
EXPECT_EQ((TypedValue("one") + TypedValue("two")).Value<std::string>(),
"onetwo");
@ -221,7 +221,7 @@ TEST(TypedValue, Difference) {
false, [](const TypedValue& a, const TypedValue& b) { return a - b; });
// difference of props of the same type
EXPECT_EQ((TypedValue(2) - TypedValue(3)).Value<int>(), -1);
EXPECT_EQ((TypedValue(2) - TypedValue(3)).Value<int64_t>(), -1);
EXPECT_FLOAT_EQ((TypedValue(2.5) - TypedValue(2.25)).Value<double>(), 0.25);
// implicit casting