This source file includes following definitions.
- Init
- InitTables
- TrimMemory
- BeginTransaction
- CommitTransaction
- GetDB
- GetCurrentVersion
- EnsureCurrentVersion
#include <algorithm>
#include <string>
#include "base/strings/string_util.h"
#include "chrome/browser/history/archived_database.h"
#include "sql/transaction.h"
namespace history {
namespace {
static const int kCurrentVersionNumber = 4;
static const int kCompatibleVersionNumber = 2;
}
ArchivedDatabase::ArchivedDatabase() {
}
ArchivedDatabase::~ArchivedDatabase() {
}
bool ArchivedDatabase::Init(const base::FilePath& file_name) {
db_.set_page_size(4096);
db_.set_cache_size(64);
db_.set_exclusive_locking();
if (!db_.Open(file_name))
return false;
if (!InitTables()) {
db_.Close();
return false;
}
return true;
}
bool ArchivedDatabase::InitTables() {
sql::Transaction transaction(&db_);
if (!transaction.Begin())
return false;
if (!meta_table_.Init(&db_, kCurrentVersionNumber,
kCompatibleVersionNumber))
return false;
if (!CreateURLTable(false) || !InitVisitTable() ||
!InitKeywordSearchTermsTable())
return false;
CreateMainURLIndex();
CreateKeywordSearchTermsIndices();
if (EnsureCurrentVersion() != sql::INIT_OK)
return false;
return transaction.Commit();
}
void ArchivedDatabase::TrimMemory(bool aggressively) {
db_.TrimMemory(aggressively);
}
void ArchivedDatabase::BeginTransaction() {
db_.BeginTransaction();
}
void ArchivedDatabase::CommitTransaction() {
db_.CommitTransaction();
}
sql::Connection& ArchivedDatabase::GetDB() {
return db_;
}
int ArchivedDatabase::GetCurrentVersion() {
return kCurrentVersionNumber;
}
sql::InitStatus ArchivedDatabase::EnsureCurrentVersion() {
if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
LOG(WARNING) << "Archived database is too new.";
return sql::INIT_TOO_NEW;
}
int cur_version = meta_table_.GetVersionNumber();
if (cur_version == 1) {
if (!DropStarredIDFromURLs()) {
LOG(WARNING) << "Unable to update archived database to version 2.";
return sql::INIT_FAILURE;
}
++cur_version;
meta_table_.SetVersionNumber(cur_version);
meta_table_.SetCompatibleVersionNumber(
std::min(cur_version, kCompatibleVersionNumber));
}
if (cur_version == 2) {
++cur_version;
meta_table_.SetVersionNumber(cur_version);
}
if (cur_version == 3) {
if (!MigrateVisitsWithoutDuration()) {
LOG(WARNING) << "Unable to update archived database to version 4.";
return sql::INIT_FAILURE;
}
++cur_version;
meta_table_.SetVersionNumber(cur_version);
}
LOG_IF(WARNING, cur_version < kCurrentVersionNumber) <<
"Archived database version " << cur_version << " is too old to handle.";
return sql::INIT_OK;
}
}