Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update interface. #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/common/datatypes/CommonCpp2Ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Point;
struct LineString;
struct Polygon;
struct Geography;
struct Duration;
} // namespace nebula

namespace apache::thrift {
Expand All @@ -52,5 +53,6 @@ SPECIALIZE_CPP2OPS(nebula::Point);
SPECIALIZE_CPP2OPS(nebula::LineString);
SPECIALIZE_CPP2OPS(nebula::Polygon);
SPECIALIZE_CPP2OPS(nebula::Geography);
SPECIALIZE_CPP2OPS(nebula::Duration);

} // namespace apache::thrift
125 changes: 125 additions & 0 deletions include/common/datatypes/Duration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#pragma once

#include <sstream>

#include "common/time/Constants.h"

namespace nebula {

// Duration equals to months + seconds + microseconds
// The base between months and days is not fixed, so we store years and months
// separately.
struct Duration {
// day + hours + minutes + seconds + microseconds
int64_t seconds;
int32_t microseconds;
// years + months
int32_t months;

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 monthsInYear() const { return months % 12; }

int64_t days() const { return seconds / time::kSecondsOfDay; }

int64_t hours() const { return seconds % time::kSecondsOfDay / time::kSecondsOfHour; }

int64_t minutes() const { return seconds % time::kSecondsOfHour / time::kSecondsOfMinute; }

int64_t secondsInMinute() const { return seconds % time::kSecondsOfMinute; }

int64_t microsecondsInSecond() const { return 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);
}

Duration operator-(const Duration& rhs) const {
return Duration(months - rhs.months, seconds - rhs.seconds, microseconds - rhs.microseconds);
}

Duration& addYears(int32_t y) {
months += y * 12;
return *this;
}

Duration& addQuarters(int32_t q) {
months += q * 3;
return *this;
}

Duration& addMonths(int32_t m) {
months += m;
return *this;
}

Duration& addWeeks(int32_t w) {
seconds += (w * 7 * time::kSecondsOfDay);
return *this;
}

Duration& addDays(int64_t d) {
seconds += d * time::kSecondsOfDay;
return *this;
}

Duration& addHours(int64_t h) {
seconds += h * time::kSecondsOfHour;
return *this;
}

Duration& addMinutes(int64_t minutes) {
seconds += minutes * time::kSecondsOfMinute;
return *this;
}

Duration& addSeconds(int64_t s) {
seconds += s;
return *this;
}

Duration& addMilliseconds(int64_t ms) {
seconds += ms / 1000;
microseconds += ((ms % 1000) * 1000);
return *this;
}

Duration& addMicroseconds(int32_t us) {
microseconds += us;
return *this;
}

// can't compare
bool operator<(const Duration& rhs) const = delete;

bool operator==(const Duration& rhs) const {
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);
}
};

} // namespace nebula

namespace std {

// Inject a customized hash function
template <>
struct hash<nebula::Duration> {
std::size_t operator()(const nebula::Duration& d) const noexcept;
};

} // namespace std
200 changes: 200 additions & 0 deletions include/common/datatypes/DurationOps-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#pragma once

#include <thrift/lib/cpp2/GeneratedCodeHelper.h>
#include <thrift/lib/cpp2/gen/module_types_tcc.h>
#include <thrift/lib/cpp2/protocol/ProtocolReaderStructReadState.h>

#include "common/datatypes/CommonCpp2Ops.h"
#include "common/datatypes/Duration.h"

