Navigation Menu

Skip to content

Commit

Permalink
Changed how siblings are handled in generate_from_map_reduce.
Browse files Browse the repository at this point in the history
  • Loading branch information
misaka committed Jul 18, 2010
1 parent 2786112 commit ef43750
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
33 changes: 20 additions & 13 deletions riak-client/lib/riak/robject.rb
Expand Up @@ -38,7 +38,7 @@ class RObject
# @return [Object] the data stored in Riak at this object's key. Varies in format by content-type, defaulting to String from the response body.
attr_accessor :data

# @return [Set<Link>] an Set of {Riak::Link} objects for relationships between this object and other resources
# @return [Set<Link>] a Set of {Riak::Link} objects for relationships between this object and other resources
attr_accessor :links

# @return [String] the ETag header from the most recent HTTP response, useful for caching and reloading
Expand All @@ -50,6 +50,12 @@ class RObject
# @return [Hash] a hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion
attr_accessor :meta

# @return [Boolean] a flag indicating whether this object has conflicting sibling objects
attr_accessor :conflict

# @param [Array] siblings a list of RObjects that are a sibling to this one
attr_writer :siblings

# Create a new object from the response we get from map/reduce.
# @param [Client] client an active client object to contact the server with
# @param [Array] map_response the response given us from a map operation
Expand All @@ -68,18 +74,20 @@ class RObject
def self.generate_from_map_reduce(client,response)
vclock = response[0]['vclock'] if response[0]['vclock'].present?

if response[0]['values'].length == 1
robj = new(client.bucket(response[0]['bucket']), response[0]['key'])
robj.vclock = vclock
robj.load_from_map_reduce(response[0]['values'][0])
[ robj ]
else
response[0]['values'].map do |values|
robj = new(client.bucket(response[0]['bucket']), response[0]['key'])
robj.vclock = vclock
robj.load_from_map_reduce(values,true)
robj = new(client.bucket(response[0]['bucket']), response[0]['key'])
robj.vclock = vclock
robj.load_from_map_reduce(response[0]['values'][0])

if response[0]['values'].length >= 1
robj.conflict = true
robj.siblings = response[0]['values'][1..-1].map do |values|
sibling = new(client.bucket(response[0]['bucket']), response[0]['key'])
sibling.vclock = vclock
sibling.load_from_map_reduce(values)
end
end

robj
end

# Create a new object manually
Expand Down Expand Up @@ -130,7 +138,7 @@ def load(response)
# "X-Riak-Meta"=>{"X-Riak-Meta-King-Of-Robots"=>"I"}},
# "data"=>
# "{\"email\":\"misaka@pobox.com\",\"confirmed\":true,\"_type\":\"User\"}"}
def load_from_map_reduce(response, conflict=nil)
def load_from_map_reduce(response)
metadata = response['metadata']
extract_if_present(metadata, 'X-Riak-VTag', :etag)
extract_if_present(metadata, 'content-type', :content_type)
Expand All @@ -146,7 +154,6 @@ def load_from_map_reduce(response, conflict=nil)
]
end
extract_if_present(response, 'data', :data) { |v| deserialize(v) }
@conflict = conflict
self
end

Expand Down
34 changes: 16 additions & 18 deletions riak-client/spec/riak/object_spec.rb
Expand Up @@ -233,12 +233,12 @@
end

it "should load the content type" do
@object = Riak::RObject.generate_from_map_reduce(@client,@sample_response).first
@object = Riak::RObject.generate_from_map_reduce(@client,@sample_response)
@object.content_type.should == "application/json"
end

it "should load the body data" do
@object = Riak::RObject.generate_from_map_reduce(@client, @sample_response).first
@object = Riak::RObject.generate_from_map_reduce(@client, @sample_response)
@object.data.should be_present
end

Expand All @@ -249,43 +249,43 @@
and_return({"email" => "mail@test.com", "_type" => "User"})
Riak::RObject.stub!( :new ).and_return( new_robj )

@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response ).first
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response )
@object.data.should == {"email" => "mail@test.com", "_type" => "User"}
end

it "should set the vclock" do
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response ).first
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response )
@object.vclock.should == "a85hYGBgzmDKBVIsCfs+fc9gSmTMY2WQYN9wlA8q/HvGVn+osCKScFV3/hKosDpIOAsA"
end

it "should load and parse links" do
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response ).first
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response )
@object.links.should have(1).item
@object.links.first.url.should == "/riak/addresses/A2cbUQ2KEMbeyWGtdz97LoTi1DN"
@object.links.first.rel.should == "home_address"
end

it "should set the ETag" do
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response ).first
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response )
@object.etag.should == "5bnavU3rrubcxLI8EvFXhB"
end

it "should set modified date" do
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response ).first
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response )
@object.last_modified.to_i.should == Time.httpdate("Mon, 12 Jul 2010 21:37:43 GMT").to_i
end

it "should load meta information" do
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response ).first
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response )
@object.meta["King-Of-Robots"].should == ["I"]
end

it "should set the key" do
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response ).first
@object = Riak::RObject.generate_from_map_reduce( @client, @sample_response )
@object.key.should == "A2IbUQ2KEMbe4WGtdL97LoTi1DN"
end

it "should return multiple objects when there are multiple values" do
it "should add siblings when there are multiple values" do
response = @sample_response
response[0]['values'] << {
"metadata"=> {
Expand All @@ -297,13 +297,12 @@
},
"data"=> "{\"email\":\"mail@domain.com\",\"_type\":\"User\"}"
}
@objects = Riak::RObject.generate_from_map_reduce( @client, response )
@objects.length.should == 2
@objects[0].etag.should == "5bnavU3rrubcxLI8EvFXhB"
@objects[1].etag.should == "7jDZLdu0fIj2iRsjGD8qq8"
@object = Riak::RObject.generate_from_map_reduce( @client, response )
@object.siblings.length.should == 1
@object.siblings[0].etag.should == "7jDZLdu0fIj2iRsjGD8qq8"
end

it "should return conflicted objects when there are multiple values" do
it "should return a conflicted object when there are multiple values" do
response = @sample_response
response[0]['values'] << {
"metadata"=> {
Expand All @@ -315,9 +314,8 @@
},
"data"=> "{\"email\":\"mail@domain.com\",\"_type\":\"User\"}"
}
@objects = Riak::RObject.generate_from_map_reduce( @client, response )
@objects[0].should be_conflict
@objects[1].should be_conflict
@object = Riak::RObject.generate_from_map_reduce( @client, response )
@object.should be_conflict
end

end
Expand Down

0 comments on commit ef43750

Please sign in to comment.