Google Cloud Storage C++ Client  1.32.1
A C++ Client Library for Google Cloud Storage
lifecycle_rule.cc
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/storage/lifecycle_rule.h"
16 #include "google/cloud/internal/absl_str_join_quiet.h"
17 #include <algorithm>
18 #include <iostream>
19 
20 namespace google {
21 namespace cloud {
22 namespace storage {
23 inline namespace STORAGE_CLIENT_NS {
24 std::ostream& operator<<(std::ostream& os, LifecycleRuleAction const& rhs) {
25  return os << "LifecycleRuleAction={" << rhs.type << ", " << rhs.storage_class
26  << "}";
27 }
28 
30  return LifecycleRuleAction{"Delete", {}};
31 }
32 
35 }
36 
39 }
40 
43 }
44 
47 }
48 
51 }
52 
55 }
56 
59 }
60 
61 LifecycleRuleAction LifecycleRule::SetStorageClass(std::string storage_class) {
62  return LifecycleRuleAction{"SetStorageClass", std::move(storage_class)};
63 }
64 
65 std::ostream& operator<<(std::ostream& os, LifecycleRuleCondition const& rhs) {
66  os << "LifecycleRuleCondition={";
67  char const* sep = "";
68  if (rhs.age.has_value()) {
69  os << sep << "age=" << *rhs.age;
70  sep = ", ";
71  }
72  if (rhs.created_before.has_value()) {
73  os << sep << "created_before=" << *rhs.created_before;
74  sep = ", ";
75  }
76  if (rhs.is_live.has_value()) {
77  auto flags = os.flags();
78  os << sep << "is_live=" << std::boolalpha << *rhs.is_live;
79  os.flags(flags);
80  sep = ", ";
81  }
82  if (rhs.matches_storage_class.has_value()) {
83  os << sep << "matches_storage_class=[";
84  os << absl::StrJoin(*rhs.matches_storage_class, ", ");
85  os << "]";
86  sep = ", ";
87  }
88  if (rhs.num_newer_versions.has_value()) {
89  os << sep << "num_newer_versions=" << *rhs.num_newer_versions;
90  sep = ", ";
91  }
92  if (rhs.days_since_noncurrent_time.has_value()) {
93  os << sep
94  << "days_since_noncurrent_time=" << *rhs.days_since_noncurrent_time;
95  sep = ", ";
96  }
97  if (rhs.noncurrent_time_before.has_value()) {
98  os << sep << "noncurrent_time_before=" << *rhs.noncurrent_time_before;
99  sep = ", ";
100  }
101  if (rhs.days_since_custom_time.has_value()) {
102  os << sep << "days_since_custom_time=" << *rhs.days_since_custom_time;
103  sep = ", ";
104  }
105  if (rhs.custom_time_before.has_value()) {
106  os << sep << "custom_time_before=" << *rhs.custom_time_before;
107  }
108  return os << "}";
109 }
110 
111 void LifecycleRule::MergeConditions(LifecycleRuleCondition& result,
112  LifecycleRuleCondition const& rhs) {
113  if (rhs.age.has_value()) {
114  if (result.age.has_value()) {
115  result.age = std::min(*result.age, *rhs.age);
116  } else {
117  auto tmp = *rhs.age;
118  result.age.emplace(std::forward<std::int32_t>(tmp));
119  }
120  }
121  if (rhs.created_before.has_value()) {
122  if (result.created_before.has_value()) {
123  result.created_before =
124  std::max(*result.created_before, *rhs.created_before);
125  } else {
126  result.created_before.emplace(std::move(*rhs.created_before));
127  }
128  }
129  if (rhs.is_live.has_value()) {
130  if (result.is_live.has_value()) {
131  if (result.is_live.value() != rhs.is_live.value()) {
132  google::cloud::internal::ThrowInvalidArgument(
133  "Cannot set is_live to both true and false in LifecycleRule "
134  "condition");
135  }
136  } else {
137  auto tmp = *rhs.is_live;
138  result.is_live.emplace(std::forward<bool>(tmp));
139  }
140  }
141  if (rhs.matches_storage_class.has_value()) {
142  if (result.matches_storage_class.has_value()) {
143  std::vector<std::string> a;
144  a.swap(*result.matches_storage_class);
145  std::sort(a.begin(), a.end());
146  std::vector<std::string> b = *rhs.matches_storage_class;
147  std::sort(b.begin(), b.end());
148  std::vector<std::string> tmp;
149  std::set_intersection(a.begin(), a.end(), b.begin(), b.end(),
150  std::back_inserter(tmp));
151  result.matches_storage_class.emplace(std::move(tmp));
152  } else {
153  auto tmp = *rhs.matches_storage_class;
154  result.matches_storage_class.emplace(std::move(tmp));
155  }
156  }
157  if (rhs.num_newer_versions.has_value()) {
158  if (result.num_newer_versions.has_value()) {
159  result.num_newer_versions =
160  std::max(*result.num_newer_versions, *rhs.num_newer_versions);
161  } else {
162  auto tmp = *rhs.num_newer_versions;
163  result.num_newer_versions.emplace(std::forward<std::int32_t>(tmp));
164  }
165  }
166  if (rhs.days_since_noncurrent_time.has_value()) {
167  if (result.days_since_noncurrent_time.has_value()) {
168  result.days_since_noncurrent_time = (std::max)(
170  } else {
172  }
173  }
174  if (rhs.noncurrent_time_before.has_value()) {
175  if (result.noncurrent_time_before.has_value()) {
176  result.noncurrent_time_before = (std::min)(*result.noncurrent_time_before,
178  } else {
180  }
181  }
182  if (rhs.days_since_custom_time.has_value()) {
183  if (result.days_since_custom_time.has_value()) {
184  result.days_since_custom_time = (std::max)(*result.days_since_custom_time,
186  } else {
188  }
189  }
190  if (rhs.custom_time_before.has_value()) {
191  if (result.custom_time_before.has_value()) {
192  result.custom_time_before =
193  (std::min)(*result.custom_time_before, *rhs.custom_time_before);
194  } else {
196  }
197  }
198 }
199 
200 std::ostream& operator<<(std::ostream& os, LifecycleRule const& rhs) {
201  return os << "LifecycleRule={condition=" << rhs.condition()
202  << ", action=" << rhs.action() << "}";
203 }
204 
205 } // namespace STORAGE_CLIENT_NS
206 } // namespace storage
207 } // namespace cloud
208 } // namespace google