Skip to content

Commit

Permalink
fix: Remove Guava ImmutableList from API surface (#411)
Browse files Browse the repository at this point in the history
Replace com.google.common.collect.ImmutableList
with java.util.List

BREAKING CHANGE
  • Loading branch information
nielm committed Sep 10, 2020
1 parent 7c150cc commit b35304e
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 44 deletions.
16 changes: 15 additions & 1 deletion google-cloud-spanner/clirr-ignored-differences.xml
Expand Up @@ -227,7 +227,21 @@
<className>com/google/cloud/spanner/connection/Connection</className>
<method>* executeQueryAsync(*)</method>
</difference>


<!-- Use List interface in Async API (compared to 1.60.0)-->
<difference>
<differenceType>7006</differenceType>
<className>com/google/cloud/spanner/AsyncResultSet</className>
<method>com.google.common.collect.ImmutableList toList(com.google.common.base.Function)</method>
<to>java.util.List</to>
</difference>
<difference>
<differenceType>7006</differenceType>
<className>com/google/cloud/spanner/ForwardingAsyncResultSet</className>
<method>com.google.common.collect.ImmutableList toList(com.google.common.base.Function)</method>
<to>java.util.List</to>
</difference>

<!-- Adding operation RPCs to InstanceAdminClient. -->
<difference>
<differenceType>7012</differenceType>
Expand Down
Expand Up @@ -18,7 +18,7 @@

import com.google.api.core.ApiFuture;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -203,8 +203,7 @@ interface ReadyCallback {
* inline executor such as {@code MoreExecutors.directExecutor()}; using such an executor may
* degrade the performance of the Spanner library.
*/
<T> ApiFuture<ImmutableList<T>> toListAsync(
Function<StructReader, T> transformer, Executor executor);
<T> ApiFuture<List<T>> toListAsync(Function<StructReader, T> transformer, Executor executor);

/**
* Transforms the row cursor into an immutable list using the given transformer function. {@code
Expand All @@ -222,5 +221,5 @@ <T> ApiFuture<ImmutableList<T>> toListAsync(
*
* @param transformer function which will be used to transform the row. It should not return null.
*/
<T> ImmutableList<T> toList(Function<StructReader, T> transformer) throws SpannerException;
<T> List<T> toList(Function<StructReader, T> transformer) throws SpannerException;
}
Expand Up @@ -31,6 +31,7 @@
import com.google.spanner.v1.ResultSetStats;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -483,12 +484,12 @@ public void resume() {
}

private static class CreateListCallback<T> implements ReadyCallback {
private final SettableApiFuture<ImmutableList<T>> future;
private final SettableApiFuture<List<T>> future;
private final Function<StructReader, T> transformer;
private final ImmutableList.Builder<T> builder = ImmutableList.builder();

private CreateListCallback(
SettableApiFuture<ImmutableList<T>> future, Function<StructReader, T> transformer) {
SettableApiFuture<List<T>> future, Function<StructReader, T> transformer) {
this.future = future;
this.transformer = transformer;
}
Expand Down Expand Up @@ -516,20 +517,20 @@ public CallbackResponse cursorReady(AsyncResultSet resultSet) {
}

@Override
public <T> ApiFuture<ImmutableList<T>> toListAsync(
public <T> ApiFuture<List<T>> toListAsync(
Function<StructReader, T> transformer, Executor executor) {
synchronized (monitor) {
Preconditions.checkState(!closed, "This AsyncResultSet has been closed");
Preconditions.checkState(
this.state == State.INITIALIZED, "This AsyncResultSet has already been used.");
final SettableApiFuture<ImmutableList<T>> res = SettableApiFuture.<ImmutableList<T>>create();
final SettableApiFuture<List<T>> res = SettableApiFuture.<List<T>>create();
CreateListCallback<T> callback = new CreateListCallback<T>(res, transformer);
ApiFuture<Void> finished = setCallback(executor, callback);
return ApiFutures.transformAsync(
finished,
new ApiAsyncFunction<Void, ImmutableList<T>>() {
new ApiAsyncFunction<Void, List<T>>() {
@Override
public ApiFuture<ImmutableList<T>> apply(Void input) throws Exception {
public ApiFuture<List<T>> apply(Void input) throws Exception {
return res;
}
},
Expand All @@ -538,9 +539,8 @@ public ApiFuture<ImmutableList<T>> apply(Void input) throws Exception {
}

@Override
public <T> ImmutableList<T> toList(Function<StructReader, T> transformer)
throws SpannerException {
ApiFuture<ImmutableList<T>> future = toListAsync(transformer, MoreExecutors.directExecutor());
public <T> List<T> toList(Function<StructReader, T> transformer) throws SpannerException {
ApiFuture<List<T>> future = toListAsync(transformer, MoreExecutors.directExecutor());
try {
return future.get();
} catch (ExecutionException e) {
Expand Down
Expand Up @@ -19,7 +19,7 @@
import com.google.api.core.ApiFuture;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.concurrent.Executor;

/** Forwarding implementation of {@link AsyncResultSet} that forwards all calls to a delegate. */
Expand Down Expand Up @@ -52,14 +52,13 @@ public void resume() {
}

@Override
public <T> ApiFuture<ImmutableList<T>> toListAsync(
public <T> ApiFuture<List<T>> toListAsync(
Function<StructReader, T> transformer, Executor executor) {
return delegate.toListAsync(transformer, executor);
}

@Override
public <T> ImmutableList<T> toList(Function<StructReader, T> transformer)
throws SpannerException {
public <T> List<T> toList(Function<StructReader, T> transformer) throws SpannerException {
return delegate.toList(transformer);
}
}
Expand Up @@ -176,7 +176,7 @@ public void toList() throws Exception {
for (int i = 0; i < TEST_RUNS; i++) {
try (AsyncResultSetImpl impl =
new AsyncResultSetImpl(executorProvider, createResultSet(), bufferSize)) {
ImmutableList<Row> list =
List<Row> list =
impl.toList(
new Function<StructReader, Row>() {
@Override
Expand All @@ -198,7 +198,7 @@ public void toListWithErrors() throws Exception {
try (AsyncResultSetImpl impl =
new AsyncResultSetImpl(
executorProvider, createResultSetWithErrors(1.0 / resultSetSize), bufferSize)) {
ImmutableList<Row> list =
List<Row> list =
impl.toList(
new Function<StructReader, Row>() {
@Override
Expand All @@ -219,7 +219,7 @@ public Row apply(StructReader input) {
public void asyncToList() throws Exception {
ExecutorProvider executorProvider = SpannerOptions.createDefaultAsyncExecutorProvider();
for (int bufferSize = 1; bufferSize < resultSetSize * 2; bufferSize *= 2) {
List<ApiFuture<ImmutableList<Row>>> futures = new ArrayList<>(TEST_RUNS);
List<ApiFuture<List<Row>>> futures = new ArrayList<>(TEST_RUNS);
ExecutorService executor = createExecService(32);
for (int i = 0; i < TEST_RUNS; i++) {
try (AsyncResultSet impl =
Expand All @@ -235,8 +235,8 @@ public Row apply(StructReader input) {
executor));
}
}
List<ImmutableList<Row>> lists = ApiFutures.allAsList(futures).get();
for (ImmutableList<Row> list : lists) {
List<List<Row>> lists = ApiFutures.allAsList(futures).get();
for (List<Row> list : lists) {
assertThat(list).containsExactlyElementsIn(createExpectedRows());
}
executor.shutdown();
Expand Down
Expand Up @@ -27,8 +27,8 @@
import com.google.cloud.spanner.AsyncResultSet.CursorState;
import com.google.cloud.spanner.AsyncResultSet.ReadyCallback;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -117,7 +117,7 @@ public void toList() {
when(delegate.getCurrentRowAsStruct()).thenReturn(mock(Struct.class));
try (AsyncResultSetImpl rs =
new AsyncResultSetImpl(simpleProvider, delegate, AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
ImmutableList<Object> list =
List<Object> list =
rs.toList(
new Function<StructReader, Object>() {
@Override
Expand Down Expand Up @@ -160,7 +160,7 @@ public void toListAsync() throws InterruptedException, ExecutionException {
when(delegate.getCurrentRowAsStruct()).thenReturn(mock(Struct.class));
try (AsyncResultSetImpl rs =
new AsyncResultSetImpl(simpleProvider, delegate, AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
ApiFuture<ImmutableList<Object>> future =
ApiFuture<List<Object>> future =
rs.toListAsync(
new Function<StructReader, Object>() {
@Override
Expand Down
Expand Up @@ -596,11 +596,11 @@ public String apply(Struct input) {
@Test
public void asyncRunnerRead() throws Exception {
AsyncRunner runner = client().runAsync();
ApiFuture<ImmutableList<String>> val =
ApiFuture<List<String>> val =
runner.runAsync(
new AsyncWork<ImmutableList<String>>() {
new AsyncWork<List<String>>() {
@Override
public ApiFuture<ImmutableList<String>> doWorkAsync(TransactionContext txn) {
public ApiFuture<List<String>> doWorkAsync(TransactionContext txn) {
return txn.readAsync(READ_TABLE_NAME, KeySet.all(), READ_COLUMN_NAMES)
.toListAsync(
new Function<StructReader, String>() {
Expand Down
Expand Up @@ -51,6 +51,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -960,17 +961,17 @@ public ApiFuture<String> apply(TransactionContext txn, Struct input)

@Test
public void asyncTransactionManagerRead() throws Exception {
AsyncTransactionStep<Void, ImmutableList<String>> res;
AsyncTransactionStep<Void, List<String>> res;
try (AsyncTransactionManager mgr = client().transactionManagerAsync()) {
TransactionContextFuture txn = mgr.beginAsync();
while (true) {
try {
res =
txn.then(
new AsyncTransactionFunction<Void, ImmutableList<String>>() {
new AsyncTransactionFunction<Void, List<String>>() {
@Override
public ApiFuture<ImmutableList<String>> apply(
TransactionContext txn, Void input) throws Exception {
public ApiFuture<List<String>> apply(TransactionContext txn, Void input)
throws Exception {
return txn.readAsync(READ_TABLE_NAME, KeySet.all(), READ_COLUMN_NAMES)
.toListAsync(
new Function<StructReader, String>() {
Expand Down
Expand Up @@ -32,7 +32,6 @@
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
import com.google.common.base.Function;
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import io.grpc.Server;
Expand Down Expand Up @@ -317,8 +316,8 @@ public void readOnlyTransaction() throws Exception {
mockSpanner.putStatementResult(
StatementResult.query(statement2, generateKeyValueResultSet(ContiguousSet.closed(1, 3))));

ApiFuture<ImmutableList<String>> values1;
ApiFuture<ImmutableList<String>> values2;
ApiFuture<List<String>> values1;
ApiFuture<List<String>> values2;
try (ReadOnlyTransaction tx = client.readOnlyTransaction()) {
try (AsyncResultSet rs = tx.executeQueryAsync(statement1)) {
values1 =
Expand Down Expand Up @@ -346,9 +345,9 @@ public String apply(StructReader input) {
ApiFuture<Iterable<String>> allValues =
ApiFutures.transform(
ApiFutures.allAsList(Arrays.asList(values1, values2)),
new ApiFunction<List<ImmutableList<String>>, Iterable<String>>() {
new ApiFunction<List<List<String>>, Iterable<String>>() {
@Override
public Iterable<String> apply(List<ImmutableList<String>> input) {
public Iterable<String> apply(List<List<String>> input) {
return Iterables.mergeSorted(
input,
new Comparator<String>() {
Expand Down
Expand Up @@ -31,7 +31,6 @@
import com.google.cloud.spanner.v1.SpannerSettings;
import com.google.common.base.Function;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.ListValue;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.StructType;
Expand Down Expand Up @@ -275,7 +274,7 @@ public void singleUseSelect() throws InterruptedException {
@Test
public void singleUseSelectAsync() throws Exception {
invalidateSessionPool();
ApiFuture<ImmutableList<Long>> list;
ApiFuture<List<Long>> list;
try (AsyncResultSet rs = client.singleUse().executeQueryAsync(SELECT1AND2)) {
list = rs.toListAsync(TO_LONG, executor);
assertThat(list.get()).containsExactly(1L, 2L);
Expand Down
Expand Up @@ -343,8 +343,8 @@ public ApiFuture<long[]> doWorkAsync(TransactionContext txn) {
public void readOnlyTransaction() throws Exception {
ImmutableList<String> keys1 = ImmutableList.of("k10", "k11", "k12");
ImmutableList<String> keys2 = ImmutableList.of("k1", "k2", "k3");
ApiFuture<ImmutableList<String>> values1;
ApiFuture<ImmutableList<String>> values2;
ApiFuture<List<String>> values1;
ApiFuture<List<String>> values2;
try (ReadOnlyTransaction tx = client.readOnlyTransaction()) {
try (AsyncResultSet rs =
tx.executeQueryAsync(
Expand Down Expand Up @@ -382,9 +382,9 @@ public String apply(StructReader input) {
ApiFuture<Iterable<String>> allValues =
ApiFutures.transform(
ApiFutures.allAsList(Arrays.asList(values1, values2)),
new ApiFunction<List<ImmutableList<String>>, Iterable<String>>() {
new ApiFunction<List<List<String>>, Iterable<String>>() {
@Override
public Iterable<String> apply(List<ImmutableList<String>> input) {
public Iterable<String> apply(List<List<String>> input) {
return Iterables.mergeSorted(
input,
new Comparator<String>() {
Expand Down

0 comments on commit b35304e

Please sign in to comment.