Search in sources :

Example 1 with TimeOrder

use of com.serotonin.m2m2.db.dao.pointvalue.TimeOrder in project ma-core-public by MangoAutomation.

the class PointValueDao method streamPointValues.

/**
 * Stream the point values for a single point, for the time range {@code [from,to)}.
 * Values are streamed in either ascending or descending time order.
 *
 * <p>The values should be lazily fetched from the underlying database. If this is not supported, the values should be
 * pre-fetched in chunks of size {@link #chunkSize()} and buffered out.</p>
 *
 * <p>The limit can often be omitted, as it is only useful for implementations which pre-fetch and buffer
 * with small limits (i.e. less than the {@link #chunkSize()}).</p>
 *
 * <p>The returned {@link Stream} <strong>must</strong> be closed, use a try-with-resources block.</p>
 * <pre>{@code
 * try (var stream = streamPointValues(point, from, to, null, ASCENDING)) {
 *     // use stream
 * }
 * }</pre>
 *
 * @param vo the data point
 * @param from from time (epoch ms), inclusive
 * @param to to time (epoch ms), exclusive
 * @param limit maximum number of values to return (if null, no limit is applied)
 * @param sortOrder time order in which to return point values
 * @throws IllegalArgumentException if vo is null, if to is less than from
 */
default Stream<IdPointValueTime> streamPointValues(DataPointVO vo, @Nullable Long from, @Nullable Long to, @Nullable Integer limit, TimeOrder sortOrder, int chunkSize) {
    PointValueDao.validateNotNull(vo);
    PointValueDao.validateTimePeriod(from, to);
    PointValueDao.validateLimit(limit);
    PointValueDao.validateNotNull(sortOrder);
    PointValueDao.validateChunkSize(chunkSize);
    if (limit != null) {
        chunkSize = Math.min(limit, chunkSize);
    }
    PointValueIterator it = new PointValueIterator(this, vo, from, to, chunkSize, sortOrder);
    Spliterator<IdPointValueTime> spliterator = Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.DISTINCT | Spliterator.SORTED);
    return StreamSupport.stream(spliterator, false);
}
Also used : PointValueIterator(com.infiniteautomation.mango.db.iterators.PointValueIterator) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime)

Example 2 with TimeOrder

use of com.serotonin.m2m2.db.dao.pointvalue.TimeOrder in project ma-core-public by infiniteautomation.

the class NumericPointValueDaoTestHelper method streamPointValuesCombined.

public void streamPointValuesCombined(TimeOrder order) {
    var values = timeIndexedValues();
    AtomicLong count = new AtomicLong();
    AtomicLong previousTime = new AtomicLong(order == TimeOrder.ASCENDING ? Long.MIN_VALUE : Long.MAX_VALUE);
    Set<Integer> seriesIds = new HashSet<>();
    try (var stream = dao.streamPointValuesCombined(vos, startTs, endTs, null, order)) {
        stream.forEach(pvt -> {
            long lastTimestamp = previousTime.getAndSet(pvt.getTime());
            count.incrementAndGet();
            PointValueTime expectedValue = values.get(pvt.getSeriesId()).get(pvt.getTime());
            seriesIds.add(pvt.getSeriesId());
            if (order == TimeOrder.ASCENDING) {
                Assert.assertTrue(pvt.getTime() >= lastTimestamp);
            } else {
                Assert.assertTrue(pvt.getTime() <= lastTimestamp);
            }
            Assert.assertNotNull(expectedValue);
            Assert.assertEquals(expectedValue.getValue(), pvt.getValue());
        });
    }
    Assert.assertEquals(vos.size(), seriesIds.size());
    Assert.assertEquals(totalSampleCount * 2, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) HashSet(java.util.HashSet)

Example 3 with TimeOrder

use of com.serotonin.m2m2.db.dao.pointvalue.TimeOrder in project ma-core-public by infiniteautomation.

the class MockPointValueDao method streamPointValues.

@Override
public Stream<IdPointValueTime> streamPointValues(DataPointVO vo, @Nullable Long from, @Nullable Long to, @Nullable Integer limit, TimeOrder sortOrder, int chunkSize) {
    PointValueDao.validateNotNull(vo);
    PointValueDao.validateTimePeriod(from, to);
    PointValueDao.validateLimit(limit);
    PointValueDao.validateNotNull(sortOrder);
    PointValueDao.validateChunkSize(chunkSize);
    var values = data.getOrDefault(vo.getSeriesId(), Collections.emptyNavigableMap());
    // noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (values) {
        var filteredValues = filterValues(values, from, to);
        if (sortOrder == TimeOrder.DESCENDING) {
            filteredValues = filteredValues.descendingMap();
        }
        var stream = filteredValues.values().stream();
        if (limit != null) {
            stream = stream.limit(limit);
        }
        return stream.map(v -> new IdPointValueTime(vo.getSeriesId(), v.getValue(), v.getTime()));
    }
}
Also used : IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime)

