Skip to content

Change index creation to declarative. #2795

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
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
47 changes: 39 additions & 8 deletions src/main/java/org/springframework/data/redis/core/IndexWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*
* @author Christoph Strobl
* @author Rob Winch
* @author Junghoon Ban
* @since 1.7
*/
class IndexWriter {
Expand Down Expand Up @@ -127,7 +128,7 @@ public void removeKeyFromIndexes(String keyspace, Object key) {
Assert.notNull(key, "Key must not be null");

byte[] binKey = toBytes(key);
byte[] indexHelperKey = ByteUtils.concatAll(toBytes(keyspace + ":"), binKey, toBytes(":idx"));
byte[] indexHelperKey = createIndexHelperKey(keyspace, binKey);

for (byte[] indexKey : connection.sMembers(indexHelperKey)) {

Expand Down Expand Up @@ -171,8 +172,7 @@ protected void removeKeyFromExistingIndexes(byte[] key, IndexedData indexedData)

Assert.notNull(indexedData, "IndexedData must not be null");

Set<byte[]> existingKeys = connection
.keys(toBytes(indexedData.getKeyspace() + ":" + indexedData.getIndexName() + ":*"));
Set<byte[]> existingKeys = connection.keys(createIndexKey(indexedData, "*"));

if (!CollectionUtils.isEmpty(existingKeys)) {
for (byte[] existingKey : existingKeys) {
Expand Down Expand Up @@ -216,24 +216,23 @@ protected void addKeyToIndex(byte[] key, IndexedData indexedData) {
return;
}

byte[] indexKey = toBytes(indexedData.getKeyspace() + ":" + indexedData.getIndexName() + ":");
indexKey = ByteUtils.concat(indexKey, toBytes(value));
byte[] indexKey = createIndexKey(indexedData, value);
connection.sAdd(indexKey, key);

// keep track of indexes used for the object
connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey);
connection.sAdd(createIndexHelperKey(indexedData.getKeyspace(), key), indexKey);
} else if (indexedData instanceof GeoIndexedPropertyValue propertyValue) {

Object value = propertyValue.getValue();
if (value == null) {
return;
}

byte[] indexKey = toBytes(indexedData.getKeyspace() + ":" + indexedData.getIndexName());
byte[] indexKey = createIndexKey(indexedData);
connection.geoAdd(indexKey, propertyValue.getPoint(), key);

// keep track of indexes used for the object
connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey);
connection.sAdd(createIndexHelperKey(indexedData.getKeyspace(), key), indexKey);
} else {
throw new IllegalArgumentException(
String.format("Cannot write index data for unknown index type %s", indexedData.getClass()));
Expand All @@ -260,6 +259,38 @@ private byte[] toBytes(@Nullable Object source) {
source.getClass()));
}

/**
* Creates the index key for the given {@link IndexedData}.
*
* @param indexedData must not be {@literal null}.
* @return index key.
*/
private byte[] createIndexKey(IndexedData indexedData) {
return toBytes(indexedData.getKeyspace() + ":" + indexedData.getIndexName());
}

/**
* Creates the index key for the given {@link IndexedData} and {@code value}.
*
* @param indexedData must not be {@literal null}.
* @param value must not be {@literal null}.
* @return index key.
*/
private byte[] createIndexKey(IndexedData indexedData, Object value) {
return ByteUtils.concatAll(createIndexKey(indexedData), toBytes(":"), toBytes(value));
}

/**
* Creates the index helper key for the given {@code keyspace} and {@code key}.
*
* @param keyspace must not be {@literal null}.
* @param key must not be {@literal null}.
* @return index helper key.
*/
private byte[] createIndexHelperKey(String keyspace, byte[] key) {
return ByteUtils.concatAll(toBytes(keyspace + ":"), key, toBytes(":idx"));
}

/**
* @author Christoph Strobl
* @since 1.8
Expand Down