Class: Boffin::Keyspace

Inherits:
Object
  • Object
show all
Defined in:
lib/boffin/keyspace.rb

Overview

Responsible for generating keys to store hit data in Redis.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracker, is_uniq = false) ⇒ Keyspace

Returns a new instance of Keyspace.

Parameters:

  • tracker (Tracker)

    The Tracker that is using this Keyspace

  • is_uniq (true, false) (defaults to: false)

    If specified all keys will include .uniq after the root portion. Used for easily scoping data for tracking unique hits.



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

#configObject (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.

Parameters:

  • types (Array<Symbol>, Symbol)
  • instance (Object)

Returns:

  • (String)

See Also:



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

Parameters:

  • type (Symbol)

    The hit type that will use the generated key

  • unit (Symbol)

    The time interval: :hours, :days, or :months

  • size (Fixnum)

    The window size of the specified time interval being calculated

  • starting_at (Time, Date) (defaults to: Time.now)

    The time at which to start counting back from

Returns:

  • (Array<String>)

    An array of keys for the range of intervals

See Also:



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.

Parameters:

  • types (Array<Symbol>, Symbol)
  • instance (Object) (defaults to: nil)

Returns:

  • (String)

See Also:



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.

Parameters:

  • types (Array<Symbol>, Symbol)

    An array of hit types [:views, :likes], or a singular hit type :views

  • instance (Object) (defaults to: nil)

    If provided, the keyspace will be scoped to a particilar instance, used for hit counters: boffin:listing.<instance>

Returns:

  • (String)

    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.

Parameters:

  • type (Symbol)

    The hit type that will use the generated key

  • unit (Symbol)

    The time interval: :hours, :days, or :months

  • time (Time)

    The time for which to extract an interval from.



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>

Parameters:

  • types (Array<Symbol>, Symbol)
  • unit (Symbol)

    The time interval: :hours, :days, or :months

  • size (Fixnum)

    The window size of the specified time interval being calculated

Returns:

  • (String)

See Also:



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.

Parameters:

  • weighted_hit_types (Hash)

    The types and weights of hits of which a union was calculated: { views: 1, likes: 2 }

  • unit (Symbol)

    The time interval: :hours, :days, or :months

  • size (Fixnum)

    The window size of the specified time interval being calculated

Returns:

  • (String)

See Also:



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.

Parameters:

  • type (Symbol)

    The hit type that will use the generated key

  • window (String)

    Represents a period of time: "2011-01-01-01", "2011-01-01", "2011-01"

Returns:

  • (String)

See Also:



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.

Parameters:

  • instance (Object) (defaults to: nil)

    Object that will be used to prefix the key namespace this is used for keys that deal with object instances. (See Utils#object_as_key)

Returns:

  • (String)

    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.

Returns:

  • (true, false)

    true if this keyspace is scoped for unique data



20
21
22
# File 'lib/boffin/keyspace.rb', line 20

def unique_namespace?
  @uniq
end