This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from patientsknowbest/feature/PHR-7233_move_hi…
…bernate_resources_to_pkb_common PHR-7233 - Move commons-hibernate optimizer to pkb-common
- Loading branch information
Showing
7 changed files
with
270 additions
and
35 deletions.
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
error/src/main/java/com/pkb/common/error/ValidationException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...c/main/java/com/pkb/infrastructure/hibernate/id/enhanced/ResettablePooledLoOptimizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.pkb.infrastructure.hibernate.id.enhanced; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.hibernate.id.IntegralDataTypeHolder; | ||
import org.hibernate.id.enhanced.AccessCallback; | ||
import org.hibernate.id.enhanced.Optimizer; | ||
import org.hibernate.id.enhanced.PooledLoOptimizer; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
|
||
import com.pkb.common.ClearableInternalState; | ||
|
||
/** | ||
* Extend PooledLoOptimizer to track instances and reset internal state (for running tests) | ||
*/ | ||
public class ResettablePooledLoOptimizer implements ClearableInternalState, Optimizer { | ||
|
||
public static final String OPTIMIZER_CANONICAL_NAME ="com.pkb.infrastructure.hibernate.id.enhanced.ResettablePooledLoOptimizer"; | ||
|
||
private PooledLoOptimizer optimizer; | ||
|
||
// only used for resetting state (in e2e tests) | ||
private static final Cache<ClearableInternalState, Boolean> clearableInstances = CacheBuilder.newBuilder() | ||
.weakKeys() | ||
.build(); | ||
private boolean lazyAddedToCache = false; | ||
|
||
/** | ||
* Constructs a ResettablePooledLoOptimizer. | ||
* | ||
* @param returnClass The Java type of the values to be generated | ||
* @param incrementSize The increment size. | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public ResettablePooledLoOptimizer(Class returnClass, int incrementSize) { | ||
this.optimizer = newOptimizer(returnClass, incrementSize); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private PooledLoOptimizer newOptimizer(Class returnClass, int incrementSize) { | ||
return new PooledLoOptimizer(returnClass, incrementSize); | ||
} | ||
|
||
@Override | ||
public void clearState() { | ||
this.optimizer = newOptimizer(optimizer.getReturnClass(), optimizer.getIncrementSize()); | ||
} | ||
|
||
public static void clearStateGlobal() { | ||
for (ClearableInternalState instance : clearableInstances.asMap().keySet()) { | ||
instance.clearState(); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized Serializable generate(AccessCallback callback) { | ||
if (!lazyAddedToCache) { | ||
//noinspection AccessToStaticFieldLockedOnInstance | ||
clearableInstances.put(this, Boolean.TRUE); | ||
lazyAddedToCache = true; | ||
} | ||
return optimizer.generate(callback); | ||
} | ||
|
||
@Override | ||
public boolean applyIncrementSizeToSourceValues() { | ||
return optimizer.applyIncrementSizeToSourceValues(); | ||
} | ||
|
||
@Override | ||
public final int getIncrementSize() { | ||
return optimizer.getIncrementSize(); | ||
} | ||
|
||
@Override | ||
public IntegralDataTypeHolder getLastSourceValue() { | ||
return optimizer.getLastSourceValue(); | ||
} | ||
|
||
/** | ||
* Getter for property 'returnClass'. This is the Java | ||
* class which is used to represent the id (e.g. {@link Long}). | ||
* | ||
* @return Value for property 'returnClass'. | ||
*/ | ||
@SuppressWarnings({"UnusedDeclaration", "rawtypes"}) | ||
public final Class getReturnClass() { | ||
return optimizer.getReturnClass(); | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
...st/java/com/pkb/infrastructure/hibernate/id/enhanced/ResettablePooledLoOptimizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.pkb.infrastructure.hibernate.id.enhanced; | ||
|
||
import static com.github.karsaig.approvalcrest.MatcherAssert.assertThat; | ||
import static com.pkb.infrastructure.hibernate.id.enhanced.ResettablePooledLoOptimizer.OPTIMIZER_CANONICAL_NAME; | ||
import static org.hamcrest.core.IsInstanceOf.instanceOf; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.hibernate.id.IdentifierGeneratorHelper; | ||
import org.hibernate.id.IntegralDataTypeHolder; | ||
import org.hibernate.id.enhanced.AccessCallback; | ||
import org.hibernate.id.enhanced.Optimizer; | ||
import org.hibernate.id.enhanced.OptimizerFactory; | ||
import org.junit.Test; | ||
|
||
public class ResettablePooledLoOptimizerTest { | ||
|
||
@Test | ||
public void optimizerFoundByHibernate() { | ||
Optimizer optimizer = OptimizerFactory.buildOptimizer(OPTIMIZER_CANONICAL_NAME, Long.class, 50, -1); | ||
|
||
assertThat(optimizer, instanceOf(ResettablePooledLoOptimizer.class)); | ||
} | ||
|
||
@Test | ||
public void optimizerBasicUsage() { | ||
SourceMock sequence = new SourceMock(1, 3); | ||
Optimizer optimizer = OptimizerFactory.buildOptimizer(ResettablePooledLoOptimizer.class.getCanonicalName(), Long.class, 3, 1); | ||
|
||
assertEquals(0, sequence.getTimesCalled()); | ||
assertEquals(-1, sequence.getCurrentValue()); | ||
|
||
Long next = (Long)optimizer.generate(sequence); | ||
assertEquals(1, next.intValue()); | ||
assertEquals(1, sequence.getTimesCalled()); | ||
assertEquals(1, sequence.getCurrentValue()); | ||
|
||
next = (Long)optimizer.generate(sequence); | ||
assertEquals(2, next.intValue()); | ||
assertEquals(1, sequence.getTimesCalled()); | ||
assertEquals(1, sequence.getCurrentValue()); | ||
|
||
next = (Long)optimizer.generate(sequence); | ||
assertEquals(3, next.intValue()); | ||
assertEquals(1, sequence.getTimesCalled()); | ||
assertEquals(1, sequence.getCurrentValue()); | ||
|
||
next = (Long)optimizer.generate(sequence); | ||
assertEquals(4, next.intValue()); | ||
assertEquals(2, sequence.getTimesCalled()); | ||
assertEquals(4, sequence.getCurrentValue()); | ||
} | ||
|
||
@Test | ||
public void optimizerResetCausesIncrementEarlier() { | ||
SourceMock sequence = new SourceMock(1, 3); | ||
ResettablePooledLoOptimizer optimizer = (ResettablePooledLoOptimizer)OptimizerFactory.buildOptimizer( | ||
ResettablePooledLoOptimizer.class.getCanonicalName(), | ||
Long.class, | ||
3, | ||
1); | ||
|
||
assertEquals(0, sequence.getTimesCalled()); | ||
assertEquals(-1, sequence.getCurrentValue()); | ||
|
||
Long next = (Long)optimizer.generate(sequence); | ||
assertEquals(1, next.intValue()); | ||
assertEquals(1, sequence.getTimesCalled()); | ||
assertEquals(1, sequence.getCurrentValue()); | ||
|
||
ResettablePooledLoOptimizer.clearStateGlobal(); | ||
|
||
next = (Long)optimizer.generate(sequence); | ||
assertEquals(4, next.intValue()); | ||
assertEquals(2, sequence.getTimesCalled()); | ||
assertEquals(4, sequence.getCurrentValue()); | ||
|
||
next = (Long)optimizer.generate(sequence); | ||
assertEquals(5, next.intValue()); | ||
assertEquals(2, sequence.getTimesCalled()); | ||
assertEquals(4, sequence.getCurrentValue()); | ||
} | ||
|
||
/** | ||
* Largely pulled from hibernate-core's org.hibernate.id.enhanced.OptimizerUnitTest.SourceMock | ||
*/ | ||
private static class SourceMock implements AccessCallback { | ||
private IdentifierGeneratorHelper.BasicHolder value = new IdentifierGeneratorHelper.BasicHolder(Long.class); | ||
private long initialValue; | ||
private int increment; | ||
private int timesCalled ; | ||
|
||
private SourceMock(long initialValue, int increment) { | ||
this(initialValue, increment, 0 ); | ||
} | ||
|
||
private SourceMock(long initialValue, int increment, int timesCalled) { | ||
this.increment = increment; | ||
this.timesCalled = timesCalled; | ||
|
||
if (timesCalled == 0) { | ||
this.value.initialize( -1 ); | ||
this.initialValue = initialValue; | ||
} | ||
else { | ||
this.value.initialize( initialValue ); | ||
this.initialValue = 1; | ||
} | ||
} | ||
|
||
@Override | ||
public IntegralDataTypeHolder getNextValue() { | ||
try { | ||
if (timesCalled == 0) { | ||
initValue(); | ||
return value.copy(); | ||
} | ||
else { | ||
return value.add(increment).copy(); | ||
} | ||
} | ||
finally { | ||
timesCalled++; | ||
} | ||
} | ||
|
||
@Override | ||
public String getTenantIdentifier() { | ||
return "test"; | ||
} | ||
|
||
private void initValue() { | ||
this.value.initialize(initialValue); | ||
} | ||
|
||
private int getTimesCalled() { | ||
return timesCalled; | ||
} | ||
|
||
private long getCurrentValue() { | ||
return value == null ? -1 : value.getActualLongValue(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters