Class: Ucode::Site::SearchIndex
- Inherits:
-
Object
- Object
- Ucode::Site::SearchIndex
- Includes:
- Repo::AtomicWrites
- Defined in:
- lib/ucode/site/search_index.rb
Overview
Builds the client-side search payload consumed by MiniSearch.
Input: ‘output/index/labels.json` written by `Repo::AggregateWriter`
— a flat `{ "U+XXXX" => { name, gc, sc } }` map (~160k entries,
~5 MB raw).
Output: ‘output/index/search.json` — an array of `{ id, name, gc, sc }`
objects, ready to feed `new MiniSearch(payload, ...)`.
Streaming: labels.json is parsed incrementally via the stdlib JSON parser; the entire payload is materialised once for atomic write. For ~160k codepoints this peaks around ~30 MB — acceptable for a build-time tool.
Idempotent: re-runs are byte-compared no-ops via AtomicWrites.
Instance Method Summary collapse
-
#build ⇒ Integer?
Build and write ‘search.json`.
-
#initialize(output_root) ⇒ SearchIndex
constructor
A new instance of SearchIndex.
-
#target_path ⇒ Pathname
The path that #build writes to.
Methods included from Repo::AtomicWrites
#same_content?, #to_pretty_json, #write_atomic
Constructor Details
#initialize(output_root) ⇒ SearchIndex
Returns a new instance of SearchIndex.
30 31 32 |
# File 'lib/ucode/site/search_index.rb', line 30 def initialize(output_root) @output_root = Pathname.new(output_root) end |
Instance Method Details
#build ⇒ Integer?
Build and write ‘search.json`. Returns the entry count, or nil if labels.json is absent (nothing to index).
37 38 39 40 41 42 43 44 45 |
# File 'lib/ucode/site/search_index.rb', line 37 def build labels = load_labels return nil unless labels entries = labels.map { |cp_id, | entry_for(cp_id, ) } payload = JSON.generate(entries) write_atomic(target_path, payload) entries.size end |
#target_path ⇒ Pathname
The path that #build writes to. Exposed so specs and the site generator can reference it without duplicating the convention.
50 51 52 |
# File 'lib/ucode/site/search_index.rb', line 50 def target_path Pathname(@output_root).join("index", "search.json") end |