Skip to content

Commit f19d62a

Browse files
committed
feat(snowflake): Use string as type for snowflake ids
And use gmp to do all the arithemetics as that support large numbers. Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
1 parent 66cca05 commit f19d62a

File tree

15 files changed

+438
-243
lines changed

15 files changed

+438
-243
lines changed

.github/workflows/phpunit-32bits.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
- 'version.php'
99
- '.github/workflows/phpunit-32bits.yml'
1010
- 'tests/phpunit-autotest.xml'
11+
- 'lib/private/DB/Snowflake/*'
1112
workflow_dispatch:
1213
schedule:
1314
- cron: "15 1 * * 1-6"

core/Migrations/Version33000Date20250819110529.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
3434
$table->addColumn('id', Types::BIGINT, ['notnull' => true, 'length' => 20, 'unsigned' => true]);
3535
$table->addColumn('bucket_name', Types::STRING, ['notnull' => true, 'length' => 40]);
3636
$table->addColumn('object_store_name', Types::STRING, ['notnull' => true, 'length' => 40]);
37+
$table->addUniqueIndex(['bucket_name', 'object_store_name']);
3738
$table->setPrimaryKey(['id']);
3839
}
3940

lib/composer/composer/autoload_classmap.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
'OCP\\AppFramework\\Db\\DoesNotExistException' => $baseDir . '/lib/public/AppFramework/Db/DoesNotExistException.php',
7777
'OCP\\AppFramework\\Db\\Entity' => $baseDir . '/lib/public/AppFramework/Db/Entity.php',
7878
'OCP\\AppFramework\\Db\\IMapperException' => $baseDir . '/lib/public/AppFramework/Db/IMapperException.php',
79-
'OCP\\AppFramework\\Db\\ISnowflake' => $baseDir . '/lib/public/AppFramework/Db/ISnowflake.php',
8079
'OCP\\AppFramework\\Db\\MultipleObjectsReturnedException' => $baseDir . '/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php',
8180
'OCP\\AppFramework\\Db\\QBMapper' => $baseDir . '/lib/public/AppFramework/Db/QBMapper.php',
8281
'OCP\\AppFramework\\Db\\TTransactional' => $baseDir . '/lib/public/AppFramework/Db/TTransactional.php',
@@ -312,6 +311,7 @@
312311
'OCP\\DB\\IPreparedStatement' => $baseDir . '/lib/public/DB/IPreparedStatement.php',
313312
'OCP\\DB\\IResult' => $baseDir . '/lib/public/DB/IResult.php',
314313
'OCP\\DB\\ISchemaWrapper' => $baseDir . '/lib/public/DB/ISchemaWrapper.php',
314+
'OCP\\DB\\ISnowflake' => $baseDir . '/lib/public/DB/ISnowflake.php',
315315
'OCP\\DB\\QueryBuilder\\ICompositeExpression' => $baseDir . '/lib/public/DB/QueryBuilder/ICompositeExpression.php',
316316
'OCP\\DB\\QueryBuilder\\IExpressionBuilder' => $baseDir . '/lib/public/DB/QueryBuilder/IExpressionBuilder.php',
317317
'OCP\\DB\\QueryBuilder\\IFunctionBuilder' => $baseDir . '/lib/public/DB/QueryBuilder/IFunctionBuilder.php',
@@ -1038,9 +1038,6 @@
10381038
'OC\\AppFramework\\Bootstrap\\ServiceAliasRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceAliasRegistration.php',
10391039
'OC\\AppFramework\\Bootstrap\\ServiceFactoryRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceFactoryRegistration.php',
10401040
'OC\\AppFramework\\Bootstrap\\ServiceRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceRegistration.php',
1041-
'OC\\AppFramework\\Db\\NextcloudSequenceResolver' => $baseDir . '/lib/private/AppFramework/Db/NextcloudSequenceResolver.php',
1042-
'OC\\AppFramework\\Db\\Snowflake' => $baseDir . '/lib/private/AppFramework/Db/Snowflake.php',
1043-
'OC\\AppFramework\\Db\\SnowflakeGenerator' => $baseDir . '/lib/private/AppFramework/Db/SnowflakeGenerator.php',
10441041
'OC\\AppFramework\\DependencyInjection\\DIContainer' => $baseDir . '/lib/private/AppFramework/DependencyInjection/DIContainer.php',
10451042
'OC\\AppFramework\\Http' => $baseDir . '/lib/private/AppFramework/Http.php',
10461043
'OC\\AppFramework\\Http\\Dispatcher' => $baseDir . '/lib/private/AppFramework/Http/Dispatcher.php',
@@ -1596,6 +1593,9 @@
15961593
'OC\\DB\\SQLiteSessionInit' => $baseDir . '/lib/private/DB/SQLiteSessionInit.php',
15971594
'OC\\DB\\SchemaWrapper' => $baseDir . '/lib/private/DB/SchemaWrapper.php',
15981595
'OC\\DB\\SetTransactionIsolationLevel' => $baseDir . '/lib/private/DB/SetTransactionIsolationLevel.php',
1596+
'OC\\DB\\Snowflake\\NextcloudSequenceResolver' => $baseDir . '/lib/private/DB/Snowflake/NextcloudSequenceResolver.php',
1597+
'OC\\DB\\Snowflake\\Snowflake' => $baseDir . '/lib/private/DB/Snowflake/Snowflake.php',
1598+
'OC\\DB\\Snowflake\\SnowflakeGenerator' => $baseDir . '/lib/private/DB/Snowflake/SnowflakeGenerator.php',
15991599
'OC\\Dashboard\\Manager' => $baseDir . '/lib/private/Dashboard/Manager.php',
16001600
'OC\\DatabaseException' => $baseDir . '/lib/private/DatabaseException.php',
16011601
'OC\\DatabaseSetupException' => $baseDir . '/lib/private/DatabaseSetupException.php',

