com.esotericsoftware.kryo.Serializer Java Examples

The following examples show how to use com.esotericsoftware.kryo.Serializer. 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: ThreadCommSlave.java    From ytk-mp4j with MIT License 6 votes vote down vote up
/**
 * Set union, the set with the same key will be reduced(union) together.
 * @param mapData map set data
 * @param elementSerializer element object Kryo serializer
 * @param elementType element object class

 * @return the set with the same key will be reduced together.
 * @throws Mp4jException
 */
public <T> Map<String, Set<T>> allreduceMapSetUnion(Map<String, Set<T>> mapData, Serializer<T> elementSerializer, Class<T> elementType) throws Mp4jException {

    Operand operand = Operands.OBJECT_OPERAND(new ProcessCommSlave.Mp4jSetSerializer<>(elementSerializer, elementType), elementType);
    IOperator operator = new IObjectOperator<Set<T>>() {
        @Override
        public Set<T> apply(Set<T> o1, Set<T> o2) {
            for (T val : o2) {
                o1.add(val);
            }
            return o1;
        }
    };

    return allreduceMap(mapData, operand, operator);

}
 
Example #2
Source File: GryoRegistrator.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private boolean checkForAndApplySerializerOverride(final Map<Class<?>, Serializer<?>> serializerOverrides,
                                                   final Kryo kryo, Class<?> targetClass) {
    if (serializerOverrides.containsKey(targetClass)) {
        final Serializer<?> ser = serializerOverrides.get(targetClass);
        if (null == ser) {
            // null means use Kryo's default serializer
            log.debug("Registering {} with default serializer per overrides", targetClass);
            kryo.register(targetClass);
        } else {
            // nonnull means use that serializer
            log.debug("Registering {} with serializer {} per overrides", targetClass, ser);
            kryo.register(targetClass, ser);
        }
        return true;
    }
    return false;
}
 
Example #3
Source File: FastSerializer.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 快速写入
 * 
 * @param kryo
 * @param output
 * @param defaultRegistration
 * @param value
 */
public static void fastWrite(Kryo kryo, Output output, Registration defaultRegistration, Object value) {
	if (value == null) {
		kryo.writeClass(output, null);
		return;
	}

	Class<?> type = value.getClass();

	if (defaultRegistration.getType().equals(type)) {
		if (defaultRegistration.getId() == FastClassResolver.NAME) {
			((FastClassResolver) kryo.getClassResolver()).writeName(output, type, defaultRegistration);
		} else {
			output.writeVarInt(defaultRegistration.getId() + 2, true);
		}

		kryo.writeObject(output, value, defaultRegistration.getSerializer());
	} else {
		Registration registration = kryo.writeClass(output, value.getClass());
		Serializer<?> serializer = registration.getSerializer();
		kryo.writeObject(output, value, serializer);
	}
}
 
Example #4
Source File: KryoSerializerSnapshot.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(
	MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers,
	MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses,
	MergeResult<String, KryoRegistration> reconfiguredRegistrations) {

	if (reconfiguredDefaultKryoSerializers.isOrderedSubset() &&
		reconfiguredDefaultKryoSerializerClasses.isOrderedSubset() &&
		reconfiguredRegistrations.isOrderedSubset()) {

		return TypeSerializerSchemaCompatibility.compatibleAsIs();
	}

	// reconfigure a new KryoSerializer
	KryoSerializer<T> reconfiguredSerializer = new KryoSerializer<>(
		snapshotData.getTypeClass(),
		reconfiguredDefaultKryoSerializers.getMerged(),
		reconfiguredDefaultKryoSerializerClasses.getMerged(),
		reconfiguredRegistrations.getMerged());

	return TypeSerializerSchemaCompatibility.compatibleWithReconfiguredSerializer(reconfiguredSerializer);
}
 
