Skip to content

Commit 96cdd1d

Browse files
author
Ryan Colobong
committed
Added Sequence Generator and a Factory Delegate utility for Mongo
1 parent 7ca5856 commit 96cdd1d

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.sipfoundry.commons.mongo;
2+
3+
import org.springframework.dao.DataAccessException;
4+
import org.springframework.data.mongodb.MongoDbFactory;
5+
6+
import com.mongodb.DB;
7+
8+
public class MongoDelegateFactory implements MongoDbFactory {
9+
10+
private MongoDbFactory m_delegateFactory;
11+
private String m_dbName;
12+
13+
public MongoDelegateFactory(MongoDbFactory delegateFactory, String dbName) {
14+
this.m_delegateFactory = delegateFactory;
15+
this.m_dbName = dbName;
16+
}
17+
18+
@Override
19+
public DB getDb() throws DataAccessException {
20+
return getDb(m_dbName);
21+
}
22+
23+
@Override
24+
public DB getDb(String dbName) throws DataAccessException {
25+
return m_delegateFactory.getDb(dbName);
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.sipfoundry.commons.mongo;
2+
3+
import org.apache.commons.lang.StringUtils;
4+
import org.springframework.util.Assert;
5+
6+
import com.mongodb.BasicDBObject;
7+
import com.mongodb.DBCollection;
8+
9+
public class MongoSequenceCounter {
10+
11+
private static final String COUNTER_COLLECTION_NAME = "counter";
12+
private static final String COUNTER_KEY_NAME = "sequence";
13+
14+
private MongoSpringTemplate m_dbTemplate;
15+
private String m_counterCollectionName;
16+
17+
public MongoSequenceCounter() {
18+
19+
}
20+
21+
public MongoSequenceCounter(MongoSpringTemplate dbTemplate) {
22+
this(dbTemplate, COUNTER_COLLECTION_NAME);
23+
}
24+
25+
public MongoSequenceCounter(MongoSpringTemplate dbTemplate, String collectionName) {
26+
this.m_dbTemplate = dbTemplate;
27+
this.m_counterCollectionName = collectionName;
28+
}
29+
30+
public long getCurrentSequence(String key) {
31+
Assert.notNull(m_dbTemplate, "MongoTemplate must not be null");
32+
33+
BasicDBObject result = (BasicDBObject)getCounterCollection().findOne(
34+
new BasicDBObject(MongoConstants.ID, key));
35+
if(result != null) {
36+
return result.getLong(COUNTER_KEY_NAME);
37+
}
38+
39+
return 0;
40+
}
41+
42+
public long getNextSequence(String key) {
43+
Assert.notNull(m_dbTemplate, "MongoTemplate must not be null");
44+
45+
//Use automic increment operation to prevent race condition
46+
//during multiple calls
47+
BasicDBObject updateQuery = new BasicDBObject("$inc"
48+
, new BasicDBObject(COUNTER_KEY_NAME, 1));
49+
50+
//Return the modified/incremented object.
51+
BasicDBObject result = (BasicDBObject)getCounterCollection().findAndModify(
52+
new BasicDBObject(MongoConstants.ID, key),
53+
null, null, false, updateQuery, true, true
54+
);
55+
return result.getLong(COUNTER_KEY_NAME);
56+
}
57+
58+
59+
private DBCollection getCounterCollection() {
60+
return m_dbTemplate.getDb().getCollection(getCounterCollectionName());
61+
}
62+
63+
public MongoSpringTemplate getDbTemplate() {
64+
return m_dbTemplate;
65+
}
66+
67+
public void setDbTemplate(MongoSpringTemplate dbTemplate) {
68+
this.m_dbTemplate = dbTemplate;
69+
}
70+
71+
public String getCounterCollectionName() {
72+
if(StringUtils.isBlank(m_counterCollectionName)) {
73+
m_counterCollectionName = COUNTER_COLLECTION_NAME;
74+
}
75+
76+
return m_counterCollectionName;
77+
}
78+
79+
public void setCounterCollectionName(String counterCollectionName) {
80+
this.m_counterCollectionName = counterCollectionName;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)