transactions support

This commit is contained in:
shimon committed 2025-07-14 11:20:00 +03:00
1 parent deaa0015b3
commit 71c062c42b
1 file changed
+104 -2
+104 -2
View File
@@ -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;
}
}
}