From 6c75cdb08bb92b303c428665a3be2ecb881cafde Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Tue, 3 Apr 2018 18:12:49 -0700 Subject: [PATCH] bigquery: add location property We also add JobId.Builder since the static constructors are getting unwieldy. The property isn't threaded through in the right places yet, so we keep it package-private for now. --- .../java/com/google/cloud/bigquery/JobId.java | 103 ++++++++++-------- 1 file changed, 60 insertions(+), 43 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java index 0ca5d571c66b..7d88dc8cc1ac 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/JobId.java @@ -19,89 +19,106 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.api.services.bigquery.model.JobReference; - +import com.google.auto.value.AutoValue; import java.io.Serializable; -import java.util.Objects; import java.util.UUID; +import javax.annotation.Nullable; -/** - * Google BigQuery Job identity. - */ -public final class JobId implements Serializable { - - private static final long serialVersionUID = 1225914835379688976L; +/** Google BigQuery Job identity. */ +@AutoValue +public abstract class JobId implements Serializable { - private final String project; - private final String job; + private static final long serialVersionUID = 1225914835379688977L; + JobId() { + // Users cannot extend this, but AutoValue can. + } /** - * Returns project's user-defined id. + * Returns job's project id. + * + *

When sending requests with null project, the client will attempt to infer the project name + * from the environment. */ - public String getProject() { - return project; - } + @Nullable + public abstract String getProject(); + /** + * Returns the job's id. + * + *

The server returns null job id for dry-run queries. + */ + @Nullable + public abstract String getJob(); /** - * Returns the job's user-defined id. + * Returns the job's location. + * + *

When sending requests, the location must be specified for jobs whose location not "US" or + * "EU". */ - public String getJob() { - return job; + @Nullable + abstract String getLocation(); + + public abstract Builder toBuilder(); + + public static Builder newBuilder() { + return new AutoValue_JobId.Builder(); } - private JobId(String project, String job) { - this.project = project; - this.job = job; + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setProject(String project); + + public abstract Builder setJob(String job); + + /** {@code setJob} to a pseudo-random string. */ + public Builder setRandomJob() { + return setJob(UUID.randomUUID().toString()); + } + + abstract Builder setLocation(String location); + + public abstract JobId build(); } /** * Creates a job identity given project's and job's user-defined id. */ public static JobId of(String project, String job) { - return new JobId(checkNotNull(project), checkNotNull(job)); + return newBuilder().setProject(checkNotNull(project)).setJob(checkNotNull(job)).build(); } /** * Creates a job identity given only its user-defined id. */ public static JobId of(String job) { - return new JobId(null, checkNotNull(job)); + return newBuilder().setJob(checkNotNull(job)).build(); } /** * Creates a job identity with autogenerated id and no project specified. */ public static JobId of() { - return new JobId(null, UUID.randomUUID().toString()); - } - - @Override - public boolean equals(Object obj) { - return obj == this - || obj instanceof JobId - && Objects.equals(toPb(), ((JobId) obj).toPb()); - } - - @Override - public int hashCode() { - return Objects.hash(project, job); - } - - @Override - public String toString() { - return toPb().toString(); + return newBuilder().setRandomJob().build(); } JobId setProjectId(String projectId) { - return getProject() != null ? this : JobId.of(projectId, getJob()); + return getProject() != null ? this : toBuilder().setProject(projectId).build(); } JobReference toPb() { - return new JobReference().setProjectId(project).setJobId(job); + return new JobReference() + .setProjectId(getProject()) + .setJobId(getJob()) + .setLocation(getLocation()); } static JobId fromPb(JobReference jobRef) { - return new JobId(jobRef.getProjectId(), jobRef.getJobId()); + return newBuilder() + .setProject(jobRef.getProjectId()) + .setJob(jobRef.getJobId()) + .setLocation(jobRef.getLocation()) + .build(); } }