Skip to content
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

[PR] Add support for native memory compression and decompression #311

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
11c1c8f
added support for native memory compression
VladRodionov Apr 27, 2024
909ad36
Merge branch 'master' of https://github.com/VladRodionov/zstd-jni
VladRodionov Apr 27, 2024
b904897
added artifact installation script to a local maven repo
VladRodionov May 22, 2024
e9da410
remove instal.sh
VladRodionov May 31, 2024
04375ef
Merge branch 'luben:master' into master
VladRodionov May 31, 2024
05f759c
to support multiplatform builds
VladRodionov Jun 4, 2024
5ef09e1
Merge branch 'luben:master' into master
VladRodionov Jun 4, 2024
1c4328d
Merge branch 'master' of https://github.com/carrotdata/zstd-jni
VladRodionov Jun 4, 2024
7f40ab8
to support multiplatform build
VladRodionov Jun 4, 2024
a471754
to support multiplatform builds
VladRodionov Jun 5, 2024
ce41492
to support multiplatform builds
VladRodionov Jun 5, 2024
c75f02f
removed support binaries
VladRodionov Jun 5, 2024
f1b1e94
added instal_jar.sh
VladRodionov Jul 22, 2024
5e12c5b
add os dependent script
Jul 22, 2024
cd407ee
Fix grammatical error
Jul 22, 2024
a63e0c0
Update README.md
VladRodionov Aug 9, 2024
4a6c263
add repo for platform binaries
VladRodionov Aug 12, 2024
b3565ad
remove lib
VladRodionov Aug 12, 2024
7718d7a
multiplatform support libraries
VladRodionov Aug 12, 2024
9e183e3
Merge branch 'master' of https://github.com/carrotdata/zstd-jni
VladRodionov Aug 12, 2024
3ad6ec4
multiplatform jar with libraries
VladRodionov Aug 12, 2024
083a70f
Merge branch 'luben:master' into master
VladRodionov Aug 28, 2024
702877a
Linux/aarch version with glibc 2.39 dependency
VladRodionov Aug 28, 2024
5cc0d96
Merge branch 'master' of https://github.com/carrotdata/zstd-jni
VladRodionov Aug 28, 2024
31e76d2
linux/amd64 with glibc 2.31
Aug 29, 2024
637d8e6
upgrade to 1.5.6-4
VladRodionov Aug 29, 2024
f77bab5
upgrade to 1.5.6-4
VladRodionov Aug 29, 2024
808b7c0
upgrade to 1.5.6-4
VladRodionov Aug 29, 2024
645e431
upgrade to 1.5.6-4
VladRodionov Aug 29, 2024
7596eb5
Update README.md
VladRodionov Sep 3, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ project/plugins/project
.idea/
.bloop/
.metals/
.bsp/
tags
local.properties
zstd-jni.iml
Expand Down
79 changes: 4 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,87 +68,16 @@ v0.5.2, as that version is not published to Maven. Before compiling, you need to
$ cd sbt-java-module-info && ./sbt publishLocal && cd -
```

Compile and test:
Create package:
```
$ ./sbt compile test package
$ ./sbt compile package
```

If you want to publish it to you local ivy2 repository:
Insatll package locally into maven repository:
```
$ ./sbt publishLocal
$ ./install-jar.sh
```

Binary releases
---------------

The binary releases are architecture dependent because we are embedding the
native library in the provided Jar file. Currently they are built for
*linux-amd64*, *linux-i386*, *linux-aarch64*, *linux-armhf*, *linux-ppc64*,
*linux-ppc64le*, *linux-mips64*, *linux-s390x*, *linux-riskv64*, *linux-loongarch64*,
*win-amd64*, *win-x86*, *win-aarch64*, *darwin-x86_64* (MacOS X), *darwin-aarch64*,
*freebsd-amd64*, and *freebsd-i386*.

More builds will be available if I get access to more platforms.

You can find published releases on Maven Central.

<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>VERSION</version>
</dependency>

sbt dependency:

libraryDependencies += "com.github.luben" % "zstd-jni" % "VERSION"

Single architecture classified jars are also published. They can be used like:

<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>VERSION</version>
<classifier>linux_amd64</classifier>
</dependency>

or for sbt:

libraryDependencies += "com.github.luben" % "zstd-jni" % "VERSION" classifier "linux_amd64"

Link for direct download if you don't use a dependency manager:

- https://repo1.maven.org/maven2/com/github/luben/zstd-jni/

If there is not yet a binary release compatible with your platform look how
to build it locally under the [Building](#building-and-dependencies) section.

Android support
---------------

Zstd-jni is usable in Android applications by importing the sources in Android
Studio. I guess using git sub-modules will also work.

Android archive (*zstd-jni.aar*) is also published on maven central. You will need
to add the repository in your build.gradle, e.g.:

allprojects {
repositories {
jcenter()
mavenCentral()
}
}

as it is not added by default by Android Studio. And then add dependency on the
prebuilt android archive (aar):

dependencies {
implementation "com.github.luben:zstd-jni:VERSION@aar"
testImplementation "com.github.luben:zstd-jni:VERSION"
}

For example Android app and how to declare dependencies and use zstd-jni, consult
the 2nd and 3rd commit of: https://github.com/luben/ZstdAndroidExample

License
-------

Expand Down
88 changes: 88 additions & 0 deletions install-jar-os.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

# Determine the OS type
case "$(uname -s)" in
Linux)
OS_TYPE="linux"
;;
Darwin)
OS_TYPE="darwin"
;;
MINGW32_NT* | MINGW64_NT*)
OS_TYPE="win"
;;
*)
echo "Unsupported OS"
exit 1
;;
esac

# Determine the architecture
case "$(uname -m)" in
x86_64)
ARCH_TYPE="amd64"
;;
aarch64)
ARCH_TYPE="aarch64"
;;
i386 | i686)
ARCH_TYPE="i386"
;;
arm)
ARCH_TYPE="arm"
;;
arm64)
ARCH_TYPE="aarch64"
;;
ppc64)
ARCH_TYPE="ppc64"
;;
mips64)
ARCH_TYPE="mips64"
;;
s390x)
ARCH_TYPE="s390x"
;;
riscv64)
ARCH_TYPE="riscv64"
;;
loongarch64)
ARCH_TYPE="loongarch64"
;;
*)
echo "Unsupported architecture"
exit 1
;;
esac

echo Build for:
echo OS_TYPE=$OS_TYPE
echo ARCH_TYPE=$ARCH_TYPE

# Define the directory and file pattern
TARGET_DIR="./target"
FILE_PATTERN="zstd-jni-*"

# Find the jar file matching the pattern
JAR_FILE=$(find "$TARGET_DIR" | grep ${FILE_PATTERN} | grep ${OS_TYPE} | grep ${ARCH_TYPE} | grep "\.jar")
echo OS-Specific Artifact: $JAR_FILE

# Check if the jar file exists
if [[ -z "$JAR_FILE" ]]; then
echo "Error: No jar file matching the pattern '$FILE_PATTERN' found in '$TARGET_DIR'."
exit 1
fi

# Extract the version from the jar file name
VERSION=$(echo "$JAR_FILE" | sed -E 's/.*-([0-9]+\.[0-9]+\.[0-9]+-[0-9]+)-[^-]+\.jar/\1/')

# Check if version was extracted successfully
if [[ -z "$VERSION" ]]; then
echo "Error: Unable to extract version from jar file name '$JAR_FILE'."
exit 1
fi
echo VERSION=$VERSION

# Run the Maven install command with the extracted version
echo "Installing zstd-jni library to the local maven repository"
mvn install:install-file -Dfile="$JAR_FILE" -DgroupId=com.carrotdata -DartifactId=zstd-jni -Dversion="$VERSION" -Dpackaging=jar
27 changes: 27 additions & 0 deletions install-jar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

Copy link
Owner

Choose a reason for hiding this comment

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

Can you make this filename more descriptive and put a comment on top? Or just don't include it in the commit?

Copy link
Author

@VladRodionov VladRodionov May 30, 2024

Choose a reason for hiding this comment

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

I will remove this file from the commit. I had tried to add tests to this feature, but it has turned out that it's not that straightforward task. I had to enable sun.misc.Unsafe access to be able to allocate native memory and Scala (sbt) just makes this impossible (at least for me). I have zero experience in Scala and its tooling. In Java 9+ the access to this class is prohibited by default, so you have to specify additional command line args:
java --add-opens jdk.unsupported/sun.misc=ALL-UNNAMED --add-opens java.base/jdk.internal.misc=ALL-UNNAMED

For some reason this does not work with sbt. Probably I did something wrong.

Copy link
Owner

Choose a reason for hiding this comment

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

Can you try to add these options to https://github.com/luben/zstd-jni/blob/master/build.sbt#L39 . I just pushed a change to run the tests in forked JVM so these options will apply.

Copy link
Author

@VladRodionov VladRodionov Jun 1, 2024

Choose a reason for hiding this comment

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

I recently moved repo fork to a new owner (organization) - https://github.com/carrotdata/zstd-jni/tree/master. The current PR is in some kind of a zombie state after that. I am going to close this PR and open new one, @luben . What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

install-jar.sh has been removed.

Copy link
Owner

Choose a reason for hiding this comment

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

I recently moved repo fork to a new owner (organization) - https://github.com/carrotdata/zstd-jni/tree/master. The current PR is in some kind of a zombie state after that. I am going to close this PR and open new one, @luben . What do you think?

Sure, we can continue on a new PR

Copy link
Author

@VladRodionov VladRodionov Jun 1, 2024

Choose a reason for hiding this comment

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

It looks like we can keep the old one. I removed 'install-jar.sh and rebased it to the luben:master.

Copy link
Owner

@luben luben Jun 18, 2024

Choose a reason for hiding this comment

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

I just pushed a change to run the tests in forked JVM so these options will apply.

Hmm, it was not so easy - forking broke builds on Windows and Mac OS, not sure why. So I reverted that

# Define the directory and file pattern
TARGET_DIR="./target"
FILE_PATTERN="zstd-jni-*.jar"

# Find the jar file matching the pattern
JAR_FILE=$(find "$TARGET_DIR" -name "$FILE_PATTERN" -print -quit)

# Check if the jar file exists
if [[ -z "$JAR_FILE" ]]; then
echo "Error: No jar file matching the pattern '$FILE_PATTERN' found in '$TARGET_DIR'."
exit 1
fi

# Extract the version from the jar file name
VERSION=$(basename "$JAR_FILE" | sed -n 's/^zstd-jni-\(.*\)\.jar$/\1/p')

# Check if version was extracted successfully
if [[ -z "$VERSION" ]]; then
echo "Error: Unable to extract version from jar file name '$JAR_FILE'."
exit 1
fi

# Run the Maven install command with the extracted version
echo "Installing zstd-jni library to the local maven repository"
mvn install:install-file -Dfile="$JAR_FILE" -DgroupId=com.carrotdata -DartifactId=zstd-jni -Dversion="$VERSION" -Dpackaging=jar
Binary file added lib/darwin/aarch64/libzstd-jni-1.5.6-4.dylib
Binary file not shown.
Binary file added lib/darwin/x86_64/libzstd-jni-1.5.6-4.dylib
Binary file not shown.
Binary file added lib/linux/aarch64/libzstd-jni-1.5.6-4.so
Binary file not shown.
Binary file added lib/linux/amd64/libzstd-jni-1.5.6-4.so
Binary file not shown.
Binary file added lib/zstd-jni-1.5.6-4.jar
Binary file not shown.
90 changes: 90 additions & 0 deletions src/main/java/com/github/luben/zstd/ZstdCompressCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,98 @@ public int compressByteArray(byte[] dstBuff, int dstOffset, int dstSize, byte[]
}
}


/**
* Compress native memory - to native memory, reusing this ZstdCompressCtx.
* @param dstBuff destination memory buffer address
* @param dstSize destination memory buffer size
* @param srcBuff source memory buffer address
* @param srcSize source memory buffer size
* @return the number of bytes written into buffer 'dstBuff'.
*/
public int compressNativeNative(long dstBuff, int dstSize, long srcBuff, int srcSize) {
ensureOpen();
acquireSharedLock();

try {
long size = compressUnsafeNativeNative0(nativePtr, dstBuff, dstSize, srcBuff, srcSize);
if (Zstd.isError(size)) {
throw new ZstdException(size);
}
if (size > Integer.MAX_VALUE) {
throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
}
return (int) size;
} finally {
releaseSharedLock();
}
}

/**
* Compress native memory - to byte array, reusing this ZstdCompressCtx.
* @param dstBuff destination buffer
* @param dstOffset offset in the destination buffer
* @param dstSize destination buffer size
* @param srcBuff source memory buffer address
* @param srcSize source memory buffer size
* @return the number of bytes written into buffer 'dstBuff'.
*/
public int compressNativeByteArray(byte[] dstBuff, int dstOffset, int dstSize, long srcBuff, int srcSize) {
ensureOpen();
acquireSharedLock();

try {
long size = compressUnsafeNativeByteArray0(nativePtr, dstBuff, dstOffset, dstSize, srcBuff, srcSize);
if (Zstd.isError(size)) {
throw new ZstdException(size);
}
if (size > Integer.MAX_VALUE) {
throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
}
return (int) size;
} finally {
releaseSharedLock();
}
}

/**
* Compress byte array - to native memory, reusing this ZstdCompressCtx.
* @param dstBuff destination memory buffer address
* @param dstSize destination memory buffer size
* @param srcBuff source buffer
* @param srcOffset source buffer offset
* @param srcSize source buffer size
* @return the number of bytes written into buffer 'dstBuff'.
*/
public int compressByteArrayNative(long dstBuff, int dstSize, byte[] srcBuff, int srcOffset,
int srcSize) {
ensureOpen();
acquireSharedLock();

try {
long size = compressUnsafeByteArrayNative0(nativePtr, dstBuff, dstSize, srcBuff, srcOffset,
srcSize);
if (Zstd.isError(size)) {
throw new ZstdException(size);
}
if (size > Integer.MAX_VALUE) {
throw new ZstdException(Zstd.errGeneric(), "Output size is greater than MAX_INT");
}
return (int) size;
} finally {
releaseSharedLock();
}
}

private static native long compressByteArray0(long ptr, byte[] dst, int dstOffset, int dstSize, byte[] src, int srcOffset, int srcSize);

private static native long compressUnsafeNativeNative0(long ptr, long dst, int dstSize, long src, int srcSize);

private static native long compressUnsafeNativeByteArray0(long ptr, byte[] dst, int dstOffset, int dstSize, long src, int srcSize);

private static native long compressUnsafeByteArrayNative0(long ptr, long dst, int dstSize, byte[] src, int srcOffset, int srcSize);


/* Convenience methods */

/**
Expand Down
Loading