Example #5
Source File: KryoTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testSQLRef() throws Exception {
    SQLRef sqlRef = new SQLRef(new HBaseRowLocation(new byte[] {0, 1, 2,3,4,5,6,7,8,9}));

    Output output = new Output(new byte[30],30);
    Serializer serializer = kryo.getSerializer(SQLRef.class);
    serializer.write(kryo, output, sqlRef);
    
    byte[] bytes = output.toBytes();
    assertNotNull(bytes);

    Input input = new Input(new ByteArrayInputStream(bytes), bytes.length);
    SQLRef out = (SQLRef) serializer.read(kryo, input, SQLRef.class);

    assertNotNull(out);
    assertEquals(sqlRef, out);
}
 
Example #6
Source File: ExecutionConfig.java    From flink with Apache License 2.0 6 votes vote down vote up
private LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> parseKryoSerializers(
		ClassLoader classLoader,
		List<String> kryoSerializers) {
	return kryoSerializers.stream()
		.map(v -> Arrays.stream(v.split(","))
			.map(p -> p.split(":"))
			.collect(
				Collectors.toMap(
					arr -> arr[0], // entry key
					arr -> arr[1] // entry value
				)
			)
		)
		.collect(Collectors.toMap(
			m -> loadClass(m.get("class"), classLoader, "Could not load class for kryo serialization"),
			m -> loadClass(m.get("serializer"), classLoader, "Could not load serializer's class"),
			(m1, m2) -> {
				throw new IllegalArgumentException("Duplicated serializer for class: " + m1);
			},
			LinkedHashMap::new
		));
}
 
Example #7
Source File: ObjectSpace.java    From kryonet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void write (Kryo kryo, Output output) {
	output.writeInt(objectID, true);
	output.writeInt(cachedMethod.methodClassID, true);
	output.writeByte(cachedMethod.methodIndex);

	Serializer[] serializers = cachedMethod.serializers;
	Object[] args = this.args;
	for (int i = 0, n = serializers.length; i < n; i++) {
		Serializer serializer = serializers[i];
		if (serializer != null)
			kryo.writeObjectOrNull(output, args[i], serializer);
		else
			kryo.writeClassAndObject(output, args[i]);
	}

	output.writeByte(responseData);
}
 
Example #8
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 6 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(
	MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers,
	MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses,
	MergeResult<String, KryoRegistration> reconfiguredRegistrations) {

	if (reconfiguredDefaultKryoSerializers.isOrderedSubset() &&
		reconfiguredDefaultKryoSerializerClasses.isOrderedSubset() &&
		reconfiguredRegistrations.isOrderedSubset()) {

		return TypeSerializerSchemaCompatibility.compatibleAsIs();
	}

	// reconfigure a new KryoSerializer
	KryoSerializer<T> reconfiguredSerializer = new KryoSerializer<>(
		snapshotData.getTypeClass(),
		reconfiguredDefaultKryoSerializers.getMerged(),
		reconfiguredDefaultKryoSerializerClasses.getMerged(),
		reconfiguredRegistrations.getMerged());

	return TypeSerializerSchemaCompatibility.compatibleWithReconfiguredSerializer(reconfiguredSerializer);
}
 
Example #9
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 6 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(
	MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers,
	MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses,
	MergeResult<String, KryoRegistration> reconfiguredRegistrations) {

	if (reconfiguredDefaultKryoSerializers.isOrderedSubset() &&
		reconfiguredDefaultKryoSerializerClasses.isOrderedSubset() &&
		reconfiguredRegistrations.isOrderedSubset()) {

		return TypeSerializerSchemaCompatibility.compatibleAsIs();
	}

	// reconfigure a new KryoSerializer
	KryoSerializer<T> reconfiguredSerializer = new KryoSerializer<>(
		snapshotData.getTypeClass(),
		reconfiguredDefaultKryoSerializers.getMerged(),
		reconfiguredDefaultKryoSerializerClasses.getMerged(),
		reconfiguredRegistrations.getMerged());

	return TypeSerializerSchemaCompatibility.compatibleWithReconfiguredSerializer(reconfiguredSerializer);
}
 
