Skip to content

Commit

Permalink
OGM-1141 Add support for MongoDB 3.2
Browse files Browse the repository at this point in the history
MongoDB 3.2 does not support the $query syntax anymore so we have to
extract the different parts of the criteria to use the cursor API.
  • Loading branch information
gsmet authored and Sanne committed Oct 4, 2016
1 parent f4a3d94 commit 746e6fb
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bom/pom.xml
Expand Up @@ -25,8 +25,8 @@
<classmateVersion>1.0.0</classmateVersion>
<ehcacheVersion>2.6.9</ehcacheVersion>
<infinispanVersion>8.2.1.Final</infinispanVersion>
<mongodbVersion>3.2.2</mongodbVersion>
<fongodbVersion>2.0.6</fongodbVersion>
<mongodbVersion>3.3.0</mongodbVersion>
<fongodbVersion>2.0.7</fongodbVersion>
<neo4jVersion>2.3.5</neo4jVersion>
<!-- Update dependency versions accordingly when updating C* -->
<cassandraVersion>3.0.2</cassandraVersion>
Expand Down
Expand Up @@ -7,7 +7,8 @@ written in C++ with strong emphasis on ease of use.
The nested nature of documents make it a particularly natural fit for most object representations.

This implementation is based upon the MongoDB Java driver.
The currently supported version is {mongodb-version}.

We currently support version {mongodb-version} and 3.2.

=== Configuring MongoDB

Expand Down
Expand Up @@ -94,6 +94,7 @@
import org.hibernate.ogm.type.impl.StringCalendarDateType;
import org.hibernate.ogm.type.spi.GridType;
import org.hibernate.ogm.util.impl.CollectionHelper;
import org.hibernate.ogm.util.impl.StringHelper;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.parboiled.Parboiled;
Expand Down Expand Up @@ -912,9 +913,79 @@ private static DBObject stage(String key, Object value) {

private static ClosableIterator<Tuple> doFind(MongoDBQueryDescriptor query, QueryParameters queryParameters, DBCollection collection,
EntityKeyMetadata entityKeyMetadata) {
DBCursor cursor = collection.find( query.getCriteria(), query.getProjection() );
if ( query.getOrderBy() != null ) {
cursor.sort( query.getOrderBy() );
BasicDBObject criteria = (BasicDBObject) query.getCriteria();
DBObject orderby = query.getOrderBy();
String hintIndexName = null;
DBObject hintDBObject = null;
int maxScan = -1;
long maxTimeMS = -1;
boolean snapshot = false;
DBObject min = null;
DBObject max = null;
String comment = null;
boolean explain = false;

// We need to extract the different parts of the criteria and pass them to the cursor API
if ( criteria.containsField( "$query" ) ) {
if ( orderby == null ) {
orderby = (DBObject) criteria.get( "$orderby" );
}

Object hintObject = criteria.get( "$hint" );
if ( hintObject instanceof String ) {
hintIndexName = (String) hintObject;
}
else if ( hintObject instanceof DBObject ) {
hintDBObject = (DBObject) hintObject;
}
maxScan = criteria.getInt( "$maxScan", -1 );
maxTimeMS = criteria.getLong( "$maxTimeMS", -1 );

snapshot = criteria.getBoolean( "$snapshot", false );
min = (DBObject) criteria.get( "$min" );
max = (DBObject) criteria.get( "$max" );

comment = criteria.getString( "$comment" );
explain = criteria.getBoolean( "$explain", false );

criteria = (BasicDBObject) criteria.get( "$query" );
}

DBCursor cursor = collection.find( criteria, query.getProjection() );

if ( orderby != null ) {
cursor.sort( orderby );
}

if ( !StringHelper.isNullOrEmptyString( hintIndexName ) ) {
cursor.hint( hintIndexName );
}
if ( hintDBObject != null ) {
cursor.hint( hintDBObject );
}
if ( maxScan > 0 ) {
cursor.maxScan( maxScan );
}
if ( maxTimeMS > 0 ) {
cursor.maxTime( maxTimeMS, TimeUnit.MILLISECONDS );
}
// orderby and snapshot are exclusive
if ( orderby == null && snapshot ) {
cursor.snapshot();
}
if ( min != null ) {
cursor.min( min );
}
if ( max != null ) {
cursor.max( max );
}

if ( !StringHelper.isNullOrEmptyString( comment ) ) {
cursor.comment( comment );
}

if ( explain ) {
cursor.explain();
}

// apply firstRow/maxRows if present
Expand Down
Expand Up @@ -244,6 +244,25 @@ public void testQueryWithRegexOperator() throws Exception {
session.close();
}

@Test
public void testQueryWithOptions() throws Exception {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();

String nativeQuery = "{ $query : { author : 'Oscar Wilde' }, $orderby : { name : 1 }, $explain: true, $comment: 'a very useful comment',"
+ "$maxTimeMS: 500 }";
@SuppressWarnings("unchecked")
List<OscarWildePoem> result = session.createNativeQuery( nativeQuery )
.addEntity( OscarWildePoem.TABLE_NAME, OscarWildePoem.class )
.list();

assertThat( result ).onProperty( "id" ).containsExactly( 2L, 3L, 1L );

transaction.commit();
session.clear();
session.close();
}

private void assertAreEquals(OscarWildePoem expectedPoem, OscarWildePoem poem) {
assertThat( poem ).isNotNull();
assertThat( poem.getId() ).as( "Wrong Id" ).isEqualTo( expectedPoem.getId() );
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -57,7 +57,7 @@
<skipUnitTests>false</skipUnitTests>

<!-- The following properties configure the embedded MongoDB instance used for tests -->
<embeddedMongoDbVersion>3.0.2</embeddedMongoDbVersion>
<embeddedMongoDbVersion>3.0.12</embeddedMongoDbVersion>
<embeddedMongoDbBindIp>127.0.0.1</embeddedMongoDbBindIp>
<embeddedMongoDbTempDir>${project.build.directory}${file.separator}embeddedMongoDb${file.separator}extracted</embeddedMongoDbTempDir>
<embeddedMongoDbLogDir>${project.build.directory}${file.separator}embeddedMongoDb${file.separator}logs</embeddedMongoDbLogDir>
Expand Down

0 comments on commit 746e6fb

Please sign in to comment.