OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "services/url_response_disk_cache/url_response_disk_cache_db.h" | 5 #include "services/url_response_disk_cache/url_response_disk_cache_db.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 return version; | 161 return version; |
162 } | 162 } |
163 | 163 |
164 void URLResponseDiskCacheDB::SetVersion(uint64_t version) { | 164 void URLResponseDiskCacheDB::SetVersion(uint64_t version) { |
165 leveldb::Status status = db_->Put( | 165 leveldb::Status status = db_->Put( |
166 leveldb::WriteOptions(), kVersionKey, | 166 leveldb::WriteOptions(), kVersionKey, |
167 leveldb::Slice(reinterpret_cast<char*>(&version), sizeof(version))); | 167 leveldb::Slice(reinterpret_cast<char*>(&version), sizeof(version))); |
168 DCHECK(status.ok()); | 168 DCHECK(status.ok()); |
169 } | 169 } |
170 | 170 |
171 void URLResponseDiskCacheDB::PutNew(const std::string& request_origin, | 171 void URLResponseDiskCacheDB::Put(CacheKeyPtr key, CacheEntryPtr entry) { |
172 const std::string& url, | |
173 CacheEntryPtr entry) { | |
174 CacheKeyPtr key = CacheKey::New(); | |
175 key->request_origin = request_origin; | |
176 key->url = url; | |
177 key->timestamp = base::Time::Now().ToInternalValue(); | |
178 std::string key_string; | 172 std::string key_string; |
179 Serialize(key.Pass(), &key_string); | 173 Serialize(key.Pass(), &key_string); |
180 std::string entry_string; | 174 std::string entry_string; |
181 Serialize(entry.Pass(), &entry_string); | 175 Serialize(entry.Pass(), &entry_string); |
182 leveldb::Status s = | 176 leveldb::Status s = |
183 db_->Put(leveldb::WriteOptions(), key_string, entry_string); | 177 db_->Put(leveldb::WriteOptions(), key_string, entry_string); |
184 DCHECK(s.ok()); | 178 DCHECK(s.ok()); |
185 } | 179 } |
186 | 180 |
| 181 CacheEntryPtr URLResponseDiskCacheDB::Get(CacheKeyPtr key) { |
| 182 std::string key_string; |
| 183 std::string entry_string; |
| 184 Serialize(key.Pass(), &key_string); |
| 185 leveldb::Status status = |
| 186 db_->Get(leveldb::ReadOptions(), key_string, &entry_string); |
| 187 if (status.IsNotFound()) |
| 188 return CacheEntryPtr(); |
| 189 DCHECK(status.ok()); |
| 190 CacheEntryPtr result; |
| 191 Deserialize(entry_string, &result); |
| 192 return result.Pass(); |
| 193 } |
| 194 |
| 195 void URLResponseDiskCacheDB::PutNew(const std::string& request_origin, |
| 196 const std::string& url, |
| 197 CacheEntryPtr entry) { |
| 198 CacheKeyPtr key = CacheKey::New(); |
| 199 key->request_origin = request_origin; |
| 200 key->url = url; |
| 201 key->timestamp = base::Time::Now().ToInternalValue(); |
| 202 Put(key.Pass(), entry.Pass()); |
| 203 } |
| 204 |
187 CacheEntryPtr URLResponseDiskCacheDB::GetNewest( | 205 CacheEntryPtr URLResponseDiskCacheDB::GetNewest( |
188 const std::string& request_origin, | 206 const std::string& request_origin, |
189 const std::string& url) { | 207 const std::string& url, |
| 208 CacheKeyPtr* output_key) { |
190 CacheKeyPtr key = CacheKey::New(); | 209 CacheKeyPtr key = CacheKey::New(); |
191 key->request_origin = request_origin; | 210 key->request_origin = request_origin; |
192 key->url = url; | 211 key->url = url; |
193 key->timestamp = std::numeric_limits<int64>::max(); | 212 key->timestamp = std::numeric_limits<int64>::max(); |
194 std::string key_string; | 213 std::string key_string; |
195 Serialize(key.Pass(), &key_string); | 214 Serialize(key.Pass(), &key_string); |
196 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); | 215 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); |
197 it->Seek(key_string); | 216 it->Seek(key_string); |
198 CacheEntryPtr result; | 217 CacheEntryPtr result; |
199 if (it->Valid()) { | 218 if (it->Valid()) { |
200 Deserialize(it->key(), &key); | 219 Deserialize(it->key(), &key); |
201 if (key->request_origin == request_origin && key->url == url) { | 220 if (key->request_origin == request_origin && key->url == url) { |
202 Deserialize(it->value(), &result); | 221 Deserialize(it->value(), &result); |
| 222 if (output_key) { |
| 223 *output_key = key.Pass(); |
| 224 } |
203 } | 225 } |
204 } | 226 } |
205 return result.Pass(); | 227 return result.Pass(); |
206 } | 228 } |
207 | 229 |
208 void URLResponseDiskCacheDB::Delete(CacheKeyPtr key) { | 230 void URLResponseDiskCacheDB::Delete(CacheKeyPtr key) { |
209 std::string key_string; | 231 std::string key_string; |
210 Serialize(key.Pass(), &key_string); | 232 Serialize(key.Pass(), &key_string); |
211 leveldb::Status s = db_->Delete(leveldb::WriteOptions(), key_string); | 233 leveldb::Status s = db_->Delete(leveldb::WriteOptions(), key_string); |
212 DCHECK(s.ok()); | 234 DCHECK(s.ok()); |
213 } | 235 } |
214 | 236 |
215 scoped_ptr<URLResponseDiskCacheDB::Iterator> | 237 scoped_ptr<URLResponseDiskCacheDB::Iterator> |
216 URLResponseDiskCacheDB::GetIterator() { | 238 URLResponseDiskCacheDB::GetIterator() { |
217 return make_scoped_ptr(new Iterator(db_)); | 239 return make_scoped_ptr(new Iterator(db_)); |
218 } | 240 } |
219 | 241 |
220 URLResponseDiskCacheDB::~URLResponseDiskCacheDB() {} | 242 URLResponseDiskCacheDB::~URLResponseDiskCacheDB() {} |
221 | 243 |
222 } // namespace mojo | 244 } // namespace mojo |
OLD | NEW |