Skip to content

Commit fae36aa

Browse files
visualYJDrock-git
authored andcommitted
[feat][client] Add search all document command
1 parent 0520373 commit fae36aa

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

src/client_v2/document_index.cc

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
#include <cstdint>
1818

19+
#include "client_v2/coordinator.h"
1920
#include "client_v2/pretty.h"
2021
#include "common/helper.h"
2122
#include "fmt/format.h"
2223

24+
const int kBatchSize = 3;
25+
2326
namespace client_v2 {
2427

2528
void SetUpDocumentIndexSubCommands(CLI::App& app) {
@@ -28,6 +31,7 @@ void SetUpDocumentIndexSubCommands(CLI::App& app) {
2831
SetUpDocumentBatchAdd(app);
2932
SetUpDocumentDelete(app);
3033
SetUpDocumentSearch(app);
34+
SetUpDocumentSearchAll(app);
3135
SetUpDocumentBatchQuery(app);
3236
SetUpDocumentScanQuery(app);
3337
SetUpDocumentGetMaxId(app);
@@ -324,6 +328,69 @@ void SendDocumentSearch(DocumentSearchOptions const& opt) {
324328
Pretty::Show(response);
325329
}
326330

331+
void SendDocumentSearchAll(DocumentSearchOptions const& opt) {
332+
// dingodb::pb::document::DocumentSearchRequest request;
333+
// dingodb::pb::document::DocumentSearchResponse response;
334+
335+
if (opt.query_string.empty()) {
336+
std::cout << "query_string is empty" << std::endl;
337+
return;
338+
}
339+
340+
auto response = SendSearchAllByStreamMode(opt);
341+
std::cout << "search all documents response:" << response.DebugString() << std::endl;
342+
Pretty::Show(response);
343+
}
344+
345+
dingodb::pb::document::DocumentSearchAllResponse SendSearchAllByStreamMode(DocumentSearchOptions const& opt) {
346+
dingodb::pb::document::DocumentSearchAllRequest request;
347+
dingodb::pb::document::DocumentSearchAllResponse response;
348+
auto* parameter = request.mutable_parameter();
349+
parameter->set_query_string(opt.query_string);
350+
parameter->set_without_scalar_data(opt.without_scalar);
351+
parameter->set_query_unlimited(true);
352+
request.mutable_stream_meta()->set_limit(kBatchSize);
353+
if (opt.doc_id > 0) {
354+
parameter->add_document_ids(opt.doc_id);
355+
}
356+
*(request.mutable_context()) = RegionRouter::GetInstance().GenConext(opt.region_id);
357+
358+
for (;;) {
359+
dingodb::pb::document::DocumentSearchAllResponse sub_response;
360+
// maybe current store interaction is not store node, so need reset.
361+
InteractionManager::GetInstance().ResetStoreInteraction();
362+
auto status = InteractionManager::GetInstance().SendRequestWithContext("DocumentService", "DocumentSearchAll",
363+
request, sub_response);
364+
std::cout << "search all request: " << request.DebugString() << ", sub_response: " << sub_response.DebugString()
365+
<< std::endl;
366+
if (!status.ok()) {
367+
response.mutable_error()->set_errcode(dingodb::pb::error::Errno(status.error_code()));
368+
response.mutable_error()->set_errmsg(status.error_str());
369+
break;
370+
}
371+
372+
if (sub_response.error().errcode() != dingodb::pb::error::OK) {
373+
*response.mutable_error() = sub_response.error();
374+
break;
375+
}
376+
377+
// set request stream id
378+
if (!sub_response.stream_meta().stream_id().empty()) {
379+
request.mutable_stream_meta()->set_stream_id(sub_response.stream_meta().stream_id());
380+
}
381+
382+
// copy data
383+
for (int i = 0; i < sub_response.document_with_scores_size(); ++i) {
384+
response.add_document_with_scores()->Swap(&sub_response.mutable_document_with_scores()->at(i));
385+
}
386+
if (!sub_response.stream_meta().has_more()) {
387+
break;
388+
}
389+
}
390+
391+
return response;
392+
}
393+
327394
void SendDocumentBatchQuery(DocumentBatchQueryOptions const& opt) {
328395
dingodb::pb::document::DocumentBatchQueryRequest request;
329396
dingodb::pb::document::DocumentBatchQueryResponse response;
@@ -645,6 +712,26 @@ void RunDocumentSearch(DocumentSearchOptions const& opt) {
645712
client_v2::SendDocumentSearch(opt);
646713
}
647714

715+
void SetUpDocumentSearchAll(CLI::App& app) {
716+
auto opt = std::make_shared<DocumentSearchOptions>();
717+
auto* cmd = app.add_subcommand("DocumentSearchAll", "Document search all documents")->group("Document Commands");
718+
cmd->add_option("--coor_url", opt->coor_url, "Coordinator url, default:file://./coor_list");
719+
cmd->add_option("--region_id", opt->region_id, "Request parameter region id")->required();
720+
cmd->add_option("--query_string", opt->query_string, "Request parameter query_string")->required();
721+
cmd->add_option("--without_scalar", opt->without_scalar, "Request parameter without_scalar")
722+
->default_val(false)
723+
->default_str("false");
724+
cmd->add_option("--doc_id", opt->doc_id, "Request parameter alive id");
725+
cmd->callback([opt]() { RunDocumentSearchAll(*opt); });
726+
}
727+
728+
void RunDocumentSearchAll(DocumentSearchOptions const& opt) {
729+
if (!SetUpStore(opt.coor_url, {}, opt.region_id)) {
730+
exit(-1);
731+
}
732+
client_v2::SendDocumentSearchAll(opt);
733+
}
734+
648735
void SetUpDocumentBatchQuery(CLI::App& app) {
649736
auto opt = std::make_shared<DocumentBatchQueryOptions>();
650737
auto* cmd = app.add_subcommand("DocumentBatchQuery", "Document batch query")->group("Document Commands");

src/client_v2/document_index.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ struct DocumentSearchOptions {
9797
void SetUpDocumentSearch(CLI::App &app);
9898
void RunDocumentSearch(DocumentSearchOptions const &opt);
9999

100+
void SetUpDocumentSearchAll(CLI::App &app);
101+
void RunDocumentSearchAll(DocumentSearchOptions const &opt);
102+
100103
struct DocumentBatchQueryOptions {
101104
std::string coor_url;
102105
int64_t region_id;
@@ -156,13 +159,15 @@ void SendDocumentAdd(DocumentAddOptions const &opt);
156159
void SendDocumentBatchAdd(DocumentAddOptions const &opt);
157160
void SendDocumentDelete(DocumentDeleteOptions const &opt);
158161
void SendDocumentSearch(DocumentSearchOptions const &opt);
162+
void SendDocumentSearchAll(DocumentSearchOptions const &opt);
159163
void SendDocumentBatchQuery(DocumentBatchQueryOptions const &opt);
160164
void SendDocumentGetMaxId(DocumentGetMaxIdOptions const &opt);
161165
void SendDocumentGetMinId(DocumentGetMinIdOptions const &opt);
162166
void SendDocumentScanQuery(DocumentScanQueryOptions const &opt);
163167
void SendDocumentCount(DocumentCountOptions const &opt);
164168
void SendDocumentGetRegionMetrics(DocumentGetRegionMetricsOptions const &opt);
165169

170+
dingodb::pb::document::DocumentSearchAllResponse SendSearchAllByStreamMode(DocumentSearchOptions const &opt);
166171
} // namespace client_v2
167172

168173
#endif // DINGODB_CLIENT_DOCUMENT_INDEX_H_

src/client_v2/pretty.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,30 @@ void Pretty::Show(dingodb::pb::document::DocumentSearchResponse& response) {
922922

923923
PrintTable(rows);
924924
}
925+
926+
void Pretty::Show(dingodb::pb::document::DocumentSearchAllResponse& response) {
927+
if (ShowError(response.error())) {
928+
return;
929+
}
930+
if (response.document_with_scores_size() == 0) {
931+
std::cout << "Not search document ." << std::endl;
932+
return;
933+
}
934+
std::vector<std::vector<ftxui::Element>> rows = {{
935+
ftxui::paragraph("DocumentId"),
936+
ftxui::paragraph("Score"),
937+
}};
938+
for (auto const& document_with_score : response.document_with_scores()) {
939+
std::vector<ftxui::Element> row = {
940+
ftxui::paragraph(fmt::format("{}", document_with_score.document_with_id().id())),
941+
ftxui::paragraph(fmt::format("{}", document_with_score.score())),
942+
};
943+
rows.push_back(row);
944+
}
945+
946+
PrintTable(rows);
947+
}
948+
925949
void Pretty::Show(dingodb::pb::document::DocumentBatchQueryResponse& response) {
926950
if (ShowError(response.error())) {
927951
return;

src/client_v2/pretty.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Pretty {
5656

5757
static void Show(dingodb::pb::meta::CreateIndexResponse &response);
5858
static void Show(dingodb::pb::document::DocumentSearchResponse &response);
59+
static void Show(dingodb::pb::document::DocumentSearchAllResponse &response);
5960
static void Show(dingodb::pb::document::DocumentBatchQueryResponse &response);
6061
static void Show(dingodb::pb::document::DocumentGetBorderIdResponse &response);
6162
static void Show(dingodb::pb::document::DocumentScanQueryResponse &response);

0 commit comments

Comments
 (0)