|
| 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 "google/cloud/storage/bucket_metadata.h" |
| 17 | +#include <mutex> |
| 18 | +#include <utility> |
| 19 | + |
| 20 | +namespace google { |
| 21 | +namespace cloud { |
| 22 | +namespace storage_internal { |
| 23 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 24 | + |
| 25 | +BucketCacheEntry BucketCacheEntry::FromMetadata( |
| 26 | + storage::BucketMetadata const& m) { |
| 27 | + std::string loc = m.location(); |
| 28 | + if (m.location_type() == "multi-region" || |
| 29 | + m.location_type() == "dual-region") { |
| 30 | + loc = "global"; |
| 31 | + } |
| 32 | + return { |
| 33 | + "projects/" + std::to_string(m.project_number()) + "/buckets/" + m.name(), |
| 34 | + std::move(loc)}; |
| 35 | +} |
| 36 | + |
| 37 | +void BucketMetadataCache::MoveToFront(std::list<std::string>::iterator it) { |
| 38 | + list_.splice(list_.begin(), list_, it); |
| 39 | +} |
| 40 | + |
| 41 | +absl::optional<BucketCacheEntry> BucketMetadataCache::Get( |
| 42 | + std::string const& bucket_name) { |
| 43 | + std::unique_lock<std::mutex> lk(mu_); |
| 44 | + auto it = map_.find(bucket_name); |
| 45 | + if (it == map_.end()) return absl::nullopt; |
| 46 | + |
| 47 | + MoveToFront(it->second.second); |
| 48 | + return it->second.first; |
| 49 | +} |
| 50 | + |
| 51 | +void BucketMetadataCache::Put(std::string const& bucket_name, |
| 52 | + BucketCacheEntry entry) { |
| 53 | + if (max_size_ == 0) return; |
| 54 | + std::unique_lock<std::mutex> lk(mu_); |
| 55 | + auto it = map_.find(bucket_name); |
| 56 | + if (it != map_.end()) { |
| 57 | + it->second.first = std::move(entry); |
| 58 | + MoveToFront(it->second.second); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (map_.size() >= max_size_ && !list_.empty()) { |
| 63 | + auto oldest = list_.back(); |
| 64 | + list_.pop_back(); |
| 65 | + map_.erase(oldest); |
| 66 | + } |
| 67 | + |
| 68 | + list_.push_front(bucket_name); |
| 69 | + map_[bucket_name] = {std::move(entry), list_.begin()}; |
| 70 | +} |
| 71 | + |
| 72 | +void BucketMetadataCache::Invalidate(std::string const& bucket_name) { |
| 73 | + std::unique_lock<std::mutex> lk(mu_); |
| 74 | + auto it = map_.find(bucket_name); |
| 75 | + if (it != map_.end()) { |
| 76 | + list_.erase(it->second.second); |
| 77 | + map_.erase(it); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void BucketMetadataCache::Clear() { |
| 82 | + std::unique_lock<std::mutex> lk(mu_); |
| 83 | + map_.clear(); |
| 84 | + list_.clear(); |
| 85 | +} |
| 86 | + |
| 87 | +bool BucketMetadataCache::StartFetch(std::string const& bucket_name) { |
| 88 | + std::unique_lock<std::mutex> lk(mu_); |
| 89 | + if (in_flight_fetch_.find(bucket_name) != in_flight_fetch_.end()) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + in_flight_fetch_.insert(bucket_name); |
| 93 | + return true; |
| 94 | +} |
| 95 | + |
| 96 | +void BucketMetadataCache::EndFetch(std::string const& bucket_name) { |
| 97 | + std::unique_lock<std::mutex> lk(mu_); |
| 98 | + in_flight_fetch_.erase(bucket_name); |
| 99 | +} |
| 100 | + |
| 101 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 102 | +} // namespace storage_internal |
| 103 | +} // namespace cloud |
| 104 | +} // namespace google |
0 commit comments