|
| 1 | +// EXAMPLE: bitmap_tutorial |
| 2 | +package io.redis.examples; |
| 3 | + |
| 4 | +import redis.clients.jedis.UnifiedJedis; |
| 5 | +import redis.clients.jedis.args.BitOP; |
| 6 | +// REMOVE_START |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | +// REMOVE_END |
| 12 | + |
| 13 | +public class BitMapsExample { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void run() { |
| 17 | + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); |
| 18 | + |
| 19 | + // REMOVE_START |
| 20 | + jedis.del("pings:2024-01-01-00:00"); |
| 21 | + // REMOVE_END |
| 22 | + |
| 23 | + // STEP_START ping |
| 24 | + boolean res1 = jedis.setbit("pings:2024-01-01-00:00", 123, true); |
| 25 | + System.out.println(res1); // >>> false |
| 26 | + |
| 27 | + boolean res2 = jedis.getbit("pings:2024-01-01-00:00", 123); |
| 28 | + System.out.println(res2); // >>> true |
| 29 | + |
| 30 | + boolean res3 = jedis.getbit("pings:2024-01-01-00:00", 456); |
| 31 | + System.out.println(res3); // >>> false |
| 32 | + // STEP_END |
| 33 | + |
| 34 | + // REMOVE_START |
| 35 | + assertFalse(res1); |
| 36 | + assertTrue(res2); |
| 37 | + assertFalse(res3); |
| 38 | + // REMOVE_END |
| 39 | + |
| 40 | + // STEP_START bitcount |
| 41 | + long res4 = jedis.bitcount("pings:2024-01-01-00:00"); |
| 42 | + System.out.println(res4); // >>> 1 |
| 43 | + // STEP_END |
| 44 | + |
| 45 | + // REMOVE_START |
| 46 | + assertEquals(1, res4); |
| 47 | + // REMOVE_END |
| 48 | + // REMOVE_START |
| 49 | + jedis.del("A", "B", "C", "R"); |
| 50 | + // REMOVE_END |
| 51 | + |
| 52 | + // STEP_START bitop_setup |
| 53 | + jedis.setbit("A", 0, true); |
| 54 | + jedis.setbit("A", 1, true); |
| 55 | + jedis.setbit("A", 3, true); |
| 56 | + jedis.setbit("A", 4, true); |
| 57 | + |
| 58 | + byte[] res5 = jedis.get("A".getBytes()); |
| 59 | + System.out.println(String.format("%8s", Integer.toBinaryString(res5[0] & 0xFF)).replace(' ', '0')); |
| 60 | + // >>> 11011000 |
| 61 | + |
| 62 | + jedis.setbit("B", 3, true); |
| 63 | + jedis.setbit("B", 4, true); |
| 64 | + jedis.setbit("B", 7, true); |
| 65 | + |
| 66 | + byte[] res6 = jedis.get("B".getBytes()); |
| 67 | + System.out.println(String.format("%8s", Integer.toBinaryString(res6[0] & 0xFF)).replace(' ', '0')); |
| 68 | + // >>> 00011001 |
| 69 | + |
| 70 | + jedis.setbit("C", 1, true); |
| 71 | + jedis.setbit("C", 2, true); |
| 72 | + jedis.setbit("C", 4, true); |
| 73 | + jedis.setbit("C", 5, true); |
| 74 | + |
| 75 | + byte[] res7 = jedis.get("C".getBytes()); |
| 76 | + System.out.println(String.format("%8s", Integer.toBinaryString(res7[0] & 0xFF)).replace(' ', '0')); |
| 77 | + // >>> 01101100 |
| 78 | + // STEP_END |
| 79 | + // REMOVE_START |
| 80 | + assertEquals(0b11011000, res5[0] & 0xFF); |
| 81 | + assertEquals(0b00011001, res6[0] & 0xFF); |
| 82 | + assertEquals(0b01101100, res7[0] & 0xFF); |
| 83 | + // REMOVE_END |
| 84 | + |
| 85 | + // STEP_START bitop_and |
| 86 | + jedis.bitop(BitOP.AND, "R", "A", "B", "C"); |
| 87 | + byte[] res8 = jedis.get("R".getBytes()); |
| 88 | + System.out.println(String.format("%8s", Integer.toBinaryString(res8[0] & 0xFF)).replace(' ', '0')); |
| 89 | + // >>> 00001000 |
| 90 | + // STEP_END |
| 91 | + // REMOVE_START |
| 92 | + assertEquals(0b00001000, res8[0] & 0xFF); |
| 93 | + // REMOVE_END |
| 94 | + |
| 95 | + // STEP_START bitop_or |
| 96 | + jedis.bitop(BitOP.OR, "R", "A", "B", "C"); |
| 97 | + byte[] res9 = jedis.get("R".getBytes()); |
| 98 | + System.out.println(String.format("%8s", Integer.toBinaryString(res9[0] & 0xFF)).replace(' ', '0')); |
| 99 | + // >>> 11111101 |
| 100 | + // STEP_END |
| 101 | + // REMOVE_START |
| 102 | + assertEquals(0b11111101, res9[0] & 0xFF); |
| 103 | + // REMOVE_END |
| 104 | + |
| 105 | + // STEP_START bitop_xor |
| 106 | + jedis.bitop(BitOP.XOR, "R", "A", "B"); |
| 107 | + byte[] res10 = jedis.get("R".getBytes()); |
| 108 | + System.out.println(String.format("%8s", Integer.toBinaryString(res10[0] & 0xFF)).replace(' ', '0')); |
| 109 | + // >>> 11000001 |
| 110 | + // STEP_END |
| 111 | + // REMOVE_START |
| 112 | + assertEquals(0b11000001, res10[0] & 0xFF); |
| 113 | + // REMOVE_END |
| 114 | + |
| 115 | + // STEP_START bitop_not |
| 116 | + jedis.bitop(BitOP.NOT, "R", "A"); |
| 117 | + byte[] res11 = jedis.get("R".getBytes()); |
| 118 | + System.out.println(String.format("%8s", Integer.toBinaryString(res11[0] & 0xFF)).replace(' ', '0')); |
| 119 | + // >>> 00100111 |
| 120 | + // STEP_END |
| 121 | + // REMOVE_START |
| 122 | + assertEquals(0b00100111, res11[0] & 0xFF); |
| 123 | + // REMOVE_END |
| 124 | + |
| 125 | + // STEP_START bitop_diff |
| 126 | + jedis.bitop(BitOP.DIFF, "R", "A", "B", "C"); |
| 127 | + byte[] res12 = jedis.get("R".getBytes()); |
| 128 | + System.out.println(String.format("%8s", Integer.toBinaryString(res12[0] & 0xFF)).replace(' ', '0')); |
| 129 | + // >>> 10000000 |
| 130 | + // STEP_END |
| 131 | + // REMOVE_START |
| 132 | + assertEquals(0b10000000, res12[0] & 0xFF); |
| 133 | + // REMOVE_END |
| 134 | + |
| 135 | + // STEP_START bitop_diff1 |
| 136 | + jedis.bitop(BitOP.DIFF1, "R", "A", "B", "C"); |
| 137 | + byte[] res13 = jedis.get("R".getBytes()); |
| 138 | + System.out.println(String.format("%8s", Integer.toBinaryString(res13[0] & 0xFF)).replace(' ', '0')); |
| 139 | + // >>> 00100101 |
| 140 | + // STEP_END |
| 141 | + // REMOVE_START |
| 142 | + assertEquals(0b00100101, res13[0] & 0xFF); |
| 143 | + // REMOVE_END |
| 144 | + |
| 145 | + // STEP_START bitop_andor |
| 146 | + jedis.bitop(BitOP.ANDOR, "R", "A", "B", "C"); |
| 147 | + byte[] res14 = jedis.get("R".getBytes()); |
| 148 | + System.out.println(String.format("%8s", Integer.toBinaryString(res14[0] & 0xFF)).replace(' ', '0')); |
| 149 | + // >>> 01011000 |
| 150 | + // STEP_END |
| 151 | + // REMOVE_START |
| 152 | + assertEquals(0b01011000, res14[0] & 0xFF); |
| 153 | + // REMOVE_END |
| 154 | + |
| 155 | + // STEP_START bitop_one |
| 156 | + jedis.bitop(BitOP.ONE, "R", "A", "B", "C"); |
| 157 | + byte[] res15 = jedis.get("R".getBytes()); |
| 158 | + System.out.println(String.format("%8s", Integer.toBinaryString(res15[0] & 0xFF)).replace(' ', '0')); |
| 159 | + // >>> 10100101 |
| 160 | + // STEP_END |
| 161 | + // REMOVE_START |
| 162 | + assertEquals(0b10100101, res15[0] & 0xFF); |
| 163 | + // REMOVE_END |
| 164 | + |
| 165 | + |
| 166 | +// HIDE_START |
| 167 | + jedis.close(); |
| 168 | + } |
| 169 | +} |
| 170 | +// HIDE_END |
0 commit comments