timoninmaxim commented on code in PR #311: URL: https://github.com/apache/ignite-extensions/pull/311#discussion_r2247181015
########## modules/cdc-ext/src/main/java/org/apache/ignite/cdc/postgresql/IgniteToPostgreSqlCdcApplier.java: ########## @@ -0,0 +1,688 @@ +/* + * 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.cdc.postgresql; + +import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.sql.DataSource; +import org.apache.ignite.IgniteException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.cache.CacheEntryVersion; +import org.apache.ignite.cache.QueryEntity; +import org.apache.ignite.cdc.CdcCacheEvent; +import org.apache.ignite.cdc.CdcEvent; +import org.apache.ignite.internal.util.typedef.F; + +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.UNDEFINED_CACHE_ID; + +/** */ +public class IgniteToPostgreSqlCdcApplier { + /** */ + public static final String DFLT_SQL_TYPE = "OTHER"; + + /** */ + public static final Map<String, String> JAVA_TO_SQL_TYPES; + + /** */ + public static final Set<String> SQL_TYPES_WITH_PRECISION_ONLY; + + /** */ + public static final Set<String> SQL_TYPES_WITH_PRECISION_AND_SCALE; + + static { + Map<String, String> javaToSqlTypes = new HashMap<>(); + + javaToSqlTypes.put("java.lang.String", "VARCHAR"); + javaToSqlTypes.put("java.lang.Integer", "INT"); + javaToSqlTypes.put("int", "INT"); + javaToSqlTypes.put("java.lang.Long", "BIGINT"); + javaToSqlTypes.put("long", "BIGINT"); + javaToSqlTypes.put("java.lang.Boolean", "BOOLEAN"); + javaToSqlTypes.put("boolean", "BOOLEAN"); + javaToSqlTypes.put("java.lang.Double", "DOUBLE PRECISION"); + javaToSqlTypes.put("double", "DOUBLE PRECISION"); + javaToSqlTypes.put("java.lang.Float", "REAL"); + javaToSqlTypes.put("float", "REAL"); + javaToSqlTypes.put("java.math.BigDecimal", "DECIMAL"); + javaToSqlTypes.put("java.lang.Short", "SMALLINT"); + javaToSqlTypes.put("short", "SMALLINT"); + javaToSqlTypes.put("java.lang.Byte", "SMALLINT"); + javaToSqlTypes.put("byte", "SMALLINT"); + javaToSqlTypes.put("java.util.UUID", "UUID"); + javaToSqlTypes.put("[B", "BYTEA"); + javaToSqlTypes.put("java.lang.Object", "OTHER"); + + JAVA_TO_SQL_TYPES = Collections.unmodifiableMap(javaToSqlTypes); + + Set<String> sqlTypesWithPrecisionOnly = new HashSet<>(); + + sqlTypesWithPrecisionOnly.add("VARCHAR"); + sqlTypesWithPrecisionOnly.add("DOUBLE PRECISION"); + + SQL_TYPES_WITH_PRECISION_ONLY = Collections.unmodifiableSet(sqlTypesWithPrecisionOnly); + + Set<String> sqlTypesWithPrecisionAndScale = new HashSet<>(); + + sqlTypesWithPrecisionAndScale.add("DECIMAL"); + sqlTypesWithPrecisionAndScale.add("REAL"); + + SQL_TYPES_WITH_PRECISION_AND_SCALE = Collections.unmodifiableSet(sqlTypesWithPrecisionAndScale); + } + + /** */ + private final DataSource dataSrc; + + /** */ + private final long batchSize; + + /** */ + private final IgniteLogger log; + + /** */ + private final Map<Integer, String> cacheIdToUpsertQry = new HashMap<>(); + + /** */ + private final Map<Integer, String> cacheIdToDeleteQry = new HashMap<>(); + + /** */ + private final Map<Integer, Set<String>> cacheIdToPrimaryKeys = new HashMap<>(); + + /** */ + private final Map<Integer, Set<String>> cacheIdToFields = new HashMap<>(); + + /** */ + private final Set<Object> curKeys = new HashSet<>(); + + /** + * @param dataSrc {@link DataSource} - connection pool to PostgreSql + * @param batchSize the number of CDC events to include in a single batch + * @param log the {@link IgniteLogger} instance used for logging CDC processing events + */ + public IgniteToPostgreSqlCdcApplier( + DataSource dataSrc, + long batchSize, + IgniteLogger log + ) { + this.dataSrc = dataSrc; + this.batchSize = batchSize; + this.log = log; + } + + /** + * @param evts an {@link Iterator} of {@link CdcEvent} objects to be applied + * @return the total number of events successfully batched and executed + */ + public long applyEvents(Iterator<CdcEvent> evts) { + Connection conn = null; + + try { Review Comment: `try (Connection conn = dataSrc.getConnection)` - it will close the connection automatically. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org