LongBitFormat应用

LongBitFormat是一个工具类,可以把多项小的数据值压缩在一个long类型的数据中,以下为一个测试用例,PREFERED_BLOCK_SIZE占48位,REPLICATION占12位,STORAGE_POLICY_ID占4位,共占64位,并且提供了从一个long类型值中,提取各值的方法。还提供了一个根据各值,得到组合的long值的方法toLong(long  preferredBlockSize, short replication,
            byte storagePolicyID)。


import org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite;
import org.apache.hadoop.hdfs.util.LongBitFormat;

public class LongFormatTest {
    static enum HeaderFormat {
        PREFERRED_BLOCK_SIZE(null, 48, 1),
        REPLICATION(PREFERRED_BLOCK_SIZE.BITS, 12, 1),
        STORAGE_POLICY_ID(REPLICATION.BITS, BlockStoragePolicySuite.ID_BIT_LENGTH,
            0);

        private final LongBitFormat BITS;

        private HeaderFormat(LongBitFormat previous, int length, long min) {
          BITS = new LongBitFormat(name(), previous, length, min);
        }

        static short getReplication(long header) {
          return (short)REPLICATION.BITS.retrieve(header);
        }

        static long getPreferredBlockSize(long header) {
          return PREFERRED_BLOCK_SIZE.BITS.retrieve(header);
        }

        static byte getStoragePolicyID(long header) {
          return (byte)STORAGE_POLICY_ID.BITS.retrieve(header);
        }

        static long toLong(long preferredBlockSize, short replication,
            byte storagePolicyID) {
          long h = 0;
          h = PREFERRED_BLOCK_SIZE.BITS.combine(preferredBlockSize, h);
          h = REPLICATION.BITS.combine(replication, h);
          h = STORAGE_POLICY_ID.BITS.combine(storagePolicyID, h);
          return h;
        }
      }
    public static void main(String[] args) {
        long blockSize = 512;
        System.out.println("block size:         " + HeaderFormat.getPreferredBlockSize(blockSize));
        long replication  = 3L  << 48;
        
        System.out.println("replication:        " + HeaderFormat.getReplication(replication));
        long storagePolicyID = 2L << 60;
        System.out.println("storagePolicyID:    " + HeaderFormat.getStoragePolicyID(storagePolicyID));
        System.out.println("added value:        "+ (blockSize + replication  +storagePolicyID));
        System.out.println("get combined value :" + HeaderFormat.toLong(512, (short)3 , (byte)2));
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值