From 7df88df4b533c193f029834541be16ff2e4b75f5 Mon Sep 17 00:00:00 2001 From: Tyler Hobbs Date: Mon, 16 Apr 2012 11:33:47 -0500 Subject: [PATCH] Match C* sorting of TimeUUIDs when timestamps tie Cassandra uses a signed byte array comparison to break ties when two UUIDs have matching timestamp components. The slice end templates we use to make the lowest or highest possible UUID with the same timestamp needed to be changed in order to match this. --- pycassa/util.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pycassa/util.py b/pycassa/util.py index 74a7f88a..7d212eb0 100644 --- a/pycassa/util.py +++ b/pycassa/util.py @@ -79,16 +79,26 @@ def convert_time_to_uuid(time_arg, lowest_val=True, randomize=False): clock_seq_hi_variant = (rand_bits & 0xff00L) / 0x100 # 8 bits, 8 offset node = (rand_bits & 0xffffffffffff0000L) / 0x10000L # 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 + # positive byte range for this particular byte. if lowest_val: # Make the lowest value UUID with the same timestamp - clock_seq_low = 0 & 0xffL - clock_seq_hi_variant = 0 & 0x3fL - node = 0 & 0xffffffffffffL # 48 bits + clock_seq_low = 0x80L + clock_seq_hi_variant = 0 & 0x3fL # The two most significant bits + # will be 0 and 1, no matter what + node = 0x808080808080L # 48 bits else: # Make the highest value UUID with the same timestamp - clock_seq_low = 0xffL - clock_seq_hi_variant = 0x3fL - node = 0xffffffffffffL # 48 bits + clock_seq_low = 0x7fL + clock_seq_hi_variant = 0x3fL # The two most significant bits will + # 0 and 1, no matter what + 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)