Chromium Code Reviews| 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 #ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ | 5 #ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ |
| 6 #define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ | 6 #define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/containers/mru_cache.h" | |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 15 #include "base/trace_event/memory_dump_provider.h" | 16 #include "base/trace_event/memory_dump_provider.h" |
| 16 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/comparator.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/comparator.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
| 19 | 20 |
| 20 namespace leveldb { | 21 namespace leveldb { |
| 21 class Comparator; | 22 class Comparator; |
| 22 class DB; | 23 class DB; |
| 23 class FilterPolicy; | 24 class FilterPolicy; |
| 25 class Iterator; | |
| 24 class Env; | 26 class Env; |
| 25 class Snapshot; | 27 class Snapshot; |
| 26 } | 28 } |
| 27 | 29 |
| 28 namespace content { | 30 namespace content { |
| 29 | 31 |
| 30 class LevelDBComparator; | 32 class LevelDBComparator; |
| 31 class LevelDBDatabase; | 33 class LevelDBDatabase; |
| 32 class LevelDBIterator; | 34 class LevelDBIterator; |
| 33 class LevelDBWriteBatch; | 35 class LevelDBWriteBatch; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 51 LevelDBLock() {} | 53 LevelDBLock() {} |
| 52 virtual ~LevelDBLock() {} | 54 virtual ~LevelDBLock() {} |
| 53 | 55 |
| 54 private: | 56 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(LevelDBLock); | 57 DISALLOW_COPY_AND_ASSIGN(LevelDBLock); |
| 56 }; | 58 }; |
| 57 | 59 |
| 58 class CONTENT_EXPORT LevelDBDatabase | 60 class CONTENT_EXPORT LevelDBDatabase |
| 59 : public base::trace_event::MemoryDumpProvider { | 61 : public base::trace_event::MemoryDumpProvider { |
| 60 public: | 62 public: |
| 63 // Necessary because every iterator hangs onto leveldb blocks which can be | |
| 64 // large. See https://crbug/696055. | |
| 65 static const size_t kDefaultMaxOpenIteratorsPerDatabase = 50; | |
| 66 | |
| 61 class ComparatorAdapter : public leveldb::Comparator { | 67 class ComparatorAdapter : public leveldb::Comparator { |
| 62 public: | 68 public: |
| 63 explicit ComparatorAdapter(const LevelDBComparator* comparator); | 69 explicit ComparatorAdapter(const LevelDBComparator* comparator); |
| 64 | 70 |
| 65 int Compare(const leveldb::Slice& a, | 71 int Compare(const leveldb::Slice& a, |
| 66 const leveldb::Slice& b) const override; | 72 const leveldb::Slice& b) const override; |
| 67 | 73 |
| 68 const char* Name() const override; | 74 const char* Name() const override; |
| 69 | 75 |
| 70 void FindShortestSeparator(std::string* start, | 76 void FindShortestSeparator(std::string* start, |
| 71 const leveldb::Slice& limit) const override; | 77 const leveldb::Slice& limit) const override; |
| 72 void FindShortSuccessor(std::string* key) const override; | 78 void FindShortSuccessor(std::string* key) const override; |
| 73 | 79 |
| 74 private: | 80 private: |
| 75 const LevelDBComparator* comparator_; | 81 const LevelDBComparator* comparator_; |
| 76 }; | 82 }; |
| 77 | 83 |
| 84 // |max_open_cursors| cannot be 0. | |
| 78 static leveldb::Status Open(const base::FilePath& file_name, | 85 static leveldb::Status Open(const base::FilePath& file_name, |
| 79 const LevelDBComparator* comparator, | 86 const LevelDBComparator* comparator, |
| 80 std::unique_ptr<LevelDBDatabase>* db, | 87 std::unique_ptr<LevelDBDatabase>* db, |
| 88 size_t max_open_cursors, | |
|
pwnall
2017/03/27 17:08:04
Sorry I caught this so late, but I think this para
dmurph
2017/03/27 21:32:04
Good catch, thanks.
| |
| 81 bool* is_disk_full = 0); | 89 bool* is_disk_full = 0); |
| 90 | |
| 82 static std::unique_ptr<LevelDBDatabase> OpenInMemory( | 91 static std::unique_ptr<LevelDBDatabase> OpenInMemory( |
| 83 const LevelDBComparator* comparator); | 92 const LevelDBComparator* comparator); |
| 84 static leveldb::Status Destroy(const base::FilePath& file_name); | 93 static leveldb::Status Destroy(const base::FilePath& file_name); |
| 85 static std::unique_ptr<LevelDBLock> LockForTesting( | 94 static std::unique_ptr<LevelDBLock> LockForTesting( |
| 86 const base::FilePath& file_name); | 95 const base::FilePath& file_name); |
| 87 ~LevelDBDatabase() override; | 96 ~LevelDBDatabase() override; |
| 88 | 97 |
| 89 leveldb::Status Put(const base::StringPiece& key, std::string* value); | 98 leveldb::Status Put(const base::StringPiece& key, std::string* value); |
| 90 leveldb::Status Remove(const base::StringPiece& key); | 99 leveldb::Status Remove(const base::StringPiece& key); |
| 91 virtual leveldb::Status Get(const base::StringPiece& key, | 100 virtual leveldb::Status Get(const base::StringPiece& key, |
| 92 std::string* value, | 101 std::string* value, |
| 93 bool* found, | 102 bool* found, |
| 94 const LevelDBSnapshot* = 0); | 103 const LevelDBSnapshot* = 0); |
| 95 leveldb::Status Write(const LevelDBWriteBatch& write_batch); | 104 leveldb::Status Write(const LevelDBWriteBatch& write_batch); |
| 96 std::unique_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0); | 105 std::unique_ptr<LevelDBIterator> CreateIterator(const LevelDBSnapshot* = 0); |
| 97 const LevelDBComparator* Comparator() const; | 106 const LevelDBComparator* Comparator() const; |
| 98 void Compact(const base::StringPiece& start, const base::StringPiece& stop); | 107 void Compact(const base::StringPiece& start, const base::StringPiece& stop); |
| 99 void CompactAll(); | 108 void CompactAll(); |
| 100 | 109 |
| 101 // base::trace_event::MemoryDumpProvider implementation. | 110 // base::trace_event::MemoryDumpProvider implementation. |
| 102 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, | 111 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 103 base::trace_event::ProcessMemoryDump* pmd) override; | 112 base::trace_event::ProcessMemoryDump* pmd) override; |
| 104 | 113 |
| 114 void SetCursorPoolSizeForTesting(size_t size); | |
| 115 | |
| 105 protected: | 116 protected: |
| 106 LevelDBDatabase(); | 117 LevelDBDatabase(size_t max_open_iterators); |
| 107 | 118 |
| 108 private: | 119 private: |
| 109 friend class LevelDBSnapshot; | 120 friend class LevelDBSnapshot; |
| 121 friend class LevelDBIteratorImpl; | |
| 122 | |
| 123 // Methods for iterator pooling. | |
| 124 std::unique_ptr<leveldb::Iterator> CreateLevelDBIterator( | |
| 125 const leveldb::Snapshot*); | |
| 126 void NotifyIteratorUsed(LevelDBIterator*); | |
| 127 void NotifyIteratorDestroyed(LevelDBIterator*); | |
| 110 | 128 |
| 111 void CloseDatabase(); | 129 void CloseDatabase(); |
| 112 | 130 |
| 113 std::unique_ptr<leveldb::Env> env_; | 131 std::unique_ptr<leveldb::Env> env_; |
| 114 std::unique_ptr<leveldb::Comparator> comparator_adapter_; | 132 std::unique_ptr<leveldb::Comparator> comparator_adapter_; |
| 115 std::unique_ptr<leveldb::DB> db_; | 133 std::unique_ptr<leveldb::DB> db_; |
| 116 std::unique_ptr<const leveldb::FilterPolicy> filter_policy_; | 134 std::unique_ptr<const leveldb::FilterPolicy> filter_policy_; |
| 117 const LevelDBComparator* comparator_; | 135 const LevelDBComparator* comparator_; |
| 136 | |
| 137 struct DetachIteratorOnDestruct { | |
|
pwnall
2017/03/27 17:08:04
OnDelete / WhenDestroyed / OnDestruction?
Also, d
dmurph
2017/03/27 21:32:04
Done - we now only have the destructor in the .cc
| |
| 138 DetachIteratorOnDestruct(); | |
| 139 explicit DetachIteratorOnDestruct(LevelDBIterator*); | |
| 140 DetachIteratorOnDestruct(DetachIteratorOnDestruct&& that); | |
| 141 ~DetachIteratorOnDestruct(); | |
| 142 | |
| 143 private: | |
| 144 LevelDBIterator* it_ = nullptr; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(DetachIteratorOnDestruct); | |
| 147 }; | |
| 148 | |
| 149 // Despite the type name, this object uses LRU eviction. | |
| 150 size_t lru_max_size_ = 0; | |
| 151 base::HashingMRUCache<LevelDBIterator*, DetachIteratorOnDestruct> | |
| 152 iterator_lru_; | |
| 153 | |
| 154 // Recorded for UMA reporting. | |
| 155 uint32_t num_iterators_ = 0; | |
| 156 uint32_t max_iterators_ = 0; | |
| 157 | |
| 118 std::string file_name_for_tracing; | 158 std::string file_name_for_tracing; |
| 119 }; | 159 }; |
| 120 | 160 |
| 121 } // namespace content | 161 } // namespace content |
| 122 | 162 |
| 123 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ | 163 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ |
| OLD | NEW |