lib/composer/composer/autoload_static.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
117117
'OCP\\AppFramework\\Db\\DoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/DoesNotExistException.php',
118118
'OCP\\AppFramework\\Db\\Entity' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/Entity.php',
119119
'OCP\\AppFramework\\Db\\IMapperException' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/IMapperException.php',
120-
'OCP\\AppFramework\\Db\\ISnowflake' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/ISnowflake.php',
121120
'OCP\\AppFramework\\Db\\MultipleObjectsReturnedException' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php',
122121
'OCP\\AppFramework\\Db\\QBMapper' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/QBMapper.php',
123122
'OCP\\AppFramework\\Db\\TTransactional' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/TTransactional.php',
@@ -353,6 +352,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
353352
'OCP\\DB\\IPreparedStatement' => __DIR__ . '/../../..' . '/lib/public/DB/IPreparedStatement.php',
354353
'OCP\\DB\\IResult' => __DIR__ . '/../../..' . '/lib/public/DB/IResult.php',
355354
'OCP\\DB\\ISchemaWrapper' => __DIR__ . '/../../..' . '/lib/public/DB/ISchemaWrapper.php',
355+
'OCP\\DB\\ISnowflake' => __DIR__ . '/../../..' . '/lib/public/DB/ISnowflake.php',
356356
'OCP\\DB\\QueryBuilder\\ICompositeExpression' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/ICompositeExpression.php',
357357
'OCP\\DB\\QueryBuilder\\IExpressionBuilder' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/IExpressionBuilder.php',
358358
'OCP\\DB\\QueryBuilder\\IFunctionBuilder' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/IFunctionBuilder.php',
@@ -1079,9 +1079,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
10791079
'OC\\AppFramework\\Bootstrap\\ServiceAliasRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceAliasRegistration.php',
10801080
'OC\\AppFramework\\Bootstrap\\ServiceFactoryRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceFactoryRegistration.php',
10811081
'OC\\AppFramework\\Bootstrap\\ServiceRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceRegistration.php',
1082-
'OC\\AppFramework\\Db\\NextcloudSequenceResolver' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Db/NextcloudSequenceResolver.php',
1083-
'OC\\AppFramework\\Db\\Snowflake' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Db/Snowflake.php',
1084-
'OC\\AppFramework\\Db\\SnowflakeGenerator' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Db/SnowflakeGenerator.php',
10851082
'OC\\AppFramework\\DependencyInjection\\DIContainer' => __DIR__ . '/../../..' . '/lib/private/AppFramework/DependencyInjection/DIContainer.php',
10861083
'OC\\AppFramework\\Http' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http.php',
10871084
'OC\\AppFramework\\Http\\Dispatcher' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/Dispatcher.php',
@@ -1637,6 +1634,9 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
16371634
'OC\\DB\\SQLiteSessionInit' => __DIR__ . '/../../..' . '/lib/private/DB/SQLiteSessionInit.php',
16381635
'OC\\DB\\SchemaWrapper' => __DIR__ . '/../../..' . '/lib/private/DB/SchemaWrapper.php',
16391636
'OC\\DB\\SetTransactionIsolationLevel' => __DIR__ . '/../../..' . '/lib/private/DB/SetTransactionIsolationLevel.php',
1637+
'OC\\DB\\Snowflake\\NextcloudSequenceResolver' => __DIR__ . '/../../..' . '/lib/private/DB/Snowflake/NextcloudSequenceResolver.php',
1638+
'OC\\DB\\Snowflake\\Snowflake' => __DIR__ . '/../../..' . '/lib/private/DB/Snowflake/Snowflake.php',
1639+
'OC\\DB\\Snowflake\\SnowflakeGenerator' => __DIR__ . '/../../..' . '/lib/private/DB/Snowflake/SnowflakeGenerator.php',
16401640
'OC\\Dashboard\\Manager' => __DIR__ . '/../../..' . '/lib/private/Dashboard/Manager.php',
16411641
'OC\\DatabaseException' => __DIR__ . '/../../..' . '/lib/private/DatabaseException.php',
16421642
'OC\\DatabaseSetupException' => __DIR__ . '/../../..' . '/lib/private/DatabaseSetupException.php',

