Class: NoSE::Loader::SqlLoader

Inherits:
LoaderBase show all
Defined in:
lib/nose/loader/sql.rb

Overview

Load data from a MySQL database into a backend

Instance Method Summary collapse

Methods inherited from LoaderBase

#model, #workload

Constructor Details

#initialize(workload = nil, backend = nil) ⇒ SqlLoader

Returns a new instance of SqlLoader.



9
10
11
12
13
14
# File 'lib/nose/loader/sql.rb', line 9

def initialize(workload = nil, backend = nil)
  @logger = Logging.logger['nose::loader::sqlloader']

  @workload = workload
  @backend = backend
end

Instance Method Details

#load(indexes, config, show_progress = false, limit = nil, skip_existing = true) ⇒ Object

Load a generated set of indexes with data from MySQL



17
18
19
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
45
46
# File 'lib/nose/loader/sql.rb', line 17

def load(indexes, config, show_progress = false, limit = nil,
         skip_existing = true)
  indexes.map!(&:to_id_graph).uniq! if @backend.by_id_graph

  # XXX Assuming backend is thread-safe
  Parallel.each(indexes, in_threads: 2) do |index|
    client = new_client config

    # Skip this index if it's not empty
    if skip_existing && !@backend.index_empty?(index)
      @logger.info "Skipping index #{index.inspect}" if show_progress
      next
    end
    @logger.info index.inspect if show_progress

    query = index_sql client, index, limit

    result_chunk = []
    query.each do |result|
      result = Hash[result.map { |k, v| [k.to_s, v] }]
      result_chunk.push result
      if result_chunk.length >= 100
        @backend.index_insert_chunk index, result_chunk
        result_chunk = []
      end
    end
    @backend.index_insert_chunk index, result_chunk \
      unless result_chunk.empty?
  end
end