All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.uber.hoodie.common.table.timeline.HoodieDefaultTimeline Maven / Gradle / Ivy

There is a newer version: 0.4.7
Show newest version
/*
 *  Copyright (c) 2016 Uber Technologies, Inc. ([email protected])
 *
 *  Licensed 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 com.uber.hoodie.common.table.timeline;

import com.uber.hoodie.common.table.HoodieTimeline;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * HoodieDefaultTimeline is a default implementation of the HoodieTimeline.
 * It provides methods to inspect a List[HoodieInstant]. Function to get the details of the instant
 * is passed in as a lamdba.
 *
 * @see HoodieTimeline
 */
public class HoodieDefaultTimeline implements HoodieTimeline {
    private final transient static Logger log = LogManager.getLogger(HoodieDefaultTimeline.class);

    protected Function> details;
    protected List instants;

    public HoodieDefaultTimeline(Stream instants,
        Function> details) {
        this.instants = instants.collect(Collectors.toList());
        this.details = details;
    }

    /**
     * For serailizing and de-serializing
     *
     * @deprecated
     */
    public HoodieDefaultTimeline() {
    }

    public HoodieTimeline filterInflights() {
        return new HoodieDefaultTimeline(instants.stream().filter(HoodieInstant::isInflight),
            details);
    }

    public HoodieTimeline filterCompletedInstants() {
        return new HoodieDefaultTimeline(instants.stream().filter(s -> !s.isInflight()), details);
    }

    @Override
    public HoodieDefaultTimeline findInstantsInRange(String startTs, String endTs) {
        return new HoodieDefaultTimeline(instants.stream().filter(
            s -> HoodieTimeline.compareTimestamps(s.getTimestamp(), startTs, GREATER) &&
                    HoodieTimeline.compareTimestamps(
                s.getTimestamp(), endTs, LESSER_OR_EQUAL)), details);
    }

    @Override
    public HoodieDefaultTimeline findInstantsAfter(String commitTime, int numCommits) {
        return new HoodieDefaultTimeline(
            instants.stream().filter(s -> HoodieTimeline.compareTimestamps(s.getTimestamp(), commitTime, GREATER))
                .limit(numCommits), details);
    }

    @Override
    public boolean empty() {
        return !instants.stream().findFirst().isPresent();
    }

    @Override
    public int countInstants() {
        return new Long(instants.stream().count()).intValue();
    }

    @Override
    public Optional firstInstant() {
        return instants.stream().findFirst();
    }

    @Override
    public Optional nthInstant(int n) {
        if (empty() || n >= countInstants()) {
            return Optional.empty();
        }
        return Optional.of(instants.get(n));
    }

    @Override
    public Optional lastInstant() {
        return empty() ? Optional.empty() : nthInstant(countInstants() - 1);
    }

    @Override
    public Optional nthFromLastInstant(int n) {
        if (countInstants() < n + 1) {
            return Optional.empty();
        }
        return nthInstant(countInstants() - 1 - n);
    }

    @Override
    public boolean containsInstant(HoodieInstant instant) {
        return instants.stream().anyMatch(s -> s.equals(instant));
    }

    @Override
    public boolean containsOrBeforeTimelineStarts(String instant) {
        return instants.stream().anyMatch(s -> s.getTimestamp().equals(instant))
            || isBeforeTimelineStarts(instant);
    }

    @Override
    public Stream getInstants() {
        return instants.stream();
    }

    @Override
    public boolean isBeforeTimelineStarts(String instant) {
        Optional firstCommit = firstInstant();
        return firstCommit.isPresent() &&
                HoodieTimeline.compareTimestamps(instant, firstCommit.get().getTimestamp(), LESSER);
    }


    @Override
    public Optional getInstantDetails(HoodieInstant instant) {
        return details.apply(instant);
    }

    @Override
    public String toString() {
        return this.getClass().getName() + ": " + instants.stream().map(Object::toString)
            .collect(Collectors.joining(","));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy