Store updated flag in the entity, not in the delegate

Closes #9774
This commit is contained in:
Michal Hajas 2022-01-24 10:26:11 +01:00 committed by Hynek Mlnařík
parent 4136bf7700
commit de161d02b9
7 changed files with 72 additions and 24 deletions

View file

@ -26,5 +26,5 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE)
public @interface GenerateHotRodEntityImplementation {
String implementInterface();
String inherits() default "org.keycloak.models.map.common.UpdatableEntity.Impl";
String inherits() default "org.keycloak.models.map.storage.hotRod.common.UpdatableHotRodEntityDelegateImpl";
}

View file

@ -77,6 +77,8 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
if (interfaceClass == null || interfaceClass.isEmpty()) return;
TypeElement parentClassElement = elements.getTypeElement(hotRodAnnotation.inherits());
if (parentClassElement == null) return;
boolean parentClassHasGeneric = !getGenericsDeclaration(parentClassElement.asType()).isEmpty();
TypeElement parentInterfaceElement = elements.getTypeElement(interfaceClass);
if (parentInterfaceElement == null) return;
@ -119,9 +121,11 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
pw.println("import java.util.stream.Collectors;");
pw.println();
pw.println("// DO NOT CHANGE THIS CLASS, IT IS GENERATED AUTOMATICALLY BY " + GenerateHotRodEntityImplementationsProcessor.class.getSimpleName());
pw.println("public class " + hotRodSimpleClassName + " extends " + parentClassElement.getQualifiedName().toString() + " implements "
pw.println("public class " + hotRodSimpleClassName
+ " extends "
+ parentClassElement.getQualifiedName().toString() + (parentClassHasGeneric ? "<" + e.getQualifiedName().toString() + ">" : "")
+ " implements "
+ parentInterfaceElement.getQualifiedName().toString()
+ ", " + generalHotRodDelegate.getQualifiedName().toString() + "<" + e.getQualifiedName().toString() + ">"
+ " {");
pw.println();
pw.println(" private final " + className + " " + ENTITY_VARIABLE + ";");
@ -325,7 +329,7 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
pw.println(" p0 = " + deepClone(fieldType, "p0") + ";");
}
pw.println(" " + hotRodFieldType.toString() + " migrated = " + migrateToType(hotRodFieldType, firstParameterType, "p0") + ";");
pw.println(" updated |= ! Objects.equals(" + hotRodEntityField(fieldName) + ", migrated);");
pw.println(" " + hotRodEntityField("updated") + " |= ! Objects.equals(" + hotRodEntityField(fieldName) + ", migrated);");
pw.println(" " + hotRodEntityField(fieldName) + " = migrated;");
pw.println(" }");
return true;
@ -338,10 +342,10 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
}
pw.println(" " + collectionItemType.toString() + " migrated = " + migrateToType(collectionItemType, firstParameterType, "p0") + ";");
if (isSetType(typeElement)) {
pw.println(" updated |= " + hotRodEntityField(fieldName) + ".add(migrated);");
pw.println(" " + hotRodEntityField("updated") + " |= " + hotRodEntityField(fieldName) + ".add(migrated);");
} else {
pw.println(" " + hotRodEntityField(fieldName) + ".add(migrated);");
pw.println(" updated = true;");
pw.println(" " + hotRodEntityField("updated") + " = true;");
}
pw.println(" }");
return true;
@ -350,7 +354,7 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
pw.println(" @SuppressWarnings(\"unchecked\") @Override public " + method.getReturnType() + " " + method.getSimpleName() + "(" + firstParameterType + " p0) {");
if (isMapType(typeElement)) {
// Maps are stored as sets
pw.println(" this.updated |= " + hotRodUtils.getQualifiedName().toString() + ".removeFromSetByMapKey("
pw.println(" " + hotRodEntityField("updated") + " |= " + hotRodUtils.getQualifiedName().toString() + ".removeFromSetByMapKey("
+ hotRodEntityField(fieldName) + ", "
+ "p0, "
+ keyGetterReference(collectionItemType) + ");"
@ -358,7 +362,7 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
} else {
pw.println(" if (" + hotRodEntityField(fieldName) + " == null) { return; }");
pw.println(" boolean removed = " + hotRodEntityField(fieldName) + ".remove(p0);");
pw.println(" updated |= removed;");
pw.println(" " + hotRodEntityField("updated") + " |= removed;");
}
pw.println(" }");
return true;
@ -371,12 +375,12 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
if (! isImmutableFinalType(secondParameterType)) {
pw.println(" p1 = " + deepClone(secondParameterType, "p1") + ";");
}
pw.println(" this.updated |= " + hotRodUtils.getQualifiedName().toString() + ".removeFromSetByMapKey("
pw.println(" " + hotRodEntityField("updated") + " |= " + hotRodUtils.getQualifiedName().toString() + ".removeFromSetByMapKey("
+ hotRodEntityField(fieldName) + ", "
+ "p0, "
+ keyGetterReference(collectionItemType) + ");"
);
pw.println(" this.updated |= !valueUndefined && " + hotRodEntityField(fieldName)
pw.println(" " + hotRodEntityField("updated") + " |= !valueUndefined && " + hotRodEntityField(fieldName)
+ ".add(" + migrateToType(collectionItemType, new TypeMirror[]{firstParameterType, secondParameterType}, new String[]{"p0", "p1"}) + ");");
pw.println(" }");
return true;

View file

@ -22,10 +22,9 @@ import org.infinispan.protostream.annotations.ProtoField;
import org.keycloak.models.map.annotations.GenerateHotRodEntityImplementation;
import org.keycloak.models.map.storage.hotRod.common.AbstractHotRodEntity;
import org.keycloak.models.map.storage.hotRod.common.HotRodAttributeEntity;
import org.keycloak.models.map.storage.hotRod.common.HotRodEntityDelegate;
import org.keycloak.models.map.storage.hotRod.common.HotRodPair;
import org.keycloak.models.map.client.MapClientEntity;
import org.keycloak.models.map.common.UpdatableEntity;
import org.keycloak.models.map.storage.hotRod.common.UpdatableHotRodEntityDelegateImpl;
import java.util.Collection;
import java.util.LinkedList;
@ -40,7 +39,7 @@ import java.util.stream.Stream;
inherits = "org.keycloak.models.map.storage.hotRod.client.HotRodClientEntity.AbstractHotRodClientEntityDelegate"
)
@ProtoDoc("@Indexed")
public class HotRodClientEntity implements AbstractHotRodEntity {
public class HotRodClientEntity extends AbstractHotRodEntity {
@ProtoField(number = 1, required = true)
public int entityVersion = 1;
@ -160,7 +159,7 @@ public class HotRodClientEntity implements AbstractHotRodEntity {
@ProtoField(number = 36)
public Integer nodeReRegistrationTimeout;
public static abstract class AbstractHotRodClientEntityDelegate extends UpdatableEntity.Impl implements HotRodEntityDelegate<HotRodClientEntity>, MapClientEntity {
public static abstract class AbstractHotRodClientEntityDelegate extends UpdatableHotRodEntityDelegateImpl<HotRodClientEntity> implements MapClientEntity {
@Override
public String getId() {
@ -172,13 +171,13 @@ public class HotRodClientEntity implements AbstractHotRodEntity {
HotRodClientEntity entity = getHotRodEntity();
if (entity.id != null) throw new IllegalStateException("Id cannot be changed");
entity.id = id;
this.updated |= id != null;
entity.updated |= id != null;
}
@Override
public void setClientId(String clientId) {
HotRodClientEntity entity = getHotRodEntity();
this.updated |= ! Objects.equals(entity.clientId, clientId);
entity.updated |= ! Objects.equals(entity.clientId, clientId);
entity.clientId = clientId;
entity.clientIdLowercase = clientId == null ? null : clientId.toLowerCase();
}

View file

@ -26,7 +26,7 @@ import java.util.Objects;
import java.util.Set;
@GenerateHotRodEntityImplementation(implementInterface = "org.keycloak.models.map.client.MapProtocolMapperEntity")
public class HotRodProtocolMapperEntity implements AbstractHotRodEntity {
public class HotRodProtocolMapperEntity extends AbstractHotRodEntity {
@ProtoField(number = 1)
public String id;
@ProtoField(number = 2)

View file

@ -17,5 +17,18 @@
package org.keycloak.models.map.storage.hotRod.common;
public interface AbstractHotRodEntity {
import org.keycloak.models.map.common.UpdatableEntity;
public abstract class AbstractHotRodEntity implements UpdatableEntity {
public boolean updated;
@Override
public boolean isUpdated() {
return this.updated;
}
@Override
public void clearUpdatedFlag() {
this.updated = false;
}
}

View file

@ -0,0 +1,33 @@
/*
* Copyright 2022 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.keycloak.models.map.storage.hotRod.common;
import org.keycloak.models.map.common.UpdatableEntity;
public abstract class UpdatableHotRodEntityDelegateImpl<E extends UpdatableEntity> implements HotRodEntityDelegate<E> {
@Override
public boolean isUpdated() {
return getHotRodEntity().isUpdated();
}
@Override
public void clearUpdatedFlag() {
getHotRodEntity().clearUpdatedFlag();
}
}

View file

@ -20,11 +20,10 @@ package org.keycloak.models.map.storage.hotRod.group;
import org.infinispan.protostream.annotations.ProtoDoc;
import org.infinispan.protostream.annotations.ProtoField;
import org.keycloak.models.map.annotations.GenerateHotRodEntityImplementation;
import org.keycloak.models.map.common.UpdatableEntity;
import org.keycloak.models.map.group.MapGroupEntity;
import org.keycloak.models.map.storage.hotRod.common.AbstractHotRodEntity;
import org.keycloak.models.map.storage.hotRod.common.HotRodAttributeEntityNonIndexed;
import org.keycloak.models.map.storage.hotRod.common.HotRodEntityDelegate;
import org.keycloak.models.map.storage.hotRod.common.UpdatableHotRodEntityDelegateImpl;
import java.util.Objects;
import java.util.Set;
@ -34,9 +33,9 @@ import java.util.Set;
inherits = "org.keycloak.models.map.storage.hotRod.group.HotRodGroupEntity.AbstractHotRodGroupEntityDelegate"
)
@ProtoDoc("@Indexed")
public class HotRodGroupEntity implements AbstractHotRodEntity {
public class HotRodGroupEntity extends AbstractHotRodEntity {
public static abstract class AbstractHotRodGroupEntityDelegate extends UpdatableEntity.Impl implements HotRodEntityDelegate<HotRodGroupEntity>, MapGroupEntity {
public static abstract class AbstractHotRodGroupEntityDelegate extends UpdatableHotRodEntityDelegateImpl<HotRodGroupEntity> implements MapGroupEntity {
@Override
public String getId() {
@ -48,13 +47,13 @@ public class HotRodGroupEntity implements AbstractHotRodEntity {
HotRodGroupEntity entity = getHotRodEntity();
if (entity.id != null) throw new IllegalStateException("Id cannot be changed");
entity.id = id;
this.updated |= id != null;
entity.updated |= id != null;
}
@Override
public void setName(String name) {
HotRodGroupEntity entity = getHotRodEntity();
updated |= ! Objects.equals(entity.name, name);
entity.updated |= ! Objects.equals(entity.name, name);
entity.name = name;
entity.nameLowercase = name == null ? null : name.toLowerCase();
}