2 * Copyright 2014-2021 Open Networking Foundation
3 * Copyright 2023 PANTHEON.tech, s.r.o.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.atomix.storage.journal;
19 import com.google.common.annotations.Beta;
20 import com.google.common.annotations.VisibleForTesting;
21 import io.atomix.utils.serializer.KryoJournalSerdesBuilder;
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.ByteBufUtil;
24 import io.netty.buffer.Unpooled;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.nio.ByteBuffer;
31 * Support for serialization of {@link Journal} entries.
33 * @deprecated due to dependency on outdated Kryo library, {@link ByteBufMapper} to be used instead.
35 @Deprecated(forRemoval = true, since="9.0.3")
36 public interface JournalSerdes {
38 * Serializes given object to byte array.
40 * @param obj Object to serialize
41 * @return serialized bytes
43 byte[] serialize(Object obj);
46 * Serializes given object to byte array.
48 * @param obj Object to serialize
49 * @param bufferSize maximum size of serialized bytes
50 * @return serialized bytes
52 byte[] serialize(Object obj, int bufferSize);
55 * Serializes given object to byte buffer.
57 * @param obj Object to serialize
58 * @param buffer to write to
60 void serialize(Object obj, ByteBuffer buffer);
63 * Serializes given object to OutputStream.
65 * @param obj Object to serialize
66 * @param stream to write to
68 void serialize(Object obj, OutputStream stream);
71 * Serializes given object to OutputStream.
73 * @param obj Object to serialize
74 * @param stream to write to
75 * @param bufferSize size of the buffer in front of the stream
77 void serialize(Object obj, OutputStream stream, int bufferSize);
80 * Deserializes given byte array to Object.
82 * @param bytes serialized bytes
83 * @param <T> deserialized Object type
84 * @return deserialized Object
86 <T> T deserialize(byte[] bytes);
89 * Deserializes given byte buffer to Object.
91 * @param buffer input with serialized bytes
92 * @param <T> deserialized Object type
93 * @return deserialized Object
95 <T> T deserialize(final ByteBuffer buffer);
98 * Deserializes given InputStream to an Object.
100 * @param stream input stream
101 * @param <T> deserialized Object type
102 * @return deserialized Object
104 <T> T deserialize(InputStream stream);
107 * Deserializes given InputStream to an Object.
109 * @param stream input stream
110 * @param <T> deserialized Object type
111 * @param bufferSize size of the buffer in front of the stream
112 * @return deserialized Object
114 <T> T deserialize(final InputStream stream, final int bufferSize);
117 * Returns a {@link ByteBufMapper} backed by this object.
119 * @return a {@link ByteBufMapper} backed by this object
121 default <T> ByteBufMapper<T> toMapper() {
122 return new ByteBufMapper<>() {
124 public ByteBuf objectToBytes(final T obj) {
125 return Unpooled.wrappedBuffer(serialize(obj));
129 public T bytesToObject(final ByteBuf buf) {
130 // FIXME: ByteBufUtil creates a copy -- we do not want to do that!
131 return deserialize(ByteBufUtil.getBytes(buf));
137 * Creates a new {@link JournalSerdes} builder.
141 static Builder builder() {
142 return new KryoJournalSerdesBuilder();
146 * Builder for {@link JournalSerdes}.
150 * Builds a {@link JournalSerdes} instance.
152 * @return A {@link JournalSerdes} implementation.
154 JournalSerdes build();
157 * Builds a {@link JournalSerdes} instance.
159 * @param friendlyName friendly name for the namespace
160 * @return A {@link JournalSerdes} implementation.
162 JournalSerdes build(String friendlyName);
165 * Registers serializer for the given set of classes.
167 * When multiple classes are registered with an explicitly provided serializer, the namespace guarantees
168 * all instances will be serialized with the same type ID.
170 * @param classes list of classes to register
171 * @param serdes serializer to use for the class
172 * @return this builder
174 Builder register(EntrySerdes<?> serdes, Class<?>... classes);
177 * Sets the namespace class loader.
179 * @param classLoader the namespace class loader
180 * @return this builder
182 Builder setClassLoader(ClassLoader classLoader);
186 * Input data stream exposed to {@link EntrySerdes#read(EntryInput)}.
189 interface EntryInput {
191 byte[] readBytes(int length) throws IOException;
193 long readLong() throws IOException;
195 String readString() throws IOException;
197 Object readObject() throws IOException;
200 int readVarInt() throws IOException;
204 * Output data stream exposed to {@link EntrySerdes#write(EntryOutput, Object)}.
207 interface EntryOutput {
209 void writeBytes(byte[] bytes) throws IOException;
211 void writeLong(long value) throws IOException;
213 void writeObject(Object value) throws IOException;
215 void writeString(String value) throws IOException;
218 void writeVarInt(int value) throws IOException;
222 * A serializer/deserializer for an entry.
224 * @param <T> Entry type
226 interface EntrySerdes<T> {
228 T read(EntryInput input) throws IOException;
230 void write(EntryOutput output, T entry) throws IOException;