-
-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor the current search functionality into a search module #781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| sourcemeta_library(NAMESPACE sourcemeta PROJECT one NAME search | ||
| SOURCES search.cc) | ||
|
|
||
| target_link_libraries(sourcemeta_one_search PUBLIC sourcemeta::core::json) | ||
| target_link_libraries(sourcemeta_one_search PUBLIC sourcemeta::core::io) | ||
| target_link_libraries(sourcemeta_one_search PRIVATE sourcemeta::one::metapack) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #ifndef SOURCEMETA_ONE_SEARCH_H_ | ||
| #define SOURCEMETA_ONE_SEARCH_H_ | ||
|
|
||
| #ifndef SOURCEMETA_ONE_SEARCH_EXPORT | ||
| #include <sourcemeta/one/search_export.h> | ||
| #endif | ||
|
|
||
| #include <sourcemeta/core/io.h> | ||
| #include <sourcemeta/core/json.h> | ||
|
|
||
| #include <cstddef> // std::size_t | ||
| #include <cstdint> // std::uint8_t | ||
| #include <filesystem> // std::filesystem::path | ||
| #include <memory> // std::unique_ptr | ||
| #include <string> // std::string | ||
| #include <string_view> // std::string_view | ||
| #include <vector> // std::vector | ||
|
|
||
| namespace sourcemeta::one { | ||
|
|
||
| struct SearchEntry { | ||
| std::string path; | ||
| std::string title; | ||
| std::string description; | ||
| }; | ||
|
|
||
| SOURCEMETA_ONE_SEARCH_EXPORT | ||
| auto make_search(std::vector<SearchEntry> &&entries) | ||
| -> std::vector<std::uint8_t>; | ||
|
|
||
| SOURCEMETA_ONE_SEARCH_EXPORT | ||
| auto search(const std::uint8_t *payload, std::size_t payload_size, | ||
| std::string_view query) -> sourcemeta::core::JSON; | ||
|
|
||
| class SOURCEMETA_ONE_SEARCH_EXPORT SearchView { | ||
| public: | ||
| explicit SearchView(std::filesystem::path path); | ||
| ~SearchView(); | ||
|
|
||
| SearchView(const SearchView &) = delete; | ||
| SearchView(SearchView &&) = delete; | ||
| auto operator=(const SearchView &) -> SearchView & = delete; | ||
| auto operator=(SearchView &&) -> SearchView & = delete; | ||
|
|
||
| auto search(std::string_view query) -> sourcemeta::core::JSON; | ||
|
|
||
| private: | ||
| std::filesystem::path path_; | ||
| std::unique_ptr<sourcemeta::core::FileView> view_; | ||
| const std::uint8_t *payload_{nullptr}; | ||
| std::size_t payload_size_{0}; | ||
| auto ensure_open() -> void; | ||
| }; | ||
|
|
||
| } // namespace sourcemeta::one | ||
|
|
||
| #endif |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #include <sourcemeta/one/search.h> | ||
|
|
||
| #include <sourcemeta/one/metapack.h> | ||
|
|
||
| #include <algorithm> // std::ranges::search | ||
| #include <cassert> // assert | ||
| #include <cctype> // std::tolower | ||
| #include <sstream> // std::ostringstream | ||
| #include <utility> // std::move | ||
|
|
||
| namespace sourcemeta::one { | ||
|
|
||
| auto make_search(std::vector<SearchEntry> &&entries) | ||
| -> std::vector<std::uint8_t> { | ||
| // Prioritise entries that have more metadata filled in, | ||
| // then sort lexicographically by path | ||
| std::ranges::sort(entries, [](const SearchEntry &left, | ||
| const SearchEntry &right) { | ||
| const auto left_score = | ||
| (!left.title.empty() ? 1 : 0) + (!left.description.empty() ? 1 : 0); | ||
| const auto right_score = | ||
| (!right.title.empty() ? 1 : 0) + (!right.description.empty() ? 1 : 0); | ||
| if (left_score != right_score) { | ||
| return left_score > right_score; | ||
| } | ||
|
|
||
| // TODO: Ideally we sort based on schema health too, given | ||
| // lint results | ||
| return left.path < right.path; | ||
| }); | ||
|
|
||
| std::ostringstream buffer; | ||
| for (const auto &entry : entries) { | ||
| auto json_entry{sourcemeta::core::JSON::make_array()}; | ||
| json_entry.push_back(sourcemeta::core::JSON{entry.path}); | ||
| json_entry.push_back(sourcemeta::core::JSON{entry.title}); | ||
| json_entry.push_back(sourcemeta::core::JSON{entry.description}); | ||
| sourcemeta::core::stringify(json_entry, buffer); | ||
| buffer << '\n'; | ||
| } | ||
|
|
||
| const auto result{buffer.str()}; | ||
| return {result.begin(), result.end()}; | ||
| } | ||
|
|
||
| auto search(const std::uint8_t *payload, const std::size_t payload_size, | ||
| const std::string_view query) -> sourcemeta::core::JSON { | ||
| auto result{sourcemeta::core::JSON::make_array()}; | ||
| if (payload_size == 0) { | ||
| return result; | ||
| } | ||
|
|
||
| assert(payload != nullptr); | ||
| // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
| const std::string_view data{reinterpret_cast<const char *>(payload), | ||
| payload_size}; | ||
|
|
||
| std::size_t line_start{0}; | ||
| while (line_start < data.size()) { | ||
| auto line_end{data.find('\n', line_start)}; | ||
| if (line_end == std::string_view::npos) { | ||
| line_end = data.size(); | ||
| } | ||
|
|
||
| const auto line{data.substr(line_start, line_end - line_start)}; | ||
| line_start = line_end + 1; | ||
|
|
||
| if (line.empty()) { | ||
| continue; | ||
| } | ||
|
|
||
| if (std::ranges::search(line, query, [](const auto left, const auto right) { | ||
| return std::tolower(static_cast<unsigned char>(left)) == | ||
| std::tolower(static_cast<unsigned char>(right)); | ||
| }).empty()) { | ||
| continue; | ||
| } | ||
|
|
||
| auto entry{sourcemeta::core::JSON::make_object()}; | ||
| const std::string line_string{line}; | ||
| auto line_json{sourcemeta::core::parse_json(line_string)}; | ||
| entry.assign("path", std::move(line_json.at(0))); | ||
| entry.assign("title", std::move(line_json.at(1))); | ||
| entry.assign("description", std::move(line_json.at(2))); | ||
| result.push_back(std::move(entry)); | ||
|
|
||
| constexpr auto MAXIMUM_SEARCH_COUNT{10}; | ||
| if (result.array_size() >= MAXIMUM_SEARCH_COUNT) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| SearchView::SearchView(std::filesystem::path path) : path_{std::move(path)} {} | ||
|
|
||
| SearchView::~SearchView() = default; | ||
|
|
||
| auto SearchView::ensure_open() -> void { | ||
| if (this->view_) { | ||
| return; | ||
| } | ||
|
|
||
| assert(std::filesystem::exists(this->path_)); | ||
| assert(this->path_.is_absolute()); | ||
| this->view_ = std::make_unique<sourcemeta::core::FileView>(this->path_); | ||
| const auto payload_start_option{metapack_payload_offset(*this->view_)}; | ||
| assert(payload_start_option.has_value()); | ||
| const auto &payload_start{payload_start_option.value()}; | ||
| this->payload_size_ = this->view_->size() - payload_start; | ||
| if (this->payload_size_ > 0) { | ||
| this->payload_ = this->view_->as<std::uint8_t>(payload_start); | ||
| } | ||
| } | ||
|
|
||
| auto SearchView::search(const std::string_view query) | ||
| -> sourcemeta::core::JSON { | ||
| this->ensure_open(); | ||
| return sourcemeta::one::search(this->payload_, this->payload_size_, query); | ||
| } | ||
|
|
||
| } // namespace sourcemeta::one | ||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
search()constructs astd::string_viewfrompayloadeven whenpayloadmay benullptr(tests callsearch(nullptr, 0, ...)), which can violatestring_view's pointer preconditions even for size 0. Consider guarding the empty-payload case before buildingdata.Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.