index: add serialization function for index targets
Since target_parser is responsible for deserializing target strings, the function that serializes them belongs in the same class.
This commit is contained in:
@@ -41,9 +41,12 @@
|
||||
|
||||
#include "secondary_index.hh"
|
||||
#include "index/target_parser.hh"
|
||||
#include "cql3/statements/index_target.hh"
|
||||
|
||||
#include <regex>
|
||||
#include <boost/range/algorithm/find_if.hpp>
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
|
||||
#include "json.hh"
|
||||
|
||||
@@ -139,4 +142,43 @@ sstring target_parser::get_target_column_name_from_string(const sstring& targets
|
||||
return targets;
|
||||
}
|
||||
|
||||
sstring target_parser::serialize_targets(const std::vector<::shared_ptr<cql3::statements::index_target>>& targets) {
|
||||
using cql3::statements::index_target;
|
||||
|
||||
struct as_json_visitor {
|
||||
Json::Value operator()(const index_target::multiple_columns& columns) const {
|
||||
Json::Value json_array(Json::arrayValue);
|
||||
for (const auto& column : columns) {
|
||||
json_array.append(Json::Value(column->to_string()));
|
||||
}
|
||||
return json_array;
|
||||
}
|
||||
|
||||
Json::Value operator()(const index_target::single_column& column) const {
|
||||
return Json::Value(column->to_string());
|
||||
}
|
||||
};
|
||||
|
||||
if (targets.size() == 1 && std::holds_alternative<index_target::single_column>(targets.front()->value)) {
|
||||
return std::get<index_target::single_column>(targets.front()->value)->to_string();
|
||||
}
|
||||
|
||||
Json::Value json_map(Json::objectValue);
|
||||
Json::Value pk_json = std::visit(as_json_visitor(), targets.front()->value);
|
||||
if (!pk_json.isArray()) {
|
||||
Json::Value pk_array(Json::arrayValue);
|
||||
pk_array.append(std::move(pk_json));
|
||||
pk_json = std::move(pk_array);
|
||||
}
|
||||
json_map[PK_TARGET_KEY] = std::move(pk_json);
|
||||
if (targets.size() > 1) {
|
||||
Json::Value ck_json(Json::arrayValue);
|
||||
for (unsigned i = 1; i < targets.size(); ++i) {
|
||||
ck_json.append(std::visit(as_json_visitor(), targets.at(i)->value));
|
||||
}
|
||||
json_map[CK_TARGET_KEY] = ck_json;
|
||||
}
|
||||
return json::to_sstring(json_map);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -61,6 +61,8 @@ struct target_parser {
|
||||
static bool is_local(sstring target_string);
|
||||
|
||||
static sstring get_target_column_name_from_string(const sstring& targets);
|
||||
|
||||
static sstring serialize_targets(const std::vector<::shared_ptr<cql3::statements::index_target>>& targets);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user