Class: Boffin::Keyspace
- Inherits:
-
Object
- Object
- Boffin::Keyspace
- Defined in:
- lib/boffin/keyspace.rb
Overview
Responsible for generating keys to store hit data in Redis.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#hit_count(types, instance) ⇒ String
Calculates the hit root and postfixes it with ":hit_count", this key is used to store a count of total hits ever made.
-
#hit_time_windows(type, unit, size, starting_at = Time.now) ⇒ Array<String>
Generates keys for each interval in the calculated range of time.
-
#hits(types, instance = nil) ⇒ String
Calculates the hit root and postfixes it with ":hits", this key is used for a sorted set that stores unique hit count data.
-
#hits_root(types, instance = nil) ⇒ String
The root portion of the hits keyspace:
boffin:listing:views
,boffin:listing.5:views
. -
#hits_time_window(type, unit, time) ⇒ Object
Generates a key for a sorted set that is used to store hit data for a particular interval of time.
-
#hits_union(types, unit, size) ⇒ String
Returns a key that is used for storing the result set of a union for the provided window of time.
-
#hits_union_multi(weighted_hit_types, unit, size) ⇒ String
Returns a key that is used for storing the result set of a union of hit unions.
-
#hits_window(type, window) ⇒ String
Generates a key that is used for storing hit data for a particular interval in time.
-
#initialize(tracker, is_uniq = false) ⇒ Keyspace
constructor
A new instance of Keyspace.
-
#root(instance = nil) ⇒ String
The root portion of a key:
boffin:listing
,boffin:listing.5
, orboffin:listing.5:uniq
. -
#unique_namespace? ⇒ true, false
true
if this keyspace is scoped for unique data.
Constructor Details
#initialize(tracker, is_uniq = false) ⇒ Keyspace
Returns a new instance of Keyspace.
12 13 14 15 16 |
# File 'lib/boffin/keyspace.rb', line 12 def initialize(tracker, is_uniq = false) @config = tracker.config @ns = tracker.namespace @uniq = is_uniq ? true : false end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/boffin/keyspace.rb', line 5 def config @config end |
Instance Method Details
#hit_count(types, instance) ⇒ String
Calculates the hit root and postfixes it with ":hit_count", this key is used to store a count of total hits ever made.
66 67 68 |
# File 'lib/boffin/keyspace.rb', line 66 def hit_count(types, instance) "#{hits_root(types, instance)}:hit_count" end |
#hit_time_windows(type, unit, size, starting_at = Time.now) ⇒ Array<String>
Generates keys for each interval in the calculated range of time
128 129 130 131 |
# File 'lib/boffin/keyspace.rb', line 128 def hit_time_windows(type, unit, size, starting_at = Time.now) Utils.time_ago_range(starting_at, unit => size). map { |time| hits_time_window(type, unit, time) } end |
#hits(types, instance = nil) ⇒ String
Calculates the hit root and postfixes it with ":hits", this key is used for a sorted set that stores unique hit count data.
56 57 58 |
# File 'lib/boffin/keyspace.rb', line 56 def hits(types, instance = nil) "#{hits_root(types, instance)}:hits" end |
#hits_root(types, instance = nil) ⇒ String
Returns The root portion of the hits keyspace: boffin:listing:views
,
boffin:listing.5:views
.
46 47 48 |
# File 'lib/boffin/keyspace.rb', line 46 def hits_root(types, instance = nil) "#{root(instance)}:#{[types].flatten.join('_')}" end |
#hits_time_window(type, unit, time) ⇒ Object
Generates a key for a sorted set that is used to store hit data for a
particular interval of time. For example, the :days
interval of
2011-08-26 11:24:41 would be 2011-08-26.
142 143 144 |
# File 'lib/boffin/keyspace.rb', line 142 def hits_time_window(type, unit, time) hits_window(type, time.strftime(INTERVAL_FORMATS[unit])) end |
#hits_union(types, unit, size) ⇒ String
Returns a key that is used for storing the result set of a union for the
provided window of time. Calls #hits, and then appends
:current.<unit>_<size>
80 81 82 |
# File 'lib/boffin/keyspace.rb', line 80 def hits_union(types, unit, size) "#{hits(types)}:current.#{unit}_#{size}" end |
#hits_union_multi(weighted_hit_types, unit, size) ⇒ String
Returns a key that is used for storing the result set of a union of hit unions.
95 96 97 98 |
# File 'lib/boffin/keyspace.rb', line 95 def hits_union_multi(weighted_hit_types, unit, size) types = weighted_hit_types.map { |type, weight| "#{type}_#{weight}" } hits_union(types, unit, size) end |
#hits_window(type, window) ⇒ String
Generates a key that is used for storing hit data for a particular interval in time. You'll probably want to use #hits_time_window as it will generate the window string for you.
111 112 113 |
# File 'lib/boffin/keyspace.rb', line 111 def hits_window(type, window) "#{hits(type)}.#{window}" end |
#root(instance = nil) ⇒ String
Returns The root portion of a key: boffin:listing
, boffin:listing.5
, or
boffin:listing.5:uniq
.
30 31 32 33 34 35 |
# File 'lib/boffin/keyspace.rb', line 30 def root(instance = nil) slug = instance ? Utils.object_as_key(instance) : nil "#{@config.namespace}:#{@ns}".tap { |s| s << ".#{slug}" if slug s << ":uniq" if @uniq } end |
#unique_namespace? ⇒ true, false
Returns true
if this keyspace is scoped for unique data.
20 21 22 |
# File 'lib/boffin/keyspace.rb', line 20 def unique_namespace? @uniq end |