Class: SQLiteSweep::ResultFile
- Inherits:
-
Object
- Object
- SQLiteSweep::ResultFile
- 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.
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Absolute path to the JSONL output file.
-
#row_count ⇒ Integer
readonly
Total number of rows written so far.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the file handle.
-
#initialize ⇒ ResultFile
constructor
A new instance of ResultFile.
-
#write(result) ⇒ Object
Writes all rows from a Result to the file.
Constructor Details
#initialize ⇒ ResultFile
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
#path ⇒ String (readonly)
Returns Absolute path to the JSONL output file.
21 22 23 |
# File 'lib/sqlitesweep/result_file.rb', line 21 def path @path end |
#row_count ⇒ Integer (readonly)
Returns 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
#close ⇒ Object
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.
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 |