com.kolibrifx.plovercrest.server.internal.streams.TableStreamProvider 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;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.stream.Collectors;
import com.kolibrifx.common.Disposable;
import com.kolibrifx.plovercrest.client.AbstractTableSerializer;
import com.kolibrifx.plovercrest.client.TableMetadata;
import com.kolibrifx.plovercrest.client.TableSerializer;
import com.kolibrifx.plovercrest.client.TableSerializerFactory;
import com.kolibrifx.plovercrest.server.PlovercrestEngine;
import com.kolibrifx.plovercrest.server.Table;
import com.kolibrifx.plovercrest.server.TableInfo;
import com.kolibrifx.plovercrest.server.internal.TableNamesListenerHandler;
import com.kolibrifx.plovercrest.server.streams.Stream;
import com.kolibrifx.plovercrest.server.streams.StreamEngine;
import com.kolibrifx.plovercrest.server.streams.StreamInfo;
import com.kolibrifx.plovercrest.server.streams.StreamNamesListener;
import com.kolibrifx.plovercrest.server.streams.StreamProvider;
public class TableStreamProvider implements StreamProvider {
private static class ByteBufferReadSerializer extends AbstractTableSerializer {
@Override
public void serialize(final ByteBuffer out, final ByteBuffer m) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuffer unserialize(final long timestamp, final ByteBuffer in) {
return in;
}
@Override
public Class elementClass() {
return ByteBuffer.class;
}
@Override
public long getTimestampForMessage(final ByteBuffer message) {
throw new UnsupportedOperationException();
}
}
private static final ByteBufferReadSerializer NOOP_READER = new ByteBufferReadSerializer();
private final PlovercrestEngine engine;
private final TableSerializerFactory serializerFactory;
public TableStreamProvider(final PlovercrestEngine engine, final TableSerializerFactory serializerFactory) {
this.engine = engine;
this.serializerFactory = serializerFactory;
}
@SuppressWarnings("unchecked")
private TableSerializer getSerializer(final Class elementClass, final TableMetadata metadata) {
if (elementClass == ByteBuffer.class) {
// hackish: rethink this
return (TableSerializer) NOOP_READER;
}
return serializerFactory.createSerializer(metadata, elementClass);
}
@Override
public Stream tryOpen(final StreamEngine tools, final String name, final Class elementClass) {
final Table table = engine.open(name);
if (table == null) {
return null;
}
return new TableStream<>(engine, table, getSerializer(elementClass, table.getInfo().getMetadata()));
}
@Override
public Collection list() {
return engine.getTableNames().stream().map(x -> new StreamInfo(x, true)).collect(Collectors.toList());
}
@Override
public boolean tryCreate(final TableInfo info) {
engine.create(info); // can throw
return true;
}
@Override
public Disposable addAvailableStreamsListener(final StreamNamesListener listener) {
return new TableNamesListenerHandler(engine, listener);
}
}