-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultCompletion.php
93 lines (83 loc) · 2.11 KB
/
ResultCompletion.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
<?php
namespace Aternos\Rados\Completion;
use Aternos\Rados\Cluster\Pool\IOContext;
use Aternos\Rados\Exception\CompletionException;
use Aternos\Rados\Exception\RadosException;
/**
* @template T
*/
abstract class ResultCompletion extends Completion
{
/**
* @var T $result
*/
protected mixed $result = null;
protected bool $resultParsed = false;
/**
* @param IOContext $ioContext
* @internal Completions are returned from async operations and should not be created manually
*/
public function __construct(protected IOContext $ioContext)
{
parent::__construct($this->ioContext->getFFI());
$this->ioContext->registerChildObject($this);
}
/**
* Cancel async operation
*
* @return $this
* @throws RadosException
* @noinspection PhpUndefinedMethodInspection
*/
public function cancel(): static
{
CompletionException::handle($this->ffi->rados_aio_cancel($this->ioContext->getCData(), $this->getCData()));
return $this;
}
/**
* @return T
*/
abstract public function parseResult();
/**
* @throws CompletionException
* @throws RadosException
* @return T
*/
public function getResult()
{
if (!$this->isComplete()) {
throw new CompletionException("Operation is not complete yet");
}
if (!$this->resultParsed) {
$this->result = $this->parseResult();
$this->resultParsed = true;
}
return $this->result;
}
/**
* @return T
* @throws RadosException
*/
public function waitAndGetResult()
{
$this->waitForComplete();
return $this->getResult();
}
/**
* @inheritDoc
* @internal This method should not be called directly, use getResult or waitAndGetResult instead
*/
public function getReturnValue(): int
{
return parent::getReturnValue();
}
/**
* @return void
* @throws RadosException
*/
protected function releaseCData(): void
{
parent::releaseCData();
$this->cancel();
}
}