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

RandomLoader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 21.95 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 9
loc 41
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B load_index() 0 16 5
A load() 9 9 3
A initialize() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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