Skip to content

Commit a8f408f

Browse files
Simple Lazy working
1 parent cb29ea5 commit a8f408f

8 files changed

Lines changed: 202 additions & 29 deletions

File tree

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/exceptions/BinaryHandlerOnlyForCopyingException.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.eclipse.serializer.reference.Lazy;
66
import org.eclipse.serializer.reference.ObjectSwizzling;
7+
import org.eclipse.serializer.reference.Swizzling;
78

89

910
public interface SpringDataEclipseStoreLazy<T> extends Lazy<T>
@@ -13,11 +14,6 @@ static <T> SpringDataEclipseStoreLazy.Default<T> build(final T objectToWrapInLaz
1314
return new Default<>(objectToWrapInLazy);
1415
}
1516

16-
static SpringDataEclipseStoreLazy.Default<?> buildFromStorage(final long objectId, final ObjectSwizzling loader)
17-
{
18-
return new Default<>(objectId, loader);
19-
}
20-
2117
static SpringDataEclipseStoreLazy.Default<?> buildOnlyForStorage(final long objectId)
2218
{
2319
return new Default<>(objectId);
@@ -28,8 +24,9 @@ static SpringDataEclipseStoreLazy.Default<?> buildOnlyForStorage(final long obje
2824
class Default<T> implements SpringDataEclipseStoreLazy<T>
2925
{
3026
private Lazy<T> wrappedLazy;
31-
private long objectId;
32-
private ObjectSwizzling loader;
27+
private long objectId = Swizzling.notFoundId();
28+
private transient ObjectSwizzling loader;
29+
private transient boolean isStored = false;
3330

3431
private Default(final T wrappedObject)
3532
{
@@ -80,14 +77,25 @@ public T peek()
8077
@Override
8178
public T clear()
8279
{
80+
if(!this.isStored())
81+
{
82+
throw new IllegalStateException("Cannot clear an unstored lazy reference.");
83+
}
84+
// Make sure to save the correct objectId.
85+
this.objectId = this.objectId();
8386
this.wrappedLazy = null;
8487
return null;
8588
}
8689

8790
@Override
8891
public boolean isStored()
8992
{
90-
return this.ensureLazy().isStored();
93+
return this.isStored;
94+
}
95+
96+
void setStored()
97+
{
98+
this.isStored = true;
9199
}
92100

93101
@Override

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import org.eclipse.serializer.reference.ObjectSwizzling;
1313
import org.eclipse.serializer.reflect.XReflect;
1414

15-
import software.xdev.spring.data.eclipse.store.exceptions.BinaryHandlerOnlyForCopyingException;
16-
1715

1816
/**
1917
* Copied from
@@ -55,6 +53,7 @@ public final void store(
5553
final long referenceOid = instance.objectId();
5654
data.storeEntityHeader(Binary.referenceBinaryLength(1), this.typeId(), objectId);
5755
data.store_long(referenceOid);
56+
instance.setStored();
5857
}
5958

6059
@SuppressWarnings("unchecked")
@@ -75,7 +74,7 @@ public final void updateState(
7574
final PersistenceLoadHandler handler
7675
)
7776
{
78-
throw new BinaryHandlerOnlyForCopyingException();
77+
// no-op
7978
}
8079

8180
@Override

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/helper/TestData.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public final class TestData
2222
public static final String LAST_NAME_ALTERNATIVE = "Nicks";
2323
public static final String FIRST_NAME_ALTERNATIVE = "Stevie";
2424

25+
public static final String DUMMY_STRING = "-- skldfoöüä+#+!(/%&/%($§\"=)()nxjkß? _:.. ,-.;";
26+
public static final String DUMMY_STRING_ALTERNATIVE = " abc ";
27+
2528
private TestData()
2629
{
2730
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/lazy/LazyTest.java

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@
1515
*/
1616
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.lazy;
1717

18+
import static software.xdev.spring.data.eclipse.store.helper.TestUtil.restartDatastore;
19+
1820
import org.eclipse.serializer.collections.lazy.LazyArrayList;
1921
import org.eclipse.serializer.collections.lazy.LazyList;
22+
import org.eclipse.serializer.reference.Lazy;
2023
import org.junit.jupiter.api.Assertions;
2124
import org.junit.jupiter.api.Test;
2225
import org.springframework.beans.factory.annotation.Autowired;
2326
import org.springframework.test.context.ContextConfiguration;
2427

28+
import software.xdev.spring.data.eclipse.store.helper.TestData;
2529
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
2630
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
31+
import software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipseStoreLazy;
2732

2833

2934
@IsolatedTestAnnotations
@@ -34,10 +39,10 @@ class LazyTest
3439
private LazyTestConfiguration configuration;
3540

3641
@Test
37-
void lazyListStore(final ObjectWithLazyListRepository repository)
42+
void lazyListStore(@Autowired final ObjectWithLazyListRepository repository)
3843
{
3944
final ObjectWithLazyList newList = new ObjectWithLazyList();
40-
final SimpleObject objectToStore = new SimpleObject("Test");
45+
final SimpleObject objectToStore = new SimpleObject(TestData.DUMMY_STRING);
4146
final LazyArrayList lazyArrayList = new LazyArrayList();
4247
lazyArrayList.add(objectToStore);
4348
newList.setLazyList(lazyArrayList);
@@ -53,4 +58,121 @@ void lazyListStore(final ObjectWithLazyListRepository repository)
5358
}
5459
);
5560
}
61+
62+
@Test
63+
void lazyStore(@Autowired final ObjectWithLazyRepository repository)
64+
{
65+
final ObjectWithLazy newLazy = new ObjectWithLazy();
66+
final SimpleObject objectToStore = new SimpleObject(TestData.DUMMY_STRING);
67+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore));
68+
repository.save(newLazy);
69+
70+
TestUtil.doBeforeAndAfterRestartOfDatastore(
71+
this.configuration,
72+
() -> {
73+
Assertions.assertEquals(1, repository.findAll().size());
74+
final Lazy<SimpleObject> lazy = repository.findAll().get(0).getLazy();
75+
Assertions.assertEquals(objectToStore, lazy.get());
76+
}
77+
);
78+
}
79+
80+
@Test
81+
void lazyClearBeforeSave(@Autowired final ObjectWithLazyRepository repository)
82+
{
83+
final ObjectWithLazy newLazy = new ObjectWithLazy();
84+
final SimpleObject objectToStore = new SimpleObject(TestData.DUMMY_STRING);
85+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore));
86+
Assertions.assertThrows(IllegalStateException.class, () -> newLazy.getLazy().clear());
87+
}
88+
89+
@Test
90+
void lazyClearAfterSave(@Autowired final ObjectWithLazyRepository repository)
91+
{
92+
final ObjectWithLazy newLazy = new ObjectWithLazy();
93+
final SimpleObject objectToStore = new SimpleObject(TestData.DUMMY_STRING);
94+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore));
95+
repository.save(newLazy);
96+
newLazy.getLazy().clear();
97+
98+
TestUtil.doBeforeAndAfterRestartOfDatastore(
99+
this.configuration,
100+
() -> {
101+
Assertions.assertEquals(1, repository.findAll().size());
102+
final Lazy<SimpleObject> lazy = repository.findAll().get(0).getLazy();
103+
Assertions.assertEquals(objectToStore, lazy.get());
104+
}
105+
);
106+
}
107+
108+
@Test
109+
void lazyChangeAfterSave(@Autowired final ObjectWithLazyRepository repository)
110+
{
111+
final ObjectWithLazy newLazy = new ObjectWithLazy();
112+
final SimpleObject objectToStore = new SimpleObject(TestData.DUMMY_STRING);
113+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore));
114+
repository.save(newLazy);
115+
116+
final SimpleObject objectToStore2 = new SimpleObject(TestData.DUMMY_STRING_ALTERNATIVE);
117+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore2));
118+
repository.save(newLazy);
119+
120+
TestUtil.doBeforeAndAfterRestartOfDatastore(
121+
this.configuration,
122+
() -> {
123+
Assertions.assertEquals(1, repository.findAll().size());
124+
final Lazy<SimpleObject> lazy = repository.findAll().get(0).getLazy();
125+
Assertions.assertEquals(objectToStore2, lazy.get());
126+
}
127+
);
128+
}
129+
130+
@Test
131+
void lazyChangeBeforeSave(@Autowired final ObjectWithLazyRepository repository)
132+
{
133+
final ObjectWithLazy newLazy = new ObjectWithLazy();
134+
final SimpleObject objectToStore = new SimpleObject(TestData.DUMMY_STRING);
135+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore));
136+
137+
final SimpleObject objectToStore2 = new SimpleObject(TestData.DUMMY_STRING_ALTERNATIVE);
138+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore2));
139+
repository.save(newLazy);
140+
141+
TestUtil.doBeforeAndAfterRestartOfDatastore(
142+
this.configuration,
143+
() -> {
144+
Assertions.assertEquals(1, repository.findAll().size());
145+
final Lazy<SimpleObject> lazy = repository.findAll().get(0).getLazy();
146+
Assertions.assertEquals(objectToStore2, lazy.get());
147+
}
148+
);
149+
}
150+
151+
@Test
152+
void lazyChangeAfterRestart(@Autowired final ObjectWithLazyRepository repository)
153+
{
154+
final ObjectWithLazy newLazy = new ObjectWithLazy();
155+
final SimpleObject objectToStore = new SimpleObject(TestData.DUMMY_STRING);
156+
newLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore));
157+
repository.save(newLazy);
158+
159+
restartDatastore(this.configuration);
160+
161+
Assertions.assertEquals(1, repository.findAll().size());
162+
final ObjectWithLazy reloadedObjectWithLazy = repository.findAll().get(0);
163+
Assertions.assertEquals(objectToStore, reloadedObjectWithLazy.getLazy().get());
164+
165+
final SimpleObject objectToStore2 = new SimpleObject(TestData.DUMMY_STRING_ALTERNATIVE);
166+
reloadedObjectWithLazy.setLazy(SpringDataEclipseStoreLazy.build(objectToStore2));
167+
repository.save(reloadedObjectWithLazy);
168+
169+
TestUtil.doBeforeAndAfterRestartOfDatastore(
170+
this.configuration,
171+
() -> {
172+
Assertions.assertEquals(1, repository.findAll().size());
173+
final Lazy<SimpleObject> lazy = repository.findAll().get(0).getLazy();
174+
Assertions.assertEquals(objectToStore2, lazy.get());
175+
}
176+
);
177+
}
56178
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.lazy;
17+
18+
import org.eclipse.serializer.reference.Lazy;
19+
20+
21+
public class ObjectWithLazy
22+
{
23+
private Lazy<SimpleObject> lazy;
24+
25+
public Lazy<SimpleObject> getLazy()
26+
{
27+
return this.lazy;
28+
}
29+
30+
public void setLazy(final Lazy<SimpleObject> lazy)
31+
{
32+
this.lazy = lazy;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.lazy;
17+
18+
import org.springframework.data.repository.ListCrudRepository;
19+
20+
21+
public interface ObjectWithLazyRepository extends ListCrudRepository<ObjectWithLazy, Integer>
22+
{
23+
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/special/types/TypesData.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,6 @@ public static Stream<Arguments> generateData()
439439
id -> new LazyDaoObject(id, SpringDataEclipseStoreLazy.build("1")),
440440
object -> object.setValue(SpringDataEclipseStoreLazy.build("2"))
441441
),
442-
new TestArguments<>(
443-
LazyRepository.class,
444-
id -> new LazyDaoObject(id, SpringDataEclipseStoreLazy.build("1")),
445-
object -> object.getValue().clear()
446-
),
447442
new TestArguments<>(
448443
MapRepository.class,
449444
id -> new MapDaoObject(id, new LazyHashMap<>()),

0 commit comments

Comments
 (0)