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

feat(java): support meta compression by Zstd #1696

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
61 changes: 61 additions & 0 deletions java/fury-meta-compressor-zstd/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>fury-parent</artifactId>
<groupId>org.apache.fury</groupId>
<version>0.6.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>fury-meta-compressor-zstd</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.deploy.skip>true</maven.deploy.skip>
<maven.javadoc.skip>true</maven.javadoc.skip>
<fury.java.rootdir>${basedir}/..</fury.java.rootdir>
<log4j.version>2.20.0</log4j.version>
</properties>


<dependencies>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
</dependency>
<dependency>
<groupId>org.apache.fury</groupId>
<artifactId>fury-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.fury</groupId>
<artifactId>fury-test-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.meta.zstd;

import com.github.luben.zstd.Zstd;
import org.apache.fury.meta.MetaCompressor;


/** A meta compressor based on {@link Zstd} compression algorithm. */
public class ZstdMetaCompressor implements MetaCompressor {
@Override
public byte[] compress(byte[] data, int offset, int size) {
byte[] compressData = new byte[size];
System.arraycopy(data, offset, compressData, 0, size);
return Zstd.compress(compressData);
}

@Override
public byte[] decompress(byte[] data, int offset, int size) {
byte[] decompressData = new byte[size];
System.arraycopy(data, offset, decompressData, 0, size);
Comment on lines +37 to +38
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
byte[] decompressData = new byte[size];
System.arraycopy(data, offset, decompressData, 0, size);
byte[] decompressData = Arrays.copyOfRange(data, offset, offset + size);


byte[] buffer = new byte[(int) Zstd.getFrameContentSize(decompressData)];
Zstd.decompress(buffer, decompressData);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems not used

return buffer;
}

@Override
public int hashCode() {
return ZstdMetaCompressor.class.hashCode();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
return o != null && getClass() == o.getClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.meta.zstd;

import lombok.Data;

@Data
public class SomeClass {
private Integer code;
private String msg;

public static SomeClass buildTestInstance() {
SomeClass someClass = new SomeClass();
someClass.setMsg("this is zstd test");
someClass.setCode(100);
return someClass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.meta.zstd;

import org.apache.fury.Fury;
import org.apache.fury.config.FuryBuilder;
import org.apache.fury.config.Language;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.nio.charset.StandardCharsets;

public class ZstdMetaCompressorTest {

@Test
public void testZstd() {
ZstdMetaCompressor zstdMetaCompressor = new ZstdMetaCompressor();

final String data = "A meta compressor based on {@link Zstd} compression algorithm.";
byte[] originByte = data.getBytes(StandardCharsets.UTF_8);

// compress
byte[] compressByte = zstdMetaCompressor.compress(originByte, 0, originByte.length);

// decompress
byte[] decompress = zstdMetaCompressor.decompress(compressByte, 0, compressByte.length);
System.out.println(new String(decompress, StandardCharsets.UTF_8));
Assert.assertEquals(new String(decompress, StandardCharsets.UTF_8), data);
}

@Test
public void testFuryWithZstd() {
Fury fury = new FuryBuilder()
.withLanguage(Language.JAVA)
.requireClassRegistration(true)
.withMetaCompressor(new ZstdMetaCompressor())
.build();
fury.register(SomeClass.class);

SomeClass someClass = SomeClass.buildTestInstance();
byte[] bytes = fury.serialize(someClass);
SomeClass copySomeClass = (SomeClass) fury.deserialize(bytes);

Assert.assertNotSame(someClass, copySomeClass);
Assert.assertEquals(copySomeClass.getCode(), someClass.getCode());
Assert.assertEquals(copySomeClass.getMsg(), someClass.getMsg());
}

}
8 changes: 7 additions & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<module>fury-core</module>
<module>fury-test-core</module>
<module>fury-testsuite</module>
<module>fury-meta-compressor-zstd</module>
</modules>

<properties>
Expand All @@ -88,7 +89,12 @@

<dependencyManagement>
<dependencies>
<dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>1.5.6-3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.12</version>
Expand Down
Loading