bigfatsea / SUID

Simple Unique Identifier

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SUID (Simple Unique Identifier)

Simple Unique Identifier, influenced by ObjectId, is a lightweight GUID/UUID implementation.

Spec:

    timestamp + increment + instanceId
    +-----------+--------------+------------+
    | 63...32   | 31...8       | 7...0      |
    +-----------+--------------+------------+
    | timestamp | increment    | instanceId |
    +-----------+--------------+------------+

Exmaple:

	System.out.println("\n--- get id");
	System.out.println(toStringLong(id()));
	System.out.println(toStringLong(id()));

	System.out.println("\n--- get id by app id");
	System.out.println(toStringLong(id(13)));
	System.out.println(toStringLong(id(13)));

	System.out.println("\n--- duplication test ...");
	long id = 0, ts = System.currentTimeMillis();
	Set<Long> set = new HashSet<Long>();
	for (int i = 0; i < 1000000; i++) {
		id = id(i & 1);
		if (!set.add(id))
			System.out.println("!!!!!! duplication found:" + toStringLong(id));
	}
	System.out.println("--- duplication test passed, cost " + (System.currentTimeMillis() - ts) + "ms");

About

Simple Unique Identifier

License:MIT License


Languages

Language:Java 100.0%