Skip to content

Commit 15b82da

Browse files
authored
Merge pull request #13811 from nextcloud/feat/snowflakeIds
2 parents 0172563 + f5fb92b commit 15b82da

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

.codespellignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
SME
2-
te
1+
hasTable
32
jus
43
nin
4+
SME
5+
te

admin_manual/occ_command.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ occ command Directory
4646
* :ref:`system_tags_commands_label`
4747
* :ref:`antivirus_commands_label`
4848
* :ref:`setupchecks_commands_label`
49+
* :ref:`snowflakes_commands_label`
4950
* :ref:`share_operations_label`
5051
* `Debugging`_
5152

@@ -2273,6 +2274,28 @@ Example output::
22732274
✓ Internet connectivity
22742275
...
22752276

2277+
.. _snowflakes_commands_label:
2278+
2279+
Snowflake IDs
2280+
-------------
2281+
2282+
You can decode Snowflake IDs with occ::
2283+
2284+
sudo -E -u www-data php occ decode-snowflake <snowflake_id>
2285+
2286+
Example output::
2287+
2288+
+--------------------+-------------------------+
2289+
| Snowflake ID | 6768789079123765868 |
2290+
| Seconds | 1575981518 |
2291+
| Milliseconds | 50 |
2292+
| Created from CLI | no |
2293+
| Server ID | 441 |
2294+
| Sequence ID | 2668 |
2295+
| Creation timestamp | 3335258318.050 |
2296+
| Creation date | 2075-09-09 12:38:38.050 |
2297+
+--------------------+-------------------------+
2298+
22762299
.. _share_operations_label:
22772300

22782301
Share operations

developer_manual/digging_deeper/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Digging deeper
3838
security
3939
settings
4040
setup_checks
41+
snowflake_ids
4142
speech-to-text
4243
talk
4344
task_processing
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)