|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.pipe.pattern; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.pipe.config.constant.PipeSourceConstant; |
| 23 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.IoTDBTreePattern; |
| 24 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.TreePattern; |
| 25 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.UnionIoTDBTreePattern; |
| 26 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.WithExclusionIoTDBTreePattern; |
| 27 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.WithExclusionTreePattern; |
| 28 | +import org.apache.iotdb.pipe.api.customizer.parameter.PipeParameters; |
| 29 | +import org.apache.iotdb.pipe.api.exception.PipeException; |
| 30 | + |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import java.util.HashMap; |
| 35 | + |
| 36 | +public class TreePatternPruningTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testUnionInternalPruning_Cover() { |
| 40 | + final PipeParameters params = |
| 41 | + new PipeParameters( |
| 42 | + new HashMap<String, String>() { |
| 43 | + { |
| 44 | + put(PipeSourceConstant.SOURCE_PATH_KEY, "root.db.d1.*,root.db.d1.s1"); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params); |
| 49 | + |
| 50 | + Assert.assertTrue("Should be IoTDBTreePattern", result instanceof IoTDBTreePattern); |
| 51 | + Assert.assertEquals("root.db.d1.*", result.getPattern()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testUnionInternalPruning_Duplicates() { |
| 56 | + final PipeParameters params = |
| 57 | + new PipeParameters( |
| 58 | + new HashMap<String, String>() { |
| 59 | + { |
| 60 | + put(PipeSourceConstant.SOURCE_PATH_KEY, "root.db.d1,root.db.d1"); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params); |
| 65 | + |
| 66 | + Assert.assertTrue(result instanceof IoTDBTreePattern); |
| 67 | + Assert.assertEquals("root.db.d1", result.getPattern()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testInclusionPrunedByExclusion_Partial() { |
| 72 | + final PipeParameters params = |
| 73 | + new PipeParameters( |
| 74 | + new HashMap<String, String>() { |
| 75 | + { |
| 76 | + put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.d1,root.sg.d2"); |
| 77 | + put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.sg.d1"); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params); |
| 82 | + |
| 83 | + Assert.assertTrue(result instanceof WithExclusionIoTDBTreePattern); |
| 84 | + final WithExclusionIoTDBTreePattern exclusionPattern = (WithExclusionIoTDBTreePattern) result; |
| 85 | + |
| 86 | + Assert.assertEquals("root.sg.d2", exclusionPattern.getInclusionPattern().getPattern()); |
| 87 | + Assert.assertEquals("root.sg.d1", exclusionPattern.getExclusionPattern().getPattern()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testInclusionPrunedByExclusion_FullCoverage_Exception() { |
| 92 | + final PipeParameters params = |
| 93 | + new PipeParameters( |
| 94 | + new HashMap<String, String>() { |
| 95 | + { |
| 96 | + put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.d1"); |
| 97 | + put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.sg.**"); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + try { |
| 102 | + TreePattern.parsePipePatternFromSourceParameters(params); |
| 103 | + Assert.fail("Should throw PipeException because Exclusion fully covers Inclusion"); |
| 104 | + } catch (final PipeException ignored) { |
| 105 | + // Expected exception |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testComplexPruning() { |
| 111 | + final PipeParameters params = |
| 112 | + new PipeParameters( |
| 113 | + new HashMap<String, String>() { |
| 114 | + { |
| 115 | + put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.A,root.sg.B,root.sg.A.sub"); |
| 116 | + put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.sg.A,root.sg.A.**"); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params); |
| 121 | + |
| 122 | + Assert.assertTrue(result instanceof WithExclusionIoTDBTreePattern); |
| 123 | + final WithExclusionIoTDBTreePattern excPattern = (WithExclusionIoTDBTreePattern) result; |
| 124 | + |
| 125 | + Assert.assertEquals("root.sg.B", excPattern.getInclusionPattern().getPattern()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testComplexPruning_Prefix() { |
| 130 | + final PipeParameters params = |
| 131 | + new PipeParameters( |
| 132 | + new HashMap<String, String>() { |
| 133 | + { |
| 134 | + put(PipeSourceConstant.SOURCE_PATTERN_KEY, "root.sg.A,root.sg.B,root.sg.A.sub"); |
| 135 | + put(PipeSourceConstant.SOURCE_PATTERN_EXCLUSION_KEY, "root.sg.A"); |
| 136 | + put(PipeSourceConstant.SOURCE_PATTERN_FORMAT_KEY, "prefix"); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params); |
| 141 | + |
| 142 | + Assert.assertTrue(result instanceof WithExclusionTreePattern); |
| 143 | + final WithExclusionTreePattern excPattern = (WithExclusionTreePattern) result; |
| 144 | + |
| 145 | + Assert.assertEquals("root.sg.B", excPattern.getInclusionPattern().getPattern()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testUnionPreservedWhenNotCovered() { |
| 150 | + final PipeParameters params = |
| 151 | + new PipeParameters( |
| 152 | + new HashMap<String, String>() { |
| 153 | + { |
| 154 | + put(PipeSourceConstant.SOURCE_PATH_KEY, "root.sg.d1,root.sg.d2"); |
| 155 | + put(PipeSourceConstant.SOURCE_PATH_EXCLUSION_KEY, "root.other"); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + final TreePattern result = TreePattern.parsePipePatternFromSourceParameters(params); |
| 160 | + |
| 161 | + Assert.assertTrue(result instanceof WithExclusionIoTDBTreePattern); |
| 162 | + final WithExclusionIoTDBTreePattern excResult = (WithExclusionIoTDBTreePattern) result; |
| 163 | + |
| 164 | + Assert.assertTrue(excResult.getInclusionPattern() instanceof UnionIoTDBTreePattern); |
| 165 | + final UnionIoTDBTreePattern unionInc = (UnionIoTDBTreePattern) excResult.getInclusionPattern(); |
| 166 | + Assert.assertEquals(2, unionInc.getPatterns().size()); |
| 167 | + } |
| 168 | +} |
0 commit comments