com.kolibrifx.plovercrest.server.internal.local.LocalMultiTableWriter 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.util.HashMap;
import com.kolibrifx.plovercrest.client.AsyncCallback;
import com.kolibrifx.plovercrest.client.MultiTableWriter;
import com.kolibrifx.plovercrest.client.MultiTableWriterSerializerFactory;
import com.kolibrifx.plovercrest.client.PlovercrestException;
import com.kolibrifx.plovercrest.client.Table;
import com.kolibrifx.plovercrest.client.TableSerializer;
import com.kolibrifx.plovercrest.client.TableWriter;
import com.kolibrifx.plovercrest.server.internal.streams.UpdateLastValidTimestampViaStreams;
import com.kolibrifx.plovercrest.server.local.DefaultPlovercrestLocal;
import com.kolibrifx.plovercrest.server.streams.StreamEngine;
public class LocalMultiTableWriter extends MultiTableWriter {
private final HashMap> writers;
private final DefaultPlovercrestLocal plovercrest;
private final UpdateLastValidTimestampViaStreams updateLastValidTimestampActivator;
public LocalMultiTableWriter(final DefaultPlovercrestLocal plovercrest,
final StreamEngine streamEngine,
final MultiTableWriterSerializerFactory factory,
final WriterMode mode) {
super(factory, mode);
writers = new HashMap>();
this.plovercrest = plovercrest;
if (mode == WriterMode.LIVE) {
this.updateLastValidTimestampActivator = new UpdateLastValidTimestampViaStreams(streamEngine);
} else {
this.updateLastValidTimestampActivator = null;
}
}
private TableWriter getOrCreateWriter(final String tableName) {
TableWriter writer = writers.get(tableName);
if (writer != null) {
return writer;
}
final TableSerializer serializer = factory.createTableSerializer(tableName);
Table table = plovercrest.open(tableName, serializer.elementClass());
if (table == null) {
table = plovercrest.create(tableName, serializer);
if (table == null) {
throw new PlovercrestException("LocalMultiTableWriter failed to create table: " + tableName);
}
}
writer = table.getWriter();
writers.put(tableName, writer);
if (updateLastValidTimestampActivator != null) {
updateLastValidTimestampActivator.addStream(tableName);
}
return writer;
}
@Override
public void write(final String tableName, final T element) {
final TableWriter writer = getOrCreateWriter(tableName);
writer.write(element);
}
@Override
public void close() {
if (updateLastValidTimestampActivator != null) {
updateLastValidTimestampActivator.close();
}
}
@Override
public void forceRoundtrip() {
// no-op
}
@Override
public void asyncRoundtrip(final AsyncCallback cb) {
cb.onSuccess();
}
}