com.kolibrifx.plovercrest.server.internal.streams.builtin.InMemoryStream 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.streams.builtin;
import java.util.List;
import com.kolibrifx.common.NotImplementedException;
import com.kolibrifx.plovercrest.client.TableMetadata;
import com.kolibrifx.plovercrest.client.TimestampExtractor;
import com.kolibrifx.plovercrest.server.streams.PollableReader;
import com.kolibrifx.plovercrest.server.streams.ReaderPosition;
import com.kolibrifx.plovercrest.server.streams.Stream;
import com.kolibrifx.plovercrest.server.streams.StreamDataTriggerer;
import com.kolibrifx.plovercrest.server.streams.StreamObserver;
import com.kolibrifx.plovercrest.server.streams.StreamWriter;
public class InMemoryStream implements Stream {
private final List data;
private final TimestampExtractor timestampExtractor;
private final Class elementClass;
private final TableMetadata metadata;
private final String name;
public InMemoryStream(final Class elementClass,
final String name,
final List data,
final TimestampExtractor timestampExtractor,
final TableMetadata metadata) {
this.elementClass = elementClass;
this.name = name;
this.data = data;
this.timestampExtractor = timestampExtractor;
this.metadata = metadata;
}
@Override
public PollableReader createReaderFromTimestamp(final long timestamp, final StreamObserver observer) {
return createReaderFromIndex(indexOfTimestamp(timestamp), observer);
}
@Override
public PollableReader createReaderFromIndex(final long index, final StreamObserver observer) {
return new PollableReader() {
int i = (int) index;
@Override
public void registerDataTriggerer(final StreamDataTriggerer triggerer) {
// do nothing
}
@Override
public boolean poll() {
if (i >= data.size()) {
observer.onObserveEnd(getLastValidTimestamp(), data.size());
return false;
} else {
final T message = data.get(i);
final long timestamp = timestampExtractor.getTimestampForMessage(message);
observer.onObserve(timestamp, index, message);
i++;
return true;
}
}
@Override
public ReaderPosition getReaderPosition() {
return ReaderPosition.createIndexPosition(i);
}
};
}
@Override
public String getName() {
return name;
}
@Override
public Class getElementClass() {
return elementClass;
}
@Override
public StreamWriter getWriter() {
throw new NotImplementedException();
}
@Override
public long getFirstTimestamp() {
if (data.isEmpty()) {
return -1;
}
return timestampExtractor.getTimestampForMessage(data.get(0));
}
@Override
public long getLastTimestamp() {
if (data.isEmpty()) {
return -1;
}
return timestampExtractor.getTimestampForMessage(data.get(data.size() - 1));
}
@Override
public long getDataLengthInBytes() {
return 0;
}
@Override
public long getEntryCount() {
return data.size();
}
@Override
public TableMetadata getMetadata() {
return metadata;
}
@Override
public long getLastValidTimestamp() {
return getLastTimestamp();
}
@Override
public boolean isFrozen() {
return false;
}
@Override
public boolean delete() {
return false;
}
@Override
public boolean rename(final String newName) {
return false;
}
private int indexOfTimestamp(final long timestamp) {
// take-previous
for (int i = 0; i < data.size(); i++) {
final long timestampForMessage = timestampExtractor.getTimestampForMessage(data.get(i));
if (timestampForMessage == timestamp) {
return i;
}
if (timestampForMessage > timestamp) {
return Math.max(0, i - 1);
}
}
// return index beyond end
return data.size();
}
@Override
public long seekPreviousTimestamp(final long targetTimestamp) {
final int i = indexOfTimestamp(targetTimestamp);
if (i == data.size()) {
return getLastTimestamp();
} else {
return timestampExtractor.getTimestampForMessage(data.get(i));
}
}
}