From 8bf03d39ba9c36ab8e936cce31c9388940924e59 Mon Sep 17 00:00:00 2001 From: shylock <33566796+Shylock-Hg@users.noreply.github.com> Date: Fri, 31 Dec 2021 15:57:07 +0800 Subject: [PATCH] Support duration. (#83) --- .clang-format | 2 +- include/common/datatypes/DataSet.h | 48 ++- include/common/datatypes/Date.h | 12 +- include/common/datatypes/Duration.h | 37 +- include/common/datatypes/Edge.h | 8 +- include/common/datatypes/Geography.h | 80 +++- include/common/datatypes/HostAddr.h | 4 +- include/common/datatypes/KeyValue.h | 4 +- include/common/datatypes/List.h | 40 +- include/common/datatypes/Map.h | 24 +- include/common/datatypes/Path.h | 20 +- include/common/datatypes/Set.h | 28 +- include/common/datatypes/Value.h | 109 ++++- include/common/datatypes/ValueOps-inl.h | 54 ++- include/common/datatypes/Vertex.h | 20 +- .../common/geo/io/wkb/ByteOrderDataIOStream.h | 8 +- include/common/graph/Response.h | 32 +- include/nebula/client/ConnectionPool.h | 4 +- include/nebula/client/Session.h | 20 +- include/nebula/sclient/StorageClient.h | 4 +- src/CMakeLists.txt | 1 + src/client/Connection.cpp | 12 +- src/client/ConnectionPool.cpp | 4 +- src/client/Session.cpp | 4 +- src/client/tests/ConnectionTest.cpp | 18 + src/client/tests/SessionTest.cpp | 15 + src/datatypes/Date.cpp | 4 +- src/datatypes/Duration.cpp | 11 +- src/datatypes/Geography.cpp | 8 +- src/datatypes/HostAddr.cpp | 4 +- src/datatypes/Value.cpp | 382 ++++++++++++++---- src/geo/io/wkb/WKBWriter.cpp | 4 +- src/sclient/ScanEdgeIter.cpp | 8 +- src/thrift/ThriftClientManager.h | 4 +- 34 files changed, 821 insertions(+), 216 deletions(-) diff --git a/.clang-format b/.clang-format index 1f43cf47..01ad18ce 100644 --- a/.clang-format +++ b/.clang-format @@ -22,7 +22,7 @@ AllowAllConstructorInitializersOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortBlocksOnASingleLine: Never AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: All +AllowShortFunctionsOnASingleLine: Empty AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: WithoutElse AllowShortLoopsOnASingleLine: true diff --git a/include/common/datatypes/DataSet.h b/include/common/datatypes/DataSet.h index b0858d4e..670675bb 100644 --- a/include/common/datatypes/DataSet.h +++ b/include/common/datatypes/DataSet.h @@ -46,9 +46,13 @@ struct DataSet { return *this; } - const std::vector& keys() const { return colNames; } + const std::vector& keys() const { + return colNames; + } - const std::vector& rowValues(std::size_t index) const { return rows[index].values; } + const std::vector& rowValues(std::size_t index) const { + return rows[index].values; + } std::vector colValues(const std::string& colName) const { std::vector col; @@ -67,13 +71,21 @@ struct DataSet { using iterator = std::vector::iterator; using const_iterator = std::vector::const_iterator; - iterator begin() { return rows.begin(); } + iterator begin() { + return rows.begin(); + } - const_iterator begin() const { return rows.begin(); } + const_iterator begin() const { + return rows.begin(); + } - iterator end() { return rows.end(); } + iterator end() { + return rows.end(); + } - const_iterator end() const { return rows.end(); } + const_iterator end() const { + return rows.end(); + } template ::value, T>::type> @@ -124,13 +136,21 @@ struct DataSet { rows.clear(); } - void __clear() { clear(); } + void __clear() { + clear(); + } - std::size_t size() const { return rowSize(); } + std::size_t size() const { + return rowSize(); + } - std::size_t rowSize() const { return rows.size(); } + std::size_t rowSize() const { + return rows.size(); + } - std::size_t colSize() const { return colNames.size(); } + std::size_t colSize() const { + return colNames.size(); + } std::string toString() const { std::stringstream os; @@ -151,9 +171,13 @@ struct DataSet { return os.str(); } - bool operator==(const DataSet& rhs) const { return colNames == rhs.colNames && rows == rhs.rows; } + bool operator==(const DataSet& rhs) const { + return colNames == rhs.colNames && rows == rhs.rows; + } }; -inline std::ostream& operator<<(std::ostream& os, const DataSet& d) { return os << d.toString(); } +inline std::ostream& operator<<(std::ostream& os, const DataSet& d) { + return os << d.toString(); +} } // namespace nebula diff --git a/include/common/datatypes/Date.h b/include/common/datatypes/Date.h index 0dbabc3b..adad7cbf 100644 --- a/include/common/datatypes/Date.h +++ b/include/common/datatypes/Date.h @@ -31,7 +31,9 @@ struct Date { day = 1; } - void __clear() { clear(); } + void __clear() { + clear(); + } void reset(int16_t y, int8_t m, int8_t d) { year = y; @@ -88,7 +90,9 @@ struct Time { microsec = 0; } - void __clear() { clear(); } + void __clear() { + clear(); + } bool operator==(const Time& rhs) const { return hour == rhs.hour && minute == rhs.minute && sec == rhs.sec && microsec == rhs.microsec; @@ -169,7 +173,9 @@ struct DateTime { microsec = 0; } - void __clear() { clear(); } + void __clear() { + clear(); + } bool operator==(const DateTime& rhs) const { return year == rhs.year && month == rhs.month && day == rhs.day && hour == rhs.hour && diff --git a/include/common/datatypes/Duration.h b/include/common/datatypes/Duration.h index dd58dc50..525806a5 100644 --- a/include/common/datatypes/Duration.h +++ b/include/common/datatypes/Duration.h @@ -24,21 +24,37 @@ struct Duration { Duration() : seconds(0), microseconds(0), months(0) {} Duration(int32_t m, int64_t s, int32_t us) : seconds(s), microseconds(us), months(m) {} - int64_t years() const { return months / 12; } + int64_t years() const { + return months / 12; + } - int64_t monthsInYear() const { return months % 12; } + int64_t monthsInYear() const { + return months % 12; + } - int64_t days() const { return seconds / time::kSecondsOfDay; } + int64_t days() const { + return seconds / time::kSecondsOfDay; + } - int64_t hours() const { return seconds % time::kSecondsOfDay / time::kSecondsOfHour; } + int64_t hours() const { + return seconds % time::kSecondsOfDay / time::kSecondsOfHour; + } - int64_t minutes() const { return seconds % time::kSecondsOfHour / time::kSecondsOfMinute; } + int64_t minutes() const { + return seconds % time::kSecondsOfHour / time::kSecondsOfMinute; + } - int64_t secondsInMinute() const { return seconds % time::kSecondsOfMinute; } + int64_t secondsInMinute() const { + return seconds % time::kSecondsOfMinute; + } - int64_t microsecondsInSecond() const { return microseconds; } + int64_t microsecondsInSecond() const { + return microseconds; + } - Duration operator-() const { return Duration(-months, -seconds, -microseconds); } + Duration operator-() const { + return Duration(-months, -seconds, -microseconds); + } Duration operator+(const Duration& rhs) const { return Duration(months + rhs.months, seconds + rhs.seconds, microseconds + rhs.microseconds); @@ -106,10 +122,7 @@ struct Duration { return months == rhs.months && seconds == rhs.seconds && microseconds == rhs.microseconds; } - std::string toString() const { - return folly::sformat( - "P{}MT{}.{:0>6}000S", months, seconds + microseconds / 1000000, microseconds % 1000000); - } + std::string toString() const; }; } // namespace nebula diff --git a/include/common/datatypes/Edge.h b/include/common/datatypes/Edge.h index 5f1593a3..46f66be3 100644 --- a/include/common/datatypes/Edge.h +++ b/include/common/datatypes/Edge.h @@ -45,7 +45,9 @@ struct Edge { void clear(); - void __clear() { clear(); } + void __clear() { + clear(); + } std::string toString() const; @@ -66,7 +68,9 @@ struct Edge { const Value& value(const std::string& key) const; }; -inline std::ostream& operator<<(std::ostream& os, const Edge& v) { return os << v.toString(); } +inline std::ostream& operator<<(std::ostream& os, const Edge& v) { + return os << v.toString(); +} } // namespace nebula diff --git a/include/common/datatypes/Geography.h b/include/common/datatypes/Geography.h index 847e48d8..b9482b68 100644 --- a/include/common/datatypes/Geography.h +++ b/include/common/datatypes/Geography.h @@ -49,12 +49,16 @@ struct Coordinate { x = 0.0; y = 0.0; } - void __clear() { clear(); } + void __clear() { + clear(); + } bool operator==(const Coordinate& rhs) const { return std::abs(x - rhs.x) < kEpsilon && std::abs(y - rhs.y) < kEpsilon; } - bool operator!=(const Coordinate& rhs) const { return !(*this == rhs); } + bool operator!=(const Coordinate& rhs) const { + return !(*this == rhs); + } bool operator<(const Coordinate& rhs) const { if (x != rhs.x) { return x < rhs.x; @@ -73,11 +77,19 @@ struct Point { explicit Point(const Coordinate& v) : coord(v) {} explicit Point(Coordinate&& v) : coord(std::move(v)) {} - void clear() { coord.clear(); } - void __clear() { clear(); } + void clear() { + coord.clear(); + } + void __clear() { + clear(); + } - bool operator==(const Point& rhs) const { return coord == rhs.coord; } - bool operator<(const Point& rhs) const { return coord < rhs.coord; } + bool operator==(const Point& rhs) const { + return coord == rhs.coord; + } + bool operator<(const Point& rhs) const { + return coord < rhs.coord; + } }; struct LineString { @@ -87,13 +99,23 @@ struct LineString { explicit LineString(const std::vector& v) : coordList(v) {} explicit LineString(std::vector&& v) : coordList(std::move(v)) {} - uint32_t numCoord() const { return coordList.size(); } + uint32_t numCoord() const { + return coordList.size(); + } - void clear() { coordList.clear(); } - void __clear() { clear(); } + void clear() { + coordList.clear(); + } + void __clear() { + clear(); + } - bool operator==(const LineString& rhs) const { return coordList == rhs.coordList; } - bool operator<(const LineString& rhs) const { return coordList < rhs.coordList; } + bool operator==(const LineString& rhs) const { + return coordList == rhs.coordList; + } + bool operator<(const LineString& rhs) const { + return coordList < rhs.coordList; + } }; struct Polygon { @@ -103,13 +125,23 @@ struct Polygon { explicit Polygon(const std::vector>& v) : coordListList(v) {} explicit Polygon(std::vector>&& v) : coordListList(std::move(v)) {} - uint32_t numCoordList() const { return coordListList.size(); } + uint32_t numCoordList() const { + return coordListList.size(); + } - void clear() { coordListList.clear(); } - void __clear() { clear(); } + void clear() { + coordListList.clear(); + } + void __clear() { + clear(); + } - bool operator==(const Polygon& rhs) const { return coordListList == rhs.coordListList; } - bool operator<(const Polygon& rhs) const { return coordListList < rhs.coordListList; } + bool operator==(const Polygon& rhs) const { + return coordListList == rhs.coordListList; + } + bool operator<(const Polygon& rhs) const { + return coordListList < rhs.coordListList; + } }; struct Geography { @@ -137,17 +169,25 @@ struct Geography { std::string asWKB() const; - std::string toString() const { return asWKT(); } + std::string toString() const { + return asWKT(); + } - void clear() { geo_.~variant(); } + void clear() { + geo_.~variant(); + } - void __clear() { clear(); } + void __clear() { + clear(); + } bool operator==(const Geography& rhs) const; bool operator<(const Geography& rhs) const; }; -inline std::ostream& operator<<(std::ostream& os, const Geography& g) { return os << g.toString(); } +inline std::ostream& operator<<(std::ostream& os, const Geography& g) { + return os << g.toString(); +} } // namespace nebula diff --git a/include/common/datatypes/HostAddr.h b/include/common/datatypes/HostAddr.h index c21c654e..80ee85a7 100644 --- a/include/common/datatypes/HostAddr.h +++ b/include/common/datatypes/HostAddr.h @@ -30,7 +30,9 @@ struct HostAddr { port = 0; } - void __clear() { clear(); } + void __clear() { + clear(); + } std::string toString() const { std::stringstream os; diff --git a/include/common/datatypes/KeyValue.h b/include/common/datatypes/KeyValue.h index 2ed03cd6..1b83dfd3 100644 --- a/include/common/datatypes/KeyValue.h +++ b/include/common/datatypes/KeyValue.h @@ -22,7 +22,9 @@ struct KeyValue { value.clear(); } - void __clear() { clear(); } + void __clear() { + clear(); + } bool operator==(const KeyValue& rhs) const { if (key != rhs.key) { diff --git a/include/common/datatypes/List.h b/include/common/datatypes/List.h index 06fbbed3..0eccc8bc 100644 --- a/include/common/datatypes/List.h +++ b/include/common/datatypes/List.h @@ -18,12 +18,18 @@ struct List { List() = default; List(const List&) = default; List(List&&) noexcept = default; - explicit List(std::vector&& vals) { values = std::move(vals); } + explicit List(std::vector&& vals) { + values = std::move(vals); + } explicit List(const std::vector& l) : values(l) {} - bool empty() const { return values.empty(); } + bool empty() const { + return values.empty(); + } - void reserve(std::size_t n) { values.reserve(n); } + void reserve(std::size_t n) { + values.reserve(n); + } template ::value>::type> @@ -31,9 +37,13 @@ struct List { values.emplace_back(std::forward(v)); } - void clear() { values.clear(); } + void clear() { + values.clear(); + } - void __clear() { clear(); } + void __clear() { + clear(); + } List& operator=(const List& rhs) { if (this == &rhs) { @@ -50,22 +60,32 @@ struct List { return *this; } - bool operator==(const List& rhs) const { return values == rhs.values; } + bool operator==(const List& rhs) const { + return values == rhs.values; + } - bool operator<(const List& rhs) const { return values < rhs.values; } + bool operator<(const List& rhs) const { + return values < rhs.values; + } - const Value& operator[](size_t i) const { return values[i]; } + const Value& operator[](size_t i) const { + return values[i]; + } bool contains(const Value& value) const { return std::find(values.begin(), values.end(), value) != values.end(); } - size_t size() const { return values.size(); } + size_t size() const { + return values.size(); + } std::string toString() const; }; -inline std::ostream& operator<<(std::ostream& os, const List& l) { return os << l.toString(); } +inline std::ostream& operator<<(std::ostream& os, const List& l) { + return os << l.toString(); +} } // namespace nebula diff --git a/include/common/datatypes/Map.h b/include/common/datatypes/Map.h index 128994f1..865ef9a2 100644 --- a/include/common/datatypes/Map.h +++ b/include/common/datatypes/Map.h @@ -17,7 +17,9 @@ struct Map { Map() = default; Map(const Map&) = default; Map(Map&&) noexcept = default; - explicit Map(std::unordered_map values) { kvs = std::move(values); } + explicit Map(std::unordered_map values) { + kvs = std::move(values); + } Map& operator=(const Map& rhs) { if (this == &rhs) { @@ -34,15 +36,21 @@ struct Map { return *this; } - void clear() { kvs.clear(); } + void clear() { + kvs.clear(); + } - void __clear() { clear(); } + void __clear() { + clear(); + } // the configs of rocksdb will use the interface, so the value need modify to // string std::string toString() const; - bool operator==(const Map& rhs) const { return kvs == rhs.kvs; } + bool operator==(const Map& rhs) const { + return kvs == rhs.kvs; + } bool contains(const Value& value) const { if (!value.isStr()) { @@ -59,10 +67,14 @@ struct Map { return iter->second; } - size_t size() const { return kvs.size(); } + size_t size() const { + return kvs.size(); + } }; -inline std::ostream& operator<<(std::ostream& os, const Map& m) { return os << m.toString(); } +inline std::ostream& operator<<(std::ostream& os, const Map& m) { + return os << m.toString(); +} } // namespace nebula diff --git a/include/common/datatypes/Path.h b/include/common/datatypes/Path.h index f71391b9..9b993e60 100644 --- a/include/common/datatypes/Path.h +++ b/include/common/datatypes/Path.h @@ -42,7 +42,9 @@ struct Step { props.clear(); } - void __clear() { clear(); } + void __clear() { + clear(); + } std::string toString() const { std::stringstream os; @@ -115,7 +117,9 @@ struct Path { steps.clear(); } - void __clear() { clear(); } + void __clear() { + clear(); + } std::string toString() const { std::stringstream os; @@ -144,9 +148,13 @@ struct Path { return *this; } - bool operator==(const Path& rhs) const { return src == rhs.src && steps == rhs.steps; } + bool operator==(const Path& rhs) const { + return src == rhs.src && steps == rhs.steps; + } - void addStep(Step step) { steps.emplace_back(std::move(step)); } + void addStep(Step step) { + steps.emplace_back(std::move(step)); + } void reverse(); @@ -174,7 +182,9 @@ inline void swap(Step& a, Step& b) { b = std::move(tmp); } -inline std::ostream& operator<<(std::ostream& os, const Path& p) { return os << p.toString(); } +inline std::ostream& operator<<(std::ostream& os, const Path& p) { + return os << p.toString(); +} } // namespace nebula diff --git a/include/common/datatypes/Set.h b/include/common/datatypes/Set.h index 909b85a3..7649d8d6 100644 --- a/include/common/datatypes/Set.h +++ b/include/common/datatypes/Set.h @@ -17,11 +17,17 @@ struct Set { Set() = default; Set(const Set&) = default; Set(Set&&) noexcept = default; - explicit Set(std::unordered_set value) { values = std::move(value); } + explicit Set(std::unordered_set value) { + values = std::move(value); + } - void clear() { values.clear(); } + void clear() { + values.clear(); + } - void __clear() { clear(); } + void __clear() { + clear(); + } std::string toString() const; @@ -41,14 +47,22 @@ struct Set { return *this; } - bool operator==(const Set& rhs) const { return values == rhs.values; } + bool operator==(const Set& rhs) const { + return values == rhs.values; + } - bool contains(const Value& value) const { return values.count(value) != 0; } + bool contains(const Value& value) const { + return values.count(value) != 0; + } - size_t size() const { return values.size(); } + size_t size() const { + return values.size(); + } }; -inline std::ostream& operator<<(std::ostream& os, const Set& s) { return os << s.toString(); } +inline std::ostream& operator<<(std::ostream& os, const Set& s) { + return os << s.toString(); +} } // namespace nebula diff --git a/include/common/datatypes/Value.h b/include/common/datatypes/Value.h index 74451f28..bb6c64f6 100644 --- a/include/common/datatypes/Value.h +++ b/include/common/datatypes/Value.h @@ -8,6 +8,7 @@ #include #include "common/datatypes/Date.h" +#include "common/datatypes/Duration.h" #include "common/thrift/ThriftTypes.h" namespace apache { @@ -74,6 +75,7 @@ struct Value { SET = 1UL << 13, DATASET = 1UL << 14, GEOGRAPHY = 1UL << 15, + DURATION = 1UL << 16, NULLVALUE = 1UL << 63, }; @@ -127,14 +129,25 @@ struct Value { Value(DataSet&& v); // NOLINT Value(const Geography& v); // NOLINT Value(Geography&& v); // NOLINT - ~Value() { clear(); } + Value(const Duration& v); // NOLINT + Value(Duration&& v); // NOLINT + ~Value() { + clear(); + } - Type type() const noexcept { return type_; } + Type type() const noexcept { + return type_; + } const std::string& typeName() const; + static const std::string toString(Type type); - bool empty() const { return type_ == Type::__EMPTY__; } - bool isNull() const { return type_ == Type::NULLVALUE; } + bool empty() const { + return type_ == Type::__EMPTY__; + } + bool isNull() const { + return type_ == Type::NULLVALUE; + } bool isBadNull() const { if (!isNull()) { return false; @@ -144,26 +157,63 @@ struct Value { null == NullType::ERR_OVERFLOW || null == NullType::UNKNOWN_PROP || null == NullType::DIV_BY_ZERO || null == NullType::OUT_OF_RANGE; } - bool isNumeric() const { return type_ == Type::INT || type_ == Type::FLOAT; } - bool isBool() const { return type_ == Type::BOOL; } - bool isInt() const { return type_ == Type::INT; } - bool isFloat() const { return type_ == Type::FLOAT; } - bool isStr() const { return type_ == Type::STRING; } - bool isDate() const { return type_ == Type::DATE; } - bool isTime() const { return type_ == Type::TIME; } - bool isDateTime() const { return type_ == Type::DATETIME; } - bool isVertex() const { return type_ == Type::VERTEX; } - bool isEdge() const { return type_ == Type::EDGE; } - bool isPath() const { return type_ == Type::PATH; } - bool isList() const { return type_ == Type::LIST; } - bool isMap() const { return type_ == Type::MAP; } - bool isSet() const { return type_ == Type::SET; } - bool isDataSet() const { return type_ == Type::DATASET; } - bool isGeography() const { return type_ == Type::GEOGRAPHY; } + bool isNumeric() const { + return type_ == Type::INT || type_ == Type::FLOAT; + } + bool isBool() const { + return type_ == Type::BOOL; + } + bool isInt() const { + return type_ == Type::INT; + } + bool isFloat() const { + return type_ == Type::FLOAT; + } + bool isStr() const { + return type_ == Type::STRING; + } + bool isDate() const { + return type_ == Type::DATE; + } + bool isTime() const { + return type_ == Type::TIME; + } + bool isDateTime() const { + return type_ == Type::DATETIME; + } + bool isVertex() const { + return type_ == Type::VERTEX; + } + bool isEdge() const { + return type_ == Type::EDGE; + } + bool isPath() const { + return type_ == Type::PATH; + } + bool isList() const { + return type_ == Type::LIST; + } + bool isMap() const { + return type_ == Type::MAP; + } + bool isSet() const { + return type_ == Type::SET; + } + bool isDataSet() const { + return type_ == Type::DATASET; + } + bool isGeography() const { + return type_ == Type::GEOGRAPHY; + } + bool isDuration() const { + return type_ == Type::DURATION; + } void clear(); - void __clear() { clear(); } + void __clear() { + clear(); + } Value& operator=(Value&& rhs) noexcept; Value& operator=(const Value& rhs); @@ -215,6 +265,9 @@ struct Value { void setGeography(const Geography& v); void setGeography(Geography&& v); void setGeography(std::unique_ptr&& v); + void setDuration(const Duration& v); + void setDuration(Duration&& v); + void setDuration(std::unique_ptr&& v); const NullType& getNull() const; const bool& getBool() const; @@ -240,6 +293,8 @@ struct Value { const DataSet* getDataSetPtr() const; const Geography& getGeography() const; const Geography* getGeographyPtr() const; + const Duration& getDuration() const; + const Duration* getDurationPtr() const; NullType moveNull(); bool moveBool(); @@ -257,6 +312,7 @@ struct Value { Set moveSet(); DataSet moveDataSet(); Geography moveGeography(); + Duration moveDuration(); NullType& mutableNull(); bool& mutableBool(); @@ -274,8 +330,11 @@ struct Value { Set& mutableSet(); DataSet& mutableDataSet(); Geography& mutableGeography(); + Duration& mutableDuration(); - static const Value& null() noexcept { return kNullValue; } + static const Value& null() noexcept { + return kNullValue; + } std::string toString() const; @@ -307,6 +366,7 @@ struct Value { std::unique_ptr uVal; std::unique_ptr gVal; std::unique_ptr ggVal; + std::unique_ptr duVal; Storage() {} ~Storage() {} @@ -383,6 +443,11 @@ struct Value { void setGG(std::unique_ptr&& v); void setGG(const Geography& v); void setGG(Geography&& v); + // Duration value + void setDU(const std::unique_ptr& v); + void setDU(std::unique_ptr&& v); + void setDU(const Duration& v); + void setDU(Duration&& v); }; static_assert(sizeof(Value) == 16UL, "The size of Value should be 16UL"); diff --git a/include/common/datatypes/ValueOps-inl.h b/include/common/datatypes/ValueOps-inl.h index d2104e40..43050c84 100644 --- a/include/common/datatypes/ValueOps-inl.h +++ b/include/common/datatypes/ValueOps-inl.h @@ -1,6 +1,9 @@ -/* Copyright (c) 2020 vesoft inc. All rights reserved. +/* + * + * Copyright (c) 2020 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License. * - * obj source code is licensed under Apache 2.0 License. */ #pragma once @@ -13,6 +16,7 @@ #include "common/datatypes/CommonCpp2Ops.h" #include "common/datatypes/DataSetOps-inl.h" #include "common/datatypes/DateOps-inl.h" +#include "common/datatypes/DurationOps-inl.h" #include "common/datatypes/EdgeOps-inl.h" #include "common/datatypes/GeographyOps-inl.h" #include "common/datatypes/ListOps-inl.h" @@ -81,6 +85,9 @@ struct TccStructTraits { } else if (_fname == "ggVal") { fid = 16; _ftype = apache::thrift::protocol::T_STRUCT; + } else if (_fname == "duVal") { + fid = 17; + _ftype = apache::thrift::protocol::T_STRUCT; } } }; @@ -243,6 +250,18 @@ uint32_t Cpp2Ops::write(Protocol* proto, nebula::Value const* obj xfer += proto->writeFieldEnd(); break; } + case nebula::Value::Type::DURATION: { + xfer += proto->writeFieldBegin("duVal", protocol::T_STRUCT, 17); + if (obj->getDurationPtr()) { + xfer += Cpp2Ops::write(proto, obj->getDurationPtr()); + } else { + xfer += proto->writeStructBegin("Duration"); + xfer += proto->writeStructEnd(); + xfer += proto->writeFieldStop(); + } + xfer += proto->writeFieldEnd(); + break; + } case nebula::Value::Type::__EMPTY__: { break; } @@ -434,6 +453,17 @@ void Cpp2Ops::read(Protocol* proto, nebula::Value* obj) { } break; } + case 17: { + if (readState.fieldType == apache::thrift::protocol::T_STRUCT) { + obj->setDuration(nebula::Duration()); + auto ptr = std::make_unique(); + Cpp2Ops::read(proto, ptr.get()); + obj->setDuration(std::move(ptr)); + } else { + proto->skip(readState.fieldType); + } + break; + } default: { proto->skip(readState.fieldType); break; @@ -578,6 +608,16 @@ uint32_t Cpp2Ops::serializedSize(Protocol const* proto, nebula::V } break; } + case nebula::Value::Type::DURATION: { + xfer += proto->serializedFieldSize("duVal", protocol::T_STRUCT, 17); + if (obj->getDurationPtr()) { + xfer += Cpp2Ops::serializedSize(proto, obj->getDurationPtr()); + } else { + xfer += proto->serializedStructSize("Duration"); + xfer += proto->serializedSizeStop(); + } + break; + } case nebula::Value::Type::__EMPTY__: { break; } @@ -716,6 +756,16 @@ uint32_t Cpp2Ops::serializedSizeZC(Protocol const* proto, nebula: } break; } + case nebula::Value::Type::DURATION: { + xfer += proto->serializedFieldSize("duVal", protocol::T_STRUCT, 17); + if (obj->getDurationPtr()) { + xfer += Cpp2Ops::serializedSizeZC(proto, obj->getDurationPtr()); + } else { + xfer += proto->serializedStructSize("Duration"); + xfer += proto->serializedSizeStop(); + } + break; + } case nebula::Value::Type::__EMPTY__: { break; } diff --git a/include/common/datatypes/Vertex.h b/include/common/datatypes/Vertex.h index 5d3eac49..2862abcb 100644 --- a/include/common/datatypes/Vertex.h +++ b/include/common/datatypes/Vertex.h @@ -29,7 +29,9 @@ struct Tag { props.clear(); } - void __clear() { clear(); } + void __clear() { + clear(); + } std::string toString() const; @@ -49,7 +51,9 @@ struct Tag { return *this; } - bool operator==(const Tag& rhs) const { return name == rhs.name && props == rhs.props; } + bool operator==(const Tag& rhs) const { + return name == rhs.name && props == rhs.props; + } }; struct Vertex { @@ -66,7 +70,9 @@ struct Vertex { tags.clear(); } - void __clear() { clear(); } + void __clear() { + clear(); + } std::string toString() const; @@ -74,7 +80,9 @@ struct Vertex { Vertex& operator=(const Vertex& rhs); - bool operator==(const Vertex& rhs) const { return vid == rhs.vid && tags == rhs.tags; } + bool operator==(const Vertex& rhs) const { + return vid == rhs.vid && tags == rhs.tags; + } bool operator<(const Vertex& rhs) const; @@ -89,7 +97,9 @@ inline void swap(Vertex& a, Vertex& b) { b = std::move(temp); } -inline std::ostream& operator<<(std::ostream& os, const Vertex& v) { return os << v.toString(); } +inline std::ostream& operator<<(std::ostream& os, const Vertex& v) { + return os << v.toString(); +} } // namespace nebula diff --git a/include/common/geo/io/wkb/ByteOrderDataIOStream.h b/include/common/geo/io/wkb/ByteOrderDataIOStream.h index ddecd861..2a240e15 100644 --- a/include/common/geo/io/wkb/ByteOrderDataIOStream.h +++ b/include/common/geo/io/wkb/ByteOrderDataIOStream.h @@ -18,9 +18,13 @@ class ByteOrderDataOutStream { ~ByteOrderDataOutStream() = default; - std::string str() const { return stream_.str(); } + std::string str() const { + return stream_.str(); + } - void setByteOrder(ByteOrder order) { byteOrder_ = order; } + void setByteOrder(ByteOrder order) { + byteOrder_ = order; + } void writeUint8(uint8_t v); diff --git a/include/common/graph/Response.h b/include/common/graph/Response.h index 6665b6b3..f87a0888 100644 --- a/include/common/graph/Response.h +++ b/include/common/graph/Response.h @@ -211,7 +211,9 @@ struct AuthResponse { errorMsg = nullptr; } - void clear() { __clear(); } + void clear() { + __clear(); + } bool operator==(const AuthResponse &rhs) const { if (errorCode != rhs.errorCode) { @@ -244,7 +246,9 @@ struct ProfilingStats { otherStats = nullptr; } - void clear() { __clear(); } + void clear() { + __clear(); + } bool operator==(const ProfilingStats &rhs) const { if (rows != rhs.rows) { @@ -276,7 +280,9 @@ struct PlanNodeBranchInfo { conditionNodeId = -1; } - void clear() { __clear(); } + void clear() { + __clear(); + } bool operator==(const PlanNodeBranchInfo &rhs) const { return isDoBranch == rhs.isDoBranch && conditionNodeId == rhs.conditionNodeId; @@ -294,9 +300,13 @@ struct Pair { value.clear(); } - void clear() { __clear(); } + void clear() { + __clear(); + } - bool operator==(const Pair &rhs) const { return key == rhs.key && value == rhs.value; } + bool operator==(const Pair &rhs) const { + return key == rhs.key && value == rhs.value; + } std::string key; std::string value; @@ -313,7 +323,9 @@ struct PlanNodeDescription { dependencies = nullptr; } - void clear() { __clear(); } + void clear() { + __clear(); + } bool operator==(const PlanNodeDescription &rhs) const; @@ -337,7 +349,9 @@ struct PlanDescription { optimize_time_in_us = 0; } - void clear() { __clear(); } + void clear() { + __clear(); + } bool operator==(const PlanDescription &rhs) const { return planNodeDescs == rhs.planNodeDescs && nodeIndexMap == rhs.nodeIndexMap && @@ -364,7 +378,9 @@ struct ExecutionResponse { comment.reset(); } - void clear() { __clear(); } + void clear() { + __clear(); + } bool operator==(const ExecutionResponse &rhs) const { if (errorCode != rhs.errorCode) { diff --git a/include/nebula/client/ConnectionPool.h b/include/nebula/client/ConnectionPool.h index 73023f78..a3c6d8f3 100644 --- a/include/nebula/client/ConnectionPool.h +++ b/include/nebula/client/ConnectionPool.h @@ -41,7 +41,9 @@ class ConnectionPool { // The count may can't perform if can't create enough valid connection void newConnection(std::size_t cursor, std::size_t count); - std::size_t nextCursor() { return cursor_ >= address_.size() ? cursor_ = 0 : cursor_++; } + std::size_t nextCursor() { + return cursor_ >= address_.size() ? cursor_ = 0 : cursor_++; + } std::size_t cursor_{0}; // host, port diff --git a/include/nebula/client/Session.h b/include/nebula/client/Session.h index b5394c3f..704e5074 100644 --- a/include/nebula/client/Session.h +++ b/include/nebula/client/Session.h @@ -45,7 +45,9 @@ class Session { session.pool_ = nullptr; session.offsetSecs_ = 0; } - ~Session() { release(); } + ~Session() { + release(); + } ExecutionResponse execute(const std::string &stmt); @@ -61,14 +63,22 @@ class Session { void release(); - bool valid() const { return sessionId_ > 0; } + bool valid() const { + return sessionId_ > 0; + } - const std::string &timeZoneName() const { return timezoneName_; } + const std::string &timeZoneName() const { + return timezoneName_; + } - int32_t timeZoneOffsetSecs() const { return offsetSecs_; } + int32_t timeZoneOffsetSecs() const { + return offsetSecs_; + } // convert the time to server time zone - void toLocal(DataSet &data) { return toLocal(data, offsetSecs_); } + void toLocal(DataSet &data) { + return toLocal(data, offsetSecs_); + } // convert the time to specific time zone static void toLocal(DataSet &data, int32_t offsetSecs); diff --git a/include/nebula/sclient/StorageClient.h b/include/nebula/sclient/StorageClient.h index b3eae691..812e56da 100644 --- a/include/nebula/sclient/StorageClient.h +++ b/include/nebula/sclient/StorageClient.h @@ -79,7 +79,9 @@ class StorageClient { bool onlyLatestVersion = false, bool enableReadFromFollower = true); // plato needed - MetaClient* getMetaClient() { return mClient_.get(); } + MetaClient* getMetaClient() { + return mClient_.get(); + } private: std::pair doScanEdge( diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d95849f8..d699eba9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,6 +29,7 @@ set(NEBULA_COMMON_SOURCES datatypes/Set.cpp datatypes/Value.cpp datatypes/Vertex.cpp + datatypes/Duration.cpp graph/Response.cpp time/TimeConversion.cpp geo/io/wkt/WKTWriter.cpp diff --git a/src/client/Connection.cpp b/src/client/Connection.cpp index e4c6ce28..f538bd84 100644 --- a/src/client/Connection.cpp +++ b/src/client/Connection.cpp @@ -29,7 +29,9 @@ class NebulaConnectionErrMessageCallback : public folly::AsyncSocket::ErrMessage * a message read from error queue associated * with the socket. */ - void errMessage(const cmsghdr &) noexcept override { DLOG(ERROR) << "Connection error."; } + void errMessage(const cmsghdr &) noexcept override { + DLOG(ERROR) << "Connection error."; + } /** * errMessageError() will be invoked if an error occurs reading a message @@ -41,7 +43,9 @@ class NebulaConnectionErrMessageCallback : public folly::AsyncSocket::ErrMessage DLOG(ERROR) << "Connection error: " << ex.what(); } - static auto &instance() { return cb_; } + static auto &instance() { + return cb_; + } private: static NebulaConnectionErrMessageCallback cb_; @@ -199,7 +203,9 @@ void Connection::asyncExecuteJson(int64_t sessionId, client_->future_executeJson(sessionId, stmt).thenValue(std::move(cb)); } -bool Connection::isOpen() { return ping(); } +bool Connection::isOpen() { + return ping(); +} void Connection::close() { if (client_ != nullptr) { diff --git a/src/client/ConnectionPool.cpp b/src/client/ConnectionPool.cpp index 58e3819b..3b6d4c27 100644 --- a/src/client/ConnectionPool.cpp +++ b/src/client/ConnectionPool.cpp @@ -11,7 +11,9 @@ namespace nebula { -ConnectionPool::~ConnectionPool() { close(); } +ConnectionPool::~ConnectionPool() { + close(); +} void ConnectionPool::init(const std::vector &addresses, const Config &config) { for (const auto &addr : addresses) { diff --git a/src/client/Session.cpp b/src/client/Session.cpp index c83aafcf..afd795d3 100644 --- a/src/client/Session.cpp +++ b/src/client/Session.cpp @@ -29,7 +29,9 @@ void Session::asyncExecuteJson(const std::string &stmt, ExecuteJsonCallback cb) sessionId_, stmt, [cb = std::move(cb)](auto &&json) { cb(std::move(json)); }); } -bool Session::ping() { return conn_.ping(); } +bool Session::ping() { + return conn_.ping(); +} ErrorCode Session::retryConnect() { pool_->giveBack(std::move(conn_)); diff --git a/src/client/tests/ConnectionTest.cpp b/src/client/tests/ConnectionTest.cpp index 02bb2fb5..993ff5af 100644 --- a/src/client/tests/ConnectionTest.cpp +++ b/src/client/tests/ConnectionTest.cpp @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -182,6 +183,23 @@ TEST_F(ConnectionTest, JsonResult) { b1.wait(); } +TEST_F(ConnectionTest, DurationResult) { + nebula::Connection c; + + ASSERT_TRUE(c.open(kServerHost, 9669, 10, false, "")); + + // auth + auto authResp = c.authenticate("root", "nebula"); + ASSERT_EQ(authResp.errorCode, nebula::ErrorCode::SUCCEEDED); + + auto resp = c.execute(*authResp.sessionId, "YIELD duration({years: 1, seconds: 2}) AS d"); + ASSERT_EQ(resp.errorCode, nebula::ErrorCode::SUCCEEDED); + + nebula::DataSet expected({"d"}); + expected.emplace_back(nebula::Row({nebula::Duration().addYears(1).addSeconds(2)})); + EXPECT_TRUE(verifyResultWithoutOrder(*resp.data, expected)); +} + TEST_F(ConnectionTest, InvalidPort) { nebula::Connection c; diff --git a/src/client/tests/SessionTest.cpp b/src/client/tests/SessionTest.cpp index afde7b40..36b790e8 100644 --- a/src/client/tests/SessionTest.cpp +++ b/src/client/tests/SessionTest.cpp @@ -243,6 +243,21 @@ TEST_F(SessionTest, JsonResult) { b.wait(); } +TEST_F(SessionTest, DurationResult) { + nebula::ConnectionPool pool; + nebula::Config c{10, 0, 10, 0, "", false}; + pool.init({kServerHost ":9669"}, c); + auto session = pool.getSession("root", "nebula"); + ASSERT_TRUE(session.valid()); + + auto resp = session.execute("YIELD duration({years: 1, seconds: 2}) AS d"); + ASSERT_EQ(resp.errorCode, nebula::ErrorCode::SUCCEEDED); + + nebula::DataSet expected({"d"}); + expected.emplace_back(nebula::Row({nebula::Duration().addYears(1).addSeconds(2)})); + EXPECT_TRUE(verifyResultWithoutOrder(*resp.data, expected)); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); nebula::init(&argc, &argv); diff --git a/src/datatypes/Date.cpp b/src/datatypes/Date.cpp index 6252588e..4a2d310c 100644 --- a/src/datatypes/Date.cpp +++ b/src/datatypes/Date.cpp @@ -15,7 +15,9 @@ namespace nebula { const int64_t kDaysSoFar[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; const int64_t kLeapDaysSoFar[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}; -Date::Date(uint64_t days) { fromInt(days); } +Date::Date(uint64_t days) { + fromInt(days); +} int64_t Date::toInt() const { // Year diff --git a/src/datatypes/Duration.cpp b/src/datatypes/Duration.cpp index 0eff713b..0081fa9e 100644 --- a/src/datatypes/Duration.cpp +++ b/src/datatypes/Duration.cpp @@ -5,12 +5,21 @@ #include "common/datatypes/Duration.h" +#include #include #include #include -namespace nebula {} // namespace nebula +namespace nebula { + +std::string Duration::toString() const { + return folly::format( + "P{}MT{}.{:0>6}000S", months, seconds + microseconds / 1000000, microseconds % 1000000) + .str(); +} + +} // namespace nebula namespace std { diff --git a/src/datatypes/Geography.cpp b/src/datatypes/Geography.cpp index aa958118..c4ae3634 100644 --- a/src/datatypes/Geography.cpp +++ b/src/datatypes/Geography.cpp @@ -84,9 +84,13 @@ Polygon& Geography::mutablePolygon() { return std::get(geo_); } -std::string Geography::asWKT() const { return geo::WKTWriter().write(*this); } +std::string Geography::asWKT() const { + return geo::WKTWriter().write(*this); +} -std::string Geography::asWKB() const { return geo::WKBWriter().write(*this); } +std::string Geography::asWKB() const { + return geo::WKBWriter().write(*this); +} bool Geography::operator==(const Geography& rhs) const { auto lhsShape = shape(); diff --git a/src/datatypes/HostAddr.cpp b/src/datatypes/HostAddr.cpp index 6d83e109..14ad8f9c 100644 --- a/src/datatypes/HostAddr.cpp +++ b/src/datatypes/HostAddr.cpp @@ -13,7 +13,9 @@ bool HostAddr::operator==(const HostAddr& rhs) const { return host == rhs.host && port == rhs.port; } -bool HostAddr::operator!=(const HostAddr& rhs) const { return !(*this == rhs); } +bool HostAddr::operator!=(const HostAddr& rhs) const { + return !(*this == rhs); +} bool HostAddr::operator<(const HostAddr& rhs) const { if (host == rhs.host) { diff --git a/src/datatypes/Value.cpp b/src/datatypes/Value.cpp index ce6bce22..011a447d 100644 --- a/src/datatypes/Value.cpp +++ b/src/datatypes/Value.cpp @@ -73,6 +73,9 @@ std::size_t hash::operator()(const nebula::Value& v) const noexce case nebula::Value::Type::SET: { return hash()(v.getSet()); } + case nebula::Value::Type::DURATION: { + return hash()(v.getDuration()); + } case nebula::Value::Type::DATASET: { LOG(FATAL) << "Hash for DATASET has not been implemented"; } @@ -171,6 +174,10 @@ Value::Value(Value&& rhs) noexcept : type_(Value::Type::__EMPTY__) { setGG(std::move(rhs.value_.ggVal)); break; } + case Type::DURATION: { + setDU(std::move(rhs.value_.duVal)); + break; + } default: { assert(false); break; @@ -251,6 +258,10 @@ Value::Value(const Value& rhs) : type_(Value::Type::__EMPTY__) { setGG(rhs.value_.ggVal); break; } + case Type::DURATION: { + setDU(rhs.value_.duVal); + break; + } default: { assert(false); break; @@ -258,95 +269,173 @@ Value::Value(const Value& rhs) : type_(Value::Type::__EMPTY__) { } } -Value::Value(const NullType& v) { setN(v); } +Value::Value(const NullType& v) { + setN(v); +} -Value::Value(NullType&& v) { setN(std::move(v)); } +Value::Value(NullType&& v) { + setN(std::move(v)); +} -Value::Value(const bool& v) { setB(v); } +Value::Value(const bool& v) { + setB(v); +} -Value::Value(bool&& v) { setB(std::move(v)); } +Value::Value(bool&& v) { + setB(std::move(v)); +} -Value::Value(const int8_t& v) { setI(v); } +Value::Value(const int8_t& v) { + setI(v); +} -Value::Value(int8_t&& v) { setI(std::move(v)); } +Value::Value(int8_t&& v) { + setI(std::move(v)); +} -Value::Value(const int16_t& v) { setI(v); } +Value::Value(const int16_t& v) { + setI(v); +} -Value::Value(int16_t&& v) { setI(std::move(v)); } +Value::Value(int16_t&& v) { + setI(std::move(v)); +} -Value::Value(const int32_t& v) { setI(v); } +Value::Value(const int32_t& v) { + setI(v); +} -Value::Value(int32_t&& v) { setI(std::move(v)); } +Value::Value(int32_t&& v) { + setI(std::move(v)); +} -Value::Value(const int64_t& v) { setI(v); } +Value::Value(const int64_t& v) { + setI(v); +} -Value::Value(int64_t&& v) { setI(std::move(v)); } +Value::Value(int64_t&& v) { + setI(std::move(v)); +} -Value::Value(const double& v) { setF(v); } +Value::Value(const double& v) { + setF(v); +} -Value::Value(double&& v) { setF(std::move(v)); } +Value::Value(double&& v) { + setF(std::move(v)); +} -Value::Value(const std::string& v) { setS(v); } +Value::Value(const std::string& v) { + setS(v); +} -Value::Value(std::string&& v) { setS(std::move(v)); } +Value::Value(std::string&& v) { + setS(std::move(v)); +} -Value::Value(const char* v) { setS(v); } +Value::Value(const char* v) { + setS(v); +} -Value::Value(const Date& v) { setD(v); } +Value::Value(const Date& v) { + setD(v); +} -Value::Value(Date&& v) { setD(std::move(v)); } +Value::Value(Date&& v) { + setD(std::move(v)); +} -Value::Value(const Time& v) { setT(v); } +Value::Value(const Time& v) { + setT(v); +} -Value::Value(Time&& v) { setT(std::move(v)); } +Value::Value(Time&& v) { + setT(std::move(v)); +} -Value::Value(const DateTime& v) { setDT(v); } +Value::Value(const DateTime& v) { + setDT(v); +} -Value::Value(DateTime&& v) { setDT(std::move(v)); } +Value::Value(DateTime&& v) { + setDT(std::move(v)); +} -Value::Value(const Vertex& v) { setV(v); } +Value::Value(const Vertex& v) { + setV(v); +} -Value::Value(Vertex&& v) { setV(std::move(v)); } +Value::Value(Vertex&& v) { + setV(std::move(v)); +} -Value::Value(const Edge& v) { setE(v); } +Value::Value(const Edge& v) { + setE(v); +} -Value::Value(Edge&& v) { setE(std::move(v)); } +Value::Value(Edge&& v) { + setE(std::move(v)); +} -Value::Value(const Path& v) { setP(v); } +Value::Value(const Path& v) { + setP(v); +} -Value::Value(Path&& v) { setP(std::move(v)); } +Value::Value(Path&& v) { + setP(std::move(v)); +} Value::Value(const List& v) { auto c = std::make_unique(v); setL(std::move(c)); } -Value::Value(List&& v) { setL(std::make_unique(std::move(v))); } +Value::Value(List&& v) { + setL(std::make_unique(std::move(v))); +} Value::Value(const Map& v) { auto c = std::make_unique(v); setM(std::move(c)); } -Value::Value(Map&& v) { setM(std::make_unique(std::move(v))); } +Value::Value(Map&& v) { + setM(std::make_unique(std::move(v))); +} Value::Value(const Set& v) { auto c = std::make_unique(v); setU(std::move(c)); } -Value::Value(Set&& v) { setU(std::make_unique(std::move(v))); } +Value::Value(Set&& v) { + setU(std::make_unique(std::move(v))); +} Value::Value(const DataSet& v) { auto c = std::make_unique(v); setG(std::move(c)); } -Value::Value(DataSet&& v) { setG(std::make_unique(std::move(v))); } +Value::Value(DataSet&& v) { + setG(std::make_unique(std::move(v))); +} + +Value::Value(const Geography& v) { + setGG(std::make_unique(v)); +} -Value::Value(const Geography& v) { setGG(std::make_unique(v)); } +Value::Value(Geography&& v) { + setGG(std::make_unique(std::move(v))); +} -Value::Value(Geography&& v) { setGG(std::make_unique(std::move(v))); } +Value::Value(const Duration& v) { + setDU(std::make_unique(v)); +} + +Value::Value(Duration&& v) { + setDU(std::make_unique(std::move(v))); +} const std::string& Value::typeName() const { static const std::unordered_map typeNames = { @@ -367,6 +456,7 @@ const std::string& Value::typeName() const { {Type::SET, "set"}, {Type::DATASET, "dataset"}, {Type::GEOGRAPHY, "geography"}, + {Type::DURATION, "duration"}, }; static const std::unordered_map nullTypes = { @@ -630,6 +720,21 @@ void Value::setGeography(std::unique_ptr&& v) { setGG(std::move(v)); } +void Value::setDuration(const Duration& v) { + clear(); + setDU(v); +} + +void Value::setDuration(Duration&& v) { + clear(); + setDU(std::move(v)); +} + +void Value::setDuration(std::unique_ptr&& v) { + clear(); + setDU(std::move(v)); +} + const NullType& Value::getNull() const { CHECK_EQ(type_, Type::NULLVALUE); return value_.nVal; @@ -750,6 +855,16 @@ const Geography* Value::getGeographyPtr() const { return value_.ggVal.get(); } +const Duration& Value::getDuration() const { + CHECK_EQ(type_, Type::DURATION); + return *value_.duVal; +} + +const Duration* Value::getDurationPtr() const { + CHECK_EQ(type_, Type::DURATION); + return value_.duVal.get(); +} + NullType& Value::mutableNull() { CHECK_EQ(type_, Type::NULLVALUE); return value_.nVal; @@ -830,6 +945,11 @@ Geography& Value::mutableGeography() { return *(value_.ggVal); } +Duration& Value::mutableDuration() { + CHECK_EQ(type_, Type::DURATION); + return *value_.duVal; +} + NullType Value::moveNull() { CHECK_EQ(type_, Type::NULLVALUE); NullType v = std::move(value_.nVal); @@ -942,6 +1062,13 @@ Geography Value::moveGeography() { return v; } +Duration Value::moveDuration() { + CHECK_EQ(type_, Type::DURATION); + Duration v = std::move(*value_.duVal); + clear(); + return v; +} + void Value::clear() { switch (type_) { case Type::__EMPTY__: { @@ -1011,6 +1138,10 @@ void Value::clear() { destruct(value_.ggVal); break; } + case Type::DURATION: { + destruct(value_.duVal); + break; + } } type_ = Type::__EMPTY__; } @@ -1088,6 +1219,10 @@ Value& Value::operator=(Value&& rhs) noexcept { setGG(std::move(rhs.value_.ggVal)); break; } + case Type::DURATION: { + setDU(std::move(rhs.value_.duVal)); + break; + } default: { assert(false); break; @@ -1170,6 +1305,10 @@ Value& Value::operator=(const Value& rhs) { setGG(rhs.value_.ggVal); break; } + case Type::DURATION: { + setDU(rhs.value_.duVal); + break; + } default: { assert(false); break; @@ -1429,6 +1568,26 @@ void Value::setGG(Geography&& v) { new (std::addressof(value_.ggVal)) std::unique_ptr(new Geography(std::move(v))); } +void Value::setDU(const std::unique_ptr& v) { + type_ = Type::DURATION; + new (std::addressof(value_.duVal)) std::unique_ptr(new Duration(*v)); +} + +void Value::setDU(std::unique_ptr&& v) { + type_ = Type::DURATION; + new (std::addressof(value_.duVal)) std::unique_ptr(std::move(v)); +} + +void Value::setDU(const Duration& v) { + type_ = Type::DURATION; + new (std::addressof(value_.duVal)) std::unique_ptr(new Duration(v)); +} + +void Value::setDU(Duration&& v) { + type_ = Type::DURATION; + new (std::addressof(value_.duVal)) std::unique_ptr(new Duration(std::move(v))); +} + std::string Value::toString() const { switch (type_) { case Value::Type::__EMPTY__: { @@ -1499,6 +1658,9 @@ std::string Value::toString() const { } case Value::Type::GEOGRAPHY: { return getGeography().toString(); + } + case Value::Type::DURATION: { + return getDuration().toString(); } // no default so the compiler will warning when lack } @@ -1694,6 +1856,11 @@ Value Value::lessThan(const Value& v) const { case Value::Type::GEOGRAPHY: { return getGeography() < v.getGeography(); } + case Value::Type::DURATION: { + // Duration can't compare, + // e.g. What is the result of `duration('P1M') < duration('P30D')`? + return kNullBadType; + } case Value::Type::NULLVALUE: case Value::Type::__EMPTY__: { return kNullBadType; @@ -1784,6 +1951,9 @@ Value Value::equal(const Value& v) const { case Value::Type::GEOGRAPHY: { return getGeography() == v.getGeography(); } + case Value::Type::DURATION: { + return getDuration() == v.getDuration(); + } case Value::Type::NULLVALUE: case Value::Type::__EMPTY__: { return false; @@ -1799,82 +1969,70 @@ void swap(Value& a, Value& b) { b = std::move(temp); } -std::ostream& operator<<(std::ostream& os, const Value::Type& type) { +/*static*/ const std::string Value::toString(Type type) { switch (type) { case Value::Type::__EMPTY__: { - os << "__EMPTY__"; - break; + return "__EMPTY__"; } case Value::Type::NULLVALUE: { - os << "NULL"; - break; + return "NULL"; } case Value::Type::BOOL: { - os << "BOOL"; - break; + return "BOOL"; } case Value::Type::INT: { - os << "INT"; - break; + return "INT"; } case Value::Type::FLOAT: { - os << "FLOAT"; - break; + return "FLOAT"; } case Value::Type::STRING: { - os << "STRING"; - break; + return "STRING"; } case Value::Type::DATE: { - os << "DATE"; - break; + return "DATE"; } case Value::Type::TIME: { - os << "TIME"; - break; + return "TIME"; } case Value::Type::DATETIME: { - os << "DATETIME"; - break; + return "DATETIME"; } case Value::Type::VERTEX: { - os << "VERTEX"; - break; + return "VERTEX"; } case Value::Type::EDGE: { - os << "EDGE"; - break; + return "EDGE"; } case Value::Type::PATH: { - os << "PATH"; - break; + return "PATH"; } case Value::Type::LIST: { - os << "LIST"; - break; + return "LIST"; } case Value::Type::MAP: { - os << "MAP"; - break; + return "MAP"; } case Value::Type::SET: { - os << "SET"; - break; + return "SET"; } case Value::Type::DATASET: { - os << "DATASET"; - break; + return "DATASET"; } case Value::Type::GEOGRAPHY: { - os << "GEOGRAPHY"; - break; + return "GEOGRAPHY"; + } + case Value::Type::DURATION: { + return "DURATION"; } default: { - os << "__UNKNOWN__"; - break; + return "__UNKNOWN__"; } } +} +std::ostream& operator<<(std::ostream& os, const Value::Type& type) { + os << Value::toString(type); return os; } @@ -2001,6 +2159,9 @@ Value operator+(const Value& lhs, const Value& rhs) { ret.values.insert(ret.values.begin(), lhs); return ret; } + case Value::Type::DURATION: { + return lhs.getDate() + rhs.getDuration(); + } default: { return Value::kNullBadType; } @@ -2016,6 +2177,9 @@ Value operator+(const Value& lhs, const Value& rhs) { ret.values.insert(ret.values.begin(), lhs); return ret; } + case Value::Type::DURATION: { + return lhs.getTime() + rhs.getDuration(); + } default: { return Value::kNullBadType; } @@ -2031,6 +2195,9 @@ Value operator+(const Value& lhs, const Value& rhs) { ret.values.insert(ret.values.begin(), lhs); return ret; } + case Value::Type::DURATION: { + return lhs.getDateTime() + rhs.getDuration(); + } default: { return Value::kNullBadType; } @@ -2051,6 +2218,7 @@ Value operator+(const Value& lhs, const Value& rhs) { case Value::Type::DATE: case Value::Type::TIME: case Value::Type::DATETIME: + case Value::Type::DURATION: case Value::Type::VERTEX: case Value::Type::EDGE: case Value::Type::PATH: @@ -2135,6 +2303,16 @@ Value operator+(const Value& lhs, const Value& rhs) { } } } + case Value::Type::DURATION: { + switch (rhs.type()) { + case Value::Type::DURATION: { + return lhs.getDuration() + rhs.getDuration(); + } + default: { + return Value::kNullBadType; + } + } + } default: { return Value::kNullBadType; } @@ -2190,6 +2368,39 @@ Value operator-(const Value& lhs, const Value& rhs) { case Value::Type::DATE: { return lhs.getDate().toInt() - rhs.getDate().toInt(); } + case Value::Type::DURATION: { + return lhs.getDate() - rhs.getDuration(); + } + default: { + return Value::kNullBadType; + } + } + } + case Value::Type::TIME: { + switch (rhs.type()) { + case Value::Type::DURATION: { + return lhs.getTime() - rhs.getDuration(); + } + default: { + return Value::kNullBadType; + } + } + } + case Value::Type::DATETIME: { + switch (rhs.type()) { + case Value::Type::DURATION: { + return lhs.getDateTime() - rhs.getDuration(); + } + default: { + return Value::kNullBadType; + } + } + } + case Value::Type::DURATION: { + switch (rhs.type()) { + case Value::Type::DURATION: { + return lhs.getDuration() - rhs.getDuration(); + } default: { return Value::kNullBadType; } @@ -2396,6 +2607,10 @@ Value operator-(const Value& rhs) { auto val = -rhs.getFloat(); return val; } + case Value::Type::DURATION: { + auto val = -rhs.getDuration(); + return val; + } default: { return Value::kNullBadType; } @@ -2488,6 +2703,10 @@ bool operator<(const Value& lhs, const Value& rhs) { case Value::Type::GEOGRAPHY: { return lhs.getGeography() < rhs.getGeography(); } + case Value::Type::DURATION: { + DLOG(FATAL) << "Duration is not comparable."; + return false; + } case Value::Type::NULLVALUE: case Value::Type::__EMPTY__: { return false; @@ -2575,6 +2794,9 @@ bool operator==(const Value& lhs, const Value& rhs) { case Value::Type::GEOGRAPHY: { return lhs.getGeography() == rhs.getGeography(); } + case Value::Type::DURATION: { + return lhs.getDuration() == rhs.getDuration(); + } case Value::Type::NULLVALUE: case Value::Type::__EMPTY__: { return false; @@ -2584,13 +2806,21 @@ bool operator==(const Value& lhs, const Value& rhs) { return false; } -bool operator!=(const Value& lhs, const Value& rhs) { return !(lhs == rhs); } +bool operator!=(const Value& lhs, const Value& rhs) { + return !(lhs == rhs); +} -bool operator>(const Value& lhs, const Value& rhs) { return rhs < lhs; } +bool operator>(const Value& lhs, const Value& rhs) { + return rhs < lhs; +} -bool operator<=(const Value& lhs, const Value& rhs) { return !(rhs < lhs); } +bool operator<=(const Value& lhs, const Value& rhs) { + return !(rhs < lhs); +} -bool operator>=(const Value& lhs, const Value& rhs) { return !(lhs < rhs); } +bool operator>=(const Value& lhs, const Value& rhs) { + return !(lhs < rhs); +} Value operator&&(const Value& lhs, const Value& rhs) { if (lhs.isNull()) { diff --git a/src/geo/io/wkb/WKBWriter.cpp b/src/geo/io/wkb/WKBWriter.cpp index 232a230d..e17f19ec 100644 --- a/src/geo/io/wkb/WKBWriter.cpp +++ b/src/geo/io/wkb/WKBWriter.cpp @@ -41,7 +41,9 @@ std::string WKBWriter::write(const Geography& geog, ByteOrder byteOrder) { } } -void WKBWriter::writePoint(const Point& point) { writeCoordinate(point.coord); } +void WKBWriter::writePoint(const Point& point) { + writeCoordinate(point.coord); +} void WKBWriter::writeLineString(const LineString& line) { auto coordList = line.coordList; diff --git a/src/sclient/ScanEdgeIter.cpp b/src/sclient/ScanEdgeIter.cpp index 48560beb..b8ca85a7 100644 --- a/src/sclient/ScanEdgeIter.cpp +++ b/src/sclient/ScanEdgeIter.cpp @@ -13,9 +13,13 @@ namespace nebula { ScanEdgeIter::ScanEdgeIter(StorageClient* client, storage::cpp2::ScanEdgeRequest* req, bool hasNext) : client_(client), req_(req), hasNext_(hasNext) {} -bool ScanEdgeIter::hasNext() { return hasNext_; } +bool ScanEdgeIter::hasNext() { + return hasNext_; +} -ScanEdgeIter::~ScanEdgeIter() { delete req_; } +ScanEdgeIter::~ScanEdgeIter() { + delete req_; +} DataSet ScanEdgeIter::next() { if (!hasNext()) { diff --git a/src/thrift/ThriftClientManager.h b/src/thrift/ThriftClientManager.h index ee3af4e6..3f2f20dc 100644 --- a/src/thrift/ThriftClientManager.h +++ b/src/thrift/ThriftClientManager.h @@ -21,7 +21,9 @@ class ThriftClientManager final { bool compatibility = false, uint32_t timeout = 0); - ~ThriftClientManager() { VLOG(3) << "~ThriftClientManager"; } + ~ThriftClientManager() { + VLOG(3) << "~ThriftClientManager"; + } explicit ThriftClientManager(int32_t connTimeoutInMs, bool enableSSL, const std::string& CAPath) : connTimeoutInMs_(connTimeoutInMs), enableSSL_(enableSSL), CAPath_(CAPath) {