-
Notifications
You must be signed in to change notification settings - Fork 14
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
implement the gossip api with Spear.cluster_info/2 #21
Conversation
lib/spear/uuid.ex
Outdated
@doc """ | ||
Reproduces a UUID from a pair of most and least-significant bits | ||
|
||
This function is an adaptation of the OpenJDK 8 source for turning | ||
the `java.util.UUID` into a typical string The java source code is as | ||
follows: | ||
|
||
```java | ||
public String toString() { | ||
return (digits(mostSigBits >> 32, 8) + "-" + | ||
digits(mostSigBits >> 16, 4) + "-" + | ||
digits(mostSigBits, 4) + "-" + | ||
digits(leastSigBits >> 48, 4) + "-" + | ||
digits(leastSigBits, 12)); | ||
} | ||
|
||
private static String digits(long val, int digits) { | ||
long hi = 1L << (digits * 4); | ||
return Long.toHexString(hi | (val & (hi - 1))).substring(1); | ||
} | ||
``` | ||
|
||
## Examples | ||
|
||
iex> Spear.Uuid.from_structured(-1466833724069688543, -8694761462116790879) | ||
"eba4c27f-e443-4b21-8756-00845bc5cda1" | ||
""" | ||
@doc since: "0.5.0" | ||
@spec from_structured(integer(), integer()) :: String.t() | ||
def from_structured(most_sig_bits, least_sig_bits) do | ||
[ | ||
digits(most_sig_bits >>> 32, 8), | ||
digits(most_sig_bits >>> 16, 4), | ||
digits(most_sig_bits, 4), | ||
digits(least_sig_bits >>> 48, 4), | ||
digits(least_sig_bits, 12) | ||
] | ||
|> Enum.intersperse(?-) | ||
|> IO.iodata_to_binary() | ||
end | ||
|
||
defp digits(val, digits) do | ||
hi = 1 <<< (digits * 4) | ||
|
||
<<_drop_first_byte, rest::binary>> = | ||
(hi ||| (val &&& hi - 1)) | ||
|> Integer.to_string(16) | ||
|> String.downcase() | ||
|
||
rest | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a nice big time 🐉 thread on the stand board for this thing
apparently you can describe a uuid with just two 64bit longs which is nice because a string uuid takes up many more bits (288 I think?)
it's used by object oriented languages for their UUID objects so it's not surprising that I didn't find an implementation for this scanning over the most popular erlang & elixir uuid generators
had to dig into the java source code and translate
# I'll say "this code is not novel and probably belongs to Ecto" but I've | ||
# heard that laywers generally don't like the term "probably." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "probably"s fine if it only ends up as a civil lawsuit :)
dunno exactly how one uses this yet but seems useful
connects #7