return new Tuple( EmptyTupleSnapshot.SINGLETON );
}
@Override
public void nextValue(RowKey key, IntegralDataTypeHolder value, int increment, int initialValue) {
DBCollection currentCollection = this.currentDB.getCollection( key.getTable() );
DBObject query = this.prepareIdObject( key );
//all columns should match to find the value
BasicDBObject update = new BasicDBObject();
//FIXME should "value" be hardcoded?
//FIXME how to set the initialValue if the document is not present? It seems the inc value is used as initial new value
Integer incrementObject = increment == 1 ? ONE : Integer.valueOf( increment );
this.addSubQuery( "$inc", update, SEQUENCE_VALUE, incrementObject );
DBObject result = currentCollection.findAndModify( query, null, null, false, update, false, true );
Object idFromDB;
idFromDB = result == null ? null : result.get( SEQUENCE_VALUE );
if ( idFromDB == null ) {
//not inserted yet so we need to add initial value to increment to have the right next value in the DB
//FIXME that means there is a small hole as when there was not value in the DB, we do add initial value in a non atomic way
BasicDBObject updateForInitial = new BasicDBObject();
this.addSubQuery( "$inc", updateForInitial, SEQUENCE_VALUE, initialValue );
currentCollection.findAndModify( query, null, null, false, updateForInitial, false, true );
idFromDB = initialValue; //first time we ask this value
}
else {
idFromDB = result.get( SEQUENCE_VALUE );
}