Example usage for org.joda.time Interval overlaps

List of usage examples for org.joda.time Interval overlaps

Introduction

In this page you can find the example usage for org.joda.time Interval overlaps.

Prototype

public boolean overlaps(ReadableInterval interval) 

Source Link

Document

Does this time interval overlap the specified time interval.

Usage

From source file:io.druid.indexing.overlord.TaskLockbox.java

License:Apache License

/**
 * Return all locks that overlap some search interval.
 *//* ww w.java2s  . com*/
private List<TaskLockPosse> findLockPossesForInterval(final String dataSource, final Interval interval) {
    giant.lock();

    try {
        final NavigableMap<Interval, TaskLockPosse> dsRunning = running.get(dataSource);
        if (dsRunning == null) {
            // No locks at all
            return Collections.emptyList();
        } else {
            // Tasks are indexed by locked interval, which are sorted by interval start. Intervals are non-overlapping, so:
            final NavigableSet<Interval> dsLockbox = dsRunning.navigableKeySet();
            final Iterable<Interval> searchIntervals = Iterables.concat(
                    // Single interval that starts at or before ours
                    Collections.singletonList(dsLockbox
                            .floor(new Interval(interval.getStart(), new DateTime(JodaUtils.MAX_INSTANT)))),

                    // All intervals that start somewhere between our start instant (exclusive) and end instant (exclusive)
                    dsLockbox.subSet(new Interval(interval.getStart(), new DateTime(JodaUtils.MAX_INSTANT)),
                            false, new Interval(interval.getEnd(), interval.getEnd()), false));

            return Lists
                    .newArrayList(FunctionalIterable.create(searchIntervals).filter(new Predicate<Interval>() {
                        @Override
                        public boolean apply(@Nullable Interval searchInterval) {
                            return searchInterval != null && searchInterval.overlaps(interval);
                        }
                    }).transform(new Function<Interval, TaskLockPosse>() {
                        @Override
                        public TaskLockPosse apply(Interval interval) {
                            return dsRunning.get(interval);
                        }
                    }));
        }
    } finally {
        giant.unlock();
    }
}

From source file:io.druid.java.util.common.JodaUtils.java

License:Apache License

public static ArrayList<Interval> condenseIntervals(Iterable<Interval> intervals) {
    ArrayList<Interval> retVal = Lists.newArrayList();

    final SortedSet<Interval> sortedIntervals;

    if (intervals instanceof SortedSet) {
        sortedIntervals = (SortedSet<Interval>) intervals;
    } else {//www  . java 2  s.  c  o m
        sortedIntervals = Sets.newTreeSet(Comparators.intervalsByStartThenEnd());
        for (Interval interval : intervals) {
            sortedIntervals.add(interval);
        }
    }

    if (sortedIntervals.isEmpty()) {
        return Lists.newArrayList();
    }

    Iterator<Interval> intervalsIter = sortedIntervals.iterator();
    Interval currInterval = intervalsIter.next();
    while (intervalsIter.hasNext()) {
        Interval next = intervalsIter.next();

        if (currInterval.abuts(next)) {
            currInterval = new Interval(currInterval.getStart(), next.getEnd());
        } else if (currInterval.overlaps(next)) {
            DateTime nextEnd = next.getEnd();
            DateTime currEnd = currInterval.getEnd();
            currInterval = new Interval(currInterval.getStart(), nextEnd.isAfter(currEnd) ? nextEnd : currEnd);
        } else {
            retVal.add(currInterval);
            currInterval = next;
        }
    }
    retVal.add(currInterval);

    return retVal;
}

From source file:io.druid.segment.incremental.IncrementalIndexStorageAdapter.java

License:Apache License

