Class: Mistri::Stores::JSONL

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/stores/jsonl.rb

Overview

One JSONL file per session under a directory: durable sessions without a database, and a transcript any tool can read line by line.

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ JSONL

Returns a new instance of JSONL.



11
12
13
14
# File 'lib/mistri/stores/jsonl.rb', line 11

def initialize(dir)
  @dir = dir
  FileUtils.mkdir_p(dir)
end

Instance Method Details

#append(id, entry) ⇒ Object

One write call per line: concurrent appenders (a steer, a child's report) interleave whole lines, never fragments.



18
19
20
21
# File 'lib/mistri/stores/jsonl.rb', line 18

def append(id, entry)
  File.write(path(id), "#{JSON.generate(entry)}\n", mode: "a")
  nil
end

#load(id) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/mistri/stores/jsonl.rb', line 23

def load(id)
  return [] unless File.exist?(path(id))

  File.readlines(path(id)).filter_map do |line|
    JSON.parse(line) unless line.strip.empty?
  end
end