Commit 501268dd authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Image Fetcher: Add CacheOption enum to map CacheStrategy in protobuf.

CacheStrategy enum is defined in cached_image_metadata.proto, this
should not be used beyond ImageMetadataStoreLevelDB.

This CL adds a cpp enum that maps to CacheStrategy protobuf enum.

Bug: 1067049,1058534
Change-Id: If13ac706e3c1f0a8cd2376ce1d7e5d26c5b571c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2158064Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Reviewed-by: default avatarBrandon Wylie <wylieb@chromium.org>
Commit-Queue: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#761036}
parent c4b715cc
......@@ -224,7 +224,7 @@ void ImageCache::RunEvictionOnStartup() {
void ImageCache::RunEvictionWhenFull() {
// Storage is within limits, bail out.
if (metadata_store_->GetEstimatedSize(CacheStrategy::BEST_EFFORT) <
if (metadata_store_->GetEstimatedSize(CacheOption::kBestEffort) <
kCacheMaxSize) {
return;
}
......
......@@ -48,9 +48,9 @@ class ImageMetadataStore {
// Returns all the keys this store has.
virtual void GetAllKeys(KeysCallback callback) = 0;
// Returns the total size of what's in metadata for a given cache strategy,
// Returns the total size of what's in metadata for a given cache option,
// possibly incorrect.
virtual int64_t GetEstimatedSize(CacheStrategy cache_strategy) = 0;
virtual int64_t GetEstimatedSize(CacheOption cache_option) = 0;
// Deletes all metadata that's been cached before the boundary given as
// |expiration_time|.
......
......@@ -33,6 +33,15 @@ int64_t ToDatabaseTime(base::Time time) {
return time.since_origin().InMicroseconds();
}
CacheOption ToCacheOption(CacheStrategy cache_strategy) {
switch (cache_strategy) {
case CacheStrategy::BEST_EFFORT:
return CacheOption::kBestEffort;
case CacheStrategy::HOLD_UNTIL_EXPIRED:
return CacheOption::kHoldUntilExpired;
}
}
// The folder where the data will be stored on disk.
const char kImageDatabaseFolder[] = "cached_image_fetcher_images";
......@@ -125,9 +134,9 @@ void ImageMetadataStoreLevelDB::SaveImageMetadata(
metadata_proto.set_cache_strategy(CacheStrategy::HOLD_UNTIL_EXPIRED);
metadata_proto.set_expiration_interval(
expiration_interval->InMicroseconds());
estimated_size_[CacheStrategy::HOLD_UNTIL_EXPIRED] += data_size;
estimated_size_[CacheOption::kHoldUntilExpired] += data_size;
} else {
estimated_size_[CacheStrategy::BEST_EFFORT] += data_size;
estimated_size_[CacheOption::kBestEffort] += data_size;
}
auto entries_to_save = std::make_unique<MetadataKeyEntryVector>();
......@@ -177,9 +186,8 @@ void ImageMetadataStoreLevelDB::GetAllKeys(KeysCallback callback) {
std::move(callback)));
}
int64_t ImageMetadataStoreLevelDB::GetEstimatedSize(
CacheStrategy cache_strategy) {
return estimated_size_[cache_strategy];
int64_t ImageMetadataStoreLevelDB::GetEstimatedSize(CacheOption cache_option) {
return estimated_size_[cache_option];
}
void ImageMetadataStoreLevelDB::EvictImageMetadata(base::Time expiration_time,
......@@ -269,10 +277,10 @@ void ImageMetadataStoreLevelDB::EvictImageMetadataImpl(
}
std::vector<std::string> keys_to_remove;
std::map<CacheStrategy, std::vector<const CachedImageMetadataProto*>>
std::map<CacheOption, std::vector<const CachedImageMetadataProto*>>
entries_map;
for (const CachedImageMetadataProto& entry : *entries) {
entries_map[entry.cache_strategy()].push_back(&entry);
entries_map[ToCacheOption(entry.cache_strategy())].push_back(&entry);
}
for (auto& cache_strategy : entries_map) {
......@@ -294,7 +302,7 @@ void ImageMetadataStoreLevelDB::EvictImageMetadataImpl(
}
void ImageMetadataStoreLevelDB::GetMetadataToRemove(
CacheStrategy cache_strategy,
CacheOption cache_option,
std::vector<const CachedImageMetadataProto*> entries,
base::Time expiration_time,
const size_t bytes_left,
......@@ -303,8 +311,8 @@ void ImageMetadataStoreLevelDB::GetMetadataToRemove(
size_t total_bytes_stored = 0;
int64_t expiration_database_time = ToDatabaseTime(expiration_time);
switch (cache_strategy) {
case CacheStrategy::BEST_EFFORT:
switch (cache_option) {
case CacheOption::kBestEffort:
// Removes expired entries.
for (const CachedImageMetadataProto* entry : entries) {
DCHECK_EQ(entry->cache_strategy(), CacheStrategy::BEST_EFFORT);
......@@ -327,9 +335,9 @@ void ImageMetadataStoreLevelDB::GetMetadataToRemove(
total_bytes_stored -= entry->data_size();
}
}
estimated_size_[cache_strategy] = total_bytes_stored;
estimated_size_[cache_option] = total_bytes_stored;
break;
case CacheStrategy::HOLD_UNTIL_EXPIRED:
case CacheOption::kHoldUntilExpired:
int64_t now = ToDatabaseTime(clock_->Now());
int64_t total_size = 0;
for (const auto* entry : entries) {
......@@ -341,7 +349,7 @@ void ImageMetadataStoreLevelDB::GetMetadataToRemove(
total_size += entry->data_size();
}
}
estimated_size_[cache_strategy] = total_size;
estimated_size_[cache_option] = total_size;
break;
}
}
......
......@@ -64,7 +64,7 @@ class ImageMetadataStoreLevelDB : public ImageMetadataStore {
// which means it hasn't been calculated yet, or it's an over-estimate. In
// the case of an over estimate, it will be recified if you call into an
// eviction routine.
int64_t GetEstimatedSize(CacheStrategy cache_strategy) override;
int64_t GetEstimatedSize(CacheOption cache_option) override;
void EvictImageMetadata(base::Time expiration_time,
const size_t bytes_left,
KeysCallback callback) override;
......@@ -89,7 +89,7 @@ class ImageMetadataStoreLevelDB : public ImageMetadataStore {
KeysCallback callback,
bool success,
std::unique_ptr<std::vector<CachedImageMetadataProto>> entries);
void GetMetadataToRemove(CacheStrategy cache_strategy,
void GetMetadataToRemove(CacheOption cache_option,
std::vector<const CachedImageMetadataProto*> entries,
base::Time expiration_time,
const size_t bytes_left,
......@@ -98,7 +98,7 @@ class ImageMetadataStoreLevelDB : public ImageMetadataStore {
std::vector<std::string> deleted_keys,
bool success);
std::map<CacheStrategy, int64_t> estimated_size_;
std::map<CacheOption, int64_t> estimated_size_;
InitializationStatus initialization_status_;
std::unique_ptr<leveldb_proto::ProtoDatabase<CachedImageMetadataProto>>
database_;
......
......@@ -328,14 +328,14 @@ TEST_F(CachedImageFetcherImageMetadataStoreLevelDBTest, GetAllKeysLoadFailed) {
TEST_F(CachedImageFetcherImageMetadataStoreLevelDBTest, GetEstimatedSize) {
PrepareDatabase(true);
EXPECT_EQ(5, metadata_store()->GetEstimatedSize(CacheStrategy::BEST_EFFORT));
EXPECT_EQ(5, metadata_store()->GetEstimatedSize(CacheOption::kBestEffort));
metadata_store()->SaveImageMetadata(kOtherImageKey, 15,
/* needs_transcoding */ false,
base::TimeDelta::FromDays(7));
EXPECT_EQ(5, metadata_store()->GetEstimatedSize(CacheStrategy::BEST_EFFORT));
EXPECT_EQ(15, metadata_store()->GetEstimatedSize(
CacheStrategy::HOLD_UNTIL_EXPIRED));
EXPECT_EQ(5, metadata_store()->GetEstimatedSize(CacheOption::kBestEffort));
EXPECT_EQ(15,
metadata_store()->GetEstimatedSize(CacheOption::kHoldUntilExpired));
}
TEST_F(CachedImageFetcherImageMetadataStoreLevelDBTest,
......@@ -361,11 +361,10 @@ TEST_F(CachedImageFetcherImageMetadataStoreLevelDBTest, GarbageCollect) {
/* needs_transcoding */ false,
base::TimeDelta::FromSeconds(1));
db()->UpdateCallback(true);
EXPECT_EQ(metadata_store()->GetEstimatedSize(CacheStrategy::BEST_EFFORT),
EXPECT_EQ(metadata_store()->GetEstimatedSize(CacheOption::kBestEffort),
kImageDataLength);
EXPECT_EQ(
metadata_store()->GetEstimatedSize(CacheStrategy::HOLD_UNTIL_EXPIRED),
100);
EXPECT_EQ(metadata_store()->GetEstimatedSize(CacheOption::kHoldUntilExpired),
100);
// Calling GC with something to be collected.
EXPECT_CALL(
......@@ -381,9 +380,9 @@ TEST_F(CachedImageFetcherImageMetadataStoreLevelDBTest, GarbageCollect) {
ASSERT_FALSE(IsDataPresent(kImageKey));
ASSERT_FALSE(IsDataPresent(kOtherImageKey));
EXPECT_EQ(metadata_store()->GetEstimatedSize(CacheStrategy::BEST_EFFORT), 0);
EXPECT_EQ(
metadata_store()->GetEstimatedSize(CacheStrategy::HOLD_UNTIL_EXPIRED), 0);
EXPECT_EQ(metadata_store()->GetEstimatedSize(CacheOption::kBestEffort), 0);
EXPECT_EQ(metadata_store()->GetEstimatedSize(CacheOption::kHoldUntilExpired),
0);
}
TEST_F(CachedImageFetcherImageMetadataStoreLevelDBTest, GarbageCollectNoHits) {
......
......@@ -21,6 +21,13 @@ enum class InitializationStatus {
INIT_FAILURE,
};
// Controls how cached image fetcher manages disk cache files. Maps to
// CacheStrategy in cached_image_metadata.proto.
enum class CacheOption {
kBestEffort = 0,
kHoldUntilExpired = 1,
};
// Returns the resulting raw image data as a std::string. Data will be returned
// using move semantics. If |needs_transcoding| is true, this data must be
// decoded in a sandbox process.
......
......@@ -8,7 +8,8 @@ option optimize_for = LITE_RUNTIME;
package image_fetcher;
// Controls how cached image fetcher manages disk cache files.
// Controls how cached image fetcher manages disk cache files. Maps to
// CacheOption enum in cpp.
enum CacheStrategy {
// If reached storage space limit, the cache file may be deleted based on LRU
// eviction. The cache files also have a default expiration time after
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment