Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Upgraded MongoDB driver dependency to mongodb-driver-legacy 4.11.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jyemin committed Aug 15, 2024
1 parent 7773e66 commit eaef47d
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 75 deletions.
4 changes: 2 additions & 2 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<!-- MongoDB -->

<version.org.mongodb>3.6.2</version.org.mongodb>
<version.org.mongodb.mongo-java-driver>3.11.2</version.org.mongodb.mongo-java-driver>
<version.org.mongodb.mongo-java-driver>4.11.3</version.org.mongodb.mongo-java-driver>

<!-- Neo4j -->

Expand Down Expand Up @@ -489,7 +489,7 @@
<!-- MongoDB -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<artifactId>mongodb-driver-legacy</artifactId>
<version>${version.org.mongodb.mongo-java-driver}</version>
</dependency>

Expand Down
2 changes: 1 addition & 1 deletion featurepack/mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<artifactId>mongodb-driver-legacy</artifactId>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<module xmlns="urn:jboss:module:1.3" name="org.hibernate.ogm.mongodb" slot="${module-slot.org.hibernate.ogm.short-id}">
<resources>
<artifact name="${org.hibernate.ogm:hibernate-ogm-mongodb}" />
<artifact name="${org.mongodb:mongo-java-driver}" />
<artifact name="${org.mongodb:mongodb-driver-legacy}" />
</resources>
<dependencies>
<module name="org.hibernate.ogm" slot="${module-slot.org.hibernate.ogm.short-id}" />
Expand Down
2 changes: 1 addition & 1 deletion mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<artifactId>mongodb-driver-legacy</artifactId>
</dependency>
<dependency>
<groupId>org.parboiled</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.hibernate.AssertionFailure;
import org.hibernate.ogm.datastore.document.association.impl.DocumentHelpers;
Expand Down Expand Up @@ -1230,36 +1231,51 @@ private ClosableIterator<Tuple> doFind(MongoDBQueryDescriptor query, QueryParame
EntityKeyMetadata entityKeyMetadata) {
Document criteria = query.getCriteria();
Document orderby = query.getOrderBy();
int maxTimeMS = -1;

Document modifiers = new Document();
// We need to extract the different parts of the criteria and pass them to the cursor API
FindIterable<Document> prepareFind = collection.find();

// We need to extract the different parts of the criteria and pass them to the cursor API
if ( criteria.containsKey( "$query" ) ) {

prepareFind.filter( (Bson) criteria.get( "$query" ) );

if ( orderby == null ) {
orderby = (Document) criteria.get( "$orderby" );
}
maxTimeMS = criteria.getInteger( "$maxTimeMS", -1 );

addModifier( modifiers, criteria, "$hint" );
addModifier( modifiers, criteria, "$maxScan" );
addModifier( modifiers, criteria, "$snapshot", false );
addModifier( modifiers, criteria, "$min" );
addModifier( modifiers, criteria, "$max" );
addModifier( modifiers, criteria, "$comment" );
addModifier( modifiers, criteria, "$explain", false );
if ( criteria.containsKey( "$maxTimeMS") ) {
prepareFind.maxTime(criteria.getInteger("$maxTimeMS"), TimeUnit.MILLISECONDS);
}

if ( criteria.containsKey( "$hint") ) {
Object hint = criteria.get( "$hint" );
if ( hint instanceof String ) {
prepareFind.hintString( (String) hint );
} else if ( hint instanceof Bson ) {
prepareFind.hint( (Bson) hint );
}
}

if ( criteria.containsKey( "$min" ) ) {
prepareFind.min( (Bson) criteria.get( "$min" ) );
}

criteria = (Document) criteria.get( "$query" );
if ( criteria.containsKey( "$max" ) ) {
prepareFind.max( (Bson) criteria.get( "$max" ) );
}

if ( criteria.containsKey( "$comment" ) ) {
prepareFind.comment( criteria.getString( "$comment" ) );
}
}

FindIterable<Document> prepareFind = collection.find( criteria ).modifiers( modifiers ).projection( query.getProjection() );
prepareFind = prepareFind.projection( query.getProjection() );

if ( orderby != null ) {
prepareFind.sort( orderby );
}
if ( maxTimeMS > 0 ) {
prepareFind.maxTime( maxTimeMS, TimeUnit.MILLISECONDS );
}

// apply firstRow/maxRows if present
// apply firstRow/maxRows if present
if ( queryParameters.getRowSelection().getFirstRow() != null ) {
prepareFind.skip( queryParameters.getRowSelection().getFirstRow() );
}
Expand Down Expand Up @@ -1492,18 +1508,23 @@ private static int doDrop(MongoCollection<Document> collection) {
* @param obj A JSON object representing a write concern.
* @return The parsed write concern or <code>null</code> if <code>obj</code> is <code>null</code>.
*/
@SuppressWarnings("deprecation")
private static WriteConcern getWriteConcern(Document obj) {
WriteConcern wc = null;
if ( obj != null ) {
Object w = obj.get( "w" );
Boolean j = (Boolean) obj.get( "j" );
Integer t = (Integer) obj.get( "wtimeout" );
if ( w instanceof String ) {
wc = new WriteConcern( (String) w, ( t != null ? t : 0 ), false, ( j != null ? j : false ) );
wc = new WriteConcern( (String) w );
}
if ( w instanceof Number ) {
wc = new WriteConcern( ( (Number) w ).intValue(), ( t != null ? t : 0 ), false, ( j != null ? j : false ) );
wc = new WriteConcern( ( (Number) w ).intValue() );
}
if (t != null) {
wc = wc.withWTimeout(t, TimeUnit.MILLISECONDS);
}
if (j != null) {
wc = wc.withJournal(j);
}
}
return wc;
Expand Down Expand Up @@ -1814,7 +1835,6 @@ private static WriteConcern getWriteConcern(AssociationContext associationContex
*
* Thus, for each parameter of the write concern, we keep the stricter one for the resulting merged write concern.
*/
@SuppressWarnings("deprecation")
private static WriteConcern mergeWriteConcern(WriteConcern original, WriteConcern writeConcern) {
if ( original == null ) {
return writeConcern;
Expand All @@ -1828,7 +1848,6 @@ else if ( original.equals( writeConcern ) ) {

Object wObject;
int wTimeoutMS;
boolean fsync;
Boolean journal;

if ( original.getWObject() instanceof String ) {
Expand All @@ -1841,9 +1860,7 @@ else if ( writeConcern.getWObject() instanceof String ) {
wObject = Math.max( original.getW(), writeConcern.getW() );
}

wTimeoutMS = Math.min( original.getWtimeout(), writeConcern.getWtimeout() );

fsync = original.getFsync() || writeConcern.getFsync();
wTimeoutMS = Math.min( original.getWTimeout(TimeUnit.MILLISECONDS), writeConcern.getWTimeout(TimeUnit.MILLISECONDS) );

if ( original.getJournal() == null ) {
journal = writeConcern.getJournal();
Expand All @@ -1856,10 +1873,10 @@ else if ( writeConcern.getJournal() == null ) {
}

if ( wObject instanceof String ) {
return new WriteConcern( (String) wObject, wTimeoutMS, fsync, journal );
return new WriteConcern( (String) wObject).withWTimeout(wTimeoutMS, TimeUnit.MILLISECONDS).withJournal(journal);
}
else {
return new WriteConcern( (int) wObject, wTimeoutMS, fsync, journal );
return new WriteConcern( (int) wObject).withWTimeout(wTimeoutMS, TimeUnit.MILLISECONDS).withJournal(journal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mongodb.ReadConcern;
Expand Down Expand Up @@ -144,15 +142,12 @@ private String getAuthenticationDatabaseName() {
return authenticationDatabaseName;
}

public List<MongoCredential> buildCredentials() {
public MongoCredential buildCredential() {
if ( getUsername() != null ) {
return Collections.singletonList(
authenticationMechanism.createCredential(
return authenticationMechanism.createCredential(
getUsername(),
getAuthenticationDatabaseName(),
getPassword()
)
);
getPassword());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ private void startClientAndExtractDatabase() {

protected MongoClient createMongoClient(MongoDBConfiguration config) {
MongoClientOptions clientOptions = config.buildOptions();
List<MongoCredential> credentials = config.buildCredentials();
MongoCredential credential = config.buildCredential();
log.connectingToMongo( config.getHosts().toString(), clientOptions.getConnectTimeout() );
try {
List<ServerAddress> serverAddresses = new ArrayList<>( config.getHosts().size() );
for ( Hosts.HostAndPort hostAndPort : config.getHosts() ) {
serverAddresses.add( new ServerAddress( hostAndPort.getHost(), hostAndPort.getPort() ) );
}
return credentials == null
return credential == null
? new MongoClient( serverAddresses, clientOptions )
: new MongoClient( serverAddresses, credentials, clientOptions );
: new MongoClient( serverAddresses, credential, clientOptions );
}
catch (RuntimeException e) {
throw log.unableToInitializeMongoDB( e );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public MongoCredential createCredential(String username, String databaseName, St

@Override
public MongoCredential createCredential(String username, String databaseName, String password) {
return MongoCredential.createMongoCRCredential( username, databaseName, asCharArray( password ) );
return MongoCredential.createCredential( username, databaseName, asCharArray( password ) );
}
},
PLAIN {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum WriteConcernType {
* Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush
* the data to disk.
*/
FSYNCED(WriteConcern.FSYNCED),
FSYNCED(WriteConcern.JOURNALED),

/**
* Exceptions are raised for network issues, and server errors; the write operation waits for the server to group
Expand All @@ -46,7 +46,7 @@ public enum WriteConcernType {
* Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write
* operation.
*/
REPLICA_ACKNOWLEDGED(WriteConcern.REPLICA_ACKNOWLEDGED),
REPLICA_ACKNOWLEDGED(WriteConcern.W2),

/**
* Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testDefaultAuthenticationMechanism() throws Exception {
// will start the service
TestHelper.getDefaultTestStandardServiceRegistry( cfg ).getService( DatastoreProvider.class );

assertThat( provider.leakingClient.getCredentialsList().get( 0 ).getMechanism() ).isEqualTo( null );
assertThat( provider.leakingClient.getCredential().getMechanism() ).isEqualTo( null );
}

@Test
Expand All @@ -94,8 +94,7 @@ public void testSCRAMSHA1AuthenticationMechanism() throws Exception {
TestHelper.getDefaultTestStandardServiceRegistry( cfg ).getService( DatastoreProvider.class );

assertThat(
provider.leakingClient.getCredentialsList()
.get( 0 )
provider.leakingClient.getCredential()
.getMechanism()
).isEqualTo( MongoCredential.SCRAM_SHA_1_MECHANISM );
}
Expand All @@ -111,8 +110,7 @@ public void testX509AuthenticationMechanism() throws Exception {
TestHelper.getDefaultTestStandardServiceRegistry( cfg ).getService( DatastoreProvider.class );

assertThat(
provider.leakingClient.getCredentialsList()
.get( 0 )
provider.leakingClient.getCredential()
.getMechanism()
).isEqualTo( MongoCredential.MONGODB_X509_MECHANISM );
}
Expand All @@ -127,8 +125,7 @@ public void testGSSAPIAuthenticationMechanism() throws Exception {
TestHelper.getDefaultTestStandardServiceRegistry( cfg ).getService( DatastoreProvider.class );

assertThat(
provider.leakingClient.getCredentialsList()
.get( 0 )
provider.leakingClient.getCredential()
.getMechanism()
).isEqualTo( MongoCredential.GSSAPI_MECHANISM );
}
Expand All @@ -143,8 +140,7 @@ public void testPlainAuthenticationMechanism() throws Exception {
TestHelper.getDefaultTestStandardServiceRegistry( cfg ).getService( DatastoreProvider.class );

assertThat(
provider.leakingClient.getCredentialsList()
.get( 0 )
provider.leakingClient.getCredential()
.getMechanism()
).isEqualTo( MongoCredential.PLAIN_MECHANISM );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public void shouldApplyValueGivenViaProperties() {

@Test
public void shouldApplyValueGivenViaGlobalOptions() {
configuration.writeConcern( WriteConcernType.FSYNCED );
configuration.writeConcern( WriteConcernType.MAJORITY );

MongoDBConfiguration config = new MongoDBConfiguration( reader, getGlobalOptions() );
assertEquals( config.buildOptions().getWriteConcern(), WriteConcern.FSYNCED );
assertEquals( config.buildOptions().getWriteConcern(), WriteConcern.MAJORITY );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void setupBuilder() {
@Test
public void testWriteConcernForEntity() throws Exception {
OptionsContainer options = source.getEntityOptions( EntityWriteConcernExample.class );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( com.mongodb.WriteConcern.REPLICA_ACKNOWLEDGED );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( com.mongodb.WriteConcern.W2 );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public void setupBuilder() {
@Test
public void testWriteConcernGivenByTypeOnGlobalLevel() throws Exception {
mongoOptions
.writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED );
.writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED);

OptionsContainer options = getSource().getGlobalOptions();
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.REPLICA_ACKNOWLEDGED );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.W2 );
}

@Test
Expand All @@ -62,20 +62,20 @@ public void testWriteConcernGivenByInstanceOnGlobalLevel() throws Exception {
@Test
public void testWriteConcernGivenByTypePriority() throws Exception {
mongoOptions
.writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED )
.writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED)
.entity( ExampleForMongoDBMapping.class )
.writeConcern( WriteConcernType.MAJORITY )
.property( "content", ElementType.FIELD )
.writeConcern( WriteConcernType.FSYNCED );
.writeConcern( WriteConcernType.JOURNALED );

OptionsContainer options = getSource().getGlobalOptions();
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.REPLICA_ACKNOWLEDGED );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.W2 );

options = getSource().getEntityOptions( ExampleForMongoDBMapping.class );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.MAJORITY );

options = getSource().getPropertyOptions( ExampleForMongoDBMapping.class, "content" );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.FSYNCED );
assertThat( options.getUnique( WriteConcernOption.class ) ).isEqualTo( WriteConcern.ACKNOWLEDGED.withJournal(true) );
}

@Test
Expand Down Expand Up @@ -135,7 +135,7 @@ private static final class ExampleForMongoDBMapping {
private static class ReplicaConfigurableWriteConcern extends WriteConcern {

public ReplicaConfigurableWriteConcern(int numberOfRequiredReplicas) {
super( numberOfRequiredReplicas, 0, false, true );
super( numberOfRequiredReplicas );
}
}
}
Loading

0 comments on commit eaef47d

Please sign in to comment.