Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/media_gallery/media_gallery_database.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "sql/diagnostic_error_delegate.h" | |
| 13 #include "sql/statement.h" | |
| 14 #include "sql/transaction.h" | |
| 15 | |
| 16 #if defined(OS_WIN) | |
| 17 #include "base/sys_string_conversions.h" | |
| 18 #endif // OS_WIN | |
| 19 | |
| 20 #define MEDIA_GALLERY_COLLECTION_ROW_FIELDS \ | |
| 21 " collections.id, collections.path, collections.last_modified_time, " \ | |
| 22 "collections.entry_count, collections.all_parsed " | |
| 23 | |
| 24 namespace media_gallery { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const int kCurrentVersionNumber = 1; | |
| 29 const int kCompatibleVersionNumber = 1; | |
| 30 const FilePath::CharType kMediaGalleryDatabaseName[] = | |
| 31 FILE_PATH_LITERAL(".media_gallery.db"); | |
| 32 | |
| 33 class HistogramName { | |
| 34 public: | |
| 35 static const char* name() { | |
| 36 return "Sqlite.MediaGallery.Error"; | |
| 37 } | |
| 38 }; | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 MediaGalleryDatabase::MediaGalleryDatabase() { } | |
| 43 | |
| 44 MediaGalleryDatabase::~MediaGalleryDatabase() { } | |
| 45 | |
| 46 sql::InitStatus MediaGalleryDatabase::Init(const FilePath& database_dir) { | |
| 47 // Set the exceptional sqlite error handler. | |
| 48 db_.set_error_delegate(new sql::DiagnosticErrorDelegate<HistogramName>()); | |
| 49 | |
| 50 // Set the database page size to something a little larger to give us | |
| 51 // better performance (we're typically seek rather than bandwidth limited). | |
| 52 // This only has an effect before any tables have been created, otherwise | |
| 53 // this is a NOP. Must be a power of 2 and a max of 8192. | |
| 54 db_.set_page_size(4096); | |
| 55 | |
| 56 // Increase the cache size. The page size, plus a little extra, times this | |
| 57 // value, tells us how much memory the cache will use maximum. | |
| 58 // 6000 * 4KB = 24MB | |
| 59 db_.set_cache_size(6000); | |
| 60 | |
| 61 if (!db_.Open(database_dir.Append(FilePath(kMediaGalleryDatabaseName)))) | |
| 62 return sql::INIT_FAILURE; | |
| 63 | |
| 64 return InitInternal(&db_); | |
| 65 } | |
| 66 | |
| 67 sql::InitStatus MediaGalleryDatabase::InitInternal(sql::Connection* db) { | |
| 68 // Wrap the rest of init in a tranaction. This will prevent the database from | |
| 69 // getting corrupted if we crash in the middle of initialization or migration. | |
| 70 sql::Transaction committer(db); | |
| 71 if (!committer.Begin()) | |
| 72 return sql::INIT_FAILURE; | |
| 73 | |
| 74 // Prime the cache. | |
| 75 db->Preload(); | |
| 76 | |
| 77 // Create the tables and indices. | |
| 78 if (!meta_table_.Init(db, GetCurrentVersion(), kCompatibleVersionNumber)) | |
| 79 return sql::INIT_FAILURE; | |
| 80 | |
| 81 if (!CreateCollectionsTable(db)) | |
| 82 return sql::INIT_FAILURE; | |
| 83 | |
| 84 // Version check. | |
| 85 sql::InitStatus version_status = EnsureCurrentVersion(); | |
| 86 if (version_status != sql::INIT_OK) | |
| 87 return version_status; | |
| 88 | |
| 89 return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; | |
| 90 } | |
| 91 | |
| 92 sql::Connection& MediaGalleryDatabase::GetDB() { | |
| 93 return db_; | |
| 94 } | |
| 95 | |
| 96 sql::InitStatus MediaGalleryDatabase::EnsureCurrentVersion() { | |
| 97 // We can't read databases newer than we were designed for. | |
| 98 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | |
| 99 VLOG(1) << "Media Gallery database is too new."; | |
| 100 return sql::INIT_TOO_NEW; | |
| 101 } | |
| 102 int cur_version = meta_table_.GetVersionNumber(); | |
| 103 if (cur_version == 0) { | |
| 104 DVLOG(1) << "Initializing the Media Gallery database."; | |
| 105 | |
| 106 ++cur_version; | |
| 107 meta_table_.SetVersionNumber(cur_version); | |
| 108 meta_table_.SetCompatibleVersionNumber( | |
| 109 std::min(cur_version, kCompatibleVersionNumber)); | |
| 110 } | |
| 111 return sql::INIT_OK; | |
| 112 } | |
| 113 | |
| 114 // static | |
| 115 int MediaGalleryDatabase::GetCurrentVersion() { | |
| 116 return kCurrentVersionNumber; | |
| 117 } | |
| 118 | |
| 119 CollectionId MediaGalleryDatabase::CreateCollectionRow( | |
| 120 CollectionRow* row) { | |
| 121 const char* sql = "INSERT INTO collections" | |
| 122 "(path, last_modified_time, entry_count, all_parsed) " | |
| 123 "VALUES(?, ?, ?, ?)"; | |
| 124 | |
| 125 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, sql)); | |
| 126 #if defined(OS_WIN) | |
| 127 // We standardize on UTF8 encoding for paths. | |
| 128 std::string path(base::SysWideToUTF8(row->path.value())); | |
| 129 #elif defined(OS_POSIX) | |
| 130 std::string path(row->path.value()); | |
| 131 #endif | |
| 132 | |
| 133 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
| 134 // We standardize on POSIX-style paths, since any platform can understand | |
| 135 // them. | |
| 136 base::ReplaceChars(path, "\\", "/", &path); | |
|
Lei Zhang
2012/03/07 21:24:37
IWYU - #include "base/string_util.h"
tpayne
2012/03/07 21:34:55
Done.
| |
| 137 #endif // FILE_PATH_USES_WIN_SEPARATORS | |
| 138 | |
| 139 statement.BindString(0, path.c_str()); | |
| 140 statement.BindInt64(1, row->last_modified_time.ToInternalValue()); | |
| 141 statement.BindInt(2, row->entry_count); | |
| 142 statement.BindInt(3, row->all_parsed ? 1 : 0); | |
| 143 | |
| 144 if (!statement.Run()) { | |
| 145 VLOG(0) << "Failed to add collection " << row->path.value() | |
| 146 << " to table media_gallery.collections"; | |
| 147 return 0; | |
| 148 } | |
| 149 return row->id = GetDB().GetLastInsertRowId(); | |
| 150 } | |
| 151 | |
| 152 bool MediaGalleryDatabase::GetCollectionRow(CollectionId id, | |
| 153 CollectionRow* row) { | |
| 154 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 155 "SELECT" MEDIA_GALLERY_COLLECTION_ROW_FIELDS | |
| 156 "FROM collections WHERE id=?")); | |
| 157 statement.BindInt64(0, id); | |
| 158 | |
| 159 if (statement.Step()) { | |
| 160 FillCollectionRow(statement, row); | |
| 161 return true; | |
| 162 } | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 // Convenience method to fill a row from a statement. Must be in sync with the | |
| 167 // columns in MEDIA_GALLERY_COLLECTION_ROW_FIELDS | |
| 168 void MediaGalleryDatabase::FillCollectionRow(const sql::Statement& statement, | |
| 169 CollectionRow* row) { | |
| 170 row->id = statement.ColumnInt64(0); | |
| 171 #if defined(OS_WIN) | |
| 172 row->path = FilePath(base::SysUTF8ToWide(statement.ColumnString(1))); | |
| 173 #elif defined(OS_POSIX) | |
| 174 row->path = FilePath(statement.ColumnString(1)); | |
| 175 #endif // OS_WIN | |
| 176 row->last_modified_time = base::Time::FromInternalValue( | |
| 177 statement.ColumnInt64(2)); | |
| 178 row->entry_count = statement.ColumnInt(3); | |
| 179 row->all_parsed = statement.ColumnInt(4) != 0; | |
| 180 } | |
| 181 | |
| 182 // static | |
| 183 bool MediaGalleryDatabase::DoesCollectionsTableExist(sql::Connection *db) { | |
| 184 return db->DoesTableExist("collections"); | |
| 185 } | |
| 186 | |
| 187 // static | |
| 188 bool MediaGalleryDatabase::CreateCollectionsTable(sql::Connection* db) { | |
| 189 if (DoesCollectionsTableExist(db)) | |
| 190 return true; | |
| 191 | |
| 192 // TODO(tpayne): Add unique constraint on path once we standardize on path | |
| 193 // format. | |
| 194 const char *sql = "CREATE TABLE collections" | |
|
Lei Zhang
2012/03/07 21:24:37
nit: "char *" -> "char*"
tpayne
2012/03/07 21:34:55
Done.
| |
| 195 " (id INTEGER PRIMARY KEY," | |
| 196 "path LONGVARCHAR NOT NULL," | |
| 197 "last_modified_time INTEGER NOT NULL," | |
| 198 "entry_count INTEGER DEFAULT 0 NOT NULL," | |
| 199 "all_parsed INTEGER DEFAULT 0 NOT NULL)"; | |
| 200 return db->Execute(sql); | |
| 201 } | |
| 202 | |
| 203 } // namespace media_gallery | |
| OLD | NEW |