Skip to content

Commit

Permalink
refactor: adjust saveAll Doc failures to work with Java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Oct 23, 2024
1 parent 9a23260 commit e8186db
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.redis.om.spring.serialization.gson.GsonListOfType;
import com.redis.om.spring.util.ObjectUtils;
import com.redis.om.spring.vectorize.Embedder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyAccessor;
Expand Down Expand Up @@ -51,6 +53,7 @@
import org.springframework.util.ReflectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.search.Query;
import redis.clients.jedis.search.SearchResult;
Expand All @@ -64,8 +67,10 @@
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

import static com.redis.om.spring.util.ObjectUtils.*;
Expand All @@ -74,6 +79,8 @@
public class SimpleRedisDocumentRepository<T, ID> extends SimpleKeyValueRepository<T, ID>
implements RedisDocumentRepository<T, ID> {

private final static Logger logger = LoggerFactory.getLogger(SimpleRedisDocumentRepository.class);

protected final RedisModulesOperations<String> modulesOperations;
protected final EntityInformation<T, ID> metadata;
protected final KeyValueOperations operations;
Expand Down Expand Up @@ -172,6 +179,7 @@ public boolean setExpiration(ID id, Long expiration, TimeUnit timeUnit) {
public <S extends T> List<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
List<S> saved = new ArrayList<>();
List<Object> entityIds = new ArrayList<>();

try (Jedis jedis = modulesOperations.client().getJedis().get()) {
Pipeline pipeline = jedis.pipelined();
Expand All @@ -188,6 +196,8 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
.getProperty(Objects.requireNonNull(keyValueEntity.getIdProperty()));
keyValueEntity.getPropertyAccessor(entity).setProperty(keyValueEntity.getIdProperty(), id);

entityIds.add(id);

String keyspace = keyValueEntity.getKeySpace();
byte[] objectKey = createKey(keyspace, Objects.requireNonNull(id).toString());

Expand Down Expand Up @@ -215,7 +225,22 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {

saved.add(entity);
}
pipeline.sync();

List<Object> responses = pipeline.syncAndReturnAll();

// Process responses using streams to avoid iterator issues
if (responses != null && !responses.isEmpty()) {
long failedCount = IntStream.range(0, Math.min(responses.size(), entityIds.size()))
.filter(i -> responses.get(i) instanceof JedisDataException)
.peek(i -> logger.warn("Failed JSON.SET command for entity with id: {} Error: {}",
entityIds.get(i),
((JedisDataException) responses.get(i)).getMessage()))
.count();

if (failedCount > 0) {
logger.warn("Total failed JSON.SET commands: {}", failedCount);
}
}
}

return saved;
Expand Down

0 comments on commit e8186db

Please sign in to comment.