forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableInfo.java
More file actions
240 lines (204 loc) · 6.95 KB
/
TableInfo.java
File metadata and controls
240 lines (204 loc) · 6.95 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.gcloud.bigquery;
import com.google.api.services.bigquery.model.Streamingbuffer;
import com.google.api.services.bigquery.model.Table;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Objects;
/**
* A Google BigQuery Table information. A BigQuery table is a standard, two-dimensional table with
* individual records organized in rows, and a data type assigned to each column (also called a
* field). Individual fields within a record may contain nested and repeated children fields. Every
* table is described by a schema that describes field names, types, and other information.
*
* @see <a href="https://cloud.google.com/bigquery/docs/tables">Managing Tables</a>
*/
public class TableInfo extends BaseTableInfo {
private static final long serialVersionUID = -5910575573063546949L;
private final String location;
private final StreamingBuffer streamingBuffer;
/**
* Google BigQuery Table's Streaming Buffer information. This class contains information on a
* table's streaming buffer as the estimated size in number of rows/bytes.
*/
public static class StreamingBuffer implements Serializable {
private static final long serialVersionUID = -6713971364725267597L;
private final long estimatedRows;
private final long estimatedBytes;
private final long oldestEntryTime;
StreamingBuffer(long estimatedRows, long estimatedBytes, long oldestEntryTime) {
this.estimatedRows = estimatedRows;
this.estimatedBytes = estimatedBytes;
this.oldestEntryTime = oldestEntryTime;
}
/**
* Returns a lower-bound estimate of the number of rows currently in the streaming buffer.
*/
public long estimatedRows() {
return estimatedRows;
}
/**
* Returns a lower-bound estimate of the number of bytes currently in the streaming buffer.
*/
public long estimatedBytes() {
return estimatedBytes;
}
/**
* Returns the timestamp of the oldest entry in the streaming buffer, in milliseconds since
* epoch.
*/
public long oldestEntryTime() {
return oldestEntryTime;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("estimatedRows", estimatedRows)
.add("estimatedBytes", estimatedBytes)
.add("oldestEntryTime", oldestEntryTime)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(estimatedRows, estimatedBytes, oldestEntryTime);
}
@Override
public boolean equals(Object obj) {
return obj instanceof StreamingBuffer
&& Objects.equals(toPb(), ((StreamingBuffer) obj).toPb());
}
Streamingbuffer toPb() {
return new Streamingbuffer()
.setEstimatedBytes(BigInteger.valueOf(estimatedBytes))
.setEstimatedRows(BigInteger.valueOf(estimatedRows))
.setOldestEntryTime(BigInteger.valueOf(oldestEntryTime));
}
static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) {
return new StreamingBuffer(streamingBufferPb.getEstimatedRows().longValue(),
streamingBufferPb.getEstimatedBytes().longValue(),
streamingBufferPb.getOldestEntryTime().longValue());
}
}
public static final class Builder extends BaseTableInfo.Builder<TableInfo, Builder> {
private String location;
private StreamingBuffer streamingBuffer;
private Builder() {}
private Builder(TableInfo tableInfo) {
super(tableInfo);
this.location = tableInfo.location;
this.streamingBuffer = tableInfo.streamingBuffer;
}
protected Builder(Table tablePb) {
super(tablePb);
this.location = tablePb.getLocation();
if (tablePb.getStreamingBuffer() != null) {
this.streamingBuffer = StreamingBuffer.fromPb(tablePb.getStreamingBuffer());
}
}
Builder location(String location) {
this.location = location;
return self();
}
Builder streamingBuffer(StreamingBuffer streamingBuffer) {
this.streamingBuffer = streamingBuffer;
return self();
}
/**
* Creates a {@code TableInfo} object.
*/
@Override
public TableInfo build() {
return new TableInfo(this);
}
}
private TableInfo(Builder builder) {
super(builder);
this.location = builder.location;
this.streamingBuffer = builder.streamingBuffer;
}
/**
* Returns the geographic location where the table should reside. This value is inherited from the
* dataset.
*
* @see <a href="https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects#dataset-location">
* Dataset Location</a>
*/
public String location() {
return location;
}
/**
* Returns information on the table's streaming buffer if any exists. Returns {@code null} if no
* streaming buffer exists.
*/
public StreamingBuffer streamingBuffer() {
return streamingBuffer;
}
/**
* Returns a builder for a BigQuery Table.
*
* @param tableId table id
* @param schema the schema of the table
*/
public static Builder builder(TableId tableId, Schema schema) {
return new Builder().tableId(tableId).type(Type.TABLE).schema(schema);
}
/**
* Creates BigQuery table given its type.
*
* @param tableId table id
* @param schema the schema of the table
*/
public static TableInfo of(TableId tableId, Schema schema) {
return builder(tableId, schema).build();
}
/**
* Returns a builder for the {@code TableInfo} object.
*/
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
ToStringHelper toStringHelper() {
return super.toStringHelper()
.add("location", location)
.add("streamingBuffer", streamingBuffer);
}
@Override
public boolean equals(Object obj) {
return obj instanceof TableInfo && baseEquals((TableInfo) obj);
}
@Override
public int hashCode() {
return Objects.hash(baseHashCode(), location, streamingBuffer);
}
@Override
Table toPb() {
Table tablePb = super.toPb();
tablePb.setLocation(location);
if (streamingBuffer != null) {
tablePb.setStreamingBuffer(streamingBuffer.toPb());
}
return tablePb;
}
@SuppressWarnings("unchecked")
static TableInfo fromPb(Table tablePb) {
return new Builder(tablePb).build();
}
}