PakhomovAlexander commented on code in PR #3563:
URL: https://github.com/apache/ignite-3/pull/3563#discussion_r1554562093


##########
modules/eventlog/src/test/java/org/apache/ignite/internal/eventlog/ser/EventSerializerRegistryImplTest.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.eventlog.ser;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.util.Collections;
+import java.util.Map;
+import org.apache.ignite.internal.eventlog.api.Event;
+import org.apache.ignite.internal.eventlog.event.EventImpl;
+import org.apache.ignite.internal.eventlog.event.EventUser;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+class EventSerializerRegistryImplTest {

Review Comment:
   That is a nice test that is showing the use case of the registry. I would 
like to see integration tests that shows the following: 
   
   - I can define a custom event and actually serialize it and see the result 
of the serialization. 
   - I can put a custom type into `EventImpl#fields()`, register a serializer 
for this type and see the result of the serialization (probably won't pass). 
   
   You can use existing test `JsonEventSerializerTest`.



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/ser/JacksonBasedJsonSerializer.java:
##########
@@ -18,26 +18,26 @@
 package org.apache.ignite.internal.eventlog.ser;
 
 import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonSerializer;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializerProvider;
 import com.fasterxml.jackson.databind.module.SimpleModule;
 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
 import java.io.IOException;
 import org.apache.ignite.internal.eventlog.api.Event;
-import org.apache.ignite.internal.eventlog.event.EventImpl;
 import org.apache.ignite.internal.eventlog.event.EventUser;
 import org.apache.ignite.internal.lang.IgniteInternalException;
 import org.apache.ignite.lang.ErrorGroups.Common;
 
-/** Serializes events to JSON. */
-public class JsonEventSerializer implements EventSerializer {
+/** Serializes events to JSON. Uses provided json serializer to serialize 
events of specified class. */
+public class JacksonBasedJsonSerializer<T extends Event> implements 
EventSerializer {
     private final ObjectMapper mapper;
 
     /** Default constructor. */
-    public JsonEventSerializer() {
+    public JacksonBasedJsonSerializer(Class<T> eventClass, JsonSerializer<T> 
eventJsonSerializer) {
         mapper = new ObjectMapper();
         SimpleModule module = new SimpleModule();
-        module.addSerializer(EventImpl.class, new 
EventImplJacksonSerializer());
+        module.addSerializer(eventClass, eventJsonSerializer);
         module.addSerializer(EventUser.class, new 
EventUserJacksonSerializer());

Review Comment:
   Can you please define such a registry that can be used to register a 
serializer for any type, including EventUser? I don't like that we can register 
the serializer for the Event but can not register a serializer for EventUser 
with the same approach.



##########
modules/eventlog/src/main/java/org/apache/ignite/internal/eventlog/ser/EventSerializerRegistryImpl.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.eventlog.ser;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.ignite.internal.eventlog.api.Event;
+
+public class EventSerializerRegistryImpl implements EventSerializerRegistry {
+    private final ReadWriteLock guard = new ReentrantReadWriteLock();
+    private final Map<Class<? extends Event>, EventSerializer> serializers = 
new HashMap<>();
+
+
+    @Override
+    public void register(Class<? extends Event> eventClass, EventSerializer 
eventSerializer) {
+        guard.writeLock().lock();
+        try {
+            if (serializers.containsKey(eventClass)) {
+                throw new IllegalStateException("EventSerializer for class " + 
eventClass.getCanonicalName() + " is already registered");
+            } else {
+                serializers.put(eventClass, eventSerializer);
+            }
+        } finally {
+            guard.writeLock().unlock();
+        }
+    }
+
+    @Override
+    public EventSerializer findSerializer(Class<? extends Event> eventClass) {

Review Comment:
   ```suggestion
       @Nullable
       public EventSerializer findSerializer(Class<? extends Event> eventClass) 
{
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to