Skip to content

Commit

Permalink
Allow mass-overwriting of index entries while preserving expected int…
Browse files Browse the repository at this point in the history
…ernal representation. Ref #17
  • Loading branch information
Sean Cribbs committed Apr 4, 2012
1 parent 86a3e66 commit dc96d6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/riak/robject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,17 @@ def self.load_from_mapreduce(client, response)
def initialize(bucket, key=nil)
@bucket, @key = bucket, key
@links, @meta = Set.new, {}
@indexes = Hash.new {|h,k| h[k] = Set.new }
@indexes = new_index_hash
yield self if block_given?
end

def indexes=(hash)
@indexes = hash.inject(new_index_hash) do |h, (k,v)|
h[k].merge([*v])
h
end
end

# Load object data from a map/reduce response item.
# This method is used by RObject::load_from_mapreduce to instantiate the necessary
# objects.
Expand Down Expand Up @@ -312,5 +319,9 @@ def extract_if_present(hash, key, attribute=nil)
send("#{attribute}=", value)
end
end

def new_index_hash
Hash.new {|h,k| h[k] = Set.new }
end
end
end
8 changes: 8 additions & 0 deletions spec/riak/robject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@
@object.links.length.should == 1
end

it "should allow mass-overwriting indexes while preserving default behavior" do
@object = described_class.new(@bucket, 'foo')
@object.indexes = {"ts_int" => [12345], "foo_bin" => "bar"}
@object.indexes['ts_int'].should == Set.new([12345])
@object.indexes['foo_bin'].should == Set.new(["bar"])
@object.indexes['unset_bin'].should == Set.new
end

describe "when storing the object normally" do
before :each do
@backend = mock("Backend")
Expand Down
7 changes: 7 additions & 0 deletions spec/support/unified_backend_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@
end
end

it "should store an object with indexes", :version => "1.0.0" do
@robject.indexes['foo_bin'] << 'bar'
@backend.store_object(@robject, :returnbody => true)
@robject.indexes.should include('foo_bin')
@robject.indexes['foo_bin'].should include('bar')
end

after do
expect { @backend.fetch_object("test", "store") }.should_not raise_error(Riak::FailedRequest)
end
Expand Down

0 comments on commit dc96d6c

Please sign in to comment.