Skip to content

Commit

Permalink
Handle Zstd parameter switches correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Coder-256 authored and luben committed Nov 30, 2023
1 parent b55246e commit 96be04a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 29 deletions.
37 changes: 35 additions & 2 deletions src/main/java/com/github/luben/zstd/Zstd.java
Expand Up @@ -8,10 +8,40 @@
import com.github.luben.zstd.ZstdDecompressCtx;

public class Zstd {

static {
Native.load();
}

/**
* Note: This enum controls features which are conditionally beneficial.
* Zstd typically will make a final decision on whether or not to enable the
* feature ({@link AUTO}), but setting the switch to {@link ENABLE} or
* {@link DISABLE} allows for force enabling/disabling the feature.
*/
public static enum ParamSwitch {
/**
* Let the library automatically determine whether the feature shall be enabled
*/
AUTO(0),
/**
* Force-enable the feature
*/
ENABLE(1),
/**
* Do not use the feature
*/
DISABLE(2);

private int val;
ParamSwitch(int val) {
this.val = val;
}

public int getValue() {
return val;
}
}

/**
* Compresses buffer 'src' into buffer 'dst'.
*
Expand Down Expand Up @@ -581,7 +611,10 @@ public static long decompressDirectByteBufferFastDict(ByteBuffer dst, int dstOff
public static native int setDecompressionLongMax(long stream, int windowLogMax);
public static native int setDecompressionMagicless(long stream, boolean useMagicless);
public static native int setRefMultipleDDicts(long stream, boolean useMultiple);
public static native int setValidateSequences(long stream, boolean validateSequences);
public static native int setValidateSequences(long stream, int validateSequences);
public static native int setSequenceProducerFallback(long stream, boolean fallbackFlag);
public static native int setSearchForExternalRepcodes(long stream, int searchRepcodes);
public static native int setEnableLongDistanceMatching(long stream, int enableLDM);

/* Utility methods */
/**
Expand Down
59 changes: 51 additions & 8 deletions src/main/java/com/github/luben/zstd/ZstdCompressCtx.java
Expand Up @@ -280,7 +280,7 @@ public ZstdCompressCtx setLong(int windowLog) {
* Register an external sequence producer
* @param producer the user-defined {@link SequenceProducer} to register.
*/
public void registerSequenceProducer(SequenceProducer producer) {
public ZstdCompressCtx registerSequenceProducer(SequenceProducer producer) {
ensureOpen();
acquireSharedLock();
if (this.seqprod != null) {
Expand All @@ -296,32 +296,75 @@ public void registerSequenceProducer(SequenceProducer producer) {
}
this.seqprod = producer;
releaseSharedLock();
return this;
}

/**
* Enable or disable sequence producer fallback
* @param fallbackFlag fall back to the default internal sequence producer if an external
* sequence producer returns an error code, default: false
*/
public ZstdCompressCtx setSequenceProducerFallback(boolean fallbackFlag){
public ZstdCompressCtx setSequenceProducerFallback(boolean fallbackFlag) {
ensureOpen();
acquireSharedLock();
setSequenceProducerFallback0(nativePtr, fallbackFlag);
releaseSharedLock();
try {
long result = Zstd.setSequenceProducerFallback(nativePtr, fallbackFlag);
if (Zstd.isError(result)) {
throw new ZstdException(result);
}
} finally {
releaseSharedLock();
}
return this;
}

/**
* Set whether to search external sequences for repeated offsets that can be
* encoded as repcodes.
* @param searchRepcodes whether to search for repcodes
*/
public ZstdCompressCtx setSearchForExternalRepcodes(Zstd.ParamSwitch searchRepcodes) {
ensureOpen();
acquireSharedLock();
try {
long result = Zstd.setSearchForExternalRepcodes(nativePtr, searchRepcodes.getValue());
if (Zstd.isError(result)) {
throw new ZstdException(result);
}
} finally {
releaseSharedLock();
}
return this;
}
private static native void setSequenceProducerFallback0(long ptr, boolean fallbackFlag);

/**
* Enable or disable sequence validation. Useful for the sequence-level API
* and with external sequence producers.
* @param validateSequences whether to validate all sequences, default: false
* @param validateSequences whether to enable sequence validation
*/
public ZstdCompressCtx setValidateSequences(Zstd.ParamSwitch validateSequences) {
ensureOpen();
acquireSharedLock();
try {
long result = Zstd.setValidateSequences(nativePtr, validateSequences.getValue());
if (Zstd.isError(result)) {
throw new ZstdException(result);
}
} finally {
releaseSharedLock();
}
return this;
}

/**
* Enable or disable long-distance matching.
* @param ldm whether to enable long-distance matching.
*/
public ZstdCompressCtx setValidateSequences(boolean validateSequences) {
public ZstdCompressCtx setEnableLongDistanceMatching(Zstd.ParamSwitch enableLDM) {
ensureOpen();
acquireSharedLock();
try {
long result = Zstd.setValidateSequences(nativePtr, validateSequences);
long result = Zstd.setEnableLongDistanceMatching(nativePtr, enableLDM.getValue());
if (Zstd.isError(result)) {
throw new ZstdException(result);
}
Expand Down
12 changes: 0 additions & 12 deletions src/main/native/jni_fast_zstd.c
Expand Up @@ -307,18 +307,6 @@ JNIEXPORT void JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_setDictID0
ZSTD_CCtx_setParameter(cctx, ZSTD_c_dictIDFlag, (dictIDFlag == JNI_TRUE));
}

/*
* Class: com_github_luben_zstd_ZstdCompressCtx
* Method: setSequenceProducerFallback0
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_setSequenceProducerFallback0
(JNIEnv *env, jclass clazz, jlong ptr, jboolean fallbackFlag)
{
ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t) ptr;
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableSeqProducerFallback, (fallbackFlag == JNI_TRUE));
}

/*
* Class: com_github_luben_zstd_ZstdCompressCtx
* Method: loadCDictFast0
Expand Down
38 changes: 34 additions & 4 deletions src/main/native/jni_zstd.c
Expand Up @@ -398,10 +398,10 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setCompressionLong
ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t) stream;
if (windowLog < ZSTD_WINDOWLOG_MIN || windowLog > ZSTD_WINDOWLOG_LIMIT_DEFAULT) {
// disable long matching and reset to default windowLog size
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 0);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_disable);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 0);
} else {
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, windowLog);
}
return 0;
Expand Down Expand Up @@ -543,13 +543,43 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setRefMultipleDDicts
/*
* Class: com_github_luben_zstd_Zstd
* Method: setValidateSequences
* Signature: (JZ)I
* Signature: (JI)I
*/
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setValidateSequences
(JNIEnv *env, jclass obj, jlong stream, jboolean validateSequences) {
(JNIEnv *env, jclass obj, jlong stream, jint validateSequences) {
return ZSTD_CCtx_setParameter((ZSTD_CCtx *)(intptr_t) stream, ZSTD_c_validateSequences, validateSequences);
}

/*
* Class: com_github_luben_zstd_Zstd
* Method: setSequenceProducerFallback
* Signature: (JZ)I
*/
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setSequenceProducerFallback
(JNIEnv *env, jclass obj, jlong stream, jboolean fallbackFlag) {
return ZSTD_CCtx_setParameter((ZSTD_CCtx*)(intptr_t) stream, ZSTD_c_enableSeqProducerFallback, (fallbackFlag == JNI_TRUE));
}

/*
* Class: com_github_luben_zstd_Zstd
* Method: setSearchForExternalRepcodes
* Signature: (JZ)I
*/
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setSearchForExternalRepcodes
(JNIEnv *env, jclass obj, jlong stream, jint searchRepcodes) {
return ZSTD_CCtx_setParameter((ZSTD_CCtx*)(intptr_t) stream, ZSTD_c_searchForExternalRepcodes, searchRepcodes);
}

/*
* Class: com_github_luben_zstd_Zstd
* Method: setEnableLongDistanceMatching
* Signature: (JZ)I
*/
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setEnableLongDistanceMatching
(JNIEnv *env, jclass obj, jlong stream, jint enableLDM) {
return ZSTD_CCtx_setParameter((ZSTD_CCtx*)(intptr_t) stream, ZSTD_c_enableLongDistanceMatching, enableLDM);
}

/*
* Class: com_github_luben_zstd_Zstd
* Methods: header constants access
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/Zstd.scala
Expand Up @@ -1239,7 +1239,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
def freeState(@unused state: Long) = {}
}
cctx.registerSequenceProducer(seqProd)
cctx.setValidateSequences(true)
cctx.setValidateSequences(Zstd.ParamSwitch.ENABLE)
cctx.setSequenceProducerFallback(false)
cctx.setPledgedSrcSize(size)
val compressedBuffer = ByteBuffer.allocateDirect(Zstd.compressBound(size).toInt)
Expand Down Expand Up @@ -1303,7 +1303,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
}

cctx.registerSequenceProducer(seqProd)
cctx.setValidateSequences(true)
cctx.setValidateSequences(Zstd.ParamSwitch.ENABLE)
cctx.setSequenceProducerFallback(false)
cctx.setPledgedSrcSize(size)

Expand Down Expand Up @@ -1347,7 +1347,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
}

cctx.registerSequenceProducer(seqProd)
cctx.setValidateSequences(true)
cctx.setValidateSequences(Zstd.ParamSwitch.ENABLE)
cctx.setSequenceProducerFallback(true) // !!
cctx.setPledgedSrcSize(size)

Expand Down

0 comments on commit 96be04a

Please sign in to comment.