Module: Redlics::Key

Extended by:
Key
Included in:
Key
Defined in:
lib/redlics/key.rb

Overview

Key namespace

Instance Method Summary collapse

Instance Method Details

#bucketize?(context, options = {}) ⇒ Boolean

Check if Redlics can bucketize.

Parameters:

  • context (Hash)

    the hash of a context defined in Redlics::CONTEXTS

  • options (Hash) (defaults to: {})

    configuration options

Returns:

  • (Boolean)

    true if can bucketize, false if not



102
103
104
# File 'lib/redlics/key.rb', line 102

def bucketize?(context, options = {})
  context[:long] == :counter && Redlics.config.bucket && !options[:id].nil?
end

#decode(string) ⇒ Integer

Decode a number with a mapping table.

Parameters:

  • string (String)

    the string to encode

Returns:

  • (Integer)

    the decoded string as integer



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/redlics/key.rb', line 77

def decode(string)
  decoded = ''
  string = string.to_s
  token = 0
  while token <= string.size - 1
    number = decode_map[string[token]].to_s
    decoded += number.size == 1 ? "0#{number}" : number
    token += 1
  end
  decoded.to_i
end

#encode(number) ⇒ String

Encode a number with a mapping table.

Parameters:

  • number (Integer)

    the number to encode

Returns:

  • (String)

    the encoded number as string



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/redlics/key.rb', line 61

def encode(number)
  encoded = ''
  number = number.to_s
  number = (number.size % 2) != 0 ? "0#{number}" : number
  token = 0
  while token <= number.size - 1
    encoded += encode_map[number[token..token+1].to_i.to_s].to_s
    token += 2
  end
  encoded
end

#exists?(key) ⇒ Boolean

Check if a key exists in Redis.

Parameters:

  • string (String)

    the key name to check

Returns:

  • (Boolean)

    true id key exists, false if not



93
94
95
# File 'lib/redlics/key.rb', line 93

def exists?(key)
  Redlics.redis { |r| r.exists(key) }
end

#name(context, event, granularity, past, options = {}) ⇒ String, Array

Construct the key name with given parameters.

Parameters:

  • context (Hash)

    the hash of a context defined in Redlics::CONTEXTS

  • event (String)

    event name with eventual Redis namespace separator

  • granularity (Symbol)

    existing granularity

  • past (Time)

    a time object

  • options (Hash) (defaults to: {})

    configuration options

Returns:

  • (String)

    unbucketized key name

  • (Array)

    bucketized key name



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/redlics/key.rb', line 17

def name(context, event, granularity, past, options = {})
  past ||= Time.now
  event ||= 'nil'
  granularity = Granularity.validate(context, granularity).first
  event = encode_event(event) if Redlics.config.encode[:events]
  key = "#{context[:short]}#{Redlics.config.separator}#{event}#{Redlics.config.separator}#{time_format(granularity, past)}"
  key = with_namespace(key) if options[:namespaced]
  return bucketize(key, options[:id]) if bucketize?(context, options)
  return unbucketize(key, options[:id]) if context[:long] == :counter && !options[:id].nil?
  key
end

#timeframed(context, event, time_object, options = {}) ⇒ Array

Construct an array with all keys of a time frame in a given granularity.

Parameters:

  • context (Hash)

    the hash of a context defined in Redlics::CONTEXTS

  • event (String)

    event name with eventual Redis namespace separator

  • time_object (Symbol)

    time object predefined in Redlics::TimeFrame.init_with_symbol

  • time_object (Hash)

    time object with keys ‘from` and `to`

  • time_object (Range)

    time object as range

  • time_object (Time)

    time object

  • options (Hash) (defaults to: {})

    configuration options

Returns:

  • (Array)

    array with all keys of a time frame in a given granularity



39
40
41
42
43
44
45
# File 'lib/redlics/key.rb', line 39

def timeframed(context, event, time_object, options = {})
  options = { namespaced: true }.merge(options)
  timeframe = TimeFrame.new(context, time_object, options)
  timeframe.splat do |time|
    name(context, event, timeframe.granularity, time, options)
  end
end

#unique_namespaceString

Create a unique operation key in Redis.

Returns:

  • (String)

    the created unique operation key



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/redlics/key.rb', line 108

def unique_namespace
  loop do
    ns = operation
    unless exists?(ns)
      Redlics.redis do |conn|
        conn.pipelined do |redis|
          redis.set(ns, 0)
          redis.expire(ns, Redlics.config.operation_expiration)
        end
      end
      break ns
    end
  end
end

#with_namespace(key) ⇒ String

Prepend namespace to a key.

Parameters:

  • key (String)

    the key name

Returns:

  • (String)

    the key name with prepended namespace



51
52
53
54
55
# File 'lib/redlics/key.rb', line 51

def with_namespace(key)
  return key unless Redlics.config.namespace.length > 0
  return key if key.split(Redlics.config.separator).first == Redlics.config.namespace.to_s
  "#{Redlics.config.namespace}#{Redlics.config.separator}#{key}"
end