![JAR search and dependency download from the Maven repository](/logo.png)
com.mongodb.MongoDatabaseImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
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;
import com.mongodb.client.ListCollectionsIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.operation.CommandReadOperation;
import com.mongodb.operation.CreateCollectionOperation;
import com.mongodb.operation.DropDatabaseOperation;
import com.mongodb.operation.OperationExecutor;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import static com.mongodb.MongoClient.getDefaultCodecRegistry;
import static com.mongodb.assertions.Assertions.notNull;
class MongoDatabaseImpl implements MongoDatabase {
private final String name;
private final ReadPreference readPreference;
private final CodecRegistry codecRegistry;
private final WriteConcern writeConcern;
private final OperationExecutor executor;
MongoDatabaseImpl(final String name, final CodecRegistry codecRegistry, final ReadPreference readPreference,
final WriteConcern writeConcern, final OperationExecutor executor) {
this.name = notNull("name", name);
this.codecRegistry = notNull("codecRegistry", codecRegistry);
this.readPreference = notNull("readPreference", readPreference);
this.writeConcern = notNull("writeConcern", writeConcern);
this.executor = notNull("executor", executor);
}
@Override
public String getName() {
return name;
}
@Override
public CodecRegistry getCodecRegistry() {
return codecRegistry;
}
@Override
public ReadPreference getReadPreference() {
return readPreference;
}
@Override
public WriteConcern getWriteConcern() {
return writeConcern;
}
@Override
public MongoDatabase withCodecRegistry(final CodecRegistry codecRegistry) {
return new MongoDatabaseImpl(name, codecRegistry, readPreference, writeConcern, executor);
}
@Override
public MongoDatabase withReadPreference(final ReadPreference readPreference) {
return new MongoDatabaseImpl(name, codecRegistry, readPreference, writeConcern, executor);
}
@Override
public MongoDatabase withWriteConcern(final WriteConcern writeConcern) {
return new MongoDatabaseImpl(name, codecRegistry, readPreference, writeConcern, executor);
}
@Override
public MongoCollection getCollection(final String collectionName) {
return getCollection(collectionName, Document.class);
}
@Override
public MongoCollection getCollection(final String collectionName, final Class documentClass) {
return new MongoCollectionImpl(new MongoNamespace(name, collectionName), documentClass, codecRegistry, readPreference,
writeConcern, executor);
}
@Override
public Document runCommand(final Bson command) {
return runCommand(command, Document.class);
}
@Override
public Document runCommand(final Bson command, final ReadPreference readPreference) {
return runCommand(command, readPreference, Document.class);
}
@Override
public TResult runCommand(final Bson command, final Class resultClass) {
return runCommand(command, ReadPreference.primary(), resultClass);
}
@Override
public TResult runCommand(final Bson command, final ReadPreference readPreference, final Class resultClass) {
notNull("readPreference", readPreference);
return executor.execute(new CommandReadOperation(getName(), toBsonDocument(command), codecRegistry.get(resultClass)),
readPreference);
}
@Override
public void drop() {
executor.execute(new DropDatabaseOperation(name));
}
@Override
public MongoIterable listCollectionNames() {
return new ListCollectionsIterableImpl(name, BsonDocument.class, getDefaultCodecRegistry(), ReadPreference.primary(),
executor).map(new Function() {
@Override
public String apply(final BsonDocument result) {
return result.getString("name").getValue();
}
});
}
@Override
public ListCollectionsIterable listCollections() {
return listCollections(Document.class);
}
@Override
public ListCollectionsIterable listCollections(final Class resultClass) {
return new ListCollectionsIterableImpl(name, resultClass, codecRegistry, ReadPreference.primary(), executor);
}
@Override
public void createCollection(final String collectionName) {
createCollection(collectionName, new CreateCollectionOptions());
}
@Override
public void createCollection(final String collectionName, final CreateCollectionOptions createCollectionOptions) {
executor.execute(new CreateCollectionOperation(name, collectionName)
.capped(createCollectionOptions.isCapped())
.sizeInBytes(createCollectionOptions.getSizeInBytes())
.autoIndex(createCollectionOptions.isAutoIndex())
.maxDocuments(createCollectionOptions.getMaxDocuments())
.usePowerOf2Sizes(createCollectionOptions.isUsePowerOf2Sizes())
.storageEngineOptions(toBsonDocument(createCollectionOptions.getStorageEngineOptions())));
}
private BsonDocument toBsonDocument(final Bson document) {
return document == null ? null : document.toBsonDocument(BsonDocument.class, codecRegistry);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy