package com.mongodb.client.internal;
import com.mongodb.MongoNamespace;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.client.DistinctIterable;
import com.mongodb.client.model.Collation;
import com.mongodb.internal.operation.SyncOperations;
import com.mongodb.lang.Nullable;
import com.mongodb.operation.BatchCursor;
import com.mongodb.operation.ReadOperation;
import com.mongodb.client.ClientSession;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import java.util.concurrent.TimeUnit;
import static com.mongodb.assertions.Assertions.notNull;
class DistinctIterableImpl<TDocument, TResult> extends MongoIterableImpl<TResult> implements DistinctIterable<TResult> {
private final SyncOperations<TDocument> operations;
private final Class<TResult> resultClass;
private final String fieldName;
private Bson filter;
private long maxTimeMS;
private Collation collation;
DistinctIterableImpl(@Nullable final ClientSession clientSession, final MongoNamespace namespace, final Class<TDocument> documentClass,
final Class<TResult> resultClass, final CodecRegistry codecRegistry, final ReadPreference readPreference,
final ReadConcern readConcern, final OperationExecutor executor, final String fieldName, final Bson filter) {
this(clientSession, namespace, documentClass, resultClass, codecRegistry, readPreference, readConcern, executor, fieldName,
filter, true);
}
DistinctIterableImpl(@Nullable final ClientSession clientSession, final MongoNamespace namespace, final Class<TDocument> documentClass,
final Class<TResult> resultClass, final CodecRegistry codecRegistry, final ReadPreference readPreference,
final ReadConcern readConcern, final OperationExecutor executor, final String fieldName, final Bson filter,
final boolean retryReads) {
super(clientSession, executor, readConcern, readPreference, retryReads);
this.operations = new SyncOperations<TDocument>(namespace, documentClass, readPreference, codecRegistry, retryReads);
this.resultClass = notNull("resultClass", resultClass);
this.fieldName = notNull("mapFunction", fieldName);
this.filter = filter;
}
@Override
public DistinctIterable<TResult> filter(@Nullable final Bson filter) {
this.filter = filter;
return this;
}
@Override
public DistinctIterable<TResult> maxTime(final long maxTime, final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit);
return this;
}
@Override
public DistinctIterable<TResult> batchSize(final int batchSize) {
super.batchSize(batchSize);
return this;
}
@Override
public DistinctIterable<TResult> collation(@Nullable final Collation collation) {
this.collation = collation;
return this;
}
@Override
public ReadOperation<BatchCursor<TResult>> asReadOperation() {
return operations.distinct(fieldName, filter, resultClass, maxTimeMS, collation);
}
}