-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move unique_id functionality to base62 module
- Loading branch information
1 parent
8d87cd2
commit 956ffd9
Showing
5 changed files
with
76 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule EventBus.Util.Base62 do | ||
@moduledoc false | ||
|
||
@mapping '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' | ||
|
||
@doc """ | ||
Generates partially sequential, base62 unique identifier | ||
""" | ||
@spec unique_id() :: String.t() | ||
def unique_id do | ||
now() <> node_id() <> random(4, 14_776_336) | ||
end | ||
|
||
@doc """ | ||
Converts given integer to base62 | ||
""" | ||
@spec encode(integer()) :: String.t() | ||
def encode(num) when num < 62 do | ||
<< Enum.at(@mapping, num) >> | ||
end | ||
|
||
def encode(num) do | ||
encode(div(num, 62)) <> encode(rem(num, 62)) | ||
end | ||
|
||
# Generates random base62 string with crypto:strong_rand_bytes | ||
defp random(size, max) do | ||
size | ||
|> :crypto.strong_rand_bytes() | ||
|> :crypto.bytes_to_integer() | ||
|> rem(max) | ||
|> encode() | ||
|> String.pad_leading(size, "0") | ||
end | ||
|
||
# Current time (microseconds) encoded in base62 | ||
defp now do | ||
encode(System.os_time(:microseconds)) | ||
end | ||
|
||
# Assigns a random node_id on first call | ||
defp node_id do | ||
case Application.get_env(:event_bus, :node_id) do | ||
nil -> save_node_id(random(3, 238_328)) | ||
nid -> nid | ||
end | ||
end | ||
|
||
defp save_node_id(node_id) do | ||
Application.put_env(:event_bus, :node_id, node_id, persistent: true) | ||
node_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule EventBus.Util.Base62Test do | ||
use ExUnit.Case | ||
alias EventBus.Util.Base62 | ||
|
||
test ".encode" do | ||
assert "0" == Base62.encode(0) | ||
assert "z" == Base62.encode(61) | ||
assert "10" == Base62.encode(62) | ||
assert "1p0uwg6tOzJ" == Base62.encode(1529891323138833953) | ||
end | ||
|
||
test ".unique_id" do | ||
refute Base62.unique_id() == Base62.unique_id() | ||
end | ||
|
||
test ".unique_id length" do | ||
assert 16 == String.length(Base62.unique_id()) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
defmodule EventBus.Util.StringTest do | ||
use ExUnit.Case | ||
alias EventBus.Util.String, as: StringUtil | ||
|
||
test "generates unique_id" do | ||
refute StringUtil.unique_id() == StringUtil.unique_id() | ||
end | ||
end |