|
| 1 | +// EXAMPLE: bitmap_tutorial |
| 2 | +package io.redis.examples.async; |
| 3 | + |
| 4 | +import io.lettuce.core.*; |
| 5 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 6 | +import io.lettuce.core.api.async.RedisAsyncCommands; |
| 7 | +import io.lettuce.core.codec.ByteArrayCodec; |
| 8 | + |
| 9 | +import java.util.concurrent.CompletableFuture; |
| 10 | + |
| 11 | +// REMOVE_START |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | +// REMOVE_END |
| 15 | + |
| 16 | +public class BitmapExample { |
| 17 | + |
| 18 | + // REMOVE_START |
| 19 | + @Test |
| 20 | + // REMOVE_END |
| 21 | + public void run() { |
| 22 | + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); |
| 23 | + |
| 24 | + try (StatefulRedisConnection<String, String> connection = redisClient.connect(); |
| 25 | + StatefulRedisConnection<byte[], byte[]> bytesConnection = redisClient.connect(ByteArrayCodec.INSTANCE)) { |
| 26 | + RedisAsyncCommands<String, String> async = connection.async(); |
| 27 | + RedisAsyncCommands<byte[], byte[]> asyncBytes = bytesConnection.async(); |
| 28 | + |
| 29 | + // REMOVE_START |
| 30 | + async.del("pings:2024-01-01-00:00", "A", "B", "C", "R").toCompletableFuture().join(); |
| 31 | + // REMOVE_END |
| 32 | + |
| 33 | + // STEP_START ping |
| 34 | + CompletableFuture<Void> ping = async.setbit("pings:2024-01-01-00:00", 123, 1).thenCompose(res1 -> { |
| 35 | + System.out.println(res1); // >>> 0 |
| 36 | + // REMOVE_START |
| 37 | + assertThat(res1).isEqualTo(0); |
| 38 | + // REMOVE_END |
| 39 | + return async.getbit("pings:2024-01-01-00:00", 123); |
| 40 | + }).thenCompose(res2 -> { |
| 41 | + System.out.println(res2); // >>> 1 |
| 42 | + // REMOVE_START |
| 43 | + assertThat(res2).isEqualTo(1); |
| 44 | + // REMOVE_END |
| 45 | + return async.getbit("pings:2024-01-01-00:00", 456); |
| 46 | + }).thenAccept(res3 -> { |
| 47 | + System.out.println(res3); // >>> 0 |
| 48 | + // REMOVE_START |
| 49 | + assertThat(res3).isEqualTo(0); |
| 50 | + // REMOVE_END |
| 51 | + }).toCompletableFuture(); |
| 52 | + // STEP_END |
| 53 | + ping.join(); |
| 54 | + |
| 55 | + // STEP_START bitcount |
| 56 | + CompletableFuture<Void> bitcount = async.bitcount("pings:2024-01-01-00:00").thenAccept(res4 -> { |
| 57 | + System.out.println(res4); // >>> 1 |
| 58 | + // REMOVE_START |
| 59 | + assertThat(res4).isEqualTo(1); |
| 60 | + // REMOVE_END |
| 61 | + }).toCompletableFuture(); |
| 62 | + // STEP_END |
| 63 | + bitcount.join(); |
| 64 | + |
| 65 | + // STEP_START bitop_setup |
| 66 | + CompletableFuture<Void> setup = async.setbit("A", 0, 1).thenCompose(v -> async.setbit("A", 1, 1)) |
| 67 | + .thenCompose(v -> async.setbit("A", 3, 1)).thenCompose(v -> async.setbit("A", 4, 1)) |
| 68 | + .thenCompose(v -> async.setbit("B", 3, 1)).thenCompose(v -> async.setbit("B", 4, 1)) |
| 69 | + .thenCompose(v -> async.setbit("B", 7, 1)).thenCompose(v -> async.setbit("C", 1, 1)) |
| 70 | + .thenCompose(v -> async.setbit("C", 2, 1)).thenCompose(v -> async.setbit("C", 4, 1)) |
| 71 | + .thenCompose(v -> async.setbit("C", 5, 1)).thenCompose(v -> asyncBytes.get("A".getBytes())) |
| 72 | + .thenApply(res -> { |
| 73 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 74 | + String bitsA = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 75 | + System.out.println(bitsA); // >>> 11011000 |
| 76 | + // REMOVE_START |
| 77 | + assertThat(bitsA).isEqualTo("11011000"); |
| 78 | + // REMOVE_END |
| 79 | + return bitsA; |
| 80 | + }).thenCompose(v -> asyncBytes.get("B".getBytes())).thenApply(res -> { |
| 81 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 82 | + String bitsB = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 83 | + System.out.println(bitsB); // >>> 00011001 |
| 84 | + // REMOVE_START |
| 85 | + assertThat(bitsB).isEqualTo("00011001"); |
| 86 | + // REMOVE_END |
| 87 | + return bitsB; |
| 88 | + }) |
| 89 | + // Print C |
| 90 | + .thenCompose(v -> asyncBytes.get("C".getBytes())).thenAccept(res -> { |
| 91 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 92 | + String bitsC = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 93 | + System.out.println(bitsC); // >>> 01101100 |
| 94 | + // REMOVE_START |
| 95 | + assertThat(bitsC).isEqualTo("01101100"); |
| 96 | + // REMOVE_END |
| 97 | + }).toCompletableFuture(); |
| 98 | + // STEP_END |
| 99 | + setup.join(); |
| 100 | + |
| 101 | + // STEP_START bitop_and |
| 102 | + CompletableFuture<Void> andOp = async.bitopAnd("R", "A", "B", "C") |
| 103 | + .thenCompose(len -> asyncBytes.get("R".getBytes())).thenAccept(res -> { |
| 104 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 105 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 106 | + System.out.println(bits); // >>> 00001000 |
| 107 | + // REMOVE_START |
| 108 | + assertThat(bits).isEqualTo("00001000"); |
| 109 | + // REMOVE_END |
| 110 | + }).toCompletableFuture(); |
| 111 | + // STEP_END |
| 112 | + andOp.join(); |
| 113 | + |
| 114 | + // STEP_START bitop_or |
| 115 | + CompletableFuture<Void> orOp = async.bitopOr("R", "A", "B", "C").thenCompose(len -> asyncBytes.get("R".getBytes())) |
| 116 | + .thenAccept(res -> { |
| 117 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 118 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 119 | + System.out.println(bits); // >>> 11111101 |
| 120 | + // REMOVE_START |
| 121 | + assertThat(bits).isEqualTo("11111101"); |
| 122 | + // REMOVE_END |
| 123 | + }).toCompletableFuture(); |
| 124 | + // STEP_END |
| 125 | + orOp.join(); |
| 126 | + |
| 127 | + // STEP_START bitop_xor |
| 128 | + CompletableFuture<Void> xorOp = async.bitopXor("R", "A", "B").thenCompose(len -> asyncBytes.get("R".getBytes())) |
| 129 | + .thenAccept(res -> { |
| 130 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 131 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 132 | + System.out.println(bits); // >>> 11000001 |
| 133 | + // REMOVE_START |
| 134 | + assertThat(bits).isEqualTo("11000001"); |
| 135 | + // REMOVE_END |
| 136 | + }).toCompletableFuture(); |
| 137 | + // STEP_END |
| 138 | + xorOp.join(); |
| 139 | + |
| 140 | + // STEP_START bitop_not |
| 141 | + CompletableFuture<Void> notOp = async.bitopNot("R", "A").thenCompose(len -> asyncBytes.get("R".getBytes())) |
| 142 | + .thenAccept(res -> { |
| 143 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 144 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 145 | + System.out.println(bits); // >>> 00100111 |
| 146 | + // REMOVE_START |
| 147 | + assertThat(bits).isEqualTo("00100111"); |
| 148 | + // REMOVE_END |
| 149 | + }).toCompletableFuture(); |
| 150 | + // STEP_END |
| 151 | + notOp.join(); |
| 152 | + |
| 153 | + // STEP_START bitop_diff |
| 154 | + CompletableFuture<Void> diffOp = async.bitopDiff("R", "A", "B", "C") |
| 155 | + .thenCompose(len -> asyncBytes.get("R".getBytes())).thenAccept(res -> { |
| 156 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 157 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 158 | + System.out.println(bits); // >>> 10000000 |
| 159 | + // REMOVE_START |
| 160 | + assertThat(bits).isEqualTo("10000000"); |
| 161 | + // REMOVE_END |
| 162 | + }).toCompletableFuture(); |
| 163 | + // STEP_END |
| 164 | + diffOp.join(); |
| 165 | + |
| 166 | + // STEP_START bitop_diff1 |
| 167 | + CompletableFuture<Void> diff1Op = async.bitopDiff1("R", "A", "B", "C") |
| 168 | + .thenCompose(len -> asyncBytes.get("R".getBytes())).thenAccept(res -> { |
| 169 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 170 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 171 | + System.out.println(bits); // >>> 00100101 |
| 172 | + // REMOVE_START |
| 173 | + assertThat(bits).isEqualTo("00100101"); |
| 174 | + // REMOVE_END |
| 175 | + }).toCompletableFuture(); |
| 176 | + // STEP_END |
| 177 | + diff1Op.join(); |
| 178 | + |
| 179 | + // STEP_START bitop_andor |
| 180 | + CompletableFuture<Void> andorOp = async.bitopAndor("R", "A", "B", "C") |
| 181 | + .thenCompose(len -> asyncBytes.get("R".getBytes())).thenAccept(res -> { |
| 182 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 183 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 184 | + System.out.println(bits); // >>> 01011000 |
| 185 | + // REMOVE_START |
| 186 | + assertThat(bits).isEqualTo("01011000"); |
| 187 | + // REMOVE_END |
| 188 | + }).toCompletableFuture(); |
| 189 | + // STEP_END |
| 190 | + andorOp.join(); |
| 191 | + |
| 192 | + // STEP_START bitop_one |
| 193 | + CompletableFuture<Void> oneOp = async.bitopOne("R", "A", "B", "C") |
| 194 | + .thenCompose(len -> asyncBytes.get("R".getBytes())).thenAccept(res -> { |
| 195 | + byte b = (res != null && res.length > 0) ? res[0] : 0; |
| 196 | + String bits = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); |
| 197 | + System.out.println(bits); // >>> 10100101 |
| 198 | + // REMOVE_START |
| 199 | + assertThat(bits).isEqualTo("10100101"); |
| 200 | + // REMOVE_END |
| 201 | + }).toCompletableFuture(); |
| 202 | + // STEP_END |
| 203 | + oneOp.join(); |
| 204 | + |
| 205 | + // REMOVE_START |
| 206 | + async.del("pings:2024-01-01-00:00", "A", "B", "C", "R").toCompletableFuture().join(); |
| 207 | + // REMOVE_END |
| 208 | + } finally { |
| 209 | + redisClient.shutdown(); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments