Skip to content
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

Support atomic updates for Composite ID #970

Merged
merged 5 commits into from
Mar 10, 2020
Merged

Support atomic updates for Composite ID #970

merged 5 commits into from
Mar 10, 2020

Conversation

shatalov-boris
Copy link
Contributor

@serggl PR for #958

About atomic updates:
So, atomic_update and atomic_update! now accept instance as key to handle id_prefix.
If id_prefix defined as String - atomic_update can be used with ID as the key without any problem.
Also, I added the warning for situations when id_prefix is Block or Symbol and atomic_update is used with ID as key.

About remove by id:
The only thing I changed in remove_by_id method is adding support for id_prefix defined as String because we can fetch it on a class level.
Also, I added a similar warning for id_prefix defined as Block or Symbol.

All of the above changes are made for sunspot gem.
But there is also one small change for sunspot_rails gem:
I modified solr_atomic_update and solr_atomic_update! methods to always pass instance as the key instead of ID. These methods stand for the atomic update on the instance level, so this change is required to handle id_prefix:

post1.atomic_update body: 'A New Body', title: 'Another New Title'

Sunspot:
Allow to pass instance to `atomic_update` method.
Show warning if `id_prefix` is defined but ID provided as key to `atomic_update`. Also, add this information to README.
Add specs for different `id_prefix` definition.
Add specs for new method of Sunspot::Setup.

Sunspot Rails:
Pass instance instead of ID on the instance level `atomic_update`
Do not show warning for `id_prefix` defined as String.
Add specs.
@@ -40,6 +40,12 @@ module Sunspot
NoSetupError = Class.new(StandardError)
IllegalSearchError = Class.new(StandardError)
NotImplementedError = Class.new(StandardError)
AtomicUpdateWarningMessage = lambda do |class_name|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have more explicit Exception class name here? 'Warning' s a too common verb. Must be something related to composite ids IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serggl I've changed both names to more reasonable ones. I just wanted to avoid using too long name :)

AtomicUpdateWarningMessage = lambda do |class_name|
"WARNING: `id_prefix` is defined for #{class_name}. Use instance as key for `atomic_update` instead of ID."
end
RemoveByIdWarningMessage = lambda do |class_name|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same suggestion as above also goes here

#
# ==== Returns
#
# String:: value for `id_prefix`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I correctly assume this can also result in Symbol?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serggl No, it's only for String. Symbol also requires an instance because it's actually a method call. I updated the documentation.

else
id_prefix = clazz_setup.id_prefix_for_class
instance_id = key.respond_to?(:id) ? key.id : key
id = Adapters::InstanceAdapter.index_id_for("#{id_prefix}#{clazz.name}", instance_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move id definition out of the if block? I think it would be more clear if return value of the conditional block would be assigned to its value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serggl changed it

@@ -54,9 +54,19 @@ def remove(*models)
# Remove the model from the Solr index by specifying the class and ID
#
def remove_by_id(class_name, *ids)
id_prefix = nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the value of this var should better be assigned below, with a return value of if conditional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serggl this one is fixed too

@serggl
Copy link
Collaborator

serggl commented Mar 9, 2020

@heaven do you mind also reviewing this?

Copy link
Collaborator

@serggl serggl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me 👍. But I would like some other 👀 to review this as well, so let it sit open for some time

@heaven
Copy link
Contributor

heaven commented Mar 10, 2020

@serggl sure, will do.

Copy link
Contributor

@heaven heaven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@serggl serggl merged commit aa236c1 into sunspot:master Mar 10, 2020
@shatalov-boris shatalov-boris deleted the support-atomic-updates-for-composite-id branch March 11, 2020 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants