-
Notifications
You must be signed in to change notification settings - Fork 460
feat(storage): add sample for hierarchical namespace recursive delete #16402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d759528
b68189e
b13e3b9
b966473
2b1feb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,15 +30,16 @@ void RemoveStaleFolders( | |
| google::cloud::storagecontrol_v2::StorageControlClient client, | ||
| std::string const& bucket_name, std::string const& prefix, | ||
| std::chrono::system_clock::time_point created_time_limit) { | ||
| std::regex re(prefix + R"re(-[a-z]{32})re"); | ||
| auto const parent = std::string{"projects/_/buckets/"} + bucket_name; | ||
| std::regex re(parent + "/folders/" + prefix + | ||
| R"re(-(recursive-)?[a-z]{32})re"); | ||
| for (auto folder : client.ListFolders(parent)) { | ||
| if (!folder) throw std::move(folder).status(); | ||
| if (!std::regex_match(folder->name(), re)) continue; | ||
| auto const create_time = | ||
| google::cloud::internal::ToChronoTimePoint(folder->create_time()); | ||
| if (create_time > created_time_limit) continue; | ||
| (void)client.DeleteFolder(folder->name()); | ||
| (void)client.DeleteFolderRecursive(folder->name()).get(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -133,6 +134,27 @@ void RenameFolder(google::cloud::storagecontrol_v2::StorageControlClient client, | |
| (std::move(client), argv.at(0), argv.at(1), argv.at(2)); | ||
| } | ||
|
|
||
| void DeleteFolderRecursive( | ||
| google::cloud::storagecontrol_v2::StorageControlClient client, | ||
| std::vector<std::string> const& argv) { | ||
| // [START storage_control_delete_folder_recursive] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this sample consistent with other snippets in this file:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done Co-authored by AI Agent |
||
| namespace storagecontrol = google::cloud::storagecontrol_v2; | ||
| [](storagecontrol::StorageControlClient client, | ||
| std::string const& bucket_name, std::string const& folder_id) { | ||
| auto const name = std::string{"projects/_/buckets/"} + bucket_name + | ||
| "/folders/" + folder_id; | ||
| // Start a delete folder recursive operation and block until it completes. | ||
| // Real applications may want to setup a callback, wait on a coroutine, or | ||
| // poll until it completes. | ||
| auto result = client.DeleteFolderRecursive(name).get(); | ||
| if (!result) throw std::move(result).status(); | ||
|
|
||
| std::cout << "Deleted folder recursively: " << folder_id << "\n"; | ||
| } | ||
| // [END storage_control_delete_folder_recursive] | ||
| (std::move(client), argv.at(0), argv.at(1)); | ||
| } | ||
|
|
||
| void AutoRun(std::vector<std::string> const& argv) { | ||
| namespace examples = google::cloud::testing_util; | ||
| namespace storagecontrol = google::cloud::storagecontrol_v2; | ||
|
|
@@ -176,6 +198,42 @@ void AutoRun(std::vector<std::string> const& argv) { | |
|
|
||
| std::cout << "\nRunning DeleteFolder() example" << std::endl; | ||
| DeleteFolder(client, {bucket_name, dest_folder_id}); | ||
|
|
||
| auto const recursive_parent_id = | ||
| prefix + "-recursive-" + | ||
| google::cloud::internal::Sample(generator, 32, | ||
| "abcdefghijklmnopqrstuvwxyz"); | ||
| auto const recursive_child_id = recursive_parent_id + "/child"; | ||
|
|
||
| std::cout << "\nRunning CreateFolder() for recursive test parent" | ||
| << std::endl; | ||
| CreateFolder(client, {bucket_name, recursive_parent_id}); | ||
|
|
||
| std::cout << "\nRunning CreateFolder() for recursive test child" << std::endl; | ||
| CreateFolder(client, {bucket_name, recursive_child_id}); | ||
|
|
||
| std::cout << "\nRunning DeleteFolderRecursive() example" << std::endl; | ||
| DeleteFolderRecursive(client, {bucket_name, recursive_parent_id}); | ||
|
|
||
| // Verify deletion by checking that getting the parent and child folders fail | ||
| // with NOT_FOUND. | ||
| try { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's verify that recursive_child_id was also deleted and returns kNotFound.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done Co-authored by AI Agent |
||
| GetFolder(client, {bucket_name, recursive_parent_id}); | ||
| throw std::runtime_error("Parent folder was not deleted recursively"); | ||
| } catch (google::cloud::Status const& status) { | ||
| if (status.code() != google::cloud::StatusCode::kNotFound) { | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| GetFolder(client, {bucket_name, recursive_child_id}); | ||
| throw std::runtime_error("Child folder was not deleted recursively"); | ||
| } catch (google::cloud::Status const& status) { | ||
| if (status.code() != google::cloud::StatusCode::kNotFound) { | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
@@ -207,6 +265,8 @@ int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape) | |
| Example example({ | ||
| make_entry("create-folder", {"bucket-name", "folder-id"}, CreateFolder), | ||
| make_entry("delete-folder", {"bucket-name", "folder-id"}, DeleteFolder), | ||
| make_entry("delete-folder-recursive", {"bucket-name", "folder-id"}, | ||
| DeleteFolderRecursive), | ||
| make_entry("get-folder", {"bucket-name", "folder-id"}, GetFolder), | ||
| make_entry("list-folders", {"bucket-name"}, ListFolders), | ||
| make_entry("rename-folder", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, folder->name() returns the full resource name (e.g. projects/_/buckets//folders/<folder_id>). Because std::regex_match requires the full string to match the pattern, matching against
prefix + ...will fail.Here and below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Co-authored by AI Agent