ni-ze commented on code in PR #81:
URL: 
https://github.com/apache/rocketmq-schema-registry/pull/81#discussion_r1175965105


##########
storage-jdbc/src/main/java/org/apache/rocketmq/schema/registry/storage/jdbc/handler/SchemaHandler.java:
##########
@@ -0,0 +1,289 @@
+/*
+ * 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.rocketmq.schema.registry.storage.jdbc.handler;
+
+import com.google.common.collect.Lists;
+import com.hazelcast.config.Config;
+import com.hazelcast.core.EntryEvent;
+import com.hazelcast.core.Hazelcast;
+import com.hazelcast.core.HazelcastInstance;
+import com.hazelcast.map.IMap;
+import com.hazelcast.map.listener.EntryAddedListener;
+import com.hazelcast.map.listener.EntryRemovedListener;
+import com.hazelcast.map.listener.EntryUpdatedListener;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.schema.registry.common.QualifiedName;
+import org.apache.rocketmq.schema.registry.common.constant.SchemaConstants;
+import 
org.apache.rocketmq.schema.registry.common.context.StorageServiceContext;
+import org.apache.rocketmq.schema.registry.common.exception.SchemaException;
+import 
org.apache.rocketmq.schema.registry.common.exception.SchemaExistException;
+import 
org.apache.rocketmq.schema.registry.common.exception.SchemaNotFoundException;
+import org.apache.rocketmq.schema.registry.common.model.SchemaInfo;
+import org.apache.rocketmq.schema.registry.common.model.SchemaRecordInfo;
+import org.apache.rocketmq.schema.registry.common.model.SubjectInfo;
+import org.springframework.util.CollectionUtils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static 
org.apache.rocketmq.schema.registry.storage.jdbc.store.JdbcSchemaMapStore.SCHEMAS;
+import static 
org.apache.rocketmq.schema.registry.storage.jdbc.store.JdbcSubjectMapStore.SUBJECTS;
+
+@Slf4j
+public class SchemaHandler extends IHandler {
+    private final IMap<String, SchemaInfo> schemas;
+    private final IMap<String, SchemaRecordInfo> subjects;
+    private final HazelcastInstance hazelcastInstance;
+
+    public SchemaHandler(String hazelcastYamlConfigPath) {
+        Config config;
+        try {
+            config = Config.loadFromFile(new File(hazelcastYamlConfigPath));
+        } catch (FileNotFoundException e) {
+            throw new SchemaException(String.format("File [%s] not found", 
hazelcastYamlConfigPath), e);
+        }
+
+        this.hazelcastInstance = Hazelcast.newHazelcastInstance(config);
+        this.subjects = this.hazelcastInstance.getMap(SUBJECTS);
+        this.subjects.loadAll(true);
+
+        this.schemas = this.hazelcastInstance.getMap(SCHEMAS);
+        this.schemas.loadAll(true);
+        this.schemas.addEntryListener(new 
SchemaChangeEntryListener(this.subjects), true);
+        loadAllSubject();
+    }
+
+    private void loadAllSubject() {
+        for (Map.Entry<String, SchemaInfo> schema : schemas.entrySet()) {
+            SchemaInfo schemaInfo = schema.getValue();
+            List<SchemaRecordInfo> allSchemaRecords = 
schemaInfo.getDetails().getSchemaRecords();
+            for (SchemaRecordInfo record : allSchemaRecords) {
+                List<String> recordSubjects =
+                        
record.getSubjects().stream().map(SubjectInfo::fullName).collect(Collectors.toList());
+                recordSubjects.forEach(subject -> {
+                    subjects.put(subject, record);
+                });
+            }
+        }
+    }
+
+    @Override
+    public void register(SchemaInfo schema) {
+        if (schemas.containsKey(schema.schemaFullName())) {
+            throw new SchemaExistException(schema.getQualifiedName());
+        }
+        schemas.put(schema.schemaFullName(), schema);
+    }
+
+    /**
+     * update schema
+     *
+     * @param update
+     */
+    @Override
+    public void updateSchema(SchemaInfo update) {
+        if (!schemas.containsKey(update.schemaFullName())) {
+            throw new SchemaNotFoundException(update.getQualifiedName());
+        }
+        // Get lock
+        schemas.lock(update.schemaFullName());
+        try {
+            SchemaInfo current = schemas.get(update.schemaFullName());
+            boolean hasVersionDeleted = current.getRecordCount() > 
update.getRecordCount();
+            if (current.getLastModifiedTime() != null && 
update.getLastModifiedTime() != null &&
+                    
current.getLastModifiedTime().after(update.getLastModifiedTime())) {
+                log.info("Current Schema is later version, no need to 
update.");
+                return;
+            }
+            if (current.getLastRecordVersion() == 
update.getLastRecordVersion() && !hasVersionDeleted) {
+                log.info("Schema version is the same, no need to update.");
+                return;
+            }
+            if (current.getLastRecordVersion() > update.getLastRecordVersion() 
&& !hasVersionDeleted) {
+                throw new SchemaException("Schema version is invalid, update: "
+                        + update.getLastRecordVersion() + ", but current: " + 
current.getLastRecordVersion());
+            }
+            schemas.put(update.schemaFullName(), update);
+        } finally {
+            // unlock
+            schemas.unlock(update.schemaFullName());
+        }
+    }
+
+    @Override
+    public void deleteSchema(QualifiedName qualifiedName) {
+        schemas.lock(qualifiedName.schemaFullName());
+        try {
+            if (!schemas.containsKey(qualifiedName.schemaFullName())) {
+                throw new SchemaNotFoundException(qualifiedName);
+            }
+            schemas.delete(qualifiedName);
+        } finally {
+            schemas.unlock(qualifiedName.schemaFullName());
+        }
+    }
+
+    @Override
+    public void deleteBySubject(QualifiedName qualifiedName) {
+        schemas.lock(qualifiedName.subjectFullName());
+        try {
+            SchemaInfo schemaInfo = 
getSchemaInfoBySubject(qualifiedName.subjectFullName());
+            if (schemaInfo == null) {
+                throw new SchemaNotFoundException(qualifiedName);
+            }
+            schemas.delete(schemaInfo.schemaFullName());
+        } finally {
+            schemas.unlock(qualifiedName.subjectFullName());
+        }
+    }
+
+    @Override
+    public void deleteByVersion(QualifiedName name) {
+        SchemaInfo schemaInfo = getSchemaInfoBySubject(name.subjectFullName());
+        if (schemaInfo == null || schemaInfo.getDetails() == null || 
schemaInfo.getDetails().getSchemaRecords() == null) {
+            throw new SchemaNotFoundException(name);
+        }

Review Comment:
   Maybe return null is better. The same as getBySubject method. 



-- 
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