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 5 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
4 changes: 4 additions & 0 deletions java/fury-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<artifactId>slf4j-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
</dependency>
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we use povided dependency?

Copy link
Contributor Author

@orisgarno orisgarno Feb 6, 2025

Choose a reason for hiding this comment

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

im quite new to maven. ive made changes in the new commit, is it suffice?
d1a2930

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, d1a2930 is OK. Could you remove zstd-jni from fury-core module, it's used in fury-extensions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops forgot to remove that. Done

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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;

public class ZstdMetaCompressor implements MetaCompressor {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we add a test with org.apache.fury.config.FuryBuilder#withMetaCompressor set to this compressor?

Copy link
Contributor Author

@orisgarno orisgarno Feb 6, 2025

Choose a reason for hiding this comment

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

do you mean this?
java/fury-extensions/src/test/java/org/apache/fury/meta/ClassDefEncoderTest.java
or anything specific in mind?


@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];
Zstd.compressByteArray(
compressedData,
0,
(int) maxCompressedSize,
data,
offset,
size,
Zstd.defaultCompressionLevel());

return compressedData;
}

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

@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
Expand Up @@ -50,6 +50,21 @@ public void testBasicClassDef() throws Exception {
Assert.assertEquals(classDef.getFieldsInfo(), fieldsInfo);
}

@Test
public void testBasicClassDef_zstdMetaCompressor() throws Exception {
Fury fury =
Fury.builder().withMetaShare(true).withMetaCompressor(new ZstdMetaCompressor()).build();
Class<ClassDefTest.TestFieldsOrderClass1> type = ClassDefTest.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);
}

@Test
public void testBigMetaEncoding() {
for (Class<?> type :
Expand Down
6 changes: 6 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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 +106,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