@Override public synchronized Serializable generate(AccessCallback callback) { final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() ); if ( generationState.hiValue == null ) { generationState.value = callback.getNextValue(); // unfortunately not really safe to normalize this // to 1 as an initial value like we do the others // because we would not be able to control this if // we are using a sequence... if ( generationState.value.lt( 1 ) ) { log.pooledOptimizerReportedInitialValue( generationState.value ); } // the call to obtain next-value just gave us the initialValue if ( ( initialValue == -1 && generationState.value.lt( incrementSize ) ) || generationState.value.eq( initialValue ) ) { generationState.hiValue = callback.getNextValue(); } else { generationState.hiValue = generationState.value; generationState.value = generationState.hiValue.copy().subtract( incrementSize - 1 ); } } else if ( generationState.value.gt( generationState.hiValue ) ) { generationState.hiValue = callback.getNextValue(); generationState.value = generationState.hiValue.copy().subtract( incrementSize - 1 ); } return generationState.value.makeValueThenIncrement(); }
@Override public synchronized Serializable generate(AccessCallback callback) { final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() ); if ( generationState.lastSourceValue == null ) { // first call, so initialize ourselves. we need to read the database // value and set up the 'bucket' boundaries generationState.lastSourceValue = callback.getNextValue(); while ( generationState.lastSourceValue.lt( 1 ) ) { generationState.lastSourceValue = callback.getNextValue(); } // upperLimit defines the upper end of the bucket values generationState.upperLimit = generationState.lastSourceValue.copy().multiplyBy( incrementSize ).increment(); // initialize value to the low end of the bucket generationState.value = generationState.upperLimit.copy().subtract( incrementSize ); } else if ( ! generationState.upperLimit.gt( generationState.value ) ) { generationState.lastSourceValue = callback.getNextValue(); generationState.upperLimit = generationState.lastSourceValue.copy().multiplyBy( incrementSize ).increment(); generationState.value = generationState.upperLimit.copy().subtract( incrementSize ); } return generationState.value.makeValueThenIncrement(); }
@Test public void testBasicHiloAlgorithm() { // mimic an initialValue of 1 and increment of 20 final long initialValue = 1; final long incrementSize = 2; // initialization IntegralDataTypeHolder lastSourceValue = makeHolder().initialize( 1 ); IntegralDataTypeHolder upperLimit = lastSourceValue.copy().multiplyBy( incrementSize ).increment(); IntegralDataTypeHolder value = upperLimit.copy().subtract( incrementSize ); assertEquals( 1, lastSourceValue.makeValue().longValue() ); assertEquals( 3, upperLimit.makeValue().longValue() ); assertEquals( 1, value.makeValue().longValue() ); value.increment(); value.increment(); assertFalse( upperLimit.gt( value ) ); // at which point we would "clock over" lastSourceValue.increment(); upperLimit = lastSourceValue.copy().multiplyBy( incrementSize ).increment(); assertEquals( 2, lastSourceValue.makeValue().longValue() ); assertEquals( 5, upperLimit.makeValue().longValue() ); assertEquals( 3, value.makeValue().longValue() ); } }
holder.subtract( 1 ); fail();
@Override public synchronized Serializable generate(AccessCallback callback) { if ( hiValue == null ) { value = callback.getNextValue(); // unfortunately not really safe to normalize this // to 1 as an initial value like we do the others // because we would not be able to control this if // we are using a sequence... if (value.lt(1)) LOG.pooledOptimizerReportedInitialValue(value); // the call to obtain next-value just gave us the initialValue if ( ( initialValue == -1 && value.lt( incrementSize ) ) || value.eq( initialValue ) ) { hiValue = callback.getNextValue(); } else { hiValue = value; value = hiValue.copy().subtract( incrementSize ); } } else if ( ! hiValue.gt( value ) ) { hiValue = callback.getNextValue(); value = hiValue.copy().subtract( incrementSize ); } return value.makeValueThenIncrement(); }
@Override public synchronized Serializable generate(AccessCallback callback) { if ( hiValue == null ) { value = callback.getNextValue(); // unfortunately not really safe to normalize this // to 1 as an initial value like we do the others // because we would not be able to control this if // we are using a sequence... if (value.lt(1)) LOG.pooledOptimizerReportedInitialValue(value); // the call to obtain next-value just gave us the initialValue if ( ( initialValue == -1 && value.lt( incrementSize ) ) || value.eq( initialValue ) ) { hiValue = callback.getNextValue(); } else { hiValue = value; value = hiValue.copy().subtract( incrementSize ); } } else if ( ! hiValue.gt( value ) ) { hiValue = callback.getNextValue(); value = hiValue.copy().subtract( incrementSize ); } return value.makeValueThenIncrement(); }
@Override public synchronized Serializable generate(AccessCallback callback) { final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() ); if ( generationState.hiValue == null ) { generationState.value = callback.getNextValue(); // unfortunately not really safe to normalize this // to 1 as an initial value like we do the others // because we would not be able to control this if // we are using a sequence... if ( generationState.value.lt( 1 ) ) { log.pooledOptimizerReportedInitialValue( generationState.value ); } // the call to obtain next-value just gave us the initialValue if ( ( initialValue == -1 && generationState.value.lt( incrementSize ) ) || generationState.value.eq( initialValue ) ) { generationState.hiValue = callback.getNextValue(); } else { generationState.hiValue = generationState.value; generationState.value = generationState.hiValue.copy().subtract( incrementSize - 1 ); } } else if ( generationState.value.gt( generationState.hiValue ) ) { generationState.hiValue = callback.getNextValue(); generationState.value = generationState.hiValue.copy().subtract( incrementSize - 1 ); } return generationState.value.makeValueThenIncrement(); }
@Override public synchronized Serializable generate(AccessCallback callback) { final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() ); if ( generationState.lastSourceValue == null ) { // first call, so initialize ourselves. we need to read the database // value and set up the 'bucket' boundaries generationState.lastSourceValue = callback.getNextValue(); while ( generationState.lastSourceValue.lt( 1 ) ) { generationState.lastSourceValue = callback.getNextValue(); } // upperLimit defines the upper end of the bucket values generationState.upperLimit = generationState.lastSourceValue.copy().multiplyBy( incrementSize ).increment(); // initialize value to the low end of the bucket generationState.value = generationState.upperLimit.copy().subtract( incrementSize ); } else if ( ! generationState.upperLimit.gt( generationState.value ) ) { generationState.lastSourceValue = callback.getNextValue(); generationState.upperLimit = generationState.lastSourceValue.copy().multiplyBy( incrementSize ).increment(); generationState.value = generationState.upperLimit.copy().subtract( incrementSize ); } return generationState.value.makeValueThenIncrement(); }
@Override public synchronized Serializable generate(AccessCallback callback) { if ( lastSourceValue == null ) { // first call, so initialize ourselves. we need to read the database // value and set up the 'bucket' boundaries lastSourceValue = callback.getNextValue(); while ( lastSourceValue.lt( 1 ) ) { lastSourceValue = callback.getNextValue(); } // upperLimit defines the upper end of the bucket values upperLimit = lastSourceValue.copy().multiplyBy( incrementSize ).increment(); // initialize value to the low end of the bucket value = upperLimit.copy().subtract( incrementSize ); } else if ( ! upperLimit.gt( value ) ) { lastSourceValue = callback.getNextValue(); upperLimit = lastSourceValue.copy().multiplyBy( incrementSize ).increment(); } return value.makeValueThenIncrement(); }
@Override public synchronized Serializable generate(AccessCallback callback) { if ( lastSourceValue == null ) { // first call, so initialize ourselves. we need to read the database // value and set up the 'bucket' boundaries lastSourceValue = callback.getNextValue(); while ( lastSourceValue.lt( 1 ) ) { lastSourceValue = callback.getNextValue(); } // upperLimit defines the upper end of the bucket values upperLimit = lastSourceValue.copy().multiplyBy( incrementSize ).increment(); // initialize value to the low end of the bucket value = upperLimit.copy().subtract( incrementSize ); } else if ( ! upperLimit.gt( value ) ) { lastSourceValue = callback.getNextValue(); upperLimit = lastSourceValue.copy().multiplyBy( incrementSize ).increment(); } return value.makeValueThenIncrement(); }