redis bitmapビット操作.


Redisでは2.2.0バージョンからsetbitgetbitbitcountなどいくつかのbitmap関連コマンドが追加されました.新しいコマンドですが、新しいデータ型は追加されていません.redisはbitmapに対してバイナリbitビットの動作を実現し、いくつかの状態情報を記録する.1バイトが8ビットを占めます.bitビットのバイナリの0と1でそれぞれ1つの状態を表し、ある状態情報を記録する.すべての状態がバイナリベースのbitビットを記録しているからです.そのため、これらの状態の占有スペースを大幅に節約しました.
 
構文はredisドキュメントを参照してください.
 
Jedisはredis bitmapに対して操作情報を以下のように設定する.
public String   tesetBitMap(@PathVariable String  key){
        jimClient.setBit("bitmattest",0,true);// bit   0      1
        jimClient.setBit("bitmattest",1,true);
        jimClient.setBit("bitmattest",2,true);
        jimClient.setBit("bitmattest",3,true);
        jimClient.setBit("bitmattest",4,false);
        jimClient.setBit("bitmattest",5,false);
        jimClient.setBit("bitmattest",6,true);
        jimClient.setBit("bitmattest",7,true);// bit   7      1     0 - 7         
        jimClient.setBit("bitmattest",8,true);//8          bit   15            bit 
        jimClient.setBit("bitmattest",9,true);
        jimClient.setBit("bitmattest",10,false);
        jimClient.setBit("bitmattest",11,false);
        jimClient.setBit("bitmattest",17,true);
        jimClient.setBit("bitmattest",20,true);
        jimClient.setBit("bitmattest",23,true);
        jimClient.setBit("bitmattest",24,true);

        jimClient.setBit("bitmattest",26,false);

        long a = jimClient.bitCount("bitmattest");//        1   
        long b11 = jimClient.bitCount("bitmattest",0L,0L);//   1   ( 0    7 )   1   
        long b12 = jimClient.bitCount("bitmattest",0L,1L);//       ( 0    15 )   1   
        long b123 = jimClient.bitCount("bitmattest",2L,5L);//              ( 16    39 )   1   

       boolean a0 =  jimClient.getBit("bitmattest",0);
       boolean a1 =  jimClient.getBit("bitmattest",1);
       boolean a2 =  jimClient.getBit("bitmattest",2);
       boolean a3 =  jimClient.getBit("bitmattest",3);
       boolean a4 =  jimClient.getBit("bitmattest",4);
       boolean a5 =  jimClient.getBit("bitmattest",5);
       boolean a6 =  jimClient.getBit("bitmattest",6);
       boolean a7 =  jimClient.getBit("bitmattest",7);
       boolean a8 =  jimClient.getBit("bitmattest",8);
       boolean a9 =  jimClient.getBit("bitmattest",9);
       boolean a10 =  jimClient.getBit("bitmattest",10);


        byte[] aaa =  jimClient.get("bitmattest".getBytes());//    bitmap
//       String bbb = new String(aaa);
        BitSet  set =  PromotionUserBitSetHelper.byteArray2BitSet(aaa); //  java  BitSet BitSet      Java api

        boolean b0 = set.get(0);
        boolean b1 = set.get(1);
        boolean b2 = set.get(2);
        boolean b3 = set.get(3);
        boolean b4 = set.get(4);
        boolean b5 = set.get(5);
        boolean b6 = set.get(6);
        boolean b7 = set.get(7);
        boolean b8 = set.get(8);
        boolean b9 = set.get(9);
        boolean b10 = set.get(10);


        System.out.println("a0=" + a0 + " ");
        System.out.println("a1=" + a1 + " ");
        System.out.println("a2=" + a2 + " ");
        System.out.println("a3=" + a3 + " ");
        System.out.println("a4=" + a4 + " ");
        System.out.println("a5=" + a5 + " ");
        System.out.println("a6=" + a6 + " ");
        System.out.println("a7=" + a7 + " ");
        System.out.println("a8=" + a8 + " ");
        System.out.println("a9=" + a9 + " ");
        System.out.println("a10=" + a10 + " ");
        System.out.println("     true:" + a);

        System.out.println("b0=" + b0);
        System.out.println("b1=" + b1);
        System.out.println("b2=" + b2);
        System.out.println("b3=" + b3);
        System.out.println("b4=" + b4);
        System.out.println("b5=" + b5);
        System.out.println("b6=" + b6);
        System.out.println("b7=" + b7);
        System.out.println("b8=" + b8);
        System.out.println("b9=" + b9);
        System.out.println("b10=" + b10);
        return "123";
    }

JavaのbyteとBitSetの様々な変換ツールクラス:

public class PromotionUserBitSetHelper {

	
	
	static final String BIN_PREFIX = "01";
 

	/**
	 * bitset offset  
	 *               index=0
	 *            
	 *            
	 *              
	 *              
	 *       actType   
	 *       
	 * 
	 */
	public static Integer calculateOffset(PromotionNewUser pnu ){  
		return pnu.getFirstActType()*2-pnu.getFirstActRole();
	}
	
	public static Integer calculateOffset(FirstUserPromotionDTO dto ){  
		return dto.getActType()*2-dto.getActRole();
	}
	
	public static BitSet byteArray2BitSet(byte[] bytes) {
		BitSet bitSet = new BitSet(bytes.length * 8);
		int index = 0;
		for (int i = 0; i < bytes.length; i++) {
			for (int j = 7; j >= 0; j--) {
				bitSet.set(index++, (bytes[i] & (1 << j)) >> j == 1 ? true
						: false);
			}
		}
		return bitSet;
	}
	
	
	
	public static String binary(byte[] bytes, int radix) {
		return new BigInteger(1, bytes).toString(radix);//    1    
	}
 

	public static byte[] bitSet2ByteArray(BitSet bitSet,int maxSize) {
		byte[] bytes = new byte[maxSize];
		for (int i = 0; i < maxSize * 8; i++) {
			int index = i / 8;
			int offset = 7 - i % 8;
			bytes[index] |= (bitSet.get(i) ? 1 : 0) << offset;
		}
		return bytes;
	}
	 
	
	public static String toBinaryString(BitSet bitset,int maxSize){
		if(bitset == null){
			return "0";
		} 
		StringBuilder sb = new StringBuilder(maxSize * 8);

		for (int i = 1; i < maxSize * 8; i++) {
			sb.append(bitset.get(i)?1:0);
		}
		return sb.toString();
	}
	
	public static String bitSet2BinString(BitSet bitset){

		if(bitset == null){
			return BIN_PREFIX+"0";
		}
		 
 		return BIN_PREFIX + PromotionUserBitSetHelper.toBinaryString(bitset,UserActConstant.MAX_BYTE_ARRAY_SIZE);
	} 


	public static BitSet binaryToBitSet(String binString){
		if(StringUtils.isBlank(binString)){
			return null;
		}
		
		
		if (binString.startsWith(BIN_PREFIX)) {
			int size = UserActConstant.MAX_BYTE_ARRAY_SIZE * 8;
			BitSet bs = new BitSet(size);
			binString = binString.substring(BIN_PREFIX.length());
			for(int i = 0;i< size && i< binString.length(); i++){
				if(binString.charAt(i) == '1'){
				    bs.set(i);
				} 
			} 
		}

		return null;
	}

}