Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ dependencies {
}
```

To upgrade an index all the way to version 8:
To upgrade an index all the way to version 9:

```java
new IndexUpgrader(textIndexPath, null)
.upgradeTo(LuceneVersion.VERSION_8);
.upgradeTo(LuceneVersion.VERSION_9);
```

The upgrade will be a no-op if the index is already at that version.
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include("testgen:lucene5")
include("testgen:lucene6")
include("testgen:lucene7")
include("testgen:lucene8")
include("testgen:lucene9")

dependencyResolutionManagement {
repositories {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/trypticon/luceneupgrader/LuceneVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.trypticon.luceneupgrader.lucene6.VersionUpgrader6;
import org.trypticon.luceneupgrader.lucene7.VersionUpgrader7;
import org.trypticon.luceneupgrader.lucene8.VersionUpgrader8;
import org.trypticon.luceneupgrader.lucene9.VersionUpgrader9;

import javax.annotation.Nonnull;
import java.nio.file.Path;
Expand Down Expand Up @@ -69,6 +70,13 @@ protected VersionUpgrader createUpgrader(@Nonnull Path directory, @Nonnull InfoS
protected VersionUpgrader createUpgrader(@Nonnull Path directory, @Nonnull InfoStream infoStream) {
return new VersionUpgrader8(directory, infoStream);
}
},

VERSION_9(9) {
@Override
protected VersionUpgrader createUpgrader(@Nonnull Path directory, @Nonnull InfoStream infoStream) {
return new VersionUpgrader9(directory, infoStream);
}
};

private final int number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public LuceneVersion guess(@Nonnull Directory directory) throws IOException {
throw new UnknownFormatException("Appears to be like version 5-6 but major version " +
"is unrecognised: " + majorVersion);
}
} else if (actualVersion >= 7 && actualVersion <= 10) { // VERSION_70 thru VERSION_86
} else if (actualVersion >= 7 && actualVersion <= 11) { // VERSION_70 thru VERSION_9.
// Skip over 16-byte ID.
segments.skipBytes(16);

Expand All @@ -102,8 +102,10 @@ public LuceneVersion guess(@Nonnull Directory directory) throws IOException {
return LuceneVersion.VERSION_7;
case 8:
return LuceneVersion.VERSION_8;
case 9:
return LuceneVersion.VERSION_9;
default:
throw new UnknownFormatException("Appears to be like version 6-8 but major version " +
throw new UnknownFormatException("Appears to be like version 6-9 but major version " +
"is unrecognised: " + createdVersion);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.trypticon.luceneupgrader.lucene9;

import org.trypticon.luceneupgrader.lucene9.internal.lucene.analysis.Analyzer;
import org.trypticon.luceneupgrader.lucene9.internal.lucene.index.*;
import org.trypticon.luceneupgrader.lucene9.internal.lucene.store.Directory;
import org.trypticon.luceneupgrader.lucene9.internal.lucene.store.FSDirectory;
import org.trypticon.luceneupgrader.lucene9.internal.lucene.util.Version;
import org.trypticon.luceneupgrader.FileUtils;
import org.trypticon.luceneupgrader.InfoStream;
import org.trypticon.luceneupgrader.VersionUpgrader;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Upgrades an index to Lucene 9 format.
*/
public class VersionUpgrader9 implements VersionUpgrader {

@Nonnull
private final Path path;

@Nonnull
private final InfoStream infoStream;

public VersionUpgrader9(@Nonnull Path path, @Nonnull InfoStream infoStream) {
this.path = path;
this.infoStream = infoStream;
}

@Override
public void upgrade() throws IOException {
Path oldPath = path.resolveSibling(path.getFileName() + ".old");
Path tempPath = path.resolveSibling(path.getFileName() + ".tmp");

FileUtils.insecureRecursiveDelete(tempPath);
Files.createDirectory(tempPath);

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new FailAnalyzer());
indexWriterConfig.setMergePolicy(new LogByteSizeMergePolicy());
indexWriterConfig.setMergeScheduler(new SerialMergeScheduler());
indexWriterConfig.setInfoStream(new AdaptedInfoStream(infoStream));
indexWriterConfig.setIndexCreatedVersionMajor(9);

try (Directory sourceDirectory = FSDirectory.open(path);
Directory destinationDirectory = FSDirectory.open(tempPath);
IndexReader reader = DirectoryReader.open(sourceDirectory);
IndexWriter writer = new IndexWriter(destinationDirectory, indexWriterConfig)) {

CodecReader[] codecReaders = reader.leaves().stream()
.map(context -> new VersionOverridingCodecReader((CodecReader) context.reader()))
.toArray(CodecReader[]::new);

writer.addIndexes(codecReaders);
writer.commit();
}

Files.move(path, oldPath);
Files.move(tempPath, path);
FileUtils.insecureRecursiveDelete(oldPath);
}

/**
* Adapts Lucene's info stream to pass messages to ours.
*/
private static class AdaptedInfoStream
extends org.trypticon.luceneupgrader.lucene9.internal.lucene.util.InfoStream {
private final InfoStream infoStream;

private AdaptedInfoStream(InfoStream infoStream) {
this.infoStream = infoStream;
}

@Override
public void message(String component, String message) {
infoStream.message(component, message);
}

@Override
public boolean isEnabled(String component) {
return infoStream.isEnabled(component);
}

@Override
public void close() throws IOException {
//
}
}

private static class VersionOverridingCodecReader extends FilterCodecReader {
private final LeafMetaData metadata;

private VersionOverridingCodecReader(CodecReader in) {
super(in);

LeafMetaData superMetadata = super.getMetaData();
metadata = new LeafMetaData(9, Version.LUCENE_9_12_2, superMetadata.getSort(), superMetadata.hasBlocks());
}

@Override
public LeafMetaData getMetaData() {
return metadata;
}

@Override
public CacheHelper getReaderCacheHelper() {
return in.getReaderCacheHelper();
}

@Override
public CacheHelper getCoreCacheHelper() {
return in.getCoreCacheHelper();
}
}

/**
* An analyser which deliberately fails, because we don't want to be analysing text at all.
*/
private static class FailAnalyzer extends Analyzer {
@Override
protected TokenStreamComponents createComponents(String s) {
throw new UnsupportedOperationException("This analyser isn't supported for indexing");
}
}
}
Loading
Loading