Skip to content

Commit cd26674

Browse files
Implemented LazyEntitiyData as Lazy
1 parent a46bdf1 commit cd26674

6 files changed

Lines changed: 108 additions & 36 deletions

File tree

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/core/EntityProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import java.util.ArrayList;
1919
import java.util.Collection;
2020
import java.util.List;
21+
import java.util.Objects;
2122
import java.util.Optional;
22-
import java.util.Set;
2323
import java.util.stream.Collectors;
2424
import java.util.stream.Stream;
2525

@@ -38,7 +38,7 @@ public void addEntityData(final EntityData<? extends T, ID> entityData)
3838

3939
public Stream<? extends T> stream()
4040
{
41-
return this.entityDataList.stream().map(EntityData::getEntities).flatMap(Set::stream);
41+
return this.entityDataList.stream().flatMap(EntityData::getEntitiesAsStream);
4242
}
4343

4444
public Collection<T> toCollection()
@@ -61,8 +61,8 @@ public Optional<T> findAnyEntityWithId(final ID id)
6161
{
6262
return (Optional<T>)this.entityDataList
6363
.stream()
64-
.map(entityData -> entityData.getEntitiesById().get(id))
65-
.filter(e -> e != null)
64+
.map(entityData -> entityData.getEntityById(id))
65+
.filter(Objects::nonNull)
6666
.findAny();
6767
}
6868
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collection;
1919
import java.util.HashMap;
2020
import java.util.function.Function;
21+
import java.util.stream.Stream;
2122

2223
import software.xdev.spring.data.eclipse.store.core.IdentitySet;
2324

@@ -34,11 +35,11 @@ public interface EntityData<T, ID>
3435
*/
3536
void setIdGetter(final Function<T, ID> idGetter);
3637

37-
IdentitySet<T> getEntities();
38+
Stream<T> getEntitiesAsStream();
3839

39-
ID getLastId();
40+
boolean containsEntity(final T entity);
4041

41-
HashMap<ID, T> getEntitiesById();
42+
ID getLastId();
4243

4344
long getEntityCount();
4445

@@ -51,4 +52,6 @@ public interface EntityData<T, ID>
5152
Collection<Object> removeEntityAndReturnObjectsToStore(final T entityToRemove);
5253

5354
Collection<Object> removeAllEntitiesAndReturnObjectsToStore();
55+
56+
T getEntityById(ID id);
5457
}

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

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.function.Function;
22+
import java.util.stream.Stream;
23+
24+
import org.eclipse.serializer.reference.Lazy;
25+
import org.eclipse.serializer.reference.Referencing;
2226

2327
import software.xdev.spring.data.eclipse.store.core.IdentitySet;
28+
import software.xdev.spring.data.eclipse.store.repository.lazy.SpringDataEclipseStoreLazy;
2429

2530

