ijokarumawak commented on a change in pull request #3341: NIFI-6082: Added DatabaseRecordLookupService, refactored common DB utils URL: https://github.com/apache/nifi/pull/3341#discussion_r263674275
########## File path: nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/db/SimpleDatabaseLookupService.java ########## @@ -0,0 +1,160 @@ +/* + * 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.nifi.lookup.db; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.apache.commons.lang3.StringUtils; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.controller.ControllerServiceInitializationContext; +import org.apache.nifi.dbcp.DBCPService; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.lookup.LookupFailureException; +import org.apache.nifi.lookup.StringLookupService; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.util.Tuple; +import org.apache.nifi.util.db.JdbcCommon; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class SimpleDatabaseLookupService extends AbstractDatabaseLookupService implements StringLookupService { + + private volatile Cache<Tuple<String, Object>, String> cache; + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + + private volatile String lookupValueColumn; + + static final PropertyDescriptor LOOKUP_VALUE_COLUMN = + new PropertyDescriptor.Builder() + .name("lookup-value-column") + .displayName("Lookup Value Column") + .description("The column whose value will be returned when the Lookup value is matched") + .required(true) + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + @Override + protected void init(final ControllerServiceInitializationContext context) { + final List<PropertyDescriptor> properties = new ArrayList<>(); + properties.add(DBCP_SERVICE); + properties.add(TABLE_NAME); + properties.add(LOOKUP_KEY_COLUMN); + properties.add(LOOKUP_VALUE_COLUMN); + properties.add(CACHE_SIZE); + properties.add(CLEAR_CACHE_ON_ENABLED); + this.properties = Collections.unmodifiableList(properties); + } + + @OnEnabled + public void onEnabled(final ConfigurationContext context) { + this.dbcpService = context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class); + this.lookupKeyColumn = context.getProperty(LOOKUP_KEY_COLUMN).evaluateAttributeExpressions().getValue(); + this.lookupValueColumn = context.getProperty(LOOKUP_VALUE_COLUMN).evaluateAttributeExpressions().getValue(); + int cacheSize = context.getProperty(CACHE_SIZE).evaluateAttributeExpressions().asInteger(); + boolean clearCache = context.getProperty(CLEAR_CACHE_ON_ENABLED).asBoolean(); + if (this.cache == null || (cacheSize > 0 && clearCache)) { + this.cache = Caffeine.newBuilder() + .maximumSize(cacheSize) + .build(); + } + + options = JdbcCommon.AvroConversionOptions.builder() Review comment: This options instance is not used by SimpleDatabaseLookupService. Please remove it. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
