Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monotonic randomness support #473

Merged
merged 1 commit into from
Aug 25, 2020
Merged

Add monotonic randomness support #473

merged 1 commit into from
Aug 25, 2020

Conversation

ahawker
Copy link
Owner

@ahawker ahawker commented Aug 17, 2020

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.

This implementation should conform to the monotonic implementation found in https://github.com/ulid/spec even though there is discussion/concerns about it. See: ulid/spec#40, ulid/spec#11, ulid/javascript#74

Status: Ready

If merged, this PR fixes #306.

TODO:

  • Add tests for api package
  • Add tests for providers package
  • Add tests for consts module
  • Add tests to assert default/monotonic api modules conform to public interface
  • Fix broken py35 tests
  • Fix linter errors

Usage:

>>> import ulid
>>> ulid.new()
<ULID('01EFZ5YBXG4G619GDRWESKW04C')>
>>> from ulid import monotonic as ulid
>>> ulid.new()
<ULID('01EFZ5YJ74PE8284B5M74CZBVW')>
>>> import time
>>> ts = time.time()
>>> ulid.from_timestamp(ts)
<ULID('01EFZ62V7VTEQR4Q788PSBBQP8')>
>>> ulid.from_timestamp(ts)
<ULID('01EFZ62V7VTEQR4Q788PSBBQP9')>
>>> ulid.from_timestamp(ts)
<ULID('01EFZ62V7VTEQR4Q788PSBBQPA')>
>>> ulid.from_timestamp(ts)
<ULID('01EFZ62V7VTEQR4Q788PSBBQPB')>
>>> ulid.from_timestamp(time.time())
<ULID('01EFZ648ZWCY8FEVYTN1YKBZXG')>

@ahawker ahawker force-pushed the issue/306 branch 2 times, most recently from 1d2b3e3 to 3c64273 Compare August 18, 2020 00:09
@ahawker ahawker force-pushed the issue/306 branch 3 times, most recently from 8f77e0d to 7948ce3 Compare August 25, 2020 01:14
@codecov
Copy link

codecov bot commented Aug 25, 2020

Codecov Report

Merging #473 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #473   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6        13    +7     
  Lines          368       467   +99     
  Branches        77        79    +2     
=========================================
+ Hits           368       467   +99     
Impacted Files Coverage Δ
ulid/api/__init__.py 100.00% <100.00%> (ø)
ulid/api/api.py 100.00% <100.00%> (ø)
ulid/api/default.py 100.00% <100.00%> (ø)
ulid/api/monotonic.py 100.00% <100.00%> (ø)
ulid/consts.py 100.00% <100.00%> (ø)
ulid/hints.py 100.00% <100.00%> (ø)
ulid/providers/__init__.py 100.00% <100.00%> (ø)
ulid/providers/base.py 100.00% <100.00%> (ø)
ulid/providers/default.py 100.00% <100.00%> (ø)
ulid/providers/monotonic.py 100.00% <100.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 888b087...7ce680a. Read the comment docs.

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.
@ahawker ahawker merged commit 7ce680a into master Aug 25, 2020
@personal-probot personal-probot bot deleted the issue/306 branch August 25, 2020 02:47
@ahawker ahawker mentioned this pull request Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Monotonicity
1 participant