Skip to content

Commit

Permalink
HSEARCH-3047 Map UUID to String type at mapping stage
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever authored and yrodiere committed Feb 20, 2019
1 parent 2252c83 commit 9c6539f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
Expand Up @@ -57,15 +57,15 @@ public Class<?> getTypeWithIdentifierBridge2() {

@Override
public Optional<DefaultValueBridgeExpectations<UUID, ?>> getDefaultValueBridgeExpectations() {
return Optional.of( new DefaultValueBridgeExpectations<UUID, UUID>() {
return Optional.of( new DefaultValueBridgeExpectations<UUID, String>() {
@Override
public Class<UUID> getProjectionType() {
return UUID.class;
}

@Override
public Class<UUID> getIndexFieldJavaType() {
return UUID.class;
public Class<String> getIndexFieldJavaType() {
return String.class;
}

@Override
Expand All @@ -74,8 +74,8 @@ public List<UUID> getEntityPropertyValues() {
}

@Override
public List<UUID> getDocumentFieldValues() {
return getEntityPropertyValues();
public List<String> getDocumentFieldValues() {
return getStrings();
}

@Override
Expand Down Expand Up @@ -157,7 +157,8 @@ public static List<UUID> getSequence() {
new UUID( Long.MIN_VALUE, 0L ),
new UUID( Long.MIN_VALUE, 1L ),
new UUID( Long.MAX_VALUE, Long.MIN_VALUE ),
new UUID( Long.MAX_VALUE, Long.MAX_VALUE )
new UUID( Long.MAX_VALUE, Long.MAX_VALUE ),
UUID.fromString( "8cea97f9-9696-4299-9f05-636a208b6c1f" )
);
}

Expand All @@ -168,7 +169,8 @@ public static List<String> getStrings() {
"80000000-0000-0000-0000-000000000000",
"80000000-0000-0000-0000-000000000001",
"7fffffff-ffff-ffff-8000-000000000000",
"7fffffff-ffff-ffff-7fff-ffffffffffff"
"7fffffff-ffff-ffff-7fff-ffffffffffff",
"8cea97f9-9696-4299-9f05-636a208b6c1f"
);
}
}
@@ -0,0 +1,66 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.bridge.builtin.impl;

import java.util.UUID;

import org.hibernate.search.engine.backend.types.converter.FromDocumentFieldValueConverter;
import org.hibernate.search.engine.backend.types.converter.runtime.FromDocumentFieldValueConvertContext;
import org.hibernate.search.engine.backend.types.dsl.StandardIndexFieldTypeContext;
import org.hibernate.search.mapper.pojo.bridge.ValueBridge;
import org.hibernate.search.mapper.pojo.bridge.binding.ValueBridgeBindingContext;
import org.hibernate.search.mapper.pojo.bridge.runtime.ValueBridgeToIndexedValueContext;

public final class DefaultUUIDValueBridge implements ValueBridge<UUID, String> {

@Override
public String toString() {
return getClass().getSimpleName();
}

@Override
@SuppressWarnings("unchecked") // The bridge resolver performs the checks using reflection
public StandardIndexFieldTypeContext<?, String> bind(ValueBridgeBindingContext<UUID> context) {
return context.getTypeFactory().asString()
.projectionConverter( PojoDefaultUUIDFromDocumentFieldValueConverter.INSTANCE );
}

@Override
public String toIndexedValue(UUID value, ValueBridgeToIndexedValueContext context) {
return value == null ? null : value.toString();
}

@Override
public UUID cast(Object value) {
return (UUID) value;
}

@Override
public boolean isCompatibleWith(ValueBridge<?, ?> other) {
return getClass().equals( other.getClass() );
}

private static class PojoDefaultUUIDFromDocumentFieldValueConverter
implements FromDocumentFieldValueConverter<String, UUID> {
private static final PojoDefaultUUIDFromDocumentFieldValueConverter INSTANCE = new PojoDefaultUUIDFromDocumentFieldValueConverter();

@Override
public boolean isConvertedTypeAssignableTo(Class<?> superTypeCandidate) {
return superTypeCandidate.isAssignableFrom( UUID.class );
}

@Override
public UUID convert(String value, FromDocumentFieldValueConvertContext context) {
return value == null ? null : UUID.fromString( value );
}

@Override
public boolean isCompatibleWith(FromDocumentFieldValueConverter<?, ?> other) {
return INSTANCE.equals( other );
}
}
}
Expand Up @@ -48,6 +48,7 @@
import org.hibernate.search.mapper.pojo.bridge.builtin.impl.DefaultLongIdentifierBridge;
import org.hibernate.search.mapper.pojo.bridge.builtin.impl.DefaultShortIdentifierBridge;
import org.hibernate.search.mapper.pojo.bridge.builtin.impl.DefaultUUIDIdentifierBridge;
import org.hibernate.search.mapper.pojo.bridge.builtin.impl.DefaultUUIDValueBridge;
import org.hibernate.search.mapper.pojo.bridge.builtin.impl.PassThroughValueBridge;
import org.hibernate.search.mapper.pojo.bridge.mapping.BridgeBuilder;
import org.hibernate.search.mapper.pojo.logging.impl.Log;
Expand Down Expand Up @@ -98,7 +99,7 @@ public BridgeResolver(TypePatternMatcherFactory typePatternMatcherFactory) {
addValueBridgeForExactRawType( Double.class, ignored -> BeanHolder.of( new PassThroughValueBridge<>( Double.class ) ) );
addValueBridgeForExactRawType( BigDecimal.class, ignored -> BeanHolder.of( new PassThroughValueBridge<>( BigDecimal.class ) ) );
addValueBridgeForExactRawType( BigInteger.class, ignored -> BeanHolder.of( new PassThroughValueBridge<>( BigInteger.class ) ) );
addValueBridgeForExactRawType( UUID.class, ignored -> BeanHolder.of( new PassThroughValueBridge<>( UUID.class ) ) );
addValueBridgeForExactRawType( UUID.class, ignored -> BeanHolder.of( new DefaultUUIDValueBridge() ) );
addValueBridgeForExactRawType( LocalDateTime.class, ignored -> BeanHolder.of( new PassThroughValueBridge<>( LocalDateTime.class ) ) );
addValueBridgeForExactRawType( LocalTime.class, ignored -> BeanHolder.of( new PassThroughValueBridge<>( LocalTime.class ) ) );
addValueBridgeForExactRawType( ZonedDateTime.class, ignored -> BeanHolder.of( new PassThroughValueBridge<>( ZonedDateTime.class ) ) );
Expand Down

0 comments on commit 9c6539f

Please sign in to comment.