Skip to content

Commit

Permalink
fix: honor custom search index names for Hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed May 22, 2024
1 parent 00f893e commit 09fdb2b
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,33 +207,26 @@ public <T> T get(Object id, String keyspace, Class<T> type) {
@Override
public void deleteAllOf(String keyspace) {
Class<?> type = indexer.getEntityClassForKeyspace(keyspace);
Optional<String> maybeSearchIndex = indexer.getIndexName(keyspace);
if (maybeSearchIndex.isPresent()) {
SearchOperations<String> searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get());
searchOps.dropIndexAndDocuments();
indexer.createIndexFor(type);
}
String searchIndex = indexer.getIndexName(keyspace);
SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
searchOps.dropIndexAndDocuments();
indexer.createIndexFor(type);
}

public <T> List<String> getAllIds(String keyspace, Class<T> type) {
Optional<String> maybeSearchIndex = indexer.getIndexName(keyspace);
List<String> keys = List.of();
if (maybeSearchIndex.isPresent()) {
SearchOperations<String> searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get());
Optional<Field> maybeIdField = getIdFieldForEntityClass(type);
String idField = maybeIdField.map(Field::getName).orElse("id");

Query query = new Query("*");
query.returnFields(idField);
SearchResult searchResult = searchOps.search(query);

keys = searchResult.getDocuments().stream()
.map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter))
.map(e -> maybeIdField.map(field -> getIdFieldForEntity(field, e)).orElse(null)).filter(Objects::nonNull)
.map(Object::toString).toList();
}

return keys;
String searchIndex = indexer.getIndexName(keyspace);
SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
Optional<Field> maybeIdField = getIdFieldForEntityClass(type);
String idField = maybeIdField.map(Field::getName).orElse("id");

Query query = new Query("*");
query.returnFields(idField);
SearchResult searchResult = searchOps.search(query);

return searchResult.getDocuments().stream()
.map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter))
.map(e -> maybeIdField.map(field -> getIdFieldForEntity(field, e)).orElse(null)).filter(Objects::nonNull)
.map(Object::toString).toList();
}

/**
Expand All @@ -250,26 +243,20 @@ public <T> List<String> getAllIds(String keyspace, Class<T> type) {
@SuppressWarnings("unchecked")
@Override
public <T> List<T> getAllOf(String keyspace, Class<T> type, long offset, int rows) {
Optional<String> maybeSearchIndex = indexer.getIndexName(keyspace);

List<T> result = List.of();
if (maybeSearchIndex.isPresent()) {
SearchOperations<String> searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get());
Query query = new Query("*");
offset = Math.max(0, offset);
int limit = rows;
if (limit <= 0) {
limit = redisOMProperties.getRepository().getQuery().getLimit();
}
query.limit(Math.toIntExact(offset), limit);
SearchResult searchResult = searchOps.search(query);

result = (List<T>) searchResult.getDocuments().stream() //
.map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter)) //
.toList();
String searchIndex = indexer.getIndexName(keyspace);
SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
Query query = new Query("*");
offset = Math.max(0, offset);
int limit = rows;
if (limit <= 0) {
limit = redisOMProperties.getRepository().getQuery().getLimit();
}
query.limit(Math.toIntExact(offset), limit);
SearchResult searchResult = searchOps.search(query);

return result;
return (List<T>) searchResult.getDocuments().stream() //
.map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter)) //
.toList();
}

/*
Expand Down Expand Up @@ -304,19 +291,15 @@ public <T> T delete(Object id, String keyspace, Class<T> type) {
*/
@Override
public long count(String keyspace) {
long count = 0L;
Optional<String> maybeIndexName = indexer.getIndexName(keyspace);
if (maybeIndexName.isPresent()) {
SearchOperations<String> search = modulesOperations.opsForSearch(maybeIndexName.get());
// FT.SEARCH index * LIMIT 0 0
Query query = new Query("*");
query.limit(0, 0);
String indexName = indexer.getIndexName(keyspace);
SearchOperations<String> search = modulesOperations.opsForSearch(indexName);
// FT.SEARCH index * LIMIT 0 0
Query query = new Query("*");
query.limit(0, 0);

SearchResult result = search.search(query);
SearchResult result = search.search(query);

count = result.getTotalResults();
}
return count;
return result.getTotalResults();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,35 @@ public <T> T get(String key, Class<T> type) {
*/
@Override
public <T> List<T> getAllOf(String keyspace, Class<T> type, long offset, int rows) {
Optional<String> maybeSearchIndex = indexer.getIndexName(keyspace);

List<T> result = List.of();
if (maybeSearchIndex.isPresent()) {
SearchOperations<String> searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get());
Query query = new Query("*");
offset = Math.max(0, offset);
int limit = rows;
if (limit <= 0) {
limit = redisOMProperties.getRepository().getQuery().getLimit();
}
query.limit(Math.toIntExact(offset), limit);
SearchResult searchResult = searchOps.search(query);
Gson gson = gsonBuilder.create();
result = searchResult.getDocuments().stream()
.map(d -> gson.fromJson(SafeEncoder.encode((byte[]) d.get("$")), type)) //
.toList();
String searchIndex = indexer.getIndexName(keyspace);
SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
Query query = new Query("*");
offset = Math.max(0, offset);
int limit = rows;
if (limit <= 0) {
limit = redisOMProperties.getRepository().getQuery().getLimit();
}

return result;
query.limit(Math.toIntExact(offset), limit);
SearchResult searchResult = searchOps.search(query);
Gson gson = gsonBuilder.create();
return searchResult.getDocuments().stream()
.map(d -> gson.fromJson(SafeEncoder.encode((byte[]) d.get("$")), type)) //
.toList();
}

public <T> List<String> getAllKeys(String keyspace, Class<T> type) {
Optional<String> maybeSearchIndex = indexer.getIndexName(keyspace);

List<String> keys = List.of();
if (maybeSearchIndex.isPresent()) {
SearchOperations<String> searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get());
Optional<Field> maybeIdField = ObjectUtils.getIdFieldForEntityClass(type);
String idField = maybeIdField.map(Field::getName).orElse("id");

Query query = new Query("*");
query.returnFields(idField);
SearchResult searchResult = searchOps.search(query);

keys = searchResult.getDocuments().stream().map(Document::getId) //
.toList();
}

return keys;
String searchIndex = indexer.getIndexName(keyspace);
SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
Optional<Field> maybeIdField = ObjectUtils.getIdFieldForEntityClass(type);
String idField = maybeIdField.map(Field::getName).orElse("id");

Query query = new Query("*");
query.returnFields(idField);
SearchResult searchResult = searchOps.search(query);

return searchResult.getDocuments().stream() //
.map(Document::getId) //
.toList();
}

/*
Expand Down Expand Up @@ -219,12 +208,10 @@ public <T> T delete(Object id, String keyspace, Class<T> type) {
@Override
public void deleteAllOf(String keyspace) {
Class<?> type = indexer.getEntityClassForKeyspace(keyspace);
Optional<String> maybeSearchIndex = indexer.getIndexName(keyspace);
if (maybeSearchIndex.isPresent()) {
SearchOperations<String> searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get());
searchOps.dropIndexAndDocuments();
indexer.createIndexFor(type);
}
String searchIndex = indexer.getIndexName(keyspace);
SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
searchOps.dropIndexAndDocuments();
indexer.createIndexFor(type);
}

/*
Expand All @@ -236,18 +223,15 @@ public void deleteAllOf(String keyspace) {
@Override
public long count(String keyspace) {
long count = 0L;
Optional<String> maybeIndexName = indexer.getIndexName(keyspace);
if (maybeIndexName.isPresent()) {
SearchOperations<String> search = modulesOperations.opsForSearch(maybeIndexName.get());
// FT.SEARCH index * LIMIT 0 0
Query query = new Query("*");
query.limit(0, 0);
String indexName = indexer.getIndexName(keyspace);
SearchOperations<String> search = modulesOperations.opsForSearch(indexName);
// FT.SEARCH index * LIMIT 0 0
Query query = new Query("*");
query.limit(0, 0);

SearchResult result = search.search(query);
SearchResult result = search.search(query);

count = result.getTotalResults();
}
return count;
return result.getTotalResults();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ public void dropIndexFor(Class<?> cl) {
dropIndex(cl, false, false);
}

public Optional<String> getIndexName(String keyspace) {
public String getIndexName(String keyspace) {
return getIndexName(keyspaceToEntityClass.get(getKeyspace(keyspace)));
}

public Optional<String> getIndexName(Class<?> entityClass) {
public String getIndexName(Class<?> entityClass) {
if (entityClass != null && entityClassToIndexName.containsKey(entityClass)) {
return Optional.of(entityClassToIndexName.get(entityClass));
return entityClassToIndexName.get(entityClass);
} else {
return Optional.empty();
return entityClass.getName() + "Idx";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class RediSearchQuery implements RepositoryQuery {
private static final Log logger = LogFactory.getLog(RediSearchQuery.class);

private final QueryMethod queryMethod;
private final String searchIndex;
private final RedisOMProperties redisOMProperties;
private final boolean hasLanguageParameter;
// aggregation fields
Expand Down Expand Up @@ -104,8 +103,6 @@ public RediSearchQuery(//
this.indexer = indexer;
this.queryMethod = queryMethod;
this.domainType = this.queryMethod.getEntityInformation().getJavaType();
Optional<String> maybeIndex = indexer.getIndexName(this.domainType);
this.searchIndex = maybeIndex.orElse(this.domainType.getName() + "Idx");
this.gsonBuilder = gsonBuilder;
this.redisOMProperties = redisOMProperties;

Expand Down Expand Up @@ -402,7 +399,8 @@ private Object executeQuery(Object[] parameters) {
ParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor);

SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);
boolean excludeNullParams = !isNullParamQuery;
String preparedQuery = prepareQuery(parameters, excludeNullParams);
Query query = new Query(preparedQuery);
Expand Down Expand Up @@ -518,7 +516,8 @@ private Object parseDocumentResult(redis.clients.jedis.search.Document doc) {
}

private Object executeDeleteQuery(Object[] parameters) {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);
String baseQuery = prepareQuery(parameters, true);
AggregationBuilder aggregation = new AggregationBuilder(baseQuery);

Expand Down Expand Up @@ -571,7 +570,8 @@ private Object executeDeleteQuery(Object[] parameters) {
}

private Object executeAggregation(Object[] parameters) {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);

// build the aggregation
AggregationBuilder aggregation = new AggregationBuilder(value);
Expand Down Expand Up @@ -683,7 +683,8 @@ private Object executeAggregation(Object[] parameters) {
}

private Object executeFtTagVals() {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);

return ops.tagVals(this.value);
}
Expand Down Expand Up @@ -766,7 +767,8 @@ private Gson getGson() {
}

private Object executeNullQuery(Object[] parameters) {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);
String baseQuery = prepareQuery(parameters, true);

AggregationBuilder aggregation = new AggregationBuilder(baseQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class RedisEnhancedQuery implements RepositoryQuery {
private static final Log logger = LogFactory.getLog(RedisEnhancedQuery.class);

private final QueryMethod queryMethod;
private final String searchIndex;
private final RedisOMProperties redisOMProperties;
private final boolean hasLanguageParameter;
// aggregation fields
Expand Down Expand Up @@ -106,8 +105,6 @@ public RedisEnhancedQuery(QueryMethod queryMethod, //
this.modulesOperations = (RedisModulesOperations<String>) rmo;
this.queryMethod = queryMethod;
this.domainType = this.queryMethod.getEntityInformation().getJavaType();
Optional<String> maybeIndex = indexer.getIndexName(this.domainType);
this.searchIndex = maybeIndex.orElse(this.domainType.getName() + "Idx");
this.redisOMProperties = redisOMProperties;
this.redisOperations = redisOperations;
this.mappingConverter = new MappingRedisOMConverter(null, new ReferenceResolverImpl(redisOperations));
Expand Down Expand Up @@ -402,7 +399,8 @@ private Object executeQuery(Object[] parameters) {
ParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor);

SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);
boolean excludeNullParams = !isNullParamQuery;
String preparedQuery = prepareQuery(parameters, excludeNullParams);
Query query = new Query(preparedQuery);
Expand Down Expand Up @@ -511,7 +509,8 @@ private Object executeQuery(Object[] parameters) {
}

private Object executeDeleteQuery(Object[] parameters) {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);
String baseQuery = prepareQuery(parameters, true);
AggregationBuilder aggregation = new AggregationBuilder(baseQuery);

Expand Down Expand Up @@ -580,7 +579,8 @@ private Object executeDeleteQuery(Object[] parameters) {
}

private Object executeAggregation(Object[] parameters) {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);

// build the aggregation
AggregationBuilder aggregation = new AggregationBuilder(value);
Expand Down Expand Up @@ -692,7 +692,8 @@ private Object executeAggregation(Object[] parameters) {
}

private Object executeFtTagVals() {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);

return ops.tagVals(this.value);
}
Expand Down Expand Up @@ -769,7 +770,8 @@ private String prepareQuery(final Object[] parameters, boolean excludeNullParams
}

private Object executeNullQuery(Object[] parameters) {
SearchOperations<String> ops = modulesOperations.opsForSearch(searchIndex);
String indexName = indexer.getIndexName(this.domainType);
SearchOperations<String> ops = modulesOperations.opsForSearch(indexName);
String baseQuery = prepareQuery(parameters, true);

AggregationBuilder aggregation = new AggregationBuilder(baseQuery);
Expand Down
Loading

0 comments on commit 09fdb2b

Please sign in to comment.