|
| 1 | +/* |
| 2 | + * Copyright (2025) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.delta.kernel.internal.files; |
| 17 | + |
| 18 | +import static io.delta.kernel.internal.util.Preconditions.checkArgument; |
| 19 | + |
| 20 | +import io.delta.kernel.internal.lang.ListUtils; |
| 21 | +import java.util.List; |
| 22 | +import java.util.function.Function; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +public final class LogDataUtils { |
| 27 | + |
| 28 | + private LogDataUtils() {} |
| 29 | + |
| 30 | + public static void validateLogDataContainsOnlyRatifiedStagedCommits( |
| 31 | + List<? extends ParsedLogData> logDatas) { |
| 32 | + for (ParsedLogData logData : logDatas) { |
| 33 | + checkArgument( |
| 34 | + logData instanceof ParsedCatalogCommitData && logData.isFile(), |
| 35 | + "Only staged ratified commits are supported, but found: " + logData); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static void validateLogDataIsSortedContiguous(List<? extends ParsedLogData> logDatas) { |
| 40 | + if (logDatas.size() > 1) { |
| 41 | + for (int i = 1; i < logDatas.size(); i++) { |
| 42 | + final ParsedLogData prev = logDatas.get(i - 1); |
| 43 | + final ParsedLogData curr = logDatas.get(i); |
| 44 | + checkArgument( |
| 45 | + prev.getVersion() + 1 == curr.getVersion(), |
| 46 | + String.format( |
| 47 | + "Log data must be sorted and contiguous, but found: %s and %s", prev, curr)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Combines a list of published Deltas and ratified Deltas into a single list of Deltas such that |
| 54 | + * there is exactly one {@link ParsedDeltaData} per version. When there is both a published Delta |
| 55 | + * and a ratified staged Delta for the same version, prioritizes the ratified Delta. |
| 56 | + * |
| 57 | + * <p>The method requires but does not validate the following: |
| 58 | + * |
| 59 | + * <ul> |
| 60 | + * <li>{@code publishedDeltas} are sorted and contiguous |
| 61 | + * <li>{@code ratifiedDeltas} are sorted and contiguous |
| 62 | + * <li>the commit versions present in {@code publishedDeltas} and {@code ratifiedDeltas}, when |
| 63 | + * combined, reflect a contiguous version range. In other words, if the two do not overlap, |
| 64 | + * publishedDeltas.last = ratifiedDeltas.first + 1). |
| 65 | + * </ul> |
| 66 | + */ |
| 67 | + public static List<ParsedDeltaData> combinePublishedAndRatifiedDeltasWithCatalogPriority( |
| 68 | + List<ParsedDeltaData> publishedDeltas, List<ParsedDeltaData> ratifiedDeltas) { |
| 69 | + if (ratifiedDeltas.isEmpty()) { |
| 70 | + return publishedDeltas; |
| 71 | + } |
| 72 | + |
| 73 | + if (publishedDeltas.isEmpty()) { |
| 74 | + return ratifiedDeltas; |
| 75 | + } |
| 76 | + |
| 77 | + final long firstRatified = ratifiedDeltas.get(0).getVersion(); |
| 78 | + final long lastRatified = ListUtils.getLast(ratifiedDeltas).getVersion(); |
| 79 | + |
| 80 | + return Stream.of( |
| 81 | + publishedDeltas.stream().filter(x -> x.getVersion() < firstRatified), |
| 82 | + ratifiedDeltas.stream(), |
| 83 | + publishedDeltas.stream().filter(x -> x.getVersion() > lastRatified)) |
| 84 | + .flatMap(Function.identity()) |
| 85 | + .collect(Collectors.toList()); |
| 86 | + } |
| 87 | +} |
0 commit comments