From a3593b86e503a764f9c1f0ca40c14a738f35e3d6 Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Wed, 12 Apr 2023 18:47:43 +0000 Subject: [PATCH] Implementing a Writeable for Protobuf Signed-off-by: Vacha Shah --- .../licenses/protobuf-java-3.22.2.jar.sha1 | 1 - server/build.gradle | 1 + .../licenses/protobuf-java-3.22.2.jar.sha1 | 0 .../common/io/stream/BaseWriteable.java | 53 +++++++++++++ .../common/io/stream/ProtobufWriteable.java | 76 +++++++++++++++++++ 5 files changed, 130 insertions(+), 1 deletion(-) delete mode 100644 plugins/repository-hdfs/licenses/protobuf-java-3.22.2.jar.sha1 rename {plugins/repository-gcs => server}/licenses/protobuf-java-3.22.2.jar.sha1 (100%) create mode 100644 server/src/main/java/org/opensearch/common/io/stream/BaseWriteable.java create mode 100644 server/src/main/java/org/opensearch/common/io/stream/ProtobufWriteable.java diff --git a/plugins/repository-hdfs/licenses/protobuf-java-3.22.2.jar.sha1 b/plugins/repository-hdfs/licenses/protobuf-java-3.22.2.jar.sha1 deleted file mode 100644 index 80feeec023e7b..0000000000000 --- a/plugins/repository-hdfs/licenses/protobuf-java-3.22.2.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -fdee98b8f6abab73f146a4edb4c09e56f8278d03 \ No newline at end of file diff --git a/server/build.gradle b/server/build.gradle index 2eea312699798..c2040011304cf 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -115,6 +115,7 @@ dependencies { // utilities api project(":libs:opensearch-cli") api 'com.carrotsearch:hppc:0.8.1' + api 'com.google.protobuf:protobuf-java:3.22.2' // time handling, remove with java 8 time api "joda-time:joda-time:${versions.joda}" diff --git a/plugins/repository-gcs/licenses/protobuf-java-3.22.2.jar.sha1 b/server/licenses/protobuf-java-3.22.2.jar.sha1 similarity index 100% rename from plugins/repository-gcs/licenses/protobuf-java-3.22.2.jar.sha1 rename to server/licenses/protobuf-java-3.22.2.jar.sha1 diff --git a/server/src/main/java/org/opensearch/common/io/stream/BaseWriteable.java b/server/src/main/java/org/opensearch/common/io/stream/BaseWriteable.java new file mode 100644 index 0000000000000..7d7fa18bb6afa --- /dev/null +++ b/server/src/main/java/org/opensearch/common/io/stream/BaseWriteable.java @@ -0,0 +1,53 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.common.io.stream; + +import java.io.IOException; + +/** + * This interface can be extended to different types of serialization and deserialization mechanisms. + * + * @opensearch.internal + */ +public interface BaseWriteable { + + /** + * Write this into the stream output. + */ + void writeTo(T out) throws IOException; + + /** + * Reference to a method that can write some object to a given type. + */ + @FunctionalInterface + interface Writer { + + /** + * Write {@code V}-type {@code value} to the {@code T}-type stream. + * + * @param out Output to write the {@code value} too + * @param value The value to add + */ + void write(T out, V value) throws IOException; + } + + /** + * Reference to a method that can read some object from a given stream type. + */ + @FunctionalInterface + interface Reader { + + /** + * Read {@code V}-type value from a {@code T}-type stream. + * + * @param in Input to read the value from + */ + V read(S in) throws IOException; + } +} diff --git a/server/src/main/java/org/opensearch/common/io/stream/ProtobufWriteable.java b/server/src/main/java/org/opensearch/common/io/stream/ProtobufWriteable.java new file mode 100644 index 0000000000000..a5d5201c52022 --- /dev/null +++ b/server/src/main/java/org/opensearch/common/io/stream/ProtobufWriteable.java @@ -0,0 +1,76 @@ +/* +* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +/* +* Modifications Copyright OpenSearch Contributors. See +* GitHub history for details. +*/ + +package org.opensearch.common.io.stream; + +import java.io.IOException; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; + +/** + * Implementers can be written to write to output and read from input using Protobuf. +* +* @opensearch.internal +*/ +public interface ProtobufWriteable extends BaseWriteable { + + /** + * Write this into the stream output. + */ + public abstract void writeTo(com.google.protobuf.CodedOutputStream out) throws IOException; + + /** + * Reference to a method that can write some object to a {@link com.google.protobuf.CodedOutputStream}. + * Most classes should implement {@link ProtobufWriteable} and the {@link ProtobufWriteable#writeTo(CodedOutputStream)} method should use + * {@link CodedOutputStream} methods directly or this indirectly: + *

+     * public void writeTo(CodedOutputStream out) throws IOException {
+     *     out.writeVInt(someValue);
+     * }
+     * 
+ */ + @FunctionalInterface + interface Writer { + + /** + * Write {@code V}-type {@code value} to the {@code out}put stream. + * + * @param out Output to write the {@code value} too + * @param value The value to add + */ + void write(com.google.protobuf.CodedOutputStream out, V value) throws IOException; + + } + + /** + * Reference to a method that can read some object from a stream. By convention this is a constructor that takes + * {@linkplain com.google.protobuf.CodedInputStream} as an argument for most classes and a static method for things like enums. + *

+     * public MyClass(final CodedInputStream in) throws IOException {
+     *     this.someValue = in.readVInt();
+     * }
+     * 
+ */ + @FunctionalInterface + interface Reader { + + /** + * Read {@code V}-type value from a stream. + * + * @param in Input to read the value from + */ + V read(com.google.protobuf.CodedInputStream in) throws IOException; + + } + +}