|
| 1 | +package redis.clients.jedis.commands.unified.pipeline; |
| 2 | + |
| 3 | +import static redis.clients.jedis.util.AssertUtil.assertPipelineSyncAll; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import redis.clients.jedis.commands.unified.pooled.PooledCommandsTestHelper; |
| 12 | + |
| 13 | +public class HashesPipelineCommandsTest extends PipelineCommandsTestBase { |
| 14 | + |
| 15 | + @BeforeClass |
| 16 | + public static void prepare() throws InterruptedException { |
| 17 | + jedis = PooledCommandsTestHelper.getPooled(); |
| 18 | + } |
| 19 | + |
| 20 | + @AfterClass |
| 21 | + public static void cleanUp() { |
| 22 | + jedis.close(); |
| 23 | + } |
| 24 | +// |
| 25 | +// @Before |
| 26 | +// public void setUp() { |
| 27 | +// PooledCommandsTestHelper.clearData(); |
| 28 | +// } |
| 29 | +// |
| 30 | +// @After |
| 31 | +// public void tearDown() { |
| 32 | +// PooledCommandsTestHelper.clearData(); |
| 33 | +// } |
| 34 | + |
| 35 | + final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 }; |
| 36 | + final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 }; |
| 37 | + final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C }; |
| 38 | + |
| 39 | + final byte[] bbar1 = { 0x05, 0x06, 0x07, 0x08, 0x0A }; |
| 40 | + final byte[] bbar2 = { 0x05, 0x06, 0x07, 0x08, 0x0B }; |
| 41 | + final byte[] bbar3 = { 0x05, 0x06, 0x07, 0x08, 0x0C }; |
| 42 | + final byte[] bbarstar = { 0x05, 0x06, 0x07, 0x08, '*' }; |
| 43 | + |
| 44 | + @Test |
| 45 | + public void hset() { |
| 46 | + pipe.hset("foo", "bar", "car"); |
| 47 | + pipe.hset("foo", "bar", "foo"); |
| 48 | + |
| 49 | + // Binary |
| 50 | + pipe.hset(bfoo, bbar, bcar); |
| 51 | + pipe.hset(bfoo, bbar, bfoo); |
| 52 | + |
| 53 | + assertPipelineSyncAll( |
| 54 | + Arrays.<Object>asList(1L, 0L, 1L, 0L), |
| 55 | + pipe.syncAndReturnAll()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void hget() { |
| 60 | + pipe.hset("foo", "bar", "car"); |
| 61 | + pipe.hget("bar", "foo"); |
| 62 | + pipe.hget("foo", "car"); |
| 63 | + pipe.hget("foo", "bar"); |
| 64 | + |
| 65 | + // Binary |
| 66 | + pipe.hset(bfoo, bbar, bcar); |
| 67 | + pipe.hget(bbar, bfoo); |
| 68 | + pipe.hget(bfoo, bcar); |
| 69 | + pipe.hget(bfoo, bbar); |
| 70 | + |
| 71 | + assertPipelineSyncAll( |
| 72 | + Arrays.<Object>asList( |
| 73 | + 1L, null, null, "car", |
| 74 | + 1L, null, null, bcar), |
| 75 | + pipe.syncAndReturnAll()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void hsetnx() { |
| 80 | + pipe.hsetnx("foo", "bar", "car"); |
| 81 | + pipe.hget("foo", "bar"); |
| 82 | + |
| 83 | + pipe.hsetnx("foo", "bar", "foo"); |
| 84 | + pipe.hget("foo", "bar"); |
| 85 | + |
| 86 | + pipe.hsetnx("foo", "car", "bar"); |
| 87 | + pipe.hget("foo", "car"); |
| 88 | + |
| 89 | + // Binary |
| 90 | + pipe.hsetnx(bfoo, bbar, bcar); |
| 91 | + pipe.hget(bfoo, bbar); |
| 92 | + |
| 93 | + pipe.hsetnx(bfoo, bbar, bfoo); |
| 94 | + pipe.hget(bfoo, bbar); |
| 95 | + |
| 96 | + pipe.hsetnx(bfoo, bcar, bbar); |
| 97 | + pipe.hget(bfoo, bcar); |
| 98 | + |
| 99 | + assertPipelineSyncAll( |
| 100 | + Arrays.<Object>asList( |
| 101 | + 1L, "car", 0L, "car", 1L, "bar", |
| 102 | + 1L, bcar, 0L, bcar, 1L, bbar), |
| 103 | + pipe.syncAndReturnAll()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void hmset() { |
| 108 | + Map<String, String> hash = new HashMap<>(); |
| 109 | + hash.put("bar", "car"); |
| 110 | + hash.put("car", "bar"); |
| 111 | + pipe.hmset("foo", hash); |
| 112 | + pipe.hget("foo", "bar"); |
| 113 | + pipe.hget("foo", "car"); |
| 114 | + |
| 115 | + // Binary |
| 116 | + Map<byte[], byte[]> bhash = new HashMap<>(); |
| 117 | + bhash.put(bbar, bcar); |
| 118 | + bhash.put(bcar, bbar); |
| 119 | + pipe.hmset(bfoo, bhash); |
| 120 | + pipe.hget(bfoo, bbar); |
| 121 | + pipe.hget(bfoo, bcar); |
| 122 | + |
| 123 | + assertPipelineSyncAll( |
| 124 | + Arrays.<Object>asList("OK", "car", "bar", "OK", bcar, bbar), |
| 125 | + pipe.syncAndReturnAll()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void hsetVariadic() { |
| 130 | + Map<String, String> hash = new HashMap<>(); |
| 131 | + hash.put("bar", "car"); |
| 132 | + hash.put("car", "bar"); |
| 133 | + pipe.hset("foo", hash); |
| 134 | + pipe.hget("foo", "bar"); |
| 135 | + pipe.hget("foo", "car"); |
| 136 | + |
| 137 | + // Binary |
| 138 | + Map<byte[], byte[]> bhash = new HashMap<>(); |
| 139 | + bhash.put(bbar, bcar); |
| 140 | + bhash.put(bcar, bbar); |
| 141 | + pipe.hset(bfoo, bhash); |
| 142 | + pipe.hget(bfoo, bbar); |
| 143 | + pipe.hget(bfoo, bcar); |
| 144 | + |
| 145 | + assertPipelineSyncAll( |
| 146 | + Arrays.<Object>asList(2L, "car", "bar", 2L, bcar, bbar), |
| 147 | + pipe.syncAndReturnAll()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void hmget() { |
| 152 | + Map<String, String> hash = new HashMap<>(); |
| 153 | + hash.put("bar", "car"); |
| 154 | + hash.put("car", "bar"); |
| 155 | + pipe.hmset("foo", hash); |
| 156 | + |
| 157 | + pipe.hmget("foo", "bar", "car", "foo"); |
| 158 | + List<String> expected = new ArrayList<>(); |
| 159 | + expected.add("car"); |
| 160 | + expected.add("bar"); |
| 161 | + expected.add(null); |
| 162 | + |
| 163 | + // Binary |
| 164 | + Map<byte[], byte[]> bhash = new HashMap<>(); |
| 165 | + bhash.put(bbar, bcar); |
| 166 | + bhash.put(bcar, bbar); |
| 167 | + pipe.hmset(bfoo, bhash); |
| 168 | + |
| 169 | + pipe.hmget(bfoo, bbar, bcar, bfoo); |
| 170 | + List<byte[]> bexpected = new ArrayList<>(); |
| 171 | + bexpected.add(bcar); |
| 172 | + bexpected.add(bbar); |
| 173 | + bexpected.add(null); |
| 174 | + |
| 175 | + assertPipelineSyncAll( |
| 176 | + Arrays.<Object>asList( |
| 177 | + "OK", Arrays.asList("car", "bar", null), |
| 178 | + "OK", Arrays.asList(bcar, bbar, null)), |
| 179 | + pipe.syncAndReturnAll()); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void hincrBy() { |
| 184 | + pipe.hincrBy("foo", "bar", 1); |
| 185 | + pipe.hincrBy("foo", "bar", -1); |
| 186 | + pipe.hincrBy("foo", "bar", -10); |
| 187 | + |
| 188 | + // Binary |
| 189 | + pipe.hincrBy(bfoo, bbar, 1); |
| 190 | + pipe.hincrBy(bfoo, bbar, -1); |
| 191 | + pipe.hincrBy(bfoo, bbar, -10); |
| 192 | + |
| 193 | + assertPipelineSyncAll( |
| 194 | + Arrays.<Object>asList(1L, 0L, -10L, 1L, 0L, -10L), |
| 195 | + pipe.syncAndReturnAll()); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + public void hincrByFloat() { |
| 200 | + pipe.hincrByFloat("foo", "bar", 1.5d); |
| 201 | + pipe.hincrByFloat("foo", "bar", -1.5d); |
| 202 | + pipe.hincrByFloat("foo", "bar", -10.7d); |
| 203 | + |
| 204 | + // Binary |
| 205 | + pipe.hincrByFloat(bfoo, bbar, 1.5d); |
| 206 | + pipe.hincrByFloat(bfoo, bbar, -1.5d); |
| 207 | + pipe.hincrByFloat(bfoo, bbar, -10.7d); |
| 208 | + |
| 209 | + assertPipelineSyncAll( |
| 210 | + Arrays.<Object>asList(1.5, 0d, -10.7, 1.5, 0d, -10.7), |
| 211 | + pipe.syncAndReturnAll()); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void hexists() { |
| 216 | + Map<String, String> hash = new HashMap<>(); |
| 217 | + hash.put("bar", "car"); |
| 218 | + hash.put("car", "bar"); |
| 219 | + pipe.hset("foo", hash); |
| 220 | + |
| 221 | + pipe.hexists("bar", "foo"); |
| 222 | + pipe.hexists("foo", "foo"); |
| 223 | + pipe.hexists("foo", "bar"); |
| 224 | + |
| 225 | + // Binary |
| 226 | + Map<byte[], byte[]> bhash = new HashMap<>(); |
| 227 | + bhash.put(bbar, bcar); |
| 228 | + bhash.put(bcar, bbar); |
| 229 | + pipe.hset(bfoo, bhash); |
| 230 | + |
| 231 | + pipe.hexists(bbar, bfoo); |
| 232 | + pipe.hexists(bfoo, bfoo); |
| 233 | + pipe.hexists(bfoo, bbar); |
| 234 | + |
| 235 | + assertPipelineSyncAll( |
| 236 | + Arrays.<Object>asList( |
| 237 | + 2L, false, false, true, |
| 238 | + 2L, false, false, true), |
| 239 | + pipe.syncAndReturnAll()); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + public void hdel() { |
| 244 | + Map<String, String> hash = new HashMap<>(); |
| 245 | + hash.put("bar", "car"); |
| 246 | + hash.put("car", "bar"); |
| 247 | + pipe.hset("foo", hash); |
| 248 | + |
| 249 | + pipe.hdel("bar", "foo"); |
| 250 | + pipe.hdel("foo", "foo"); |
| 251 | + pipe.hdel("foo", "bar"); |
| 252 | + pipe.hget("foo", "bar"); |
| 253 | + |
| 254 | + // Binary |
| 255 | + Map<byte[], byte[]> bhash = new HashMap<>(); |
| 256 | + bhash.put(bbar, bcar); |
| 257 | + bhash.put(bcar, bbar); |
| 258 | + pipe.hset(bfoo, bhash); |
| 259 | + |
| 260 | + pipe.hdel(bbar, bfoo); |
| 261 | + pipe.hdel(bfoo, bfoo); |
| 262 | + pipe.hdel(bfoo, bbar); |
| 263 | + pipe.hget(bfoo, bbar); |
| 264 | + |
| 265 | + assertPipelineSyncAll( |
| 266 | + Arrays.<Object>asList( |
| 267 | + 2L, 0L, 0L, 1L, null, |
| 268 | + 2L, 0L, 0L, 1L, null), |
| 269 | + pipe.syncAndReturnAll()); |
| 270 | + } |
| 271 | + |
| 272 | + @Test |
| 273 | + public void hlen() { |
| 274 | + Map<String, String> hash = new HashMap<>(); |
| 275 | + hash.put("bar", "car"); |
| 276 | + hash.put("car", "bar"); |
| 277 | + pipe.hset("foo", hash); |
| 278 | + |
| 279 | + pipe.hlen("bar"); |
| 280 | + pipe.hlen("foo"); |
| 281 | + |
| 282 | + // Binary |
| 283 | + Map<byte[], byte[]> bhash = new HashMap<>(); |
| 284 | + bhash.put(bbar, bcar); |
| 285 | + bhash.put(bcar, bbar); |
| 286 | + pipe.hset(bfoo, bhash); |
| 287 | + |
| 288 | + pipe.hlen(bbar); |
| 289 | + pipe.hlen(bfoo); |
| 290 | + |
| 291 | + assertPipelineSyncAll( |
| 292 | + Arrays.<Object>asList(2L, 0L, 2L, 2L, 0L, 2L), |
| 293 | + pipe.syncAndReturnAll()); |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void hkeys() { |
| 298 | + Map<String, String> hash = new LinkedHashMap<>(); |
| 299 | + hash.put("bar", "car"); |
| 300 | + hash.put("car", "bar"); |
| 301 | + pipe.hset("foo", hash); |
| 302 | + |
| 303 | + pipe.hkeys("foo"); |
| 304 | + Set<String> expected = new LinkedHashSet<>(); |
| 305 | + expected.add("bar"); |
| 306 | + expected.add("car"); |
| 307 | + |
| 308 | + // Binary |
| 309 | + Map<byte[], byte[]> bhash = new LinkedHashMap<>(); |
| 310 | + bhash.put(bbar, bcar); |
| 311 | + bhash.put(bcar, bbar); |
| 312 | + pipe.hset(bfoo, bhash); |
| 313 | + |
| 314 | + pipe.hkeys(bfoo); |
| 315 | + Set<byte[]> bexpected = new LinkedHashSet<>(); |
| 316 | + bexpected.add(bbar); |
| 317 | + bexpected.add(bcar); |
| 318 | + |
| 319 | + assertPipelineSyncAll( |
| 320 | + Arrays.<Object>asList( |
| 321 | + 2L, new HashSet<>(Arrays.asList("bar", "car")), |
| 322 | + 2L, new HashSet<>(Arrays.asList(bbar, bcar))), |
| 323 | + pipe.syncAndReturnAll()); |
| 324 | + } |
| 325 | + |
| 326 | + @Test |
| 327 | + public void hvals() { |
| 328 | + Map<String, String> hash = new LinkedHashMap<>(); |
| 329 | + hash.put("bar", "car"); |
| 330 | + //hash.put("car", "bar"); |
| 331 | + pipe.hset("foo", hash); |
| 332 | + |
| 333 | + pipe.hvals("foo"); |
| 334 | + |
| 335 | + // Binary |
| 336 | + Map<byte[], byte[]> bhash = new LinkedHashMap<>(); |
| 337 | + bhash.put(bbar, bcar); |
| 338 | + //bhash.put(bcar, bbar); |
| 339 | + pipe.hset(bfoo, bhash); |
| 340 | + |
| 341 | + pipe.hvals(bfoo); |
| 342 | + |
| 343 | + assertPipelineSyncAll( |
| 344 | + Arrays.<Object>asList( |
| 345 | + //2L, Arrays.asList("bar", "car"), |
| 346 | + //2L, Arrays.asList(bbar, bcar)), |
| 347 | + 1L, Arrays.asList("car"), |
| 348 | + 1L, Arrays.asList(bcar)), |
| 349 | + pipe.syncAndReturnAll()); |
| 350 | + } |
| 351 | + |
| 352 | + @Test |
| 353 | + public void hgetAll() { |
| 354 | + Map<String, String> hash = new HashMap<>(); |
| 355 | + hash.put("bar", "car"); |
| 356 | + //hash.put("car", "bar"); |
| 357 | + pipe.hset("foo", hash); |
| 358 | + |
| 359 | + pipe.hgetAll("foo"); |
| 360 | + |
| 361 | + // Binary |
| 362 | + Map<byte[], byte[]> bhash = new HashMap<>(); |
| 363 | + bhash.put(bbar, bcar); |
| 364 | + //bhash.put(bcar, bbar); |
| 365 | + pipe.hset(bfoo, bhash); |
| 366 | + |
| 367 | + pipe.hgetAll(bfoo); |
| 368 | + |
| 369 | +// assertPipelineSyncAll( |
| 370 | +// Arrays.<Object>asList( |
| 371 | +// 1L, hash, |
| 372 | +// 1L, bhash), |
| 373 | +// pipe.syncAndReturnAll()); |
| 374 | + pipe.syncAndReturnAll(); |
| 375 | + } |
| 376 | + |
| 377 | + @Test |
| 378 | + public void hstrlen() { |
| 379 | + pipe.hstrlen("foo", "key"); |
| 380 | + pipe.hset("foo", "key", "value"); |
| 381 | + pipe.hstrlen("foo", "key"); |
| 382 | + |
| 383 | + pipe.hstrlen(bfoo, bbar); |
| 384 | + pipe.hset(bfoo, bbar, bcar); |
| 385 | + pipe.hstrlen(bfoo, bbar); |
| 386 | + |
| 387 | + assertPipelineSyncAll( |
| 388 | + Arrays.<Object>asList(0L, 1L, 5L, 0L, 1L, 4L), |
| 389 | + pipe.syncAndReturnAll()); |
| 390 | + } |
| 391 | +} |
0 commit comments