Java Code Examples for com.esotericsoftware.kryo.Kryo#setReferences()

The following examples show how to use com.esotericsoftware.kryo.Kryo#setReferences() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSerializer.java    From jea with Apache License 2.0 6 votes vote down vote up
/**
 * 根据所注册的Class,初始化Kryo实例
 * 
 * @return
 */
protected Kryo initKryo() {
	Kryo kryo = new Kryo();
	kryo.setReferences(true); 
	kryo.setRegistrationRequired(true); 
	kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
	Set<Class<?>> keys = propertyMap.keySet();
	for(Class<?> key : keys){
		if(propertyMap.get(key) != null){
			kryo.register(key, propertyMap.get(key));
		} else {
			kryo.register(key);
		}
	}
	
	return kryo;
}
 
Example 2
Source File: KryoRedisSerializer.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public T deserialize(byte[] bytes) throws SerializationException {
    if (bytes == null || bytes.length <= 0) {
        return null;
    }

    Kryo kryo = kryos.get();
    kryo.setReferences(false);
    kryo.register(clazz);

    try (Input input = new Input(bytes)) {
        return (T) kryo.readClassAndObject(input);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

    return null;
}
 
Example 3
Source File: HopscotchCollection.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected HopscotchCollection( final Kryo kryo, final Input input ) {
    final boolean oldReferences = kryo.getReferences();
    kryo.setReferences(false);

    capacity = input.readInt();
    size = 0;
    buckets = (T[])new Object[capacity];
    status = new byte[capacity];
    int nElements = input.readInt();
    while ( nElements-- > 0 ) {
        add((T)kryo.readClassAndObject(input));
    }

    kryo.setReferences(oldReferences);
}
 
Example 4
Source File: PSTreeNode.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private PSTreeNode(final Kryo kryo, final Input input) {
    final boolean oldReferences = kryo.getReferences();
    kryo.setReferences(false);

    name = input.readString();
    rank = input.readString();
    parent = input.readInt();
    length = input.readLong();
    final int numChildren = input.readInt();
    children = new HashSet<>(numChildren);
    for (int i = 0; i < numChildren; i++) {
        children.add(Integer.valueOf(input.readString()));
    }

    kryo.setReferences(oldReferences);
}
 
Example 5
Source File: DbStoragePlainFile.java    From Paper with Apache License 2.0 5 votes vote down vote up
private Kryo createKryoInstance(boolean compatibilityMode) {
    Kryo kryo = new Kryo();

    if (compatibilityMode) {
        kryo.getFieldSerializerConfig().setOptimizedGenerics(true);
    }

    kryo.register(PaperTable.class);
    kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
    kryo.setReferences(false);

    // Serialize Arrays$ArrayList
    //noinspection ArraysAsListWithZeroOrOneArgument
    kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
    // Serialize inner AbstractList$SubAbstractListRandomAccess
    kryo.addDefaultSerializer(new ArrayList<>().subList(0, 0).getClass(),
            new NoArgCollectionSerializer());
    // Serialize AbstractList$SubAbstractList
    kryo.addDefaultSerializer(new LinkedList<>().subList(0, 0).getClass(),
            new NoArgCollectionSerializer());
    // To keep backward compatibility don't change the order of serializers above

    // UUID support
    kryo.register(UUID.class, new UUIDSerializer());

    for (Class<?> clazz : mCustomSerializers.keySet())
        kryo.register(clazz, mCustomSerializers.get(clazz));

    kryo.setInstantiatorStrategy(
            new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));

    return kryo;
}
 
Example 6
Source File: KryoSerializer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@Override
protected Kryo initialValue() throws Exception {
    Kryo kryo = new Kryo();
    for (Class<?> type : useJavaSerializerTypes) {
        kryo.addDefaultSerializer(type, JavaSerializer.class);
    }
    kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
    kryo.setRegistrationRequired(false);
    kryo.setReferences(false);
    return kryo;
}
 
