|
| 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