All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kolibrifx.plovercrest.server.internal.local.LocalTableWriter Maven / Gradle / Ivy

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.nio.ByteBuffer;
import org.apache.log4j.Logger;
import com.kolibrifx.plovercrest.client.PlovercrestException;
import com.kolibrifx.plovercrest.client.TableSerializer;
import com.kolibrifx.plovercrest.client.TableWriter;
import com.kolibrifx.plovercrest.client.internal.ErrorListenerSubject;
import com.kolibrifx.plovercrest.server.streams.StreamWriter;

public class LocalTableWriter extends TableWriter {
    private static final Logger log = Logger.getLogger(LocalTableWriter.class);

    private final StreamWriter tableWriter;
    private final ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);

    public LocalTableWriter(final StreamWriter tableWriter,
                            final TableSerializer serializer,
                            final ErrorListenerSubject errorListenerSubject) {
        super(serializer, errorListenerSubject);
        this.tableWriter = tableWriter;
    }

    @Override
    public void write(final T m) {
        try {
            buffer.clear();
            serializer.serialize(buffer, m);
            buffer.flip();
            tableWriter.write(serializer.getTimestampForMessage(m), buffer);
        } catch (final PlovercrestException e) {
            log.error(e.getMessage(), e);
            errorListenerSubject.onWriteError(e, m);
            // not rethrowing exceptions; we want to be compatible with the remote interface,
            // and attempt to write subsequent writes.
            // TODO: this is bad for connect errors, find a better strategy there...?
        }
    }

    @Override
    public void freeze() {
        tableWriter.freeze();
    }

    @Override
    public boolean isFrozen() {
        return tableWriter.isFrozen();
    }

    @Override
    public void unfreeze() {
        tableWriter.unfreeze();
    }

    @Override
    public void flush() {
        tableWriter.flush();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy