|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 8 | + */ |
| 9 | +namespace OC\BackgroundJob; |
| 10 | + |
| 11 | +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
| 12 | +use InvalidArgumentException; |
| 13 | +use OCP\BackgroundJob\IJob; |
| 14 | +use OCP\IDBConnection; |
| 15 | +use OCP\Snowflake\ISnowflakeGenerator; |
| 16 | + |
| 17 | +/** |
| 18 | + * Map background job classes and their ID in database |
| 19 | + * |
| 20 | + * Uses a rapid hash to speed-up lookups |
| 21 | + */ |
| 22 | +final class JobClassesRegistry { |
| 23 | + /** |
| 24 | + * @var array<string,string> |
| 25 | + */ |
| 26 | + private array $registry = []; |
| 27 | + |
| 28 | + private const TABLE = 'job_classes_registry'; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly IDBConnection $connection, |
| 32 | + private readonly ISnowflakeGenerator $snowflakeGenerator, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + private function loadRegistry(): void { |
| 37 | + if ($this->registry !== []) { |
| 38 | + return; |
| 39 | + } |
| 40 | + $qb = $this->connection->getQueryBuilder(); |
| 41 | + $result = $qb->select('class_id', 'class_name')->from(self::TABLE)->executeQuery(); |
| 42 | + foreach ($result->iterateAssociative() as $row) { |
| 43 | + $this->registry[$row['class_name']] = (string)$row['class_id']; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Resolve current ID or generates a new one |
| 49 | + */ |
| 50 | + public function getId(string $className): string { |
| 51 | + $this->loadRegistry(); |
| 52 | + if (isset($this->registry[$className])) { |
| 53 | + return $this->registry[$className]; |
| 54 | + } |
| 55 | + |
| 56 | + if (!class_exists($className)) { |
| 57 | + throw new InvalidArgumentException('Class ' . $className . ' doesn’t exists'); |
| 58 | + } |
| 59 | + if (!isset(class_implements($className)[IJob::class])) { |
| 60 | + throw new InvalidArgumentException('Class ' . $className . ' isn’t an instance of ' . IJob::class); |
| 61 | + } |
| 62 | + |
| 63 | + $qb = $this->connection->getQueryBuilder(); |
| 64 | + $hashedName = $this->hashName($className); |
| 65 | + try { |
| 66 | + $classId = $this->snowflakeGenerator->nextId(); |
| 67 | + $qb |
| 68 | + ->insert(self::TABLE) |
| 69 | + ->values([ |
| 70 | + 'class_id' => $qb->createNamedParameter($classId), |
| 71 | + 'class_name' => $qb->createNamedParameter($className), |
| 72 | + 'class_hash' => $qb->createNamedParameter($hashedName), |
| 73 | + ]) |
| 74 | + ->executeStatement(); |
| 75 | + $this->registry[$className] = $classId; |
| 76 | + |
| 77 | + return $classId; |
| 78 | + } catch (UniqueConstraintViolationException $e) { |
| 79 | + // Class was probably added by a concurrent process |
| 80 | + // Try to load it |
| 81 | + $result = $qb |
| 82 | + ->select('class_id') |
| 83 | + ->from(self::TABLE) |
| 84 | + ->where($qb->expr()->eq('class_hash', $hashedName)) |
| 85 | + ->andWhere($qb->expr()->eq('class_name', $className)) |
| 86 | + ->executeQuery(); |
| 87 | + if ($classId = $result->fetchOne()) { |
| 88 | + $classId = (string)$classId; |
| 89 | + $this->registry[$className] = $classId; |
| 90 | + |
| 91 | + return $classId; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + throw new \Exception('Fail to retrieve ' . $className . ' ID', previous: $e ?? null); |
| 96 | + } |
| 97 | + |
| 98 | + public function getName(string|int $classId): string { |
| 99 | + $this->loadRegistry(); |
| 100 | + $classId = (string)$classId; |
| 101 | + $className = array_search($classId, $this->registry, true); |
| 102 | + if ($className === false) { |
| 103 | + throw new InvalidArgumentException('Class ID ' . $classId . ' doesn’t match any class name'); |
| 104 | + } |
| 105 | + |
| 106 | + return $className; |
| 107 | + } |
| 108 | + |
| 109 | + private function hashName(string $className): int { |
| 110 | + return hexdec(hash('xxh32', $className)); |
| 111 | + } |
| 112 | +} |
0 commit comments