table/two_level_iterator.cc

filter: Statementstmt 66.6% 60 / 90 Branchbran 39.7% 35 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 53.3% 95 / 178 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« util/env_posix.cc ↑ Summary db/builder.cc »
1 // Copyright (c) 2011 The LevelDB 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. See the AUTHORS file for names of contributors.
4  
5 #include "table/two_level_iterator.h"
6  
7 #include "leveldb/table.h"
8 #include "table/block.h"
9 #include "table/format.h"
10 #include "table/iterator_wrapper.h"
11  
12 namespace leveldb {
13  
14 namespace {
15  
16 typedef Iterator* (*BlockFunction)(void*, const ReadOptions&, const Slice&);
17  
18 class TwoLevelIterator: public Iterator {
19 public:
20 TwoLevelIterator(
21 Iterator* index_iter,
22 BlockFunction block_function,
23 void* arg,
24 const ReadOptions& options);
25  
26 virtual ~TwoLevelIterator();
27  
28 virtual void Seek(const Slice& target);
29 virtual void SeekToFirst();
30 virtual void SeekToLast();
31 virtual void Next();
32 virtual void Prev();
33  
34 41 virtual bool Valid() const {
35 41 return data_iter_.Valid();
36 }
37 13 virtual Slice key() const {
38 13 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 13 13
39 13 return data_iter_.key();
40 }
41 0 virtual Slice value() const {
42 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
43 0 return data_iter_.value();
44 }
45 1 virtual Status status() const {
46 // It'd be nice if status() returned a const Status& instead of a Status
47 1 ▶ if (!index_iter_.status().ok()) {
Branch: 2 branches
BranchTrueFalseTotal
if (!index_iter_.status().ok()) { 1 0 1
if (!index_iter_.status().ok()) { 0 1 1
48 0 return index_iter_.status();
49 1 ▶ } else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) {
Branch: 6 branches
BranchTrueFalseTotal
} else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) { 0 1 1
} else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) { 0 0 0
} else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) { 0 0 0
} else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) { 0 1 1
} else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) { 0 1 1
} else if (data_iter_.iter() != NULL && !data_iter_.status().ok()) { 0 0 0
50 0 return data_iter_.status();
51 } else {
52 1 return status_;
53 }
54 }
55  
56 private:
57 1 void SaveError(const Status& s) {
58 1 ▶ if (status_.ok() && !s.ok()) status_ = s;
Branch: 3 branches
BranchTrueFalseTotal
if (status_.ok() && !s.ok()) status_ = s; 1 0 1
if (status_.ok() && !s.ok()) status_ = s; 0 1 1
if (status_.ok() && !s.ok()) status_ = s; 0 1 1
59 1 }
60 void SkipEmptyDataBlocksForward();
61 void SkipEmptyDataBlocksBackward();
62 void SetDataIterator(Iterator* data_iter);
63 void InitDataBlock();
64  
65 BlockFunction block_function_;
66 void* arg_;
67 const ReadOptions options_;
68 Status status_;
69 IteratorWrapper index_iter_;
70 IteratorWrapper data_iter_; // May be NULL
71 // If data_iter_ is non-NULL, then "data_block_handle_" holds the
72 // "index_value" passed to block_function_ to create the data_iter_.
73 std::string data_block_handle_;
74 };
75  
76 2 TwoLevelIterator::TwoLevelIterator(
77 Iterator* index_iter,
78 BlockFunction block_function,
79 void* arg,
80 2 const ReadOptions& options)
81 2 : block_function_(block_function),
82 2 arg_(arg),
83 2 options_(options),
84 2 ▶ index_iter_(index_iter),
Branch: 1 branch
BranchTrueFalseTotal
index_iter_(index_iter), 2 0 2
85 4 ▶ data_iter_(NULL) {
Branch: 1 branch
BranchTrueFalseTotal
data_iter_(NULL) { 2 0 2
86 2 }
87  
88 4 TwoLevelIterator::~TwoLevelIterator() {
89 4 }
90  
91 0 void TwoLevelIterator::Seek(const Slice& target) {
92 0 index_iter_.Seek(target);
93 0 InitDataBlock();
94 0 ▶ if (data_iter_.iter() != NULL) data_iter_.Seek(target);
Branch: 1 branch
BranchTrueFalseTotal
if (data_iter_.iter() != NULL) data_iter_.Seek(target); 0 0 0
95 0 SkipEmptyDataBlocksForward();
96 0 }
97  
98 1 void TwoLevelIterator::SeekToFirst() {
99 1 index_iter_.SeekToFirst();
100 1 InitDataBlock();
101 1 ▶ if (data_iter_.iter() != NULL) data_iter_.SeekToFirst();
Branch: 1 branch
BranchTrueFalseTotal
if (data_iter_.iter() != NULL) data_iter_.SeekToFirst(); 1 0 1
102 1 SkipEmptyDataBlocksForward();
103 1 }
104  
105 0 void TwoLevelIterator::SeekToLast() {
106 0 index_iter_.SeekToLast();
107 0 InitDataBlock();
108 0 ▶ if (data_iter_.iter() != NULL) data_iter_.SeekToLast();
Branch: 1 branch
BranchTrueFalseTotal
if (data_iter_.iter() != NULL) data_iter_.SeekToLast(); 0 0 0
109 0 SkipEmptyDataBlocksBackward();
110 0 }
111  
112 13 void TwoLevelIterator::Next() {
113 13 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 13 13
114 13 data_iter_.Next();
115 13 SkipEmptyDataBlocksForward();
116 13 }
117  
118 0 void TwoLevelIterator::Prev() {
119 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
120 0 data_iter_.Prev();
121 0 SkipEmptyDataBlocksBackward();
122 0 }
123  
124  
125 14 void TwoLevelIterator::SkipEmptyDataBlocksForward() {
126 29 ▶ while (data_iter_.iter() == NULL || !data_iter_.Valid()) {
Branch: 3 branches
BranchTrueFalseTotal
while (data_iter_.iter() == NULL || !data_iter_.Valid()) { 14 1 15
while (data_iter_.iter() == NULL || !data_iter_.Valid()) { 1 13 14
while (data_iter_.iter() == NULL || !data_iter_.Valid()) { 2 13 15
127 // Move to next block
128 2 ▶ if (!index_iter_.Valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!index_iter_.Valid()) { 1 1 2
129 1 SetDataIterator(NULL);
130 1 return;
131 }
132 1 index_iter_.Next();
133 1 InitDataBlock();
134 1 ▶ if (data_iter_.iter() != NULL) data_iter_.SeekToFirst();
Branch: 1 branch
BranchTrueFalseTotal
if (data_iter_.iter() != NULL) data_iter_.SeekToFirst(); 1 0 1
135 }
136 }
137  
138 0 void TwoLevelIterator::SkipEmptyDataBlocksBackward() {
139 0 ▶ while (data_iter_.iter() == NULL || !data_iter_.Valid()) {
Branch: 3 branches
BranchTrueFalseTotal
while (data_iter_.iter() == NULL || !data_iter_.Valid()) { 0 0 0
while (data_iter_.iter() == NULL || !data_iter_.Valid()) { 0 0 0
while (data_iter_.iter() == NULL || !data_iter_.Valid()) { 0 0 0
140 // Move to next block
141 0 ▶ if (!index_iter_.Valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!index_iter_.Valid()) { 0 0 0
142 0 SetDataIterator(NULL);
143 0 return;
144 }
145 0 index_iter_.Prev();
146 0 InitDataBlock();
147 0 ▶ if (data_iter_.iter() != NULL) data_iter_.SeekToLast();
Branch: 1 branch
BranchTrueFalseTotal
if (data_iter_.iter() != NULL) data_iter_.SeekToLast(); 0 0 0
148 }
149 }
150  
151 3 void TwoLevelIterator::SetDataIterator(Iterator* data_iter) {
152 3 ▶ if (data_iter_.iter() != NULL) SaveError(data_iter_.status());
Branch: 3 branches
BranchTrueFalseTotal
if (data_iter_.iter() != NULL) SaveError(data_iter_.status()); 1 2 3
if (data_iter_.iter() != NULL) SaveError(data_iter_.status()); 1 0 1
if (data_iter_.iter() != NULL) SaveError(data_iter_.status()); 1 0 1
153 3 data_iter_.Set(data_iter);
154 3 }
155  
156 2 void TwoLevelIterator::InitDataBlock() {
157 2 ▶ if (!index_iter_.Valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!index_iter_.Valid()) { 1 1 2
158 1 SetDataIterator(NULL);
159 } else {
160 1 ▶ Slice handle = index_iter_.value();
Branch: 1 branch
BranchTrueFalseTotal
Slice handle = index_iter_.value(); 1 0 1
161 1 ▶ if (data_iter_.iter() != NULL && handle.compare(data_block_handle_) == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (data_iter_.iter() != NULL && handle.compare(data_block_handle_) == 0) { 0 1 1
if (data_iter_.iter() != NULL && handle.compare(data_block_handle_) == 0) { 0 0 0
if (data_iter_.iter() != NULL && handle.compare(data_block_handle_) == 0) { 0 0 0
if (data_iter_.iter() != NULL && handle.compare(data_block_handle_) == 0) { 1 0 1
162 // data_iter_ is already constructed with this iterator, so
163 // no need to change anything
164 } else {
165 1 ▶ Iterator* iter = (*block_function_)(arg_, options_, handle);
Branch: 1 branch
BranchTrueFalseTotal
Iterator* iter = (*block_function_)(arg_, options_, handle); 1 0 1
166 1 ▶ data_block_handle_.assign(handle.data(), handle.size());
Branch: 1 branch
BranchTrueFalseTotal
data_block_handle_.assign(handle.data(), handle.size()); 1 0 1
167 1 ▶ SetDataIterator(iter);
Branch: 1 branch
BranchTrueFalseTotal
SetDataIterator(iter); 1 0 1
168 }
169 }
170 2 }
171  
172 }
173  
174 2 Iterator* NewTwoLevelIterator(
175 Iterator* index_iter,
176 BlockFunction block_function,
177 void* arg,
178 const ReadOptions& options) {
179 2 ▶ return new TwoLevelIterator(index_iter, block_function, arg, options);
Branch: 2 branches
BranchTrueFalseTotal
return new TwoLevelIterator(index_iter, block_function, arg, options); 2 0 2
return new TwoLevelIterator(index_iter, block_function, arg, options); 0 0 0
180 }
181  
182 }
« util/env_posix.cc ↑ Summary db/builder.cc »