Skip to content
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

Bsb/qbe updates and bulk updates #477

Merged
merged 2 commits into from
Jun 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.redis.om.spring.util.ObjectUtils.isInnerClassWithEnclosing;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;

@SupportedAnnotationTypes(
Expand Down Expand Up @@ -110,24 +111,36 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}

void generateMetaModelClass(final Element annotatedElement) throws IOException {
String qualifiedGenEntityName = annotatedElement.asType().toString() + "$";
final String entityName = ObjectUtils.shortName(annotatedElement.asType().toString());
final String genEntityName = entityName + "$";
TypeName entity = TypeName.get(annotatedElement.asType());
Pair<Boolean, String> innerClassInfo = isInnerClassWithEnclosing(annotatedElement);
boolean isInnerClass = innerClassInfo.getFirst();
String enclosingClassName = innerClassInfo.getSecond();

messager.printMessage(Diagnostic.Kind.NOTE, "Generating Entity Metamodel: " + qualifiedGenEntityName);
String qualifiedName = annotatedElement.asType().toString();

Map<? extends Element, String> enclosedFields = getInstanceFields(annotatedElement);
final String entityName;
final String packageName;
final String genEntityName;

final PackageElement packageElement = processingEnvironment.getElementUtils().getPackageOf(annotatedElement);
String packageName;
PackageElement packageElement = processingEnvironment.getElementUtils().getPackageOf(annotatedElement);
if (packageElement.isUnnamed()) {
messager.printMessage(Diagnostic.Kind.WARNING, "Class " + entityName + " has an unnamed package.");
messager.printMessage(Diagnostic.Kind.WARNING,
"Class " + annotatedElement.getSimpleName() + " has an unnamed package.");
packageName = "";
entityName = qualifiedName; // Use the full name as the entity name for packageless classes
} else {
packageName = packageElement.getQualifiedName().toString();
entityName = isInnerClass ? annotatedElement.getSimpleName().toString() : ObjectUtils.shortName(qualifiedName);
}

genEntityName = entityName + "$";

String qualifiedGenEntityName = (packageName.isEmpty() ? "" : packageName + ".") + genEntityName;
TypeName entity = TypeName.get(annotatedElement.asType());

messager.printMessage(Diagnostic.Kind.NOTE, "Generating Entity Metamodel: " + qualifiedGenEntityName);

Map<? extends Element, String> enclosedFields = getInstanceFields(annotatedElement);

List<FieldSpec> interceptors = new ArrayList<>();
List<ObjectGraphFieldSpec> fields = new ArrayList<>();
List<CodeBlock> initCodeBlocks = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public class MetamodelUtils {
public static List<MetamodelField<?, ?>> getMetamodelFieldsForProperties(Class<?> entityClass,
Collection<String> properties) {
List<MetamodelField<?, ?>> result = new ArrayList<>();

String metamodelClassName = getMetamodelClassName(entityClass);

try {
Class<?> metamodel = Class.forName(entityClass.getName() + "$");
Class<?> metamodel = Class.forName(metamodelClassName);
for (var property : properties) {
try {
result.add((MetamodelField<?, ?>) metamodel.getField(ObjectUtils.staticField(property)).get(null));
Expand All @@ -41,4 +44,17 @@ public class MetamodelUtils {
}
return result;
}

private static String getMetamodelClassName(Class<?> entityClass) {
if (entityClass.isMemberClass()) {
// For both static and non-static nested classes
Class<?> enclosingClass = entityClass.getEnclosingClass();
String enclosingClassName = enclosingClass.getSimpleName();
String entityClassName = entityClass.getSimpleName();
return entityClass.getPackage().getName() + "." + enclosingClassName + "_" + entityClassName + "$";
} else {
// For top-level classes
return entityClass.getName() + "$";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redis.om.spring.repository;

import com.redis.om.spring.metamodel.MetamodelField;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
Expand Down Expand Up @@ -40,4 +41,10 @@ public interface RedisDocumentRepository<T, ID> extends KeyValueRepository<T, ID
<S extends T> S update(S entity);

String getKeyspace();

// QBE Extensions

<S extends T> S update(Example<S> example);

<S extends T> void updateAll(Iterable<Example<S>> examples);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redis.om.spring.repository;

import com.redis.om.spring.metamodel.MetamodelField;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
Expand Down Expand Up @@ -32,4 +33,10 @@ public interface RedisEnhancedRepository<T, ID> extends KeyValueRepository<T, ID
boolean setExpiration(ID id, Long expiration, TimeUnit timeUnit);

String getKeyspace();

// QBE Extensions

<S extends T> S update(Example<S> example);

<S extends T> void updateAll(Iterable<Example<S>> examples);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.redis.om.spring.id.ULIDIdentifierGenerator;
import com.redis.om.spring.indexing.RediSearchIndexer;
import com.redis.om.spring.metamodel.MetamodelField;
import com.redis.om.spring.metamodel.MetamodelUtils;
import com.redis.om.spring.ops.RedisModulesOperations;
import com.redis.om.spring.ops.json.JSONOperations;
import com.redis.om.spring.ops.search.SearchOperations;
Expand Down Expand Up @@ -488,6 +489,102 @@ public <S extends T, R> R findBy(Example<S> example, Function<FetchableFluentQue
mappingConverter.getMappingContext()));
}

@Override
public <S extends T> S update(Example<S> example) {
S probe = example.getProbe();
ExampleMatcher matcher = example.getMatcher();
ID id = metadata.getId(probe);

if (id == null) {
throw new IllegalArgumentException("Example object must have an ID");
}

String key = getKey(id);

Class<?> entityType = metadata.getJavaType();
List<UpdateOperation> updateOperations = new ArrayList<>();

List<MetamodelField<?, ?>> metamodelFields = MetamodelUtils.getMetamodelFieldsForProperties(entityType,
getAllProperties(entityType));

for (MetamodelField<?, ?> metamodelField : metamodelFields) {
String propertyName = metamodelField.getSearchAlias();

if (shouldIncludeProperty(matcher, propertyName)) {
Object value = getPropertyValue(probe, propertyName);

if (value != null) {
updateOperations.add(new UpdateOperation(key, metamodelField, value));
}
}
}

executePipelinedUpdates(updateOperations);

// Use JSON GET operation to fetch the updated entity
return (S) getJSONOperations().get(key, entityType);
}

@Override
public <S extends T> void updateAll(Iterable<Example<S>> examples) {
if (!examples.iterator().hasNext()) {
return; // No examples to process
}

List<UpdateOperation> updateOperations = new ArrayList<>();
Class<?> entityType = metadata.getJavaType();
List<MetamodelField<?, ?>> metamodelFields = MetamodelUtils.getMetamodelFieldsForProperties(entityType,
getAllProperties(entityType));

for (Example<S> example : examples) {
S probe = example.getProbe();
ExampleMatcher matcher = example.getMatcher();
ID id = metadata.getId(probe);

if (id == null) {
throw new IllegalArgumentException("Example object must have an ID");
}

String key = getKey(id);

for (MetamodelField<?, ?> metamodelField : metamodelFields) {
String propertyName = metamodelField.getSearchAlias();

if (shouldIncludeProperty(matcher, propertyName)) {
Object value = getPropertyValue(probe, propertyName);

if (value != null) {
updateOperations.add(new UpdateOperation(key, metamodelField, value));
}
}
}
}

executePipelinedUpdates(updateOperations);
}

private JSONOperations<String> getJSONOperations() {
return modulesOperations.opsForJSON();
}

private void executePipelinedUpdates(List<UpdateOperation> updateOperations) {
try (Jedis jedis = modulesOperations.client().getJedis().get()) {
Pipeline pipeline = jedis.pipelined();

for (UpdateOperation op : updateOperations) {
List<byte[]> args = new ArrayList<>(4);
args.add(SafeEncoder.encode(op.key));
args.add(SafeEncoder.encode(Path2.of(op.field.getJSONPath()).toString()));
args.add(SafeEncoder.encode(new Gson().toJson(op.value)));
args.add(SafeEncoder.encode("XX"));

pipeline.sendCommand(JsonCommand.SET, args.toArray(new byte[0][]));
}

pipeline.sync();
}
}

private SearchOperations<String> getSearchOps() {
String keyspace = indexer.getKeyspaceForEntityClass(metadata.getJavaType());
String searchIndex = indexer.getIndexName(keyspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.redis.om.spring.id.ULIDIdentifierGenerator;
import com.redis.om.spring.indexing.RediSearchIndexer;
import com.redis.om.spring.metamodel.MetamodelField;
import com.redis.om.spring.metamodel.MetamodelUtils;
import com.redis.om.spring.ops.RedisModulesOperations;
import com.redis.om.spring.ops.search.SearchOperations;
import com.redis.om.spring.repository.RedisEnhancedRepository;
Expand Down Expand Up @@ -39,18 +40,17 @@
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.search.Query;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.util.SafeEncoder;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static com.redis.om.spring.RedisOMProperties.MAX_SEARCH_RESULTS;
import static com.redis.om.spring.util.ObjectUtils.pageFromSlice;
import static com.redis.om.spring.util.ObjectUtils.*;

public class SimpleRedisEnhancedRepository<T, ID> extends SimpleKeyValueRepository<T, ID>
implements RedisEnhancedRepository<T, ID> {
Expand Down Expand Up @@ -230,6 +230,85 @@ public String getKeyspace() {
return indexer.getKeyspaceForEntityClass(metadata.getJavaType());
}

@Override
public <S extends T> S update(Example<S> example) {
S probe = example.getProbe();
ExampleMatcher matcher = example.getMatcher();
ID id = metadata.getId(probe);

if (id == null) {
throw new IllegalArgumentException("Example object must have an ID");
}

String key = getKey(id);

Class<?> entityType = metadata.getJavaType();
List<UpdateOperation> updateOperations = new ArrayList<>();

List<MetamodelField<?, ?>> metamodelFields = MetamodelUtils.getMetamodelFieldsForProperties(entityType,
getAllProperties(entityType));

for (MetamodelField<?, ?> metamodelField : metamodelFields) {
String propertyName = metamodelField.getSearchAlias();

if (propertyName.equals("id")) {
continue;
}

if (shouldIncludeProperty(matcher, propertyName)) {
Object value = getPropertyValue(probe, propertyName);

if (value != null) {
updateOperations.add(new UpdateOperation(key, metamodelField, value));
}
}
}

if (!updateOperations.isEmpty()) {
executePipelinedUpdates(updateOperations);
}

return (S) findById(id).orElseThrow(() -> new RuntimeException("Failed to fetch updated entity"));
}

@Override
public <S extends T> void updateAll(Iterable<Example<S>> examples) {
if (!examples.iterator().hasNext()) {
return; // No examples to process
}

List<UpdateOperation> updateOperations = new ArrayList<>();
Class<?> entityType = metadata.getJavaType();
List<MetamodelField<?, ?>> metamodelFields = MetamodelUtils.getMetamodelFieldsForProperties(entityType,
getAllProperties(entityType));

for (Example<S> example : examples) {
S probe = example.getProbe();
ExampleMatcher matcher = example.getMatcher();
ID id = metadata.getId(probe);

if (id == null) {
throw new IllegalArgumentException("Example object must have an ID");
}

String key = getKey(id);

for (MetamodelField<?, ?> metamodelField : metamodelFields) {
String propertyName = metamodelField.getSearchAlias();

if (shouldIncludeProperty(matcher, propertyName)) {
Object value = getPropertyValue(probe, propertyName);

if (value != null) {
updateOperations.add(new UpdateOperation(key, metamodelField, value));
}
}
}
}

executePipelinedUpdates(updateOperations);
}

private String getKey(Object id) {
var maybeIdentifierFilter = indexer.getIdentifierFilterFor(metadata.getJavaType());
if (maybeIdentifierFilter.isPresent()) {
Expand Down Expand Up @@ -356,6 +435,43 @@ public <S extends T, R> R findBy(Example<S> example, Function<FetchableFluentQue
mappingConverter.getMappingContext()));
}

private void executePipelinedUpdates(List<UpdateOperation> updateOperations) {
try (Jedis jedis = modulesOperations.client().getJedis().get()) {
Pipeline pipeline = jedis.pipelined();

Map<String, Map<byte[], byte[]>> updates = new HashMap<>();

for (UpdateOperation op : updateOperations) {
byte[] value = convertToBinary(op.field, op.value);
if (value != null && value.length > 0) {
updates.computeIfAbsent(op.key, k -> new HashMap<>())
.put(SafeEncoder.encode(op.field.getSearchAlias()), value);
}
}

for (Map.Entry<String, Map<byte[], byte[]>> entry : updates.entrySet()) {
if (!entry.getValue().isEmpty()) {
pipeline.hmset(SafeEncoder.encode(entry.getKey()), entry.getValue());
}
}

pipeline.sync();
}
}

private byte[] convertToBinary(MetamodelField<?, ?> field, Object value) {
if (value == null) {
return null;
}
if (value instanceof String) {
return SafeEncoder.encode((String) value);
}
RedisData redisData = new RedisData();
mappingConverter.write(value, redisData);
byte[] binaryValue = redisData.getBucket().get(field.getSearchAlias());
return binaryValue != null && binaryValue.length > 0 ? binaryValue : null;
}

private SearchOperations<String> getSearchOps() {
String keyspace = indexer.getKeyspaceForEntityClass(metadata.getJavaType());
String searchIndex = indexer.getIndexName(keyspace);
Expand Down
Loading
Loading