com.mongodb.async.client.MongoIterableSubscription Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-async Show documentation
Show all versions of mongodb-driver-async Show documentation
The MongoDB Asynchronous Driver
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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 com.mongodb.async.client;
import com.mongodb.MongoException;
import com.mongodb.async.AsyncBatchCursor;
import com.mongodb.async.SingleResultCallback;
import java.util.List;
final class MongoIterableSubscription extends AbstractSubscription {
private final MongoIterable mongoIterable;
/* protected by `this` */
private boolean isReading;
private boolean completed;
/* protected by `this` */
private volatile AsyncBatchCursor batchCursor;
MongoIterableSubscription(final MongoIterable mongoIterable, final Observer super TResult> observer) {
super(observer);
this.mongoIterable = mongoIterable;
observer.onSubscribe(this);
}
@Override
void requestInitialData() {
mongoIterable.batchSize(calculateBatchSize());
mongoIterable.batchCursor(new SingleResultCallback>() {
@Override
public void onResult(final AsyncBatchCursor result, final Throwable t) {
if (t != null) {
onError(t);
} else if (result != null) {
batchCursor = result;
requestMoreData();
} else {
onError(new MongoException("Unexpected error, no AsyncBatchCursor returned from the MongoIterable."));
}
}
});
}
@Override
boolean checkCompleted() {
return completed;
}
@Override
void postTerminate() {
try {
if (batchCursor != null) {
batchCursor.close();
}
} catch (Exception e) {
// do nothing
}
}
@Override
void requestMoreData() {
boolean mustRead = false;
synchronized (this) {
if (!isReading && !isTerminated() && batchCursor != null) {
isReading = true;
mustRead = true;
}
}
if (mustRead) {
batchCursor.setBatchSize(calculateBatchSize());
batchCursor.next(new SingleResultCallback>() {
@Override
public void onResult(final List result, final Throwable t) {
synchronized (MongoIterableSubscription.this) {
isReading = false;
}
if (t != null) {
onError(t);
} else {
addToQueue(result);
synchronized (MongoIterableSubscription.this) {
if (result == null) {
completed = true;
}
}
tryProcessResultsQueue();
}
}
});
}
}
/**
* Returns the batchSize to be used with the cursor.
*
* If the batch size has been set on the MongoIterable that is used, otherwise the requested demand is used. When using requested
* demand, values less than 2 would close the cursor so that is the minimum batchSize and `Integer.MAX_VALUE` is the maximum.
*
* @return the batchSize to use
*/
private int calculateBatchSize() {
Integer batchSize = mongoIterable.getBatchSize();
if (batchSize != null) {
return batchSize;
}
long requested = getRequested();
if (requested <= 1) {
return 2;
} else if (requested < Integer.MAX_VALUE) {
return (int) requested;
} else {
return Integer.MAX_VALUE;
}
}
}