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 CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
6 #define CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 6 #define CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 void Bind(mojom::LevelDBWrapperRequest request); | 51 void Bind(mojom::LevelDBWrapperRequest request); |
52 | 52 |
53 size_t bytes_used() const { return bytes_used_; } | 53 size_t bytes_used() const { return bytes_used_; } |
54 | 54 |
55 // Commence aggressive flushing. This should be called early during startup, | 55 // Commence aggressive flushing. This should be called early during startup, |
56 // before any localStorage writing. Currently scheduled writes will not be | 56 // before any localStorage writing. Currently scheduled writes will not be |
57 // rescheduled and will be flushed at the scheduled time after which | 57 // rescheduled and will be flushed at the scheduled time after which |
58 // aggressive flushing will commence. | 58 // aggressive flushing will commence. |
59 static void EnableAggressiveCommitDelay(); | 59 static void EnableAggressiveCommitDelay(); |
60 | 60 |
| 61 // Commits any uncommitted data to the database as soon as possible. This |
| 62 // usually means data will be committed immediately, but if we're currently |
| 63 // waiting on the result of initializing our map the commit won't happen |
| 64 // until the load has finished. |
| 65 void ScheduleImmediateCommit(); |
| 66 |
| 67 // LevelDBWrapper: |
| 68 void AddObserver(mojom::LevelDBObserverAssociatedPtrInfo observer) override; |
| 69 void Put(const std::vector<uint8_t>& key, |
| 70 const std::vector<uint8_t>& value, |
| 71 const std::string& source, |
| 72 const PutCallback& callback) override; |
| 73 void Delete(const std::vector<uint8_t>& key, |
| 74 const std::string& source, |
| 75 const DeleteCallback& callback) override; |
| 76 void DeleteAll(const std::string& source, |
| 77 const DeleteAllCallback& callback) override; |
| 78 void Get(const std::vector<uint8_t>& key, |
| 79 const GetCallback& callback) override; |
| 80 void GetAll( |
| 81 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo complete_callback, |
| 82 const GetAllCallback& callback) override; |
| 83 |
61 private: | 84 private: |
62 friend class LevelDBWrapperImplTest; | |
63 | |
64 using ValueMap = std::map<std::vector<uint8_t>, std::vector<uint8_t>>; | 85 using ValueMap = std::map<std::vector<uint8_t>, std::vector<uint8_t>>; |
65 | 86 |
66 // Used to rate limit commits. | 87 // Used to rate limit commits. |
67 class RateLimiter { | 88 class RateLimiter { |
68 public: | 89 public: |
69 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); | 90 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); |
70 | 91 |
71 void add_samples(size_t samples) { samples_ += samples; } | 92 void add_samples(size_t samples) { samples_ += samples; } |
72 | 93 |
73 // Computes the total time needed to process the total samples seen | 94 // Computes the total time needed to process the total samples seen |
(...skipping 13 matching lines...) Expand all Loading... |
87 }; | 108 }; |
88 | 109 |
89 struct CommitBatch { | 110 struct CommitBatch { |
90 bool clear_all_first; | 111 bool clear_all_first; |
91 std::set<std::vector<uint8_t>> changed_keys; | 112 std::set<std::vector<uint8_t>> changed_keys; |
92 | 113 |
93 CommitBatch(); | 114 CommitBatch(); |
94 ~CommitBatch(); | 115 ~CommitBatch(); |
95 }; | 116 }; |
96 | 117 |
97 // LevelDBWrapperImpl: | |
98 void AddObserver(mojom::LevelDBObserverAssociatedPtrInfo observer) override; | |
99 void Put(const std::vector<uint8_t>& key, | |
100 const std::vector<uint8_t>& value, | |
101 const std::string& source, | |
102 const PutCallback& callback) override; | |
103 void Delete(const std::vector<uint8_t>& key, | |
104 const std::string& source, | |
105 const DeleteCallback& callback) override; | |
106 void DeleteAll(const std::string& source, | |
107 const DeleteAllCallback& callback) override; | |
108 void Get(const std::vector<uint8_t>& key, | |
109 const GetCallback& callback) override; | |
110 void GetAll( | |
111 mojom::LevelDBWrapperGetAllCallbackAssociatedPtrInfo complete_callback, | |
112 const GetAllCallback& callback) override; | |
113 | |
114 void OnConnectionError(); | 118 void OnConnectionError(); |
115 void LoadMap(const base::Closure& completion_callback); | 119 void LoadMap(const base::Closure& completion_callback); |
116 void OnLoadComplete(leveldb::mojom::DatabaseError status, | 120 void OnLoadComplete(leveldb::mojom::DatabaseError status, |
117 std::vector<leveldb::mojom::KeyValuePtr> data); | 121 std::vector<leveldb::mojom::KeyValuePtr> data); |
118 void CreateCommitBatchIfNeeded(); | 122 void CreateCommitBatchIfNeeded(); |
119 void StartCommitTimer(); | 123 void StartCommitTimer(); |
120 base::TimeDelta ComputeCommitDelay() const; | 124 base::TimeDelta ComputeCommitDelay() const; |
121 void CommitChanges(); | 125 void CommitChanges(); |
122 void OnCommitComplete(leveldb::mojom::DatabaseError error); | 126 void OnCommitComplete(leveldb::mojom::DatabaseError error); |
123 | 127 |
(...skipping 16 matching lines...) Expand all Loading... |
140 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; | 144 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; |
141 | 145 |
142 static bool s_aggressive_flushing_enabled_; | 146 static bool s_aggressive_flushing_enabled_; |
143 | 147 |
144 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); | 148 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); |
145 }; | 149 }; |
146 | 150 |
147 } // namespace content | 151 } // namespace content |
148 | 152 |
149 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 153 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
OLD | NEW |