Skip to content
Browse files

Specify correct uuid variant in convert_time_to_uuid

Related to #180
  • Loading branch information...
1 parent 79534e8 commit 242f786ba63e95ed38312f5e3a33a6b10c1d89a0 @thobbs thobbs committed
Showing with 9 additions and 8 deletions.
  1. +9 −8 pycassa/util.py
View
17 pycassa/util.py
@@ -84,9 +84,10 @@ 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
@@ -94,19 +95,19 @@ def convert_time_to_uuid(time_arg, lowest_val=True, randomize=False):
# 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)

0 comments on commit 242f786

Please sign in to comment.
Something went wrong with that request. Please try again.