Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Created July 10, 2012 15:30
Show Gist options
  • Select an option

  • Save eklitzke/3084083 to your computer and use it in GitHub Desktop.

Select an option

Save eklitzke/3084083 to your computer and use it in GitHub Desktop.
void NGramIndexReader::Find(const std::string &query,
SearchResults *results) {
if (query.size() < ngram_size_) {
FindSmall(query, results);
return;
}
// split the query into its constituent ngrams
std::set<std::string> ngrams;
for (std::string::size_type i = 0;
i <= query.length() - ngram_size_; i++) {
ngrams.insert(query.substr(i, ngram_size_));
}
assert(!ngrams.empty());
std::vector<std::thread> threads;
std::size_t parallelism = std::thread::hardware_concurrency() + 1;
threads.reserve(parallelism);
auto it = shards_.begin();
while (true) {
// build up a vector of threads to do searches
for (std::size_t i = 0; i < parallelism; i++) {
SSTableReader *reader = *it;
threads.push_back(std::thread(&NGramIndexReader::FindShard, this, query,
ngrams, *reader, results));
if (++it == shards_.end()) {
break;
}
}
for (std::size_t i = 0; i < threads.size(); i++) {
threads[i].join();
}
if (results->IsFull() || it == shards_.end()) {
break;
}
threads.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment