Skip to content

Commit 35beb29

Browse files
Lazy storing working for simplest test
1 parent f202ee2 commit 35beb29

16 files changed

Lines changed: 370 additions & 267 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package software.xdev.spring.data.eclipse.store.exceptions;
2+
3+
public class BinaryHandlerOnlyForCopyingException extends RuntimeException
4+
{
5+
public BinaryHandlerOnlyForCopyingException()
6+
{
7+
super(
8+
"This handler should only be used for copying objects one time. It shouldn't be used for anything else, "
9+
+ "yet it looks like you are using it for something else.");
10+
}
11+
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/importer/EclipseStoreDataImporter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,11 @@ private <T> void createRepositoryForType(
205205
storageInstance.getRegistry(),
206206
storageInstance,
207207
storageInstance,
208-
new SupportedChecker.Implementation()),
209-
domainClass);
208+
new SupportedChecker.Implementation(),
209+
storageInstance
210+
),
211+
domainClass
212+
);
210213
}
211214

212215
private record EntityManagerFactoryRepositoryListPair(

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/EclipseStoreStorage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.eclipse.serializer.persistence.binary.jdk17.java.util.BinaryHandlerImmutableCollectionsList12;
2727
import org.eclipse.serializer.persistence.binary.jdk17.java.util.BinaryHandlerImmutableCollectionsSet12;
2828
import org.eclipse.serializer.persistence.types.Storer;
29+
import org.eclipse.serializer.reference.ObjectSwizzling;
2930
import org.eclipse.store.storage.embedded.types.EmbeddedStorageFoundation;
3031
import org.eclipse.store.storage.types.StorageManager;
3132
import org.slf4j.Logger;
@@ -40,7 +41,7 @@
4041
import software.xdev.spring.data.eclipse.store.repository.support.reposyncer.SimpleRepositorySynchronizer;
4142

4243
public class EclipseStoreStorage
43-
implements EntityListProvider, IdSetterProvider, PersistableChecker
44+
implements EntityListProvider, IdSetterProvider, PersistableChecker, ObjectSwizzling
4445
{
4546
private static final Logger LOG = LoggerFactory.getLogger(EclipseStoreStorage.class);
4647
private final Map<Class<?>, String> entityClassToRepositoryName = new HashMap<>();
@@ -311,4 +312,11 @@ public boolean isPersistable(final Class<?> clazz)
311312
this.ensureEntitiesInRoot();
312313
return this.persistenceChecker.isPersistable(clazz);
313314
}
315+
316+
@Override
317+
public Object getObject(final long objectId)
318+
{
319+
this.ensureEntitiesInRoot();
320+
return this.storageManager.getObject(objectId);
321+
}
314322
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/lazy/LazyProxyGenerator.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,9 @@ private LazyProxyGenerator()
1010
{
1111
}
1212

13-
public static <L extends Lazy<T>, T> L generateLazyProxy(final L lazy, final Class<L> clazz)
14-
{
15-
final ProxyFactory f = new ProxyFactory(clazz, new LazyInterceptor<>(lazy));
16-
return (L)f.getProxy();
17-
}
18-
1913
public static <T> Lazy<T> generateLazyProxy(final Lazy<T> lazy)
2014
{
21-
return generateLazyProxy(lazy, Lazy.class);
22-
}
23-
24-
public static <T> Lazy.Default<T> generateLazyDefaultProxy(final Lazy.Default<T> lazy)
25-
{
26-
return generateLazyProxy(lazy, Lazy.Default.class);
15+
final ProxyFactory f = new ProxyFactory(Lazy.class, new LazyInterceptor<>(lazy));
16+
return (Lazy<T>)f.getProxy();
2717
}
2818
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package software.xdev.spring.data.eclipse.store.repository.lazy;
2+
3+
import java.util.Objects;
4+
5+
import org.eclipse.serializer.reference.Lazy;
6+
import org.eclipse.serializer.reference.ObjectSwizzling;
7+
8+
9+
public interface SpringDataEclipseStoreLazy<T> extends Lazy<T>
10+
{
11+
static <T> SpringDataEclipseStoreLazy.Default<T> build(final T objectToWrapInLazy)
12+
{
13+
return new Default<>(objectToWrapInLazy);
14+
}
15+
16+
static SpringDataEclipseStoreLazy.Default<?> buildFromStorage(final long objectId, final ObjectSwizzling loader)
17+
{
18+
return new Default<>(objectId, loader);
19+
}
20+
21+
static SpringDataEclipseStoreLazy.Default<?> buildOnlyForStorage(final long objectId)
22+
{
23+
return new Default<>(objectId);
24+
}
25+
26+
long objectId();
27+
28+
class Default<T> implements SpringDataEclipseStoreLazy<T>
29+
{
30+
private Lazy<T> wrappedLazy;
31+
private long objectId;
32+
private ObjectSwizzling loader;
33+
34+
private Default(final T wrappedObject)
35+
{
36+
this.wrappedLazy = Lazy.Reference(wrappedObject);
37+
}
38+
39+
private Default(final long objectId, final ObjectSwizzling loader)
40+
{
41+
this.objectId = objectId;
42+
this.loader = loader;
43+
}
44+
45+
private Default(final long objectId)
46+
{
47+
this.objectId = objectId;
48+
}
49+
50+
private Lazy<T> ensureLazy()
51+
{
52+
if(this.wrappedLazy == null)
53+
{
54+
Objects.requireNonNull(this.loader);
55+
Objects.requireNonNull(this.objectId);
56+
this.wrappedLazy = Lazy.Reference((T)this.loader.getObject(this.objectId));
57+
}
58+
return this.wrappedLazy;
59+
}
60+
61+
@SuppressWarnings("all")
62+
public static final Class<SpringDataEclipseStoreLazy.Default<?>> genericType()
63+
{
64+
// no idea how to get ".class" to work otherwise in conjunction with generics.
65+
return (Class)SpringDataEclipseStoreLazy.Default.class;
66+
}
67+
68+
@Override
69+
public T get()
70+
{
71+
return this.ensureLazy().get();
72+
}
73+
74+
@Override
75+
public T peek()
76+
{
77+
return this.ensureLazy().peek();
78+
}
79+
80+
@Override
81+
public T clear()
82+
{
83+
this.wrappedLazy = null;
84+
return null;
85+
}
86+
87+
@Override
88+
public boolean isStored()
89+
{
90+
return this.ensureLazy().isStored();
91+
}
92+
93+
@Override
94+
public boolean isLoaded()
95+
{
96+
if(this.wrappedLazy == null)
97+
{
98+
return false;
99+
}
100+
return this.ensureLazy().isLoaded();
101+
}
102+
103+
@Override
104+
public long lastTouched()
105+
{
106+
return this.ensureLazy().lastTouched();
107+
}
108+
109+
@Override
110+
public boolean clear(final ClearingEvaluator clearingEvaluator)
111+
{
112+
if(this.wrappedLazy != null)
113+
{
114+
return this.wrappedLazy.clear(clearingEvaluator);
115+
}
116+
return true;
117+
}
118+
119+
@Override
120+
public long objectId()
121+
{
122+
if(this.wrappedLazy != null && this.wrappedLazy instanceof Lazy.Default<T>)
123+
{
124+
return ((Lazy.Default<T>)this.wrappedLazy).objectId();
125+
}
126+
return this.objectId;
127+
}
128+
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package software.xdev.spring.data.eclipse.store.repository.lazy;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.util.Objects;
5+
6+
import org.eclipse.serializer.persistence.binary.types.AbstractBinaryHandlerCustom;
7+
import org.eclipse.serializer.persistence.binary.types.Binary;
8+
import org.eclipse.serializer.persistence.types.PersistenceLoadHandler;
9+
import org.eclipse.serializer.persistence.types.PersistenceReferenceLoader;
10+
import org.eclipse.serializer.persistence.types.PersistenceStoreHandler;
11+
import org.eclipse.serializer.reference.Lazy;
12+
import org.eclipse.serializer.reference.ObjectSwizzling;
13+
import org.eclipse.serializer.reflect.XReflect;
14+
15+
import software.xdev.spring.data.eclipse.store.exceptions.BinaryHandlerOnlyForCopyingException;
16+
17+
18+
/**
19+
* Copied from
20+
* {@link org.eclipse.serializer.persistence.binary.org.eclipse.serializer.reference.BinaryHandlerLazyDefault}.
21+
*/
22+
public final class SpringDataEclipseStoreLazyBinaryHandler
23+
extends AbstractBinaryHandlerCustom<SpringDataEclipseStoreLazy.Default<?>>
24+
{
25+
@SuppressWarnings("rawtypes")
26+
static final Constructor<SpringDataEclipseStoreLazy.Default> CONSTRUCTOR = XReflect.setAccessible(
27+
XReflect.getDeclaredConstructor(
28+
SpringDataEclipseStoreLazy.Default.class,
29+
long.class,
30+
ObjectSwizzling.class
31+
)
32+
);
33+
34+
private final ObjectSwizzling objectSwizzling;
35+
36+
public SpringDataEclipseStoreLazyBinaryHandler(final ObjectSwizzling objectSwizzling)
37+
{
38+
super(
39+
SpringDataEclipseStoreLazy.Default.genericType(),
40+
CustomFields(
41+
CustomField(Object.class, "subject")
42+
)
43+
);
44+
this.objectSwizzling = Objects.requireNonNull(objectSwizzling);
45+
}
46+
47+
@Override
48+
public final void store(
49+
final Binary data,
50+
final SpringDataEclipseStoreLazy.Default<?> instance,
51+
final long objectId,
52+
final PersistenceStoreHandler<Binary> handler
53+
)
54+
{
55+
final long referenceOid = instance.objectId();
56+
data.storeEntityHeader(Binary.referenceBinaryLength(1), this.typeId(), objectId);
57+
data.store_long(referenceOid);
58+
}
59+
60+
@SuppressWarnings("unchecked")
61+
@Override
62+
public final SpringDataEclipseStoreLazy.Default<?> create(final Binary data, final PersistenceLoadHandler handler)
63+
{
64+
final long objectId = data.read_long(0);
65+
66+
return Lazy.register(
67+
XReflect.invoke(CONSTRUCTOR, objectId, this.objectSwizzling)
68+
);
69+
}
70+
71+
@Override
72+
public final void updateState(
73+
final Binary data,
74+
final SpringDataEclipseStoreLazy.Default<?> instance,
75+
final PersistenceLoadHandler handler
76+
)
77+
{
78+
throw new BinaryHandlerOnlyForCopyingException();
79+
}
80+
81+
@Override
82+
public final void complete(
83+
final Binary data,
84+
final SpringDataEclipseStoreLazy.Default<?> instance,
85+
final PersistenceLoadHandler handler
86+
)
87+
{
88+
// no-op for normal implementation (see non-reference-hashing collections for other examples)
89+
}
90+
91+
@Override
92+
public final boolean hasPersistedReferences()
93+
{
94+
return true;
95+
}
96+
97+
@Override
98+
public final boolean hasPersistedVariableLength()
99+
{
100+
return false;
101+
}
102+
103+
@Override
104+
public final boolean hasVaryingPersistedLengthInstances()
105+
{
106+
return false;
107+
}
108+
109+
@Override
110+
public final void iterateLoadableReferences(
111+
final Binary offset,
112+
final PersistenceReferenceLoader iterator
113+
)
114+
{
115+
// the lazy reference is not naturally loadable, but special-handled by this handler
116+
}
117+
}

0 commit comments

Comments
 (0)