Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: content/browser/indexed_db/leveldb/leveldb_database.h

Issue 2760163002: [IndexedDB] Pool and evict leveldb iterators, to save memory (Closed)
Patch Set: comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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,
87 size_t max_open_cursors,
80 std::unique_ptr<LevelDBDatabase>* db, 88 std::unique_ptr<LevelDBDatabase>* db,
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
105 protected: 114 protected:
106 LevelDBDatabase(); 115 LevelDBDatabase(size_t max_open_iterators);
107 116
108 private: 117 private:
109 friend class LevelDBSnapshot; 118 friend class LevelDBSnapshot;
119 friend class LevelDBIteratorImpl;
120
121 // Methods for iterator pooling.
122 std::unique_ptr<leveldb::Iterator> CreateLevelDBIterator(
123 const leveldb::Snapshot*);
124 void OnIteratorUsed(LevelDBIterator*);
125 void OnIteratorDestroyed(LevelDBIterator*);
110 126
111 void CloseDatabase(); 127 void CloseDatabase();
112 128
113 std::unique_ptr<leveldb::Env> env_; 129 std::unique_ptr<leveldb::Env> env_;
114 std::unique_ptr<leveldb::Comparator> comparator_adapter_; 130 std::unique_ptr<leveldb::Comparator> comparator_adapter_;
115 std::unique_ptr<leveldb::DB> db_; 131 std::unique_ptr<leveldb::DB> db_;
116 std::unique_ptr<const leveldb::FilterPolicy> filter_policy_; 132 std::unique_ptr<const leveldb::FilterPolicy> filter_policy_;
117 const LevelDBComparator* comparator_; 133 const LevelDBComparator* comparator_;
134
135 struct DetachIteratorOnDestruct {
136 DetachIteratorOnDestruct() {}
137 explicit DetachIteratorOnDestruct(LevelDBIterator* it) : it_(it) {}
138 DetachIteratorOnDestruct(DetachIteratorOnDestruct&& that) {
139 it_ = that.it_;
140 that.it_ = nullptr;
141 }
142 ~DetachIteratorOnDestruct();
143
144 private:
145 LevelDBIterator* it_ = nullptr;
146
147 DISALLOW_COPY_AND_ASSIGN(DetachIteratorOnDestruct);
148 };
149
150 // Despite the type name, this object uses LRU eviction.
151 size_t lru_max_size_ = 0;
152 base::HashingMRUCache<LevelDBIterator*, DetachIteratorOnDestruct>
153 iterator_lru_;
154
155 // Recorded for UMA reporting.
156 uint32_t num_iterators_ = 0;
157 uint32_t max_iterators_ = 0;
158
118 std::string file_name_for_tracing; 159 std::string file_name_for_tracing;
119 }; 160 };
120 161
121 } // namespace content 162 } // namespace content
122 163
123 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_ 164 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698