namespace apache {
namespace thrift {

/**************************************
*
* Ops for class Duration
*
*************************************/
namespace detail {

template <>
struct TccStructTraits<nebula::Duration> {
static void translateFieldName(MAYBE_UNUSED folly::StringPiece _fname,
MAYBE_UNUSED int16_t& fid,
MAYBE_UNUSED apache::thrift::protocol::TType& _ftype) {
if (_fname == "seconds") {
fid = 1;
_ftype = apache::thrift::protocol::T_I64;
} else if (_fname == "microseconds") {
fid = 2;
_ftype = apache::thrift::protocol::T_I32;
} else if (_fname == "months") {
fid = 3;
_ftype = apache::thrift::protocol::T_I32;
}
}
};

} // namespace detail

inline constexpr protocol::TType Cpp2Ops<nebula::Duration>::thriftType() {
return apache::thrift::protocol::T_STRUCT;
}

template <class Protocol>
uint32_t Cpp2Ops<nebula::Duration>::write(Protocol* proto, nebula::Duration const* obj) {
uint32_t xfer = 0;
xfer += proto->writeStructBegin("Duration");

xfer += proto->writeFieldBegin("seconds", apache::thrift::protocol::T_I64, 1);
xfer += detail::pm::protocol_methods<type_class::integral, int64_t>::write(*proto, obj->seconds);
xfer += proto->writeFieldEnd();

xfer += proto->writeFieldBegin("microseconds", apache::thrift::protocol::T_I32, 2);
xfer +=
detail::pm::protocol_methods<type_class::integral, int32_t>::write(*proto, obj->microseconds);
xfer += proto->writeFieldEnd();

xfer += proto->writeFieldBegin("months", apache::thrift::protocol::T_I32, 3);
xfer += detail::pm::protocol_methods<type_class::integral, int32_t>::write(*proto, obj->months);
xfer += proto->writeFieldEnd();

xfer += proto->writeFieldStop();
xfer += proto->writeStructEnd();

return xfer;
}

template <class Protocol>
void Cpp2Ops<nebula::Duration>::read(Protocol* proto, nebula::Duration* obj) {
detail::ProtocolReaderStructReadState<Protocol> readState;

readState.readStructBegin(proto);

using apache::thrift::protocol::TProtocolException;

if (UNLIKELY(!readState.advanceToNextField(proto, 0, 1, protocol::T_I64))) {
goto _loop;
}

_readField_seconds : {
detail::pm::protocol_methods<type_class::integral, int64_t>::read(*proto, obj->seconds);
}

if (UNLIKELY(!readState.advanceToNextField(proto, 1, 2, protocol::T_I32))) {
goto _loop;
}

_readField_microseconds : {
detail::pm::protocol_methods<type_class::integral, int32_t>::read(*proto, obj->microseconds);
}

if (UNLIKELY(!readState.advanceToNextField(proto, 2, 3, protocol::T_I32))) {
goto _loop;
}

_readField_months : {
detail::pm::protocol_methods<type_class::integral, int32_t>::read(*proto, obj->months);
}

if (UNLIKELY(!readState.advanceToNextField(proto, 3, 0, protocol::T_STOP))) {
goto _loop;
}

_end:
readState.readStructEnd(proto);

return;

_loop:
if (readState.fieldType == apache::thrift::protocol::T_STOP) {
goto _end;
}
if (proto->kUsesFieldNames()) {
detail::TccStructTraits<nebula::Duration>::translateFieldName(
readState.fieldName(), readState.fieldId, readState.fieldType);
}

switch (readState.fieldId) {
case 1: {
if (LIKELY(readState.fieldType == apache::thrift::protocol::T_I64)) {
goto _readField_seconds;
} else {
goto _skip;
}
}
case 2: {
if (LIKELY(readState.fieldType == apache::thrift::protocol::T_I32)) {
goto _readField_microseconds;
} else {
goto _skip;
}
}
case 3: {
if (LIKELY(readState.fieldType == apache::thrift::protocol::T_I32)) {
goto _readField_months;
} else {
goto _skip;
}
}
default: {
_skip:
proto->skip(readState.fieldType);
readState.readFieldEnd(proto);
readState.readFieldBeginNoInline(proto);
goto _loop;
}
}
}

template <class Protocol>
uint32_t Cpp2Ops<nebula::Duration>::serializedSize(Protocol const* proto,
nebula::Duration const* obj) {
uint32_t xfer = 0;
xfer += proto->serializedStructSize("Duration");

xfer += proto->serializedFieldSize("seconds", apache::thrift::protocol::T_I64, 1);
xfer += detail::pm::protocol_methods<type_class::integral, int64_t>::serializedSize<false>(
*proto, obj->seconds);

xfer += proto->serializedFieldSize("microseconds", apache::thrift::protocol::T_I32, 2);
xfer += detail::pm::protocol_methods<type_class::integral, int32_t>::serializedSize<false>(
*proto, obj->microseconds);

xfer += proto->serializedFieldSize("months", apache::thrift::protocol::T_I32, 3);
xfer += detail::pm::protocol_methods<type_class::integral, int32_t>::serializedSize<false>(
*proto, obj->months);

xfer += proto->serializedSizeStop();
return xfer;
}

template <class Protocol>
uint32_t Cpp2Ops<nebula::Duration>::serializedSizeZC(Protocol const* proto,
nebula::Duration const* obj) {
uint32_t xfer = 0;
xfer += proto->serializedStructSize("Duration");

xfer += proto->serializedFieldSize("seconds", apache::thrift::protocol::T_I64, 1);
xfer += detail::pm::protocol_methods<type_class::integral, int64_t>::serializedSize<false>(
*proto, obj->seconds);

xfer += proto->serializedFieldSize("microseconds", apache::thrift::protocol::T_I32, 2);
xfer += detail::pm::protocol_methods<type_class::integral, int32_t>::serializedSize<false>(
*proto, obj->microseconds);

xfer += proto->serializedFieldSize("months", apache::thrift::protocol::T_I32, 3);
xfer += detail::pm::protocol_methods<type_class::integral, int32_t>::serializedSize<false>(
*proto, obj->months);

xfer += proto->serializedSizeStop();
return xfer;
}

} // namespace thrift
} // namespace apache
19 changes: 19 additions & 0 deletions include/common/time/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#pragma once

namespace nebula {
namespace time {

static constexpr int kDayOfLeapYear = 366;
static constexpr int kDayOfCommonYear = 365;

static constexpr int64_t kSecondsOfMinute = 60;
static constexpr int64_t kSecondsOfHour = 60 * kSecondsOfMinute;
static constexpr int64_t kSecondsOfDay = 24 * kSecondsOfHour;

} // namespace time
} // namespace nebula
25 changes: 25 additions & 0 deletions src/datatypes/Duration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "common/datatypes/Duration.h"

#include <folly/String.h>
#include <folly/hash/Hash.h>

#include <sstream>

namespace nebula {} // namespace nebula

namespace std {

// Inject a customized hash function
std::size_t hash<nebula::Duration>::operator()(const nebula::Duration& d) const noexcept {
size_t hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(&d.months), sizeof(d.months));
hv = folly::hash::fnv64_buf(reinterpret_cast<const void*>(d.seconds), sizeof(d.seconds), hv);
return folly::hash::fnv64_buf(
reinterpret_cast<const void*>(d.microseconds), sizeof(d.microseconds), hv);
}

} // namespace std
Loading