Skip to content

Commit ab9f016

Browse files
committed
move BucketMetadataCache to separate file
1 parent a2e7b31 commit ab9f016

4 files changed

Lines changed: 159 additions & 86 deletions

File tree

google/cloud/storage/google_cloud_cpp_storage.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ add_library(
7474
internal/bucket_access_control_parser.h
7575
internal/bucket_acl_requests.cc
7676
internal/bucket_acl_requests.h
77+
internal/bucket_metadata_cache.cc
78+
internal/bucket_metadata_cache.h
7779
internal/bucket_metadata_parser.cc
7880
internal/bucket_metadata_parser.h
7981
internal/bucket_requests.cc
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2026 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+
// https://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/internal/bucket_metadata_cache.h"
16+
#include <mutex>
17+
#include <utility>
18+
19+
namespace google {
20+
namespace cloud {
21+
namespace storage_internal {
22+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
23+
24+
absl::optional<BucketCacheEntry> BucketMetadataCache::Get(
25+
std::string const& bucket_name) {
26+
std::lock_guard<std::mutex> lock(mu_);
27+
auto it = map_.find(bucket_name);
28+
if (it == map_.end()) return absl::nullopt;
29+
30+
list_.erase(it->second.second);
31+
list_.push_front(bucket_name);
32+
it->second.second = list_.begin();
33+
return it->second.first;
34+
}
35+
36+
void BucketMetadataCache::Put(std::string const& bucket_name,
37+
BucketCacheEntry entry) {
38+
std::lock_guard<std::mutex> lock(mu_);
39+
auto it = map_.find(bucket_name);
40+
if (it != map_.end()) {
41+
it->second.first = std::move(entry);
42+
list_.erase(it->second.second);
43+
list_.push_front(bucket_name);
44+
it->second.second = list_.begin();
45+
return;
46+
}
47+
48+
if (map_.size() >= max_size_) {
49+
auto oldest = list_.back();
50+
list_.pop_back();
51+
map_.erase(oldest);
52+
}
53+
54+
list_.push_front(bucket_name);
55+
map_[bucket_name] = {std::move(entry), list_.begin()};
56+
}
57+
58+
void BucketMetadataCache::Invalidate(std::string const& bucket_name) {
59+
std::lock_guard<std::mutex> lock(mu_);
60+
auto it = map_.find(bucket_name);
61+
if (it != map_.end()) {
62+
list_.erase(it->second.second);
63+
map_.erase(it);
64+
}
65+
}
66+
67+
void BucketMetadataCache::Clear() {
68+
std::lock_guard<std::mutex> lock(mu_);
69+
map_.clear();
70+
list_.clear();
71+
in_flight_fetch_.clear();
72+
}
73+
74+
bool BucketMetadataCache::StartFetch(std::string const& bucket_name) {
75+
std::lock_guard<std::mutex> lock(mu_);
76+
if (in_flight_fetch_.find(bucket_name) != in_flight_fetch_.end()) {
77+
return false;
78+
}
79+
in_flight_fetch_.insert(bucket_name);
80+
return true;
81+
}
82+
83+
void BucketMetadataCache::EndFetch(std::string const& bucket_name) {
84+
std::lock_guard<std::mutex> lock(mu_);
85+
in_flight_fetch_.erase(bucket_name);
86+
}
87+
88+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
89+
} // namespace storage_internal
90+
} // namespace cloud
91+
} // namespace google
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2026 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+
// https://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+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_BUCKET_METADATA_CACHE_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_BUCKET_METADATA_CACHE_H
17+
18+
#include "google/cloud/storage/version.h"
19+
#include "absl/types/optional.h"
20+
#include <cstddef>
21+
#include <list>
22+
#include <mutex>
23+
#include <string>
24+
#include <unordered_map>
25+
#include <unordered_set>
26+
#include <utility>
27+
28+
namespace google {
29+
namespace cloud {
30+
namespace storage_internal {
31+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
32+
33+
struct BucketCacheEntry {
34+
std::string id;
35+
std::string location;
36+
};
37+
38+
class BucketMetadataCache {
39+
public:
40+
explicit BucketMetadataCache(std::size_t max_size = 10000)
41+
: max_size_(max_size) {}
42+
43+
absl::optional<BucketCacheEntry> Get(std::string const& bucket_name);
44+
void Put(std::string const& bucket_name, BucketCacheEntry entry);
45+
void Invalidate(std::string const& bucket_name);
46+
void Clear();
47+
bool StartFetch(std::string const& bucket_name);
48+
void EndFetch(std::string const& bucket_name);
49+
50+
private:
51+
std::size_t max_size_;
52+
std::mutex mu_;
53+
std::list<std::string> list_;
54+
std::unordered_map<std::string, std::pair<BucketCacheEntry,
55+
std::list<std::string>::iterator>>
56+
map_;
57+
std::unordered_set<std::string> in_flight_fetch_;
58+
};
59+
60+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
61+
} // namespace storage_internal
62+
} // namespace cloud
63+
} // namespace google
64+
65+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_BUCKET_METADATA_CACHE_H

google/cloud/storage/internal/tracing_connection.h

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,106 +15,21 @@
1515
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_TRACING_CONNECTION_H
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_TRACING_CONNECTION_H
1717

18+
#include "google/cloud/storage/internal/bucket_metadata_cache.h"
1819
#include "google/cloud/storage/internal/storage_connection.h"
1920
#include "google/cloud/storage/parallel_upload.h"
2021
#include "google/cloud/storage/version.h"
21-
#include "absl/types/optional.h"
2222
#include <future>
23-
#include <list>
2423
#include <memory>
2524
#include <mutex>
2625
#include <string>
27-
#include <unordered_map>
28-
#include <unordered_set>
2926
#include <vector>
3027

3128
namespace google {
3229
namespace cloud {
3330
namespace storage_internal {
3431
GOOGLE_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-
in_flight_fetch_.clear();
92-
}
93-
94-
bool StartFetch(std::string const& bucket_name) {
95-
std::lock_guard<std::mutex> lock(mu_);
96-
if (in_flight_fetch_.find(bucket_name) != in_flight_fetch_.end()) {
97-
return false;
98-
}
99-
in_flight_fetch_.insert(bucket_name);
100-
return true;
101-
}
102-
103-
void EndFetch(std::string const& bucket_name) {
104-
std::lock_guard<std::mutex> lock(mu_);
105-
in_flight_fetch_.erase(bucket_name);
106-
}
107-
108-
private:
109-
std::size_t max_size_;
110-
std::mutex mu_;
111-
std::list<std::string> list_;
112-
std::unordered_map<std::string, std::pair<BucketCacheEntry,
113-
std::list<std::string>::iterator>>
114-
map_;
115-
std::unordered_set<std::string> in_flight_fetch_;
116-
};
117-
11833
class TracingConnection : public storage::internal::StorageConnection {
11934
public:
12035
explicit TracingConnection(std::shared_ptr<StorageConnection> impl);

0 commit comments

Comments
 (0)