
com.mongodb.async.client.AwaitingWriteOperationIterable 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 all versions of mongodb-driver-async
The MongoDB Asynchronous Driver
package com.mongodb.async.client;
import com.mongodb.Block;
import com.mongodb.Function;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.AsyncBatchCursor;
import com.mongodb.operation.AsyncOperationExecutor;
import com.mongodb.operation.AsyncWriteOperation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
class AwaitingWriteOperationIterable implements MongoIterable {
private final MongoIterable delegated;
private final List> callbacks = new ArrayList>();
private boolean writeCompleted;
private Throwable thrown;
AwaitingWriteOperationIterable(final AsyncWriteOperation writeOperation, final AsyncOperationExecutor executor,
final MongoIterable delegated) {
this.delegated = delegated;
executor.execute(writeOperation, new SingleResultCallback() {
@Override
public void onResult(final W result, final Throwable t) {
synchronized (AwaitingWriteOperationIterable.this) {
writeCompleted = true;
thrown = t;
for (SingleResultCallback cur : callbacks) {
cur.onResult(null, t);
}
}
}
});
}
@Override
public void first(final SingleResultCallback callback) {
boolean localWriteCompleted;
synchronized (this) {
localWriteCompleted = writeCompleted;
if (!localWriteCompleted) {
callbacks.add(new SingleResultCallback() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
delegated.first(callback);
}
}
});
}
}
if (localWriteCompleted) {
if (thrown != null) {
callback.onResult(null, thrown);
} else {
delegated.first(callback);
}
}
}
@Override
public void forEach(final Block super T> block, final SingleResultCallback callback) {
boolean localWriteCompleted;
synchronized (this) {
localWriteCompleted = writeCompleted;
if (!localWriteCompleted) {
callbacks.add(new SingleResultCallback() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
delegated.forEach(block, callback);
}
}
});
}
}
if (localWriteCompleted) {
if (thrown != null) {
callback.onResult(null, thrown);
} else {
delegated.forEach(block, callback);
}
}
}
@Override
public > void into(final A target, final SingleResultCallback callback) {
boolean localWriteCompleted;
synchronized (this) {
localWriteCompleted = writeCompleted;
if (!localWriteCompleted) {
callbacks.add(new SingleResultCallback() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
delegated.into(target, callback);
}
}
});
}
}
if (localWriteCompleted) {
if (thrown != null) {
callback.onResult(null, thrown);
} else {
delegated.into(target, callback);
}
}
}
@Override
public MongoIterable map(final Function mapper) {
return new MappingIterable(this, mapper);
}
@Override
public AwaitingWriteOperationIterable batchSize(final int batchSize) {
delegated.batchSize(batchSize);
return this;
}
@Override
public void batchCursor(final SingleResultCallback> callback) {
boolean localWriteCompleted;
synchronized (this) {
localWriteCompleted = writeCompleted;
if (!localWriteCompleted) {
callbacks.add(new SingleResultCallback() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
delegated.batchCursor(callback);
}
}
});
}
}
if (localWriteCompleted) {
if (thrown != null) {
callback.onResult(null, thrown);
} else {
delegated.batchCursor(callback);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy