forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobStatus.java
More file actions
181 lines (156 loc) · 5.74 KB
/
JobStatus.java
File metadata and controls
181 lines (156 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery;
import com.google.api.core.ApiFunction;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* A Google BigQuery Job status. Objects of this class can be examined when polling an asynchronous
* job to see if the job completed.
*/
public class JobStatus implements Serializable {
private static final long serialVersionUID = -714976456815445365L;
/** Possible states that a BigQuery Job can assume. */
public static final class State extends StringEnumValue {
private static final long serialVersionUID = 818920627219751204L;
private static final ApiFunction<String, State> CONSTRUCTOR =
new ApiFunction<String, State>() {
@Override
public State apply(String constant) {
return new State(constant);
}
};
private static final StringEnumType<State> type = new StringEnumType(State.class, CONSTRUCTOR);
/** The BigQuery Job is waiting to be executed. */
public static final State PENDING = type.createAndRegister("PENDING");
/** The BigQuery Job is being executed. */
public static final State RUNNING = type.createAndRegister("RUNNING");
/**
* The BigQuery Job has completed either succeeding or failing. If failed {@link #getError()}
* will be non-null.
*/
public static final State DONE = type.createAndRegister("DONE");
private State(String constant) {
super(constant);
}
/**
* Get the State for the given String constant, and throw an exception if the constant is not
* recognized.
*/
public static State valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}
/** Get the State for the given String constant, and allow unrecognized values. */
public static State valueOf(String constant) {
return type.valueOf(constant);
}
/** Return the known values for State. */
public static State[] values() {
return type.values();
}
}
private final State state;
private final BigQueryError error;
private final List<BigQueryError> executionErrors;
JobStatus(State state) {
this.state = state;
this.error = null;
this.executionErrors = null;
}
JobStatus(State state, BigQueryError error, List<BigQueryError> executionErrors) {
this.state = state;
this.error = error;
this.executionErrors = executionErrors != null ? ImmutableList.copyOf(executionErrors) : null;
}
/**
* Returns the state of the job. A {@link State#PENDING} job is waiting to be executed. A {@link
* State#RUNNING} is being executed. A {@link State#DONE} job has completed either succeeding or
* failing. If failed {@link #getError()} will be non-null.
*/
public State getState() {
return state;
}
/**
* Returns the final error result of the job. If present, indicates that the job has completed and
* was unsuccessful.
*
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">Troubleshooting
* Errors</a>
*/
@Nullable
public BigQueryError getError() {
return error;
}
/**
* Returns all errors encountered during the running of the job. Errors here do not necessarily
* mean that the job has completed or was unsuccessful.
*
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">Troubleshooting
* Errors</a>
*/
public List<BigQueryError> getExecutionErrors() {
return executionErrors;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("state", state)
.add("error", error)
.add("executionErrors", executionErrors)
.toString();
}
@Override
public final int hashCode() {
return Objects.hash(state, error, executionErrors);
}
@Override
public final boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(JobStatus.class)
&& Objects.equals(toPb(), ((JobStatus) obj).toPb());
}
com.google.api.services.bigquery.model.JobStatus toPb() {
com.google.api.services.bigquery.model.JobStatus statusPb =
new com.google.api.services.bigquery.model.JobStatus();
if (state != null) {
statusPb.setState(state.toString());
}
if (error != null) {
statusPb.setErrorResult(error.toPb());
}
if (executionErrors != null) {
statusPb.setErrors(Lists.transform(executionErrors, BigQueryError.TO_PB_FUNCTION));
}
return statusPb;
}
static JobStatus fromPb(com.google.api.services.bigquery.model.JobStatus statusPb) {
List<BigQueryError> allErrors = null;
if (statusPb.getErrors() != null) {
allErrors = Lists.transform(statusPb.getErrors(), BigQueryError.FROM_PB_FUNCTION);
}
BigQueryError error =
statusPb.getErrorResult() != null ? BigQueryError.fromPb(statusPb.getErrorResult()) : null;
return new JobStatus(State.valueOf(statusPb.getState()), error, allErrors);
}
}