global statement removal: hphp/test [7/x]
[hiphop-php.git] / hphp / test / slow / ext_redis / scan.php
blob98a1a25a67d236cfd3894aed4a5302c1d617aeeb
1 <?php
3 include (__DIR__ . '/redis.inc');
5 $r = NewRedisTestInstance();
6 ExtRedisScan::$prefix = GetTestKeyName(__FILE__) . ':';
7 $r->setOption(Redis::OPT_PREFIX, ExtRedisScan::$prefix);
8 $r->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
9 $ret = $r->delete('scan');
10 $ret = $r->mset(array('key:one' => 'one', 'key:two' => 'two',
11 'key:three' => 'three','key:four' => 'four'));
14 * The PHP5 extension we're patterning after doesn't try to turn
15 * OPT_PREFIX into a pattern when doing scan(), so scan returns
16 * global state. Filter it ourselves.
18 function performScan($fn){
19 $cursor = null;
20 $returns = array();
21 while (($retval = $fn($cursor))){
22 foreach ($retval as $key){
24 if (strstr($key, ExtRedisScan::$prefix) !== false){
25 $returns []= substr($key, strlen(ExtRedisScan::$prefix));
29 // Normalize return order
30 sort($returns);
31 return $returns;
34 try {
35 $ret = performScan(function(&$cursor) use($r){
36 // Scan without patterns.
37 return $r->scan($cursor);
38 });
39 var_dump($ret);
41 $ret = performScan(function(&$cursor) use($r){
42 // catch key:two and key:three
44 return $r->scan($cursor, ExtRedisScan::$prefix . 'key:t*');
45 });
46 var_dump($ret);
48 $ret = performScan(function(&$cursor) use($r){
49 // nothing.
51 return $r->scan($cursor, ExtRedisScan::$prefix . 'nokey:t*');
52 });
53 var_dump($ret);
54 } finally {
55 $r->delete('key:one');
56 $r->delete('key:two');
57 $r->delete('key:three');
58 $r->delete('key:four');
61 abstract final class ExtRedisScan {
62 public static $prefix;