Skip to content

Commit

Permalink
Support IP:Port semantically (vesoft-inc#356)
Browse files Browse the repository at this point in the history
* Support IP:Port semantically

* Address @dangleptr's comments

* Fixed stupid compiling error
  • Loading branch information
dutor authored May 13, 2019
1 parent 313b290 commit 6e8e93b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/graph/test/SchemaTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST_F(SchemaTest, metaCommunication) {
ASSERT_NE(nullptr, client);
{
cpp2::ExecutionResponse resp;
std::string query = "ADD HOSTS(\"127.0.0.1:1000\", \"127.0.0.1:1100\")";
std::string query = "ADD HOSTS 127.0.0.1:1000, 127.0.0.1:1100";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
Expand Down Expand Up @@ -243,7 +243,7 @@ TEST_F(SchemaTest, metaCommunication) {
}
{
cpp2::ExecutionResponse resp;
std::string query = "REMOVE HOSTS(\"127.0.0.1:1000\", \"127.0.0.1:1100\")";
std::string query = "REMOVE HOSTS 127.0.0.1:1000, 127.0.0.1:1100";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
Expand Down
6 changes: 4 additions & 2 deletions src/parser/AdminSentences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ std::string ShowSentence::toString() const {
std::string HostList::toString() const {
std::string buf;
buf.reserve(256);
for (auto &host : hostStrs_) {
buf += *host;
for (auto &host : hosts_) {
buf += network::NetworkUtils::intToIPv4(host->first);
buf += ":";
buf += std::to_string(host->second);
buf += ",";
}
buf.resize(buf.size() - 1);
Expand Down
21 changes: 10 additions & 11 deletions src/parser/AdminSentences.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,23 @@ inline std::ostream& operator<<(std::ostream &os, ShowSentence::ShowType type) {

class HostList final {
public:
void addHost(std::string *hoststr) {
hostStrs_.emplace_back(hoststr);
void addHost(HostAddr *addr) {
hosts_.emplace_back(addr);
}

std::string toString() const;

std::vector<HostAddr> toHosts() const {
std::vector<HostAddr> hosts() const {
std::vector<HostAddr> result;
result.resize(hostStrs_.size());
auto getHostAddr = [] (const auto &ptr) {
return network::NetworkUtils::toHostAddr(folly::trimWhitespace(*(ptr.get())));
};
std::transform(hostStrs_.begin(), hostStrs_.end(), result.begin(), getHostAddr);
result.reserve(hosts_.size());
for (auto &host : hosts_) {
result.emplace_back(*host);
}
return result;
}

private:
std::vector<std::unique_ptr<std::string>> hostStrs_;
std::vector<std::unique_ptr<HostAddr>> hosts_;
};


Expand All @@ -90,7 +89,7 @@ class AddHostsSentence final : public Sentence {
}

std::vector<HostAddr> hosts() const {
return hosts_->toHosts();
return hosts_->hosts();
}

std::string toString() const override;
Expand All @@ -111,7 +110,7 @@ class RemoveHostsSentence final : public Sentence {
}

std::vector<HostAddr> hosts() const {
return hosts_->toHosts();
return hosts_->hosts();
}

std::string toString() const override;
Expand Down
29 changes: 21 additions & 8 deletions src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class GraphScanner;
nebula::EdgeList *edge_list;
nebula::ArgumentList *argument_list;
nebula::HostList *host_list;
nebula::HostAddr *host_item;
nebula::SpaceOptList *space_opt_list;
nebula::SpaceOptItem *space_opt_item;
nebula::AlterTagOptList *alter_tag_opt_list;
Expand Down Expand Up @@ -94,7 +95,7 @@ class GraphScanner;

/* token type specification */
%token <boolval> BOOL
%token <intval> INTEGER
%token <intval> INTEGER IPV4
%token <doubleval> DOUBLE
%token <strval> STRING VARIABLE LABEL

Expand Down Expand Up @@ -130,12 +131,13 @@ class GraphScanner;
%type <update_item> update_item
%type <edge_list> edge_list
%type <host_list> host_list
%type <host_item> host_item
%type <space_opt_list> space_opt_list
%type <space_opt_item> space_opt_item
%type <alter_tag_opt_list> alter_tag_opt_list
%type <alter_tag_opt_item> alter_tag_opt_item

%type <intval> ttl_spec
%type <intval> ttl_spec port

%type <colspec> column_spec
%type <colspeclist> column_spec_list
Expand Down Expand Up @@ -850,27 +852,27 @@ show_sentence
;

add_hosts_sentence
: KW_ADD KW_HOSTS L_PAREN host_list R_PAREN {
: KW_ADD KW_HOSTS host_list {
auto sentence = new AddHostsSentence();
sentence->setHosts($4);
sentence->setHosts($3);
$$ = sentence;
}
;

remove_hosts_sentence
: KW_REMOVE KW_HOSTS L_PAREN host_list R_PAREN {
: KW_REMOVE KW_HOSTS host_list {
auto sentence = new RemoveHostsSentence();
sentence->setHosts($4);
sentence->setHosts($3);
$$ = sentence;
}
;

host_list
: STRING {
: host_item {
$$ = new HostList();
$$->addHost($1);
}
| host_list COMMA STRING {
| host_list COMMA host_item {
$$ = $1;
$$->addHost($3);
}
Expand All @@ -879,6 +881,17 @@ host_list
}
;

host_item
: IPV4 COLON port {
$$ = new nebula::HostAddr();
$$->first = $1;
$$->second = $3;
}
/* TODO(dutor) Support hostname and IPv6 */
;

port : INTEGER { $$ = $1; }

create_space_sentence
: KW_CREATE KW_SPACE LABEL L_PAREN space_opt_list R_PAREN {
auto sentence = new CreateSpaceSentence($3);
Expand Down
9 changes: 9 additions & 0 deletions src/parser/scanner.lex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ LABEL ([a-zA-Z][_a-zA-Z0-9]*)
DEC ([0-9])
HEX ([0-9a-fA-F])
OCT ([0-7])
IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])


%%
Expand Down Expand Up @@ -226,6 +227,14 @@ OCT ([0-7])
}
return TokenType::LABEL;
}
{IP_OCTET}(\.{IP_OCTET}){3} {
uint32_t octets[4] = {0};
sscanf(yytext, "%i.%i.%i.%i", &octets[3], &octets[2], &octets[1], &octets[0]);
// The bytes order conforms to the one used in NetworkUtils
uint32_t ipv4 = (octets[3] << 24) | (octets[2] << 16) | (octets[1] << 8) | octets[0];
yylval->intval = ipv4;
return TokenType::IPV4;
}
0[Xx]{HEX}+ {
int64_t val = 0;
sscanf(yytext, "%lx", &val);
Expand Down
10 changes: 9 additions & 1 deletion src/parser/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ add_executable(
ParserTest.cpp
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:fs_obj>
)
nebula_link_libraries(
parser_test
Expand All @@ -17,6 +19,8 @@ add_executable(
ScannerTest.cpp
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:fs_obj>
)
nebula_link_libraries(
scanner_test
Expand All @@ -33,6 +37,8 @@ add_executable(
ExpressionTest.cpp
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:fs_obj>
)
nebula_link_libraries(
expression_test
Expand All @@ -45,8 +51,10 @@ nebula_add_test(expression_test)
add_executable(
expression_encode_decode_bm
ExpressionEncodeDecodeBenchmark.cpp
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:fs_obj>
)
nebula_link_libraries(
expression_encode_decode_bm
Expand Down
4 changes: 2 additions & 2 deletions src/parser/test/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ TEST(Parser, Find) {
TEST(Parser, AdminOperation) {
{
GQLParser parser;
std::string query = "add hosts (\"127.0.0.1:1000\", \"127.0.0.1:9000\")";
std::string query = "add hosts 127.0.0.1:1000, 127.0.0.1:9000";
auto result = parser.parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
Expand All @@ -380,7 +380,7 @@ TEST(Parser, AdminOperation) {
}
{
GQLParser parser;
std::string query = "remove hosts (\"127.0.0.1:1000\", \"127.0.0.1:9000\")";
std::string query = "remove hosts 127.0.0.1:1000, 127.0.0.1:9000";
auto result = parser.parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/test/ScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ TEST(Scanner, Basic) {
CHECK_SEMANTIC_VALUE("+123.456", TokenType::DOUBLE, 123.456),
CHECK_SEMANTIC_VALUE("-123.456", TokenType::DOUBLE, -123.456),

CHECK_SEMANTIC_VALUE("127.0.0.1", TokenType::IPV4, 0x7F000001),

CHECK_SEMANTIC_VALUE("\"Hello\"", TokenType::STRING, "Hello"),
CHECK_SEMANTIC_VALUE("\"Hello\\\\\"", TokenType::STRING, "Hello\\"),
CHECK_SEMANTIC_VALUE("\"He\\nllo\"", TokenType::STRING, "He\nllo"),
Expand Down

0 comments on commit 6e8e93b

Please sign in to comment.