Class: Textus::Ports::RawIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/ports/raw_index.rb

Overview

Content-addressable index for the raw lane. Maps content hashes and URLs to their current canonical key. Lives under <root>/.state/indexes/raw.yaml (gitignored, regenerable — truth is in the on-disk raw entries).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:) ⇒ RawIndex

Returns a new instance of RawIndex.



12
13
14
15
# File 'lib/textus/ports/raw_index.rb', line 12

def initialize(root:)
  @root = root
  @path = Layout.raw_index(root)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/textus/ports/raw_index.rb', line 17

def path
  @path
end

Instance Method Details

#find_by_hash(content_hash) ⇒ Object



33
34
35
36
# File 'lib/textus/ports/raw_index.rb', line 33

def find_by_hash(content_hash)
  index = load
  index["hashes"]&.fetch(content_hash, nil)
end

#find_by_url(url) ⇒ Object



38
39
40
41
42
43
# File 'lib/textus/ports/raw_index.rb', line 38

def find_by_url(url)
  return nil unless url

  index = load
  index["urls"]&.fetch(url, nil)
end

#loadObject



19
20
21
22
23
24
25
# File 'lib/textus/ports/raw_index.rb', line 19

def load
  return empty_index unless File.exist?(@path)

  YAML.safe_load_file(@path) || empty_index
rescue StandardError
  empty_index
end

#save(index) ⇒ Object



27
28
29
30
31
# File 'lib/textus/ports/raw_index.rb', line 27

def save(index)
  FileUtils.mkdir_p(File.dirname(@path))
  File.write(@path, YAML.dump(index))
  index
end

#upsert(content_hash:, url:, key:) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/textus/ports/raw_index.rb', line 45

def upsert(content_hash:, url:, key:)
  index = load
  index["hashes"] ||= {}
  index["urls"] ||= {}
  index["hashes"][content_hash] = key
  index["urls"][url] = key if url
  save(index)
end