-
Notifications
You must be signed in to change notification settings - Fork 685
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
SOLR-17623: Simple ordered map should implement map #3048
SOLR-17623: Simple ordered map should implement map #3048
Conversation
Adding unit test for newly added method
Nitpicky, but can you title the PR |
@dsmiley Could you please have a look at the implementation oaf the method from the Map interface? |
I am curious if there is a way to implement values() and entrySet(0 without an unchecked cast, e.g. (T) nvPairs.get(i) |
As the PR template indicates, the PR title must use exactly the JIRA syntax. Thus all-caps and hyphen separating SOLR and 17623, probably then a colon. If you do it correctly, moments later a bot will update the JIRA to link to the PR. (albeit sometimes the bot is delayed inexplicably) |
solr/solrj/src/java/org/apache/solr/common/util/SimpleOrderedMap.java
Outdated
Show resolved
Hide resolved
solr/solrj/src/java/org/apache/solr/common/util/SimpleOrderedMap.java
Outdated
Show resolved
Hide resolved
solr/solrj/src/java/org/apache/solr/common/util/SimpleOrderedMap.java
Outdated
Show resolved
Hide resolved
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class SimpleOrderedMapTest extends Assert { |
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.
No; in Solr we extend SolrTestCase at a minimum for a unit test
solr/solrj/src/test/org/apache/solr/common/util/SimpleOrderedMapTest.java
Show resolved
Hide resolved
Almost nobody could answer a cast question like that without literally working on it in their IDE. I'll take a look at a future iteration of yours but meanwhile, don't worry about it -- it's not important. |
@dsmiley Considering your improvements on NamedList..asShallowMap, could I just delegate the method keySet, values and entrySet to the shallowMap e.g. : public Collection values() { |
I would implement the methods here, copying bits of code from #3050 if needed so that you don't call NamedList.asShallowMap. Take from that whatever you want. I'm not sure NamedList.asShallowMap was a wise idea since it can't implement the Map contract if the keys repeat. It might have pre-dated SimpleOrderedMap. Assuming you finish the PR here, I'm inclined to maybe make NamedList.asShallowMap do an instanceof check for SimpleOrderedMap and if so return "this" but otherwise throw an exception. Or remove it. Ideally we only use NamedList that isn't SimpleOrderedMap for the rare cases where it's appropriate. |
…Set/AbstractMap without creating new datastructure
@dsmiley Please have another look, thanks! |
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.
As part of this PR, let's have NamedList.asShallowMap do an instanceof check for Map and return it if so. That will lead to real usage / code coverage of the changes you've done.
} | ||
|
||
/** | ||
* Returns a shared, empty, and immutable instance of SimpleOrderedMap. | ||
* Returns {@code true} if this map maps one or more keys to the specified value. |
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.
"one or more" -- why more? Any way, don't feel obliged to re-specify javadocs for interface implementations since it's automatically inherited.
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.
the java-doc is automatically inherited? isn't that only the case if I use the {@inheritdoc} tag in the java doc itself. And even then, it does not inherit the param, return and throw part of the java-doc, unless I tag them one by one. Nevertheless, I will move to {@inheritdoc} where possible, in order not to repeat java doc.
With respect to "one or more", I did not come up with that phrasing, that is on Map#contains, and although it sounds strange, it is technically correct.
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.
If we don't have anything extra to say beyond the default javadoc, then add nothing at all (no javadoc). If you want to add a tidbit of extra info to say something O(N) then you can use {@inheritDoc}
and add the sentence. But I think you could just augment the class level javadocs to warn users about usage of this as a general Map for large maps.
return EMPTY; | ||
@Override | ||
public boolean containsValue(final Object value) { | ||
int sz = size(); |
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.
instead of all these lines, could be a one-liner values().contains(value). It'll be rare if ever for any Solr code to call this.
} | ||
|
||
/** | ||
* Returns a shared, empty, and immutable instance of SimpleOrderedMap. | ||
* Returns {@code true} if this map maps one or more keys to the specified value. |
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.
If we don't have anything extra to say beyond the default javadoc, then add nothing at all (no javadoc). If you want to add a tidbit of extra info to say something O(N) then you can use {@inheritDoc}
and add the sentence. But I think you could just augment the class level javadocs to warn users about usage of this as a general Map for large maps.
solr/solrj/src/test/org/apache/solr/common/util/SimpleOrderedMapTest.java
Show resolved
Hide resolved
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.
Looks good; thank you! Just needs a CHANGES.txt entry.
My hope is for NamedList.asShallowMap's implementation to go away, ultimately mandating that if you want a Map, you must have a SimpleOrderedMap. That can be a separate issue/PR.
solr/solrj/src/java/org/apache/solr/common/util/SimpleOrderedMap.java
Outdated
Show resolved
Hide resolved
Wouldn't it be a better approach just to override asShallowMap in SimpleOrderedMap and return itself? |
:face-palm: of course; wow I have no excuse for myself. Not enough coffee maybe.
Separately in another issue/pr, NamedList needs some more attention around this and toMap. Definitely deprecating that one. |
…t.asShallowMap(boolean) private
I interpreted you thumbs up on my comment early as OK for both of the issue mentioned, hence I committed and pushed it, was too quick:). Shall I revert it? |
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.
I have a slight concern if there's existing code relying on quirky sad behaviors of NamedList.asShallowMap having weird/sad implementations of keySet
, values
, entrySet
because it calls asMap(1)
on them. Had it been asMap(0)
, probably lower/no concern. Maybe this change should be in Solr 10 only. On the other hand... maybe it's an unlikely impact? Hard to say... right away I see SolrXmlConfig calling .asShallowMap().entrySet()
.
@@ -238,6 +238,14 @@ public int indexOf(String name, int start) { | |||
return -1; | |||
} | |||
|
|||
/*** | |||
* Scans the list sequentially beginning at index 0 |
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.
nitpick: javadoc should include clarity that it's the index of the name. It's not great to require looking at a parameter's name to disambiguate the meaning of a method.
@@ -403,8 +411,8 @@ public Map<String, T> asShallowMap() { | |||
return asShallowMap(false); | |||
} | |||
|
|||
public Map<String, T> asShallowMap(boolean allowDps) { | |||
return new Map<String, T>() { | |||
private Map<String, T> asShallowMap(boolean allowDps) { |
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.
now's not the time to make private but feel free to deprecate.
} | ||
|
||
@Override | ||
public Map<String, T> asShallowMap() { |
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.
nitpick but place this up above implementing Map interfaces rather than arbitrarily somewhere in the middle.
I've added that under Solr 9.9.0, is that correct? |
Isn't SolrXmlConfig always calling .asShallowMap on a NamedList, which is created in org.apache.solr.common.ConfigNode#forEachChild? So the changes do not make a difference here, or do they? |
…-Map # Conflicts: # solr/CHANGES.txt
I removed the asShallowMap returning SimpleOrderedMap so such a decision can be in Solr 10. |
You are welcome, thanks for your support! Now it is failing with: No idea what this means |
As you are a very new contributor, I can tell by your changes and your last question that you have not been running |
Ah yes, I've forgot about that one, was for some reason under the impression running the 'tidy' is sufficient, now I know:) |
Also added NamedList.indexOf(name) convenience method. Does *not* adjust asShallowMap() yet. Coming soon. --------- Co-authored-by: David Smiley <[email protected]> (cherry picked from commit dac5fe1)
https://issues.apache.org/jira/browse/SOLR-17623
Implementing the interface Map on SimpleOrderedMap and adding Unit-Test for the new methods