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

API: Fix retainAll and removeAll in CharSequenceSet #7133

Merged
merged 4 commits into from
Mar 22, 2023

Conversation

zhongyujiang
Copy link
Contributor

Found this while browsing the code, and checked that this method does not seem to be used in the project at present, luckily.

@github-actions github-actions bot added the API label Mar 17, 2023
Copy link
Contributor

@amogh-jahagirdar amogh-jahagirdar left a comment

Choose a reason for hiding this comment

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

@zhongyujiang great catch, this looks good to me. Can you add a test to TestCharSequenceSet? cc @rdblue @jackye1995

@zhongyujiang
Copy link
Contributor Author

zhongyujiang commented Mar 18, 2023

@amogh-jahagirdar Thanks for your comment, when adding tests I found that currently retainAll can not work properly. The reason is that Iterables#retainAll will test whether each element in wrapperSet exists in toRetain set and retain an element when toRetain#contains returns true. However, wrapperSet is a Set<CharSequenceWrapper> and what we want to retain should be a collection of CharSequence. In such cases toRetain#contains will always return false because their classes are different. The same happens in removeAll.

In the new commit, the passed in CharSequence objects will be converted to CharSequenceWrapper and then for comparison, thus fixes this problem.

@zhongyujiang zhongyujiang changed the title API: Fix CharSequenceSet#retainAll. API: Fix retainAll and removeAll in CharSequenceSet Mar 18, 2023
@zhongyujiang
Copy link
Contributor Author

The failed task is iceberg-spark:iceberg-spark-3.2_2.12:checkstyleTest, not relevant to this.


Assert.assertTrue("Set should be changed", set.retainAll(ImmutableList.of("456", "789", 123)));
Assert.assertTrue("Should not retain element \"123\"", set.size() == 1);
Assert.assertTrue("Should retain element \"456\"", set.contains("456"));
Copy link
Contributor

Choose a reason for hiding this comment

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

let's rewrite L46+L47 to Assertions.assertThat(set).hasSize(1).contains("456"). Same for all the places below.

Also let's try and avoid assertTrue / assertFalse as much as possible as it doesn't provide enough context if a check ever fails (See #7131 for some background)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, also changed L45 and other boolean assertions, this uses overridingErrorMessage to provide error message and thefore increases the number of lines, but allows us not to mix Assert and Assertions. Do you think it's OK?

Copy link
Contributor

Choose a reason for hiding this comment

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

yep that looks good, thanks

public boolean removeAll(Collection<?> objects) {
if (objects != null) {
return Iterables.removeAll(wrapperSet, objects);
return objects.stream().filter(this::remove).count() != 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

why not do the same as in L142ff?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removeAll iterates the objects and checks whether these elements exist in the removeFrom set, in this case we don't need to wrap all elements at once, use this#remove can take advantage of the wrapper in the ThreadLocal wrappers.

return false;
}

@Override
@SuppressWarnings("CollectionUndefinedEquality")
Copy link
Contributor

Choose a reason for hiding this comment

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

is this one necessary? I don't see errorprone complaining about it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It complained in the previous CI.

Copy link
Contributor

@nastra nastra left a comment

Choose a reason for hiding this comment

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

LGTM, @jackye1995 could you also take a look please?

Copy link
Contributor

@jackye1995 jackye1995 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! thanks for the fix!

@jackye1995 jackye1995 merged commit 793db20 into apache:master Mar 22, 2023
@zhongyujiang zhongyujiang deleted the fix_CharSequenceSet branch March 23, 2023 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants