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.esotericsoftware.kryo.KryoException;
20 import com.google.common.annotations.Beta;
21 import com.google.common.annotations.VisibleForTesting;
22 import io.atomix.utils.serializer.KryoJournalSerdesBuilder;
23 import io.netty.buffer.ByteBuf;
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 void objectToBytes(final T obj, final ByteBuf bytes) throws IOException {
124 final var buffer = bytes.nioBuffer();
126 serialize(obj, buffer);
127 } catch (KryoException e) {
128 throw new IOException(e);
130 // adjust writerIndex so that readableBytes() the bytes written
131 bytes.writerIndex(bytes.readerIndex() + buffer.position());
136 public T bytesToObject(final long index, final ByteBuf bytes) {
137 return deserialize(bytes.nioBuffer());
143 * Creates a new {@link JournalSerdes} builder.
147 static Builder builder() {
148 return new KryoJournalSerdesBuilder();
152 * Builder for {@link JournalSerdes}.
156 * Builds a {@link JournalSerdes} instance.
158 * @return A {@link JournalSerdes} implementation.
160 JournalSerdes build();
163 * Builds a {@link JournalSerdes} instance.
165 * @param friendlyName friendly name for the namespace
166 * @return A {@link JournalSerdes} implementation.
168 JournalSerdes build(String friendlyName);
171 * Registers serializer for the given set of classes.
174 * When multiple classes are registered with an explicitly provided serializer, the namespace guarantees
175 * all instances will be serialized with the same type ID.
177 * @param classes list of classes to register
178 * @param serdes serializer to use for the class
179 * @return this builder
181 Builder register(EntrySerdes<?> serdes, Class<?>... classes);
184 * Sets the namespace class loader.
186 * @param classLoader the namespace class loader
187 * @return this builder
189 Builder setClassLoader(ClassLoader classLoader);
193 * Input data stream exposed to {@link EntrySerdes#read(EntryInput)}.
196 interface EntryInput {
198 byte[] readBytes(int length) throws IOException;
200 long readLong() throws IOException;
202 String readString() throws IOException;
204 Object readObject() throws IOException;
207 int readVarInt() throws IOException;
211 * Output data stream exposed to {@link EntrySerdes#write(EntryOutput, Object)}.
214 interface EntryOutput {
216 void writeBytes(byte[] bytes) throws IOException;
218 void writeLong(long value) throws IOException;
220 void writeObject(Object value) throws IOException;
222 void writeString(String value) throws IOException;
225 void writeVarInt(int value) throws IOException;
229 * A serializer/deserializer for an entry.
231 * @param <T> Entry type
233 interface EntrySerdes<T> {
235 T read(EntryInput input) throws IOException;
237 void write(EntryOutput output, T entry) throws IOException;