io.protostuff.Message Java Examples

The following examples show how to use io.protostuff.Message. 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: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema() {
	Schema<T> schema = this.schema;
	if (schema == null) {
		synchronized (this) {
			if ((schema = this.schema) == null) {
				if (Message.class.isAssignableFrom(typeClass)) {
					// use the message's schema.
					Message<T> m = (Message<T>) createMessageInstance(typeClass);
					this.schema = schema = m.cachedSchema();
				} else {
					// create new
					this.schema = schema = strategy.newSchema(typeClass);
				}
			}
		}
	}

	return schema;
}
 
Example #2
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber,
        Message<T> message) throws IOException
{
    int id;
    BaseHS<T> wrapper = (BaseHS<T>) getSchemaWrapper(message.getClass(), true);

    // wait till everything is completely set
    while (0 == (id = wrapper.id))
        LockSupport.parkNanos(1);

    output.writeUInt32(fieldNumber, id, false);

    // TODO allow the wrapper to return an override schema?
    return message.cachedSchema();
}
 
Example #3
Source File: ProtostuffUtil.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Clone  a Protostuff object
 *
 * @param t the protobuf message to copy
 * @return a deep copy of {@code t}
 */
public static <T extends Message<T>> T copy(T t) {
  try {
    Schema<T> schema = t.cachedSchema();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GraphIOUtil.writeDelimitedTo(new DataOutputStream(out), t, schema);
    // TODO: avoid array copy
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    T newMessage = schema.newMessage();
    GraphIOUtil.mergeDelimitedFrom(in, newMessage, schema);
    return newMessage;
  } catch (IOException e) {
    throw UserException.dataReadError(e)
      .message("Failure decoding object, please ensure that you ran dremio-admin upgrade on Dremio.")
      .build(logger);
  }
}
 
Example #4
Source File: ProtostuffEncoder.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, Message<T> msg, List<Object> out) throws Exception {
  Schema<T> schema = msg.cachedSchema();

  LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput();
  schema.writeTo(lcpo, (T) msg);

  List<ByteBuffer> buffers = lcpo.buffer.finish();

  long size = lcpo.buffer.size();
  if (size > Integer.MAX_VALUE) {
    throw new EncoderException("Serialized form was too large, actual size: " + size);
  }

  out.add(Unpooled.wrappedBuffer(buffers.toArray(new ByteBuffer[]{})));
}
 
Example #5
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema()
{
    Schema<T> schema = this.schema;
    if (schema == null)
    {
        synchronized (this)
        {
            if ((schema = this.schema) == null)
            {
                if (Message.class.isAssignableFrom(typeClass))
                {
                    // use the message's schema.
                    final Message<T> m = (Message<T>) createMessageInstance(typeClass);
                    this.schema = schema = m.cachedSchema();
                }
                else
                {
                    // create new
                    this.schema = schema = strategy.newSchema(typeClass);
                }
            }
        }
    }

    return schema;
}
 
Example #6
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber,
        Message<T> message) throws IOException
{
    final BaseHS<T> wrapper = (BaseHS<T>) pojoMapping.get(message.getClass());

    if (wrapper == null)
        throw new UnknownTypeException("pojo: " + message.getClass());

    output.writeUInt32(fieldNumber, wrapper.id, false);

    // TODO allow the wrapper to return an override schema?
    return message.cachedSchema();
}
 
Example #7
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema()
{
    Schema<T> schema = this.schema;
    if (schema == null)
    {
        synchronized (this)
        {
            if ((schema = this.schema) == null)
            {
                if (Message.class.isAssignableFrom(typeClass))
                {
                    // use the message's schema.
                    final Message<T> m = (Message<T>) createMessageInstance(typeClass);
                    this.schema = schema = m.cachedSchema();
                }
                else
                {
                    // create new
                    this.schema = schema = strategy.newSchema(typeClass);
                }
            }
        }
    }

    return schema;
}
 
Example #8
Source File: CompatTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static <T extends Message<T>> Schema<T> getCachedSchema(Class<T> clazz)
        throws InstantiationException, IllegalAccessException
{
    Schema<T> schema = clazz.newInstance().cachedSchema();
    // System.err.println("! " + schema + " | " +
    // System.identityHashCode(schema));
    return schema;
}
 
