-
Notifications
You must be signed in to change notification settings - Fork 262
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
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2de4407
feat(java): zstd meta compressor
orisgarno 59645e9
add unit test
orisgarno 2c433d4
Merge branch 'main' into zstd-direct-array
orisgarno cafdaec
Merge branch 'main' into zstd-direct-array
orisgarno c78a006
rerun test
orisgarno be83d4b
separate module for extensions
orisgarno d1a2930
provided scope for zstd dependency
orisgarno 5348a98
Merge branch 'main' into zstd-direct-array
orisgarno 6e5e408
Merge branch 'main' into zstd-direct-array
orisgarno db549c9
remove zstd from core
orisgarno 4dd6099
Merge branch 'main' into zstd-direct-array
orisgarno a1f4359
copy response to clamp the size to its original bound
orisgarno e7f2b91
copy response to clamp the size to its original bound
orisgarno fb72a67
copy response to clamp the size to its original bound
orisgarno 1b974f2
fix code style
orisgarno 5e40b68
remove zstd from parent pom
orisgarno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
68 changes: 68 additions & 0 deletions
68
java/fury-extensions/src/main/java/org/apache/fury/meta/ZstdMetaCompressor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
@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(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
java/fury-extensions/src/test/java/org/apache/fury/meta/ClassDefEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you share why its better to have its copies? @chaokunyang
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure whether
maxCompressedSize
is exactly the compressed size fo zstd, or it's a n estimated max sizeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After read zstd code, I got the idea why. Nice catch.
Lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me check for the decompress as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated