Skip to content

Commit

Permalink
HSEARCH-3118 Ensure the index structure is initialized by MMapDirecto…
Browse files Browse the repository at this point in the history
…ryProvider.createDirectory

We'll need that in the following commits, because the index writer will
be created lazily, so the index files won't be created on startup
anymore, which means in particular that someone trying to query a brand
new index will get exceptions because of missing files.
  • Loading branch information
yrodiere committed May 22, 2019
1 parent 9450c4c commit 844279a
Showing 1 changed file with 11 additions and 22 deletions.
Expand Up @@ -7,20 +7,16 @@
package org.hibernate.search.backend.lucene.lowlevel.directory.impl;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;

import org.hibernate.search.backend.lucene.logging.impl.Log;
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.hibernate.search.util.common.logging.impl.LoggerFactory;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.MMapDirectory;

public class MMapDirectoryProvider implements DirectoryProvider {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

private final EventContext backendContext;

private final Path rootDirectory;
Expand All @@ -40,24 +36,17 @@ public String toString() {

@Override
public Directory createDirectory(String indexName) throws IOException {
EventContext indexContext = backendContext.append( EventContexts.fromIndexName( indexName ) );
Path directoryPath = rootDirectory.resolve( indexName );
initializeIndexDirectory( directoryPath );
return new MMapDirectory( directoryPath );
}

private void initializeIndexDirectory(Path indexDirectory) {
if ( Files.exists( indexDirectory ) ) {
if ( !Files.isDirectory( indexDirectory ) || !Files.isWritable( indexDirectory ) ) {
throw log.localDirectoryIndexRootDirectoryNotWritableDirectory( indexDirectory, backendContext );
}
DirectoryHelper.makeSanityCheckedFilesystemDirectory( directoryPath, indexContext );
Directory directory = new MMapDirectory( directoryPath );
try {
DirectoryHelper.initializeIndexIfNeeded( directory, indexContext );
}
else {
try {
Files.createDirectories( indexDirectory );
}
catch (Exception e) {
throw log.unableToCreateIndexRootDirectoryForLocalDirectoryBackend( indexDirectory, backendContext, e );
}
catch (IOException | RuntimeException e) {
new SuppressingCloser( e ).push( directory );
throw e;
}
return directory;
}
}

0 comments on commit 844279a

Please sign in to comment.