package org.mengyun.tcctransaction.repository.helper;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.transaction.xa.Xid;

/**
 * Created by changming.xie on 9/15/16.
 */
public class RedisHelper {


    public static byte[] getRedisKey(String keyPrefix, Xid xid) {
        return new StringBuilder().append(keyPrefix).append(xid.toString()).toString().getBytes();
    }

    public static byte[] getRedisKey(String keyPrefix, String globalTransactionId, String branchQualifier) {
        return new StringBuilder().append(keyPrefix)
                .append("globalTransactionId").append(":").append(globalTransactionId).append(",")
                .append("branchQualifier").append(":").append(branchQualifier).toString().getBytes();
    }

    public static byte[] getVersionKey(String keyPrefix, Xid xid) {
        return new StringBuilder().append("VER:").append(keyPrefix).append(xid.toString()).toString().getBytes();
    }

    public static byte[] getVersionKey(String keyPrefix, String globalTransactionId, String branchQualifier) {
        return new StringBuilder().append("VER:").append(keyPrefix).append(globalTransactionId).append(":").append(branchQualifier).toString().getBytes();
    }

    public static <T> T execute(JedisPool jedisPool, JedisCallback<T> callback) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return callback.doInJedis(jedis);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
@@ -505,32 +505,80 @@
 nchQualifier) %7B%0A
+%0A        if (keyPrefix.startsWith(%22AGG%22)) %7B%0A    
         return n
@@ -622,32 +622,36 @@
                 
+   
+ 
 .append(%22globalT
@@ -717,16 +717,20 @@
 nd(%22,%22)%0A
+    
         
@@ -815,32 +815,246 @@
 g().getBytes();%0A
+        %7D else %7B%0A            return new StringBuilder().append(keyPrefix)%0A                    .append(globalTransactionId).append(%22:%22)%0A                    .append(branchQualifier).toString().getBytes();%0A        %7D%0A%0A
     %7D%0A%0A    publi

modify getRedisKey