You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "ni-ze (via GitHub)" <gi...@apache.org> on 2023/05/03 11:35:16 UTC

[GitHub] [rocketmq-schema-registry] ni-ze commented on a diff in pull request #81: [ISSUE #78]Storage supports jdbc

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


##########
storage-jdbc/src/main/java/org/apache/rocketmq/schema/registry/storage/jdbc/store/JdbcSchemaMapStore.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.store;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.hazelcast.core.HazelcastInstance;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.schema.registry.common.exception.SchemaException;
+import org.apache.rocketmq.schema.registry.common.json.JsonConverter;
+import org.apache.rocketmq.schema.registry.common.json.JsonConverterImpl;
+import org.apache.rocketmq.schema.registry.common.model.SchemaInfo;
+import org.apache.rocketmq.schema.registry.storage.jdbc.common.Operator;
+import org.apache.rocketmq.schema.registry.storage.jdbc.dialect.DatabaseDialect;
+import org.apache.rocketmq.schema.registry.storage.jdbc.dialect.DatabaseDialectProvider;
+import org.apache.rocketmq.schema.registry.storage.jdbc.dialect.DiscoverDialectFactory;
+import org.springframework.util.Assert;
+
+import java.io.IOException;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import static org.apache.rocketmq.schema.registry.storage.jdbc.configs.JdbcStorageConfigConstants.STORAGE_JDBC_TYPE;
+import static org.apache.rocketmq.schema.registry.storage.jdbc.configs.JdbcStorageConfigConstants.STORAGE_JDBC_TYPE_MYSQL;
+
+/**
+ * Jdbc map store
+ */
+@Slf4j
+public class JdbcSchemaMapStore implements IMapStore<String, SchemaInfo> {
+
+    public final static String SCHEMAS = "schemas";
+    private DatabaseDialect dialect;
+    private JsonConverter converter;
+
+    @Override
+    public void init(HazelcastInstance hazelcastInstance, Properties config, String mapKey) {
+        Assert.isTrue(mapKey.equals(SCHEMAS), "Schema map key should be [schemas]");
+        String type = config.getProperty(STORAGE_JDBC_TYPE, STORAGE_JDBC_TYPE_MYSQL);
+        DatabaseDialectProvider provider = DiscoverDialectFactory.getDialectProvider(type);
+        try {
+            this.dialect = provider.createDialect(config);
+        } catch (IOException e) {
+            throw new SchemaException("Create jdbc storage instance failed.", e);
+        }
+        this.converter = new JsonConverterImpl();
+    }
+
+    @Override
+    public void store(String s, SchemaInfo schemaInfo) {
+        try (PreparedStatement ps = dialect.createPreparedStatement(
+            dialect.getConnection(),
+            dialect.buildUpsertStatement(dialect.tableId(), dialect.fields()))
+        ) {
+            List<String> keyFieldValues = Lists.newArrayList(schemaInfo.schemaFullName());
+            List<String> noKeyFieldValues = Lists.newArrayList(converter.toString(schemaInfo));
+            dialect.bindRecord(ps, keyFieldValues, noKeyFieldValues, Operator.UPSERT);
+            ps.executeUpdate();
+        } catch (SQLException sqe) {
+            throw new SchemaException("Registry schema handler", sqe);
+        }
+    }
+
+    @Override
+    public void storeAll(Map<String, SchemaInfo> map) {
+        Set<Map.Entry<String, SchemaInfo>> schemaInfos = map.entrySet();
+        for (Map.Entry<String, SchemaInfo> schemaInfoEntry : schemaInfos) {
+            store(schemaInfoEntry.getKey(), schemaInfoEntry.getValue());
+        }
+    }
+
+    @Override
+    public void delete(String key) {
+        try (PreparedStatement statement = dialect.createPreparedStatement(
+                dialect.getConnection(),
+                dialect.buildDeleteStatement(dialect.tableId(), Lists.newArrayList(dialect.fields().get(0))))) {
+            dialect.bindRecord(statement, Lists.newArrayList(key), null, Operator.DELETE);
+            statement.executeUpdate();

Review Comment:
   这里是null的话,应该是会报错空指针的。



-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org