Skip to content

DATAREDIS-530 - Fix PartialUpdate removing existing indexes. #207

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>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAREDIS-530-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/springframework/data/redis/core/IndexWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public void createIndexes(Object key, Iterable<IndexedData> indexValues) {
* @param indexValues can be {@literal null}.
*/
public void updateIndexes(Object key, Iterable<IndexedData> indexValues) {
createOrUpdateIndexes(key, indexValues, IndexWriteMode.PARTIAL_UPDATE);
}

/**
* Updates indexes by first removing key from existing one and then persisting new index data.
*
* @param key must not be {@literal null}.
* @param indexValues can be {@literal null}.
*/
public void deleteAndUpdateIndexes(Object key, Iterable<IndexedData> indexValues) {
createOrUpdateIndexes(key, indexValues, IndexWriteMode.UPDATE);
}

Expand All @@ -96,7 +106,7 @@ private void createOrUpdateIndexes(Object key, Iterable<IndexedData> indexValues
removeKeyFromIndexes(data.getKeyspace(), binKey);
}
}

} else if (ObjectUtils.nullSafeEquals(IndexWriteMode.PARTIAL_UPDATE, writeMode)) {
removeKeyFromExistingIndexes(binKey, indexValues);
}

Expand Down Expand Up @@ -229,6 +239,6 @@ private byte[] toBytes(Object source) {
*/
private static enum IndexWriteMode {

CREATE, UPDATE
CREATE, UPDATE, PARTIAL_UPDATE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
if (isNew) {
indexWriter.createIndexes(key, rdo.getIndexedData());
} else {
indexWriter.updateIndexes(key, rdo.getIndexedData());
indexWriter.deleteAndUpdateIndexes(key, rdo.getIndexedData());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.data.keyvalue.core.KeyValueAdapter;
import org.springframework.data.keyvalue.core.KeyValueCallback;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -135,6 +134,7 @@ public void update(Object objectToUpdate) {

if (objectToUpdate instanceof PartialUpdate) {
doPartialUpdate((PartialUpdate<?>) objectToUpdate);
return;
}

super.update(objectToUpdate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.util.ReflectionTestUtils.*;

Expand Down Expand Up @@ -98,20 +97,22 @@ public void destroyShouldNotDestroyConnectionFactory() throws Exception {

/**
* @see DATAREDIS-512
* @see DATAREDIS-530
*/
@Test
public void putShouldRemoveExistingIndexValuesWhenUpdating() {

RedisData rd = new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("_id", "1")));
rd.addIndexedData(new SimpleIndexedPropertyValue("persons", "firstname", "rand"));

when(redisConnectionMock.keys(any(byte[].class)))
when(redisConnectionMock.sMembers(org.mockito.Matchers.any(byte[].class)))
.thenReturn(new LinkedHashSet<byte[]>(Arrays.asList("persons:firstname:rand".getBytes())));
when(redisConnectionMock.del((byte[][]) anyVararg())).thenReturn(1L);

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

verify(redisConnectionMock, times(1)).sRem(any(byte[].class), any(byte[].class));
verify(redisConnectionMock, times(1)).sRem(org.mockito.Matchers.any(byte[].class),
org.mockito.Matchers.any(byte[].class));
}

/**
Expand All @@ -123,20 +124,20 @@ public void putShouldNotTryToRemoveExistingIndexValuesWhenInsertingNew() {
RedisData rd = new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("_id", "1")));
rd.addIndexedData(new SimpleIndexedPropertyValue("persons", "firstname", "rand"));

when(redisConnectionMock.sMembers(any(byte[].class)))
when(redisConnectionMock.sMembers(org.mockito.Matchers.any(byte[].class)))
.thenReturn(new LinkedHashSet<byte[]>(Arrays.asList("persons:firstname:rand".getBytes())));
when(redisConnectionMock.del((byte[][]) anyVararg())).thenReturn(0L);

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

verify(redisConnectionMock, never()).sRem(any(byte[].class), (byte[][]) anyVararg());
verify(redisConnectionMock, never()).sRem(org.mockito.Matchers.any(byte[].class), (byte[][]) anyVararg());
}

/**
* @see DATAREDIS-491
*/
@Test
public void shouldInitKeyExpirationListenerOnStartup() throws Exception{
public void shouldInitKeyExpirationListenerOnStartup() throws Exception {

adapter.destroy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,81 @@ public Void doInRedis(RedisConnection connection) throws DataAccessException {
});
}

/**
* @see DATAREDIS-530
*/
@Test
public void partialUpdateShouldLeaveIndexesNotInvolvedInUpdateUntouched() {

final Person rand = new Person();
rand.firstname = "rand";
rand.lastname = "al-thor";
rand.email = "rand@twof.com";

template.insert(rand);

/*
* Set the lastname and make sure we've an index on it afterwards
*/
PartialUpdate<Person> update = PartialUpdate.newPartialUpdate(rand.id, Person.class).set("lastname", "doe");

template.doPartialUpdate(update);

nativeTemplate.execute(new RedisCallback<Void>() {

@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {

assertThat(connection.hGet(("template-test-person:" + rand.id).getBytes(), "lastname".getBytes()),
is(equalTo("doe".getBytes())));
assertThat(connection.exists("template-test-person:email:rand@twof.com".getBytes()), is(true));
assertThat(connection.exists("template-test-person:lastname:al-thor".getBytes()), is(false));
assertThat(connection.sIsMember("template-test-person:lastname:doe".getBytes(), rand.id.getBytes()), is(true));
return null;
}
});
}

/**
* @see DATAREDIS-530
*/
@Test
public void updateShouldAlterIndexesCorrectlyWhenValuesGetRemovedFromHash() {

final Person rand = new Person();
rand.firstname = "rand";
rand.lastname = "al-thor";
rand.email = "rand@twof.com";

template.insert(rand);

nativeTemplate.execute(new RedisCallback<Void>() {

@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {

assertThat(connection.exists("template-test-person:email:rand@twof.com".getBytes()), is(true));
assertThat(connection.exists("template-test-person:lastname:al-thor".getBytes()), is(true));
return null;
}
});

rand.lastname = null;

template.update(rand);

nativeTemplate.execute(new RedisCallback<Void>() {

@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {

assertThat(connection.exists("template-test-person:email:rand@twof.com".getBytes()), is(true));
assertThat(connection.exists("template-test-person:lastname:al-thor".getBytes()), is(false));
return null;
}
});
}

@EqualsAndHashCode
@RedisHash("template-test-type-mapping")
static class VariousTypes {
Expand Down Expand Up @@ -828,6 +903,7 @@ static class Person {
@Id String id;
String firstname;
@Indexed String lastname;
@Indexed String email;
Integer age;
List<String> nicknames;

Expand Down