sijie closed pull request #2624: [schema] Add ByteBuf schema and fix ByteBuffer schema URL: https://github.com/apache/incubator-pulsar/pull/2624
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/ByteBufSchema.java b/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/ByteBufSchema.java new file mode 100644 index 0000000000..4e7e6d0d25 --- /dev/null +++ b/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/ByteBufSchema.java @@ -0,0 +1,65 @@ +/** + * 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.pulsar.client.impl.schema; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; +import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.common.schema.SchemaInfo; +import org.apache.pulsar.common.schema.SchemaType; + +/** + * A variant `Bytes` schema that takes {@link io.netty.buffer.ByteBuf}. + */ +public class ByteBufSchema implements Schema<ByteBuf> { + + public static ByteBufSchema of() { + return INSTANCE; + } + + private static final ByteBufSchema INSTANCE = new ByteBufSchema(); + private static final SchemaInfo SCHEMA_INFO = new SchemaInfo() + .setName("ByteBuf") + .setType(SchemaType.BYTES) + .setSchema(new byte[0]); + + @Override + public byte[] encode(ByteBuf message) { + if (message == null) { + return null; + } + + return ByteBufUtil.getBytes(message); + } + + @Override + public ByteBuf decode(byte[] bytes) { + if (null == bytes) { + return null; + } else { + return Unpooled.wrappedBuffer(bytes); + } + } + + @Override + public SchemaInfo getSchemaInfo() { + return SCHEMA_INFO; + } +} diff --git a/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/ByteBufferSchema.java b/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/ByteBufferSchema.java index ee8ba66fa6..251cd93dcb 100644 --- a/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/ByteBufferSchema.java +++ b/pulsar-client-schema/src/main/java/org/apache/pulsar/client/impl/schema/ByteBufferSchema.java @@ -24,7 +24,7 @@ import org.apache.pulsar.common.schema.SchemaType; /** - * A bytebuffer schema. + * A bytebuffer schema is effectively a `BYTES` schema. */ public class ByteBufferSchema implements Schema<ByteBuffer> { @@ -35,7 +35,7 @@ public static ByteBufferSchema of() { private static final ByteBufferSchema INSTANCE = new ByteBufferSchema(); private static final SchemaInfo SCHEMA_INFO = new SchemaInfo() .setName("ByteBuffer") - .setType(SchemaType.BYTEBUFFER) + .setType(SchemaType.BYTES) .setSchema(new byte[0]); @Override diff --git a/pulsar-client-schema/src/test/java/org/apache/pulsar/client/schema/PrimitiveSchemaTest.java b/pulsar-client-schema/src/test/java/org/apache/pulsar/client/schema/PrimitiveSchemaTest.java index fdac5395e2..aac2d8c5c1 100644 --- a/pulsar-client-schema/src/test/java/org/apache/pulsar/client/schema/PrimitiveSchemaTest.java +++ b/pulsar-client-schema/src/test/java/org/apache/pulsar/client/schema/PrimitiveSchemaTest.java @@ -18,9 +18,11 @@ */ package org.apache.pulsar.client.schema; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; +import io.netty.buffer.Unpooled; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.HashMap; @@ -28,6 +30,7 @@ import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.impl.schema.ByteBufSchema; import org.apache.pulsar.client.impl.schema.ByteBufferSchema; import org.apache.pulsar.client.impl.schema.ByteSchema; import org.apache.pulsar.client.impl.schema.BytesSchema; @@ -55,8 +58,9 @@ put(LongSchema.of(), Arrays.asList(922337203685477580L, -922337203685477581L)); put(FloatSchema.of(), Arrays.asList(5678567.12312f, -5678567.12341f)); put(DoubleSchema.of(), Arrays.asList(5678567.12312d, -5678567.12341d)); - put(BytesSchema.of(), Arrays.asList("my string".getBytes())); - put(ByteBufferSchema.of(), Arrays.asList(ByteBuffer.allocate(10).put("my string".getBytes()))); + put(BytesSchema.of(), Arrays.asList("my string".getBytes(UTF_8))); + put(ByteBufferSchema.of(), Arrays.asList(ByteBuffer.allocate(10).put("my string".getBytes(UTF_8)))); + put(ByteBufSchema.of(), Arrays.asList(Unpooled.wrappedBuffer("my string".getBytes(UTF_8)))); } }; @@ -94,8 +98,8 @@ public void allSchemasShouldHaveSchemaType() { assertEquals(SchemaType.DOUBLE, DoubleSchema.of().getSchemaInfo().getType()); assertEquals(SchemaType.STRING, StringSchema.utf8().getSchemaInfo().getType()); assertEquals(SchemaType.BYTES, BytesSchema.of().getSchemaInfo().getType()); - assertEquals(SchemaType.BYTEBUFFER, ByteBufferSchema.of().getSchemaInfo().getType()); - + assertEquals(SchemaType.BYTES, ByteBufferSchema.of().getSchemaInfo().getType()); + assertEquals(SchemaType.BYTES, ByteBufSchema.of().getSchemaInfo().getType()); } diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java b/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java index 87c0956a32..a19d587e17 100644 --- a/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/schema/SchemaType.java @@ -67,11 +67,6 @@ */ BYTES, - /** - * A bytebuffer. - */ - BYTEBUFFER, - /** * JSON object encoding and validation */ diff --git a/tests/scripts/pre-integ-tests.sh b/tests/scripts/pre-integ-tests.sh index ab83c53b84..1a465db1ab 100755 --- a/tests/scripts/pre-integ-tests.sh +++ b/tests/scripts/pre-integ-tests.sh @@ -24,7 +24,7 @@ ulimit -a pwd df -h ps -eo euser,pid,ppid,pgid,start,pcpu,pmem,cmd -docker network prune -f --filter name=pulsarnet_* +docker system prune -f docker system events > docker.debug-info & echo $! > docker-log.pid docker pull apachepulsar/s3mock:latest docker pull alpine/socat:latest ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
