lazy_leveldb.h 4.1 KB

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