Example #10
Source File: Namespace.java    From atomix with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static List<RegistrationBlock> buildRegistrationBlocks(NamespaceConfig config) {
  List<Pair<Class<?>[], Serializer<?>>> types = new ArrayList<>();
  List<RegistrationBlock> blocks = new ArrayList<>();
  blocks.addAll(Namespaces.BASIC.registeredBlocks);
  for (NamespaceTypeConfig type : config.getTypes()) {
    try {
      if (type.getId() == null) {
        types.add(Pair.of(new Class[]{type.getType()}, type.getSerializer() != null ? type.getSerializer().newInstance() : null));
      } else {
        blocks.add(new RegistrationBlock(type.getId(), Collections.singletonList(Pair.of(new Class[]{type.getType()}, type.getSerializer().newInstance()))));
      }
    } catch (InstantiationException | IllegalAccessException e) {
      throw new ConfigurationException("Failed to instantiate serializer from configuration", e);
    }
  }
  blocks.add(new RegistrationBlock(FLOATING_ID, types));
  return blocks;
}
 
Example #11
Source File: ProcessCommSlave.java    From ytk-mp4j with MIT License 5 votes vote down vote up
/**
 * Set intersection, the set with the same key will be reduced(intersect) together.
 * @param mapData map set data
 * @param elementSerializer element object Kryo serializer
 * @param elementType element object class

 * @return the set with the same key will be reduced(intersect) together.
 * @throws Mp4jException
 */
public <T> Map<String, Set<T>> allreduceMapSetIntersection(Map<String, Set<T>> mapData, Serializer<T> elementSerializer, Class<T> elementType) throws Mp4jException {
    Operand operand = Operands.OBJECT_OPERAND(new Mp4jSetSerializer<>(elementSerializer, elementType), elementType);
    IOperator operator = new IObjectOperator<Set<T>>() {
        @Override
        public Set<T> apply(Set<T> o1, Set<T> o2) {
            o1.retainAll(o2);
            return o1;
        }
    };

    return allreduceMap(mapData, operand, operator);
}
 
Example #12
Source File: KryoUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Apply a list of {@link KryoRegistration} to a Kryo instance. The list of registrations is
 * assumed to already be a final resolution of all possible registration overwrites.
 *
 * <p>The registrations are applied in the given order and always specify the registration id as
 * the next available id in the Kryo instance (providing the id just extra ensures nothing is
 * overwritten, and isn't strictly required);
 *
 * @param kryo the Kryo instance to apply the registrations
 * @param resolvedRegistrations the registrations, which should already be resolved of all possible registration overwrites
 */
public static void applyRegistrations(Kryo kryo, Collection<KryoRegistration> resolvedRegistrations) {

	Serializer<?> serializer;
	for (KryoRegistration registration : resolvedRegistrations) {
		serializer = registration.getSerializer(kryo);

		if (serializer != null) {
			kryo.register(registration.getRegisteredClass(), serializer, kryo.getNextRegistrationId());
		} else {
			kryo.register(registration.getRegisteredClass(), kryo.getNextRegistrationId());
		}
	}
}
 
Example #13
Source File: KryoNamespace.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a Kryo instance.
 *
 * @return Kryo instance
 */
@Override
public Kryo create() {
    log.trace("Creating Kryo instance for {}", this);
    Kryo kryo = new Kryo();
    kryo.setRegistrationRequired(registrationRequired);

    // If compatible serialization is enabled, override the default serializer.
    if (compatible) {
        kryo.setDefaultSerializer(CompatibleFieldSerializer::new);
    }

    // TODO rethink whether we want to use StdInstantiatorStrategy
    kryo.setInstantiatorStrategy(
            new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));

    for (RegistrationBlock block : registeredBlocks) {
        int id = block.begin();
        if (id == FLOATING_ID) {
            id = kryo.getNextRegistrationId();
        }
        for (Pair<Class<?>[], Serializer<?>> entry : block.types()) {
            register(kryo, entry.getLeft(), entry.getRight(), id++);
        }
    }
    return kryo;
}
 
Example #14
Source File: CompatibleKryo.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public Serializer getDefaultSerializer(Class type) {
    if (type == null) {
        throw new IllegalArgumentException("type cannot be null.");
    }

    if (!type.isArray() && !ReflectionUtils.checkZeroArgConstructor(type)) {
        if (logger.isWarnEnabled()) {
            logger.warn(type + " has no zero-arg constructor and this will affect the serialization performance");
        }
        return new JavaSerializer();
    }
    return super.getDefaultSerializer(type);
}
 
Example #15
Source File: KryoSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method that takes lists of registered types and their serializers, and resolve
 * them into a single list such that the result will resemble the final registration
 * result in Kryo.
 */
private static LinkedHashMap<String, KryoRegistration> buildKryoRegistrations(
		Class<?> serializedType,
		LinkedHashSet<Class<?>> registeredTypes,
		LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> registeredTypesWithSerializerClasses,
		LinkedHashMap<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypesWithSerializers) {

	final LinkedHashMap<String, KryoRegistration> kryoRegistrations = new LinkedHashMap<>();

	kryoRegistrations.put(serializedType.getName(), new KryoRegistration(serializedType));

	for (Class<?> registeredType : checkNotNull(registeredTypes)) {
		kryoRegistrations.put(registeredType.getName(), new KryoRegistration(registeredType));
	}

	for (Map.Entry<Class<?>, Class<? extends Serializer<?>>> registeredTypeWithSerializerClassEntry :
			checkNotNull(registeredTypesWithSerializerClasses).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerClassEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerClassEntry.getKey(),
						registeredTypeWithSerializerClassEntry.getValue()));
	}

	for (Map.Entry<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypeWithSerializerEntry :
			checkNotNull(registeredTypesWithSerializers).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerEntry.getKey(),
						registeredTypeWithSerializerEntry.getValue()));
	}

	// add Avro support if flink-avro is available; a dummy otherwise
	AvroUtils.getAvroUtils().addAvroGenericDataArrayRegistration(kryoRegistrations);

	return kryoRegistrations;
}
 
Example #16
Source File: ThreadCommSlave.java    From ytk-mp4j with MIT License 5 votes vote down vote up
/**
 *  List concat, the lists with the same key will be reduced(concat) together.
 * @param mapData map list data
 * @param rootRank root rank
 * @param elementSerializer element object Kryo serializer
 * @param elementType element object class

 * @return if this process and thread is root, the lists with the same key will be reduced(concat) together,
 *         otherwise, invalid map or null is returned.
 * @throws Mp4jException
 */
public <T> Map<String, List<T>> reduceMapListConcat(Map<String, List<T>> mapData, int rootRank, int rootThreadId, Serializer<T> elementSerializer, Class<T> elementType) throws Mp4jException {
    Operand operand = Operands.OBJECT_OPERAND(new ProcessCommSlave.Mp4jListSerializer<>(elementSerializer, elementType), elementType);
    IOperator operator = new IObjectOperator<List<T>>() {
        @Override
        public List<T> apply(List<T> o1, List<T> o2) {
            for (T val : o2) {
                o1.add(val);
            }
            return o1;
        }
    };

    return reduceMap(mapData, operand, operator, rootRank, rootThreadId);
}
 
Example #17
Source File: ObjectOperand.java    From ytk-mp4j with MIT License 5 votes vote down vote up
public Mp4jObjectArrayReduceSerializer(ArrayMetaData<T[]> arrayMetaData,
                                       IObjectOperator<T> operator,
                                       Serializer<T> serializer,
                                       Class<T> type
                                       ) {
    this.setAcceptsNull(true);
    this.arrayMetaData = arrayMetaData;
    this.operator = operator;
    this.serializer = serializer;
    this.type = type;
}
 
