<commit_before>package com.zenplanner.sql;

import com.google.common.base.Joiner;
import com.sun.javafx.image.ByteToBytePixelConverter;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.lang3.ArrayUtils;

import java.nio.ByteBuffer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class UuidTest extends TestCase {

    public UuidTest(String testName) {
        super(testName);
    }

    public static Test suite() {
        return new TestSuite(UuidTest.class);
    }

    public void testApp() throws Exception {
        // Generate interesting UUIDs
        List<UUID> testUuids = new ArrayList<>();
        for(int i = 0; i < 16; i++) {
            byte[] bytes = new byte[16];
            for(int b = 0; b < 256; b += 64) {
                bytes[i] = (byte)b;
                UUID original = byteArrayToUuid(bytes);
                testUuids.add(original);
            }
        }

        // Create select statement
        String valueClause = Joiner.on("')),\n(CONVERT(uniqueidentifier, '").join(testUuids);
        String sql = String.format("SELECT \n\t*\nFROM (\n\tVALUES (CONVERT(uniqueidentifier, '%s'))\n\t) tg(UUID)\norder by UUID", valueClause);

        // Get 100 random UUIDs sorted by SQL
        List<UUID> sqlList = new ArrayList<>();
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String conStr = "jdbc:jtds:sqlserver://localhost:1433/ZenPlanner-Development;user=zenwebdev;password=<PASSWORD>!";
        try (Connection con = DriverManager.getConnection(conStr)) {
            try (Statement stmt = con.createStatement()) {
                try (ResultSet rs = stmt.executeQuery(sql)) {
                    while (rs.next()) {
                        byte[] bytes = rs.getBytes(1);
                        String str = rs.getString(1);
                        UUID strUuid = UUID.fromString(str);
                        UUID binUuid = byteArrayToUuid(bytes);
                        Assert.assertEquals(strUuid, binUuid);

                        byte[] out = uuidToByteArray(binUuid);
                        Assert.assertTrue(Arrays.equals(bytes, out));

                        sqlList.add(binUuid);
                    }
                }
            }
        }

        // Clone the list and sort with Java
        List<UUID> javaList = sqlList.stream().sorted(UuidTest::sqlUuidCompare).collect(Collectors.toList());

        // Test for correct order
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < javaList.size(); i++) {
            Comparable sqlUuid = sqlList.get(i);
            Comparable javaUuid = javaList.get(i);
            //Assert.assertEquals(sqlUuid, javaUuid);
            sb.append(String.format("%s %s\n", sqlUuid, javaUuid));
        }
        String res = sb.toString();
        System.out.println(res);
    }

    private static byte[] uuidToByteArray(UUID uuid) {

        // Turn into byte array
        ByteBuffer bb = ByteBuffer.allocate(16);
        bb.putLong(uuid.getMostSignificantBits());
        bb.putLong(uuid.getLeastSignificantBits());
        bb.rewind();
        byte[] bytes = new byte[16];
        bb.get(bytes);

        // Transform
        bb = transformUuid(bytes);

        // Turn into byte array
        bb.get(bytes);

        return bytes;
    }

    private static UUID byteArrayToUuid(byte[] bytes) {
        ByteBuffer bb = transformUuid(bytes);
        long hi = bb.getLong();
        long low = bb.getLong();
        UUID uuid = new UUID(hi, low);
        return uuid;
    }

    private static int sqlUuidCompare(UUID leftUuid, UUID rightUuid) {
        byte[] leftBytes = uuidToByteArray(leftUuid);
        byte[] rightBytes = uuidToByteArray(rightUuid);

        // Compare node
        byte[] leftNode = Arrays.copyOfRange(leftBytes, 10, 16); // node
        byte[] rightNode = Arrays.copyOfRange(rightBytes, 10, 16); // node
        for(int i = 10; i < 16; i++) {
            int val = (leftBytes[i] & 0xFF) - (rightBytes[i] & 0xFF);
            if(val != 0) {
                return val;
            }
        }

        throw new RuntimeException("Comparison beyond node not implemented!");
    }

    private static ByteBuffer transformUuid(byte[] bytes) {
        if (bytes.length != 16) {
            throw new RuntimeException("Invalid UUID bytes!");
        }

        // Get hi
        byte[] time_low = Arrays.copyOfRange(bytes, 0, 4);
        byte[] time_mid = Arrays.copyOfRange(bytes, 4, 6);
        byte[] time_hi = Arrays.copyOfRange(bytes, 6, 8); // actually time_hi + version

        // Get low
        byte[] clock_seq = Arrays.copyOfRange(bytes, 8, 10); // actually variant + clock_seq
        byte[] node = Arrays.copyOfRange(bytes, 10, 16); // node

        // Transform
        ArrayUtils.reverse(time_low);
        ArrayUtils.reverse(time_mid);
        ArrayUtils.reverse(time_hi);

        // Rebuild
        ByteBuffer bb = ByteBuffer.allocate(16);
        bb.put(time_low);
        bb.put(time_mid);
        bb.put(time_hi);
        bb.put(clock_seq);
        bb.put(node);

        // Grab longs
        bb.rewind();
        return bb;
    }

    private static String addDashes(String hex) {
        return String.format("%s-%s-%s-%s-%s",
                hex.substring(0, 8),
                hex.substring(8, 12),
                hex.substring(12, 16),
                hex.substring(16, 20),
                hex.substring(20, 32));
    }
}
<commit_msg>We have a working UUID comparator!<commit_after>package com.zenplanner.sql;

import com.google.common.base.Joiner;
import com.sun.javafx.image.ByteToBytePixelConverter;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.lang3.ArrayUtils;

import java.nio.ByteBuffer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class UuidTest extends TestCase {

    public UuidTest(String testName) {
        super(testName);
    }

    public static Test suite() {
        return new TestSuite(UuidTest.class);
    }

    public void testApp() throws Exception {
        // Generate interesting UUIDs
        List<UUID> testUuids = new ArrayList<>();
        for(int i = 0; i < 16; i++) {
            byte[] bytes = new byte[16];
            for(int b = 0; b < 256; b += 64) {
                bytes[i] = (byte)b;
                UUID original = byteArrayToUuid(bytes);
                testUuids.add(original);
            }
        }

        // Create select statement
        String valueClause = Joiner.on("')),\n(CONVERT(uniqueidentifier, '").join(testUuids);
        String sql = String.format("SELECT \n\t*\nFROM (\n\tVALUES (CONVERT(uniqueidentifier, '%s'))\n\t) tg(UUID)\norder by UUID", valueClause);

        // Get 100 random UUIDs sorted by SQL
        List<UUID> sqlList = new ArrayList<>();
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String conStr = "jdbc:jtds:sqlserver://localhost:1433/ZenPlanner-Development;user=zenwebdev;password=<PASSWORD>!";
        try (Connection con = DriverManager.getConnection(conStr)) {
            try (Statement stmt = con.createStatement()) {
                try (ResultSet rs = stmt.executeQuery(sql)) {
                    while (rs.next()) {
                        byte[] bytes = rs.getBytes(1);
                        String str = rs.getString(1);
                        UUID strUuid = UUID.fromString(str);
                        UUID binUuid = byteArrayToUuid(bytes);
                        Assert.assertEquals(strUuid, binUuid);

                        byte[] out = uuidToByteArray(binUuid);
                        Assert.assertTrue(Arrays.equals(bytes, out));

                        sqlList.add(binUuid);
                    }
                }
            }
        }

        // Clone the list and sort with Java
        List<UUID> javaList = sqlList.stream().sorted(UuidTest::sqlUuidCompare).collect(Collectors.toList());

        // Test for correct order
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < javaList.size(); i++) {
            Comparable sqlUuid = sqlList.get(i);
            Comparable javaUuid = javaList.get(i);
            Assert.assertEquals(sqlUuid, javaUuid);
            sb.append(String.format("%s %s\n", sqlUuid, javaUuid));
        }
        String res = sb.toString();
        System.out.println(res);
    }

    private static byte[] uuidToByteArray(UUID uuid) {

        // Turn into byte array
        ByteBuffer bb = ByteBuffer.allocate(16);
        bb.putLong(uuid.getMostSignificantBits());
        bb.putLong(uuid.getLeastSignificantBits());
        bb.rewind();
        byte[] bytes = new byte[16];
        bb.get(bytes);

        // Transform
        bb = transformUuid(bytes);

        // Turn into byte array
        bb.get(bytes);

        return bytes;
    }

    private static UUID byteArrayToUuid(byte[] bytes) {
        ByteBuffer bb = transformUuid(bytes);
        long hi = bb.getLong();
        long low = bb.getLong();
        UUID uuid = new UUID(hi, low);
        return uuid;
    }

    private static int sqlUuidCompare(UUID leftUuid, UUID rightUuid) {
        byte[] leftBytes = uuidToByteArray(leftUuid);
        byte[] rightBytes = uuidToByteArray(rightUuid);

        // Compare node
        for(int i = 10; i < 16; i++) {
            int val = (leftBytes[i] & 0xFF) - (rightBytes[i] & 0xFF);
            if(val != 0) {
                return val;
            }
        }

        // Compare clock_seq
        for(int i = 8; i < 10; i++) {
            int val = (leftBytes[i] & 0xFF) - (rightBytes[i] & 0xFF);
            if(val != 0) {
                return val;
            }
        }

        // Compare time_hi
        for(int i = 6; i < 8; i++) {
            int val = (leftBytes[i] & 0xFF) - (rightBytes[i] & 0xFF);
            if(val != 0) {
                return val;
            }
        }

        // Compare time_mid
        for(int i = 4; i < 6; i++) {
            int val = (leftBytes[i] & 0xFF) - (rightBytes[i] & 0xFF);
            if(val != 0) {
                return val;
            }
        }

        // Compare time_low
        for(int i = 0; i < 4; i++) {
            int val = (leftBytes[i] & 0xFF) - (rightBytes[i] & 0xFF);
            if(val != 0) {
                return val;
            }
        }

        return 0; // They are equal!
    }

    private static ByteBuffer transformUuid(byte[] bytes) {
        if (bytes.length != 16) {
            throw new RuntimeException("Invalid UUID bytes!");
        }

        // Get hi
        byte[] time_low = Arrays.copyOfRange(bytes, 0, 4);
        byte[] time_mid = Arrays.copyOfRange(bytes, 4, 6);
        byte[] time_hi = Arrays.copyOfRange(bytes, 6, 8); // actually time_hi + version

        // Get low
        byte[] clock_seq = Arrays.copyOfRange(bytes, 8, 10); // actually variant + clock_seq
        byte[] node = Arrays.copyOfRange(bytes, 10, 16); // node

        // Transform
        ArrayUtils.reverse(time_low);
        ArrayUtils.reverse(time_mid);
        ArrayUtils.reverse(time_hi);

        // Rebuild
        ByteBuffer bb = ByteBuffer.allocate(16);
        bb.put(time_low);
        bb.put(time_mid);
        bb.put(time_hi);
        bb.put(clock_seq);
        bb.put(node);

        // Grab longs
        bb.rewind();
        return bb;
    }

    private static String addDashes(String hex) {
        return String.format("%s-%s-%s-%s-%s",
                hex.substring(0, 8),
                hex.substring(8, 12),
                hex.substring(12, 16),
                hex.substring(16, 20),
                hex.substring(20, 32));
    }
}

{'fraction_non_alphanumeric': 0.12436213991769547, 'fraction_numerical': 0.013497942386831276, 'mean_word_length': 1.896543504171633, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '520756', 'n_tokens_mistral': 3841, 'n_tokens_neox': 3573, 'n_words': 823}