Class: SwitchBoard::RedisDataset

Inherits:
AbstractDataset show all
Defined in:
lib/switch_board/datasets/redis_dataset.rb

Constant Summary collapse

LOCK_MAP_KEY =
"switch_board::locked_ids"

Instance Attribute Summary collapse

Attributes inherited from AbstractDataset

#persistance

Instance Method Summary collapse

Methods inherited from AbstractDataset

#get_next, #set_persistance

Constructor Details

#initialize(host = "127.0.0.1", port = 6379, name = "redis_switchbord") ⇒ RedisDataset

Returns a new instance of RedisDataset.



13
14
15
16
# File 'lib/switch_board/datasets/redis_dataset.rb', line 13

def initialize(host = "127.0.0.1", port = 6379, name = "redis_switchbord")
  @con = Redis.new(:host => host, :port => port)
  @name = name
end

Instance Attribute Details

#conObject

Returns the value of attribute con.



11
12
13
# File 'lib/switch_board/datasets/redis_dataset.rb', line 11

def con
  @con
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/switch_board/datasets/redis_dataset.rb', line 11

def name
  @name
end

#switchboardObject

Returns the value of attribute switchboard.



11
12
13
# File 'lib/switch_board/datasets/redis_dataset.rb', line 11

def switchboard
  @switchboard
end

Instance Method Details

#cleanupObject



18
19
20
21
22
23
# File 'lib/switch_board/datasets/redis_dataset.rb', line 18

def cleanup
  ## clean up keys, used mainly for testing
  @con.del @name
  @con.del "#{LOCK_MAP_KEY}_z"
  @con.del "#{LOCK_MAP_KEY}_h"
end

#get_all_locked_idsObject



69
70
71
72
# File 'lib/switch_board/datasets/redis_dataset.rb', line 69

def get_all_locked_ids
  clean_old_keys
  @con.hgetall "#{LOCK_MAP_KEY}_h"
end

#get_all_my_locked_ids(uid) ⇒ Object



79
80
81
82
# File 'lib/switch_board/datasets/redis_dataset.rb', line 79

def get_all_my_locked_ids(uid)
  res = get_all_locked_ids
  get_all_locked_ids.select {|key, key_uid|  key_uid.to_s == uid.to_s }
end

#get_all_their_locked_ids(uid) ⇒ Object



74
75
76
77
# File 'lib/switch_board/datasets/redis_dataset.rb', line 74

def get_all_their_locked_ids(uid)
  res = get_all_locked_ids
  res.reject {|key, key_uid|  key_uid.to_s == uid.to_s }
end

#get_lockedObject



25
26
27
28
# File 'lib/switch_board/datasets/redis_dataset.rb', line 25

def get_locked
  active_lockers = list_lockers.map { |item| JSON.parse(item)}
  active_lockers
end

#id_locked?(id_to_check) ⇒ Boolean

Check if key exists to see if it is locked and it has not expired before getting keys, remove expired keys

Returns:

  • (Boolean)


60
61
62
# File 'lib/switch_board/datasets/redis_dataset.rb', line 60

def id_locked?(id_to_check)
  @con.hexists("#{LOCK_MAP_KEY}_h", id_to_check)
end

#list_lockersObject



40
41
42
# File 'lib/switch_board/datasets/redis_dataset.rb', line 40

def list_lockers
  list_lockers ||= (@con.smembers @name).map { |item| JSON.parse(item)}
end

#lock_id(locker_uid, id_to_lock, expire_in_sec = 5) ⇒ Object

Locking mechanisem is based on sorted set, sorted set is used to allow a simulation of expiration time on the keys in the map



50
51
52
53
54
55
56
# File 'lib/switch_board/datasets/redis_dataset.rb', line 50

def lock_id(locker_uid, id_to_lock, expire_in_sec = 5)
  now = redis_time
  @con.multi do
    @con.zadd("#{LOCK_MAP_KEY}_z", (now + expire_in_sec), id_to_lock)
    @con.hset("#{LOCK_MAP_KEY}_h", id_to_lock, locker_uid)
  end
end

#locker(uid) ⇒ Object



44
45
46
# File 'lib/switch_board/datasets/redis_dataset.rb', line 44

def locker(uid)
  (list_lockers.select {|locker| locker["uid"] == uid}).first
end

#register_locker(uid, name) ⇒ Object



34
35
36
37
38
# File 'lib/switch_board/datasets/redis_dataset.rb', line 34

def register_locker(uid, name)
  @con.sadd @name, {uid: uid, name: name, created_at: redis_time}.to_json.to_s
  list_lockers ## update lockers list
  true
end

#unlock_id(locker_uid, id_to_unlock) ⇒ Object



65
66
67
# File 'lib/switch_board/datasets/redis_dataset.rb', line 65

def unlock_id(locker_uid, id_to_unlock)
  @con.hdel("#{LOCK_MAP_KEY}_h", id_to_unlock)
end