Skip to content
Snippets Groups Projects
Commit abb7fe21 authored by yaphet's avatar yaphet Committed by laura-ding
Browse files

[Query] Support Description about Graph Space (#388)

* support describe space

* support describe space

* support desc space

* address dangleptr's comment

* address dutor's comment

* using async

* address dangleptr's comment

* fix

* fix copyright

* address dangleptr's comment

* fix

* fix
parent 78146105
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,7 @@ add_library(
RemoveHostsExecutor.cpp
CreateSpaceExecutor.cpp
DropSpaceExecutor.cpp
DescribeSpaceExecutor.cpp
ShowExecutor.cpp
YieldExecutor.cpp
)
......
......@@ -28,28 +28,25 @@ void DescribeEdgeExecutor::execute() {
auto edgeType = ectx()->schemaManager()->toEdgeType(spaceId, *name);
auto schema = ectx()->schemaManager()->getEdgeSchema(spaceId, edgeType);
resp_ = std::make_unique<cpp2::ExecutionResponse>();
do {
if (schema == nullptr) {
onError_(Status::Error("Schema not found for edge `%s'", name->c_str()));
return;
}
std::vector<std::string> header{"Field", "Type"};
resp_->set_column_names(std::move(header));
uint32_t numFields = schema->getNumFields();
std::vector<cpp2::RowValue> rows;
for (uint32_t index = 0; index < numFields; index++) {
std::vector<cpp2::ColumnValue> row;
row.resize(2);
row[0].set_str(schema->getFieldName(index));
row[1].set_str(valueTypeToString(schema->getFieldType(index)));
rows.emplace_back();
rows.back().set_columns(std::move(row));
}
resp_->set_rows(std::move(rows));
} while (false);
if (schema == nullptr) {
onError_(Status::Error("Schema not found for edge `%s'", name->c_str()));
return;
}
std::vector<std::string> header{"Field", "Type"};
resp_->set_column_names(std::move(header));
uint32_t numFields = schema->getNumFields();
std::vector<cpp2::RowValue> rows;
for (uint32_t index = 0; index < numFields; index++) {
std::vector<cpp2::ColumnValue> row;
row.resize(2);
row[0].set_str(schema->getFieldName(index));
row[1].set_str(valueTypeToString(schema->getFieldType(index)));
rows.emplace_back();
rows.back().set_columns(std::move(row));
}
resp_->set_rows(std::move(rows));
DCHECK(onFinish_);
onFinish_();
}
......
/* Copyright (c) 2019 - 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/DescribeSpaceExecutor.h"
#include "meta/SchemaManager.h"
namespace nebula {
namespace graph {
DescribeSpaceExecutor::DescribeSpaceExecutor(Sentence *sentence,
ExecutionContext *ectx) : Executor(ectx) {
sentence_ = static_cast<DescribeSpaceSentence*>(sentence);
}
Status DescribeSpaceExecutor::prepare() {
return Status::OK();;
}
void DescribeSpaceExecutor::execute() {
auto *name = sentence_->spaceName();
auto spaceId = ectx()->schemaManager()->toGraphSpaceID(*name);
auto future = ectx()->getMetaClient()->getSpace(spaceId);
auto *runner = ectx()->rctx()->runner();
auto cb = [this] (auto &&resp) {
if (!resp.ok()) {
onError_(Status::Error("Space not found"));
return;
}
resp_ = std::make_unique<cpp2::ExecutionResponse>();
std::vector<std::string> header{"ID", "Name", "Partition number", "Replica Factor"};
resp_->set_column_names(std::move(header));
std::vector<cpp2::RowValue> rows;
std::vector<cpp2::ColumnValue> row;
row.resize(4);
row[0].set_integer(resp.value().get_space_id());
auto properties = resp.value().get_properties();
row[1].set_str(properties.get_space_name());
row[2].set_integer(properties.get_partition_num());
row[3].set_integer(properties.get_replica_factor());
rows.emplace_back();
rows.back().set_columns(std::move(row));
resp_->set_rows(std::move(rows));
DCHECK(onFinish_);
onFinish_();
};
auto error = [this] (auto &&e) {
LOG(ERROR) << "Exception caught: " << e.what();
DCHECK(onError_);
onError_(Status::Error("Internal error"));
return;
};
std::move(future).via(runner).thenValue(cb).thenError(error);
}
void DescribeSpaceExecutor::setupResponse(cpp2::ExecutionResponse &resp) {
resp = std::move(*resp_);
}
} // namespace graph
} // namespace nebula
/* Copyright (c) 2019 - 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 GRAPH_DESCRIBESPACEEXECUTOR_H_
#define GRAPH_DESCRIBESPACEEXECUTOR_H_
#include "base/Base.h"
#include "graph/Executor.h"
namespace nebula {
namespace graph {
class DescribeSpaceExecutor final : public Executor {
public:
DescribeSpaceExecutor(Sentence *sentence, ExecutionContext *ectx);
const char* name() const override {
return "DescribeSpaceExecutor";
}
Status MUST_USE_RESULT prepare() override;
void execute() override;
void setupResponse(cpp2::ExecutionResponse &resp) override;
private:
DescribeSpaceSentence *sentence_{nullptr};
std::string *tag_{nullptr};
std::unique_ptr<cpp2::ExecutionResponse> resp_;
};
} // namespace graph
} // namespace nebula
#endif // GRAPH_DESCRIBESPACEEXECUTOR_H_
......@@ -29,28 +29,25 @@ void DescribeTagExecutor::execute() {
auto schema = ectx()->schemaManager()->getTagSchema(spaceId, tagId);
resp_ = std::make_unique<cpp2::ExecutionResponse>();
do {
if (schema == nullptr) {
onError_(Status::Error("Schema not found for tag `%s'", name->c_str()));
return;
}
std::vector<std::string> header{"Field", "Type"};
resp_->set_column_names(std::move(header));
uint32_t numFields = schema->getNumFields();
std::vector<cpp2::RowValue> rows;
for (uint32_t index = 0; index < numFields; index++) {
std::vector<cpp2::ColumnValue> row;
row.resize(2);
row[0].set_str(schema->getFieldName(index));
row[1].set_str(valueTypeToString(schema->getFieldType(index)));
rows.emplace_back();
rows.back().set_columns(std::move(row));
}
resp_->set_rows(std::move(rows));
} while (false);
if (schema == nullptr) {
onError_(Status::Error("Schema not found for tag `%s'", name->c_str()));
return;
}
std::vector<std::string> header{"Field", "Type"};
resp_->set_column_names(std::move(header));
uint32_t numFields = schema->getNumFields();
std::vector<cpp2::RowValue> rows;
for (uint32_t index = 0; index < numFields; index++) {
std::vector<cpp2::ColumnValue> row;
row.resize(2);
row[0].set_str(schema->getFieldName(index));
row[1].set_str(valueTypeToString(schema->getFieldType(index)));
rows.emplace_back();
rows.back().set_columns(std::move(row));
}
resp_->set_rows(std::move(rows));
DCHECK(onFinish_);
onFinish_();
}
......
......@@ -28,6 +28,7 @@
#include "graph/AddHostsExecutor.h"
#include "graph/RemoveHostsExecutor.h"
#include "graph/CreateSpaceExecutor.h"
#include "graph/DescribeSpaceExecutor.h"
#include "graph/DropSpaceExecutor.h"
#include "graph/YieldExecutor.h"
......@@ -95,6 +96,9 @@ std::unique_ptr<Executor> Executor::makeExecutor(Sentence *sentence) {
case Sentence::Kind::kDropSpace:
executor = std::make_unique<DropSpaceExecutor>(sentence, ectx());
break;
case Sentence::Kind::kDescribeSpace:
executor = std::make_unique<DescribeSpaceExecutor>(sentence, ectx());
break;
case Sentence::Kind::kYield:
executor = std::make_unique<YieldExecutor>(sentence, ectx());
break;
......
/* Copyright (c) 2018 vesoft inc. All rights reserved.
/* Copyright (c) 2019 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
......
......@@ -59,6 +59,17 @@ TEST_F(SchemaTest, metaCommunication) {
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
sleep(FLAGS_load_data_interval_secs + 1);
{
cpp2::ExecutionResponse resp;
std::string query = "DESCRIBE SPACE default_space";
auto code = client->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
std::vector<std::tuple<int, std::string, int, int>> expected{
{1, "default_space", 9, 3},
};
ASSERT_TRUE(verifyResult(resp, expected));
}
sleep(FLAGS_load_data_interval_secs + 1);
{
cpp2::ExecutionResponse resp;
std::string query = "USE default_space";
......
......@@ -49,25 +49,13 @@ std::string HostList::toString() const {
std::string AddHostsSentence::toString() const {
std::string buf;
buf.reserve(256);
buf += "ADD HOSTS (";
buf += hosts_->toString();
buf += ") ";
return buf;
return folly::stringPrintf("ADD HOSTS (%s) ", hosts_->toString().c_str());
}
std::string RemoveHostsSentence::toString() const {
std::string buf;
buf.reserve(256);
buf += "REMOVE HOSTS (";
buf += hosts_->toString();
buf += ") ";
return buf;
return folly::stringPrintf("REMOVE HOSTS (%s) ", hosts_->toString().c_str());
}
std::string SpaceOptItem::toString() const {
switch (optType_) {
case PARTITION_NUM:
......@@ -96,23 +84,16 @@ std::string SpaceOptList::toString() const {
std::string CreateSpaceSentence::toString() const {
std::string buf;
buf.reserve(256);
buf += "CREATE SPACE ";
buf += *spaceName_;
buf += "(";
buf += spaceOpts_->toString();
buf += ") ";
return buf;
return folly::stringPrintf("CREATE SPACE %s(%s) ", spaceName_.get()->c_str(),
spaceOpts_->toString().c_str());
}
std::string DropSpaceSentence::toString() const {
std::string buf;
buf.reserve(256);
buf += "DROP SPACE ";
buf += *spaceName_;
return buf;
return folly::stringPrintf("DROP SPACE %s", spaceName_.get()->c_str());
}
std::string DescribeSpaceSentence::toString() const {
return folly::stringPrintf("DESCRIBE SPACE %s", spaceName_.get()->c_str());
}
} // namespace nebula
......@@ -6,7 +6,6 @@
#ifndef PARSER_ADMINSENTENCES_H_
#define PARSER_ADMINSENTENCES_H_
#include "base/Base.h"
#include "parser/Clauses.h"
#include "parser/Sentence.h"
#include "network/NetworkUtils.h"
......@@ -251,6 +250,24 @@ private:
std::unique_ptr<std::string> spaceName_;
};
class DescribeSpaceSentence final : public Sentence {
public:
explicit DescribeSpaceSentence(std::string *spaceName) {
spaceName_.reset(spaceName);
kind_ = Kind::kDescribeSpace;
}
std::string* spaceName() {
return spaceName_.get();
}
std::string toString() const override;
private:
std::unique_ptr<std::string> spaceName_;
};
} // namespace nebula
#endif // PARSER_ADMINSENTENCES_H_
......@@ -42,6 +42,7 @@ public:
kRemoveHosts,
kCreateSpace,
kDropSpace,
kDescribeSpace,
kYield,
kCreateUser,
kDropUser,
......
......@@ -156,7 +156,7 @@ class GraphScanner;
%type <sentence> traverse_sentence set_sentence piped_sentence assignment_sentence
%type <sentence> maintain_sentence insert_vertex_sentence insert_edge_sentence
%type <sentence> mutate_sentence update_vertex_sentence update_edge_sentence delete_vertex_sentence delete_edge_sentence
%type <sentence> show_sentence add_hosts_sentence remove_hosts_sentence create_space_sentence
%type <sentence> show_sentence add_hosts_sentence remove_hosts_sentence create_space_sentence describe_space_sentence
%type <sentence> drop_space_sentence
%type <sentence> yield_sentence
%type <sentence> create_user_sentence alter_user_sentence drop_user_sentence change_password_sentence
......@@ -937,6 +937,12 @@ create_space_sentence
}
;
describe_space_sentence
: KW_DESCRIBE KW_SPACE LABEL {
$$ = new DescribeSpaceSentence($3);
}
;
space_opt_list
: space_opt_item {
$$ = new SpaceOptList();
......@@ -1105,6 +1111,7 @@ maintain_sentence
| add_hosts_sentence { $$ = $1; }
| remove_hosts_sentence { $$ = $1; }
| create_space_sentence { $$ = $1; }
| describe_space_sentence { $$ = $1; }
| drop_space_sentence { $$ = $1; }
| yield_sentence {
// Now we take YIELD as a normal maintenance sentence.
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment