DATAREDIS-425 - Support distinct indexName

This commit adds support for distinguishing between
the property path that is being indexed and the
name of the index that should be used.

This is useful when you do not want the property
path and index name to be the same. For example,
if you want to use Spel expressions, it may not be
possible to use the path as the name of the index.

Original PR #160
This commit is contained in:
Rob Winch
2015-12-14 17:13:53 -06:00
committed by Christoph Strobl
parent bd7728e6ad
commit 40c2582302
8 changed files with 160 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@ import org.springframework.data.redis.core.mapping.RedisMappingContext;
/**
* @author Christoph Strobl
* @auhtor Rob Winch
*/
@RunWith(MockitoJUnitRunner.class)
public class IndexWriterUnitTests {
@@ -145,7 +146,7 @@ public class IndexWriterUnitTests {
static class StubIndxedData implements IndexedData {
@Override
public String getPath() {
public String getIndexName() {
return "address.city";
}

View File

@@ -296,7 +296,7 @@ public class IndexResolverImplUnitTests {
IndexedData index = resolve("list.[0].name", "rand");
assertThat(index.getPath(), is("list.name"));
assertThat(index.getIndexName(), is("list.name"));
}
/**
@@ -311,7 +311,7 @@ public class IndexResolverImplUnitTests {
IndexedData index = resolve("map.[foo].name", "rand");
assertThat(index.getPath(), is("map.foo.name"));
assertThat(index.getIndexName(), is("map.foo.name"));
}
/**
@@ -326,7 +326,7 @@ public class IndexResolverImplUnitTests {
IndexedData index = resolve("map.[0].name", "rand");
assertThat(index.getPath(), is("map.0.name"));
assertThat(index.getIndexName(), is("map.0.name"));
}
/**
@@ -405,6 +405,27 @@ public class IndexResolverImplUnitTests {
assertThat(indexes, hasItem(new SimpleIndexedPropertyValue(KEYSPACE_PERSON, "items.type", "hat")));
}
/**
* @see DATAREDIS-425
*/
@Test
public void resolveIndexAllowCustomIndexName() {
indexConfig.addIndexDefinition(new RedisIndexSetting(KEYSPACE_PERSON, "items.type", "itemsType", IndexType.SIMPLE));
Item hat = new Item();
hat.type = "hat";
TaVeren mat = new TaVeren();
mat.items = new ArrayList<Object>(2);
mat.items.add(hat);
mat.items.add("foxhead medallion");
Set<IndexedData> indexes = indexResolver.resolveIndexesFor(ClassTypeInformation.from(TaVeren.class), mat);
assertThat(indexes.size(), is(1));
assertThat(indexes, hasItem(new SimpleIndexedPropertyValue(KEYSPACE_PERSON, "itemsType", "hat")));
}
private IndexedData resolve(String path, Object value) {
return indexResolver.resolveIndex(KEYSPACE_PERSON, path, propertyMock, value);
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.core.index;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.redis.core.index.IndexConfiguration.RedisIndexSetting;
/**
* @author Rob Winch
*/
public class IndexConfigurationUnitTests {
/**
* @see DATAREDIS-425
*/
@Test
public void redisIndexSettingIndexNameDefaulted() {
String path = "path";
RedisIndexSetting setting = new RedisIndexSetting("keyspace", path);
assertThat(setting.getIndexName(), equalTo(path));
}
/**
* @see DATAREDIS-425
*/
@Test
public void redisIndexSettingIndexNameExplicit() {
String indexName = "indexName";
RedisIndexSetting setting = new RedisIndexSetting("keyspace", "index", indexName, IndexType.SIMPLE);
assertThat(setting.getIndexName(), equalTo(indexName));
}
/**
* @see DATAREDIS-425
*/
@Test
public void redisIndexSettingIndexNameUsedInEquals() {
RedisIndexSetting setting1 = new RedisIndexSetting("keyspace", "path", "indexName1", IndexType.SIMPLE);
RedisIndexSetting setting2 = new RedisIndexSetting(setting1.getKeyspace(), setting1.getPath(),
setting1.getIndexName() + "other", setting1.getType());
assertThat(setting1, not(equalTo(setting2)));
}
/**
* @see DATAREDIS-425
*/
@Test
public void redisIndexSettingIndexNameUsedInHashCode() {
RedisIndexSetting setting1 = new RedisIndexSetting("keyspace", "path", "indexName1", IndexType.SIMPLE);
RedisIndexSetting setting2 = new RedisIndexSetting(setting1.getKeyspace(), setting1.getPath(),
setting1.getIndexName() + "other", setting1.getType());
assertThat(setting1.hashCode(), not(equalTo(setting2.hashCode())));
}
}