Search in sources :

Example 51 with Kryo

use of com.esotericsoftware.kryo.Kryo in project storm by apache.

the class KinesisConnectionInfo method getKryoSerializedBytes.

private byte[] getKryoSerializedBytes(final Object obj) {
    final Kryo kryo = new Kryo();
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final Output output = new Output(os);
    kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
    kryo.writeClassAndObject(output, obj);
    output.flush();
    return os.toByteArray();
}
Also used : StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Kryo(com.esotericsoftware.kryo.Kryo)

Example 52 with Kryo

use of com.esotericsoftware.kryo.Kryo in project potato by eyeem.

the class KryoTransportLayer method loadSync.

public boolean loadSync(Storage.List storageList) {
    Kryo kyro = new Kryo();
    Storage storage = storageList.getStorage();
    Class klazz = storage.classname();
    try {
        Input input = new Input(new FileInputStream(filename(storageList)));
        HashMap<String, Object> data = kyro.readObject(input, HashMap.class);
        ArrayList list = (ArrayList) data.get("list");
        input.close();
        Storage.List transaction = storageList.transaction();
        transaction.meta = (HashMap<String, Object>) data.get("meta");
        // don't add objects that already exist in cache as they're most likely fresher
        for (Object loadedObject : list) {
            Object storedObject = storage.get(storage.id(loadedObject));
            transaction.add(storedObject != null ? storedObject : loadedObject);
        }
        transaction.commit(new Storage.Subscription.Action(Storage.Subscription.LOADED));
        return true;
    } catch (FileNotFoundException e) {
        // clean up
        deleteFilesRecursively(getBaseDir(klazz), klazz);
        Log.w(klazz.getSimpleName(), "load() error: file " + filename(storageList) + " missing");
    } catch (Throwable e) {
        Log.e(klazz.getSimpleName(), "load() error", e);
    }
    // prolly should be other thing
    storageList.publish(new Storage.Subscription.Action(Storage.Subscription.LOADED));
    return false;
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) Input(com.esotericsoftware.kryo.io.Input) ObjectStreamClass(java.io.ObjectStreamClass) Kryo(com.esotericsoftware.kryo.Kryo)

Example 53 with Kryo

use of com.esotericsoftware.kryo.Kryo in project potato by eyeem.

the class KryoTransportLayer method saveSync.

public boolean saveSync(Storage.List list, int limit) {
    Class klazz = null;
    try {
        klazz = list.getStorage().classname();
        File dir = new File(dirname(klazz));
        dir.mkdirs();
        Kryo kyro = new Kryo();
        Output output;
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("list", list.toArrayList(limit));
        data.put("meta", list.meta);
        output = new Output(new FileOutputStream(filename(list)));
        kyro.writeObject(output, data);
        output.close();
        return true;
    } catch (Throwable e) {
        if (klazz != null)
            Log.e(klazz.getSimpleName(), "save() error", e);
        return false;
    }
}
Also used : HashMap(java.util.HashMap) Output(com.esotericsoftware.kryo.io.Output) FileOutputStream(java.io.FileOutputStream) ObjectStreamClass(java.io.ObjectStreamClass) File(java.io.File) Kryo(com.esotericsoftware.kryo.Kryo)

Example 54 with Kryo

use of com.esotericsoftware.kryo.Kryo in project carbonite by eveliotc.

the class CacheFactoryImp method buildDefault.

@SuppressWarnings("unchecked")
private Cache<String, T> buildDefault(CacheBuilder options) {
    final CacheType cacheType = options.cacheType();
    switch(cacheType) {
        case MEMORY:
            return new MemoryLruCache<String, T>(new MemoryLruCache.Options(LRU_SIZE));
        case STORAGE:
            // TODO yikes a builder or something, plus kryo needs more config
            final Class type = options.type();
            final File dir = buildCacheDir(options.context(), type);
            final Serializer<T> serializer = new KryoSerializer<T>(new Kryo(), type);
            final Options storageOpts = new Options(dir, MINIMAL_CAPACITY, type, serializer);
            return new StorageLruCache<T>(storageOpts);
        default:
            illegalState(true, "Not yet implemented cache type " + cacheType);
            return null;
    }
}
Also used : MemoryLruCache(info.evelio.carbonite.cache.MemoryLruCache) Options(info.evelio.carbonite.cache.StorageLruCache.Options) CacheOptions(info.evelio.carbonite.cache.CacheOptions) StorageLruCache(info.evelio.carbonite.cache.StorageLruCache) File(java.io.File) Kryo(com.esotericsoftware.kryo.Kryo) CacheType(info.evelio.carbonite.cache.CacheType) KryoSerializer(info.evelio.carbonite.serialization.KryoSerializer)

Example 55 with Kryo

use of com.esotericsoftware.kryo.Kryo in project storm by apache.

