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

net.quasardb.qdb.ts.AutoFlushWriter Maven / Gradle / Ivy

Go to download

API for the JNI components of the QuasarDB API for Java. Should not be included directly.

There is a newer version: 3.14.1
Show newest version
package net.quasardb.qdb.ts;

import java.io.IOException;
import java.io.Flushable;
import java.lang.AutoCloseable;
import java.nio.channels.SeekableByteChannel;
import java.util.*;

import net.quasardb.qdb.*;
import net.quasardb.qdb.jni.*;

/**
 * An implementation of a Writer that automatically flushes the local cache when
 * a certain threshold has been reached.
 *
 * As with Writer, usage of instances of this class is not thread-safe. Use an
 * AutFlushWriter instance per Thread in multi-threaded situations.
 */
public final class AutoFlushWriter extends Writer {

    long counter;
    long threshold;

    /**
     * Initialize a new auto-flushing timeseries table with a default threshold of 50000 rows.
     *
     * @param session Active connection with the QdbCluster
     * @param tables Timeseries tables we're writing to.
     */
    protected AutoFlushWriter(Session session, Table[] tables) {
        this(session, tables, false);
    }

    /**
     * Initialize a new auto-flushing timeseries table with a default threshold of 50000 rows.
     *
     * @param session Active connection with the QdbCluster
     * @param tables Timeseries tables we're writing to.
     * @param async When true, uses high-speed async writes
     */
    protected AutoFlushWriter(Session session, Table[] tables, boolean async) {
        this(session, tables, 50000, async);
    }

    /**
     * Initialize a new auto-flushing timeseries table.
     *
     * @param session Active connection with the QdbCluster
     * @param tables Timeseries tables we're writing to.
     * @param threshold The amount of rows to keep in local buffer before automatic flushing occurs.
     */
    protected AutoFlushWriter(Session session, Table[] tables, long threshold) {
        this(session, tables, threshold, false);
    }

    /**
     * Initialize a new auto-flushing timeseries table.
     *
     * @param session Active connection with the QdbCluster
     * @param tables Timeseries tables we're writing to.
     * @param threshold The amount of rows to keep in local buffer before automatic flushing occurs.
     * @param async When true, uses high-speed async writes
     */
    protected AutoFlushWriter(Session session, Table[] tables, long threshold, boolean async) {
        super(session, tables, async);

        this.counter = 0;
        this.threshold = threshold;
    }

    @Override
    public void append(Integer offset, Timespec timestamp, Value[] values) throws IOException {
        super.append(offset, timestamp, values);

        if (++counter >= threshold) {
            try {
                this.flush();
            } finally {
                this.counter = 0;
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy