leveldb_database.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2014 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_LEVELDB_PROTO_INTERNAL_LEVELDB_DATABASE_H_
  5. #define COMPONENTS_LEVELDB_PROTO_INTERNAL_LEVELDB_DATABASE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/component_export.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/strings/string_split.h"
  13. #include "components/leveldb_proto/public/proto_database.h"
  14. #include "third_party/leveldatabase/env_chromium.h"
  15. namespace base {
  16. class FilePath;
  17. class HistogramBase;
  18. } // namespace base
  19. namespace leveldb {
  20. class DB;
  21. class Env;
  22. } // namespace leveldb
  23. namespace leveldb_proto {
  24. // Interacts with the LevelDB third party module.
  25. // Once constructed, function calls and destruction should all occur on the
  26. // same thread (not necessarily the same as the constructor).
  27. class COMPONENT_EXPORT(LEVELDB_PROTO) LevelDB {
  28. public:
  29. // Decides the next action based on the key.
  30. //
  31. // Returns `kSkipAndStop` when |while_callback| is false. Skips entries when
  32. // |filter| is false. Doesn't ever return `kLoadAndStop`.
  33. static Enums::KeyIteratorAction ComputeIteratorAction(
  34. const KeyFilter& while_callback,
  35. const KeyFilter& filter,
  36. const std::string& key);
  37. // Constructor. Does *not* open a leveldb - only initialize this class.
  38. // |client_name| is the name of the "client" that owns this instance. Used
  39. // for UMA statics as so: LevelDB.<value>.<client name>. It is best to not
  40. // change once shipped.
  41. explicit LevelDB(const char* client_name);
  42. LevelDB(const LevelDB&) = delete;
  43. LevelDB& operator=(const LevelDB&) = delete;
  44. virtual ~LevelDB();
  45. // Initializes a leveldb with the given options. If |database_dir| is
  46. // empty, this opens an in-memory db.
  47. virtual bool Init(const base::FilePath& database_dir,
  48. const leveldb_env::Options& options);
  49. virtual leveldb::Status Init(const base::FilePath& database_dir,
  50. const leveldb_env::Options& options,
  51. bool destroy_on_corruption);
  52. virtual bool Save(const base::StringPairs& pairs_to_save,
  53. const std::vector<std::string>& keys_to_remove,
  54. leveldb::Status* status);
  55. virtual bool UpdateWithRemoveFilter(const base::StringPairs& entries_to_save,
  56. const KeyFilter& delete_key_filter,
  57. leveldb::Status* status);
  58. virtual bool UpdateWithRemoveFilter(const base::StringPairs& entries_to_save,
  59. const KeyFilter& delete_key_filter,
  60. const std::string& target_prefix,
  61. leveldb::Status* status);
  62. virtual bool Load(std::vector<std::string>* entries);
  63. virtual bool LoadWithFilter(const KeyFilter& filter,
  64. std::vector<std::string>* entries);
  65. virtual bool LoadWithFilter(const KeyFilter& filter,
  66. std::vector<std::string>* entries,
  67. const leveldb::ReadOptions& options,
  68. const std::string& target_prefix);
  69. virtual bool LoadKeysAndEntries(
  70. std::map<std::string, std::string>* keys_entries);
  71. virtual bool LoadKeysAndEntriesWithFilter(
  72. const KeyFilter& filter,
  73. std::map<std::string, std::string>* keys_entries);
  74. virtual bool LoadKeysAndEntriesWithFilter(
  75. const KeyFilter& filter,
  76. std::map<std::string, std::string>* keys_entries,
  77. const leveldb::ReadOptions& options,
  78. const std::string& target_prefix);
  79. // Retrieves consecutive keys and values.
  80. //
  81. // Starts at or after |start_key|. Loads entries when |controller| returns
  82. // `kLoadAndContinue` or `kLoadAndStop`. Finishes when |controller| returns
  83. // `kLoadAndStop` or `kSkipAndStop`.
  84. virtual bool LoadKeysAndEntriesWhile(
  85. std::map<std::string, std::string>* keys_entries,
  86. const leveldb::ReadOptions& options,
  87. const std::string& start_key,
  88. const KeyIteratorController& controller);
  89. // Retrieves consecutive keys and values.
  90. //
  91. // Starts at or after |start_key|. Skips entries when |filter| returns
  92. // `false`. Stops scanning and skips the entry when |while_callback| returns
  93. // `false`.
  94. virtual bool LoadKeysAndEntriesWhile(
  95. const KeyFilter& filter,
  96. std::map<std::string, std::string>* keys_entries,
  97. const leveldb::ReadOptions& options,
  98. const std::string& start_key,
  99. const KeyFilter& while_callback);
  100. virtual bool LoadKeys(std::vector<std::string>* keys);
  101. virtual bool LoadKeys(const std::string& target_prefix,
  102. std::vector<std::string>* keys);
  103. virtual bool Get(const std::string& key,
  104. bool* found,
  105. std::string* entry,
  106. leveldb::Status* status);
  107. // Close (if currently open) and then destroy (i.e. delete) the database
  108. // directory.
  109. virtual leveldb::Status Destroy();
  110. // Returns true if we successfully read the approximate memory usage property
  111. // from the LevelDB.
  112. bool GetApproximateMemoryUse(uint64_t* approx_mem);
  113. private:
  114. DFAKE_MUTEX(thread_checker_);
  115. // The declaration order of these members matters: |db_| depends on |env_| and
  116. // therefore has to be destructed first.
  117. std::unique_ptr<leveldb::Env> env_;
  118. std::unique_ptr<leveldb::DB> db_;
  119. base::FilePath database_dir_;
  120. leveldb_env::Options open_options_;
  121. raw_ptr<base::HistogramBase> approx_memtable_mem_histogram_;
  122. };
  123. } // namespace leveldb_proto
  124. #endif // COMPONENTS_LEVELDB_PROTO_INTERNAL_LEVELDB_DATABASE_H_