Created
July 2, 2012 06:34
-
-
Save eklitzke/3031458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| commit 912767abf9ee5271061b35ec718f1d27482dbdbd | |
| Author: Evan Klitzke <evan@eklitzke.org> | |
| Date: Sun Jul 1 23:40:49 2012 -0700 | |
| add reader threads | |
| diff --git a/src/index_reader.cc b/src/index_reader.cc | |
| index adf7f27..f890b44 100644 | |
| --- a/src/index_reader.cc | |
| +++ b/src/index_reader.cc | |
| @@ -11,6 +11,7 @@ | |
| #include <fstream> | |
| #include <iostream> | |
| #include <set> | |
| +#include <thread> | |
| namespace codesearch { | |
| bool IndexReader::Verify() { | |
| @@ -44,11 +45,15 @@ bool IndexReader::Initialize() { | |
| bool IndexReader::Search(const std::string &query, | |
| SearchResults *results) { | |
| results->Reset(); | |
| + std::vector<std::thread> threads; | |
| for (const auto &shard : shards_) { | |
| - if (!shard->Search(query, results)) { | |
| - results->Reset(); | |
| - return false; | |
| - } | |
| + threads.push_back(std::thread(&ShardReader::Search, shard, query)); | |
| + } | |
| + | |
| + std::size_t i = 0; | |
| + for (const auto & shard : shards_) { | |
| + threads[i++].join(); | |
| + results->Extend(shard->results()); | |
| } | |
| return true; | |
| } | |
| diff --git a/src/search_results.h b/src/search_results.h | |
| index 0bb61d5..9f285c2 100644 | |
| --- a/src/search_results.h | |
| +++ b/src/search_results.h | |
| @@ -32,7 +32,13 @@ class SearchResults { | |
| SearchResult r(filename, line_num, line_text); | |
| results_.push_back(r); | |
| } | |
| - const std::vector<SearchResult> &results() const { return results_; }; | |
| + | |
| + void Extend(const SearchResults &other) { | |
| + const auto &other_results = other.results(); | |
| + results_.insert(results_.end(), other_results.begin(), other_results.end()); | |
| + } | |
| + | |
| + const std::vector<SearchResult>& results() const { return results_; }; | |
| private: | |
| std::vector<SearchResult> results_; | |
| }; | |
| diff --git a/src/shard_reader.cc b/src/shard_reader.cc | |
| index 56bf1aa..1ac521f 100644 | |
| --- a/src/shard_reader.cc | |
| +++ b/src/shard_reader.cc | |
| @@ -7,13 +7,11 @@ | |
| #include <algorithm> | |
| #include <iostream> | |
| #include <fstream> | |
| - | |
| #include <set> | |
| #include <leveldb/cache.h> | |
| #include <leveldb/options.h> | |
| - | |
| namespace codesearch { | |
| bool ShardReader::Initialize() { | |
| @@ -48,7 +46,8 @@ bool ShardReader::Initialize() { | |
| return true; | |
| } | |
| -bool ShardReader::Search(const std::string &query, SearchResults *results) { | |
| +bool ShardReader::Search(const std::string &query) { | |
| + results_.Reset(); | |
| if (query.size() < ngram_size_) { | |
| return true; | |
| } | |
| @@ -105,13 +104,12 @@ bool ShardReader::Search(const std::string &query, SearchResults *results) { | |
| FileValue fileval; | |
| fileval.ParseFromString(value); | |
| - results->AddResult(fileval.filename(), pos.file_line(), pos_line); | |
| + results_.AddResult(fileval.filename(), pos.file_line(), pos_line); | |
| } | |
| } | |
| return true; | |
| } | |
| - | |
| bool ShardReader::GetCandidates(const std::string &ngram, | |
| std::vector<std::uint64_t> *candidates) { | |
| assert(candidates->empty()); | |
| diff --git a/src/shard_reader.h b/src/shard_reader.h | |
| index 11f398f..be4ba89 100644 | |
| --- a/src/shard_reader.h | |
| +++ b/src/shard_reader.h | |
| @@ -27,7 +27,10 @@ class ShardReader { | |
| bool Initialize(); | |
| // Returns true on success, false on failure. | |
| - bool Search(const std::string &query, SearchResults *results); | |
| + bool Search(const std::string &query); | |
| + | |
| + // Access the search results. | |
| + const SearchResults& results() const { return results_; } | |
| ~ShardReader(); | |
| @@ -40,6 +43,8 @@ class ShardReader { | |
| leveldb::DB* ngrams_db_; | |
| leveldb::DB* positions_db_; | |
| + SearchResults results_; | |
| + | |
| bool GetCandidates(const std::string &ngram, | |
| std::vector<std::uint64_t> *candidates); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment