Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd67c27

Browse files
author
lstolowski
committed
Refactor: remove usages of deprecated objects such DBCollection, DB, DBObject, BasicDBObject in favor of newly introduced in mongo-java-driver-v3: Document, MongoCollection, MongoDatabase
1 parent 1eb0485 commit fd67c27

12 files changed

Lines changed: 219 additions & 191 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<dependency>
7373
<groupId>org.jongo</groupId>
7474
<artifactId>jongo</artifactId>
75-
<version>1.1</version>
75+
<version>1.3.0</version>
7676
</dependency>
7777
<dependency>
7878
<groupId>org.springframework.data</groupId>

src/main/java/com/github/mongobee/Mongobee.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public Mongobee(MongoClientURI mongoClientURI) {
7272
}
7373

7474
/**
75-
* <p>Constructor takes db.mongodb.Mongo object as a parameter.
76-
* </p><p>For more details about <tt>Mongo</tt> please see com.mongodb.Mongo docs
75+
* <p>Constructor takes db.mongodb.MongoClient object as a parameter.
76+
* </p><p>For more details about <tt>MongoClient</tt> please see com.mongodb.MongoClient docs
7777
* </p>
7878
*
7979
* @param mongoClient database connection client

src/main/java/com/github/mongobee/changeset/ChangeEntry.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.github.mongobee.changeset;
22

3-
import com.mongodb.BasicDBObject;
4-
import com.mongodb.DBObject;
5-
63
import java.util.Date;
74

5+
import org.bson.Document;
6+
87
/**
98
* Entry in the changes collection log {@link ChangeEntry#CHANGELOG_COLLECTION}
109
* Type: entity class.
10+
*
1111
* @author lstolowski
1212
* @since 27/07/2014
1313
*/
@@ -34,30 +34,30 @@ public ChangeEntry(String changeId, String author, Date timestamp, String change
3434
this.changeSetMethodName = changeSetMethodName;
3535
}
3636

37-
public DBObject buildFullDBObject(){
38-
BasicDBObject entry = new BasicDBObject();
39-
37+
public Document buildFullDBObject() {
38+
Document entry = new Document();
39+
4040
entry.append(KEY_CHANGEID, this.changeId)
41-
.append(KEY_AUTHOR, this.author)
42-
.append(KEY_TIMESTAMP, this.timestamp)
43-
.append(KEY_CHANGELOGCLASS, this.changeLogClass)
44-
.append(KEY_CHANGESETMETHOD, this.changeSetMethodName);
45-
41+
.append(KEY_AUTHOR, this.author)
42+
.append(KEY_TIMESTAMP, this.timestamp)
43+
.append(KEY_CHANGELOGCLASS, this.changeLogClass)
44+
.append(KEY_CHANGESETMETHOD, this.changeSetMethodName);
45+
4646
return entry;
4747
}
48-
49-
public DBObject buildSearchQueryDBObject(){
50-
return new BasicDBObject()
51-
.append(KEY_CHANGEID, this.changeId)
52-
.append(KEY_AUTHOR, this.author);
48+
49+
public Document buildSearchQueryDBObject() {
50+
return new Document()
51+
.append(KEY_CHANGEID, this.changeId)
52+
.append(KEY_AUTHOR, this.author);
5353
}
5454

5555
@Override
5656
public String toString() {
5757
return "[ChangeSet: id=" + this.changeId +
58-
", author=" + this.author +
59-
", changeLogClass=" + this.changeLogClass +
60-
", changeSetMethod=" + this.changeSetMethodName + "]";
58+
", author=" + this.author +
59+
", changeLogClass=" + this.changeLogClass +
60+
", changeSetMethod=" + this.changeSetMethodName + "]";
6161
}
6262

6363
public String getChangeId() {

src/main/java/com/github/mongobee/dao/ChangeEntryDao.java

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
import static com.github.mongobee.changeset.ChangeEntry.CHANGELOG_COLLECTION;
44
import static org.springframework.util.StringUtils.hasText;
55

6-
import java.net.UnknownHostException;
7-
6+
import org.bson.Document;
87
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
109

1110
import com.github.mongobee.changeset.ChangeEntry;
1211
import com.github.mongobee.exception.MongobeeConfigurationException;
1312
import com.github.mongobee.exception.MongobeeConnectionException;
1413
import com.mongodb.DB;
15-
import com.mongodb.DBCollection;
16-
import com.mongodb.DBObject;
17-
import com.mongodb.Mongo;
1814
import com.mongodb.MongoClient;
1915
import com.mongodb.MongoClientURI;
20-
import com.mongodb.WriteResult;
16+
import com.mongodb.client.MongoCollection;
2117
import com.mongodb.client.MongoDatabase;
2218

2319
/**
@@ -27,38 +23,42 @@
2723
public class ChangeEntryDao {
2824
private static final Logger logger = LoggerFactory.getLogger("Mongobee dao");
2925

30-
private DB db;
3126
private MongoDatabase mongoDatabase;
32-
private Mongo mongo;
27+
private DB db; // only for Jongo driver compatibility - do not use in other contexts
28+
private MongoClient mongoClient;
3329
private ChangeEntryIndexDao indexDao = new ChangeEntryIndexDao();
3430

3531
private LockDao lockDao = new LockDao();
3632

37-
public DB getDb() {
38-
return db;
39-
}
40-
4133
public MongoDatabase getMongoDatabase() {
4234
return mongoDatabase;
4335
}
4436

45-
public DB connectMongoDb(MongoClient mongo, String dbName) throws MongobeeConfigurationException {
37+
/**
38+
* @deprecated implemented only for Jongo driver compatibility and backward compatibility - do not use in other contexts
39+
* @return com.mongodb.DB
40+
*/
41+
public DB getDb() {
42+
return db;
43+
}
44+
45+
public MongoDatabase connectMongoDb(MongoClient mongo, String dbName) throws MongobeeConfigurationException {
4646
if (!hasText(dbName)) {
4747
throw new MongobeeConfigurationException("DB name is not set. Should be defined in MongoDB URI or via setter");
4848
} else {
49-
this.mongo = mongo;
5049

50+
this.mongoClient = mongo;
5151

52-
db = mongo.getDB(dbName); // FIXME
53-
52+
db = mongo.getDB(dbName); // for Jongo driver and backward compatibility (constructor has required parameter Jongo(DB) )
5453
mongoDatabase = mongo.getDatabase(dbName);
55-
ensureChangeLogCollectionIndex(db.getCollection(CHANGELOG_COLLECTION));
54+
55+
ensureChangeLogCollectionIndex(mongoDatabase.getCollection(CHANGELOG_COLLECTION));
5656
initializeLock();
57-
return db;
57+
return mongoDatabase;
5858
}
5959
}
6060

61-
public DB connectMongoDb(MongoClientURI mongoClientURI, String dbName)
61+
public MongoDatabase connectMongoDb(MongoClientURI mongoClientURI, String dbName)
6262
throws MongobeeConfigurationException, MongobeeConnectionException {
6363

6464
final MongoClient mongoClient = new MongoClient(mongoClientURI);
@@ -73,44 +73,45 @@ public DB connectMongoDb(MongoClientURI mongoClientURI, String dbName)
7373
*/
7474
public boolean acquireProcessLock() throws MongobeeConnectionException {
7575
verifyDbConnection();
76-
return lockDao.acquireLock(getDb());
76+
return lockDao.acquireLock(getMongoDatabase());
7777
}
7878

7979
public void releaseProcessLock() throws MongobeeConnectionException {
8080
verifyDbConnection();
81-
lockDao.releaseLock(getDb());
81+
lockDao.releaseLock(getMongoDatabase());
8282
}
8383

8484
public boolean isProccessLockHeld() throws MongobeeConnectionException {
8585
verifyDbConnection();
86-
return lockDao.isLockHeld(getDb());
86+
return lockDao.isLockHeld(getMongoDatabase());
8787
}
8888

8989
public boolean isNewChange(ChangeEntry changeEntry) throws MongobeeConnectionException {
9090
verifyDbConnection();
9191

92-
DBCollection mongobeeChangeLog = getDb().getCollection(CHANGELOG_COLLECTION);
93-
DBObject entry = mongobeeChangeLog.findOne(changeEntry.buildSearchQueryDBObject());
92+
MongoCollection<Document> mongobeeChangeLog = getMongoDatabase().getCollection(CHANGELOG_COLLECTION);
93+
Document entry = mongobeeChangeLog.find(changeEntry.buildSearchQueryDBObject()).first();
9494

9595
return entry == null;
9696
}
9797

98-
public WriteResult save(ChangeEntry changeEntry) throws MongobeeConnectionException {
98+
public void save(ChangeEntry changeEntry) throws MongobeeConnectionException {
9999
verifyDbConnection();
100100

101-
DBCollection mongobeeLog = getDb().getCollection(CHANGELOG_COLLECTION);
102-
return mongobeeLog.save(changeEntry.buildFullDBObject());
101+
MongoCollection<Document> mongobeeLog = getMongoDatabase().getCollection(CHANGELOG_COLLECTION);
102+
103+
mongobeeLog.insertOne(changeEntry.buildFullDBObject());
103104
}
104105

105106
private void verifyDbConnection() throws MongobeeConnectionException {
106-
if (getDb() == null) {
107+
if (getMongoDatabase() == null) {
107108
throw new MongobeeConnectionException("Database is not connected. Mongobee has thrown an unexpected error",
108109
new NullPointerException());
109110
}
110111
}
111112

112-
private void ensureChangeLogCollectionIndex(DBCollection collection) {
113-
DBObject index = indexDao.findRequiredChangeAndAuthorIndex(db);
113+
private void ensureChangeLogCollectionIndex(MongoCollection<Document> collection) {
114+
Document index = indexDao.findRequiredChangeAndAuthorIndex(mongoDatabase);
114115
if (index == null) {
115116
indexDao.createRequiredUniqueIndex(collection);
116117
logger.debug("Index in collection " + CHANGELOG_COLLECTION + " was created");
@@ -123,11 +124,11 @@ private void ensureChangeLogCollectionIndex(DBCollection collection) {
123124
}
124125

125126
public void close() {
126-
this.mongo.close();
127+
this.mongoClient.close();
127128
}
128129

129130
private void initializeLock() {
130-
lockDao.intitializeLock(db);
131+
lockDao.intitializeLock(mongoDatabase);
131132
}
132133

133134
/* Visible for testing */

src/main/java/com/github/mongobee/dao/ChangeEntryIndexDao.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,39 @@
22

33
import static com.github.mongobee.changeset.ChangeEntry.CHANGELOG_COLLECTION;
44

5+
import org.bson.Document;
6+
57
import com.github.mongobee.changeset.ChangeEntry;
6-
import com.mongodb.BasicDBObject;
7-
import com.mongodb.DB;
88
import com.mongodb.DBCollection;
9-
import com.mongodb.DBObject;
9+
import com.mongodb.client.MongoCollection;
10+
import com.mongodb.client.MongoDatabase;
11+
import com.mongodb.client.model.IndexOptions;
1012

1113
/**
1214
* @author lstolowski
1315
* @since 10.12.14
1416
*/
1517
public class ChangeEntryIndexDao {
1618

17-
public void createRequiredUniqueIndex(DBCollection collection) {
18-
collection.createIndex(new BasicDBObject()
19+
public void createRequiredUniqueIndex(MongoCollection<Document> collection) {
20+
collection.createIndex(new Document()
1921
.append(ChangeEntry.KEY_CHANGEID, 1)
2022
.append(ChangeEntry.KEY_AUTHOR, 1),
21-
new BasicDBObject().append("unique", true));
23+
new IndexOptions().unique(true)
24+
);
2225
}
2326

24-
public DBObject findRequiredChangeAndAuthorIndex(DB db) {
25-
DBCollection indexes = db.getCollection("system.indexes");
26-
DBObject index = indexes.findOne(new BasicDBObject()
27+
public Document findRequiredChangeAndAuthorIndex(MongoDatabase db) {
28+
MongoCollection<Document> indexes = db.getCollection("system.indexes");
29+
Document index = indexes.find(new Document()
2730
.append("ns", db.getName() + "." + CHANGELOG_COLLECTION)
28-
.append("key", new BasicDBObject().append(ChangeEntry.KEY_CHANGEID, 1).append(ChangeEntry.KEY_AUTHOR, 1))
29-
);
31+
.append("key", new Document().append(ChangeEntry.KEY_CHANGEID, 1).append(ChangeEntry.KEY_AUTHOR, 1))
32+
).first();
3033

3134
return index;
3235
}
3336

34-
public boolean isUnique(DBObject index) {
37+
public boolean isUnique(Document index) {
3538
Object unique = index.get("unique");
3639
if (unique != null && unique instanceof Boolean) {
3740
return (Boolean) unique;
@@ -40,7 +43,11 @@ public boolean isUnique(DBObject index) {
4043
}
4144
}
4245

43-
public void dropIndex(DBCollection collection, DBObject index) {
46+
public void dropIndex(MongoCollection<Document> collection, Document index) {
47+
collection.dropIndex(index.get("name").toString());
48+
}
49+
50+
public void dropIndex(DBCollection collection, Document index) {
4451
collection.dropIndex(index.get("name").toString());
4552
}
4653

src/main/java/com/github/mongobee/dao/LockDao.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
11
package com.github.mongobee.dao;
22

3-
import com.mongodb.BasicDBObject;
4-
import com.mongodb.DB;
5-
import com.mongodb.DBObject;
6-
import com.mongodb.DuplicateKeyException;
3+
import org.bson.Document;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import com.mongodb.ErrorCategory;
8+
import com.mongodb.MongoWriteException;
9+
import com.mongodb.client.MongoDatabase;
10+
import com.mongodb.client.model.IndexOptions;
711

812
/**
913
* @author colsson11
1014
* @since 13.01.15
1115
*/
1216
public class LockDao {
13-
17+
private static final Logger logger = LoggerFactory.getLogger(LockDao.class);
1418
private static final String COLLECTION = "mongobeelock";
1519
private static final String KEY_PROP_NAME = "key";
1620

1721
private static final int INDEX_SORT_ASC = 1;
1822

1923
private static final String LOCK_ENTRY_KEY_VAL = "LOCK";
2024

21-
public void intitializeLock(DB db) {
25+
public void intitializeLock(MongoDatabase db) {
2226
createCollectionAndUniqueIndexIfNotExists(db);
2327
}
2428

25-
private void createCollectionAndUniqueIndexIfNotExists(DB db) {
26-
DBObject indexKeys = new BasicDBObject(KEY_PROP_NAME, INDEX_SORT_ASC);
27-
DBObject indexOptions = new BasicDBObject("unique", true).append("name", "mongobeelock_key_idx");
29+
private void createCollectionAndUniqueIndexIfNotExists(MongoDatabase db) {
30+
Document indexKeys = new Document(KEY_PROP_NAME, INDEX_SORT_ASC);
31+
IndexOptions indexOptions = new IndexOptions().unique(true).name("mongobeelock_key_idx");
2832

2933
db.getCollection(COLLECTION).createIndex(indexKeys, indexOptions);
3034
}
3135

32-
public boolean acquireLock(DB db) {
36+
public boolean acquireLock(MongoDatabase db) {
3337

34-
DBObject insertObj = new BasicDBObject(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL).append("status", "LOCK_HELD");
38+
Document insertObj = new Document(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL).append("status", "LOCK_HELD");
3539

3640
// acquire lock by attempting to insert the same value in the collection - if it already exists (i.e. lock held)
3741
// there will be an exception
3842
try {
39-
db.getCollection(COLLECTION).insert(insertObj);
40-
} catch (DuplicateKeyException ex) {
43+
db.getCollection(COLLECTION).insertOne(insertObj);
44+
} catch (MongoWriteException ex) {
45+
if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
46+
logger.warn("Duplicate key exception while acquireLock. Probably the lock has been already acquired.");
47+
}
4148
return false;
4249
}
4350
return true;
4451
}
4552

46-
public void releaseLock(DB db) {
53+
public void releaseLock(MongoDatabase db) {
4754
// release lock by deleting collection entry
48-
db.getCollection(COLLECTION).remove(new BasicDBObject(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL));
55+
db.getCollection(COLLECTION).deleteMany(new Document(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL));
4956
}
5057

5158
/**
@@ -54,7 +61,7 @@ public void releaseLock(DB db) {
5461
* @param db
5562
* @return true if the lock is currently held
5663
*/
57-
public boolean isLockHeld(DB db) {
64+
public boolean isLockHeld(MongoDatabase db) {
5865
return db.getCollection(COLLECTION).count() == 1;
5966
}
6067

0 commit comments

Comments
 (0)