Skip to content

Commit

Permalink
reduce memory usage on annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcGuiot committed Sep 29, 2024
1 parent a3c5779 commit 772dd6e
Show file tree
Hide file tree
Showing 41 changed files with 231 additions and 190 deletions.
22 changes: 12 additions & 10 deletions src/main/java/org/globsframework/core/metamodel/Annotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.Ref;
import org.globsframework.core.utils.exceptions.ItemNotFound;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -15,16 +15,22 @@ public interface Annotations {

Stream<Glob> streamAnnotations();

Stream<Glob> streamAnnotations(GlobType type);
default Stream<Glob> streamAnnotations(GlobType type) {
return streamAnnotations().filter(glob -> glob.getType().equals(type));
}

boolean hasAnnotation(Key key);

Collection<Glob> getAnnotations();

Glob getAnnotation(Key key);

Glob findAnnotation(Key key);

default Glob getAnnotation(Key key) {
Glob annotation = findAnnotation(key);
if (annotation == null) {
throw new ItemNotFound(key.toString());
}
return annotation;
}

default <T> T getValueOrDefault(Key key, Field field, T defaultValue) {
Glob annotation = findAnnotation(key);
if (annotation != null) {
Expand All @@ -42,8 +48,4 @@ default boolean findAnnotation(Key key, Ref<Glob> result) {
return result.get() != null;
}

default Glob findUniqueAnnotation(GlobType globType) {
Optional<Glob> first = streamAnnotations(globType).findFirst();
return first.isPresent() ? first.get() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.globsframework.core.model.Key;
import org.globsframework.core.model.KeyBuilder;
import org.globsframework.core.model.MutableGlob;
import org.globsframework.core.utils.container.specific.HashEmptyGlobContainer;

import java.util.LinkedHashMap;

Expand All @@ -31,7 +32,7 @@ public static MutableGlob create(String value) {
DefaultGlobType globType = new DefaultGlobType("FieldName");
DefaultFieldFactory factory = new DefaultFieldFactory(globType);
TYPE = globType;
NAME = factory.addString("name", false, 0, 0, null, new LinkedHashMap<>());
NAME = factory.addString("name", false, 0, 0, null, HashEmptyGlobContainer.Helper.allocate(1));
globType.completeInit();
UNIQUE_KEY = KeyBuilder.newEmptyKey(TYPE);
NAME.addAnnotation(create("name"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.Utils;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.InvalidParameter;

import java.util.LinkedHashMap;
Expand All @@ -23,7 +24,7 @@ abstract public class AbstractField extends DefaultAnnotations {

protected AbstractField(String name, GlobType globType,
Class valueClass, int index, int keyIndex, boolean isKeyField,
Object defaultValue, DataType dataType, LinkedHashMap<Key, Glob> annotations) {
Object defaultValue, DataType dataType, HashContainer<Key, Glob> annotations) {
super(annotations);
this.keyIndex = keyIndex;
this.defaultValue = defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.LinkedHashMap;

public class DefaultBigDecimalArrayField extends AbstractField implements BigDecimalArrayField {
public DefaultBigDecimalArrayField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, BigDecimal defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultBigDecimalArrayField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, BigDecimal defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, BigDecimal[].class, index, keyIndex, isKeyField, defaultValue, DataType.BigDecimalArray, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.math.BigDecimal;
import java.util.LinkedHashMap;

public class DefaultBigDecimalField extends AbstractField implements BigDecimalField {
public DefaultBigDecimalField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex,
BigDecimal defaultValue, LinkedHashMap<Key, Glob> annotations) {
BigDecimal defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, BigDecimal.class, index, keyIndex, isKeyField, defaultValue, DataType.BigDecimal, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Arrays;
import java.util.LinkedHashMap;

public class DefaultBlobField extends AbstractField implements BlobField {

public DefaultBlobField(String name, GlobType globType, int index, LinkedHashMap<Key, Glob> annotations) {
public DefaultBlobField(String name, GlobType globType, int index, HashContainer<Key, Glob> annotations) {
super(name, globType, byte[].class, index, -1, false, null, DataType.Bytes, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Arrays;
import java.util.LinkedHashMap;

public class DefaultBooleanArrayField extends AbstractField implements BooleanArrayField {

public DefaultBooleanArrayField(String name, GlobType globType,
int index, boolean isKeyField, final int keyIndex, Boolean defaultValue, LinkedHashMap<Key, Glob> annotations) {
int index, boolean isKeyField, final int keyIndex, Boolean defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, boolean[].class, index, keyIndex, isKeyField, defaultValue, DataType.BooleanArray, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.LinkedHashMap;

public class DefaultBooleanField extends AbstractField implements BooleanField {
public DefaultBooleanField(String name, GlobType globType, int index, boolean isKeyField, final int keyIndex, Boolean defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultBooleanField(String name, GlobType globType, int index, boolean isKeyField, final int keyIndex, Boolean defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, Boolean.class, index, keyIndex, isKeyField, defaultValue, DataType.Boolean, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.time.LocalDate;
import java.util.LinkedHashMap;

public class DefaultDateField extends AbstractField implements DateField {
public DefaultDateField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, LocalDate defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultDateField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, LocalDate defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, LocalDate.class, index, keyIndex, isKeyField, defaultValue, DataType.Date, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.time.ZonedDateTime;
import java.util.LinkedHashMap;

public class DefaultDateTimeField extends AbstractField implements DateTimeField {
public DefaultDateTimeField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, ZonedDateTime defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultDateTimeField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, ZonedDateTime defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, ZonedDateTime.class, index, keyIndex, isKeyField, defaultValue, DataType.DateTime, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Arrays;
import java.util.LinkedHashMap;

public class DefaultDoubleArrayField extends AbstractField implements DoubleArrayField {

public DefaultDoubleArrayField(String name, GlobType globType,
int index, boolean isKeyField, final int keyIndex, Double defaultValue, LinkedHashMap<Key, Glob> annotations) {
int index, boolean isKeyField, final int keyIndex, Double defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, double[].class, index, keyIndex, isKeyField, defaultValue, DataType.DoubleArray, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.LinkedHashMap;

public class DefaultDoubleField extends AbstractField implements DoubleField {

public DefaultDoubleField(String name, GlobType globType,
int index, boolean isKeyField, final int keyIndex, Double defaultValue, LinkedHashMap<Key, Glob> annotations) {
int index, boolean isKeyField, final int keyIndex, Double defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, Double.class, index, keyIndex, isKeyField, defaultValue, DataType.Double, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.InvalidParameter;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;

public class DefaultGlobArrayField extends AbstractField implements GlobArrayField {
private final GlobType targetType;

public DefaultGlobArrayField(String name, GlobType globType, GlobType targetType,
int index, boolean isKeyField, final int keyIndex, LinkedHashMap<Key, Glob> annotations) {
int index, boolean isKeyField, final int keyIndex, HashContainer<Key, Glob> annotations) {
super(name, globType, Glob[].class, index, keyIndex, isKeyField, null, DataType.GlobArray, annotations);
this.targetType = targetType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.InvalidParameter;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;

public class DefaultGlobField extends AbstractField implements GlobField {
private final GlobType targetType;

public DefaultGlobField(String name, GlobType globType, GlobType targetType,
int index, boolean isKeyField, final int keyIndex, LinkedHashMap<Key, Glob> annotations) {
int index, boolean isKeyField, final int keyIndex, HashContainer<Key, Glob> annotations) {
super(name, globType, Glob.class, index, keyIndex, isKeyField, null, DataType.Glob, annotations);
this.targetType = targetType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.InvalidParameter;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultGlobUnionArrayField extends AbstractField implements GlobArrayUnionField {
private final Map<String, GlobType> targetTypes;

public DefaultGlobUnionArrayField(String name, GlobType globType, Collection<GlobType> targetTypes,
int index, boolean isKeyField, final int keyIndex, LinkedHashMap<Key, Glob> annotations) {
int index, boolean isKeyField, final int keyIndex, HashContainer<Key, Glob> annotations) {
super(name, globType, Glob[].class, index, keyIndex, isKeyField, null, DataType.GlobUnionArray, annotations);
this.targetTypes = new ConcurrentHashMap<>();
targetTypes.forEach(this::__add__);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.InvalidParameter;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultGlobUnionField extends AbstractField implements GlobUnionField {
private final Map<String, GlobType> targetTypes;

public DefaultGlobUnionField(String name, GlobType globType, Collection<GlobType> targetTypes,
int index, boolean isKeyField, final int keyIndex, LinkedHashMap<Key, Glob> annotations) {
int index, boolean isKeyField, final int keyIndex, HashContainer<Key, Glob> annotations) {
super(name, globType, Glob.class, index, keyIndex, isKeyField, null, DataType.GlobUnion, annotations);
this.targetTypes = new ConcurrentHashMap<>();
targetTypes.forEach(this::__add__);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Arrays;
import java.util.LinkedHashMap;

public class DefaultIntegerArrayField extends AbstractField implements IntegerArrayField {
public DefaultIntegerArrayField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Integer defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultIntegerArrayField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Integer defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, int[].class, index, keyIndex, isKeyField, defaultValue, DataType.IntegerArray, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.LinkedHashMap;

public class DefaultIntegerField extends AbstractField implements IntegerField {
public DefaultIntegerField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Integer defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultIntegerField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Integer defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, Integer.class, index, keyIndex, isKeyField, defaultValue, DataType.Integer, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.Arrays;
import java.util.LinkedHashMap;

public class DefaultLongArrayField extends AbstractField implements LongArrayField {
public DefaultLongArrayField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Long defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultLongArrayField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Long defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, long[].class, index, keyIndex, isKeyField, defaultValue, DataType.LongArray, annotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import org.globsframework.core.metamodel.type.DataType;
import org.globsframework.core.model.Glob;
import org.globsframework.core.model.Key;
import org.globsframework.core.utils.container.hash.HashContainer;
import org.globsframework.core.utils.exceptions.UnexpectedApplicationState;

import java.util.LinkedHashMap;

public class DefaultLongField extends AbstractField implements LongField {
public DefaultLongField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Long defaultValue, LinkedHashMap<Key, Glob> annotations) {
public DefaultLongField(String name, GlobType globType, int index, boolean isKeyField, int keyIndex, Long defaultValue, HashContainer<Key, Glob> annotations) {
super(name, globType, Long.class, index, keyIndex, isKeyField, defaultValue, DataType.Long, annotations);
}

Expand Down
Loading

0 comments on commit 772dd6e

Please sign in to comment.