| 85 | } |
| 86 | |
| 87 | void InMemoryIterator::SeekKey(const std::string &key) { |
| 88 | bool found = false; |
| 89 | std::map<std::string, DataVector::iterator>::iterator it = sstable_->index_.find(key); |
| 90 | if (it != sstable_->index_.end()) { |
| 91 | cur_it_ = it->second; |
| 92 | found = true; |
| 93 | } |
| 94 | // FIXME(yesp) : is it right? |
| 95 | if (!found && (key < sstable_->data_.begin()->first)) { |
| 96 | cur_it_ = sstable_->data_.begin(); |
| 97 | found = true; |
| 98 | } |
| 99 | if (!found && (key > sstable_->data_.back().first)) { |
| 100 | valid_ = false; |
| 101 | return; |
| 102 | } |
| 103 | if (!found) { |
| 104 | DataVector::size_type n = sstable_->data_.size(); |
| 105 | unsigned int begin = 0; |
| 106 | unsigned int end = n - 1; |
| 107 | unsigned int mid = 0; |
| 108 | while (begin <= end) { |
| 109 | mid = (begin + end) / 2; |
| 110 | LOG(INFO)<< "begin:" << begin << ";mid:" << mid << ";end:" << end; |
| 111 | const std::pair<std::string, std::vector<std::string> >& midPair = sstable_->data_[mid]; |
| 112 | if (midPair.first == key) { |
| 113 | break; |
| 114 | } else if (midPair.first < key) { |
| 115 | begin = mid + 1; |
| 116 | } else if (midPair.first > key) { |
| 117 | end = mid - 1; |
| 118 | } |
| 119 | } |
| 120 | cur_it_ = sstable_->data_.begin() + mid; |
| 121 | } |
| 122 | pos_ = 0; |
| 123 | size_ = cur_it_->second.size(); |
| 124 | valid_ = true; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | } // namespace toft |