From 71c062c42bca6a4f709ddb86670089d5d4a5d7d5 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 14 Jul 2025 11:20:00 +0300 Subject: [PATCH] transactions support --- src/Client.php | 106 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 0a9b3d11fc..511c01211b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,6 +6,7 @@ use MongoDB\BSON; use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; use stdClass; +use MongoDB\BSON\Int64; class Client { @@ -36,6 +37,10 @@ class Client public const COMMAND_AGGREGATE = "aggregate"; public const COMMAND_DISTINCT = "distinct"; public const COMMAND_MAP_REDUCE = "mapReduce"; + public const COMMAND_START_SESSION = "startSession"; + public const COMMAND_COMMIT_TRANSACTION = "commitTransaction"; + public const COMMAND_ABORT_TRANSACTION = "abortTransaction"; + public const COMMAND_END_SESSIONS = "endSessions"; /** @@ -139,6 +144,7 @@ class Client $sections = BSON\fromPHP($params); $message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections; + return $this->send($message); } @@ -225,7 +231,6 @@ class Client if (property_exists($result, "nonce") && $result->ok === 1.0) { return $result; } - if ($result->ok === 1.0) { return $result; } @@ -515,6 +520,7 @@ class Client } $cleanUpdates[$k] = $v; } + $this->query( array_merge([ @@ -754,6 +760,101 @@ class Client ]); } + /** + * Start a new logical session. Returns the session id object.. + * + * @return object + * @throws Exception + */ + public function startSession(): object + { + $result = $this->query([ + self::COMMAND_START_SESSION => 1 + ], 'admin'); + + return $result->id->id; + } + + /** + * Commit a transaction. + * + * @param array $lsid + * @param int $txnNumber + * @param bool $autocommit + * @return mixed + * @throws Exception + */ + public function commitTransaction(array $lsid, int $txnNumber, bool $autocommit = false) + { + $txnNumber = new \MongoDB\BSON\Int64($txnNumber); + + $result = $this->query([ + self::COMMAND_COMMIT_TRANSACTION => 1, + 'lsid' => $lsid, + 'txnNumber' => $txnNumber, + 'autocommit' => $autocommit + ], 'admin'); + + // End the session after successful commit + $this->endSessions([$lsid]); + + return $result; + } + + /** + * Abort (rollback) a transaction. + * + * @param array $lsid + * @param int $txnNumber + * @param bool $autocommit + * @return mixed + * @throws Exception + */ + public function abortTransaction(array $lsid, int $txnNumber, bool $autocommit = false) + { + + $txnNumber = new \MongoDB\BSON\Int64($txnNumber); + + $result = $this->query([ + self::COMMAND_ABORT_TRANSACTION => 1, + 'lsid' => $lsid, + 'txnNumber' => $txnNumber, + 'autocommit' => $autocommit + ], 'admin'); + + // End the session after successful rollback + $this->endSessions([$lsid]); + + return $result; + } + + /** + * End sessions. + * + * @param array $lsids + * @param array $options + * @return mixed + * @throws Exception + */ + public function endSessions(array $lsids, array $options = []) + { + // Extract session IDs from the format ['id' => sessionId] and format as objects + $sessionIds = array_map(function ($lsid) { + $sessionId = $lsid['id'] ?? $lsid; + return ['id' => $sessionId]; + }, $lsids); + + return $this->query( + array_merge( + [ + self::COMMAND_END_SESSIONS => $sessionIds, + ], + $options + ), + 'admin' + ); + } + /** * Convert an assoc array to an object (stdClass). * @@ -822,4 +923,5 @@ class Client return $cleanedFilters; } -} + +} \ No newline at end of file