Skip to content

Commit 7007619

Browse files
DOC-5743 PHP bitmap examples
1 parent 207955f commit 7007619

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// EXAMPLE: bitmap_tutorial
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use Predis\Client as PredisClient;
7+
8+
class DtBitmapTest
9+
// REMOVE_START
10+
extends PredisTestCase
11+
// REMOVE_END
12+
{
13+
public function testDtBitmap() {
14+
$r = new PredisClient([
15+
'scheme' => 'tcp',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'password' => '',
19+
'database' => 0,
20+
]);
21+
// REMOVE_START
22+
$r->flushall();
23+
// REMOVE_END
24+
25+
// STEP_START ping
26+
$res1 = $r->setbit('pings:2024-01-01-00:00', 123, 1);
27+
echo $res1 . PHP_EOL;
28+
// >>> 0
29+
30+
$res2 = $r->getbit('pings:2024-01-01-00:00', 123);
31+
echo $res2 . PHP_EOL;
32+
// >>> 1
33+
34+
$res3 = $r->getbit('pings:2024-01-01-00:00', 456);
35+
echo $res3 . PHP_EOL;
36+
// >>> 0
37+
// STEP_END
38+
// REMOVE_START
39+
$this->assertEquals(0, $res1);
40+
// REMOVE_END
41+
42+
// STEP_START bitcount
43+
// Ensure the bit is set
44+
$r->setbit('pings:2024-01-01-00:00', 123, 1);
45+
$res4 = $r->bitcount('pings:2024-01-01-00:00');
46+
echo $res4 . PHP_EOL;
47+
// >>> 1
48+
// STEP_END
49+
// REMOVE_START
50+
$this->assertEquals(1, $res4);
51+
// REMOVE_END
52+
53+
// STEP_START bitop_setup
54+
$r->setbit('A', 0, 1);
55+
$r->setbit('A', 1, 1);
56+
$r->setbit('A', 3, 1);
57+
$r->setbit('A', 4, 1);
58+
59+
$res5 = $r->get('A');
60+
echo str_pad(decbin(ord($res5)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
61+
// >>> 11011000
62+
63+
$r->setbit('B', 3, 1);
64+
$r->setbit('B', 4, 1);
65+
$r->setbit('B', 7, 1);
66+
67+
$res6 = $r->get('B');
68+
echo str_pad(decbin(ord($res6)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
69+
// >>> 00011001
70+
71+
$r->setbit('C', 1, 1);
72+
$r->setbit('C', 2, 1);
73+
$r->setbit('C', 4, 1);
74+
$r->setbit('C', 5, 1);
75+
76+
$res7 = $r->get('C');
77+
echo str_pad(decbin(ord($res7)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
78+
// >>> 01101100
79+
// STEP_END
80+
// REMOVE_START
81+
$this->assertEquals(0b11011000, ord($res5));
82+
$this->assertEquals(0b00011001, ord($res6));
83+
$this->assertEquals(0b01101100, ord($res7));
84+
// REMOVE_END
85+
86+
// STEP_START bitop_and
87+
$r->bitop('AND', 'R', 'A', 'B', 'C');
88+
$res8 = $r->get('R');
89+
echo str_pad(decbin(ord($res8)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
90+
// >>> 00001000
91+
// STEP_END
92+
// REMOVE_START
93+
$this->assertEquals(0b00001000, ord($res8));
94+
// REMOVE_END
95+
96+
// STEP_START bitop_or
97+
$r->bitop('OR', 'R', 'A', 'B', 'C');
98+
$res9 = $r->get('R');
99+
echo str_pad(decbin(ord($res9)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
100+
// >>> 11111101
101+
// STEP_END
102+
// REMOVE_START
103+
$this->assertEquals(0b11111101, ord($res9));
104+
// REMOVE_END
105+
106+
// STEP_START bitop_xor
107+
$r->bitop('XOR', 'R', 'A', 'B');
108+
$res10 = $r->get('R');
109+
echo str_pad(decbin(ord($res10)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
110+
// >>> 11000001
111+
// STEP_END
112+
// REMOVE_START
113+
$this->assertEquals(0b11000001, ord($res10));
114+
// REMOVE_END
115+
116+
// STEP_START bitop_not
117+
$r->bitop('NOT', 'R', 'A');
118+
$res11 = $r->get('R');
119+
echo str_pad(decbin(ord($res11)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
120+
// >>> 00100111
121+
// STEP_END
122+
// REMOVE_START
123+
$this->assertEquals(0b00100111, ord($res11));
124+
// REMOVE_END
125+
126+
// STEP_START bitop_diff
127+
$r->bitop('DIFF', 'R', 'A', 'B', 'C');
128+
$res12 = $r->get('R');
129+
echo str_pad(decbin(ord($res12)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
130+
// >>> 10000000
131+
// STEP_END
132+
// REMOVE_START
133+
$this->assertEquals(0b10000000, ord($res12));
134+
// REMOVE_END
135+
136+
// STEP_START bitop_diff1
137+
$r->bitop('DIFF1', 'R', 'A', 'B', 'C');
138+
$res13 = $r->get('R');
139+
echo str_pad(decbin(ord($res13)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
140+
// >>> 00100101
141+
// STEP_END
142+
// REMOVE_START
143+
$this->assertEquals(0b00100101, ord($res13));
144+
// REMOVE_END
145+
146+
// STEP_START bitop_andor
147+
$r->bitop('ANDOR', 'R', 'A', 'B', 'C');
148+
$res14 = $r->get('R');
149+
echo str_pad(decbin(ord($res14)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
150+
// >>> 01011000
151+
// STEP_END
152+
// REMOVE_START
153+
$this->assertEquals(0b01011000, ord($res14));
154+
// REMOVE_END
155+
156+
// STEP_START bitop_one
157+
$r->bitop('ONE', 'R', 'A', 'B', 'C');
158+
$res15 = $r->get('R');
159+
echo str_pad(decbin(ord($res15)), 8, '0', STR_PAD_LEFT) . PHP_EOL;
160+
// >>> 10100101
161+
// STEP_END
162+
// REMOVE_START
163+
$this->assertEquals(0b10100101, ord($res15));
164+
// REMOVE_END
165+
}
166+
}
167+

0 commit comments

Comments
 (0)