Chromium Code Reviews| 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/optional.h" | |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "content/common/leveldb_wrapper.mojom.h" | 17 #include "content/common/leveldb_wrapper.mojom.h" |
| 17 #include "mojo/public/cpp/bindings/binding_set.h" | 18 #include "mojo/public/cpp/bindings/binding_set.h" |
| 18 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | 19 #include "mojo/public/cpp/bindings/interface_ptr_set.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 // This is a wrapper around a leveldb::mojom::LevelDBDatabase. Multiple | 23 // This is a wrapper around a leveldb::mojom::LevelDBDatabase. Multiple |
| 23 // interface | 24 // interface |
| 24 // pointers can be bound to the same object. The wrapper adds a couple of | 25 // pointers can be bound to the same object. The wrapper adds a couple of |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 45 void Bind(mojom::LevelDBWrapperRequest request); | 46 void Bind(mojom::LevelDBWrapperRequest request); |
| 46 void AddObserver(mojom::LevelDBObserverPtr observer); | 47 void AddObserver(mojom::LevelDBObserverPtr observer); |
| 47 | 48 |
| 48 // Commence aggressive flushing. This should be called early during startup, | 49 // Commence aggressive flushing. This should be called early during startup, |
| 49 // before any localStorage writing. Currently scheduled writes will not be | 50 // before any localStorage writing. Currently scheduled writes will not be |
| 50 // rescheduled and will be flushed at the scheduled time after which | 51 // rescheduled and will be flushed at the scheduled time after which |
| 51 // aggressive flushing will commence. | 52 // aggressive flushing will commence. |
| 52 static void EnableAggressiveCommitDelay(); | 53 static void EnableAggressiveCommitDelay(); |
| 53 | 54 |
| 54 private: | 55 private: |
| 55 using ValueMap = std::map<mojo::Array<uint8_t>, mojo::Array<uint8_t>>; | 56 using ValueMap = |
| 57 std::map<std::vector<uint8_t>, base::Optional<std::vector<uint8_t>>>; | |
|
michaeln
2016/08/02 00:28:29
why is the value type optional?
leonhsl(Using Gerrit)
2016/08/02 11:11:05
Yeah, it is to distinguish between puts and delete
| |
| 56 | 58 |
| 57 // Used to rate limit commits. | 59 // Used to rate limit commits. |
| 58 class RateLimiter { | 60 class RateLimiter { |
| 59 public: | 61 public: |
| 60 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); | 62 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum); |
| 61 | 63 |
| 62 void add_samples(size_t samples) { samples_ += samples; } | 64 void add_samples(size_t samples) { samples_ += samples; } |
| 63 | 65 |
| 64 // Computes the total time needed to process the total samples seen | 66 // Computes the total time needed to process the total samples seen |
| 65 // at the desired rate. | 67 // at the desired rate. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 80 struct CommitBatch { | 82 struct CommitBatch { |
| 81 bool clear_all_first; | 83 bool clear_all_first; |
| 82 ValueMap changed_values; | 84 ValueMap changed_values; |
| 83 | 85 |
| 84 CommitBatch(); | 86 CommitBatch(); |
| 85 ~CommitBatch(); | 87 ~CommitBatch(); |
| 86 size_t GetDataSize() const; | 88 size_t GetDataSize() const; |
| 87 }; | 89 }; |
| 88 | 90 |
| 89 // LevelDBWrapperImpl: | 91 // LevelDBWrapperImpl: |
| 90 void Put(mojo::Array<uint8_t> key, | 92 void Put(const std::vector<uint8_t>& key, |
| 91 mojo::Array<uint8_t> value, | 93 const std::vector<uint8_t>& value, |
| 92 const mojo::String& source, | 94 const std::string& source, |
| 93 const PutCallback& callback) override; | 95 const PutCallback& callback) override; |
| 94 void Delete(mojo::Array<uint8_t> key, | 96 void Delete(const std::vector<uint8_t>& key, |
| 95 const mojo::String& source, | 97 const std::string& source, |
| 96 const DeleteCallback& callback) override; | 98 const DeleteCallback& callback) override; |
| 97 void DeleteAll(const mojo::String& source, | 99 void DeleteAll(const std::string& source, |
| 98 const DeleteAllCallback& callback) override; | 100 const DeleteAllCallback& callback) override; |
| 99 void Get(mojo::Array<uint8_t> key, const GetCallback& callback) override; | 101 void Get(const std::vector<uint8_t>& key, |
| 100 void GetAll(const mojo::String& source, | 102 const GetCallback& callback) override; |
| 103 void GetAll(const std::string& source, | |
| 101 const GetAllCallback& callback) override; | 104 const GetAllCallback& callback) override; |
| 102 | 105 |
| 103 void OnConnectionError(); | 106 void OnConnectionError(); |
| 104 void LoadMap(const base::Closure& completion_callback); | 107 void LoadMap(const base::Closure& completion_callback); |
| 105 void OnLoadComplete(leveldb::mojom::DatabaseError status, | 108 void OnLoadComplete(leveldb::mojom::DatabaseError status, |
| 106 mojo::Array<leveldb::mojom::KeyValuePtr> data); | 109 mojo::Array<leveldb::mojom::KeyValuePtr> data); |
| 107 void CreateCommitBatchIfNeeded(); | 110 void CreateCommitBatchIfNeeded(); |
| 108 void StartCommitTimer(); | 111 void StartCommitTimer(); |
| 109 base::TimeDelta ComputeCommitDelay() const; | 112 base::TimeDelta ComputeCommitDelay() const; |
| 110 void CommitChanges(); | 113 void CommitChanges(); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 128 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; | 131 base::WeakPtrFactory<LevelDBWrapperImpl> weak_ptr_factory_; |
| 129 | 132 |
| 130 static bool s_aggressive_flushing_enabled_; | 133 static bool s_aggressive_flushing_enabled_; |
| 131 | 134 |
| 132 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); | 135 DISALLOW_COPY_AND_ASSIGN(LevelDBWrapperImpl); |
| 133 }; | 136 }; |
| 134 | 137 |
| 135 } // namespace content | 138 } // namespace content |
| 136 | 139 |
| 137 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ | 140 #endif // CONTENT_BROWSER_LEVELDB_WRAPPER_IMPL_H_ |
| OLD | NEW |