package com.mongodb.operation;
import com.mongodb.MongoNamespace;
import com.mongodb.WriteConcern;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncWriteBinding;
import com.mongodb.binding.WriteBinding;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.ServerDescription;
import com.mongodb.session.SessionContext;
import org.bson.BsonDocument;
import org.bson.BsonInt64;
import org.bson.FieldNameValidator;
import org.bson.codecs.Decoder;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.operation.CommandOperationHelper.executeRetryableCommand;
import static com.mongodb.operation.OperationHelper.isRetryableWrite;
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo;
@Deprecated
public abstract class BaseFindAndModifyOperation<T> implements AsyncWriteOperation<T>, WriteOperation<T> {
private final MongoNamespace namespace;
private final WriteConcern writeConcern;
private final boolean retryWrites;
private final Decoder<T> decoder;
protected BaseFindAndModifyOperation(final MongoNamespace namespace, final WriteConcern writeConcern,
final boolean retryWrites, final Decoder<T> decoder) {
this.namespace = notNull("namespace", namespace);
this.writeConcern = notNull("writeConcern", writeConcern);
this.retryWrites = retryWrites;
this.decoder = notNull("decoder", decoder);
}
@Override
public T execute(final WriteBinding binding) {
return executeRetryableCommand(binding, getDatabaseName(), null, getFieldNameValidator(),
CommandResultDocumentCodec.create(getDecoder(), "value"),
getCommandCreator(binding.getSessionContext()),
FindAndModifyHelper.<T>transformer());
}
@Override
public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<T> callback) {
executeRetryableCommand(binding, getDatabaseName(), null, getFieldNameValidator(),
CommandResultDocumentCodec.create(getDecoder(), "value"),
getCommandCreator(binding.getSessionContext()), FindAndModifyHelper.<T>asyncTransformer(), callback);
}
protected abstract String getDatabaseName();
public MongoNamespace getNamespace() {
return namespace;
}
public WriteConcern getWriteConcern() {
return writeConcern;
}
public Decoder<T> getDecoder() {
return decoder;
}
public boolean isRetryWrites() {
return retryWrites;
}
protected abstract CommandCreator getCommandCreator(SessionContext sessionContext);
protected void addTxnNumberToCommand(final ServerDescription serverDescription, final ConnectionDescription connectionDescription,
final BsonDocument commandDocument, final SessionContext sessionContext) {
if (isRetryableWrite(isRetryWrites(), getWriteConcern(), serverDescription, connectionDescription, sessionContext)) {
commandDocument.put("txnNumber", new BsonInt64(sessionContext.advanceTransactionNumber()));
}
}
protected void addWriteConcernToCommand(final ConnectionDescription connectionDescription, final BsonDocument commandDocument,
final SessionContext sessionContext) {
if (getWriteConcern().isAcknowledged() && !getWriteConcern().isServerDefault()
&& serverIsAtLeastVersionThreeDotTwo(connectionDescription) && !sessionContext.hasActiveTransaction()) {
commandDocument.put("writeConcern", getWriteConcern().asDocument());
}
}
protected abstract FieldNameValidator getFieldNameValidator();
}