Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
bright-starry-sky committed May 17, 2021
1 parent 1b626c8 commit 020da5c
Show file tree
Hide file tree
Showing 24 changed files with 819 additions and 170 deletions.
1 change: 1 addition & 0 deletions src/executor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ nebula_add_library(
maintain/TagIndexExecutor.cpp
maintain/EdgeExecutor.cpp
maintain/EdgeIndexExecutor.cpp
maintain/FTIndexExecutor.cpp
mutate/InsertExecutor.cpp
mutate/DeleteExecutor.cpp
mutate/UpdateExecutor.cpp
Expand Down
10 changes: 10 additions & 0 deletions src/executor/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "executor/maintain/EdgeIndexExecutor.h"
#include "executor/maintain/TagExecutor.h"
#include "executor/maintain/TagIndexExecutor.h"
#include "executor/maintain/FTIndexExecutor.h"
#include "executor/mutate/DeleteExecutor.h"
#include "executor/mutate/InsertExecutor.h"
#include "executor/mutate/UpdateExecutor.h"
Expand Down Expand Up @@ -263,12 +264,18 @@ Executor *Executor::makeExecutor(QueryContext *qctx, const PlanNode *node) {
case PlanNode::Kind::kCreateEdgeIndex: {
return pool->add(new CreateEdgeIndexExecutor(node, qctx));
}
case PlanNode::Kind::kCreateFTIndex: {
return pool->add(new CreateFTIndexExecutor(node, qctx));
}
case PlanNode::Kind::kDropTagIndex: {
return pool->add(new DropTagIndexExecutor(node, qctx));
}
case PlanNode::Kind::kDropEdgeIndex: {
return pool->add(new DropEdgeIndexExecutor(node, qctx));
}
case PlanNode::Kind::kDropFTIndex: {
return pool->add(new DropFTIndexExecutor(node, qctx));
}
case PlanNode::Kind::kDescTagIndex: {
return pool->add(new DescTagIndexExecutor(node, qctx));
}
Expand Down Expand Up @@ -464,6 +471,9 @@ Executor *Executor::makeExecutor(QueryContext *qctx, const PlanNode *node) {
case PlanNode::Kind::kShowTSClients: {
return pool->add(new ShowTSClientsExecutor(node, qctx));
}
case PlanNode::Kind::kShowFTIndexes: {
return pool->add(new ShowFTIndexesExecutor(node, qctx));
}
case PlanNode::Kind::kSignInTSService: {
return pool->add(new SignInTSServiceExecutor(node, qctx));
}
Expand Down
30 changes: 29 additions & 1 deletion src/executor/admin/SpaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "planner/plan/Admin.h"
#include "util/SchemaUtil.h"
#include "util/ScopedTimer.h"
#include "util/FTIndexUtils.h"

namespace nebula {
namespace graph {
Expand Down Expand Up @@ -95,9 +96,22 @@ folly::Future<Status> DropSpaceExecutor::execute() {
SCOPED_TIMER(&execTime_);

auto *dsNode = asNode<DropSpace>(node());

// prepare text search index before drop meta data.
std::vector<std::string> ftIndexes;
auto spaceIdRet = qctx()->getMetaClient()->getSpaceIdByNameFromCache(dsNode->getSpaceName());
if (spaceIdRet.ok()) {
auto ftIndexesRet = qctx()->getMetaClient()->getFTIndexBySpaceFromCache(spaceIdRet.value());
NG_RETURN_IF_ERROR(ftIndexesRet);
auto map = std::move(ftIndexesRet).value();
transform(map.begin(), map.end(), ftIndexes.begin(), [](auto pair) { return pair.first; });
} else {
LOG(WARNING) << "Get space ID failed when prepare text index: " << dsNode->getSpaceName();
}

return qctx()->getMetaClient()->dropSpace(dsNode->getSpaceName(), dsNode->getIfExists())
.via(runner())
.thenValue([this, dsNode](StatusOr<bool> resp) {
.thenValue([this, dsNode, spaceIdRet, ftIndexes](StatusOr<bool> resp) {
if (!resp.ok()) {
LOG(ERROR) << "Drop space `" << dsNode->getSpaceName()
<< "' failed: " << resp.status();
Expand All @@ -109,6 +123,20 @@ folly::Future<Status> DropSpaceExecutor::execute() {
spaceInfo.id = -1;
qctx()->rctx()->session()->setSpace(std::move(spaceInfo));
}
if (!ftIndexes.empty()) {
auto tsRet = FTIndexUtils::getTSClients(qctx()->getMetaClient());
if (!tsRet.ok()) {
LOG(WARNING) << "Get text search clients failed";
return Status::OK();
}
for (const auto& ftindex : ftIndexes) {
auto ftRet = FTIndexUtils::dropTSIndex(std::move(tsRet).value(), ftindex);
if (!ftRet.ok()) {
LOG(WARNING) << "Drop fulltext index `"
<< ftindex << "' failed: " << ftRet.status();
}
}
}
return Status::OK();
});
}
Expand Down
106 changes: 106 additions & 0 deletions src/executor/maintain/FTIndexExecutor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* Copyright (c) 2021 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.
*/

#include "common/interface/gen-cpp2/meta_types.h"

#include "context/QueryContext.h"
#include "executor/maintain/FTIndexExecutor.h"
#include "planner/plan/Maintain.h"
#include "util/FTIndexUtils.h"

namespace nebula {
namespace graph {

folly::Future<Status> CreateFTIndexExecutor::execute() {
SCOPED_TIMER(&execTime_);
auto *inode = asNode<CreateFTIndex>(node());
return qctx()
->getMetaClient()
->createFTIndex(inode->getIndexName(),
inode->getIndex())
.via(runner())
.thenValue([inode](StatusOr<IndexID> resp) {
if (!resp.ok()) {
LOG(ERROR) << "Create fulltext index `"
<< inode->getIndexName() << "' "
<< "failed: " << resp.status();
return resp.status();
}
return Status::OK();
});
}

folly::Future<Status> DropFTIndexExecutor::execute() {
auto *inode = asNode<DropFTIndex>(node());
auto spaceId = qctx()->rctx()->session()->space().id;
return qctx()
->getMetaClient()
->dropFTIndex(spaceId, inode->getName())
.via(runner())
.thenValue([this, inode, spaceId](StatusOr<bool> resp) {
if (!resp.ok()) {
LOG(ERROR) << "SpaceId: " << spaceId << ", Drop fulltext index `"
<< inode->getName() << "' failed: " << resp.status();
return resp.status();
}
auto tsRet = FTIndexUtils::getTSClients(qctx()->getMetaClient());
if (!tsRet.ok()) {
LOG(WARNING) << "Get text search clients failed";
}
auto ftRet = FTIndexUtils::dropTSIndex(std::move(tsRet).value(), inode->getName());
if (!ftRet.ok()) {
LOG(WARNING) << "Drop fulltext index '"
<< inode->getName() << "' failed: " << ftRet.status();
}
return Status::OK();
});
}

folly::Future<Status> ShowFTIndexesExecutor::execute() {
SCOPED_TIMER(&execTime_);
auto spaceId = qctx()->rctx()->session()->space().id;
return qctx()->getMetaClient()->listFTIndexes().via(runner()).thenValue(
[this, spaceId](StatusOr<std::unordered_map<std::string, meta::cpp2::FTIndex>> resp) {
if (!resp.ok()) {
LOG(ERROR) << "SpaceId: " << spaceId << ", Show fulltext indexes failed"
<< resp.status();
return resp.status();
}

auto indexes = std::move(resp).value();
DataSet dataSet;
dataSet.colNames = {"Name", "Schema Type", "Schema Name", "Fields"};
for (auto& index : indexes) {
if (index.second.get_space_id() != spaceId) {
continue;
}
auto shmId = index.second.get_depend_schema();
auto isEdge = shmId.getType() == meta::cpp2::SchemaID::Type::edge_type;
auto shmNameRet = isEdge
? this->qctx_->schemaMng()->toEdgeName(spaceId, shmId.get_edge_type())
: this->qctx_->schemaMng()->toTagName(spaceId, shmId.get_tag_id());
if (!shmNameRet.ok()) {
LOG(ERROR) << "SpaceId: " << spaceId << ", Get schema name failed";
return shmNameRet.status();
}
std::string fields;
folly::join(", ", index.second.get_fields(), fields);
Row row;
row.values.emplace_back(index.first);
row.values.emplace_back(isEdge ? "Edge" : "Tag");
row.values.emplace_back(std::move(shmNameRet).value());
row.values.emplace_back(std::move(fields));
dataSet.rows.emplace_back(std::move(row));
}
return finish(ResultBuilder()
.value(Value(std::move(dataSet)))
.iter(Iterator::Kind::kDefault)
.finish());
});
}

} // namespace graph
} // namespace nebula
42 changes: 42 additions & 0 deletions src/executor/maintain/FTIndexExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright (c) 2021 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.
*/

#ifndef EXECUTOR_ADMIN_SHOW_FT_INDEXES_EXECUTOR_H_
#define EXECUTOR_ADMIN_SHOW_FT_INDEXES_EXECUTOR_H_

#include "executor/Executor.h"

namespace nebula {
namespace graph {

class ShowFTIndexesExecutor final : public Executor {
public:
ShowFTIndexesExecutor(const PlanNode *node, QueryContext *qctx)
: Executor("ShowFTIndexesExecutor", node, qctx) {}

folly::Future<Status> execute() override;
};

class CreateFTIndexExecutor final : public Executor {
public:
CreateFTIndexExecutor(const PlanNode *node, QueryContext *qctx)
: Executor("CreateFTIndexExecutor", node, qctx) {}

folly::Future<Status> execute() override;
};

class DropFTIndexExecutor final : public Executor {
public:
DropFTIndexExecutor(const PlanNode *node, QueryContext *qctx)
: Executor("DropFTIndexExecutor", node, qctx) {}

folly::Future<Status> execute() override;
};

} // namespace graph
} // namespace nebula

#endif // EXECUTOR_ADMIN_SHOW_FT_INDEXES_EXECUTOR_H_
32 changes: 32 additions & 0 deletions src/parser/MaintainSentences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,36 @@ std::string DropHostFromZoneSentence::toString() const {
return buf;
}

std::string CreateFTIndexSentence::toString() const {
std::string buf;
buf.reserve(256);
buf += "CREATE FULLTEXT INDEX ";
buf += *indexName_;
buf += " ON ";
if (isEdge_) {
buf += " EDGE ";
} else {
buf += " TAG ";
}
buf += *schemaName_;
buf += "(";
std::vector<std::string> fieldDefs;
for (const auto& field : fields()) {
fieldDefs.emplace_back(field);
}
std::string fields;
folly::join(", ", fieldDefs, fields);
buf += fields;
buf += ")";
return buf;
}

std::string DropFTIndexSentence::toString() const {
return folly::stringPrintf("DROP FULLTEXT INDEX %s", indexName_.get()->c_str());
}

std::string ShowFTIndexesSentence::toString() const {
return "SHOW FULLTEXT INDEXES";
}

} // namespace nebula
69 changes: 69 additions & 0 deletions src/parser/MaintainSentences.h
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,75 @@ class DropHostFromZoneSentence : public Sentence {
std::unique_ptr<HostAddr> address_;
};

#ifndef FULLTEXT_INDEX_NAME_PREFIX
#define FULLTEXT_INDEX_NAME_PREFIX "nebula_"
#endif
class CreateFTIndexSentence final : public Sentence {
public:
CreateFTIndexSentence(bool isEdge,
std::string* indexName,
std::string* schemaName,
NameLabelList *fields) {
isEdge_ = isEdge;
indexName_.reset(indexName);
schemaName_.reset(schemaName);
fields_.reset(fields);
kind_ = Kind::kCreateFTIndex;
}

std::string toString() const override;

bool isEdge() {
return isEdge_;
}
const std::string* indexName() const {
return indexName_.get();
}

const std::string* schemaName() const {
return schemaName_.get();
}

std::vector<std::string> fields() const {
std::vector<std::string> result;
auto fields = fields_->labels();
result.resize(fields.size());
auto get = [] (auto ptr) { return *ptr; };
std::transform(fields.begin(), fields.end(), result.begin(), get);
return result;
}

private:
bool isEdge_;
std::unique_ptr<std::string> indexName_;
std::unique_ptr<std::string> schemaName_;
std::unique_ptr<NameLabelList> fields_;
};
class DropFTIndexSentence final : public Sentence {
public:
explicit DropFTIndexSentence(std::string *indexName) {
indexName_.reset(indexName);
kind_ = Kind::kDropFTIndex;
}

std::string toString() const override;

const std::string* name() const {
return indexName_.get();
}

private:
std::unique_ptr<std::string> indexName_;
};

class ShowFTIndexesSentence final : public Sentence {
public:
ShowFTIndexesSentence() {
kind_ = Kind::kShowFTIndexes;
}
std::string toString() const override;
};


} // namespace nebula

Expand Down
3 changes: 3 additions & 0 deletions src/parser/Sentence.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Sentence {
kShowZones,
kShowStats,
kShowTSClients,
kShowFTIndexes,
kDeleteVertices,
kDeleteEdges,
kLookup,
Expand Down Expand Up @@ -126,6 +127,8 @@ class Sentence {
kShowListener,
kSignInTSService,
kSignOutTSService,
kCreateFTIndex,
kDropFTIndex,
};

Kind kind() const {
Expand Down
Loading

0 comments on commit 020da5c

Please sign in to comment.