Example #9
Source File: RuntimeSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Invoked only when applications are having pipe io operations.
 */
@SuppressWarnings("unchecked")
static <T> Pipe.Schema<T> resolvePipeSchema(Schema<T> schema,
        Class<? super T> clazz, boolean throwIfNone)
{
    if (Message.class.isAssignableFrom(clazz))
    {
        try
        {
            // use the pipe schema of code-generated messages if available.
            java.lang.reflect.Method m = clazz.getDeclaredMethod("getPipeSchema");
            return (Pipe.Schema<T>) m.invoke(null);
        }
        catch (Exception e)
        {
            // ignore
        }
    }

    if (RuntimeSchema.class.isAssignableFrom(schema.getClass()))
        return ((RuntimeSchema<T>) schema).getPipeSchema();

    if (throwIfNone)
        throw new RuntimeException("No pipe schema for: " + clazz);

    return null;
}
 
Example #10
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Schema<T> getSchema()
{
    Schema<T> schema = this.schema;
    if (schema == null)
    {
        synchronized (this)
        {
            if ((schema = this.schema) == null)
            {
                if (Message.class.isAssignableFrom(typeClass))
                {
                    // use the message's schema.
                    Message<T> m = (Message<T>) createMessageInstance(typeClass);
                    this.schema = schema = m.cachedSchema();
                }
                else
                {
                    // create new
                    this.schema = schema = strategy
                            .newSchema(typeClass);
                }
            }
        }
    }

    return schema;
}
 
Example #11
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber,
        Message<T> message) throws IOException
{
    output.writeString(fieldNumber, message.getClass().getName(), false);

    return message.cachedSchema();
}
 
Example #12
Source File: ObjectSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
private static boolean isComponentPojo(Class<?> componentType)
{
    return Message.class.isAssignableFrom(componentType) ||
            (!Throwable.class.isAssignableFrom(componentType) && 
                    !Map.class.isAssignableFrom(componentType) && 
                    !Collection.class.isAssignableFrom(componentType));
}
 
Example #13
Source File: RpcMessage.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
static Message getSubMsg(ReplicationWireMessage wireMessage) {
  if (wireMessage.getAppendEntries() != null) {
    return wireMessage.getAppendEntries();
  }

  if (wireMessage.getAppendEntriesReply() != null) {
    return wireMessage.getAppendEntriesReply();
  }

  if (wireMessage.getRequestVote() != null) {
    return wireMessage.getRequestVote();
  }

  if (wireMessage.getRequestVoteReply() != null) {
    return wireMessage.getRequestVoteReply();
  }

  if (wireMessage.getPreElectionPoll() != null) {
    return wireMessage.getPreElectionPoll();
  }

  if (wireMessage.getPreElectionReply() != null) {
    return wireMessage.getPreElectionReply();
  }

  return null;
}
 
