Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[power bookmarks] Add database class for sync metadata
Change-Id: I7d2185d61ae92f79b321610212f2c11608e1152b Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4014852 Commit-Queue: Brandon Wylie <wylieb@chromium.org> Reviewed-by: bttk - <bttk@chromium.org> Reviewed-by: Rushan Suleymanov <rushans@google.com> Reviewed-by: Yuheng Huang <yuhengh@chromium.org> Cr-Commit-Position: refs/heads/main@{#1078411}
- Loading branch information
Brandon Wylie
authored and
Chromium LUCI CQ
committed
Dec 2, 2022
1 parent
a43252c
commit ab058af
Showing
8 changed files
with
351 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
include_rules = [ | ||
"+components/sync", | ||
"+sql", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
components/power_bookmarks/storage/power_bookmark_sync_metadata_database.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/power_bookmarks/storage/power_bookmark_sync_metadata_database.h" | ||
|
||
#include <memory> | ||
|
||
#include "base/big_endian.h" | ||
#include "base/logging.h" | ||
#include "base/time/time.h" | ||
#include "components/sync/model/metadata_batch.h" | ||
#include "components/sync/protocol/entity_metadata.pb.h" | ||
#include "sql/statement.h" | ||
|
||
namespace power_bookmarks { | ||
|
||
namespace { | ||
|
||
// Key in sql::MetaTable, the value is a serialization of syner::ModelTypeState, | ||
// which tracks the overall sync state of the power bookmark datatype. | ||
const char kPowerBookmarkModelTypeStateKey[] = "power_bookmark_type_state"; | ||
|
||
} // namespace | ||
|
||
// Description of database table: | ||
// | ||
// sync_metadata | ||
// storage_key The guid of an entry in the saves table. | ||
// value Serialized sync EntityMetadata, which tracks the sync | ||
// state of each power bookmark entity. | ||
PowerBookmarkSyncMetadataDatabase::PowerBookmarkSyncMetadataDatabase( | ||
sql::Database* db, | ||
sql::MetaTable* meta_table) | ||
: db_(db), meta_table_(meta_table) {} | ||
|
||
PowerBookmarkSyncMetadataDatabase::~PowerBookmarkSyncMetadataDatabase() = | ||
default; | ||
|
||
bool PowerBookmarkSyncMetadataDatabase::Init() { | ||
if (!db_->DoesTableExist("sync_metadata")) { | ||
static constexpr char kInitSql[] = | ||
// clang-format off | ||
"CREATE TABLE sync_metadata " | ||
"(storage_key TEXT PRIMARY KEY NOT NULL, value BLOB)"; | ||
// clang-format on | ||
DCHECK(db_->IsSQLValid(kInitSql)); | ||
|
||
if (!db_->Execute(kInitSql)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool PowerBookmarkSyncMetadataDatabase::UpdateEntityMetadata( | ||
syncer::ModelType model_type, | ||
const std::string& storage_key, | ||
const sync_pb::EntityMetadata& metadata) { | ||
DCHECK(!storage_key.empty()); | ||
|
||
static constexpr char kUpdateSyncMetadataSql[] = | ||
// clang-format off | ||
"INSERT OR REPLACE INTO sync_metadata " | ||
"(storage_key, value) VALUES(?, ?)"; | ||
// clang-format on | ||
DCHECK(db_->IsSQLValid(kUpdateSyncMetadataSql)); | ||
|
||
sql::Statement s(db_->GetUniqueStatement(kUpdateSyncMetadataSql)); | ||
s.BindString(0, storage_key); | ||
s.BindString(1, metadata.SerializeAsString()); | ||
|
||
return s.Run(); | ||
} | ||
|
||
bool PowerBookmarkSyncMetadataDatabase::ClearEntityMetadata( | ||
syncer::ModelType model_type, | ||
const std::string& storage_key) { | ||
DCHECK(!storage_key.empty()); | ||
|
||
static constexpr char kClearSyncMetadataSql[] = | ||
// clang-format off | ||
"DELETE FROM sync_metadata WHERE storage_key=?"; | ||
// clang-format on | ||
DCHECK(db_->IsSQLValid(kClearSyncMetadataSql)); | ||
|
||
sql::Statement s(db_->GetUniqueStatement(kClearSyncMetadataSql)); | ||
s.BindString(0, storage_key); | ||
|
||
return s.Run(); | ||
} | ||
|
||
bool PowerBookmarkSyncMetadataDatabase::UpdateModelTypeState( | ||
syncer::ModelType model_type, | ||
const sync_pb::ModelTypeState& model_type_state) { | ||
DCHECK_GT(meta_table_->GetVersionNumber(), 0); | ||
|
||
std::string serialized_state = model_type_state.SerializeAsString(); | ||
return meta_table_->SetValue(kPowerBookmarkModelTypeStateKey, | ||
serialized_state); | ||
} | ||
|
||
bool PowerBookmarkSyncMetadataDatabase::ClearModelTypeState( | ||
syncer::ModelType model_type) { | ||
DCHECK_GT(meta_table_->GetVersionNumber(), 0); | ||
return meta_table_->DeleteKey(kPowerBookmarkModelTypeStateKey); | ||
} | ||
|
||
bool PowerBookmarkSyncMetadataDatabase::GetAllEntityMetadata( | ||
syncer::MetadataBatch* metadata_batch) { | ||
DCHECK(metadata_batch); | ||
|
||
static constexpr char kGetAllEntityMetadataSql[] = | ||
// clang-format off | ||
"SELECT storage_key, value FROM sync_metadata"; | ||
// clang-format on | ||
DCHECK(db_->IsSQLValid(kGetAllEntityMetadataSql)); | ||
|
||
sql::Statement s(db_->GetUniqueStatement(kGetAllEntityMetadataSql)); | ||
|
||
while (s.Step()) { | ||
std::string storage_key = s.ColumnString(0); | ||
std::string serialized_metadata = s.ColumnString(1); | ||
auto entity_metadata = std::make_unique<sync_pb::EntityMetadata>(); | ||
if (!entity_metadata->ParseFromString(serialized_metadata)) { | ||
DLOG(WARNING) << "Failed to deserialize POWER_BOOKMARK model type " | ||
"sync_pb::EntityMetadata."; | ||
return false; | ||
} | ||
metadata_batch->AddMetadata(storage_key, std::move(entity_metadata)); | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace power_bookmarks |
74 changes: 74 additions & 0 deletions
74
components/power_bookmarks/storage/power_bookmark_sync_metadata_database.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2022 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_SYNC_METADATA_DATABASE_H_ | ||
#define COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_SYNC_METADATA_DATABASE_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/gtest_prod_util.h" | ||
#include "base/memory/raw_ptr.h" | ||
#include "components/sync/base/model_type.h" | ||
#include "components/sync/model/sync_metadata_store.h" | ||
#include "sql/meta_table.h" | ||
|
||
namespace sql { | ||
class Database; | ||
class MetaTable; | ||
} // namespace sql | ||
|
||
namespace syncer { | ||
class MetadataBatch; | ||
} // namespace syncer | ||
|
||
namespace power_bookmarks { | ||
|
||
class PowerBookmarkSyncMetadataDatabaseTest; | ||
|
||
// A sync metadata database maintains two things: the entity metadata table and | ||
// the datatype state, stored in the MetaTable. The entity metadata table | ||
// contains metadata (sync states) for each entity. The datatype state is the | ||
// overall state of the power bookmarks datatype. | ||
class PowerBookmarkSyncMetadataDatabase : public syncer::SyncMetadataStore { | ||
public: | ||
// After construction, Init() must be called before doing anything else to | ||
// make sure the database is initialized. | ||
PowerBookmarkSyncMetadataDatabase(sql::Database* db, | ||
sql::MetaTable* meta_table); | ||
|
||
PowerBookmarkSyncMetadataDatabase(const PowerBookmarkSyncMetadataDatabase&) = | ||
delete; | ||
PowerBookmarkSyncMetadataDatabase& operator=( | ||
const PowerBookmarkSyncMetadataDatabase&) = delete; | ||
|
||
~PowerBookmarkSyncMetadataDatabase() override; | ||
|
||
// Makes sure the tables and indices are properly set up. Must be called | ||
// before anything else. | ||
bool Init(); | ||
|
||
// syncer::SyncMetadataStore implementation. | ||
bool UpdateEntityMetadata(syncer::ModelType model_type, | ||
const std::string& storage_key, | ||
const sync_pb::EntityMetadata& metadata) override; | ||
bool ClearEntityMetadata(syncer::ModelType model_type, | ||
const std::string& storage_key) override; | ||
bool UpdateModelTypeState( | ||
syncer::ModelType model_type, | ||
const sync_pb::ModelTypeState& model_type_state) override; | ||
bool ClearModelTypeState(syncer::ModelType model_type) override; | ||
|
||
// Reads all sync_pb::EntityMetadata and fills `metadata_batch` with it. | ||
bool GetAllEntityMetadata(syncer::MetadataBatch* metadata_batch); | ||
|
||
private: | ||
friend class PowerBookmarkSyncMetadataDatabaseTest; | ||
|
||
const raw_ptr<sql::Database> db_; | ||
const raw_ptr<sql::MetaTable> meta_table_; | ||
}; | ||
|
||
} // namespace power_bookmarks | ||
|
||
#endif // COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_SYNC_METADATA_DATABASE_H_ |
Oops, something went wrong.