Skip to content

Commit

Permalink
Begun implementing the IndexHolders
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Mar 10, 2015
1 parent 12c32a0 commit e3d450a
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 133 deletions.
Expand Up @@ -25,10 +25,13 @@
import com.speedment.codegen.lang.models.Javadoc; import com.speedment.codegen.lang.models.Javadoc;
import com.speedment.codegen.lang.models.Type; import com.speedment.codegen.lang.models.Type;
import static com.speedment.codegen.lang.models.constants.DefaultJavadocTag.AUTHOR; import static com.speedment.codegen.lang.models.constants.DefaultJavadocTag.AUTHOR;
import com.speedment.codegen.lang.models.constants.DefaultType;
import com.speedment.codegen.lang.models.implementation.FileImpl; import com.speedment.codegen.lang.models.implementation.FileImpl;
import com.speedment.codegen.lang.models.implementation.JavadocImpl; import com.speedment.codegen.lang.models.implementation.JavadocImpl;
import com.speedment.orm.code.model.java.DefaultJavaClassTranslator; import com.speedment.orm.code.model.java.DefaultJavaClassTranslator;
import com.speedment.orm.config.model.Column;
import com.speedment.orm.config.model.Table; import com.speedment.orm.config.model.Table;
import java.util.stream.Stream;