@Override
public Sequence<Cursor> makeCursors(final Filter filter, final Interval interval, final QueryGranularity gran) {
    if (index.isEmpty()) {
        return Sequences.empty();
    }//from   w w w  . j a  v a2s.  com

    Interval actualIntervalTmp = interval;

    final Interval dataInterval = new Interval(getMinTime().getMillis(),
            gran.next(gran.truncate(getMaxTime().getMillis())));

    if (!actualIntervalTmp.overlaps(dataInterval)) {
        return Sequences.empty();
    }

    if (actualIntervalTmp.getStart().isBefore(dataInterval.getStart())) {
        actualIntervalTmp = actualIntervalTmp.withStart(dataInterval.getStart());
    }
    if (actualIntervalTmp.getEnd().isAfter(dataInterval.getEnd())) {
        actualIntervalTmp = actualIntervalTmp.withEnd(dataInterval.getEnd());
    }

    final Interval actualInterval = actualIntervalTmp;

    return Sequences.map(
            Sequences.simple(gran.iterable(actualInterval.getStartMillis(), actualInterval.getEndMillis())),
            new Function<Long, Cursor>() {
                EntryHolder currEntry = new EntryHolder();
                private final ValueMatcher filterMatcher;

                {
                    filterMatcher = makeFilterMatcher(filter, currEntry);
                }

                @Override
                public Cursor apply(@Nullable final Long input) {
                    final long timeStart = Math.max(input, actualInterval.getStartMillis());

                    return new Cursor() {
                        private Iterator<Map.Entry<IncrementalIndex.TimeAndDims, Integer>> baseIter;
                        private ConcurrentNavigableMap<IncrementalIndex.TimeAndDims, Integer> cursorMap;
                        final DateTime time;
                        int numAdvanced = -1;
                        boolean done;

                        {
                            cursorMap = index.getSubMap(
                                    new IncrementalIndex.TimeAndDims(timeStart, new String[][] {}),
                                    new IncrementalIndex.TimeAndDims(
                                            Math.min(actualInterval.getEndMillis(), gran.next(input)),
                                            new String[][] {}));
                            time = gran.toDateTime(input);

                            reset();
                        }

                        @Override
                        public DateTime getTime() {
                            return time;
                        }

                        @Override
                        public void advance() {
                            if (!baseIter.hasNext()) {
                                done = true;
                                return;
                            }

                            while (baseIter.hasNext()) {
                                if (Thread.interrupted()) {
                                    throw new QueryInterruptedException();
                                }

                                currEntry.set(baseIter.next());

                                if (filterMatcher.matches()) {
                                    return;
                                }
                            }

                            if (!filterMatcher.matches()) {
                                done = true;
                            }
                        }

                        @Override
                        public void advanceTo(int offset) {
                            int count = 0;
                            while (count < offset && !isDone()) {
                                advance();
                                count++;
                            }
                        }

                        @Override
                        public boolean isDone() {
                            return done;
                        }

                        @Override
                        public void reset() {
                            baseIter = cursorMap.entrySet().iterator();

                            if (numAdvanced == -1) {
                                numAdvanced = 0;
                            } else {
                                Iterators.advance(baseIter, numAdvanced);
                            }

                            if (Thread.interrupted()) {
                                throw new QueryInterruptedException();
                            }

                            boolean foundMatched = false;
                            while (baseIter.hasNext()) {
                                currEntry.set(baseIter.next());
                                if (filterMatcher.matches()) {
                                    foundMatched = true;
                                    break;
                                }

                                numAdvanced++;
                            }

                            done = !foundMatched && (cursorMap.size() == 0 || !baseIter.hasNext());
                        }

                        @Override
                        public DimensionSelector makeDimensionSelector(final String dimension,
                                @Nullable final ExtractionFn extractionFn) {
                            if (dimension.equals(Column.TIME_COLUMN_NAME)) {
                                return new SingleScanTimeDimSelector(makeLongColumnSelector(dimension),
                                        extractionFn);
                            }

                            final IncrementalIndex.DimDim dimValLookup = index.getDimension(dimension);
                            if (dimValLookup == null) {
                                return NULL_DIMENSION_SELECTOR;
                            }

                            final int maxId = dimValLookup.size();
                            final int dimIndex = index.getDimensionIndex(dimension);

                            return new DimensionSelector() {
                                @Override
                                public IndexedInts getRow() {
                                    final ArrayList<Integer> vals = Lists.newArrayList();
                                    if (dimIndex < currEntry.getKey().getDims().length) {
                                        final String[] dimVals = currEntry.getKey().getDims()[dimIndex];
                                        if (dimVals != null) {
                                            for (String dimVal : dimVals) {
                                                int id = dimValLookup.getId(dimVal);
                                                if (id < maxId) {
                                                    vals.add(id);
                                                }
                                            }
                                        }
                                    }
                                    // check for null entry
                                    if (vals.isEmpty() && dimValLookup.contains(null)) {
                                        int id = dimValLookup.getId(null);
                                        if (id < maxId) {
                                            vals.add(id);
                                        }
                                    }

                                    return new IndexedInts() {
                                        @Override
                                        public int size() {
                                            return vals.size();
                                        }

                                        @Override
                                        public int get(int index) {
                                            return vals.get(index);
                                        }

                                        @Override
                                        public Iterator<Integer> iterator() {
                                            return vals.iterator();
                                        }

                                        @Override
                                        public void fill(int index, int[] toFill) {
                                            throw new UnsupportedOperationException("fill not supported");
                                        }

                                        @Override
                                        public void close() throws IOException {

                                        }
                                    };
                                }

                                @Override
                                public int getValueCardinality() {
                                    return maxId;
                                }

                                @Override
                                public String lookupName(int id) {
                                    final String value = dimValLookup.getValue(id);
                                    return extractionFn == null ? value : extractionFn.apply(value);

                                }

                                @Override
                                public int lookupId(String name) {
                                    if (extractionFn != null) {
                                        throw new UnsupportedOperationException(
                                                "cannot perform lookup when applying an extraction function");
                                    }
                                    return dimValLookup.getId(name);
                                }
                            };
                        }

                        @Override
                        public FloatColumnSelector makeFloatColumnSelector(String columnName) {
                            final Integer metricIndexInt = index.getMetricIndex(columnName);
                            if (metricIndexInt == null) {
                                return new FloatColumnSelector() {
                                    @Override
                                    public float get() {
                                        return 0.0f;
                                    }
                                };
                            }

                            final int metricIndex = metricIndexInt;
                            return new FloatColumnSelector() {
                                @Override
                                public float get() {
                                    return index.getMetricFloatValue(currEntry.getValue(), metricIndex);
                                }
                            };
                        }

                        @Override
                        public LongColumnSelector makeLongColumnSelector(String columnName) {
                            if (columnName.equals(Column.TIME_COLUMN_NAME)) {
                                return new LongColumnSelector() {
                                    @Override
                                    public long get() {
                                        return currEntry.getKey().getTimestamp();
                                    }
                                };
                            }
                            final Integer metricIndexInt = index.getMetricIndex(columnName);
                            if (metricIndexInt == null) {
                                return new LongColumnSelector() {
                                    @Override
                                    public long get() {
                                        return 0L;
                                    }
                                };
                            }

                            final int metricIndex = metricIndexInt;

                            return new LongColumnSelector() {
                                @Override
                                public long get() {
                                    return index.getMetricLongValue(currEntry.getValue(), metricIndex);
                                }
                            };
                        }

                        @Override
                        public ObjectColumnSelector makeObjectColumnSelector(String column) {
                            if (column.equals(Column.TIME_COLUMN_NAME)) {
                                return new ObjectColumnSelector<Long>() {
                                    @Override
                                    public Class classOfObject() {
                                        return Long.TYPE;
                                    }

                                    @Override
                                    public Long get() {
                                        return currEntry.getKey().getTimestamp();
                                    }
                                };
                            }

                            final Integer metricIndexInt = index.getMetricIndex(column);
                            if (metricIndexInt != null) {
                                final int metricIndex = metricIndexInt;

                                final ComplexMetricSerde serde = ComplexMetrics
                                        .getSerdeForType(index.getMetricType(column));
                                return new ObjectColumnSelector() {
                                    @Override
                                    public Class classOfObject() {
                                        return serde.getObjectStrategy().getClazz();
                                    }

                                    @Override
                                    public Object get() {
                                        return index.getMetricObjectValue(currEntry.getValue(), metricIndex);
                                    }
                                };
                            }

                            final Integer dimensionIndexInt = index.getDimensionIndex(column);

                            if (dimensionIndexInt != null) {
                                final int dimensionIndex = dimensionIndexInt;
                                return new ObjectColumnSelector<Object>() {
                                    @Override
                                    public Class classOfObject() {
                                        return Object.class;
                                    }

                                    @Override
                                    public Object get() {
                                        IncrementalIndex.TimeAndDims key = currEntry.getKey();
                                        if (key == null) {
                                            return null;
                                        }

                                        String[][] dims = key.getDims();
                                        if (dimensionIndex >= dims.length) {
                                            return null;
                                        }

                                        final String[] dimVals = dims[dimensionIndex];
                                        if (dimVals == null || dimVals.length == 0) {
                                            return null;
                                        }
                                        if (dimVals.length == 1) {
                                            return dimVals[0];
                                        }
                                        return dimVals;
                                    }
                                };
                            }

                            return null;
                        }
                    };
                }
            });
}

