Skip to content

Commit

Permalink
separated code that depends only on the core driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Goina committed Aug 7, 2024
1 parent 9028e26 commit b0bafa9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.janelia.colormipsearch.dao.mongo.PPPmURLsMongoDao;
import org.janelia.colormipsearch.dao.mongo.PublishedLMImageMongoDao;
import org.janelia.colormipsearch.dao.mongo.PublishedURLsMongoDao;
import org.janelia.colormipsearch.dao.mongo.support.MongoDBHelper;
import org.janelia.colormipsearch.dao.mongo.support.MongoClientProvider;
import org.janelia.colormipsearch.model.AbstractNeuronEntity;
import org.janelia.colormipsearch.model.AbstractSessionEntity;
import org.janelia.colormipsearch.model.CDMatchEntity;
Expand All @@ -34,7 +34,8 @@ public static synchronized DaosProvider getInstance(Config config) {
private final IdGenerator idGenerator;

private DaosProvider(Config config) {
MongoClient mongoClient = MongoDBHelper.createMongoClient(config.getStringPropertyValue("MongoDB.ConnectionURL"),
MongoClient mongoClient = MongoClientProvider.createMongoClient(
config.getStringPropertyValue("MongoDB.ConnectionURL"),
config.getStringPropertyValue("MongoDB.Server"),
config.getStringPropertyValue("MongoDB.AuthDatabase"),
config.getStringPropertyValue("MongoDB.Username"),
Expand All @@ -48,7 +49,7 @@ private DaosProvider(Config config) {
config.getIntegerPropertyValue("MongoDB.MaxConnectionIdleSecs", 0),
config.getIntegerPropertyValue("MongoDB.MaxConnectionLifeSecs", 0)
);
this.mongoDatabase = MongoDBHelper.createMongoDatabase(mongoClient, config.getStringPropertyValue("MongoDB.Database"));
this.mongoDatabase = MongoClientProvider.createMongoDatabase(mongoClient, config.getStringPropertyValue("MongoDB.Database"));
this.idGenerator = new TimebasedIdGenerator(config.getIntegerPropertyValue("TimebasedId.Context", 0));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.janelia.colormipsearch.dao.mongo.support;

import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;

public class MongoClientProvider {

public static MongoClient createMongoClient(
String mongoConnectionURL,
String mongoServer,
String mongoAuthDatabase,
String mongoUsername,
String mongoPassword,
String mongoReplicaSet,
boolean useSSL,
int connectionsPerHost,
int connectTimeoutInMillis,
int maxConnecting,
int maxWaitTimeInSecs,
int maxConnectionIdleTimeInSecs,
int maxConnLifeTimeInSecs) {
return createMongoClient(
MongoSettingsProvider.prepareMongoSettings(
mongoConnectionURL, mongoServer, mongoAuthDatabase, mongoUsername, mongoPassword, mongoReplicaSet, useSSL,
connectionsPerHost, connectTimeoutInMillis, maxConnecting, maxWaitTimeInSecs, maxConnectionIdleTimeInSecs, maxConnLifeTimeInSecs
)
);
}

public static MongoClient createMongoClient(MongoClientSettings clientSettings) {
return MongoClients.create(clientSettings);
}

public static MongoDatabase createMongoDatabase(MongoClient mongoClient, String mongoDatabaseName) {
return mongoClient.getDatabase(mongoDatabaseName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;

import org.apache.commons.lang3.StringUtils;
import org.bson.codecs.configuration.CodecRegistries;
Expand All @@ -26,11 +23,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MongoDBHelper {
public class MongoSettingsProvider {

private static final Logger LOG = LoggerFactory.getLogger(MongoDBHelper.class);
private static final Logger LOG = LoggerFactory.getLogger(MongoSettingsProvider.class);

public static MongoClient createMongoClient(
public static MongoClientSettings prepareMongoSettings(
String mongoConnectionURL,
String mongoServer,
String mongoAuthDatabase,
Expand All @@ -44,7 +41,7 @@ public static MongoClient createMongoClient(
int maxWaitTimeInSecs,
int maxConnectionIdleTimeInSecs,
int maxConnLifeTimeInSecs) {
CodecRegistry codecRegistry = RegistryHelper.createCodecRegistry();
CodecRegistry codecRegistry = createCodecRegistry();
MongoClientSettings.Builder mongoClientSettingsBuilder = MongoClientSettings.builder()
.codecRegistry(CodecRegistries.fromRegistries(
MongoClientSettings.getDefaultCodecRegistry(),
Expand Down Expand Up @@ -101,14 +98,28 @@ public static MongoClient createMongoClient(
char[] passwordChars = StringUtils.isBlank(mongoPassword) ? null : mongoPassword.toCharArray();
mongoClientSettingsBuilder.credential(MongoCredential.createCredential(mongoUsername, mongoAuthDatabase, passwordChars));
}
return MongoClients.create(mongoClientSettingsBuilder.build());

return mongoClientSettingsBuilder.build();
}

public static MongoDatabase createMongoDatabase(MongoClient mongoClient, String mongoDatabaseName) {
return mongoClient.getDatabase(mongoDatabaseName);
private static CodecRegistry createCodecRegistry() {
ObjectMapper objectMapper = createMongoObjectMapper();

return CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(
new BigIntegerCodec(),
new FileDataCodec(objectMapper)
),
CodecRegistries.fromProviders(
new EnumCodecProvider()
),
CodecRegistries.fromProviders(
new JacksonCodecProvider(objectMapper)
)
);
}

static ObjectMapper createMongoObjectMapper() {
private static ObjectMapper createMongoObjectMapper() {
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter(MongoIgnoredFieldFilter.FILTER_NAME, new MongoIgnoredFieldFilter());
return new ObjectMapper()
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>5.0.1</version>
<version>5.1.2</version>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>5.0.1</version>
<version>5.1.2</version>
</dependency>

<!-- Testing -->
Expand Down

0 comments on commit b0bafa9

Please sign in to comment.