Skip to content

Commit

Permalink
Add monotonic randomness support
Browse files Browse the repository at this point in the history
Doing this required a refactoring of how timestamp/randomness values
were generated. We've broken these into "provider" implementations
with the "default" provider being the implementation that exists today,
e.g. randomness values are random even on identical timestamp values.

A "monotonic" provider has been added which monotonically increments
the first randomness value on timestamp collision until an overflow.

Additionally, the API has been broken out into a subpackage so we can
stay agnostic to the provider and just plug it in. Work has been done
to maintain the existing package interface for backwards compatibility.
  • Loading branch information
ahawker committed Aug 17, 2020
1 parent bcbbe28 commit 1d2b3e3
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 281 deletions.
7 changes: 3 additions & 4 deletions tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ def test_module_has_submodule_interface(submodule):
assert hasattr(mod, i)


def test_module_exposes_api_and_ulid_interfaces_via_all():
def test_module_exposes_api_interfaces_via_all():
"""
Assert that :mod:`~ulid` exposes the :attr:`~ulid.api.__all__` and :attr:`~ulid.ulid.__all__`
attributes in its public interface.
Assert that :mod:`~ulid` exposes the :attr:`~ulid.api.__all__` attributes in its public interface.
"""
assert mod.__all__ == api.__all__ + ulid.__all__
assert mod.__all__ == api.__all__
40 changes: 20 additions & 20 deletions ulid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
:copyright: (c) 2017 Andrew Hawker.
:license: Apache 2.0, see LICENSE for more details.
"""
from . import api, ulid
from .api import default, monotonic

create = api.create
from_bytes = api.from_bytes
from_int = api.from_int
from_randomness = api.from_randomness
from_str = api.from_str
from_timestamp = api.from_timestamp
from_uuid = api.from_uuid
new = api.new
parse = api.parse
create = default.create
from_bytes = default.from_bytes
from_int = default.from_int
from_randomness = default.from_randomness
from_str = default.from_str
from_timestamp = default.from_timestamp
from_uuid = default.from_uuid
new = default.new
parse = default.parse

MIN_TIMESTAMP = api.MIN_TIMESTAMP
MAX_TIMESTAMP = api.MAX_TIMESTAMP
MIN_RANDOMNESS = api.MIN_RANDOMNESS
MAX_RANDOMNESS = api.MAX_RANDOMNESS
MIN_ULID = api.MIN_ULID
MAX_ULID = api.MAX_ULID
MIN_TIMESTAMP = default.MIN_TIMESTAMP
MAX_TIMESTAMP = default.MAX_TIMESTAMP
MIN_RANDOMNESS = default.MIN_RANDOMNESS
MAX_RANDOMNESS = default.MAX_RANDOMNESS
MIN_ULID = default.MIN_ULID
MAX_ULID = default.MAX_ULID

Timestamp = ulid.Timestamp
Randomness = ulid.Randomness
ULID = ulid.ULID
Timestamp = default.Timestamp
Randomness = default.Randomness
ULID = default.ULID

__all__ = api.__all__ + ulid.__all__
__all__ = default.__all__

__version__ = '0.2.0'
255 changes: 0 additions & 255 deletions ulid/api.py

This file was deleted.

31 changes: 31 additions & 0 deletions ulid/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
ulid/api
~~~~~~~~
Defines the public API of the `ulid` package.
"""
from .. import consts, ulid
from . import default

create = default.create
from_bytes = default.from_bytes
from_int = default.from_int
from_randomness = default.from_randomness
from_str = default.from_str
from_timestamp = default.from_timestamp
from_uuid = default.from_uuid
new = default.new
parse = default.parse

MIN_TIMESTAMP = consts.MIN_TIMESTAMP
MAX_TIMESTAMP = consts.MAX_TIMESTAMP
MIN_RANDOMNESS = consts.MIN_RANDOMNESS
MAX_RANDOMNESS = consts.MAX_RANDOMNESS
MIN_ULID = consts.MIN_ULID
MAX_ULID = consts.MAX_ULID

Timestamp = ulid.Timestamp
Randomness = ulid.Randomness
ULID = ulid.ULID

__all__ = default.__all__
Loading

0 comments on commit 1d2b3e3

Please sign in to comment.