Class: Riakpb::MapReduce::Phase

Inherits:
Object
  • Object
show all
Includes:
Util::Translation
Defined in:
lib/riakpb/map_reduce.rb

Overview

Represents an individual phase in a map-reduce pipeline. Generally you’ll want to call methods of Riakpb::MapReduce instead of using this directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(options = {}) ⇒ Phase

Creates a phase in the map-reduce pipeline

Parameters:

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

    options for the phase

Options Hash (options):

  • :type (Symbol)

    one of :map, :reduce, :link

  • :language (String) — default: "javascript"

    “erlang” or “javascript”

  • :function (String, Array, Hash)

    In the case of Javascript, a literal function in a string, or a hash with :bucket and :key. In the case of Erlang, an Array of [module, function]. For a :link phase, a hash including any of :bucket, :tag or a WalkSpec.

  • :keep (Boolean) — default: false

    whether to return the results of this phase

  • :arg (Array) — default: nil

    any extra static arguments to pass to the phase



157
158
159
160
161
162
163
# File 'lib/riakpb/map_reduce.rb', line 157

def initialize(options={})
  self.type = options[:type]
  self.language = options[:language] || "javascript"
  self.function = options[:function]
  self.keep = options[:keep] || false
  self.arg = options[:arg]
end

Instance Attribute Details

#argArray

Returns any extra static arguments to pass to the phase.

Returns:

  • (Array)

    any extra static arguments to pass to the phase



148
149
150
# File 'lib/riakpb/map_reduce.rb', line 148

def arg
  @arg
end

#functionString, ...

Returns For :map and :reduce types, the Javascript function to run (as a string or hash with bucket/key), or the module + function in Erlang to run. For a :link type, a WalkSpec or an equivalent hash.

Returns:

  • (String, Array<String, String>, Hash, WalkSpec)

    For :map and :reduce types, the Javascript function to run (as a string or hash with bucket/key), or the module + function in Erlang to run. For a :link type, a WalkSpec or an equivalent hash.



139
140
141
# File 'lib/riakpb/map_reduce.rb', line 139

def function
  @function
end

#keepBoolean

Returns whether results of this phase will be returned.

Returns:

  • (Boolean)

    whether results of this phase will be returned



145
146
147
# File 'lib/riakpb/map_reduce.rb', line 145

def keep
  @keep
end

#languageString

Returns the language of the phase’s function - “javascript” or “erlang”. Meaningless for :link type phases.

Returns:

  • (String)

    the language of the phase’s function - “javascript” or “erlang”. Meaningless for :link type phases.



142
143
144
# File 'lib/riakpb/map_reduce.rb', line 142

def language
  @language
end

#typeSymbol

Returns the type of phase - :map, :reduce, or :link.

Returns:

  • (Symbol)

    the type of phase - :map, :reduce, or :link



136
137
138
# File 'lib/riakpb/map_reduce.rb', line 136

def type
  @type
end

Instance Method Details

#as_json(options = nil) ⇒ Hash

Converts the phase to its JSON-compatible representation for job invocation.

Returns:

  • (Hash)

    a Hash-equivalent of the phase



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/riakpb/map_reduce.rb', line 194

def as_json(options=nil)
  obj = case type
        when :map, :reduce
          defaults = {:language => language, :keep => keep}
          case function
          when Hash
            defaults.merge(function)
          when String
            if function =~ /\s*function/
              defaults.merge("source" => function)
            else
              defaults.merge("name" => function)
            end
          when Array
            defaults.merge("module" => function[0], "function" => function[1])
          end
        when :link
          function.merge({:keep => keep})
        end
  obj["arg"] = arg if arg
  { type => obj }
end

#to_json(options = nil) ⇒ String

Converts the phase to JSON for use while invoking a job.

Returns:

  • (String)

    a JSON representation of the phase



188
189
190
# File 'lib/riakpb/map_reduce.rb', line 188

def to_json(options=nil)
  ActiveSupport::JSON.encode(as_json, options)
end