Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: content/browser/media_gallery/media_gallery_database.cc

Issue 9567035: Added MediaGalleryDatabase (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix windows compile Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
9 #include "base/file_path.h"
10 #include "base/logging.h"
11 #if defined(OS_WIN)
Lei Zhang 2012/03/07 01:41:42 Put this below, even though the linter will compla
tpayne 2012/03/07 18:04:03 Done.
12 #include "base/sys_string_conversions.h"
13 #endif // OS_WIN
14 #include "sql/diagnostic_error_delegate.h"
15 #include "sql/statement.h"
16 #include "sql/transaction.h"
17
18 #define MEDIA_GALLERY_COLLECTION_ROW_FIELDS \
19 " collections.id, collections.path, collections.last_modified_time, " \
20 "collections.entry_count, collections.all_parsed "
21
22 namespace media_gallery {
23
24 namespace {
25
26 static const int kCurrentVersionNumber = 1;
Lei Zhang 2012/03/07 01:41:42 You don't need static inside an anonymous namespac
tpayne 2012/03/07 18:04:03 Done.
27 static const int kCompatibleVersionNumber = 1;
28 static const FilePath::CharType kMediaGalleryDatabaseName[] =
Lei Zhang 2012/03/07 01:41:42 This will create a static initializers. Declare co
tpayne 2012/03/07 18:04:03 I don't understand how it will create a static ini
Lei Zhang 2012/03/07 20:59:08 Whoops, I misread that as FilePath::StringType som
29 FILE_PATH_LITERAL(".media_gallery.db");
30
31 class HistogramName {
32 public:
33 static const char* name() {
34 return "Sqlite.MediaGallery.Error";
35 }
36 };
37
38 } // namespace
39
40 MediaGalleryDatabase::MediaGalleryDatabase() { }
41
42 MediaGalleryDatabase::~MediaGalleryDatabase() { }
43
44 sql::InitStatus MediaGalleryDatabase::Init(const FilePath& database_dir) {
45 // Set the exceptional sqlite error handler.
46 db_.set_error_delegate(new sql::DiagnosticErrorDelegate<HistogramName>());
47
48 // Set the database page size to something a little larger to give us
49 // better performance (we're typically seek rather than bandwidth limited).
50 // This only has an effect before any tables have been created, otherwise
51 // this is a NOP. Must be a power of 2 and a max of 8192.
52 db_.set_page_size(4096);
53
54 // Increase the cache size. The page size, plus a little extra, times this
55 // value, tells us how much memory the cache will use maximum.
56 // 6000 * 4KB = 24MB
57 db_.set_cache_size(6000);
58
59 if (!db_.Open(database_dir.Append(FilePath(kMediaGalleryDatabaseName))))
60 return sql::INIT_FAILURE;
61
62 return InitInternal(&db_);
63 }
64
65 sql::InitStatus MediaGalleryDatabase::InitInternal(sql::Connection* db) {
66 // Wrap the rest of init in a tranaction. This will prevent the database from
67 // getting corrupted if we crash in the middle of initialization or migration.
68 sql::Transaction committer(db);
69 if (!committer.Begin())
70 return sql::INIT_FAILURE;
71
72 // Prime the cache.
73 db->Preload();
74
75 // Create the tables and indices.
76 if (!meta_table_.Init(db, GetCurrentVersion(), kCompatibleVersionNumber))
77 return sql::INIT_FAILURE;
78
79 if (!CreateCollectionsTable(db)) {
Lei Zhang 2012/03/07 01:41:42 nit: no need for parenthesis for a one line condit
tpayne 2012/03/07 18:04:03 Done.
80 return sql::INIT_FAILURE;
81 }
82
83 // Version check.
84 sql::InitStatus version_status = EnsureCurrentVersion();
85 if (version_status != sql::INIT_OK)
86 return version_status;
87
88 return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE;
89 }
90
91 sql::Connection& MediaGalleryDatabase::GetDB() {
92 return db_;
93 }
94
95 sql::InitStatus MediaGalleryDatabase::EnsureCurrentVersion() {
96 // We can't read databases newer than we were designed for.
Lei Zhang 2012/03/07 01:41:42 nit: indentation.
tpayne 2012/03/07 18:04:03 Done.
97 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
98 LOG(WARNING) << "Media Gallery database is too new.";
Lei Zhang 2012/03/07 01:41:42 VLOG? http://www.chromium.org/developers/coding-st
tpayne 2012/03/07 18:04:03 Done.
99 return sql::INIT_TOO_NEW;
100 }
101 int cur_version = meta_table_.GetVersionNumber();
102 if (cur_version == 0) {
103 LOG(WARNING) << "Initializing the Media Gallery database.";
104
105 ++cur_version;
106 meta_table_.SetVersionNumber(cur_version);
107 meta_table_.SetCompatibleVersionNumber(
108 std::min(cur_version, kCompatibleVersionNumber));
109 }
110 return sql::INIT_OK;
111 }
112
113 // static
114 int MediaGalleryDatabase::GetCurrentVersion() {
115 return kCurrentVersionNumber;
116 }
117
118 CollectionId MediaGalleryDatabase::CreateCollectionRow(
119 CollectionRow* row) {
120 const char* sql = "INSERT INTO collections"
121 "(path, last_modified_time, entry_count, all_parsed) "
122 "VALUES(?, ?, ?, ?)";
123
124 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, sql));
125 #if defined(OS_WIN)
126 statement.BindString(0, base::SysWideToUTF8(row->path.value().c_str()));
127 #elif defined(OS_POSIX)
128 statement.BindString(0, row->path.value().c_str());
129 #endif // OS_WIN
130 statement.BindInt64(1, row->last_modified_time.ToInternalValue());
131 statement.BindInt(2, row->entry_count);
132 statement.BindInt(3, row->all_parsed ? 1 : 0);
133
134 if (!statement.Run()) {
135 VLOG(0) << "Failed to add collection " << row->path.value()
136 << " to table media_gallery.collections";
137 return 0;
138 }
139 return row->id = GetDB().GetLastInsertRowId();
140 }
141
142 bool MediaGalleryDatabase::GetCollectionRow(CollectionId id,
143 CollectionRow* row) {
144 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
145 "SELECT" MEDIA_GALLERY_COLLECTION_ROW_FIELDS
146 "FROM collections WHERE id=?"));
147 statement.BindInt64(0, id);
148
149 if (statement.Step()) {
150 FillCollectionRow(statement, row);
151 return true;
152 }
153 return false;
154 }
155
156 // Convenience method to fill a row from a statement. Must be in sync with the
157 // columns in MEDIA_GALLERY_COLLECTION_ROW_FIELDS
158 void MediaGalleryDatabase::FillCollectionRow(const sql::Statement& s,
Lei Zhang 2012/03/07 01:41:42 nit: can we not use "s" and "i" as parameter names
tpayne 2012/03/07 18:04:03 Done.
159 CollectionRow* i) {
160 DCHECK(i);
Lei Zhang 2012/03/07 01:41:42 No need. If |i| is NULL, we'll immediately crash o
tpayne 2012/03/07 18:04:03 Done.
161 i->id = s.ColumnInt64(0);
162 #if defined(OS_WIN)
163 i->path = FilePath(base::SysUTF8ToWide(s.ColumnString(1)));
164 #elif defined(OS_POSIX)
165 i->path = FilePath(s.ColumnString(1));
166 #endif // OS_WIN
167 i->last_modified_time = base::Time::FromInternalValue(s.ColumnInt64(2));
168 i->entry_count = s.ColumnInt(3);
169 i->all_parsed = s.ColumnInt(4) != 0;
170 }
171
172 // static
173 bool MediaGalleryDatabase::DoesCollectionsTableExist(sql::Connection *db) {
174 return db->DoesTableExist("collections");
175 }
176
177 // static
178 bool MediaGalleryDatabase::CreateCollectionsTable(sql::Connection* db) {
179 if (DoesCollectionsTableExist(db))
180 return true;
181
182 const char *sql = "CREATE TABLE collections"
183 " (id INTEGER PRIMARY KEY,"
184 "path LONGVARCHAR NOT NULL,"
185 "last_modified_time INTEGER NOT NULL,"
186 "entry_count INTEGER DEFAULT 0 NOT NULL,"
187 "all_parsed INTEGER DEFAULT 0 NOT NULL)";
188 return db->Execute(sql);
189 }
190
191 } // namespace media_gallery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698