Search in sources :

Example 1 with Persistent

use of org.apache.gora.persistency.Persistent in project gora by apache.

the class CassandraStore method getUnionSchema.

/**
   * Given an object and the object schema this function obtains,
   * from within the UNION schema, the position of the type used.
   * If no data type can be inferred then we return a default value
   * of position 0.
   * @param pValue
   * @param pUnionSchema
   * @return the unionSchemaPosition.
   */
private int getUnionSchema(Object pValue, Schema pUnionSchema) {
    int unionSchemaPos = 0;
    //    String valueType = pValue.getClass().getSimpleName();
    for (Schema currentSchema : pUnionSchema.getTypes()) {
        Type schemaType = currentSchema.getType();
        if (pValue instanceof CharSequence && schemaType.equals(Type.STRING))
            return unionSchemaPos;
        else if (pValue instanceof ByteBuffer && schemaType.equals(Type.BYTES))
            return unionSchemaPos;
        else if (pValue instanceof Integer && schemaType.equals(Type.INT))
            return unionSchemaPos;
        else if (pValue instanceof Long && schemaType.equals(Type.LONG))
            return unionSchemaPos;
        else if (pValue instanceof Double && schemaType.equals(Type.DOUBLE))
            return unionSchemaPos;
        else if (pValue instanceof Float && schemaType.equals(Type.FLOAT))
            return unionSchemaPos;
        else if (pValue instanceof Boolean && schemaType.equals(Type.BOOLEAN))
            return unionSchemaPos;
        else if (pValue instanceof Map && schemaType.equals(Type.MAP))
            return unionSchemaPos;
        else if (pValue instanceof List && schemaType.equals(Type.ARRAY))
            return unionSchemaPos;
        else if (pValue instanceof Persistent && schemaType.equals(Type.RECORD))
            return unionSchemaPos;
        unionSchemaPos++;
    }
    // if we weren't able to determine which data type it is, then we return the default
    return DEFAULT_UNION_SCHEMA;
}
Also used : Schema(org.apache.avro.Schema) Persistent(org.apache.gora.persistency.Persistent) ByteBuffer(java.nio.ByteBuffer) Type(org.apache.avro.Schema.Type) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with Persistent

use of org.apache.gora.persistency.Persistent in project gora by apache.

the class GoraSerializerTypeInferer method getSerializer.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Serializer<T> getSerializer(Object value) {
    Serializer serializer = null;
    if (value == null) {
        serializer = ByteBufferSerializer.get();
    } else if (value instanceof CharSequence) {
        serializer = CharSequenceSerializer.get();
    } else if (value instanceof Utf8) {
        serializer = CharSequenceSerializer.get();
    } else if (value instanceof Boolean) {
        serializer = BooleanSerializer.get();
    } else if (value instanceof ByteBuffer) {
        serializer = ByteBufferSerializer.get();
    } else if (value instanceof byte[]) {
        serializer = BytesArraySerializer.get();
    } else if (value instanceof Double) {
        serializer = DoubleSerializer.get();
    } else if (value instanceof Float) {
        serializer = FloatSerializer.get();
    } else if (value instanceof Integer) {
        serializer = IntegerSerializer.get();
    } else if (value instanceof Long) {
        serializer = LongSerializer.get();
    } else if (value instanceof String) {
        serializer = StringSerializer.get();
    } else if (value instanceof SpecificFixed) {
        serializer = SpecificFixedSerializer.get(value.getClass());
    } else if (value instanceof GenericArray) {
        Schema schema = ((GenericArray) value).getSchema();
        if (schema.getType() == Type.ARRAY) {
            schema = schema.getElementType();
        }
        serializer = ListSerializer.get(schema);
    } else if (value instanceof Map) {
        Map map = (Map) value;
        if (map.size() == 0) {
            serializer = ByteBufferSerializer.get();
        } else {
            Object value0 = map.values().iterator().next();
            Schema schema = TypeUtils.getSchema(value0);
            serializer = MapSerializer.get(schema);
        }
    } else if (value instanceof Persistent) {
        serializer = ObjectSerializer.get();
    } else {
        serializer = SerializerTypeInferer.getSerializer(value);
    }
    return serializer;
}
Also used : Schema(org.apache.avro.Schema) SpecificFixed(org.apache.avro.specific.SpecificFixed) Persistent(org.apache.gora.persistency.Persistent) ByteBuffer(java.nio.ByteBuffer) Utf8(org.apache.avro.util.Utf8) GenericArray(org.apache.avro.generic.GenericArray) Map(java.util.Map) Serializer(me.prettyprint.hector.api.Serializer) DoubleSerializer(me.prettyprint.cassandra.serializers.DoubleSerializer) BytesArraySerializer(me.prettyprint.cassandra.serializers.BytesArraySerializer) ByteBufferSerializer(me.prettyprint.cassandra.serializers.ByteBufferSerializer) FloatSerializer(me.prettyprint.cassandra.serializers.FloatSerializer) ObjectSerializer(me.prettyprint.cassandra.serializers.ObjectSerializer) LongSerializer(me.prettyprint.cassandra.serializers.LongSerializer) StringSerializer(me.prettyprint.cassandra.serializers.StringSerializer) BooleanSerializer(me.prettyprint.cassandra.serializers.BooleanSerializer) IntegerSerializer(me.prettyprint.cassandra.serializers.IntegerSerializer)

