You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by si...@apache.org on 2019/01/28 20:03:59 UTC
[pulsar] branch master updated: [pulsar-client] add BooleanSchema
(#3415)
This is an automated email from the ASF dual-hosted git repository.
sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new e8bb6de [pulsar-client] add BooleanSchema (#3415)
e8bb6de is described below
commit e8bb6de4121800dd7a19ae68030c413b0a5f3251
Author: wpl <12...@qq.com>
AuthorDate: Tue Jan 29 04:03:54 2019 +0800
[pulsar-client] add BooleanSchema (#3415)
---
.../apache/pulsar/common/schema/SchemaType.java | 6 ++
.../pulsar/client/impl/schema/BooleanSchema.java | 70 ++++++++++++++++++++++
.../client/impl/schema/PrimitiveSchemaTest.java | 2 +
3 files changed, 78 insertions(+)
diff --git a/pulsar-client-api/src/main/java/org/apache/pulsar/common/schema/SchemaType.java b/pulsar-client-api/src/main/java/org/apache/pulsar/common/schema/SchemaType.java
index 6073ca5..42884af 100644
--- a/pulsar-client-api/src/main/java/org/apache/pulsar/common/schema/SchemaType.java
+++ b/pulsar-client-api/src/main/java/org/apache/pulsar/common/schema/SchemaType.java
@@ -28,6 +28,12 @@ public enum SchemaType {
NONE,
/**
+ * boolean schema defined
+ * @since 2.3.0
+ */
+ BOOLEAN,
+
+ /**
* Simple String encoding with UTF-8
*/
STRING,
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/BooleanSchema.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/BooleanSchema.java
new file mode 100644
index 0000000..5b27dfc
--- /dev/null
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/BooleanSchema.java
@@ -0,0 +1,70 @@
+/**
+ * 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 org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.SchemaSerializationException;
+import org.apache.pulsar.common.schema.SchemaInfo;
+import org.apache.pulsar.common.schema.SchemaType;
+
+/**
+ * A schema for `Boolean`.
+ */
+public class BooleanSchema implements Schema<Boolean> {
+
+ public static BooleanSchema of() {
+ return INSTANCE;
+ }
+
+ private static final BooleanSchema INSTANCE = new BooleanSchema();
+ private static final SchemaInfo SCHEMA_INFO = new SchemaInfo()
+ .setName("Boolean")
+ .setType(SchemaType.BOOLEAN)
+ .setSchema(new byte[0]);
+
+ @Override
+ public void validate(byte[] message) {
+ if (message.length != 1) {
+ throw new SchemaSerializationException("Size of data received by BooleanSchema is not 1");
+ }
+ }
+
+ @Override
+ public byte[] encode(Boolean message) {
+ if (null == message) {
+ return null;
+ } else {
+ return new byte[]{(byte)(message ? 1 : 0)};
+ }
+ }
+
+ @Override
+ public Boolean decode(byte[] bytes) {
+ if (null == bytes) {
+ return null;
+ }
+ validate(bytes);
+ return bytes[0] != 0;
+ }
+
+ @Override
+ public SchemaInfo getSchemaInfo() {
+ return SCHEMA_INFO;
+ }
+}
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/PrimitiveSchemaTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/PrimitiveSchemaTest.java
index 0932bc3..0084997 100644
--- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/PrimitiveSchemaTest.java
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/PrimitiveSchemaTest.java
@@ -51,6 +51,7 @@ public class PrimitiveSchemaTest {
final private Map<Schema, List<Object>> testData = new HashMap() {
{
+ put(BooleanSchema.of(), Arrays.asList(false, true));
put(StringSchema.utf8(), Arrays.asList("my string"));
put(ByteSchema.of(), Arrays.asList((byte) 32767, (byte) -32768));
put(ShortSchema.of(), Arrays.asList((short) 32767, (short) -32768));
@@ -90,6 +91,7 @@ public class PrimitiveSchemaTest {
@Test
public void allSchemasShouldHaveSchemaType() {
+ assertEquals(SchemaType.BOOLEAN, BooleanSchema.of().getSchemaInfo().getType());
assertEquals(SchemaType.INT8, ByteSchema.of().getSchemaInfo().getType());
assertEquals(SchemaType.INT16, ShortSchema.of().getSchemaInfo().getType());
assertEquals(SchemaType.INT32, IntSchema.of().getSchemaInfo().getType());