Skip to content
Kward Search API index

Class: Kward::SessionCatalog

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/session_catalog.rb

Overview

Rebuildable lightweight summaries for persisted session event logs.

Session JSONL files remain authoritative. Catalog entries are accepted only while their source fingerprint matches and may be deleted at any time.

Constant Summary collapse

VERSION =
1
INDEX_DIRECTORY =
".index"
FILENAME =
"sessions.json"

Instance Method Summary collapse

Constructor Details

#initialize(session_dir:) ⇒ SessionCatalog

Returns a new instance of SessionCatalog.



15
16
17
18
19
# File 'lib/kward/session_catalog.rb', line 15

def initialize(session_dir:)
  @path = File.join(session_dir, INDEX_DIRECTORY, FILENAME)
  @entries = load_entries
  @dirty = false
end

Instance Method Details

#fetch(path) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/kward/session_catalog.rb', line 21

def fetch(path)
  entry = @entries[entry_key(path)]
  return nil unless entry.is_a?(Hash)
  return nil unless entry["source"] == fingerprint(path)

  entry["summary"] if entry["summary"].is_a?(Hash)
rescue StandardError
  nil
end

#fingerprint(path) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/kward/session_catalog.rb', line 62

def fingerprint(path)
  stat = File.stat(path)
  {
    "size" => stat.size,
    "inode" => stat.ino,
    "mtimeSeconds" => stat.mtime.to_i,
    "mtimeNanoseconds" => stat.mtime.nsec
  }
end

#flushObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kward/session_catalog.rb', line 50

def flush
  return unless @dirty

  PrivateFile.write_json(@path, {
    "version" => VERSION,
    "entries" => @entries
  })
  @dirty = false
rescue StandardError
  nil
end

#remove(path) ⇒ Object



39
40
41
# File 'lib/kward/session_catalog.rb', line 39

def remove(path)
  @dirty = true if @entries.delete(entry_key(path))
end

#retain(paths) ⇒ Object



43
44
45
46
47
48
# File 'lib/kward/session_catalog.rb', line 43

def retain(paths)
  keys = paths.to_h { |path| [entry_key(path), true] }
  previous_size = @entries.size
  @entries.delete_if { |key, _entry| !keys[key] }
  @dirty = true if @entries.size != previous_size
end

#write(path, summary, fingerprint: self.fingerprint(path)) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/kward/session_catalog.rb', line 31

def write(path, summary, fingerprint: self.fingerprint(path))
  @entries[entry_key(path)] = {
    "source" => fingerprint,
    "summary" => summary
  }
  @dirty = true
end