Skip to content

Commit

Permalink
ISPN-10452 Remove AsElementAttributeSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Fernandes authored and tristantarrant committed Aug 2, 2019
1 parent 7749437 commit 16467c9
Show file tree
Hide file tree
Showing 36 changed files with 1,141 additions and 360 deletions.
Expand Up @@ -7,7 +7,6 @@
import org.infinispan.commons.configuration.attributes.Attribute; import org.infinispan.commons.configuration.attributes.Attribute;
import org.infinispan.commons.configuration.attributes.AttributeDefinition; import org.infinispan.commons.configuration.attributes.AttributeDefinition;
import org.infinispan.commons.configuration.attributes.AttributeSerializer; import org.infinispan.commons.configuration.attributes.AttributeSerializer;
import org.infinispan.commons.configuration.attributes.AttributeSerializer.SerializationMode;
import org.infinispan.commons.configuration.attributes.AttributeSet; import org.infinispan.commons.configuration.attributes.AttributeSet;
import org.infinispan.commons.configuration.elements.ElementDefinition; import org.infinispan.commons.configuration.elements.ElementDefinition;


Expand Down Expand Up @@ -60,15 +59,6 @@ private void readNestedAttribute(ConfigurationBuilderInfo builderInfo, String el
AttributeSet attributes = builderInfo.attributes(); AttributeSet attributes = builderInfo.attributes();
if (attributes == null) throw new CacheConfigurationException( if (attributes == null) throw new CacheConfigurationException(
String.format("Cannot find any attribute or element under '%s' to handle element '%s'", builderInfo, element)); String.format("Cannot find any attribute or element under '%s' to handle element '%s'", builderInfo, element));
//Handle attributes serialized as elements
for (Attribute a : attributes.attributes()) {
AttributeDefinition<?> attributeDefinition = a.getAttributeDefinition();
AttributeSerializer<?, ?, ?> serializerConfig = attributeDefinition.getSerializerConfig();
SerializationMode serializationMode = serializerConfig.getSerializationMode();
if (serializationMode == SerializationMode.AS_ELEMENT && serializerConfig.canRead(element, nesting, null, attributeDefinition)) {
readAttribute(builderInfo, element, nesting, null, null);
}
}
// Handle attributes serialized as name/value pair // Handle attributes serialized as name/value pair
Pair simpleAttribute1 = findSimpleAttribute(element, null, nesting, builderInfo); Pair simpleAttribute1 = findSimpleAttribute(element, null, nesting, builderInfo);
if (simpleAttribute1 != null) { if (simpleAttribute1 != null) {
Expand Down Expand Up @@ -105,7 +95,7 @@ private void readArray(ConfigurationBuilderInfo builderInfo, String enclosing, S
private void readAttribute(ConfigurationBuilderInfo builderInfo, String enclosing, String nesting, String name, Object value) { private void readAttribute(ConfigurationBuilderInfo builderInfo, String enclosing, String nesting, String name, Object value) {
Pair pair = findSimpleAttribute(enclosing, nesting, name, builderInfo); Pair pair = findSimpleAttribute(enclosing, nesting, name, builderInfo);
if (pair != null) { if (pair != null) {
readAttribute(enclosing, nesting, value, pair.attribute, pair.builderInfo); readAttribute(enclosing, value, pair.attribute, pair.builderInfo);
} else { } else {
ElementDefinition element = builderInfo.getElementDefinition(); ElementDefinition element = builderInfo.getElementDefinition();
if (element != null && element.isSynthetic(name)) { if (element != null && element.isSynthetic(name)) {
Expand All @@ -115,10 +105,10 @@ private void readAttribute(ConfigurationBuilderInfo builderInfo, String enclosin
} }
} }


private void readAttribute(String enclosing, String nesting, Object value, Attribute a, ConfigurationBuilderInfo builderInfo) { private void readAttribute(String enclosing, Object value, Attribute a, ConfigurationBuilderInfo builderInfo) {
AttributeDefinition<?> attributeDefinition = a.getAttributeDefinition(); AttributeDefinition<?> attributeDefinition = a.getAttributeDefinition();
AttributeSerializer serializerConfig = attributeDefinition.getSerializerConfig(); AttributeSerializer serializerConfig = attributeDefinition.getSerializerConfig();
Object attrValue = serializerConfig.readAttributeValue(enclosing, nesting, attributeDefinition, value, builderInfo); Object attrValue = serializerConfig.readAttributeValue(enclosing, attributeDefinition, value, builderInfo);
a.set(attrValue); a.set(attrValue);
} }


Expand Down
Expand Up @@ -95,11 +95,16 @@ private void writeAttributes(Json parent, AttributeSet attributeSet, Configurati
} }


if (attribute.isModified()) { if (attribute.isModified()) {
if (attrName != null && !attrName.isEmpty() && attrValue != null) { if (attrName == null && attrValue instanceof Map) {
if (topLevelElement.isEmpty()) { Map<String, Object> valueMap = (Map<String, Object>) attrValue;
json.set(attrName, attrValue); valueMap.forEach(json::set);
} else { } else {
json.at(topLevelElement, Json.object()).set(attrName, attrValue); if (attrName != null && !attrName.isEmpty() && attrValue != null) {
if (topLevelElement.isEmpty()) {
json.set(attrName, attrValue);
} else {
json.at(topLevelElement, Json.object()).set(attrName, attrValue);
}
} }
} }
} }
Expand Down

This file was deleted.

Expand Up @@ -17,17 +17,10 @@
*/ */
public abstract class AttributeSerializer<T, U extends ConfigurationInfo, B extends ConfigurationBuilderInfo> { public abstract class AttributeSerializer<T, U extends ConfigurationInfo, B extends ConfigurationBuilderInfo> {


public enum SerializationMode {AS_ELEMENT, AS_ATTRIBUTE}

public boolean canRead(String enclosing, String nestingName, String nestedName, AttributeDefinition attributeDefinition) { public boolean canRead(String enclosing, String nestingName, String nestedName, AttributeDefinition attributeDefinition) {
return nestingName == null && nestedName != null && nestedName.equals(attributeDefinition.xmlName()); return nestingName == null && nestedName != null && nestedName.equals(attributeDefinition.xmlName());
} }


public SerializationMode getSerializationMode() {
return SerializationMode.AS_ATTRIBUTE;
}


/** /**
* Returns the parent element that this attribute should be placed under when serializing, or empty String if the attribute is not nested. * Returns the parent element that this attribute should be placed under when serializing, or empty String if the attribute is not nested.
*/ */
Expand All @@ -54,20 +47,14 @@ public Object getSerializationValue(Attribute<T> attribute, U configurationEleme
* for this instance of serializer. * for this instance of serializer.
* *
* @param enclosingElement The parent element where the attribute is located. * @param enclosingElement The parent element where the attribute is located.
* @param nesting In case the attributed is serialized as an element, the name of this element or null otherwise.
* @param attributeDefinition The serialized attribute definition. * @param attributeDefinition The serialized attribute definition.
* @param attrValue The serialize attribute value. * @param attrValue The serialize attribute value.
* @param builderInfo the {@link ConfigurationBuilderInfo} where the attribute is defined. * @param builderInfo the {@link ConfigurationBuilderInfo} where the attribute is defined.
* @return The attribute value deserialized. * @return The attribute value deserialized.
*/ */
public Object readAttributeValue(String enclosingElement, String nesting, AttributeDefinition attributeDefinition, Object attrValue, B builderInfo) { public Object readAttributeValue(String enclosingElement, AttributeDefinition attributeDefinition, Object attrValue, B builderInfo) {
if (attrValue == null) return null; if (attrValue == null) return null;
Class type = attributeDefinition.getType(); Class type = attributeDefinition.getType();
AttributeSerializer serializerConfig = attributeDefinition.getSerializerConfig();
SerializationMode serializationMode = serializerConfig.getSerializationMode();

boolean isElementSerialization = serializationMode == SerializationMode.AS_ELEMENT;
if (isElementSerialization) return null;


if (attrValue instanceof Map && type == TypedProperties.class) { if (attrValue instanceof Map && type == TypedProperties.class) {
TypedProperties typedProperties = new TypedProperties(); TypedProperties typedProperties = new TypedProperties();
Expand Down
Expand Up @@ -14,7 +14,7 @@ public class ClassAttributeSerializer<T, U extends ConfigurationInfo, B extends
public static final AttributeSerializer<Object, ConfigurationInfo, ConfigurationBuilderInfo> INSTANCE = new ClassAttributeSerializer<>(); public static final AttributeSerializer<Object, ConfigurationInfo, ConfigurationBuilderInfo> INSTANCE = new ClassAttributeSerializer<>();


@Override @Override
public Object readAttributeValue(String enclosingElement, String nesting, AttributeDefinition attributeDefinition, Object attrValue, B builderInfo) { public Object readAttributeValue(String enclosingElement, AttributeDefinition attributeDefinition, Object attrValue, B builderInfo) {
return Util.getInstance(attrValue.toString(), builderInfo.getClass().getClassLoader()); return Util.getInstance(attrValue.toString(), builderInfo.getClass().getClassLoader());
} }


Expand Down
Expand Up @@ -48,7 +48,7 @@ public boolean canRead(String enclosing, String nestingName, String nestedName,
} }


@Override @Override
public Object readAttributeValue(String enclosing, String nesting, AttributeDefinition attributeDefinition, Object value, ConfigurationBuilderInfo builderInfo) { public Object readAttributeValue(String enclosing, AttributeDefinition attributeDefinition, Object value, ConfigurationBuilderInfo builderInfo) {
return CacheMode.fromParts(enclosing.substring(0, enclosing.indexOf("-")), value.toString()); return CacheMode.fromParts(enclosing.substring(0, enclosing.indexOf("-")), value.toString());
} }


Expand Down
Expand Up @@ -29,7 +29,7 @@ public class GroupsConfiguration implements Matchable<GroupsConfiguration>, Conf
public final static AttributeDefinition<List<Grouper<?>>> GROUPERS = AttributeDefinition.builder("groupers", null, (Class<List<Grouper<?>>>) (Class<?>) List.class).initializer(LinkedList::new) public final static AttributeDefinition<List<Grouper<?>>> GROUPERS = AttributeDefinition.builder("groupers", null, (Class<List<Grouper<?>>>) (Class<?>) List.class).initializer(LinkedList::new)
.serializer(new AttributeSerializer<List<Grouper<?>>, GroupsConfiguration, GroupsConfigurationBuilder>() { .serializer(new AttributeSerializer<List<Grouper<?>>, GroupsConfiguration, GroupsConfigurationBuilder>() {
@Override @Override
public Object readAttributeValue(String enclosingElement, String nesting, AttributeDefinition attributeDefinition, Object attrValue, GroupsConfigurationBuilder builderInfo) { public Object readAttributeValue(String enclosingElement, AttributeDefinition attributeDefinition, Object attrValue, GroupsConfigurationBuilder builderInfo) {
List<String> values = (List<String>) attrValue; List<String> values = (List<String>) attrValue;
return values.stream().map(v -> Util.getInstance(v, builderInfo.getClass().getClassLoader())).collect(Collectors.toList()); return values.stream().map(v -> Util.getInstance(v, builderInfo.getClass().getClassLoader())).collect(Collectors.toList());
} }
Expand Down
Expand Up @@ -2,14 +2,10 @@


import static org.infinispan.configuration.parsing.Element.MEMORY; import static org.infinispan.configuration.parsing.Element.MEMORY;


import org.infinispan.commons.configuration.AbstractTypedPropertiesConfiguration; import java.util.Collections;
import java.util.List;

import org.infinispan.commons.configuration.ConfigurationInfo; import org.infinispan.commons.configuration.ConfigurationInfo;
import org.infinispan.commons.configuration.attributes.AsElementAttributeSerializer;
import org.infinispan.commons.configuration.attributes.Attribute;
import org.infinispan.commons.configuration.attributes.AttributeDefinition;
import org.infinispan.commons.configuration.attributes.AttributeSerializer;
import org.infinispan.commons.configuration.attributes.AttributeSet;
import org.infinispan.commons.configuration.attributes.IdentityAttributeCopier;
import org.infinispan.commons.configuration.attributes.Matchable; import org.infinispan.commons.configuration.attributes.Matchable;
import org.infinispan.commons.configuration.elements.DefaultElementDefinition; import org.infinispan.commons.configuration.elements.DefaultElementDefinition;
import org.infinispan.commons.configuration.elements.ElementDefinition; import org.infinispan.commons.configuration.elements.ElementDefinition;
Expand All @@ -23,158 +19,101 @@
*/ */
public class MemoryConfiguration implements Matchable<MemoryConfiguration>, ConfigurationInfo { public class MemoryConfiguration implements Matchable<MemoryConfiguration>, ConfigurationInfo {


private static AttributeSerializer<StorageType, MemoryConfiguration, MemoryConfigurationBuilder> STORAGE_SERIALIZER = new AsElementAttributeSerializer<StorageType, MemoryConfiguration, MemoryConfigurationBuilder>() { private final List<ConfigurationInfo> subElements;
@Override private final MemoryStorageConfiguration memoryStorageConfiguration;
public boolean canRead(String enclosing, String nestingName, String nestedName, AttributeDefinition attributeDefinition) {
return nestedName == null && StorageType.forElement(nestingName) != null;
}

@Override
public String getParentElement(MemoryConfiguration configurationElement) {
StorageType storageType = configurationElement.storageType();
return storageType != null ? storageType.getElement().getLocalName() : null;
}

@Override
public Object readAttributeValue(String enclosingElement, String nesting, AttributeDefinition attributeDefinition, Object attrValue, MemoryConfigurationBuilder builderInfo) {
return StorageType.forElement(nesting);
}

};

private static AttributeSerializer<Object, MemoryConfiguration, MemoryConfigurationBuilder> UNDER_STORAGE = new AttributeSerializer<Object, MemoryConfiguration, MemoryConfigurationBuilder>() {
@Override
public String getParentElement(MemoryConfiguration configurationElement) {
StorageType storageType = configurationElement.storageType();
return storageType != null ? storageType.getElement().getLocalName() : null;
}

@Override
public boolean canRead(String enclosing, String nestingName, String nestedName, AttributeDefinition attributeDefinition) {
return StorageType.forElement(nestingName) != null && nestedName.equals(attributeDefinition.xmlName());
}
};

public static final AttributeDefinition<Integer> ADDRESS_COUNT = AttributeDefinition.builder("address-count", 1_048_576).serializer(UNDER_STORAGE).build();
public static final AttributeDefinition<StorageType> STORAGE_TYPE = AttributeDefinition
.builder("storage", StorageType.OBJECT).copier(IdentityAttributeCopier.INSTANCE)
.serializer(STORAGE_SERIALIZER)
.immutable().build();
public static final AttributeDefinition<Long> SIZE = AttributeDefinition.builder("size", -1L).serializer(UNDER_STORAGE).build();
public static final AttributeDefinition<EvictionType> EVICTION_TYPE = AttributeDefinition.builder("type", EvictionType.COUNT).xmlName(org.infinispan.configuration.parsing.Attribute.EVICTION.getLocalName()).serializer(UNDER_STORAGE).build();
public static final AttributeDefinition<EvictionStrategy> EVICTION_STRATEGY = AttributeDefinition.builder("strategy", EvictionStrategy.NONE).serializer(UNDER_STORAGE).build();


public static final ElementDefinition ELEMENT_DEFINITION = new DefaultElementDefinition(MEMORY.getLocalName()); public static final ElementDefinition ELEMENT_DEFINITION = new DefaultElementDefinition(MEMORY.getLocalName());


static public AttributeSet attributeDefinitionSet() { MemoryConfiguration(MemoryStorageConfiguration memoryStorageConfiguration) {
return new AttributeSet(MemoryConfiguration.class, AbstractTypedPropertiesConfiguration.attributeSet(), this.memoryStorageConfiguration = memoryStorageConfiguration;
STORAGE_TYPE, SIZE, EVICTION_TYPE, EVICTION_STRATEGY, ADDRESS_COUNT); this.subElements = Collections.singletonList(memoryStorageConfiguration);
}

private final Attribute<Long> size;
private final Attribute<EvictionType> evictionType;
private final Attribute<EvictionStrategy> evictionStrategy;
private final Attribute<StorageType> storageType;
private final Attribute<Integer> addressCount;
private final AttributeSet attributes;

MemoryConfiguration(AttributeSet attributes) {
this.attributes = attributes;
storageType = attributes.attribute(STORAGE_TYPE);
size = attributes.attribute(SIZE);
evictionType = attributes.attribute(EVICTION_TYPE);
evictionStrategy = attributes.attribute(EVICTION_STRATEGY);
addressCount = attributes.attribute(ADDRESS_COUNT);
} }


@Override @Override
public ElementDefinition getElementDefinition() { public ElementDefinition getElementDefinition() {
return ELEMENT_DEFINITION; return ELEMENT_DEFINITION;
} }


@Override
public List<ConfigurationInfo> subElements() {
return subElements;
}

/** /**
* Storage type to use for the data container * Storage type to use for the data container
* @return * @return
*/ */
public StorageType storageType() { public StorageType storageType() {
return storageType.get(); return memoryStorageConfiguration.storageType();
} }


/** /**
* Size of the eviction, -1 if disabled * Size of the eviction, -1 if disabled
* @return * @return
*/ */
public long size() { public long size() {
return size.get(); return memoryStorageConfiguration.size();
} }


public void size(long newSize) { public void size(long newSize) {
size.set(newSize); memoryStorageConfiguration.size(newSize);
} }


/** /**
* The configured eviction type * The configured eviction type
* @return * @return
*/ */
public EvictionType evictionType() { public EvictionType evictionType() {
return evictionType.get(); return memoryStorageConfiguration.evictionType();
} }


/** /**
* The configured eviction strategy * The configured eviction strategy
* @return * @return
*/ */
public EvictionStrategy evictionStrategy() { public EvictionStrategy evictionStrategy() {
return evictionStrategy.get(); return memoryStorageConfiguration.evictionStrategy();
} }


/** /**
* Returns whether remove eviction is in use * Returns whether remove eviction is in use
* @return * @return
*/ */
public boolean isEvictionEnabled() { public boolean isEvictionEnabled() {
return size.get() > 0 && evictionStrategy.get().isRemovalBased(); return memoryStorageConfiguration.size() > 0 && memoryStorageConfiguration.evictionStrategy().isRemovalBased();
} }


/** /**
* The address pointer count * The address pointer count
* @return * @return
*/ */
public int addressCount() { public int addressCount() {
return addressCount.get(); return memoryStorageConfiguration.addressCount();
}

public AttributeSet attributes() {
return attributes;
} }


@Override @Override
public boolean equals(Object obj) { public boolean equals(Object o) {
if (this == obj) if (this == o) return true;
return true; if (o == null || getClass() != o.getClass()) return false;
if (obj == null)
return false; MemoryConfiguration that = (MemoryConfiguration) o;
if (getClass() != obj.getClass())
return false; return memoryStorageConfiguration.equals(that.memoryStorageConfiguration);
MemoryConfiguration other = (MemoryConfiguration) obj;
if (attributes == null) {
if (other.attributes != null)
return false;
} else if (!attributes.equals(other.attributes))
return false;
return true;
} }


@Override
public int hashCode() { public int hashCode() {
final int prime = 31; return memoryStorageConfiguration.hashCode();
int result = 1;
result = prime * result + ((attributes == null) ? 0 : attributes.hashCode());
return result;
} }


@Override @Override
public String toString() { public String toString() {
return "MemoryConfiguration [attributes=" + attributes + "]"; return "MemoryConfiguration{" +
"memoryStorageConfiguration=" + memoryStorageConfiguration +
'}';
} }


public MemoryStorageConfiguration heapConfiguration() {
return memoryStorageConfiguration;
}
} }

0 comments on commit 16467c9

Please sign in to comment.