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

Side by Side Diff: components/leveldb/leveldb_database_impl.cc

Issue 2285623002: [Leveldb] Use std::{string,vector} instead of mojo::{String,Array}. (Closed)
Patch Set: Address comments from Yuzhu Created 4 years, 3 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/leveldb/leveldb_database_impl.h ('k') | components/leveldb/leveldb_service_impl.h » ('j') | 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/leveldb/leveldb_database_impl.h" 5 #include "components/leveldb/leveldb_database_impl.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/optional.h"
10 #include "base/rand_util.h" 11 #include "base/rand_util.h"
11 #include "components/leveldb/env_mojo.h" 12 #include "components/leveldb/env_mojo.h"
12 #include "components/leveldb/public/cpp/util.h" 13 #include "components/leveldb/public/cpp/util.h"
13 #include "mojo/common/common_type_converters.h"
14 #include "third_party/leveldatabase/src/include/leveldb/db.h" 14 #include "third_party/leveldatabase/src/include/leveldb/db.h"
15 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 15 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
16 16
17 namespace leveldb { 17 namespace leveldb {
18 namespace { 18 namespace {
19 19
20 template <typename T> 20 template <typename T>
21 uint64_t GetSafeRandomId(const std::map<uint64_t, T>& m) { 21 uint64_t GetSafeRandomId(const std::map<uint64_t, T>& m) {
22 // Associate a random unsigned 64 bit handle to |s|, checking for the highly 22 // Associate a random unsigned 64 bit handle to |s|, checking for the highly
23 // improbable id duplication or castability to null. 23 // improbable id duplication or castability to null.
(...skipping 29 matching lines...) Expand all
53 environment_(std::move(environment)), 53 environment_(std::move(environment)),
54 db_(std::move(db)) {} 54 db_(std::move(db)) {}
55 55
56 LevelDBDatabaseImpl::~LevelDBDatabaseImpl() { 56 LevelDBDatabaseImpl::~LevelDBDatabaseImpl() {
57 for (auto& p : iterator_map_) 57 for (auto& p : iterator_map_)
58 delete p.second; 58 delete p.second;
59 for (auto& p : snapshot_map_) 59 for (auto& p : snapshot_map_)
60 db_->ReleaseSnapshot(p.second); 60 db_->ReleaseSnapshot(p.second);
61 } 61 }
62 62
63 void LevelDBDatabaseImpl::Put(mojo::Array<uint8_t> key, 63 void LevelDBDatabaseImpl::Put(const std::vector<uint8_t>& key,
64 mojo::Array<uint8_t> value, 64 const std::vector<uint8_t>& value,
65 const PutCallback& callback) { 65 const PutCallback& callback) {
66 leveldb::Status status = 66 leveldb::Status status =
67 db_->Put(leveldb::WriteOptions(), GetSliceFor(key), GetSliceFor(value)); 67 db_->Put(leveldb::WriteOptions(), GetSliceFor(key), GetSliceFor(value));
68 callback.Run(LeveldbStatusToError(status)); 68 callback.Run(LeveldbStatusToError(status));
69 } 69 }
70 70
71 void LevelDBDatabaseImpl::Delete(mojo::Array<uint8_t> key, 71 void LevelDBDatabaseImpl::Delete(const std::vector<uint8_t>& key,
72 const DeleteCallback& callback) { 72 const DeleteCallback& callback) {
73 leveldb::Status status = 73 leveldb::Status status =
74 db_->Delete(leveldb::WriteOptions(), GetSliceFor(key)); 74 db_->Delete(leveldb::WriteOptions(), GetSliceFor(key));
75 callback.Run(LeveldbStatusToError(status)); 75 callback.Run(LeveldbStatusToError(status));
76 } 76 }
77 77
78 void LevelDBDatabaseImpl::DeletePrefixed( 78 void LevelDBDatabaseImpl::DeletePrefixed(
79 mojo::Array<uint8_t> key_prefix, 79 const std::vector<uint8_t>& key_prefix,
80 const DeletePrefixedCallback& callback) { 80 const DeletePrefixedCallback& callback) {
81 leveldb::WriteBatch batch; 81 leveldb::WriteBatch batch;
82 leveldb::Status status = DeletePrefixedHelper( 82 leveldb::Status status = DeletePrefixedHelper(
83 GetSliceFor(key_prefix), &batch); 83 GetSliceFor(key_prefix), &batch);
84 if (status.ok()) 84 if (status.ok())
85 status = db_->Write(leveldb::WriteOptions(), &batch); 85 status = db_->Write(leveldb::WriteOptions(), &batch);
86 callback.Run(LeveldbStatusToError(status)); 86 callback.Run(LeveldbStatusToError(status));
87 } 87 }
88 88
89 void LevelDBDatabaseImpl::Write( 89 void LevelDBDatabaseImpl::Write(
90 mojo::Array<mojom::BatchedOperationPtr> operations, 90 std::vector<mojom::BatchedOperationPtr> operations,
91 const WriteCallback& callback) { 91 const WriteCallback& callback) {
92 leveldb::WriteBatch batch; 92 leveldb::WriteBatch batch;
93 93
94 for (size_t i = 0; i < operations.size(); ++i) { 94 for (size_t i = 0; i < operations.size(); ++i) {
95 switch (operations[i]->type) { 95 switch (operations[i]->type) {
96 case mojom::BatchOperationType::PUT_KEY: { 96 case mojom::BatchOperationType::PUT_KEY: {
97 batch.Put(GetSliceFor(operations[i]->key), 97 if (operations[i]->value) {
98 GetSliceFor(operations[i]->value)); 98 batch.Put(GetSliceFor(operations[i]->key),
99 GetSliceFor(*(operations[i]->value)));
100 } else {
101 batch.Put(GetSliceFor(operations[i]->key), leveldb::Slice());
102 }
99 break; 103 break;
100 } 104 }
101 case mojom::BatchOperationType::DELETE_KEY: { 105 case mojom::BatchOperationType::DELETE_KEY: {
102 batch.Delete(GetSliceFor(operations[i]->key)); 106 batch.Delete(GetSliceFor(operations[i]->key));
103 break; 107 break;
104 } 108 }
105 case mojom::BatchOperationType::DELETE_PREFIXED_KEY: { 109 case mojom::BatchOperationType::DELETE_PREFIXED_KEY: {
106 DeletePrefixedHelper(GetSliceFor(operations[i]->key), &batch); 110 DeletePrefixedHelper(GetSliceFor(operations[i]->key), &batch);
107 break; 111 break;
108 } 112 }
109 } 113 }
110 } 114 }
111 115
112 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 116 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
113 callback.Run(LeveldbStatusToError(status)); 117 callback.Run(LeveldbStatusToError(status));
114 } 118 }
115 119
116 void LevelDBDatabaseImpl::Get(mojo::Array<uint8_t> key, 120 void LevelDBDatabaseImpl::Get(const std::vector<uint8_t>& key,
117 const GetCallback& callback) { 121 const GetCallback& callback) {
118 std::string value; 122 std::string value;
119 leveldb::Status status = 123 leveldb::Status status =
120 db_->Get(leveldb::ReadOptions(), GetSliceFor(key), &value); 124 db_->Get(leveldb::ReadOptions(), GetSliceFor(key), &value);
121 callback.Run(LeveldbStatusToError(status), mojo::Array<uint8_t>::From(value)); 125 callback.Run(LeveldbStatusToError(status), StdStringToUint8Vector(value));
122 } 126 }
123 127
124 void LevelDBDatabaseImpl::GetPrefixed(mojo::Array<uint8_t> key_prefix, 128 void LevelDBDatabaseImpl::GetPrefixed(const std::vector<uint8_t>& key_prefix,
125 const GetPrefixedCallback& callback) { 129 const GetPrefixedCallback& callback) {
126 mojo::Array<mojom::KeyValuePtr> data; 130 std::vector<mojom::KeyValuePtr> data;
127 leveldb::Status status = ForEachWithPrefix( 131 leveldb::Status status = ForEachWithPrefix(
128 db_.get(), GetSliceFor(key_prefix), 132 db_.get(), GetSliceFor(key_prefix),
129 [&data](const leveldb::Slice& key, const leveldb::Slice& value) { 133 [&data](const leveldb::Slice& key, const leveldb::Slice& value) {
130 mojom::KeyValuePtr kv = mojom::KeyValue::New(); 134 mojom::KeyValuePtr kv = mojom::KeyValue::New();
131 kv->key = GetArrayFor(key); 135 kv->key = GetVectorFor(key);
132 kv->value = GetArrayFor(value); 136 kv->value = GetVectorFor(value);
133 data.push_back(std::move(kv)); 137 data.push_back(std::move(kv));
134 }); 138 });
135 callback.Run(LeveldbStatusToError(status), std::move(data)); 139 callback.Run(LeveldbStatusToError(status), std::move(data));
136 } 140 }
137 141
138 void LevelDBDatabaseImpl::GetSnapshot(const GetSnapshotCallback& callback) { 142 void LevelDBDatabaseImpl::GetSnapshot(const GetSnapshotCallback& callback) {
139 const Snapshot* s = db_->GetSnapshot(); 143 const Snapshot* s = db_->GetSnapshot();
140 uint64_t new_id = GetSafeRandomId(snapshot_map_); 144 uint64_t new_id = GetSafeRandomId(snapshot_map_);
141 snapshot_map_.insert(std::make_pair(new_id, s)); 145 snapshot_map_.insert(std::make_pair(new_id, s));
142 callback.Run(new_id); 146 callback.Run(new_id);
143 } 147 }
144 148
145 void LevelDBDatabaseImpl::ReleaseSnapshot(uint64_t snapshot_id) { 149 void LevelDBDatabaseImpl::ReleaseSnapshot(uint64_t snapshot_id) {
146 auto it = snapshot_map_.find(snapshot_id); 150 auto it = snapshot_map_.find(snapshot_id);
147 if (it != snapshot_map_.end()) { 151 if (it != snapshot_map_.end()) {
148 db_->ReleaseSnapshot(it->second); 152 db_->ReleaseSnapshot(it->second);
149 snapshot_map_.erase(it); 153 snapshot_map_.erase(it);
150 } 154 }
151 } 155 }
152 156
153 void LevelDBDatabaseImpl::GetFromSnapshot(uint64_t snapshot_id, 157 void LevelDBDatabaseImpl::GetFromSnapshot(uint64_t snapshot_id,
154 mojo::Array<uint8_t> key, 158 const std::vector<uint8_t>& key,
155 const GetCallback& callback) { 159 const GetCallback& callback) {
156 // If the snapshot id is invalid, send back invalid argument 160 // If the snapshot id is invalid, send back invalid argument
157 auto it = snapshot_map_.find(snapshot_id); 161 auto it = snapshot_map_.find(snapshot_id);
158 if (it == snapshot_map_.end()) { 162 if (it == snapshot_map_.end()) {
159 callback.Run(mojom::DatabaseError::INVALID_ARGUMENT, 163 callback.Run(mojom::DatabaseError::INVALID_ARGUMENT,
160 mojo::Array<uint8_t>()); 164 std::vector<uint8_t>());
161 return; 165 return;
162 } 166 }
163 167
164 std::string value; 168 std::string value;
165 leveldb::ReadOptions options; 169 leveldb::ReadOptions options;
166 options.snapshot = it->second; 170 options.snapshot = it->second;
167 leveldb::Status status = db_->Get(options, GetSliceFor(key), &value); 171 leveldb::Status status = db_->Get(options, GetSliceFor(key), &value);
168 callback.Run(LeveldbStatusToError(status), mojo::Array<uint8_t>::From(value)); 172 callback.Run(LeveldbStatusToError(status), StdStringToUint8Vector(value));
169 } 173 }
170 174
171 void LevelDBDatabaseImpl::NewIterator(const NewIteratorCallback& callback) { 175 void LevelDBDatabaseImpl::NewIterator(const NewIteratorCallback& callback) {
172 Iterator* iterator = db_->NewIterator(leveldb::ReadOptions()); 176 Iterator* iterator = db_->NewIterator(leveldb::ReadOptions());
173 uint64_t new_id = GetSafeRandomId(iterator_map_); 177 uint64_t new_id = GetSafeRandomId(iterator_map_);
174 iterator_map_.insert(std::make_pair(new_id, iterator)); 178 iterator_map_.insert(std::make_pair(new_id, iterator));
175 callback.Run(new_id); 179 callback.Run(new_id);
176 } 180 }
177 181
178 void LevelDBDatabaseImpl::NewIteratorFromSnapshot( 182 void LevelDBDatabaseImpl::NewIteratorFromSnapshot(
(...skipping 21 matching lines...) Expand all
200 delete it->second; 204 delete it->second;
201 iterator_map_.erase(it); 205 iterator_map_.erase(it);
202 } 206 }
203 } 207 }
204 208
205 void LevelDBDatabaseImpl::IteratorSeekToFirst( 209 void LevelDBDatabaseImpl::IteratorSeekToFirst(
206 uint64_t iterator_id, 210 uint64_t iterator_id,
207 const IteratorSeekToFirstCallback& callback) { 211 const IteratorSeekToFirstCallback& callback) {
208 auto it = iterator_map_.find(iterator_id); 212 auto it = iterator_map_.find(iterator_id);
209 if (it == iterator_map_.end()) { 213 if (it == iterator_map_.end()) {
210 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, nullptr, 214 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, base::nullopt,
211 nullptr); 215 base::nullopt);
212 return; 216 return;
213 } 217 }
214 218
215 it->second->SeekToFirst(); 219 it->second->SeekToFirst();
216 220
217 ReplyToIteratorMessage(it->second, callback); 221 ReplyToIteratorMessage(it->second, callback);
218 } 222 }
219 223
220 void LevelDBDatabaseImpl::IteratorSeekToLast( 224 void LevelDBDatabaseImpl::IteratorSeekToLast(
221 uint64_t iterator_id, 225 uint64_t iterator_id,
222 const IteratorSeekToLastCallback& callback) { 226 const IteratorSeekToLastCallback& callback) {
223 auto it = iterator_map_.find(iterator_id); 227 auto it = iterator_map_.find(iterator_id);
224 if (it == iterator_map_.end()) { 228 if (it == iterator_map_.end()) {
225 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, nullptr, 229 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, base::nullopt,
226 nullptr); 230 base::nullopt);
227 return; 231 return;
228 } 232 }
229 233
230 it->second->SeekToLast(); 234 it->second->SeekToLast();
231 235
232 ReplyToIteratorMessage(it->second, callback); 236 ReplyToIteratorMessage(it->second, callback);
233 } 237 }
234 238
235 void LevelDBDatabaseImpl::IteratorSeek( 239 void LevelDBDatabaseImpl::IteratorSeek(
236 uint64_t iterator_id, 240 uint64_t iterator_id,
237 mojo::Array<uint8_t> target, 241 const std::vector<uint8_t>& target,
238 const IteratorSeekToLastCallback& callback) { 242 const IteratorSeekToLastCallback& callback) {
239 auto it = iterator_map_.find(iterator_id); 243 auto it = iterator_map_.find(iterator_id);
240 if (it == iterator_map_.end()) { 244 if (it == iterator_map_.end()) {
241 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, nullptr, 245 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, base::nullopt,
242 nullptr); 246 base::nullopt);
243 return; 247 return;
244 } 248 }
245 249
246 it->second->Seek(GetSliceFor(target)); 250 it->second->Seek(GetSliceFor(target));
247 251
248 ReplyToIteratorMessage(it->second, callback); 252 ReplyToIteratorMessage(it->second, callback);
249 } 253 }
250 254
251 void LevelDBDatabaseImpl::IteratorNext(uint64_t iterator_id, 255 void LevelDBDatabaseImpl::IteratorNext(uint64_t iterator_id,
252 const IteratorNextCallback& callback) { 256 const IteratorNextCallback& callback) {
253 auto it = iterator_map_.find(iterator_id); 257 auto it = iterator_map_.find(iterator_id);
254 if (it == iterator_map_.end()) { 258 if (it == iterator_map_.end()) {
255 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, nullptr, 259 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, base::nullopt,
256 nullptr); 260 base::nullopt);
257 return; 261 return;
258 } 262 }
259 263
260 it->second->Next(); 264 it->second->Next();
261 265
262 ReplyToIteratorMessage(it->second, callback); 266 ReplyToIteratorMessage(it->second, callback);
263 } 267 }
264 268
265 void LevelDBDatabaseImpl::IteratorPrev(uint64_t iterator_id, 269 void LevelDBDatabaseImpl::IteratorPrev(uint64_t iterator_id,
266 const IteratorPrevCallback& callback) { 270 const IteratorPrevCallback& callback) {
267 auto it = iterator_map_.find(iterator_id); 271 auto it = iterator_map_.find(iterator_id);
268 if (it == iterator_map_.end()) { 272 if (it == iterator_map_.end()) {
269 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, nullptr, 273 callback.Run(false, mojom::DatabaseError::INVALID_ARGUMENT, base::nullopt,
270 nullptr); 274 base::nullopt);
271 return; 275 return;
272 } 276 }
273 277
274 it->second->Prev(); 278 it->second->Prev();
275 279
276 ReplyToIteratorMessage(it->second, callback); 280 ReplyToIteratorMessage(it->second, callback);
277 } 281 }
278 282
279 void LevelDBDatabaseImpl::ReplyToIteratorMessage( 283 void LevelDBDatabaseImpl::ReplyToIteratorMessage(
280 leveldb::Iterator* it, 284 leveldb::Iterator* it,
281 const IteratorSeekToFirstCallback& callback) { 285 const IteratorSeekToFirstCallback& callback) {
282 if (!it->Valid()) { 286 if (!it->Valid()) {
283 callback.Run(false, LeveldbStatusToError(it->status()), nullptr, nullptr); 287 callback.Run(false, LeveldbStatusToError(it->status()), base::nullopt,
288 base::nullopt);
284 return; 289 return;
285 } 290 }
286 291
287 callback.Run(true, LeveldbStatusToError(it->status()), GetArrayFor(it->key()), 292 callback.Run(true, LeveldbStatusToError(it->status()),
288 GetArrayFor(it->value())); 293 GetVectorFor(it->key()), GetVectorFor(it->value()));
289 } 294 }
290 295
291 leveldb::Status LevelDBDatabaseImpl::DeletePrefixedHelper( 296 leveldb::Status LevelDBDatabaseImpl::DeletePrefixedHelper(
292 const leveldb::Slice& key_prefix, 297 const leveldb::Slice& key_prefix,
293 leveldb::WriteBatch* batch) { 298 leveldb::WriteBatch* batch) {
294 leveldb::Status status = ForEachWithPrefix(db_.get(), key_prefix, 299 leveldb::Status status = ForEachWithPrefix(db_.get(), key_prefix,
295 [batch](const leveldb::Slice& key, const leveldb::Slice& value) { 300 [batch](const leveldb::Slice& key, const leveldb::Slice& value) {
296 batch->Delete(key); 301 batch->Delete(key);
297 }); 302 });
298 return status; 303 return status;
299 } 304 }
300 305
301 } // namespace leveldb 306 } // namespace leveldb
OLDNEW
« no previous file with comments | « components/leveldb/leveldb_database_impl.h ('k') | components/leveldb/leveldb_service_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698