Skip to content

Commit

Permalink
Implementing a Writeable for Protobuf
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <[email protected]>
  • Loading branch information
VachaShah committed Apr 12, 2023
1 parent b4e361c commit a3593b8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.

This file was deleted.

1 change: 1 addition & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T, S> {

/**
* 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<T, V> {

/**
* 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<S, V> {

/**
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<com.google.protobuf.CodedOutputStream, com.google.protobuf.CodedInputStream> {

/**
* 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 <em>use</em>
* {@link CodedOutputStream} methods directly or this indirectly:
* <pre><code>
* public void writeTo(CodedOutputStream out) throws IOException {
* out.writeVInt(someValue);
* }
* </code></pre>
*/
@FunctionalInterface
interface Writer<V> {

/**
* 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.
* <pre><code>
* public MyClass(final CodedInputStream in) throws IOException {
* this.someValue = in.readVInt();
* }
* </code></pre>
*/
@FunctionalInterface
interface Reader<V> {

/**
* 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;

}

}

0 comments on commit a3593b8

Please sign in to comment.