mongo-entitystore: remove usage of deprecated mongo client APIs
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/7ac53388 Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/7ac53388 Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/7ac53388 Branch: refs/heads/develop Commit: 7ac5338854990d488d7165d2c2c21fa70b91035b Parents: d3ae3cd Author: Paul Merlin <[email protected]> Authored: Sat Dec 3 12:03:51 2016 +0100 Committer: Paul Merlin <[email protected]> Committed: Sat Dec 3 12:03:51 2016 +0100 ---------------------------------------------------------------------- dependencies.gradle | 2 +- .../entitystore/mongodb/MongoAccessors.java | 6 +- .../mongodb/MongoEntityStoreConfiguration.java | 46 +++++--- .../mongodb/MongoMapEntityStoreMixin.java | 104 ++++++++++--------- .../mongodb/EmbedMongoMapEntityStoreTest.java | 2 +- .../mongodb/MongoMapEntityStoreTest.java | 2 +- .../MongoMapEntityStoreWithCacheTest.java | 2 +- 7 files changed, 94 insertions(+), 70 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/dependencies.gradle ---------------------------------------------------------------------- diff --git a/dependencies.gradle b/dependencies.gradle index b14783e..a56c160 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -63,7 +63,7 @@ def jettyVersion = '9.2.17.v20160517' // 9.3.x Tests fail! def leveldbVersion = '0.9' def leveldbJniVersion = '1.8' def liquibaseVersion = '3.5.3' -def mongodbVersion = '3.3.0' +def mongodbVersion = '3.4.0' def restletVersion = '2.3.7' def rdfVersion = '2.7.16' // 2.8.x change query results!! 4.x exists def riakVersion = '2.1.0' http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java index a043329..4dea5e8 100644 --- a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java +++ b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoAccessors.java @@ -19,16 +19,14 @@ */ package org.apache.zest.entitystore.mongodb; -import com.mongodb.DB; import com.mongodb.MongoClient; +import com.mongodb.client.MongoDatabase; public interface MongoAccessors { - MongoClient mongoInstanceUsed(); - DB dbInstanceUsed(); + MongoDatabase dbInstanceUsed(); String collectionUsed(); - } http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java index 835095f..ee1d32e 100644 --- a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java +++ b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoEntityStoreConfiguration.java @@ -57,21 +57,39 @@ public interface MongoEntityStoreConfiguration enum WriteConcern { - - /** Exceptions are raised for network issues, but not server errors */ - NORMAL, - /** Exceptions are raised for network issues, and server errors; waits on a server for the write operation */ - SAFE, - /** Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation */ - MAJORITY, - /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk*/ - FSYNC_SAFE, - /** Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk*/ - JOURNAL_SAFE, - /** Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/ - REPLICAS_SAFE; + /** + * Write operations that use this write concern will wait for acknowledgement, + * using the default write concern configured on the server. + * This is the default value. + */ + ACKNOWLEDGED, + /** + * Write operations that use this write concern will wait for acknowledgement from a single member. + */ + W1, + /** + * Write operations that use this write concern will wait for acknowledgement from two members. + */ + W2, + /** + * Write operations that use this write concern will wait for acknowledgement from three members. + */ + W3, + /** + * Write operations that use this write concern will return as soon as the message is written to the socket. + * Exceptions are raised for network issues, but not server errors. + */ + UNACKNOWLEDGED, + /** + * Write operations wait for the server to group commit to the journal file on disk. + */ + JOURNALED, + /** + * Exceptions are raised for network issues, and server errors; + * waits on a majority of servers for the write operation. + */ + MAJORITY; } - } // END SNIPPET: config \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java index 93f5af0..9b988f5 100644 --- a/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java +++ b/extensions/entitystore-mongodb/src/main/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreMixin.java @@ -20,14 +20,15 @@ package org.apache.zest.entitystore.mongodb; import com.mongodb.BasicDBObject; -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBCursor; -import com.mongodb.DBObject; import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.WriteConcern; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.MongoDatabase; import com.mongodb.util.JSON; import java.io.IOException; import java.io.Reader; @@ -37,6 +38,7 @@ import java.io.Writer; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.apache.zest.api.configuration.Configuration; import org.apache.zest.api.entity.EntityDescriptor; @@ -50,6 +52,10 @@ import org.apache.zest.io.Sender; import org.apache.zest.spi.entitystore.EntityNotFoundException; import org.apache.zest.spi.entitystore.EntityStoreException; import org.apache.zest.spi.entitystore.helpers.MapEntityStore; +import org.bson.Document; +import org.bson.conversions.Bson; + +import static com.mongodb.client.model.Filters.eq; /** * MongoDB implementation of MapEntityStore. @@ -70,7 +76,7 @@ public class MongoMapEntityStoreMixin private String username; private char[] password; private MongoClient mongo; - private DB db; + private MongoDatabase db; @Override public void activateService() @@ -79,20 +85,21 @@ public class MongoMapEntityStoreMixin loadConfiguration(); // Create Mongo driver and open the database + MongoClientOptions options = MongoClientOptions.builder().writeConcern( writeConcern ).build(); if( username.isEmpty() ) { - mongo = new MongoClient( serverAddresses ); + mongo = new MongoClient( serverAddresses, options ); } else { MongoCredential credential = MongoCredential.createMongoCRCredential( username, databaseName, password ); - mongo = new MongoClient( serverAddresses, Arrays.asList( credential ) ); + mongo = new MongoClient( serverAddresses, Collections.singletonList( credential ), options ); } - db = mongo.getDB( databaseName ); + db = mongo.getDatabase( databaseName ); // Create index if needed - DBCollection entities = db.getCollection( collectionName ); - if( entities.getIndexInfo().isEmpty() ) + MongoCollection<Document> entities = db.getCollection( collectionName ); + if( !entities.listIndexes().iterator().hasNext() ) { entities.createIndex( new BasicDBObject( IDENTITY_COLUMN, 1 ) ); } @@ -139,24 +146,27 @@ public class MongoMapEntityStoreMixin // If write concern not configured, set it to normal switch( config.writeConcern().get() ) { - case FSYNC_SAFE: - writeConcern = WriteConcern.FSYNC_SAFE; + case W1: + writeConcern = WriteConcern.W1; break; - case JOURNAL_SAFE: - writeConcern = WriteConcern.JOURNAL_SAFE; + case W2: + writeConcern = WriteConcern.W2; break; - case MAJORITY: - writeConcern = WriteConcern.MAJORITY; + case W3: + writeConcern = WriteConcern.W3; break; - case REPLICAS_SAFE: - writeConcern = WriteConcern.REPLICAS_SAFE; + case UNACKNOWLEDGED: + writeConcern = WriteConcern.UNACKNOWLEDGED; break; - case SAFE: - writeConcern = WriteConcern.SAFE; + case JOURNALED: + writeConcern = WriteConcern.JOURNALED; + break; + case MAJORITY: + writeConcern = WriteConcern.MAJORITY; break; - case NORMAL: + case ACKNOWLEDGED: default: - writeConcern = WriteConcern.NORMAL; + writeConcern = WriteConcern.ACKNOWLEDGED; } // Username and password are defaulted to empty strings @@ -186,7 +196,7 @@ public class MongoMapEntityStoreMixin } @Override - public DB dbInstanceUsed() + public MongoDatabase dbInstanceUsed() { return db; } @@ -201,13 +211,14 @@ public class MongoMapEntityStoreMixin public Reader get( EntityReference entityReference ) throws EntityStoreException { - DBObject entity = db.getCollection( collectionName ).findOne( byIdentity( entityReference ) ); - if( entity == null ) + MongoCursor<Document> cursor = db.getCollection( collectionName ) + .find( byIdentity( entityReference ) ) + .limit( 1 ).iterator(); + if( !cursor.hasNext() ) { throw new EntityNotFoundException( entityReference ); } - DBObject bsonState = (DBObject) entity.get( STATE_COLUMN ); - + Document bsonState = (Document) cursor.next().get( STATE_COLUMN ); String jsonState = JSON.serialize( bsonState ); return new StringReader( jsonState ); } @@ -216,7 +227,7 @@ public class MongoMapEntityStoreMixin public void applyChanges( MapChanges changes ) throws IOException { - final DBCollection entities = db.getCollection( collectionName ); + final MongoCollection<Document> entities = db.getCollection( collectionName ); changes.visitMap( new MapChanger() { @@ -231,14 +242,11 @@ public class MongoMapEntityStoreMixin throws IOException { super.close(); - - String jsonState = toString(); - DBObject bsonState = (DBObject) JSON.parse( jsonState ); - - BasicDBObject entity = new BasicDBObject(); + Document bsonState = Document.parse( toString() ); + Document entity = new Document(); entity.put( IDENTITY_COLUMN, ref.identity().toString() ); entity.put( STATE_COLUMN, bsonState ); - entities.insert( entity, writeConcern ); + entities.insertOne( entity ); } }; } @@ -254,13 +262,11 @@ public class MongoMapEntityStoreMixin throws IOException { super.close(); - - DBObject bsonState = (DBObject) JSON.parse( toString() ); - - BasicDBObject entity = new BasicDBObject(); + Document bsonState = Document.parse( toString() ); + Document entity = new Document(); entity.put( IDENTITY_COLUMN, ref.identity().toString() ); entity.put( STATE_COLUMN, bsonState ); - entities.update( byIdentity( ref ), entity, false, false, writeConcern ); + entities.replaceOne( byIdentity( ref ), entity ); } }; } @@ -269,12 +275,15 @@ public class MongoMapEntityStoreMixin public void removeEntity( EntityReference ref, EntityDescriptor entityDescriptor ) throws EntityNotFoundException { - DBObject entity = entities.findOne( byIdentity( ref ) ); - if( entity == null ) + Bson byIdFilter = byIdentity( ref ); + MongoCursor<Document> cursor = db.getCollection( collectionName ) + .find( byIdFilter ) + .limit( 1 ).iterator(); + if( !cursor.hasNext() ) { throw new EntityNotFoundException( ref ); } - entities.remove( entity, writeConcern ); + entities.deleteOne( byIdFilter ); } } ); } @@ -296,11 +305,10 @@ public class MongoMapEntityStoreMixin Receiver<? super Reader, ReceiverThrowableType> receiver ) throws ReceiverThrowableType, IOException { - DBCursor cursor = db.getCollection( collectionName ).find(); - while( cursor.hasNext() ) + FindIterable<Document> cursor = db.getCollection( collectionName ).find(); + for( Document eachEntity : cursor ) { - DBObject eachEntity = cursor.next(); - DBObject bsonState = (DBObject) eachEntity.get( STATE_COLUMN ); + Document bsonState = (Document) eachEntity.get( STATE_COLUMN ); String jsonState = JSON.serialize( bsonState ); receiver.receive( new StringReader( jsonState ) ); } @@ -310,8 +318,8 @@ public class MongoMapEntityStoreMixin }; } - private DBObject byIdentity( EntityReference entityReference ) + private Bson byIdentity( EntityReference entityReference ) { - return new BasicDBObject( IDENTITY_COLUMN, entityReference.identity().toString() ); + return eq( IDENTITY_COLUMN, entityReference.identity().toString() ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java index b528b54..b5dc6e9 100644 --- a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java +++ b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/EmbedMongoMapEntityStoreTest.java @@ -83,7 +83,7 @@ public class EmbedMongoMapEntityStoreTest extends AbstractEntityStoreTest MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class ) .declareDefaults(); - mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE ); + mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.MAJORITY ); mongoConfig.database().set( "zest:test" ); mongoConfig.collection().set( testName.getMethodName() ); mongoConfig.hostname().set( "localhost" ); http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java index b7cf7b1..65bb810 100644 --- a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java +++ b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreTest.java @@ -62,7 +62,7 @@ public class MongoMapEntityStoreTest // END SNIPPET: assembly MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class ).declareDefaults(); - mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE ); + mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.MAJORITY ); mongoConfig.database().set( "zest:test" ); mongoConfig.collection().set( "zest:test:entities" ); // START SNIPPET: assembly http://git-wip-us.apache.org/repos/asf/zest-java/blob/7ac53388/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java ---------------------------------------------------------------------- diff --git a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java index b7355a4..a308803 100644 --- a/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java +++ b/extensions/entitystore-mongodb/src/test/java/org/apache/zest/entitystore/mongodb/MongoMapEntityStoreWithCacheTest.java @@ -58,7 +58,7 @@ public class MongoMapEntityStoreWithCacheTest new MongoDBEntityStoreAssembler().withConfig( config, Visibility.layer ).assemble( module ); MongoEntityStoreConfiguration mongoConfig = config.forMixin( MongoEntityStoreConfiguration.class ).declareDefaults(); - mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.FSYNC_SAFE ); + mongoConfig.writeConcern().set( MongoEntityStoreConfiguration.WriteConcern.MAJORITY ); mongoConfig.database().set( "zest:test" ); mongoConfig.collection().set( "zest:test:entities" ); }
