Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3589 Remove some duplicated code in the Lucene backend
- Loading branch information
Showing
11 changed files
with
228 additions
and
278 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...h/backend/lucene/types/aggregation/impl/AbstractLuceneFieldAggregationBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.backend.lucene.types.aggregation.impl; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
|
||
import org.hibernate.search.backend.lucene.logging.impl.Log; | ||
import org.hibernate.search.backend.lucene.types.codec.impl.LuceneFieldCodec; | ||
import org.hibernate.search.engine.backend.types.converter.spi.DslConverter; | ||
import org.hibernate.search.engine.backend.types.converter.spi.ProjectionConverter; | ||
import org.hibernate.search.engine.reporting.spi.EventContexts; | ||
import org.hibernate.search.util.common.logging.impl.LoggerFactory; | ||
|
||
public abstract class AbstractLuceneFieldAggregationBuilderFactory<F> | ||
implements LuceneFieldAggregationBuilderFactory { | ||
protected static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() ); | ||
|
||
protected final boolean aggregable; | ||
|
||
protected final DslConverter<?, ? extends F> toFieldValueConverter; | ||
protected final ProjectionConverter<? super F, ?> fromFieldValueConverter; | ||
|
||
public AbstractLuceneFieldAggregationBuilderFactory( | ||
boolean aggregable, DslConverter<?, ? extends F> toFieldValueConverter, | ||
ProjectionConverter<? super F, ?> fromFieldValueConverter) { | ||
this.aggregable = aggregable; | ||
this.toFieldValueConverter = toFieldValueConverter; | ||
this.fromFieldValueConverter = fromFieldValueConverter; | ||
} | ||
|
||
@Override | ||
public boolean hasCompatibleCodec(LuceneFieldAggregationBuilderFactory other) { | ||
if ( !getClass().equals( other.getClass() ) ) { | ||
return false; | ||
} | ||
AbstractLuceneFieldAggregationBuilderFactory<?> castedOther = | ||
(AbstractLuceneFieldAggregationBuilderFactory<?>) other; | ||
return aggregable == castedOther.aggregable && getCodec().isCompatibleWith( castedOther.getCodec() ); | ||
} | ||
|
||
@Override | ||
public boolean hasCompatibleConverter(LuceneFieldAggregationBuilderFactory other) { | ||
if ( !getClass().equals( other.getClass() ) ) { | ||
return false; | ||
} | ||
AbstractLuceneFieldAggregationBuilderFactory<?> castedOther = | ||
(AbstractLuceneFieldAggregationBuilderFactory<?>) other; | ||
return toFieldValueConverter.isCompatibleWith( castedOther.toFieldValueConverter ) | ||
&& fromFieldValueConverter.isCompatibleWith( castedOther.fromFieldValueConverter ); | ||
} | ||
|
||
protected abstract LuceneFieldCodec<F> getCodec(); | ||
|
||
protected void checkAggregable(String absoluteFieldPath) { | ||
if ( !aggregable ) { | ||
throw log.nonAggregableField( absoluteFieldPath, | ||
EventContexts.fromIndexFieldAbsolutePath( absoluteFieldPath ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...rch/backend/lucene/types/projection/impl/AbstractLuceneFieldProjectionBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.backend.lucene.types.projection.impl; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.Set; | ||
|
||
import org.hibernate.search.backend.lucene.logging.impl.Log; | ||
import org.hibernate.search.backend.lucene.search.projection.impl.LuceneFieldProjectionBuilder; | ||
import org.hibernate.search.backend.lucene.types.codec.impl.LuceneFieldCodec; | ||
import org.hibernate.search.engine.backend.types.converter.spi.ProjectionConverter; | ||
import org.hibernate.search.engine.reporting.spi.EventContexts; | ||
import org.hibernate.search.engine.search.common.ValueConvert; | ||
import org.hibernate.search.engine.search.projection.spi.FieldProjectionBuilder; | ||
import org.hibernate.search.util.common.logging.impl.LoggerFactory; | ||
|
||
public abstract class AbstractLuceneFieldProjectionBuilderFactory<F> implements LuceneFieldProjectionBuilderFactory { | ||
protected static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() ); | ||
|
||
protected final boolean projectable; | ||
|
||
protected final ProjectionConverter<? super F, ?> converter; | ||
protected final ProjectionConverter<? super F, F> rawConverter; | ||
|
||
protected final LuceneFieldCodec<F> codec; | ||
|
||
public AbstractLuceneFieldProjectionBuilderFactory( | ||
boolean projectable, ProjectionConverter<? super F, ?> converter, | ||
ProjectionConverter<? super F, F> rawConverter, LuceneFieldCodec<F> codec) { | ||
this.projectable = projectable; | ||
this.converter = converter; | ||
this.rawConverter = rawConverter; | ||
this.codec = codec; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") // We check the cast is legal by asking the converter | ||
public <T> FieldProjectionBuilder<T> createFieldValueProjectionBuilder(Set<String> indexNames, String absoluteFieldPath, String nestedDocumentPath, | ||
Class<T> expectedType, ValueConvert convert) { | ||
checkProjectable( absoluteFieldPath ); | ||
|
||
ProjectionConverter<? super F, ?> requestConverter = getConverter( convert ); | ||
if ( !requestConverter.isConvertedTypeAssignableTo( expectedType ) ) { | ||
throw log.invalidProjectionInvalidType( absoluteFieldPath, expectedType, | ||
EventContexts.fromIndexFieldAbsolutePath( absoluteFieldPath ) ); | ||
} | ||
|
||
return (FieldProjectionBuilder<T>) new LuceneFieldProjectionBuilder<>( | ||
indexNames, absoluteFieldPath, nestedDocumentPath, requestConverter, codec | ||
); | ||
} | ||
|
||
@Override | ||
public boolean hasCompatibleCodec(LuceneFieldProjectionBuilderFactory other) { | ||
if ( !getClass().equals( other.getClass() ) ) { | ||
return false; | ||
} | ||
AbstractLuceneFieldProjectionBuilderFactory<?> castedOther = | ||
(AbstractLuceneFieldProjectionBuilderFactory<?>) other; | ||
return projectable == castedOther.projectable && codec.isCompatibleWith( castedOther.codec ); | ||
} | ||
|
||
@Override | ||
public boolean hasCompatibleConverter(LuceneFieldProjectionBuilderFactory other) { | ||
if ( !getClass().equals( other.getClass() ) ) { | ||
return false; | ||
} | ||
AbstractLuceneFieldProjectionBuilderFactory<?> castedOther = | ||
(AbstractLuceneFieldProjectionBuilderFactory<?>) other; | ||
return converter.isCompatibleWith( castedOther.converter ); | ||
} | ||
|
||
protected void checkProjectable(String absoluteFieldPath) { | ||
if ( !projectable ) { | ||
throw log.nonProjectableField( absoluteFieldPath, | ||
EventContexts.fromIndexFieldAbsolutePath( absoluteFieldPath ) ); | ||
} | ||
} | ||
|
||
protected ProjectionConverter<? super F, ?> getConverter(ValueConvert convert) { | ||
switch ( convert ) { | ||
case NO: | ||
return rawConverter; | ||
case YES: | ||
default: | ||
return converter; | ||
} | ||
} | ||
} |
Oops, something went wrong.