Skip to content

Commit

Permalink
Put merged instance into second level cache on transaction commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jun 19, 2017
1 parent 5248267 commit 1d7868a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public Object mergeChangesOnObject(ObjectChangeSet changeSet) {
}
} else {
builder.mergeChanges(changeSet);
final Object identifier = EntityPropertiesUtils.getPrimaryKey(original, uow.getMetamodel());
uow.putObjectIntoCache(identifier, original, changeSet.getEntityDescriptor());
}
return clone;
}
Expand All @@ -77,10 +79,11 @@ public void mergeNewObject(ObjectChangeSet changeSet) {
}
if (!changeSet.isNew()) {
mergeChangesOnObject(changeSet);
return;
}
// Put the original object into the shared session cache
Object newObject = changeSet.getChangedObject();
final Object primaryKey = EntityPropertiesUtils.getPrimaryKey(newObject, uow.getMetamodel());
uow.putObjectIntoCache(primaryKey, newObject, changeSet.getEntityDescriptor());
final Object identifier = EntityPropertiesUtils.getPrimaryKey(newObject, uow.getMetamodel());
uow.putObjectIntoCache(identifier, newObject, changeSet.getEntityDescriptor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ private <T> T mergeDetachedInternal(T entity, Descriptor descriptor) {
if (chSet.hasChanges()) {
et.getLifecycleListenerManager().invokePostUpdateCallbacks(clone);
}
getUowChangeSet().addObjectChangeSet(chSet);
getUowChangeSet().addObjectChangeSet(copyChangeSet(chSet, original, clone, descriptor));
} catch (OWLEntityExistsException e) {
unregisterObject(clone);
throw e;
Expand All @@ -627,6 +627,13 @@ private <T> T mergeDetachedInternal(T entity, Descriptor descriptor) {
return et.getJavaType().cast(clone);
}

private ObjectChangeSet copyChangeSet(ObjectChangeSet changeSet, Object original, Object clone,
Descriptor descriptor) {
final ObjectChangeSet newChangeSet = ChangeSetFactory.createObjectChangeSet(original, clone, descriptor);
changeSet.getChanges().forEach(newChangeSet::addChangeRecord);
return newChangeSet;
}

@Override
void registerEntityWithPersistenceContext(Object entity, UnitOfWorkImpl uow) {
parent.registerEntityWithPersistenceContext(entity, uow);
Expand Down Expand Up @@ -1062,8 +1069,8 @@ private void removeIndirectCollections(Object entity) {
}
}

void putObjectIntoCache(Object primaryKey, Object entity, Descriptor descriptor) {
cacheManager.add(primaryKey, entity, descriptor);
void putObjectIntoCache(Object identifier, Object entity, Descriptor descriptor) {
cacheManager.add(identifier, entity, descriptor);
}

private Object getIdentifier(Object entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,22 @@ public void testMergeChangesFromChangeSet() throws Exception {
OWLClassB cloneTwo = new OWLClassB(objTwo.getUri());
cloneOne.setStringAttribute("testAtt");
uowChangeSet.addDeletedObjectChangeSet(createChangeSet(objTwo, cloneTwo));
final ObjectChangeSet ochs = createChangeSet(objOne, cloneOne);
ochs.addChangeRecord(
final ObjectChangeSet changeSet = createChangeSet(objOne, cloneOne);
changeSet.addChangeRecord(
new ChangeRecordImpl(metamodelMocks.forOwlClassB().stringAttribute(), cloneOne.getStringAttribute()));
uowChangeSet.addObjectChangeSet(ochs);
uowChangeSet.addObjectChangeSet(changeSet);
mm.mergeChangesFromChangeSet(uowChangeSet);
verify(uow).removeObjectFromCache(objTwo, defaultDescriptor.getContext());
verify(cloneBuilder).mergeChanges(ochs);
verify(cloneBuilder).mergeChanges(changeSet);
}

@Test
public void mergeChangesFromChangeSetWithNewObjectPutsOriginalIntoCache() {
final OWLClassB objOne = new OWLClassB(Generators.createIndividualIdentifier());
objOne.setStringAttribute("ABeautifulAttribute");
final OWLClassB clone = new OWLClassB(objOne.getUri());
final ObjectChangeSet ochs = createChangeSet(objOne, clone);
uowChangeSet.addNewObjectChangeSet(ochs);
final ObjectChangeSet changeSet = createChangeSet(objOne, clone);
uowChangeSet.addNewObjectChangeSet(changeSet);
mm.mergeChangesFromChangeSet(uowChangeSet);
verify(uow).putObjectIntoCache(objOne.getUri(), objOne, defaultDescriptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ public void mergeRegistersChangesInUoWChangeSet() throws Exception {

assertTrue(uow.hasChanges());
final UnitOfWorkChangeSet changeSet = uow.getUowChangeSet();
final ObjectChangeSet objectChanges = changeSet.getExistingObjectChanges(result);
final ObjectChangeSet objectChanges = changeSet.getExistingObjectChanges(entityA);
assertNotNull(objectChanges);
assertEquals(2, objectChanges.getChanges().size());
final String strAttName = OWLClassA.getStrAttField().getName();
Expand Down Expand Up @@ -935,4 +935,19 @@ public void registerReplacesAlsoInheritedCollectionInstancesWithIndirectVersions
final OWLClassR clone = uow.readObject(OWLClassR.class, entityR.getUri(), descriptor);
assertTrue(clone.getTypes() instanceof IndirectSet);
}

@Test
public void commitPutsIntoCacheInstanceMergedAsDetachedDuringTransaction() {
final OWLClassA original = new OWLClassA(entityA.getUri());
original.setStringAttribute("originalStringAttribute");
when(storageMock.contains(entityA.getUri(), OWLClassA.class, descriptor)).thenReturn(true);
when(storageMock.find(new LoadingParameters<>(OWLClassA.class, entityA.getUri(), descriptor, true)))
.thenReturn(original);

final OWLClassA merged = uow.mergeDetached(entityA, descriptor);
assertNotNull(merged);
assertEquals(entityA.getStringAttribute(), merged.getStringAttribute());
uow.commit();
verify(cacheManagerMock).add(entityA.getUri(), original, descriptor);
}
}

0 comments on commit 1d7868a

Please sign in to comment.