Source:
public long getNextSequenceValue(String name, long init, long inc, long poolSize) { LOGGER.debug("getNextSequenceValue({})", name); String fullName=coll.getFullName()+"."+name; rwl.readLock().lock(); SequenceInfo si=sequenceInfo.get(fullName); rwl.readLock().unlock(); if(si==null) { rwl.writeLock().lock(); si=sequenceInfo.get(fullName); if(si==null) { si=new SequenceInfo(fullName); sequenceInfo.put(fullName,si); } rwl.writeLock().unlock(); } si.lock.lock(); long ret=0; try { if(si!=null) { Long next=si.nextId(); if(next!=null) { return next; } } BasicDBObject q = new BasicDBObject(NAME, name); DBObject doc = coll.findOne(q,null,ReadPreference.primary()); if (doc == null) { LOGGER.debug("inserting sequence record name={}, init={}, inc={}", name, init, inc); if (inc == 0) { inc = 1; } BasicDBObject u = new BasicDBObject(). append(NAME, name). append(INIT, init). append(INC, inc). append(VALUE, init); try { coll.insert(u, WriteConcern.ACKNOWLEDGED); } catch (Exception e) { LOGGER.debug("Insertion failed with {}, trying to read", e); } doc = coll.findOne(q,null,ReadPreference.primary()); if (doc == null) { throw new RuntimeException("Cannot generate value for " + name); } } LOGGER.debug("Sequence doc={}", doc); Long increment = (Long) doc.get(INC); if(poolSize>1&&si!=null) { si.inc=increment; increment*=poolSize; } BasicDBObject u = new BasicDBObject(). append("$inc", new BasicDBObject(VALUE, increment)); doc = coll.findAndModify(q, u); ret = (Long) doc.get(VALUE); if(poolSize>1&&si!=null) { si.poolSize=poolSize-1; si.nextIdInPool=ret+si.inc; } LOGGER.debug("{} -> {}", name, ret); } finally { if(si!=null) { si.lock.unlock(); } } return ret; }

Target:
public long getNextSequenceValue(String name, long init, long inc, long poolSize) { LOGGER.debug("getNextSequenceValue({})", name); String fullName=coll.getFullName()+"."+name; rwl.readLock().lock(); SequenceInfo si=sequenceInfo.get(fullName); rwl.readLock().unlock(); if(si==null) { rwl.writeLock().lock(); si=sequenceInfo.get(fullName); if(si==null) { si=new SequenceInfo(fullName); sequenceInfo.put(fullName,si); } rwl.writeLock().unlock(); } si.lock.lock(); long ret=0; try { Long next=si.nextId(); if(next!=null) { return next; } BasicDBObject q = new BasicDBObject(NAME, name); DBObject doc = coll.findOne(q,null,ReadPreference.primary()); if (doc == null) { LOGGER.debug("inserting sequence record name={}, init={}, inc={}", name, init, inc); if (inc == 0) { inc = 1; } BasicDBObject u = new BasicDBObject(). append(NAME, name). append(INIT, init). append(INC, inc). append(VALUE, init); try { coll.insert(u, WriteConcern.ACKNOWLEDGED); } catch (Exception e) { LOGGER.debug("Insertion failed with {}, trying to read", e); } doc = coll.findOne(q,null,ReadPreference.primary()); if (doc == null) { throw new RuntimeException("Cannot generate value for " + name); } } LOGGER.debug("Sequence doc={}", doc); Long increment = (Long) doc.get(INC); if(poolSize>1) { si.inc=increment; increment*=poolSize; } BasicDBObject u = new BasicDBObject(). append("$inc", new BasicDBObject(VALUE, increment)); doc = coll.findAndModify(q, u); ret = (Long) doc.get(VALUE); if(poolSize>1) { si.poolSize=poolSize-1; si.nextIdInPool=ret+si.inc; } LOGGER.debug("{} -> {}", name, ret); } finally { si.lock.unlock(); } return ret; }