From source file:io.druid.segment.indexing.granularity.ArbitraryGranularitySpec.java

License:Apache License

@JsonCreator
public ArbitraryGranularitySpec(@JsonProperty("queryGranularity") QueryGranularity queryGranularity,
        @JsonProperty("intervals") List<Interval> inputIntervals) {
    this.queryGranularity = queryGranularity;
    this.intervals = Sets.newTreeSet(Comparators.intervalsByStartThenEnd());

    if (inputIntervals == null) {
        inputIntervals = Lists.newArrayList();
    }/*  www.  j a v a 2  s.  com*/

    // Insert all intervals
    for (final Interval inputInterval : inputIntervals) {
        intervals.add(inputInterval);
    }

    // Ensure intervals are non-overlapping (but they may abut each other)
    final PeekingIterator<Interval> intervalIterator = Iterators.peekingIterator(intervals.iterator());
    while (intervalIterator.hasNext()) {
        final Interval currentInterval = intervalIterator.next();

        if (intervalIterator.hasNext()) {
            final Interval nextInterval = intervalIterator.peek();
            if (currentInterval.overlaps(nextInterval)) {
                throw new IllegalArgumentException(
                        String.format("Overlapping intervals: %s, %s", currentInterval, nextInterval));
            }
        }
    }
}

From source file:io.druid.segment.QueryableIndexStorageAdapter.java

License:Apache License

@Override
public Sequence<Cursor> makeCursors(Filter filter, Interval interval, QueryGranularity gran) {
    Interval actualInterval = interval;

    long minDataTimestamp = getMinTime().getMillis();
    long maxDataTimestamp = getMaxTime().getMillis();
    final Interval dataInterval = new Interval(minDataTimestamp, gran.next(gran.truncate(maxDataTimestamp)));

    if (!actualInterval.overlaps(dataInterval)) {
        return Sequences.empty();
    }// w  w  w .  java  2  s  .  c  o  m

    if (actualInterval.getStart().isBefore(dataInterval.getStart())) {
        actualInterval = actualInterval.withStart(dataInterval.getStart());
    }
    if (actualInterval.getEnd().isAfter(dataInterval.getEnd())) {
        actualInterval = actualInterval.withEnd(dataInterval.getEnd());
    }

    final Offset offset;
    if (filter == null) {
        offset = new NoFilterOffset(0, index.getNumRows());
    } else {
        final ColumnSelectorBitmapIndexSelector selector = new ColumnSelectorBitmapIndexSelector(
                index.getBitmapFactoryForDimensions(), index);

        offset = new BitmapOffset(selector.getBitmapFactory(), filter.getBitmapIndex(selector));
    }

    return Sequences.filter(
            new CursorSequenceBuilder(index, actualInterval, gran, offset, maxDataTimestamp).build(),
            Predicates.<Cursor>notNull());
}

From source file:io.druid.server.ClientInfoResource.java

License:Apache License

