Skip to content

Commit 169a1d1

Browse files
committed
Use StatCache in ParallelDriver
1 parent 28a62c8 commit 169a1d1

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

lib/Internal/FileTask.php

-6
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,12 @@ public function run(Environment $environment) {
131131
case "readlink":
132132
case "lstat":
133133
case "exists":
134-
case "isfile":
135-
case "isdir":
136134
case "mkdir":
137135
case "scandir":
138136
case "rmdir":
139137
case "chmod":
140138
case "chown":
141139
case "touch":
142-
case "size":
143-
case "mtime":
144-
case "atime":
145-
case "ctime":
146140
case "get":
147141
case "put":
148142
return ([new BlockingDriver, $this->operation])(...$this->args);

lib/ParallelDriver.php

+74-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Amp\Parallel\Worker\TaskException;
1010
use Amp\Parallel\Worker\WorkerException;
1111
use Amp\Promise;
12+
use Amp\Success;
13+
use function Amp\call;
1214

1315
class ParallelDriver implements Driver {
1416
/**
@@ -67,14 +69,28 @@ private function runFileTask(Internal\FileTask $task): \Generator {
6769
* {@inheritdoc}
6870
*/
6971
public function unlink(string $path): Promise {
70-
return new Coroutine($this->runFileTask(new Internal\FileTask("unlink", [$path])));
72+
return call(function () use ($path) {
73+
$result = yield from $this->runFileTask(new Internal\FileTask("unlink", [$path]));
74+
StatCache::clear($path);
75+
return $result;
76+
});
7177
}
7278

7379
/**
7480
* {@inheritdoc}
7581
*/
7682
public function stat(string $path): Promise {
77-
return new Coroutine($this->runFileTask(new Internal\FileTask("stat", [$path])));
83+
if ($stat = StatCache::get($path)) {
84+
return new Success($stat);
85+
}
86+
87+
return call(function () use ($path) {
88+
$stat = yield from $this->runFileTask(new Internal\FileTask("stat", [$path]));
89+
if (!empty($stat)) {
90+
StatCache::set($path, $stat);
91+
}
92+
return $stat;
93+
});
7894
}
7995

8096
/**
@@ -88,14 +104,32 @@ public function rename(string $from, string $to): Promise {
88104
* {@inheritdoc}
89105
*/
90106
public function isfile(string $path): Promise {
91-
return new Coroutine($this->runFileTask(new Internal\FileTask("isfile", [$path])));
107+
return call(function () use ($path) {
108+
$stat = yield $this->stat($path);
109+
if (empty($stat)) {
110+
return false;
111+
}
112+
if ($stat["mode"] & 0100000) {
113+
return true;
114+
}
115+
return false;
116+
});
92117
}
93118

94119
/**
95120
* {@inheritdoc}
96121
*/
97122
public function isdir(string $path): Promise {
98-
return new Coroutine($this->runFileTask(new Internal\FileTask("isdir", [$path])));
123+
return call(function () use ($path) {
124+
$stat = yield $this->stat($path);
125+
if (empty($stat)) {
126+
return false;
127+
}
128+
if ($stat["mode"] & 0040000) {
129+
return true;
130+
}
131+
return false;
132+
});
99133
}
100134

101135
/**
@@ -137,7 +171,11 @@ public function scandir(string $path): Promise {
137171
* {@inheritdoc}
138172
*/
139173
public function rmdir(string $path): Promise {
140-
return new Coroutine($this->runFileTask(new Internal\FileTask("rmdir", [$path])));
174+
return call(function () use ($path) {
175+
$result = yield from $this->runFileTask(new Internal\FileTask("rmdir", [$path]));
176+
StatCache::clear($path);
177+
return $result;
178+
});
141179
}
142180

143181
/**
@@ -165,28 +203,55 @@ public function exists(string $path): Promise {
165203
* {@inheritdoc}
166204
*/
167205
public function size(string $path): Promise {
168-
return new Coroutine($this->runFileTask(new Internal\FileTask("size", [$path])));
206+
return call(function () use ($path) {
207+
$stat = yield $this->stat($path);
208+
if (empty($stat)) {
209+
throw new FilesystemException("Specified path does not exist");
210+
}
211+
if ($stat["mode"] & 0100000) {
212+
return $stat["size"];
213+
}
214+
throw new FilesystemException("Specified path is not a regular file");
215+
});
169216
}
170217

171218
/**
172219
* {@inheritdoc}
173220
*/
174221
public function mtime(string $path): Promise {
175-
return new Coroutine($this->runFileTask(new Internal\FileTask("mtime", [$path])));
222+
return call(function () use ($path) {
223+
$stat = yield $this->stat($path);
224+
if (empty($stat)) {
225+
throw new FilesystemException("Specified path does not exist");
226+
}
227+
return $stat["mtime"];
228+
});
176229
}
177230

178231
/**
179232
* {@inheritdoc}
180233
*/
181234
public function atime(string $path): Promise {
182-
return new Coroutine($this->runFileTask(new Internal\FileTask("atime", [$path])));
235+
return call(function () use ($path) {
236+
$stat = yield $this->stat($path);
237+
if (empty($stat)) {
238+
throw new FilesystemException("Specified path does not exist");
239+
}
240+
return $stat["atime"];
241+
});
183242
}
184243

185244
/**
186245
* {@inheritdoc}
187246
*/
188247
public function ctime(string $path): Promise {
189-
return new Coroutine($this->runFileTask(new Internal\FileTask("ctime", [$path])));
248+
return call(function () use ($path) {
249+
$stat = yield $this->stat($path);
250+
if (empty($stat)) {
251+
throw new FilesystemException("Specified path does not exist");
252+
}
253+
return $stat["ctime"];
254+
});
190255
}
191256

192257
/**

0 commit comments

Comments
 (0)