`
lynnwong
  • 浏览: 36724 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Tair Transcode

    博客分类:
  • tair
 
阅读更多
在Tair中,对于传输的对象做编码和解码的相关转码工作。
目前,Tair支持的转码序列化类型有11种,分别是:
// serialize type
    public static final int TAIR_STYPE_INT = 1;
    public static final int TAIR_STYPE_STRING = 2;
    public static final int TAIR_STYPE_BOOL = 3;
    public static final int TAIR_STYPE_LONG = 4;
    public static final int TAIR_STYPE_DATE = 5;
    public static final int TAIR_STYPE_BYTE = 6;
    public static final int TAIR_STYPE_FLOAT = 7;
    public static final int TAIR_STYPE_DOUBLE = 8;
    public static final int TAIR_STYPE_BYTEARRAY = 9;
    public static final int TAIR_STYPE_SERIALIZE = 10;
    public static final int TAIR_STYPE_INCDATA = 11;


/**
 * (C) 2007-2010 Taobao Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
package com.taobao.tair.etc;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class TranscoderUtil {
    private static final Log log = LogFactory.getLog(TranscoderUtil.class);

    public static byte[] encodeLong(long number) {
        byte[] rt = new byte[8];

        rt[7] = (byte) (number & 0xFF);
        rt[6] = (byte) ((number >> 8) & 0xFF);
        rt[5] = (byte) ((number >> 16) & 0xFF);
        rt[4] = (byte) ((number >> 24) & 0xFF);
        rt[3] = (byte) ((number >> 32) & 0xFF);
        rt[2] = (byte) ((number >> 40) & 0xFF);
        rt[1] = (byte) ((number >> 48) & 0xFF);
        rt[0] = (byte) ((number >> 56) & 0xFF);
        return rt;
    }

    public static long decodeLong(byte[] data) {
        long rv = 0;

        for (byte i : data) {
            rv = (rv << 8) | ((i < 0) ? (256 + i)
                                      : i);
        }

        return rv;
    }

    public static byte[] encodeInt(int number) {
        byte[] fg = new byte[4];

        fg[3] = (byte) (number & 0xFF);
        fg[2] = (byte) ((number >> 8) & 0xFF);
        fg[1] = (byte) ((number >> 16) & 0xFF);
        fg[0] = (byte) ((number >> 24) & 0xFF);
        return fg;
    }

    public static int decodeInt(byte[] data) {
        assert data.length <= 4 : "Too long to be an int (" + data.length + ") bytes";
        return (int) decodeLong(data);
    }

    public static int getInt(byte[] data, int offset) {
    	int rv = 0;
    	rv = ((data[offset+3] < 0) ? (256 + data[offset+3]) : data[offset+3]);
    	rv = (rv << 8) | ((data[offset+2] < 0) ? (256 + data[offset+2]) : data[offset+2]);
    	rv = (rv << 8) | ((data[offset+1] < 0) ? (256 + data[offset+1]) : data[offset+1]);
    	rv = (rv << 8) | ((data[offset] < 0) ? (256 + data[offset]) : data[offset]);
    	return rv;
    }

    public static byte[] encodeByte(byte in) {
        return new byte[] { in };
    }

    public static byte decodeByte(byte[] in) {
        assert in.length <= 1 : "Too long for a byte";

        byte rv = 0;

        if (in.length == 1) {
            rv = in[0];
        }

        return rv;
    }

    public static byte[] encodeBoolean(boolean b) {
        byte[] rv = new byte[1];

        rv[0] = (byte) (b ? '1'
                          : '0');
        return rv;
    }

    public static boolean decodeBoolean(byte[] in) {
        assert in.length == 1 : "Wrong length for a boolean";
        return in[0] == '1';
    }

    public static byte[] compress(byte[] in) {
        if (in == null) {
            throw new NullPointerException("Can't compress null");
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream      gz  = null;

        try {
            gz = new GZIPOutputStream(bos);
            gz.write(in);
        } catch (IOException e) {
            throw new RuntimeException("IO exception compressing data", e);
        } finally {
            try {
                gz.close();
                bos.close();
            } catch (Exception e) {
                // should not happen
            }
        }

        byte[] rv = bos.toByteArray();

        if (log.isInfoEnabled()) {
            log.info("compressed value, size from [" + in.length + "] to [" + rv.length + "]");
        }

        return rv;
    }

    public static byte[] decompress(byte[] in) {
        ByteArrayOutputStream bos = null;

        if (in != null) {
            ByteArrayInputStream bis = new ByteArrayInputStream(in);

            bos = new ByteArrayOutputStream();

            GZIPInputStream gis = null;

            try {
                gis = new GZIPInputStream(bis);

                byte[] buf = new byte[8192];
                int    r   = -1;

                while ((r = gis.read(buf)) > 0) {
                    bos.write(buf, 0, r);
                }
            } catch (IOException e) {
                bos = null;
                throw new RuntimeException(e);
            } finally {
                try {
                    gis.close();
                    bos.close();
                } catch (Exception e) {
                }
            }
        }

        return (bos == null) ? null
                             : bos.toByteArray();
    }

    public static byte[] serialize(Object o) {
        if (o == null) {
            throw new NullPointerException("Can't serialize null");
        }

        byte[] rv = null;

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream    os  = new ObjectOutputStream(bos);

            os.writeObject(o);
            os.close();
            bos.close();
            rv = bos.toByteArray();
        } catch (IOException e) {
            throw new IllegalArgumentException("Non-serializable object", e);
        }

        return rv;
    }

    public static Object deserialize(byte[] in) {
        Object rv = null;

        try {
            if (in != null) {
                ByteArrayInputStream bis = new ByteArrayInputStream(in);
                ObjectInputStream    is  = new ObjectInputStream(bis);

                rv                       = is.readObject();
                is.close();
                bis.close();
            }
        } catch (Exception e) {
            throw new RuntimeException("deserialize failed", e);
        }

        return rv;
    }

    public static String decodeString(byte[] data, String charset) {
        String rv = null;

        try {
            if (data != null) {
                rv = new String(data, charset);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return rv;
    }

    /**
     * Encode a string into the current character set.
     */
    public static byte[] encodeString(String in, String charset) {
        byte[] rv = null;

        try {
            rv = in.getBytes(charset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return rv;
    }
}
0
0
分享到:
评论

相关推荐

    tair.zip, tair源码

    tair源码

    分布式缓存tair介绍

    tair 是淘宝自己开发的一个分布式 key/value 存储引擎. tair 分为持久化和非持久化两种使用方式. 非持久化的 tair 可以看成是一个分布式缓存. 持久化的 tair 将数据存放于磁盘中. 为了解决磁盘损坏导致数据丢失, ...

    轻松入门淘宝开源NoSQL框架Tair视频教程

    在Tair出现之前的很长一段时间里,像redis、memcache这些知名NoSql数据库是不支持分布式的,在这样的背景下,由淘宝网自主开发并在2010.6开源的一个高性能、高扩展、高可靠分布式缓存,类似map的key/value结构,在...

    tair 服务端源码包

    淘宝tair源代码包,tair 是淘宝自己开发的一个分布式 key/value 存储引擎。

    tair与redis比较

    文档总结了tair与redis的有缺点,提供给大家应用多为参考

    tair缓存开发指南

    tair缓存开发指南,淘宝开元kv存储系统说明书

    tair源码java镜像

    tair源码,java,镜像,nosql数据库,阿里巴巴淘宝源码

    tair-2.3分布式key-value存储引擎

    tair 是淘宝自己开发的一个分布式 key/value 存储引擎. tair 分为持久化和非持久化两种使用方式. 非持久化的 tair 可以看成是一个分布式缓存. 持久化的 tair 将数据存放于磁盘中. 为了解决磁盘损坏导致数据丢失, ...

    tair-2.3.tar.tar

    淘宝tair源代码包,tair 是淘宝自己开发的一个分布式 key/value 存储引擎。

    tair集群配置

    Tair集群配置,tair的配置步骤,配置过程需要注意的问题

    com.taobao.tair

    com.taobao.tair

    分布式缓存tair的内部结构介绍

    根据总体介绍文档,tair是一个分布式的key-value存储系统,并且支持不同存储引擎的体系结构,但系统同时只能用一种存储引擎。整个系统主要包括了config_server模块,data_server 模块,storage模块以及其他公用的...

    tair使用测试

    淘宝TAIR简单使用案例,配合博文《搭建淘宝Tair服务器》学习。

    tair开发指南

    tair缓存开发指南,阿里巴巴开源的缓存系统

    tair3-client-3.0.1.13.jar

    maven 淘宝下的jar包啊

    王玉法:Tair存储引擎之路

    在12月1日的“NoSQL & NewSQL”主题论坛上,来自阿里巴巴集团核心系统研发高级开发工程师王玉法分享了题为《Tair存储引擎之路》的主题演讲,他首先给大家介绍了Tair存储引擎的大概情况,随后,王玉法给大家分享了...

    memcache、redis、tair性能对比测试报告

    memcache、redis、tair性能对比测试报告,分布缓存技术预言中有包括ehcache、memcache、redis、tair,还包括了基于MongoDB的分布式技术。测试中,考虑到各自功能的差异化特点,其中选择了memcache、redis、tair功能...

    Redis企业版Tair性能增强系列产品概述.pdf

    Redis企业版Tair性能增强系列产品概述.pdf

    Introduction to Tair

    Introduction to Tair 博文链接:https://lynnwong.iteye.com/blog/735424

    Tair中的类型转换

    NULL 博文链接:https://lynnwong.iteye.com/blog/1285742

Global site tag (gtag.js) - Google Analytics