Skip to content

Commit

Permalink
Specify correct uuid variant in convert_time_to_uuid
Browse files Browse the repository at this point in the history
Related to pycassa#180
  • Loading branch information
thobbs committed Jan 19, 2013
1 parent 79534e8 commit 242f786
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pycassa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,30 @@ def convert_time_to_uuid(time_arg, lowest_val=True, randomize=False):

if randomize:
rand_bits = random.getrandbits(8 + 8 + 48)
clock_seq_low = rand_bits & 0xffL # 8 bits, no offset
clock_seq_hi_variant = (rand_bits & 0xff00L) / 0x100 # 8 bits, 8 offset
node = (rand_bits & 0xffffffffffff0000L) / 0x10000L # 48 bits, 16 offset
clock_seq_low = rand_bits & 0xffL # 8 bits, no offset
# keep the first two bits as 10 for the uuid variant
clock_seq_hi_variant = 0b10000000 | (0b00111111 & ((rand_bits & 0xff00L) >> 8)) # 8 bits, 8 offset
node = (rand_bits & 0xffffffffffff0000L) >> 16 # 48 bits, 16 offset
else:
# In the event of a timestamp tie, Cassandra compares the two
# byte arrays directly. This is a *signed* comparison of each byte
# in the two arrays. So, we have to make each byte -128 or +127 for
# this to work correctly.
#
# For the clock_seq_hi_variant, we don't get to pick the two most
# significant bits (they're always 01), so we are dealing with a
# significant bits (they're always 10), so we are dealing with a
# positive byte range for this particular byte.
if lowest_val:
# Make the lowest value UUID with the same timestamp
clock_seq_low = 0x80L
clock_seq_hi_variant = 0 & 0x3fL # The two most significant bits
# will be 0 and 1, no matter what
clock_seq_hi_variant = 0 & 0x80L # The two most significant bits
# will be 10 for the variant
node = 0x808080808080L # 48 bits
else:
# Make the highest value UUID with the same timestamp
clock_seq_low = 0x7fL
clock_seq_hi_variant = 0x3fL # The two most significant bits will
# 0 and 1, no matter what
clock_seq_hi_variant = 0xbfL # The two most significant bits will
# 10 for the variant
node = 0x7f7f7f7f7f7fL # 48 bits
return uuid.UUID(fields=(time_low, time_mid, time_hi_version,
clock_seq_hi_variant, clock_seq_low, node), version=1)
Expand Down

0 comments on commit 242f786

Please sign in to comment.