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

Enhance SearchFilter.QueryType and FailedFuture #396

Merged
merged 11 commits into from
Jul 28, 2017

Conversation

kevinhinterlong
Copy link
Member

This adds some minor enhancements to FailedFuture and allows for a QueryType to be found from its serialized name.

/**
* Get the QueryType enum fromType it's search type.
* @param type Type of the query type (for serialization)
* @return the enum QueryType
Copy link
Contributor

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)
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

2017

@QubitPi QubitPi self-assigned this Jun 13, 2017
Copy link
Collaborator

@asifmansoora asifmansoora left a 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 👍

Copy link
Contributor

@QubitPi QubitPi left a 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.
Copy link
Contributor

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) {
Copy link
Contributor

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)

Copy link
Member Author

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;
}

@yahoo yahoo deleted a comment Jun 19, 2017
/**
* Get the QueryType enum from it's search type.
*
* @param type The type of query (i.e. "insensitive_contains")
Copy link
Contributor

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")

Copy link
Member Author

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.
Copy link
Contributor

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

Copy link
Member Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

final String type

Copy link
Member Author

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);
Copy link
Contributor

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;
}
Copy link
Contributor

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
Copy link
Contributor

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.
Copy link
Contributor

@QubitPi QubitPi Jun 21, 2017

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
Copy link
Contributor

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()}

Copy link
Member Author

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}.
Copy link
Contributor

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}

Copy link
Member Author

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?

Copy link
Contributor

@QubitPi QubitPi Jul 20, 2017

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.

Copy link
Member Author

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) {
Copy link
Contributor

@QubitPi QubitPi Jun 21, 2017

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).

Copy link
Member Author

@kevinhinterlong kevinhinterlong Jun 21, 2017

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.

@kevinhinterlong kevinhinterlong force-pushed the smallCoreEnhancement branch 2 times, most recently from b715d5c to bec1580 Compare June 21, 2017 14:28
@yahoo yahoo deleted a comment Jun 21, 2017
*
* @return the enum QueryType.
*/
public static QueryType fromType(String type) {
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

@archolewa Agree

Copy link
Contributor

@QubitPi QubitPi left a 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) {
Copy link
Contributor

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() {

Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

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

@yahoo yahoo deleted a comment Jul 18, 2017
@yahoo yahoo deleted a comment Jul 18, 2017
*
* @return a completed {@link CompletableFuture} which will successfully return an item.
*/
public static <E> CompletableFuture<E> returning(E item) {
Copy link
Contributor

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?

Copy link
Member Author

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.

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 remove it if it's no longer needed

Copy link
Contributor

@QubitPi QubitPi left a 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) {
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 remove it if it's no longer needed

* Private constructor - all methods static.
*/
private CompletedFuture() {
//Intentionally left blank.
Copy link
Contributor

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}.
Copy link
Contributor

@QubitPi QubitPi Jul 20, 2017

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.
Copy link
Contributor

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")
Copy link
Contributor

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");
Copy link
Contributor

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");
Copy link
Contributor

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.
*
Copy link
Contributor

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.
Copy link
Contributor

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);
Copy link
Contributor

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?).

@yahoo yahoo deleted a comment Jul 24, 2017
@yahoo yahoo deleted a comment Jul 24, 2017
@yahoo yahoo deleted a comment Jul 28, 2017
@yahoo yahoo deleted a comment Jul 28, 2017
@yahoo yahoo deleted a comment Jul 28, 2017
@yahoo yahoo deleted a comment Jul 28, 2017
@QubitPi QubitPi merged commit 90fcf47 into yahoo:master Jul 28, 2017
@kevinhinterlong kevinhinterlong deleted the smallCoreEnhancement branch August 8, 2017 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants