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.Unpooled;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.ByteBuffer;
30 * Support for serialization of {@link Journal} entries.
32 * @deprecated due to dependency on outdated Kryo library, {@link ByteBufMapper} to be used instead.
34 @Deprecated(forRemoval = true, since = "9.0.3")
35 public interface JournalSerdes {
37 * Serializes given object to byte array.
39 * @param obj Object to serialize
40 * @return serialized bytes
42 byte[] serialize(Object obj);
45 * Serializes given object to byte array.
47 * @param obj Object to serialize
48 * @param bufferSize maximum size of serialized bytes
49 * @return serialized bytes
51 byte[] serialize(Object obj, int bufferSize);
54 * Serializes given object to byte buffer.
56 * @param obj Object to serialize
57 * @param buffer to write to
59 void serialize(Object obj, ByteBuffer buffer);
62 * Serializes given object to OutputStream.
64 * @param obj Object to serialize
65 * @param stream to write to
67 void serialize(Object obj, OutputStream stream);
70 * Serializes given object to OutputStream.
72 * @param obj Object to serialize
73 * @param stream to write to
74 * @param bufferSize size of the buffer in front of the stream
76 void serialize(Object obj, OutputStream stream, int bufferSize);
79 * Deserializes given byte array to Object.
81 * @param bytes serialized bytes
82 * @param <T> deserialized Object type
83 * @return deserialized Object
85 <T> T deserialize(byte[] bytes);
88 * Deserializes given byte buffer to Object.
90 * @param buffer input with serialized bytes
91 * @param <T> deserialized Object type
92 * @return deserialized Object
94 <T> T deserialize(ByteBuffer buffer);
97 * Deserializes given InputStream to an Object.
99 * @param stream input stream
100 * @param <T> deserialized Object type
101 * @return deserialized Object
103 <T> T deserialize(InputStream stream);
106 * Deserializes given InputStream to an Object.
108 * @param stream input stream
109 * @param <T> deserialized Object type
110 * @param bufferSize size of the buffer in front of the stream
111 * @return deserialized Object
113 <T> T deserialize(InputStream stream, int bufferSize);
116 * Returns a {@link ByteBufMapper} backed by this object.
118 * @return a {@link ByteBufMapper} backed by this object
120 default <T> ByteBufMapper<T> toMapper() {
121 return new ByteBufMapper<>() {
123 public ByteBuf objectToBytes(final T obj) {
124 return Unpooled.wrappedBuffer(serialize(obj));
128 public T bytesToObject(final ByteBuf buf) {
129 return deserialize(buf.nioBuffer());
135 * Creates a new {@link JournalSerdes} builder.
139 static Builder builder() {
140 return new KryoJournalSerdesBuilder();
144 * Builder for {@link JournalSerdes}.
148 * Builds a {@link JournalSerdes} instance.
150 * @return A {@link JournalSerdes} implementation.
152 JournalSerdes build();
155 * Builds a {@link JournalSerdes} instance.
157 * @param friendlyName friendly name for the namespace
158 * @return A {@link JournalSerdes} implementation.
160 JournalSerdes build(String friendlyName);
163 * Registers serializer for the given set of classes.
166 * When multiple classes are registered with an explicitly provided serializer, the namespace guarantees
167 * all instances will be serialized with the same type ID.
169 * @param classes list of classes to register
170 * @param serdes serializer to use for the class
171 * @return this builder
173 Builder register(EntrySerdes<?> serdes, Class<?>... classes);
176 * Sets the namespace class loader.
178 * @param classLoader the namespace class loader
179 * @return this builder
181 Builder setClassLoader(ClassLoader classLoader);
185 * Input data stream exposed to {@link EntrySerdes#read(EntryInput)}.
188 interface EntryInput {
190 byte[] readBytes(int length) throws IOException;
192 long readLong() throws IOException;
194 String readString() throws IOException;
196 Object readObject() throws IOException;
199 int readVarInt() throws IOException;
203 * Output data stream exposed to {@link EntrySerdes#write(EntryOutput, Object)}.
206 interface EntryOutput {
208 void writeBytes(byte[] bytes) throws IOException;
210 void writeLong(long value) throws IOException;
212 void writeObject(Object value) throws IOException;
214 void writeString(String value) throws IOException;
217 void writeVarInt(int value) throws IOException;
221 * A serializer/deserializer for an entry.
223 * @param <T> Entry type
225 interface EntrySerdes<T> {
227 T read(EntryInput input) throws IOException;
229 void write(EntryOutput output, T entry) throws IOException;