|
| 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 === 8 |
| 42 | + ? $this->decode64bits((int)$this->id) |
| 43 | + : $this->decode32bits((float)$this->id); |
| 44 | + } |
| 45 | + |
| 46 | + private function decode64bits(int $id) { |
| 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 | + // Decode second half |
| 55 | + $this->milliseconds = $secondHalf >> 22; |
| 56 | + $this->serverId = ($secondHalf >> 13) & 0x1FF; |
| 57 | + $this->isCli = (bool)(($secondHalf >> 12) & 0x1); |
| 58 | + $this->sequenceId = $secondHalf & 0xFFF; |
| 59 | + } |
| 60 | + |
| 61 | + private function decode32bits(float $id) { |
| 62 | + $id = sprintf('%016x', (string)$this->id); |
| 63 | + // First half doesn't use first bit so it fit in signed 32 bits int |
| 64 | + $firstHalf = hexdec(substr($id, 0, 8)); |
| 65 | + // Second half must be splited to avoid issues with signed 32 bits int |
| 66 | + $thirdQuarter = hexdec(substr($id, 8, 4)); |
| 67 | + $fourthQuarter = hexdec(substr($id, 12, 4)); |
| 68 | + |
| 69 | + $this->seconds = $firstHalf & 0x7FFFFFFF; |
| 70 | + |
| 71 | + $this->milliseconds = ($thirdQuarter >> 6) & 0x3FF; |
| 72 | + |
| 73 | + $this->serverId = (($thirdQuarter & 0x3F) << 3) | (($fourthQuarter >> 13) & 0x7); |
| 74 | + $this->isCli = (bool)(($fourthQuarter >> 12) & 0x1); |
| 75 | + $this->sequenceId = $fourthQuarter & 0xFFF; |
| 76 | + } |
| 77 | + |
| 78 | + #[Override] |
| 79 | + public function isCli(): bool { |
| 80 | + return $this->isCli; |
| 81 | + } |
| 82 | + |
| 83 | + #[Override] |
| 84 | + public function numeric(): int|float { |
| 85 | + return $this->id; |
| 86 | + } |
| 87 | + |
| 88 | + #[Override] |
| 89 | + public function seconds(): int { |
| 90 | + $this->decode(); |
| 91 | + return $this->seconds; |
| 92 | + } |
| 93 | + |
| 94 | + #[Override] |
| 95 | + public function milliseconds(): int { |
| 96 | + $this->decode(); |
| 97 | + return $this->milliseconds; |
| 98 | + } |
| 99 | + |
| 100 | + #[Override] |
| 101 | + public function createdAt(): float { |
| 102 | + $this->decode(); |
| 103 | + return $this->seconds + self::TS_OFFSET + ($this->milliseconds / 1000); |
| 104 | + } |
| 105 | + |
| 106 | + #[Override] |
| 107 | + public function serverId(): int { |
| 108 | + $this->decode(); |
| 109 | + return $this->serverId; |
| 110 | + } |
| 111 | + |
| 112 | + #[Override] |
| 113 | + public function sequenceId(): int { |
| 114 | + $this->decode(); |
| 115 | + return $this->sequenceId; |
| 116 | + } |
| 117 | +} |
0 commit comments