You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2021/03/10 06:44:24 UTC

[flink-statefun] 01/01: [FLINK-21705] Add a SliceType

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

tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git

commit b1c50db25b9610e0362ab56dbd718b39d63cee2d
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
AuthorDate: Wed Mar 10 14:35:34 2021 +0800

    [FLINK-21705] Add a SliceType
    
    A SliceType is a type that simply treats the value as raw bytes. The
    serializer implementation simply forwards the bytes as is, without any
    copying. Since slices are immutable, the type is considered immutable.
    
    This type can come in handy if users would like to avoid serialization /
    deserialization in certain scenarios, e.g. sending state values as a
    message to another function.
    
    This closes #209.
---
 .../flink/statefun/sdk/java/types/SliceType.java   | 66 ++++++++++++++++++++++
 .../sdk/java/types/SanityPrimitiveTypeTest.java    |  9 +++
 2 files changed, 75 insertions(+)

diff --git a/statefun-sdk-java/src/main/java/org/apache/flink/statefun/sdk/java/types/SliceType.java b/statefun-sdk-java/src/main/java/org/apache/flink/statefun/sdk/java/types/SliceType.java
new file mode 100644
index 0000000..1a95462
--- /dev/null
+++ b/statefun-sdk-java/src/main/java/org/apache/flink/statefun/sdk/java/types/SliceType.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.flink.statefun.sdk.java.types;
+
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.flink.statefun.sdk.java.TypeName;
+import org.apache.flink.statefun.sdk.java.slice.Slice;
+
+public final class SliceType implements Type<Slice> {
+
+  private static final Set<TypeCharacteristics> IMMUTABLE_TYPE_CHARS =
+      Collections.unmodifiableSet(EnumSet.of(TypeCharacteristics.IMMUTABLE_VALUES));
+
+  private final TypeName typename;
+  private final Serializer serializer = new Serializer();
+
+  public SliceType(TypeName typename) {
+    this.typename = Objects.requireNonNull(typename);
+  }
+
+  @Override
+  public TypeName typeName() {
+    return typename;
+  }
+
+  @Override
+  public TypeSerializer<Slice> typeSerializer() {
+    return serializer;
+  }
+
+  @Override
+  public Set<TypeCharacteristics> typeCharacteristics() {
+    return IMMUTABLE_TYPE_CHARS;
+  }
+
+  private static final class Serializer implements TypeSerializer<Slice> {
+    @Override
+    public Slice serialize(Slice slice) {
+      return slice;
+    }
+
+    @Override
+    public Slice deserialize(Slice slice) {
+      return slice;
+    }
+  }
+}
diff --git a/statefun-sdk-java/src/test/java/org/apache/flink/statefun/sdk/java/types/SanityPrimitiveTypeTest.java b/statefun-sdk-java/src/test/java/org/apache/flink/statefun/sdk/java/types/SanityPrimitiveTypeTest.java
index 73650e8..01e7884 100644
--- a/statefun-sdk-java/src/test/java/org/apache/flink/statefun/sdk/java/types/SanityPrimitiveTypeTest.java
+++ b/statefun-sdk-java/src/test/java/org/apache/flink/statefun/sdk/java/types/SanityPrimitiveTypeTest.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 import java.util.concurrent.ThreadLocalRandom;
+import org.apache.flink.statefun.sdk.java.TypeName;
 import org.apache.flink.statefun.sdk.java.slice.Slice;
 import org.apache.flink.statefun.sdk.java.slice.Slices;
 import org.apache.flink.statefun.sdk.shaded.com.google.protobuf.InvalidProtocolBufferException;
@@ -80,6 +81,14 @@ public class SanityPrimitiveTypeTest {
   }
 
   @Test
+  public void testSlice() {
+    final TypeName typename = TypeName.typeNameOf("test.namespace", "test.name");
+    assertRoundTrip(
+        new SliceType(typename), Slices.wrap("payload-foobar".getBytes(StandardCharsets.UTF_8)));
+    assertRoundTrip(new SliceType(typename), Slices.wrap(new byte[] {}));
+  }
+
+  @Test
   public void testRandomCompatibilityWithAnIntegerWrapper() throws InvalidProtocolBufferException {
     TypeSerializer<Integer> serializer = Types.integerType().typeSerializer();
     for (int i = 0; i < 1_000_000; i++) {