| Total Complexity | 9 |
| Total Lines | 41 |
| Duplicated Lines | 21.95 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 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, |
|
|
|
|||
| 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 |
||
| 49 |