af9ccb56f4e0960695783e2a3318f44773aa9d0b
[controller.git] / third-party / 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.esotericsoftware.kryo.Serializer;
20 import io.atomix.utils.serializer.Namespace;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.nio.ByteBuffer;
24
25 /**
26  * Support for serialization of {@link Journal} entries.
27  */
28 public interface JournalSerdes {
29     /**
30      * Serializes given object to byte array.
31      *
32      * @param obj Object to serialize
33      * @return serialized bytes
34      */
35     byte[] serialize(Object obj);
36
37     /**
38      * Serializes given object to byte array.
39      *
40      * @param obj        Object to serialize
41      * @param bufferSize maximum size of serialized bytes
42      * @return serialized bytes
43      */
44     byte[] serialize(Object obj, int bufferSize);
45
46     /**
47      * Serializes given object to byte buffer.
48      *
49      * @param obj    Object to serialize
50      * @param buffer to write to
51      */
52     void serialize(Object obj, ByteBuffer buffer);
53
54     /**
55      * Serializes given object to OutputStream.
56      *
57      * @param obj    Object to serialize
58      * @param stream to write to
59      */
60     void serialize(Object obj, OutputStream stream);
61
62     /**
63      * Serializes given object to OutputStream.
64      *
65      * @param obj        Object to serialize
66      * @param stream     to write to
67      * @param bufferSize size of the buffer in front of the stream
68      */
69     void serialize(Object obj, OutputStream stream, int bufferSize);
70
71     /**
72      * Deserializes given byte array to Object.
73      *
74      * @param bytes serialized bytes
75      * @param <T>   deserialized Object type
76      * @return deserialized Object
77      */
78     <T> T deserialize(byte[] bytes);
79
80     /**
81      * Deserializes given byte buffer to Object.
82      *
83      * @param buffer input with serialized bytes
84      * @param <T>    deserialized Object type
85      * @return deserialized Object
86      */
87     <T> T deserialize(final ByteBuffer buffer);
88
89     /**
90      * Deserializes given InputStream to an Object.
91      *
92      * @param stream input stream
93      * @param <T>    deserialized Object type
94      * @return deserialized Object
95      */
96     <T> T deserialize(InputStream stream);
97
98     /**
99      * Deserializes given InputStream to an Object.
100      *
101      * @param stream     input stream
102      * @param <T>        deserialized Object type
103      * @param bufferSize size of the buffer in front of the stream
104      * @return deserialized Object
105      */
106     <T> T deserialize(final InputStream stream, final int bufferSize);
107
108     /**
109      * Creates a new {@link JournalSerdes} builder.
110      *
111      * @return builder
112      */
113     static Builder builder() {
114         return Namespace.builder();
115     }
116
117     /**
118      * Builder for {@link JournalSerdes}.
119      */
120     interface Builder {
121         /**
122          * Builds a {@link JournalSerdes} instance.
123          *
124          * @return A {@link JournalSerdes} implementation.
125          */
126         JournalSerdes build();
127
128         /**
129          * Builds a {@link JournalSerdes} instance.
130          *
131          * @param friendlyName friendly name for the namespace
132          * @return A {@link JournalSerdes} implementation.
133          */
134         JournalSerdes build(String friendlyName);
135
136         /**
137          * Registers serializer for the given set of classes.
138          * <p>
139          * When multiple classes are registered with an explicitly provided serializer, the namespace guarantees
140          * all instances will be serialized with the same type ID.
141          *
142          * @param classes    list of classes to register
143          * @param serializer serializer to use for the class
144          * @return this builder
145          */
146         Builder register(Serializer<?> serializer, Class<?>... classes);
147
148         /**
149          * Sets the namespace class loader.
150          *
151          * @param classLoader the namespace class loader
152          * @return this builder
153          */
154         Builder setClassLoader(ClassLoader classLoader);
155     }
156 }