Class: Judges::Impex

Inherits:
Object show all
Defined in:
lib/judges/impex.rb

Overview

Import/Export of factbases.

This class provides functionality for importing and exporting Factbase objects to and from binary files. It handles file I/O operations with proper logging and error handling.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(loog, file) ⇒ Impex

Initialize a new Impex instance.

Examples:

Create an Impex instance

impex = Judges::Impex.new(logger, '/path/to/factbase.fb')

Parameters:

  • loog (Loog)

    Logging facility for recording import/export operations

  • file (String)

    File path for import/export operations



28
29
30
31
# File 'lib/judges/impex.rb', line 28

def initialize(loog, file)
  @loog = loog
  @file = file
end

Instance Method Details

#export(fb) ⇒ Object

Export factbase to file.

Exports the given Factbase instance to the file specified during initialization. Creates any necessary parent directories automatically. The operation is timed and logged with file size and fact count information.

Examples:

Export a factbase

fb = Factbase.new
# ... add facts to fb ...
impex.export(fb) # Saves to file specified in constructor

Parameters:

  • fb (Factbase)

    The factbase to export. All facts in this factbase will be serialized to the binary file format.



96
97
98
99
100
101
102
# File 'lib/judges/impex.rb', line 96

def export(fb)
  elapsed(@loog, level: Logger::INFO) do
    FileUtils.mkdir_p(File.dirname(@file))
    File.binwrite(@file, fb.export)
    throw :"Factbase exported to #{@file.to_rel} (#{File.size(@file)} bytes, #{fb.size} facts)"
  end
end

#import(strict: true) ⇒ Factbase

Import factbase from file.

Creates a new Factbase instance and imports data from the file specified during initialization. The operation is timed and logged. If the file doesn’t exist, behavior depends on the strict parameter.

Examples:

Import with strict mode (default)

fb = impex.import # Raises error if file missing

Import with non-strict mode

fb = impex.import(strict: false) # Returns empty factbase if file missing

Parameters:

  • strict (Boolean) (defaults to: true)

    Whether to raise error if file doesn’t exist. When true (default), raises an error if file is missing. When false, logs a message and returns an empty factbase.

Returns:

  • (Factbase)

    The imported factbase, or empty factbase if file doesn’t exist and strict is false

Raises:

  • (RuntimeError)

    If file doesn’t exist and strict is true



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/judges/impex.rb', line 49

def import(strict: true)
  fb = Factbase.new
  if File.exist?(@file)
    elapsed(@loog, level: Logger::INFO) do
      fb.import(File.binread(@file))
      throw :"The factbase imported from #{@file.to_rel} (#{File.size(@file)} bytes, #{fb.size} facts)"
    end
  else
    raise "The factbase is absent at #{@file.to_rel}" if strict
    @loog.info("Nothing to import from #{@file.to_rel} (file not found)")
  end
  fb
end

#import_to(fb) ⇒ Object

Import factbase from file into existing factbase.

Imports data from the file into an existing Factbase instance rather than creating a new one. This is useful when you need to merge data into an already populated factbase. The operation is timed and logged.

Examples:

Import into existing factbase

fb = Factbase.new
# ... populate fb with some data ...
impex.import_to(fb) # Adds data from file to existing facts

Parameters:

  • fb (Factbase)

    The factbase to import into. The imported data will be added to this existing factbase.

Raises:

  • (RuntimeError)

    If file doesn’t exist



76
77
78
79
80
81
82
# File 'lib/judges/impex.rb', line 76

def import_to(fb)
  raise "The factbase is absent at #{@file.to_rel}" unless File.exist?(@file)
  elapsed(@loog, level: Logger::INFO) do
    fb.import(File.binread(@file))
    throw :"The factbase loaded from #{@file.to_rel} (#{File.size(@file)} bytes, #{fb.size} facts)"
  end
end