Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import javax.annotation.Nonnull;

/** Represents a Firestore Database and is the entry point for all Firestore operations */
public interface Firestore extends Service<FirestoreOptions> {
public interface Firestore extends Service<FirestoreOptions>, AutoCloseable {

/**
* Gets a {@link CollectionReference} that refers to the collection at the specified path.
Expand Down Expand Up @@ -91,4 +91,12 @@ <T> ApiFuture<T> runTransaction(
*/
@Nonnull
WriteBatch batch();

/**
* Closes the gRPC channels associated with this instance and frees up their resources. This
* method blocks until all channels are closed. Once this method is called, this Firestore client
* is no longer usable.
*/
@Override
void close() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,12 @@ class FirestoreImpl implements Firestore {
private static final String AUTO_ID_ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

/** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */
static String autoId() {
StringBuilder builder = new StringBuilder();
int maxRandom = AUTO_ID_ALPHABET.length();
for (int i = 0; i < AUTO_ID_LENGTH; i++) {
builder.append(AUTO_ID_ALPHABET.charAt(RANDOM.nextInt(maxRandom)));
}
return builder.toString();
}

private final FirestoreRpc firestoreClient;
private final FirestoreOptions firestoreOptions;
private final ResourcePath databasePath;

private boolean closed;

FirestoreImpl(FirestoreOptions options) {
this(options, options.getFirestoreRpc());
}
Expand All @@ -81,6 +73,16 @@ static String autoId() {
ResourcePath.create(DatabaseRootName.of(options.getProjectId(), options.getDatabaseId()));
}

/** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */
static String autoId() {
StringBuilder builder = new StringBuilder();
int maxRandom = AUTO_ID_ALPHABET.length();
for (int i = 0; i < AUTO_ID_LENGTH; i++) {
builder.append(AUTO_ID_ALPHABET.charAt(RANDOM.nextInt(maxRandom)));
}
return builder.toString();
}

@Nonnull
@Override
public WriteBatch batch() {
Expand Down Expand Up @@ -324,6 +326,7 @@ FirestoreRpc getClient() {
/** Request funnel for all read/write requests. */
<RequestT, ResponseT> ApiFuture<ResponseT> sendRequest(
RequestT requestT, UnaryCallable<RequestT, ResponseT> callable) {
Preconditions.checkState(!closed, "Firestore client has already been closed");
return callable.futureCall(requestT);
}

Expand All @@ -332,18 +335,26 @@ <RequestT, ResponseT> void streamRequest(
RequestT requestT,
ApiStreamObserver<ResponseT> responseObserverT,
ServerStreamingCallable<RequestT, ResponseT> callable) {
Preconditions.checkState(!closed, "Firestore client has already been closed");
callable.serverStreamingCall(requestT, responseObserverT);
}

/** Request funnel for all bidirectional streaming requests. */
<RequestT, ResponseT> ApiStreamObserver<RequestT> streamRequest(
ApiStreamObserver<ResponseT> responseObserverT,
BidiStreamingCallable<RequestT, ResponseT> callable) {
Preconditions.checkState(!closed, "Firestore client has already been closed");
return callable.bidiStreamingCall(responseObserverT);
}

@Override
public FirestoreOptions getOptions() {
return firestoreOptions;
}

@Override
public void close() throws Exception {
firestoreClient.close();
closed = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -92,6 +93,11 @@ public void before() {
randomDoc = randomColl.document();
}

@After
public void after() throws Exception {
firestore.close();
}

private DocumentReference addDocument(String key, Object value, Object... fields)
throws Exception {
DocumentReference documentReference = randomColl.document();
Expand Down