diff --git a/src/cache.cpp b/src/cache.cpp index 0f64a9cb0e..5c2effa444 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -338,21 +338,17 @@ bool StatCache::AddStat(const std::string& key, const headers_t& meta, bool forc } S3FS_PRN_INFO3("add stat cache entry[path=%s]", key.c_str()); - bool found; - bool do_truncate; - { - AutoLock lock(&StatCache::stat_cache_lock); - found = stat_cache.end() != stat_cache.find(key); - do_truncate = stat_cache.size() > CacheSize; - } + AutoLock lock(&StatCache::stat_cache_lock); - if(found){ - DelStat(key.c_str()); + if(stat_cache.end() != stat_cache.find(key)){ + // found cache + DelStat(key.c_str(), AutoLock::ALREADY_LOCKED); }else{ - if(do_truncate){ + // check: need to truncate cache + if(stat_cache.size() > CacheSize){ // cppcheck-suppress unmatchedSuppression // cppcheck-suppress knownConditionTrueFalse - if(!TruncateCache()){ + if(!TruncateCache(AutoLock::ALREADY_LOCKED)){ return false; } } @@ -386,9 +382,6 @@ bool StatCache::AddStat(const std::string& key, const headers_t& meta, bool forc } } - // add - AutoLock lock(&StatCache::stat_cache_lock); - const auto& value = stat_cache[key] = std::move(ent); // check symbolic link cache @@ -465,21 +458,17 @@ bool StatCache::AddNoObjectCache(const std::string& key) } S3FS_PRN_INFO3("add no object cache entry[path=%s]", key.c_str()); - bool found; - bool do_truncate; - { - AutoLock lock(&StatCache::stat_cache_lock); - found = stat_cache.end() != stat_cache.find(key); - do_truncate = stat_cache.size() > CacheSize; - } + AutoLock lock(&StatCache::stat_cache_lock); - if(found){ - DelStat(key.c_str()); + if(stat_cache.end() != stat_cache.find(key)){ + // found + DelStat(key.c_str(), AutoLock::ALREADY_LOCKED); }else{ - if(do_truncate){ + // check: need to truncate cache + if(stat_cache.size() > CacheSize){ // cppcheck-suppress unmatchedSuppression // cppcheck-suppress knownConditionTrueFalse - if(!TruncateCache()){ + if(!TruncateCache(AutoLock::ALREADY_LOCKED)){ return false; } } @@ -495,9 +484,6 @@ bool StatCache::AddNoObjectCache(const std::string& key) ent.meta.clear(); SetStatCacheTime(ent.cache_date); // Set time. - // add - AutoLock lock(&StatCache::stat_cache_lock); - stat_cache[key] = std::move(ent); // check symbolic link cache @@ -533,9 +519,9 @@ void StatCache::ChangeNoTruncateFlag(const std::string& key, bool no_truncate) } } -bool StatCache::TruncateCache() +bool StatCache::TruncateCache(AutoLock::Type locktype) { - AutoLock lock(&StatCache::stat_cache_lock); + AutoLock lock(&StatCache::stat_cache_lock, locktype); if(stat_cache.empty()){ return true; @@ -675,21 +661,17 @@ bool StatCache::AddSymlink(const std::string& key, const std::string& value) } S3FS_PRN_INFO3("add symbolic link cache entry[path=%s, value=%s]", key.c_str(), value.c_str()); - bool found; - bool do_truncate; - { - AutoLock lock(&StatCache::stat_cache_lock); - found = symlink_cache.end() != symlink_cache.find(key); - do_truncate = symlink_cache.size() > CacheSize; - } + AutoLock lock(&StatCache::stat_cache_lock); - if(found){ - DelSymlink(key.c_str()); + if(symlink_cache.end() != symlink_cache.find(key)){ + // found + DelSymlink(key.c_str(), AutoLock::ALREADY_LOCKED); }else{ - if(do_truncate){ + // check: need to truncate cache + if(symlink_cache.size() > CacheSize){ // cppcheck-suppress unmatchedSuppression // cppcheck-suppress knownConditionTrueFalse - if(!TruncateSymlink()){ + if(!TruncateSymlink(AutoLock::ALREADY_LOCKED)){ return false; } } @@ -701,17 +683,14 @@ bool StatCache::AddSymlink(const std::string& key, const std::string& value) ent.hit_count = 0; SetStatCacheTime(ent.cache_date); // Set time(use the same as Stats). - // add - AutoLock lock(&StatCache::stat_cache_lock); - symlink_cache[key] = std::move(ent); return true; } -bool StatCache::TruncateSymlink() +bool StatCache::TruncateSymlink(AutoLock::Type locktype) { - AutoLock lock(&StatCache::stat_cache_lock); + AutoLock lock(&StatCache::stat_cache_lock, locktype); if(symlink_cache.empty()){ return true; @@ -868,7 +847,7 @@ bool StatCache::GetNotruncateCache(const std::string& parentdir, notruncate_file notruncate_dir_map_t::iterator iter = notruncate_file_cache.find(dirpath); if(iter == notruncate_file_cache.end()){ - // nothing to add + // not found directory map return true; } diff --git a/src/cache.h b/src/cache.h index c3a03db2de..5157f7423d 100644 --- a/src/cache.h +++ b/src/cache.h @@ -108,9 +108,9 @@ class StatCache void Clear(); bool GetStat(const std::string& key, struct stat* pst, headers_t* meta, bool overcheck, const char* petag, bool* pisforce); // Truncate stat cache - bool TruncateCache(); + bool TruncateCache(AutoLock::Type locktype = AutoLock::NONE); // Truncate symbolic link cache - bool TruncateSymlink(); + bool TruncateSymlink(AutoLock::Type locktype = AutoLock::NONE); bool AddNotruncateCache(const std::string& key); bool DelNotruncateCache(const std::string& key);