Skip to content

Commit

Permalink
Use StatCache in ParallelDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jul 18, 2017
1 parent 28a62c8 commit 169a1d1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
6 changes: 0 additions & 6 deletions lib/Internal/FileTask.php
Expand Up @@ -131,18 +131,12 @@ public function run(Environment $environment) {
case "readlink":
case "lstat":
case "exists":
case "isfile":
case "isdir":
case "mkdir":
case "scandir":
case "rmdir":
case "chmod":
case "chown":
case "touch":
case "size":
case "mtime":
case "atime":
case "ctime":
case "get":
case "put":
return ([new BlockingDriver, $this->operation])(...$this->args);
Expand Down
83 changes: 74 additions & 9 deletions lib/ParallelDriver.php
Expand Up @@ -9,6 +9,8 @@
use Amp\Parallel\Worker\TaskException;
use Amp\Parallel\Worker\WorkerException;
use Amp\Promise;
use Amp\Success;
use function Amp\call;

class ParallelDriver implements Driver {
/**
Expand Down Expand Up @@ -67,14 +69,28 @@ private function runFileTask(Internal\FileTask $task): \Generator {
* {@inheritdoc}
*/
public function unlink(string $path): Promise {
return new Coroutine($this->runFileTask(new Internal\FileTask("unlink", [$path])));
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 {
return new Coroutine($this->runFileTask(new Internal\FileTask("stat", [$path])));
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;
});
}

/**
Expand All @@ -88,14 +104,32 @@ public function rename(string $from, string $to): Promise {
* {@inheritdoc}
*/
public function isfile(string $path): Promise {
return new Coroutine($this->runFileTask(new Internal\FileTask("isfile", [$path])));
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 new Coroutine($this->runFileTask(new Internal\FileTask("isdir", [$path])));
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
return false;
}
if ($stat["mode"] & 0040000) {
return true;
}
return false;
});
}

/**
Expand Down Expand Up @@ -137,7 +171,11 @@ public function scandir(string $path): Promise {
* {@inheritdoc}
*/
public function rmdir(string $path): Promise {
return new Coroutine($this->runFileTask(new Internal\FileTask("rmdir", [$path])));
return call(function () use ($path) {
$result = yield from $this->runFileTask(new Internal\FileTask("rmdir", [$path]));
StatCache::clear($path);
return $result;
});
}

/**
Expand Down Expand Up @@ -165,28 +203,55 @@ public function exists(string $path): Promise {
* {@inheritdoc}
*/
public function size(string $path): Promise {
return new Coroutine($this->runFileTask(new Internal\FileTask("size", [$path])));
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new FilesystemException("Specified path does not exist");
}
if ($stat["mode"] & 0100000) {
return $stat["size"];
}
throw new FilesystemException("Specified path is not a regular file");
});
}

/**
* {@inheritdoc}
*/
public function mtime(string $path): Promise {
return new Coroutine($this->runFileTask(new Internal\FileTask("mtime", [$path])));
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new FilesystemException("Specified path does not exist");
}
return $stat["mtime"];
});
}

/**
* {@inheritdoc}
*/
public function atime(string $path): Promise {
return new Coroutine($this->runFileTask(new Internal\FileTask("atime", [$path])));
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new FilesystemException("Specified path does not exist");
}
return $stat["atime"];
});
}

/**
* {@inheritdoc}
*/
public function ctime(string $path): Promise {
return new Coroutine($this->runFileTask(new Internal\FileTask("ctime", [$path])));
return call(function () use ($path) {
$stat = yield $this->stat($path);
if (empty($stat)) {
throw new FilesystemException("Specified path does not exist");
}
return $stat["ctime"];
});
}

/**
Expand Down

0 comments on commit 169a1d1

Please sign in to comment.