shink commented on code in PR #16003: URL: https://github.com/apache/pulsar/pull/16003#discussion_r902285585
########## pulsar-io/mongo/src/main/java/org/apache/pulsar/io/mongodb/MongoSourceConfig.java: ########## @@ -0,0 +1,102 @@ +/** + * 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.pulsar.io.mongodb; + +import static com.google.common.base.Preconditions.checkNotNull; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; +import org.apache.pulsar.io.core.annotations.FieldDoc; + +/** + * Configuration class for the MongoDB Source Connectors. + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +public class MongoSourceConfig extends MongoAbstractConfig { + + private static final long serialVersionUID = 1152890092264945317L; + + public static final SyncType DEFAULT_SYNC_TYPE = SyncType.INCR_SYNC; + + public static final String DEFAULT_SYNC_TYPE_STR = "incr"; + + @FieldDoc( + defaultValue = DEFAULT_SYNC_TYPE_STR, + help = "The message synchronization type of the source connector. " + + "The field values can be of two types: incr and full. " + + "When it is set to incr, the source connector will only watch for changes made from now on. " + + "When it is set to full, the source connector will synchronize currently existing messages " + + "and watch for future changes." + ) + private SyncType syncType = DEFAULT_SYNC_TYPE; + + public static MongoSourceConfig load(String yamlFile) throws IOException { + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + final MongoSourceConfig cfg = mapper.readValue(new File(yamlFile), MongoSourceConfig.class); + + return cfg; + } + + public static MongoSourceConfig load(Map<String, Object> map) throws IOException { + final ObjectMapper mapper = new ObjectMapper(); + final MongoSourceConfig cfg = + mapper.readValue(new ObjectMapper().writeValueAsString(map), MongoSourceConfig.class); + + return cfg; + } + + /** + * Override syncType setter method. + * + * @param syncType Sync type string. + */ + public void setSyncType(String syncType) { + if ("full".equalsIgnoreCase(syncType) + || "full_sync".equalsIgnoreCase(syncType) + || "full-sync".equalsIgnoreCase(syncType)) { + + this.syncType = SyncType.FULL_SYNC; + return; + } + + if ("incr".equalsIgnoreCase(syncType) + || "increment".equalsIgnoreCase(syncType) + || "incr_sync".equalsIgnoreCase(syncType) + || "incr-sync".equalsIgnoreCase(syncType)) { + + this.syncType = SyncType.INCR_SYNC; + return; + } + + this.syncType = DEFAULT_SYNC_TYPE; + } + + @Override + public void validate() { + super.validate(); + checkNotNull(getSyncType(), "syncType not set."); Review Comment: @315157973 Yes, it is. This is redundant. I will remove it in the next commit. ########## pulsar-io/mongo/src/main/java/org/apache/pulsar/io/mongodb/MongoSource.java: ########## @@ -79,38 +78,46 @@ public MongoSource(Supplier<MongoClient> clientProvider) { public void open(Map<String, Object> config, SourceContext sourceContext) throws Exception { log.info("Open MongoDB Source"); - mongoConfig = MongoConfig.load(config); - mongoConfig.validate(false, false); + mongoSourceConfig = MongoSourceConfig.load(config); + mongoSourceConfig.validate(); if (clientProvider != null) { mongoClient = clientProvider.get(); } else { - mongoClient = MongoClients.create(mongoConfig.getMongoUri()); + mongoClient = MongoClients.create(mongoSourceConfig.getMongoUri()); } - if (StringUtils.isEmpty(mongoConfig.getDatabase())) { + if (StringUtils.isEmpty(mongoSourceConfig.getDatabase())) { // Watch all databases log.info("Watch all"); stream = mongoClient.watch(); } else { - final MongoDatabase db = mongoClient.getDatabase(mongoConfig.getDatabase()); + final MongoDatabase db = mongoClient.getDatabase(mongoSourceConfig.getDatabase()); - if (StringUtils.isEmpty(mongoConfig.getCollection())) { + if (StringUtils.isEmpty(mongoSourceConfig.getCollection())) { // Watch all collections in a database log.info("Watch db: {}", db.getName()); stream = db.watch(); } else { // Watch a collection - - final MongoCollection<Document> collection = db.getCollection(mongoConfig.getCollection()); - log.info("Watch collection: {} {}", db.getName(), mongoConfig.getCollection()); + final MongoCollection<Document> collection = db.getCollection(mongoSourceConfig.getCollection()); + log.info("Watch collection: {}.{}", db.getName(), mongoSourceConfig.getCollection()); stream = collection.watch(); } } - stream.batchSize(mongoConfig.getBatchSize()).fullDocument(FullDocument.UPDATE_LOOKUP); + stream.batchSize(mongoSourceConfig.getBatchSize()) + .fullDocument(FullDocument.UPDATE_LOOKUP); + + if (SyncType.FULL_SYNC.equals(mongoSourceConfig.getSyncType())) { Review Comment: @315157973 Yes, using `==` is indeed better. I will do it in the next commit. -- 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]
