Class: SolidCache::MaglevHash

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

Defined Under Namespace

Classes: Preferences

Constant Summary collapse

TABLE_SIZE =

Must be prime

2053

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ MaglevHash

Returns a new instance of MaglevHash.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
# File 'lib/solid_cache/maglev_hash.rb', line 12

def initialize(nodes)
  raise ArgumentError, "No nodes specified" if nodes.count == 0
  raise ArgumentError, "Maximum node count is #{TABLE_SIZE}" if nodes.count > TABLE_SIZE

  @nodes = nodes.uniq.sort
  @lookup = build_lookup
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



7
8
9
# File 'lib/solid_cache/maglev_hash.rb', line 7

def nodes
  @nodes
end

Instance Method Details

#node(key) ⇒ Object



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

def node(key)
  nodes[lookup[quick_hash(key) % TABLE_SIZE]]
end