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

Side by Side Diff: components/ntp_snippets/ntp_snippets_database.cc

Issue 2033723002: NTPSnippetsDatabase: support multiple concurrent loads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@leveldb
Patch Set: Created 4 years, 6 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
« no previous file with comments | « components/ntp_snippets/ntp_snippets_database.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "components/ntp_snippets/ntp_snippets_database.h" 5 #include "components/ntp_snippets/ntp_snippets_database.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "components/leveldb_proto/proto_database_impl.h" 10 #include "components/leveldb_proto/proto_database_impl.h"
(...skipping 19 matching lines...) Expand all
30 database_initialized_(false), 30 database_initialized_(false),
31 weak_ptr_factory_(this) { 31 weak_ptr_factory_(this) {
32 database_->Init(kDatabaseUMAClientName, database_dir, 32 database_->Init(kDatabaseUMAClientName, database_dir,
33 base::Bind(&NTPSnippetsDatabase::OnDatabaseInited, 33 base::Bind(&NTPSnippetsDatabase::OnDatabaseInited,
34 weak_ptr_factory_.GetWeakPtr())); 34 weak_ptr_factory_.GetWeakPtr()));
35 } 35 }
36 36
37 NTPSnippetsDatabase::~NTPSnippetsDatabase() {} 37 NTPSnippetsDatabase::~NTPSnippetsDatabase() {}
38 38
39 void NTPSnippetsDatabase::Load(const SnippetsLoadedCallback& callback) { 39 void NTPSnippetsDatabase::Load(const SnippetsLoadedCallback& callback) {
40 DCHECK(callback_.is_null());
41 DCHECK(!callback.is_null());
42 callback_ = callback;
43 if (database_ && database_initialized_) 40 if (database_ && database_initialized_)
44 LoadImpl(); 41 LoadImpl(callback);
42 else
43 pending_load_callbacks_.emplace_back(callback);
45 } 44 }
46 45
47 void NTPSnippetsDatabase::Save(const NTPSnippet& snippet) { 46 void NTPSnippetsDatabase::Save(const NTPSnippet& snippet) {
48 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); 47 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector());
49 entries_to_save->emplace_back(snippet.id(), snippet.ToProto()); 48 entries_to_save->emplace_back(snippet.id(), snippet.ToProto());
50 SaveImpl(std::move(entries_to_save)); 49 SaveImpl(std::move(entries_to_save));
51 } 50 }
52 51
53 void NTPSnippetsDatabase::Save(const NTPSnippet::PtrVector& snippets) { 52 void NTPSnippetsDatabase::Save(const NTPSnippet::PtrVector& snippets) {
54 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); 53 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector());
(...skipping 15 matching lines...) Expand all
70 } 69 }
71 70
72 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { 71 void NTPSnippetsDatabase::OnDatabaseInited(bool success) {
73 DCHECK(!database_initialized_); 72 DCHECK(!database_initialized_);
74 if (!success) { 73 if (!success) {
75 DVLOG(1) << "NTPSnippetsDatabase init failed."; 74 DVLOG(1) << "NTPSnippetsDatabase init failed.";
76 database_.reset(); 75 database_.reset();
77 return; 76 return;
78 } 77 }
79 database_initialized_ = true; 78 database_initialized_ = true;
80 if (!callback_.is_null()) 79 for (const SnippetsLoadedCallback& callback : pending_load_callbacks_)
81 LoadImpl(); 80 LoadImpl(callback);
82 } 81 }
83 82
84 void NTPSnippetsDatabase::OnDatabaseLoaded( 83 void NTPSnippetsDatabase::OnDatabaseLoaded(
84 const SnippetsLoadedCallback& callback,
85 bool success, 85 bool success,
86 std::unique_ptr<std::vector<SnippetProto>> entries) { 86 std::unique_ptr<std::vector<SnippetProto>> entries) {
87 if (!success) { 87 if (!success) {
88 DVLOG(1) << "NTPSnippetsDatabase load failed."; 88 DVLOG(1) << "NTPSnippetsDatabase load failed.";
89 database_.reset(); 89 database_.reset();
90 return; 90 return;
91 } 91 }
92 92
93 std::unique_ptr<std::vector<std::string>> keys_to_remove( 93 std::unique_ptr<std::vector<std::string>> keys_to_remove(
94 new std::vector<std::string>()); 94 new std::vector<std::string>());
95 95
96 NTPSnippet::PtrVector snippets; 96 NTPSnippet::PtrVector snippets;
97 for (const SnippetProto& proto : *entries) { 97 for (const SnippetProto& proto : *entries) {
98 std::unique_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromProto(proto); 98 std::unique_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromProto(proto);
99 if (snippet) { 99 if (snippet) {
100 snippets.emplace_back(std::move(snippet)); 100 snippets.emplace_back(std::move(snippet));
101 } else { 101 } else {
102 LOG(WARNING) << "Invalid proto from DB " << proto.id(); 102 LOG(WARNING) << "Invalid proto from DB " << proto.id();
103 keys_to_remove->emplace_back(proto.id()); 103 keys_to_remove->emplace_back(proto.id());
104 } 104 }
105 } 105 }
106 106
107 // We only start loading if we have a non-null callback, but it's possible 107 callback.Run(std::move(snippets));
108 // that it's been cleared in the meantime.
109 DCHECK(!callback_.is_null());
110 callback_.Run(std::move(snippets));
111 callback_.Reset();
112 108
113 // If any of the snippet protos couldn't be converted to actual snippets, 109 // If any of the snippet protos couldn't be converted to actual snippets,
114 // clean them up now. 110 // clean them up now.
115 if (!keys_to_remove->empty()) 111 if (!keys_to_remove->empty())
116 DeleteImpl(std::move(keys_to_remove)); 112 DeleteImpl(std::move(keys_to_remove));
117 } 113 }
118 114
119 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { 115 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) {
120 if (!success) { 116 if (!success) {
121 DVLOG(1) << "NTPSnippetsDatabase save failed."; 117 DVLOG(1) << "NTPSnippetsDatabase save failed.";
122 database_.reset(); 118 database_.reset();
123 } 119 }
124 } 120 }
125 121
126 void NTPSnippetsDatabase::LoadImpl() { 122 void NTPSnippetsDatabase::LoadImpl(const SnippetsLoadedCallback& callback) {
127 DCHECK(database_); 123 DCHECK(database_);
128 DCHECK(database_initialized_); 124 DCHECK(database_initialized_);
129 database_->LoadEntries(base::Bind(&NTPSnippetsDatabase::OnDatabaseLoaded, 125 database_->LoadEntries(base::Bind(&NTPSnippetsDatabase::OnDatabaseLoaded,
130 weak_ptr_factory_.GetWeakPtr())); 126 weak_ptr_factory_.GetWeakPtr(),
127 callback));
131 } 128 }
132 129
133 void NTPSnippetsDatabase::SaveImpl( 130 void NTPSnippetsDatabase::SaveImpl(
134 std::unique_ptr<KeyEntryVector> entries_to_save) { 131 std::unique_ptr<KeyEntryVector> entries_to_save) {
135 if (!database_ || !database_initialized_) 132 if (!database_ || !database_initialized_)
136 return; 133 return;
137 134
138 std::unique_ptr<std::vector<std::string>> keys_to_remove( 135 std::unique_ptr<std::vector<std::string>> keys_to_remove(
139 new std::vector<std::string>()); 136 new std::vector<std::string>());
140 database_->UpdateEntries(std::move(entries_to_save), 137 database_->UpdateEntries(std::move(entries_to_save),
141 std::move(keys_to_remove), 138 std::move(keys_to_remove),
142 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, 139 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved,
143 weak_ptr_factory_.GetWeakPtr())); 140 weak_ptr_factory_.GetWeakPtr()));
144 } 141 }
145 142
146 void NTPSnippetsDatabase::DeleteImpl( 143 void NTPSnippetsDatabase::DeleteImpl(
147 std::unique_ptr<std::vector<std::string>> keys_to_remove) { 144 std::unique_ptr<std::vector<std::string>> keys_to_remove) {
148 if (!database_ || !database_initialized_) 145 if (!database_ || !database_initialized_)
149 return; 146 return;
150 147
151 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); 148 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector());
152 database_->UpdateEntries(std::move(entries_to_save), 149 database_->UpdateEntries(std::move(entries_to_save),
153 std::move(keys_to_remove), 150 std::move(keys_to_remove),
154 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, 151 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved,
155 weak_ptr_factory_.GetWeakPtr())); 152 weak_ptr_factory_.GetWeakPtr()));
156 } 153 }
157 154
158 } // namespace ntp_snippets 155 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_database.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698