1212use InvalidArgumentException ;
1313use OCP \BackgroundJob \IJob ;
1414use OCP \IDBConnection ;
15+ use OCP \Snowflake \ISnowflakeGenerator ;
1516
1617/**
1718 * Map background job classes and their ID in database
1819 *
1920 * Uses a rapid hash to speed-up lookups
2021 */
2122final class JobClassesRegistry {
22- private array $ register = [];
23+ private array $ registry = [];
2324
2425 private const TABLE = 'job_classes_registry ' ;
2526
2627 public function __construct (
2728 private readonly IDBConnection $ connection ,
29+ private readonly ISnowflakeGenerator $ snowflakeGenerator ,
2830 ) {
2931 }
3032
3133 private function loadRegistry (): void {
32- if ($ this ->register !== []) {
34+ if ($ this ->registry !== []) {
3335 return ;
3436 }
3537 $ qb = $ this ->connection ->getQueryBuilder ();
3638 $ result = $ qb ->select ('class_id ' , 'class_name ' )->from (self ::TABLE )->executeQuery ();
3739 foreach ($ result ->iterateAssociative () as $ row ) {
38- $ this ->register [$ row ['class_name ' ]] = (int )$ row ['class_id ' ];
40+ $ this ->registry [$ row ['class_name ' ]] = (string )$ row ['class_id ' ];
3941 }
4042 }
4143
4244 /**
4345 * Resolve current ID or generates a new one
4446 */
45- public function getId (string $ className ): int {
47+ public function getId (string $ className ): string {
4648 $ this ->loadRegistry ();
47- if (isset ($ this ->register [$ className ])) {
48- return $ this ->register [$ className ];
49+ if (isset ($ this ->registry [$ className ])) {
50+ return $ this ->registry [$ className ];
4951 }
5052
5153 if (!class_exists ($ className )) {
@@ -58,12 +60,18 @@ public function getId(string $className): int {
5860 $ qb = $ this ->connection ->getQueryBuilder ();
5961 $ hashedName = $ this ->hashName ($ className );
6062 try {
63+ $ classId = $ this ->snowflakeGenerator ->nextId ();
6164 $ qb
6265 ->insert (self ::TABLE )
63- ->setValue ('class_name ' , $ qb ->expr ()->literal ($ className ))
64- ->setValue ('class_hash ' , $ qb ->expr ()->literal ($ hashedName ))
66+ ->values ([
67+ 'class_id ' => $ qb ->createNamedParameter ($ classId ),
68+ 'class_name ' => $ qb ->createNamedParameter ($ className ),
69+ 'class_hash ' => $ qb ->createNamedParameter ($ hashedName ),
70+ ])
6571 ->executeStatement ();
66- return $ qb ->getLastInsertId ();
72+ $ this ->registry [$ className ] = $ classId ;
73+
74+ return $ classId ;
6775 } catch (UniqueConstraintViolationException ) {
6876 // Class was probably added by a concurrent process
6977 // Try to load it
@@ -74,16 +82,20 @@ public function getId(string $className): int {
7482 ->andWhere ($ qb ->expr ()->eq ('class_name ' , $ className ))
7583 ->executeQuery ();
7684 if ($ classId = $ result ->fetchOne ()) {
77- return (int )$ classId ;
85+ $ classId = (string )$ classId ;
86+ $ this ->registry [$ className ] = $ classId ;
87+
88+ return $ classId ;
7889 }
7990 }
8091
8192 throw new \Exception ('Fail to retrieve ' . $ className . ' ID ' );
8293 }
8394
84- public function getName (int $ classId ): string {
95+ public function getName (string | int $ classId ): string {
8596 $ this ->loadRegistry ();
86- $ className = array_search ($ classId , $ this ->register , true );
97+ $ classId = (string )$ classId ;
98+ $ className = array_search ($ classId , $ this ->registry , true );
8799 if ($ className === false ) {
88100 throw new InvalidArgumentException ('Class ID ' . $ classId . ' doesn’t match any class name ' );
89101 }
0 commit comments