Skip to content

Commit 1d2bd3a

Browse files
Fix - Nonlazy functionality
1 parent cd26674 commit 1d2bd3a

9 files changed

Lines changed: 106 additions & 45 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,14 @@ private void ensureEntityData()
209209
public <T, ID> void createNewEntityData(final Class<T> entityClass, final VersionedRoot root)
210210
{
211211
final IdManager<T, ID> idManager = this.ensureIdManager(entityClass);
212-
if(this.entityClassToRepository.get(entityClass).isLazy())
212+
final SimpleEclipseStoreRepository<?, ?> repository = this.entityClassToRepository.get(entityClass);
213+
if(repository != null && repository.isLazy())
213214
{
214-
root.getCurrentRootData().createNewLazyEntityData(entityClass, idManager::getId);
215+
root.getCurrentRootData().createNewLazyEntityData(entityClass, idManager);
215216
}
216217
else
217218
{
218-
root.getCurrentRootData().createNewEntityData(entityClass, idManager::getId);
219+
root.getCurrentRootData().createNewEntityData(entityClass, idManager);
219220
}
220221
}
221222

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public Root getRootDataV1()
6161
return this.rootDataV1;
6262
}
6363

64+
/**
65+
* @deprecated and is only used in tests
66+
*/
67+
@Deprecated
68+
public void setRootDataV2(final RootDataV2 rootDataV2)
69+
{
70+
this.rootDataV2 = rootDataV2;
71+
}
72+
6473
public RootDataV2 getRootDataV2()
6574
{
6675
return this.rootDataV2;
@@ -69,7 +78,7 @@ public RootDataV2 getRootDataV2()
6978
@SuppressWarnings("checkstyle:MethodName")
7079
public RootDataV2_4 getRootDataV2_4()
7180
{
72-
return rootDataV2_4;
81+
return this.rootDataV2_4;
7382
}
7483

7584
public void clearOldRootData()

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/update/scripts/v2_0_0_InitializeVersioning.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
import software.xdev.micromigration.eclipsestore.MigrationEmbeddedStorageManager;
2222
import software.xdev.micromigration.scripts.Context;
23-
import software.xdev.spring.data.eclipse.store.repository.root.EntityData;
2423
import software.xdev.spring.data.eclipse.store.repository.root.RootDataV2;
2524
import software.xdev.spring.data.eclipse.store.repository.root.VersionedRoot;
25+
import software.xdev.spring.data.eclipse.store.repository.root.v2_4.EntityData;
2626

2727

2828
/**
@@ -44,10 +44,12 @@ public void loggedMigrate(final Context<VersionedRoot, MigrationEmbeddedStorageM
4444
versionedRoot.getRootDataV1().getEntityLists().forEach(
4545
(entityName, entities) ->
4646
{
47-
final EntityData<Object, Object> entityData = versionedRoot.getRootDataV2().getEntityData(entityName);
47+
final EntityData<Object, Object> entityData =
48+
versionedRoot.getCurrentRootData().getEntityData(entityName);
4849
if(entityData == null)
4950
{
5051
LOG.warn("Dropping entities {} because there is no repository in the new root.", entityName);
52+
return;
5153
}
5254
entities.forEach(entity -> entityData.ensureEntityAndReturnObjectsToStore(entity));
5355
context.getStorageManager().getNativeStorageManager().storeAll(entityData.getObjectsToStore());
@@ -57,7 +59,8 @@ public void loggedMigrate(final Context<VersionedRoot, MigrationEmbeddedStorageM
5759
versionedRoot.getRootDataV1().getLastIds().forEach(
5860
(entityName, lastId) ->
5961
{
60-
final EntityData<Object, Object> entityData = versionedRoot.getRootDataV2().getEntityData(entityName);
62+
final software.xdev.spring.data.eclipse.store.repository.root.EntityData<Object, Object> entityData =
63+
versionedRoot.getRootDataV2().getEntityData(entityName);
6164
entityData.setLastId(lastId);
6265
context.getStorageManager().store(entityData);
6366
LOG.info("Migrated last id of entities {}.", entityName);

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/update/scripts/v2_4_0_InitializeLazy.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ public class v2_4_0_InitializeLazy extends LoggingUpdateScript
4040
public void loggedMigrate(final Context<VersionedRoot, MigrationEmbeddedStorageManager> context)
4141
{
4242
final VersionedRoot versionedRoot = context.getMigratingObject();
43-
versionedRoot.getRootDataV2().getEntityListsToStore().forEach(
44-
(entityName, entities) ->
45-
{
46-
final software.xdev.spring.data.eclipse.store.repository.root.v2_4.EntityData<Object, Object>
47-
newEntityData = versionedRoot.getRootDataV2_4().getEntityData(entityName);
48-
entities.getEntities().forEach(newEntityData::ensureEntityAndReturnObjectsToStore);
49-
newEntityData.setLastId(entities.getLastId());
50-
context.getStorageManager().getNativeStorageManager().storeAll(newEntityData.getObjectsToStore());
51-
LOG.info("Migrated entities {}.", entityName);
52-
}
53-
);
43+
if(versionedRoot.getRootDataV2() != null)
44+
{
45+
versionedRoot.getRootDataV2().getEntityListsToStore().forEach(
46+
(entityName, entities) ->
47+
{
48+
final software.xdev.spring.data.eclipse.store.repository.root.v2_4.EntityData<Object, Object>
49+
newEntityData = versionedRoot.getCurrentRootData().getEntityData(entityName);
50+
entities.getEntities().forEach(newEntityData::ensureEntityAndReturnObjectsToStore);
51+
newEntityData.setLastId(entities.getLastId());
52+
context.getStorageManager().getNativeStorageManager().storeAll(newEntityData.getObjectsToStore());
53+
LOG.info("Migrated entities {}.", entityName);
54+
}
55+
);
56+
}
5457
versionedRoot.clearOldRootData();
5558
context.getStorageManager().store(versionedRoot);
5659
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/v2_4/EntityData.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.stream.Stream;
2222

2323
import software.xdev.spring.data.eclipse.store.core.IdentitySet;
24+
import software.xdev.spring.data.eclipse.store.repository.support.id.IdGetter;
2425

2526

2627
/**
@@ -33,7 +34,7 @@ public interface EntityData<T, ID>
3334
/**
3435
* Accepts {@code null} if no id field is defined
3536
*/
36-
void setIdGetter(final Function<T, ID> idGetter);
37+
void setIdGetter(final IdGetter<T, ID> idGetter);
3738

3839
Stream<T> getEntitiesAsStream();
3940

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/v2_4/LazyEntityData.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import software.xdev.spring.data.eclipse.store.core.IdentitySet;
2828
import software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipseStoreLazy;
29+
import software.xdev.spring.data.eclipse.store.repository.support.id.IdGetter;
2930

3031

3132
/**
@@ -45,7 +46,7 @@ public class LazyEntityData<T, ID> implements EntityData<T, ID>
4546
*/
4647
private final HashMap<ID, Lazy<T>> entitiesById;
4748

48-
private transient Function<T, ID> idGetter;
49+
private transient IdGetter<T, ID> idGetter;
4950

5051
public LazyEntityData()
5152
{
@@ -57,7 +58,7 @@ public LazyEntityData()
5758
* Accepts {@code null} if no id field is defined
5859
*/
5960
@Override
60-
public void setIdGetter(final Function<T, ID> idGetter)
61+
public void setIdGetter(final IdGetter<T, ID> idGetter)
6162
{
6263
this.idGetter = idGetter;
6364

@@ -81,7 +82,7 @@ public boolean containsEntity(final T entity)
8182
}
8283
else
8384
{
84-
final ID id = this.idGetter.apply(entity);
85+
final ID id = this.idGetter.getId(entity);
8586
return this.entitiesById.containsKey(id);
8687
}
8788
}
@@ -91,7 +92,7 @@ private void ensureEntitiesAndEntitiesByIdAreTheSameSize()
9192
if(this.idGetter != null && this.entities.size() != this.entitiesById.size())
9293
{
9394
this.entitiesById.clear();
94-
this.entities.forEach(entity -> this.entitiesById.put(this.idGetter.apply(entity.get()), entity));
95+
this.entities.forEach(entity -> this.entitiesById.put(this.idGetter.getId(entity.get()), entity));
9596
}
9697
if(this.idGetter == null)
9798
{
@@ -134,7 +135,7 @@ public Collection<Object> ensureEntityAndReturnObjectsToStore(final T entityToSt
134135
this.entities.add(newLazyEntity);
135136
if(this.idGetter != null)
136137
{
137-
this.entitiesById.put(this.idGetter.apply(entityToStore), newLazyEntity);
138+
this.entitiesById.put(this.idGetter.getId(entityToStore), newLazyEntity);
138139
}
139140
return this.getObjectsToStore();
140141
}
@@ -160,7 +161,7 @@ public Collection<Object> removeEntityAndReturnObjectsToStore(final T entityToRe
160161
}
161162
else
162163
{
163-
final ID id = this.idGetter.apply(entityToRemove);
164+
final ID id = this.idGetter.getId(entityToRemove);
164165
final Lazy<T> lazyReference = this.entitiesById.get(id);
165166
this.entities.remove(lazyReference);
166167
this.entitiesById.remove(id);

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/v2_4/NonLazyEntityData.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.stream.Stream;
2323

2424
import software.xdev.spring.data.eclipse.store.core.IdentitySet;
25+
import software.xdev.spring.data.eclipse.store.repository.support.id.IdGetter;
2526

2627

2728
/**
@@ -41,7 +42,7 @@ public class NonLazyEntityData<T, ID> implements EntityData<T, ID>
4142
*/
4243
private final HashMap<ID, T> entitiesById;
4344

44-
private transient Function<T, ID> idGetter;
45+
private transient IdGetter<T, ID> idGetter;
4546

4647
public NonLazyEntityData()
4748
{
@@ -53,7 +54,7 @@ public NonLazyEntityData()
5354
* Accepts {@code null} if no id field is defined
5455
*/
5556
@Override
56-
public void setIdGetter(final Function<T, ID> idGetter)
57+
public void setIdGetter(final IdGetter<T, ID> idGetter)
5758
{
5859
this.idGetter = idGetter;
5960

@@ -75,8 +76,9 @@ public boolean containsEntity(final T entity)
7576
}
7677
else
7778
{
78-
final ID id = this.idGetter.apply(entity);
79-
return this.entitiesById.containsKey(id);
79+
final ID id = this.idGetter.getId(entity);
80+
final T existingEntity = this.entitiesById.get(id);
81+
return existingEntity != null && existingEntity == entity;
8082
}
8183
}
8284

@@ -85,7 +87,7 @@ private void ensureEntitiesAndEntitiesByIdAreTheSameSize()
8587
if(this.idGetter != null && this.entities.size() != this.entitiesById.size())
8688
{
8789
this.entitiesById.clear();
88-
this.entities.forEach(entity -> this.entitiesById.put(this.idGetter.apply(entity), entity));
90+
this.entities.forEach(entity -> this.entitiesById.put(this.idGetter.getId(entity), entity));
8991
}
9092
if(this.idGetter == null)
9193
{
@@ -114,16 +116,17 @@ public void setLastId(final Object lastId)
114116
@Override
115117
public Collection<Object> ensureEntityAndReturnObjectsToStore(final T entityToStore)
116118
{
117-
if(!this.containsEntity(entityToStore))
119+
Collection<Object> listToSave = List.of();
120+
if(this.entities.add(entityToStore))
118121
{
119-
this.entities.add(entityToStore);
120-
if(this.idGetter != null)
121-
{
122-
this.entitiesById.put(this.idGetter.apply(entityToStore), entityToStore);
123-
}
124-
return this.getObjectsToStore();
122+
listToSave = this.getObjectsToStore();
125123
}
126-
return List.of();
124+
if(this.idGetter != null && this.entitiesById.get(this.idGetter.getId(entityToStore)) != entityToStore)
125+
{
126+
this.entitiesById.put(this.idGetter.getId(entityToStore), entityToStore);
127+
listToSave = this.getObjectsToStore();
128+
}
129+
return listToSave;
127130
}
128131

129132
@Override
@@ -144,7 +147,7 @@ public Collection<Object> removeEntityAndReturnObjectsToStore(final T entityToRe
144147
this.entities.remove(entityToRemove);
145148
if(this.idGetter != null)
146149
{
147-
this.entitiesById.remove(this.idGetter.apply(entityToRemove));
150+
this.entitiesById.remove(this.idGetter.getId(entityToRemove));
148151
}
149152
return this.getObjectsToStore();
150153
}

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/v2_4/RootDataV2_4.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
20-
import java.util.function.Function;
20+
21+
import software.xdev.spring.data.eclipse.store.repository.support.id.IdGetter;
2122

2223

2324
/**
@@ -59,14 +60,14 @@ public <T, ID> EntityData<T, ID> getEntityData(final String entityClassName)
5960
return (EntityData<T, ID>)this.entityLists.get(entityClassName);
6061
}
6162

62-
public <T, ID> void createNewEntityData(final Class<T> entityClass, final Function<T, ID> idGetter)
63+
public <T, ID> void createNewEntityData(final Class<T> entityClass, final IdGetter<T, ID> idGetter)
6364
{
6465
final NonLazyEntityData<T, ID> entityData = new NonLazyEntityData<>();
6566
entityData.setIdGetter(idGetter);
6667
this.entityLists.put(this.getEntityName(entityClass), entityData);
6768
}
6869

69-
public <T, ID> void createNewLazyEntityData(final Class<T> entityClass, final Function<T, ID> idGetter)
70+
public <T, ID> void createNewLazyEntityData(final Class<T> entityClass, final IdGetter<T, ID> idGetter)
7071
{
7172
final LazyEntityData<T, ID> entityData = new LazyEntityData<>();
7273
entityData.setIdGetter(idGetter);

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/migration/MigrationTest.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@
3030
import org.springframework.test.context.ContextConfiguration;
3131
import org.springframework.util.FileSystemUtils;
3232

33+
import software.xdev.micromigration.version.MigrationVersion;
3334
import software.xdev.spring.data.eclipse.store.helper.TestData;
3435
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
3536
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
3637
import software.xdev.spring.data.eclipse.store.repository.Root;
38+
import software.xdev.spring.data.eclipse.store.repository.root.RootDataV2;
39+
import software.xdev.spring.data.eclipse.store.repository.root.VersionedRoot;
3740

3841

42+
@SuppressWarnings({"deprecation", "checkstyle:MethodName"})
3943
@IsolatedTestAnnotations
4044
@ContextConfiguration(classes = {MigrationTestConfiguration.class})
4145
class MigrationTest
@@ -53,9 +57,9 @@ public MigrationTest(final MigrationTestConfiguration configuration, final UserR
5357
}
5458

5559
@Test
56-
void simpleMigrateFromV000ToV200() throws IOException
60+
void simpleMigrateFromV0_0_0ToNewest() throws IOException
5761
{
58-
this.initOldData();
62+
this.initV1_0_0Data();
5963
TestUtil.doBeforeAndAfterRestartOfDatastore(
6064
this.configuration,
6165
() -> {
@@ -66,7 +70,21 @@ void simpleMigrateFromV000ToV200() throws IOException
6670
);
6771
}
6872

69-
private void initOldData() throws IOException
73+
@Test
74+
void simpleMigrateFromV2_0_0ToNewest() throws IOException
75+
{
76+
this.initV2_0_0Data();
77+
TestUtil.doBeforeAndAfterRestartOfDatastore(
78+
this.configuration,
79+
() -> {
80+
final List<User> foundUsers = this.userRepository.findAll();
81+
Assertions.assertEquals(1, foundUsers.size());
82+
Assertions.assertEquals(TEST_USER, foundUsers.get(0));
83+
}
84+
);
85+
}
86+
87+
private void initV1_0_0Data() throws IOException
7088
{
7189
// Delete already created data
7290
this.configuration.getStorageInstance().stop();
@@ -84,4 +102,25 @@ private void initOldData() throws IOException
84102
storageManager.storeRoot();
85103
}
86104
}
105+
106+
private void initV2_0_0Data() throws IOException
107+
{
108+
// Delete already created data
109+
this.configuration.getStorageInstance().stop();
110+
FileSystemUtils.deleteRecursively(Path.of(this.configuration.getStorageDirectory()));
111+
112+
// Init old data
113+
try(final EmbeddedStorageManager storageManager =
114+
Foundation(Storage.Configuration(Storage.FileProvider(Path.of(this.configuration.getStorageDirectory()))))
115+
.start())
116+
{
117+
final VersionedRoot oldRoot = new VersionedRoot();
118+
oldRoot.setRootDataV2(new RootDataV2());
119+
oldRoot.getRootDataV2().createNewEntityData(User.class, e -> null);
120+
oldRoot.getRootDataV2().getEntityData(User.class).ensureEntityAndReturnObjectsToStore(TEST_USER);
121+
oldRoot.setVersion(new MigrationVersion(2, 0, 0));
122+
storageManager.setRoot(oldRoot);
123+
storageManager.storeRoot();
124+
}
125+
}
87126
}

0 commit comments

Comments
 (0)