Skip to content

Commit 58b3a28

Browse files
committed
more refactoring
1 parent b41469e commit 58b3a28

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

google/cloud/storage/internal/bucket_metadata_cache.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ namespace cloud {
2121
namespace storage_internal {
2222
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2323

24+
void BucketMetadataCache::MoveToFront(std::list<std::string>::iterator it) {
25+
list_.splice(list_.begin(), list_, it);
26+
}
27+
2428
absl::optional<BucketCacheEntry> BucketMetadataCache::Get(
2529
std::string const& bucket_name) {
26-
std::lock_guard<std::mutex> lock(mu_);
30+
std::unique_lock<std::mutex> lk(mu_);
2731
auto it = map_.find(bucket_name);
2832
if (it == map_.end()) return absl::nullopt;
2933

30-
list_.erase(it->second.second);
31-
list_.push_front(bucket_name);
32-
it->second.second = list_.begin();
34+
MoveToFront(it->second.second);
3335
return it->second.first;
3436
}
3537

3638
void BucketMetadataCache::Put(std::string const& bucket_name,
3739
BucketCacheEntry entry) {
38-
std::lock_guard<std::mutex> lock(mu_);
40+
std::unique_lock<std::mutex> lk(mu_);
3941
auto it = map_.find(bucket_name);
4042
if (it != map_.end()) {
4143
it->second.first = std::move(entry);
42-
list_.erase(it->second.second);
43-
list_.push_front(bucket_name);
44-
it->second.second = list_.begin();
44+
MoveToFront(it->second.second);
4545
return;
4646
}
4747

@@ -56,7 +56,7 @@ void BucketMetadataCache::Put(std::string const& bucket_name,
5656
}
5757

5858
void BucketMetadataCache::Invalidate(std::string const& bucket_name) {
59-
std::lock_guard<std::mutex> lock(mu_);
59+
std::unique_lock<std::mutex> lk(mu_);
6060
auto it = map_.find(bucket_name);
6161
if (it != map_.end()) {
6262
list_.erase(it->second.second);
@@ -65,14 +65,14 @@ void BucketMetadataCache::Invalidate(std::string const& bucket_name) {
6565
}
6666

6767
void BucketMetadataCache::Clear() {
68-
std::lock_guard<std::mutex> lock(mu_);
68+
std::unique_lock<std::mutex> lk(mu_);
6969
map_.clear();
7070
list_.clear();
7171
in_flight_fetch_.clear();
7272
}
7373

7474
bool BucketMetadataCache::StartFetch(std::string const& bucket_name) {
75-
std::lock_guard<std::mutex> lock(mu_);
75+
std::unique_lock<std::mutex> lk(mu_);
7676
if (in_flight_fetch_.find(bucket_name) != in_flight_fetch_.end()) {
7777
return false;
7878
}
@@ -81,7 +81,7 @@ bool BucketMetadataCache::StartFetch(std::string const& bucket_name) {
8181
}
8282

8383
void BucketMetadataCache::EndFetch(std::string const& bucket_name) {
84-
std::lock_guard<std::mutex> lock(mu_);
84+
std::unique_lock<std::mutex> lk(mu_);
8585
in_flight_fetch_.erase(bucket_name);
8686
}
8787

google/cloud/storage/internal/bucket_metadata_cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class BucketMetadataCache {
4848
void EndFetch(std::string const& bucket_name);
4949

5050
private:
51+
void MoveToFront(std::list<std::string>::iterator it);
52+
5153
std::size_t max_size_;
5254
std::mutex mu_;
5355
std::list<std::string> list_;

google/cloud/storage/internal/tracing_connection.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "google/cloud/internal/opentelemetry.h"
1919
#include "google/cloud/options.h"
2020
#include <algorithm>
21-
#include <iostream>
2221
#include <memory>
2322
#include <string>
2423
#include <utility>
@@ -48,7 +47,7 @@ void TracingConnection::ResetCacheForTesting() { cache().Clear(); }
4847
Options TracingConnection::options() const { return impl_->options(); }
4948

5049
void TracingConnection::CleanupCompletedTasks() {
51-
std::lock_guard<std::mutex> lock(mu_);
50+
std::unique_lock<std::mutex> lk(mu_);
5251
bg_tasks_.erase(std::remove_if(bg_tasks_.begin(), bg_tasks_.end(),
5352
[](std::future<void> const& f) {
5453
return f.wait_for(std::chrono::seconds(0)) ==
@@ -71,14 +70,6 @@ void TracingConnection::MaybeTriggerBackgroundFetch(
7170
google::cloud::internal::OptionsSpan span(current_options);
7271
storage::internal::GetBucketMetadataRequest request(bucket_name);
7372
auto result = impl_->GetBucketMetadata(request);
74-
std::cout << "BG Thread: GetBucketMetadata returned ok: " << result.ok()
75-
<< "\n";
76-
if (!result.ok()) {
77-
std::cout << "BG Thread: GetBucketMetadata status: "
78-
<< result.status().message()
79-
<< " code: " << static_cast<int>(result.status().code())
80-
<< "\n";
81-
}
8273

8374
BucketCacheEntry entry;
8475
if (result.ok()) {

0 commit comments

Comments
 (0)