Class: FlatKit::Merge

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/flat_kit/merge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventEmitter

#_listeners, #add_listener, #count_listeners, #notify_listeners, #remove_listener, #remove_listeners

Constructor Details

#initialize(inputs:, input_fallback: "auto", output:, output_fallback: "auto", compare_fields:) ⇒ Merge

Returns a new instance of Merge.



10
11
12
13
14
15
16
17
18
# File 'lib/flat_kit/merge.rb', line 10

def initialize(inputs:, input_fallback: "auto",
               output:, output_fallback: "auto",
               compare_fields:)
  @compare_fields = compare_fields
  @readers = ::FlatKit::Reader.create_readers_from_paths(paths: inputs, compare_fields: @compare_fields,
                                                         fallback: input_fallback)
  @writer  = ::FlatKit::Writer.create_writer_from_path(path: output, fallback: output_fallback,
                                                       reader_format: @readers.first.format_name)
end

Instance Attribute Details

#compare_fieldsObject (readonly)

Returns the value of attribute compare_fields.



8
9
10
# File 'lib/flat_kit/merge.rb', line 8

def compare_fields
  @compare_fields
end

#readersObject (readonly)

Returns the value of attribute readers.



6
7
8
# File 'lib/flat_kit/merge.rb', line 6

def readers
  @readers
end

#writerObject (readonly)

Returns the value of attribute writer.



7
8
9
# File 'lib/flat_kit/merge.rb', line 7

def writer
  @writer
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flat_kit/merge.rb', line 20

def call
  ::FlatKit.logger.debug "Merging the following files into #{writer.destination}"
  ::FlatKit.logger.debug "Using this key for sorting: #{compare_fields.join(", ")}"
  readers.each do |r|
    ::FlatKit.logger.debug "  #{r.source}"
  end

  merge_tree = ::FlatKit::MergeTree.new(readers)

  notify_listeners(name: :start, data: :start)
  merge_tree.each do |record|

    position = writer.write(record)
    meta = { position: position }
    notify_listeners(name: :record, data: record, meta: meta)
  end
  notify_listeners(name: :stop, data: :stop)

  readers.each do |r|
    ::FlatKit.logger.debug "  #{r.source} produced #{r.count} records"
  end

  writer.close
  ::FlatKit.logger.debug "Wrote #{writer.count} records to #{writer.destination}"
end