本文整理了Java中org.apache.tinkerpop.shaded.kryo.Kryo
类的一些代码示例,展示了Kryo
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Kryo
类的具体详情如下:
包路径:org.apache.tinkerpop.shaded.kryo.Kryo
类名称:Kryo
暂无
代码示例来源:origin: JanusGraph/janusgraph
@Override
public P read(Kryo kryo, Input input, Class<P> aClass) {
final String predicate = input.readString();
final boolean isCollection = input.readByte() == (byte) 0;
final Object value;
if (isCollection) {
value = new ArrayList();
final int size = input.readInt();
for (int ix = 0; ix < size; ix++) {
((List) value).add(kryo.readClassAndObject(input));
}
} else {
value = kryo.readClassAndObject(input);
}
try {
return createPredicateWithValue(predicate, value);
} catch (final Exception e) {
log.info("Couldn't deserialize class: " + aClass + ", predicate: " + predicate + ", isCollection: "
+ isCollection + ",value: " + value, e);
throw new IllegalStateException(e.getMessage(), e);
}
}
代码示例来源:origin: JanusGraph/janusgraph
@Override
public void write(Kryo kryo, Output output, P p) {
output.writeString(
p instanceof ConnectiveP ? (p instanceof AndP ? "and" : "or") : p.getBiPredicate().toString());
if (p instanceof ConnectiveP || p.getValue() instanceof Collection) {
output.writeByte((byte) 0);
final Collection<?> coll = p instanceof ConnectiveP ? ((ConnectiveP<?>) p).getPredicates()
: (Collection) p.getValue();
output.writeInt(coll.size());
coll.forEach(v -> kryo.writeClassAndObject(output, v));
} else {
output.writeByte((byte) 1);
kryo.writeClassAndObject(output, p.getValue());
}
}
代码示例来源:origin: hugegraph/hugegraph
@SuppressWarnings("unused")
private static Map<HugeKeys, Object> readEntry(Kryo kryo, Input input) {
int columnSize = input.readInt();
Map<HugeKeys, Object> map = new LinkedHashMap<>();
for (int i = 0; i < columnSize; i++) {
HugeKeys key = kryo.readObject(input, HugeKeys.class);
Object val = kryo.readClassAndObject(input);
map.put(key, val);
}
return map;
}
代码示例来源:origin: hugegraph/hugegraph
@Override
public void write(Kryo kryo, Output output, Optional<?> optional) {
if (optional.isPresent()) {
kryo.writeClassAndObject(output, optional.get());
} else {
kryo.writeObject(output, null);
}
}
代码示例来源:origin: hugegraph/hugegraph
public static Kryo kryo() {
Kryo kryo = kryos.get();
if (kryo != null) {
return kryo;
}
kryo = new Kryo();
kryo.addDefaultSerializer(UUID.class, new Serializer<UUID>() {
@Override
public UUID read(Kryo kryo, Input input, Class<UUID> c) {
return new UUID(input.readLong(), input.readLong());
}
@Override
public void write(Kryo kryo, Output output, UUID uuid) {
output.writeLong(uuid.getMostSignificantBits());
output.writeLong(uuid.getLeastSignificantBits());
}
});
kryos.set(kryo);
return kryo;
}
代码示例来源:origin: apache/tinkerpop
@Override
public <T> T readObject(final ShadedInputAdapter input, final Class<T> type) {
return shadedKryo.readObject(input.getShadedInput(), type);
}
代码示例来源:origin: apache/tinkerpop
@Override
public void writeObject(final ShadedOutputAdapter output, final Object object) {
shadedKryo.writeObject(output.getShadedOutput(), object);
}
代码示例来源:origin: apache/tinkerpop
@Override
public ResponseMessage deserializeResponse(final ByteBuf msg) throws SerializationException {
try {
final Kryo kryo = kryoThreadLocal.get();
final byte[] payload = new byte[msg.capacity()];
msg.readBytes(payload);
try (final Input input = new Input(payload)) {
final UUID requestId = kryo.readObjectOrNull(input, UUID.class);
final int status = input.readShort();
final String statusMsg = input.readString();
final Map<String,Object> statusAttributes = (Map<String,Object>) kryo.readClassAndObject(input);
final Object result = kryo.readClassAndObject(input);
final Map<String,Object> metaAttributes = (Map<String,Object>) kryo.readClassAndObject(input);
return ResponseMessage.build(requestId)
.code(ResponseStatusCode.getFromValue(status))
.statusMessage(statusMsg)
.statusAttributes(statusAttributes)
.result(result)
.responseMetaData(metaAttributes)
.create();
}
} catch (Exception ex) {
logger.warn(String.format("Response [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
throw new SerializationException(ex);
}
}
代码示例来源:origin: apache/tinkerpop
@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, final ByteBufAllocator allocator) throws SerializationException {
ByteBuf encodedMessage = null;
try {
final Kryo kryo = kryoThreadLocal.get();
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final Output output = new Output(baos, bufferSize);
// request id - if present
kryo.writeObjectOrNull(output, responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class);
// status
output.writeShort(responseMessage.getStatus().getCode().getValue());
output.writeString(responseMessage.getStatus().getMessage());
kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes());
// result
kryo.writeClassAndObject(output, serializeToString ? serializeResultToString(responseMessage) : responseMessage.getResult().getData());
kryo.writeClassAndObject(output, responseMessage.getResult().getMeta());
final long size = output.total();
if (size > Integer.MAX_VALUE)
throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size));
output.flush();
encodedMessage = allocator.buffer((int) size);
encodedMessage.writeBytes(baos.toByteArray());
}
return encodedMessage;
} catch (Exception ex) {
if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);
logger.warn(String.format("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
throw new SerializationException(ex);
}
}
代码示例来源:origin: apache/tinkerpop
@Override
public <T> T deserialize(final ByteBuffer byteBuffer, final ClassLoader classLoader, final ClassTag<T> classTag) {
this.input.setBuffer(byteBuffer.array());
return this.gryoSerializer.getGryoPool().readWithKryo(kryo -> {
kryo.setClassLoader(classLoader);
return (T) kryo.readClassAndObject(this.input);
});
}
代码示例来源:origin: apache/tinkerpop
@Override
public Kryo createMapper() {
final Kryo kryo = new Kryo(classResolver.get(), new MapReferenceResolver(), new DefaultStreamFactory());
kryo.addDefaultSerializer(Map.Entry.class, new UtilSerializers.EntrySerializer());
kryo.setRegistrationRequired(registrationRequired);
kryo.setReferences(referenceTracking);
for (TypeRegistration tr : typeRegistrations)
tr.registerWith(kryo);
return kryo;
}
代码示例来源:origin: apache/tinkerpop
@Override
public void writeObjectOrNull(final ShadedOutputAdapter output, final Object object, final Class type) {
shadedKryo.writeObjectOrNull(output.getShadedOutput(), object, type);
}
}
代码示例来源:origin: apache/tinkerpop
@Override
public <T> T readObjectOrNull(final ShadedInputAdapter input, final Class<T> type) {
return shadedKryo.readObjectOrNull(input.getShadedInput(), type);
}
代码示例来源:origin: hugegraph/hugegraph
private static void writeEntry(Kryo kryo,
Output output,
Map<HugeKeys, Object> schema) {
/* Write columns size and data */
output.writeInt(schema.keySet().size());
for (Map.Entry<HugeKeys, Object> entry : schema.entrySet()) {
kryo.writeObject(output, entry.getKey());
kryo.writeClassAndObject(output, entry.getValue());
}
}
代码示例来源:origin: apache/tinkerpop
@Override
public RequestMessage deserializeRequest(final ByteBuf msg) throws SerializationException {
try {
final Kryo kryo = kryoThreadLocal.get();
final byte[] payload = new byte[msg.readableBytes()];
msg.readBytes(payload);
try (final Input input = new Input(payload)) {
// by the time the message gets here, the mime length/type have been already read, so this part just
// needs to process the payload.
return kryo.readObject(input, RequestMessage.class);
}
} catch (Exception ex) {
logger.warn(String.format("Request [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV3d0.class.getName()), ex);
throw new SerializationException(ex);
}
}
代码示例来源:origin: hugegraph/hugegraph
public static byte[] toKryo(Object value) {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
Output output = new Output(bos, 256)) {
kryo().writeObject(output, value);
output.flush();
return bos.toByteArray();
} catch (IOException e) {
throw new BackendException("Failed to serialize: %s", e, value);
}
}
代码示例来源:origin: org.apache.tinkerpop/gremlin-driver
@Override
public ResponseMessage deserializeResponse(final ByteBuf msg) throws SerializationException {
try {
final Kryo kryo = kryoThreadLocal.get();
final byte[] payload = new byte[msg.capacity()];
msg.readBytes(payload);
try (final Input input = new Input(payload)) {
final UUID requestId = kryo.readObjectOrNull(input, UUID.class);
final int status = input.readShort();
final String statusMsg = input.readString();
final Map<String,Object> statusAttributes = (Map<String,Object>) kryo.readClassAndObject(input);
final Object result = kryo.readClassAndObject(input);
final Map<String,Object> metaAttributes = (Map<String,Object>) kryo.readClassAndObject(input);
return ResponseMessage.build(requestId)
.code(ResponseStatusCode.getFromValue(status))
.statusMessage(statusMsg)
.statusAttributes(statusAttributes)
.result(result)
.responseMetaData(metaAttributes)
.create();
}
} catch (Exception ex) {
logger.warn(String.format("Response [%s] could not be deserialized by %s.", msg, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
throw new SerializationException(ex);
}
}
代码示例来源:origin: org.apache.tinkerpop/gremlin-driver
@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, final ByteBufAllocator allocator) throws SerializationException {
ByteBuf encodedMessage = null;
try {
final Kryo kryo = kryoThreadLocal.get();
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final Output output = new Output(baos, bufferSize);
// request id - if present
kryo.writeObjectOrNull(output, responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null, UUID.class);
// status
output.writeShort(responseMessage.getStatus().getCode().getValue());
output.writeString(responseMessage.getStatus().getMessage());
kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes());
// result
kryo.writeClassAndObject(output, serializeToString ? serializeResultToString(responseMessage) : responseMessage.getResult().getData());
kryo.writeClassAndObject(output, responseMessage.getResult().getMeta());
final long size = output.total();
if (size > Integer.MAX_VALUE)
throw new SerializationException(String.format("Message size of %s exceeds allocatable space", size));
output.flush();
encodedMessage = allocator.buffer((int) size);
encodedMessage.writeBytes(baos.toByteArray());
}
return encodedMessage;
} catch (Exception ex) {
if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);
logger.warn(String.format("Response [%s] could not be serialized by %s.", responseMessage, AbstractGryoMessageSerializerV1d0.class.getName()), ex);
throw new SerializationException(ex);
}
}
代码示例来源:origin: org.apache.tinkerpop/gremlin-core
@Override
public Kryo createMapper() {
final Kryo kryo = new Kryo(classResolver.get(), new MapReferenceResolver(), new DefaultStreamFactory());
kryo.addDefaultSerializer(Map.Entry.class, new UtilSerializers.EntrySerializer());
kryo.setRegistrationRequired(registrationRequired);
kryo.setReferences(referenceTracking);
for (TypeRegistration tr : typeRegistrations)
tr.registerWith(kryo);
return kryo;
}
代码示例来源:origin: com.baidu.hugegraph/hugegraph-core
public static Kryo kryo() {
Kryo kryo = kryos.get();
if (kryo != null) {
return kryo;
}
kryo = new Kryo();
kryo.addDefaultSerializer(UUID.class, new Serializer<UUID>() {
@Override
public UUID read(Kryo kryo, Input input, Class<UUID> c) {
return new UUID(input.readLong(), input.readLong());
}
@Override
public void write(Kryo kryo, Output output, UUID uuid) {
output.writeLong(uuid.getMostSignificantBits());
output.writeLong(uuid.getLeastSignificantBits());
}
});
kryos.set(kryo);
return kryo;
}
内容来源于网络,如有侵权,请联系作者删除!