Skip to content

Lazy #65

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

Merged
merged 20 commits into from
Apr 24, 2024
Merged

Lazy #65

Show file tree
Hide file tree
Changes from all commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 1.0.4

* Added possibility to use multiple storages
* Added Lazy support
* Updated EclipseStore to version 1.3.1

# 1.0.3

Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* xref:installation.adoc[Installation]
* xref:configuration.adoc[Configuration]
* xref:working-copies.adoc[Working Copies]
* xref:lazies.adoc[Lazy References]
* xref:migration.adoc[Migration]
* xref:known-issues.adoc[Known issues]
8 changes: 0 additions & 8 deletions docs/modules/ROOT/pages/known-issues.adoc
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
= Known issues

== Lazy references

One of the core features of EclipseStore is its https://docs.eclipsestore.io/manual/storage/loading-data/lazy-loading/index.html[Lazy references].
Unfortunately this requires our library to implement some kind of proxy.
That's something that takes a lot of effort to implement in our {product-name}.

We created https://github.com/xdev-software/spring-data-eclipse-store/issues/31[an issue] for that but right now we *do not support Lazy references*.

== Query annotations

In Spring-Data-JPA you can write a Query over methods of repositories like this:
Expand Down
40 changes: 40 additions & 0 deletions docs/modules/ROOT/pages/lazies.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
= Lazy References

Lazy Loading is an essential part of EclipseStore.
The basic mechanism is best explained in the https://docs.eclipsestore.io/manual/storage/loading-data/lazy-loading/index.html[EclipseStore-Docs]. +
In essence java objects which are wrapped in a *Lazy-Reference are not loaded with the startup of the EclipseStore-Storage but only if ``get()`` is called* on them.

Lazy References are essential for big data sets that can't get loaded into memory.
Since {product-name} operates with xref:working-copies.adoc[working copies] using the EclipseStore-Lazy-References is not possible. +
If you are using the EclipseStore-Lazy-References, all references would be resolved and loaded into memory as soon as a working copy is created, because the ``get()``-Method is called to create a full working copy.

That's why we implemented ``SpringDataEclipseStoreLazy``. +
The usage is the same as with the EclipseStore-Lazies, but they are handled very differently.

Simply wrap any kind of java object in the SpringDataEclipseStoreLazy-Wrapper and the wrapped object has a lazy loading behaviour.

CAUTION: Lazy-References are not only loaded when needed, but also https://docs.eclipsestore.io/manual/storage/loading-data/lazy-loading/clearing-lazy-references.html#automatically[*cleared when they are no longer needed*]!

Example: ``SpringDataEclipseStoreLazy.build(new HashMap<String, Pet>())``

[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/tree/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/owner/Owner.java[Example from complex demo]"]
----
package software.xdev.spring.data.eclipse.store.demo.complex.owner;
//...
import software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipseStoreLazy;

public class Owner extends Person
{
private String address;

private Lazy<List<Pet>> pets = SpringDataEclipseStoreLazy.build(new ArrayList<>());
//...
----

== Internas

SpringDataEclipseStoreLazies work as a proxy for the EclipseStore-Lazies.
As far as EclipseStore is concerned, a SpringDataEclipseStoreLazy-Object is a normal Java object that contains a Lazy-Reference.

But when {product-name} creates the working copy, *the SpringDataEclipseStoreLazy-Reference is not resolved* but instead only a reference to the original Lazy-Object in EclipseStore is loaded.
As soon as ``get()`` is called on the SpringDataEclipseStoreLazy, a *new working copy of the lazy object* is created.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ public void run(final String... args)
);
}
);

LOG.info("----Owner-Lazy Pet loading----");
this.ownerRepository.findAll().forEach(
o -> o.getPets().forEach(
pet -> LOG.info(String.format(
"Pet %s has owner %s %s",
pet.getName(),
o.getFirstName(),
o.getLastName()))
)
);
}

private static Visit createVisit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.util.List;
import java.util.Optional;

import org.eclipse.serializer.reference.Lazy;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;

import software.xdev.spring.data.eclipse.store.demo.complex.model.Person;
import software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipseStoreLazy;


public class Owner extends Person
Expand All @@ -33,7 +35,7 @@ public class Owner extends Person

