Class: Pyper::Pipes::Cassandra::ModKey

Inherits:
Object
  • Object
show all
Defined in:
lib/pyper/pipes/cassandra/mod_key.rb

Overview

Adds the :mod_key field to the output attributes, which is based on the hash of a particular field in the input attributes. If the pipe is configured with an id field of :id, then the input

{ id: 'abc' }

would result in an output of

{ id: 'abc', mod_key: 22 }

Here the value 22 is within the range [0,mod_size - 1] and is uniquely determined by id.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod_size = 100, id_field = :id) ⇒ ModKey

Returns a new instance of ModKey.

Parameters:

  • mod_size (Integer) (defaults to: 100)

    mod keys will fall within the range [0,mod_key - 1]

  • id_field (Symbol) (defaults to: :id)

    the attribute to use when generating the mod key.



16
17
18
19
# File 'lib/pyper/pipes/cassandra/mod_key.rb', line 16

def initialize(mod_size = 100, id_field = :id)
  @mod_size = mod_size
  @id_field = id_field
end

Instance Attribute Details

#id_fieldObject (readonly)

Returns the value of attribute id_field.



12
13
14
# File 'lib/pyper/pipes/cassandra/mod_key.rb', line 12

def id_field
  @id_field
end

#mod_sizeObject (readonly)

Returns the value of attribute mod_size.



12
13
14
# File 'lib/pyper/pipes/cassandra/mod_key.rb', line 12

def mod_size
  @mod_size
end

Instance Method Details

#mod(value) ⇒ Object



28
29
30
# File 'lib/pyper/pipes/cassandra/mod_key.rb', line 28

def mod(value)
  Zlib::crc32(value) % mod_size
end

#pipe(attributes, status) ⇒ Hash

Returns The attribute hash with the mod_key field added.

Parameters:

  • attributes (Hash)

    An attribute hash

  • status (Hash)

    The mutable status field

Returns:

  • (Hash)

    The attribute hash with the mod_key field added



24
25
26
# File 'lib/pyper/pipes/cassandra/mod_key.rb', line 24

def pipe(attributes, status)
  attributes.merge!(:mod_key => mod(attributes[id_field]))
end