This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git


The following commit(s) were added to refs/heads/main by this push:
     new a80140ac feat(java): zstd meta compressor (#2042)
a80140ac is described below

commit a80140aca047d6c3ced2b7f0ff090ed154429f68
Author: orisgarno <[email protected]>
AuthorDate: Fri Feb 7 14:01:56 2025 +0700

    feat(java): zstd meta compressor (#2042)
    
    <!--
    **Thanks for contributing to Fury.**
    
    **If this is your first time opening a PR on fury, you can refer to
    
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**
    
    Contribution Checklist
    
    - The **Apache Fury (incubating)** community has restrictions on the
    naming of pr titles. You can also find instructions in
    [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).
    
    - Fury has a strong focus on performance. If the PR you submit will have
    an impact on performance, please benchmark it first and provide the
    benchmark result here.
    -->
    
    ## What does this PR do?
    create zstd metacompressor as an option. let zstd access the src arr
    instead of copy to new array.
    
    ## Related issues
    
    <!--
    Is there any related issue? Please attach here.
    
    - #xxxx0
    - #xxxx1
    - #xxxx2
    -->
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fury/issues/new/choose) describing the
    need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 java/fury-extensions/pom.xml                       | 94 ++++++++++++++++++++++
 .../org/apache/fury/meta/ZstdMetaCompressor.java   | 69 ++++++++++++++++
 .../org/apache/fury/meta/ClassDefEncoderTest.java  | 54 +++++++++++++
 java/pom.xml                                       |  1 +
 4 files changed, 218 insertions(+)

diff --git a/java/fury-extensions/pom.xml b/java/fury-extensions/pom.xml
new file mode 100644
index 00000000..e68f75f5
--- /dev/null
+++ b/java/fury-extensions/pom.xml
@@ -0,0 +1,94 @@
+<?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>
+    <zstd.version>1.5.6-9</zstd.version>
+    <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>
+      <version>${zstd.version}</version>
+      <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>
diff --git 
a/java/fury-extensions/src/main/java/org/apache/fury/meta/ZstdMetaCompressor.java
 
b/java/fury-extensions/src/main/java/org/apache/fury/meta/ZstdMetaCompressor.java
new file mode 100644
index 00000000..d61f6052
--- /dev/null
+++ 
b/java/fury-extensions/src/main/java/org/apache/fury/meta/ZstdMetaCompressor.java
@@ -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();
+  }
+}
diff --git 
a/java/fury-extensions/src/test/java/org/apache/fury/meta/ClassDefEncoderTest.java
 
b/java/fury-extensions/src/test/java/org/apache/fury/meta/ClassDefEncoderTest.java
new file mode 100644
index 00000000..d581e941
--- /dev/null
+++ 
b/java/fury-extensions/src/test/java/org/apache/fury/meta/ClassDefEncoderTest.java
@@ -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);
+  }
+}
diff --git a/java/pom.xml b/java/pom.xml
index e1691b23..afe04037 100644
--- a/java/pom.xml
+++ b/java/pom.xml
@@ -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>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to