Skip to content

Commit 253adaf

Browse files
committed
Take another approach for the dict trainer min samples
1 parent 8b24ff0 commit 253adaf

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,13 @@ public static long decompressedDirectByteBufferSize(ByteBuffer src, int srcPosit
708708
* @return the number of bytes into buffer 'dictBuffer' or an error code if
709709
* it fails (which can be tested using ZSTD_isError())
710710
*/
711-
public static native long trainFromBuffer(byte[][] samples, byte[] dictBuffer, boolean legacy);
711+
public static long trainFromBuffer(byte[][] samples, byte[] dictBuffer, boolean legacy) {
712+
if (samples.length <= 10) {
713+
throw new ZstdException(Zstd.errGeneric(), "nb of samples too low");
714+
}
715+
return trainFromBuffer0(samples, dictBuffer, legacy);
716+
}
717+
private static native long trainFromBuffer0(byte[][] samples, byte[] dictBuffer, boolean legacy);
712718

713719
/**
714720
* Creates a new dictionary to tune a kind of samples
@@ -720,7 +726,13 @@ public static long decompressedDirectByteBufferSize(ByteBuffer src, int srcPosit
720726
* @return the number of bytes into buffer 'dictBuffer' or an error code if
721727
* it fails (which can be tested using ZSTD_isError())
722728
*/
723-
public static native long trainFromBufferDirect(ByteBuffer samples, int[] sampleSizes, ByteBuffer dictBuffer, boolean legacy);
729+
public static long trainFromBufferDirect(ByteBuffer samples, int[] sampleSizes, ByteBuffer dictBuffer, boolean legacy) {
730+
if (sampleSizes.length <= 10) {
731+
throw new ZstdException(Zstd.errGeneric(), "nb of samples too low");
732+
}
733+
return trainFromBufferDirect0(samples, sampleSizes, dictBuffer, legacy);
734+
}
735+
private static native long trainFromBufferDirect0(ByteBuffer samples, int[] sampleSizes, ByteBuffer dictBuffer, boolean legacy);
724736

725737
/**
726738
* Get DictId from a compressed frame

src/main/native/jni_zdict.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
#include <stdlib.h>
88
#include <string.h>
99

10-
JNIEXPORT jlong Java_com_github_luben_zstd_Zstd_trainFromBuffer
10+
JNIEXPORT jlong Java_com_github_luben_zstd_Zstd_trainFromBuffer0
1111
(JNIEnv *env, jclass obj, jobjectArray samples, jbyteArray dictBuffer, jboolean legacy) {
1212
size_t size = 0;
1313
jsize num_samples = (*env)->GetArrayLength(env, samples);
14-
if (num_samples <= 10) {
15-
jclass eClass = (*env)->FindClass(env, "Ljava/lang/RuntimeException;");
16-
(*env)->ThrowNew(env, eClass, "nb of samples too low");
17-
goto E1;
18-
}
1914
size_t *samples_sizes = malloc(sizeof(size_t) * num_samples);
2015
if (!samples_sizes) {
2116
jclass eClass = (*env)->FindClass(env, "Ljava/lang/OutOfMemoryError;");
@@ -60,7 +55,7 @@ E2: free(samples_sizes);
6055
E1: return size;
6156
}
6257

63-
JNIEXPORT jlong Java_com_github_luben_zstd_Zstd_trainFromBufferDirect
58+
JNIEXPORT jlong Java_com_github_luben_zstd_Zstd_trainFromBufferDirect0
6459
(JNIEnv *env, jclass obj, jobject samples, jintArray sampleSizes, jobject dictBuffer, jboolean legacy) {
6560

6661
size_t size = 0;
@@ -70,11 +65,6 @@ JNIEXPORT jlong Java_com_github_luben_zstd_Zstd_trainFromBufferDirect
7065

7166
/* convert sized from int to size_t */
7267
jsize num_samples = (*env)->GetArrayLength(env, sampleSizes);
73-
if (num_samples <= 10) {
74-
jclass eClass = (*env)->FindClass(env, "Ljava/lang/RuntimeException;");
75-
(*env)->ThrowNew(env, eClass, "nb of samples too low");
76-
goto E1;
77-
}
7868
size_t *samples_sizes = malloc(sizeof(size_t) * num_samples);
7969
if (!samples_sizes) {
8070
jclass eClass = (*env)->FindClass(env, "Ljava/lang/OutOfMemoryError;");

0 commit comments

Comments
 (0)