Skip to content

Commit 313e83e

Browse files
committed
Use int32 conversion logic from postgres
1 parent 3f63186 commit 313e83e

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/LockId/PostgresLockId.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function fromLockId(
3939
$lockStringId = (string)$lockId;
4040

4141
return new self(
42-
id: self::generateIdFromString($lockStringId),
42+
id: self::convertStringToSignedInt32($lockStringId),
4343
humanReadableValue: $lockStringId,
4444
);
4545
}
@@ -51,15 +51,19 @@ public static function fromLockId(
5151
* The crc32 function returns an unsigned 32-bit integer (0 to 4_294_967_295).
5252
* Postgres advisory locks require a signed 32-bit integer (-2_147_483_648 to 2_147_483_647).
5353
*
54-
* This method shifts the crc32 output into the signed int32 range by subtracting (2^31) + 1,
55-
* ensuring the result fits within Postgres's required range and preserves uniqueness.
54+
* This method converts the unsigned crc32 result to a signed 32-bit integer,
55+
* matching the way PostgreSQL interprets int4 values internally.
5656
*
57-
* @param string $string The input string to hash into an int32 lock ID.
57+
* @param string $string The input string to hash into a signed int32 lock ID.
5858
* @return int The signed 32-bit integer suitable for Postgres advisory locks.
5959
*/
60-
private static function generateIdFromString(
60+
private static function convertStringToSignedInt32(
6161
string $string,
6262
): int {
63-
return crc32($string) - self::DB_INT32_VALUE_MAX - 1;
63+
$unsignedInt = crc32($string);
64+
65+
return $unsignedInt > 0x7FFFFFFF
66+
? $unsignedInt - 0x100000000
67+
: $unsignedInt;
6468
}
6569
}

test/Unit/LockId/PostgresLockIdTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function test_it_can_create_postgres_lock_id_from_lock_id(): void
4242

4343
$postgresLockId = PostgresLockId::fromLockId($lockId);
4444

45-
$this->assertSame(1484750348, $postgresLockId->id);
45+
$this->assertSame(-662733300, $postgresLockId->id);
4646
}
4747

4848
public function test_it_can_create_postgres_lock_id_from_lock_id_with_value(): void
@@ -51,6 +51,6 @@ public function test_it_can_create_postgres_lock_id_from_lock_id_with_value(): v
5151

5252
$postgresLockId = PostgresLockId::fromLockId($lockId);
5353

54-
$this->assertSame(-1364850700, $postgresLockId->id);
54+
$this->assertSame(782632948, $postgresLockId->id);
5555
}
5656
}

0 commit comments

Comments
 (0)