Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store reference inside Lazy when Lazy is marked (CDI integration) #386

Merged
merged 2 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/modules/misc/pages/integrations/cdi.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ The `@Store` CDI interceptor on the calling method makes sure that all instances

By default, the CDI interceptor will use an asynchronous way of storing the instances so that the response can be sent faster since it doesn't need to wait before data is stored.

If you _mark_ a `Lazy` instance, the reference that is _inside_ the Lazy reference is also stored to the storage target. This is to make sure that an item that is added to a List within the Lazy is also stored. And you don't need to mark the Lazy and the reference _inside_ it separately.

Also, if you mark any `Lazy` instance of MicroStream, it will be cleared automatically when it is stored.

You can change this behaviour by specifying different member values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ void initializePump()

public void storeChanged(final Object dirtyInstance, final boolean clearLazy)
{
if (dirtyInstance instanceof Lazy) {
// When a Lazy is marked, the developer probably wants to store the referenced instance in the Lazy.
this.manager.store(((Lazy<?>) dirtyInstance).get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lazy#get always loads the reference. I would recommend to use Lazy#peek

Object lazyRef = ((Lazy<?>)dirtyInstance).peek();
if(lazyRef != null)
{
  this.manager.store(lazyRef);
}

}
this.manager.store(dirtyInstance);
if (clearLazy && dirtyInstance instanceof Lazy)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ public void storeAsynchronousWithLazy() throws InterruptedException
// Some logic that needs to be done to handle Lazy properly. (so that we can check .isLoaded)
Mockito.doAnswer((Answer<Void>) invocationOnMock ->
{
Lazy.Default lazy = (Lazy.Default) invocationOnMock.getArguments()[0];
lazy.$link(1L, objectSwizzlingMock);
Object argument = invocationOnMock.getArguments()[0];
if (argument instanceof Lazy)
{
Lazy.Default lazy = (Lazy.Default) argument;
lazy.$link(1L, objectSwizzlingMock);
}
return null;
})
.when(storageManagerMock)
Expand Down Expand Up @@ -294,8 +298,12 @@ public void storeSynchronousWithLazy() throws InterruptedException
// Some logic that needs to be done to handle Lazy properly. (so that we can check .isLoaded)
Mockito.doAnswer((Answer<Void>) invocationOnMock ->
{
Lazy.Default lazy = (Lazy.Default) invocationOnMock.getArguments()[0];
lazy.$link(1L, objectSwizzlingMock);
Object argument = invocationOnMock.getArguments()[0];
if (argument instanceof Lazy)
{
Lazy.Default lazy = (Lazy.Default) argument;
lazy.$link(1L, objectSwizzlingMock);
}
return null;
})
.when(storageManagerMock)
Expand Down