|
| 1 | +#include <sourcemeta/one/search.h> |
| 2 | + |
| 3 | +#include <sourcemeta/one/metapack.h> |
| 4 | + |
| 5 | +#include <algorithm> // std::ranges::search |
| 6 | +#include <cassert> // assert |
| 7 | +#include <cctype> // std::tolower |
| 8 | +#include <sstream> // std::ostringstream |
| 9 | +#include <utility> // std::move |
| 10 | + |
| 11 | +namespace sourcemeta::one { |
| 12 | + |
| 13 | +auto make_search(std::vector<SearchEntry> &&entries) |
| 14 | + -> std::vector<std::uint8_t> { |
| 15 | + // Prioritise entries that have more metadata filled in, |
| 16 | + // then sort lexicographically by path |
| 17 | + std::ranges::sort(entries, |
| 18 | + [](const SearchEntry &left, const SearchEntry &right) { |
| 19 | + const auto left_score = |
| 20 | + (!left.title.empty() ? 1 : 0) + |
| 21 | + (!left.description.empty() ? 1 : 0); |
| 22 | + const auto right_score = |
| 23 | + (!right.title.empty() ? 1 : 0) + |
| 24 | + (!right.description.empty() ? 1 : 0); |
| 25 | + if (left_score != right_score) { |
| 26 | + return left_score > right_score; |
| 27 | + } |
| 28 | + |
| 29 | + // TODO: Ideally we sort based on schema health too, given |
| 30 | + // lint results |
| 31 | + if (left_score > 0) { |
| 32 | + return left.path < right.path; |
| 33 | + } |
| 34 | + |
| 35 | + return false; |
| 36 | + }); |
| 37 | + |
| 38 | + std::ostringstream buffer; |
| 39 | + for (const auto &entry : entries) { |
| 40 | + auto json_entry{sourcemeta::core::JSON::make_array()}; |
| 41 | + json_entry.push_back(sourcemeta::core::JSON{entry.path}); |
| 42 | + json_entry.push_back(sourcemeta::core::JSON{entry.title}); |
| 43 | + json_entry.push_back(sourcemeta::core::JSON{entry.description}); |
| 44 | + sourcemeta::core::stringify(json_entry, buffer); |
| 45 | + buffer << '\n'; |
| 46 | + } |
| 47 | + |
| 48 | + const auto result{buffer.str()}; |
| 49 | + return {result.begin(), result.end()}; |
| 50 | +} |
| 51 | + |
| 52 | +auto search(const std::uint8_t *payload, const std::size_t payload_size, |
| 53 | + const std::string_view query) -> sourcemeta::core::JSON { |
| 54 | + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 55 | + const std::string_view data{reinterpret_cast<const char *>(payload), |
| 56 | + payload_size}; |
| 57 | + |
| 58 | + auto result{sourcemeta::core::JSON::make_array()}; |
| 59 | + std::size_t line_start{0}; |
| 60 | + while (line_start < data.size()) { |
| 61 | + auto line_end{data.find('\n', line_start)}; |
| 62 | + if (line_end == std::string_view::npos) { |
| 63 | + line_end = data.size(); |
| 64 | + } |
| 65 | + |
| 66 | + const auto line{data.substr(line_start, line_end - line_start)}; |
| 67 | + line_start = line_end + 1; |
| 68 | + |
| 69 | + if (line.empty()) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + if (std::ranges::search(line, query, [](const auto left, const auto right) { |
| 74 | + return std::tolower(left) == std::tolower(right); |
| 75 | + }).empty()) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + auto entry{sourcemeta::core::JSON::make_object()}; |
| 80 | + const std::string line_string{line}; |
| 81 | + auto line_json{sourcemeta::core::parse_json(line_string)}; |
| 82 | + entry.assign("path", std::move(line_json.at(0))); |
| 83 | + entry.assign("title", std::move(line_json.at(1))); |
| 84 | + entry.assign("description", std::move(line_json.at(2))); |
| 85 | + result.push_back(std::move(entry)); |
| 86 | + |
| 87 | + constexpr auto MAXIMUM_SEARCH_COUNT{10}; |
| 88 | + if (result.array_size() >= MAXIMUM_SEARCH_COUNT) { |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return result; |
| 94 | +} |
| 95 | + |
| 96 | +SearchView::SearchView(std::filesystem::path path) : path_{std::move(path)} {} |
| 97 | + |
| 98 | +SearchView::~SearchView() = default; |
| 99 | + |
| 100 | +auto SearchView::ensure_open() -> void { |
| 101 | + if (this->view_) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + assert(std::filesystem::exists(this->path_)); |
| 106 | + assert(this->path_.is_absolute()); |
| 107 | + this->view_ = std::make_unique<sourcemeta::core::FileView>(this->path_); |
| 108 | + const auto payload_start_option{metapack_payload_offset(*this->view_)}; |
| 109 | + assert(payload_start_option.has_value()); |
| 110 | + const auto &payload_start{payload_start_option.value()}; |
| 111 | + this->payload_size_ = this->view_->size() - payload_start; |
| 112 | + this->payload_ = this->view_->as<std::uint8_t>(payload_start); |
| 113 | +} |
| 114 | + |
| 115 | +auto SearchView::search(const std::string_view query) |
| 116 | + -> sourcemeta::core::JSON { |
| 117 | + this->ensure_open(); |
| 118 | + return sourcemeta::one::search(this->payload_, this->payload_size_, query); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace sourcemeta::one |
0 commit comments