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();
}
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;
}
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;
}
}
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;
}
}
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;
}
Aggregations