Class: KyotoTycoon::Cursor
- Inherits:
-
Object
- Object
- KyotoTycoon::Cursor
- Includes:
- Enumerable
- Defined in:
- lib/kyototycoon/cursor.rb
Instance Attribute Summary collapse
-
#cur ⇒ Object
readonly
Returns the value of attribute cur.
Instance Method Summary collapse
- #current(step = nil) ⇒ Object
- #delete! ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(kt, cur) ⇒ Cursor
constructor
A new instance of Cursor.
- #jump(key = nil) ⇒ Object
- #jump_back(key = nil) ⇒ Object
- #key(step = nil) ⇒ Object
- #remove ⇒ Object
- #seize ⇒ Object
- #step ⇒ Object (also: #next)
- #step_back ⇒ Object (also: #prev)
- #value(step = nil) ⇒ Object
- #value=(value, xt = nil, step = nil) ⇒ Object
Constructor Details
#initialize(kt, cur) ⇒ Cursor
Returns a new instance of Cursor.
8 9 10 11 12 |
# File 'lib/kyototycoon/cursor.rb', line 8 def initialize(kt, cur) @kt = kt @cur = cur ObjectSpace.define_finalizer(self){delete!} end |
Instance Attribute Details
#cur ⇒ Object (readonly)
Returns the value of attribute cur.
6 7 8 |
# File 'lib/kyototycoon/cursor.rb', line 6 def cur @cur end |
Instance Method Details
#current(step = nil) ⇒ Object
69 70 71 72 |
# File 'lib/kyototycoon/cursor.rb', line 69 def current(step=nil) res = request('/rpc/cur_get', {"step" => step}) [res["key"], res["value"]] end |
#delete! ⇒ Object
83 84 85 |
# File 'lib/kyototycoon/cursor.rb', line 83 def delete! request('/rpc/cur_delete') end |
#each(&block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/kyototycoon/cursor.rb', line 14 def each(&block) return to_enum(:each) unless block_given? jump if current == [nil,nil] start_key = key begin @kt.logger.debug("cursor each start with key=#{start_key}") loop do tmp = current(1) @kt.logger.debug("cursor each key=#{tmp.first}") break if tmp == [nil,nil] block.call(tmp) end ensure jump(start_key) end end |
#jump(key = nil) ⇒ Object
31 32 33 34 |
# File 'lib/kyototycoon/cursor.rb', line 31 def jump(key=nil) request('/rpc/cur_jump',{:key => key}) self end |
#jump_back(key = nil) ⇒ Object
36 37 38 39 |
# File 'lib/kyototycoon/cursor.rb', line 36 def jump_back(key=nil) request('/rpc/cur_jump_back',{:key => key}) self end |
#key(step = nil) ⇒ Object
65 66 67 |
# File 'lib/kyototycoon/cursor.rb', line 65 def key(step=nil) request('/rpc/cur_get_key', {"step" => step})["key"] end |
#remove ⇒ Object
79 80 81 |
# File 'lib/kyototycoon/cursor.rb', line 79 def remove request('/rpc/cur_remove') end |
#seize ⇒ Object
74 75 76 77 |
# File 'lib/kyototycoon/cursor.rb', line 74 def seize res = request('/rpc/cur_seize') [res["key"], res["value"], (res["xt"].nil? ? nil : Time.at(res["xt"].to_i) )] end |
#step ⇒ Object Also known as: next
41 42 43 44 |
# File 'lib/kyototycoon/cursor.rb', line 41 def step request('/rpc/cur_step') self end |
#step_back ⇒ Object Also known as: prev
47 48 49 50 |
# File 'lib/kyototycoon/cursor.rb', line 47 def step_back request('/rpc/cur_step_back') self end |
#value(step = nil) ⇒ Object
53 54 55 |
# File 'lib/kyototycoon/cursor.rb', line 53 def value(step=nil) request('/rpc/cur_get_value', {"step" => step})["value"] end |
#value=(value, xt = nil, step = nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/kyototycoon/cursor.rb', line 57 def value=(value, xt=nil, step=nil) request('/rpc/cur_set_value', { "value" => value, "xt" => xt, "step" => step, }) end |