diff options
author | yuzo@chromium.org <yuzo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-02 04:27:53 +0000 |
---|---|---|
committer | yuzo@chromium.org <yuzo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-02 04:27:53 +0000 |
commit | 25a5dcb7ce35ca15a9e4db1c13e8ec0db3b387b4 (patch) | |
tree | 5bc68d43f3a1cfb468c9c51b55ce56ab78a2ce34 /chrome/browser | |
parent | fc2eedbe7f2895cca80d8eb2d8364551ec49f925 (diff) | |
download | chromium_src-25a5dcb7ce35ca15a9e4db1c13e8ec0db3b387b4.zip chromium_src-25a5dcb7ce35ca15a9e4db1c13e8ec0db3b387b4.tar.gz chromium_src-25a5dcb7ce35ca15a9e4db1c13e8ec0db3b387b4.tar.bz2 |
Refactor MetaTableHelper to remove redundant table name prefixing with database
name. Also, PrimeCache logic duplicated in HistoryBackend and
SQLitePersistentCookieStore is moved to this class.
BUG=none
TEST=n/a
Review URL: http://codereview.chromium.org/150094
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19802 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/meta_table_helper.cc | 52 | ||||
-rw-r--r-- | chrome/browser/meta_table_helper.h | 11 | ||||
-rw-r--r-- | chrome/browser/meta_table_helper_unittest.cc | 28 |
3 files changed, 74 insertions, 17 deletions
diff --git a/chrome/browser/meta_table_helper.cc b/chrome/browser/meta_table_helper.cc index 6667197..5ce6aea 100644 --- a/chrome/browser/meta_table_helper.cc +++ b/chrome/browser/meta_table_helper.cc @@ -12,6 +12,24 @@ static const char kVersionKey[] = "version"; static const char kCompatibleVersionKey[] = "last_compatible_version"; +// static +void MetaTableHelper::PrimeCache(const std::string& db_name, sqlite3* db) { + // A statement must be open for the preload command to work. If the meta + // table doesn't exist, it probably means this is a new database and there + // is nothing to preload (so it's OK we do nothing). + SQLStatement dummy; + if (!DoesSqliteTableExist(db, db_name.c_str(), "meta")) + return; + std::string sql("SELECT * from "); + appendMetaTableName(db_name, &sql); + if (dummy.prepare(db, sql.c_str()) != SQLITE_OK) + return; + if (dummy.step() != SQLITE_ROW) + return; + + sqlite3Preload(db); +} + MetaTableHelper::MetaTableHelper() : db_(NULL) { } @@ -28,13 +46,9 @@ bool MetaTableHelper::Init(const std::string& db_name, if (!DoesSqliteTableExist(db_, db_name.c_str(), "meta")) { // Build the sql. std::string sql("CREATE TABLE "); - if (!db_name.empty()) { - // Want a table name of the form db_name.meta - sql.append(db_name); - sql.push_back('.'); - } - sql.append("meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," - "value LONGVARCHAR)"); + appendMetaTableName(db_name, &sql); + sql.append("(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," + "value LONGVARCHAR)"); if (sqlite3_exec(db_, sql.c_str(), NULL, NULL, NULL) != SQLITE_OK) return false; @@ -130,15 +144,22 @@ int MetaTableHelper::GetCompatibleVersionNumber() { return version; } +// static +void MetaTableHelper::appendMetaTableName(const std::string& db_name, + std::string* sql) { + if (!db_name.empty()) { + sql->append(db_name); + sql->push_back('.'); + } + sql->append("meta"); +} + bool MetaTableHelper::PrepareSetStatement(SQLStatement* statement, const std::string& key) { DCHECK(db_ && statement); std::string sql("INSERT OR REPLACE INTO "); - if (db_name_.size() > 0) { - sql.append(db_name_); - sql.push_back('.'); - } - sql.append("meta(key,value) VALUES(?,?)"); + appendMetaTableName(db_name_, &sql); + sql.append("(key,value) VALUES(?,?)"); if (statement->prepare(db_, sql.c_str()) != SQLITE_OK) { NOTREACHED() << sqlite3_errmsg(db_); return false; @@ -151,11 +172,8 @@ bool MetaTableHelper::PrepareGetStatement(SQLStatement* statement, const std::string& key) { DCHECK(db_ && statement); std::string sql("SELECT value FROM "); - if (db_name_.size() > 0) { - sql.append(db_name_); - sql.push_back('.'); - } - sql.append("meta WHERE key = ?"); + appendMetaTableName(db_name_, &sql); + sql.append(" WHERE key = ?"); if (statement->prepare(db_, sql.c_str()) != SQLITE_OK) { NOTREACHED() << sqlite3_errmsg(db_); return false; diff --git a/chrome/browser/meta_table_helper.h b/chrome/browser/meta_table_helper.h index 027ea5d..a55f136 100644 --- a/chrome/browser/meta_table_helper.h +++ b/chrome/browser/meta_table_helper.h @@ -20,6 +20,12 @@ class SQLStatement; // database to use. class MetaTableHelper { public: + // Primes the sqlite cache by filling it with the file in sequence + // until there is no more data or the cache is full. Since this is one + // contiguous read operation, it is much faster than letting the pages come + // in on-demand (which causes lots of seeks). + static void PrimeCache(const std::string& db_name, sqlite3* db); + // Creates a new MetaTableHelper. After construction you must invoke // Init with the appropriate database. MetaTableHelper(); @@ -69,6 +75,11 @@ class MetaTableHelper { bool GetValue(const std::string& key, int64* value); private: + friend class MetaTableHelperTest; + + // Appends the meta table name to the SQL statement. + static void appendMetaTableName(const std::string& db_name, std::string* sql); + // Conveniences to prepare the two types of statements used by // MetaTableHelper. bool PrepareSetStatement(SQLStatement* statement, const std::string& key); diff --git a/chrome/browser/meta_table_helper_unittest.cc b/chrome/browser/meta_table_helper_unittest.cc new file mode 100644 index 0000000..0d8ae5b --- /dev/null +++ b/chrome/browser/meta_table_helper_unittest.cc @@ -0,0 +1,28 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Tests for MetaTableHelper. + +#include "chrome/browser/meta_table_helper.h" +#include "testing/gtest/include/gtest/gtest.h" + +class MetaTableHelperTest : public testing::Test { + public: + static void appendMetaTableName(const std::string& db_name, + std::string* sql) { + MetaTableHelper::appendMetaTableName(db_name, sql); + } +}; + +TEST_F(MetaTableHelperTest, EmptyDbName) { + std::string sql("select * from "); + MetaTableHelperTest::appendMetaTableName(std::string(), &sql); + EXPECT_EQ("select * from meta", sql); +} + +TEST_F(MetaTableHelperTest, NonEmptyDbName) { + std::string sql("select * from "); + MetaTableHelperTest::appendMetaTableName(std::string("mydb"), &sql); + EXPECT_EQ("select * from mydb.meta", sql); +} |