|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OC; |
| 11 | + |
| 12 | +use OCP\ISnowflakeId; |
| 13 | +use Override; |
| 14 | + |
| 15 | +/** |
| 16 | + * Nextcloud Snowflake ID |
| 17 | + * |
| 18 | + * Get information about Snowflake Id |
| 19 | + * |
| 20 | + * @since 33.0.0 |
| 21 | + */ |
| 22 | +final class SnowflakeId implements ISnowflakeId { |
| 23 | + private int $seconds = 0; |
| 24 | + private int $milliseconds = 0; |
| 25 | + private bool $isCli = false; |
| 26 | + /** @var int<0, 511> */ |
| 27 | + private int $serverId = 0; |
| 28 | + /** @var int<0, 4095> */ |
| 29 | + private int $sequenceId = 0; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + private readonly int|float $id, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + private function decode(): void { |
| 37 | + if ($this->seconds !== 0) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + PHP_INT_SIZE === 9 // FIXME 8 |
| 42 | + ? $this->decode64bits() |
| 43 | + : $this->decode32bits(); |
| 44 | + } |
| 45 | + |
| 46 | + private function decode64bits(): void { |
| 47 | + $id = (int)$this->id; |
| 48 | + $firstHalf = $id >> 32; |
| 49 | + $secondHalf = $id & 0xFFFFFFFF; |
| 50 | + |
| 51 | + // First half without first bit is seconds |
| 52 | + $this->seconds = $firstHalf & 0x7FFFFFFF; |
| 53 | + |
| 54 | + printf( |
| 55 | + "Debug: %08x %08x\n", |
| 56 | + $firstHalf, |
| 57 | + $secondHalf, |
| 58 | + ); |
| 59 | + |
| 60 | + // Decode second half |
| 61 | + $this->milliseconds = $secondHalf >> 22; |
| 62 | + $this->serverId = ($secondHalf >> 13) & 0x1FF; |
| 63 | + $this->isCli = (bool)(($secondHalf >> 12) & 0x1); |
| 64 | + $this->sequenceId = $secondHalf & 0xFFF; |
| 65 | + } |
| 66 | + |
| 67 | + private function decode32bits(): void { |
| 68 | + $id = str_pad(base_convert(number_format($this->id, 0, '', ''), 10, 16), 16, '0', STR_PAD_LEFT); |
| 69 | + $firstQuarter = hexdec(substr($id, 0, 4)); |
| 70 | + $secondQuarter = hexdec(substr($id, 4, 4)); |
| 71 | + $thirdQuarter = hexdec(substr($id, 8, 4)); |
| 72 | + $fourthQuarter = hexdec(substr($id, 12, 4)); |
| 73 | + |
| 74 | + printf("\nDebug: %s\n", number_format($this->id, 0, '', '')); |
| 75 | + printf("Debug: %s\n", $id); |
| 76 | + printf( |
| 77 | + "Debug: %04x %04x %04x %04x\n", |
| 78 | + $firstQuarter, |
| 79 | + $secondQuarter, |
| 80 | + $thirdQuarter, |
| 81 | + $fourthQuarter, |
| 82 | + ); |
| 83 | + |
| 84 | + $this->seconds = (($firstQuarter & 0x7FFF) << 16) | ($secondQuarter & 0xFFFF); |
| 85 | + |
| 86 | + $this->milliseconds = ($thirdQuarter >> 6) & 0x3FF; |
| 87 | + |
| 88 | + $this->serverId = (($thirdQuarter & 0x3F) << 3) | (($fourthQuarter >> 13) & 0x7); |
| 89 | + $this->isCli = (bool)(($fourthQuarter >> 12) & 0x1); |
| 90 | + $this->sequenceId = $fourthQuarter & 0xFFF; |
| 91 | + } |
| 92 | + |
| 93 | + #[Override] |
| 94 | + public function isCli(): bool { |
| 95 | + return $this->isCli; |
| 96 | + } |
| 97 | + |
| 98 | + #[Override] |
| 99 | + public function numeric(): int|float { |
| 100 | + return $this->id; |
| 101 | + } |
| 102 | + |
| 103 | + #[Override] |
| 104 | + public function seconds(): int { |
| 105 | + $this->decode(); |
| 106 | + return $this->seconds; |
| 107 | + } |
| 108 | + |
| 109 | + #[Override] |
| 110 | + public function milliseconds(): int { |
| 111 | + $this->decode(); |
| 112 | + return $this->milliseconds; |
| 113 | + } |
| 114 | + |
| 115 | + #[Override] |
| 116 | + public function createdAt(): float { |
| 117 | + $this->decode(); |
| 118 | + return $this->seconds + self::TS_OFFSET + ($this->milliseconds / 1000); |
| 119 | + } |
| 120 | + |
| 121 | + #[Override] |
| 122 | + public function serverId(): int { |
| 123 | + $this->decode(); |
| 124 | + return $this->serverId; |
| 125 | + } |
| 126 | + |
| 127 | + #[Override] |
| 128 | + public function sequenceId(): int { |
| 129 | + $this->decode(); |
| 130 | + return $this->sequenceId; |
| 131 | + } |
| 132 | +} |
0 commit comments