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): zstd meta compressor #2042

Merged
merged 16 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 15 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
92 changes: 92 additions & 0 deletions java/fury-extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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>
<groupId>org.apache.fury</groupId>
<artifactId>fury-parent</artifactId>
<version>0.10.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>fury-extensions</artifactId>

<description>
Apache Fury™ is a blazingly fast multi-language serialization framework powered by jit and zero-copy.

Apache Fury (incubating) is an effort undergoing incubation at the Apache
Software Foundation (ASF), sponsored by the Apache Incubator PMC.

Incubation is required of all newly accepted projects until a further review
indicates that the infrastructure, communications, and decision making process
have stabilized in a manner consistent with other successful ASF projects.

While incubation status is not necessarily a reflection of the completeness
or stability of the code, it does indicate that the project has yet to be
fully endorsed by the ASF.
</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<fury.java.rootdir>${basedir}/..</fury.java.rootdir>
</properties>

<dependencies>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<scope>provided</scope>
</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>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>org.apache.fury.extension</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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;

import com.github.luben.zstd.Zstd;
import com.github.luben.zstd.ZstdException;
import java.util.Arrays;

public class ZstdMetaCompressor implements MetaCompressor {

@Override
public byte[] compress(byte[] data, int offset, int size) {
long maxCompressedSize = Zstd.compressBound(size);
if (maxCompressedSize > Integer.MAX_VALUE) {
throw new ZstdException(Zstd.errGeneric(), "Max output size is greater than MAX_INT");
}
byte[] compressedData = new byte[(int) maxCompressedSize];
long originalSize =
Zstd.compressByteArray(
compressedData,
0,
(int) maxCompressedSize,
data,
offset,
size,
Zstd.defaultCompressionLevel());

return Arrays.copyOf(compressedData, (int) originalSize);
}

@Override
public byte[] decompress(byte[] data, int offset, int size) {
int decompressedSize = (int) Zstd.getFrameContentSize(data, offset, size, false);
byte[] decompressedBytes = new byte[decompressedSize];
long originalSize =
Zstd.decompressByteArray(decompressedBytes, 0, decompressedSize, data, offset, size);
return Arrays.copyOf(decompressedBytes, (int) originalSize);
}

@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,54 @@
/*
* 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;

import static org.apache.fury.meta.ClassDefEncoder.buildFieldsInfo;
import static org.apache.fury.meta.ClassDefEncoder.getClassFields;

import java.util.List;
import org.apache.fury.Fury;
import org.apache.fury.memory.MemoryBuffer;
import org.testng.Assert;
import org.testng.annotations.Test;

public class ClassDefEncoderTest {

static class TestFieldsOrderClass1 {
private int intField2;
private boolean booleanField;
private Object objField;
private long longField;
}

@Test
public void testBasicClassDef_zstdMetaCompressor() throws Exception {
Fury fury =
Fury.builder().withMetaShare(true).withMetaCompressor(new ZstdMetaCompressor()).build();
Class<TestFieldsOrderClass1> type = TestFieldsOrderClass1.class;
List<ClassDef.FieldInfo> fieldsInfo = buildFieldsInfo(fury.getClassResolver(), type);
MemoryBuffer buffer =
ClassDefEncoder.encodeClassDef(
fury.getClassResolver(), type, getClassFields(type, fieldsInfo), true);
ClassDef classDef = ClassDef.readClassDef(fury.getClassResolver(), buffer);
Assert.assertEquals(classDef.getClassName(), type.getName());
Assert.assertEquals(classDef.getFieldsInfo().size(), type.getDeclaredFields().length);
Assert.assertEquals(classDef.getFieldsInfo(), fieldsInfo);
}
}
7 changes: 7 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<modules>
<module>fury-format</module>
<module>fury-core</module>
<module>fury-extensions</module>
<module>fury-test-core</module>
<module>fury-testsuite</module>
</modules>
Expand All @@ -81,6 +82,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<guava.version>32.1.2-jre</guava.version>
<janino.version>3.1.10</janino.version>
<zstd.version>1.5.6-9</zstd.version>
orisgarno marked this conversation as resolved.
Show resolved Hide resolved
<commons_codec.version>1.13</commons_codec.version>
<fury.java.rootdir>${basedir}</fury.java.rootdir>
<maven-spotless-plugin.version>2.41.1</maven-spotless-plugin.version>
Expand All @@ -105,6 +107,11 @@
<artifactId>janino</artifactId>
<version>${janino.version}</version>
</dependency>
<dependency>
orisgarno marked this conversation as resolved.
Show resolved Hide resolved
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>${zstd.version}</version>
</dependency>
<!-- Cxeb68d52e-5509 -->
<dependency>
<groupId>commons-codec</groupId>
Expand Down
Loading