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

implement the gossip api with Spear.cluster_info/2 #21

Merged
merged 4 commits into from
Apr 20, 2021
Merged

Conversation

the-mikedavis
Copy link
Collaborator

dunno exactly how one uses this yet but seems useful

connects #7

@the-mikedavis the-mikedavis self-assigned this Apr 19, 2021
Comment on lines 28 to 78
@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
Copy link
Collaborator Author

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

@the-mikedavis the-mikedavis requested a review from a team April 19, 2021 21:53
Comment on lines +96 to +97
# 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."
Copy link
Member

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 :)

@the-mikedavis the-mikedavis merged commit 5e88fea into main Apr 20, 2021
@the-mikedavis the-mikedavis deleted the gossip branch April 20, 2021 18:41
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.

2 participants