| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 84 |
| 84 LevelDBDatabase::LevelDBDatabase() {} | 85 LevelDBDatabase::LevelDBDatabase() {} |
| 85 | 86 |
| 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( |
| 94 leveldb::Env* env, | 95 leveldb::Comparator* comparator, |
| 95 const base::FilePath& path, | 96 leveldb::Env* env, |
| 96 leveldb::DB** db) { | 97 const base::FilePath& path, |
| 98 leveldb::DB** db, |
| 99 scoped_ptr<const leveldb::FilterPolicy>* filter_policy) { |
| 100 filter_policy->reset(leveldb::NewBloomFilterPolicy(10)); |
| 97 leveldb::Options options; | 101 leveldb::Options options; |
| 98 options.comparator = comparator; | 102 options.comparator = comparator; |
| 99 options.create_if_missing = true; | 103 options.create_if_missing = true; |
| 100 options.paranoid_checks = true; | 104 options.paranoid_checks = true; |
| 105 options.filter_policy = filter_policy->get(); |
| 101 options.compression = leveldb::kSnappyCompression; | 106 options.compression = leveldb::kSnappyCompression; |
| 102 | 107 |
| 103 // For info about the troubles we've run into with this parameter, see: | 108 // 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 | 109 // https://code.google.com/p/chromium/issues/detail?id=227313#c11 |
| 105 options.max_open_files = 80; | 110 options.max_open_files = 80; |
| 106 options.env = env; | 111 options.env = env; |
| 107 | 112 |
| 108 // ChromiumEnv assumes UTF8, converts back to FilePath before using. | 113 // ChromiumEnv assumes UTF8, converts back to FilePath before using. |
| 109 return leveldb::DB::Open(options, path.AsUTF8Unsafe(), db); | 114 leveldb::Status s = leveldb::DB::Open(options, path.AsUTF8Unsafe(), db); |
| 115 |
| 116 return s; |
| 110 } | 117 } |
| 111 | 118 |
| 112 leveldb::Status LevelDBDatabase::Destroy(const base::FilePath& file_name) { | 119 leveldb::Status LevelDBDatabase::Destroy(const base::FilePath& file_name) { |
| 113 leveldb::Options options; | 120 leveldb::Options options; |
| 114 options.env = leveldb::IDBEnv(); | 121 options.env = leveldb::IDBEnv(); |
| 115 // ChromiumEnv assumes UTF8, converts back to FilePath before using. | 122 // ChromiumEnv assumes UTF8, converts back to FilePath before using. |
| 116 return leveldb::DestroyDB(file_name.AsUTF8Unsafe(), options); | 123 return leveldb::DestroyDB(file_name.AsUTF8Unsafe(), options); |
| 117 } | 124 } |
| 118 | 125 |
| 119 namespace { | 126 namespace { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 leveldb::Status LevelDBDatabase::Open(const base::FilePath& file_name, | 270 leveldb::Status LevelDBDatabase::Open(const base::FilePath& file_name, |
| 264 const LevelDBComparator* comparator, | 271 const LevelDBComparator* comparator, |
| 265 scoped_ptr<LevelDBDatabase>* result, | 272 scoped_ptr<LevelDBDatabase>* result, |
| 266 bool* is_disk_full) { | 273 bool* is_disk_full) { |
| 267 base::TimeTicks begin_time = base::TimeTicks::Now(); | 274 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 268 | 275 |
| 269 scoped_ptr<ComparatorAdapter> comparator_adapter( | 276 scoped_ptr<ComparatorAdapter> comparator_adapter( |
| 270 new ComparatorAdapter(comparator)); | 277 new ComparatorAdapter(comparator)); |
| 271 | 278 |
| 272 leveldb::DB* db; | 279 leveldb::DB* db; |
| 273 const leveldb::Status s = | 280 scoped_ptr<const leveldb::FilterPolicy> filter_policy; |
| 274 OpenDB(comparator_adapter.get(), leveldb::IDBEnv(), file_name, &db); | 281 const leveldb::Status s = OpenDB(comparator_adapter.get(), |
| 282 leveldb::IDBEnv(), |
| 283 file_name, |
| 284 &db, |
| 285 &filter_policy); |
| 275 | 286 |
| 276 if (!s.ok()) { | 287 if (!s.ok()) { |
| 277 HistogramLevelDBError("WebCore.IndexedDB.LevelDBOpenErrors", s); | 288 HistogramLevelDBError("WebCore.IndexedDB.LevelDBOpenErrors", s); |
| 278 int free_space_k_bytes = CheckFreeSpace("Failure", file_name); | 289 int free_space_k_bytes = CheckFreeSpace("Failure", file_name); |
| 279 // Disks with <100k of free space almost never succeed in opening a | 290 // Disks with <100k of free space almost never succeed in opening a |
| 280 // leveldb database. | 291 // leveldb database. |
| 281 if (is_disk_full) | 292 if (is_disk_full) |
| 282 *is_disk_full = free_space_k_bytes >= 0 && free_space_k_bytes < 100; | 293 *is_disk_full = free_space_k_bytes >= 0 && free_space_k_bytes < 100; |
| 283 | 294 |
| 284 LOG(ERROR) << "Failed to open LevelDB database from " | 295 LOG(ERROR) << "Failed to open LevelDB database from " |
| 285 << file_name.AsUTF8Unsafe() << "," << s.ToString(); | 296 << file_name.AsUTF8Unsafe() << "," << s.ToString(); |
| 286 return s; | 297 return s; |
| 287 } | 298 } |
| 288 | 299 |
| 289 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime", | 300 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime", |
| 290 base::TimeTicks::Now() - begin_time); | 301 base::TimeTicks::Now() - begin_time); |
| 291 | 302 |
| 292 CheckFreeSpace("Success", file_name); | 303 CheckFreeSpace("Success", file_name); |
| 293 | 304 |
| 294 (*result).reset(new LevelDBDatabase); | 305 (*result).reset(new LevelDBDatabase); |
| 295 (*result)->db_ = make_scoped_ptr(db); | 306 (*result)->db_ = make_scoped_ptr(db); |
| 296 (*result)->comparator_adapter_ = comparator_adapter.Pass(); | 307 (*result)->comparator_adapter_ = comparator_adapter.Pass(); |
| 297 (*result)->comparator_ = comparator; | 308 (*result)->comparator_ = comparator; |
| 309 (*result)->filter_policy_ = filter_policy.Pass(); |
| 298 | 310 |
| 299 return s; | 311 return s; |
| 300 } | 312 } |
| 301 | 313 |
| 302 scoped_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory( | 314 scoped_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory( |
| 303 const LevelDBComparator* comparator) { | 315 const LevelDBComparator* comparator) { |
| 304 scoped_ptr<ComparatorAdapter> comparator_adapter( | 316 scoped_ptr<ComparatorAdapter> comparator_adapter( |
| 305 new ComparatorAdapter(comparator)); | 317 new ComparatorAdapter(comparator)); |
| 306 scoped_ptr<leveldb::Env> in_memory_env(leveldb::NewMemEnv(leveldb::IDBEnv())); | 318 scoped_ptr<leveldb::Env> in_memory_env(leveldb::NewMemEnv(leveldb::IDBEnv())); |
| 307 | 319 |
| 308 leveldb::DB* db; | 320 leveldb::DB* db; |
| 309 const leveldb::Status s = OpenDB( | 321 scoped_ptr<const leveldb::FilterPolicy> filter_policy; |
| 310 comparator_adapter.get(), in_memory_env.get(), base::FilePath(), &db); | 322 const leveldb::Status s = OpenDB(comparator_adapter.get(), |
| 323 in_memory_env.get(), |
| 324 base::FilePath(), |
| 325 &db, |
| 326 &filter_policy); |
| 311 | 327 |
| 312 if (!s.ok()) { | 328 if (!s.ok()) { |
| 313 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString(); | 329 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString(); |
| 314 return scoped_ptr<LevelDBDatabase>(); | 330 return scoped_ptr<LevelDBDatabase>(); |
| 315 } | 331 } |
| 316 | 332 |
| 317 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); | 333 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); |
| 318 result->env_ = in_memory_env.Pass(); | 334 result->env_ = in_memory_env.Pass(); |
| 319 result->db_ = make_scoped_ptr(db); | 335 result->db_ = make_scoped_ptr(db); |
| 320 result->comparator_adapter_ = comparator_adapter.Pass(); | 336 result->comparator_adapter_ = comparator_adapter.Pass(); |
| 321 result->comparator_ = comparator; | 337 result->comparator_ = comparator; |
| 338 result->filter_policy_ = filter_policy.Pass(); |
| 322 | 339 |
| 323 return result.Pass(); | 340 return result.Pass(); |
| 324 } | 341 } |
| 325 | 342 |
| 326 leveldb::Status LevelDBDatabase::Put(const StringPiece& key, | 343 leveldb::Status LevelDBDatabase::Put(const StringPiece& key, |
| 327 std::string* value) { | 344 std::string* value) { |
| 328 base::TimeTicks begin_time = base::TimeTicks::Now(); | 345 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 329 | 346 |
| 330 leveldb::WriteOptions write_options; | 347 leveldb::WriteOptions write_options; |
| 331 write_options.sync = kSyncWrites; | 348 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); | 427 const leveldb::Slice start_slice = MakeSlice(start); |
| 411 const leveldb::Slice stop_slice = MakeSlice(stop); | 428 const leveldb::Slice stop_slice = MakeSlice(stop); |
| 412 // NULL batch means just wait for earlier writes to be done | 429 // NULL batch means just wait for earlier writes to be done |
| 413 db_->Write(leveldb::WriteOptions(), NULL); | 430 db_->Write(leveldb::WriteOptions(), NULL); |
| 414 db_->CompactRange(&start_slice, &stop_slice); | 431 db_->CompactRange(&start_slice, &stop_slice); |
| 415 } | 432 } |
| 416 | 433 |
| 417 void LevelDBDatabase::CompactAll() { db_->CompactRange(NULL, NULL); } | 434 void LevelDBDatabase::CompactAll() { db_->CompactRange(NULL, NULL); } |
| 418 | 435 |
| 419 } // namespace content | 436 } // namespace content |
| OLD | NEW |