Class: Hive::Mocks::Storage
- Inherits:
-
Object
- Object
- Hive::Mocks::Storage
- Defined in:
- lib/hive/mocks/storage.rb
Instance Method Summary collapse
- #del(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ Storage
constructor
A new instance of Storage.
- #map_del(key) ⇒ Object
- #map_get(key, name) ⇒ Object
- #map_get_all_keys(key) ⇒ Object
-
#map_set(key, name, value) ⇒ Object
Maps.
- #map_size(key) ⇒ Object
-
#put(key, value) ⇒ Object
Simple values.
-
#queue_add(queue_name, item, score) ⇒ Object
Priority Queue.
- #queue_del(queue_name) ⇒ Object
-
#queue_pop(queue_name, max_score = Time.now.to_i) ⇒ Object
pop the lowest item from the queue IFF it scores <= max_score.
- #queue_pop_sync(queue_name, max_score = Time.now.to_i, options = {}) ⇒ Object
- #reconnect_after_fork ⇒ Object
-
#set_add(key, value) ⇒ Object
Sets.
- #set_get_all(key) ⇒ Object
- #set_member?(key, value) ⇒ Boolean
- #set_remove(key, value) ⇒ Object
- #set_size(key) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Storage
Returns a new instance of Storage.
5 6 7 |
# File 'lib/hive/mocks/storage.rb', line 5 def initialize @storage = {} end |
Instance Method Details
#del(key) ⇒ Object
27 28 29 |
# File 'lib/hive/mocks/storage.rb', line 27 def del( key ) @storage.delete( key ) end |
#get(key) ⇒ Object
23 24 25 |
# File 'lib/hive/mocks/storage.rb', line 23 def get( key ) @storage[key] end |
#map_del(key) ⇒ Object
73 74 75 |
# File 'lib/hive/mocks/storage.rb', line 73 def map_del( key ) @storage.delete( key ) end |
#map_get(key, name) ⇒ Object
61 62 63 |
# File 'lib/hive/mocks/storage.rb', line 61 def map_get( key, name ) (@storage[key] || {}) [name] end |
#map_get_all_keys(key) ⇒ Object
65 66 67 |
# File 'lib/hive/mocks/storage.rb', line 65 def map_get_all_keys( key ) (@storage[key] || {}).keys end |
#map_set(key, name, value) ⇒ Object
Maps
56 57 58 59 |
# File 'lib/hive/mocks/storage.rb', line 56 def map_set( key, name, value ) @storage[key] ||= {} @storage[key][name] = value end |
#map_size(key) ⇒ Object
69 70 71 |
# File 'lib/hive/mocks/storage.rb', line 69 def map_size( key ) (@storage[key] || {} ).size end |
#put(key, value) ⇒ Object
Simple values
19 20 21 |
# File 'lib/hive/mocks/storage.rb', line 19 def put( key, value ) @storage[key] = value end |
#queue_add(queue_name, item, score) ⇒ Object
Priority Queue
79 80 81 82 83 |
# File 'lib/hive/mocks/storage.rb', line 79 def queue_add( queue_name, item, score ) queue = @storage[queue_name] ||= [] queue << [ item, score ] queue.sort_by! { |it| it.last } end |
#queue_del(queue_name) ⇒ Object
108 109 110 |
# File 'lib/hive/mocks/storage.rb', line 108 def queue_del( queue_name ) @storage.delete( queue_name ) end |
#queue_pop(queue_name, max_score = Time.now.to_i) ⇒ Object
pop the lowest item from the queue IFF it scores <= max_score
86 87 88 89 90 91 92 93 94 |
# File 'lib/hive/mocks/storage.rb', line 86 def queue_pop( queue_name, max_score = Time.now.to_i ) queue = @storage[queue_name] || [] return nil if queue.size == 0 if queue.first.last <= max_score then queue.shift.first else nil end end |
#queue_pop_sync(queue_name, max_score = Time.now.to_i, options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hive/mocks/storage.rb', line 96 def queue_pop_sync( queue_name, max_score = Time.now.to_i, = {} ) timeout = [:timeout] || 1 deadline = Time.now.to_f + timeout loop do result = queue_pop( queue_name, max_score ) return result if result raise Timeout::Error if Time.now.to_f > deadline end end |
#reconnect_after_fork ⇒ Object
9 10 11 |
# File 'lib/hive/mocks/storage.rb', line 9 def reconnect_after_fork # nop end |
#set_add(key, value) ⇒ Object
Sets
33 34 35 36 |
# File 'lib/hive/mocks/storage.rb', line 33 def set_add( key, value ) @storage[key] ||= [] @storage[key] << value unless @storage[key].member?(value) end |
#set_get_all(key) ⇒ Object
50 51 52 |
# File 'lib/hive/mocks/storage.rb', line 50 def set_get_all( key ) @storage[key] || [] end |
#set_member?(key, value) ⇒ Boolean
46 47 48 |
# File 'lib/hive/mocks/storage.rb', line 46 def set_member?( key, value ) (@storage[key] || []).member?( value ) end |
#set_remove(key, value) ⇒ Object
42 43 44 |
# File 'lib/hive/mocks/storage.rb', line 42 def set_remove( key, value ) (@storage[key] || [] ).delete( value ) end |
#set_size(key) ⇒ Object
38 39 40 |
# File 'lib/hive/mocks/storage.rb', line 38 def set_size( key ) (@storage[key] || [] ).size end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/hive/mocks/storage.rb', line 13 def to_s "#{self.class.name}()" end |