Example #14
Source File: OLogProtostuffContent.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
public static <T extends Schema<T> & Message<T>> OLogProtostuffContent<T> deserialize(ByteBuffer buffer,
                                                                                      Schema<T> schema) {
  final ByteBufferInput input = new ByteBufferInput(buffer, false);
  final T message = schema.newMessage();

  try {
    schema.mergeFrom(input, message);
    return new OLogProtostuffContent<>(message);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #15
Source File: RpcMessage.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
protected RpcMessage(long to, long from, String quorumId, Message message) {
  this.to = to;
  this.from = from;
  this.quorumId = quorumId;

  this.message = message;
}
 
Example #16
Source File: ProtostuffMessageBodyWriter.java    From datawave with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void writeTo(Object message, Class<?> clazz, Type type, Annotation[] annotations, MediaType media, MultivaluedMap<String,Object> httpHeaders,
                OutputStream out) throws IOException, WebApplicationException {
    
    // TODO: Figure out a method to add the proto file location in the response headers.
    // This map must be mofified before any data is written to out,
    // since at that time the response headers will be flushed.
    
    Schema<Object> schema = null;
    if (message instanceof Message) {
        Message<Object> msg = (Message<Object>) message;
        schema = msg.cachedSchema();
    } else {
        schema = (Schema<Object>) RuntimeSchema.getSchema(clazz);
    }
    
    try {
        if (MediaType.APPLICATION_XML_TYPE.equals(media) || MediaType.TEXT_XML_TYPE.equals(media)) {
            XmlIOUtil.writeTo(out, message, schema);
        } else if ("text/yaml".equals(media.toString()) || "text/x-yaml".equals(media.toString()) || "application/x-yaml".equals(media.toString())) {
            YamlIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protobuf".equals(media.toString())) {
            ProtobufIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protostuff".equals(media.toString())) {
            ProtostuffIOUtil.writeTo(out, message, schema, buffer);
        } else if (MediaType.APPLICATION_JSON_TYPE.equals(media)) {
            IOContext ctx = new IOContext(JsonIOUtil.DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);
            UTF8JsonGenerator generator = new UTF8JsonGenerator(ctx, JsonIOUtil.DEFAULT_JSON_FACTORY.getGeneratorFeatures(),
                            JsonIOUtil.DEFAULT_JSON_FACTORY.getCodec(), out);
            try {
                JsonIOUtil.writeTo(generator, message, schema, false);
            } finally {
                generator.close();
            }
        }
    } finally {
        buffer.clear();
    }
}
 
Example #17
Source File: ProtostuffUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param to immutable
 * @param from immutable
 * @param <T>
 * @return result of merge from into to
 */
public static <T extends Message<T>> T merge(T to, T from) throws IOException {
  Schema<T> schema = from.cachedSchema();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  GraphIOUtil.writeDelimitedTo(new DataOutputStream(out), from, schema);
  ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  T cloneTo = copy(to);
  GraphIOUtil.mergeDelimitedFrom(in, cloneTo, schema);
  return cloneTo;
}
 
Example #18
Source File: JobsProtoUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Generic method to convert Protostuff to Protobuf. Uses LegacyProtobufSerializer because deserializing with the
 * regular protobuf serializer does not handle repeated fields correctly.
 * @param protobufParser Parser for protobuf object
 * @param protostuff Protostuff object to convert
 * @param <M> Type of Protobuf
 * @param <T> Type of Protobuff
 * @return Converted object as Protobuf
 */
private static <M extends GeneratedMessageV3, T extends Message<T> & Schema<T>>
M toBuf(Parser<M> protobufParser, T protostuff) {
  try {
    LinkedBuffer buffer = LinkedBuffer.allocate();
    byte[] bytes = ProtobufIOUtil.toByteArray(protostuff, protostuff.cachedSchema(), buffer);
    // LegacyProtobufSerializer is necessary as it deals with stuff/buf grouping differences
    return LegacyProtobufSerializer.parseFrom(protobufParser, bytes);
  } catch (InvalidProtocolBufferException e) {
    throw new IllegalArgumentException("Cannot convert from protostuff to protobuf");
  }
}
 
Example #19
Source File: FastIdStrategy.java    From turbo-rpc with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> Schema<T> writeMessageIdTo(Output output, int fieldNumber, Message<T> message) throws IOException {
	output.writeString(fieldNumber, message.getClass().getName(), false);

	return message.cachedSchema();
}
 
Example #20
Source File: ProtostuffMessageBodyWriter.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isWriteable(Class<?> clazz, Type type, Annotation[] annotations, MediaType media) {
    return Message.class.isAssignableFrom(clazz);
}
 
Example #21
Source File: SerializerFactory.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
<P extends Message<P>> Serializer<P, STORE> visitProtostuffFormat(Class<P> clazz) throws DatastoreFatalException;
 
Example #22
Source File: RuntimeFieldFactory.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the runtime field factory of the given {@code clazz}.
 */
public static RuntimeFieldFactory<?> getFieldFactory(Class<?> clazz,
        IdStrategy strategy)
{
    if (strategy.isDelegateRegistered(clazz))
        return DELEGATE;

    final RuntimeFieldFactory<?> inline = __inlineValues.get(clazz
            .getName());
    if (inline != null)
        return inline;

    if (Message.class.isAssignableFrom(clazz))
        return POJO;

    if (clazz.isEnum())
        return ENUM;

    // Of all the scalar (inline) fields, java.lang.Number is the only
    // abstract
    // super type, hence we can filter it here
    // Note that it has 10 built-in subtypes
    if (clazz.isArray() || Object.class == clazz || Number.class == clazz
            || Class.class == clazz || Enum.class == clazz)
    {
        return OBJECT;
    }
    
    // allow user to register a custom schema for throwable/map/collection
    
    if (strategy.isRegistered(clazz))
        return clazz.isInterface() ? POJO : POLYMORPHIC_POJO;
    
    if (Throwable.class.isAssignableFrom(clazz))
        return OBJECT;
    
    if (Map.class.isAssignableFrom(clazz))
        return RuntimeMapFieldFactory.MAP;

    if (Collection.class.isAssignableFrom(clazz))
    {
        // repeated fields.
        return COLLECTION;
    }

    // Enums or boxed types of primitives do implement interfaces.
    // Although serializing polymorphic pojos declared as interfaces will be
    // a
    // little bit slower than before, this is more correct.
    //
    // In versions prior to 1.0.5, it would serialize a field declared as
    // java.lang.Serializable like a polymorphic pojo even though it
    // gets assigned enum/string/number types.
    //
    // If you have declared fields as serializable, it wont be compatible
    if (clazz.isInterface())
        return OBJECT;

    // checks delegated to POLYMORPHIC_POJO
    return POLYMORPHIC_POJO;
}
 
Example #23
Source File: SerializerFactory.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
<P extends com.google.protobuf.Message> Serializer<P, STORE> visitProtobufFormat(Class<P> clazz) throws DatastoreFatalException;
 
Example #24
Source File: Serializer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static <IN extends Message<IN>> Serializer<IN, byte[]> of(Schema<IN> schema) {
  return new ProtostuffSerializer<>(schema);
}
 
Example #25
Source File: IdStrategy.java    From protostuff with Apache License 2.0 4 votes vote down vote up
protected abstract <T> Schema<T> writeMessageIdTo(Output output,
int fieldNumber, Message<T> message) throws IOException;
 
Example #26
Source File: RuntimeFieldFactory.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the runtime field factory of the given {@code clazz}.
 */
public static RuntimeFieldFactory<?> getFieldFactory(Class<?> clazz,
        IdStrategy strategy)
{
    if (strategy.isDelegateRegistered(clazz))
        return DELEGATE;

    final RuntimeFieldFactory<?> inline = __inlineValues.get(clazz
            .getName());
    if (inline != null)
        return inline;

    if (Message.class.isAssignableFrom(clazz))
        return POJO;

    if (clazz.isEnum())
        return ENUM;

    // Of all the scalar (inline) fields, java.lang.Number is the only
    // abstract
    // super type, hence we can filter it here
    // Note that it has 10 built-in subtypes
    if (clazz.isArray() || Object.class == clazz || Number.class == clazz
            || Class.class == clazz || Enum.class == clazz)
    {
        return OBJECT;
    }
    
    // allow user to register a custom schema for throwable/map/collection
    
    if (strategy.isRegistered(clazz))
        return clazz.isInterface() ? POJO : POLYMORPHIC_POJO;
    
    if (Throwable.class.isAssignableFrom(clazz))
        return OBJECT;
    
    if (Map.class.isAssignableFrom(clazz))
        return RuntimeMapFieldFactory.MAP;

    if (Collection.class.isAssignableFrom(clazz))
    {
        // repeated fields.
        return COLLECTION;
    }

    // Enums or boxed types of primitives do implement interfaces.
    // Although serializing polymorphic pojos declared as interfaces will be
    // a
    // little bit slower than before, this is more correct.
    //
    // In versions prior to 1.0.5, it would serialize a field declared as
    // java.lang.Serializable like a polymorphic pojo even though it
    // gets assigned enum/string/number types.
    //
    // If you have declared fields as serializable, it wont be compatible
    if (clazz.isInterface())
        return OBJECT;

    // checks delegated to POLYMORPHIC_POJO
    return POLYMORPHIC_POJO;
}
 
Example #27
Source File: RuntimeRepeatedFieldFactory.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> Field<T> create(int number, String name,
        java.lang.reflect.Field f, IdStrategy strategy)
{
    final Class<?> clazz = f.getType();
    final Morph morph = f.getAnnotation(Morph.class);
    
    if (0 != (IdStrategy.POJO_SCHEMA_ON_COLLECTION_FIELDS & strategy.flags) && 
            (morph == null || morph.value()))
    {
        if (!clazz.getName().startsWith("java.util") && 
                pojo(clazz, morph, strategy))
        {
            return POJO.create(number, name, f, strategy);
        }
        
        return OBJECT.create(number, name, f, strategy);
    }
    
    if (morph != null)
    {
        // can be used to override the configured system property:
        // RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS

        // In this context, Morph annotation will force using a
        // collection
        // schema only for this particular field.
        return RuntimeCollectionFieldFactory.getFactory().create(
                number, name, f, strategy);
    }

    if (EnumSet.class.isAssignableFrom(clazz))
    {
        final Class<Object> enumType = (Class<Object>) getGenericType(
                f, 0);
        if (enumType == null)
        {
            // still handle the serialization of EnumSets even without
            // generics
            return RuntimeFieldFactory.OBJECT.create(number, name, f,
                    strategy);
        }

        return createCollectionEnumV(number, name, f, strategy
                .getEnumIO(enumType).getEnumSetFactory(), enumType,
                strategy);
    }

    final MessageFactory messageFactory = strategy
            .getCollectionFactory(clazz);

    final Class<Object> genericType = (Class<Object>) getGenericType(f,
            0);
    if (genericType == null || ((Map.class.isAssignableFrom(genericType) || 
            Collection.class.isAssignableFrom(genericType)) && 
            !strategy.isRegistered(genericType)))
    {
        // the value is not a simple parameterized type.
        return createCollectionObjectV(number, name, f, messageFactory,
                genericType, PolymorphicSchemaFactories.OBJECT,
                strategy);
    }

    final Delegate<Object> inline = getDelegateOrInline(genericType,
            strategy);
    if (inline != null)
        return createCollectionInlineV(number, name, f, messageFactory,
                inline);

    if (Message.class.isAssignableFrom(genericType))
        return createCollectionPojoV(number, name, f, messageFactory,
                genericType, strategy);

    if (genericType.isEnum())
        return createCollectionEnumV(number, name, f, messageFactory,
                genericType, strategy);

    final PolymorphicSchema.Factory factory = PolymorphicSchemaFactories
            .getFactoryFromRepeatedValueGenericType(genericType);
    if (factory != null)
    {
        return createCollectionObjectV(number, name, f, messageFactory,
                genericType, factory, strategy);
    }

    if (pojo(genericType, morph, strategy))
        return createCollectionPojoV(number, name, f, messageFactory,
                genericType, strategy);

    if (genericType.isInterface())
    {
        return createCollectionObjectV(number, name, f, messageFactory,
                genericType, PolymorphicSchemaFactories.OBJECT,
                strategy);
    }

    return createCollectionPolymorphicV(number, name, f,
            messageFactory, genericType, strategy);
}
 
Example #28
Source File: IdStrategy.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
protected abstract <T> Schema<T> writeMessageIdTo(Output output,
int fieldNumber, Message<T> message) throws IOException;
 
Example #29
Source File: ReplicatorElectionTest.java    From c5-replicator with Apache License 2.0 4 votes vote down vote up
public void receiveReply(Message message) {
  RpcRequest requestMessage = request.getRequest();
  request.reply(
      new RpcWireReply(requestMessage.from, requestMessage.to, QUORUM_ID, message));
}
 
Example #30
Source File: RpcWireReply.java    From c5-replicator with Apache License 2.0 4 votes vote down vote up
public RpcWireReply(long to, long from, String quorumId, Message message) {
  super(to, from, quorumId, message);
}