/** /**
* *
Expand Down Expand Up @@ -73,6 +76,7 @@ public String getImplName() {
MANAGER = new ClassType("Manager", "Impl"); MANAGER = new ClassType("Manager", "Impl");


protected final Generic protected final Generic
GENERIC_OF_PK = Generic.of().add(typeOfPK()),
GENERIC_OF_ENTITY = Generic.of().add(ENTITY.getType()), GENERIC_OF_ENTITY = Generic.of().add(ENTITY.getType()),
GENERIC_OF_MANAGER = Generic.of().add(MANAGER.getType()), GENERIC_OF_MANAGER = Generic.of().add(MANAGER.getType()),
GENERIC_OF_BUILDER = Generic.of().add(BUILDER.getType()); GENERIC_OF_BUILDER = Generic.of().add(BUILDER.getType());
Expand All @@ -82,6 +86,26 @@ public BaseEntityTranslator(CodeGenerator cg, Table configEntity) {
this.cg = cg; this.cg = cg;
} }


protected Type typeOfPK() {
final long pks = primaryKeyColumns().count();

if (pks == 0) {
throw new UnsupportedOperationException("Table '" + table().getName() + "' does not have a valid primary key.");
}

final Class<?> first = primaryKeyColumns().findAny().get().getColumn().getMapping();

if (pks == 1) {
return Type.of(first);
} else {
if (primaryKeyColumns().allMatch(c -> c.getColumn().getMapping().equals(first))) {
return DefaultType.list(Type.of(first));
} else {
return DefaultType.list(DefaultType.WILDCARD);
}
}
}

protected abstract String getFileName(); protected abstract String getFileName();


@Override @Override
Expand Down
Expand Up @@ -21,11 +21,12 @@
import com.speedment.codegen.lang.models.File; import com.speedment.codegen.lang.models.File;
import com.speedment.codegen.lang.models.Import; import com.speedment.codegen.lang.models.Import;
import com.speedment.codegen.lang.models.Class; import com.speedment.codegen.lang.models.Class;
import com.speedment.codegen.lang.models.Generic;
import com.speedment.codegen.lang.models.Method; import com.speedment.codegen.lang.models.Method;
import com.speedment.codegen.lang.models.Type; import com.speedment.codegen.lang.models.Type;
import static com.speedment.codegen.lang.models.constants.DefaultAnnotationUsage.GENERATED;
import static com.speedment.codegen.lang.models.constants.DefaultAnnotationUsage.OVERRIDE; import static com.speedment.codegen.lang.models.constants.DefaultAnnotationUsage.OVERRIDE;
import com.speedment.orm.config.model.Table; import com.speedment.orm.config.model.Table;
import com.speedment.orm.core.manager.AbstractManager;
import com.speedment.orm.platform.Platform; import com.speedment.orm.platform.Platform;
import com.speedment.orm.platform.component.ProjectComponent; import com.speedment.orm.platform.component.ProjectComponent;
import java.util.stream.Stream; import java.util.stream.Stream;
Expand All @@ -44,6 +45,11 @@ public EntityManagerImplTranslator(CodeGenerator cg, Table configEntity) {
protected Class make(File file) { protected Class make(File file) {
return new ClassBuilder(MANAGER.getImplName()).build() return new ClassBuilder(MANAGER.getImplName()).build()
.public_() .public_()
.setSupertype(Type.of(AbstractManager.class)
.add(Generic.of().add(typeOfPK()))
.add(Generic.of().add(ENTITY.getType()))
.add(Generic.of().add(BUILDER.getType()))
)
.add(MANAGER.getType()) .add(MANAGER.getType())


.call(i -> file.add(Import.of(Type.of(Platform.class)))) .call(i -> file.add(Import.of(Type.of(Platform.class))))
Expand Down Expand Up @@ -82,7 +88,7 @@ protected String getJavadocRepresentText() {


@Override @Override
protected String getFileName() { protected String getFileName() {
return MANAGER.getName(); return MANAGER.getImplName();
} }


@Override @Override
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.speedment.orm.code.model.java.entity; package com.speedment.orm.code.model.java.entity;


import com.speedment.codegen.base.CodeGenerator; import com.speedment.codegen.base.CodeGenerator;
import com.speedment.codegen.lang.models.Field;
import com.speedment.codegen.lang.models.File; import com.speedment.codegen.lang.models.File;
import com.speedment.codegen.lang.models.Import; import com.speedment.codegen.lang.models.Import;
import com.speedment.codegen.lang.models.Interface; import com.speedment.codegen.lang.models.Interface;
Expand All @@ -29,6 +30,8 @@
import com.speedment.orm.core.manager.Manager; import com.speedment.orm.core.manager.Manager;
import com.speedment.orm.platform.Platform; import com.speedment.orm.platform.Platform;
import com.speedment.orm.platform.component.ManagerComponent; import com.speedment.orm.platform.component.ManagerComponent;
import java.util.Arrays;
import java.util.stream.Collectors;


/** /**
* *
Expand All @@ -44,7 +47,9 @@ public EntityManagerTranslator(CodeGenerator cg, Table configEntity) {
protected Interface make(File file) { protected Interface make(File file) {
return new InterfaceBuilder(MANAGER.getName()).build() return new InterfaceBuilder(MANAGER.getName()).build()
.public_() .public_()
.add(Type.of(Manager.class).add(GENERIC_OF_ENTITY).add(GENERIC_OF_BUILDER)) .add(Type.of(Manager.class).add(GENERIC_OF_PK).add(GENERIC_OF_ENTITY).add(GENERIC_OF_BUILDER))

.add(primaryKeyFor(file))


.add(Method.of("getTableName", STRING).default_().add(OVERRIDE) .add(Method.of("getTableName", STRING).default_().add(OVERRIDE)
.add("return \"" + table().getRelativeName(project()) + "\";")) .add("return \"" + table().getRelativeName(project()) + "\";"))
Expand All @@ -66,6 +71,23 @@ protected Interface make(File file) {
".class).manager(" + MANAGER.getName() + ".class);")) ".class).manager(" + MANAGER.getName() + ".class);"))
; ;
} }

protected Method primaryKeyFor(File file) {
final Method method = Method.of("primaryKeyFor", typeOfPK()).default_().add(OVERRIDE)
.add(Field.of("entity", ENTITY.getType()));

if (primaryKeyColumns().count() == 1) {
method.add("return entity.get" + typeName(primaryKeyColumns().findAny().get().getColumn()) + "();");
} else {
file.add(Import.of(Type.of(Arrays.class)));
method.add(primaryKeyColumns()
.map(pkc -> "entity.get" + typeName(pkc.getColumn()) + "()")
.collect(Collectors.joining(", ", "return Arrays.asList(", ");"))
);
}

return method;
}


@Override @Override
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/speedment/orm/core/entity/Entity.java
@@ -0,0 +1,10 @@
package com.speedment.orm.core.entity;

/**
*
* @author Emil Forslund
*/
public interface Entity<PK> {

PK getPrimaryKey();
}
20 changes: 20 additions & 0 deletions src/main/java/com/speedment/orm/core/manager/AbstractManager.java
@@ -0,0 +1,20 @@
package com.speedment.orm.core.manager;

