Skip to content

Commit 96be04a

Browse files
Coder-256luben
authored andcommitted
Handle Zstd parameter switches correctly
1 parent b55246e commit 96be04a

File tree

5 files changed

+123
-29
lines changed

5 files changed

+123
-29
lines changed

src/main/java/com/github/luben/zstd/Zstd.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,40 @@
88
import com.github.luben.zstd.ZstdDecompressCtx;
99

1010
public class Zstd {
11-
1211
static {
1312
Native.load();
1413
}
14+
15+
/**
16+
* Note: This enum controls features which are conditionally beneficial.
17+
* Zstd typically will make a final decision on whether or not to enable the
18+
* feature ({@link AUTO}), but setting the switch to {@link ENABLE} or
19+
* {@link DISABLE} allows for force enabling/disabling the feature.
20+
*/
21+
public static enum ParamSwitch {
22+
/**
23+
* Let the library automatically determine whether the feature shall be enabled
24+
*/
25+
AUTO(0),
26+
/**
27+
* Force-enable the feature
28+
*/
29+
ENABLE(1),
30+
/**
31+
* Do not use the feature
32+
*/
33+
DISABLE(2);
34+
35+
private int val;
36+
ParamSwitch(int val) {
37+
this.val = val;
38+
}
39+
40+
public int getValue() {
41+
return val;
42+
}
43+
}
44+
1545
/**
1646
* Compresses buffer 'src' into buffer 'dst'.
1747
*
@@ -581,7 +611,10 @@ public static long decompressDirectByteBufferFastDict(ByteBuffer dst, int dstOff
581611
public static native int setDecompressionLongMax(long stream, int windowLogMax);
582612
public static native int setDecompressionMagicless(long stream, boolean useMagicless);
583613
public static native int setRefMultipleDDicts(long stream, boolean useMultiple);
584-
public static native int setValidateSequences(long stream, boolean validateSequences);
614+
public static native int setValidateSequences(long stream, int validateSequences);
615+
public static native int setSequenceProducerFallback(long stream, boolean fallbackFlag);
616+
public static native int setSearchForExternalRepcodes(long stream, int searchRepcodes);
617+
public static native int setEnableLongDistanceMatching(long stream, int enableLDM);
585618

586619
/* Utility methods */
587620
/**

src/main/java/com/github/luben/zstd/ZstdCompressCtx.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public ZstdCompressCtx setLong(int windowLog) {
280280
* Register an external sequence producer
281281
* @param producer the user-defined {@link SequenceProducer} to register.
282282
*/
283-
public void registerSequenceProducer(SequenceProducer producer) {
283+
public ZstdCompressCtx registerSequenceProducer(SequenceProducer producer) {
284284
ensureOpen();
285285
acquireSharedLock();
286286
if (this.seqprod != null) {
@@ -296,32 +296,75 @@ public void registerSequenceProducer(SequenceProducer producer) {
296296
}
297297
this.seqprod = producer;
298298
releaseSharedLock();
299+
return this;
299300
}
300301

301302
/**
302303
* Enable or disable sequence producer fallback
303304
* @param fallbackFlag fall back to the default internal sequence producer if an external
304305
* sequence producer returns an error code, default: false
305306
*/
306-
public ZstdCompressCtx setSequenceProducerFallback(boolean fallbackFlag){
307+
public ZstdCompressCtx setSequenceProducerFallback(boolean fallbackFlag) {
307308
ensureOpen();
308309
acquireSharedLock();
309-
setSequenceProducerFallback0(nativePtr, fallbackFlag);
310-
releaseSharedLock();
310+
try {
311+
long result = Zstd.setSequenceProducerFallback(nativePtr, fallbackFlag);
312+
if (Zstd.isError(result)) {
313+
throw new ZstdException(result);
314+
}
315+
} finally {
316+
releaseSharedLock();
317+
}
318+
return this;
319+
}
320+
321+
/**
322+
* Set whether to search external sequences for repeated offsets that can be
323+
* encoded as repcodes.
324+
* @param searchRepcodes whether to search for repcodes
325+
*/
326+
public ZstdCompressCtx setSearchForExternalRepcodes(Zstd.ParamSwitch searchRepcodes) {
327+
ensureOpen();
328+
acquireSharedLock();
329+
try {
330+
long result = Zstd.setSearchForExternalRepcodes(nativePtr, searchRepcodes.getValue());
331+
if (Zstd.isError(result)) {
332+
throw new ZstdException(result);
333+
}
334+
} finally {
335+
releaseSharedLock();
336+
}
311337
return this;
312338
}
313-
private static native void setSequenceProducerFallback0(long ptr, boolean fallbackFlag);
314339

315340
/**
316341
* Enable or disable sequence validation. Useful for the sequence-level API
317342
* and with external sequence producers.
318-
* @param validateSequences whether to validate all sequences, default: false
343+
* @param validateSequences whether to enable sequence validation
344+
*/
345+
public ZstdCompressCtx setValidateSequences(Zstd.ParamSwitch validateSequences) {
346+
ensureOpen();
347+
acquireSharedLock();
348+
try {
349+
long result = Zstd.setValidateSequences(nativePtr, validateSequences.getValue());
350+
if (Zstd.isError(result)) {
351+
throw new ZstdException(result);
352+
}
353+
} finally {
354+
releaseSharedLock();
355+
}
356+
return this;
357+
}
358+
359+
/**
360+
* Enable or disable long-distance matching.
361+
* @param ldm whether to enable long-distance matching.
319362
*/
320-
public ZstdCompressCtx setValidateSequences(boolean validateSequences) {
363+
public ZstdCompressCtx setEnableLongDistanceMatching(Zstd.ParamSwitch enableLDM) {
321364
ensureOpen();
322365
acquireSharedLock();
323366
try {
324-
long result = Zstd.setValidateSequences(nativePtr, validateSequences);
367+
long result = Zstd.setEnableLongDistanceMatching(nativePtr, enableLDM.getValue());
325368
if (Zstd.isError(result)) {
326369
throw new ZstdException(result);
327370
}

src/main/native/jni_fast_zstd.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,6 @@ JNIEXPORT void JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_setDictID0
307307
ZSTD_CCtx_setParameter(cctx, ZSTD_c_dictIDFlag, (dictIDFlag == JNI_TRUE));
308308
}
309309

310-
/*
311-
* Class: com_github_luben_zstd_ZstdCompressCtx
312-
* Method: setSequenceProducerFallback0
313-
* Signature: (JZ)V
314-
*/
315-
JNIEXPORT void JNICALL Java_com_github_luben_zstd_ZstdCompressCtx_setSequenceProducerFallback0
316-
(JNIEnv *env, jclass clazz, jlong ptr, jboolean fallbackFlag)
317-
{
318-
ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t) ptr;
319-
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableSeqProducerFallback, (fallbackFlag == JNI_TRUE));
320-
}
321-
322310
/*
323311
* Class: com_github_luben_zstd_ZstdCompressCtx
324312
* Method: loadCDictFast0

src/main/native/jni_zstd.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setCompressionLong
398398
ZSTD_CCtx* cctx = (ZSTD_CCtx*)(intptr_t) stream;
399399
if (windowLog < ZSTD_WINDOWLOG_MIN || windowLog > ZSTD_WINDOWLOG_LIMIT_DEFAULT) {
400400
// disable long matching and reset to default windowLog size
401-
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 0);
401+
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_disable);
402402
ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 0);
403403
} else {
404-
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1);
404+
ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable);
405405
ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, windowLog);
406406
}
407407
return 0;
@@ -543,13 +543,43 @@ JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setRefMultipleDDicts
543543
/*
544544
* Class: com_github_luben_zstd_Zstd
545545
* Method: setValidateSequences
546-
* Signature: (JZ)I
546+
* Signature: (JI)I
547547
*/
548548
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setValidateSequences
549-
(JNIEnv *env, jclass obj, jlong stream, jboolean validateSequences) {
549+
(JNIEnv *env, jclass obj, jlong stream, jint validateSequences) {
550550
return ZSTD_CCtx_setParameter((ZSTD_CCtx *)(intptr_t) stream, ZSTD_c_validateSequences, validateSequences);
551551
}
552552

553+
/*
554+
* Class: com_github_luben_zstd_Zstd
555+
* Method: setSequenceProducerFallback
556+
* Signature: (JZ)I
557+
*/
558+
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setSequenceProducerFallback
559+
(JNIEnv *env, jclass obj, jlong stream, jboolean fallbackFlag) {
560+
return ZSTD_CCtx_setParameter((ZSTD_CCtx*)(intptr_t) stream, ZSTD_c_enableSeqProducerFallback, (fallbackFlag == JNI_TRUE));
561+
}
562+
563+
/*
564+
* Class: com_github_luben_zstd_Zstd
565+
* Method: setSearchForExternalRepcodes
566+
* Signature: (JZ)I
567+
*/
568+
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setSearchForExternalRepcodes
569+
(JNIEnv *env, jclass obj, jlong stream, jint searchRepcodes) {
570+
return ZSTD_CCtx_setParameter((ZSTD_CCtx*)(intptr_t) stream, ZSTD_c_searchForExternalRepcodes, searchRepcodes);
571+
}
572+
573+
/*
574+
* Class: com_github_luben_zstd_Zstd
575+
* Method: setEnableLongDistanceMatching
576+
* Signature: (JZ)I
577+
*/
578+
JNIEXPORT jint JNICALL Java_com_github_luben_zstd_Zstd_setEnableLongDistanceMatching
579+
(JNIEnv *env, jclass obj, jlong stream, jint enableLDM) {
580+
return ZSTD_CCtx_setParameter((ZSTD_CCtx*)(intptr_t) stream, ZSTD_c_enableLongDistanceMatching, enableLDM);
581+
}
582+
553583
/*
554584
* Class: com_github_luben_zstd_Zstd
555585
* Methods: header constants access

src/test/scala/Zstd.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
12391239
def freeState(@unused state: Long) = {}
12401240
}
12411241
cctx.registerSequenceProducer(seqProd)
1242-
cctx.setValidateSequences(true)
1242+
cctx.setValidateSequences(Zstd.ParamSwitch.ENABLE)
12431243
cctx.setSequenceProducerFallback(false)
12441244
cctx.setPledgedSrcSize(size)
12451245
val compressedBuffer = ByteBuffer.allocateDirect(Zstd.compressBound(size).toInt)
@@ -1303,7 +1303,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
13031303
}
13041304

13051305
cctx.registerSequenceProducer(seqProd)
1306-
cctx.setValidateSequences(true)
1306+
cctx.setValidateSequences(Zstd.ParamSwitch.ENABLE)
13071307
cctx.setSequenceProducerFallback(false)
13081308
cctx.setPledgedSrcSize(size)
13091309

@@ -1347,7 +1347,7 @@ class ZstdSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {
13471347
}
13481348

13491349
cctx.registerSequenceProducer(seqProd)
1350-
cctx.setValidateSequences(true)
1350+
cctx.setValidateSequences(Zstd.ParamSwitch.ENABLE)
13511351
cctx.setSequenceProducerFallback(true) // !!
13521352
cctx.setPledgedSrcSize(size)
13531353

0 commit comments

Comments
 (0)