nl.topicus.jdbc.shaded.com.google.cloud.AsyncPageImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spanner-jdbc Show documentation
Show all versions of spanner-jdbc Show documentation
JDBC Driver for Google Cloud Spanner
/*
* 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 nl.topicus.jdbc.shaded.com.google.cloud;
import nl.topicus.jdbc.shaded.com.google.api.core.ApiFuture;
import nl.topicus.jdbc.shaded.com.google.api.core.ApiFutures;
import nl.topicus.jdbc.shaded.com.google.api.core.InternalApi;
import nl.topicus.jdbc.shaded.com.google.api.gax.paging.AsyncPage;
import nl.topicus.jdbc.shaded.com.google.api.gax.paging.Page;
import nl.topicus.jdbc.shaded.com.google.common.base.Throwables;
import nl.topicus.jdbc.shaded.com.google.common.util.concurrent.Uninterruptibles;
import java.io.Serializable;
import java.util.concurrent.ExecutionException;
/**
* Base implementation for asynchronously consuming Google Cloud paginated results.
*
* @param the value type that the page holds
*/
@InternalApi
public class AsyncPageImpl extends PageImpl implements AsyncPage {
private static final long serialVersionUID = -6009473188630364906L;
private final NextPageFetcher asyncPageFetcher;
/**
* Interface for asynchronously fetching the next page of results from the service.
*
* @param the value type that the page holds
*/
public interface NextPageFetcher extends Serializable {
ApiFuture> getNextPage();
}
private static class SyncNextPageFetcher implements PageImpl.NextPageFetcher {
private static final long serialVersionUID = -4124568632363525351L;
private final NextPageFetcher asyncPageFetcher;
private SyncNextPageFetcher(NextPageFetcher asyncPageFetcher) {
this.asyncPageFetcher = asyncPageFetcher;
}
@Override
public Page getNextPage() {
try {
return asyncPageFetcher != null
? Uninterruptibles.getUninterruptibly(asyncPageFetcher.getNextPage()) : null;
} catch (ExecutionException ex) {
Throwables.throwIfUnchecked(ex.getCause());
throw new RuntimeException(ex);
}
}
}
/**
* Creates an {@code AsyncPageImpl} object.
*/
public AsyncPageImpl(NextPageFetcher asyncPageFetcher, String cursor, Iterable results) {
super(new SyncNextPageFetcher(asyncPageFetcher), cursor, results);
this.asyncPageFetcher = asyncPageFetcher;
}
@Override
public ApiFuture> getNextPageAsync() {
if (getNextPageToken() == null || asyncPageFetcher == null) {
return ApiFutures.immediateFuture(null);
}
return asyncPageFetcher.getNextPage();
}
}