@GET
@Path("/{dataSourceName}")
@Produces(MediaType.APPLICATION_JSON)/*from  w ww.ja  v  a 2 s.co  m*/
public Map<String, Object> getDatasource(@PathParam("dataSourceName") String dataSourceName,
        @QueryParam("interval") String interval, @QueryParam("full") String full) {
    if (full == null) {
        return ImmutableMap.<String, Object>of(KEY_DIMENSIONS,
                getDatasourceDimensions(dataSourceName, interval), KEY_METRICS,
                getDatasourceMetrics(dataSourceName, interval));
    }

    Interval theInterval;
    if (interval == null || interval.isEmpty()) {
        DateTime now = getCurrentTime();
        theInterval = new Interval(segmentMetadataQueryConfig.getDefaultHistory(), now);
    } else {
        theInterval = new Interval(interval);
    }

    TimelineLookup<String, ServerSelector> timeline = timelineServerView
            .getTimeline(new TableDataSource(dataSourceName));
    Iterable<TimelineObjectHolder<String, ServerSelector>> serversLookup = timeline != null
            ? timeline.lookup(theInterval)
            : null;
    if (serversLookup == null || Iterables.isEmpty(serversLookup)) {
        return Collections.EMPTY_MAP;
    }
    Map<Interval, Object> servedIntervals = new TreeMap<>(new Comparator<Interval>() {
        @Override
        public int compare(Interval o1, Interval o2) {
            if (o1.equals(o2) || o1.overlaps(o2)) {
                return 0;
            } else {
                return o1.isBefore(o2) ? -1 : 1;
            }
        }
    });

    for (TimelineObjectHolder<String, ServerSelector> holder : serversLookup) {
        final Set<Object> dimensions = Sets.newHashSet();
        final Set<Object> metrics = Sets.newHashSet();
        final PartitionHolder<ServerSelector> partitionHolder = holder.getObject();
        if (partitionHolder.isComplete()) {
            for (ServerSelector server : partitionHolder.payloads()) {
                final DataSegment segment = server.getSegment();
                dimensions.addAll(segment.getDimensions());
                metrics.addAll(segment.getMetrics());
            }
        }

        servedIntervals.put(holder.getInterval(),
                ImmutableMap.of(KEY_DIMENSIONS, dimensions, KEY_METRICS, metrics));
    }

    //collapse intervals if they abut and have same set of columns
    Map<String, Object> result = Maps.newLinkedHashMap();
    Interval curr = null;
    Map<String, Set<String>> cols = null;
    for (Map.Entry<Interval, Object> e : servedIntervals.entrySet()) {
        Interval ival = e.getKey();
        if (curr != null && curr.abuts(ival) && cols.equals(e.getValue())) {
            curr = curr.withEnd(ival.getEnd());
        } else {
            if (curr != null) {
                result.put(curr.toString(), cols);
            }
            curr = ival;
            cols = (Map<String, Set<String>>) e.getValue();
        }
    }
    //add the last one in
    if (curr != null) {
        result.put(curr.toString(), cols);
    }
    return result;
}

From source file:io.druid.server.ClientInfoResource.java

License:Apache License

@GET
@Path("/{dataSourceName}/dimensions")
@Produces(MediaType.APPLICATION_JSON)//from   w ww . j  av  a  2s .  c o  m
public Iterable<String> getDatasourceDimensions(@PathParam("dataSourceName") String dataSourceName,
        @QueryParam("interval") String interval) {
    final List<DataSegment> segments = getSegmentsForDatasources().get(dataSourceName);
    final Set<String> dims = Sets.newHashSet();

    if (segments == null || segments.isEmpty()) {
        return dims;
    }

    Interval theInterval;
    if (interval == null || interval.isEmpty()) {
        DateTime now = getCurrentTime();
        theInterval = new Interval(segmentMetadataQueryConfig.getDefaultHistory(), now);
    } else {
        theInterval = new Interval(interval);
    }

    for (DataSegment segment : segments) {
        if (theInterval.overlaps(segment.getInterval())) {
            dims.addAll(segment.getDimensions());
        }
    }

    return dims;
}

From source file:io.druid.server.ClientInfoResource.java

License:Apache License

@GET
@Path("/{dataSourceName}/metrics")
@Produces(MediaType.APPLICATION_JSON)/*from w w w . j  ava 2s. c om*/
public Iterable<String> getDatasourceMetrics(@PathParam("dataSourceName") String dataSourceName,
        @QueryParam("interval") String interval) {
    final List<DataSegment> segments = getSegmentsForDatasources().get(dataSourceName);
    final Set<String> metrics = Sets.newHashSet();

    if (segments == null || segments.isEmpty()) {
        return metrics;
    }

    Interval theInterval;
    if (interval == null || interval.isEmpty()) {
        DateTime now = getCurrentTime();
        theInterval = new Interval(segmentMetadataQueryConfig.getDefaultHistory(), now);
    } else {
        theInterval = new Interval(interval);
    }

    for (DataSegment segment : segments) {
        if (theInterval.overlaps(segment.getInterval())) {
            metrics.addAll(segment.getMetrics());
        }
    }

    return metrics;
}

From source file:io.druid.server.coordinator.rules.PeriodLoadRule.java

License:Apache License

@Override
public boolean appliesTo(Interval interval, DateTime referenceTimestamp) {
    final Interval currInterval = new Interval(period, referenceTimestamp);
    return currInterval.overlaps(interval) && interval.getStartMillis() >= currInterval.getStartMillis();
}

From source file:io.druid.server.coordinator.rules.Rules.java

License:Apache License

public static boolean eligibleForLoad(Period period, Interval interval, DateTime referenceTimestamp) {
    final Interval currInterval = new Interval(period, referenceTimestamp);
    return currInterval.overlaps(interval) && interval.getStartMillis() >= currInterval.getStartMillis();
}