All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.internal.operation.DistinctOperation Maven / Gradle / Ivy

Go to download

The Java operations layer for the MongoDB Java Driver. Third parties can wrap this layer to provide custom higher-level APIs

The newest version!
/*
 * 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.MongoNamespace;
import com.mongodb.client.model.Collation;
import com.mongodb.internal.async.AsyncBatchCursor;
import com.mongodb.internal.async.SingleResultCallback;
import com.mongodb.internal.binding.AsyncReadBinding;
import com.mongodb.internal.binding.ReadBinding;
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.codecs.Codec;
import org.bson.codecs.Decoder;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.internal.operation.AsyncOperationHelper.asyncSingleBatchCursorTransformer;
import static com.mongodb.internal.operation.AsyncOperationHelper.executeRetryableReadAsync;
import static com.mongodb.internal.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.internal.operation.DocumentHelper.putIfNotNull;
import static com.mongodb.internal.operation.OperationHelper.LOGGER;
import static com.mongodb.internal.operation.OperationReadConcernHelper.appendReadConcernToCommand;
import static com.mongodb.internal.operation.SyncOperationHelper.executeRetryableRead;
import static com.mongodb.internal.operation.SyncOperationHelper.singleBatchCursorTransformer;

/**
 * Finds the distinct values for a specified field across a single collection.
 *
 * 

This class is not part of the public API and may be removed or changed at any time

*/ public class DistinctOperation implements AsyncReadOperation>, ReadOperation> { private static final String VALUES = "values"; private final MongoNamespace namespace; private final String fieldName; private final Decoder decoder; private boolean retryReads; private BsonDocument filter; private Collation collation; private BsonValue comment; private BsonValue hint; public DistinctOperation(final MongoNamespace namespace, final String fieldName, final Decoder decoder) { this.namespace = notNull("namespace", namespace); this.fieldName = notNull("fieldName", fieldName); this.decoder = notNull("decoder", decoder); } public BsonDocument getFilter() { return filter; } public DistinctOperation filter(@Nullable final BsonDocument filter) { this.filter = filter; return this; } public DistinctOperation retryReads(final boolean retryReads) { this.retryReads = retryReads; return this; } public boolean getRetryReads() { return retryReads; } public Collation getCollation() { return collation; } public DistinctOperation collation(@Nullable final Collation collation) { this.collation = collation; return this; } public BsonValue getComment() { return comment; } public DistinctOperation comment(final BsonValue comment) { this.comment = comment; return this; } public BsonValue getHint() { return hint; } public DistinctOperation hint(@Nullable final BsonValue hint) { this.hint = hint; return this; } @Override public BatchCursor execute(final ReadBinding binding) { return executeRetryableRead(binding, namespace.getDatabaseName(), getCommandCreator(), createCommandDecoder(), singleBatchCursorTransformer(VALUES), retryReads); } @Override public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback> callback) { executeRetryableReadAsync(binding, namespace.getDatabaseName(), getCommandCreator(), createCommandDecoder(), asyncSingleBatchCursorTransformer(VALUES), retryReads, errorHandlingCallback(callback, LOGGER)); } private Codec createCommandDecoder() { return CommandResultDocumentCodec.create(decoder, VALUES); } private CommandCreator getCommandCreator() { return (operationContext, serverDescription, connectionDescription) -> { BsonDocument commandDocument = new BsonDocument("distinct", new BsonString(namespace.getCollectionName())); appendReadConcernToCommand(operationContext.getSessionContext(), connectionDescription.getMaxWireVersion(), commandDocument); commandDocument.put("key", new BsonString(fieldName)); putIfNotNull(commandDocument, "query", filter); if (collation != null) { commandDocument.put("collation", collation.asDocument()); } putIfNotNull(commandDocument, "comment", comment); putIfNotNull(commandDocument, "hint", hint); return commandDocument; }; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy