OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/browser/indexed_db/leveldb/leveldb_database.h" | 5 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
6 | 6 |
7 #include <cerrno> | 7 #include <cerrno> |
8 #include <string> | |
9 | 8 |
10 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
14 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_piece.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/sys_info.h" | 17 #include "base/sys_info.h" |
18 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 18 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
19 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" | 19 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" |
20 #include "content/browser/indexed_db/leveldb/leveldb_slice.h" | |
21 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" | 20 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" |
22 #include "third_party/leveldatabase/env_chromium.h" | 21 #include "third_party/leveldatabase/env_chromium.h" |
23 #include "third_party/leveldatabase/env_idb.h" | 22 #include "third_party/leveldatabase/env_idb.h" |
24 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 23 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
25 #include "third_party/leveldatabase/src/include/leveldb/comparator.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/comparator.h" |
26 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
27 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
28 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | 27 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
29 | 28 |
| 29 using base::StringPiece; |
| 30 |
30 namespace content { | 31 namespace content { |
31 | 32 |
32 static leveldb::Slice MakeSlice(const std::vector<char>& value) { | 33 static leveldb::Slice MakeSlice(const StringPiece& s) { |
33 DCHECK_GT(value.size(), static_cast<size_t>(0)); | 34 return leveldb::Slice(s.begin(), s.size()); |
34 return leveldb::Slice(&*value.begin(), value.size()); | |
35 } | 35 } |
36 | 36 |
37 static leveldb::Slice MakeSlice(const LevelDBSlice& s) { | 37 static StringPiece MakeStringPiece(const leveldb::Slice& s) { |
38 return leveldb::Slice(s.begin(), s.end() - s.begin()); | 38 return StringPiece(s.data(), s.size()); |
39 } | |
40 | |
41 static LevelDBSlice MakeLevelDBSlice(const leveldb::Slice& s) { | |
42 return LevelDBSlice(s.data(), s.data() + s.size()); | |
43 } | 39 } |
44 | 40 |
45 class ComparatorAdapter : public leveldb::Comparator { | 41 class ComparatorAdapter : public leveldb::Comparator { |
46 public: | 42 public: |
47 explicit ComparatorAdapter(const LevelDBComparator* comparator) | 43 explicit ComparatorAdapter(const LevelDBComparator* comparator) |
48 : comparator_(comparator) {} | 44 : comparator_(comparator) {} |
49 | 45 |
50 virtual int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const | 46 virtual int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const |
51 OVERRIDE { | 47 OVERRIDE { |
52 return comparator_->Compare(MakeLevelDBSlice(a), MakeLevelDBSlice(b)); | 48 return comparator_->Compare(MakeStringPiece(a), MakeStringPiece(b)); |
53 } | 49 } |
54 | 50 |
55 virtual const char* Name() const OVERRIDE { return comparator_->Name(); } | 51 virtual const char* Name() const OVERRIDE { return comparator_->Name(); } |
56 | 52 |
57 // TODO(jsbell): Support the methods below in the future. | 53 // TODO(jsbell): Support the methods below in the future. |
58 virtual void FindShortestSeparator(std::string* start, | 54 virtual void FindShortestSeparator(std::string* start, |
59 const leveldb::Slice& limit) const | 55 const leveldb::Slice& limit) const |
60 OVERRIDE {} | 56 OVERRIDE {} |
61 virtual void FindShortSuccessor(std::string* key) const OVERRIDE {} | 57 virtual void FindShortSuccessor(std::string* key) const OVERRIDE {} |
62 | 58 |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 319 |
324 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); | 320 scoped_ptr<LevelDBDatabase> result(new LevelDBDatabase); |
325 result->env_ = in_memory_env.Pass(); | 321 result->env_ = in_memory_env.Pass(); |
326 result->db_ = make_scoped_ptr(db); | 322 result->db_ = make_scoped_ptr(db); |
327 result->comparator_adapter_ = comparator_adapter.Pass(); | 323 result->comparator_adapter_ = comparator_adapter.Pass(); |
328 result->comparator_ = comparator; | 324 result->comparator_ = comparator; |
329 | 325 |
330 return result.Pass(); | 326 return result.Pass(); |
331 } | 327 } |
332 | 328 |
333 bool LevelDBDatabase::Put(const LevelDBSlice& key, | 329 bool LevelDBDatabase::Put(const StringPiece& key, std::string* value) { |
334 std::vector<char>* value) { | |
335 leveldb::WriteOptions write_options; | 330 leveldb::WriteOptions write_options; |
336 write_options.sync = true; | 331 write_options.sync = true; |
337 | 332 |
338 const leveldb::Status s = | 333 const leveldb::Status s = |
339 db_->Put(write_options, MakeSlice(key), MakeSlice(*value)); | 334 db_->Put(write_options, MakeSlice(key), MakeSlice(*value)); |
340 if (s.ok()) | 335 if (s.ok()) |
341 return true; | 336 return true; |
342 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); | 337 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); |
343 return false; | 338 return false; |
344 } | 339 } |
345 | 340 |
346 bool LevelDBDatabase::Remove(const LevelDBSlice& key) { | 341 bool LevelDBDatabase::Remove(const StringPiece& key) { |
347 leveldb::WriteOptions write_options; | 342 leveldb::WriteOptions write_options; |
348 write_options.sync = true; | 343 write_options.sync = true; |
349 | 344 |
350 const leveldb::Status s = db_->Delete(write_options, MakeSlice(key)); | 345 const leveldb::Status s = db_->Delete(write_options, MakeSlice(key)); |
351 if (s.ok()) | 346 if (s.ok()) |
352 return true; | 347 return true; |
353 if (s.IsNotFound()) | 348 if (s.IsNotFound()) |
354 return false; | 349 return false; |
355 LOG(ERROR) << "LevelDB remove failed: " << s.ToString(); | 350 LOG(ERROR) << "LevelDB remove failed: " << s.ToString(); |
356 return false; | 351 return false; |
357 } | 352 } |
358 | 353 |
359 bool LevelDBDatabase::Get(const LevelDBSlice& key, | 354 bool LevelDBDatabase::Get(const StringPiece& key, |
360 std::string* value, | 355 std::string* value, |
361 bool* found, | 356 bool* found, |
362 const LevelDBSnapshot* snapshot) { | 357 const LevelDBSnapshot* snapshot) { |
363 *found = false; | 358 *found = false; |
364 leveldb::ReadOptions read_options; | 359 leveldb::ReadOptions read_options; |
365 read_options.verify_checksums = true; // TODO(jsbell): Disable this if the | 360 read_options.verify_checksums = true; // TODO(jsbell): Disable this if the |
366 // performance impact is too great. | 361 // performance impact is too great. |
367 read_options.snapshot = snapshot ? snapshot->snapshot_ : 0; | 362 read_options.snapshot = snapshot ? snapshot->snapshot_ : 0; |
368 | 363 |
369 const leveldb::Status s = db_->Get(read_options, MakeSlice(key), value); | 364 const leveldb::Status s = db_->Get(read_options, MakeSlice(key), value); |
(...skipping 20 matching lines...) Expand all Loading... |
390 return false; | 385 return false; |
391 } | 386 } |
392 | 387 |
393 namespace { | 388 namespace { |
394 class IteratorImpl : public LevelDBIterator { | 389 class IteratorImpl : public LevelDBIterator { |
395 public: | 390 public: |
396 virtual ~IteratorImpl() {} | 391 virtual ~IteratorImpl() {} |
397 | 392 |
398 virtual bool IsValid() const OVERRIDE; | 393 virtual bool IsValid() const OVERRIDE; |
399 virtual void SeekToLast() OVERRIDE; | 394 virtual void SeekToLast() OVERRIDE; |
400 virtual void Seek(const LevelDBSlice& target) OVERRIDE; | 395 virtual void Seek(const StringPiece& target) OVERRIDE; |
401 virtual void Next() OVERRIDE; | 396 virtual void Next() OVERRIDE; |
402 virtual void Prev() OVERRIDE; | 397 virtual void Prev() OVERRIDE; |
403 virtual LevelDBSlice Key() const OVERRIDE; | 398 virtual StringPiece Key() const OVERRIDE; |
404 virtual LevelDBSlice Value() const OVERRIDE; | 399 virtual StringPiece Value() const OVERRIDE; |
405 | 400 |
406 private: | 401 private: |
407 friend class content::LevelDBDatabase; | 402 friend class content::LevelDBDatabase; |
408 explicit IteratorImpl(scoped_ptr<leveldb::Iterator> iterator); | 403 explicit IteratorImpl(scoped_ptr<leveldb::Iterator> iterator); |
409 void CheckStatus(); | 404 void CheckStatus(); |
410 | 405 |
411 scoped_ptr<leveldb::Iterator> iterator_; | 406 scoped_ptr<leveldb::Iterator> iterator_; |
412 }; | 407 }; |
413 } | 408 } |
414 | 409 |
415 IteratorImpl::IteratorImpl(scoped_ptr<leveldb::Iterator> it) | 410 IteratorImpl::IteratorImpl(scoped_ptr<leveldb::Iterator> it) |
416 : iterator_(it.Pass()) {} | 411 : iterator_(it.Pass()) {} |
417 | 412 |
418 void IteratorImpl::CheckStatus() { | 413 void IteratorImpl::CheckStatus() { |
419 const leveldb::Status s = iterator_->status(); | 414 const leveldb::Status s = iterator_->status(); |
420 if (!s.ok()) | 415 if (!s.ok()) |
421 LOG(ERROR) << "LevelDB iterator error: " << s.ToString(); | 416 LOG(ERROR) << "LevelDB iterator error: " << s.ToString(); |
422 } | 417 } |
423 | 418 |
424 bool IteratorImpl::IsValid() const { return iterator_->Valid(); } | 419 bool IteratorImpl::IsValid() const { return iterator_->Valid(); } |
425 | 420 |
426 void IteratorImpl::SeekToLast() { | 421 void IteratorImpl::SeekToLast() { |
427 iterator_->SeekToLast(); | 422 iterator_->SeekToLast(); |
428 CheckStatus(); | 423 CheckStatus(); |
429 } | 424 } |
430 | 425 |
431 void IteratorImpl::Seek(const LevelDBSlice& target) { | 426 void IteratorImpl::Seek(const StringPiece& target) { |
432 iterator_->Seek(MakeSlice(target)); | 427 iterator_->Seek(MakeSlice(target)); |
433 CheckStatus(); | 428 CheckStatus(); |
434 } | 429 } |
435 | 430 |
436 void IteratorImpl::Next() { | 431 void IteratorImpl::Next() { |
437 DCHECK(IsValid()); | 432 DCHECK(IsValid()); |
438 iterator_->Next(); | 433 iterator_->Next(); |
439 CheckStatus(); | 434 CheckStatus(); |
440 } | 435 } |
441 | 436 |
442 void IteratorImpl::Prev() { | 437 void IteratorImpl::Prev() { |
443 DCHECK(IsValid()); | 438 DCHECK(IsValid()); |
444 iterator_->Prev(); | 439 iterator_->Prev(); |
445 CheckStatus(); | 440 CheckStatus(); |
446 } | 441 } |
447 | 442 |
448 LevelDBSlice IteratorImpl::Key() const { | 443 StringPiece IteratorImpl::Key() const { |
449 DCHECK(IsValid()); | 444 DCHECK(IsValid()); |
450 return MakeLevelDBSlice(iterator_->key()); | 445 return MakeStringPiece(iterator_->key()); |
451 } | 446 } |
452 | 447 |
453 LevelDBSlice IteratorImpl::Value() const { | 448 StringPiece IteratorImpl::Value() const { |
454 DCHECK(IsValid()); | 449 DCHECK(IsValid()); |
455 return MakeLevelDBSlice(iterator_->value()); | 450 return MakeStringPiece(iterator_->value()); |
456 } | 451 } |
457 | 452 |
458 scoped_ptr<LevelDBIterator> LevelDBDatabase::CreateIterator( | 453 scoped_ptr<LevelDBIterator> LevelDBDatabase::CreateIterator( |
459 const LevelDBSnapshot* snapshot) { | 454 const LevelDBSnapshot* snapshot) { |
460 leveldb::ReadOptions read_options; | 455 leveldb::ReadOptions read_options; |
461 read_options.verify_checksums = true; // TODO(jsbell): Disable this if the | 456 read_options.verify_checksums = true; // TODO(jsbell): Disable this if the |
462 // performance impact is too great. | 457 // performance impact is too great. |
463 read_options.snapshot = snapshot ? snapshot->snapshot_ : 0; | 458 read_options.snapshot = snapshot ? snapshot->snapshot_ : 0; |
464 | 459 |
465 scoped_ptr<leveldb::Iterator> i(db_->NewIterator(read_options)); | 460 scoped_ptr<leveldb::Iterator> i(db_->NewIterator(read_options)); |
466 return scoped_ptr<LevelDBIterator>(new IteratorImpl(i.Pass())); | 461 return scoped_ptr<LevelDBIterator>(new IteratorImpl(i.Pass())); |
467 } | 462 } |
468 | 463 |
469 const LevelDBComparator* LevelDBDatabase::Comparator() const { | 464 const LevelDBComparator* LevelDBDatabase::Comparator() const { |
470 return comparator_; | 465 return comparator_; |
471 } | 466 } |
472 | 467 |
473 } // namespace content | 468 } // namespace content |
OLD | NEW |