|
| 1 | +/* |
| 2 | + * Copyright 2019-2021 StreamThoughts. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. |
| 7 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | + * (the "License"); you may not use this file except in compliance with |
| 9 | + * the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package io.streamthoughts.kafka.connect.filepulse.fs.filter; |
| 20 | + |
| 21 | +import io.streamthoughts.kafka.connect.filepulse.fs.PredicateFileListFilter; |
| 22 | +import io.streamthoughts.kafka.connect.filepulse.source.FileObjectMeta; |
| 23 | +import org.apache.kafka.common.config.AbstractConfig; |
| 24 | +import org.apache.kafka.common.config.ConfigDef; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.util.Map; |
| 29 | +import java.util.function.Predicate; |
| 30 | + |
| 31 | +/** |
| 32 | + * A {@link PredicateFileListFilter} that allows excluding from processing files |
| 33 | + * that have not been modified since either a given maximum or minimum time in ms. |
| 34 | + */ |
| 35 | +public class SizeFileListFilter extends PredicateFileListFilter { |
| 36 | + |
| 37 | + private final static String GROUP = "SizeFileListFilter"; |
| 38 | + |
| 39 | + private static final Logger LOG = LoggerFactory.getLogger(SizeFileListFilter.class); |
| 40 | + |
| 41 | + public static final String FILE_MINIMUM_SIZE_MS_CONFIG = "file.filter.minimum.size.bytes"; |
| 42 | + private static final String FILE_MINIMUM_AGE_MS_DOC = |
| 43 | + "The minimum size in bytes of a file to be eligible for processing (default: 0)."; |
| 44 | + private static final long FILE_MINIMUM_SIZE_MS_DEFAULT = 0L; |
| 45 | + |
| 46 | + public static final String FILE_MAXIMUM_SIZE_MS_CONFIG = "file.filter.maximum.size.bytes"; |
| 47 | + private static final String FILE_MAXIMUM_SIZE_MS_DOC = |
| 48 | + "The maximum size in bytes of a file to be eligible for processing (default: Long.MAX_VALUE)."; |
| 49 | + private static final long FILE_MAXIMUM_SIZE_MS_DEFAULT = Long.MAX_VALUE; |
| 50 | + |
| 51 | + private Predicate<FileObjectMeta> minimumSizePredicate; |
| 52 | + |
| 53 | + private Predicate<FileObjectMeta> maximumSizePredicate; |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritDoc} |
| 57 | + */ |
| 58 | + @Override |
| 59 | + public void configure(final Map<String, ?> props) { |
| 60 | + final AbstractConfig abstractConfig = new AbstractConfig(getConfigDef(), props); |
| 61 | + final Long minimumSizeBytes = abstractConfig.getLong(FILE_MINIMUM_SIZE_MS_CONFIG); |
| 62 | + this.minimumSizePredicate = it -> it.contentLength() >= minimumSizeBytes; |
| 63 | + |
| 64 | + final Long maximumSizeBytes = abstractConfig.getLong(FILE_MAXIMUM_SIZE_MS_CONFIG); |
| 65 | + this.maximumSizePredicate = it -> it.contentLength() <= maximumSizeBytes; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritDoc} |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public boolean test(final FileObjectMeta meta) { |
| 73 | + |
| 74 | + if (!minimumSizePredicate.test(meta)) { |
| 75 | + LOG.debug( |
| 76 | + "Filtered '{}'. File do not match minimum size bytes predicate.", |
| 77 | + meta |
| 78 | + ); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + if (!maximumSizePredicate.test(meta)) { |
| 83 | + LOG.debug( |
| 84 | + "Filtered '{}'. File do not match maximum size bytes predicate.", |
| 85 | + meta |
| 86 | + ); |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + private static ConfigDef getConfigDef() { |
| 94 | + int groupCounter = 0; |
| 95 | + return new ConfigDef() |
| 96 | + .define( |
| 97 | + FILE_MINIMUM_SIZE_MS_CONFIG, |
| 98 | + ConfigDef.Type.LONG, |
| 99 | + FILE_MINIMUM_SIZE_MS_DEFAULT, |
| 100 | + ConfigDef.Range.atLeast(0), |
| 101 | + ConfigDef.Importance.HIGH, |
| 102 | + FILE_MINIMUM_AGE_MS_DOC, |
| 103 | + GROUP, |
| 104 | + groupCounter++, |
| 105 | + ConfigDef.Width.NONE, |
| 106 | + FILE_MINIMUM_SIZE_MS_CONFIG |
| 107 | + ) |
| 108 | + .define( |
| 109 | + FILE_MAXIMUM_SIZE_MS_CONFIG, |
| 110 | + ConfigDef.Type.LONG, |
| 111 | + FILE_MAXIMUM_SIZE_MS_DEFAULT, |
| 112 | + ConfigDef.Range.atLeast(0), |
| 113 | + ConfigDef.Importance.HIGH, |
| 114 | + FILE_MAXIMUM_SIZE_MS_DOC, |
| 115 | + GROUP, |
| 116 | + groupCounter++, |
| 117 | + ConfigDef.Width.NONE, |
| 118 | + FILE_MAXIMUM_SIZE_MS_CONFIG |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments