Search in sources :

Example 1 with WrappedValue

use of com.google.appengine.api.datastore.Entity.WrappedValue in project appengine-java-standard by GoogleCloudPlatform.

the class DataTypeTranslator method toV1Value.

private static Value.Builder toV1Value(@Nullable Object value) {
    boolean indexed = true;
    boolean forceIndexedEmbeddedEntity = false;
    if (value instanceof WrappedValue) {
        WrappedValue wrappedValue = (WrappedValue) value;
        indexed = wrappedValue.isIndexed();
        forceIndexedEmbeddedEntity = wrappedValue.getForceIndexedEmbeddedEntity();
        value = wrappedValue.getValue();
    }
    if (value instanceof Collection<?>) {
        Collection<?> values = (Collection<?>) value;
        if (values.isEmpty()) {
            if (DatastoreServiceConfig.getEmptyListSupport()) {
                return Value.newBuilder().setExcludeFromIndexes(!indexed).setArrayValue(ArrayValue.getDefaultInstance());
            } else {
                // null and empty list is lost.
                return toV1Value(null, indexed, forceIndexedEmbeddedEntity);
            }
        } else {
            Value.Builder valueBuilder = Value.newBuilder();
            for (Object listValue : values) {
                valueBuilder.getArrayValueBuilder().addValues(toV1Value(listValue, indexed, forceIndexedEmbeddedEntity));
            }
            return valueBuilder;
        }
    } else {
        return toV1Value(value, indexed, forceIndexedEmbeddedEntity);
    }
}
Also used : UnindexedValue(com.google.appengine.api.datastore.Entity.UnindexedValue) UserValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.UserValue) PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue) ReferenceValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.ReferenceValue) NullValue(com.google.protobuf.NullValue) ArrayValue(com.google.datastore.v1.ArrayValue) Value(com.google.datastore.v1.Value) WrappedValue(com.google.appengine.api.datastore.Entity.WrappedValue) Collection(java.util.Collection) WrappedValue(com.google.appengine.api.datastore.Entity.WrappedValue)

Example 2 with WrappedValue

use of com.google.appengine.api.datastore.Entity.WrappedValue in project appengine-java-standard by GoogleCloudPlatform.

the class DataTypeTranslator method addPropertiesToPb.

/**
 * Add all of the properties in the specified map to an {@code EntityProto}. This involves
 * determining the type of each property and creating the proper type-specific protocol buffer.
 *
 * <p>If the property value is an {@link UnindexedValue}, or if it's a type that is never indexed,
 * e.g. {@code Text} and {@code Blob}, it's added to {@code EntityProto.raw_property}. Otherwise
 * it's added to {@code EntityProto.property}.
 *
 * @param map A not {@code null} map of all the properties which will be set on {@code proto}
 * @param proto A not {@code null} protocol buffer
 */
public static void addPropertiesToPb(Map<String, ?> map, EntityProto proto) {
    for (Map.Entry<String, ?> entry : map.entrySet()) {
        String name = entry.getKey();
        boolean forceIndexedEmbeddedEntity = false;
        boolean indexed = true;
        Object value = entry.getValue();
        if (entry.getValue() instanceof WrappedValue) {
            WrappedValue wrappedValue = (WrappedValue) entry.getValue();
            forceIndexedEmbeddedEntity = wrappedValue.getForceIndexedEmbeddedEntity();
            indexed = wrappedValue.isIndexed();
            value = wrappedValue.getValue();
        }
        if (value instanceof Collection<?>) {
            Collection<?> values = (Collection<?>) value;
            addListPropertyToPb(proto, name, indexed, values, forceIndexedEmbeddedEntity);
        } else {
            addPropertyToPb(name, value, indexed, forceIndexedEmbeddedEntity, false, proto);
        }
    }
}
Also used : Collection(java.util.Collection) ByteString(com.google.protobuf.ByteString) HashMap(java.util.HashMap) Map(java.util.Map) WrappedValue(com.google.appengine.api.datastore.Entity.WrappedValue)

Example 3 with WrappedValue

use of com.google.appengine.api.datastore.Entity.WrappedValue in project appengine-java-standard by GoogleCloudPlatform.

the class PropertyContainer method setPropertiesFrom.

/**
 * A convenience method that populates properties from those in the given container.
 *
 * <p>This method transfers information about unindexed properties and clones any mutable values.
 *
 * @param src The container from which we will populate ourself.
 */
public void setPropertiesFrom(PropertyContainer src) {
    for (Map.Entry<String, @Nullable Object> entry : src.getPropertyMap().entrySet()) {
        String name = entry.getKey();
        Object entryValue = entry.getValue();
        boolean isUnindexedValue = false;
        boolean isWrappedValue = false;
        boolean indexed = true;
        boolean forceIndexedEntityValue = false;
        Object valueToAdd = entryValue;
        if (entryValue instanceof UnindexedValue) {
            isUnindexedValue = true;
            indexed = false;
            valueToAdd = ((UnindexedValue) entryValue).getValue();
        } else if (entryValue instanceof WrappedValue) {
            WrappedValue wrappedValue = (WrappedValue) entryValue;
            isWrappedValue = true;
            indexed = wrappedValue.isIndexed();
            forceIndexedEntityValue = wrappedValue.getForceIndexedEmbeddedEntity();
            valueToAdd = wrappedValue.getValue();
        }
        // Clone collections
        if (valueToAdd instanceof Collection<?>) {
            // Convert all collections to an ArrayList, as that is the representation
            // returned from the datastore.
            Collection<?> srcColl = (Collection<?>) valueToAdd;
            Collection<Object> destColl = new ArrayList<Object>(srcColl.size());
            valueToAdd = destColl;
            for (Object element : srcColl) {
                destColl.add(cloneIfMutable(element));
            }
        } else {
            valueToAdd = cloneIfMutable(valueToAdd);
        }
        if (isUnindexedValue) {
            valueToAdd = new UnindexedValue(valueToAdd);
        } else if (isWrappedValue) {
            valueToAdd = new WrappedValueImpl(valueToAdd, indexed, forceIndexedEntityValue);
        }
        // Put the copy in the map
        getPropertyMap().put(name, valueToAdd);
    }
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) UnindexedValue(com.google.appengine.api.datastore.Entity.UnindexedValue) WrappedValueImpl(com.google.appengine.api.datastore.Entity.WrappedValueImpl) HashMap(java.util.HashMap) Map(java.util.Map) WrappedValue(com.google.appengine.api.datastore.Entity.WrappedValue)

Aggregations

WrappedValue (com.google.appengine.api.datastore.Entity.WrappedValue)3 Collection (java.util.Collection)3 UnindexedValue (com.google.appengine.api.datastore.Entity.UnindexedValue)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 WrappedValueImpl (com.google.appengine.api.datastore.Entity.WrappedValueImpl)1 ArrayValue (com.google.datastore.v1.ArrayValue)1 Value (com.google.datastore.v1.Value)1 ByteString (com.google.protobuf.ByteString)1 NullValue (com.google.protobuf.NullValue)1 PropertyValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)1 ReferenceValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.ReferenceValue)1 UserValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.UserValue)1 ArrayList (java.util.ArrayList)1