Centralize IO buffer allocation
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / JournalSerdes.java
1 /*
2  * Copyright 2014-2021 Open Networking Foundation
3  * Copyright 2023 PANTHEON.tech, s.r.o.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package io.atomix.storage.journal;
18
19 import com.google.common.annotations.Beta;
20 import com.google.common.annotations.VisibleForTesting;
21 import io.atomix.utils.serializer.KryoJournalSerdesBuilder;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.ByteBuffer;
26
27 /**
28  * Support for serialization of {@link Journal} entries.
29  *
30  * @deprecated due to dependency on outdated Kryo library, {@link JournalSerializer} to be used instead.
31  */
32 @Deprecated(forRemoval = true, since="9.0.3")
33 public interface JournalSerdes {
34     /**
35      * Serializes given object to byte array.
36      *
37      * @param obj Object to serialize
38      * @return serialized bytes
39      */
40     byte[] serialize(Object obj);
41
42     /**
43      * Serializes given object to byte array.
44      *
45      * @param obj        Object to serialize
46      * @param bufferSize maximum size of serialized bytes
47      * @return serialized bytes
48      */
49     byte[] serialize(Object obj, int bufferSize);
50
51     /**
52      * Serializes given object to byte buffer.
53      *
54      * @param obj    Object to serialize
55      * @param buffer to write to
56      */
57     void serialize(Object obj, ByteBuffer buffer);
58
59     /**
60      * Serializes given object to OutputStream.
61      *
62      * @param obj    Object to serialize
63      * @param stream to write to
64      */
65     void serialize(Object obj, OutputStream stream);
66
67     /**
68      * Serializes given object to OutputStream.
69      *
70      * @param obj        Object to serialize
71      * @param stream     to write to
72      * @param bufferSize size of the buffer in front of the stream
73      */
74     void serialize(Object obj, OutputStream stream, int bufferSize);
75
76     /**
77      * Deserializes given byte array to Object.
78      *
79      * @param bytes serialized bytes
80      * @param <T>   deserialized Object type
81      * @return deserialized Object
82      */
83     <T> T deserialize(byte[] bytes);
84
85     /**
86      * Deserializes given byte buffer to Object.
87      *
88      * @param buffer input with serialized bytes
89      * @param <T>    deserialized Object type
90      * @return deserialized Object
91      */
92     <T> T deserialize(final ByteBuffer buffer);
93
94     /**
95      * Deserializes given InputStream to an Object.
96      *
97      * @param stream input stream
98      * @param <T>    deserialized Object type
99      * @return deserialized Object
100      */
101     <T> T deserialize(InputStream stream);
102
103     /**
104      * Deserializes given InputStream to an Object.
105      *
106      * @param stream     input stream
107      * @param <T>        deserialized Object type
108      * @param bufferSize size of the buffer in front of the stream
109      * @return deserialized Object
110      */
111     <T> T deserialize(final InputStream stream, final int bufferSize);
112
113     /**
114      * Creates a new {@link JournalSerdes} builder.
115      *
116      * @return builder
117      */
118     static Builder builder() {
119         return new KryoJournalSerdesBuilder();
120     }
121
122     /**
123      * Builder for {@link JournalSerdes}.
124      */
125     interface Builder {
126         /**
127          * Builds a {@link JournalSerdes} instance.
128          *
129          * @return A {@link JournalSerdes} implementation.
130          */
131         JournalSerdes build();
132
133         /**
134          * Builds a {@link JournalSerdes} instance.
135          *
136          * @param friendlyName friendly name for the namespace
137          * @return A {@link JournalSerdes} implementation.
138          */
139         JournalSerdes build(String friendlyName);
140
141         /**
142          * Registers serializer for the given set of classes.
143          * <p>
144          * When multiple classes are registered with an explicitly provided serializer, the namespace guarantees
145          * all instances will be serialized with the same type ID.
146          *
147          * @param classes list of classes to register
148          * @param serdes  serializer to use for the class
149          * @return this builder
150          */
151         Builder register(EntrySerdes<?> serdes, Class<?>... classes);
152
153         /**
154          * Sets the namespace class loader.
155          *
156          * @param classLoader the namespace class loader
157          * @return this builder
158          */
159         Builder setClassLoader(ClassLoader classLoader);
160     }
161
162     /**
163      * Input data stream exposed to {@link EntrySerdes#read(EntryInput)}.
164      */
165     @Beta
166     interface EntryInput {
167
168         byte[] readBytes(int length) throws IOException;
169
170         long readLong() throws IOException;
171
172         String readString() throws IOException;
173
174         Object readObject() throws IOException;
175
176         @VisibleForTesting
177         int readVarInt() throws IOException;
178     }
179
180     /**
181      * Output data stream exposed to {@link EntrySerdes#write(EntryOutput, Object)}.
182      */
183     @Beta
184     interface EntryOutput {
185
186         void writeBytes(byte[] bytes) throws IOException;
187
188         void writeLong(long value) throws IOException;
189
190         void writeObject(Object value) throws IOException;
191
192         void writeString(String value) throws IOException;
193
194         @VisibleForTesting
195         void writeVarInt(int value) throws IOException;
196     }
197
198     /**
199      * A serializer/deserializer for an entry.
200      *
201      * @param <T> Entry type
202      */
203     interface EntrySerdes<T> {
204
205         T read(EntryInput input) throws IOException;
206
207         void write(EntryOutput output, T entry) throws IOException;
208     }
209 }