Skip to content

Commit

Permalink
feat(kotlin): Introduce kotlin package with stdlib collections and te…
Browse files Browse the repository at this point in the history
…sts (#1877)



## What does this PR do?

This PR adds a base project for Kotlin on the JVM.
- It adds a pom.xml based on the java project.
- Adds a Serializer class that registers base collection classes not
covered by the JVM.
- Adds code to test all documented collections in the kotlin stdlib via
TestNG.
- Adds register and adds serializers for EmptyList, EmptySet, EmptyMap
classes.
- This PR does not handle kotlin `withDefault` wrapper classes, as that
requires serializing arbitrary lambdas.

## Related issues

- #683

## Does this PR introduce any user-facing change?

This PR creates a new fury-kotlin JAR, no documentation yet from the
main project.

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?

## Benchmark
N/A

---------

Co-authored-by: Shawn Yang <[email protected]>
  • Loading branch information
wywen and chaokunyang authored Oct 14, 2024
1 parent c8698b6 commit 0673c7b
Show file tree
Hide file tree
Showing 7 changed files with 626 additions and 0 deletions.
21 changes: 21 additions & 0 deletions kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Apache Fury™ Kotlin

This provides additional Fury support for Kotlin Serialization on JVM:

Most standard kotlin types are already supported out of the box with the default Fury java implementation.

Fury Kotlin provides additional tests and implementation support for Kotlin types.

Fury Kotlin is tested and works with the following types:

- stdlib `collection`: `ArrayDeque`, `ArrayList`, `HashMap`,`HashSet`, `LinkedHashSet`, `LinkedHashMap`.
- `ArrayList`, `HashMap`,`HashSet`, `LinkedHashSet`, `LinkedHashMap` works out of the box with the default java implementation.

Additional support is added for the following classes in kotlin:

- Empty collections: `emptyList`, `emptyMap`, `emptySet`
- Collections: `ArrayDeque`

Additional Notes:

- wrappers classes created from `withDefault` method is currently not supported.
182 changes: 182 additions & 0 deletions kotlin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?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">

<groupId>org.apache.fury</groupId>
<artifactId>fury-kotlin</artifactId>
<version>0.9.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<doclint>none</doclint>
<bottom>
Copyright © 2023-2024, The Apache Software Foundation. Apache Fury™, Fury™, and Apache
are either registered trademarks or trademarks of the Apache Software Foundation.
</bottom>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>2.0.20</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.fury</groupId>
<artifactId>fury-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-testng</artifactId>
<version>2.0.20</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>2.0.20</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.serializer.kotlin;

import org.apache.fury.Fury;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.serializer.collection.CollectionSerializers;
import org.apache.fury.serializer.collection.MapSerializers;


/**
* KotlinSerializers provide default serializers for kotlin.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class KotlinSerializers {
public static void registerSerializers(Fury fury) {
ClassResolver resolver = fury.getClassResolver();

// EmptyList
Class emptyListClass = KotlinToJavaClass.INSTANCE.getEmptyListClass();
resolver.register(emptyListClass);
resolver.registerSerializer(emptyListClass, new CollectionSerializers.EmptyListSerializer(fury, emptyListClass));

// EmptySet
Class emptySetClass = KotlinToJavaClass.INSTANCE.getEmptySetClass();
resolver.register(emptySetClass);
resolver.registerSerializer(emptySetClass, new CollectionSerializers.EmptySetSerializer(fury, emptySetClass));

// EmptyMap
Class emptyMapClass = KotlinToJavaClass.INSTANCE.getEmptyMapClass();
resolver.register(emptyMapClass);
resolver.registerSerializer(emptyMapClass, new MapSerializers.EmptyMapSerializer(fury, emptyMapClass));

// Non-Java collection implementation in kotlin stdlib.
Class arrayDequeClass = KotlinToJavaClass.INSTANCE.getArrayDequeClass();
resolver.register(arrayDequeClass);
resolver.registerSerializer(arrayDequeClass, new KotlinArrayDequeSerializer(fury, arrayDequeClass));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.serializer.kotlin

/**
* Collection builder interface adapts an iterable to allow incremental build by the fury protocol.
*/
interface CollectionBuilder <E, T: Iterable<E>> {
fun add(element: E): Boolean
fun result(): T
}

/**
* Abstract builder for [kotlin.collections.List] interface.
*/
abstract class AbstractListBuilder<E, T:List<E>>
: CollectionBuilder<E, T>, AdaptedCollection<E>() {
protected open val builder: MutableList<E> = mutableListOf()
override val size: Int
get() = builder.size
override fun add(element: E) =
builder.add(element)
override fun iterator(): MutableIterator<E> = builder.iterator()
}

/**
* Builder for [kotlin.collections.ArrayDeque].
*/
class ArrayDequeBuilder<E>(
override val builder: ArrayDeque<E>
): AbstractListBuilder<E, ArrayDeque<E>>() {
override fun result(): ArrayDeque<E> = builder
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.serializer.kotlin

import org.apache.fury.Fury
import org.apache.fury.memory.MemoryBuffer
import org.apache.fury.serializer.collection.AbstractCollectionSerializer

/**
* Serializer for kotlin collections.
*/
@Suppress("UNCHECKED_CAST")
abstract class AbstractKotlinCollectionSerializer<E, T: Iterable<E>>(
fury: Fury,
cls: Class<T>
) : AbstractCollectionSerializer<T>(fury, cls) {
abstract override fun onCollectionWrite(buffer: MemoryBuffer, value: T): Collection<E>

override fun read(buffer: MemoryBuffer): T {
val collection = newCollection(buffer)
val numElements = getAndClearNumElements()
if (numElements != 0) readElements(fury, buffer, collection, numElements)
return onCollectionRead(collection)
}

override fun onCollectionRead(collection: Collection<*>): T {
val builder = collection as CollectionBuilder<E, T>
return builder.result()
}
}

/**
* Serializer for [[kotlin.collections.ArrayDeque]].
*/
class KotlinArrayDequeSerializer<E> (
fury: Fury,
cls: Class<ArrayDeque<E>>,
) : AbstractKotlinCollectionSerializer<E, ArrayDeque<E>>(fury, cls) {
override fun onCollectionWrite(buffer: MemoryBuffer, value: ArrayDeque<E>): Collection<E> {
val adapter = IterableAdapter<E>(value)
buffer.writeVarUint32Small7(adapter.size)
return adapter
}
override fun newCollection(buffer: MemoryBuffer): Collection<E> {
val numElements = buffer.readVarUint32()
setNumElements(numElements)
return ArrayDequeBuilder<E>(ArrayDeque<E>(numElements))
}
}

typealias AdaptedCollection<E> = java.util.AbstractCollection<E>

/**
* An adapter which wraps a kotlin iterable into a [[java.util.Collection]].
*/
private class IterableAdapter<E>(
coll: Iterable<E>
) : AdaptedCollection<E>() {
private val mutableList = coll.toMutableList()

override val size: Int
get() = mutableList.count()

override fun iterator(): MutableIterator<E> =
mutableList.iterator()
}
Loading

0 comments on commit 0673c7b

Please sign in to comment.