Skip to content

Commit 72129e7

Browse files
committed
Cache common PersistentProperty attributes.
We now use CachingCassandraPersistentProperty to pre-compute primary key and embedded flags to reduce computation load when using these properties. Resolves #1082
1 parent 0f99b8f commit 72129e7

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/BasicCassandraPersistentTupleProperty.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.cassandra.core.mapping;
1717

18+
import org.springframework.data.cassandra.core.cql.Ordering;
1819
import org.springframework.data.mapping.MappingException;
1920
import org.springframework.data.mapping.model.Property;
2021
import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -93,6 +94,15 @@ public Integer getOrdinal() {
9394
return this.ordinal;
9495
}
9596

97+
/* (non-Javadoc)
98+
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#getPrimaryKeyOrdering()
99+
*/
100+
@Nullable
101+
@Override
102+
public Ordering getPrimaryKeyOrdering() {
103+
return null;
104+
}
105+
96106
/* (non-Javadoc)
97107
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#isClusterKeyColumn()
98108
*/
@@ -125,6 +135,14 @@ public boolean isPrimaryKeyColumn() {
125135
return false;
126136
}
127137

138+
/* (non-Javadoc)
139+
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#isEmbedded()
140+
*/
141+
@Override
142+
public boolean isEmbedded() {
143+
return false;
144+
}
145+
128146
/* (non-Javadoc)
129147
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#setColumnName(org.springframework.data.cassandra.core.cql.CqlIdentifier)
130148
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
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+
* https://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 org.springframework.data.cassandra.core.mapping;
17+
18+
import org.springframework.data.cassandra.core.cql.Ordering;
19+
import org.springframework.data.mapping.model.Property;
20+
import org.springframework.data.mapping.model.SimpleTypeHolder;
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* {@link BasicCassandraPersistentProperty} that pre-computes primary key and embedded flags.
25+
*
26+
* @author Mark Paluch
27+
* @since 3.1.4
28+
*/
29+
public class CachingCassandraPersistentProperty extends BasicCassandraPersistentProperty {
30+
31+
private final @Nullable Ordering primaryKeyOrdering;
32+
private final boolean isCompositePrimaryKey;
33+
private final boolean isClusterKeyColumn;
34+
private final boolean isPartitionKeyColumn;
35+
private final boolean isPrimaryKeyColumn;
36+
private final boolean isEmbedded;
37+
38+
public CachingCassandraPersistentProperty(Property property, CassandraPersistentEntity<?> owner,
39+
SimpleTypeHolder simpleTypeHolder) {
40+
super(property, owner, simpleTypeHolder);
41+
42+
primaryKeyOrdering = super.getPrimaryKeyOrdering();
43+
isCompositePrimaryKey = super.isCompositePrimaryKey();
44+
isClusterKeyColumn = super.isClusterKeyColumn();
45+
isPartitionKeyColumn = super.isPartitionKeyColumn();
46+
isPrimaryKeyColumn = super.isPrimaryKeyColumn();
47+
isEmbedded = super.isEmbedded();
48+
}
49+
50+
/* (non-Javadoc)
51+
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#getPrimaryKeyOrdering()
52+
*/
53+
@Nullable
54+
@Override
55+
public Ordering getPrimaryKeyOrdering() {
56+
return primaryKeyOrdering;
57+
}
58+
59+
/* (non-Javadoc)
60+
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#isCompositePrimaryKey()
61+
*/
62+
@Override
63+
public boolean isCompositePrimaryKey() {
64+
return isCompositePrimaryKey;
65+
}
66+
67+
/* (non-Javadoc)
68+
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#isClusterKeyColumn()
69+
*/
70+
@Override
71+
public boolean isClusterKeyColumn() {
72+
return isClusterKeyColumn;
73+
}
74+
75+
/* (non-Javadoc)
76+
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#isPartitionKeyColumn()
77+
*/
78+
@Override
79+
public boolean isPartitionKeyColumn() {
80+
return isPartitionKeyColumn;
81+
}
82+
83+
/* (non-Javadoc)
84+
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#isPrimaryKeyColumn()
85+
*/
86+
@Override
87+
public boolean isPrimaryKeyColumn() {
88+
return isPrimaryKeyColumn;
89+
}
90+
91+
/* (non-Javadoc)
92+
* @see org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentProperty#isEmbedded()
93+
*/
94+
@Override
95+
public boolean isEmbedded() {
96+
return isEmbedded;
97+
}
98+
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/CassandraMappingContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ protected CassandraPersistentProperty createPersistentProperty(Property property
402402

403403
BasicCassandraPersistentProperty persistentProperty = owner.isTupleType()
404404
? new BasicCassandraPersistentTupleProperty(property, owner, simpleTypeHolder)
405-
: new BasicCassandraPersistentProperty(property, owner, simpleTypeHolder);
405+
: new CachingCassandraPersistentProperty(property, owner, simpleTypeHolder);
406406

407407
persistentProperty.setNamingStrategy(this.namingStrategy);
408408
Optional.ofNullable(this.applicationContext).ifPresent(persistentProperty::setApplicationContext);

0 commit comments

Comments
 (0)