Package org.apache.hadoop.hdfs.server.datanode.BlockDataFile

Examples of org.apache.hadoop.hdfs.server.datanode.BlockDataFile.RandomAccessor


  public void truncateBlock(long oldBlockFileLen, long newlen)
      throws IOException {
    if (newlen == 0) {
      // Special case for truncating to 0 length, since there's no previous
      // chunk.
      RandomAccessor ra = blockDataFile.getRandomAccessor();
      try {
        // truncate blockFile
        ra.setLength(newlen);
      } finally {
        ra.close();
      }
      // update metaFile
      RandomAccessFile metaRAF = new RandomAccessFile(metafile, "rw");
      try {
        metaRAF.setLength(BlockMetadataHeader.getHeaderSize());
      } finally {
        metaRAF.close();
      }
      return;
    }
    DataChecksum dcs = BlockMetadataHeader.readHeader(metafile).getChecksum();
    int checksumsize = dcs.getChecksumSize();
    int bpc = dcs.getBytesPerChecksum();
    long newChunkCount = (newlen - 1) / bpc + 1;
    long newmetalen = BlockMetadataHeader.getHeaderSize() + newChunkCount
        * checksumsize;
    long lastchunkoffset = (newChunkCount - 1) * bpc;
    int lastchunksize = (int) (newlen - lastchunkoffset);
    byte[] b = new byte[Math.max(lastchunksize, checksumsize)];

    RandomAccessor ra = blockDataFile.getRandomAccessor();
    try {
      // truncate blockFile
      ra.setLength(newlen);

      // read last chunk
      ra.seek(lastchunkoffset);
      ra.readFully(b, 0, lastchunksize);
    } finally {
      ra.close();
    }

    // compute checksum
    dcs.update(b, 0, lastchunksize);
    dcs.writeValue(b, 0, false);
View Full Code Here


  public void truncateBlock(long newBlockLen)
      throws IOException {
    if (newBlockLen == 0) {
      // Special case for truncating to 0 length, since there's no previous
      // chunk.
      RandomAccessor ra = blockDataFile.getRandomAccessor();
      try {
        ra.setLength(BlockInlineChecksumReader.getHeaderSize());
      } finally {
        ra.close();
      }
      return;
    }

    DataChecksum dcs = DataChecksum.newDataChecksum(this.checksumType, this.bytesPerChecksum);
    this.checksumSize = dcs.getChecksumSize();

    long newBlockFileSize = BlockInlineChecksumReader
        .getFileLengthFromBlockSize(newBlockLen, bytesPerChecksum, checksumSize);

    int lastchunksize = (int) (newBlockLen % bytesPerChecksum);

    RandomAccessor ra = blockDataFile.getRandomAccessor();
    try {
      // truncate blockFile
      ra.setLength(newBlockFileSize);

      if (lastchunksize != 0) {
        // Calculate last partial checksum.
        long lastchunkoffset = BlockInlineChecksumReader.getPosFromBlockOffset(
            newBlockLen - lastchunksize, bytesPerChecksum, checksumSize);

        byte[] b = new byte[Math.max(lastchunksize, checksumSize)];

        // read last chunk
        ra.seek(lastchunkoffset);
        ra.readFully(b, 0, lastchunksize);

        // compute checksum
        dcs.update(b, 0, lastchunksize);
        dcs.writeValue(b, 0, false);

        ra.seek(newBlockFileSize - checksumSize);
        ra.write(b, 0, checksumSize);
      }
    } finally {
      ra.close();
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.hadoop.hdfs.server.datanode.BlockDataFile.RandomAccessor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.