Class: Smith::AclTypeCache

Inherits:
Object
  • Object
show all
Includes:
MurmurHash3, Singleton
Defined in:
lib/smith/messaging/acl_type_cache.rb

Constant Summary collapse

SUPPORTED_FORMATS =
[:string, :binary]
DEFAULT_FORMAT =
:string

Instance Method Summary collapse

Constructor Details

#initializeAclTypeCache

Returns a new instance of AclTypeCache.



16
17
18
# File 'lib/smith/messaging/acl_type_cache.rb', line 16

def initialize
  clear!
end

Instance Method Details

#add(type) ⇒ true|false

Add the type to the cashe. This will add the type for all know formats

Parameters:

  • type (Class)

    the type to add

Returns:

  • (true|false)

    true if the type was added or false if it already exists.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smith/messaging/acl_type_cache.rb', line 23

def add(type)
  if SUPPORTED_FORMATS.all? { |format| @types[format].has_key?(type) }
    false
  else
    SUPPORTED_FORMATS.each do |format|
      h = to_murmur32(type, format)
      @types[format][type] = h
      @hashes[format][h] = type
    end

    @legacy_types_by_hash[type.to_s.split(/::/)[-1].snake_case] = type
    true
  end
end

#clear!Object

Clear the internal hashes.



77
78
79
80
81
# File 'lib/smith/messaging/acl_type_cache.rb', line 77

def clear!
  @types = SUPPORTED_FORMATS.each_with_object({}) { |v, acc| acc[v] = {} }
  @hashes = SUPPORTED_FORMATS.each_with_object({}) { |v, acc| acc[v] = {} }
  @legacy_types_by_hash = {}
end

#dump_hashes(format = DEFAULT_FORMAT) ⇒ Hash

Dump the hashes hash

Parameters:

  • format (Symbol) (defaults to: DEFAULT_FORMAT)

    the format of the mumor3 hash. Defaults to Smith::AclTypeCache::DEFAULT_FORMAT

Returns:

  • (Hash)

Raises:



101
102
103
104
105
106
107
# File 'lib/smith/messaging/acl_type_cache.rb', line 101

def dump_hashes(format=DEFAULT_FORMAT)
  if @hashes.has_key?(format)
    @hashes[format]
  else
    raise ACL::UnknownTypeFormat, "Uknown format: #{format}"
  end
end

#dump_types(format = DEFAULT_FORMAT) ⇒ Hash

Dump the type hash

Parameters:

  • format (Symbol) (defaults to: DEFAULT_FORMAT)

    the format of the mumor3 hash. Defaults to Smith::AclTypeCache::DEFAULT_FORMAT

Returns:

  • (Hash)

Raises:



88
89
90
91
92
93
94
# File 'lib/smith/messaging/acl_type_cache.rb', line 88

def dump_types(format=DEFAULT_FORMAT)
  if @types.has_key?(format)
    @types[format]
  else
    raise ACL::UnknownTypeFormat, "Uknown format: #{format}"
  end
end

#get_by_hash(type, format = DEFAULT_FORMAT) ⇒ Class

Return the type given the mumur3 hash

Parameters:

  • type (Sting)

    the mumur3 hash to lookup

  • format (Symbol) (defaults to: DEFAULT_FORMAT)

    the format of the mumor3 hash. Defaults to Smith::AclTypeCache::DEFAULT_FORMAT

Returns:

  • (Class)

Raises:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/smith/messaging/acl_type_cache.rb', line 44

def get_by_hash(type, format=DEFAULT_FORMAT)
  if t = dump_hashes(format)[type]
    t
  else
    if t = @legacy_types_by_hash[type.to_s]
      t
    else
      raise ACL::UnknownError, "Unknown ACL: #{t}"
    end
  end
end

#get_by_type(type, format = DEFAULT_FORMAT) ⇒ String

Return the mumur3 hash of the given the type

Parameters:

  • type (Class)

    the class to lookup

  • format (Symbol) (defaults to: DEFAULT_FORMAT)

    the format of the mumor3 hash. Defaults to Smith::AclTypeCache::DEFAULT_FORMAT

Returns:

  • (String)


61
62
63
# File 'lib/smith/messaging/acl_type_cache.rb', line 61

def get_by_type(type, format=DEFAULT_FORMAT)
  dump_types(format)[type].tap { |t| raise ACL::UnknownError, "Unknown ACL: #{t}" if type.nil? }
end

#include?(key, opts = {}) ⇒ Boolean

Look the key up in the cache. This defaults to the key being the hash. If :by_type => true is passed in as the second argument then it will perform the lookup in the type hash.

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/smith/messaging/acl_type_cache.rb', line 68

def include?(key, opts={})
  if opts[:by_type]
    !get_by_type(key, opts.fetch(:format, DEFAULT_FORMAT)).nil?
  else
    !get_by_hash(key, opts.fetch(:format, DEFAULT_FORMAT)).nil?
  end
end