Class: Ucode::Site::SearchIndex

Inherits:
Object
  • Object
show all
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

Methods included from Repo::AtomicWrites

#same_content?, #to_pretty_json, #write_atomic

Constructor Details

#initialize(output_root) ⇒ SearchIndex

Returns a new instance of SearchIndex.

Parameters:

  • output_root (String, Pathname)


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

#buildInteger?

Build and write ‘search.json`. Returns the entry count, or nil if labels.json is absent (nothing to index).

Returns:

  • (Integer, nil)


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, meta| entry_for(cp_id, meta) }
  payload = JSON.generate(entries)
  write_atomic(target_path, payload)
  entries.size
end

#target_pathPathname

The path that #build writes to. Exposed so specs and the site generator can reference it without duplicating the convention.

Returns:

  • (Pathname)


50
51
52
# File 'lib/ucode/site/search_index.rb', line 50

def target_path
  Pathname(@output_root).join("index", "search.json")
end