Example 7
Source File: PSScorerTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReadDatabase() throws Exception {
    final File tempFile = createTempFile("test", ".dat");
    final PSTree tree_in = new PSTree(1);
    tree_in.addNode(2, "n2", 1, 100, "rank_2");
    final Map<String, Integer> map_in = new HashMap<>();
    map_in.put("acc_2a", 2);
    map_in.put("acc_2b", 2);
    final PSTaxonomyDatabase expectedDatabase = new PSTaxonomyDatabase(tree_in, map_in);

    final Kryo kryo = new Kryo();
    kryo.setReferences(false);
    final Output output = new Output(new FileOutputStream(tempFile.getPath()));
    kryo.writeObject(output, expectedDatabase);
    output.close();

    final PSTaxonomyDatabase taxonomyDatabase = PSScorer.readTaxonomyDatabase(tempFile.getPath());
    Assert.assertEquals(expectedDatabase.tree, taxonomyDatabase.tree);
    Assert.assertEquals(expectedDatabase.accessionToTaxId, taxonomyDatabase.accessionToTaxId);

    try {
        PSScorer.readTaxonomyDatabase("/bad_dir");
        Assert.fail("Did not throw UserException for bad path to readTaxonomyDatabase()");
    } catch (UserException e) {
    }
}
 
Example 8
Source File: LargeLongHopscotchSet.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected LargeLongHopscotchSet(final Kryo kryo, final Input stream) {
    final boolean oldReferences = kryo.getReferences();
    kryo.setReferences(false);

    numSets = stream.readInt();
    sets = new ArrayList<>(numSets);
    for (int i = 0; i < numSets; i++) {
        sets.add(kryo.readObject(stream, LongHopscotchSet.class));
    }

    kryo.setReferences(oldReferences);
}
 
Example 9
Source File: PSBuildReferenceTaxonomyUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Writes objects using Kryo to specified local file path.
 * NOTE: using setReferences(false), which must also be set when reading the file. Does not work with nested
 * objects that reference its parent.
 */
public static void writeTaxonomyDatabase(final String filePath, final PSTaxonomyDatabase taxonomyDatabase) {
    try {
        final Kryo kryo = new Kryo();
        kryo.setReferences(false);
        Output output = new Output(new FileOutputStream(filePath));
        kryo.writeObject(output, taxonomyDatabase);
        output.close();
    } catch (final FileNotFoundException e) {
        throw new UserException.CouldNotCreateOutputFile("Could not serialize objects to file", e);
    }
}
 
Example 10
Source File: RmiTest.java    From kryonet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Registers the same classes in the same order on both the client and server. */
static public void register (Kryo kryo) {
	kryo.register(Object.class); // Needed for Object#toString, hashCode, etc.
	kryo.register(TestObject.class);
	kryo.register(MessageWithTestObject.class);
	kryo.register(StackTraceElement.class);
	kryo.register(StackTraceElement[].class);
	kryo.register(UnsupportedOperationException.class);
	kryo.setReferences(true); // Needed for UnsupportedOperationException, which has a circular reference in the cause field.
	ObjectSpace.registerClasses(kryo);
}
 
Example 11
Source File: LargeLongHopscotchSet.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void serialize(final Kryo kryo, final Output stream) {
    final boolean oldReferences = kryo.getReferences();
    kryo.setReferences(false);

    stream.writeInt(sets.size());
    for (final LongHopscotchSet set : sets) {
        kryo.writeObject(stream, set);
    }

    kryo.setReferences(oldReferences);
}
 
Example 12
Source File: PSTree.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void serialize(final Kryo kryo, final Output output) {
    final boolean oldReferences = kryo.getReferences();
    kryo.setReferences(false);

    output.writeString(String.valueOf(root));
    output.writeInt(tree.size());
    for (final int key : tree.keySet()) {
        output.writeInt(key);
        kryo.writeObject(output, tree.get(key));
    }

    kryo.setReferences(oldReferences);
}
 
