Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3775 Move the creation of the index accessor to a new IOStrat…
…egy class
- Loading branch information
Showing
5 changed files
with
99 additions
and
42 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
...ene/src/main/java/org/hibernate/search/backend/lucene/lowlevel/index/impl/IOStrategy.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,72 @@ | ||
/* | ||
* 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.lowlevel.index.impl; | ||
|
||
import java.util.Optional; | ||
|
||
import org.hibernate.search.backend.lucene.lowlevel.directory.impl.DirectoryCreationContextImpl; | ||
import org.hibernate.search.backend.lucene.lowlevel.directory.spi.DirectoryCreationContext; | ||
import org.hibernate.search.backend.lucene.lowlevel.directory.spi.DirectoryHolder; | ||
import org.hibernate.search.backend.lucene.lowlevel.directory.spi.DirectoryProvider; | ||
import org.hibernate.search.backend.lucene.lowlevel.writer.impl.IndexWriterProvider; | ||
import org.hibernate.search.engine.environment.thread.spi.ThreadPoolProvider; | ||
import org.hibernate.search.engine.reporting.FailureHandler; | ||
import org.hibernate.search.engine.reporting.spi.EventContexts; | ||
import org.hibernate.search.util.common.impl.SuppressingCloser; | ||
import org.hibernate.search.util.common.reporting.EventContext; | ||
|
||
import org.apache.lucene.analysis.Analyzer; | ||
|
||
public final class IOStrategy { | ||
|
||
private final DirectoryProvider directoryProvider; | ||
private final ThreadPoolProvider threadPoolProvider; | ||
private final FailureHandler failureHandler; | ||
|
||
public IOStrategy(DirectoryProvider directoryProvider, ThreadPoolProvider threadPoolProvider, | ||
FailureHandler failureHandler) { | ||
this.directoryProvider = directoryProvider; | ||
this.threadPoolProvider = threadPoolProvider; | ||
this.failureHandler = failureHandler; | ||
} | ||
|
||
public IndexAccessorImpl createIndexAccessor(String indexName, EventContext eventContext, | ||
Optional<String> shardId, Analyzer analyzer) { | ||
DirectoryHolder directoryHolder; | ||
DirectoryCreationContext context = new DirectoryCreationContextImpl( | ||
shardId.isPresent() ? EventContexts.fromShardId( shardId.get() ) : null, | ||
indexName, | ||
shardId | ||
); | ||
directoryHolder = directoryProvider.createDirectoryHolder( context ); | ||
IndexWriterProvider indexWriterProvider = null; | ||
try { | ||
indexWriterProvider = createIndexWriterProvider( indexName, eventContext, analyzer, directoryHolder ); | ||
return new IndexAccessorImpl( | ||
eventContext, | ||
directoryHolder, indexWriterProvider | ||
); | ||
} | ||
catch (RuntimeException e) { | ||
new SuppressingCloser( e ) | ||
.push( IndexWriterProvider::clear, indexWriterProvider ) | ||
.push( directoryHolder ); | ||
throw e; | ||
} | ||
} | ||
|
||
private IndexWriterProvider createIndexWriterProvider(String indexName, EventContext eventContext, Analyzer analyzer, | ||
DirectoryHolder directoryHolder) { | ||
return new IndexWriterProvider( | ||
indexName, eventContext, | ||
directoryHolder, analyzer, | ||
threadPoolProvider.getThreadProvider(), | ||
failureHandler | ||
); | ||
} | ||
|
||
} |
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