com.kolibrifx.plovercrest.server.internal.local.LocalTableReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plovercrest-server Show documentation
Show all versions of plovercrest-server Show documentation
Plovercrest server library.
The newest version!
/*
* Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
*/
package com.kolibrifx.plovercrest.server.internal.local;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import com.kolibrifx.common.NotImplementedException;
import com.kolibrifx.plovercrest.client.RangeQuery;
import com.kolibrifx.plovercrest.client.TableReader;
import com.kolibrifx.plovercrest.client.TableSerializer;
import com.kolibrifx.plovercrest.client.internal.ReadInfo;
import com.kolibrifx.plovercrest.client.internal.ReaderStrategy;
import com.kolibrifx.plovercrest.server.internal.streams.RangeReadInfo;
import com.kolibrifx.plovercrest.server.internal.streams.RangeStreamObserver;
import com.kolibrifx.plovercrest.server.internal.streams.RangeStreamUtils;
import com.kolibrifx.plovercrest.server.streams.PollableReader;
import com.kolibrifx.plovercrest.server.streams.Stream;
import com.kolibrifx.plovercrest.server.streams.StreamObserver;
public class LocalTableReader extends TableReader {
private class CollectObserver implements StreamObserver {
private T[] storage = null;
private int count = 0;
@Override
public void onObserve(final long timestamp, final long index, final ByteBuffer bytes) {
final T element = serializer.unserialize(timestamp, bytes);
storage[count++] = element;
}
@Override
public void onObserveEnd(final long lastValidTimestamp, final long elementCount) {
}
@Override
public void onObserveFrozen() {
}
public void reset(final T[] storage) {
this.storage = storage;
this.count = 0;
}
}
private final PollableReader reader;
private final CollectObserver collectObserver = new CollectObserver();
private final RangeStreamObserver rangeObserver;
public LocalTableReader(final Stream stream,
final TableSerializer serializer,
final Class elementClass,
final RangeQuery range,
final String tableName) {
super(serializer, elementClass, range, tableName);
rangeObserver =
RangeStreamUtils.createRangeObserver(range.getRangeType(), range.getStart(), range.getEnd(), 0,
collectObserver);
switch (range.getRangeType()) {
case OVERLAPPING_TIME_RANGE: {
reader = stream.createReaderFromTimestamp(range.getStart(), rangeObserver);
break;
}
case NON_OVERLAPPING_TIME_RANGE: {
reader = stream.createReaderFromTimestamp(range.getStart(), rangeObserver);
break;
}
case ELEMENT_INDEX_RANGE: {
reader = stream.createReaderFromIndex(range.getStart(), rangeObserver);
break;
}
default: {
throw new NotImplementedException("Unknown range type " + range.getRangeType());
}
}
}
@Override
public ReadInfo read(final T[] storage, final ReaderStrategy strategy) {
collectObserver.reset(storage);
final AtomicBoolean timedOut = new AtomicBoolean(); // FIXME: implement timeout logic
final RangeReadInfo info = RangeStreamUtils.doRead(reader, storage.length, rangeObserver, timedOut).getInfo();
Arrays.fill(storage, collectObserver.count, storage.length, null);
return new ReadInfo(collectObserver.count, info.isAtEndOfStream(), info.getLastValidTimestamp(),
info.isAtEndOfRange());
}
}