|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.search.backend.lucene; |
| 6 | + |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import org.hibernate.search.backend.lucene.logging.impl.LuceneMiscLog; |
| 10 | +import org.hibernate.search.backend.lucene.schema.management.LuceneIndexSchemaExport; |
| 11 | +import org.hibernate.search.backend.lucene.scope.LuceneIndexScope; |
| 12 | +import org.hibernate.search.backend.lucene.search.aggregation.dsl.LuceneSearchAggregationFactory; |
| 13 | +import org.hibernate.search.backend.lucene.search.predicate.dsl.LuceneSearchPredicateFactory; |
| 14 | +import org.hibernate.search.backend.lucene.search.projection.dsl.LuceneSearchProjectionFactory; |
| 15 | +import org.hibernate.search.backend.lucene.search.query.LuceneSearchQuery; |
| 16 | +import org.hibernate.search.backend.lucene.search.query.dsl.LuceneSearchQuerySelectStep; |
| 17 | +import org.hibernate.search.backend.lucene.search.query.dsl.impl.LuceneSearchQuerySelectStepImpl; |
| 18 | +import org.hibernate.search.backend.lucene.search.query.impl.LuceneSearchQueryIndexScope; |
| 19 | +import org.hibernate.search.backend.lucene.search.sort.dsl.LuceneSearchSortFactory; |
| 20 | +import org.hibernate.search.backend.lucene.types.dsl.LuceneIndexFieldTypeFactory; |
| 21 | +import org.hibernate.search.engine.backend.scope.IndexScopeExtension; |
| 22 | +import org.hibernate.search.engine.backend.scope.spi.IndexScope; |
| 23 | +import org.hibernate.search.engine.backend.session.spi.BackendSessionContext; |
| 24 | +import org.hibernate.search.engine.backend.types.dsl.IndexFieldTypeFactory; |
| 25 | +import org.hibernate.search.engine.backend.types.dsl.IndexFieldTypeFactoryExtension; |
| 26 | +import org.hibernate.search.engine.common.schema.management.SchemaExport; |
| 27 | +import org.hibernate.search.engine.common.schema.management.SchemaExportExtension; |
| 28 | +import org.hibernate.search.engine.search.aggregation.dsl.SearchAggregationFactory; |
| 29 | +import org.hibernate.search.engine.search.aggregation.dsl.SearchAggregationFactoryExtension; |
| 30 | +import org.hibernate.search.engine.search.loading.spi.SearchLoadingContext; |
| 31 | +import org.hibernate.search.engine.search.loading.spi.SearchLoadingContextBuilder; |
| 32 | +import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory; |
| 33 | +import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactoryExtension; |
| 34 | +import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory; |
| 35 | +import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactoryExtension; |
| 36 | +import org.hibernate.search.engine.search.query.SearchQuery; |
| 37 | +import org.hibernate.search.engine.search.query.SearchQueryExtension; |
| 38 | +import org.hibernate.search.engine.search.query.dsl.SearchQueryDslExtension; |
| 39 | +import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep; |
| 40 | +import org.hibernate.search.engine.search.query.spi.SearchQueryIndexScope; |
| 41 | +import org.hibernate.search.engine.search.sort.dsl.SearchSortFactory; |
| 42 | +import org.hibernate.search.engine.search.sort.dsl.SearchSortFactoryExtension; |
| 43 | + |
| 44 | +/** |
| 45 | + * An extension for the Lucene backend, giving access to Lucene-specific features. |
| 46 | + * <p> |
| 47 | + * <strong>WARNING:</strong> while this type is API, because instances should be manipulated by users, |
| 48 | + * all of its methods are considered SPIs and therefore should never be called directly by users. |
| 49 | + * In short, users are only expected to get instances of this type from an API and pass it to another API. |
| 50 | + * |
| 51 | + * @param <H> The type of query hits. |
| 52 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 53 | + * {@code .extension( LuceneExtension.get() )}. |
| 54 | + * @param <R> The entity reference type for projections. |
| 55 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 56 | + * {@code .extension( LuceneExtension.get() )}. |
| 57 | + * @param <E> entity type for projections. |
| 58 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 59 | + * {@code .extension( LuceneExtension.get() )}. |
| 60 | + * @param <LOS> The type of the initial step of the loading options definition DSL. |
| 61 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 62 | + * {@code .extension( LuceneExtension.get() )}. |
| 63 | + * |
| 64 | + * @see #get() |
| 65 | + */ |
| 66 | +public final class LuceneExtension<H, R, E, LOS> |
| 67 | + implements SearchQueryDslExtension<LuceneSearchQuerySelectStep<R, E, LOS>, R, E, LOS>, |
| 68 | + SearchQueryExtension<LuceneSearchQuery<H>, H>, |
| 69 | + SearchPredicateFactoryExtension<LuceneSearchPredicateFactory>, |
| 70 | + SearchSortFactoryExtension<LuceneSearchSortFactory>, |
| 71 | + SearchProjectionFactoryExtension<LuceneSearchProjectionFactory<R, E>, R, E>, |
| 72 | + SearchAggregationFactoryExtension<LuceneSearchAggregationFactory>, |
| 73 | + IndexFieldTypeFactoryExtension<LuceneIndexFieldTypeFactory>, |
| 74 | + IndexScopeExtension<LuceneIndexScope>, |
| 75 | + SchemaExportExtension<LuceneIndexSchemaExport> { |
| 76 | + |
| 77 | + private static final LuceneExtension<Object, Object, Object, Object> INSTANCE = new LuceneExtension<>(); |
| 78 | + |
| 79 | + /** |
| 80 | + * Get the extension with generic parameters automatically set as appropriate for the context in which it's used. |
| 81 | + * |
| 82 | + * @param <H> The type of query hits. |
| 83 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 84 | + * {@code .extension( LuceneExtension.get() }. |
| 85 | + * @param <R> The entity reference type for projections. |
| 86 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 87 | + * {@code .extension( LuceneExtension.get() }. |
| 88 | + * @param <E> entity type for projections. |
| 89 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 90 | + * {@code .extension( LuceneExtension.get() }. |
| 91 | + * @param <LOS> The type of the initial step of the loading options definition DSL. |
| 92 | + * Users should not have to care about this, as the parameter will automatically take the appropriate value when calling |
| 93 | + * {@code .extension( LuceneExtension.get() }. |
| 94 | + * @return The extension. |
| 95 | + */ |
| 96 | + @SuppressWarnings("unchecked") // The instance works for any H, R and E |
| 97 | + public static <H, R, E, LOS> LuceneExtension<H, R, E, LOS> get() { |
| 98 | + return (LuceneExtension<H, R, E, LOS>) INSTANCE; |
| 99 | + } |
| 100 | + |
| 101 | + private LuceneExtension() { |
| 102 | + // Private constructor, use get() instead. |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * {@inheritDoc} |
| 107 | + */ |
| 108 | + @Override |
| 109 | + public Optional<LuceneSearchQuerySelectStep<R, E, LOS>> extendOptional( |
| 110 | + SearchQuerySelectStep<?, R, E, LOS, ?, ?> original, |
| 111 | + SearchQueryIndexScope<?> scope, |
| 112 | + BackendSessionContext sessionContext, |
| 113 | + SearchLoadingContextBuilder<E, LOS> loadingContextBuilder) { |
| 114 | + if ( scope instanceof LuceneSearchQueryIndexScope ) { |
| 115 | + return Optional.of( new LuceneSearchQuerySelectStepImpl<>( |
| 116 | + (LuceneSearchQueryIndexScope<?>) scope, sessionContext, loadingContextBuilder |
| 117 | + ) ); |
| 118 | + } |
| 119 | + else { |
| 120 | + return Optional.empty(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * {@inheritDoc} |
| 126 | + */ |
| 127 | + @Override |
| 128 | + public Optional<LuceneSearchQuery<H>> extendOptional(SearchQuery<H> original, |
| 129 | + SearchLoadingContext<?> loadingContext) { |
| 130 | + if ( original instanceof LuceneSearchQuery ) { |
| 131 | + return Optional.of( (LuceneSearchQuery<H>) original ); |
| 132 | + } |
| 133 | + else { |
| 134 | + return Optional.empty(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * {@inheritDoc} |
| 140 | + */ |
| 141 | + @Override |
| 142 | + public Optional<LuceneSearchPredicateFactory> extendOptional(SearchPredicateFactory original) { |
| 143 | + if ( original instanceof LuceneSearchPredicateFactory ) { |
| 144 | + return Optional.of( (LuceneSearchPredicateFactory) original ); |
| 145 | + } |
| 146 | + else { |
| 147 | + return Optional.empty(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * {@inheritDoc} |
| 153 | + */ |
| 154 | + @Override |
| 155 | + public Optional<LuceneSearchSortFactory> extendOptional( |
| 156 | + SearchSortFactory original) { |
| 157 | + if ( original instanceof LuceneSearchSortFactory ) { |
| 158 | + return Optional.of( (LuceneSearchSortFactory) original ); |
| 159 | + } |
| 160 | + else { |
| 161 | + return Optional.empty(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * {@inheritDoc} |
| 167 | + */ |
| 168 | + @Override |
| 169 | + public Optional<LuceneSearchProjectionFactory<R, E>> extendOptional(SearchProjectionFactory<R, E> original) { |
| 170 | + if ( original instanceof LuceneSearchProjectionFactory ) { |
| 171 | + return Optional.of( (LuceneSearchProjectionFactory<R, E>) original ); |
| 172 | + } |
| 173 | + else { |
| 174 | + return Optional.empty(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * {@inheritDoc} |
| 180 | + */ |
| 181 | + @Override |
| 182 | + public Optional<LuceneSearchAggregationFactory> extendOptional(SearchAggregationFactory original) { |
| 183 | + if ( original instanceof LuceneSearchAggregationFactory ) { |
| 184 | + return Optional.of( (LuceneSearchAggregationFactory) original ); |
| 185 | + } |
| 186 | + else { |
| 187 | + return Optional.empty(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * {@inheritDoc} |
| 193 | + */ |
| 194 | + @Override |
| 195 | + public LuceneIndexFieldTypeFactory extendOrFail(IndexFieldTypeFactory original) { |
| 196 | + if ( original instanceof LuceneIndexFieldTypeFactory ) { |
| 197 | + return (LuceneIndexFieldTypeFactory) original; |
| 198 | + } |
| 199 | + else { |
| 200 | + throw LuceneMiscLog.INSTANCE.luceneExtensionOnUnknownType( original ); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * {@inheritDoc} |
| 206 | + */ |
| 207 | + @Override |
| 208 | + public LuceneIndexScope extendOrFail(IndexScope original) { |
| 209 | + if ( original instanceof LuceneIndexScope ) { |
| 210 | + return (LuceneIndexScope) original; |
| 211 | + } |
| 212 | + else { |
| 213 | + throw LuceneMiscLog.INSTANCE.luceneExtensionOnUnknownType( original ); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * {@inheritDoc} |
| 219 | + */ |
| 220 | + @Override |
| 221 | + public LuceneIndexSchemaExport extendOrFail(SchemaExport original) { |
| 222 | + if ( original instanceof LuceneIndexSchemaExport ) { |
| 223 | + return (LuceneIndexSchemaExport) original; |
| 224 | + } |
| 225 | + else { |
| 226 | + throw LuceneMiscLog.INSTANCE.luceneExtensionOnUnknownType( original ); |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments