0

StorageBuckets: Converting int64_t to BucketId

This change updates bucket_id in QuotaDatabase methods
to use type BucketId instead of int64_t. BucketId was
introduced in https://crrev.com/c/2823950.

Change-Id: I5bc1a756740bf4d2078903ea0a9dc68705058aef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2906431
Reviewed-by: Marijn Kruisselbrink <mek@chromium.org>
Commit-Queue: Ayu Ishii <ayui@chromium.org>
Cr-Commit-Position: refs/heads/master@{#886309}
This commit is contained in:
Ayu Ishii
2021-05-25 16:12:26 +00:00
committed by Chromium LUCI CQ
parent ce91bcddd3
commit 3d28f50c43
3 changed files with 63 additions and 46 deletions

@ -93,20 +93,22 @@ const size_t QuotaDatabase::kIndexCount = base::size(QuotaDatabase::kIndexes);
QuotaDatabase::BucketTableEntry::BucketTableEntry() = default;
QuotaDatabase::BucketTableEntry::~BucketTableEntry() = default;
QuotaDatabase::BucketTableEntry::BucketTableEntry(const BucketTableEntry&) =
default;
QuotaDatabase::BucketTableEntry& QuotaDatabase::BucketTableEntry::operator=(
const QuotaDatabase::BucketTableEntry&) = default;
QuotaDatabase::BucketTableEntry::BucketTableEntry(
const int64_t bucket_id,
BucketId bucket_id,
url::Origin origin,
StorageType type,
std::string name,
int use_count,
const base::Time& last_accessed,
const base::Time& last_modified)
: bucket_id(bucket_id),
: bucket_id(std::move(bucket_id)),
origin(std::move(origin)),
type(type),
name(std::move(name)),
@ -291,9 +293,10 @@ bool QuotaDatabase::SetOriginLastAccessTime(const url::Origin& origin,
return true;
}
bool QuotaDatabase::SetBucketLastAccessTime(const int64_t bucket_id,
bool QuotaDatabase::SetBucketLastAccessTime(const BucketId bucket_id,
base::Time last_accessed) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!bucket_id.is_null());
if (!LazyOpen(true))
return false;
@ -307,7 +310,7 @@ bool QuotaDatabase::SetBucketLastAccessTime(const int64_t bucket_id,
sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
statement.BindInt(0, entry.use_count);
statement.BindTime(1, last_accessed);
statement.BindInt64(2, bucket_id);
statement.BindInt64(2, bucket_id.value());
if (!statement.Run())
return false;
@ -365,9 +368,10 @@ bool QuotaDatabase::SetOriginLastModifiedTime(const url::Origin& origin,
return true;
}
bool QuotaDatabase::SetBucketLastModifiedTime(const int64_t bucket_id,
bool QuotaDatabase::SetBucketLastModifiedTime(const BucketId bucket_id,
base::Time last_modified) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!bucket_id.is_null());
if (!LazyOpen(true))
return false;
@ -379,7 +383,7 @@ bool QuotaDatabase::SetBucketLastModifiedTime(const int64_t bucket_id,
"UPDATE buckets SET last_modified = ? WHERE id = ?";
sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
statement.BindTime(0, last_modified);
statement.BindInt64(1, bucket_id);
statement.BindInt64(1, bucket_id.value());
if (!statement.Run())
return false;
@ -517,15 +521,16 @@ bool QuotaDatabase::GetOriginInfo(const url::Origin& origin,
return false;
// TODO(crbug.com/889590): Use helper for url::Origin creation from string.
*entry = BucketTableEntry(statement.ColumnInt64(0), origin, type,
*entry = BucketTableEntry(BucketId(statement.ColumnInt64(0)), origin, type,
kDefaultBucket, statement.ColumnInt(1),
statement.ColumnTime(2), statement.ColumnTime(3));
return true;
}
bool QuotaDatabase::GetBucketInfo(const int64_t bucket_id,
bool QuotaDatabase::GetBucketInfo(const BucketId bucket_id,
QuotaDatabase::BucketTableEntry* entry) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!bucket_id.is_null());
if (!LazyOpen(false))
return false;
@ -542,7 +547,7 @@ bool QuotaDatabase::GetBucketInfo(const int64_t bucket_id,
"WHERE id = ?";
// clang-format on
sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
statement.BindInt64(0, bucket_id);
statement.BindInt64(0, bucket_id.value());
if (!statement.Step())
return false;
@ -595,14 +600,15 @@ bool QuotaDatabase::DeleteOriginInfo(const url::Origin& origin,
return true;
}
bool QuotaDatabase::DeleteBucketInfo(const int64_t bucket_id) {
bool QuotaDatabase::DeleteBucketInfo(const BucketId bucket_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!bucket_id.is_null());
if (!LazyOpen(false))
return false;
static constexpr char kSql[] = "DELETE FROM buckets WHERE id = ?";
sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
statement.BindInt64(0, bucket_id);
statement.BindInt64(0, bucket_id.value());
if (!statement.Run())
return false;
@ -655,7 +661,7 @@ bool QuotaDatabase::GetLRUOrigin(StorageType type,
bool QuotaDatabase::GetLRUBucket(StorageType type,
const std::set<url::Origin>& exceptions,
SpecialStoragePolicy* special_storage_policy,
absl::optional<int64_t>* bucket_id) {
absl::optional<BucketId>* bucket_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(bucket_id);
if (!LazyOpen(false))
@ -687,7 +693,7 @@ bool QuotaDatabase::GetLRUBucket(StorageType type,
continue;
}
*bucket_id = read_bucket_id;
*bucket_id = BucketId(read_bucket_id);
return true;
}
@ -727,7 +733,7 @@ bool QuotaDatabase::GetOriginsModifiedBetween(StorageType type,
}
bool QuotaDatabase::GetBucketsModifiedBetween(StorageType type,
std::set<int64_t>* bucket_ids,
std::set<BucketId>* bucket_ids,
base::Time begin,
base::Time end) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
@ -750,7 +756,7 @@ bool QuotaDatabase::GetBucketsModifiedBetween(StorageType type,
bucket_ids->clear();
while (statement.Step())
bucket_ids->insert(statement.ColumnInt64(0));
bucket_ids->insert(BucketId(statement.ColumnInt64(0)));
return statement.Succeeded();
}
@ -1009,7 +1015,7 @@ bool QuotaDatabase::DumpBucketTable(const BucketTableCallback& callback) {
sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
while (statement.Step()) {
BucketTableEntry entry(statement.ColumnInt64(0),
BucketTableEntry entry(BucketId(statement.ColumnInt64(0)),
url::Origin::Create(GURL(statement.ColumnString(1))),
static_cast<StorageType>(statement.ColumnInt(2)),
statement.ColumnString(3), statement.ColumnInt(4),

@ -46,18 +46,19 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDatabase {
public:
struct COMPONENT_EXPORT(STORAGE_BROWSER) BucketTableEntry {
BucketTableEntry();
BucketTableEntry(int64_t bucket_id,
BucketTableEntry(BucketId bucket_id,
url::Origin origin,
blink::mojom::StorageType type,
std::string name,
int use_count,
const base::Time& last_accessed,
const base::Time& last_modified);
~BucketTableEntry();
BucketTableEntry(const BucketTableEntry&);
BucketTableEntry& operator=(const BucketTableEntry&);
int64_t bucket_id = -1;
BucketId bucket_id;
url::Origin origin;
blink::mojom::StorageType type = blink::mojom::StorageType::kUnknown;
std::string name;
@ -102,7 +103,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDatabase {
// Called by QuotaClient implementers to update when the bucket was last
// accessed.
bool SetBucketLastAccessTime(int64_t bucket_id, base::Time last_accessed);
bool SetBucketLastAccessTime(BucketId bucket_id, base::Time last_accessed);
// TODO(crbug.com/1202167): Remove once all usages have updated to use
// SetBucketLastModifiedTime.
@ -112,7 +113,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDatabase {
// Called by QuotaClient implementers to update when the bucket was last
// modified.
bool SetBucketLastModifiedTime(int64_t bucket_id, base::Time last_modified);
bool SetBucketLastModifiedTime(BucketId bucket_id, base::Time last_modified);
bool GetOriginLastEvictionTime(const url::Origin& origin,
blink::mojom::StorageType type,
@ -141,7 +142,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDatabase {
// Gets the table entry for `bucket`. Returns whether the record for an
// origin bucket can be found.
bool GetBucketInfo(int64_t bucket_id, BucketTableEntry* entry);
bool GetBucketInfo(BucketId bucket_id, BucketTableEntry* entry);
// TODO(crbug.com/1202167): Remove once all usages have been updated to use
// DeleteBucketInfo. Deletes the default bucket for `origin`.
@ -149,7 +150,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDatabase {
blink::mojom::StorageType type);
// Deletes the specified bucket.
bool DeleteBucketInfo(int64_t bucket_id);
bool DeleteBucketInfo(BucketId bucket_id);
// TODO(crbug.com/1202167): Remove once all usages have been updated to use
// GetLRUBucket. Sets `origin` to the least recently used origin of origins
@ -169,7 +170,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDatabase {
bool GetLRUBucket(blink::mojom::StorageType type,
const std::set<url::Origin>& exceptions,
SpecialStoragePolicy* special_storage_policy,
absl::optional<int64_t>* bucket_id);
absl::optional<BucketId>* bucket_id);
// TODO(crbug.com/1202167): Remove once all usages have been updated to use
// GetBucketsModifiedBetween. Populates `origins` with the ones that have had
@ -183,7 +184,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaDatabase {
// Populates `bucket_ids` with the buckets that have been modified since the
// `begin` and until the `end`. Returns whether the operation succeeded.
bool GetBucketsModifiedBetween(blink::mojom::StorageType type,
std::set<int64_t>* bucket_ids,
std::set<BucketId>* bucket_ids,
base::Time begin,
base::Time end);

@ -134,7 +134,7 @@ class QuotaDatabaseTest : public testing::TestWithParam<bool> {
quota_database->db_->GetCachedStatement(SQL_FROM_HERE, kSql));
ASSERT_TRUE(statement.is_valid());
statement.BindInt64(0, entry.bucket_id);
statement.BindInt64(0, entry.bucket_id.value());
statement.BindString(1, entry.origin.GetURL().spec());
statement.BindInt(2, static_cast<int>(entry.type));
statement.BindString(3, entry.name);
@ -317,17 +317,21 @@ TEST_P(QuotaDatabaseTest, BucketLastAccessTimeLRU) {
EXPECT_TRUE(LazyOpen(&db, /*create_if_needed=*/true));
std::set<url::Origin> exceptions;
absl::optional<int64_t> bucket_id;
absl::optional<BucketId> bucket_id;
EXPECT_TRUE(db.GetLRUBucket(kTemp, exceptions, nullptr, &bucket_id));
EXPECT_FALSE(bucket_id.has_value());
// Insert bucket entries into BucketTable.
base::Time now(base::Time::Now());
base::Time now = base::Time::Now();
using Entry = QuotaDatabase::BucketTableEntry;
Entry bucket1 = Entry(0, ToOrigin("http://a/"), kTemp, "A", 99, now, now);
Entry bucket2 = Entry(1, ToOrigin("http://b/"), kTemp, "B", 0, now, now);
Entry bucket3 = Entry(2, ToOrigin("http://c/"), kTemp, "C", 1, now, now);
Entry bucket4 = Entry(3, ToOrigin("http://d/"), kPerm, "D", 5, now, now);
Entry bucket1 = Entry(BucketId(1), ToOrigin("http://example-a/"), kTemp,
"bucket_a", 99, now, now);
Entry bucket2 = Entry(BucketId(2), ToOrigin("http://example-b/"), kTemp,
"bucket_b", 0, now, now);
Entry bucket3 = Entry(BucketId(3), ToOrigin("http://example-c/"), kTemp,
"bucket_c", 1, now, now);
Entry bucket4 = Entry(BucketId(4), ToOrigin("http://example-d/"), kPerm,
"bucket_d", 5, now, now);
Entry kTableEntries[] = {bucket1, bucket2, bucket3, bucket4};
AssignBucketTable(&db, kTableEntries);
@ -484,18 +488,22 @@ TEST_P(QuotaDatabaseTest, BucketLastModifiedBetween) {
QuotaDatabase db(use_in_memory_db() ? base::FilePath() : DbPath());
EXPECT_TRUE(LazyOpen(&db, /*create_if_needed=*/true));
std::set<int64_t> bucket_ids;
std::set<BucketId> bucket_ids;
EXPECT_TRUE(db.GetBucketsModifiedBetween(kTemp, &bucket_ids, base::Time(),
base::Time::Max()));
EXPECT_TRUE(bucket_ids.empty());
// Insert bucket entries into BucketTable.
base::Time now(base::Time::Now());
base::Time now = base::Time::Now();
using Entry = QuotaDatabase::BucketTableEntry;
Entry bucket1 = Entry(0, ToOrigin("http://a/"), kTemp, "A", 0, now, now);
Entry bucket2 = Entry(1, ToOrigin("http://b/"), kTemp, "B", 0, now, now);
Entry bucket3 = Entry(2, ToOrigin("http://c/"), kTemp, "C", 0, now, now);
Entry bucket4 = Entry(3, ToOrigin("http://d/"), kPerm, "D", 0, now, now);
Entry bucket1 = Entry(BucketId(1), ToOrigin("http://example-a/"), kTemp,
"bucket_a", 0, now, now);
Entry bucket2 = Entry(BucketId(2), ToOrigin("http://example-b/"), kTemp,
"bucket_b", 0, now, now);
Entry bucket3 = Entry(BucketId(3), ToOrigin("http://example-c/"), kTemp,
"bucket_c", 0, now, now);
Entry bucket4 = Entry(BucketId(4), ToOrigin("http://example-d/"), kPerm,
"bucket_d", 0, now, now);
Entry kTableEntries[] = {bucket1, bucket2, bucket3, bucket4};
AssignBucketTable(&db, kTableEntries);
@ -662,13 +670,15 @@ TEST_P(QuotaDatabaseTest, DumpQuotaTable) {
}
TEST_P(QuotaDatabaseTest, DumpBucketTable) {
base::Time now(base::Time::Now());
base::Time now = base::Time::Now();
using Entry = QuotaDatabase::BucketTableEntry;
Entry kTableEntries[] = {
Entry(0, ToOrigin("http://go/"), kTemp, kDefaultBucket, 2147483647, now,
Entry(BucketId(1), ToOrigin("http://go/"), kTemp, kDefaultBucket,
2147483647, now, now),
Entry(BucketId(2), ToOrigin("http://oo/"), kTemp, kDefaultBucket, 0, now,
now),
Entry(BucketId(3), ToOrigin("http://gle/"), kTemp, kDefaultBucket, 1, now,
now),
Entry(1, ToOrigin("http://oo/"), kTemp, kDefaultBucket, 0, now, now),
Entry(2, ToOrigin("http://gle/"), kTemp, kDefaultBucket, 1, now, now),
};
QuotaDatabase db(use_in_memory_db() ? base::FilePath() : DbPath());
@ -685,8 +695,8 @@ TEST_P(QuotaDatabaseTest, DumpBucketTable) {
TEST_P(QuotaDatabaseTest, GetOriginInfo) {
const url::Origin kOrigin = ToOrigin("http://go/");
using Entry = QuotaDatabase::BucketTableEntry;
Entry kTableEntries[] = {Entry(0, kOrigin, kTemp, kDefaultBucket, 100,
base::Time(), base::Time())};
Entry kTableEntries[] = {Entry(BucketId(1), kOrigin, kTemp, kDefaultBucket,
100, base::Time(), base::Time())};
QuotaDatabase db(use_in_memory_db() ? base::FilePath() : DbPath());
EXPECT_TRUE(LazyOpen(&db, /*create_if_needed=*/true));
@ -712,8 +722,8 @@ TEST_P(QuotaDatabaseTest, GetOriginInfo) {
TEST_P(QuotaDatabaseTest, GetBucketInfo) {
using Entry = QuotaDatabase::BucketTableEntry;
Entry kTableEntries[] = {Entry(123, ToOrigin("http://go/"), kTemp,
"TestBucket", 100, base::Time(),
Entry kTableEntries[] = {Entry(BucketId(123), ToOrigin("http://go/"), kTemp,
"test_bucket", 100, base::Time(),
base::Time())};
QuotaDatabase db(use_in_memory_db() ? base::FilePath() : DbPath());
@ -734,7 +744,7 @@ TEST_P(QuotaDatabaseTest, GetBucketInfo) {
{
Entry entry;
EXPECT_FALSE(db.GetBucketInfo(456, &entry));
EXPECT_FALSE(db.GetBucketInfo(BucketId(456), &entry));
}
}