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

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

Introduction

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

Prototype

public void decode(ECChunk[] inputs, int[] erasedIndexes, ECChunk[] outputs) throws IOException 

Source Link

Document

Decode with inputs and erasedIndexes, generates outputs.

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;//from   www.j  ava 2 s .  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();
}