Example #18
Source File: ThreadCommSlave.java    From ytk-mp4j with MIT License 5 votes vote down vote up
/**
 * Set union
 * @param setData set data
 * @param rootRank root rank
 * @param rootThreadId root thread id

 * @return if this process and thread is root, set unison is returned,
 *         otherwise invalid set or null is returned.
 * @throws Mp4jException
 */
public <T> Set<T> reduceSetUnion(Set<T> setData, int rootRank, int rootThreadId, Serializer<T> elementSerializer, Class<T> elementType) throws Mp4jException {
    Map<String, Set<T>> mapTemp = new HashMap<>(1);
    mapTemp.put("key", setData);

    Map<String, Set<T>> mapReturn = reduceMapSetUnion(mapTemp, rootRank, rootThreadId, elementSerializer, elementType);
    if (mapReturn != null) {
        return mapReturn.get("key");
    } else {
        return null;
    }
}
 
Example #19
Source File: KryoRegistration.java    From flink with Apache License 2.0 5 votes vote down vote up
public KryoRegistration(Class<?> registeredClass, Class<? extends Serializer<?>> serializerClass) {
	this.registeredClass = Preconditions.checkNotNull(registeredClass);

	this.serializerClass = Preconditions.checkNotNull(serializerClass);
	this.serializableSerializerInstance = null;

	this.serializerDefinitionType = SerializerDefinitionType.CLASS;
}
 
Example #20
Source File: ShortOperand.java    From ytk-mp4j with MIT License 5 votes vote down vote up
@Override
public void send(Output output, MetaData metaData) throws IOException, Mp4jException {
    Kryo kryo = KryoUtils.getKryo();
    try {
        switch (container) {
            case ARRAY:
                Serializer arrSerializer = new ShortOperand.Mp4jShortArraySerializer(metaData.convertToArrayMetaData());
                if (compress) {
                    arrSerializer = new DeflateSerializer(arrSerializer);
                }
                arrSerializer.write(kryo, output, null);
                break;
            case MAP:
                Serializer mapSerializer = new ShortOperand.Mp4jShortMapSerializer(metaData.convertToMapMetaData());
                if (compress) {
                    mapSerializer = new DeflateSerializer(mapSerializer);
                }
                mapSerializer.write(kryo, output, null);

                break;
            default:
                throw new Mp4jException("unsupported container:" + container);
        }
    } catch (Exception e) {
        LOG.error("send exception", e);
        throw new Mp4jException(e);
    } finally {
        output.close();
    }


}
 
Example #21
Source File: StringOperand.java    From ytk-mp4j with MIT License 5 votes vote down vote up
@Override
public void send(Output output, MetaData metaData) throws IOException, Mp4jException {
    Kryo kryo = KryoUtils.getKryo();
    try {
        switch (container) {
            case ARRAY:
                Serializer arrSerializer = new StringOperand.Mp4jStringArraySerializer(metaData.convertToArrayMetaData());
                if (compress) {
                    arrSerializer = new DeflateSerializer(arrSerializer);
                }
                arrSerializer.write(kryo, output, null);
                break;
            case MAP:
                Serializer mapSerializer = new StringOperand.Mp4jStringMapSerializer(metaData.convertToMapMetaData());
                if (compress) {
                    mapSerializer = new DeflateSerializer(mapSerializer);
                }
                mapSerializer.write(kryo, output, null);

                break;
            default:
                throw new Mp4jException("unsupported container:" + container);
        }
    } catch (Exception e) {
        LOG.error("send exception", e);
        throw new Mp4jException(e);
    } finally {
        output.close();
    }


}
 
Example #22
Source File: KryoRegistration.java    From flink with Apache License 2.0 5 votes vote down vote up
public KryoRegistration(Class<?> registeredClass, Class<? extends Serializer<?>> serializerClass) {
	this.registeredClass = Preconditions.checkNotNull(registeredClass);

	this.serializerClass = Preconditions.checkNotNull(serializerClass);
	this.serializableSerializerInstance = null;

	this.serializerDefinitionType = SerializerDefinitionType.CLASS;
}
 
