首页

关于mapdb源码包中SerializerUUID序列化工具类实现序列化对象UUID的功能

标签:SerializerUUID,序列化统一编码,mapdb     发布时间:2018-03-02   

一、前言

关于mapdb-master源码包中org.mapdb.serializer.SerializerUUID类,实现序列化对象的全局统一编码功能,具体代码参见下面。

二、源码说明

package org.mapdb.serializer;@b@@b@import org.mapdb.DataInput2;@b@import org.mapdb.DataOutput2;@b@@b@import java.io.IOException;@b@import java.util.Arrays;@b@import java.util.Comparator;@b@import java.util.UUID;@b@@b@public class SerializerUUID implements GroupSerializer<java.util.UUID> {@b@    @Override@b@    public void serialize(DataOutput2 out, UUID value) throws IOException {@b@        out.writeLong(value.getMostSignificantBits());@b@        out.writeLong(value.getLeastSignificantBits());@b@    }@b@@b@    @Override@b@    public UUID deserialize(DataInput2 in, int available) throws IOException {@b@        return new UUID(in.readLong(), in.readLong());@b@    }@b@@b@    @Override@b@    public int fixedSize() {@b@        return 16;@b@    }@b@@b@    @Override@b@    public boolean isTrusted() {@b@        return true;@b@    }@b@@b@    @Override@b@    public int valueArraySearch(Object keys, UUID key) {@b@        return Arrays.binarySearch(valueArrayToArray(keys), key);@b@    }@b@@b@    @Override@b@    public int valueArraySearch(Object keys, UUID key, Comparator comparator) {@b@        return Arrays.binarySearch(valueArrayToArray(keys), key, comparator);@b@    }@b@@b@    @Override@b@    public void valueArraySerialize(DataOutput2 out, Object vals) throws IOException {@b@        for (long o : (long[]) vals) {@b@            out.writeLong(o);@b@        }@b@    }@b@@b@    @Override@b@    public Object valueArrayDeserialize(DataInput2 in, int size) throws IOException {@b@        size *= 2;@b@        long[] ret = new long[size];@b@        for (int i = 0; i < size; i++) {@b@            ret[i] = in.readLong();@b@        }@b@        return ret;@b@    }@b@@b@    @Override@b@    public UUID valueArrayGet(Object vals, int pos) {@b@        long[] v = (long[]) vals;@b@        pos *= 2;@b@        return new UUID(v[pos++], v[pos]);@b@    }@b@@b@    @Override@b@    public int valueArraySize(Object vals) {@b@        return ((long[]) vals).length / 2;@b@    }@b@@b@    @Override@b@    public Object valueArrayEmpty() {@b@        return new long[0];@b@    }@b@@b@    @Override@b@    public Object valueArrayPut(Object vals, int pos, UUID newValue) {@b@        pos *= 2;@b@@b@        long[] array = (long[]) vals;@b@        final long[] ret = Arrays.copyOf(array, array.length + 2);@b@@b@        if (pos < array.length) {@b@            System.arraycopy(array, pos, ret, pos + 2, array.length - pos);@b@        }@b@        ret[pos++] = newValue.getMostSignificantBits();@b@        ret[pos] = newValue.getLeastSignificantBits();@b@        return ret;@b@    }@b@@b@    @Override@b@    public Object valueArrayUpdateVal(Object vals, int pos, UUID newValue) {@b@        pos *= 2;@b@        long[] vals2 = ((long[]) vals).clone();@b@        vals2[pos++] = newValue.getMostSignificantBits();@b@        vals2[pos] = newValue.getLeastSignificantBits();@b@        return vals2;@b@    }@b@@b@@b@    @Override@b@    public Object valueArrayFromArray(Object[] objects) {@b@        long[] ret = new long[objects.length * 2];@b@        int pos = 0;@b@@b@        for (Object o : objects) {@b@            UUID uuid = (java.util.UUID) o;@b@            ret[pos++] = uuid.getMostSignificantBits();@b@            ret[pos++] = uuid.getLeastSignificantBits();@b@        }@b@@b@        return ret;@b@    }@b@@b@    @Override@b@    public Object valueArrayCopyOfRange(Object vals, int from, int to) {@b@        return Arrays.copyOfRange((long[]) vals, from * 2, to * 2);@b@    }@b@@b@    @Override@b@    public Object valueArrayDeleteValue(Object vals, int pos) {@b@        pos *= 2;@b@        long[] valsOrig = (long[]) vals;@b@        long[] vals2 = new long[valsOrig.length - 2];@b@        System.arraycopy(vals, 0, vals2, 0, pos - 2);@b@        System.arraycopy(vals, pos, vals2, pos - 2, vals2.length - (pos - 2));@b@        return vals2;@b@    }@b@//@b@//        @Override@b@//        public BTreeKeySerializer getBTreeKeySerializer(Comparator comparator) {@b@//            if(comparator!=null && comparator!=Fun.COMPARATOR) {@b@//                return super.getBTreeKeySerializer(comparator);@b@//            }@b@//            return BTreeKeySerializer.UUID;@b@//        }@b@}
  • ◆ 相关内容