|
| 1 | +.. _snowflake_ids: |
| 2 | + |
| 3 | +============= |
| 4 | +Snowflake IDs |
| 5 | +============= |
| 6 | + |
| 7 | +.. versionadded:: 33 |
| 8 | + |
| 9 | +Nextcloud integrates a customized version of Snowflake IDs (https://en.wikipedia.org/wiki/Snowflake_ID). |
| 10 | + |
| 11 | +It allows to generates unique IDs in advance and also contains information about creation: |
| 12 | + - creation time at millisecond precision |
| 13 | + - identifier of server which created the ID. It's usually a hash of server hostname or random if not hostname found. |
| 14 | + - whether the ID was created from CLI or not |
| 15 | + |
| 16 | +Store a Snowflake ID in database |
| 17 | +-------------------------------- |
| 18 | + |
| 19 | +Snowflake IDs are designed to be used as primary key in database. |
| 20 | +They should be stored as ``UNSIGNED BIGINT`` (64 bits integer, always positive) |
| 21 | + |
| 22 | +In Nextcloud migrations it looks like: |
| 23 | + |
| 24 | +.. code-block:: php |
| 25 | +
|
| 26 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 27 | + /** @var ISchemaWrapper $schema */ |
| 28 | + $schema = $schemaClosure(); |
| 29 | +
|
| 30 | + if (!$schema->hasTable('my_table')) { |
| 31 | + $table = $schema->createTable('my_table'); |
| 32 | + $table->addColumn( |
| 33 | + 'id', |
| 34 | + Types::BIGINT, |
| 35 | + ['notnull' => true, 'unsigned' => true] |
| 36 | + ); |
| 37 | + $table->setPrimaryKey(['id']); |
| 38 | +
|
| 39 | + // TODO Add other fields |
| 40 | + } |
| 41 | +
|
| 42 | +
|
| 43 | +Generate a Snowflake ID |
| 44 | +----------------------- |
| 45 | + |
| 46 | +To generate a new ID, call ``nextId`` function on the generator: |
| 47 | + |
| 48 | +.. code-block:: php |
| 49 | +
|
| 50 | + <?php |
| 51 | + declare(strict_types=1); |
| 52 | +
|
| 53 | + namespace OCA\MyApp; |
| 54 | +
|
| 55 | + use OCP\Snowflake\IGenerator; |
| 56 | +
|
| 57 | + class MyObjectFactory { |
| 58 | + public function __construct( |
| 59 | + private readonly IGenerator $generator, |
| 60 | + ) { |
| 61 | + // TODO Add your implementation |
| 62 | + } |
| 63 | +
|
| 64 | + public function create(): MyObject { |
| 65 | + /** @var string $id */ |
| 66 | + $id = $this->generator->nextId(); |
| 67 | +
|
| 68 | + // TODO Create other properties and insert into database |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | +
|
| 73 | +Decode a Snowflake ID |
| 74 | +--------------------- |
| 75 | + |
| 76 | +IDs can be decoded with ``occ decode-snowflake <id>`` command. |
| 77 | + |
| 78 | +It’s also possible to decode IDs in your code, for example to get creation time of your object: |
| 79 | + |
| 80 | + |
| 81 | +.. code-block:: php |
| 82 | +
|
| 83 | + <?php |
| 84 | + declare(strict_types=1); |
| 85 | +
|
| 86 | + namespace OCA\MyApp; |
| 87 | +
|
| 88 | + use DateTimeImmutable |
| 89 | + use OCP\Snowflake\IDecoder; |
| 90 | +
|
| 91 | + class MyObject { |
| 92 | + private string $id; |
| 93 | +
|
| 94 | + public function __construct( |
| 95 | + private readonly IDecoder $decoder, |
| 96 | + ) { |
| 97 | + // TODO Add your implementation |
| 98 | + } |
| 99 | +
|
| 100 | + public function createdAt(): DateTimeImmutable { |
| 101 | + return $this->decoder->decode($this->id)['createdAt']; |
| 102 | + } |
| 103 | + } |
0 commit comments