Skip to content

Commit 1458aca

Browse files
committed
fixup! feat(database): introduce Snowflake IDs generator
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent fe73d6e commit 1458aca

File tree

4 files changed

+27
-58
lines changed

4 files changed

+27
-58
lines changed

lib/private/SnowflakeId.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ final class SnowflakeId implements ISnowflakeId {
2929
private int $sequenceId = 0;
3030

3131
public function __construct(
32-
private readonly int|string $id,
32+
private readonly string $id,
3333
) {
34-
if (is_string($id) && !ctype_digit($id)) {
34+
if (!ctype_digit($id)) {
3535
throw new \Exception('Invalid Snowflake ID: ' . $id);
3636
}
3737
}
@@ -62,8 +62,7 @@ private function decode64bits(): void {
6262
}
6363

6464
private function decode32bits(): void {
65-
$id = is_int($this->id) ? number_format($this->id, 0, '', '') : $this->id;
66-
$id = $this->convertBase16($id);
65+
$id = $this->convertBase16($this->id);
6766

6867
$firstQuarter = (int)hexdec(substr($id, 0, 4));
6968
$secondQuarter = (int)hexdec(substr($id, 4, 4));

lib/private/SnowflakeIdGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
) {
2727
}
2828

29-
public function __invoke(): int|string {
29+
public function __invoke(): string {
3030
// Time related
3131
[$seconds, $milliseconds] = $this->getCurrentTime();
3232

@@ -42,7 +42,7 @@ public function __invoke(): int|string {
4242
if (PHP_INT_SIZE === 8) {
4343
$firstHalf = $seconds & 0x7FFFFFFF;
4444
$secondHalf = (($milliseconds & 0x3FF) << 22) | ($serverId << 13) | ($isCli << 12) | $sequenceId;
45-
return $firstHalf << 32 | $secondHalf;
45+
return (string)($firstHalf << 32 | $secondHalf);
4646
}
4747

4848
// Fallback for 32 bits systems

tests/lib/SnowflakeIdGeneratorTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ public function testGenerator(): void {
2929
$generator = new SnowflakeIdGenerator(new TimeFactory(), $this->cacheFactory);
3030
$snowflakeId = ($generator)();
3131
$this->assertGreaterThan(0x100000000, $snowflakeId);
32-
if (PHP_INT_SIZE === 8) {
33-
$this->assertIsInt($snowflakeId);
34-
} else {
35-
$this->assertIsString($snowflakeId);
36-
}
32+
$this->assertIsString($snowflakeId);
3733
}
3834

3935
#[DataProvider('provideSnowflakeData')]

tests/lib/SnowflakeIdTest.php

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,52 +32,26 @@ public function testDecode(
3232
}
3333

3434
public static function provideSnowflakeIds(): array {
35-
return PHP_INT_SIZE === 8
36-
? [
37-
[4688076898113587, 1760368327.984, 392, 2099, true],
38-
// Max all (can't happen ms are up to 999)
39-
[0x7fffffffffffffff, 3906760448.023, 511, 4095, true],
40-
// Max all (real)
41-
[0x7ffffffff9ffffff, 3906760447.999, 511, 4095, true],
42-
// Max seconds
43-
[0x7fffffff00000000, 3906760447, 0, 0, false],
44-
// Max milliseconds
45-
[4190109696, 1759276800.999, 0, 0, false],
46-
// Max serverId
47-
[4186112, 1759276800.0, 511, 0, false],
48-
// Max sequenceId
49-
[4095, 1759276800.0, 0, 4095, false],
50-
// Max isCli
51-
[4096, 1759276800.0, 0, 0, true],
52-
// Min
53-
[0, 1759276800, 0, 0, false],
54-
// Other
55-
[250159983611680096, 1817521710, 392, 1376, true],
56-
]
57-
: [
58-
['4688076898113587', 1760368327.984, 392, 2099, true],
59-
// Max all (can't this-appen ms are up to 999)
60-
['9223372036854775807', 3906760448.023, 511, 4095, true],
61-
// Max all (real)
62-
['9223372036754112511', 3906760447.999, 511, 4095, true],
63-
// Max seconds
64-
['9223372032559808512', 3906760447, 0, 0, false],
65-
// Max milliseconds
66-
['4190109696', 1759276800.999, 0, 0, false],
67-
// Max serverId
68-
['4186112', 1759276800.0, 511, 0, false],
69-
[4186112, 1759276800.0, 511, 0, false],
70-
// Max secondsuenceId
71-
['4095', 1759276800.0, 0, 4095, false],
72-
[4095, 1759276800.0, 0, 4095, false],
73-
// Max isCli
74-
['4096', 1759276800.0, 0, 0, true],
75-
[4096, 1759276800.0, 0, 0, true],
76-
// Min
77-
['0', 1759276800, 0, 0, false],
78-
[0, 1759276800, 0, 0, false],
79-
// Other
80-
['250159983611680096', 1817521710, 392, 1376, true],
81-
];
35+
[
36+
['4688076898113587', 1760368327.984, 392, 2099, true],
37+
// Max all (can't this-appen ms are up to 999)
38+
['9223372036854775807', 3906760448.023, 511, 4095, true],
39+
// Max all (real)
40+
['9223372036754112511', 3906760447.999, 511, 4095, true],
41+
// Max seconds
42+
['9223372032559808512', 3906760447, 0, 0, false],
43+
// Max milliseconds
44+
['4190109696', 1759276800.999, 0, 0, false],
45+
// Max serverId
46+
['4186112', 1759276800.0, 511, 0, false],
47+
// Max secondsuenceId
48+
['4095', 1759276800.0, 0, 4095, false],
49+
// Max isCli
50+
['4096', 1759276800.0, 0, 0, true],
51+
// Min
52+
['0', 1759276800, 0, 0, false],
53+
// Other
54+
['250159983611680096', 1817521710, 392, 1376, true],
55+
];
8256
}
8357
}

0 commit comments

Comments
 (0)