Chromium Code Reviews| Index: content/browser/media_gallery/media_gallery_database.cc |
| diff --git a/content/browser/media_gallery/media_gallery_database.cc b/content/browser/media_gallery/media_gallery_database.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8051ebc6c7cd75e6b190a533e094f6e66aad4de9 |
| --- /dev/null |
| +++ b/content/browser/media_gallery/media_gallery_database.cc |
| @@ -0,0 +1,183 @@ |
| +// Copyright (c) 2012 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. |
| + |
| +#include "content/browser/media_gallery/media_gallery_database.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/file_path.h" |
| +#include "base/logging.h" |
| +#include "sql/diagnostic_error_delegate.h" |
| +#include "sql/statement.h" |
| +#include "sql/transaction.h" |
| + |
| +#define MEDIA_GALLERY_COLLECTION_ROW_FIELDS \ |
| + " collections.id, collections.path, collections.last_modified_time, " \ |
| + "collections.entry_count, collections.all_parsed " |
| + |
| +#define COLLECTION_TABLE_NAME "collections" |
|
vandebo (ex-Chrome)
2012/03/05 23:16:03
You've hard coded the column creation, what's the
tpayne
2012/03/06 01:52:37
Done.
|
| + |
| +std::ostream& operator<<(std::ostream& out, |
|
vandebo (ex-Chrome)
2012/03/05 23:16:03
In anonymous namespace?
tpayne
2012/03/06 01:52:37
Done.
|
| + const media_gallery::MediaGalleryDatabase::CollectionRow& row) { |
| + out << "Collection" |
| + << " id " << row.GetId() |
| + << " path " << row.GetPath().value() |
| + << " last modified time " << row.GetLastModifiedTime().ToInternalValue() |
| + << " entry count " << row.GetEntryCount() |
| + << " all parsed " << (row.IsAllParsed() ? "true" : "false") << std::endl; |
| + return out; |
| +} |
| + |
| +namespace media_gallery { |
| + |
| +namespace { |
| + |
| +static const int kCurrentVersionNumber = 1; |
| +static const int kCompatibleVersionNumber = 1; |
| +static const char kMediaGalleryDatabaseName[] = "media_gallery.db"; |
|
vandebo (ex-Chrome)
2012/03/05 23:16:03
nit: we might want to use a "hidden" name, i.e. st
tpayne
2012/03/06 01:52:37
Done.
|
| + |
| +class HistogramName { |
| + public: |
| + static const char* name() { |
| + return "Sqlite.MediaGallery.Error"; |
| + } |
| +}; |
| + |
| +} // namespace |
| + |
| +MediaGalleryDatabase::MediaGalleryDatabase() { } |
| + |
| +MediaGalleryDatabase::~MediaGalleryDatabase() { } |
| + |
| +sql::InitStatus MediaGalleryDatabase::Init(const FilePath& database_dir) { |
| + // Set the exceptional sqlite error handler. |
| + db_.set_error_delegate(new sql::DiagnosticErrorDelegate<HistogramName>()); |
| + |
| + // Set the database page size to something a little larger to give us |
| + // better performance (we're typically seek rather than bandwidth limited). |
| + // This only has an effect before any tables have been created, otherwise |
| + // this is a NOP. Must be a power of 2 and a max of 8192. |
| + db_.set_page_size(4096); |
| + |
| + // Increase the cache size. The page size, plus a little extra, times this |
| + // value, tells us how much memory the cache will use maximum. |
| + // 6000 * 4MB = 24MB |
|
vandebo (ex-Chrome)
2012/03/05 23:16:03
are you calling 4MB the page size here? set_page_
tpayne
2012/03/06 01:52:37
This is copied verbatim from url_database. Looks l
|
| + db_.set_cache_size(6000); |
| + |
| + if (!db_.Open(database_dir.Append(kMediaGalleryDatabaseName))) |
| + return sql::INIT_FAILURE; |
| + |
| + return InitInternal(&db_); |
| +} |
| + |
| +sql::InitStatus MediaGalleryDatabase::InitInternal(sql::Connection* db) { |
| + // Wrap the rest of init in a tranaction. This will prevent the database from |
| + // getting corrupted if we crash in the middle of initialization or migration. |
| + sql::Transaction committer(db); |
| + if (!committer.Begin()) |
| + return sql::INIT_FAILURE; |
| + |
| + // Prime the cache. |
| + db->Preload(); |
| + |
| + // Create the tables and indices. |
| + if (!meta_table_.Init(db, GetCurrentVersion(), kCompatibleVersionNumber)) |
| + return sql::INIT_FAILURE; |
| + |
| + if (!CreateCollectionsTable(db)) { |
| + return sql::INIT_FAILURE; |
| + } |
| + |
| + // Version check. |
| + sql::InitStatus version_status = EnsureCurrentVersion(); |
| + if (version_status != sql::INIT_OK) |
| + return version_status; |
| + |
| + return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
| +} |
| + |
| +sql::InitStatus MediaGalleryDatabase::EnsureCurrentVersion() { |
| + // We can't read databases newer than we were designed for. |
| + if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
| + LOG(WARNING) << "Media Gallery database is too new."; |
| + return sql::INIT_TOO_NEW; |
| + } |
| + int cur_version = meta_table_.GetVersionNumber(); |
| + if (cur_version == 0) { |
| + LOG(WARNING) << "Initializing the Media Gallery database."; |
| + |
| + ++cur_version; |
|
vandebo (ex-Chrome)
2012/03/05 23:16:03
set it to kCurrentVersionNumber.
tpayne
2012/03/06 01:52:37
Once we have migration code, that's not going to b
|
| + meta_table_.SetVersionNumber(cur_version); |
| + meta_table_.SetCompatibleVersionNumber( |
| + std::min(cur_version, kCompatibleVersionNumber)); |
| + } |
| + return sql::INIT_OK; |
| +} |
| + |
| +// static |
| +int MediaGalleryDatabase::GetCurrentVersion() { |
|
vandebo (ex-Chrome)
2012/03/05 23:16:03
Why a method to return a constant?
tpayne
2012/03/06 01:52:37
I don't know. I cribbed this from url_database.cc.
|
| + return kCurrentVersionNumber; |
| +} |
| + |
| +COLLECTIONID MediaGalleryDatabase::CreateCollectionRow( |
| + CollectionRow* row) { |
| + const char* sql = "INSERT INTO " COLLECTION_TABLE_NAME |
| + "(path, last_modified_time, entry_count, all_parsed) " |
| + "VALUES(?, ?, ?, ?)"; |
| + |
| + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, sql)); |
| + statement.BindString(0, row->GetPath().value().c_str()); |
| + statement.BindInt64(1, row->GetLastModifiedTime().ToInternalValue()); |
| + statement.BindInt(2, row->GetEntryCount()); |
| + statement.BindInt(3, row->IsAllParsed() ? 1 : 0); |
| + |
| + if (!statement.Run()) { |
| + VLOG(0) << "Failed to add collection " << row->GetPath().value() |
| + << " to table media_gallery.collections"; |
| + return 0; |
| + } |
| + return row->id_ = GetDB().GetLastInsertRowId(); |
| +} |
| + |
| +bool MediaGalleryDatabase::GetCollectionRow(COLLECTIONID id, |
| + CollectionRow* row) { |
| + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| + "SELECT" MEDIA_GALLERY_COLLECTION_ROW_FIELDS |
| + "FROM " COLLECTION_TABLE_NAME " WHERE id=?")); |
| + statement.BindInt64(0, id); |
| + |
| + if (statement.Step()) { |
| + FillCollectionRow(statement, row); |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +// Convenience method to fill a row from a statement. Must be in sync with the |
| +// columns in MEDIA_GALLERY_COLLECTION_ROW_FIELDS |
| +void MediaGalleryDatabase::FillCollectionRow(const sql::Statement& s, |
| + CollectionRow* i) { |
| + DCHECK(i); |
| + i->id_ = s.ColumnInt64(0); |
| + i->path_ = FilePath(s.ColumnString(1)); |
| + i->last_modified_time_ = base::Time::FromInternalValue(s.ColumnInt64(2)); |
| + i->entry_count_ = s.ColumnInt(3); |
| + i->all_parsed_ = s.ColumnInt(4) != 0; |
| +} |
| + |
| +bool MediaGalleryDatabase::CreateCollectionsTable(sql::Connection* db) { |
| + const char* name = COLLECTION_TABLE_NAME; |
| + if (db->DoesTableExist(name)) |
| + return true; |
| + |
| + const char *sql = "CREATE TABLE " COLLECTION_TABLE_NAME |
| + " (id INTEGER PRIMARY KEY," |
| + "path LONGVARCHAR NOT NULL," |
| + "last_modified_time INTEGER NOT NULL," |
| + "entry_count INTEGER DEFAULT 0 NOT NULL," |
| + "all_parsed INTEGER DEFAULT 0 NOT NULL)"; |
| + return db->Execute(sql); |
| +} |
| + |
| +} // namespace media_gallery |