com.kolibrifx.plovercrest.server.TableWriter 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;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.log4j.Logger;
import com.kolibrifx.plovercrest.client.PlovercrestException;
import com.kolibrifx.plovercrest.server.internal.EventType;
import com.kolibrifx.plovercrest.server.internal.engine.DeferredTableMapper;
public class TableWriter {
private static final Logger log = Logger.getLogger(TableWriter.class);
private final DeferredTableMapper mapper;
private final Table table;
TableWriter(final Table table, final DeferredTableMapper mapper) {
this.table = table;
this.mapper = mapper;
}
public void write(final long timestamp, final ByteBuffer buffer) {
try {
mapper.write(timestamp, buffer);
table.dispatchEvent(EventType.ENTRY_WRITTEN);
} catch (final IOException e) {
throw new PlovercrestException("Failed to write", e);
} catch (final IllegalArgumentException iae) {
log.error("Caught illegal argument exception when writing to " + table.getName(), iae);
log.error("Tried to write " + buffer.toString());
throw iae;
}
}
public void updateLastValidTimestamp(final long timestamp) {
if (mapper.updateLastValidTimestamp(timestamp)) {
table.dispatchEvent(EventType.LAST_VALID_TIMESTAMP_UPDATED);
}
}
public boolean isFrozen() {
return mapper.isFrozen();
}
/**
* Freeze (write protect) the table. If it is already frozen, do nothing.
*
* Note: freezing a table will also flush it.
*/
public void freeze() {
if (mapper.markAsFrozen()) {
mapper.force();
table.dispatchEvent(EventType.TABLE_FROZEN);
}
}
/**
* If the table is frozen, unfreeze it (remove the write protection). If is is not frozen, do
* nothing. Use with care, the table was probably frozen for a reason.
*/
public void unfreeze() {
mapper.unfreeze();
// Could add a TABLE_UNFROZEN event here, but wait until we need if
}
/**
* Force changes to be written to disk (through {@link java.nio.MappedByteBuffer#force()}.
*/
public void force() {
mapper.force();
}
}