Skip to content

Commit 842a556

Browse files
authored
---
yaml --- r: 8727 b: refs/heads/master c: fb42878 h: refs/heads/master i: 8725: 963a60b 8723: e68317d 8719: 14f300a
1 parent df2f2be commit 842a556

10 files changed

Lines changed: 305 additions & 53 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: a22c945fad62972e59b5ba2285f0a2444e732018
2+
refs/heads/master: fb428785a2373235bb7d5e317c63338dd02ad4b2
33
refs/heads/travis: 47e4fee4fd5af9b2a8ce46f23c72ec95f9b195b2
44
refs/heads/gh-pages: 6daca92127d91b7c2c99490080ecf8a13fa94cde
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

trunk/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.common.base.Function;
2626
import com.google.common.base.MoreObjects;
2727
import com.google.common.collect.ImmutableList;
28-
import com.google.common.collect.ImmutableMap;
2928
import com.google.common.collect.Lists;
3029
import java.io.Serializable;
3130
import java.util.List;
@@ -69,7 +68,7 @@ public Dataset apply(DatasetInfo datasetInfo) {
6968
private final Long lastModified;
7069
private final String location;
7170
private final String selfLink;
72-
private final Map<String, String> labels;
71+
private final Labels labels;
7372

7473
/**
7574
* A builder for {@code DatasetInfo} objects.
@@ -157,7 +156,7 @@ static final class BuilderImpl extends Builder {
157156
private Long lastModified;
158157
private String location;
159158
private String selfLink;
160-
private Map<String, String> labels;
159+
private Labels labels = Labels.ZERO;
161160

162161
BuilderImpl() {}
163162

@@ -173,9 +172,7 @@ static final class BuilderImpl extends Builder {
173172
this.lastModified = datasetInfo.lastModified;
174173
this.location = datasetInfo.location;
175174
this.selfLink = datasetInfo.selfLink;
176-
this.labels = datasetInfo.labels != null
177-
? ImmutableMap.copyOf(datasetInfo.labels)
178-
: null;
175+
this.labels = datasetInfo.labels;
179176
}
180177

181178
BuilderImpl(com.google.api.services.bigquery.model.Dataset datasetPb) {
@@ -199,9 +196,7 @@ public Acl apply(Dataset.Access accessPb) {
199196
this.lastModified = datasetPb.getLastModifiedTime();
200197
this.location = datasetPb.getLocation();
201198
this.selfLink = datasetPb.getSelfLink();
202-
this.labels = datasetPb.getLabels() != null
203-
? ImmutableMap.copyOf(datasetPb.getLabels())
204-
: null;
199+
this.labels = Labels.fromPb(datasetPb.getLabels());
205200
}
206201

207202

@@ -277,9 +272,16 @@ Builder setSelfLink(String selfLink) {
277272
return this;
278273
}
279274

275+
/**
276+
* Sets the labels applied to this dataset.
277+
*
278+
* <p>When used with {@link BigQuery#update(DatasetInfo, DatasetOption...)}, setting {@code
279+
* labels} to {@code null} removes all labels; otherwise all keys that are mapped to {@code
280+
* null} values are removed and other keys are updated to their respective values.
281+
*/
280282
@Override
281283
public Builder setLabels(Map<String, String> labels) {
282-
this.labels = ImmutableMap.copyOf(labels);
284+
this.labels = Labels.fromUser(labels);
283285
return this;
284286
}
285287

@@ -411,7 +413,7 @@ public String getSelfLink() {
411413
* @see <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">Labeling Datasets</a>
412414
*/
413415
public Map<String, String> getLabels() {
414-
return labels;
416+
return labels.userMap();
415417
}
416418

417419
/**
@@ -494,9 +496,7 @@ public Dataset.Access apply(Acl acl) {
494496
}
495497
}));
496498
}
497-
if (labels != null) {
498-
datasetPb.setLabels(labels);
499-
}
499+
datasetPb.setLabels(labels.toPb());
500500
return datasetPb;
501501
}
502502

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigquery;
18+
19+
import com.google.api.client.util.Data;
20+
import com.google.auto.value.AutoValue;
21+
import com.google.auto.value.extension.memoized.Memoized;
22+
import com.google.common.base.Preconditions;
23+
import com.google.common.collect.ImmutableMap;
24+
import java.io.Serializable;
25+
import java.util.Collections;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
import javax.annotation.Nullable;
29+
30+
@AutoValue
31+
abstract class Labels implements Serializable {
32+
private static final long serialVersionUID = 1L;
33+
static final Labels ZERO = of(Collections.<String, String>emptyMap());
34+
35+
@Nullable
36+
abstract Map<String, String> userMap();
37+
38+
@Memoized
39+
@Nullable
40+
Map<String, String> toPb() {
41+
Map<String, String> userMap = userMap();
42+
if (userMap == null) {
43+
// converted to JSON null
44+
return Data.nullOf(HashMap.class);
45+
}
46+
if (userMap.isEmpty()) {
47+
// dropped from JSON
48+
return null;
49+
}
50+
HashMap<String, String> pbMap = new HashMap<>();
51+
for (Map.Entry<String, String> entry : userMap.entrySet()) {
52+
String key = entry.getKey();
53+
String val = entry.getValue();
54+
if (val == null) {
55+
val = Data.NULL_STRING;
56+
}
57+
pbMap.put(key, val);
58+
}
59+
return Collections.unmodifiableMap(pbMap);
60+
}
61+
62+
private static Labels of(Map<String, String> userMap) {
63+
Preconditions.checkArgument(
64+
userMap == null || !userMap.containsKey(null), "null keys are not supported");
65+
return new AutoValue_Labels(userMap);
66+
}
67+
68+
static Labels fromUser(Map<String, String> map) {
69+
if (map == null || map instanceof ImmutableMap) {
70+
return of(map);
71+
}
72+
// Not ImmutableMap; we support null values!
73+
return of(Collections.unmodifiableMap(new HashMap<>(map)));
74+
}
75+
76+
static Labels fromPb(Map<String, String> pb) {
77+
if (Data.isNull(pb)) {
78+
return of(null);
79+
}
80+
if (pb == null || pb.isEmpty()) {
81+
return of(Collections.<String, String>emptyMap());
82+
}
83+
84+
HashMap<String, String> map = new HashMap<>();
85+
for (Map.Entry<String, String> entry : pb.entrySet()) {
86+
String key = entry.getKey();
87+
String val = entry.getValue();
88+
if (Data.isNull(val)) {
89+
val = null;
90+
}
91+
map.put(key, val);
92+
}
93+
return of(Collections.unmodifiableMap(map));
94+
}
95+
}

trunk/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Table.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21-
import com.google.api.gax.paging.Page;
2221
import com.google.cloud.bigquery.BigQuery.JobOption;
2322
import com.google.cloud.bigquery.BigQuery.TableDataListOption;
2423
import com.google.cloud.bigquery.BigQuery.TableOption;
@@ -27,6 +26,7 @@
2726
import java.io.IOException;
2827
import java.io.ObjectInputStream;
2928
import java.util.List;
29+
import java.util.Map;
3030
import java.util.Objects;
3131

3232
/**
@@ -135,6 +135,12 @@ public TableInfo.Builder setEncryptionConfiguration(EncryptionConfiguration conf
135135
return this;
136136
}
137137

138+
@Override
139+
public Builder setLabels(Map<String, String> labels) {
140+
infoBuilder.setLabels(labels);
141+
return this;
142+
}
143+
138144
@Override
139145
public Table build() {
140146
return new Table(bigquery, infoBuilder);

trunk/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableInfo.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

2222
import com.google.api.client.util.Data;
23+
import com.google.api.core.BetaApi;
2324
import com.google.api.services.bigquery.model.Table;
2425
import com.google.common.base.Function;
2526
import com.google.common.base.MoreObjects;
26-
2727
import java.io.Serializable;
2828
import java.math.BigInteger;
29+
import java.util.Map;
2930
import java.util.Objects;
3031

3132
/**
@@ -65,6 +66,7 @@ public Table apply(TableInfo tableInfo) {
6566
private final Long lastModifiedTime;
6667
private final TableDefinition definition;
6768
private final EncryptionConfiguration encryptionConfiguration;
69+
private final Labels labels;
6870

6971
/**
7072
* A builder for {@code TableInfo} objects.
@@ -100,7 +102,6 @@ public abstract static class Builder {
100102

101103
abstract Builder setSelfLink(String selfLink);
102104

103-
104105
/**
105106
* Sets the table identity.
106107
*/
@@ -114,6 +115,19 @@ public abstract static class Builder {
114115
*/
115116
public abstract Builder setDefinition(TableDefinition definition);
116117

118+
/**
119+
* Sets the labels applied to this table.
120+
*
121+
* <p>Unstable, because labels are <a
122+
* href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables">experimental</a>.
123+
*
124+
* <p>When used with {@link BigQuery#update(TableInfo, TableOption...)}, setting {@code labels}
125+
* to {@code null} removes all labels; otherwise all keys that are mapped to {@code null} values
126+
* are removed and other keys are updated to their respective values.
127+
*/
128+
@BetaApi
129+
public abstract Builder setLabels(Map<String, String> labels);
130+
117131
/**
118132
* Creates a {@code TableInfo} object.
119133
*/
@@ -135,6 +149,7 @@ static class BuilderImpl extends Builder {
135149
private Long lastModifiedTime;
136150
private TableDefinition definition;
137151
private EncryptionConfiguration encryptionConfiguration;
152+
private Labels labels = Labels.ZERO;
138153

139154
BuilderImpl() {}
140155

@@ -150,6 +165,7 @@ static class BuilderImpl extends Builder {
150165
this.lastModifiedTime = tableInfo.lastModifiedTime;
151166
this.definition = tableInfo.definition;
152167
this.encryptionConfiguration = tableInfo.encryptionConfiguration;
168+
this.labels = tableInfo.labels;
153169
}
154170

155171
BuilderImpl(Table tablePb) {
@@ -169,6 +185,7 @@ static class BuilderImpl extends Builder {
169185
this.encryptionConfiguration =
170186
new EncryptionConfiguration.Builder(tablePb.getEncryptionConfiguration()).build();
171187
}
188+
this.labels = Labels.fromPb(tablePb.getLabels());
172189
}
173190

174191
@Override
@@ -244,6 +261,12 @@ public Builder setEncryptionConfiguration(EncryptionConfiguration configuration)
244261
}
245262

246263

264+
@Override
265+
public Builder setLabels(Map<String, String> labels) {
266+
this.labels = Labels.fromUser(labels);
267+
return this;
268+
}
269+
247270
@Override
248271
public TableInfo build() {
249272
return new TableInfo(this);
@@ -262,6 +285,7 @@ public TableInfo build() {
262285
this.lastModifiedTime = builder.lastModifiedTime;
263286
this.definition = builder.definition;
264287
this.encryptionConfiguration = builder.encryptionConfiguration;
288+
labels = builder.labels;
265289
}
266290

267291

@@ -351,6 +375,17 @@ public <T extends TableDefinition> T getDefinition() {
351375
return (T) definition;
352376
}
353377

378+
/**
379+
* Return a map for labels applied to the table.
380+
*
381+
* <p>Unstable, because labels are <a
382+
* href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables">experimental</a>.
383+
*/
384+
@BetaApi
385+
public Map<String, String> getLabels() {
386+
return labels.userMap();
387+
}
388+
354389
/**
355390
* Returns a builder for the table object.
356391
*/
@@ -372,6 +407,7 @@ public String toString() {
372407
.add("lastModifiedTime", lastModifiedTime)
373408
.add("definition", definition)
374409
.add("encryptionConfiguration", encryptionConfiguration)
410+
.add("labels", labels)
375411
.toString();
376412
}
377413

@@ -429,6 +465,7 @@ Table toPb() {
429465
if (encryptionConfiguration != null) {
430466
tablePb.setEncryptionConfiguration(encryptionConfiguration.toPb());
431467
}
468+
tablePb.setLabels(labels.toPb());
432469
return tablePb;
433470
}
434471

trunk/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/DatasetInfoTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertTrue;
2122

2223
import com.google.common.collect.ImmutableList;
2324
import com.google.common.collect.ImmutableMap;
@@ -128,7 +129,8 @@ public void testOf() {
128129
assertNull(datasetInfo.getLastModified());
129130
assertNull(datasetInfo.getLocation());
130131
assertNull(datasetInfo.getSelfLink());
131-
assertNull(datasetInfo.getLabels());
132+
assertTrue(datasetInfo.getLabels().isEmpty());
133+
132134
datasetInfo = DatasetInfo.of(DATASET_ID);
133135
assertEquals(DATASET_ID, datasetInfo.getDatasetId());
134136
assertNull(datasetInfo.getAcl());
@@ -141,7 +143,7 @@ public void testOf() {
141143
assertNull(datasetInfo.getLastModified());
142144
assertNull(datasetInfo.getLocation());
143145
assertNull(datasetInfo.getSelfLink());
144-
assertNull(datasetInfo.getLabels());
146+
assertTrue(datasetInfo.getLabels().isEmpty());
145147
}
146148

147149
@Test

0 commit comments

Comments
 (0)