OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/core/dom_distiller_database.h" | 5 #include "components/dom_distiller/core/dom_distiller_database.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
13 #include "components/dom_distiller/core/article_entry.h" | 13 #include "components/dom_distiller/core/article_entry.h" |
14 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 14 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
15 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 15 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
16 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 16 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
17 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
18 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
19 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
20 | 20 |
21 using base::MessageLoop; | 21 using base::MessageLoop; |
22 using base::SequencedTaskRunner; | 22 using base::SequencedTaskRunner; |
23 | 23 |
24 namespace dom_distiller { | 24 namespace dom_distiller { |
25 | 25 |
26 DomDistillerDatabase::LevelDB::LevelDB() {} | 26 DomDistillerDatabase::LevelDB::LevelDB() { |
| 27 thread_checker_.DetachFromThread(); |
| 28 } |
27 | 29 |
28 DomDistillerDatabase::LevelDB::~LevelDB() {} | 30 DomDistillerDatabase::LevelDB::~LevelDB() { |
| 31 DCHECK(thread_checker_.CalledOnValidThread()); |
| 32 } |
29 | 33 |
30 bool DomDistillerDatabase::LevelDB::Init(const base::FilePath& database_dir) { | 34 bool DomDistillerDatabase::LevelDB::Init(const base::FilePath& database_dir) { |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); |
| 36 |
31 leveldb::Options options; | 37 leveldb::Options options; |
32 options.create_if_missing = true; | 38 options.create_if_missing = true; |
33 options.max_open_files = 0; // Use minimum. | 39 options.max_open_files = 0; // Use minimum. |
34 | 40 |
35 std::string path = database_dir.AsUTF8Unsafe(); | 41 std::string path = database_dir.AsUTF8Unsafe(); |
36 | 42 |
37 leveldb::DB* db = NULL; | 43 leveldb::DB* db = NULL; |
38 leveldb::Status status = leveldb::DB::Open(options, path, &db); | 44 leveldb::Status status = leveldb::DB::Open(options, path, &db); |
39 if (status.IsCorruption()) { | 45 if (status.IsCorruption()) { |
40 LOG(WARNING) << "Deleting possibly-corrupt database"; | |
41 base::DeleteFile(database_dir, true); | 46 base::DeleteFile(database_dir, true); |
42 status = leveldb::DB::Open(options, path, &db); | 47 status = leveldb::DB::Open(options, path, &db); |
43 } | 48 } |
44 | 49 |
45 if (status.ok()) { | 50 if (status.ok()) { |
46 CHECK(db); | 51 CHECK(db); |
47 db_.reset(db); | 52 db_.reset(db); |
48 return true; | 53 return true; |
49 } | 54 } |
50 | 55 |
51 LOG(WARNING) << "Unable to open " << database_dir.value() << ": " | 56 LOG(WARNING) << "Unable to open " << database_dir.value() << ": " |
52 << status.ToString(); | 57 << status.ToString(); |
53 return false; | 58 return false; |
54 } | 59 } |
55 | 60 |
56 bool DomDistillerDatabase::LevelDB::Save(const EntryVector& entries) { | 61 bool DomDistillerDatabase::LevelDB::Save(const EntryVector& entries) { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 |
57 leveldb::WriteBatch updates; | 64 leveldb::WriteBatch updates; |
58 for (EntryVector::const_iterator it = entries.begin(); it != entries.end(); | 65 for (EntryVector::const_iterator it = entries.begin(); it != entries.end(); |
59 ++it) { | 66 ++it) { |
60 updates.Put(leveldb::Slice(it->entry_id()), | 67 updates.Put(leveldb::Slice(it->entry_id()), |
61 leveldb::Slice(it->SerializeAsString())); | 68 leveldb::Slice(it->SerializeAsString())); |
62 } | 69 } |
63 | 70 |
64 leveldb::WriteOptions options; | 71 leveldb::WriteOptions options; |
65 options.sync = true; | 72 options.sync = true; |
66 leveldb::Status status = db_->Write(options, &updates); | 73 leveldb::Status status = db_->Write(options, &updates); |
67 if (status.ok()) | 74 if (status.ok()) |
68 return true; | 75 return true; |
69 | 76 |
70 LOG(WARNING) << "Failed writing dom_distiller entries: " << status.ToString(); | 77 DLOG(WARNING) << "Failed writing dom_distiller entries: " |
| 78 << status.ToString(); |
71 return false; | 79 return false; |
72 } | 80 } |
73 | 81 |
74 bool DomDistillerDatabase::LevelDB::Load(EntryVector* entries) { | 82 bool DomDistillerDatabase::LevelDB::Load(EntryVector* entries) { |
| 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 |
75 leveldb::ReadOptions options; | 85 leveldb::ReadOptions options; |
76 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options)); | 86 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options)); |
77 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) { | 87 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) { |
78 leveldb::Slice value_slice = db_iterator->value(); | 88 leveldb::Slice value_slice = db_iterator->value(); |
79 | 89 |
80 ArticleEntry entry; | 90 ArticleEntry entry; |
81 if (!entry.ParseFromArray(value_slice.data(), value_slice.size())) { | 91 if (!entry.ParseFromArray(value_slice.data(), value_slice.size())) { |
82 LOG(WARNING) << "Unable to parse dom_distiller entry " | 92 DLOG(WARNING) << "Unable to parse dom_distiller entry " |
83 << db_iterator->key().ToString(); | 93 << db_iterator->key().ToString(); |
84 // TODO(cjhopman): Decide what to do about un-parseable entries. | 94 // TODO(cjhopman): Decide what to do about un-parseable entries. |
85 } | 95 } |
86 entries->push_back(entry); | 96 entries->push_back(entry); |
87 } | 97 } |
88 return true; | 98 return true; |
89 } | 99 } |
90 | 100 |
91 DomDistillerDatabase::DomDistillerDatabase( | |
92 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
93 : task_runner_(task_runner), weak_ptr_factory_(this) { | |
94 main_loop_ = MessageLoop::current(); | |
95 } | |
96 | |
97 void DomDistillerDatabase::Destroy() { | |
98 DCHECK(IsRunOnMainLoop()); | |
99 weak_ptr_factory_.InvalidateWeakPtrs(); | |
100 task_runner_->PostNonNestableTask( | |
101 FROM_HERE, | |
102 base::Bind(&DomDistillerDatabase::DestroyFromTaskRunner, | |
103 base::Unretained(this))); | |
104 } | |
105 | |
106 void DomDistillerDatabase::Init(const base::FilePath& database_dir, | |
107 InitCallback callback) { | |
108 InitWithDatabase(scoped_ptr<Database>(new LevelDB()), database_dir, callback); | |
109 } | |
110 | |
111 namespace { | 101 namespace { |
112 | 102 |
113 void RunInitCallback(DomDistillerDatabaseInterface::InitCallback callback, | 103 void RunInitCallback(DomDistillerDatabaseInterface::InitCallback callback, |
114 const bool* success) { | 104 const bool* success) { |
115 callback.Run(*success); | 105 callback.Run(*success); |
116 } | 106 } |
117 | 107 |
118 void RunSaveCallback(DomDistillerDatabaseInterface::SaveCallback callback, | 108 void RunSaveCallback(DomDistillerDatabaseInterface::SaveCallback callback, |
119 const bool* success) { | 109 const bool* success) { |
120 callback.Run(*success); | 110 callback.Run(*success); |
121 } | 111 } |
122 | 112 |
123 void RunLoadCallback(DomDistillerDatabaseInterface::LoadCallback callback, | 113 void RunLoadCallback(DomDistillerDatabaseInterface::LoadCallback callback, |
124 const bool* success, | 114 const bool* success, |
125 scoped_ptr<EntryVector> entries) { | 115 scoped_ptr<EntryVector> entries) { |
126 callback.Run(*success, entries.Pass()); | 116 callback.Run(*success, entries.Pass()); |
127 } | 117 } |
128 | 118 |
| 119 void InitFromTaskRunner(DomDistillerDatabase::Database* database, |
| 120 const base::FilePath& database_dir, |
| 121 bool* success) { |
| 122 DCHECK(success); |
| 123 |
| 124 // TODO(cjhopman): Histogram for database size. |
| 125 *success = database->Init(database_dir); |
| 126 } |
| 127 |
| 128 void SaveEntriesFromTaskRunner(DomDistillerDatabase::Database* database, |
| 129 scoped_ptr<EntryVector> entries, |
| 130 bool* success) { |
| 131 DCHECK(success); |
| 132 *success = database->Save(*entries); |
| 133 } |
| 134 |
| 135 void LoadEntriesFromTaskRunner(DomDistillerDatabase::Database* database, |
| 136 EntryVector* entries, |
| 137 bool* success) { |
| 138 DCHECK(success); |
| 139 DCHECK(entries); |
| 140 |
| 141 entries->clear(); |
| 142 *success = database->Load(entries); |
| 143 } |
| 144 |
129 } // namespace | 145 } // namespace |
130 | 146 |
| 147 DomDistillerDatabase::DomDistillerDatabase( |
| 148 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 149 : task_runner_(task_runner) { |
| 150 } |
| 151 |
| 152 void DomDistillerDatabase::Init(const base::FilePath& database_dir, |
| 153 InitCallback callback) { |
| 154 DCHECK(thread_checker_.CalledOnValidThread()); |
| 155 InitWithDatabase(scoped_ptr<Database>(new LevelDB()), database_dir, callback); |
| 156 } |
| 157 |
131 void DomDistillerDatabase::InitWithDatabase(scoped_ptr<Database> database, | 158 void DomDistillerDatabase::InitWithDatabase(scoped_ptr<Database> database, |
132 const base::FilePath& database_dir, | 159 const base::FilePath& database_dir, |
133 InitCallback callback) { | 160 InitCallback callback) { |
134 DCHECK(IsRunOnMainLoop()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
135 DCHECK(!db_); | 162 DCHECK(!db_); |
136 DCHECK(database); | 163 DCHECK(database); |
137 db_.reset(database.release()); | 164 db_.reset(database.release()); |
138 bool* success = new bool(false); | 165 bool* success = new bool(false); |
139 task_runner_->PostTaskAndReply( | 166 task_runner_->PostTaskAndReply( |
140 FROM_HERE, | 167 FROM_HERE, |
141 base::Bind(&DomDistillerDatabase::InitFromTaskRunner, | 168 base::Bind(InitFromTaskRunner, |
142 base::Unretained(this), | 169 base::Unretained(db_.get()), |
143 database_dir, | 170 database_dir, |
144 success), | 171 success), |
145 base::Bind(RunInitCallback, callback, base::Owned(success))); | 172 base::Bind(RunInitCallback, callback, base::Owned(success))); |
146 } | 173 } |
147 | 174 |
148 void DomDistillerDatabase::SaveEntries(scoped_ptr<EntryVector> entries, | 175 void DomDistillerDatabase::SaveEntries(scoped_ptr<EntryVector> entries, |
149 SaveCallback callback) { | 176 SaveCallback callback) { |
150 DCHECK(IsRunOnMainLoop()); | 177 DCHECK(thread_checker_.CalledOnValidThread()); |
151 bool* success = new bool(false); | 178 bool* success = new bool(false); |
152 task_runner_->PostTaskAndReply( | 179 task_runner_->PostTaskAndReply( |
153 FROM_HERE, | 180 FROM_HERE, |
154 base::Bind(&DomDistillerDatabase::SaveEntriesFromTaskRunner, | 181 base::Bind(SaveEntriesFromTaskRunner, |
155 base::Unretained(this), | 182 base::Unretained(db_.get()), |
156 base::Passed(&entries), | 183 base::Passed(&entries), |
157 success), | 184 success), |
158 base::Bind(RunSaveCallback, callback, base::Owned(success))); | 185 base::Bind(RunSaveCallback, callback, base::Owned(success))); |
159 } | 186 } |
160 | 187 |
161 void DomDistillerDatabase::LoadEntries(LoadCallback callback) { | 188 void DomDistillerDatabase::LoadEntries(LoadCallback callback) { |
162 DCHECK(IsRunOnMainLoop()); | 189 DCHECK(thread_checker_.CalledOnValidThread()); |
163 | |
164 bool* success = new bool(false); | 190 bool* success = new bool(false); |
165 | 191 |
166 scoped_ptr<EntryVector> entries(new EntryVector()); | 192 scoped_ptr<EntryVector> entries(new EntryVector()); |
167 // Get this pointer before entries is base::Passed() so we can use it below. | 193 // Get this pointer before entries is base::Passed() so we can use it below. |
168 EntryVector* entries_ptr = entries.get(); | 194 EntryVector* entries_ptr = entries.get(); |
169 | 195 |
170 task_runner_->PostTaskAndReply( | 196 task_runner_->PostTaskAndReply( |
171 FROM_HERE, | 197 FROM_HERE, |
172 base::Bind(&DomDistillerDatabase::LoadEntriesFromTaskRunner, | 198 base::Bind(LoadEntriesFromTaskRunner, |
173 base::Unretained(this), | 199 base::Unretained(db_.get()), |
174 entries_ptr, | 200 entries_ptr, |
175 success), | 201 success), |
176 base::Bind(RunLoadCallback, | 202 base::Bind(RunLoadCallback, |
177 callback, | 203 callback, |
178 base::Owned(success), | 204 base::Owned(success), |
179 base::Passed(&entries))); | 205 base::Passed(&entries))); |
180 } | 206 } |
181 | 207 |
182 DomDistillerDatabase::~DomDistillerDatabase() { DCHECK(IsRunByTaskRunner()); } | 208 DomDistillerDatabase::~DomDistillerDatabase() { |
183 | 209 DCHECK(thread_checker_.CalledOnValidThread()); |
184 bool DomDistillerDatabase::IsRunByTaskRunner() const { | 210 if (!task_runner_->DeleteSoon(FROM_HERE, db_.release())) { |
185 return task_runner_->RunsTasksOnCurrentThread(); | 211 DLOG(WARNING) << "DOM distiller database will not be deleted."; |
186 } | 212 } |
187 | |
188 bool DomDistillerDatabase::IsRunOnMainLoop() const { | |
189 return MessageLoop::current() == main_loop_; | |
190 } | |
191 | |
192 void DomDistillerDatabase::DestroyFromTaskRunner() { | |
193 DCHECK(IsRunByTaskRunner()); | |
194 delete this; | |
195 } | |
196 | |
197 void DomDistillerDatabase::InitFromTaskRunner( | |
198 const base::FilePath& database_dir, | |
199 bool* success) { | |
200 DCHECK(IsRunByTaskRunner()); | |
201 DCHECK(success); | |
202 | |
203 VLOG(1) << "Opening " << database_dir.value(); | |
204 | |
205 // TODO(cjhopman): Histogram for database size. | |
206 *success = db_->Init(database_dir); | |
207 } | |
208 | |
209 void DomDistillerDatabase::SaveEntriesFromTaskRunner( | |
210 scoped_ptr<EntryVector> entries, | |
211 bool* success) { | |
212 DCHECK(IsRunByTaskRunner()); | |
213 DCHECK(success); | |
214 VLOG(1) << "Saving " << entries->size() << " entry(ies) to database "; | |
215 *success = db_->Save(*entries); | |
216 } | |
217 | |
218 void DomDistillerDatabase::LoadEntriesFromTaskRunner(EntryVector* entries, | |
219 bool* success) { | |
220 DCHECK(IsRunByTaskRunner()); | |
221 DCHECK(success); | |
222 DCHECK(entries); | |
223 | |
224 entries->clear(); | |
225 *success = db_->Load(entries); | |
226 } | 213 } |
227 | 214 |
228 } // namespace dom_distiller | 215 } // namespace dom_distiller |
OLD | NEW |