Repository navigation
Expand file tree
/
Copy pathTopicManage.php
More file actions
118 lines (89 loc) · 1.79 KB
/
Copy pathTopicManage.php
File metadata and controls
118 lines (89 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Created by PhpStorm.
* User: Sunny
* Date: 2022/7/5
* Time: 2:34 PM
*/
declare( strict_types = 1 );
namespace Pulsar;
use Pulsar\IO\AbstractIO;
/**
* Class TopicManage
*
* @package Pulsar
*/
class TopicManage
{
/**
* @var int
*/
protected $partitions = 0;
/**
* @var array
*/
protected $data = [];
/**
* @var array<string,AbstractIO>
*/
protected $connections = [];
/**
* @var string
*/
protected $base;
/**
* @param string $topic
* @param int $partitions
*/
public function __construct(string $topic, int $partitions)
{
for ($i = 0; $i < $partitions; $i++) {
$this->data[] = sprintf('%s-partition-%d', $topic, $i);
}
$this->base = $topic;
$this->partitions = $partitions;
}
/**
* @return int
*/
public function count(): int
{
return $this->partitions;
}
/**
* @return string
* for producer
*/
public function one(): string
{
if ($this->partitions <= 0) {
return $this->base;
}
return $this->data[ mt_rand(0, $this->partitions - 1) ];
}
/**
* @return array
* for consumer
*/
public function all(): array
{
return $this->partitions <= 0 ? [$this->base] : $this->data;
}
/**
* @param string $topic
* @return AbstractIO
*/
public function getConnection(string $topic): AbstractIO
{
return $this->connections[ $topic ];
}
/**
* @param string $topic
* @param AbstractIO $connection
* @return void
*/
public function setConnection(string $topic, AbstractIO $connection)
{
$this->connections[ $topic ] = $connection;
}
}