Skip to content

Commit

Permalink
Merge pull request #11 from jhiemstrawisc/issue-8
Browse files Browse the repository at this point in the history
Update paths object to return array of path objects
  • Loading branch information
jhiemstrawisc authored Feb 21, 2024
2 parents bf115d2 + 61765bd commit 0c95cd6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/lotman_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,50 +564,52 @@ std::pair<json, std::string> lotman::Lot::get_restricting_attribute(const std::s
}

std::pair<json, std::string> lotman::Lot::get_lot_dirs(const bool recursive) {
json path_obj;
json path_arr = json::array();
std::string path_query = "SELECT path, recursive FROM paths WHERE lot_name = ?;";
//std::string recursive_query = "SELECT recursive FROM paths WHERE path = ? AND lot_name = ?;";
std::map<std::string, std::vector<int>> path_query_str_map{{lot_name, {1}}};
auto rp = lotman::Checks::SQL_get_matches_multi_col(path_query, 2, path_query_str_map);
if (!rp.second.empty()) { // There was an error
std::string int_err = rp.second;
std::string ext_err = "Failure on call to SQL_get_matches_multi_col: ";
return std::make_pair(path_obj, ext_err + int_err);
return std::make_pair(path_arr, ext_err + int_err);
}

for (const auto &path_rec : rp.first) { // the path is at index 0, and the recursion of the path is at index 1
json path_obj_internal;
path_obj_internal["lot_name"] = this->lot_name;
path_obj_internal["recursive"] = static_cast<bool>(std::stoi(path_rec[1]));
path_obj[path_rec[0]] = path_obj_internal;
path_obj_internal["path"] = path_rec[0];
path_arr.push_back(path_obj_internal);
}

if (recursive) { // Not recursion of path, but recursion of dirs associated to a lot, ie the directories associated with lot proper's children
auto rp_vec_str = this->get_children(true);
if (!rp_vec_str.second.empty()) { // There was an error
std::string int_err = rp_vec_str.second;
std::string ext_err = "Failure to get children.";
return std::make_pair(json(), ext_err+int_err);
return std::make_pair(json::array(), ext_err+int_err);
}
for (const auto &child : recursive_children) {
std::map<std::string, std::vector<int>> child_path_query_str_map{{child.lot_name, {1}}};
rp = lotman::Checks::SQL_get_matches_multi_col(path_query, 2, child_path_query_str_map);
if (!rp.second.empty()) { // There was an error
std::string int_err = rp.second;
std::string ext_err = "Failure on call to SQL_get_matches_multi_col: ";
return std::make_pair(path_obj, ext_err + int_err);
return std::make_pair(path_arr, ext_err + int_err);
}

for (const auto &path_rec : rp.first) {
json path_obj_internal;
path_obj_internal["lot_name"] = child.lot_name;
path_obj_internal["recursive"] = static_cast<bool>(std::stoi(path_rec[1]));
path_obj[path_rec[0]] = path_obj_internal;
path_obj_internal["path"] = path_rec[0];
path_arr.push_back(path_obj_internal);
}
}
}

return std::make_pair(path_obj, "");
return std::make_pair(path_arr, "");
}