private String telephone;

private final List<Pet> pets = new ArrayList<>();
private Lazy<List<Pet>> pets = SpringDataEclipseStoreLazy.build(new ArrayList<>());

public String getAddress()
{
Expand All @@ -52,7 +54,12 @@ public String getTelephone()

public List<Pet> getPets()
{
return this.pets;
return this.pets.get();
}

public void setPets(final List<Pet> pets)
{
this.pets = SpringDataEclipseStoreLazy.build(pets);
}

public void addPet(final Pet pet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package software.xdev.spring.data.eclipse.store.demo.complex.owner;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
Expand All @@ -28,5 +30,7 @@ public interface OwnerRepository extends Repository<Owner, Integer>

Page<Owner> findAll(Pageable pageable);

List<Owner> findAll();

void deleteAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreListCrudRepository;


public interface PersonToTestInEclipseStoreRepository extends EclipseStoreListCrudRepository<PersonToTestInEclipseStore, String>
public interface PersonToTestInEclipseStoreRepository
extends EclipseStoreListCrudRepository<PersonToTestInEclipseStore, String>
{
}
1 change: 1 addition & 0 deletions spring-data-eclipse-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
<configuration>
<properties>
<email>${project.organization.url}</email>
<year>2024</year>
</properties>
<licenseSets>
<licenseSet>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package software.xdev.spring.data.eclipse.store.exceptions;

/**
* Is used when a {@link software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipseStoreLazy} is not able
* to get unlinked from the object tree.
* <p>
* This exception should not be created by the user, but only within the Spring-Data-Eclipse-Store-Library.
* </p>
*/
public class LazyNotUnlinkableException extends RuntimeException
{
public LazyNotUnlinkableException(final String message, final Throwable e)
{
super(message, e);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -205,8 +205,11 @@ private <T> void createRepositoryForType(
storageInstance.getRegistry(),
storageInstance,
storageInstance,
new SupportedChecker.Implementation()),
domainClass);
new SupportedChecker.Implementation(),
storageInstance
),
domainClass
);
}

private record EntityManagerFactoryRepositoryListPair(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,7 @@ public EclipseStoreDataImporterComponent(
*
* @return all the newly created {@link SimpleEclipseStoreRepository} for the specific entities.
*/
@SuppressWarnings("java:S1452")
public List<SimpleEclipseStoreRepository<?, ?>> importData()
{
final Map<String, EntityManagerFactory> beansOfEms =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,8 @@
import org.eclipse.serializer.persistence.binary.jdk17.java.util.BinaryHandlerImmutableCollectionsList12;
import org.eclipse.serializer.persistence.binary.jdk17.java.util.BinaryHandlerImmutableCollectionsSet12;
import org.eclipse.serializer.persistence.types.Storer;
import org.eclipse.serializer.reference.LazyReferenceManager;
import org.eclipse.serializer.reference.ObjectSwizzling;
import org.eclipse.store.storage.embedded.types.EmbeddedStorageFoundation;
import org.eclipse.store.storage.types.StorageManager;
import org.slf4j.Logger;
Expand All @@ -40,7 +42,7 @@
import software.xdev.spring.data.eclipse.store.repository.support.reposyncer.SimpleRepositorySynchronizer;

public class EclipseStoreStorage
implements EntityListProvider, IdSetterProvider, PersistableChecker
implements EntityListProvider, IdSetterProvider, PersistableChecker, ObjectSwizzling
{
private static final Logger LOG = LoggerFactory.getLogger(EclipseStoreStorage.class);
private final Map<Class<?>, String> entityClassToRepositoryName = new HashMap<>();
Expand Down Expand Up @@ -272,6 +274,7 @@ public synchronized void stop()
this.registry.reset();
this.entityClassToIdSetter.clear();
LOG.info("Stopped storage.");
LazyReferenceManager.get().stop();
}
else
{
Expand Down Expand Up @@ -311,4 +314,11 @@ public boolean isPersistable(final Class<?> clazz)
this.ensureEntitiesInRoot();
return this.persistenceChecker.isPersistable(clazz);
}

@Override
public Object getObject(final long objectId)
{
this.ensureEntitiesInRoot();
return this.storageManager.getObject(objectId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading