1818#include " google/cloud/storage/internal/storage_connection.h"
1919#include " google/cloud/storage/parallel_upload.h"
2020#include " google/cloud/storage/version.h"
21- #include " absl/types/optional.h"
22- #include < future>
23- #include < list>
2421#include < memory>
2522#include < mutex>
2623#include < string>
@@ -33,72 +30,6 @@ namespace cloud {
3330namespace storage_internal {
3431GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3532
36- struct BucketCacheEntry {
37- std::string id;
38- std::string location;
39- };
40-
41- class BucketMetadataCache {
42- public:
43- explicit BucketMetadataCache (std::size_t max_size = 10000 )
44- : max_size_(max_size) {}
45-
46- absl::optional<BucketCacheEntry> Get (std::string const & bucket_name) {
47- std::lock_guard<std::mutex> lock (mu_);
48- auto it = map_.find (bucket_name);
49- if (it == map_.end ()) return absl::nullopt ;
50-
51- list_.erase (it->second .second );
52- list_.push_front (bucket_name);
53- it->second .second = list_.begin ();
54- return it->second .first ;
55- }
56-
57- void Put (std::string const & bucket_name, BucketCacheEntry entry) {
58- std::lock_guard<std::mutex> lock (mu_);
59- auto it = map_.find (bucket_name);
60- if (it != map_.end ()) {
61- it->second .first = entry;
62- list_.erase (it->second .second );
63- list_.push_front (bucket_name);
64- it->second .second = list_.begin ();
65- return ;
66- }
67-
68- if (map_.size () >= max_size_) {
69- auto oldest = list_.back ();
70- list_.pop_back ();
71- map_.erase (oldest);
72- }
73-
74- list_.push_front (bucket_name);
75- map_[bucket_name] = {std::move (entry), list_.begin ()};
76- }
77-
78- void Invalidate (std::string const & bucket_name) {
79- std::lock_guard<std::mutex> lock (mu_);
80- auto it = map_.find (bucket_name);
81- if (it != map_.end ()) {
82- list_.erase (it->second .second );
83- map_.erase (it);
84- }
85- }
86-
87- void Clear () {
88- std::lock_guard<std::mutex> lock (mu_);
89- map_.clear ();
90- list_.clear ();
91- }
92-
93- private:
94- std::size_t max_size_;
95- std::mutex mu_;
96- std::list<std::string> list_;
97- std::unordered_map<std::string, std::pair<BucketCacheEntry,
98- std::list<std::string>::iterator>>
99- map_;
100- };
101-
10233class TracingConnection : public storage ::internal::StorageConnection {
10334 public:
10435 explicit TracingConnection (std::shared_ptr<StorageConnection> impl);
@@ -252,33 +183,7 @@ class TracingConnection : public storage::internal::StorageConnection {
252183 std::vector<std::string> InspectStackStructure () const override ;
253184
254185 private:
255- void EnrichSpan (opentelemetry::trace::Span& span,
256- std::string const & bucket_name);
257- void EnrichSpan (opentelemetry::trace::Span& span,
258- storage::BucketMetadata const & metadata);
259- void MaybeTriggerBackgroundFetch (std::string const & bucket_name);
260- void CleanupCompletedTasks ();
261-
262- template <typename T>
263- void MaybeInvalidate (StatusOr<T> const & result,
264- std::string const & bucket_name) {
265- if (!result.ok () && result.status ().code () == StatusCode::kNotFound ) {
266- cache ().Invalidate (bucket_name);
267- }
268- }
269-
270- void MaybeInvalidate (Status const & status, std::string const & bucket_name) {
271- if (!status.ok () && status.code () == StatusCode::kNotFound ) {
272- cache ().Invalidate (bucket_name);
273- }
274- }
275-
276- static BucketMetadataCache& cache ();
277-
278186 std::shared_ptr<StorageConnection> impl_;
279- std::mutex mu_;
280- std::unordered_set<std::string> in_flight_fetch_;
281- std::vector<std::future<void >> bg_tasks_;
282187};
283188
284189std::shared_ptr<storage::internal::StorageConnection> MakeTracingClient (
0 commit comments