#1 2017-09-04 01:08:32

linbren
Member
Registered: 2017-08-11
Posts: 42

any necessary to implement Snowflake-IdWorker for global unique ID ?

AS title .


Windows 7 64bit. Delphi XE10.2 Professional.

Offline

#2 2017-09-04 08:33:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

Check SynCommon's TSynUniqueIdentifier.
They are 64-bit integer unique identifier, globally on a system (up to 32K nodes), without the need of any centralized server.

You have other features, like ID obfuscation into URI, or built-in storage of the ID timestamp creation.

Offline

#3 2017-09-04 12:39:53

emk
Member
Registered: 2013-10-24
Posts: 96

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

Means I can generate ID (64bit) on client with 100% assurance that will not collide with another ID?

Offline

#4 2017-09-04 16:28:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

The algorithm used ensure that.
If each TSynUniqueIdentifierGenerator has a single ProcessID value (set e.g. in the settings file of the node), then you can be sure that there won't be any collision.
Another benefit is that it is time-based, so it will be globally increasing (which eases a lot indexation and balancing), and you can retrieve the creation timestamp from the ID (no need to have the time stored in another field).

Offline

#5 2017-09-05 00:42:25

linbren
Member
Registered: 2017-08-11
Posts: 42

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

could be work like this :
        IdWorker worker1 = new IdWorker(1);  //process(thread) 1
        System.out.println(worker1.nextId());

        IdWorker worker2 = new IdWorker(2);  //process(thread) 2
        System.out.println(worker2.nextId());


Windows 7 64bit. Delphi XE10.2 Professional.

Offline

#6 2017-09-05 07:00:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

Yes, of course.
But in the same process, the TSynUniqueIdentifierGenerator is thread-safe, so you can use it on all threads of the same process.
No need to create several TSynUniqueIdentifierGenerator with their own process ID in the same process.

Offline

#7 2017-09-06 14:10:40

emk
Member
Registered: 2013-10-24
Posts: 96

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

ab wrote:

The algorithm used ensure that.
If each TSynUniqueIdentifierGenerator has a single ProcessID value (set e.g. in the settings file of the node), then you can be sure that there won't be any collision.
Another benefit is that it is time-based, so it will be globally increasing (which eases a lot indexation and balancing), and you can retrieve the creation timestamp from the ID (no need to have the time stored in another field).

I thought that ProcessID = Windows ProcessID or something like this. I get it now, but needs that every node to be registered and get a unique ProcessID before can post to server. It not a big deal. Also it must keep track of many instances of the client to have different ProcessID.
But I think it's not safe enough.. I saw many times (for different reasons) that client time is set back manually for let's say 2 hours, so with a "little luck" the same time can occur again in the same ProcessID.

Offline

#8 2017-09-06 14:45:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

Time is indeed to be taken on the server side, where it is supposed not to have such unexpected time changes.

Offline

#9 2017-09-07 07:33:02

linbren
Member
Registered: 2017-08-11
Posts: 42

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

generate 100000000 ids  cost => Gettickcount: 5320 ms.
can't not believe that fast .
--
it got 19 digits ,like 3231448769769799708 . a little too long;


Windows 7 64bit. Delphi XE10.2 Professional.

Offline

#10 2017-09-07 09:13:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

Do you mean it is fast, or slow?
I believe it is fast. And thread-safe. wink

19 digits is in fact a 63-bit number, so that we won't suffer for the value sign (older Delphi doesn't support properly unsigned 64-bit QWord).
Why is it "too long" for you? If it is for JavaScript process for instance (which only handles 53-bit for integers), you can just convert it into an hexadecimal string.

Offline

#11 2017-09-08 00:58:16

linbren
Member
Registered: 2017-08-11
Posts: 42

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

it is fast for sure .
"too long" , the reason is for cross language  as you wroted.

hexadecimal  is a string can not be saved as int in database.


Windows 7 64bit. Delphi XE10.2 Professional.

Offline

#12 2017-09-08 18:16:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: any necessary to implement Snowflake-IdWorker for global unique ID ?

The database is on the mORMot server, so you can save it as integer.
wink

Offline

Board footer

Powered by FluxBB