| 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 #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/rand_util.h" | 10 #include "base/rand_util.h" |
| 11 #include "components/leveldb/env_mojo.h" | 11 #include "components/leveldb/env_mojo.h" |
| 12 #include "components/leveldb/util.h" | 12 #include "components/leveldb/public/cpp/util.h" |
| 13 #include "mojo/common/common_type_converters.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 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 | 117 |
| 118 std::string value; | 118 std::string value; |
| 119 leveldb::ReadOptions options; | 119 leveldb::ReadOptions options; |
| 120 options.snapshot = it->second; | 120 options.snapshot = it->second; |
| 121 leveldb::Status status = db_->Get(options, GetSliceFor(key), &value); | 121 leveldb::Status status = db_->Get(options, GetSliceFor(key), &value); |
| 122 callback.Run(LeveldbStatusToError(status), mojo::Array<uint8_t>::From(value)); | 122 callback.Run(LeveldbStatusToError(status), mojo::Array<uint8_t>::From(value)); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void LevelDBDatabaseImpl::NewIterator(const NewIteratorCallback& callback) { |
| 126 Iterator* iterator = db_->NewIterator(leveldb::ReadOptions()); |
| 127 uint64_t new_id = GetSafeRandomId(iterator_map_); |
| 128 iterator_map_.insert(std::make_pair(new_id, iterator)); |
| 129 callback.Run(new_id); |
| 130 } |
| 131 |
| 132 void LevelDBDatabaseImpl::NewIteratorFromSnapshot( |
| 133 uint64_t snapshot_id, |
| 134 const NewIteratorCallback& callback) { |
| 135 // If the snapshot id is invalid, send back invalid argument |
| 136 auto it = snapshot_map_.find(snapshot_id); |
| 137 if (it == snapshot_map_.end()) { |
| 138 callback.Run(0); |
| 139 return; |
| 140 } |
| 141 |
| 142 leveldb::ReadOptions options; |
| 143 options.snapshot = it->second; |
| 144 |
| 145 Iterator* iterator = db_->NewIterator(options); |
| 146 uint64_t new_id = GetSafeRandomId(iterator_map_); |
| 147 iterator_map_.insert(std::make_pair(new_id, iterator)); |
| 148 callback.Run(new_id); |
| 149 } |
| 150 |
| 151 void LevelDBDatabaseImpl::ReleaseIterator(uint64_t iterator_id) { |
| 152 auto it = iterator_map_.find(iterator_id); |
| 153 if (it != iterator_map_.end()) { |
| 154 delete it->second; |
| 155 iterator_map_.erase(it); |
| 156 } |
| 157 } |
| 158 |
| 159 void LevelDBDatabaseImpl::IteratorSeekToFirst( |
| 160 uint64_t iterator_id, |
| 161 const IteratorSeekToFirstCallback& callback) { |
| 162 auto it = iterator_map_.find(iterator_id); |
| 163 if (it == iterator_map_.end()) { |
| 164 callback.Run(false, DatabaseError::INVALID_ARGUMENT, nullptr, nullptr); |
| 165 return; |
| 166 } |
| 167 |
| 168 it->second->SeekToFirst(); |
| 169 |
| 170 ReplyToIteratorMessage(it->second, callback); |
| 171 } |
| 172 |
| 173 void LevelDBDatabaseImpl::IteratorSeekToLast( |
| 174 uint64_t iterator_id, |
| 175 const IteratorSeekToLastCallback& callback) { |
| 176 auto it = iterator_map_.find(iterator_id); |
| 177 if (it == iterator_map_.end()) { |
| 178 callback.Run(false, DatabaseError::INVALID_ARGUMENT, nullptr, nullptr); |
| 179 return; |
| 180 } |
| 181 |
| 182 it->second->SeekToLast(); |
| 183 |
| 184 ReplyToIteratorMessage(it->second, callback); |
| 185 } |
| 186 |
| 187 void LevelDBDatabaseImpl::IteratorSeek( |
| 188 uint64_t iterator_id, |
| 189 mojo::Array<uint8_t> target, |
| 190 const IteratorSeekToLastCallback& callback) { |
| 191 auto it = iterator_map_.find(iterator_id); |
| 192 if (it == iterator_map_.end()) { |
| 193 callback.Run(false, DatabaseError::INVALID_ARGUMENT, nullptr, nullptr); |
| 194 return; |
| 195 } |
| 196 |
| 197 it->second->Seek(GetSliceFor(target)); |
| 198 |
| 199 ReplyToIteratorMessage(it->second, callback); |
| 200 } |
| 201 |
| 202 void LevelDBDatabaseImpl::IteratorNext(uint64_t iterator_id, |
| 203 const IteratorNextCallback& callback) { |
| 204 auto it = iterator_map_.find(iterator_id); |
| 205 if (it == iterator_map_.end()) { |
| 206 callback.Run(false, DatabaseError::INVALID_ARGUMENT, nullptr, nullptr); |
| 207 return; |
| 208 } |
| 209 |
| 210 it->second->Next(); |
| 211 |
| 212 ReplyToIteratorMessage(it->second, callback); |
| 213 } |
| 214 |
| 215 void LevelDBDatabaseImpl::IteratorPrev(uint64_t iterator_id, |
| 216 const IteratorPrevCallback& callback) { |
| 217 auto it = iterator_map_.find(iterator_id); |
| 218 if (it == iterator_map_.end()) { |
| 219 callback.Run(false, DatabaseError::INVALID_ARGUMENT, nullptr, nullptr); |
| 220 return; |
| 221 } |
| 222 |
| 223 it->second->Prev(); |
| 224 |
| 225 ReplyToIteratorMessage(it->second, callback); |
| 226 } |
| 227 |
| 228 void LevelDBDatabaseImpl::ReplyToIteratorMessage( |
| 229 leveldb::Iterator* it, |
| 230 const IteratorSeekToFirstCallback& callback) { |
| 231 if (!it->Valid()) { |
| 232 callback.Run(false, LeveldbStatusToError(it->status()), nullptr, nullptr); |
| 233 return; |
| 234 } |
| 235 |
| 236 callback.Run(true, LeveldbStatusToError(it->status()), GetArrayFor(it->key()), |
| 237 GetArrayFor(it->value())); |
| 238 } |
| 239 |
| 125 } // namespace leveldb | 240 } // namespace leveldb |
| OLD | NEW |