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 thrift files #63

Merged
merged 3 commits into from
Nov 11, 2021
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@
_build
build
modules

# IDE
.vscode/
8 changes: 8 additions & 0 deletions include/common/datatypes/CommonCpp2Ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ struct Map;
struct Set;
struct List;
struct DataSet;
struct Coordinate;
struct Point;
struct LineString;
struct Polygon;
struct Geography;
} // namespace nebula

Expand All @@ -43,6 +47,10 @@ SPECIALIZE_CPP2OPS(nebula::Map);
SPECIALIZE_CPP2OPS(nebula::Set);
SPECIALIZE_CPP2OPS(nebula::List);
SPECIALIZE_CPP2OPS(nebula::DataSet);
SPECIALIZE_CPP2OPS(nebula::Coordinate);
SPECIALIZE_CPP2OPS(nebula::Point);
SPECIALIZE_CPP2OPS(nebula::LineString);
SPECIALIZE_CPP2OPS(nebula::Polygon);
SPECIALIZE_CPP2OPS(nebula::Geography);

} // namespace apache::thrift
5 changes: 1 addition & 4 deletions include/common/datatypes/EdgeOps-inl.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/*
*
* 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.
*
*/

#pragma once
Expand Down
126 changes: 110 additions & 16 deletions include/common/datatypes/Geography.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#pragma once

#include "common/datatypes/Value.h"
#include <vector>
#include <variant>
#include <string>

// Do not include <s2/s2polygon.h> here, it will indirectly includes a header file which defines a
// enum `BEGIN`(not enum class). While Geography.h is indirectly included by parser.yy, which has a
Expand All @@ -15,6 +18,15 @@ class S2Polygon;

namespace nebula {

enum class GeoShape : uint32_t {
UNKNOWN = 0, // illegal
POINT = 1,
LINESTRING = 2,
POLYGON = 3,
};

std::ostream& operator<<(std::ostream& os, const GeoShape& shape);

// clang-format off
/*
static const std::unordered_map<GeoShape, S2Region> kShapeTypeToS2Region = {
Expand All @@ -26,33 +38,115 @@ static const std::unordered_map<GeoShape, S2Region> kShapeTypeToS2Region = {
*/
// clang-format on

// Do not construct a S2 object when constructing Geography. It's expensive.
// We just construct S2 when doing computation.
struct Geography {
std::string wkb; // TODO(jie) Is it better to store Geometry* or S2Region* here?
struct Coordinate {
double x, y;

Coordinate() = default;
Coordinate(double lng, double lat) : x(lng), y(lat) {}

Geography() = default;
void clear() {
x = 0.0;
y = 0.0;
}
void __clear() { clear(); }

std::string toString() const { return wkb; }
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 {
if (x != rhs.x) {
return x < rhs.x;
}
if (y != rhs.y) {
return y < rhs.y;
}
return false;
}
};

void clear() { wkb.clear(); }
struct Point {
Coordinate coord;

Point() = default;
explicit Point(const Coordinate& v) : coord(v) {}
explicit Point(Coordinate&& v) : coord(std::move(v)) {}

void clear() { coord.clear(); }
void __clear() { clear(); }

bool operator==(const Geography& rhs) const { return wkb == rhs.wkb; }
bool operator==(const Point& rhs) const { return coord == rhs.coord; }
bool operator<(const Point& rhs) const { return coord < rhs.coord; }
};

bool operator!=(const Geography& rhs) const { return !(wkb == rhs.wkb); }
struct LineString {
std::vector<Coordinate> coordList;

bool operator<(const Geography& rhs) const { return wkb < rhs.wkb; }
LineString() = default;
explicit LineString(const std::vector<Coordinate>& v) : coordList(v) {}
explicit LineString(std::vector<Coordinate>&& v) : coordList(std::move(v)) {}

private:
explicit Geography(const std::string& bytes) {
// TODO(jie): Must ensure the bytes is valid
wkb = bytes;
}
uint32_t numCoord() const { return coordList.size(); }

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; }
};

struct Polygon {
std::vector<std::vector<Coordinate>> coordListList;

Polygon() = default;
explicit Polygon(const std::vector<std::vector<Coordinate>>& v) : coordListList(v) {}
explicit Polygon(std::vector<std::vector<Coordinate>>&& v) : coordListList(std::move(v)) {}

uint32_t numCoordList() const { return coordListList.size(); }

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; }
};

struct Geography {
std::variant<Point, LineString, Polygon> geo_;

Geography() {}
Geography(const Point& v) : geo_(v) {} // NOLINT
Geography(Point&& v) : geo_(std::move(v)) {} // NOLINT
Geography(const LineString& v) : geo_(v) {} // NOLINT
Geography(LineString&& v) : geo_(std::move(v)) {} // NOLINT
Geography(const Polygon& v) : geo_(v) {} // NOLINT
Geography(Polygon&& v) : geo_(std::move(v)) {} // NOLINT

GeoShape shape() const;

const Point& point() const;
const LineString& lineString() const;
const Polygon& polygon() const;

Point& mutablePoint();
LineString& mutableLineString();
Polygon& mutablePolygon();

std::string asWKT() const;

std::string asWKB() const;

std::string toString() const { return asWKT(); }

void clear() { geo_.~variant(); }

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.wkb; }
inline std::ostream& operator<<(std::ostream& os, const Geography& g) { return os << g.toString(); }

} // namespace nebula

Expand Down
Loading