Skip to content

Commit a85b4f3

Browse files
committed
DATACMNS-1364 - Store persistent properties in HashMap.
We now use HashMap to store persistent properties of a PersistentEntity. An entity is built in a single thread so no concurrent modification happens. Concurrent reads may happen during entity usage which is fine as the PersistentEntity is not changed anymore. Previously, we used ConcurrentReferenceHashMap defaulting to soft references. Soft references can be cleared at the discretion of the GC in response to memory demand. So a default ConcurrentReferenceHashMap is memory-sensitive and acts like a cache with memory-based eviction rules. Persistent properties are not subject to be cached but elements of a PersistentEntity and cannot be recovered once cleared. Original pull request: #304.
1 parent b80f5c6 commit a85b4f3

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Diff for: src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import org.springframework.data.util.TypeInformation;
3232
import org.springframework.lang.Nullable;
3333
import org.springframework.util.Assert;
34-
import org.springframework.util.LinkedMultiValueMap;
34+
import org.springframework.util.CollectionUtils;
35+
import org.springframework.util.ConcurrentReferenceHashMap;
3536
import org.springframework.util.MultiValueMap;
3637
import org.springframework.util.StringUtils;
3738

@@ -95,8 +96,8 @@ public BasicPersistentEntity(TypeInformation<T> information, @Nullable Comparato
9596
this.associations = comparator == null ? new HashSet<>() : new TreeSet<>(new AssociationComparator<>(comparator));
9697

9798
this.propertyCache = new HashMap<>();
98-
this.annotationCache = new HashMap<>();
99-
this.propertyAnnotationCache = new LinkedMultiValueMap<>();
99+
this.annotationCache = new ConcurrentReferenceHashMap<>();
100+
this.propertyAnnotationCache = CollectionUtils.toMultiValueMap(new ConcurrentReferenceHashMap<>());
100101
this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE;
101102
this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType()));
102103
}

Diff for: src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,27 @@ public void considersComparatorForPropertyOrder() {
126126
assertThat(entity.getPersistentProperty("ssn")).isEqualTo(iterator.next());
127127
}
128128

129-
@Test // DATACMNS-186
129+
@Test // DATACMNS-18, DATACMNS-1364
130130
public void addingAndIdPropertySetsIdPropertyInternally() {
131131

132132
MutablePersistentEntity<Person, T> entity = createEntity(Person.class);
133133
assertThat(entity.getIdProperty()).isNull();
134134

135+
when(property.getName()).thenReturn("id");
135136
when(property.isIdProperty()).thenReturn(true);
136137
entity.addPersistentProperty(property);
137138
assertThat(entity.getIdProperty()).isEqualTo(property);
138139
}
139140

140-
@Test // DATACMNS-186
141+
@Test // DATACMNS-186, DATACMNS-1364
141142
public void rejectsIdPropertyIfAlreadySet() {
142143

143144
MutablePersistentEntity<Person, T> entity = createEntity(Person.class);
144145

146+
when(property.getName()).thenReturn("id");
145147
when(property.isIdProperty()).thenReturn(true);
146148
when(anotherProperty.isIdProperty()).thenReturn(true);
149+
when(anotherProperty.getName()).thenReturn("another");
147150

148151
entity.addPersistentProperty(property);
149152
exception.expect(MappingException.class);
@@ -331,7 +334,7 @@ static class PersistableEntity implements Persistable<Long> {
331334

332335
private final Long id = 42L;
333336

334-
/*
337+
/*
335338
* (non-Javadoc)
336339
* @see org.springframework.data.domain.Persistable#getId()
337340
*/
@@ -340,7 +343,7 @@ public Long getId() {
340343
return 4711L;
341344
}
342345

343-
/*
346+
/*
344347
* (non-Javadoc)
345348
* @see org.springframework.data.domain.Persistable#isNew()
346349
*/

0 commit comments

Comments
 (0)