Class: Drip3
Defined Under Namespace
Classes: Renewer
Constant Summary collapse
- INF =
1.0/0.0
Class Method Summary collapse
Instance Method Summary collapse
- #fetch(key) ⇒ Object (also: #[])
- #head(n = 1, tag = nil) ⇒ Object
-
#initialize(dir) ⇒ Drip3
constructor
A new instance of Drip3.
- #inspect ⇒ Object
- #key_to_time(key) ⇒ Object
- #latest?(key, tag = nil) ⇒ Boolean
- #newer(key, tag = nil) ⇒ Object
- #older(key, tag = nil) ⇒ Object
- #read(key, n = 1, at_least = 1, timeout = nil) ⇒ Object
- #read_tag(key, tag, n = 1, at_least = 1, timeout = nil) ⇒ Object
- #tag_next(tag) ⇒ Object
- #tag_prev(tag) ⇒ Object
- #time_to_key(time) ⇒ Object
- #write(obj, tag = nil) ⇒ Object
- #write_after(at, obj, tag = nil) ⇒ Object
- #write_at(at, obj, tag = nil) ⇒ Object
- #write_if_latest(cond, obj, tag = nil) ⇒ Object
Constructor Details
#initialize(dir) ⇒ Drip3
Returns a new instance of Drip3.
32 33 34 35 36 |
# File 'lib/drip3.rb', line 32 def initialize(dir) @event = Rinda::TupleSpace.new(5) @event.write([:last, 1]) setup_db(dir) end |
Class Method Details
.key_to_time(key) ⇒ Object
46 47 48 |
# File 'lib/drip3.rb', line 46 def self.key_to_time(key) Time.at(*key.divmod(1000000)) end |
.time_to_key(time) ⇒ Object
38 39 40 |
# File 'lib/drip3.rb', line 38 def self.time_to_key(time) time.tv_sec * 1000000 + time.tv_usec end |
Instance Method Details
#fetch(key) ⇒ Object Also known as: []
76 77 78 |
# File 'lib/drip3.rb', line 76 def fetch(key) do_fetch(key) end |
#head(n = 1, tag = nil) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/drip3.rb', line 81 def head(n=1, tag=nil) if tag do_head_tag(n, tag) else do_head(n) end end |
#inspect ⇒ Object
30 |
# File 'lib/drip3.rb', line 30 def inspect; to_s; end |
#key_to_time(key) ⇒ Object
50 51 52 |
# File 'lib/drip3.rb', line 50 def key_to_time(key) self.class.key_to_time(key) end |
#latest?(key, tag = nil) ⇒ Boolean
122 123 124 |
# File 'lib/drip3.rb', line 122 def latest?(key, tag=nil) do_latest(key, tag) end |
#newer(key, tag = nil) ⇒ Object
130 131 132 133 |
# File 'lib/drip3.rb', line 130 def newer(key, tag=nil) return read(key, 1, 0)[0] unless tag read_tag(key, tag, 1, 0)[0] end |
#older(key, tag = nil) ⇒ Object
126 127 128 |
# File 'lib/drip3.rb', line 126 def older(key, tag=nil) do_older(key, tag) end |
#read(key, n = 1, at_least = 1, timeout = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/drip3.rb', line 89 def read(key, n=1, at_least=1, timeout=nil) key = time_to_key(Time.now) unless key ary = do_read(key, n) while ary.size < at_least key = ary[-1][0] unless ary.empty? begin renewer = make_renewer(timeout) wait(key, renewer) rescue Rinda::RequestExpiredError return ary end ary += do_read(key, n - ary.size) end ary end |
#read_tag(key, tag, n = 1, at_least = 1, timeout = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/drip3.rb', line 105 def read_tag(key, tag, n=1, at_least=1, timeout=nil) at_least = n if n < at_least key = time_to_key(Time.now) unless key ary = do_read_tag(key, tag, n) while ary.size < at_least key = ary[-1][0] unless ary.empty? begin renewer = make_renewer(timeout) wait_tag(key, tag, renewer) rescue Rinda::RequestExpiredError return ary end ary += do_read_tag(key, tag, n - ary.size) end ary end |
#tag_next(tag) ⇒ Object
135 136 137 |
# File 'lib/drip3.rb', line 135 def tag_next(tag) do_tag_next(tag) end |
#tag_prev(tag) ⇒ Object
139 140 141 |
# File 'lib/drip3.rb', line 139 def tag_prev(tag) do_tag_prev(tag) end |
#time_to_key(time) ⇒ Object
42 43 44 |
# File 'lib/drip3.rb', line 42 def time_to_key(time) self.class.time_to_key(time) end |
#write(obj, tag = nil) ⇒ Object
54 55 56 |
# File 'lib/drip3.rb', line 54 def write(obj, tag=nil) write_after(Time.now, obj, tag) end |
#write_after(at, obj, tag = nil) ⇒ Object
58 59 60 61 62 |
# File 'lib/drip3.rb', line 58 def write_after(at, obj, tag=nil) make_key(at) do |key| do_write(key, obj, tag) end end |
#write_at(at, obj, tag = nil) ⇒ Object
64 65 66 67 68 |
# File 'lib/drip3.rb', line 64 def write_at(at, obj, tag=nil) make_key_at(at) do |key| do_write(key, obj, tag) end end |
#write_if_latest(cond, obj, tag = nil) ⇒ Object
70 71 72 73 74 |
# File 'lib/drip3.rb', line 70 def write_if_latest(cond, obj, tag=nil) make_key(Time.now) do |key| do_write(key, obj, tag, cond) end end |