com.mongodb.internal.operation.EstimatedDocumentCountOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-core Show documentation
Show all versions of mongodb-driver-core Show documentation
The Java operations layer for the MongoDB Java Driver. Third parties can ' +
'wrap this layer to provide custom higher-level APIs
/*
* 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.internal.operation;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoNamespace;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.internal.async.SingleResultCallback;
import com.mongodb.internal.binding.AsyncReadBinding;
import com.mongodb.internal.binding.ReadBinding;
import com.mongodb.internal.session.SessionContext;
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.codecs.BsonDocumentCodec;
import org.bson.codecs.Decoder;
import java.util.concurrent.TimeUnit;
import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.operation.AsyncOperationHelper.CommandReadTransformerAsync;
import static com.mongodb.internal.operation.AsyncOperationHelper.executeRetryableReadAsync;
import static com.mongodb.internal.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.internal.operation.CommandOperationHelper.isNamespaceError;
import static com.mongodb.internal.operation.CommandOperationHelper.rethrowIfNotNamespaceError;
import static com.mongodb.internal.operation.DocumentHelper.putIfNotZero;
import static com.mongodb.internal.operation.OperationReadConcernHelper.appendReadConcernToCommand;
import static com.mongodb.internal.operation.SyncOperationHelper.CommandReadTransformer;
import static com.mongodb.internal.operation.SyncOperationHelper.executeRetryableRead;
import static java.util.Collections.singletonList;
/**
* This class is not part of the public API and may be removed or changed at any time
*/
public class EstimatedDocumentCountOperation implements AsyncReadOperation, ReadOperation {
private static final Decoder DECODER = new BsonDocumentCodec();
private final MongoNamespace namespace;
private boolean retryReads;
private long maxTimeMS;
private BsonValue comment;
public EstimatedDocumentCountOperation(final MongoNamespace namespace) {
this.namespace = notNull("namespace", namespace);
}
public EstimatedDocumentCountOperation retryReads(final boolean retryReads) {
this.retryReads = retryReads;
return this;
}
public EstimatedDocumentCountOperation maxTime(final long maxTime, final TimeUnit timeUnit) {
notNull("timeUnit", timeUnit);
this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit);
return this;
}
@Nullable
public BsonValue getComment() {
return comment;
}
public EstimatedDocumentCountOperation comment(@Nullable final BsonValue comment) {
this.comment = comment;
return this;
}
@Override
public Long execute(final ReadBinding binding) {
try {
return executeRetryableRead(binding, namespace.getDatabaseName(), getCommandCreator(binding.getSessionContext()),
CommandResultDocumentCodec.create(DECODER, singletonList("firstBatch")), transformer(), retryReads);
} catch (MongoCommandException e) {
return assertNotNull(rethrowIfNotNamespaceError(e, 0L));
}
}
@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback callback) {
executeRetryableReadAsync(binding, namespace.getDatabaseName(), getCommandCreator(binding.getSessionContext()),
CommandResultDocumentCodec.create(DECODER, singletonList("firstBatch")), asyncTransformer(), retryReads,
(result, t) -> {
if (isNamespaceError(t)) {
callback.onResult(0L, null);
} else {
callback.onResult(result, t);
}
});
}
private CommandReadTransformer transformer() {
return (result, source, connection) -> transformResult(result, connection.getDescription());
}
private CommandReadTransformerAsync asyncTransformer() {
return (result, source, connection) -> transformResult(result, connection.getDescription());
}
private long transformResult(final BsonDocument result, final ConnectionDescription connectionDescription) {
return (result.getNumber("n")).longValue();
}
private CommandCreator getCommandCreator(final SessionContext sessionContext) {
return (serverDescription, connectionDescription) -> {
BsonDocument document = new BsonDocument("count", new BsonString(namespace.getCollectionName()));
appendReadConcernToCommand(sessionContext, connectionDescription.getMaxWireVersion(), document);
putIfNotZero(document, "maxTimeMS", maxTimeMS);
if (comment != null) {
document.put("comment", comment);
}
return document;
};
}
}