OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "content/browser/indexed_db/leveldb/leveldb_database.h" | 5 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
6 | 6 |
7 #include <cerrno> | 7 #include <cerrno> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
19 #include "content/browser/indexed_db/indexed_db_class_factory.h" | 19 #include "content/browser/indexed_db/indexed_db_class_factory.h" |
20 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 20 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
21 #include "content/browser/indexed_db/leveldb/leveldb_iterator_impl.h" | 21 #include "content/browser/indexed_db/leveldb/leveldb_iterator_impl.h" |
22 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" | 22 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" |
23 #include "third_party/leveldatabase/env_chromium.h" | 23 #include "third_party/leveldatabase/env_chromium.h" |
24 #include "third_party/leveldatabase/env_idb.h" | 24 #include "third_party/leveldatabase/env_idb.h" |
25 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 25 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
26 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
27 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 27 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
28 #include "third_party/leveldatabase/src/include/leveldb/filter_policy.h" | |
28 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | 29 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
29 | 30 |
30 using base::StringPiece; | 31 using base::StringPiece; |
31 | 32 |
32 namespace content { | 33 namespace content { |
33 | 34 |
34 // Forcing flushes to disk at the end of a transaction guarantees that the | 35 // Forcing flushes to disk at the end of a transaction guarantees that the |
35 // data hit disk, but drastically impacts throughput when the filesystem is | 36 // data hit disk, but drastically impacts throughput when the filesystem is |
36 // busy with background compactions. Not syncing trades off reliability for | 37 // busy with background compactions. Not syncing trades off reliability for |
37 // performance. Note that background compactions which move data from the | 38 // performance. Note that background compactions which move data from the |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 LevelDBDatabase::~LevelDBDatabase() { | 87 LevelDBDatabase::~LevelDBDatabase() { |
87 // db_'s destructor uses comparator_adapter_; order of deletion is important. | 88 // db_'s destructor uses comparator_adapter_; order of deletion is important. |
88 db_.reset(); | 89 db_.reset(); |
89 comparator_adapter_.reset(); | 90 comparator_adapter_.reset(); |
90 env_.reset(); | 91 env_.reset(); |
91 } | 92 } |
92 | 93 |
93 static leveldb::Status OpenDB(leveldb::Comparator* comparator, | 94 static leveldb::Status OpenDB(leveldb::Comparator* comparator, |
94 leveldb::Env* env, | 95 leveldb::Env* env, |
95 const base::FilePath& path, | 96 const base::FilePath& path, |
96 leveldb::DB** db) { | 97 leveldb::DB** db, |
98 const leveldb::FilterPolicy** filter_policy) { | |
jsbell
2014/09/12 00:14:44
How about passing a scoped_ptr<leveldb::FilterPoli
| |
97 leveldb::Options options; | 99 leveldb::Options options; |
98 options.comparator = comparator; | 100 options.comparator = comparator; |
99 options.create_if_missing = true; | 101 options.create_if_missing = true; |
100 options.paranoid_checks = true; | 102 options.paranoid_checks = true; |
103 options.filter_policy = leveldb::NewBloomFilterPolicy(10); | |
101 options.compression = leveldb::kSnappyCompression; | 104 options.compression = leveldb::kSnappyCompression; |
102 | 105 |
103 // For info about the troubles we've run into with this parameter, see: | 106 // For info about the troubles we've run into with this parameter, see: |
104 // https://code.google.com/p/chromium/issues/detail?id=227313#c11 | 107 // https://code.google.com/p/chromium/issues/detail?id=227313#c11 |
105 options.max_open_files = 80; | 108 options.max_open_files = 80; |
106 options.env = env; | 109 options.env = env; |
107 | 110 |
108 // ChromiumEnv assumes UTF8, converts back to FilePath before using. | 111 // ChromiumEnv assumes UTF8, converts back to FilePath before using. |
109 return leveldb::DB::Open(options, path.AsUTF8Unsafe(), db); | 112 leveldb::Status s = leveldb::DB::Open(options, path.AsUTF8Unsafe(), db); |
113 | |
114 if (s.ok()) | |
115 *filter_policy = options.filter_policy; | |
116 else | |
117 delete options.filter_policy; | |
118 | |
119 return s; | |
110 } | 120 } |
111 | 121 |
112 leveldb::Status LevelDBDatabase::Destroy(const base::FilePath& file_name) { | 122 leveldb::Status LevelDBDatabase::Destroy(const base::FilePath& file_name) { |
113 leveldb::Options options; | 123 leveldb::Options options; |
114 options.env = leveldb::IDBEnv(); | 124 options.env = leveldb::IDBEnv(); |
115 // ChromiumEnv assumes UTF8, converts back to FilePath before using. | 125 // ChromiumEnv assumes UTF8, converts back to FilePath before using. |
116 return leveldb::DestroyDB(file_name.AsUTF8Unsafe(), options); | 126 return leveldb::DestroyDB(file_name.AsUTF8Unsafe(), options); |
117 } | 127 } |
118 | 128 |
119 namespace { | 129 namespace { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 leveldb::Status LevelDBDatabase::Open(const base::FilePath& file_name, | 273 leveldb::Status LevelDBDatabase::Open(const base::FilePath& file_name, |
264 const LevelDBComparator* comparator, | 274 const LevelDBComparator* comparator, |
265 scoped_ptr<LevelDBDatabase>* result, | 275 scoped_ptr<LevelDBDatabase>* result, |
266 bool* is_disk_full) { | 276 bool* is_disk_full) { |
267 base::TimeTicks begin_time = base::TimeTicks::Now(); | 277 base::TimeTicks begin_time = base::TimeTicks::Now(); |
268 | 278 |
269 scoped_ptr<ComparatorAdapter> comparator_adapter( | 279 scoped_ptr<ComparatorAdapter> comparator_adapter( |
270 new ComparatorAdapter(comparator)); | 280 new ComparatorAdapter(comparator)); |
271 | 281 |
272 leveldb::DB* db; | 282 leveldb::DB* db; |
273 const leveldb::Status s = | 283 const leveldb::FilterPolicy* filter_policy; |
274 OpenDB(comparator_adapter.get(), leveldb::IDBEnv(), file_name, &db); | 284 const leveldb::Status s = OpenDB(comparator_adapter.get(), |
285 leveldb::IDBEnv(), | |
286 file_name, | |
287 &db, | |
288 &filter_policy); | |
275 | 289 |
276 if (!s.ok()) { | 290 if (!s.ok()) { |
277 HistogramLevelDBError("WebCore.IndexedDB.LevelDBOpenErrors", s); | 291 HistogramLevelDBError("WebCore.IndexedDB.LevelDBOpenErrors", s); |
278 int free_space_k_bytes = CheckFreeSpace("Failure", file_name); | 292 int free_space_k_bytes = CheckFreeSpace("Failure", file_name); |
279 // Disks with <100k of free space almost never succeed in opening a | 293 // Disks with <100k of free space almost never succeed in opening a |
280 // leveldb database. | 294 // leveldb database. |
281 if (is_disk_full) | 295 if (is_disk_full) |
282 *is_disk_full = free_space_k_bytes >= 0 && free_space_k_bytes < 100; | 296 *is_disk_full = free_space_k_bytes >= 0 && free_space_k_bytes < 100; |
283 | 297 |
284 LOG(ERROR) << "Failed to open LevelDB database from " | 298 LOG(ERROR) << "Failed to open LevelDB database from " |
285 << file_name.AsUTF8Unsafe() << "," << s.ToString(); | 299 << file_name.AsUTF8Unsafe() << "," << s.ToString(); |
286 return s; | 300 return s; |
287 } | 301 } |
288 | 302 |
289 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime", | 303 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime", |
290 base::TimeTicks::Now() - begin_time); | 304 base::TimeTicks::Now() - begin_time); |
291 | 305 |
292 CheckFreeSpace("Success", file_name); | 306 CheckFreeSpace("Success", file_name); |
293 | 307 |
294 (*result).reset(new LevelDBDatabase); | 308 (*result).reset(new LevelDBDatabase); |
295 (*result)->db_ = make_scoped_ptr(db); | 309 (*result)->db_ = make_scoped_ptr(db); |
296 (*result)->comparator_adapter_ = comparator_adapter.Pass(); | 310 (*result)->comparator_adapter_ = comparator_adapter.Pass(); |
297 (*result)->comparator_ = comparator; | 311 (*result)->comparator_ = comparator; |
312 (*result)->filter_policy_ = make_scoped_ptr(filter_policy); | |
298 | 313 |
299 return s; | 314 return s; |
300 } | 315 } |
301 | 316 |
302 scoped_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory( | 317 scoped_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory( |
303 const LevelDBComparator* comparator) { | 318 const LevelDBComparator* comparator) { |
304 scoped_ptr<ComparatorAdapter> comparator_adapter( | 319 scoped_ptr<ComparatorAdapter> comparator_adapter( |
305 new ComparatorAdapter(comparator)); | 320 new ComparatorAdapter(comparator)); |
306 scoped_ptr<leveldb::Env> in_memory_env(leveldb::NewMemEnv(leveldb::IDBEnv())); | 321 scoped_ptr<leveldb::Env> in_memory_env(leveldb::NewMemEnv(leveldb::IDBEnv())); |
307 | 322 |
308 leveldb::DB* db; | 323 leveldb::DB* db; |
309 const leveldb::Status s = OpenDB( | 324 const leveldb::FilterPolicy* filter_policy; |
310 comparator_adapter.get(), in_memory_env.get(), base::FilePath(), &db); | 325 const leveldb::Status s = OpenDB(comparator_adapter.get(), |
326 in_memory_env.get(), | |
327 base::FilePath(), | |
328 &db, | |
329 &filter_policy); | |
311 | 330 |
312 if (!s.ok()) { | 331 if (!s.ok()) { |
313 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString(); | 332 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString(); |
314 return scoped_ptr<LevelDBDatabase>(); | 333 return scoped_ptr<LevelDBDatabase>(); |
315 } | 334 } |
316 | 335 |
317 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); | 336 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); |
318 result->env_ = in_memory_env.Pass(); | 337 result->env_ = in_memory_env.Pass(); |
319 result->db_ = make_scoped_ptr(db); | 338 result->db_ = make_scoped_ptr(db); |
320 result->comparator_adapter_ = comparator_adapter.Pass(); | 339 result->comparator_adapter_ = comparator_adapter.Pass(); |
321 result->comparator_ = comparator; | 340 result->comparator_ = comparator; |
341 result->filter_policy_ = make_scoped_ptr(filter_policy); | |
322 | 342 |
323 return result.Pass(); | 343 return result.Pass(); |
324 } | 344 } |
325 | 345 |
326 leveldb::Status LevelDBDatabase::Put(const StringPiece& key, | 346 leveldb::Status LevelDBDatabase::Put(const StringPiece& key, |
327 std::string* value) { | 347 std::string* value) { |
328 base::TimeTicks begin_time = base::TimeTicks::Now(); | 348 base::TimeTicks begin_time = base::TimeTicks::Now(); |
329 | 349 |
330 leveldb::WriteOptions write_options; | 350 leveldb::WriteOptions write_options; |
331 write_options.sync = kSyncWrites; | 351 write_options.sync = kSyncWrites; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 const leveldb::Slice start_slice = MakeSlice(start); | 430 const leveldb::Slice start_slice = MakeSlice(start); |
411 const leveldb::Slice stop_slice = MakeSlice(stop); | 431 const leveldb::Slice stop_slice = MakeSlice(stop); |
412 // NULL batch means just wait for earlier writes to be done | 432 // NULL batch means just wait for earlier writes to be done |
413 db_->Write(leveldb::WriteOptions(), NULL); | 433 db_->Write(leveldb::WriteOptions(), NULL); |
414 db_->CompactRange(&start_slice, &stop_slice); | 434 db_->CompactRange(&start_slice, &stop_slice); |
415 } | 435 } |
416 | 436 |
417 void LevelDBDatabase::CompactAll() { db_->CompactRange(NULL, NULL); } | 437 void LevelDBDatabase::CompactAll() { db_->CompactRange(NULL, NULL); } |
418 | 438 |
419 } // namespace content | 439 } // namespace content |
OLD | NEW |