Skip to content

Commit afdd8ce

Browse files
committed
simplified result handling with weak references
1 parent daa5cbe commit afdd8ce

File tree

2 files changed

+45
-48
lines changed

2 files changed

+45
-48
lines changed

src/Bolt/BoltConnection.php

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,18 @@ public function setTimeout(float $timeout): void
143143
$this->factory->getConnection()->setTimeout($timeout);
144144
}
145145

146+
/**
147+
* Closes the connection.
148+
*
149+
* Any of the preconditioned states are: 'READY', 'STREAMING', 'TX_READY', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'.
150+
* Sends signal: 'DISCONNECT'
151+
*/
146152
public function close(): void
147153
{
148-
$this->possibleStates(['READY', 'STREAMING', 'TX_READY', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'], 'GOODBYE');
149-
150-
$this->signal('DISCONNECT');
151-
152154
$this->consumeResults();
153155

154156
$this->protocol()->goodbye();
155157

156-
$this->boltProtocol = null;
157158
$this->serverState = 'DEFUNCT';
158159
$this->subscribedResults = [];
159160
}
@@ -170,12 +171,14 @@ private function consumeResults(): void
170171
$this->subscribedResults = [];
171172
}
172173

174+
/**
175+
* Resets the connection.
176+
*
177+
* Any of the preconditioned states are: 'READY', 'STREAMING', 'TX_READY', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'.
178+
* Sends signal: 'INTERRUPT'
179+
*/
173180
public function reset(): void
174181
{
175-
$this->possibleStates(['READY', 'STREAMING', 'TX_READY', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'], 'RESET');
176-
177-
$this->signal('INTERRUPT');
178-
179182
$this->consumeResults();
180183

181184
try {
@@ -191,16 +194,15 @@ public function reset(): void
191194
}
192195

193196
/**
194-
* @param string|null $database the database to connect to
195-
* @param float|null $timeout timeout in seconds
197+
* Begins a transaction.
198+
*
199+
* Any of the preconditioned states are: 'READY', 'INTERRUPTED'.
196200
*/
197201
public function begin(?string $database, ?float $timeout): void
198202
{
199-
$this->possibleStates(['READY', 'INTERRUPTED'], 'BEGIN');
200-
201203
$this->consumeResults();
202204

203-
$extra = $this->buildExtra($database, $timeout);
205+
$extra = $this->buildRunExtra($database, $timeout);
204206
try {
205207
$this->protocol()->begin($extra);
206208
} catch (IgnoredException $e) {
@@ -215,15 +217,14 @@ public function begin(?string $database, ?float $timeout): void
215217
}
216218

217219
/**
218-
* @param string|null $database the database to connect to
219-
* @param float|null $timeout timeout in seconds
220+
* Discards a result.
221+
*
222+
* Any of the preconditioned states are: 'STREAMING', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'.
220223
*/
221224
public function discard(?int $qid): void
222225
{
223-
$this->possibleStates(['STREAMING', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'], 'DISCARD');
224-
225226
try {
226-
$extra = $this->buildExtraParam(null, $qid);
227+
$extra = $this->buildResultExtra(null, $qid);
227228
$bolt = $this->protocol();
228229

229230
if ($bolt instanceof V4) {
@@ -245,18 +246,20 @@ public function discard(?int $qid): void
245246
}
246247

247248
/**
249+
* Runs a query/statement.
250+
*
251+
* Any of the preconditioned states are: 'STREAMING', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'.
252+
*
248253
* @return BoltMeta
249254
*/
250255
public function run(string $text, array $parameters, ?string $database, ?float $timeout): array
251256
{
252-
$this->possibleStates(['READY', 'TX_READY', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'], 'RUN');
253-
254257
if (!str_starts_with($this->serverState, 'TX_')) {
255258
$this->consumeResults();
256259
}
257260

258261
try {
259-
$extra = $this->buildExtra($database, $timeout);
262+
$extra = $this->buildRunExtra($database, $timeout);
260263

261264
$tbr = $this->protocol()->run($text, $parameters, $extra);
262265

@@ -278,10 +281,13 @@ public function run(string $text, array $parameters, ?string $database, ?float $
278281
}
279282
}
280283

284+
/**
285+
* Commits a transaction.
286+
*
287+
* Any of the preconditioned states are: 'TX_READY', 'INTERRUPTED'.
288+
*/
281289
public function commit(): void
282290
{
283-
$this->possibleStates(['TX_READY', 'INTERRUPTED'], 'COMMIT');
284-
285291
$this->consumeResults();
286292

287293
try {
@@ -299,10 +305,13 @@ public function commit(): void
299305
$this->serverState = 'READY';
300306
}
301307

308+
/**
309+
* Rolls back a transaction.
310+
*
311+
* Any of the preconditioned states are: 'TX_READY', 'INTERRUPTED'.
312+
*/
302313
public function rollback(): void
303314
{
304-
$this->possibleStates(['TX_READY', 'INTERRUPTED'], 'ROLLBACK');
305-
306315
$this->consumeResults();
307316

308317
try {
@@ -321,12 +330,15 @@ public function rollback(): void
321330
}
322331

323332
/**
333+
* Pulls a result set.
334+
*
335+
* Any of the preconditioned states are: 'TX_READY', 'INTERRUPTED'.
336+
*
324337
* @return non-empty-list<list>
325338
*/
326339
public function pull(?int $qid, ?int $fetchSize): array
327340
{
328-
$this->possibleStates(['STREAMING', 'TX_STREAMING', 'FAILED', 'INTERRUPTED'], 'ROLLBACK');
329-
$extra = $this->buildExtraParam($fetchSize, $qid);
341+
$extra = $this->buildResultExtra($fetchSize, $qid);
330342

331343
$bolt = $this->protocol();
332344
try {
@@ -362,12 +374,12 @@ public function getDriverConfiguration(): DriverConfiguration
362374

363375
public function __destruct()
364376
{
365-
if ($this->serverState !== 'DISCONNECTED' && $this->serverState !== 'DEFUNCT' && $this->boltProtocol !== null) {
377+
if ($this->serverState !== 'DISCONNECTED' && $this->serverState !== 'DEFUNCT') {
366378
$this->close();
367379
}
368380
}
369381

370-
private function buildExtra(?string $database, ?float $timeout): array
382+
private function buildRunExtra(?string $database, ?float $timeout): array
371383
{
372384
$extra = [];
373385
if ($database) {
@@ -380,7 +392,7 @@ private function buildExtra(?string $database, ?float $timeout): array
380392
return $extra;
381393
}
382394

383-
public function buildExtraParam(?int $fetchSize, ?int $qid): array
395+
private function buildResultExtra(?int $fetchSize, ?int $qid): array
384396
{
385397
$extra = [];
386398
if ($fetchSize) {
@@ -394,28 +406,14 @@ public function buildExtraParam(?int $fetchSize, ?int $qid): array
394406
return $extra;
395407
}
396408

397-
/**
398-
* @return string
399-
*/
400409
public function getServerState(): string
401410
{
402411
return $this->serverState;
403412
}
404413

405-
/**
406-
* @param callable(ServerStateTransition): void $listener
407-
*/
408-
private function bindAfterTransitionEventListener($listener): void
409-
{
410-
$this->subscribedResults[] = $listener;
411-
}
412-
413-
private function possibleStates(array $array, string $string)
414-
{
415-
}
416-
417-
private function signal(string $string)
414+
private function subscribeResult(SummarizedResult $result): void
418415
{
416+
$this->subscribedResults[] = WeakReference::create($result);
419417
}
420418

421419
private function protocol(): V3

src/Bolt/BoltResult.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public function valid(): bool
143143
public function rewind(): void
144144
{
145145
// Rewind is impossible
146-
throw new BadMethodCallException('Cannot rewind a bolt result.');
147146
}
148147

149148
public function __destruct()

0 commit comments

Comments
 (0)