Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
search_index.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/detail/find_model_source.hpp>
6#include <portals/search.hpp>
7#include <portals/worker_storage.hpp>
8
9#include <memory_resource>
10#include <stop_token>
11
12namespace portals::detail {
14struct SearchBudgetExceeded {};
15
16// Count BEFORE upstream allocation, including simultaneous old/new vector capacities.
17class SearchMemory final : public ProjectionMemory {
18public:
19 explicit SearchMemory(std::size_t cap) : cap_(cap) {
20 }
21
22 std::size_t used() const noexcept {
23 return used_;
24 }
25
26 std::size_t peak() const noexcept {
27 return peak_;
28 }
29
30 void chargeExternal(std::size_t bytes) override;
31 void releaseExternal(std::size_t bytes) noexcept override;
32
33private:
34 void *do_allocate(std::size_t bytes, std::size_t alignment) override;
35 void do_deallocate(void *data, std::size_t bytes, std::size_t alignment) override;
36
37 bool do_is_equal(const std::pmr::memory_resource &other) const noexcept override {
38 return this == &other;
39 }
40
41 std::size_t cap_{}, used_{}, peak_{};
42};
43
44struct SearchUnit {
45 std::uint32_t byteFirst{}, byteCount{};
46 std::uint32_t originalFirst{}, originalLast{};
47 bool whitespace{};
48};
49
50struct SearchTerm {
51 std::uint32_t first{}, count{}, row{};
52 bool token{};
53};
54
55struct SearchIndexRow {
56 RowId id;
57 std::uint32_t textFirst{}, textCount{};
58};
59
60struct SearchNode {
61 std::uint32_t term{}, depth{}, terminalFirst{}, terminalCount{}, childrenFirst{}, childrenCount{};
62 std::uint32_t minLength{}, minOrdinal{};
63 bool minToken{};
64};
65
66class SearchIndex final : public WorkerStorageValue {
67public:
68 SearchIndex(SearchSourceIdentity identity, SearchOptions options);
69 std::size_t bytes() const noexcept;
70 SearchSourceIdentity identity;
71 SearchOptions options;
72 SearchMemory memory;
73 std::pmr::string original;
74 std::pmr::string normalized;
75 std::pmr::vector<SearchUnit> units;
76 std::pmr::vector<SearchTerm> terms;
77 std::pmr::vector<SearchIndexRow> rows;
78 std::pmr::vector<SearchNode> nodes;
79 std::pmr::vector<std::uint32_t> children;
80};
81
82bool validSearchOptions(const SearchOptions &) noexcept;
83std::expected<std::unique_ptr<SearchIndex>, SearchError>
84buildSearchIndex(const FindWorkerSource &, SearchSourceIdentity, SearchOptions, std::stop_token);
85std::expected<std::shared_ptr<const SearchResult>, SearchError>
86querySearchIndex(const SearchIndex &, std::string_view, std::uint64_t generation, std::stop_token);
88} // namespace portals::detail
consteval auto id(StyleState state=StyleState::ANY, bool readOnly=false)
Definition style_static.hpp:865
consteval auto token()
Definition style_static.hpp:814