Search in sources :

Example 6 with GuardedBy

use of com.google.errorprone.annotations.concurrent.GuardedBy in project druid by druid-io.

the class VersionedIntervalTimeline method addAtKey.

/**
 * @return boolean flag indicating whether or not we inserted or discarded something
 */
@GuardedBy("lock")
private boolean addAtKey(NavigableMap<Interval, TimelineEntry> timeline, Interval key, TimelineEntry entry) {
    boolean retVal = false;
    Interval currKey = key;
    Interval entryInterval = entry.getTrueInterval();
    if (!currKey.overlaps(entryInterval)) {
        return false;
    }
    while (entryInterval != null && currKey != null && currKey.overlaps(entryInterval)) {
        final Interval nextKey = timeline.higherKey(currKey);
        final int versionCompare = versionComparator.compare(entry.getVersion(), timeline.get(currKey).getVersion());
        if (versionCompare < 0) {
            // if overlapped.
            if (currKey.contains(entryInterval)) {
                // the version of the entry of currKey is larger than that of the given entry. Discard it
                return true;
            } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
                // | entry |
                // | cur |
                // =>        |new|
                entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
            } else {
                // | entry |
                // | cur |
                // =>  |new|
                addIntervalToTimeline(new Interval(entryInterval.getStart(), currKey.getStart()), entry, timeline);
                // =>          |new|
                if (entryInterval.getEnd().isAfter(currKey.getEnd())) {
                    entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
                } else {
                    // Discard this entry since there is no portion of the entry interval that goes past the end of the curr
                    // key interval.
                    entryInterval = null;
                }
            }
        } else if (versionCompare > 0) {
            // since the entry version is greater than the existing one, the given entry overwrites the existing one
            // if overlapped.
            final TimelineEntry oldEntry = timeline.remove(currKey);
            if (currKey.contains(entryInterval)) {
                // |      cur      |
                // | entry |
                // =>  |old|  new  |old|
                addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline);
                addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline);
                addIntervalToTimeline(entryInterval, entry, timeline);
                return true;
            } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
                // |   cur  |
                // |   entry   |
                // =>  |old|
                addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline);
            } else if (entryInterval.getEnd().isBefore(currKey.getEnd())) {
                // |   cur  |
                // |   entry   |
                // =>              |old|
                addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline);
            }
        } else {
            if (timeline.get(currKey).equals(entry)) {
                // This occurs when restoring segments
                timeline.remove(currKey);
            } else {
                throw new UOE("Cannot add overlapping segments [%s and %s] with the same version [%s]", currKey, entryInterval, entry.getVersion());
            }
        }
        currKey = nextKey;
        retVal = true;
    }
    addIntervalToTimeline(entryInterval, entry, timeline);
    return retVal;
}
Also used : UOE(org.apache.druid.java.util.common.UOE) Interval(org.joda.time.Interval) GuardedBy(com.google.errorprone.annotations.concurrent.GuardedBy)

Aggregations

GuardedBy (com.google.errorprone.annotations.concurrent.GuardedBy)6 Interval (org.joda.time.Interval)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 UOE (org.apache.druid.java.util.common.UOE)1