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