-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing a Writeable for Protobuf
Signed-off-by: Vacha Shah <[email protected]>
- Loading branch information
Showing
5 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
53 changes: 53 additions & 0 deletions
53
server/src/main/java/org/opensearch/common/io/stream/BaseWriteable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
server/src/main/java/org/opensearch/common/io/stream/ProtobufWriteable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
} |