Example 13
Source File: PSTreeNode.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void serialize(final Kryo kryo, final Output output) {
    final boolean oldReferences = kryo.getReferences();
    kryo.setReferences(false);

    output.writeString(name);
    output.writeString(rank);
    output.writeInt(parent);
    output.writeLong(length);
    output.writeInt(children.size());
    for (final int child : children) {
        output.writeString(String.valueOf(child));
    }

    kryo.setReferences(oldReferences);
}
 
Example 14
Source File: LevelDbStore.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
		Kryo kryo = new Kryo();
		kryo.setReferences(false);
		kryo.setRegistrationRequired(false);
		kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
//		kryo.register(Transaction.class);
//		kryo.register(Xid.class);
//		kryo.register(TransactionType.class);
//		kryo.register(TransactionStatus.class);
//		kryo.register(Participant.class);
		
//		Output output = new Output(new ByteArrayOutputStream());//new Output(new FileOutputStream("file.bin"));
//		
//		DefaultTransaction defaultTransaction = new DefaultTransaction();
//		defaultTransaction.setXid(new DefaultXid("1", "2", "3"));
//		
//		kryo.writeObject(output, defaultTransaction);
//		output.flush();
//		output.close();
		
		Options options = new Options();
		options.createIfMissing(true);
		options.cacheSize(100 * 1048576); // 100MB cache
		options.logger(new org.iq80.leveldb.Logger() {
			public void log(String message) {
				TransactionLog.REPOSITORY.info(message);
			}
		});
		DB db = Iq80DBFactory.factory.open(new File("txtreedb"), options);
		
//		String stats = db.getProperty("leveldb.stats");
//		System.out.println(stats);
		
		DBIterator iterator = null;
		try {
			iterator = db.iterator();
			for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
				String key = Iq80DBFactory.asString(iterator.peekNext().getKey());
				Transaction value = (Transaction) SerializationUtil.deserialize(iterator.peekNext().getValue());
				
				System.out.println(key+" = "+value);
			}
		} finally {
			if (iterator != null) {
				iterator.close();
			}
		}

//		String transactionId = "f282205a-e794-4bda-83a2-bb28b8839cad";
//		Input input = new Input(db.get(Iq80DBFactory.bytes(transactionId)));
//		Transaction transaction = (Transaction) kryo.readClassAndObject(input);
//		System.out.println(transaction);
	}
 
Example 15
Source File: JRedisSerializationUtils.java    From springJredisCache with Apache License 2.0 4 votes vote down vote up
/**
 * create a new kryo object to application use
 *
 * @return
 */
public KryoHolder creatInstnce() {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);//
    return new KryoHolder(kryo);
}
 
Example 16
Source File: KryoPoolSerializer.java    From azeroth with Apache License 2.0 4 votes vote down vote up
/**
 * create a new kryo object to application use
 * @return KryoHolder instance
 */
public KryoHolder creatInstnce() {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);//
    return new KryoHolder(kryo);
}
 
