Closed MichaelKunze closed 5 years ago
It should work with the following code
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSLockFactory;
import org.apache.lucene.store.SimpleFSDirectory;
import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder;
public class Demo {
public static void main(String[] args) throws Exception {
final var fileSystem = MemoryFileSystemBuilder.newEmpty().build();
final var indexPath = fileSystem.getPath("/index");
try (var directory = new SimpleFSDirectory(indexPath, FSLockFactory.getDefault());
var indexWriter = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()))
) {
indexWriter.addDocument(new Document());
}
}
}
I'll see whether I can get this fixed in Lucene upstream.
Thanks for the quick reply. I see what you did there. It almost works. IntelliJ has assertions for tests enabled by default. Therfore i run into (with -ea enabled):
Exception in thread "main" java.lang.AssertionError: On Linux and MacOSX fsyncing a directory should not throw IOException, we just don't want to rely on that in production (undocumented). Got: java.nio.file.FileSystemException: /index: file is a directory
at org.apache.lucene.util.IOUtils.fsync(IOUtils.java:464)
at org.apache.lucene.store.FSDirectory.syncMetaData(FSDirectory.java:310)
at org.apache.lucene.store.LockValidatingDirectoryWrapper.syncMetaData(LockValidatingDirectoryWrapper.java:62)
at org.apache.lucene.index.SegmentInfos.prepareCommit(SegmentInfos.java:771)
at org.apache.lucene.index.IndexWriter.startCommit(IndexWriter.java:4773)
at org.apache.lucene.index.IndexWriter.prepareCommitInternal(IndexWriter.java:3288)
at org.apache.lucene.index.IndexWriter.commitInternal(IndexWriter.java:3456)
at org.apache.lucene.index.IndexWriter.shutdown(IndexWriter.java:1037)
at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:1078)
at Demo.main(Demo.java:21)
Happens in MemoryFileSystem:372
. Is it suppose to do that?
Without "-ea" it's fine.
It's tricky. The current behavior of OpenJDK on Linux and macOS (I don't know about Unixes) is that in order to fsync()
a directory you need to open a FileChannel for reading, not read anything and then call force()
.
Unfortunately this is completely unspecified behavior. In addition while you can open a FileChannel for reading actually reading from it will fail.
See the following threads and issues:
http://mail.openjdk.java.net/pipermail/nio-dev/2015-January/002979.html (goes on until May)
https://bugs.openjdk.java.net/browse/JDK-8066915 https://bugs.openjdk.java.net/browse/JDK-8080629 https://issues.apache.org/jira/browse/LUCENE-6169
Trying to test a very simple add(Lucene)Document throws the following exception:
Code that produces the exception:
Uses:
implementation("org.apache.lucene:lucene-core:7.5.0")
implementation("com.github.marschall:memoryfilesystem:1.3.0")
Any ideas how to fix this? Thanks!