Example 4 with TimeOrder

use of com.serotonin.m2m2.db.dao.pointvalue.TimeOrder in project ma-core-public by infiniteautomation.

the class NumericPointValueDaoTestHelper method streamPointValues.

public void streamPointValues(TimeOrder order) {
    Map<Long, PointValueTime> values = timeIndexedValues(vo1);
    AtomicLong count = new AtomicLong();
    AtomicLong previousTime = new AtomicLong(order == TimeOrder.ASCENDING ? Long.MIN_VALUE : Long.MAX_VALUE);
    try (var stream = dao.streamPointValues(vo1, startTs, endTs, null, order)) {
        stream.forEach(pvt -> {
            long lastTimestamp = previousTime.getAndSet(pvt.getTime());
            count.incrementAndGet();
            PointValueTime expectedValue = values.get(pvt.getTime());
            if (order == TimeOrder.ASCENDING) {
                Assert.assertTrue(pvt.getTime() > lastTimestamp);
            } else {
                Assert.assertTrue(pvt.getTime() < lastTimestamp);
            }
            Assert.assertNotNull(expectedValue);
            Assert.assertEquals(expectedValue.getValue(), pvt.getValue());
        });
    }
    Assert.assertEquals(totalSampleCount, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MutableLong(org.apache.commons.lang3.mutable.MutableLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 5 with TimeOrder

use of com.serotonin.m2m2.db.dao.pointvalue.TimeOrder in project ma-core-public by infiniteautomation.

the class NumericPointValueDaoTestHelper method streamPointValuesPerPoint.

public void streamPointValuesPerPoint(TimeOrder order) {
    var values = timeIndexedValues();
    AtomicLong count = new AtomicLong();
    AtomicLong previousTime = new AtomicLong();
    Deque<Integer> seriesIds = new ArrayDeque<>();
    try (var stream = dao.streamPointValuesPerPoint(vos, startTs, endTs, null, order)) {
        stream.forEach(pvt -> {
            if (seriesIds.contains(pvt.getSeriesId())) {
                // already seen this series, ensure that it is the last one
                Assert.assertEquals(pvt.getSeriesId(), (long) Objects.requireNonNull(seriesIds.peekLast()));
            } else {
                seriesIds.add(pvt.getSeriesId());
                previousTime.set(order == TimeOrder.ASCENDING ? Long.MIN_VALUE : Long.MAX_VALUE);
            }
            long lastTimestamp = previousTime.getAndSet(pvt.getTime());
            count.incrementAndGet();
            PointValueTime expectedValue = values.get(pvt.getSeriesId()).get(pvt.getTime());
            if (order == TimeOrder.ASCENDING) {
                Assert.assertTrue(pvt.getTime() > lastTimestamp);
            } else {
                Assert.assertTrue(pvt.getTime() < lastTimestamp);
            }
            Assert.assertNotNull(expectedValue);
            Assert.assertEquals(expectedValue.getValue(), pvt.getValue());
        });
    }
    Assert.assertEquals(vos.size(), seriesIds.size());
    Assert.assertEquals(totalSampleCount * 2, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayDeque(java.util.ArrayDeque)

Aggregations

IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)8 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 ArrayDeque (java.util.ArrayDeque)2 HashSet (java.util.HashSet)2 MutableLong (org.apache.commons.lang3.mutable.MutableLong)2 PointValueIterator (com.infiniteautomation.mango.db.iterators.PointValueIterator)1