From 4315d50ce5c22843f83177025b4bb5eb51128557 Mon Sep 17 00:00:00 2001 From: jwang55 Date: Fri, 21 Jun 2024 17:19:45 +0800 Subject: [PATCH] feat(java): support meta compression by Zstd --- java/fury-meta-compressor-zstd/pom.xml | 61 +++++++++++++++++ .../fury/meta/zstd/ZstdMetaCompressor.java | 57 ++++++++++++++++ .../org/apache/fury/meta/zstd/SomeClass.java | 35 ++++++++++ .../meta/zstd/ZstdMetaCompressorTest.java | 66 +++++++++++++++++++ java/pom.xml | 8 ++- 5 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 java/fury-meta-compressor-zstd/pom.xml create mode 100644 java/fury-meta-compressor-zstd/src/main/java/org/apache/fury/meta/zstd/ZstdMetaCompressor.java create mode 100644 java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/SomeClass.java create mode 100644 java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/ZstdMetaCompressorTest.java diff --git a/java/fury-meta-compressor-zstd/pom.xml b/java/fury-meta-compressor-zstd/pom.xml new file mode 100644 index 0000000000..d1c25a6fa6 --- /dev/null +++ b/java/fury-meta-compressor-zstd/pom.xml @@ -0,0 +1,61 @@ + + + + + fury-parent + org.apache.fury + 0.6.0-SNAPSHOT + + 4.0.0 + + fury-meta-compressor-zstd + + + 8 + 8 + true + true + ${basedir}/.. + 2.20.0 + + + + + + com.github.luben + zstd-jni + + + org.apache.fury + fury-core + ${project.version} + + + org.apache.fury + fury-test-core + ${project.version} + test + + + diff --git a/java/fury-meta-compressor-zstd/src/main/java/org/apache/fury/meta/zstd/ZstdMetaCompressor.java b/java/fury-meta-compressor-zstd/src/main/java/org/apache/fury/meta/zstd/ZstdMetaCompressor.java new file mode 100644 index 0000000000..e13b74e4c1 --- /dev/null +++ b/java/fury-meta-compressor-zstd/src/main/java/org/apache/fury/meta/zstd/ZstdMetaCompressor.java @@ -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); + + byte[] buffer = new byte[(int) Zstd.getFrameContentSize(decompressData)]; + Zstd.decompress(buffer, decompressData); + 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(); + } +} diff --git a/java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/SomeClass.java b/java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/SomeClass.java new file mode 100644 index 0000000000..292084e01b --- /dev/null +++ b/java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/SomeClass.java @@ -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; + } +} diff --git a/java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/ZstdMetaCompressorTest.java b/java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/ZstdMetaCompressorTest.java new file mode 100644 index 0000000000..dfadcf578b --- /dev/null +++ b/java/fury-meta-compressor-zstd/src/test/java/org/apache/fury/meta/zstd/ZstdMetaCompressorTest.java @@ -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()); + } + +} diff --git a/java/pom.xml b/java/pom.xml index aa44ec3dad..5b143bb5fc 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -74,6 +74,7 @@ fury-core fury-test-core fury-testsuite + fury-meta-compressor-zstd @@ -88,7 +89,12 @@ - + + com.github.luben + zstd-jni + 1.5.6-3 + + org.slf4j slf4j-api 2.0.12