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 java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.ByteBuffer;
28 * Support for serialization of {@link Journal} entries.
30 * @deprecated due to dependency on outdated Kryo library, {@link JournalSerializer} to be used instead.
32 @Deprecated(forRemoval = true, since="9.0.3")
33 public interface JournalSerdes {
35 * Serializes given object to byte array.
37 * @param obj Object to serialize
38 * @return serialized bytes
40 byte[] serialize(Object obj);
43 * Serializes given object to byte array.
45 * @param obj Object to serialize
46 * @param bufferSize maximum size of serialized bytes
47 * @return serialized bytes
49 byte[] serialize(Object obj, int bufferSize);
52 * Serializes given object to byte buffer.
54 * @param obj Object to serialize
55 * @param buffer to write to
57 void serialize(Object obj, ByteBuffer buffer);
60 * Serializes given object to OutputStream.
62 * @param obj Object to serialize
63 * @param stream to write to
65 void serialize(Object obj, OutputStream stream);
68 * Serializes given object to OutputStream.
70 * @param obj Object to serialize
71 * @param stream to write to
72 * @param bufferSize size of the buffer in front of the stream
74 void serialize(Object obj, OutputStream stream, int bufferSize);
77 * Deserializes given byte array to Object.
79 * @param bytes serialized bytes
80 * @param <T> deserialized Object type
81 * @return deserialized Object
83 <T> T deserialize(byte[] bytes);
86 * Deserializes given byte buffer to Object.
88 * @param buffer input with serialized bytes
89 * @param <T> deserialized Object type
90 * @return deserialized Object
92 <T> T deserialize(final ByteBuffer buffer);
95 * Deserializes given InputStream to an Object.
97 * @param stream input stream
98 * @param <T> deserialized Object type
99 * @return deserialized Object
101 <T> T deserialize(InputStream stream);
104 * Deserializes given InputStream to an Object.
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
111 <T> T deserialize(final InputStream stream, final int bufferSize);
114 * Creates a new {@link JournalSerdes} builder.
118 static Builder builder() {
119 return new KryoJournalSerdesBuilder();
123 * Builder for {@link JournalSerdes}.
127 * Builds a {@link JournalSerdes} instance.
129 * @return A {@link JournalSerdes} implementation.
131 JournalSerdes build();
134 * Builds a {@link JournalSerdes} instance.
136 * @param friendlyName friendly name for the namespace
137 * @return A {@link JournalSerdes} implementation.
139 JournalSerdes build(String friendlyName);
142 * Registers serializer for the given set of classes.
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.
147 * @param classes list of classes to register
148 * @param serdes serializer to use for the class
149 * @return this builder
151 Builder register(EntrySerdes<?> serdes, Class<?>... classes);
154 * Sets the namespace class loader.
156 * @param classLoader the namespace class loader
157 * @return this builder
159 Builder setClassLoader(ClassLoader classLoader);
163 * Input data stream exposed to {@link EntrySerdes#read(EntryInput)}.
166 interface EntryInput {
168 byte[] readBytes(int length) throws IOException;
170 long readLong() throws IOException;
172 String readString() throws IOException;
174 Object readObject() throws IOException;
177 int readVarInt() throws IOException;
181 * Output data stream exposed to {@link EntrySerdes#write(EntryOutput, Object)}.
184 interface EntryOutput {
186 void writeBytes(byte[] bytes) throws IOException;
188 void writeLong(long value) throws IOException;
190 void writeObject(Object value) throws IOException;
192 void writeString(String value) throws IOException;
195 void writeVarInt(int value) throws IOException;
199 * A serializer/deserializer for an entry.
201 * @param <T> Entry type
203 interface EntrySerdes<T> {
205 T read(EntryInput input) throws IOException;
207 void write(EntryOutput output, T entry) throws IOException;