Class: MongoMapperParallel::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper_parallel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Key

A chunk that will be parallelized

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create the chunk.

Options Hash (opts):

  • :key (String)

    the lower bound of the range of resources to retrieve

  • :future_key (String)

    the upper bound for the range of resources to retrieve

  • :compiler (MongoMapperParallel)

    the Parallel execution object that holds the keys, javascript, and arguments.

  • :debug (Boolean)

    whether to show messages during the process.



31
32
33
34
35
36
37
38
# File 'lib/mongo_mapper_parallel.rb', line 31

def initialize(opts={})
  @key        = opts[:key]
  @compiler   = opts[:compiler]
  @position   = opts[:position]
  @future_key = opts[:future_key]
  @completed  = false
  @debug      = opts[:debug].nil? ? true : opts[:debug]
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler.



21
22
23
# File 'lib/mongo_mapper_parallel.rb', line 21

def compiler
  @compiler
end

#completedObject

Returns the value of attribute completed.



20
21
22
# File 'lib/mongo_mapper_parallel.rb', line 20

def completed
  @completed
end

#debugObject

Returns the value of attribute debug.



22
23
24
# File 'lib/mongo_mapper_parallel.rb', line 22

def debug
  @debug
end

#future_keyObject

A chunk that will be parallelized



18
19
20
# File 'lib/mongo_mapper_parallel.rb', line 18

def future_key
  @future_key
end

#keyObject

Returns the value of attribute key.



19
20
21
# File 'lib/mongo_mapper_parallel.rb', line 19

def key
  @key
end

Instance Method Details

#argsArray, Hash

The arguments to pass to the Javascript function to run on the resources

Returns:

  • (Array, Hash)

    The arguments to pass to the javascript function



49
# File 'lib/mongo_mapper_parallel.rb', line 49

def args;          @compiler.args;          end

#command_classClass

The Ruby Class representing the collection containing the resources

Returns:

  • (Class)


54
# File 'lib/mongo_mapper_parallel.rb', line 54

def command_class; @compiler.command_class; end

#compileObject

Sends the Javascript function, the range, and the arguments to the MongoDB instance for computation via the db.runCommand command.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongo_mapper_parallel.rb', line 58

def compile
  search_opts = {:name => {:$gte => @key}}
  if @future_key then search_opts[:name][:$lte] = @future_key  end
  begin
    command_class.database.command({
      :"$eval" => javascript,
      :args    => [@key, @future_key, args],
      :nolock => true
      })
  rescue Mongo::ConnectionFailure
    raise Mongo::ConnectionFailure.new "Connection disconnected at position #{@position}"
  end
  @completed = true
  puts "Completed chunk (#{@position})".green if @debug
end

#javascriptString

The javascript function to run on the resources

Returns:

  • (String)

    The function to run.



43
# File 'lib/mongo_mapper_parallel.rb', line 43

def javascript;    @compiler.javascript;    end