Guest User

Untitled

a guest
Nov 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. commit 912767abf9ee5271061b35ec718f1d27482dbdbd
  2. Author: Evan Klitzke <evan@eklitzke.org>
  3. Date: Sun Jul 1 23:40:49 2012 -0700
  4.  
  5. add reader threads
  6.  
  7. diff --git a/src/index_reader.cc b/src/index_reader.cc
  8. index adf7f27..f890b44 100644
  9. --- a/src/index_reader.cc
  10. +++ b/src/index_reader.cc
  11. @@ -11,6 +11,7 @@
  12. #include <fstream>
  13. #include <iostream>
  14. #include <set>
  15. +#include <thread>
  16.  
  17. namespace codesearch {
  18. bool IndexReader::Verify() {
  19. @@ -44,11 +45,15 @@ bool IndexReader::Initialize() {
  20. bool IndexReader::Search(const std::string &query,
  21. SearchResults *results) {
  22. results->Reset();
  23. + std::vector<std::thread> threads;
  24. for (const auto &shard : shards_) {
  25. - if (!shard->Search(query, results)) {
  26. - results->Reset();
  27. - return false;
  28. - }
  29. + threads.push_back(std::thread(&ShardReader::Search, shard, query));
  30. + }
  31. +
  32. + std::size_t i = 0;
  33. + for (const auto & shard : shards_) {
  34. + threads[i++].join();
  35. + results->Extend(shard->results());
  36. }
  37. return true;
  38. }
  39. diff --git a/src/search_results.h b/src/search_results.h
  40. index 0bb61d5..9f285c2 100644
  41. --- a/src/search_results.h
  42. +++ b/src/search_results.h
  43. @@ -32,7 +32,13 @@ class SearchResults {
  44. SearchResult r(filename, line_num, line_text);
  45. results_.push_back(r);
  46. }
  47. - const std::vector<SearchResult> &results() const { return results_; };
  48. +
  49. + void Extend(const SearchResults &other) {
  50. + const auto &other_results = other.results();
  51. + results_.insert(results_.end(), other_results.begin(), other_results.end());
  52. + }
  53. +
  54. + const std::vector<SearchResult>& results() const { return results_; };
  55. private:
  56. std::vector<SearchResult> results_;
  57. };
  58. diff --git a/src/shard_reader.cc b/src/shard_reader.cc
  59. index 56bf1aa..1ac521f 100644
  60. --- a/src/shard_reader.cc
  61. +++ b/src/shard_reader.cc
  62. @@ -7,13 +7,11 @@
  63. #include <algorithm>
  64. #include <iostream>
  65. #include <fstream>
  66. -
  67. #include <set>
  68.  
  69. #include <leveldb/cache.h>
  70. #include <leveldb/options.h>
  71.  
  72. -
  73. namespace codesearch {
  74.  
  75. bool ShardReader::Initialize() {
  76. @@ -48,7 +46,8 @@ bool ShardReader::Initialize() {
  77. return true;
  78. }
  79.  
  80. -bool ShardReader::Search(const std::string &query, SearchResults *results) {
  81. +bool ShardReader::Search(const std::string &query) {
  82. + results_.Reset();
  83. if (query.size() < ngram_size_) {
  84. return true;
  85. }
  86. @@ -105,13 +104,12 @@ bool ShardReader::Search(const std::string &query, SearchResults *results) {
  87. FileValue fileval;
  88. fileval.ParseFromString(value);
  89.  
  90. - results->AddResult(fileval.filename(), pos.file_line(), pos_line);
  91. + results_.AddResult(fileval.filename(), pos.file_line(), pos_line);
  92. }
  93. }
  94. return true;
  95. }
  96.  
  97. -
  98. bool ShardReader::GetCandidates(const std::string &ngram,
  99. std::vector<std::uint64_t> *candidates) {
  100. assert(candidates->empty());
  101. diff --git a/src/shard_reader.h b/src/shard_reader.h
  102. index 11f398f..be4ba89 100644
  103. --- a/src/shard_reader.h
  104. +++ b/src/shard_reader.h
  105. @@ -27,7 +27,10 @@ class ShardReader {
  106. bool Initialize();
  107.  
  108. // Returns true on success, false on failure.
  109. - bool Search(const std::string &query, SearchResults *results);
  110. + bool Search(const std::string &query);
  111. +
  112. + // Access the search results.
  113. + const SearchResults& results() const { return results_; }
  114.  
  115. ~ShardReader();
  116.  
  117. @@ -40,6 +43,8 @@ class ShardReader {
  118. leveldb::DB* ngrams_db_;
  119. leveldb::DB* positions_db_;
  120.  
  121. + SearchResults results_;
  122. +
  123. bool GetCandidates(const std::string &ngram,
  124. std::vector<std::uint64_t> *candidates);
  125. };
Add Comment
Please, Sign In to add comment