Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mod v2 #1131

Closed
wants to merge 8 commits into from
Closed

Mod v2 #1131

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ jobs:
run: |
echo "::set-output name=yearmonth::$(/bin/date -u "+%Y-%m")"
shell: bash
- name: Set up JDK 8
- name: Set up JDK 11
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: 8
java-version: 11
# https://github.com/actions/cache/blob/main/examples.md#java---maven
- name: Cache local Maven repository
uses: actions/cache@v4
Expand Down
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,55 @@ private ClosableIterator<Tuple> doFind(MongoDBQueryDescriptor query, QueryParame
EntityKeyMetadata entityKeyMetadata) {
Document criteria = query.getCriteria();
Document orderby = query.getOrderBy();
int maxTimeMS = -1;

Document modifiers = new Document();
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" ) );
}

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

criteria = (Document) criteria.get( "$query" );
if ( criteria.containsKey( "$comment" ) ) {
prepareFind.comment( criteria.getString( "$comment" ) );
}
}
else {
prepareFind.filter( criteria );
}

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 +1512,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 +1839,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 +1852,6 @@ else if ( original.equals( writeConcern ) ) {

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

if ( original.getWObject() instanceof String ) {
Expand All @@ -1841,9 +1864,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 +1877,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,14 @@ 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
? new MongoClient( serverAddresses, clientOptions )
: new MongoClient( serverAddresses, credentials, clientOptions );
return 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
Loading
Loading