Example usage for org.apache.hadoop.hdfs.protocol LocatedBlocks getInsertIndex

List of usage examples for org.apache.hadoop.hdfs.protocol LocatedBlocks getInsertIndex

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs.protocol LocatedBlocks getInsertIndex.

Prototype

public static int getInsertIndex(int binSearchResult) 

Source Link

Usage

From source file:com.mellanox.r4h.DFSInputStream.java

License:Apache License

/**
 * Get block at the specified position./*from  w w w. j a va2  s.  co  m*/
 * Fetch it from the namenode if not cached.
 * 
 * @param offset
 *            block corresponding to this offset in file is returned
 * @return located block
 * @throws IOException
 */
private LocatedBlock getBlockAt(long offset) throws IOException {
    synchronized (infoLock) {
        assert (locatedBlocks != null) : "locatedBlocks is null";

        final LocatedBlock blk;

        // check offset
        if (offset < 0 || offset >= getFileLength()) {
            throw new IOException("offset < 0 || offset >= getFileLength(), offset=" + offset
                    + ", locatedBlocks=" + locatedBlocks);
        } else if (offset >= locatedBlocks.getFileLength()) {
            // offset to the portion of the last block,
            // which is not known to the name-node yet;
            // getting the last block
            blk = locatedBlocks.getLastLocatedBlock();
        } else {
            // search cached blocks first
            int targetBlockIdx = locatedBlocks.findBlock(offset);
            if (targetBlockIdx < 0) { // block is not cached
                targetBlockIdx = LocatedBlocks.getInsertIndex(targetBlockIdx);
                // fetch more blocks
                final LocatedBlocks newBlocks = dfsClient.getLocatedBlocks(src, offset);
                assert (newBlocks != null) : "Could not find target position " + offset;
                locatedBlocks.insertRange(targetBlockIdx, newBlocks.getLocatedBlocks());
            }
            blk = locatedBlocks.get(targetBlockIdx);
        }
        return blk;
    }
}

From source file:com.mellanox.r4h.DFSInputStream.java

License:Apache License

/** Fetch a block from namenode and cache it */
private void fetchBlockAt(long offset) throws IOException {
    synchronized (infoLock) {
        int targetBlockIdx = locatedBlocks.findBlock(offset);
        if (targetBlockIdx < 0) { // block is not cached
            targetBlockIdx = LocatedBlocks.getInsertIndex(targetBlockIdx);
        }//w ww . j  av  a 2 s  .  com
        // fetch blocks
        final LocatedBlocks newBlocks = dfsClient.getLocatedBlocks(src, offset);
        if (newBlocks == null) {
            throw new IOException("Could not find target position " + offset);
        }
        locatedBlocks.insertRange(targetBlockIdx, newBlocks.getLocatedBlocks());
    }
}

From source file:com.mellanox.r4h.DFSInputStream.java

License:Apache License

/**
 * Get blocks in the specified range./*w ww . j  ava 2s  .  c  om*/
 * Includes only the complete blocks.
 * Fetch them from the namenode if not cached.
 */
private List<LocatedBlock> getFinalizedBlockRange(long offset, long length) throws IOException {
    synchronized (infoLock) {
        assert (locatedBlocks != null) : "locatedBlocks is null";
        List<LocatedBlock> blockRange = new ArrayList<LocatedBlock>();
        // search cached blocks first
        int blockIdx = locatedBlocks.findBlock(offset);
        if (blockIdx < 0) { // block is not cached
            blockIdx = LocatedBlocks.getInsertIndex(blockIdx);
        }
        long remaining = length;
        long curOff = offset;
        while (remaining > 0) {
            LocatedBlock blk = null;
            if (blockIdx < locatedBlocks.locatedBlockCount())
                blk = locatedBlocks.get(blockIdx);
            if (blk == null || curOff < blk.getStartOffset()) {
                LocatedBlocks newBlocks;
                newBlocks = dfsClient.getLocatedBlocks(src, curOff, remaining);
                locatedBlocks.insertRange(blockIdx, newBlocks.getLocatedBlocks());
                continue;
            }
            assert curOff >= blk.getStartOffset() : "Block not found";
            blockRange.add(blk);
            long bytesRead = blk.getStartOffset() + blk.getBlockSize() - curOff;
            remaining -= bytesRead;
            curOff += bytesRead;
            blockIdx++;
        }
        return blockRange;
    }
}