Skip to content

Commit

Permalink
QualifiedResourceNameTypeConverter: added extra field for job_id
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Sep 7, 2018
1 parent d4a1344 commit 784a8a9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/src/filed/dir_cmd.cc
Expand Up @@ -1590,7 +1590,7 @@ static bool StorageCmd(JobControlRecord *jcr)
jcr->store_bsock = sd;

if (!my_config->GetQualifiedResourceNameTypeConverter()->ResourceToString(
jcr->client_name, my_config->r_own_, qualified_resource_name)) {
jcr->client_name, my_config->r_own_, jcr->JobId, qualified_resource_name)) {
goto bail_out;
}

Expand Down
30 changes: 28 additions & 2 deletions core/src/lib/qualified_resource_name_type_converter.cc
Expand Up @@ -77,6 +77,19 @@ bool QualifiedResourceNameTypeConverter::ResourceToString(const std::string &nam
return true;
}

bool QualifiedResourceNameTypeConverter::ResourceToString(const std::string &name_of_resource,
const int &r_type,
const int &job_id,
std::string &out) const
{
std::string r_name = ResourceTypeToString(r_type);
if (r_name.empty()) {
return false;
}
out = r_name + std::string(":") + name_of_resource + std::string(":") + std::to_string(job_id);
return true;
}

template <class Container>
void split(const std::string &str, Container &cont, char delim, int max_substring)
{
Expand All @@ -91,11 +104,12 @@ void split(const std::string &str, Container &cont, char delim, int max_substrin

bool QualifiedResourceNameTypeConverter::StringToResource(std::string &name_of_resource,
int &r_type,
int &job_id,
const std::string &in) const
{
std::vector<std::string> string_list;
split(in, string_list, ':', 2);
if (string_list.size() < 2) {
split(in, string_list, ':', 3);
if (string_list.size() < 2) { /* minimum of parameters are name and r_type */
return false;
}
std::string r_type_str = string_list.at(0);
Expand All @@ -105,5 +119,17 @@ bool QualifiedResourceNameTypeConverter::StringToResource(std::string &name_of_r
}
r_type = r_type_eval;
name_of_resource = string_list.at(1);

if (string_list.size() == 3) {
int job_id_temp;
std::string job_id_str = string_list.at(2);
try {
job_id_temp = std::stoi(job_id_str);
}
catch (const std::exception &e) {
return false;
}
job_id = job_id_temp;
}
return true;
}
3 changes: 2 additions & 1 deletion core/src/lib/qualified_resource_name_type_converter.h
Expand Up @@ -28,7 +28,8 @@ class QualifiedResourceNameTypeConverter {
public:
QualifiedResourceNameTypeConverter(std::map<int, std::string> map);
bool ResourceToString(const std::string &name_of_resource, const int &r_type, std::string &out) const;
bool StringToResource(std::string &name_of_resource, int &r_type, const std::string &in) const;
bool ResourceToString(const std::string &name_of_resource, const int &r_type, const int &JobId, std::string &out) const;
bool StringToResource(std::string &name_of_resource, int &r_type, int &job_id, const std::string &in) const;

private:
std::map<int, std::string> type_name_relation_map_;
Expand Down
3 changes: 2 additions & 1 deletion core/src/lib/res.cc
Expand Up @@ -148,8 +148,9 @@ bool ConfigurationParser::GetTlsPskByFullyQualifiedResourceName(ConfigurationPar
free(fq_name_buffer);

int r_type;
int job_id;
std::string name;
bool ok = config->GetQualifiedResourceNameTypeConverter()->StringToResource(name, r_type, fq_name_in);
bool ok = config->GetQualifiedResourceNameTypeConverter()->StringToResource(name, r_type, job_id, fq_name_in);
if (!ok || r_type < 0) { return false; }
TlsResource *tls = reinterpret_cast<TlsResource *>(config->GetResWithName(r_type, name.c_str()));
if (tls) {
Expand Down
Expand Up @@ -67,11 +67,18 @@ TEST(QualifiedResourceNameTypeConverter, ResourceTypeToString)

std::string name;
int r_type;
ok = c.StringToResource(name, r_type, "kOne:Developer");
int job_id = -1;
ok = c.StringToResource(name, r_type, job_id, "kOne:Developer:123");
EXPECT_EQ(ok, true);
EXPECT_EQ(r_type, kOne);
EXPECT_EQ(job_id, 123);
EXPECT_STREQ(name.c_str(), "Developer");

ok = c.StringToResource(name, r_type, "kOneDeveloper");
ok = c.StringToResource(name, r_type, job_id, "kOneDeveloper");
EXPECT_EQ(ok, false);

job_id = -1;
ok = c.StringToResource(name, r_type, job_id, "kOne:Developer");
EXPECT_EQ(ok, true);
EXPECT_EQ(job_id, -1);
}

0 comments on commit 784a8a9

Please sign in to comment.