-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathParallelDriver.php
More file actions
296 lines (267 loc) · 7.67 KB
/
ParallelDriver.php
File metadata and controls
296 lines (267 loc) · 7.67 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
namespace Amp\File;
use Amp\Coroutine;
use Amp\Parallel\Worker;
use Amp\Parallel\Worker\Pool;
use Amp\Parallel\Worker\TaskException;
use Amp\Parallel\Worker\WorkerException;
use Amp\Promise;
use Amp\Success;
use function Amp\call;
final class ParallelDriver implements Driver
{
/**
* @var \Amp\Parallel\Worker\Pool
*/
private $pool;
/**
* @param \Amp\Parallel\Worker\Pool|null $pool
*/
public function __construct(Pool $pool = null)
{
$this->pool = $pool ?: Worker\pool();
}
/**
* {@inheritdoc}
*/
public function open(string $path, string $mode): Promise
{
return call(function () use ($path, $mode) {
$worker = $this->pool->getWorker();
try {
[$id, $size, $mode] = yield $worker->enqueue(new Internal\FileTask("fopen", [$path, $mode]));
} catch (TaskException $exception) {
throw new FilesystemException("Could not open file", $exception);
} catch (WorkerException $exception) {
throw new FilesystemException("Could not send open request to worker", $exception);
}
return new ParallelFile($worker, $id, $path, $size, $mode);
});
}
private function runFileTask(Internal\FileTask $task): \Generator
{
try {
return yield $this->pool->enqueue($task);
} catch (TaskException $exception) {
throw new FilesystemException("The file operation failed", $exception);
} catch (WorkerException $exception) {
throw new FilesystemException("Could not send the file task to worker", $exception);
}
}
/**
* {@inheritdoc}
*/
public function unlink(string $path): Promise
{
return call(function () use ($path) {
$result = yield from $this->runFileTask(new Internal\FileTask("unlink", [$path]));
StatCache::clear($path);
return $result;
});
}
/**
* {@inheritdoc}
*/
public function stat(string $path): Promise
{
if ($stat = StatCache::get($path)) {
return new Success($stat);
}
return call(function () use ($path) {
$stat = yield from $this->runFileTask(new Internal\FileTask("stat", [$path]));
if (!empty($stat)) {
StatCache::set($path, $stat);
}
return $stat;
});
}
/**
* {@inheritdoc}
*/
public function rename(string $from, string $to): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("rename", [$from, $to])));
}
/**
* {@inheritdoc}
*/
public function isfile(string $path): Promise
{
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
return false;
}
if ($stat["mode"] & 0100000) {
return true;
}
return false;
});
}
/**
* {@inheritdoc}
*/
public function isdir(string $path): Promise
{
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
return false;
}
if ($stat["mode"] & 0040000) {
return true;
}
return false;
});
}
/**
* {@inheritdoc}
*/
public function link(string $target, string $link): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("link", [$target, $link])));
}
/**
* {@inheritdoc}
*/
public function symlink(string $target, string $link): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("symlink", [$target, $link])));
}
/**
* {@inheritdoc}
*/
public function readlink(string $path): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("readlink", [$path])));
}
/**
* {@inheritdoc}
*/
public function mkdir(string $path, int $mode = 0777, bool $recursive = false): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("mkdir", [$path, $mode, $recursive])));
}
/**
* {@inheritdoc}
*/
public function scandir(string $path): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("scandir", [$path])));
}
/**
* {@inheritdoc}
*/
public function rmdir(string $path): Promise
{
return call(function () use ($path) {
$result = yield from $this->runFileTask(new Internal\FileTask("rmdir", [$path]));
StatCache::clear($path);
return $result;
});
}
/**
* {@inheritdoc}
*/
public function chmod(string $path, int $mode): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("chmod", [$path, $mode])));
}
/**
* {@inheritdoc}
*/
public function chown(string $path, int $uid, int $gid): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("chown", [$path, $uid, $gid])));
}
/**
* {@inheritdoc}
*/
public function exists(string $path): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("exists", [$path])));
}
/**
* {@inheritdoc}
*/
public function size(string $path): Promise
{
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new PathDoesNotExistException($path);
}
if ($stat["mode"] & 0100000) {
return $stat["size"];
}
throw new FilesystemException(\sprintf('Specified path "%s" is not a regular file', $path));
});
}
/**
* {@inheritdoc}
*/
public function mtime(string $path): Promise
{
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new PathDoesNotExistException($path);
}
return $stat["mtime"];
});
}
/**
* {@inheritdoc}
*/
public function atime(string $path): Promise
{
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new PathDoesNotExistException($path);
}
return $stat["atime"];
});
}
/**
* {@inheritdoc}
*/
public function ctime(string $path): Promise
{
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new PathDoesNotExistException($path);
}
return $stat["ctime"];
});
}
/**
* {@inheritdoc}
*/
public function lstat(string $path): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("lstat", [$path])));
}
/**
* {@inheritdoc}
*/
public function touch(string $path, int $time = null, int $atime = null): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("touch", [$path, $time, $atime])));
}
/**
* {@inheritdoc}
*/
public function get(string $path): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("get", [$path])));
}
/**
* {@inheritdoc}
*/
public function put(string $path, string $contents): Promise
{
return new Coroutine($this->runFileTask(new Internal\FileTask("put", [$path, $contents])));
}
}