Skip to content

Commit

Permalink
exract just the node id as hex
Browse files Browse the repository at this point in the history
  • Loading branch information
copiousfreetime committed Feb 17, 2017
1 parent 1b0f181 commit 6a0d45b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/torid/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ def to_s
"%08x-%04x-%04x-%02x%02x-%s" % elements
end

# Public: Return the hexidcimal UUID string representation of just the
# node_id. This is just the last 2 parts
def node_id_s
node_bytes = [ @node_id >> 32, @node_id & 0xFFFF_FFFF].pack("NN")
elements = node_bytes.unpack("CCa6")
node = elements[-1].unpack('C*')
elements[-1] = '%02x%02x%02x%02x%02x%02x' % node
"%02x%02x-%s" % elements
end

# Public: Compare the equality of UUID's
#
# Examples
Expand Down
6 changes: 6 additions & 0 deletions test/test_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def setup
@bytes = [ @timestamp >> 32, @timestamp & 0xFFFF_FFFF,
@node_id >> 32, @node_id & 0XFFFF_FFFF ].pack("NNNN")
@guid = "0004fd7d-f50d-e22e-0000-00000000002a"
@node_id_s = "0000-00000000002a"
end

def test_uuid_regex_matches
Expand Down Expand Up @@ -49,6 +50,11 @@ def test_extracts_node_id_from_bytes
assert_equal( @node_id , uuid.node_id )
end

def test_extracts_node_id_as_hex
uuid = ::Torid::UUID.from( @bytes)
assert_equal( @node_id_s, uuid.node_id_s )
end

def test_round_trips_uuid_string
uuid = ::Torid::UUID.from( @guid )
assert_equal( uuid.to_s, uuid.to_s )
Expand Down

0 comments on commit 6a0d45b

Please sign in to comment.