This source file includes following definitions.
- InitDB
- InitFromScratch
- InitFromDisk
- GetDB
#include "chrome/browser/history/in_memory_database.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
namespace history {
InMemoryDatabase::InMemoryDatabase() : URLDatabase() {
}
InMemoryDatabase::~InMemoryDatabase() {
}
bool InMemoryDatabase::InitDB() {
db_.set_page_size(4096);
if (!db_.OpenInMemory()) {
NOTREACHED() << "Cannot open databse " << GetDB().GetErrorMessage();
return false;
}
ignore_result(db_.Execute("PRAGMA auto_vacuum=1"));
ignore_result(db_.Execute("PRAGMA temp_store=MEMORY"));
if (!CreateURLTable(false)) {
NOTREACHED() << "Unable to create table";
db_.Close();
return false;
}
if (!InitKeywordSearchTermsTable()) {
NOTREACHED() << "Unable to create keyword search terms";
db_.Close();
return false;
}
return true;
}
bool InMemoryDatabase::InitFromScratch() {
if (!InitDB())
return false;
CreateMainURLIndex();
CreateKeywordSearchTermsIndices();
return true;
}
bool InMemoryDatabase::InitFromDisk(const base::FilePath& history_name) {
if (!InitDB())
return false;
sql::Statement attach(GetDB().GetUniqueStatement("ATTACH ? AS history"));
#if defined(OS_POSIX)
attach.BindString(0, history_name.value());
#else
attach.BindString(0, base::WideToUTF8(history_name.value()));
#endif
if (!attach.Run())
return false;
base::TimeTicks begin_load = base::TimeTicks::Now();
if (!db_.Execute(
"INSERT INTO urls SELECT * FROM history.urls WHERE typed_count > 0")) {
}
base::TimeTicks end_load = base::TimeTicks::Now();
UMA_HISTOGRAM_MEDIUM_TIMES("History.InMemoryDBPopulate",
end_load - begin_load);
UMA_HISTOGRAM_COUNTS("History.InMemoryDBItemCount", db_.GetLastChangeCount());
{
sql::Statement visit_count(db_.GetUniqueStatement(
"SELECT sum(visit_count) FROM urls"));
if (visit_count.Step()) {
UMA_HISTOGRAM_COUNTS("History.InMemoryTypedUrlVisitCount",
visit_count.ColumnInt(0));
}
}
begin_load = base::TimeTicks::Now();
if (!db_.Execute(
"INSERT OR IGNORE INTO urls SELECT u.id, u.url, u.title, u.visit_count, "
"u.typed_count, u.last_visit_time, u.hidden, u.favicon_id "
"FROM history.urls u JOIN history.keyword_search_terms kst "
"WHERE u.typed_count = 0 AND u.id = kst.url_id")) {
}
end_load = base::TimeTicks::Now();
UMA_HISTOGRAM_MEDIUM_TIMES("History.InMemoryDBKeywordURLPopulate",
end_load - begin_load);
UMA_HISTOGRAM_COUNTS("History.InMemoryDBKeywordURLItemCount",
db_.GetLastChangeCount());
begin_load = base::TimeTicks::Now();
if (!db_.Execute(
"INSERT INTO keyword_search_terms SELECT * FROM "
"history.keyword_search_terms")) {
}
end_load = base::TimeTicks::Now();
UMA_HISTOGRAM_MEDIUM_TIMES("History.InMemoryDBKeywordTermsPopulate",
end_load - begin_load);
UMA_HISTOGRAM_COUNTS("History.InMemoryDBKeywordTermsCount",
db_.GetLastChangeCount());
if (!db_.Execute("DETACH history")) {
NOTREACHED() << "Unable to detach from history database.";
return false;
}
CreateMainURLIndex();
CreateKeywordSearchTermsIndices();
return true;
}
sql::Connection& InMemoryDatabase::GetDB() {
return db_;
}
}