|
| 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|string $id, |
| 33 | + ) { |
| 34 | + if (is_string($id) && !ctype_digit($id)) { |
| 35 | + throw new \Exception('Invalid Snowflake ID: ' . $id); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private function decode(): void { |
| 40 | + if ($this->seconds !== 0) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + PHP_INT_SIZE === 8 |
| 45 | + ? $this->decode64bits() |
| 46 | + : $this->decode32bits(); |
| 47 | + } |
| 48 | + |
| 49 | + private function decode64bits(): void { |
| 50 | + $id = (int)$this->id; |
| 51 | + $firstHalf = $id >> 32; |
| 52 | + $secondHalf = $id & 0xFFFFFFFF; |
| 53 | + |
| 54 | + // First half without first bit is seconds |
| 55 | + $this->seconds = $firstHalf & 0x7FFFFFFF; |
| 56 | + |
| 57 | + // Decode second half |
| 58 | + $this->milliseconds = $secondHalf >> 22; |
| 59 | + $this->serverId = ($secondHalf >> 13) & 0x1FF; |
| 60 | + $this->isCli = (bool)(($secondHalf >> 12) & 0x1); |
| 61 | + $this->sequenceId = $secondHalf & 0xFFF; |
| 62 | + } |
| 63 | + |
| 64 | + private function decode32bits(): void { |
| 65 | + $id = is_int($this->id) ? number_format($this->id, 0, '', '') : $this->id; |
| 66 | + $id = $this->convertBase16($id); |
| 67 | + |
| 68 | + $firstQuarter = (int)hexdec(substr($id, 0, 4)); |
| 69 | + $secondQuarter = (int)hexdec(substr($id, 4, 4)); |
| 70 | + $thirdQuarter = (int)hexdec(substr($id, 8, 4)); |
| 71 | + $fourthQuarter = (int)hexdec(substr($id, 12, 4)); |
| 72 | + |
| 73 | + $this->seconds = (($firstQuarter & 0x7FFF) << 16) | ($secondQuarter & 0xFFFF); |
| 74 | + |
| 75 | + $this->milliseconds = ($thirdQuarter >> 6) & 0x3FF; |
| 76 | + |
| 77 | + $this->serverId = (($thirdQuarter & 0x3F) << 3) | (($fourthQuarter >> 13) & 0x7); |
| 78 | + $this->isCli = (bool)(($fourthQuarter >> 12) & 0x1); |
| 79 | + $this->sequenceId = $fourthQuarter & 0xFFF; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Convert base 10 number to base 16, padded to 16 characters |
| 84 | + * |
| 85 | + * Required on 32 bits systems as base_convert will lose precision with large numbers |
| 86 | + */ |
| 87 | + private function convertBase16(string $decimal): string { |
| 88 | + $hex = ''; |
| 89 | + $digits = '0123456789ABCDEF'; |
| 90 | + |
| 91 | + while (strlen($decimal) > 0 && $decimal !== '0') { |
| 92 | + $remainder = 0; |
| 93 | + $newDecimal = ''; |
| 94 | + |
| 95 | + // Perform division by 16 manually for arbitrary precision |
| 96 | + for ($i = 0; $i < strlen($decimal); $i++) { |
| 97 | + $digit = (int)$decimal[$i]; |
| 98 | + $current = $remainder * 10 + $digit; |
| 99 | + |
| 100 | + if ($current >= 16) { |
| 101 | + $quotient = (int)($current / 16); |
| 102 | + $remainder = $current % 16; |
| 103 | + $newDecimal .= chr(ord('0') + $quotient); |
| 104 | + } else { |
| 105 | + $remainder = $current; |
| 106 | + // Only add quotient digit if we already have some digits in result |
| 107 | + if (strlen($newDecimal) > 0) { |
| 108 | + $newDecimal .= '0'; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Add the remainder (0-15) as hex digit |
| 114 | + $hex = $digits[$remainder] . $hex; |
| 115 | + |
| 116 | + // Update decimal for next iteration |
| 117 | + $decimal = ltrim($newDecimal, '0'); |
| 118 | + } |
| 119 | + |
| 120 | + return str_pad($hex, 16, '0', STR_PAD_LEFT); |
| 121 | + } |
| 122 | + |
| 123 | + #[Override] |
| 124 | + public function isCli(): bool { |
| 125 | + return $this->isCli; |
| 126 | + } |
| 127 | + |
| 128 | + #[Override] |
| 129 | + public function numeric(): int|string { |
| 130 | + return $this->id; |
| 131 | + } |
| 132 | + |
| 133 | + #[Override] |
| 134 | + public function seconds(): int { |
| 135 | + $this->decode(); |
| 136 | + return $this->seconds; |
| 137 | + } |
| 138 | + |
| 139 | + #[Override] |
| 140 | + public function milliseconds(): int { |
| 141 | + $this->decode(); |
| 142 | + return $this->milliseconds; |
| 143 | + } |
| 144 | + |
| 145 | + #[Override] |
| 146 | + public function createdAt(): float { |
| 147 | + $this->decode(); |
| 148 | + return $this->seconds + self::TS_OFFSET + ($this->milliseconds / 1000); |
| 149 | + } |
| 150 | + |
| 151 | + #[Override] |
| 152 | + public function serverId(): int { |
| 153 | + $this->decode(); |
| 154 | + return $this->serverId; |
| 155 | + } |
| 156 | + |
| 157 | + #[Override] |
| 158 | + public function sequenceId(): int { |
| 159 | + $this->decode(); |
| 160 | + return $this->sequenceId; |
| 161 | + } |
| 162 | +} |
0 commit comments