Class: GroongaDelta::LocalSource

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga-delta/local-source.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, status, writer) ⇒ LocalSource

Returns a new instance of LocalSource.



22
23
24
25
26
27
# File 'lib/groonga-delta/local-source.rb', line 22

def initialize(config, status, writer)
  @logger = config.logger
  @config = config.local
  @status = status.local
  @writer = writer
end

Instance Method Details

#importObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/groonga-delta/local-source.rb', line 29

def import
  latest_number = @status.number || -1
  targets = []
  Dir.glob("#{@config.dir}/*.grn") do |path|
    case File.basename(path)
    when /\A\d+/
      number = Regexp.last_match[0]
      number = Integer(number, 10)
      next if number <= latest_number
      targets << [number, path]
    else
      next
    end
  end
  targets.sort_by! {|number, _path| number}
  parser = create_command_parser
  targets.each do |number, path|
    if latest_number == -1 and number > @config.initial_max_number
      @logger.info("Stopped initial import")
      break
    end
    @logger.info("Start importing: #{path}")
    File.open(path) do |input|
      last_line = nil
      input.each_line do |line|
        last_line = line
        parser << line
      end
      if last_line and not last_line.end_with?("\n")
        parser << line
      end
    end
    @logger.info("Imported: #{path}")
    @status.update("number" => number)
  end
end