// Path: tcc-transaction-core/src/main/java/org/pankai/tcctransaction/utils/ByteUtils.java
// public class ByteUtils {
// 
//     public static byte[] longToBytes(long l) {
//         return ByteBuffer.allocate(8).putLong(l).array();
//     }
// 
//     public static long bytesToLong(byte[] bytes) {
//         return ByteBuffer.wrap(bytes).getLong();
//     }
// }


import org.pankai.tcctransaction.utils.ByteUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.transaction.xa.Xid;
import java.util.*;
package org.pankai.tcctransaction.repository.helper;



/**
 * Created by pktczwd on 2016/12/7.
 */
public class RedisHelper {

    public static byte[] getRedisKey(String keyPrefix, Xid xid) {

        byte[] prefix = keyPrefix.getBytes();
        byte[] globalTransactionId = xid.getGlobalTransactionId();
        byte[] branchQualifier = xid.getBranchQualifier();

        byte[] key = new byte[prefix.length + globalTransactionId.length + branchQualifier.length];
        System.arraycopy(prefix, 0, key, 0, prefix.length);
        System.arraycopy(globalTransactionId, 0, key, prefix.length, globalTransactionId.length);
        System.arraycopy(branchQualifier, 0, key, prefix.length + globalTransactionId.length, branchQualifier.length);
        return key;
    }

    public static byte[] getKeyValue(JedisPool jedisPool, final byte[] key) {
        return execute(jedisPool, new JedisCallback<byte[]>() {
            @Override
            public byte[] doInJedis(Jedis jedis) {
                Map<byte[], byte[]> fieldValueMap = jedis.hgetAll(key);

                List<Map.Entry<byte[], byte[]>> entries = new ArrayList<Map.Entry<byte[], byte[]>>(fieldValueMap.entrySet());
                Collections.sort(entries, new Comparator<Map.Entry<byte[], byte[]>>() {
                    @Override
                    public int compare(Map.Entry<byte[], byte[]> entry1, Map.Entry<byte[], byte[]> entry2) {
// Path: tcc-transaction-core/src/main/java/org/pankai/tcctransaction/utils/ByteUtils.java
// public class ByteUtils {
// 
//     public static byte[] longToBytes(long l) {
//         return ByteBuffer.allocate(8).putLong(l).array();
//     }
// 
//     public static long bytesToLong(byte[] bytes) {
//         return ByteBuffer.wrap(bytes).getLong();
//     }
// }


// Path: tcc-transaction-core/src/main/java/org/pankai/tcctransaction/repository/helper/RedisHelper.java
import org.pankai.tcctransaction.utils.ByteUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.transaction.xa.Xid;
import java.util.*;

package org.pankai.tcctransaction.repository.helper;



/**
 * Created by pktczwd on 2016/12/7.
 */
public class RedisHelper {

    public static byte[] getRedisKey(String keyPrefix, Xid xid) {

        byte[] prefix = keyPrefix.getBytes();
        byte[] globalTransactionId = xid.getGlobalTransactionId();
        byte[] branchQualifier = xid.getBranchQualifier();

        byte[] key = new byte[prefix.length + globalTransactionId.length + branchQualifier.length];
        System.arraycopy(prefix, 0, key, 0, prefix.length);
        System.arraycopy(globalTransactionId, 0, key, prefix.length, globalTransactionId.length);
        System.arraycopy(branchQualifier, 0, key, prefix.length + globalTransactionId.length, branchQualifier.length);
        return key;
    }

    public static byte[] getKeyValue(JedisPool jedisPool, final byte[] key) {
        return execute(jedisPool, new JedisCallback<byte[]>() {
            @Override
            public byte[] doInJedis(Jedis jedis) {
                Map<byte[], byte[]> fieldValueMap = jedis.hgetAll(key);

                List<Map.Entry<byte[], byte[]>> entries = new ArrayList<Map.Entry<byte[], byte[]>>(fieldValueMap.entrySet());
                Collections.sort(entries, new Comparator<Map.Entry<byte[], byte[]>>() {
                    @Override
                    public int compare(Map.Entry<byte[], byte[]> entry1, Map.Entry<byte[], byte[]> entry2) {
                        return (int) (ByteUtils.bytesToLong(entry1.getKey()) - ByteUtils.bytesToLong(entry2.getKey()));