Adding .close() to Firestore Service by schmidt-sebastian · Pull Request #2807 · googleapis/google-cloud-java · GitHub
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 @@ -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;
}
}