| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef EXTENSIONS_BROWSER_VALUE_STORE_LAZY_LEVELDB_H_ | 5 #ifndef EXTENSIONS_BROWSER_VALUE_STORE_LAZY_LEVELDB_H_ |
| 6 #define EXTENSIONS_BROWSER_VALUE_STORE_LAZY_LEVELDB_H_ | 6 #define EXTENSIONS_BROWSER_VALUE_STORE_LAZY_LEVELDB_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/metrics/histogram_base.h" | 13 #include "base/metrics/histogram_base.h" |
| 14 #include "extensions/browser/value_store/value_store.h" | 14 #include "extensions/browser/value_store/value_store.h" |
| 15 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 15 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 16 | 16 |
| 17 namespace leveldb { | 17 namespace leveldb { |
| 18 class Iterator; | 18 class Iterator; |
| 19 } // namespace leveldb | 19 } // namespace leveldb |
| 20 | 20 |
| 21 // Manages a lazy connection to a leveldb database. "lazy" means that the | 21 // Manages a lazy connection to a leveldb database. "lazy" means that the |
| 22 // leveldb database will be opened, when necessary, when any *public* method is | 22 // leveldb database will be opened, when necessary, when any *public* method is |
| 23 // called. Derived classes are responsible for calling EnsureDbIsOpen() before | 23 // called. Derived classes are responsible for calling EnsureDbIsOpen() before |
| 24 // calling any other *protected* method. | 24 // calling any other *protected* method. |
| 25 class LazyLevelDb { | 25 class LazyLevelDb { |
| 26 public: | 26 public: |
| 27 // Creates a new database iterator. This iterator *must* be deleted before | 27 // Creates a new database iterator. This iterator *must* be deleted before |
| 28 // this database is closed. | 28 // this database is closed. |
| 29 ValueStore::Status CreateIterator(const leveldb::ReadOptions& read_options, | 29 ValueStore::Status CreateIterator( |
| 30 scoped_ptr<leveldb::Iterator>* iterator); | 30 const leveldb::ReadOptions& read_options, |
| 31 std::unique_ptr<leveldb::Iterator>* iterator); |
| 31 | 32 |
| 32 // Converts a leveldb::Status to a ValueStore::Status. Will also sanitize path | 33 // Converts a leveldb::Status to a ValueStore::Status. Will also sanitize path |
| 33 // to eliminate user data path. | 34 // to eliminate user data path. |
| 34 ValueStore::Status ToValueStoreError(const leveldb::Status& status); | 35 ValueStore::Status ToValueStoreError(const leveldb::Status& status); |
| 35 | 36 |
| 36 // Deletes a value (identified by |key|) from the database. | 37 // Deletes a value (identified by |key|) from the database. |
| 37 ValueStore::Status Delete(const std::string& key); | 38 ValueStore::Status Delete(const std::string& key); |
| 38 | 39 |
| 39 protected: | 40 protected: |
| 40 LazyLevelDb(const std::string& uma_client_name, const base::FilePath& path); | 41 LazyLevelDb(const std::string& uma_client_name, const base::FilePath& path); |
| 41 ~LazyLevelDb(); | 42 ~LazyLevelDb(); |
| 42 | 43 |
| 43 // Closes, if necessary, and deletes the database directory. | 44 // Closes, if necessary, and deletes the database directory. |
| 44 bool DeleteDbFile(); | 45 bool DeleteDbFile(); |
| 45 | 46 |
| 46 // Fixes the |key| or database. If |key| is not null and the database is open | 47 // Fixes the |key| or database. If |key| is not null and the database is open |
| 47 // then the key will be deleted. Otherwise the database will be repaired, and | 48 // then the key will be deleted. Otherwise the database will be repaired, and |
| 48 // failing that will be deleted. | 49 // failing that will be deleted. |
| 49 ValueStore::BackingStoreRestoreStatus FixCorruption(const std::string* key); | 50 ValueStore::BackingStoreRestoreStatus FixCorruption(const std::string* key); |
| 50 | 51 |
| 51 // Reads a |key| from the database, and populates |value| with the result. If | 52 // Reads a |key| from the database, and populates |value| with the result. If |
| 52 // the specified value does not exist in the database then an "OK" status will | 53 // the specified value does not exist in the database then an "OK" status will |
| 53 // be returned and value will be unchanged. Caller must ensure the database is | 54 // be returned and value will be unchanged. Caller must ensure the database is |
| 54 // open before calling this method. | 55 // open before calling this method. |
| 55 ValueStore::Status Read(const std::string& key, | 56 ValueStore::Status Read(const std::string& key, |
| 56 scoped_ptr<base::Value>* value); | 57 std::unique_ptr<base::Value>* value); |
| 57 | 58 |
| 58 // Opens the underlying database if not yet open. If the open fails due to | 59 // Opens the underlying database if not yet open. If the open fails due to |
| 59 // corruption will attempt to repair the database. Failing that, will attempt | 60 // corruption will attempt to repair the database. Failing that, will attempt |
| 60 // to delete the database. Will only attempt a single recovery. | 61 // to delete the database. Will only attempt a single recovery. |
| 61 ValueStore::Status EnsureDbIsOpen(); | 62 ValueStore::Status EnsureDbIsOpen(); |
| 62 | 63 |
| 63 const std::string& open_histogram_name() const { | 64 const std::string& open_histogram_name() const { |
| 64 return open_histogram_->histogram_name(); | 65 return open_histogram_->histogram_name(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 leveldb::DB* db() { return db_.get(); } | 68 leveldb::DB* db() { return db_.get(); } |
| 68 | 69 |
| 69 const leveldb::ReadOptions& read_options() const { return read_options_; } | 70 const leveldb::ReadOptions& read_options() const { return read_options_; } |
| 70 | 71 |
| 71 const leveldb::WriteOptions& write_options() const { return write_options_; } | 72 const leveldb::WriteOptions& write_options() const { return write_options_; } |
| 72 | 73 |
| 73 private: | 74 private: |
| 74 ValueStore::BackingStoreRestoreStatus LogRestoreStatus( | 75 ValueStore::BackingStoreRestoreStatus LogRestoreStatus( |
| 75 ValueStore::BackingStoreRestoreStatus restore_status) const; | 76 ValueStore::BackingStoreRestoreStatus restore_status) const; |
| 76 | 77 |
| 77 // The leveldb to which this class reads/writes. | 78 // The leveldb to which this class reads/writes. |
| 78 scoped_ptr<leveldb::DB> db_; | 79 std::unique_ptr<leveldb::DB> db_; |
| 79 // The path to the underlying leveldb. | 80 // The path to the underlying leveldb. |
| 80 const base::FilePath db_path_; | 81 const base::FilePath db_path_; |
| 81 // The options to be used when this database is lazily opened. | 82 // The options to be used when this database is lazily opened. |
| 82 leveldb::Options open_options_; | 83 leveldb::Options open_options_; |
| 83 // The options to be used for all database read operations. | 84 // The options to be used for all database read operations. |
| 84 leveldb::ReadOptions read_options_; | 85 leveldb::ReadOptions read_options_; |
| 85 // The options to be used for all database write operations. | 86 // The options to be used for all database write operations. |
| 86 leveldb::WriteOptions write_options_; | 87 leveldb::WriteOptions write_options_; |
| 87 // Set when this database has tried to repair (and failed) to prevent | 88 // Set when this database has tried to repair (and failed) to prevent |
| 88 // unbounded attempts to open a bad/unrecoverable database. | 89 // unbounded attempts to open a bad/unrecoverable database. |
| 89 bool db_unrecoverable_ = false; | 90 bool db_unrecoverable_ = false; |
| 90 // Used for UMA logging. | 91 // Used for UMA logging. |
| 91 base::HistogramBase* open_histogram_ = nullptr; | 92 base::HistogramBase* open_histogram_ = nullptr; |
| 92 base::HistogramBase* db_restore_histogram_ = nullptr; | 93 base::HistogramBase* db_restore_histogram_ = nullptr; |
| 93 base::HistogramBase* value_restore_histogram_ = nullptr; | 94 base::HistogramBase* value_restore_histogram_ = nullptr; |
| 94 | 95 |
| 95 DISALLOW_COPY_AND_ASSIGN(LazyLevelDb); | 96 DISALLOW_COPY_AND_ASSIGN(LazyLevelDb); |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 #endif // EXTENSIONS_BROWSER_VALUE_STORE_LAZY_LEVELDB_H_ | 99 #endif // EXTENSIONS_BROWSER_VALUE_STORE_LAZY_LEVELDB_H_ |
| OLD | NEW |