the class SerializationFactory method getKryo.

public static Kryo getKryo(Map conf) {
    IKryoFactory kryoFactory = (IKryoFactory) Utils.newInstance((String) conf.get(Config.TOPOLOGY_KRYO_FACTORY));
    Kryo k = kryoFactory.getKryo(conf);
    k.register(byte[].class);
    /* tuple payload serializer is specified via configuration */
    String payloadSerializerName = (String) conf.get(Config.TOPOLOGY_TUPLE_SERIALIZER);
    try {
        Class serializerClass = Class.forName(payloadSerializerName);
        Serializer serializer = resolveSerializerInstance(k, ListDelegate.class, serializerClass, conf);
        k.register(ListDelegate.class, serializer);
    } catch (ClassNotFoundException ex) {
        throw new RuntimeException(ex);
    }
    k.register(ArrayList.class, new ArrayListSerializer());
    k.register(HashMap.class, new HashMapSerializer());
    k.register(HashSet.class, new HashSetSerializer());
    k.register(BigInteger.class, new BigIntegerSerializer());
    k.register(TransactionAttempt.class);
    k.register(Values.class);
    k.register(org.apache.storm.metric.api.IMetricsConsumer.DataPoint.class);
    k.register(org.apache.storm.metric.api.IMetricsConsumer.TaskInfo.class);
    k.register(ConsList.class);
    try {
        JavaBridge.registerPrimitives(k);
        JavaBridge.registerCollections(k);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Map<String, String> registrations = normalizeKryoRegister(conf);
    kryoFactory.preRegister(k, conf);
    boolean skipMissing = (Boolean) conf.get(Config.TOPOLOGY_SKIP_MISSING_KRYO_REGISTRATIONS);
    for (Map.Entry<String, String> entry : registrations.entrySet()) {
        String serializerClassName = entry.getValue();
        try {
            Class klass = Class.forName(entry.getKey());
            Class serializerClass = null;
            if (serializerClassName != null)
                serializerClass = Class.forName(serializerClassName);
            if (serializerClass == null) {
                k.register(klass);
            } else {
                k.register(klass, resolveSerializerInstance(k, klass, serializerClass, conf));
            }
        } catch (ClassNotFoundException e) {
            if (skipMissing) {
                LOG.info("Could not find serialization or class for " + serializerClassName + ". Skipping registration...");
            } else {
                throw new RuntimeException(e);
            }
        }
    }
    kryoFactory.postRegister(k, conf);
    if (conf.get(Config.TOPOLOGY_KRYO_DECORATORS) != null) {
        for (String klassName : (List<String>) conf.get(Config.TOPOLOGY_KRYO_DECORATORS)) {
            try {
                Class klass = Class.forName(klassName);
                IKryoDecorator decorator = (IKryoDecorator) klass.newInstance();
                decorator.decorate(k);
            } catch (ClassNotFoundException e) {
                if (skipMissing) {
                    LOG.info("Could not find kryo decorator named " + klassName + ". Skipping registration...");
                } else {
                    throw new RuntimeException(e);
                }
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
    kryoFactory.postDecorate(k, conf);
    return k;
}
Also used : ArrayListSerializer(org.apache.storm.serialization.types.ArrayListSerializer) HashMapSerializer(org.apache.storm.serialization.types.HashMapSerializer) HashSetSerializer(org.apache.storm.serialization.types.HashSetSerializer) BigIntegerSerializer(com.esotericsoftware.kryo.serializers.DefaultSerializers.BigIntegerSerializer) ArrayList(java.util.ArrayList) ConsList(org.apache.storm.trident.tuple.ConsList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Kryo(com.esotericsoftware.kryo.Kryo) ArrayListSerializer(org.apache.storm.serialization.types.ArrayListSerializer) Serializer(com.esotericsoftware.kryo.Serializer) BigIntegerSerializer(com.esotericsoftware.kryo.serializers.DefaultSerializers.BigIntegerSerializer) HashMapSerializer(org.apache.storm.serialization.types.HashMapSerializer) HashSetSerializer(org.apache.storm.serialization.types.HashSetSerializer)

Aggregations

Kryo (com.esotericsoftware.kryo.Kryo)94 Input (com.esotericsoftware.kryo.io.Input)37 Output (com.esotericsoftware.kryo.io.Output)34 Test (org.junit.Test)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)14 File (java.io.File)10 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)10 List (java.util.List)9 Map (java.util.Map)8 Test (org.testng.annotations.Test)8 ArrayList (java.util.ArrayList)7 Path (org.apache.hadoop.fs.Path)7 BigIntegerSerializer (com.esotericsoftware.kryo.serializers.DefaultSerializers.BigIntegerSerializer)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 DefaultPartition (com.datatorrent.api.DefaultPartition)4 CountDownLatch (java.util.concurrent.CountDownLatch)4