Skip to content

Commit 9732bf6

Browse files
authored
Move some classes from spanner to core (#1770)
* Moved Date,Timestamp to google-cloud-core Removed ByteArrays and moved the corresponding methods into ByteArray. ByteArray already has a decent toString so removed the toString from ByteArrays. * Undo session pool test changes
1 parent 212706a commit 9732bf6

36 files changed

Lines changed: 125 additions & 249 deletions

google-cloud-core/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@
115115
<artifactId>protobuf-java</artifactId>
116116
<version>3.0.0</version>
117117
</dependency>
118+
<dependency>
119+
<groupId>com.google.protobuf</groupId>
120+
<artifactId>protobuf-java-util</artifactId>
121+
<version>3.0.0</version>
122+
</dependency>
118123
<dependency>
119124
<groupId>io.grpc</groupId>
120125
<artifactId>grpc-protobuf</artifactId>
@@ -153,5 +158,17 @@
153158
</exclusion>
154159
</exclusions>
155160
</dependency>
161+
<dependency>
162+
<groupId>com.google.truth</groupId>
163+
<artifactId>truth</artifactId>
164+
<version>0.30</version>
165+
<scope>test</scope>
166+
</dependency>
167+
<dependency>
168+
<groupId>com.google.guava</groupId>
169+
<artifactId>guava-testlib</artifactId>
170+
<version>19.0</version>
171+
<scope>test</scope>
172+
</dependency>
156173
</dependencies>
157174
</project>

google-cloud-core/src/main/java/com/google/cloud/ByteArray.java

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

1919
import com.google.common.base.MoreObjects;
2020
import com.google.common.base.MoreObjects.ToStringHelper;
21+
import com.google.common.io.BaseEncoding;
2122
import com.google.protobuf.ByteString;
2223

2324
import java.io.IOException;
@@ -33,6 +34,7 @@
3334
public class ByteArray implements Iterable<Byte>, Serializable {
3435

3536
private static final long serialVersionUID = -1908809133893782840L;
37+
private static final BaseEncoding encoder = BaseEncoding.base64();
3638

3739
private final ByteString byteString;
3840

@@ -94,6 +96,11 @@ public final String toStringUtf8() {
9496
return byteString.toStringUtf8();
9597
}
9698

99+
/** Converts this byte array to its base64 representation. */
100+
public final String toBase64() {
101+
return encoder.encode(toByteArray());
102+
}
103+
97104
/**
98105
* Returns the content of this {@code ByteArray} as a read-only {@link ByteBuffer}.
99106
*/
@@ -162,4 +169,9 @@ public static final ByteArray copyFrom(ByteBuffer bytes) {
162169
public static final ByteArray copyFrom(InputStream input) throws IOException {
163170
return new ByteArray(ByteString.readFrom(input));
164171
}
172+
173+
/** Creates a {@code ByteArray} from a base64 representation. */
174+
public static ByteArray fromBase64(String data) {
175+
return ByteArray.copyFrom(encoder.decode(data));
176+
}
165177
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Date.java renamed to google-cloud-core/src/main/java/com/google/cloud/Date.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.spanner;
17+
package com.google.cloud;
1818

1919
import com.google.common.base.Preconditions;
2020
import java.util.Objects;
2121
import java.util.regex.Matcher;
2222
import java.util.regex.Pattern;
2323

2424
/**
25-
* Represents a Cloud Spanner Date. Date is timezone independent. The range [1678-01-01, 2262-01-01)
26-
* is the legal interval for dates. A write to a date column is rejected if the value is outside of
27-
* that interval.
25+
* Represents a Date without time, such as 2017-03-17. Date is timezone independent.
2826
*/
2927
public final class Date implements Comparable<Date> {
3028

@@ -44,8 +42,7 @@ private Date(int year, int month, int dayOfMonth) {
4442
}
4543

4644
/**
47-
* Constructs a new Date instance. This does minimal checks for valid dates but it might allow
48-
* instances to be created which are outside the valid cloud spanner date range.
45+
* Constructs a new Date instance.
4946
*
5047
* @param year must be greater than 0
5148
* @param month must be between [1,12]

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Timestamp.java renamed to google-cloud-core/src/main/java/com/google/cloud/Timestamp.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.spanner;
17+
package com.google.cloud;
1818

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

@@ -29,7 +29,7 @@
2929
import org.joda.time.format.ISODateTimeFormat;
3030

3131
/**
32-
* Represents a Cloud Spanner timestamp. Timestamps have nanosecond precision and cover the range
32+
* Represents a timestamp with nanosecond precision. Timestamps cover the range
3333
* [0001-01-01, 9999-12-31].
3434
*
3535
* <p>{@code Timestamp} instances are immutable.
@@ -111,11 +111,18 @@ public int compareTo(Timestamp other) {
111111
return r;
112112
}
113113

114-
static Timestamp fromProto(com.google.protobuf.Timestamp proto) {
114+
/**
115+
* Creates an instance of Timestamp from {@code com.google.protobuf.Timestamp}.
116+
*/
117+
public static Timestamp fromProto(com.google.protobuf.Timestamp proto) {
115118
return new Timestamp(proto.getSeconds(), proto.getNanos());
116119
}
117120

118-
com.google.protobuf.Timestamp toProto() {
121+
/**
122+
* Returns a {@code com.google.protobuf.Timestamp} initialized to the same point in time as {@code
123+
* this}.
124+
*/
125+
public com.google.protobuf.Timestamp toProto() {
119126
return com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build();
120127
}
121128

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DateTest.java renamed to google-cloud-core/src/test/java/com/google/cloud/DateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.spanner;
17+
package com.google.cloud;
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

google-cloud-spanner/src/test/java/com/google/cloud/spanner/TimestampTest.java renamed to google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.spanner;
17+
package com.google.cloud;
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

@@ -28,7 +28,7 @@
2828
import org.junit.runner.RunWith;
2929
import org.junit.runners.JUnit4;
3030

31-
/** Unit tests for {@link com.google.cloud.spanner.Timestamp}. */
31+
/** Unit tests for {@link com.google.cloud.Timestamp}. */
3232
@RunWith(JUnit4.class)
3333
public class TimestampTest {
3434
private static final String TEST_TIME_ISO = "2015-10-12T15:14:54Z";

google-cloud-spanner/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,6 @@
137137
</exclusion>
138138
</exclusions>
139139
</dependency>
140-
<dependency>
141-
<groupId>com.google.protobuf</groupId>
142-
<artifactId>protobuf-java-util</artifactId>
143-
<version>3.0.0</version>
144-
</dependency>
145140
<dependency>
146141
<groupId>com.google.code.findbugs</groupId>
147142
<artifactId>jsr305</artifactId>

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbstractStructReader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static com.google.common.base.Preconditions.checkState;
2020

2121
import com.google.cloud.ByteArray;
22+
import com.google.cloud.Date;
23+
import com.google.cloud.Timestamp;
2224
import java.util.List;
2325

2426
/**

google-cloud-spanner/src/main/java/com/google/cloud/spanner/ByteArrays.java

Lines changed: 0 additions & 105 deletions
This file was deleted.

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import com.google.cloud.Timestamp;
20+
1921
/**
2022
* Interface for all the APIs that are used to read/write data into a Cloud Spanner database. An
2123
* instance of this is tied to a specific database.

0 commit comments

Comments
 (0)