lib/private/AppFramework/Db/SnowflakeGenerator.php

Lines changed: 0 additions & 164 deletions
This file was deleted.

lib/private/AppFramework/Db/NextcloudSequenceResolver.php renamed to lib/private/DB/Snowflake/NextcloudSequenceResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* SPDX-License-Identifier: AGPL-3.0-or-later
99
*/
1010

11-
namespace OC\AppFramework\Db;
11+
namespace OC\DB\Snowflake;
1212

1313
use OCP\ICacheFactory;
1414
use OCP\IMemcache;
@@ -30,7 +30,7 @@ public function isAvailable(): bool {
3030
return $this->localCache instanceof IMemcache;
3131
}
3232

33-
public function sequence(int $currentTime): int {
33+
public function sequence(float $currentTime): int {
3434
if ($this->localCache->add((string)$currentTime, 1, 10)) {
3535
return 0;
3636
}

lib/private/AppFramework/Db/Snowflake.php renamed to lib/private/DB/Snowflake/Snowflake.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
* SPDX-License-Identifier: MIT
1111
*/
1212

13-
namespace OC\AppFramework\Db;
13+
namespace OC\DB\Snowflake;
1414

15-
use OCP\AppFramework\Db\ISnowflake;
15+
use OCP\DB\ISnowflake;
1616

1717
class Snowflake implements ISnowflake {
1818

@@ -21,15 +21,16 @@ class Snowflake implements ISnowflake {
2121
*/
2222
private static array $generators = [];
2323

24-
public static function getGenerator(int $datacenter, int $workerId, NextcloudSequenceResolver $resolver): SnowflakeGenerator {
24+
public static function getGenerator(int $datacenter, int $workerId, bool $isCLI, NextcloudSequenceResolver $resolver): SnowflakeGenerator {
2525
$key = "{$datacenter}-{$workerId}";
2626
if (!isset(self::$generators[$key])) {
2727
$generator = new SnowflakeGenerator(
2828
$datacenter,
2929
$workerId,
3030
$resolver,
31+
$isCLI,
3132
);
32-
$generator->setStartTimeStamp(strtotime('2025-01-01') * 1000);
33+
$generator->setStartTimeStamp((float)strtotime('2025-01-01') * 1000);
3334
self::$generators[$key] = $generator;
3435
}
3536
return self::$generators[$key];
@@ -44,19 +45,17 @@ public static function generateWorkerId(string $hostname, int $maxWorkerId = 31)
4445

4546
public function __construct(
4647
NextcloudSequenceResolver $nextcloudSequenceResolver,
48+
bool $isCLI,
4749
) {
4850
$this->generator = static::getGenerator(
49-
-1, // ATM set randomely
51+
-1, // ATM set randomly
5052
self::generateWorkerId(gethostname()),
53+
$isCLI,
5154
$nextcloudSequenceResolver
5255
);
5356
}
5457

55-
public function id(): string {
56-
return $this->generator->id();
57-
}
58-
59-
public function parseId(string $id, bool $transform = false): array {
60-
return SnowflakeGenerator::parseId($id, $transform);
58+
public function nextId(): string {
59+
return $this->generator->nextId();
6160
}
6261
}

0 commit comments

Comments
 (0)