Example #23
Source File: ProcessCommSlave.java    From ytk-mp4j with MIT License 5 votes vote down vote up
/**
 * Set union
 * @param setData set data
 * @param rootRank root rank
 * @param elementSerializer element object Kryo serializer
 * @param elementType element object class

 * @return if this process is root, set unison is returned,
 *         otherwise invalid set or null is returned.
 * @throws Mp4jException
 */
public <T> Set<T> reduceSetUnion(Set<T> setData, int rootRank, Serializer<T> elementSerializer, Class<T> elementType) throws Mp4jException {
    Map<String, Set<T>> mapTemp = new HashMap<>(1);
    mapTemp.put("key", setData);

    Map<String, Set<T>> mapReturn = reduceMapSetUnion(mapTemp, rootRank, elementSerializer, elementType);
    if (mapReturn != null) {
        return mapReturn.get("key");
    } else {
        return null;
    }
}
 
Example #24
Source File: KryoSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method that takes lists of registered types and their serializers, and resolve
 * them into a single list such that the result will resemble the final registration
 * result in Kryo.
 */
private static LinkedHashMap<String, KryoRegistration> buildKryoRegistrations(
		Class<?> serializedType,
		LinkedHashSet<Class<?>> registeredTypes,
		LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> registeredTypesWithSerializerClasses,
		LinkedHashMap<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypesWithSerializers) {

	final LinkedHashMap<String, KryoRegistration> kryoRegistrations = new LinkedHashMap<>();

	kryoRegistrations.put(serializedType.getName(), new KryoRegistration(serializedType));

	for (Class<?> registeredType : checkNotNull(registeredTypes)) {
		kryoRegistrations.put(registeredType.getName(), new KryoRegistration(registeredType));
	}

	for (Map.Entry<Class<?>, Class<? extends Serializer<?>>> registeredTypeWithSerializerClassEntry :
			checkNotNull(registeredTypesWithSerializerClasses).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerClassEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerClassEntry.getKey(),
						registeredTypeWithSerializerClassEntry.getValue()));
	}

	for (Map.Entry<Class<?>, ExecutionConfig.SerializableSerializer<?>> registeredTypeWithSerializerEntry :
			checkNotNull(registeredTypesWithSerializers).entrySet()) {

		kryoRegistrations.put(
				registeredTypeWithSerializerEntry.getKey().getName(),
				new KryoRegistration(
						registeredTypeWithSerializerEntry.getKey(),
						registeredTypeWithSerializerEntry.getValue()));
	}

	// add Avro support if flink-avro is available; a dummy otherwise
	AvroUtils.getAvroUtils().addAvroGenericDataArrayRegistration(kryoRegistrations);

	return kryoRegistrations;
}
 
Example #25
Source File: ExecutionConfig.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a new Kryo default serializer to the Runtime.
 *
 * @param type The class of the types serialized with the given serializer.
 * @param serializerClass The class of the serializer to use.
 */
public void addDefaultKryoSerializer(Class<?> type, Class<? extends Serializer<?>> serializerClass) {
	if (type == null || serializerClass == null) {
		throw new NullPointerException("Cannot register null class or serializer.");
	}
	defaultKryoSerializerClasses.put(type, serializerClass);
}
 
