-
Notifications
You must be signed in to change notification settings - Fork 2
Support quorum options #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,27 +69,37 @@ def update(attributes) | |
| # u = User.new(:name => "John").save | ||
| # # => #<User:6kS5VHNbaed9h7gFLnVg5lmO4U7 {:name=>"John"}> | ||
| # | ||
| def save | ||
| def save(options = {}) | ||
| quorum_types = [:r, :w, :dw] | ||
| quorum = options.fetch(:quorum, {}) | ||
|
|
||
| unless quorum.empty? | ||
| raise ArgumentError.new("invalid quorum option") unless quorum.keys.select { |key| !quorum_types.include?(key) }.empty? | ||
| end | ||
|
|
||
| __robject.content_type = model.content_type | ||
| __robject.data = __persist_attributes | ||
|
|
||
| __check_unique_indices | ||
| __update_indices | ||
| __robject.store | ||
| __robject.store(quorum) | ||
|
|
||
| @id = __robject.key | ||
|
|
||
| self | ||
| end | ||
|
|
||
| # Preload all the attributes of this model from Riak. | ||
| def reload | ||
| new? ? self : self.load!(@id) | ||
| def reload(options = {}) | ||
| new? ? self : self.load!(@id, options) | ||
| end | ||
|
|
||
| # Delete the model | ||
| def delete | ||
| __robject.delete unless new? | ||
| def delete(options = {}) | ||
| opts = {} | ||
| opts.merge!(rw: options[:quorum].to_i) if options[:quorum] | ||
|
|
||
| __robject.delete(opts) unless new? | ||
| freeze | ||
| rescue Riak::FailedRequest | ||
| false | ||
|
|
@@ -99,9 +109,13 @@ def delete | |
|
|
||
| # Overwrite attributes with the persisted attributes in Riak. | ||
| # | ||
| def load!(id) | ||
| def load!(id, options = {}) | ||
| opts = {} | ||
| opts.merge!(force: true) if options[:force] | ||
| opts.merge!(r: options[:quorum].to_i) if options[:quorum] | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing whitespace detected. |
||
| self.__robject.key = id | ||
| __load_robject! id, @__robject.reload(force: true) | ||
| __load_robject! id, @__robject.reload(opts) | ||
| end | ||
|
|
||
| # Transform a RObject returned by Riak into a Ork::Document. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,17 @@ class Human | |
| test 'return nil when the id does not belong to an object of this bucket' do | ||
| assert_equal nil, Human['not_an_id'] | ||
| end | ||
|
|
||
| test 'retrieve an object with given quorum' do | ||
| quorum = 1 | ||
| robject = Human.bucket.new | ||
| Human.bucket.stubs(:new).returns(robject) | ||
| robject.expects(:reload).with(force: true, r: quorum).returns(@human1.send(:__robject)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [93/80] |
||
|
|
||
| Human[@human1.id, quorum: quorum] | ||
|
|
||
| Human.bucket.unstub(:new) | ||
| end | ||
| end | ||
|
|
||
| context '*exist?*' do | ||
|
|
@@ -45,6 +56,13 @@ class Human | |
| assert !Human.exist?('not_an_id') | ||
| assert Human.exist?(@human1.id) | ||
| end | ||
|
|
||
| test 'check existence with given quorum' do | ||
| quorum = 1 | ||
| Human.bucket.expects(:exists?).once.with(@human1.id, r: quorum).returns(true) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [83/80] |
||
|
|
||
| assert Human.exist?(@human1.id, quorum: quorum) | ||
| end | ||
| end | ||
|
|
||
| context '*all*' do | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [127/80]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good practice is to create a specific error like
InvalidQuorumOption < ArgumentErrorAlso, if you have to map/select a collection, please use a different variable.