import com.speedment.orm.config.model.Column;
import com.speedment.orm.core.Buildable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
*
* @author Emil Forslund
*/
public abstract class AbstractManager<PK, ENTITY, BUILDER extends Buildable<ENTITY>> implements Manager<PK, ENTITY, BUILDER> {

private final Map<List<Column>, IndexHolder> indexes;

public AbstractManager() {
indexes = new ConcurrentHashMap<>();
}
}
86 changes: 0 additions & 86 deletions src/main/java/com/speedment/orm/core/manager/DefaultManager.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/java/com/speedment/orm/core/manager/EntityMapper.java

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/java/com/speedment/orm/core/manager/IndexHolder.java
@@ -0,0 +1,18 @@
package com.speedment.orm.core.manager;

import java.util.stream.Stream;

/**
*
* @author Emil Forslund
*/
public interface IndexHolder<KEY, ENTITY> {

Stream<ENTITY> stream();

Stream<ENTITY> stream(KEY key);

void put(KEY key, ENTITY entity);

void remove(KEY key);
}
40 changes: 40 additions & 0 deletions src/main/java/com/speedment/orm/core/manager/KeyIndexHolder.java
@@ -0,0 +1,40 @@
package com.speedment.orm.core.manager;

import static com.speedment.util.stream.StreamUtil.streamOfNullable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

/**
*
* @author Emil Forslund
*/
public class KeyIndexHolder<KEY, ENTITY> implements IndexHolder<KEY, ENTITY> {

private final Map<KEY, ENTITY> entities;

public KeyIndexHolder() {
entities = new ConcurrentHashMap<>();
}

@Override
public Stream<ENTITY> stream() {
return entities.values().stream();
}

@Override
public Stream<ENTITY> stream(KEY key) {
return streamOfNullable(entities.get(key));
}

@Override
public void put(KEY key, ENTITY entity) {
entities.put(key, entity);
}

@Override
public void remove(KEY key) {
entities.remove(key);
}

}
9 changes: 4 additions & 5 deletions src/main/java/com/speedment/orm/core/manager/Manager.java
Expand Up @@ -24,17 +24,16 @@
import com.speedment.orm.annotations.Api; import com.speedment.orm.annotations.Api;
import com.speedment.orm.config.model.Table; import com.speedment.orm.config.model.Table;
import com.speedment.orm.core.Buildable; import com.speedment.orm.core.Buildable;
import com.speedment.orm.core.Persistable;
import com.speedment.orm.platform.component.Component;
import com.speedment.orm.platform.Speedment;
import java.util.stream.Stream; import java.util.stream.Stream;


/** /**
* *
* @author pemi * @author pemi
*/ */
@Api(version = 0) @Api(version = 0)
public interface Manager<ENTITY, BUILDER extends Buildable<ENTITY>> { public interface Manager<PK, ENTITY, BUILDER extends Buildable<ENTITY>> {

PK primaryKeyFor(ENTITY entity);


String getTableName(); String getTableName();


Expand All @@ -48,7 +47,7 @@ public interface Manager<ENTITY, BUILDER extends Buildable<ENTITY>> {
// //
// BUILDER builder(ENTITY entity); // BUILDER builder(ENTITY entity);


<M extends Manager<ENTITY, BUILDER>> Class<M> getManagerClass(); <M extends Manager<PK, ENTITY, BUILDER>> Class<M> getManagerClass();


Class<ENTITY> getEntityClass(); Class<ENTITY> getEntityClass();


Expand Down

0 comments on commit e3d450a

Please sign in to comment.