Class: Promiscuous::Dependency

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Dependency

Returns a new instance of Dependency.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/promiscuous/dependency.rb', line 6

def initialize(*args)
  options = args.extract_options!
  @type = options[:type]
  @owner = options[:owner]
  @dont_hash = options[:dont_hash]

  @internal_key = args.join('/')

  if @internal_key =~ /^[0-9]+$/
    @internal_key = @internal_key.to_i
    @hash = @internal_key
  else
    @hash = FNV.new.fnv1a_32(@internal_key)

    if Promiscuous::Config.hash_size.to_i > 0
      # We hash dependencies to have a O(1) memory footprint in Redis.
      # The hashing needs to be deterministic across instances in order to
      # function properly.
      @hash = @hash % Promiscuous::Config.hash_size.to_i
      @internal_key = @hash unless @dont_hash
    end
  end

  if @owner
    @internal_key = "#{@owner}:#{@internal_key}"
  end
end

Instance Attribute Details

#internal_keyObject

Returns the value of attribute internal_key.



4
5
6
# File 'lib/promiscuous/dependency.rb', line 4

def internal_key
  @internal_key
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/promiscuous/dependency.rb', line 4

def type
  @type
end

#versionObject

Returns the value of attribute version.



4
5
6
# File 'lib/promiscuous/dependency.rb', line 4

def version
  @version
end

Class Method Details

.parse(payload, options = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/promiscuous/dependency.rb', line 57

def self.parse(payload, options={})
  case payload
  when /^(.+):([0-9]+)$/ then new($1, options).tap { |d| d.version = $2.to_i }
  when /^(.+)$/          then new($1, options)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



53
54
55
# File 'lib/promiscuous/dependency.rb', line 53

def as_json(options={})
  @version ? [@internal_key, @version].join(':') : @internal_key
end

#eql?(other) ⇒ Boolean Also known as: ==

We need the eql? method to function properly (we use ==, uniq, …) in operation XXX The version is not taken in account.

Returns:

  • (Boolean)


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

def eql?(other)
  self.internal_key == other.internal_key
end

#hashObject



75
76
77
# File 'lib/promiscuous/dependency.rb', line 75

def hash
  self.internal_key.hash
end

#key(role) ⇒ Object



44
45
46
# File 'lib/promiscuous/dependency.rb', line 44

def key(role)
  Promiscuous::Key.new(role).join(@internal_key)
end

#read?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/promiscuous/dependency.rb', line 34

def read?
  raise "Type not set" unless @type
  @type == :read
end

#redis_node(distributed_redis = nil) ⇒ Object



48
49
50
51
# File 'lib/promiscuous/dependency.rb', line 48

def redis_node(distributed_redis=nil)
  distributed_redis ||= Promiscuous::Redis.master
  distributed_redis.nodes[@hash % distributed_redis.nodes.size]
end

#to_sObject



64
65
66
# File 'lib/promiscuous/dependency.rb', line 64

def to_s
  as_json.to_s
end

#write?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/promiscuous/dependency.rb', line 39

def write?
  raise "Type not set" unless @type
  @type == :write
end