std::pair<std::string, std::string> lotman::Lot::get_lot_from_dir(const std::string dir_path) {
Expand Down
28 changes: 21 additions & 7 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,25 @@ namespace {
rv = lotman_get_lot_dirs(lot_name, recursive, &output, &err_msg2);
ASSERT_TRUE(rv == 0);

std::unique_ptr<char, decltype(&free)> output_ptr(output, &free); // Wrap output with a unique_ptr
json json_out = json::parse(output);
ASSERT_TRUE(json_out["/1/2/3/4"]["lot_name"] == "lot4" && json_out["/1/2/3/4"]["recursive"] == true &&
json_out["/345"]["lot_name"] == "lot4" && json_out["/345"]["recursive"] == true &&
json_out["/456"]["lot_name"] == "lot5" && json_out["/456"]["recursive"] == false &&
json_out["/567"]["lot_name"] == "lot5" && json_out["/567"]["recursive"] == true);
free(output);
for (const auto &path_obj : json_out) {
if (path_obj["path"] == "/1/2/3/4" && path_obj["recursive"] == true && path_obj["lot_name"] == "lot4") {
continue;
}
else if (path_obj["path"] == "/345" && path_obj["recursive"] == true && path_obj["lot_name"] == "lot4") {
continue;
}
else if (path_obj["path"] == "/456" && path_obj["recursive"] == false && path_obj["lot_name"] == "lot5") {
continue;
}
else if (path_obj["path"] == "/567" && path_obj["recursive"] == true && path_obj["lot_name"] == "lot5") {
continue;
}
else {
ASSERT_TRUE(false) << "Unexpected path object: " << output;
}
}
}

TEST_F(LotManTest, ContextTest) {
Expand Down Expand Up @@ -603,10 +616,11 @@ namespace {
char *err_msg2;
rv = lotman_get_lot_as_json("lot3", true, &output, &err_msg);
ASSERT_TRUE(rv == 0);

json output_JSON = json::parse(output);
free(output);
json expected_output = R"({"children":["lot4","lot5"],"lot_name":"lot3","management_policy_attrs":{"creation_time":{"lot_name":"lot3","value":123.0},"dedicated_GB":{"lot_name":"sep_node","value":3.0},"deletion_time":{"lot_name":"lot3","value":333.0},"expiration_time":{"lot_name":"lot3","value":222.0},"max_num_objects":{"lot_name":"sep_node","value":10.0},"opportunistic_GB":{"lot_name":"lot2","value":1.5}},"owners":["not owner1","owner1"],"parents":["lot1","lot2","sep_node"],"paths":{"/1/2/3/4":{"lot_name":"lot4","recursive":true},"/345":{"lot_name":"lot4","recursive":true},"/456":{"lot_name":"lot5","recursive":false},"/567":{"lot_name":"lot5","recursive":true},"/another/path":{"lot_name":"lot3","recursive":true},"/foo/barr":{"lot_name":"lot3","recursive":true},"/updated/path":{"lot_name":"lot3","recursive":false}},"usage":{"GB_being_written":{"children_contrib":3.4,"self_contrib":0.0},"dedicated_GB":{"children_contrib":8.64,"self_contrib":0.0,"total":8.64},"num_objects":{"children_contrib":10.0,"self_contrib":0.0},"objects_being_written":{"children_contrib":7.0,"self_contrib":0.0},"opportunistic_GB":{"children_contrib":0.0,"self_contrib":0.0,"total":0.0},"total_GB":{"children_contrib":8.64,"self_contrib":0.0}}})"_json;
ASSERT_TRUE(output_JSON == expected_output);
json expected_output = R"({"children":["lot4","lot5"],"lot_name":"lot3","management_policy_attrs":{"creation_time":{"lot_name":"lot3","value":123.0},"dedicated_GB":{"lot_name":"sep_node","value":3.0},"deletion_time":{"lot_name":"lot3","value":333.0},"expiration_time":{"lot_name":"lot3","value":222.0},"max_num_objects":{"lot_name":"sep_node","value":10.0},"opportunistic_GB":{"lot_name":"lot2","value":1.5}},"owners":["not owner1","owner1"],"parents":["lot1","lot2","sep_node"],"paths":[{"lot_name":"lot3","path":"/another/path","recursive":true},{"lot_name":"lot3","path":"/updated/path","recursive":false},{"lot_name":"lot3","path":"/foo/barr","recursive":true},{"lot_name":"lot4","path":"/1/2/3/4","recursive":true},{"lot_name":"lot4","path":"/345","recursive":true},{"lot_name":"lot5","path":"/456","recursive":false},{"lot_name":"lot5","path":"/567","recursive":true}],"usage":{"GB_being_written":{"children_contrib":3.4,"self_contrib":0.0},"dedicated_GB":{"children_contrib":8.64,"self_contrib":0.0,"total":8.64},"num_objects":{"children_contrib":10.0,"self_contrib":0.0},"objects_being_written":{"children_contrib":7.0,"self_contrib":0.0},"opportunistic_GB":{"children_contrib":0.0,"self_contrib":0.0,"total":0.0},"total_GB":{"children_contrib":8.64,"self_contrib":0.0}}})"_json;
ASSERT_TRUE(output_JSON == expected_output) << output_JSON;
}

TEST_F(LotManTest, LotsFromDirTest) {
Expand Down

0 comments on commit 0c95cd6

Please sign in to comment.