Example 3 with Persistent

use of org.apache.gora.persistency.Persistent in project gora by apache.

the class SolrStore method getUnionSchema.

/**
   * Given an object and the object schema this function obtains, from within
   * the UNION schema, the position of the type used. If no data type can be
   * inferred then we return a default value of position 0.
   * 
   * @param pValue
   * @param pUnionSchema
   * @return the unionSchemaPosition.
   */
private int getUnionSchema(Object pValue, Schema pUnionSchema) {
    int unionSchemaPos = 0;
    for (Schema currentSchema : pUnionSchema.getTypes()) {
        Type schemaType = currentSchema.getType();
        if (pValue instanceof Utf8 && schemaType.equals(Type.STRING))
            return unionSchemaPos;
        else if (pValue instanceof ByteBuffer && schemaType.equals(Type.BYTES))
            return unionSchemaPos;
        else if (pValue instanceof Integer && schemaType.equals(Type.INT))
            return unionSchemaPos;
        else if (pValue instanceof Long && schemaType.equals(Type.LONG))
            return unionSchemaPos;
        else if (pValue instanceof Double && schemaType.equals(Type.DOUBLE))
            return unionSchemaPos;
        else if (pValue instanceof Float && schemaType.equals(Type.FLOAT))
            return unionSchemaPos;
        else if (pValue instanceof Boolean && schemaType.equals(Type.BOOLEAN))
            return unionSchemaPos;
        else if (pValue instanceof Map && schemaType.equals(Type.MAP))
            return unionSchemaPos;
        else if (pValue instanceof List && schemaType.equals(Type.ARRAY))
            return unionSchemaPos;
        else if (pValue instanceof Persistent && schemaType.equals(Type.RECORD))
            return unionSchemaPos;
        unionSchemaPos++;
    }
    // default
    return DEFAULT_UNION_SCHEMA;
}
Also used : Schema(org.apache.avro.Schema) Persistent(org.apache.gora.persistency.Persistent) ByteBuffer(java.nio.ByteBuffer) Type(org.apache.avro.Schema.Type) Utf8(org.apache.avro.util.Utf8) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 4 with Persistent

use of org.apache.gora.persistency.Persistent in project camel by apache.

the class GoraProducerTest method shouldInvokeDastorePut.

@Test
public void shouldInvokeDastorePut() throws Exception {
    when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage);
    when(mockCamelMessage.getHeader(GoraAttribute.GORA_OPERATION.value)).thenReturn("PUT");
    final Long sampleKey = new Long(2);
    when(mockCamelMessage.getHeader(GoraAttribute.GORA_KEY.value)).thenReturn(sampleKey);
    final Persistent sampleValue = mock(Persistent.class);
    when(mockCamelMessage.getBody(Persistent.class)).thenReturn(sampleValue);
    final Message outMessage = mock(Message.class);
    when(mockCamelExchange.getOut()).thenReturn(outMessage);
    final GoraProducer producer = new GoraProducer(mockGoraEndpoint, mockGoraConfiguration, mockDatastore);
    producer.process(mockCamelExchange);
    verify(mockCamelExchange, atLeastOnce()).getIn();
    verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_OPERATION.value);
    verify(mockCamelMessage, atLeastOnce()).getHeader(GoraAttribute.GORA_KEY.value);
    verify(mockCamelMessage, atLeastOnce()).getBody(Persistent.class);
    verify(mockDatastore, atMost(1)).put(sampleKey, sampleValue);
}
Also used : Message(org.apache.camel.Message) Persistent(org.apache.gora.persistency.Persistent) Test(org.junit.Test)

Aggregations

Persistent (org.apache.gora.persistency.Persistent)4 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 Schema (org.apache.avro.Schema)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Type (org.apache.avro.Schema.Type)2 Utf8 (org.apache.avro.util.Utf8)2 LinkedHashMap (java.util.LinkedHashMap)1 BooleanSerializer (me.prettyprint.cassandra.serializers.BooleanSerializer)1 ByteBufferSerializer (me.prettyprint.cassandra.serializers.ByteBufferSerializer)1 BytesArraySerializer (me.prettyprint.cassandra.serializers.BytesArraySerializer)1 DoubleSerializer (me.prettyprint.cassandra.serializers.DoubleSerializer)1 FloatSerializer (me.prettyprint.cassandra.serializers.FloatSerializer)1 IntegerSerializer (me.prettyprint.cassandra.serializers.IntegerSerializer)1 LongSerializer (me.prettyprint.cassandra.serializers.LongSerializer)1 ObjectSerializer (me.prettyprint.cassandra.serializers.ObjectSerializer)1 StringSerializer (me.prettyprint.cassandra.serializers.StringSerializer)1