Skip to content

Clear indexed data if property value is set to null #2895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.x-2882-SNAPSHOT</version>

<name>Spring Data Redis</name>
<description>Spring Data module for Redis</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void doWithPersistentProperty(RedisPersistentProperty persistentProperty)
Object propertyValue = accessor.getProperty(persistentProperty);

if (propertyValue == null) {
indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, null));
return;
}

Expand Down Expand Up @@ -212,21 +213,32 @@ protected Set<IndexedData> resolveIndex(String keyspace, String propertyPath,

IndexedData indexedData = null;
if (transformedValue == null) {
indexedData = new RemoveIndexedData(indexedData);

indexedData = new RemoveIndexedData(new IndexedData() {
@Override
public String getIndexName() {
return indexDefinition.getIndexName();
}

@Override
public String getKeyspace() {
return indexDefinition.getKeyspace();
}
});
} else {
indexedData = indexedDataFactoryProvider.getIndexedDataFactory(indexDefinition).createIndexedDataFor(value);
}
data.add(indexedData);
}
}

else if (property != null && property.isAnnotationPresent(Indexed.class)) {
else if (property != null && value != null && property.isAnnotationPresent(Indexed.class)) {

SimpleIndexDefinition indexDefinition = new SimpleIndexDefinition(keyspace, path);
indexConfiguration.addIndexDefinition(indexDefinition);

data.add(indexedDataFactoryProvider.getIndexedDataFactory(indexDefinition).createIndexedDataFor(value));
} else if (property != null && property.isAnnotationPresent(GeoIndexed.class)) {
} else if (property != null && value != null && property.isAnnotationPresent(GeoIndexed.class)) {

GeoIndexDefinition indexDefinition = new GeoIndexDefinition(keyspace, path);
indexConfiguration.addIndexDefinition(indexDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ void putWritesSimpleIndexDataCorrectly() {
assertThat(template.opsForSet().members("persons:firstname:rand")).contains("1");
}

@Test // GH-2882
void indexDataShouldBeCleardIfPropertyValueIsSetToNull() {

Person rand = new Person();
rand.firstname = "rand";

adapter.put("1", rand, "persons");

assertThat(template.keys("persons*")).contains("persons:firstname:rand");
assertThat(template.opsForSet().members("persons:firstname:rand")).contains("1");

rand.id = "1";
rand.firstname = null;
adapter.put("1", rand, "persons");

assertThat(template.keys("persons*")).doesNotContain("persons:firstname:rand");
assertThat(template.opsForSet().members("persons:firstname:rand")).doesNotContain("1");
}

@Test // DATAREDIS-744
void putWritesSimpleIndexDataWithColonCorrectly() {

Expand Down
Loading