-
Notifications
You must be signed in to change notification settings - Fork 96
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
Enhance SearchFilter.QueryType and FailedFuture #396
Conversation
/** | ||
* Get the QueryType enum fromType it's search type. | ||
* @param type Type of the query type (for serialization) | ||
* @return the enum QueryType |
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.
Empty lines before @return
|
||
/** | ||
* Get the QueryType enum fromType it's search type. | ||
* @param type Type of the query type (for serialization) |
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.
Empty lines before @param
@@ -0,0 +1,60 @@ | |||
// Copyright 2016 Yahoo Inc. |
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.
2017
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.
This PR looks good to me 👍
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.
CHANGELOG needed; make sure to explain why you add CompletedFuture
and delete the other
@@ -1,4 +1,4 @@ | |||
// Copyright 2016 Yahoo Inc. | |||
// Copyright 2017 Yahoo Inc. |
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 like this file is there since 2016 (we put based on when file is initialized)
* | ||
* @return the enum QueryType | ||
*/ | ||
public static QueryType fromType(String type) { |
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.
There is a standard way(simpler and more readable) of achieving your purpose. Example will be https://github.com/yahoo/fili/blob/master/fili-core/src/main/java/com/yahoo/bard/webservice/config/CacheFeatureFlag.java or http://grepcode.com/file_/repo1.maven.org/maven2/javax.ws.rs/jsr311-api/1.1/javax/ws/rs/core/Response.java/?v=source
Instead of fromType
, you have
public String getType() {
return type;
}
And you check type by having "contains".equals(QueryType.Contains.getType()). That's how we check type of enum (as the two link example does, as well)
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'm not sure I understand what you mean. Do you want me to enumerate each possibility to find which type the enum is instead of using the loop?
From the second example you gave they did this which is nearly identical to the way I checked the type.
public static Status fromStatusCode(final int statusCode) {
for (Status s : Status.values()) {
if (s.code == statusCode) {
return s;
}
}
return null;
}
1d49b17
to
273b31f
Compare
/** | ||
* Get the QueryType enum from it's search type. | ||
* | ||
* @param type The type of query (i.e. "insensitive_contains") |
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 type of query in String (i.e. "insensitive_contains")
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.
Same as above
@@ -42,6 +42,22 @@ | |||
QueryType(String type) { | |||
this.type = type; | |||
} | |||
|
|||
/** | |||
* Get the QueryType enum from it's search type. |
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.
Get the QueryType enum from it's search type in String
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 think this is clear from context as you can only pass in a String
to the method.
* | ||
* @return the enum QueryType | ||
*/ | ||
public static QueryType fromType(String type) { |
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.
final String type
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.
What's the reason for adding final
?
I've been told in previous PRs to remove final
from parameters
return queryType; | ||
} | ||
} | ||
throw new IllegalArgumentException("No query type corresponds to " + type); |
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 you decide to go this way, return 'null' on non-maching QueryType.
throw new ExecutionException(throwable); | ||
} else { | ||
return item; | ||
} |
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.
else
is not necessary
|
||
/** | ||
* Creates a completed future with an item to return or | ||
* if a nonnull throwable is passed it it will throw an |
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.
passed it -> provided ?
/** | ||
* Creates a completed future with an item to return or | ||
* if a nonnull throwable is passed it it will throw an | ||
* execution exception. |
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.
{@link java.util.concurrent.ExecutionException}
gives better reference
private final Throwable throwable; | ||
|
||
/** | ||
* Creates a completed future with an item to return or |
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.
an item to return on on {@link #get()}
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 think this is clear from the context and adding this could make it unclear what happens when {@link #get(long, TimeUnit)}
is called.
@@ -90,7 +90,7 @@ public TestDruidWebService() { | |||
/** | |||
* If TestDruidWebService#throwable is set, invokes the failure callback. Otherwise invokes success or failure | |||
* dependent on whether TestDruidWebService#statusCode equals 200. | |||
* Note that since this doesn't send requests to druid all the responses will be null or {@link FailedFuture}. | |||
* Note that since this doesn't send requests to druid all the responses will be null or {@link CompletedFuture}. |
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.
Since this class is in a different package
{@link com.yahoo.bard.webservice.util.CompletedFuture}
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.
It's available from context because of
import com.yahoo.bard.webservice.util.CompletedFuture;
do you want me to add the fully qualified name?
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.
This is not for readability. The reason to put complete path is for Javadoc plugin to be able to recognize and inject HTML link that points to the correct class.
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 javadoc plugin is able to correctly recognize this and inject the html link in other classes I've tried (like in CompletedFuture). Did you generate the javadoc and it didn't work correctly?
* @param item The item to return when called. | ||
* @param throwable A non null throwable if an exception should be thrown. | ||
*/ | ||
public CompletedFuture(E item, Throwable throwable) { |
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 see this constructor is called everywhere with item = null
, like new CompletedFuture<>(null, throwable)
. This makes code less readable, because readers don't know that null
means. A better approach would be
public CompletedFuture(E item, Throwable throwable) {
this.item = item;
this.throwable = throwable;
}
public CompletedFuture(Throwable throwable) {
this(null, throwable);
}
That way, you do new CompletedFuture<>(throwable)
.
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've decided to replace this with Either and give two ways to construct it which make it clear what it will do.
b715d5c
to
bec1580
Compare
* | ||
* @return the enum QueryType. | ||
*/ | ||
public static QueryType fromType(String type) { |
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 would argue that this should be returning an Optional
instead of a null
, or throwing an IllegalArgumentException
if the type
is not a recognized QueryType
.
Returning an Optional makes it clear to people using the code that they may not get a QueryType back, and need to deal with that case. This makes sense if it's totally valid to have a string type that cannot be translated to a QueryType.
The second case assumes that it's an error if we get an unrecognized type.
I'm assuming that it's an error to receive an unrecognized type, but I don't know yet what it's meant to be used for, so I can't say for certain.
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.
@QubitPi Are you fine with me reverting to the IllegalArgumentException/returning optional?
For context, this was made because SearchFilter
doesn't have a reference to its QueryType
and I have to process a SearchFilter
differently for each case (and if not found, throw an exception).
Additionally, if SearchFilter
must be one of those QueryType
then it should probably be verified in the constructor that accepts a String
queryType.
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.
@archolewa Agree
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 promise a deep review on CompletedFuture later today
* | ||
* @return the enum QueryType. | ||
*/ | ||
public static QueryType fromType(String type) { |
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.
@archolewa Agree
* Private constructor - all methods static. | ||
*/ | ||
private CompletedFuture() { | ||
|
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.
//Intentionally left blank.
/** | ||
* Creates a completed {@link java.util.concurrent.Future} which will return this item. | ||
* | ||
* @param item The item to return when called. |
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 period
* Creates a completed {@link java.util.concurrent.Future} which will return this item. | ||
* | ||
* @param item The item to return when called. | ||
* @param <E> The type to be returned by this future. |
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 period
* @param item The item to return when called. | ||
* @param <E> The type to be returned by this future. | ||
* | ||
* @return a completed {@link CompletableFuture} which will successfully return an item. |
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 period and all others as well
* | ||
* @return a completed {@link CompletableFuture} which will successfully return an item. | ||
*/ | ||
public static <E> CompletableFuture<E> returning(E item) { |
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.
Where is this method used?
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 was using this in #430 but it's no longer needed and can be deleted if that makes sense.
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.
Let's remove it if it's no longer needed
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.
Getting there. Code looks very good!
* | ||
* @return a completed {@link CompletableFuture} which will successfully return an item. | ||
*/ | ||
public static <E> CompletableFuture<E> returning(E item) { |
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.
Let's remove it if it's no longer needed
* Private constructor - all methods static. | ||
*/ | ||
private CompletedFuture() { | ||
//Intentionally left blank. |
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.
Space after //
@@ -90,7 +90,7 @@ public TestDruidWebService() { | |||
/** | |||
* If TestDruidWebService#throwable is set, invokes the failure callback. Otherwise invokes success or failure | |||
* dependent on whether TestDruidWebService#statusCode equals 200. | |||
* Note that since this doesn't send requests to druid all the responses will be null or {@link FailedFuture}. | |||
* Note that since this doesn't send requests to druid all the responses will be null or {@link CompletedFuture}. |
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.
This is not for readability. The reason to put complete path is for Javadoc plugin to be able to recognize and inject HTML link that points to the correct class.
* @param item The item to return when called | ||
* @param <E> The type to be returned by this future | ||
* | ||
* @return a completed {@link CompletableFuture} which will successfully return an item. |
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 period at end
/** | ||
* Get the QueryType enum from it's search type. | ||
* | ||
* @param type The type of query (i.e. "insensitive_contains") |
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.e. "insensitive_contains")
indicates this is the only QueryType? I would remove it.
@@ -78,18 +95,26 @@ private SearchFilter(Dimension dimension, String type, String value) { | |||
return query; | |||
} | |||
|
|||
private String getQueryType() { | |||
return query.get("type"); |
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.
type
is used multiple times. It's more maintainable to have it as a static final String.
} | ||
|
||
private String getQueryValue() { | ||
return query.get("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.
same
/** | ||
* A utility class to create futures which have already been completed | ||
* and will return as soon as it is called. | ||
* |
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.
extra line
|
||
/** | ||
* A utility class to create futures which have already been completed | ||
* and will return as soon as it is called. |
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.
can be move to the previous line
*/ | ||
public static <E> CompletableFuture<E> throwing(Throwable throwable) { | ||
CompletableFuture<E> completedFuture = CompletableFuture.supplyAsync(() -> null); | ||
completedFuture.completeExceptionally(throwable); |
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.
@michael-mclawhorn @garyluoex @archolewa I'm little concerned about this statement. According to its doc
"If not already completed, causes invocations of get() and related methods to throw the given exception."
Looks like this method wants to throw exception on complete instead (Or Javadoc of this method is simply wrong?).
- This replaces the FailedFuture by being able to be return a value or throw an exception
- Clarify usage of CompletedFuture by limiting how it can be created - add two helper methods to SearchFilter
4c92039
to
d7c486d
Compare
This adds some minor enhancements to FailedFuture and allows for a QueryType to be found from its serialized name.