Example usage for org.apache.hadoop.io.erasurecode.rawcoder RawErasureDecoder release

List of usage examples for org.apache.hadoop.io.erasurecode.rawcoder RawErasureDecoder release

Introduction

In this page you can find the example usage for org.apache.hadoop.io.erasurecode.rawcoder RawErasureDecoder release.

Prototype

public void release() 

Source Link

Document

Should be called when release this coder.

Usage

From source file:com.mellanox.erasurecode.rawcoder.RawErasureCoderValidationTest.java

License:Apache License

public static void performDecode(RawErasureDecoder decoder, RandomAccessFile inputFileReader, String inputFile,
        RandomAccessFile encodedFileReader, int[] erasuresArray, int[] redundantArray) throws IOException {

    boolean isDirect = (Boolean) decoder.getCoderOption(CoderOption.PREFER_DIRECT_BUFFER);

    ByteBuffer[] inputFileInputs = allocateBuffers(isDirect, numData);
    ByteBuffer[] encodedFileInputs = allocateBuffers(isDirect, numCode);
    ByteBuffer[] decodeInputs = allocateBuffers(isDirect, numData + numCode);
    ByteBuffer[] decodeOutputs = allocateBuffers(isDirect, erasuresArray.length);

    FileChannel inputFileChannel = inputFileReader.getChannel();
    FileChannel encodedFileChannel = encodedFileReader.getChannel();

    FileOutputStream inputfileOutputStream = new FileOutputStream(
            new File(inputFile + "." + coderIndex + ".decode.data"));
    FileChannel inputfileOutChannel = inputfileOutputStream.getChannel();

    FileOutputStream encodedFileOutputStream = new FileOutputStream(
            new File(inputFile + "." + coderIndex + ".decode.code"));
    FileChannel encodedFileOutChannel = encodedFileOutputStream.getChannel();

    long bytesLeft;

    ByteBuffer buffer;/*www.  j av a 2s.c o m*/

    // // read input files
    while ((bytesLeft = inputFileChannel.read(inputFileInputs)) > 0
            && encodedFileChannel.read(encodedFileInputs) > 0) {

        // memset unfull buffers and reset all input and encoded buffers
        resetBuffers(inputFileInputs, chunkSizeB);
        resetBuffers(encodedFileInputs, chunkSizeB);

        // prepare the decode inputs
        for (int i = 0; i < numData + numCode; i++) {
            decodeInputs[i] = i < numData ? inputFileInputs[i].duplicate()
                    : encodedFileInputs[i - numData].duplicate();
        }

        // unset erased indexes
        for (int erasedIndex : erasuresArray) {
            decodeInputs[erasedIndex] = null;
        }

        // unset redundant indexes
        for (int redundant : redundantArray) {
            decodeInputs[redundant] = null;
        }

        // perform decode
        decoder.decode(decodeInputs, erasuresArray, decodeOutputs);

        // reset positions
        resetBuffers(decodeInputs);
        resetBuffers(decodeOutputs);

        // set redundant indexes
        for (int redundant : redundantArray) {
            decodeInputs[redundant] = redundant < numData ? inputFileInputs[redundant].duplicate()
                    : encodedFileInputs[redundant - numData].duplicate();
        }

        // write to data file
        for (int i = 0; i < numData; i++) {
            buffer = decodeInputs[i] != null ? decodeInputs[i]
                    : decodeOutputs[getPlaceInArray(erasuresArray, i)];
            buffer.limit(Math.min(buffer.limit(), (int) bytesLeft));
            bytesLeft -= bytesLeft > 0 ? inputfileOutChannel.write(buffer) : 0;
        }

        // write to code file
        for (int i = numData; i < numData + numCode; i++) {
            buffer = decodeInputs[i] != null ? decodeInputs[i]
                    : decodeOutputs[getPlaceInArray(erasuresArray, i)];
            encodedFileOutChannel.write(buffer);
        }

        // reset decodeOutputs positions
        resetBuffers(decodeOutputs);
    }

    decoder.release();
    inputFileChannel.close();
    encodedFileChannel.close();
    inputfileOutputStream.close();
    encodedFileOutputStream.close();
}