com.kolibrifx.plovercrest.server.internal.protocol.MultiTableWriteHandler 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.protocol;
import java.io.Closeable;
import java.nio.ByteBuffer;
import java.util.HashMap;
import com.kolibrifx.plovercrest.client.MultiTableWriter.WriterMode;
import com.kolibrifx.plovercrest.client.PlovercrestException;
import com.kolibrifx.plovercrest.server.internal.EngineAdapter;
import com.kolibrifx.plovercrest.server.internal.streams.UpdateLastValidTimestampViaStreams;
import com.kolibrifx.plovercrest.server.streams.Stream;
import com.kolibrifx.plovercrest.server.streams.StreamEngine;
import com.kolibrifx.plovercrest.server.streams.StreamWriter;
public class MultiTableWriteHandler implements Closeable {
// private static final Logger log = Logger.getLogger(MultiTableWriteHandler.class);
private final HashMap writers;
private final StreamEngine engine;
private UpdateLastValidTimestampViaStreams updateLastValidTimestampActivator;
public MultiTableWriteHandler(final EngineAdapter engineAdapter, final WriterMode mode) {
this.engine = engineAdapter.getStreamEngine();
this.writers = new HashMap<>();
if (mode == WriterMode.LIVE) {
this.updateLastValidTimestampActivator = new UpdateLastValidTimestampViaStreams(engine);
} else {
this.updateLastValidTimestampActivator = null;
}
}
private StreamWriter getOrCreateWriter(final String tableName) {
StreamWriter writer = writers.get(tableName);
if (writer != null) {
return writer;
}
final Stream table = engine.openRaw(tableName);
if (table == null) {
throw new PlovercrestException("Attempt to write to non-existing table: " + tableName);
}
writer = table.getWriter();
writers.put(tableName, writer);
if (updateLastValidTimestampActivator != null) {
updateLastValidTimestampActivator.addStream(tableName);
}
return writer;
}
public void write(final String tableName, final long timestamp, final ByteBuffer buf) {
final StreamWriter writer = getOrCreateWriter(tableName);
writer.write(timestamp, buf);
}
@Override
public void close() {
if (updateLastValidTimestampActivator != null) {
updateLastValidTimestampActivator.close();
updateLastValidTimestampActivator = null;
}
}
}