Polishing.

Removed jetbrains annotation.
Removed attempt to cache annotation lookup.
Those lookups are already cached and obtaining them in the constructor causes overhead when they aren't requested at all.
Limit the use of Optional.

See #1099
Original pull request #1108
This commit is contained in:
Jens Schauder
2021-12-07 13:38:28 +01:00
parent d12146d78c
commit 9fc74826ff
2 changed files with 43 additions and 36 deletions

View File

@@ -17,11 +17,11 @@ package org.springframework.data.relational.core.mapping;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
@@ -46,17 +46,18 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
* @param information must not be {@literal null}.
*/
RelationalPersistentEntityImpl(TypeInformation<T> information, NamingStrategy namingStrategy) {
super(information);
final Optional<Table> optionalTableAnnotation = Optional.ofNullable(findAnnotation(Table.class));
this.namingStrategy = namingStrategy;
this.tableName = Lazy.of(() -> optionalTableAnnotation
this.tableName = Lazy.of(() -> Optional.ofNullable(findAnnotation(Table.class))
.map(Table::value)
.filter(StringUtils::hasText)
.map(this::createSqlIdentifier)
);
this.schemaName = Lazy.of(() -> optionalTableAnnotation
this.schemaName = Lazy.of(() -> Optional.ofNullable(findAnnotation(Table.class))
.map(Table::schema)
.filter(StringUtils::hasText)
.map(this::createSqlIdentifier));
@@ -84,30 +85,33 @@ class RelationalPersistentEntityImpl<T> extends BasicPersistentEntity<T, Relatio
*/
@Override
public SqlIdentifier getTableName() {
final Optional<SqlIdentifier> schema = determineCurrentEntitySchema();
final Optional<SqlIdentifier> explicitlySpecifiedTableName = tableName.get();
if (schema.isPresent()) {
return explicitlySpecifiedTableName
.map(sqlIdentifier -> SqlIdentifier.from(schema.get(), sqlIdentifier))
.orElse(SqlIdentifier.from(schema.get(), createDerivedSqlIdentifier(namingStrategy.getTableName(getType()))));
} else {
return explicitlySpecifiedTableName.orElse(createDerivedSqlIdentifier(namingStrategy.getTableName(getType())));
SqlIdentifier schema = determineCurrentEntitySchema();
Optional<SqlIdentifier> explicitlySpecifiedTableName = tableName.get();
final SqlIdentifier schemalessTableIdentifier = createDerivedSqlIdentifier(namingStrategy.getTableName(getType()));
if (schema == null) {
return explicitlySpecifiedTableName.orElse(schemalessTableIdentifier);
}
return explicitlySpecifiedTableName
.map(sqlIdentifier -> SqlIdentifier.from(schema, sqlIdentifier))
.orElse(SqlIdentifier.from(schema, schemalessTableIdentifier));
}
/**
* @return Optional of {@link SqlIdentifier} representing the current entity schema. If the schema is not specified neither
* explicitly, nor via {@link NamingStrategy}, then return {@link Optional#empty()}
* @return {@link SqlIdentifier} representing the current entity schema. If the schema is not specified, neither
* explicitly, nor via {@link NamingStrategy}, then return {@link null}
*/
@NotNull
private Optional<SqlIdentifier> determineCurrentEntitySchema() {
final Optional<SqlIdentifier> explicitlySpecifiedSchema = schemaName.get();
if (explicitlySpecifiedSchema.isPresent()) {
return explicitlySpecifiedSchema;
}
return StringUtils.hasText(namingStrategy.getSchema())
? Optional.of(createDerivedSqlIdentifier(namingStrategy.getSchema()))
: Optional.empty();
@Nullable
private SqlIdentifier determineCurrentEntitySchema() {
Optional<SqlIdentifier> explicitlySpecifiedSchema = schemaName.get();
return explicitlySpecifiedSchema.orElseGet(
() -> StringUtils.hasText(namingStrategy.getSchema())
? createDerivedSqlIdentifier(namingStrategy.getSchema())
: null);
}
/*

View File

@@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.relational.core.sql.SqlIdentifier.*;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.SqlIdentifier;
@@ -73,30 +72,34 @@ public class RelationalPersistentEntityImplUnitTests {
.isEqualTo("\"MY_SCHEMA\".\"DUMMY_ENTITY_WITH_EMPTY_ANNOTATION\"");
}
@Test // DATAJDBC-1099
@Test // GH-1099
void testRelationalPersistentEntitySchemaNameChoice() {
mappingContext = new RelationalMappingContext(NamingStrategyWithSchema.INSTANCE);
final RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithExplicitSchema.class);
final SqlIdentifier tableName = persistentEntity.getTableName();
RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithSchemaAndName.class);
SqlIdentifier tableName = persistentEntity.getTableName();
assertThat(tableName).isEqualTo(SqlIdentifier.from(SqlIdentifier.quoted("DART_VADER"), quoted("I_AM_THE_SENATE")));
assertThat(tableName.toString()).isEqualTo("\"DART_VADER\".\"I_AM_THE_SENATE\"");
}
@Test // DATAJDBC-1099
void testRelationalPersistentEntityTableOnlySchemaSpecified() {
final RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithSchemaFromNamingStrategy.class);
final SqlIdentifier tableName = persistentEntity.getTableName();
assertThat(tableName).isEqualTo(SqlIdentifier.from(quoted("ANAKYN_SKYWALKER"), quoted("ENTITY_WITH_SCHEMA_FROM_NAMING_STRATEGY")));
assertThat(tableName.toString()).isEqualTo("\"ANAKYN_SKYWALKER\".\"ENTITY_WITH_SCHEMA_FROM_NAMING_STRATEGY\"");
@Test // GH-1099
void specifiedSchemaGetsCombinedWithNameFromNamingStrategy() {
RelationalPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(EntityWithSchema.class);
SqlIdentifier tableName = persistentEntity.getTableName();
assertThat(tableName).isEqualTo(SqlIdentifier.from(quoted("ANAKYN_SKYWALKER"), quoted("ENTITY_WITH_SCHEMA")));
}
@Table(schema = "ANAKYN_SKYWALKER")
static class EntityWithSchemaFromNamingStrategy {
static class EntityWithSchema {
@Id private Long id;
}
@Table(schema = "DART_VADER", name = "I_AM_THE_SENATE")
static class EntityWithExplicitSchema {
static class EntityWithSchemaAndName {
@Id private Long id;
}