|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Redis\DataManager; |
|
4
|
|
|
|
|
5
|
|
|
use RedisProxy\RedisProxy; |
|
6
|
|
|
use UniMan\Core\Utils\Filter; |
|
7
|
|
|
|
|
8
|
|
|
class RedisHashDataManager |
|
9
|
|
|
{ |
|
10
|
|
|
private $connection; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(RedisProxy $connection) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->connection = $connection; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function itemsCount(string $table, array $filter): int |
|
18
|
|
|
{ |
|
19
|
|
|
if (!$filter) { |
|
|
|
|
|
|
20
|
|
|
return $this->connection->hlen($table); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$totalItems = 0; |
|
24
|
|
|
foreach ($filter as $filterParts) { |
|
25
|
|
|
if (isset($filterParts['key'][Filter::OPERATOR_EQUAL])) { |
|
26
|
|
|
$res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]); |
|
27
|
|
|
if ($res) { |
|
|
|
|
|
|
28
|
|
|
$item = [ |
|
29
|
|
|
'key' => $filterParts['key'][Filter::OPERATOR_EQUAL], |
|
30
|
|
|
'length' => strlen($res), |
|
31
|
|
|
'value' => $res, |
|
32
|
|
|
]; |
|
33
|
|
|
if (Filter::apply($item, $filter)) { |
|
34
|
|
|
$totalItems++; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
return $totalItems; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
$iterator = ''; |
|
41
|
|
|
do { |
|
42
|
|
|
$pattern = null; |
|
43
|
|
|
$res = $this->connection->hscan($table, $iterator, $pattern, 1000); |
|
44
|
|
|
$res = $res ?: []; |
|
45
|
|
View Code Duplication |
foreach ($res as $key => $value) { |
|
|
|
|
|
|
46
|
|
|
$item = [ |
|
47
|
|
|
'key' => $key, |
|
48
|
|
|
'length' => strlen($value), |
|
49
|
|
|
'value' => $value, |
|
50
|
|
|
]; |
|
51
|
|
|
if (Filter::apply($item, $filter)) { |
|
52
|
|
|
$totalItems++; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} while ($iterator !== 0); |
|
56
|
|
|
return $totalItems; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function items(string $table, int $page, int $onPage, array $filter = []): array |
|
60
|
|
|
{ |
|
61
|
|
|
$items = []; |
|
62
|
|
|
foreach ($filter as $filterParts) { |
|
63
|
|
|
if (!isset($filterParts['key'][Filter::OPERATOR_EQUAL])) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
$res = $this->connection->hget($table, $filterParts['key'][Filter::OPERATOR_EQUAL]); |
|
67
|
|
|
if (!$res) { |
|
|
|
|
|
|
68
|
|
|
return $items; |
|
69
|
|
|
} |
|
70
|
|
|
$item = [ |
|
71
|
|
|
'key' => $filterParts['key'][Filter::OPERATOR_EQUAL], |
|
72
|
|
|
'length' => strlen($res), |
|
73
|
|
|
'value' => $res, |
|
74
|
|
|
]; |
|
75
|
|
|
if (Filter::apply($item, $filter)) { |
|
76
|
|
|
$items[$item['key']] = $item; |
|
77
|
|
|
} |
|
78
|
|
|
return $items; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$skipped = 0; |
|
82
|
|
|
$offset = ($page - 1) * $onPage; |
|
83
|
|
|
$iterator = ''; |
|
84
|
|
View Code Duplication |
do { |
|
|
|
|
|
|
85
|
|
|
$pattern = null; |
|
86
|
|
|
$res = $this->connection->hscan($table, $iterator, $pattern, $onPage * 10); |
|
87
|
|
|
if (!$res) { |
|
88
|
|
|
return $items; |
|
89
|
|
|
} |
|
90
|
|
|
foreach ($res as $key => $value) { |
|
|
|
|
|
|
91
|
|
|
$item = [ |
|
92
|
|
|
'key' => $key, |
|
93
|
|
|
'length' => strlen($value), |
|
94
|
|
|
'value' => $value, |
|
95
|
|
|
]; |
|
96
|
|
|
if (!Filter::apply($item, $filter)) { |
|
97
|
|
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
if ($skipped < $offset) { |
|
100
|
|
|
$skipped++; |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$items[$key] = $item; |
|
105
|
|
|
if (count($items) === $onPage) { |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} while ($iterator !== 0 && count($items) < $onPage); |
|
110
|
|
|
return $items; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.