-
Notifications
You must be signed in to change notification settings - Fork 242
/
HibernateOrmAutomaticIndexingTypeFilterContext.java
149 lines (129 loc) · 5.44 KB
/
HibernateOrmAutomaticIndexingTypeFilterContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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.orm.automaticindexing.filter.impl;
import java.lang.invoke.MethodHandles;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSessionTypeContextProvider;
import org.hibernate.search.mapper.pojo.automaticindexing.filter.PojoAutomaticIndexingTypeFilterContext;
import org.hibernate.search.mapper.pojo.automaticindexing.filter.spi.PojoAutomaticIndexingTypeFilterHolder;
import org.hibernate.search.mapper.pojo.logging.impl.Log;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;
import org.hibernate.search.mapper.pojo.model.spi.PojoTypeContext;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
public class HibernateOrmAutomaticIndexingTypeFilterContext implements PojoAutomaticIndexingTypeFilterContext {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private final HibernateOrmSessionTypeContextProvider contextProvider;
private final Set<PojoRawTypeIdentifier<?>> includes = new HashSet<>();
private final Set<PojoRawTypeIdentifier<?>> excludes = new HashSet<>();
public HibernateOrmAutomaticIndexingTypeFilterContext(HibernateOrmSessionTypeContextProvider typeManager) {
this.contextProvider = typeManager;
}
@Override
public PojoAutomaticIndexingTypeFilterContext include(String name) {
addIfNotPresentInOther(
contextProvider.byEntityName().getOrFail( name ).typeIdentifier(),
includes,
excludes
);
return this;
}
@Override
public PojoAutomaticIndexingTypeFilterContext include(Class<?> clazz) {
addIfNotPresentInOther(
contextProvider.typeIdentifierResolver().resolveByJavaClass( clazz ),
includes,
excludes
);
return this;
}
@Override
public PojoAutomaticIndexingTypeFilterContext exclude(String name) {
addIfNotPresentInOther(
contextProvider.byEntityName().getOrFail( name ).typeIdentifier(),
excludes,
includes
);
return this;
}
@Override
public PojoAutomaticIndexingTypeFilterContext exclude(Class<?> clazz) {
addIfNotPresentInOther(
contextProvider.typeIdentifierResolver().resolveByJavaClass( clazz ),
excludes,
includes
);
return this;
}
public HibernateOrmAutomaticIndexingTypeFilter createFilter(PojoAutomaticIndexingTypeFilterHolder fallback) {
Set<PojoRawTypeIdentifier<?>> allIncludes = new HashSet<>();
Set<PojoRawTypeIdentifier<?>> allExcludes = new HashSet<>();
for ( PojoTypeContext<?> typeContext : contextProvider.byEntityName().values() ) {
PojoRawTypeIdentifier<?> typedIdentifier = typeContext.typeIdentifier();
//.include(...) => included no matter what; subclasses included unless excluded explicitly in the same filter
//.exclude(...) => excluded no matter what; subclasses excluded unless included explicitly in the same filter
//.include(...) + .exclude(...) for the same entity in the same filter => exception
// neither .include(...) nor .exclude(...) => defer to the superclass inclusions/exclusions,
// then to the application filter, with the same behavior, and by default include
if ( excludes.contains( typedIdentifier ) ) {
allExcludes.add( typedIdentifier );
}
else if ( includes.contains( typedIdentifier ) ) {
allIncludes.add( typedIdentifier );
}
else {
Optional<Class<?>> closestInclude = findClosestClass( typedIdentifier.javaClass(), includes );
Optional<Class<?>> closestExclude = findClosestClass( typedIdentifier.javaClass(), excludes );
if ( closestInclude.isPresent() && closestExclude.isPresent() ) {
// if include is a subclass of exclude - then include is more specific, and we allow indexing:
if ( closestExclude.get().isAssignableFrom( closestInclude.get() ) ) {
allIncludes.add( typedIdentifier );
}
else {
allExcludes.add( typedIdentifier );
}
}
else if ( closestExclude.isPresent() ) {
allExcludes.add( typedIdentifier );
}
else if ( closestInclude.isPresent() ) {
allIncludes.add( typedIdentifier );
}
// if we don't find either include or exclude then we will defer the decision to either app filter or to a default (include)
// but that will happen in the filter.
}
}
return new HibernateOrmAutomaticIndexingTypeFilter(
fallback,
Collections.unmodifiableSet( allIncludes ),
Collections.unmodifiableSet( allExcludes )
);
}
private Optional<Class<?>> findClosestClass(Class<?> current, Set<PojoRawTypeIdentifier<?>> collection) {
Class<?> closest = Object.class;
for ( PojoRawTypeIdentifier<?> identifier : collection ) {
if ( identifier.javaClass().isAssignableFrom( current ) ) {
closest = identifier.javaClass().isAssignableFrom( closest ) ? closest : identifier.javaClass();
}
}
if ( Object.class.equals( closest ) ) {
return Optional.empty();
}
else {
return Optional.of( closest );
}
}
private boolean addIfNotPresentInOther(PojoRawTypeIdentifier<?> typeIdentifier, Set<PojoRawTypeIdentifier<?>> a,
Set<PojoRawTypeIdentifier<?>> b) {
if ( b.contains( typeIdentifier ) ) {
throw log.automaticIndexingFilterCannotIncludeExcludeSameType( typeIdentifier, includes, excludes );
}
return a.add( typeIdentifier );
}
}