-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LUCENE-8739: custom codec providing Zstandard compression/decompression #439
Open
praveennish
wants to merge
13
commits into
apache:main
Choose a base branch
from
praveennish:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f7ef49a
LUCENE-8739:custom codec providing zstandard compression/decompression
praveennish a6510b4
LUCENE-8739: custom codec providing Zstandard compression/decompressi…
praveennish eb9eb07
Merge branch 'apache:main' into main
praveennish 999bdb7
Merge branch 'apache:main' into main
praveennish 01c20f3
codecs build gradle conflict resolution
praveennish a758401
Merge branch 'apache-main' into main
praveennish ef5a14e
sync up with apache lucene Dec 27th
praveennish 1cf75c4
Merge branch 'apache-main' into main
praveennish 4ea6aa4
Merge branch 'apache:main' into main
praveennish f638e67
LUCENE-8739: custom codec providing Zstandard compression/decompressi…
praveennish 85fa2b6
Merge branch 'main' of https://github.com/praveennish/lucene into main
praveennish 7fe7d91
merging changes on january 7 2022
praveennish a119cca
LUCENE-8739: custom codec providing Zstandard compression/decompressi…
praveennish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
83 changes: 83 additions & 0 deletions
83
...s/src/java/org/apache/lucene/codecs/customcompression/Lucene90CustomCompressionCodec.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,83 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.codecs.customcompression; | ||
|
||
import org.apache.lucene.codecs.FilterCodec; | ||
import org.apache.lucene.codecs.StoredFieldsFormat; | ||
import org.apache.lucene.codecs.lucene90.Lucene90Codec; | ||
|
||
/** Custom codec for different compression algorithm */ | ||
public final class Lucene90CustomCompressionCodec extends FilterCodec { | ||
|
||
private final StoredFieldsFormat storedFieldsFormat; | ||
private int compressionLevel; | ||
public static final int defaultCompressionLevel = 6; | ||
|
||
/** Compression modes */ | ||
public static enum Mode { | ||
|
||
// Currently Zstandard is supported, other compression algorithms can be implemented and | ||
// respective modes can be added for e.g. ZSTD, ZSTD_DICT, ZSTD_FAST or any other compression | ||
// algos | ||
|
||
// Zstandard without dictionary | ||
ZSTD, | ||
// Zstandard with dictionary | ||
ZSTD_DICT | ||
} | ||
/** Default codec */ | ||
public Lucene90CustomCompressionCodec() { | ||
this(Mode.ZSTD_DICT, defaultCompressionLevel); | ||
} | ||
|
||
/** new codec for a given compression algorithm and compression level */ | ||
public Lucene90CustomCompressionCodec(Mode compressionMode, int compressionLevel) { | ||
super("Lucene90CustomCompression", new Lucene90Codec()); | ||
this.compressionLevel = compressionLevel; | ||
|
||
switch (compressionMode) { | ||
case ZSTD: | ||
if (this.compressionLevel < 1 || this.compressionLevel > 22) | ||
throw new IllegalArgumentException("Invalid compression level"); | ||
|
||
this.storedFieldsFormat = | ||
new Lucene90CustomCompressionStoredFieldsFormat(Mode.ZSTD, compressionLevel); | ||
break; | ||
|
||
case ZSTD_DICT: | ||
if (this.compressionLevel < 1 || this.compressionLevel > 22) | ||
throw new IllegalArgumentException("Invalid compression level"); | ||
|
||
this.storedFieldsFormat = | ||
new Lucene90CustomCompressionStoredFieldsFormat(Mode.ZSTD_DICT, compressionLevel); | ||
break; | ||
|
||
default: | ||
throw new IllegalArgumentException("Chosen compression mode does not exist"); | ||
} | ||
} | ||
|
||
@Override | ||
public StoredFieldsFormat storedFieldsFormat() { | ||
return storedFieldsFormat; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName(); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...g/apache/lucene/codecs/customcompression/Lucene90CustomCompressionStoredFieldsFormat.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,102 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.codecs.customcompression; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import org.apache.lucene.codecs.StoredFieldsFormat; | ||
import org.apache.lucene.codecs.StoredFieldsReader; | ||
import org.apache.lucene.codecs.StoredFieldsWriter; | ||
import org.apache.lucene.codecs.compressing.CompressionMode; | ||
import org.apache.lucene.codecs.lucene90.compressing.Lucene90CompressingStoredFieldsFormat; | ||
import org.apache.lucene.index.FieldInfos; | ||
import org.apache.lucene.index.SegmentInfo; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.store.IOContext; | ||
|
||
/** Stored field format used by plugaable codec */ | ||
public class Lucene90CustomCompressionStoredFieldsFormat extends StoredFieldsFormat { | ||
// chunk size for zstandard | ||
private static final int ZSTD_BLOCK_LENGTH = 10 * 48 * 1024; | ||
|
||
public static final String MODE_KEY = | ||
Lucene90CustomCompressionStoredFieldsFormat.class.getSimpleName() + ".mode"; | ||
|
||
final Lucene90CustomCompressionCodec.Mode mode; | ||
|
||
private int compressionLevel; | ||
|
||
/** default constructor */ | ||
public Lucene90CustomCompressionStoredFieldsFormat() { | ||
this( | ||
Lucene90CustomCompressionCodec.Mode.ZSTD_DICT, | ||
Lucene90CustomCompressionCodec.defaultCompressionLevel); | ||
} | ||
|
||
/** Stored fields format with specified compression algo. */ | ||
public Lucene90CustomCompressionStoredFieldsFormat( | ||
Lucene90CustomCompressionCodec.Mode mode, int compressionLevel) { | ||
this.mode = Objects.requireNonNull(mode); | ||
this.compressionLevel = compressionLevel; | ||
} | ||
|
||
@Override | ||
public StoredFieldsReader fieldsReader( | ||
Directory directory, SegmentInfo si, FieldInfos fn, IOContext context) throws IOException { | ||
String value = si.getAttribute(MODE_KEY); | ||
if (value == null) { | ||
throw new IllegalStateException("missing value for " + MODE_KEY + " for segment: " + si.name); | ||
} | ||
Lucene90CustomCompressionCodec.Mode mode = Lucene90CustomCompressionCodec.Mode.valueOf(value); | ||
return impl(mode).fieldsReader(directory, si, fn, context); | ||
} | ||
|
||
@Override | ||
public StoredFieldsWriter fieldsWriter(Directory directory, SegmentInfo si, IOContext context) | ||
throws IOException { | ||
String previous = si.putAttribute(MODE_KEY, mode.name()); | ||
if (previous != null && previous.equals(mode.name()) == false) { | ||
throw new IllegalStateException( | ||
"found existing value for " | ||
+ MODE_KEY | ||
+ " for segment: " | ||
+ si.name | ||
+ "old=" | ||
+ previous | ||
+ ", new=" | ||
+ mode.name()); | ||
} | ||
return impl(mode).fieldsWriter(directory, si, context); | ||
} | ||
|
||
StoredFieldsFormat impl(Lucene90CustomCompressionCodec.Mode mode) { | ||
switch (mode) { | ||
case ZSTD: | ||
return new Lucene90CompressingStoredFieldsFormat( | ||
"CustomCompressionStoredFieldsZSTD", ZSTD_MODE_NO_DICT, ZSTD_BLOCK_LENGTH, 4096, 10); | ||
|
||
case ZSTD_DICT: | ||
return new Lucene90CompressingStoredFieldsFormat( | ||
"CustomCompressionStoredFieldsZSTD", ZSTD_MODE_DICT, ZSTD_BLOCK_LENGTH, 4096, 10); | ||
default: | ||
throw new AssertionError(); | ||
} | ||
} | ||
|
||
public final CompressionMode ZSTD_MODE_NO_DICT = new ZstdCompressionMode(compressionLevel); | ||
public final CompressionMode ZSTD_MODE_DICT = new ZstdDictCompressionMode(compressionLevel); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just minor suggestion:
current version is
1.5.1-1
could bring another few percent compression speed
https://github.com/facebook/zstd/releases/tag/v1.5.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Tobias,
I have tested 1.5.0-X and the data looks to be very promising with the current version. I have not tested 1.5.1-X JNI version but https://github.com/facebook/zstd/releases/tag/v1.5.1 looks good and can be picked up in the future since Zstd is very dynamic and we may see a lot of improvements over the current version!
Thanks!!