From 93e041cf8966391e981d04e2b6c7115f503feb84 Mon Sep 17 00:00:00 2001 From: steppenwolfyuetong Date: Wed, 16 Jan 2019 14:09:04 +0800 Subject: [PATCH] Implement show hosts (#80) --- src/graph/CMakeLists.txt | 1 + src/graph/Executor.cpp | 5 ++++ src/graph/ShowExecutor.cpp | 47 ++++++++++++++++++++++++++++++++ src/graph/ShowExecutor.h | 39 ++++++++++++++++++++++++++ src/parser/CMakeLists.txt | 1 + src/parser/Sentence.h | 1 + src/parser/SequentialSentences.h | 1 + src/parser/ShowSentences.cpp | 26 ++++++++++++++++++ src/parser/ShowSentences.h | 44 ++++++++++++++++++++++++++++++ src/parser/parser.yy | 11 +++++++- src/parser/scanner.lex | 5 ++++ src/parser/test/ScannerTest.cpp | 23 +++++++++++++++- 12 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 src/graph/ShowExecutor.cpp create mode 100644 src/graph/ShowExecutor.h create mode 100644 src/parser/ShowSentences.cpp create mode 100644 src/parser/ShowSentences.h diff --git a/src/graph/CMakeLists.txt b/src/graph/CMakeLists.txt index bb72a83e656..dd3f3559808 100644 --- a/src/graph/CMakeLists.txt +++ b/src/graph/CMakeLists.txt @@ -22,6 +22,7 @@ add_library( DescribeEdgeExecutor.cpp InsertVertexExecutor.cpp InsertEdgeExecutor.cpp + ShowExecutor.cpp mock/PropertiesSchema.cpp mock/EdgeSchema.cpp mock/TagSchema.cpp diff --git a/src/graph/Executor.cpp b/src/graph/Executor.cpp index e4184e45f07..922380e4be2 100644 --- a/src/graph/Executor.cpp +++ b/src/graph/Executor.cpp @@ -9,6 +9,7 @@ #include "parser/TraverseSentences.h" #include "parser/MutateSentences.h" #include "parser/MaintainSentences.h" +#include "parser/ShowSentences.h" #include "graph/GoExecutor.h" #include "graph/UseExecutor.h" #include "graph/PipeExecutor.h" @@ -20,6 +21,7 @@ #include "graph/DescribeEdgeExecutor.h" #include "graph/InsertVertexExecutor.h" #include "graph/InsertEdgeExecutor.h" +#include "graph/ShowExecutor.h" namespace nebula { namespace graph { @@ -61,6 +63,9 @@ std::unique_ptr Executor::makeExecutor(Sentence *sentence) { case Sentence::Kind::kInsertEdge: executor = std::make_unique(sentence, ectx()); break; + case Sentence::Kind::kShow: + executor = std::make_unique(sentence, ectx()); + break; case Sentence::Kind::kUnknown: LOG(FATAL) << "Sentence kind unknown"; break; diff --git a/src/graph/ShowExecutor.cpp b/src/graph/ShowExecutor.cpp new file mode 100644 index 00000000000..f50867215ad --- /dev/null +++ b/src/graph/ShowExecutor.cpp @@ -0,0 +1,47 @@ +/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved + * + * This source code is licensed under Apache 2.0 License + * (found in the LICENSE.Apache file in the root directory) + */ + +#include "base/Base.h" +#include "graph/ShowExecutor.h" +#include "storage/StorageServiceHandler.h" + + +namespace nebula { +namespace graph { + + +ShowExecutor::ShowExecutor(Sentence *sentence, + ExecutionContext *ectx) : Executor(ectx) { + sentence_ = static_cast(sentence); +} + + +Status ShowExecutor::prepare() { + return Status::OK(); +} + + +void ShowExecutor::execute() { + // TODO(YT) when StorageClient fininshed, then implement this interface + auto show_kind = sentence_->showKind(); + switch (show_kind) { + case (ShowKind::kShowHosts): + DCHECK(onFinish_); + onFinish_(); + break; + default: + LOG(FATAL) << "Show Sentence kind illegal: " < resp_; +}; + + +} // namespace graph +} // namespace nebula + +#endif // GRAPH_SHOWEXECUTOR_H_ diff --git a/src/parser/CMakeLists.txt b/src/parser/CMakeLists.txt index 906af6dcd9f..478f2f82144 100644 --- a/src/parser/CMakeLists.txt +++ b/src/parser/CMakeLists.txt @@ -19,6 +19,7 @@ add_library( MaintainSentences.cpp MutateSentences.cpp TraverseSentences.cpp + ShowSentences.cpp ) add_dependencies(parser_obj base_obj) diff --git a/src/parser/Sentence.h b/src/parser/Sentence.h index 50a38ab2c95..b7be586bcfd 100644 --- a/src/parser/Sentence.h +++ b/src/parser/Sentence.h @@ -32,6 +32,7 @@ class Sentence { kDescribeEdge, kInsertVertex, kInsertEdge, + kShow, }; Kind kind() const { diff --git a/src/parser/SequentialSentences.h b/src/parser/SequentialSentences.h index 497a2af0bfa..0e817532323 100644 --- a/src/parser/SequentialSentences.h +++ b/src/parser/SequentialSentences.h @@ -10,6 +10,7 @@ #include "parser/MaintainSentences.h" #include "parser/TraverseSentences.h" #include "parser/MutateSentences.h" +#include "parser/ShowSentences.h" namespace nebula { diff --git a/src/parser/ShowSentences.cpp b/src/parser/ShowSentences.cpp new file mode 100644 index 00000000000..18c122c24e5 --- /dev/null +++ b/src/parser/ShowSentences.cpp @@ -0,0 +1,26 @@ +/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved + * + * This source code is licensed under Apache 2.0 License + * (found in the LICENSE.Apache file in the root directory) + */ + +#include "base/Base.h" +#include "parser/ShowSentences.h" + +namespace nebula { + +std::string ShowSentence::toString() const { + std::string buf; + switch (showKind_) { + case ShowKind::kShowHosts: + buf = "SHOW HOSTS"; + break; + case ShowKind::kUnknown: + default: + LOG(FATAL) << "Show Sentence kind illegal: " << showKind_; + break; + } + return buf; +} + +} // namespace nebula diff --git a/src/parser/ShowSentences.h b/src/parser/ShowSentences.h new file mode 100644 index 00000000000..201a37e79a5 --- /dev/null +++ b/src/parser/ShowSentences.h @@ -0,0 +1,44 @@ +/* Copyright (c) 2018 - present, VE Software Inc. All rights reserved + * + * This source code is licensed under Apache 2.0 License + * (found in the LICENSE.Apache file in the root directory) + */ +#ifndef PARSER_SHOWSENTENCES_H_ +#define PARSER_SHOWSENTENCES_H_ + +#include "base/Base.h" +#include "parser/Clauses.h" +#include "parser/Sentence.h" + +namespace nebula { + +enum class ShowKind : uint32_t { + kUnknown, + kShowHosts, +}; + + +class ShowSentence final : public Sentence { +public: + explicit ShowSentence(ShowKind sKind) { + kind_ = Kind::kShow; + showKind_ = std::move(sKind); + } + + std::string toString() const override; + + ShowKind showKind() const { + return showKind_; + } + +private: + ShowKind showKind_{ShowKind::kUnknown}; +}; + +inline std::ostream& operator<<(std::ostream &os, ShowKind kind) { + return os << static_cast(kind); +} + +} // namespace nebula + +#endif // PARSER_SHOWSENTENCES_H_ diff --git a/src/parser/parser.yy b/src/parser/parser.yy index 7b27c639365..d3fc63b1b57 100644 --- a/src/parser/parser.yy +++ b/src/parser/parser.yy @@ -58,7 +58,7 @@ class GraphScanner; %token KW_MATCH KW_INSERT KW_VALUES KW_YIELD KW_RETURN KW_DEFINE KW_VERTEX KW_TTL %token KW_EDGE KW_UPDATE KW_STEPS KW_OVER KW_UPTO KW_REVERSELY KW_SPACE %token KW_INT KW_BIGINT KW_DOUBLE KW_STRING KW_BOOL KW_TAG KW_UNION KW_INTERSECT KW_MINUS -%token KW_NO KW_OVERWRITE KW_IN KW_DESCRIBE +%token KW_NO KW_OVERWRITE KW_IN KW_DESCRIBE KW_SHOW KW_HOSTS /* symbols */ %token L_PAREN R_PAREN L_BRACKET R_BRACKET L_BRACE R_BRACE COMMA %token PIPE OR AND LT LE GT GE EQ NE ADD SUB MUL DIV MOD NOT NEG ASSIGN @@ -102,6 +102,7 @@ class GraphScanner; %type traverse_sentence set_sentence piped_sentence assignment_sentence %type maintainance_sentence insert_vertex_sentence insert_edge_sentence %type mutate_sentence update_vertex_sentence update_edge_sentence +%type show_sentence %type sentence %type sentences @@ -663,6 +664,13 @@ update_edge_sentence } ; +show_sentence + : KW_SHOW KW_HOSTS { + auto sentence = new ShowSentence(ShowKind::kShowHosts); + $$ = sentence; + } + ; + mutate_sentence : insert_vertex_sentence {} | insert_edge_sentence {} @@ -677,6 +685,7 @@ maintainance_sentence | alter_edge_sentence {} | describe_tag_sentence {} | describe_edge_sentence {} + | show_sentence {} ; sentence diff --git a/src/parser/scanner.lex b/src/parser/scanner.lex index 29a404e6b1f..89f8ae16982 100644 --- a/src/parser/scanner.lex +++ b/src/parser/scanner.lex @@ -58,6 +58,9 @@ NO ([Nn][Oo]) OVERWRITE ([Oo][Vv][Ee][Rr][Ww][Rr][Ii][Tt][Ee]) TRUE ([Tt][Rr][Uu][Ee]) FALSE ([Ff][Aa][Ll][Ss][Ee]) +SHOW ([Ss][Hh][Oo][Ww]) +HOSTS ([Hh][Oo][Ss][Tt][Ss]) + LABEL ([a-zA-Z][_a-zA-Z0-9]*) DEC ([0-9]) @@ -108,6 +111,8 @@ OCT ([0-7]) {OVERWRITE} { return TokenType::KW_OVERWRITE; } {TRUE} { yylval->boolval = true; return TokenType::BOOL; } {FALSE} { yylval->boolval = false; return TokenType::BOOL; } +{SHOW} { return TokenType::KW_SHOW; } +{HOSTS} { return TokenType::KW_HOSTS; } "." { return TokenType::DOT; } "," { return TokenType::COMMA; } diff --git a/src/parser/test/ScannerTest.cpp b/src/parser/test/ScannerTest.cpp index ec9e8e78660..19d7cf3ce34 100644 --- a/src/parser/test/ScannerTest.cpp +++ b/src/parser/test/ScannerTest.cpp @@ -203,6 +203,12 @@ TEST(Scanner, Basic) { CHECK_SEMANTIC_TYPE("intersect", TokenType::KW_INTERSECT), CHECK_SEMANTIC_TYPE("MINUS", TokenType::KW_MINUS), CHECK_SEMANTIC_TYPE("minus", TokenType::KW_MINUS), + CHECK_SEMANTIC_TYPE("SHOW", TokenType::KW_SHOW), + CHECK_SEMANTIC_TYPE("show", TokenType::KW_SHOW), + CHECK_SEMANTIC_TYPE("Show", TokenType::KW_SHOW), + CHECK_SEMANTIC_TYPE("HOSTS", TokenType::KW_HOSTS), + CHECK_SEMANTIC_TYPE("hosts", TokenType::KW_HOSTS), + CHECK_SEMANTIC_TYPE("Hosts", TokenType::KW_HOSTS), CHECK_SEMANTIC_TYPE("_type", TokenType::TYPE_PROP), CHECK_SEMANTIC_TYPE("_id", TokenType::ID_PROP), @@ -233,11 +239,26 @@ TEST(Scanner, Basic) { CHECK_SEMANTIC_VALUE("\"Hello\"", TokenType::STRING, "Hello"), CHECK_SEMANTIC_VALUE("\"Hello\\\\\"", TokenType::STRING, "Hello\\"), - CHECK_SEMANTIC_VALUE("\"Hell\\o\"", TokenType::STRING, "Hello"), CHECK_SEMANTIC_VALUE("\"He\\nllo\"", TokenType::STRING, "He\nllo"), CHECK_SEMANTIC_VALUE("\"He\\\nllo\"", TokenType::STRING, "He\nllo"), CHECK_SEMANTIC_VALUE("\"\\\"Hello\\\"\"", TokenType::STRING, "\"Hello\""), + + // escape Normal character + CHECK_SEMANTIC_VALUE("\"Hell\\o\"", TokenType::STRING, "Hello"), + CHECK_SEMANTIC_VALUE("\"Hell\\\\o\"", TokenType::STRING, "Hell\\o"), + CHECK_SEMANTIC_VALUE("\"Hell\\\\\\o\"", TokenType::STRING, "Hell\\o"), CHECK_SEMANTIC_VALUE("\"\\110ello\"", TokenType::STRING, "Hello"), + CHECK_SEMANTIC_VALUE("\"\110ello\"", TokenType::STRING, "Hello"), + + CHECK_SEMANTIC_VALUE("\"\110 \"", TokenType::STRING, "H "), + CHECK_SEMANTIC_VALUE("\"\\110 \"", TokenType::STRING, "H "), + CHECK_SEMANTIC_VALUE("\"\\\110 \"", TokenType::STRING, "H "), + CHECK_SEMANTIC_VALUE("\"\\\\110 \"", TokenType::STRING, "\\110 "), + CHECK_SEMANTIC_VALUE("\"\\\\\110 \"", TokenType::STRING, "\\H "), + CHECK_SEMANTIC_VALUE("\"\\\\\\110 \"", TokenType::STRING, "\\H "), + CHECK_SEMANTIC_VALUE("\"\\\\\\\110 \"", TokenType::STRING, "\\H "), + CHECK_SEMANTIC_VALUE("\"\\\\\\\\110 \"", TokenType::STRING, "\\\\110 "), + CHECK_SEMANTIC_VALUE("\"\\\\\\\\\110 \"", TokenType::STRING, "\\\\H "), }; #undef CHECK_SEMANTIC_TYPE #undef CHECK_SEMANTIC_VALUE