Completed
Push — master ( 0fe3ca...0de097 )
by Michael
01:48
created

RandomLoader.initialize()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
# frozen_string_literal: true
2
3
module NoSE
4
  module Loader
5
    # Load some random data (mostly useful for testing)
6
    class RandomLoader < LoaderBase
7
      def initialize(workload = nil, backend = nil)
8
        @logger = Logging.logger['nose::loader::randomloader']
9
10
        @workload = workload
11
        @backend = backend
12
      end
13
14
      # Load a generated set of indexes with data from MySQL
15
      # @return [void]
16 View Code Duplication
      def load(indexes, config, show_progress = false, limit = nil,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
17
               skip_existing = true)
18
        limit = 1 if limit.nil?
19
20
        indexes.map!(&:to_id_graph).uniq! if @backend.by_id_graph
21
        indexes.uniq.each do |index|
22
          load_index index, config, show_progress, limit, skip_existing
23
        end
24
      end
25
26
      private
27
28
      # Load a single index into the backend
29
      # @return [void]
30
      def load_index(index, _config, show_progress, limit, skip_existing)
31
        # Skip this index if it's not empty
32
        if skip_existing && [email protected]_empty?(index)
33
          @logger.info "Skipping index #{index.inspect}" if show_progress
34
          return
35
        end
36
        @logger.info index.inspect if show_progress
37
38
        chunk = Array.new(limit) do
39
          Hash[index.all_fields.map do |field|
40
            [field.id, field.random_value]
41
          end]
42
        end
43
44
        @backend.index_insert_chunk index, chunk
45
      end
46
    end
47
  end
48
end
49