-
Notifications
You must be signed in to change notification settings - Fork 276
DOC-5743 bitops examples #2158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,366
−2
Merged
DOC-5743 bitops examples #2158
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2fdd089
DOC-5743 docs for BITOP with Python TCEs
andy-stark-redis 017dff4
DOC-5743 added bit diagrams
andy-stark-redis c103c83
DOC-5743 updated existing bitmap example files with bitop examples
andy-stark-redis 207955f
DOC-5743 Lettuce bitmap examples
andy-stark-redis 7007619
DOC-5743 PHP bitmap examples
andy-stark-redis e67f973
DOC-5743 fixes to hide test imports
andy-stark-redis 1e9f6d9
Update content/develop/data-types/bitmaps.md
andy-stark-redis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| // EXAMPLE: bitmap_tutorial | ||
| package io.redis.examples; | ||
|
|
||
| import redis.clients.jedis.UnifiedJedis; | ||
| import redis.clients.jedis.args.BitOP; | ||
| // REMOVE_START | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| // REMOVE_END | ||
|
|
||
| public class BitMapsExample { | ||
|
|
||
| @Test | ||
| public void run() { | ||
| UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); | ||
|
|
||
| // REMOVE_START | ||
| jedis.del("pings:2024-01-01-00:00"); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START ping | ||
| boolean res1 = jedis.setbit("pings:2024-01-01-00:00", 123, true); | ||
| System.out.println(res1); // >>> false | ||
|
|
||
| boolean res2 = jedis.getbit("pings:2024-01-01-00:00", 123); | ||
| System.out.println(res2); // >>> true | ||
|
|
||
| boolean res3 = jedis.getbit("pings:2024-01-01-00:00", 456); | ||
| System.out.println(res3); // >>> false | ||
| // STEP_END | ||
|
|
||
| // REMOVE_START | ||
| assertFalse(res1); | ||
| assertTrue(res2); | ||
| assertFalse(res3); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitcount | ||
| long res4 = jedis.bitcount("pings:2024-01-01-00:00"); | ||
| System.out.println(res4); // >>> 1 | ||
| // STEP_END | ||
|
|
||
| // REMOVE_START | ||
| assertEquals(1, res4); | ||
| // REMOVE_END | ||
| // REMOVE_START | ||
| jedis.del("A", "B", "C", "R"); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_setup | ||
| jedis.setbit("A", 0, true); | ||
| jedis.setbit("A", 1, true); | ||
| jedis.setbit("A", 3, true); | ||
| jedis.setbit("A", 4, true); | ||
|
|
||
| byte[] res5 = jedis.get("A".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res5[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 11011000 | ||
|
|
||
| jedis.setbit("B", 3, true); | ||
| jedis.setbit("B", 4, true); | ||
| jedis.setbit("B", 7, true); | ||
|
|
||
| byte[] res6 = jedis.get("B".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res6[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 00011001 | ||
|
|
||
| jedis.setbit("C", 1, true); | ||
| jedis.setbit("C", 2, true); | ||
| jedis.setbit("C", 4, true); | ||
| jedis.setbit("C", 5, true); | ||
|
|
||
| byte[] res7 = jedis.get("C".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res7[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 01101100 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b11011000, res5[0] & 0xFF); | ||
| assertEquals(0b00011001, res6[0] & 0xFF); | ||
| assertEquals(0b01101100, res7[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_and | ||
| jedis.bitop(BitOP.AND, "R", "A", "B", "C"); | ||
| byte[] res8 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res8[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 00001000 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b00001000, res8[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_or | ||
| jedis.bitop(BitOP.OR, "R", "A", "B", "C"); | ||
| byte[] res9 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res9[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 11111101 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b11111101, res9[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_xor | ||
| jedis.bitop(BitOP.XOR, "R", "A", "B"); | ||
| byte[] res10 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res10[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 11000001 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b11000001, res10[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_not | ||
| jedis.bitop(BitOP.NOT, "R", "A"); | ||
| byte[] res11 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res11[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 00100111 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b00100111, res11[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_diff | ||
| jedis.bitop(BitOP.DIFF, "R", "A", "B", "C"); | ||
| byte[] res12 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res12[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 10000000 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b10000000, res12[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_diff1 | ||
| jedis.bitop(BitOP.DIFF1, "R", "A", "B", "C"); | ||
| byte[] res13 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res13[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 00100101 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b00100101, res13[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_andor | ||
| jedis.bitop(BitOP.ANDOR, "R", "A", "B", "C"); | ||
| byte[] res14 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res14[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 01011000 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b01011000, res14[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START bitop_one | ||
| jedis.bitop(BitOP.ONE, "R", "A", "B", "C"); | ||
| byte[] res15 = jedis.get("R".getBytes()); | ||
| System.out.println(String.format("%8s", Integer.toBinaryString(res15[0] & 0xFF)).replace(' ', '0')); | ||
| // >>> 10100101 | ||
| // STEP_END | ||
| // REMOVE_START | ||
| assertEquals(0b10100101, res15[0] & 0xFF); | ||
| // REMOVE_END | ||
|
|
||
|
|
||
| // HIDE_START | ||
| jedis.close(); | ||
| } | ||
| } | ||
| // HIDE_END |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.