Class TsidFactory


  • public final class TsidFactory
    extends Object
    A factory that actually generates Time-Sorted Unique Identifiers (TSID).

    This factory is used by the TsidCreator singleton.

    Most people just need TsidCreator. However, you can use this class if you need to make some customizations, for example changing the default SecureRandom random generator to a faster pseudo-random generator.

    If a system property "tsidcreator.node" or environment variable "TSIDCREATOR_NODE" is defined, its value is utilized as node identifier. One of them should be defined to embed a machine ID in the generated TSID in order to avoid TSID collisions. Using that property or variable is highly recommended. If no property or variable is defined, a random node ID is generated at initialization.

    If a system property "tsidcreator.node.count" or environment variable "TSIDCREATOR_NODE_COUNT" is defined, its value is utilized by the constructors of this class to adjust the amount of bits needed to embed the node ID. For example, if the number 50 is given, the node bit amount is adjusted to 6, which is the minimum number of bits to accommodate 50 nodes. If no property or variable is defined, the number of bits reserved for node ID is set to 10, which can accommodate 1024 nodes.

    This class should be used as a singleton. Make sure that you create and reuse a single instance of TsidFactory per node in your distributed system.

    • Constructor Detail

      • TsidFactory

        public TsidFactory()
        It builds a new factory.

        The node identifier provided by the "tsidcreator.node" system property or the "TSIDCREATOR_NODE" environment variable is embedded in the generated TSIDs in order to avoid collisions. It is highly recommended defining that property or variable. Otherwise the node identifier will be randomly chosen.

        If a system property "tsidcreator.node.count" or environment variable "TSIDCREATOR_NODE_COUNT" is defined, its value is used to adjust the node bits amount.

      • TsidFactory

        public TsidFactory​(int node)
        It builds a new factory.

        The node identifier provided by parameter is embedded in the generated TSIDs in order to avoid collisions.

        If a system property "tsidcreator.node.count" or environment variable "TSIDCREATOR_NODE_COUNT" is defined, its value is used to adjust the node bits amount.

        Parameters:
        node - the node identifier
    • Method Detail

      • newInstance256

        public static TsidFactory newInstance256()
        Returns a new factory for up to 256 nodes and 16384 ID/ms.
        Returns:
        TsidFactory
      • newInstance256

        public static TsidFactory newInstance256​(int node)
        Returns a new factory for up to 256 nodes and 16384 ID/ms.
        Parameters:
        node - the node identifier
        Returns:
        TsidFactory
      • newInstance1024

        public static TsidFactory newInstance1024()
        Returns a new factory for up to 1024 nodes and 4096 ID/ms. It is equivalent to new TsidFactory().
        Returns:
        TsidFactory
      • newInstance1024

        public static TsidFactory newInstance1024​(int node)
        Returns a new factory for up to 1024 nodes and 4096 ID/ms. It is equivalent to new TsidFactory(int).
        Parameters:
        node - the node identifier
        Returns:
        TsidFactory
      • newInstance4096

        public static TsidFactory newInstance4096()
        Returns a new factory for up to 4096 nodes and 1024 ID/ms.
        Returns:
        TsidFactory
      • newInstance4096

        public static TsidFactory newInstance4096​(int node)
        Returns a new factory for up to 4096 nodes and 1024 ID/ms.
        Parameters:
        node - the node identifier
        Returns:
        TsidFactory
      • create

        public Tsid create()
        Returns a TSID.
        Returns:
        a TSID.