leventov commented on a change in pull request #6370: Introduce SegmentId class URL: https://github.com/apache/incubator-druid/pull/6370#discussion_r221458763
########## File path: api/src/main/java/org/apache/druid/timeline/SegmentId.java ########## @@ -0,0 +1,391 @@ +/* + * 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.druid.timeline; + +import com.fasterxml.jackson.annotation.JsonValue; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; +import com.google.common.collect.Iterables; +import com.google.common.primitives.Ints; +import org.apache.druid.guice.annotations.PublicApi; +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.Intervals; +import org.apache.druid.timeline.partition.ShardSpec; +import org.joda.time.Chronology; +import org.joda.time.DateTime; +import org.joda.time.Interval; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.stream.IntStream; + +/** + * Identifier of {@link DataSegment}. + */ +@PublicApi +public final class SegmentId implements Comparable<SegmentId> +{ + /* + * Implementation note: this class must be optimized for resident memory footprint, because segment data consumes + * a lot of heap memory on Druid Broker and Coordinator nodes. + * + * API design note: "SegmentId" is chosen as the name for this class instead of more verbose "SegmentIdentifier" or + * "DataSegmentIdentifier" because it's used very frequently and a long class name adds noticeable clutter. Variables + * of SegmentId type are recommended to be named "segmentId" rather than "identifier" or "segmentIdentifier". + */ + + /** + * {@link #dataSource} and {@link #version} are stored as canonical string values to decrease memory required for + * storing large numbers of segment identifiers. + */ + private static final Interner<String> STRING_INTERNER = Interners.newWeakInterner(); + + private static final char DELIMITER = '_'; + private static final Splitter DELIMITER_SPLITTER = Splitter.on(DELIMITER); + private static final Joiner DELIMITER_JOINER = Joiner.on(DELIMITER); + + private static final int DATE_TIME_SIZE_UPPER_LIMIT = "yyyy-MM-ddTHH:mm:ss.SSS+00:00".length(); + + public static SegmentId of(String dataSource, Interval interval, String version, int partitionNum) + { + return new SegmentId(dataSource, interval, version, partitionNum); + } + + public static SegmentId of(String dataSource, Interval interval, String version, @Nullable ShardSpec shardSpec) + { + return of(dataSource, interval, version, shardSpec != null ? shardSpec.getPartitionNum() : 0); + } + + /** + * Tries to parse a segment id from the given String representation, or returns null on failure. If returns a non-null + * {@code SegmentId} object, calling {@link #toString()} on the latter is guaranteed to return a string equal to the + * argument string of the {@code tryParse()} call. + * + * It is possible that this method may incorrectly parse a segment id, for example if the dataSource name in the + * segment id contains a DateTime parseable string such as 'datasource_2000-01-01T00:00:00.000Z' and dataSource was + * provided as 'datasource'. The desired behavior in this case would be to return null since the identifier does not + * actually belong to the provided dataSource but a non-null result would be returned. This is an edge case that would + * currently only affect paged select queries with a union dataSource of two similarly-named dataSources as in the + * given example. + * + * Another source of ambiguity is the end of a segment id like '_123' - it could always be interpreted either as the + * partitionNum of the segment id, or as the end of the version, with the implicit partitionNum of 0. This method + * prefers the first iterpretation. To iterate all possible parsings of a segment id, use {@link + * #iteratePossibleParsingsWithDataSource}. + * + * @param dataSource the dataSource corresponding to this segment id + * @param segmentId segment id + * @return a {@link SegmentId} object if the segment id could be parsed, null otherwise + */ + @Nullable + public static SegmentId tryParse(String dataSource, String segmentId) + { + List<SegmentId> possibleParsings = iteratePossibleParsingsWithDataSource(dataSource, segmentId); + return possibleParsings.isEmpty() ? null : possibleParsings.get(0); + } + + /** + * Returns a (potentially empty) lazy iteration of all possible valid parsings of the given segment id string into + * {@code SegmentId} objects. + * + * Warning: this iterable is repeats most of the parsing work on each iteration, so it should be iterated only once if Review comment: Thanks, fixed ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
