Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.luben.zstd.ZstdDictCompress;
import com.github.luben.zstd.ZstdDictDecompress;
import org.apache.cassandra.io.compress.ZstdCompressorBase;
import org.apache.cassandra.io.compress.ZstdDictionaryCompressor;
import org.apache.cassandra.utils.concurrent.Ref;
import org.apache.cassandra.utils.concurrent.RefCounted;
import org.apache.cassandra.utils.concurrent.SelfRefCounted;
Expand Down Expand Up @@ -113,7 +114,7 @@ public ZstdDictCompress dictionaryForCompression(int compressionLevel)
if (closed.get())
throw new IllegalStateException("Dictionary has been closed. " + dictId);

ZstdCompressorBase.validateCompressionLevel(compressionLevel);
ZstdCompressorBase.validateCompressionLevel(compressionLevel, ZstdDictionaryCompressor.BEST_COMPRESSION_LEVEL);

return zstdDictCompressPerLevel.computeIfAbsent(compressionLevel, level -> {
if (closed.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.github.luben.zstd.Zstd;

/**
* ZSTD Compressor
*/
public class ZstdCompressor extends ZstdCompressorBase implements ICompressor
{
public static final int BEST_COMPRESSION_LEVEL = Zstd.maxCompressionLevel();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for making ZstdCompressor backward compatible!

Maybe add a comment to explain why there are 2 different BEST_COMPRESSION_LEVEL

private static final ConcurrentHashMap<Integer, ZstdCompressor> instances = new ConcurrentHashMap<>();

/**
Expand All @@ -39,7 +42,7 @@ public class ZstdCompressor extends ZstdCompressorBase implements ICompressor
public static ZstdCompressor create(Map<String, String> options)
{
int level = getOrDefaultCompressionLevel(options);
validateCompressionLevel(level);
validateCompressionLevel(level, BEST_COMPRESSION_LEVEL);
return getOrCreate(level);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public abstract class ZstdCompressorBase implements ICompressor
{
// These might change with the version of Zstd we're using
public static final int FAST_COMPRESSION_LEVEL = Zstd.minCompressionLevel();
public static final int BEST_COMPRESSION_LEVEL = Zstd.maxCompressionLevel();

// Compressor Defaults
public static final int DEFAULT_COMPRESSION_LEVEL = 3;
Expand Down Expand Up @@ -168,9 +167,9 @@ public void compress(ByteBuffer input, ByteBuffer output) throws IOException
*
* @param level compression level
*/
public static void validateCompressionLevel(int level)
public static void validateCompressionLevel(int level, int bestCompressionLevel)
{
if (level < FAST_COMPRESSION_LEVEL || level > BEST_COMPRESSION_LEVEL)
if (level < FAST_COMPRESSION_LEVEL || level > bestCompressionLevel)
{
throw new IllegalArgumentException(String.format("%s=%d is invalid", COMPRESSION_LEVEL_OPTION_NAME, level));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

public class ZstdDictionaryCompressor extends ZstdCompressorBase implements ICompressor, IDictionaryCompressor<ZstdCompressionDictionary>
{
// see CASSANDRA-21021
public static final int BEST_COMPRESSION_LEVEL = 9;

private static final ConcurrentHashMap<Integer, ZstdDictionaryCompressor> instancesPerLevel = new ConcurrentHashMap<>();
private static final Cache<ZstdCompressionDictionary, ZstdDictionaryCompressor> instancePerDict =
Caffeine.newBuilder()
Expand Down Expand Up @@ -74,7 +77,7 @@ public class ZstdDictionaryCompressor extends ZstdCompressorBase implements ICom
public static ZstdDictionaryCompressor create(Map<String, String> options)
{
int level = getOrDefaultCompressionLevel(options);
validateCompressionLevel(level);
validateCompressionLevel(level, BEST_COMPRESSION_LEVEL);
return getOrCreate(level, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.apache.cassandra.io.compress;

import com.github.luben.zstd.Zstd;
import com.github.luben.zstd.ZstdDictTrainer;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.db.compression.ZstdCompressionDictionary;
Expand Down Expand Up @@ -104,14 +103,22 @@ public void testCreateWithDictionary()
@Test
public void testCreateWithInvalidCompressionLevel()
{
String invalidLevel = String.valueOf(Zstd.maxCompressionLevel() + 1);
String invalidLevel = String.valueOf(ZstdDictionaryCompressor.BEST_COMPRESSION_LEVEL + 1);
Map<String, String> options = Map.of(ZstdCompressor.COMPRESSION_LEVEL_OPTION_NAME, invalidLevel);

assertThatThrownBy(() -> ZstdDictionaryCompressor.create(options))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(ZstdCompressor.COMPRESSION_LEVEL_OPTION_NAME + '=' + invalidLevel + " is invalid");
}

@Test
public void testCreateWithMaxCompressionLevel()
{
String invalidLevel = String.valueOf(ZstdDictionaryCompressor.BEST_COMPRESSION_LEVEL);
Map<String, String> options = Map.of(ZstdCompressor.COMPRESSION_LEVEL_OPTION_NAME, invalidLevel);
ZstdDictionaryCompressor.create(options);
}

@Test
public void testCompressDecompressWithDictionary() throws IOException
{
Expand Down