Example #26
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 5 votes vote down vote up
private TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(KryoSerializer<T> newSerializer) {
	// merge the default serializers
	final MergeResult<Class<?>, SerializableSerializer<?>> reconfiguredDefaultKryoSerializers = mergeRightIntoLeft(
		snapshotData.getDefaultKryoSerializers(),
		optionalMapOf(newSerializer.getDefaultKryoSerializers(), Class::getName));

	if (reconfiguredDefaultKryoSerializers.hasMissingKeys()) {
		logMissingKeys(reconfiguredDefaultKryoSerializers);
		return TypeSerializerSchemaCompatibility.incompatible();
	}

	// merge default serializer classes
	final MergeResult<Class<?>, Class<? extends Serializer<?>>> reconfiguredDefaultKryoSerializerClasses = mergeRightIntoLeft(
		snapshotData.getDefaultKryoSerializerClasses(),
		optionalMapOf(newSerializer.getDefaultKryoSerializerClasses(), Class::getName));

	if (reconfiguredDefaultKryoSerializerClasses.hasMissingKeys()) {
		logMissingKeys(reconfiguredDefaultKryoSerializerClasses);
		return TypeSerializerSchemaCompatibility.incompatible();
	}

	// merge registration
	final MergeResult<String, KryoRegistration> reconfiguredRegistrations = mergeRightIntoLeft(
		snapshotData.getKryoRegistrations(),
		optionalMapOf(newSerializer.getKryoRegistrations(), Function.identity()));

	if (reconfiguredRegistrations.hasMissingKeys()) {
		logMissingKeys(reconfiguredRegistrations);
		return TypeSerializerSchemaCompatibility.incompatible();
	}

	// there are no missing keys, now we have to decide whether we are compatible as-is or we require reconfiguration.
	return resolveSchemaCompatibility(
		reconfiguredDefaultKryoSerializers,
		reconfiguredDefaultKryoSerializerClasses,
		reconfiguredRegistrations);
}
 
Example #27
Source File: KryoSerializerSnapshot.java    From flink with Apache License 2.0 5 votes vote down vote up
KryoSerializerSnapshot(Class<T> typeClass,
		LinkedHashMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers,
		LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses,
		LinkedHashMap<String, KryoRegistration> kryoRegistrations) {

	this.snapshotData = createFrom(typeClass, defaultKryoSerializers, defaultKryoSerializerClasses, kryoRegistrations);
}
 
Example #28
Source File: DefaultKryoFactory.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public Serializer getDefaultSerializer(Class type) {
    if (_override) {
        return new SerializableSerializer();
    } else {
        return super.getDefaultSerializer(type);
    }
}
 
Example #29
Source File: DoubleOperand.java    From ytk-mp4j with MIT License 5 votes vote down vote up
@Override
public void send(Output output, MetaData metaData) throws IOException, Mp4jException {
    Kryo kryo = KryoUtils.getKryo();
    try {
        switch (container) {
            case ARRAY:
                Serializer arrSerializer = new Mp4jDoubleArraySerializer(metaData.convertToArrayMetaData());
                if (compress) {
                    arrSerializer = new DeflateSerializer(arrSerializer);
                }

                arrSerializer.write(kryo, output, null);
                break;
            case MAP:
                Serializer mapSerializer = new Mp4jDoubleMapSerializer(metaData.convertToMapMetaData());
                if (compress) {
                    mapSerializer = new DeflateSerializer(mapSerializer);
                }
                mapSerializer.write(kryo, output, null);

                break;
            default:
                throw new Mp4jException("unsupported container:" + container);
        }
    } catch (Exception e) {
        LOG.error("send exception", e);
        throw new Mp4jException(e);
    } finally {
        output.close();
    }
    

}
 
Example #30
Source File: KryoUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Apply a list of {@link KryoRegistration} to a Kryo instance. The list of registrations is
 * assumed to already be a final resolution of all possible registration overwrites.
 *
 * <p>The registrations are applied in the given order and always specify the registration id as
 * the next available id in the Kryo instance (providing the id just extra ensures nothing is
 * overwritten, and isn't strictly required);
 *
 * @param kryo the Kryo instance to apply the registrations
 * @param resolvedRegistrations the registrations, which should already be resolved of all possible registration overwrites
 */
public static void applyRegistrations(Kryo kryo, Collection<KryoRegistration> resolvedRegistrations) {

	Serializer<?> serializer;
	for (KryoRegistration registration : resolvedRegistrations) {
		serializer = registration.getSerializer(kryo);

		if (serializer != null) {
			kryo.register(registration.getRegisteredClass(), serializer, kryo.getNextRegistrationId());
		} else {
			kryo.register(registration.getRegisteredClass(), kryo.getNextRegistrationId());
		}
	}
}