2631
/**
@@ -29,7 +34,7 @@
2934
*/
3035
public class LazyEntityData<T, ID> implements EntityData<T, ID>
3136
{
32-
private final IdentitySet<T> entities;
37+
private final IdentitySet<Lazy<T>> entities;
3338
private ID lastId;
3439
/**
3540
* "Why do you keep the entites at two places? This seems like a waste of space." - Yes, it seems like it is
@@ -38,7 +43,7 @@ public class LazyEntityData<T, ID> implements EntityData<T, ID>
3843
* provided by google and apache, but they implemented this also with two seperate lists so there is no benefit in
3944
* using these.
4045
*/
41-
private final HashMap<ID, T> entitiesById;
46+
private final HashMap<ID, Lazy<T>> entitiesById;
4247

4348
private transient Function<T, ID> idGetter;
4449

@@ -51,80 +56,119 @@ public LazyEntityData()
5156
/**
5257
* Accepts {@code null} if no id field is defined
5358
*/
59+
@Override
5460
public void setIdGetter(final Function<T, ID> idGetter)
5561
{
5662
this.idGetter = idGetter;
5763

5864
this.ensureEntitiesAndEntitiesByIdAreTheSameSize();
5965
}
6066

67+
@Override
68+
public Stream<T> getEntitiesAsStream()
69+
{
70+
return this.entities.stream().map(Referencing::get);
71+
}
72+
73+
@Override
74+
public boolean containsEntity(final T entity)
75+
{
76+
if(this.idGetter == null)
77+
{
78+
return this.entities
79+
.stream()
80+
.anyMatch(lazyEntity -> lazyEntity.get() == entity);
81+
}
82+
else
83+
{
84+
final ID id = this.idGetter.apply(entity);
85+
return this.entitiesById.containsKey(id);
86+
}
87+
}
88+
6189
private void ensureEntitiesAndEntitiesByIdAreTheSameSize()
6290
{
6391
if(this.idGetter != null && this.entities.size() != this.entitiesById.size())
6492
{
6593
this.entitiesById.clear();
66-
this.entities.forEach(entity -> this.entitiesById.put(this.idGetter.apply(entity), entity));
94+
this.entities.forEach(entity -> this.entitiesById.put(this.idGetter.apply(entity.get()), entity));
6795
}
6896
if(this.idGetter == null)
6997
{
7098
this.entitiesById.clear();
7199
}
72100
}
73101

74-
public IdentitySet<T> getEntities()
75-
{
76-
return this.entities;
77-
}
78-
102+
@Override
79103
public ID getLastId()
80104
{
81105
return this.lastId;
82106
}
83107

84-
public HashMap<ID, T> getEntitiesById()
108+
@Override
109+
public T getEntityById(final ID id)
85110
{
86-
return this.entitiesById;
111+
final Lazy<T> lazyEntity = this.entitiesById.get(id);
112+
return lazyEntity == null ? null : lazyEntity.get();
87113
}
88114

115+
@Override
89116
public long getEntityCount()
90117
{
91118
return this.entities.size();
92119
}
93120

121+
@Override
94122
public void setLastId(final Object lastId)
95123
{
96124
this.lastId = (ID)lastId;
97125
}
98126

127+
@Override
99128
public Collection<Object> ensureEntityAndReturnObjectsToStore(final T entityToStore)
100129
{
101-
if(!this.getEntities().contains(entityToStore))
130+
if(!this.containsEntity(entityToStore))
102131
{
103-
this.entities.add(entityToStore);
132+
final SpringDataEclipseStoreLazy.Default<T> newLazyEntity =
133+
SpringDataEclipseStoreLazy.build(entityToStore);
134+
this.entities.add(newLazyEntity);
104135
if(this.idGetter != null)
105136
{
106-
this.entitiesById.put(this.idGetter.apply(entityToStore), entityToStore);
137+
this.entitiesById.put(this.idGetter.apply(entityToStore), newLazyEntity);
107138
}
108139
return this.getObjectsToStore();
109140
}
110141
return List.of();
111142
}
112143

144+
@Override
113145
public Collection<Object> getObjectsToStore()
114146
{
115147
return List.of(this.entities.getInternalMap(), this.entitiesById);
116148
}
117149

150+
@Override
118151
public Collection<Object> removeEntityAndReturnObjectsToStore(final T entityToRemove)
119152
{
120-
this.entities.remove(entityToRemove);
121-
if(this.idGetter != null)
153+
if(this.idGetter == null)
154+
{
155+
this.entities
156+
.stream()
157+
.filter(entity -> entity.get() == entityToRemove)
158+
.findAny()
159+
.ifPresent(this.entities::remove);
160+
}
161+
else
122162
{
123-
this.entitiesById.remove(this.idGetter.apply(entityToRemove));
163+
final ID id = this.idGetter.apply(entityToRemove);
164+
final Lazy<T> lazyReference = this.entitiesById.get(id);
165+
this.entities.remove(lazyReference);
166+
this.entitiesById.remove(id);
124167
}
125168
return this.getObjectsToStore();
126169
}
127170

171+
@Override
128172
public Collection<Object> removeAllEntitiesAndReturnObjectsToStore()
129173
{
130174
this.entities.clear();

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.function.Function;
22+
import java.util.stream.Stream;
2223

2324
import software.xdev.spring.data.eclipse.store.core.IdentitySet;
2425

@@ -51,13 +52,34 @@ public NonLazyEntityData()
5152
/**
5253
* Accepts {@code null} if no id field is defined
5354
*/
55+
@Override
5456
public void setIdGetter(final Function<T, ID> idGetter)
5557
{
5658
this.idGetter = idGetter;
5759

5860
this.ensureEntitiesAndEntitiesByIdAreTheSameSize();
5961
}
6062

63+
@Override
64+
public Stream<T> getEntitiesAsStream()
65+
{
66+
return this.entities.stream();
67+
}
68+
69+
@Override
70+
public boolean containsEntity(final T entity)
71+
{
72+
if(this.idGetter == null)
73+
{
74+
return this.entities.contains(entity);
75+
}
76+
else
77+
{
78+
final ID id = this.idGetter.apply(entity);
79+
return this.entitiesById.containsKey(id);
80+
}
81+
}
82+
6183
private void ensureEntitiesAndEntitiesByIdAreTheSameSize()
6284
{
6385
if(this.idGetter != null && this.entities.size() != this.entitiesById.size())
@@ -71,34 +93,28 @@ private void ensureEntitiesAndEntitiesByIdAreTheSameSize()
7193
}
7294
}
7395

74-
public IdentitySet<T> getEntities()
75-
{
76-
return this.entities;
77-
}
78-
96+
@Override
7997
public ID getLastId()
8098
{
8199
return this.lastId;
82100
}
83101

84-
public HashMap<ID, T> getEntitiesById()
85-
{
86-
return this.entitiesById;
87-
}
88-
102+
@Override
89103
public long getEntityCount()
90104
{
91105
return this.entities.size();
92106
}
93107

108+
@Override
94109
public void setLastId(final Object lastId)
95110
{
96111
this.lastId = (ID)lastId;
97112
}
98113

114+
@Override
99115
public Collection<Object> ensureEntityAndReturnObjectsToStore(final T entityToStore)
100116
{
101-
if(!this.getEntities().contains(entityToStore))
117+
if(!this.containsEntity(entityToStore))
102118
{
103119
this.entities.add(entityToStore);
104120
if(this.idGetter != null)
@@ -110,11 +126,19 @@ public Collection<Object> ensureEntityAndReturnObjectsToStore(final T entityToSt
110126
return List.of();
111127
}
112128

129+
@Override
113130
public Collection<Object> getObjectsToStore()
114131
{
115132
return List.of(this.entities.getInternalMap(), this.entitiesById);
116133
}
117134

135+
@Override
136+
public T getEntityById(final ID id)
137+
{
138+
return this.entitiesById.get(id);
139+
}
140+
141+
@Override
118142
public Collection<Object> removeEntityAndReturnObjectsToStore(final T entityToRemove)
119143
{
120144
this.entities.remove(entityToRemove);
@@ -125,6 +149,7 @@ public Collection<Object> removeEntityAndReturnObjectsToStore(final T entityToRe
125149
return this.getObjectsToStore();
126150
}
127151

152+
@Override
128153
public Collection<Object> removeAllEntitiesAndReturnObjectsToStore()
129154
{
130155
this.entities.clear();

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/support/reposyncer/SimpleRepositorySynchronizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public SimpleRepositorySynchronizer(final RootDataV2_4 root)
5353
EntityData<Object, Object> entityDataForCurrentObject =
5454
this.root.getEntityData(objectInGraphClass);
5555
if(entityDataForCurrentObject != null
56-
&& !entityDataForCurrentObject.getEntities().contains(objectInGraph))
56+
&& !entityDataForCurrentObject.containsEntity(objectInGraph))
5757
{
5858
entityDataForCurrentObject.ensureEntityAndReturnObjectsToStore(objectInGraph);
5959
this.listsToStore.add(entityDataForCurrentObject);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public DummyEntityProvider(final Collection<T> collection)
3030
super();
3131
final EntityData<T, Void> objects = new NonLazyEntityData<>();
3232
objects.setIdGetter(i -> null);
33-
objects.getEntities().addAll(collection);
33+
collection.forEach(objects::ensureEntityAndReturnObjectsToStore);
3434
this.addEntityData(objects);
3535
}
3636

0 commit comments

Comments
 (0)