Class: MapReduce

Inherits:
Object
  • Object
show all
Defined in:
lib/map_reduce.rb,
lib/map_reduce/file.rb,
lib/map_reduce/array.rb,
lib/map_reduce/active_record.rb

Defined Under Namespace

Modules: ActiveRecord, Array, File

Constant Summary collapse

@@types =
{
  ::ActiveRecord::Base => [::Array, String, NilClass],
  ::File => String
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMapReduce

Returns a new instance of MapReduce.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/map_reduce.rb', line 18

def initialize
  @lock = false
  @offset = 0
  @limit = 1
  
  @time_began = 0

  @num_objects_grabbed = 0
  @time_spent_grabbing_objects = 0.0
  @time_spent_processing_objects = 0.0
  
  @num_queues_grabbed = 0
  @time_spent_grabbing_queues = 0.0
  
  @queue = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/map_reduce.rb', line 88

def method_missing(name, *args)
  if name.to_s =~ /(.*)=$/ && args[0].is_a?(Proc)
    self.class.instance_eval do
      define_method($1, args[0])
    end
  else
    super
  end
end

Instance Attribute Details

#inputObject Also known as: conditions

Returns the value of attribute input.



14
15
16
# File 'lib/map_reduce.rb', line 14

def input
  @input
end

#limitObject

Returns the value of attribute limit.



14
15
16
# File 'lib/map_reduce.rb', line 14

def limit
  @limit
end

#typeObject

Returns the value of attribute type.



14
15
16
# File 'lib/map_reduce.rb', line 14

def type
  @type
end

Instance Method Details

#add_time_spent_processing_objects(time) ⇒ Object



35
36
37
# File 'lib/map_reduce.rb', line 35

def add_time_spent_processing_objects(time)
  @time_spent_processing_objects += time
end

#base_typeObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/map_reduce.rb', line 111

def base_type
  check_type = @type
  type_found = false
  while check_type.superclass
    if @@types.include?(check_type)
      return check_type
    else
      check_type = check_type.superclass
    end
  end
end

#base_type_to_sObject



57
58
59
# File 'lib/map_reduce.rb', line 57

def base_type_to_s
  base_type.to_s if valid_type?
end

#map_reduce?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/map_reduce.rb', line 107

def map_reduce?
  true
end

#raise_if_invalid!Object



98
99
100
101
102
103
104
105
# File 'lib/map_reduce.rb', line 98

def raise_if_invalid!
  if not valid_type?
    raise MapReduceError, "invalid type, please make sure you provide one of the following classes or sub-classes thereof: ActiveRecord::Base, File, or Array"
  end
  if not valid_input?
    raise MapReduceError, "invalid input, please make sure you provide one of the following: #{Array(@@types[type]).join(", ")}"
  end
end

#spool=(type, input) ⇒ Object



65
66
67
68
# File 'lib/map_reduce.rb', line 65

def spool=(type, input)
  self.type = type
  self.input = input
end

#statsObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/map_reduce.rb', line 39

def stats
  {
    :time_began => @time_began,
    :num_queues_grabbed => @num_queues_grabbed,
    :time_spent_grabbing_queues => @time_spent_grabbing_objects,
    :num_objects_grabbed => @num_objects_grabbed,
    :time_spent_grabbing_objects => @time_spent_grabbing_objects,
    :time_spent_processing_objects => @time_spent_processing_objects
  }
end

#type_to_sObject



61
62
63
# File 'lib/map_reduce.rb', line 61

def type_to_s
  type.to_s if valid_type?
end

#valid?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/map_reduce.rb', line 70

def valid?
  valid_type? && valid_input?
end

#valid_input?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/map_reduce.rb', line 79

def valid_input?
  if not valid_type?
    return false
  else
    Array(@@types[base_type]).each {|input_type| return true if input.is_a?(input_type)}
    raise MapReduceError, "invalid input (#{@input.inspect}) for type: #{base_type}. Try one of the following: #{Array(@@types[base_type]).join(", ")}"
  end
end

#valid_type?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/map_reduce.rb', line 74

def valid_type?
  @@types.keys.each {|type| return true if @type && (@type < type || @type == type)}
  return false
end