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

com.nfsdb.journal.net.comsumer.FixedColumnDeltaConsumer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2014. Vlad Ilyushchenko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nfsdb.journal.net.comsumer;

import com.nfsdb.journal.column.AbstractColumn;
import com.nfsdb.journal.exceptions.JournalNetworkException;
import com.nfsdb.journal.net.AbstractChannelConsumer;
import com.nfsdb.journal.utils.ByteBuffers;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ReadableByteChannel;

public class FixedColumnDeltaConsumer extends AbstractChannelConsumer {

    private final ByteBuffer header = ByteBuffer.allocateDirect(8).order(ByteOrder.LITTLE_ENDIAN);
    private final AbstractColumn column;
    private long appendOffset;
    private long targetOffset = -1;
    private boolean headerProcessed = false;

    public FixedColumnDeltaConsumer(AbstractColumn column) {
        this.column = column;
        this.appendOffset = column.getOffset();
    }

    @Override
    public boolean isComplete() {
        return appendOffset == targetOffset;
    }

    @Override
    public void reset() {
        super.reset();
        appendOffset = column.getOffset();
        targetOffset = -1;
        headerProcessed = false;
        header.rewind();
    }

    @Override
    protected void doRead(ReadableByteChannel channel) throws JournalNetworkException {
        ByteBuffers.copy(channel, header);

        // if header is complete, extract target column size and release header
        // so we don't attempt to fill it up again
        if (!headerProcessed && header.remaining() == 0) {
            header.flip();
            this.targetOffset = appendOffset + header.getLong();
            headerProcessed = true;
        }

        while (!isComplete()) {
            ByteBuffer target = column.getBuffer(appendOffset, 1);
            int sz = ByteBuffers.copy(channel, target, (int) (targetOffset - appendOffset));
            // using non-blocking IO it should be possible not to read anything
            // we need to give up here and let the rest of execution continue
            if (sz == 0) {
                break;
            }
            appendOffset += sz;
        }
    }

    @Override
    protected void commit() {
        column.preCommit(targetOffset);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy