leveldb_iterator_impl.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/logging.h"
6#include "base/memory/scoped_ptr.h"
7#include "content/browser/indexed_db/leveldb/leveldb_iterator_impl.h"
8
9static leveldb::Slice MakeSlice(const base::StringPiece& s) {
10  return leveldb::Slice(s.begin(), s.size());
11}
12
13static base::StringPiece MakeStringPiece(const leveldb::Slice& s) {
14  return base::StringPiece(s.data(), s.size());
15}
16
17namespace content {
18
19LevelDBIteratorImpl::~LevelDBIteratorImpl() {
20}
21
22LevelDBIteratorImpl::LevelDBIteratorImpl(scoped_ptr<leveldb::Iterator> it)
23    : iterator_(it.Pass()) {
24}
25
26void LevelDBIteratorImpl::CheckStatus() {
27  const leveldb::Status& s = iterator_->status();
28  if (!s.ok())
29    LOG(ERROR) << "LevelDB iterator error: " << s.ToString();
30}
31
32bool LevelDBIteratorImpl::IsValid() const {
33  return iterator_->Valid();
34}
35
36leveldb::Status LevelDBIteratorImpl::SeekToLast() {
37  iterator_->SeekToLast();
38  CheckStatus();
39  return iterator_->status();
40}
41
42leveldb::Status LevelDBIteratorImpl::Seek(const base::StringPiece& target) {
43  iterator_->Seek(MakeSlice(target));
44  CheckStatus();
45  return iterator_->status();
46}
47
48leveldb::Status LevelDBIteratorImpl::Next() {
49  DCHECK(IsValid());
50  iterator_->Next();
51  CheckStatus();
52  return iterator_->status();
53}
54
55leveldb::Status LevelDBIteratorImpl::Prev() {
56  DCHECK(IsValid());
57  iterator_->Prev();
58  CheckStatus();
59  return iterator_->status();
60}
61
62base::StringPiece LevelDBIteratorImpl::Key() const {
63  DCHECK(IsValid());
64  return MakeStringPiece(iterator_->key());
65}
66
67base::StringPiece LevelDBIteratorImpl::Value() const {
68  DCHECK(IsValid());
69  return MakeStringPiece(iterator_->value());
70}
71
72}  // namespace content
73