Class: Blodsband::Riak::List::Element
- Inherits:
-
Object
- Object
- Blodsband::Riak::List::Element
- Defined in:
- lib/blodsband/riak/list.rb
Overview
The list element.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
The key in Riak for this element.
-
#list ⇒ Object
readonly
The Blodsband::Riak::List this element belongs to.
Class Method Summary collapse
-
.create(list, value) ⇒ Blodsband::Riak::Element
Create an element.
-
.find(list, key) ⇒ Blodsband::Riak::Element
Find an element.
Instance Method Summary collapse
-
#append(value) ⇒ Object
Append a value after this element in the list.
-
#delete ⇒ Object
Delete this element.
-
#exists? ⇒ true, false
Whether the element exists.
-
#next ⇒ String
The key to the next element in the list.
-
#next_element ⇒ Blodsband::Riak::List::Element
The next element or nil.
-
#prepend(value) ⇒ Object
Prepend a value before this element in the list.
-
#previous ⇒ String
The key to the previous element in the list.
-
#previous_element ⇒ Blodsband::Riak::List::Element
The previous element or nil.
-
#reload ⇒ Object
Make sure this element is reloaded from Riak.
-
#save ⇒ Object
Save this element.
- #set_pointers(list_version, previous_key, next_key) ⇒ Object
-
#to_s ⇒ String
A String representation of the element.
-
#value ⇒ Object
The value of this element.
-
#value=(o) ⇒ Object
The new value.
Instance Attribute Details
#key ⇒ Object (readonly)
The key in Riak for this element.
49 50 51 |
# File 'lib/blodsband/riak/list.rb', line 49 def key @key end |
#list ⇒ Object (readonly)
The Blodsband::Riak::List this element belongs to.
53 54 55 |
# File 'lib/blodsband/riak/list.rb', line 53 def list @list end |
Class Method Details
.create(list, value) ⇒ Blodsband::Riak::Element
Create an element.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/blodsband/riak/list.rb', line 82 def self.create(list, value) rval = Element.new rval.instance_eval do @list = list @key = rand(1 << 256).to_s(36) @value = value class << @value include Response end @value.instance_eval do @meta = (@meta || {}).merge("list-key" => list.key) end end rval.save rval end |
Instance Method Details
#append(value) ⇒ Object
Append a value after this element in the list.
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/blodsband/riak/list.rb', line 193 def append(value) if exists? while !list.append_element_after(self, Element.create(list, value)) list.reload reload raise ActorDeletedError.new(self, to_s) unless exists? raise ActorDeletedError.new(list, list.to_s) unless list.exists? end else raise ActorDeletedError.new(self, to_s) end end |
#delete ⇒ Object
Delete this element.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/blodsband/riak/list.rb', line 175 def delete if exists? while !list.delete_element(self) list.reload reload break unless exists? raise ActorDeletedError.new(list, list.to_s) unless list.exists? end end end |
#exists? ⇒ true, false
Returns whether the element exists.
102 103 104 105 |
# File 'lib/blodsband/riak/list.rb', line 102 def exists? reload !key.nil? && !value.nil? end |
#next ⇒ String
Returns the key to the next element in the list.
237 238 239 |
# File 'lib/blodsband/riak/list.rb', line 237 def next value.["list-next"] end |
#next_element ⇒ Blodsband::Riak::List::Element
Returns the next element or nil.
246 247 248 |
# File 'lib/blodsband/riak/list.rb', line 246 def next_element Element.find(list, self.next) end |
#prepend(value) ⇒ Object
Prepend a value before this element in the list.
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/blodsband/riak/list.rb', line 159 def prepend(value) if exists? while !list.prepend_element_before(self, Element.create(list, value)) list.reload reload raise ActorDeletedError.new(self, to_s) unless exists? raise ActorDeletedError.new(list, list.to_s) unless list.exists? end else raise ActorDeletedError.new(self, to_s) end end |
#previous ⇒ String
Returns the key to the previous element in the list.
255 256 257 |
# File 'lib/blodsband/riak/list.rb', line 255 def previous value.["list-previous"] end |
#previous_element ⇒ Blodsband::Riak::List::Element
Returns the previous element or nil.
264 265 266 |
# File 'lib/blodsband/riak/list.rb', line 264 def previous_element Element.find(list, self.previous) end |
#reload ⇒ Object
Make sure this element is reloaded from Riak.
228 229 230 |
# File 'lib/blodsband/riak/list.rb', line 228 def reload @value = nil end |
#save ⇒ Object
Save this element.
213 214 215 216 217 218 219 220 221 |
# File 'lib/blodsband/riak/list.rb', line 213 def save curr = @value @value = list.bucket.cas(key, value, value.vclock) if @value.nil? raise ConcurrentUpdateError.new(self, to_s) else #STDERR.puts("#{$$} succeeded saving #{curr} (#{curr.meta})") end end |
#set_pointers(list_version, previous_key, next_key) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/blodsband/riak/list.rb', line 271 def set_pointers(list_version, previous_key, next_key) begin list_doc = list.bucket.get(list.key) if list_doc.nil? || list_doc["version"] == list_version if previous_key.nil? value..delete("list-previous") elsif previous_key != ":unchanged" value.["list-previous"] = previous_key end if next_key.nil? value..delete("list-next") elsif next_key != ":unchanged" value.["list-next"] = next_key end save end rescue ConcurrentUpdateError => e retry if exists? end end |
#to_s ⇒ String
Returns a String representation of the element.
110 111 112 113 114 115 116 |
# File 'lib/blodsband/riak/list.rb', line 110 def to_s if exists? "#{self.previous} => #{self.class}:#{key}@#{list.key} #{value.inspect} => #{self.next}" else "#{self.class}:#{key}@#{list.key}" end end |
#value ⇒ Object
Returns the value of this element.
148 149 150 |
# File 'lib/blodsband/riak/list.rb', line 148 def value @value ||= list.bucket.get(key, :unique => true) end |
#value=(o) ⇒ Object
Does not lock the parent list, since changing the element value is independent of structural list changes. Instead both this and structural list changes just retry on ConcurrentUpdateError.
Returns the new value.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/blodsband/riak/list.rb', line 127 def value=(o) begin old_value = value @value = value.copy_to(o) value.instance_eval do @meta = old_value. end save value rescue ConcurrentUpdateError => e if !exists? raise ActorDeletedError.new(self, to_s) else retry end end end |