-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRados.php
205 lines (184 loc) · 4.9 KB
/
Rados.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Aternos\Rados;
use Aternos\Rados\Cluster\Cluster;
use Aternos\Rados\Cluster\ClusterConfig;
use Aternos\Rados\Exception\RadosException;
use Aternos\Rados\Operation\Read\ReadOperation;
use Aternos\Rados\Operation\Write\WriteOperation;
use Aternos\Rados\Util\Buffer\Buffer;
use FFI;
class Rados
{
const DEFAULT_FFI_SCOPE = "PHP_RADOS_FFI";
protected static ?Rados $instance = null;
protected bool $initialized = false;
protected ?FFI $ffi = null;
protected string $headerPath = __DIR__ . "/../includes/librados.h";
/**
* @return Rados
*/
public static function getInstance(): Rados
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
/**
* @internal Use Rados::getInstance() to get an instance
*/
public function __construct()
{
}
/**
* @return string
*/
public function getHeaderPath(): string
{
return $this->headerPath;
}
/**
* @param string $headerPath
* @return $this
*/
public function setHeaderPath(string $headerPath): Rados
{
$this->headerPath = $headerPath;
return $this;
}
/**
* @return string
*/
protected function readHeaders(): string
{
return file_get_contents($this->headerPath);
}
/**
* Initialize Rados
*
* @return Rados
*/
public function initialize(): static
{
if ($this->initialized) {
return $this;
}
$this->ffi = FFI::cdef($this->readHeaders(), "librados.so.2");
$this->initialized = true;
return $this;
}
/**
* @return $this
*/
public function preload(): static
{
$this->ffi = FFI::load($this->headerPath);
$this->initialized = true;
return $this;
}
/**
* Initiate Rados from preloaded headers
* This requires FFI preloading to be enabled and $rados->preload()
* to be called in the opcache.preload file
*
* See https://www.php.net/manual/en/ffi.examples-complete.php#ffi.examples-complete for details
*
* @return $this
*/
public function initializePreloaded(): static
{
if ($this->initialized) {
return $this;
}
$this->ffi = FFI::scope(static::DEFAULT_FFI_SCOPE);
$this->initialized = true;
return $this;
}
/**
* Get a new Rados cluster object
*
* Ceph environment variables are read when this is called, so if
* $CEPH_ARGS specifies everything you need to connect, no further
* configuration is necessary.
*
* @param string|null $userId - the user to connect as (i.e. admin, not client.admin)
* @return Cluster
* @throws RadosException
*/
public function createCluster(?string $userId = null): Cluster
{
if (!$this->initialized) {
throw new RadosException("Rados is not initialized");
}
return Cluster::create($this->ffi, $userId);
}
/**
* Extended version of createCluster
*
* Like createCluster, but
* 1) don't assume 'client\.'+id; allow full specification of name
* 2) allow specification of cluster name
* 3) flags for future expansion
*
* @param string|null $clusterName
* @param string|null $userId
* @param int $flags
* @return Cluster
* @throws RadosException
*/
public function createClusterExtended(?string $clusterName, ?string $userId, int $flags = 0): Cluster
{
if (!$this->initialized) {
throw new RadosException("Rados is not initialized");
}
return Cluster::create2($this->ffi, $clusterName, $userId, $flags);
}
/**
* Create a Cluster object using existing configuration
*
* @param ClusterConfig $config
* @return Cluster
* @throws RadosException
*/
public function createClusterWithContext(ClusterConfig $config): Cluster
{
if (!$this->initialized) {
throw new RadosException("Rados is not initialized");
}
return Cluster::createWithContext($this->ffi, $config);
}
/**
* Create a new buffer
*
* @param int $size
* @return Buffer
*/
public function createBuffer(int $size): Buffer
{
return Buffer::create($this->ffi, $size);
}
/**
* Create a new read operation
*
* @return ReadOperation
*/
public function createReadOperation(): ReadOperation
{
return ReadOperation::create($this->ffi);
}
/**
* @return WriteOperation
*/
public function createWriteOperation(): WriteOperation
{
return WriteOperation::create($this->ffi);
}
/**
* @return ?FFI
* @internal The FFI context should not be used directly
*/
public function getFFI(): ?FFI
{
return $this->ffi;
}
}