Class: SQLiteSweep::ResultFile

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlitesweep/result_file.rb

Overview

Thread-safe JSONL file writer for the :list action.

Instead of holding all results in memory (which would be problematic when sweeping millions of databases), results are streamed to a JSONL file as they arrive. Each line is a JSON object with the query row data plus a “_source” field indicating which database it came from.

The file persists after the process exits so external tools can consume it. The file path is printed to stdout as the final output of a :list sweep.

Examples:

Output file contents (one JSON object per line):

{"count(*)":42,"_source":"/data/tenant_1.sqlite3"}
{"count(*)":17,"_source":"ssh://deploy@web1/data/tenant_2.sqlite3"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultFile

Returns a new instance of ResultFile.



26
27
28
29
30
31
# File 'lib/sqlitesweep/result_file.rb', line 26

def initialize
  @path = File.join(Dir.tmpdir, "sqlitesweep_results_#{Process.pid}_#{Time.now.to_i}.jsonl")
  @file = File.open(@path, "w")
  @mutex = Mutex.new
  @row_count = 0
end

Instance Attribute Details

#pathString (readonly)

Returns Absolute path to the JSONL output file.

Returns:

  • (String)

    Absolute path to the JSONL output file.



21
22
23
# File 'lib/sqlitesweep/result_file.rb', line 21

def path
  @path
end

#row_countInteger (readonly)

Returns Total number of rows written so far.

Returns:

  • (Integer)

    Total number of rows written so far.



24
25
26
# File 'lib/sqlitesweep/result_file.rb', line 24

def row_count
  @row_count
end

Instance Method Details

#closeObject

Closes the file handle. The file itself remains on disk for consumption.



49
50
51
# File 'lib/sqlitesweep/result_file.rb', line 49

def close
  @file.close unless @file.closed?
end

#write(result) ⇒ Object

Writes all rows from a Result to the file. Each row becomes one JSON line, annotated with “_source” for provenance tracking.

Parameters:

  • result (Result)

    The query result to write.



37
38
39
40
41
42
43
44
45
46
# File 'lib/sqlitesweep/result_file.rb', line 37

def write(result)
  @mutex.synchronize do
    result.rows.each do |row|
      line = row.merge("_source" => result.source).to_json
      @file.puts(line)
      @row_count += 1
    end
    @file.flush
  end
end