Skip to content

Commit

Permalink
chore: cherry pick to 0.10 (#2048)
Browse files Browse the repository at this point in the history
<!--
**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?

<!-- Describe the purpose of this PR. -->

## 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.
-->

---------

Co-authored-by: weipeng <[email protected]>
Co-authored-by: orisgarno <[email protected]>
Co-authored-by: hn <[email protected]>
Co-authored-by: hening <[email protected]>
  • Loading branch information
5 people authored Feb 7, 2025
1 parent 924743c commit 015b852
Show file tree
Hide file tree
Showing 25 changed files with 674 additions and 1,204 deletions.
187 changes: 124 additions & 63 deletions docs/guide/java_serialization_guide.md

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions java/benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,6 @@
<artifactId>smoothie-map</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions java/fury-core/src/main/java/org/apache/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public SerializerFactory getSerializerFactory() {
return classResolver.getSerializerFactory();
}

public <T> Serializer<T> getSerializer(Class<T> cls) {
Preconditions.checkNotNull(cls);
return classResolver.getSerializer(cls);
}

@Override
public MemoryBuffer serialize(Object obj, long address, int size) {
MemoryBuffer buffer = MemoryUtils.buffer(address, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1410,11 +1410,10 @@ private ClassInfo readClassInfoWithMetaShare(MetaContext metaContext, int index)
return classInfo;
}


public ClassInfo readClassInfoWithMetaShare(MemoryBuffer buffer, Class<?> targetClass) {
assert metaContextShareEnabled;
ClassInfo classInfo =
readClassInfoWithMetaShare(buffer, fury.getSerializationContext().getMetaContext());
readClassInfoWithMetaShare(buffer, fury.getSerializationContext().getMetaContext());
Class<?> readClass = classInfo.getCls();
// replace target class if needed
if (targetClass != readClass) {
Expand All @@ -1423,8 +1422,8 @@ public ClassInfo readClassInfoWithMetaShare(MemoryBuffer buffer, Class<?> target
if (newClassInfo == null) {
// similar to create serializer for `NonexistentMetaShared`
newClassInfo =
getMetaSharedClassInfo(
classInfo.classDef.replaceRootClassTo(this, targetClass), targetClass);
getMetaSharedClassInfo(
classInfo.classDef.replaceRootClassTo(this, targetClass), targetClass);
extRegistry.transformedClassInfo.put(key, newClassInfo);
}
return newClassInfo;
Expand Down
94 changes: 94 additions & 0 deletions java/fury-extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -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.9.0</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>
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);
}
}
1 change: 1 addition & 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 Down
1 change: 1 addition & 0 deletions javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
package-lock.json
coverage
packages/hps/includes
2 changes: 1 addition & 1 deletion javascript/benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const Fury = require("@furyjs/fury");
const utils = require("@furyjs/fury/dist/lib/util");
const hps = require('@furyjs/hps');
const hps = require('@furyjs/hps').default;
const fury = new Fury.default({ hps, refTracking: false, useSliceString: true });
const Benchmark = require("benchmark");
const protobuf = require("protobufjs");
Expand Down
7 changes: 1 addition & 6 deletions javascript/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
*/

/** @type {import('ts-jest').JestConfigWithTsJest} */
const semver = require("semver");
const hpsEnable = semver.gt(process.versions.node, '20.0.0') && process.platform !== 'win32';

module.exports = {
collectCoverage: hpsEnable,
collectCoverage: true,
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: [
Expand All @@ -31,9 +29,6 @@ module.exports = {
"!**/build/**",
"!packages/fury/lib/murmurHash3.ts"
],
"testPathIgnorePatterns" : [
hpsEnable ? null : "(.*)/hps.test.ts$",
].filter(Boolean),
transform: {
'\\.ts$': ['ts-jest', {
tsconfig: {
Expand Down
Loading

0 comments on commit 015b852

Please sign in to comment.