Example 17
Source File: KryoCodec.java    From jvm-serializer with Apache License 2.0 4 votes vote down vote up
protected Kryo initialValue() {
    Kryo kryo = new Kryo();

    kryo.register(byte[].class);
    kryo.register(char[].class);
    kryo.register(short[].class);
    kryo.register(int[].class);
    kryo.register(long[].class);
    kryo.register(float[].class);
    kryo.register(double[].class);
    kryo.register(boolean[].class);
    kryo.register(String[].class);
    kryo.register(Object[].class);
    kryo.register(KryoSerializable.class);
    kryo.register(BigInteger.class);
    kryo.register(BigDecimal.class);
    kryo.register(Class.class);
    kryo.register(Date.class);
    //kryo.register(Enum.class);
    kryo.register(EnumSet.class);
    kryo.register(Currency.class);
    kryo.register(StringBuffer.class);
    kryo.register(StringBuilder.class);
    kryo.register(Collections.EMPTY_LIST.getClass());
    kryo.register(Collections.EMPTY_MAP.getClass());
    kryo.register(Collections.EMPTY_SET.getClass());
    kryo.register(Collections.singletonList(null).getClass());
    kryo.register(Collections.singletonMap(null, null).getClass());
    kryo.register(Collections.singleton(null).getClass());
    kryo.register(TreeSet.class);
    kryo.register(Collection.class);
    kryo.register(TreeMap.class);
    kryo.register(Map.class);
    try {
        kryo.register(Class.forName("sun.util.calendar.ZoneInfo"));
    } catch (ClassNotFoundException e) {
        //Noop
    }
    kryo.register(Calendar.class);
    kryo.register(Locale.class);

    kryo.register(BitSet.class);
    kryo.register(HashMap.class);
    kryo.register(Timestamp.class);
    kryo.register(ArrayList.class);

    int size = idList.size();
    for (int i = 0; i < size; i++) {
        kryo.register(classList
                        .get(i),
                serializerList
                        .get(i),
                idList.get(i));
    }
    kryo.setRegistrationRequired(true);
    kryo.setReferences(false);
    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    return kryo;
}
 
Example 18
Source File: KryoPoolSerializer.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
/**
 * create a new kryo object to application use
 * @return KryoHolder instance
 */
public KryoHolder creatInstnce() {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);//
    return new KryoHolder(kryo);
}
 
Example 19
Source File: KryoPoolSerializer.java    From J2Cache with Apache License 2.0 4 votes vote down vote up
/**
 * create a new kryo object to application use
 * @return KryoHolder instance
 */
public KryoHolder creatInstnce() {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);//
    return new KryoHolder(kryo);
}
 
Example 20
Source File: KryoContext.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
public KryoContext() {
	kryo = new Kryo(new FastClassResolver(), new MapReferenceResolver(), new DefaultStreamFactory());
	kryo.setDefaultSerializer(FastSerializer.class);
	kryo.setReferences(false);

	kryo.register(ArrayList.class);
	kryo.register(LinkedList.class);
	kryo.register(CopyOnWriteArrayList.class);
	kryo.register(HashMap.class);
	kryo.register(ConcurrentHashMap.class);
	kryo.register(TreeMap.class);
	kryo.register(TreeSet.class);
	kryo.register(HashSet.class);
	kryo.register(java.util.Date.class);
	kryo.register(java.sql.Date.class);
	kryo.register(LocalDate.class);
	kryo.register(LocalDateTime.class);
	kryo.register(byte[].class);
	kryo.register(char[].class);
	kryo.register(short[].class);
	kryo.register(int[].class);
	kryo.register(long[].class);
	kryo.register(float[].class);
	kryo.register(double[].class);
	kryo.register(boolean[].class);
	kryo.register(String[].class);
	kryo.register(Object[].class);
	kryo.register(BigInteger.class);
	kryo.register(BigDecimal.class);
	kryo.register(Class.class);
	kryo.register(Enum.class);
	kryo.register(EnumSet.class);
	kryo.register(Currency.class);
	kryo.register(StringBuffer.class);
	kryo.register(StringBuilder.class);
	kryo.register(Collections.EMPTY_LIST.getClass());
	kryo.register(Collections.EMPTY_MAP.getClass());
	kryo.register(Collections.EMPTY_SET.getClass());
	kryo.register(Collections.singletonList(null).getClass());
	kryo.register(Collections.singletonMap(null, null).getClass());
	kryo.register(Collections.singleton(null).getClass());
	kryo.register(Collection.class);
	kryo.register(Map.class);
	kryo.register(TimeZone.class);
	kryo.register(Calendar.class);
	kryo.register(Locale.class);
	kryo.register(Charset.class);
	kryo.register(URL.class);
}