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

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

Go to download

The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson

The newest version!
/*
 * Copyright (c) 2008-2014 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.operation;

import com.mongodb.Function;
import com.mongodb.MongoNamespace;
import com.mongodb.async.AsyncBatchCursor;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncConnectionSource;
import com.mongodb.binding.AsyncReadBinding;
import com.mongodb.binding.ConnectionSource;
import com.mongodb.binding.ReadBinding;
import com.mongodb.connection.AsyncConnection;
import com.mongodb.connection.Connection;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.QueryResult;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.codecs.Codec;
import org.bson.codecs.Decoder;

import java.util.concurrent.TimeUnit;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol;
import static com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocolAsync;
import static com.mongodb.operation.DocumentHelper.putIfNotNull;
import static com.mongodb.operation.DocumentHelper.putIfNotZero;
import static com.mongodb.operation.OperationHelper.releasingCallback;
import static com.mongodb.operation.OperationHelper.withConnection;

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

When possible, the distinct command uses an index to find documents and return values.

* * @param the type of the distinct value * @mongodb.driver.manual reference/command/distinct Distinct Command * @since 3.0 */ 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 BsonDocument filter; private long maxTimeMS; /** * Construct an instance. * * @param namespace the database and collection namespace for the operation. * @param fieldName the name of the field to return distinct values. * @param decoder the decoder for the result documents. */ 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); } /** * Gets the query filter. * * @return the query filter * @mongodb.driver.manual reference/method/db.collection.find/ Filter */ public BsonDocument getFilter() { return filter; } /** * Sets the query filter to apply to the query. * * @param filter the query filter, which may be null. * @return this * @mongodb.driver.manual reference/method/db.collection.find/ Filter */ public DistinctOperation filter(final BsonDocument filter) { this.filter = filter; return this; } /** * Gets the maximum execution time on the server for this operation. The default is 0, which places no limit on the execution time. * * @param timeUnit the time unit to return the result in * @return the maximum execution time in the given time unit */ public long getMaxTime(final TimeUnit timeUnit) { notNull("timeUnit", timeUnit); return timeUnit.convert(maxTimeMS, TimeUnit.MILLISECONDS); } /** * Sets the maximum execution time on the server for this operation. * * @param maxTime the max time * @param timeUnit the time unit, which may not be null * @return this */ public DistinctOperation maxTime(final long maxTime, final TimeUnit timeUnit) { notNull("timeUnit", timeUnit); this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit); return this; } @Override public BatchCursor execute(final ReadBinding binding) { return withConnection(binding, new OperationHelper.CallableWithConnectionAndSource>() { @Override public BatchCursor call(final ConnectionSource source, final Connection connection) { return executeWrappedCommandProtocol(binding, namespace.getDatabaseName(), getCommand(), createCommandDecoder(), connection, transformer(source, connection)); } }); } @Override public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback> callback) { withConnection(binding, new OperationHelper.AsyncCallableWithConnectionAndSource() { @Override public void call(final AsyncConnectionSource source, final AsyncConnection connection, final Throwable t) { if (t != null) { errorHandlingCallback(callback).onResult(null, t); } else { executeWrappedCommandProtocolAsync(binding, namespace.getDatabaseName(), getCommand(), createCommandDecoder(), connection, asyncTransformer(connection.getDescription()), releasingCallback(errorHandlingCallback(callback), source, connection)); } } }); } private Codec createCommandDecoder() { return CommandResultDocumentCodec.create(decoder, VALUES); } private QueryResult createQueryResult(final BsonDocument result, final ConnectionDescription description) { return new QueryResult(namespace, BsonDocumentWrapperHelper.toList(result, VALUES), 0L, description.getServerAddress()); } private Function> transformer(final ConnectionSource source, final Connection connection) { return new Function>() { @Override public BatchCursor apply(final BsonDocument result) { QueryResult queryResult = createQueryResult(result, connection.getDescription()); return new QueryBatchCursor(queryResult, 0, 0, decoder, source); } }; } private Function> asyncTransformer(final ConnectionDescription connectionDescription) { return new Function>() { @Override public AsyncBatchCursor apply(final BsonDocument result) { QueryResult queryResult = createQueryResult(result, connectionDescription); return new AsyncQueryBatchCursor(queryResult, 0, 0, null); } }; } private BsonDocument getCommand() { BsonDocument cmd = new BsonDocument("distinct", new BsonString(namespace.getCollectionName())); cmd.put("key", new BsonString(fieldName)); putIfNotNull(cmd, "query", filter); putIfNotZero(cmd, "maxTimeMS", maxTimeMS); return cmd; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy