table/merger.cc

filter: Statementstmt 47.6% 51 / 107 Branchbran 29.5% 29 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 39.0% 80 / 205 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« util/comparator.cc ↑ Summary table/iterator.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/merger.h"
6  
7 #include "leveldb/comparator.h"
8 #include "leveldb/iterator.h"
9 #include "table/iterator_wrapper.h"
10  
11 namespace leveldb {
12  
13 namespace {
14 class MergingIterator : public Iterator {
15 public:
16 1 MergingIterator(const Comparator* comparator, Iterator** children, int n)
17 2 : comparator_(comparator),
18 3 ▶ children_(new IteratorWrapper[n]),
Branch: 2 branches
BranchTrueFalseTotal
children_(new IteratorWrapper[n]), 1 0 1
children_(new IteratorWrapper[n]), 2 1 3
19 1 n_(n),
20 1 current_(NULL),
21 1 ▶ direction_(kForward) {
Branch: 1 branch
BranchTrueFalseTotal
direction_(kForward) { 1 0 1
22 3 ▶ for (int i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n; i++) { 2 1 3
23 2 ▶ children_[i].Set(children[i]);
Branch: 1 branch
BranchTrueFalseTotal
children_[i].Set(children[i]); 2 0 2
24 }
25 1 }
26  
27 2 virtual ~MergingIterator() {
28 3 ▶ delete[] children_;
Branch: 2 branches
BranchTrueFalseTotal
delete[] children_; 1 0 1
delete[] children_; 2 1 3
29 2 }
30  
31 58 virtual bool Valid() const {
32 58 return (current_ != NULL);
33 }
34  
35 1 virtual void SeekToFirst() {
36 3 ▶ for (int i = 0; i < n_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n_; i++) { 2 1 3
37 2 children_[i].SeekToFirst();
38 }
39 1 FindSmallest();
40 1 direction_ = kForward;
41 1 }
42  
43 0 virtual void SeekToLast() {
44 0 ▶ for (int i = 0; i < n_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n_; i++) { 0 0 0
45 0 children_[i].SeekToLast();
46 }
47 0 FindLargest();
48 0 direction_ = kReverse;
49 0 }
50  
51 0 virtual void Seek(const Slice& target) {
52 0 ▶ for (int i = 0; i < n_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n_; i++) { 0 0 0
53 0 children_[i].Seek(target);
54 }
55 0 FindSmallest();
56 0 direction_ = kForward;
57 0 }
58  
59 16 virtual void Next() {
60 16 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 16 16
61  
62 // Ensure that all children are positioned after key().
63 // If we are moving in the forward direction, it is already
64 // true for all of the non-current_ children since current_ is
65 // the smallest child and key() == current_->key(). Otherwise,
66 // we explicitly position the non-current_ children.
67 16 ▶ if (direction_ != kForward) {
Branch: 1 branch
BranchTrueFalseTotal
if (direction_ != kForward) { 0 16 16
68 0 ▶ for (int i = 0; i < n_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n_; i++) { 0 0 0
69 0 IteratorWrapper* child = &children_[i];
70 0 ▶ if (child != current_) {
Branch: 1 branch
BranchTrueFalseTotal
if (child != current_) { 0 0 0
71 0 ▶ child->Seek(key());
Branch: 2 branches
BranchTrueFalseTotal
child->Seek(key()); 0 0 0
child->Seek(key()); 0 0 0
72 0 ▶ if (child->Valid() &&
Branch: 1 branch
BranchTrueFalseTotal
if (child->Valid() && 0 0 0
73 0 ▶ comparator_->Compare(key(), child->key()) == 0) {
Branch: 4 branches
BranchTrueFalseTotal
comparator_->Compare(key(), child->key()) == 0) { 0 0 0
comparator_->Compare(key(), child->key()) == 0) { 0 0 0
comparator_->Compare(key(), child->key()) == 0) { 0 0 0
comparator_->Compare(key(), child->key()) == 0) { 0 0 0
74 0 child->Next();
75 }
76 }
77 }
78 0 direction_ = kForward;
79 }
80  
81 16 current_->Next();
82 16 FindSmallest();
83 16 }
84  
85 0 virtual void Prev() {
86 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
87  
88 // Ensure that all children are positioned before key().
89 // If we are moving in the reverse direction, it is already
90 // true for all of the non-current_ children since current_ is
91 // the largest child and key() == current_->key(). Otherwise,
92 // we explicitly position the non-current_ children.
93 0 ▶ if (direction_ != kReverse) {
Branch: 1 branch
BranchTrueFalseTotal
if (direction_ != kReverse) { 0 0 0
94 0 ▶ for (int i = 0; i < n_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n_; i++) { 0 0 0
95 0 IteratorWrapper* child = &children_[i];
96 0 ▶ if (child != current_) {
Branch: 1 branch
BranchTrueFalseTotal
if (child != current_) { 0 0 0
97 0 ▶ child->Seek(key());
Branch: 2 branches
BranchTrueFalseTotal
child->Seek(key()); 0 0 0
child->Seek(key()); 0 0 0
98 0 ▶ if (child->Valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (child->Valid()) { 0 0 0
99 // Child is at first entry >= key(). Step back one to be < key()
100 0 child->Prev();
101 } else {
102 // Child has no entries >= key(). Position at last entry.
103 0 child->SeekToLast();
104 }
105 }
106 }
107 0 direction_ = kReverse;
108 }
109  
110 0 current_->Prev();
111 0 FindLargest();
112 0 }
113  
114 22 virtual Slice key() const {
115 22 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 22 22
116 22 return current_->key();
117 }
118  
119 0 virtual Slice value() const {
120 0 ▶ assert(Valid());
Branch: 1 branch
BranchTrueFalseTotal
assert(Valid()); 0 0 0
121 0 return current_->value();
122 }
123  
124 0 virtual Status status() const {
125 0 Status status;
126 0 ▶ for (int i = 0; i < n_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n_; i++) { 0 0 0
127 0 ▶ status = children_[i].status();
Branch: 2 branches
BranchTrueFalseTotal
status = children_[i].status(); 0 0 0
status = children_[i].status(); 0 0 0
128 0 ▶ if (!status.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!status.ok()) { 0 0 0
129 0 break;
130 }
131 }
132 0 return status;
133 }
134  
135 private:
136 void FindSmallest();
137 void FindLargest();
138  
139 // We might want to use a heap in case there are lots of children.
140 // For now we use a simple array since we expect a very small number
141 // of children in leveldb.
142 const Comparator* comparator_;
143 IteratorWrapper* children_;
144 int n_;
145 IteratorWrapper* current_;
146  
147 // Which direction is the iterator moving?
148 enum Direction {
149 kForward,
150 kReverse
151 };
152 Direction direction_;
153 };
154  
155 17 void MergingIterator::FindSmallest() {
156 17 IteratorWrapper* smallest = NULL;
157 51 ▶ for (int i = 0; i < n_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n_; i++) { 34 17 51
158 34 IteratorWrapper* child = &children_[i];
159 34 ▶ if (child->Valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (child->Valid()) { 23 11 34
160 23 ▶ if (smallest == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (smallest == NULL) { 16 7 23
161 16 smallest = child;
162 7 ▶ } else if (comparator_->Compare(child->key(), smallest->key()) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (comparator_->Compare(child->key(), smallest->key()) < 0) { 7 0 7
} else if (comparator_->Compare(child->key(), smallest->key()) < 0) { 4 3 7
163 4 smallest = child;
164 }
165 }
166 }
167 17 current_ = smallest;
168 17 }
169  
170 0 void MergingIterator::FindLargest() {
171 0 IteratorWrapper* largest = NULL;
172 0 ▶ for (int i = n_-1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = n_-1; i >= 0; i--) { 0 0 0
173 0 IteratorWrapper* child = &children_[i];
174 0 ▶ if (child->Valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (child->Valid()) { 0 0 0
175 0 ▶ if (largest == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (largest == NULL) { 0 0 0
176 0 largest = child;
177 0 ▶ } else if (comparator_->Compare(child->key(), largest->key()) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (comparator_->Compare(child->key(), largest->key()) > 0) { 0 0 0
} else if (comparator_->Compare(child->key(), largest->key()) > 0) { 0 0 0
178 0 largest = child;
179 }
180 }
181 }
182 0 current_ = largest;
183 0 }
184 }
185  
186 14 Iterator* NewMergingIterator(const Comparator* cmp, Iterator** list, int n) {
187 14 ▶ assert(n >= 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(n >= 0); 0 14 14
188 14 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 14 14
189 0 return NewEmptyIterator();
190 14 ▶ } else if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 1) { 13 1 14
191 13 return list[0];
192 } else {
193 1 ▶ return new MergingIterator(cmp, list, n);
Branch: 2 branches
BranchTrueFalseTotal
return new MergingIterator(cmp, list, n); 1 0 1
return new MergingIterator(cmp, list, n); 0 0 0
194 }
195 }
196  
197 }
« util/comparator.cc ↑ Summary table/iterator.cc »