-
Notifications
You must be signed in to change notification settings - Fork 242
/
PojoDefaultMassIndexer.java
205 lines (181 loc) · 7.04 KB
/
PojoDefaultMassIndexer.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* 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.massindexing.impl;
import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import org.hibernate.search.engine.backend.session.spi.DetachedBackendSessionContext;
import org.hibernate.search.mapper.pojo.logging.impl.Log;
import org.hibernate.search.mapper.pojo.massindexing.MassIndexingFailureHandler;
import org.hibernate.search.mapper.pojo.massindexing.MassIndexingMonitor;
import org.hibernate.search.mapper.pojo.massindexing.spi.PojoMassIndexer;
import org.hibernate.search.mapper.pojo.massindexing.spi.PojoMassIndexingContext;
import org.hibernate.search.mapper.pojo.massindexing.spi.PojoMassIndexingMappingContext;
import org.hibernate.search.mapper.pojo.schema.management.spi.PojoScopeSchemaManager;
import org.hibernate.search.mapper.pojo.scope.spi.PojoScopeDelegate;
import org.hibernate.search.util.common.impl.Futures;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
/**
* Prepares and configures a BatchIndexingWorkspace to start rebuilding
* the indexes for all entity instances in the database.The type of these entities is either all indexed entities or a
* subset, always including all subtypes.
*
* @author Sanne Grinovero
*/
public class PojoDefaultMassIndexer implements PojoMassIndexer {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private final PojoMassIndexingContext indexingContext;
private final PojoMassIndexingMappingContext mappingContext;
private final PojoMassIndexingTypeContextProvider typeContextProvider;
private final Set<? extends PojoMassIndexingIndexedTypeContext<?>> targetedIndexedTypes;
private final PojoScopeSchemaManager scopeSchemaManager;
private final Collection<DetachedBackendSessionContext> detachedSessions;
private final PojoScopeDelegate<?, ?, ?> pojoScopeDelegate;
// default settings defined here:
private int typesToIndexInParallel = 1;
private int documentBuilderThreads = 6;
private boolean mergeSegmentsOnFinish = false;
private Boolean dropAndCreateSchemaOnStart;
private Boolean purgeAtStart;
private boolean mergeSegmentsAfterPurge = true;
private MassIndexingFailureHandler failureHandler;
private MassIndexingMonitor monitor;
public PojoDefaultMassIndexer(PojoMassIndexingContext indexingContext,
PojoMassIndexingMappingContext mappingContext,
PojoMassIndexingTypeContextProvider typeContextProvider,
Set<? extends PojoMassIndexingIndexedTypeContext<?>> targetedIndexedTypes,
PojoScopeSchemaManager scopeSchemaManager,
Collection<DetachedBackendSessionContext> detachedSessions,
PojoScopeDelegate<?, ?, ?> pojoScopeDelegate) {
this.indexingContext = indexingContext;
this.mappingContext = mappingContext;
this.typeContextProvider = typeContextProvider;
this.targetedIndexedTypes = targetedIndexedTypes;
this.scopeSchemaManager = scopeSchemaManager;
this.detachedSessions = detachedSessions;
this.pojoScopeDelegate = pojoScopeDelegate;
}
@Override
public PojoDefaultMassIndexer typesToIndexInParallel(int numberOfThreads) {
if ( numberOfThreads < 1 ) {
throw new IllegalArgumentException( "numberOfThreads must be at least 1" );
}
this.typesToIndexInParallel = numberOfThreads;
return this;
}
@Override
public PojoDefaultMassIndexer threadsToLoadObjects(int numberOfThreads) {
if ( numberOfThreads < 1 ) {
throw new IllegalArgumentException( "numberOfThreads must be at least 1" );
}
this.documentBuilderThreads = numberOfThreads;
return this;
}
@Override
public PojoDefaultMassIndexer mergeSegmentsOnFinish(boolean enable) {
this.mergeSegmentsOnFinish = enable;
return this;
}
@Override
public PojoDefaultMassIndexer mergeSegmentsAfterPurge(boolean enable) {
this.mergeSegmentsAfterPurge = enable;
return this;
}
@Override
public PojoDefaultMassIndexer dropAndCreateSchemaOnStart(boolean enable) {
this.dropAndCreateSchemaOnStart = enable;
return this;
}
@Override
public PojoDefaultMassIndexer purgeAllOnStart(boolean enable) {
this.purgeAtStart = enable;
return this;
}
@Override
public PojoDefaultMassIndexer monitor(MassIndexingMonitor monitor) {
this.monitor = monitor;
return this;
}
@Override
public CompletableFuture<?> start() {
PojoMassIndexingBatchCoordinator coordinator = createCoordinator();
ExecutorService executor = mappingContext.threadPoolProvider()
.newFixedThreadPool( 1,
PojoMassIndexingBatchIndexingWorkspace.THREAD_NAME_PREFIX + "Coordinator" );
try {
return Futures.runAsync( coordinator, executor );
}
finally {
executor.shutdown();
}
}
@Override
public void startAndWait() throws InterruptedException {
PojoMassIndexingBatchCoordinator coordinator = createCoordinator();
try {
coordinator.run();
}
catch (Throwable t) {
if ( Thread.interrupted() ) {
InterruptedException exception = new InterruptedException();
exception.addSuppressed( t );
throw exception;
}
else {
throw t;
}
}
}
private PojoMassIndexingBatchCoordinator createCoordinator() {
List<PojoMassIndexingIndexedTypeGroup<?>> typeGroupsToIndex = PojoMassIndexingIndexedTypeGroup.disjoint(
indexingContext, mappingContext, typeContextProvider, targetedIndexedTypes
);
typesToIndexInParallel = Math.min( typesToIndexInParallel, typeGroupsToIndex.size() );
PojoMassIndexingNotifier notifier = new PojoMassIndexingNotifier(
getOrCreateFailureHandler(),
getOrCreateMonitor()
);
if ( Boolean.TRUE.equals( dropAndCreateSchemaOnStart ) && Boolean.TRUE.equals( purgeAtStart ) ) {
log.redundantPurgeAfterDrop();
}
return new PojoMassIndexingBatchCoordinator(
mappingContext,
notifier,
typeGroupsToIndex, scopeSchemaManager,
detachedSessions, pojoScopeDelegate,
typesToIndexInParallel, documentBuilderThreads,
mergeSegmentsOnFinish,
// false by default:
Boolean.TRUE.equals( dropAndCreateSchemaOnStart ),
// false if not set explicitly and dropAndCreateSchemaOnStart is set to true, otherwise true by default:
purgeAtStart == null ? !Boolean.TRUE.equals( dropAndCreateSchemaOnStart ) : purgeAtStart,
mergeSegmentsAfterPurge
);
}
@Override
public PojoDefaultMassIndexer failureHandler(MassIndexingFailureHandler failureHandler) {
this.failureHandler = failureHandler;
return this;
}
private MassIndexingFailureHandler getOrCreateFailureHandler() {
MassIndexingFailureHandler result = failureHandler;
if ( result == null ) {
result = new PojoMassIndexingDelegatingFailureHandler( mappingContext.failureHandler() );
}
result = new PojoMassIndexingFailSafeFailureHandlerWrapper( result );
return result;
}
private MassIndexingMonitor getOrCreateMonitor() {
if ( monitor != null ) {
return monitor;
}
return new PojoMassIndexingLoggingMonitor();
}
}