Caideyipi commented on code in PR #16575: URL: https://github.com/apache/iotdb/pull/16575#discussion_r2431374672
########## iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/pipe/datastructure/pattern/UnionIoTDBTreePattern.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.iotdb.commons.pipe.datastructure.pattern; + +import org.apache.iotdb.commons.path.PartialPath; +import org.apache.iotdb.commons.path.PathPatternTree; + +import org.apache.tsfile.file.metadata.IDeviceID; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Represents a union of multiple {@link IoTDBTreePattern}s. This specialized class ensures type + * safety and provides access to methods specific to IoTDBTreePattern, such as getIntersection. + */ +public class UnionIoTDBTreePattern extends TreePattern { + + private final List<IoTDBTreePattern> patterns; + + public UnionIoTDBTreePattern( + final boolean isTreeModelDataAllowedToBeCaptured, final List<IoTDBTreePattern> patterns) { + super(isTreeModelDataAllowedToBeCaptured); + this.patterns = patterns; + } + + public UnionIoTDBTreePattern(final List<IoTDBTreePattern> patterns) { + super(true); + this.patterns = patterns; + } + + public UnionIoTDBTreePattern(final IoTDBTreePattern pattern) { + super(true); + this.patterns = Collections.singletonList(pattern); + } + + // ********************************************************************** + // IoTDBTreePattern-specific aggregated methods + // ********************************************************************** + + public boolean matchPrefixPath(final String path) { + return patterns.stream().anyMatch(p -> p.matchPrefixPath(path)); + } + + public boolean matchDevice(final String devicePath) { + return patterns.stream().anyMatch(p -> p.matchDevice(devicePath)); + } + + public boolean matchTailNode(final String tailNode) { + return patterns.stream().anyMatch(p -> p.matchTailNode(tailNode)); + } + + public List<PartialPath> getIntersection(final PartialPath partialPath) { + final List<PartialPath> allIntersections = new ArrayList<>(); + for (final IoTDBTreePattern pattern : patterns) { + allIntersections.addAll(pattern.getIntersection(partialPath)); + } + return allIntersections; + } + + public PathPatternTree getIntersection(final PathPatternTree patternTree) { + final PathPatternTree resultTree = new PathPatternTree(); + for (final IoTDBTreePattern pattern : patterns) { + final PathPatternTree intersection = pattern.getIntersection(patternTree); + intersection.getAllPathPatterns().forEach(resultTree::appendPathPattern); + } + resultTree.constructTree(); + return resultTree; + } + + public boolean isPrefix() { Review Comment: Maybe we should consider "isPrefixOrFullPath".... Because the mix of both is allowed. -- 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]
